Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:14

0001 /*
0002  *  Copyright (c) 2009 Public Software Group e. V., Berlin, Germany
0003  *
0004  *  Permission is hereby granted, free of charge, to any person obtaining a
0005  *  copy of this software and associated documentation files (the "Software"),
0006  *  to deal in the Software without restriction, including without limitation
0007  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  *  and/or sell copies of the Software, and to permit persons to whom the
0009  *  Software is furnished to do so, subject to the following conditions:
0010  *
0011  *  The above copyright notice and this permission notice shall be included in
0012  *  all copies or substantial portions of the Software.
0013  *
0014  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0019  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0020  *  DEALINGS IN THE SOFTWARE.
0021  */
0022 
0023 
0024 /*
0025  *  File name:    utf8proc.h
0026  *
0027  *  Description:
0028  *  Header files for libutf8proc, which is a mapping tool for UTF-8 strings
0029  *  with following features:
0030  *  - decomposing and composing of strings
0031  *  - replacing compatibility characters with their equivalents
0032  *  - stripping of "default ignorable characters"
0033  *    like SOFT-HYPHEN or ZERO-WIDTH-SPACE
0034  *  - folding of certain characters for string comparison
0035  *    (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-")
0036  *    (see "LUMP" option)
0037  *  - optional rejection of strings containing non-assigned code points
0038  *  - stripping of control characters
0039  *  - stripping of character marks (accents, etc.)
0040  *  - transformation of LF, CRLF, CR and NEL to line-feed (LF)
0041  *    or to the unicode chararacters for paragraph separation (PS)
0042  *    or line separation (LS).
0043  *  - unicode case folding (for case insensitive string comparisons)
0044  *  - rejection of illegal UTF-8 data
0045  *    (i.e. UTF-8 encoded UTF-16 surrogates)
0046  *  - support for korean hangul characters
0047  *  Unicode Version 5.0.0 is supported.
0048  */
0049 
0050 
0051 #ifndef UTF8PROC_H
0052 #define UTF8PROC_H
0053 
0054 
0055 #include <stdlib.h>
0056 #include <sys/types.h>
0057 #ifdef _MSC_VER
0058 typedef signed char int8_t;
0059 typedef unsigned char uint8_t;
0060 typedef short int16_t;
0061 typedef unsigned short uint16_t;
0062 typedef int int32_t;
0063 #ifdef _WIN64
0064 #define ssize_t __int64
0065 #else
0066 #define ssize_t int
0067 #endif
0068 typedef unsigned char bool;
0069 enum {false, true};
0070 #else
0071 #include <stdbool.h>
0072 #include <inttypes.h>
0073 #endif
0074 #include <limits.h>
0075 
0076 #ifdef __cplusplus
0077 extern "C" {
0078 #endif
0079 
0080 #ifndef SSIZE_MAX
0081 #define SSIZE_MAX ((size_t)SIZE_MAX/2)
0082 #endif
0083 
0084 #define UTF8PROC_NULLTERM  (1<<0)
0085 #define UTF8PROC_STABLE    (1<<1)
0086 #define UTF8PROC_COMPAT    (1<<2)
0087 #define UTF8PROC_COMPOSE   (1<<3)
0088 #define UTF8PROC_DECOMPOSE (1<<4)
0089 #define UTF8PROC_IGNORE    (1<<5)
0090 #define UTF8PROC_REJECTNA  (1<<6)
0091 #define UTF8PROC_NLF2LS    (1<<7)
0092 #define UTF8PROC_NLF2PS    (1<<8)
0093 #define UTF8PROC_NLF2LF    (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS)
0094 #define UTF8PROC_STRIPCC   (1<<9)
0095 #define UTF8PROC_CASEFOLD  (1<<10)
0096 #define UTF8PROC_CHARBOUND (1<<11)
0097 #define UTF8PROC_LUMP      (1<<12)
0098 #define UTF8PROC_STRIPMARK (1<<13)
0099 /*
0100  *  Flags being regarded by several functions in the library:
0101  *  NULLTERM:  The given UTF-8 input is NULL terminated.
0102  *  STABLE:    Unicode Versioning Stability has to be respected.
0103  *  COMPAT:    Compatiblity decomposition
0104  *             (i.e. formatting information is lost)
0105  *  COMPOSE:   Return a result with composed characters.
0106  *  DECOMPOSE: Return a result with decomposed characters.
0107  *  IGNORE:    Strip "default ignorable characters"
0108  *  REJECTNA:  Return an error, if the input contains unassigned
0109  *             code points.
0110  *  NLF2LS:    Indicating that NLF-sequences (LF, CRLF, CR, NEL) are
0111  *             representing a line break, and should be converted to the
0112  *             unicode character for line separation (LS).
0113  *  NLF2PS:    Indicating that NLF-sequences are representing a paragraph
0114  *             break, and should be converted to the unicode character for
0115  *             paragraph separation (PS).
0116  *  NLF2LF:    Indicating that the meaning of NLF-sequences is unknown.
0117  *  STRIPCC:   Strips and/or convers control characters.
0118  *             NLF-sequences are transformed into space, except if one of
0119  *             the NLF2LS/PS/LF options is given.
0120  *             HorizontalTab (HT) and FormFeed (FF) are treated as a
0121  *             NLF-sequence in this case.
0122  *             All other control characters are simply removed.
0123  *  CASEFOLD:  Performs unicode case folding, to be able to do a
0124  *             case-insensitive string comparison.
0125  *  CHARBOUND: Inserts 0xFF bytes at the beginning of each sequence which
0126  *             is representing a single grapheme cluster (see UAX#29).
0127  *  LUMP:      Lumps certain characters together
0128  *             (e.g. HYPHEN U+2010 and MINUS U+2212 to ASCII "-").
0129  *             (See lump.txt for details.)
0130  *             If NLF2LF is set, this includes a transformation of
0131  *             paragraph and line separators to ASCII line-feed (LF).
0132  *  STRIPMARK: Strips all character markings
0133  *             (non-spacing, spacing and enclosing) (i.e. accents)
0134  *             NOTE: this option works only with COMPOSE or DECOMPOSE
0135  */
0136 
0137 #define UTF8PROC_ERROR_NOMEM -1
0138 #define UTF8PROC_ERROR_OVERFLOW -2
0139 #define UTF8PROC_ERROR_INVALIDUTF8 -3
0140 #define UTF8PROC_ERROR_NOTASSIGNED -4
0141 #define UTF8PROC_ERROR_INVALIDOPTS -5
0142 /*
0143  *  Error codes being returned by almost all functions:
0144  *  ERROR_NOMEM:       Memory could not be allocated.
0145  *  ERROR_OVERFLOW:    The given string is too long to be processed.
0146  *  ERROR_INVALIDUTF8: The given string is not a legal UTF-8 string.
0147  *  ERROR_NOTASSIGNED: The REJECTNA flag was set,
0148  *                     and an unassigned code point was found.
0149  *  ERROR_INVALIDOPTS: Invalid options have been used.
0150  */
0151 
0152 typedef int16_t utf8proc_propval_t;
0153 typedef struct utf8proc_property_struct {
0154   utf8proc_propval_t category;
0155   utf8proc_propval_t combining_class;
0156   utf8proc_propval_t bidi_class;
0157   utf8proc_propval_t decomp_type;
0158   const int32_t *decomp_mapping;
0159   unsigned bidi_mirrored:1;
0160   int32_t uppercase_mapping;
0161   int32_t lowercase_mapping;
0162   int32_t titlecase_mapping;
0163   int32_t comb1st_index;
0164   int32_t comb2nd_index;
0165   unsigned comp_exclusion:1;
0166   unsigned ignorable:1;
0167   unsigned control_boundary:1;
0168   unsigned extend:1;
0169   const int32_t *casefold_mapping;
0170 } utf8proc_property_t;
0171 
0172 #define UTF8PROC_CATEGORY_LU  1
0173 #define UTF8PROC_CATEGORY_LL  2
0174 #define UTF8PROC_CATEGORY_LT  3
0175 #define UTF8PROC_CATEGORY_LM  4
0176 #define UTF8PROC_CATEGORY_LO  5
0177 #define UTF8PROC_CATEGORY_MN  6
0178 #define UTF8PROC_CATEGORY_MC  7
0179 #define UTF8PROC_CATEGORY_ME  8
0180 #define UTF8PROC_CATEGORY_ND  9
0181 #define UTF8PROC_CATEGORY_NL 10
0182 #define UTF8PROC_CATEGORY_NO 11
0183 #define UTF8PROC_CATEGORY_PC 12
0184 #define UTF8PROC_CATEGORY_PD 13
0185 #define UTF8PROC_CATEGORY_PS 14
0186 #define UTF8PROC_CATEGORY_PE 15
0187 #define UTF8PROC_CATEGORY_PI 16
0188 #define UTF8PROC_CATEGORY_PF 17
0189 #define UTF8PROC_CATEGORY_PO 18
0190 #define UTF8PROC_CATEGORY_SM 19
0191 #define UTF8PROC_CATEGORY_SC 20
0192 #define UTF8PROC_CATEGORY_SK 21
0193 #define UTF8PROC_CATEGORY_SO 22
0194 #define UTF8PROC_CATEGORY_ZS 23
0195 #define UTF8PROC_CATEGORY_ZL 24
0196 #define UTF8PROC_CATEGORY_ZP 25
0197 #define UTF8PROC_CATEGORY_CC 26
0198 #define UTF8PROC_CATEGORY_CF 27
0199 #define UTF8PROC_CATEGORY_CS 28
0200 #define UTF8PROC_CATEGORY_CO 29
0201 #define UTF8PROC_CATEGORY_CN 30
0202 #define UTF8PROC_BIDI_CLASS_L    1
0203 #define UTF8PROC_BIDI_CLASS_LRE  2
0204 #define UTF8PROC_BIDI_CLASS_LRO  3
0205 #define UTF8PROC_BIDI_CLASS_R    4
0206 #define UTF8PROC_BIDI_CLASS_AL   5
0207 #define UTF8PROC_BIDI_CLASS_RLE  6
0208 #define UTF8PROC_BIDI_CLASS_RLO  7
0209 #define UTF8PROC_BIDI_CLASS_PDF  8
0210 #define UTF8PROC_BIDI_CLASS_EN   9
0211 #define UTF8PROC_BIDI_CLASS_ES  10
0212 #define UTF8PROC_BIDI_CLASS_ET  11
0213 #define UTF8PROC_BIDI_CLASS_AN  12
0214 #define UTF8PROC_BIDI_CLASS_CS  13
0215 #define UTF8PROC_BIDI_CLASS_NSM 14
0216 #define UTF8PROC_BIDI_CLASS_BN  15
0217 #define UTF8PROC_BIDI_CLASS_B   16
0218 #define UTF8PROC_BIDI_CLASS_S   17
0219 #define UTF8PROC_BIDI_CLASS_WS  18
0220 #define UTF8PROC_BIDI_CLASS_ON  19
0221 #define UTF8PROC_DECOMP_TYPE_FONT      1
0222 #define UTF8PROC_DECOMP_TYPE_NOBREAK   2
0223 #define UTF8PROC_DECOMP_TYPE_INITIAL   3
0224 #define UTF8PROC_DECOMP_TYPE_MEDIAL    4
0225 #define UTF8PROC_DECOMP_TYPE_FINAL     5
0226 #define UTF8PROC_DECOMP_TYPE_ISOLATED  6
0227 #define UTF8PROC_DECOMP_TYPE_CIRCLE    7
0228 #define UTF8PROC_DECOMP_TYPE_SUPER     8
0229 #define UTF8PROC_DECOMP_TYPE_SUB       9
0230 #define UTF8PROC_DECOMP_TYPE_VERTICAL 10
0231 #define UTF8PROC_DECOMP_TYPE_WIDE     11
0232 #define UTF8PROC_DECOMP_TYPE_NARROW   12
0233 #define UTF8PROC_DECOMP_TYPE_SMALL    13
0234 #define UTF8PROC_DECOMP_TYPE_SQUARE   14
0235 #define UTF8PROC_DECOMP_TYPE_FRACTION 15
0236 #define UTF8PROC_DECOMP_TYPE_COMPAT   16
0237 
0238 extern const int8_t utf8proc_utf8class[256];
0239 
0240 const char *utf8proc_version(void);
0241 
0242 const char *utf8proc_errmsg(ssize_t errcode);
0243 /*
0244  *  Returns a static error string for the given error code.
0245  */
0246 
0247 ssize_t utf8proc_iterate(const uint8_t *str, ssize_t strlen, int32_t *dst);
0248 /*
0249  *  Reads a single char from the UTF-8 sequence being pointed to by 'str'.
0250  *  The maximum number of bytes read is 'strlen', unless 'strlen' is
0251  *  negative.
0252  *  If a valid unicode char could be read, it is stored in the variable
0253  *  being pointed to by 'dst', otherwise that variable will be set to -1.
0254  *  In case of success the number of bytes read is returned, otherwise a
0255  *  negative error code is returned.
0256  */
0257 
0258 bool utf8proc_codepoint_valid(int32_t uc);
0259 /*
0260  *  Returns 1, if the given unicode code-point is valid, otherwise 0.
0261  */
0262 
0263 ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst);
0264 /*
0265  *  Encodes the unicode char with the code point 'uc' as an UTF-8 string in
0266  *  the byte array being pointed to by 'dst'. This array has to be at least
0267  *  4 bytes long.
0268  *  In case of success the number of bytes written is returned,
0269  *  otherwise 0.
0270  *  This function does not check if 'uc' is a valid unicode code point.
0271  */
0272 
0273 const utf8proc_property_t *utf8proc_get_property(int32_t uc);
0274 /*
0275  *  Returns a pointer to a (constant) struct containing information about
0276  *  the unicode char with the given code point 'uc'.
0277  *  If the character is not existent a pointer to a special struct is
0278  *  returned, where 'category' is a NULL pointer.
0279  *  WARNING: The parameter 'uc' has to be in the range of 0x0000 to
0280  *           0x10FFFF, otherwise the program might crash!
0281  */
0282 
0283 ssize_t utf8proc_decompose_char(
0284   int32_t uc, int32_t *dst, ssize_t bufsize,
0285   int options, int *last_boundclass
0286 );
0287 /*
0288  *  Writes a decomposition of the unicode char 'uc' into the array being
0289  *  pointed to by 'dst'.
0290  *  Following flags in the 'options' field are regarded:
0291  *  REJECTNA:  an unassigned unicode code point leads to an error
0292  *  IGNORE:    "default ignorable" chars are stripped
0293  *  CASEFOLD:  unicode casefolding is applied
0294  *  COMPAT:    replace certain characters with their
0295  *             compatibility decomposition
0296  *  CHARBOUND: Inserts 0xFF bytes before each grapheme cluster
0297  *  LUMP:      lumps certain different characters together
0298  *  STRIPMARK: removes all character marks
0299  *  The pointer 'last_boundclass' has to point to an integer variable which
0300  *  is storing the last character boundary class, if the CHARBOUND option
0301  *  is used.
0302  *  In case of success the number of chars written is returned,
0303  *  in case of an error, a negative error code is returned.
0304  *  If the number of written chars would be bigger than 'bufsize',
0305  *  the buffer (up to 'bufsize') has inpredictable data, and the needed
0306  *  buffer size is returned.
0307  *  WARNING: The parameter 'uc' has to be in the range of 0x0000 to
0308  *           0x10FFFF, otherwise the program might crash!
0309  */
0310 
0311 ssize_t utf8proc_decompose(
0312   const uint8_t *str, ssize_t strlen,
0313   int32_t *buffer, ssize_t bufsize, int options
0314 );
0315 /*
0316  *  Does the same as 'utf8proc_decompose_char', but acts on a whole UTF-8
0317  *  string, and orders the decomposed sequences correctly.
0318  *  If the NULLTERM flag in 'options' is set, processing will be stopped,
0319  *  when a NULL byte is encounted, otherwise 'strlen' bytes are processed.
0320  *  The result in form of unicode code points is written into the buffer
0321  *  being pointed to by 'buffer', having the length of 'bufsize' entries.
0322  *  In case of success the number of chars written is returned,
0323  *  in case of an error, a negative error code is returned.
0324  *  If the number of written chars would be bigger than 'bufsize',
0325  *  the buffer (up to 'bufsize') has inpredictable data, and the needed
0326  *  buffer size is returned.
0327  */
0328 
0329 ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options);
0330 /*
0331  *  Reencodes the sequence of unicode characters given by the pointer
0332  *  'buffer' and 'length' as UTF-8.
0333  *  The result is stored in the same memory area where the data is read.
0334  *  Following flags in the 'options' field are regarded:
0335  *  NLF2LS:  converts LF, CRLF, CR and NEL into LS
0336  *  NLF2PS:  converts LF, CRLF, CR and NEL into PS
0337  *  NLF2LF:  converts LF, CRLF, CR and NEL into LF
0338  *  STRIPCC: strips or converts all non-affected control characters
0339  *  COMPOSE: tries to combine decomposed characters into composite
0340  *           characters
0341  *  STABLE:  prohibits combining characters which would violate
0342  *           the unicode versioning stability
0343  *  In case of success the length of the resulting UTF-8 string is
0344  *  returned, otherwise a negative error code is returned.
0345  *  WARNING: The amount of free space being pointed to by 'buffer', has to
0346  *           exceed the amount of the input data by one byte, and the
0347  *           entries of the array pointed to by 'str' have to be in the
0348  *           range of 0x0000 to 0x10FFFF, otherwise the program might
0349  *           crash!
0350  */
0351 
0352 ssize_t utf8proc_map(
0353   const uint8_t *str, ssize_t strlen, uint8_t **dstptr, int options
0354 );
0355 /*
0356  *  Maps the given UTF-8 string being pointed to by 'str' to a new UTF-8
0357  *  string, which is allocated dynamically, and afterwards pointed to by
0358  *  the pointer being pointed to by 'dstptr'.
0359  *  If the NULLTERM flag in the 'options' field is set, the length is
0360  *  determined by a NULL terminator, otherwise the parameter 'strlen' is
0361  *  evaluated to determine the string length, but in any case the result
0362  *  will be NULL terminated (though it might contain NULL characters
0363  *  before). Other flags in the 'options' field are passed to the functions
0364  *  defined above, and regarded as described.
0365  *  In case of success the length of the new string is returned,
0366  *  otherwise a negative error code is returned.
0367  *  NOTICE: The memory of the new UTF-8 string will have been allocated with
0368  *          'malloc', and has theirfore to be freed with 'free'.
0369  */
0370 
0371 uint8_t *utf8proc_NFD(const uint8_t *str);
0372 uint8_t *utf8proc_NFC(const uint8_t *str);
0373 uint8_t *utf8proc_NFKD(const uint8_t *str);
0374 uint8_t *utf8proc_NFKC(const uint8_t *str);
0375 /*
0376  *  Returns a pointer to newly allocated memory of a NFD, NFC, NFKD or NFKC
0377  *  normalized version of the null-terminated string 'str'.
0378  */
0379 
0380 #ifdef __cplusplus
0381 }
0382 #endif
0383 
0384 #endif
0385