1*6a54128fSAndroid Build Coastguard Worker /* 2*6a54128fSAndroid Build Coastguard Worker * Copyright (c) 2014 SGI. 3*6a54128fSAndroid Build Coastguard Worker * All rights reserved. 4*6a54128fSAndroid Build Coastguard Worker * 5*6a54128fSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or 6*6a54128fSAndroid Build Coastguard Worker * modify it under the terms of the GNU General Public License as 7*6a54128fSAndroid Build Coastguard Worker * published by the Free Software Foundation. 8*6a54128fSAndroid Build Coastguard Worker * 9*6a54128fSAndroid Build Coastguard Worker * This program is distributed in the hope that it would be useful, 10*6a54128fSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*6a54128fSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*6a54128fSAndroid Build Coastguard Worker * GNU General Public License for more details. 13*6a54128fSAndroid Build Coastguard Worker * 14*6a54128fSAndroid Build Coastguard Worker */ 15*6a54128fSAndroid Build Coastguard Worker 16*6a54128fSAndroid Build Coastguard Worker /* This code is copied from the linux kernel. We have a userspace 17*6a54128fSAndroid Build Coastguard Worker * version here to such that hashes will match that implementation. 18*6a54128fSAndroid Build Coastguard Worker */ 19*6a54128fSAndroid Build Coastguard Worker 20*6a54128fSAndroid Build Coastguard Worker #ifndef UTF8NORM_H 21*6a54128fSAndroid Build Coastguard Worker #define UTF8NORM_H 22*6a54128fSAndroid Build Coastguard Worker 23*6a54128fSAndroid Build Coastguard Worker #include <stdint.h> 24*6a54128fSAndroid Build Coastguard Worker #include <unistd.h> 25*6a54128fSAndroid Build Coastguard Worker #include <string.h> 26*6a54128fSAndroid Build Coastguard Worker 27*6a54128fSAndroid Build Coastguard Worker /* Encoding a unicode version number as a single unsigned int. */ 28*6a54128fSAndroid Build Coastguard Worker #define UNICODE_MAJ_SHIFT (16) 29*6a54128fSAndroid Build Coastguard Worker #define UNICODE_MIN_SHIFT (8) 30*6a54128fSAndroid Build Coastguard Worker 31*6a54128fSAndroid Build Coastguard Worker #define UNICODE_AGE(MAJ, MIN, REV) \ 32*6a54128fSAndroid Build Coastguard Worker (((unsigned int)(MAJ) << UNICODE_MAJ_SHIFT) | \ 33*6a54128fSAndroid Build Coastguard Worker ((unsigned int)(MIN) << UNICODE_MIN_SHIFT) | \ 34*6a54128fSAndroid Build Coastguard Worker ((unsigned int)(REV))) 35*6a54128fSAndroid Build Coastguard Worker 36*6a54128fSAndroid Build Coastguard Worker /* Highest unicode version supported by the data tables. */ 37*6a54128fSAndroid Build Coastguard Worker extern int utf8version_is_supported(uint8_t maj, uint8_t min, uint8_t rev); 38*6a54128fSAndroid Build Coastguard Worker extern int utf8version_latest(void); 39*6a54128fSAndroid Build Coastguard Worker 40*6a54128fSAndroid Build Coastguard Worker /* 41*6a54128fSAndroid Build Coastguard Worker * Look for the correct const struct utf8data for a unicode version. 42*6a54128fSAndroid Build Coastguard Worker * Returns NULL if the version requested is too new. 43*6a54128fSAndroid Build Coastguard Worker * 44*6a54128fSAndroid Build Coastguard Worker * Two normalization forms are supported: nfdi and nfdicf. 45*6a54128fSAndroid Build Coastguard Worker * 46*6a54128fSAndroid Build Coastguard Worker * nfdi: 47*6a54128fSAndroid Build Coastguard Worker * - Apply unicode normalization form NFD. 48*6a54128fSAndroid Build Coastguard Worker * - Remove any Default_Ignorable_Code_Point. 49*6a54128fSAndroid Build Coastguard Worker * 50*6a54128fSAndroid Build Coastguard Worker * nfdicf: 51*6a54128fSAndroid Build Coastguard Worker * - Apply unicode normalization form NFD. 52*6a54128fSAndroid Build Coastguard Worker * - Remove any Default_Ignorable_Code_Point. 53*6a54128fSAndroid Build Coastguard Worker * - Apply a full casefold (C + F). 54*6a54128fSAndroid Build Coastguard Worker */ 55*6a54128fSAndroid Build Coastguard Worker extern const struct utf8data *utf8nfdi(unsigned int maxage); 56*6a54128fSAndroid Build Coastguard Worker extern const struct utf8data *utf8nfdicf(unsigned int maxage); 57*6a54128fSAndroid Build Coastguard Worker 58*6a54128fSAndroid Build Coastguard Worker /* 59*6a54128fSAndroid Build Coastguard Worker * Determine the maximum age of any unicode character in the string. 60*6a54128fSAndroid Build Coastguard Worker * Returns 0 if only unassigned code points are present. 61*6a54128fSAndroid Build Coastguard Worker * Returns -1 if the input is not valid UTF-8. 62*6a54128fSAndroid Build Coastguard Worker */ 63*6a54128fSAndroid Build Coastguard Worker extern int utf8agemax(const struct utf8data *data, const char *s); 64*6a54128fSAndroid Build Coastguard Worker extern int utf8nagemax(const struct utf8data *data, const char *s, size_t len); 65*6a54128fSAndroid Build Coastguard Worker 66*6a54128fSAndroid Build Coastguard Worker /* 67*6a54128fSAndroid Build Coastguard Worker * Determine the minimum age of any unicode character in the string. 68*6a54128fSAndroid Build Coastguard Worker * Returns 0 if any unassigned code points are present. 69*6a54128fSAndroid Build Coastguard Worker * Returns -1 if the input is not valid UTF-8. 70*6a54128fSAndroid Build Coastguard Worker */ 71*6a54128fSAndroid Build Coastguard Worker extern int utf8agemin(const struct utf8data *data, const char *s); 72*6a54128fSAndroid Build Coastguard Worker extern int utf8nagemin(const struct utf8data *data, const char *s, size_t len); 73*6a54128fSAndroid Build Coastguard Worker 74*6a54128fSAndroid Build Coastguard Worker /* 75*6a54128fSAndroid Build Coastguard Worker * Determine the length of the normalized from of the string, 76*6a54128fSAndroid Build Coastguard Worker * excluding any terminating NULL byte. 77*6a54128fSAndroid Build Coastguard Worker * Returns 0 if only ignorable code points are present. 78*6a54128fSAndroid Build Coastguard Worker * Returns -1 if the input is not valid UTF-8. 79*6a54128fSAndroid Build Coastguard Worker */ 80*6a54128fSAndroid Build Coastguard Worker extern ssize_t utf8len(const struct utf8data *data, const char *s); 81*6a54128fSAndroid Build Coastguard Worker extern ssize_t utf8nlen(const struct utf8data *data, const char *s, size_t len); 82*6a54128fSAndroid Build Coastguard Worker 83*6a54128fSAndroid Build Coastguard Worker /* Needed in struct utf8cursor below. */ 84*6a54128fSAndroid Build Coastguard Worker #define UTF8HANGULLEAF (12) 85*6a54128fSAndroid Build Coastguard Worker 86*6a54128fSAndroid Build Coastguard Worker /* 87*6a54128fSAndroid Build Coastguard Worker * Cursor structure used by the normalizer. 88*6a54128fSAndroid Build Coastguard Worker */ 89*6a54128fSAndroid Build Coastguard Worker struct utf8cursor { 90*6a54128fSAndroid Build Coastguard Worker const struct utf8data *data; 91*6a54128fSAndroid Build Coastguard Worker const char *s; 92*6a54128fSAndroid Build Coastguard Worker const char *p; 93*6a54128fSAndroid Build Coastguard Worker const char *ss; 94*6a54128fSAndroid Build Coastguard Worker const char *sp; 95*6a54128fSAndroid Build Coastguard Worker unsigned int len; 96*6a54128fSAndroid Build Coastguard Worker unsigned int slen; 97*6a54128fSAndroid Build Coastguard Worker short int ccc; 98*6a54128fSAndroid Build Coastguard Worker short int nccc; 99*6a54128fSAndroid Build Coastguard Worker unsigned char hangul[UTF8HANGULLEAF]; 100*6a54128fSAndroid Build Coastguard Worker }; 101*6a54128fSAndroid Build Coastguard Worker 102*6a54128fSAndroid Build Coastguard Worker /* 103*6a54128fSAndroid Build Coastguard Worker * Initialize a utf8cursor to normalize a string. 104*6a54128fSAndroid Build Coastguard Worker * Returns 0 on success. 105*6a54128fSAndroid Build Coastguard Worker * Returns -1 on failure. 106*6a54128fSAndroid Build Coastguard Worker */ 107*6a54128fSAndroid Build Coastguard Worker extern int utf8cursor(struct utf8cursor *u8c, const struct utf8data *data, 108*6a54128fSAndroid Build Coastguard Worker const char *s); 109*6a54128fSAndroid Build Coastguard Worker extern int utf8ncursor(struct utf8cursor *u8c, const struct utf8data *data, 110*6a54128fSAndroid Build Coastguard Worker const char *s, size_t len); 111*6a54128fSAndroid Build Coastguard Worker 112*6a54128fSAndroid Build Coastguard Worker /* 113*6a54128fSAndroid Build Coastguard Worker * Get the next byte in the normalization. 114*6a54128fSAndroid Build Coastguard Worker * Returns a value > 0 && < 256 on success. 115*6a54128fSAndroid Build Coastguard Worker * Returns 0 when the end of the normalization is reached. 116*6a54128fSAndroid Build Coastguard Worker * Returns -1 if the string being normalized is not valid UTF-8. 117*6a54128fSAndroid Build Coastguard Worker */ 118*6a54128fSAndroid Build Coastguard Worker extern int utf8byte(struct utf8cursor *u8c); 119*6a54128fSAndroid Build Coastguard Worker 120*6a54128fSAndroid Build Coastguard Worker #endif /* UTF8NORM_H */ 121