xref: /xnu-8792.41.9/bsd/sys/unicode.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions  * Copyright (c) 2016-2020 Apple Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions  *
4*5c2921b0SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions  *
6*5c2921b0SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*5c2921b0SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*5c2921b0SApple OSS Distributions  * file.
12*5c2921b0SApple OSS Distributions  *
13*5c2921b0SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*5c2921b0SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*5c2921b0SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*5c2921b0SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*5c2921b0SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*5c2921b0SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*5c2921b0SApple OSS Distributions  * limitations under the License.
20*5c2921b0SApple OSS Distributions  *
21*5c2921b0SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*5c2921b0SApple OSS Distributions  */
23*5c2921b0SApple OSS Distributions 
24*5c2921b0SApple OSS Distributions #ifndef unicode_h
25*5c2921b0SApple OSS Distributions #define unicode_h
26*5c2921b0SApple OSS Distributions 
27*5c2921b0SApple OSS Distributions #ifdef KERNEL_PRIVATE
28*5c2921b0SApple OSS Distributions 
29*5c2921b0SApple OSS Distributions #include <sys/cdefs.h>
30*5c2921b0SApple OSS Distributions #include <stdbool.h>
31*5c2921b0SApple OSS Distributions 
32*5c2921b0SApple OSS Distributions /*
33*5c2921b0SApple OSS Distributions  * WARNING - callers that use the following Unicode normalization interface for on-disk
34*5c2921b0SApple OSS Distributions  * structures should be aware that the implementation will be periodically updated for
35*5c2921b0SApple OSS Distributions  * the latest Unicode standard version.
36*5c2921b0SApple OSS Distributions  */
37*5c2921b0SApple OSS Distributions 
38*5c2921b0SApple OSS Distributions enum {
39*5c2921b0SApple OSS Distributions 	/* Maximum size of UTF32 reordering buffer for stream-safe format */
40*5c2921b0SApple OSS Distributions 	kNCFStreamSafeBufMax = 32
41*5c2921b0SApple OSS Distributions };
42*5c2921b0SApple OSS Distributions 
43*5c2921b0SApple OSS Distributions /*
44*5c2921b0SApple OSS Distributions  * utf8_normalizeOptCaseFoldAndHash
45*5c2921b0SApple OSS Distributions  *
46*5c2921b0SApple OSS Distributions  * Convert a given UTF-8 string to UTF-32 in one of the following normalized forms,
47*5c2921b0SApple OSS Distributions  * as specified by the case_sens parameter, and feed the result incrementally to
48*5c2921b0SApple OSS Distributions  * the provided hash function callback:
49*5c2921b0SApple OSS Distributions  * - "canonical caseless form" (case-folded NFD, as described by definition D145
50*5c2921b0SApple OSS Distributions  *    in chapter 3 of The Unicode Standard); for case-insensitive behavior.
51*5c2921b0SApple OSS Distributions  * - standard NFD; for case-sensitive behavior (if case_sens = true).
52*5c2921b0SApple OSS Distributions  *
53*5c2921b0SApple OSS Distributions  * The input string should be valid UTF-8 that meets the criteria for stream safe
54*5c2921b0SApple OSS Distributions  * text as described in http://unicode.org/reports/tr15/#Stream_Safe_Text_Format.
55*5c2921b0SApple OSS Distributions  * It should not contain ASCII 0x00 or '/'.
56*5c2921b0SApple OSS Distributions  *
57*5c2921b0SApple OSS Distributions  * str:       The input UTF-8 string (need not be 0 terminated)
58*5c2921b0SApple OSS Distributions  * str_len:   The byte length of the input string (excluding any 0 terminator)
59*5c2921b0SApple OSS Distributions  * case_sens: False for case-insensitive behavior; generates canonical caseless form.
60*5c2921b0SApple OSS Distributions  *            True for case-sensitive behavior; generates standard NFD.
61*5c2921b0SApple OSS Distributions  * hash_func: A pointer to a hashing function to compute the hash of the
62*5c2921b0SApple OSS Distributions  *            normalized/case-folded result. buf contains buf_len bytes
63*5c2921b0SApple OSS Distributions  *            of data to be added to the hash using the caller-supplied
64*5c2921b0SApple OSS Distributions  *            context (ctx).
65*5c2921b0SApple OSS Distributions  * hash_ctx:  The context for the hash function.
66*5c2921b0SApple OSS Distributions  *
67*5c2921b0SApple OSS Distributions  * Returns: 0 on success, or
68*5c2921b0SApple OSS Distributions  *          EILSEQ: The input string contains illegal ASCII-range characters
69*5c2921b0SApple OSS Distributions  *                  (0x00 or '/'), or is not well-formed stream-safe UTF-8, or
70*5c2921b0SApple OSS Distributions  *                  contains codepoints that are non-characters or unassigned in
71*5c2921b0SApple OSS Distributions  *                  the version of Unicode currently supported.
72*5c2921b0SApple OSS Distributions  */
73*5c2921b0SApple OSS Distributions int utf8_normalizeOptCaseFoldAndHash(const char *str,
74*5c2921b0SApple OSS Distributions     size_t      str_len,
75*5c2921b0SApple OSS Distributions     bool        case_sens,
76*5c2921b0SApple OSS Distributions     void      (*hash_func)(void *buf, size_t buf_len, void *ctx),
77*5c2921b0SApple OSS Distributions     void       *hash_ctx);
78*5c2921b0SApple OSS Distributions 
79*5c2921b0SApple OSS Distributions /*
80*5c2921b0SApple OSS Distributions  * utf8_normalizeOptCaseFoldAndCompare
81*5c2921b0SApple OSS Distributions  *
82*5c2921b0SApple OSS Distributions  * Determine whether two UTF-8 strings are equal after converting each to one of the
83*5c2921b0SApple OSS Distributions  * following normalized forms, as specified by the case_sens parameter:
84*5c2921b0SApple OSS Distributions  * - "canonical caseless form" (case-folded NFD); for case-insensitive comparison.
85*5c2921b0SApple OSS Distributions  * - standard NFD; for case-sensitive comparison (if case_sens = true).
86*5c2921b0SApple OSS Distributions  * On success, sets are_equal to true if the strings are equal, or false if they are not.
87*5c2921b0SApple OSS Distributions  *
88*5c2921b0SApple OSS Distributions  * The input strings should be valid UTF-8 that meet the criteria for stream safe
89*5c2921b0SApple OSS Distributions  * text as described in http://unicode.org/reports/tr15/#Stream_Safe_Text_Format.
90*5c2921b0SApple OSS Distributions  * They should not contain ASCII 0x00 or '/'.
91*5c2921b0SApple OSS Distributions  *
92*5c2921b0SApple OSS Distributions  * strA:      A UTF-8 string to be compared (need not be 0 terminated)
93*5c2921b0SApple OSS Distributions  * strA_len:  The byte length of strA (excluding any 0 terminator)
94*5c2921b0SApple OSS Distributions  * strB:      The second UTF-8 string to be compared (need not be 0 terminated)
95*5c2921b0SApple OSS Distributions  * strB_len:  The byte length of strB (excluding any 0 terminator)
96*5c2921b0SApple OSS Distributions  * case_sens: False for case-insensitive behavior; compares canonical caseless forms.
97*5c2921b0SApple OSS Distributions  *            True for case-sensitive behavior; compares standard NFD forms.
98*5c2921b0SApple OSS Distributions  * are_equal: On success, set to true if the strings are equal, or set to false
99*5c2921b0SApple OSS Distributions  *            if they are not.
100*5c2921b0SApple OSS Distributions  *
101*5c2921b0SApple OSS Distributions  * Returns: 0 on success, or
102*5c2921b0SApple OSS Distributions  *          EILSEQ: One or both of the input strings contains illegal ASCII-range
103*5c2921b0SApple OSS Distributions  *                  characters (0x00 or '/'), or is not well-formed stream-safe UTF-8,
104*5c2921b0SApple OSS Distributions  *                  or contains codepoints that are non-characters or unassigned in
105*5c2921b0SApple OSS Distributions  *                  the version of Unicode currently supported.
106*5c2921b0SApple OSS Distributions  *                  Note: The comparison may terminate early when a difference is
107*5c2921b0SApple OSS Distributions  *                        detected, and may return 0 and set *are_equal=false even
108*5c2921b0SApple OSS Distributions  *                        if one or both strings are invalid.
109*5c2921b0SApple OSS Distributions  */
110*5c2921b0SApple OSS Distributions int utf8_normalizeOptCaseFoldAndCompare(const char *strA,
111*5c2921b0SApple OSS Distributions     size_t      strA_len,
112*5c2921b0SApple OSS Distributions     const char *strB,
113*5c2921b0SApple OSS Distributions     size_t      strB_len,
114*5c2921b0SApple OSS Distributions     bool        case_sens,
115*5c2921b0SApple OSS Distributions     bool       *are_equal);
116*5c2921b0SApple OSS Distributions 
117*5c2921b0SApple OSS Distributions /*
118*5c2921b0SApple OSS Distributions  * utf8_normalizeOptCaseFold
119*5c2921b0SApple OSS Distributions  *
120*5c2921b0SApple OSS Distributions  * Convert a given UTF-8 string to UTF-32 in one of the following normalized forms,
121*5c2921b0SApple OSS Distributions  * as specified by the case_sens parameter, and copy the result to the ustr
122*5c2921b0SApple OSS Distributions  * buffer:
123*5c2921b0SApple OSS Distributions  * - "canonical caseless form" (case-folded NFD, as described by definition D145
124*5c2921b0SApple OSS Distributions  *    in chapter 3 of The Unicode Standard); for case-insensitive behavior.
125*5c2921b0SApple OSS Distributions  * - standard NFD; for case-sensitive behavior (if case_sens = true).
126*5c2921b0SApple OSS Distributions  *
127*5c2921b0SApple OSS Distributions  * The input string should be valid UTF-8 that meets the criteria for stream safe
128*5c2921b0SApple OSS Distributions  * text as described in http://unicode.org/reports/tr15/#Stream_Safe_Text_Format.
129*5c2921b0SApple OSS Distributions  * It should not contain ASCII 0x00 or '/'.
130*5c2921b0SApple OSS Distributions  *
131*5c2921b0SApple OSS Distributions  * str:       The input UTF-8 string (need not be 0 terminated)
132*5c2921b0SApple OSS Distributions  * str_len:   The byte length of the input string (excluding any 0 terminator)
133*5c2921b0SApple OSS Distributions  * case_sens: False for case-insensitive behavior; generates canonical caseless form.
134*5c2921b0SApple OSS Distributions  *            True for case-sensitive behavior; generates standard NFD.
135*5c2921b0SApple OSS Distributions  * ustr:      A pointer to a buffer for the resulting UTF-32 string.
136*5c2921b0SApple OSS Distributions  * ustr_size: The capacity of ustr, in UTF-32 units.
137*5c2921b0SApple OSS Distributions  * ustr_len:  Pointer to a value that will be filled in with the actual length
138*5c2921b0SApple OSS Distributions  *            in UTF-32 units of the string copied to ustr.
139*5c2921b0SApple OSS Distributions  *
140*5c2921b0SApple OSS Distributions  * Returns: 0 on success, or
141*5c2921b0SApple OSS Distributions  *          EILSEQ: The input string contains illegal ASCII-range characters
142*5c2921b0SApple OSS Distributions  *                  (0x00 or '/'), or is not well-formed stream-safe UTF-8, or
143*5c2921b0SApple OSS Distributions  *                  contains codepoints that are non-characters or unassigned in
144*5c2921b0SApple OSS Distributions  *                  the version of Unicode currently supported.
145*5c2921b0SApple OSS Distributions  *          ENOMEM: ustr_size is insufficient for the resulting string. In this
146*5c2921b0SApple OSS Distributions  *                  case the value returned in *ustr_len is invalid.
147*5c2921b0SApple OSS Distributions  */
148*5c2921b0SApple OSS Distributions int utf8_normalizeOptCaseFold(const char *str,
149*5c2921b0SApple OSS Distributions     size_t      str_len,
150*5c2921b0SApple OSS Distributions     bool        case_sens,
151*5c2921b0SApple OSS Distributions     int32_t    *ustr,
152*5c2921b0SApple OSS Distributions     int32_t     ustr_size,
153*5c2921b0SApple OSS Distributions     int32_t    *ustr_len);
154*5c2921b0SApple OSS Distributions 
155*5c2921b0SApple OSS Distributions /*
156*5c2921b0SApple OSS Distributions  * utf8_normalizeOptCaseFoldToUTF8
157*5c2921b0SApple OSS Distributions  *
158*5c2921b0SApple OSS Distributions  * Convert a given UTF-8 string to UTF-8 in one of the following normalized forms,
159*5c2921b0SApple OSS Distributions  * as specified by the case_sens parameter, and copy the result to the ustr
160*5c2921b0SApple OSS Distributions  * buffer:
161*5c2921b0SApple OSS Distributions  * - "canonical caseless form" (case-folded NFD, as described by definition D145
162*5c2921b0SApple OSS Distributions  *    in chapter 3 of The Unicode Standard); for case-insensitive behavior.
163*5c2921b0SApple OSS Distributions  * - standard NFD; for case-sensitive behavior (if case_sens = true).
164*5c2921b0SApple OSS Distributions  *
165*5c2921b0SApple OSS Distributions  * The input string should be valid UTF-8 that meets the criteria for stream safe
166*5c2921b0SApple OSS Distributions  * text as described in http://unicode.org/reports/tr15/#Stream_Safe_Text_Format.
167*5c2921b0SApple OSS Distributions  * It should not contain ASCII 0x00 or '/'.
168*5c2921b0SApple OSS Distributions  *
169*5c2921b0SApple OSS Distributions  * str:       The input UTF-8 string (need not be 0 terminated)
170*5c2921b0SApple OSS Distributions  * str_len:   The byte length of the input string (excluding any 0 terminator)
171*5c2921b0SApple OSS Distributions  * case_sens: False for case-insensitive behavior; generates canonical caseless form.
172*5c2921b0SApple OSS Distributions  *            True for case-sensitive behavior; generates standard NFD.
173*5c2921b0SApple OSS Distributions  * ustr:      A pointer to a buffer for the resulting UTF-8 string.
174*5c2921b0SApple OSS Distributions  * ustr_size: The capacity of ustr, in bytes.
175*5c2921b0SApple OSS Distributions  * ustr_len:  Pointer to a value that will be filled in with the actual length
176*5c2921b0SApple OSS Distributions  *            in bytes of the string copied to ustr.
177*5c2921b0SApple OSS Distributions  *
178*5c2921b0SApple OSS Distributions  * Returns: 0 on success, or
179*5c2921b0SApple OSS Distributions  *          EILSEQ: The input string contains illegal ASCII-range characters
180*5c2921b0SApple OSS Distributions  *                  (0x00 or '/'), or is not well-formed stream-safe UTF-8, or
181*5c2921b0SApple OSS Distributions  *                  contains codepoints that are non-characters or unassigned in
182*5c2921b0SApple OSS Distributions  *                  the version of Unicode currently supported.
183*5c2921b0SApple OSS Distributions  *          ENOMEM: ustr_size is insufficient for the resulting string. In this
184*5c2921b0SApple OSS Distributions  *                  case the value returned in *ustr_len is invalid.
185*5c2921b0SApple OSS Distributions  */
186*5c2921b0SApple OSS Distributions int utf8_normalizeOptCaseFoldToUTF8(const char *str,
187*5c2921b0SApple OSS Distributions     size_t      str_len,
188*5c2921b0SApple OSS Distributions     bool        case_sens,
189*5c2921b0SApple OSS Distributions     char       *ustr,
190*5c2921b0SApple OSS Distributions     size_t      ustr_size,
191*5c2921b0SApple OSS Distributions     size_t     *ustr_len);
192*5c2921b0SApple OSS Distributions 
193*5c2921b0SApple OSS Distributions /*
194*5c2921b0SApple OSS Distributions  * utf8_normalizeOptCaseFoldAndMatchSubstring
195*5c2921b0SApple OSS Distributions  *
196*5c2921b0SApple OSS Distributions  * Determine whether the normalized UTF32 string derived from a specified UTF-8 string
197*5c2921b0SApple OSS Distributions  * strA contains another UTF32 string ustrB which has already been normalized, typically
198*5c2921b0SApple OSS Distributions  * with normalizeOptCaseFold. The normalization for both strings is one of the following,
199*5c2921b0SApple OSS Distributions  * as specified by the case_sens parameter:
200*5c2921b0SApple OSS Distributions  * - "canonical caseless form" (case-folded NFD); for case-insensitive comparison.
201*5c2921b0SApple OSS Distributions  * - standard NFD; for case-sensitive comparison (if case_sens = true).
202*5c2921b0SApple OSS Distributions  * On success, sets are_equal to true if strA contains ustrB, or false otherwise.
203*5c2921b0SApple OSS Distributions  *
204*5c2921b0SApple OSS Distributions  * The input string strA should be valid UTF-8 that meets the criteria for stream safe
205*5c2921b0SApple OSS Distributions  * text as described in http://unicode.org/reports/tr15/#Stream_Safe_Text_Format.
206*5c2921b0SApple OSS Distributions  * It should not contain ASCII 0x00 or '/'.
207*5c2921b0SApple OSS Distributions  *
208*5c2921b0SApple OSS Distributions  * strA:      A UTF-8 string (need not be 0 terminated) in which to search for the
209*5c2921b0SApple OSS Distributions  *            substring specified by ustrB.
210*5c2921b0SApple OSS Distributions  * strA_len:  The byte length of strA (excluding any 0 terminator)
211*5c2921b0SApple OSS Distributions  * ustrB:     A normalized UTF-32 substring (need not be 0 terminated) to be searched
212*5c2921b0SApple OSS Distributions  *            for in the UTF-32 string resulting from converting strA to the normalized
213*5c2921b0SApple OSS Distributions  *            UTF-32 form specified by the case_sens parameter; ustrB must already be
214*5c2921b0SApple OSS Distributions  *            in that form. Normally this will be produced using normalizeOptCaseFold.
215*5c2921b0SApple OSS Distributions  * ustrB_len: The length of ustrB in UTF-32 units (excluding any 0 terminator).
216*5c2921b0SApple OSS Distributions  * case_sens: False for case-insensitive matching; compares canonical caseless forms.
217*5c2921b0SApple OSS Distributions  *            True for case-sensitive matching; compares standard NFD forms.
218*5c2921b0SApple OSS Distributions  * buf:       Pointer to caller-supplied working memory for storing the portion of
219*5c2921b0SApple OSS Distributions  *            strA which has been converted to normalized UTF-32.
220*5c2921b0SApple OSS Distributions  * buf_size:  The size of buf.
221*5c2921b0SApple OSS Distributions  * has_match: On success, set to true if strA (when converter to UTF-32 and normalized
222*5c2921b0SApple OSS Distributions  *            per case_sens) contains ustrB, set to false otherwise.
223*5c2921b0SApple OSS Distributions  *
224*5c2921b0SApple OSS Distributions  * Returns: 0 on success, or
225*5c2921b0SApple OSS Distributions  *          EILSEQ: strA contains illegal ASCII-range characters (0x00 or '/'), or is
226*5c2921b0SApple OSS Distributions  *                  not well-formed stream-safe UTF-8, or contains codepoints that are
227*5c2921b0SApple OSS Distributions  *                  non-characters or unassigned in the version of Unicode currently
228*5c2921b0SApple OSS Distributions  *                  supported.
229*5c2921b0SApple OSS Distributions  *                  Note: The search may terminate early when a match is detected, and
230*5c2921b0SApple OSS Distributions  *                        may return 0 and set *has_match=true even if strA is invalid.
231*5c2921b0SApple OSS Distributions  *          ENOMEM: buf_size is insufficient.
232*5c2921b0SApple OSS Distributions  */
233*5c2921b0SApple OSS Distributions int utf8_normalizeOptCaseFoldAndMatchSubstring(const char    *strA,
234*5c2921b0SApple OSS Distributions     size_t         strA_len,
235*5c2921b0SApple OSS Distributions     const int32_t *ustrB,
236*5c2921b0SApple OSS Distributions     int32_t        ustrB_len,
237*5c2921b0SApple OSS Distributions     bool           case_sens,
238*5c2921b0SApple OSS Distributions     void          *buf,
239*5c2921b0SApple OSS Distributions     size_t         buf_size,
240*5c2921b0SApple OSS Distributions     bool          *has_match);
241*5c2921b0SApple OSS Distributions 
242*5c2921b0SApple OSS Distributions /*
243*5c2921b0SApple OSS Distributions  * utf8_normalizeOptCaseFoldGetUVersion
244*5c2921b0SApple OSS Distributions  *
245*5c2921b0SApple OSS Distributions  * Get the Unicode and code version currently associated with the normalizeOptCaseFold
246*5c2921b0SApple OSS Distributions  * functions. The caller allocates the version array and passes it to the function,
247*5c2921b0SApple OSS Distributions  * which will fill out the array as follows:
248*5c2921b0SApple OSS Distributions  * version[0] = Unicode major version; for Unicode 6.3.0 this would be 6
249*5c2921b0SApple OSS Distributions  * version[1] = Unicode minor version; for Unicode 6.3.0 this would be 3
250*5c2921b0SApple OSS Distributions  * version[2] = Unicode patch version; for Unicode 6.3.0 this would be 0
251*5c2921b0SApple OSS Distributions  * version[3] = Code revision level; for any given Unicode version, this value starts
252*5c2921b0SApple OSS Distributions  *              at 0 and is incremented for each significant revision to the
253*5c2921b0SApple OSS Distributions  *              normalizeOptCaseFold functions.
254*5c2921b0SApple OSS Distributions  */
255*5c2921b0SApple OSS Distributions void utf8_normalizeOptCaseFoldGetUVersion(unsigned char version[4]);
256*5c2921b0SApple OSS Distributions 
257*5c2921b0SApple OSS Distributions #endif /* KERNEL_PRIVATE */
258*5c2921b0SApple OSS Distributions 
259*5c2921b0SApple OSS Distributions #endif  /* unicode_h */
260