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