1 /*
2  *  Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
3  *
4  *  Permission is hereby granted, free of charge, to any person obtaining a
5  *  copy of this software and associated documentation files (the "Software"),
6  *  to deal in the Software without restriction, including without limitation
7  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  *  and/or sell copies of the Software, and to permit persons to whom the
9  *  Software is furnished to do so, subject to the following conditions:
10  *
11  *  The above copyright notice and this permission notice shall be included in
12  *  all copies or substantial portions of the Software.
13  *
14  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  *  DEALINGS IN THE SOFTWARE.
21  */
22 
23 
24 /*
25  *  File name:    utf8proc.h
26  *
27  *  Description:
28  *  Header files for libutf8proc, which is a mapping tool for UTF-8 strings
29  *  with following features:
30  *  - decomposing and composing of strings
31  *  - replacing compatibility characters with their equivalents
32  *  - stripping of "default ignorable characters"
33  *    like SOFT-HYPHEN or ZERO-WIDTH-SPACE
34  *  - folding of certain characters for string comparison
35  *    (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-")
36  *    (see "LUMP" option)
37  *  - optional rejection of strings containing non-assigned code points
38  *  - stripping of control characters
39  *  - stripping of character marks (accents, etc.)
40  *  - transformation of LF, CRLF, CR and NEL to line-feed (LF)
41  *    or to the unicode chararacters for paragraph separation (PS)
42  *    or line separation (LS).
43  *  - unicode case folding (for case insensitive string comparisons)
44  *  - rejection of illegal UTF-8 data
45  *    (i.e. UTF-8 encoded UTF-16 surrogates)
46  *  - support for korean hangul characters
47  *  Unicode Version 5.0.0 is supported.
48  */
49 
50 
51 #ifndef UTF8PROC_H
52 #define UTF8PROC_H
53 
54 
55 #include <stdlib.h>
56 #include <sys/types.h>
57 #ifdef _MSC_VER
58 typedef signed char int8_t;
59 typedef unsigned char uint8_t;
60 typedef short int16_t;
61 typedef unsigned short uint16_t;
62 typedef int int32_t;
63 #ifdef _WIN64
64 #define ssize_t __int64
65 #else
66 #define ssize_t int
67 #endif
68 typedef unsigned char bool;
69 enum {false, true};
70 #else
71 #include <stdbool.h>
72 #include <inttypes.h>
73 #endif
74 #include <limits.h>
75 
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 
80 #ifndef SSIZE_MAX
81 #define SSIZE_MAX ((size_t)SIZE_MAX/2)
82 #endif
83 
84 #define UTF8PROC_NULLTERM  (1<<0)
85 #define UTF8PROC_STABLE    (1<<1)
86 #define UTF8PROC_COMPAT    (1<<2)
87 #define UTF8PROC_COMPOSE   (1<<3)
88 #define UTF8PROC_DECOMPOSE (1<<4)
89 #define UTF8PROC_IGNORE    (1<<5)
90 #define UTF8PROC_REJECTNA  (1<<6)
91 #define UTF8PROC_NLF2LS    (1<<7)
92 #define UTF8PROC_NLF2PS    (1<<8)
93 #define UTF8PROC_NLF2LF    (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS)
94 #define UTF8PROC_STRIPCC   (1<<9)
95 #define UTF8PROC_CASEFOLD  (1<<10)
96 #define UTF8PROC_CHARBOUND (1<<11)
97 #define UTF8PROC_LUMP      (1<<12)
98 #define UTF8PROC_STRIPMARK (1<<13)
99 /*
100  *  Flags being regarded by several functions in the library:
101  *  NULLTERM:  The given UTF-8 input is NULL terminated.
102  *  STABLE:    Unicode Versioning Stability has to be respected.
103  *  COMPAT:    Compatiblity decomposition
104  *             (i.e. formatting information is lost)
105  *  COMPOSE:   Return a result with composed characters.
106  *  DECOMPOSE: Return a result with decomposed characters.
107  *  IGNORE:    Strip "default ignorable characters"
108  *  REJECTNA:  Return an error, if the input contains unassigned
109  *             code points.
110  *  NLF2LS:    Indicating that NLF-sequences (LF, CRLF, CR, NEL) are
111  *             representing a line break, and should be converted to the
112  *             unicode character for line separation (LS).
113  *  NLF2PS:    Indicating that NLF-sequences are representing a paragraph
114  *             break, and should be converted to the unicode character for
115  *             paragraph separation (PS).
116  *  NLF2LF:    Indicating that the meaning of NLF-sequences is unknown.
117  *  STRIPCC:   Strips and/or convers control characters.
118  *             NLF-sequences are transformed into space, except if one of
119  *             the NLF2LS/PS/LF options is given.
120  *             HorizontalTab (HT) and FormFeed (FF) are treated as a
121  *             NLF-sequence in this case.
122  *             All other control characters are simply removed.
123  *  CASEFOLD:  Performs unicode case folding, to be able to do a
124  *             case-insensitive string comparison.
125  *  CHARBOUND: Inserts 0xFF bytes at the beginning of each sequence which
126  *             is representing a single grapheme cluster (see UAX#29).
127  *  LUMP:      Lumps certain characters together
128  *             (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-").
129  *             (See lump.txt for details.)
130  *             If NLF2LF is set, this includes a transformation of
131  *             paragraph and line separators to ASCII line-feed (LF).
132  *  STRIPMARK: Strips all character markings
133  *             (non-spacing, spacing and enclosing) (i.e. accents)
134  *             NOTE: this option works only with COMPOSE or DECOMPOSE
135  */
136 
137 #define UTF8PROC_ERROR_NOMEM -1
138 #define UTF8PROC_ERROR_OVERFLOW -2
139 #define UTF8PROC_ERROR_INVALIDUTF8 -3
140 #define UTF8PROC_ERROR_NOTASSIGNED -4
141 #define UTF8PROC_ERROR_INVALIDOPTS -5
142 /*
143  *  Error codes being returned by almost all functions:
144  *  ERROR_NOMEM:       Memory could not be allocated.
145  *  ERROR_OVERFLOW:    The given string is too long to be processed.
146  *  ERROR_INVALIDUTF8: The given string is not a legal UTF-8 string.
147  *  ERROR_NOTASSIGNED: The REJECTNA flag was set,
148  *                     and an unassigned code point was found.
149  *  ERROR_INVALIDOPTS: Invalid options have been used.
150  */
151 
152 typedef int16_t utf8proc_propval_t;
153 typedef struct utf8proc_property_struct {
154   utf8proc_propval_t category;
155   utf8proc_propval_t combining_class;
156   utf8proc_propval_t bidi_class;
157   utf8proc_propval_t decomp_type;
158   const int32_t *decomp_mapping;
159   unsigned bidi_mirrored:1;
160   int32_t uppercase_mapping;
161   int32_t lowercase_mapping;
162   int32_t titlecase_mapping;
163   int32_t comb1st_index;
164   int32_t comb2nd_index;
165   unsigned comp_exclusion:1;
166   unsigned ignorable:1;
167   unsigned control_boundary:1;
168   unsigned extend:1;
169   const int32_t *casefold_mapping;
170 } utf8proc_property_t;
171 
172 #define UTF8PROC_CATEGORY_LU  1
173 #define UTF8PROC_CATEGORY_LL  2
174 #define UTF8PROC_CATEGORY_LT  3
175 #define UTF8PROC_CATEGORY_LM  4
176 #define UTF8PROC_CATEGORY_LO  5
177 #define UTF8PROC_CATEGORY_MN  6
178 #define UTF8PROC_CATEGORY_MC  7
179 #define UTF8PROC_CATEGORY_ME  8
180 #define UTF8PROC_CATEGORY_ND  9
181 #define UTF8PROC_CATEGORY_NL 10
182 #define UTF8PROC_CATEGORY_NO 11
183 #define UTF8PROC_CATEGORY_PC 12
184 #define UTF8PROC_CATEGORY_PD 13
185 #define UTF8PROC_CATEGORY_PS 14
186 #define UTF8PROC_CATEGORY_PE 15
187 #define UTF8PROC_CATEGORY_PI 16
188 #define UTF8PROC_CATEGORY_PF 17
189 #define UTF8PROC_CATEGORY_PO 18
190 #define UTF8PROC_CATEGORY_SM 19
191 #define UTF8PROC_CATEGORY_SC 20
192 #define UTF8PROC_CATEGORY_SK 21
193 #define UTF8PROC_CATEGORY_SO 22
194 #define UTF8PROC_CATEGORY_ZS 23
195 #define UTF8PROC_CATEGORY_ZL 24
196 #define UTF8PROC_CATEGORY_ZP 25
197 #define UTF8PROC_CATEGORY_CC 26
198 #define UTF8PROC_CATEGORY_CF 27
199 #define UTF8PROC_CATEGORY_CS 28
200 #define UTF8PROC_CATEGORY_CO 29
201 #define UTF8PROC_CATEGORY_CN 30
202 #define UTF8PROC_BIDI_CLASS_L    1
203 #define UTF8PROC_BIDI_CLASS_LRE  2
204 #define UTF8PROC_BIDI_CLASS_LRO  3
205 #define UTF8PROC_BIDI_CLASS_R    4
206 #define UTF8PROC_BIDI_CLASS_AL   5
207 #define UTF8PROC_BIDI_CLASS_RLE  6
208 #define UTF8PROC_BIDI_CLASS_RLO  7
209 #define UTF8PROC_BIDI_CLASS_PDF  8
210 #define UTF8PROC_BIDI_CLASS_EN   9
211 #define UTF8PROC_BIDI_CLASS_ES  10
212 #define UTF8PROC_BIDI_CLASS_ET  11
213 #define UTF8PROC_BIDI_CLASS_AN  12
214 #define UTF8PROC_BIDI_CLASS_CS  13
215 #define UTF8PROC_BIDI_CLASS_NSM 14
216 #define UTF8PROC_BIDI_CLASS_BN  15
217 #define UTF8PROC_BIDI_CLASS_B   16
218 #define UTF8PROC_BIDI_CLASS_S   17
219 #define UTF8PROC_BIDI_CLASS_WS  18
220 #define UTF8PROC_BIDI_CLASS_ON  19
221 #define UTF8PROC_DECOMP_TYPE_FONT      1
222 #define UTF8PROC_DECOMP_TYPE_NOBREAK   2
223 #define UTF8PROC_DECOMP_TYPE_INITIAL   3
224 #define UTF8PROC_DECOMP_TYPE_MEDIAL    4
225 #define UTF8PROC_DECOMP_TYPE_FINAL     5
226 #define UTF8PROC_DECOMP_TYPE_ISOLATED  6
227 #define UTF8PROC_DECOMP_TYPE_CIRCLE    7
228 #define UTF8PROC_DECOMP_TYPE_SUPER     8
229 #define UTF8PROC_DECOMP_TYPE_SUB       9
230 #define UTF8PROC_DECOMP_TYPE_VERTICAL 10
231 #define UTF8PROC_DECOMP_TYPE_WIDE     11
232 #define UTF8PROC_DECOMP_TYPE_NARROW   12
233 #define UTF8PROC_DECOMP_TYPE_SMALL    13
234 #define UTF8PROC_DECOMP_TYPE_SQUARE   14
235 #define UTF8PROC_DECOMP_TYPE_FRACTION 15
236 #define UTF8PROC_DECOMP_TYPE_COMPAT   16
237 
238 extern const int8_t utf8proc_utf8class[256];
239 
240 const char *utf8proc_version(void);
241 
242 const char *utf8proc_errmsg(ssize_t errcode);
243 /*
244  *  Returns a static error string for the given error code.
245  */
246 
247 ssize_t utf8proc_iterate(const uint8_t *str, ssize_t strlen, int32_t *dst);
248 /*
249  *  Reads a single char from the UTF-8 sequence being pointed to by 'str'.
250  *  The maximum number of bytes read is 'strlen', unless 'strlen' is
251  *  negative.
252  *  If a valid unicode char could be read, it is stored in the variable
253  *  being pointed to by 'dst', otherwise that variable will be set to -1.
254  *  In case of success the number of bytes read is returned, otherwise a
255  *  negative error code is returned.
256  */
257 
258 bool utf8proc_codepoint_valid(int32_t uc);
259 /*
260  *  Returns 1, if the given unicode code-point is valid, otherwise 0.
261  */
262 
263 ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst);
264 /*
265  *  Encodes the unicode char with the code point 'uc' as an UTF-8 string in
266  *  the byte array being pointed to by 'dst'. This array has to be at least
267  *  4 bytes long.
268  *  In case of success the number of bytes written is returned,
269  *  otherwise 0.
270  *  This function does not check if 'uc' is a valid unicode code point.
271  */
272 
273 const utf8proc_property_t *utf8proc_get_property(int32_t uc);
274 /*
275  *  Returns a pointer to a (constant) struct containing information about
276  *  the unicode char with the given code point 'uc'.
277  *  If the character is not existent a pointer to a special struct is
278  *  returned, where 'category' is a NULL pointer.
279  *  WARNING: The parameter 'uc' has to be in the range of 0x0000 to
280  *           0x10FFFF, otherwise the program might crash!
281  */
282 
283 ssize_t utf8proc_decompose_char(
284   int32_t uc, int32_t *dst, ssize_t bufsize,
285   int options, int *last_boundclass
286 );
287 /*
288  *  Writes a decomposition of the unicode char 'uc' into the array being
289  *  pointed to by 'dst'.
290  *  Following flags in the 'options' field are regarded:
291  *  REJECTNA:  an unassigned unicode code point leads to an error
292  *  IGNORE:    "default ignorable" chars are stripped
293  *  CASEFOLD:  unicode casefolding is applied
294  *  COMPAT:    replace certain characters with their
295  *             compatibility decomposition
296  *  CHARBOUND: Inserts 0xFF bytes before each grapheme cluster
297  *  LUMP:      lumps certain different characters together
298  *  STRIPMARK: removes all character marks
299  *  The pointer 'last_boundclass' has to point to an integer variable which
300  *  is storing the last character boundary class, if the CHARBOUND option
301  *  is used.
302  *  In case of success the number of chars written is returned,
303  *  in case of an error, a negative error code is returned.
304  *  If the number of written chars would be bigger than 'bufsize',
305  *  the buffer (up to 'bufsize') has inpredictable data, and the needed
306  *  buffer size is returned.
307  *  WARNING: The parameter 'uc' has to be in the range of 0x0000 to
308  *           0x10FFFF, otherwise the program might crash!
309  */
310 
311 ssize_t utf8proc_decompose(
312   const uint8_t *str, ssize_t strlen,
313   int32_t *buffer, ssize_t bufsize, int options
314 );
315 /*
316  *  Does the same as 'utf8proc_decompose_char', but acts on a whole UTF-8
317  *  string, and orders the decomposed sequences correctly.
318  *  If the NULLTERM flag in 'options' is set, processing will be stopped,
319  *  when a NULL byte is encounted, otherwise 'strlen' bytes are processed.
320  *  The result in form of unicode code points is written into the buffer
321  *  being pointed to by 'buffer', having the length of 'bufsize' entries.
322  *  In case of success the number of chars written is returned,
323  *  in case of an error, a negative error code is returned.
324  *  If the number of written chars would be bigger than 'bufsize',
325  *  the buffer (up to 'bufsize') has inpredictable data, and the needed
326  *  buffer size is returned.
327  */
328 
329 ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options);
330 /*
331  *  Reencodes the sequence of unicode characters given by the pointer
332  *  'buffer' and 'length' as UTF-8.
333  *  The result is stored in the same memory area where the data is read.
334  *  Following flags in the 'options' field are regarded:
335  *  NLF2LS:  converts LF, CRLF, CR and NEL into LS
336  *  NLF2PS:  converts LF, CRLF, CR and NEL into PS
337  *  NLF2LF:  converts LF, CRLF, CR and NEL into LF
338  *  STRIPCC: strips or converts all non-affected control characters
339  *  COMPOSE: tries to combine decomposed characters into composite
340  *           characters
341  *  STABLE:  prohibits combining characters which would violate
342  *           the unicode versioning stability
343  *  In case of success the length of the resulting UTF-8 string is
344  *  returned, otherwise a negative error code is returned.
345  *  WARNING: The amount of free space being pointed to by 'buffer', has to
346  *           exceed the amount of the input data by one byte, and the
347  *           entries of the array pointed to by 'str' have to be in the
348  *           range of 0x0000 to 0x10FFFF, otherwise the program might
349  *           crash!
350  */
351 
352 ssize_t utf8proc_map(
353   const uint8_t *str, ssize_t strlen, uint8_t **dstptr, int options
354 );
355 /*
356  *  Maps the given UTF-8 string being pointed to by 'str' to a new UTF-8
357  *  string, which is allocated dynamically, and afterwards pointed to by
358  *  the pointer being pointed to by 'dstptr'.
359  *  If the NULLTERM flag in the 'options' field is set, the length is
360  *  determined by a NULL terminator, otherwise the parameter 'strlen' is
361  *  evaluated to determine the string length, but in any case the result
362  *  will be NULL terminated (though it might contain NULL characters
363  *  before). Other flags in the 'options' field are passed to the functions
364  *  defined above, and regarded as described.
365  *  In case of success the length of the new string is returned,
366  *  otherwise a negative error code is returned.
367  *  NOTICE: The memory of the new UTF-8 string will have been allocated with
368  *          'malloc', and has theirfore to be freed with 'free'.
369  */
370 
371 uint8_t *utf8proc_NFD(const uint8_t *str);
372 uint8_t *utf8proc_NFC(const uint8_t *str);
373 uint8_t *utf8proc_NFKD(const uint8_t *str);
374 uint8_t *utf8proc_NFKC(const uint8_t *str);
375 /*
376  *  Returns a pointer to newly allocated memory of a NFD, NFC, NFKD or NFKC
377  *  normalized version of the null-terminated string 'str'.
378  */
379 
380 #ifdef __cplusplus
381 }
382 #endif
383 
384 #endif
385 
386