jbe@0: /* jbe@0: * Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany jbe@0: * Author: Jan Behrens jbe@0: * All rights reserved. jbe@0: * jbe@0: * Redistribution and use in source and binary forms, with or without jbe@0: * modification, are permitted provided that the following conditions are jbe@0: * met: jbe@0: * jbe@0: * 1. Redistributions of source code must retain the above copyright jbe@0: * notice, this list of conditions and the following disclaimer. jbe@0: * 2. Redistributions in binary form must reproduce the above copyright jbe@0: * notice, this list of conditions and the following disclaimer in the jbe@0: * documentation and/or other materials provided with the distribution. jbe@0: * 3. Neither the name of the FlexiGuided GmbH nor the names of its jbe@0: * contributors may be used to endorse or promote products derived from jbe@0: * this software without specific prior written permission. jbe@0: * jbe@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS jbe@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT jbe@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A jbe@0: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER jbe@0: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, jbe@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, jbe@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jbe@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF jbe@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING jbe@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS jbe@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jbe@0: * jbe@0: * jbe@0: * This library contains derived data from a modified version of the jbe@0: * Unicode data files. jbe@0: * jbe@0: * The original data files are available at jbe@0: * http://www.unicode.org/Public/UNIDATA/ jbe@0: * jbe@0: * Please notice the copyright statement in the file "utf8proc_data.c". jbe@0: * jbe@0: */ jbe@0: jbe@0: jbe@0: /* jbe@0: * File name: utf8proc.h jbe@1: * Version: 0.2 jbe@0: * Last changed: 2006-05-31 jbe@0: * jbe@0: * Description: jbe@0: * Header files for libutf8proc, which is a mapping tool for UTF-8 strings jbe@0: * with following features: jbe@0: * - decomposing and composing of strings jbe@0: * - replacing compatibility characters with their equivalents jbe@0: * - stripping of "default ignorable characters" jbe@0: * like SOFT-HYPHEN or ZERO-WIDTH-SPACE jbe@0: * - optional rejection of strings containing non-assigned code points jbe@0: * - stripping of control characters jbe@0: * - transformation of LF, CRLF, CR and NEL to line-feed (LF) jbe@0: * or to the unicode chararacters for paragraph separation (PS) jbe@0: * or line separation (LS). jbe@0: * - unicode case folding (for case insensitive string comparisons) jbe@0: * - rejection of illegal UTF-8 data (i.e. UTF-8 encoded UTF-16 surrogates) jbe@0: * - support for korean hangul characters jbe@0: * Unicode Version 4.1.0 is supported. jbe@0: */ jbe@0: jbe@0: jbe@0: #ifndef UTF8PROC_H jbe@0: #define UTF8PROC_H jbe@0: jbe@0: jbe@0: #include jbe@0: #include jbe@0: #include jbe@0: #include jbe@0: #include jbe@0: jbe@0: #ifndef SSIZE_MAX jbe@0: #define SSIZE_MAX (SIZE_MAX/2) jbe@0: #endif jbe@0: jbe@0: #define UTF8PROC_NULLTERM (1<<0) jbe@0: #define UTF8PROC_STABLE (1<<1) jbe@0: #define UTF8PROC_COMPAT (1<<2) jbe@0: #define UTF8PROC_COMPOSE (1<<3) jbe@0: #define UTF8PROC_IGNORE (1<<4) jbe@0: #define UTF8PROC_REJECTNA (1<<5) jbe@0: #define UTF8PROC_NLF2LS (1<<6) jbe@0: #define UTF8PROC_NLF2PS (1<<7) jbe@0: #define UTF8PROC_NLF2LF (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS) jbe@0: #define UTF8PROC_STRIPCC (1<<8) jbe@0: #define UTF8PROC_CASEFOLD (1<<9) jbe@0: /* jbe@0: * Flags being regarded by several functions in the library: jbe@0: * NULLTERM: The given UTF-8 input is NULL terminated. jbe@0: * STABLE: Unicode Versioning Stability has to be respected. jbe@0: * COMPAT: Compatiblity decomposition (i.e. formatting information is lost) jbe@0: * COMPOSE: Return a result with composed characters, instead of decomposed. jbe@0: * IGNORE: Strip "default ignorable characters" jbe@0: * REJECTNA: Return an error, if the input contains unassigned code points. jbe@0: * NLF2LS: Indicating that NLF-sequences (LF, CRLF, CR, NEL) are jbe@0: * representing a line break, and should be converted to the jbe@0: * unicode character for line separation (LS). jbe@0: * NLF2PS: Indicating that NLF-sequences are representing a paragraph jbe@0: * break, and should be converted to the unicode character for jbe@0: * paragraph separation (PS). jbe@0: * NLF2LF: Indicating that the meaning of NLF-sequences is unknown. jbe@0: * STRIPCC: Strips and/or convers control characters. jbe@0: * NLF-sequences are transformed into space, except if one of the jbe@0: * NLF2LS/PS/LF options is given. jbe@0: * HorizontalTab (HT) and FormFeed (FF) are treated as a jbe@0: * NLF-sequence in this case. jbe@0: * All other control characters are simply removed. jbe@0: * CASEFOLD: Performs unicode case folding, to be able to do a jbe@0: * case-insensitive string comparison. jbe@0: */ jbe@0: jbe@0: #define UTF8PROC_ERROR_NOMEM -1 jbe@0: #define UTF8PROC_ERROR_OVERFLOW -2 jbe@0: #define UTF8PROC_ERROR_INVALIDUTF8 -3 jbe@0: #define UTF8PROC_ERROR_NOTASSIGNED -4 jbe@0: /* jbe@0: * Error codes being returned by almost all functions: jbe@0: * ERROR_NOMEM: Memory could not be allocated. jbe@0: * ERROR_OVERFLOW: The given string is too long to be processed. jbe@0: * ERROR_INVALIDUTF8: The given string is not a legal UTF-8 string. jbe@0: * ERROR_NOTASSIGNED: The REJECTNA flag was set, jbe@0: * and an unassigned code point was found. jbe@0: */ jbe@0: jbe@0: typedef struct utf8proc_property_struct { jbe@0: const char *category; jbe@0: const int16_t combining_class; jbe@0: const char *bidi_class; jbe@0: const char *decomp_type; jbe@0: const int32_t *decomp_mapping; jbe@0: const unsigned bidi_mirrored:1; jbe@0: const int32_t uppercase_mapping; jbe@0: const int32_t lowercase_mapping; jbe@0: const int32_t titlecase_mapping; jbe@0: const int32_t comb1st_index; jbe@0: const int32_t comb2nd_index; jbe@0: const unsigned comp_exclusion:1; jbe@0: const unsigned ignorable:1; jbe@0: const int32_t *casefold_mapping; jbe@0: } utf8proc_property_t; jbe@0: jbe@0: extern const int8_t utf8proc_utf8class[256]; jbe@0: jbe@0: const char *utf8proc_errmsg(ssize_t errcode); jbe@0: /* jbe@0: * Returns a static error string for the given error code. jbe@0: */ jbe@0: jbe@0: ssize_t utf8proc_iterate(uint8_t *str, ssize_t strlen, int32_t *dst); jbe@0: /* jbe@0: * Reads a single char from the UTF-8 sequence being pointed to by 'str'. jbe@0: * The maximum number of bytes read is 'strlen', unless 'strlen' is jbe@0: * negative. jbe@0: * If a valid unicode char could be read, it is stored in the variable jbe@0: * being pointed to by 'dst', otherwise that variable will be set to -1. jbe@0: * In case of success the number of bytes read is returned, otherwise a jbe@0: * negative error code is returned. jbe@0: */ jbe@0: jbe@0: ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst); jbe@0: /* jbe@0: * Encodes the unicode char with the code point 'uc' as an UTF-8 string in jbe@0: * the byte array being pointed to by 'dst'. This array has to be at least jbe@0: * 4 bytes long. jbe@0: * In case of success the number of bytes written is returned, otherwise 0. jbe@0: * This function does not check if 'uc' is a valid unicode code point. jbe@0: */ jbe@0: jbe@0: const utf8proc_property_t *utf8proc_get_property(int32_t uc); jbe@0: /* jbe@0: * Returns a pointer to a (constant) struct containing information about jbe@0: * the unicode char with the given code point 'uc'. jbe@0: * If the character is not existent a pointer to a special struct is jbe@0: * returned, where 'category' is a NULL pointer. jbe@0: * WARNING: The parameter 'uc' has to be in the range of 0x0000 to jbe@0: * 0x10FFFF, otherwise the program might crash! jbe@0: */ jbe@0: jbe@0: ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufsize, jbe@0: int options); jbe@0: /* jbe@0: * Writes a decomposition of the unicode char 'uc' into the array being jbe@0: * pointed to by 'dst'. jbe@0: * Following flags in the 'options' field are regarded: jbe@0: * REJECTNA: an unassigned unicode code point leads to an error jbe@0: * IGNORE: "default ignorable" chars are stripped jbe@0: * CASEFOLD: unicode casefolding is applied jbe@0: * COMPAT: replace certain characters with their jbe@0: * compatibility decomposition jbe@0: * In case of success the number of chars written is returned, jbe@0: * in case of an error, a negative error code is returned. jbe@0: * If the number of written chars would be bigger than 'bufsize', jbe@0: * the buffer (up to 'bufsize') has inpredictable data, and the needed jbe@0: * buffer size is returned. jbe@0: * WARNING: The parameter 'uc' has to be in the range of 0x0000 to jbe@0: * 0x10FFFF, otherwise the program might crash! jbe@0: */ jbe@0: jbe@0: ssize_t utf8proc_decompose(uint8_t *str, ssize_t strlen, jbe@0: int32_t *buffer, ssize_t bufsize, int options); jbe@0: /* jbe@0: * Does the same as 'utf8proc_decompose_char', but acts on a whole UTF-8 jbe@0: * string, and orders the decomposed sequences correctly. jbe@0: * If the NULLTERM flag in 'options' is set, processing will be stopped, jbe@0: * when a NULL byte is encounted, otherwise 'strlen' bytes are processed. jbe@0: * The result in form of unicode code points is written into the buffer jbe@0: * being pointed to by 'buffer', having the length of 'bufsize' entries. jbe@0: * In case of success the number of chars written is returned, jbe@0: * in case of an error, a negative error code is returned. jbe@0: * If the number of written chars would be bigger than 'bufsize', jbe@0: * the buffer (up to 'bufsize') has inpredictable data, and the needed jbe@0: * buffer size is returned. jbe@0: */ jbe@0: jbe@0: ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options); jbe@0: /* jbe@0: * Reencodes the sequence of unicode characters given by the pointer jbe@0: * 'buffer' and 'length' as UTF-8. jbe@0: * The result is stored in the same memory area where the data is read. jbe@0: * Following flags in the 'options' field are regarded: jbe@0: * NLF2LS: converts LF, CRLF, CR and NEL into LS jbe@0: * NLF2PS: converts LF, CRLF, CR and NEL into PS jbe@0: * NLF2LF: converts LF, CRLF, CR and NEL into LF jbe@0: * STRIPCC: strips or converts all non-affected control characters jbe@0: * COMPOSE: tries to combine decomposed characters into composite characters jbe@0: * STABLE: prohibits combining characters which would violate jbe@0: * the unicode versioning stability jbe@0: * In case of success the length of the resulting UTF-8 string is returned, jbe@0: * otherwise a negative error code is returned. jbe@0: * WARNING: The amount of free space being pointed to by 'buffer', has to jbe@0: * exceed the amount of the input data by one byte, and the jbe@0: * entries of the array pointed to by 'str' have to be in the jbe@0: * range of 0x0000 to 0x10FFFF, otherwise the program might crash! jbe@0: */ jbe@0: jbe@0: ssize_t utf8proc_map(uint8_t *str, ssize_t strlen, uint8_t **dstptr, jbe@0: int options); jbe@0: /* jbe@0: * Maps the given UTF-8 string being pointed to by 'str' to a new UTF-8 jbe@0: * string, which is allocated dynamically, and afterwards pointed to by the jbe@0: * pointer being pointed to by 'dstptr'. jbe@0: * If the NULLTERM flag in the 'options' field is set, the length is jbe@0: * determined by a NULL terminator, otherwise the parameter 'strlen' is jbe@0: * evaluated to determine the string length, but in any case the result jbe@0: * will be NULL terminated (though it might contain NULL characters before). jbe@0: * Other flags in the 'options' field are passed to the functions defined jbe@0: * above, and regarded as described. jbe@0: * In case of success the length of the new string is returned, jbe@0: * otherwise a negative error code is returned. jbe@0: * NOTICE: The memory of the new UTF-8 string will have been allocated with jbe@0: * 'malloc', and has theirfore to be freed with 'free'. jbe@0: */ jbe@0: jbe@0: uint8_t *utf8proc_NFD(uint8_t *str); jbe@0: uint8_t *utf8proc_NFC(uint8_t *str); jbe@0: uint8_t *utf8proc_NFKD(uint8_t *str); jbe@0: uint8_t *utf8proc_NFKC(uint8_t *str); jbe@0: /* jbe@0: * Returns a pointer to newly allocated memory of a NFD, NFC, NFKD or NFKC jbe@0: * normalized version of the null-terminated string 'str'. jbe@0: */ jbe@0: jbe@0: jbe@0: #endif jbe@0: jbe@0: