1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 ***************************************************************************
5 * Copyright (C) 2008-2015, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 ***************************************************************************
8 * file name: uspoof.cpp
9 * encoding: UTF-8
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2008Feb13
14 * created by: Andy Heninger
15 *
16 * Unicode Spoof Detection
17 */
18 #include "unicode/ubidi.h"
19 #include "unicode/utypes.h"
20 #include "unicode/normalizer2.h"
21 #include "unicode/uspoof.h"
22 #include "unicode/ustring.h"
23 #include "unicode/utf16.h"
24 #include "cmemory.h"
25 #include "cstring.h"
26 #include "mutex.h"
27 #include "scriptset.h"
28 #include "uassert.h"
29 #include "ucln_in.h"
30 #include "uspoof_impl.h"
31 #include "umutex.h"
32
33
34 #if !UCONFIG_NO_NORMALIZATION
35
36 U_NAMESPACE_USE
37
38
39 //
40 // Static Objects used by the spoof impl, their thread safe initialization and their cleanup.
41 //
42 static UnicodeSet *gInclusionSet = nullptr;
43 static UnicodeSet *gRecommendedSet = nullptr;
44 static const Normalizer2 *gNfdNormalizer = nullptr;
45 static UInitOnce gSpoofInitStaticsOnce {};
46
47 namespace {
48
49 UBool U_CALLCONV
uspoof_cleanup()50 uspoof_cleanup() {
51 delete gInclusionSet;
52 gInclusionSet = nullptr;
53 delete gRecommendedSet;
54 gRecommendedSet = nullptr;
55 gNfdNormalizer = nullptr;
56 gSpoofInitStaticsOnce.reset();
57 return true;
58 }
59
initializeStatics(UErrorCode & status)60 void U_CALLCONV initializeStatics(UErrorCode &status) {
61 static const char16_t *inclusionPat =
62 u"['\\-.\\:\\u00B7\\u0375\\u058A\\u05F3\\u05F4\\u06FD\\u06FE\\u0F0B\\u2010"
63 u"\\u2019\\u2027\\u30A0\\u30FB]";
64 gInclusionSet = new UnicodeSet(UnicodeString(inclusionPat), status);
65 if (gInclusionSet == nullptr) {
66 status = U_MEMORY_ALLOCATION_ERROR;
67 return;
68 }
69 gInclusionSet->freeze();
70
71 // Note: data from IdentifierStatus.txt & IdentifierType.txt
72 // There is tooling to generate this constant in the unicodetools project:
73 // org.unicode.text.tools.RecommendedSetGenerator
74 // It will print the Java and C++ code to the console for easy copy-paste into this file.
75 static const char16_t *recommendedPat =
76 u"[0-9A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u0131\\u0134-\\u013E"
77 u"\\u0141-\\u0148\\u014A-\\u017E\\u018F\\u01A0\\u01A1\\u01AF\\u01B0\\u01CD-"
78 u"\\u01DC\\u01DE-\\u01E3\\u01E6-\\u01F0\\u01F4\\u01F5\\u01F8-\\u021B\\u021E"
79 u"\\u021F\\u0226-\\u0233\\u0259\\u02BB\\u02BC\\u02EC\\u0300-\\u0304\\u0306-"
80 u"\\u030C\\u030F-\\u0311\\u0313\\u0314\\u031B\\u0323-\\u0328\\u032D\\u032E"
81 u"\\u0330\\u0331\\u0335\\u0338\\u0339\\u0342\\u0345\\u037B-\\u037D\\u0386"
82 u"\\u0388-\\u038A\\u038C\\u038E-\\u03A1\\u03A3-\\u03CE\\u03FC-\\u045F\\u048A-"
83 u"\\u04FF\\u0510-\\u0529\\u052E\\u052F\\u0531-\\u0556\\u0559\\u0561-\\u0586"
84 u"\\u05B4\\u05D0-\\u05EA\\u05EF-\\u05F2\\u0620-\\u063F\\u0641-\\u0655\\u0660-"
85 u"\\u0669\\u0670-\\u0672\\u0674\\u0679-\\u068D\\u068F-\\u06A0\\u06A2-\\u06D3"
86 u"\\u06D5\\u06E5\\u06E6\\u06EE-\\u06FC\\u06FF\\u0750-\\u07B1\\u0870-\\u0887"
87 u"\\u0889-\\u088E\\u08A0-\\u08AC\\u08B2\\u08B5-\\u08C9\\u0901-\\u094D\\u094F"
88 u"\\u0950\\u0956\\u0957\\u0960-\\u0963\\u0966-\\u096F\\u0971-\\u0977\\u0979-"
89 u"\\u097F\\u0981-\\u0983\\u0985-\\u098C\\u098F\\u0990\\u0993-\\u09A8\\u09AA-"
90 u"\\u09B0\\u09B2\\u09B6-\\u09B9\\u09BC-\\u09C4\\u09C7\\u09C8\\u09CB-\\u09CE"
91 u"\\u09D7\\u09E0-\\u09E3\\u09E6-\\u09F1\\u09FE\\u0A01-\\u0A03\\u0A05-\\u0A0A"
92 u"\\u0A0F\\u0A10\\u0A13-\\u0A28\\u0A2A-\\u0A30\\u0A32\\u0A35\\u0A38\\u0A39"
93 u"\\u0A3C\\u0A3E-\\u0A42\\u0A47\\u0A48\\u0A4B-\\u0A4D\\u0A5C\\u0A66-\\u0A74"
94 u"\\u0A81-\\u0A83\\u0A85-\\u0A8D\\u0A8F-\\u0A91\\u0A93-\\u0AA8\\u0AAA-\\u0AB0"
95 u"\\u0AB2\\u0AB3\\u0AB5-\\u0AB9\\u0ABC-\\u0AC5\\u0AC7-\\u0AC9\\u0ACB-\\u0ACD"
96 u"\\u0AD0\\u0AE0-\\u0AE3\\u0AE6-\\u0AEF\\u0AFA-\\u0AFF\\u0B01-\\u0B03\\u0B05-"
97 u"\\u0B0C\\u0B0F\\u0B10\\u0B13-\\u0B28\\u0B2A-\\u0B30\\u0B32\\u0B33\\u0B35-"
98 u"\\u0B39\\u0B3C-\\u0B43\\u0B47\\u0B48\\u0B4B-\\u0B4D\\u0B55-\\u0B57\\u0B5F-"
99 u"\\u0B61\\u0B66-\\u0B6F\\u0B71\\u0B82\\u0B83\\u0B85-\\u0B8A\\u0B8E-\\u0B90"
100 u"\\u0B92-\\u0B95\\u0B99\\u0B9A\\u0B9C\\u0B9E\\u0B9F\\u0BA3\\u0BA4\\u0BA8-"
101 u"\\u0BAA\\u0BAE-\\u0BB9\\u0BBE-\\u0BC2\\u0BC6-\\u0BC8\\u0BCA-\\u0BCD\\u0BD0"
102 u"\\u0BD7\\u0BE6-\\u0BEF\\u0C01-\\u0C0C\\u0C0E-\\u0C10\\u0C12-\\u0C28\\u0C2A-"
103 u"\\u0C33\\u0C35-\\u0C39\\u0C3C-\\u0C44\\u0C46-\\u0C48\\u0C4A-\\u0C4D\\u0C55"
104 u"\\u0C56\\u0C5D\\u0C60\\u0C61\\u0C66-\\u0C6F\\u0C80\\u0C82\\u0C83\\u0C85-"
105 u"\\u0C8C\\u0C8E-\\u0C90\\u0C92-\\u0CA8\\u0CAA-\\u0CB3\\u0CB5-\\u0CB9\\u0CBC-"
106 u"\\u0CC4\\u0CC6-\\u0CC8\\u0CCA-\\u0CCD\\u0CD5\\u0CD6\\u0CDD\\u0CE0-\\u0CE3"
107 u"\\u0CE6-\\u0CEF\\u0CF1-\\u0CF3\\u0D00\\u0D02\\u0D03\\u0D05-\\u0D0C\\u0D0E-"
108 u"\\u0D10\\u0D12-\\u0D3A\\u0D3D-\\u0D43\\u0D46-\\u0D48\\u0D4A-\\u0D4E\\u0D54-"
109 u"\\u0D57\\u0D60\\u0D61\\u0D66-\\u0D6F\\u0D7A-\\u0D7F\\u0D82\\u0D83\\u0D85-"
110 u"\\u0D8E\\u0D91-\\u0D96\\u0D9A-\\u0DA5\\u0DA7-\\u0DB1\\u0DB3-\\u0DBB\\u0DBD"
111 u"\\u0DC0-\\u0DC6\\u0DCA\\u0DCF-\\u0DD4\\u0DD6\\u0DD8-\\u0DDE\\u0DF2\\u0E01-"
112 u"\\u0E32\\u0E34-\\u0E3A\\u0E40-\\u0E4E\\u0E50-\\u0E59\\u0E81\\u0E82\\u0E84"
113 u"\\u0E86-\\u0E8A\\u0E8C-\\u0EA3\\u0EA5\\u0EA7-\\u0EB2\\u0EB4-\\u0EBD\\u0EC0-"
114 u"\\u0EC4\\u0EC6\\u0EC8-\\u0ECE\\u0ED0-\\u0ED9\\u0EDE\\u0EDF\\u0F00\\u0F20-"
115 u"\\u0F29\\u0F35\\u0F37\\u0F3E-\\u0F42\\u0F44-\\u0F47\\u0F49-\\u0F4C\\u0F4E-"
116 u"\\u0F51\\u0F53-\\u0F56\\u0F58-\\u0F5B\\u0F5D-\\u0F68\\u0F6A-\\u0F6C\\u0F71"
117 u"\\u0F72\\u0F74\\u0F7A-\\u0F80\\u0F82-\\u0F84\\u0F86-\\u0F92\\u0F94-\\u0F97"
118 u"\\u0F99-\\u0F9C\\u0F9E-\\u0FA1\\u0FA3-\\u0FA6\\u0FA8-\\u0FAB\\u0FAD-\\u0FB8"
119 u"\\u0FBA-\\u0FBC\\u0FC6\\u1000-\\u1049\\u1050-\\u109D\\u10C7\\u10CD\\u10D0-"
120 u"\\u10F0\\u10F7-\\u10FA\\u10FD-\\u10FF\\u1200-\\u1248\\u124A-\\u124D\\u1250-"
121 u"\\u1256\\u1258\\u125A-\\u125D\\u1260-\\u1288\\u128A-\\u128D\\u1290-\\u12B0"
122 u"\\u12B2-\\u12B5\\u12B8-\\u12BE\\u12C0\\u12C2-\\u12C5\\u12C8-\\u12D6\\u12D8-"
123 u"\\u1310\\u1312-\\u1315\\u1318-\\u135A\\u135D-\\u135F\\u1380-\\u138F\\u1780-"
124 u"\\u17A2\\u17A5-\\u17A7\\u17A9-\\u17B3\\u17B6-\\u17CD\\u17D0\\u17D2\\u17D7"
125 u"\\u17DC\\u17E0-\\u17E9\\u1C90-\\u1CBA\\u1CBD-\\u1CBF\\u1E00-\\u1E99\\u1E9E"
126 u"\\u1EA0-\\u1EF9\\u1F00-\\u1F15\\u1F18-\\u1F1D\\u1F20-\\u1F45\\u1F48-\\u1F4D"
127 u"\\u1F50-\\u1F57\\u1F59\\u1F5B\\u1F5D\\u1F5F-\\u1F70\\u1F72\\u1F74\\u1F76"
128 u"\\u1F78\\u1F7A\\u1F7C\\u1F80-\\u1FB4\\u1FB6-\\u1FBA\\u1FBC\\u1FC2-\\u1FC4"
129 u"\\u1FC6-\\u1FC8\\u1FCA\\u1FCC\\u1FD0-\\u1FD2\\u1FD6-\\u1FDA\\u1FE0-\\u1FE2"
130 u"\\u1FE4-\\u1FEA\\u1FEC\\u1FF2-\\u1FF4\\u1FF6-\\u1FF8\\u1FFA\\u1FFC\\u2D27"
131 u"\\u2D2D\\u2D80-\\u2D96\\u2DA0-\\u2DA6\\u2DA8-\\u2DAE\\u2DB0-\\u2DB6\\u2DB8-"
132 u"\\u2DBE\\u2DC0-\\u2DC6\\u2DC8-\\u2DCE\\u2DD0-\\u2DD6\\u2DD8-\\u2DDE\\u3005-"
133 u"\\u3007\\u3041-\\u3096\\u3099\\u309A\\u309D\\u309E\\u30A1-\\u30FA\\u30FC-"
134 u"\\u30FE\\u3105-\\u312D\\u312F\\u31A0-\\u31BF\\u3400-\\u4DBF\\u4E00-\\u9FFF"
135 u"\\uA67F\\uA717-\\uA71F\\uA788\\uA78D\\uA792\\uA793\\uA7AA\\uA7C0-\\uA7CA"
136 u"\\uA7D0\\uA7D1\\uA7D3\\uA7D5-\\uA7D9\\uA9E7-\\uA9FE\\uAA60-\\uAA76\\uAA7A-"
137 u"\\uAA7F\\uAB01-\\uAB06\\uAB09-\\uAB0E\\uAB11-\\uAB16\\uAB20-\\uAB26\\uAB28-"
138 u"\\uAB2E\\uAB66\\uAB67\\uAC00-\\uD7A3\\uFA0E\\uFA0F\\uFA11\\uFA13\\uFA14"
139 u"\\uFA1F\\uFA21\\uFA23\\uFA24\\uFA27-\\uFA29\\U00011301\\U00011303"
140 u"\\U0001133B\\U0001133C\\U00016FF0\\U00016FF1\\U0001B11F-\\U0001B122"
141 u"\\U0001B132\\U0001B150-\\U0001B152\\U0001B155\\U0001B164-\\U0001B167"
142 u"\\U0001DF00-\\U0001DF1E\\U0001DF25-\\U0001DF2A\\U0001E08F\\U0001E7E0-"
143 u"\\U0001E7E6\\U0001E7E8-\\U0001E7EB\\U0001E7ED\\U0001E7EE\\U0001E7F0-"
144 u"\\U0001E7FE\\U00020000-\\U0002A6DF\\U0002A700-\\U0002B739\\U0002B740-"
145 u"\\U0002B81D\\U0002B820-\\U0002CEA1\\U0002CEB0-\\U0002EBE0\\U0002EBF0-"
146 u"\\U0002EE5D\\U00030000-\\U0003134A\\U00031350-\\U000323AF]";
147
148 gRecommendedSet = new UnicodeSet(UnicodeString(recommendedPat), status);
149 if (gRecommendedSet == nullptr) {
150 status = U_MEMORY_ALLOCATION_ERROR;
151 delete gInclusionSet;
152 return;
153 }
154 gRecommendedSet->freeze();
155 gNfdNormalizer = Normalizer2::getNFDInstance(status);
156 ucln_i18n_registerCleanup(UCLN_I18N_SPOOF, uspoof_cleanup);
157 }
158
159 } // namespace
160
uspoof_internalInitStatics(UErrorCode * status)161 U_CFUNC void uspoof_internalInitStatics(UErrorCode *status) {
162 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
163 }
164
165 U_CAPI USpoofChecker * U_EXPORT2
uspoof_open(UErrorCode * status)166 uspoof_open(UErrorCode *status) {
167 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
168 if (U_FAILURE(*status)) {
169 return nullptr;
170 }
171 SpoofImpl *si = new SpoofImpl(*status);
172 if (si == nullptr) {
173 *status = U_MEMORY_ALLOCATION_ERROR;
174 return nullptr;
175 }
176 if (U_FAILURE(*status)) {
177 delete si;
178 return nullptr;
179 }
180 return si->asUSpoofChecker();
181 }
182
183
184 U_CAPI USpoofChecker * U_EXPORT2
uspoof_openFromSerialized(const void * data,int32_t length,int32_t * pActualLength,UErrorCode * status)185 uspoof_openFromSerialized(const void *data, int32_t length, int32_t *pActualLength,
186 UErrorCode *status) {
187 if (U_FAILURE(*status)) {
188 return nullptr;
189 }
190
191 if (data == nullptr) {
192 *status = U_ILLEGAL_ARGUMENT_ERROR;
193 return nullptr;
194 }
195
196 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
197 if (U_FAILURE(*status))
198 {
199 return nullptr;
200 }
201
202 SpoofData *sd = new SpoofData(data, length, *status);
203 if (sd == nullptr) {
204 *status = U_MEMORY_ALLOCATION_ERROR;
205 return nullptr;
206 }
207
208 if (U_FAILURE(*status)) {
209 delete sd;
210 return nullptr;
211 }
212
213 SpoofImpl *si = new SpoofImpl(sd, *status);
214 if (si == nullptr) {
215 *status = U_MEMORY_ALLOCATION_ERROR;
216 delete sd; // explicit delete as the destructor for si won't be called.
217 return nullptr;
218 }
219
220 if (U_FAILURE(*status)) {
221 delete si; // no delete for sd, as the si destructor will delete it.
222 return nullptr;
223 }
224
225 if (pActualLength != nullptr) {
226 *pActualLength = sd->size();
227 }
228 return si->asUSpoofChecker();
229 }
230
231
232 U_CAPI USpoofChecker * U_EXPORT2
uspoof_clone(const USpoofChecker * sc,UErrorCode * status)233 uspoof_clone(const USpoofChecker *sc, UErrorCode *status) {
234 const SpoofImpl *src = SpoofImpl::validateThis(sc, *status);
235 if (src == nullptr) {
236 return nullptr;
237 }
238 SpoofImpl *result = new SpoofImpl(*src, *status); // copy constructor
239 if (result == nullptr) {
240 *status = U_MEMORY_ALLOCATION_ERROR;
241 return nullptr;
242 }
243 if (U_FAILURE(*status)) {
244 delete result;
245 result = nullptr;
246 }
247 return result->asUSpoofChecker();
248 }
249
250
251 U_CAPI void U_EXPORT2
uspoof_close(USpoofChecker * sc)252 uspoof_close(USpoofChecker *sc) {
253 UErrorCode status = U_ZERO_ERROR;
254 SpoofImpl *This = SpoofImpl::validateThis(sc, status);
255 delete This;
256 }
257
258
259 U_CAPI void U_EXPORT2
uspoof_setChecks(USpoofChecker * sc,int32_t checks,UErrorCode * status)260 uspoof_setChecks(USpoofChecker *sc, int32_t checks, UErrorCode *status) {
261 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
262 if (This == nullptr) {
263 return;
264 }
265
266 // Verify that the requested checks are all ones (bits) that
267 // are acceptable, known values.
268 if (checks & ~(USPOOF_ALL_CHECKS | USPOOF_AUX_INFO)) {
269 *status = U_ILLEGAL_ARGUMENT_ERROR;
270 return;
271 }
272
273 This->fChecks = checks;
274 }
275
276
277 U_CAPI int32_t U_EXPORT2
uspoof_getChecks(const USpoofChecker * sc,UErrorCode * status)278 uspoof_getChecks(const USpoofChecker *sc, UErrorCode *status) {
279 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
280 if (This == nullptr) {
281 return 0;
282 }
283 return This->fChecks;
284 }
285
286 U_CAPI void U_EXPORT2
uspoof_setRestrictionLevel(USpoofChecker * sc,URestrictionLevel restrictionLevel)287 uspoof_setRestrictionLevel(USpoofChecker *sc, URestrictionLevel restrictionLevel) {
288 UErrorCode status = U_ZERO_ERROR;
289 SpoofImpl *This = SpoofImpl::validateThis(sc, status);
290 if (This != nullptr) {
291 This->fRestrictionLevel = restrictionLevel;
292 This->fChecks |= USPOOF_RESTRICTION_LEVEL;
293 }
294 }
295
296 U_CAPI URestrictionLevel U_EXPORT2
uspoof_getRestrictionLevel(const USpoofChecker * sc)297 uspoof_getRestrictionLevel(const USpoofChecker *sc) {
298 UErrorCode status = U_ZERO_ERROR;
299 const SpoofImpl *This = SpoofImpl::validateThis(sc, status);
300 if (This == nullptr) {
301 return USPOOF_UNRESTRICTIVE;
302 }
303 return This->fRestrictionLevel;
304 }
305
306 U_CAPI void U_EXPORT2
uspoof_setAllowedLocales(USpoofChecker * sc,const char * localesList,UErrorCode * status)307 uspoof_setAllowedLocales(USpoofChecker *sc, const char *localesList, UErrorCode *status) {
308 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
309 if (This == nullptr) {
310 return;
311 }
312 This->setAllowedLocales(localesList, *status);
313 }
314
315 U_CAPI const char * U_EXPORT2
uspoof_getAllowedLocales(USpoofChecker * sc,UErrorCode * status)316 uspoof_getAllowedLocales(USpoofChecker *sc, UErrorCode *status) {
317 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
318 if (This == nullptr) {
319 return nullptr;
320 }
321 return This->getAllowedLocales(*status);
322 }
323
324
325 U_CAPI const USet * U_EXPORT2
uspoof_getAllowedChars(const USpoofChecker * sc,UErrorCode * status)326 uspoof_getAllowedChars(const USpoofChecker *sc, UErrorCode *status) {
327 const UnicodeSet *result = uspoof_getAllowedUnicodeSet(sc, status);
328 return result->toUSet();
329 }
330
331 U_CAPI const UnicodeSet * U_EXPORT2
uspoof_getAllowedUnicodeSet(const USpoofChecker * sc,UErrorCode * status)332 uspoof_getAllowedUnicodeSet(const USpoofChecker *sc, UErrorCode *status) {
333 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
334 if (This == nullptr) {
335 return nullptr;
336 }
337 return This->fAllowedCharsSet;
338 }
339
340
341 U_CAPI void U_EXPORT2
uspoof_setAllowedChars(USpoofChecker * sc,const USet * chars,UErrorCode * status)342 uspoof_setAllowedChars(USpoofChecker *sc, const USet *chars, UErrorCode *status) {
343 const UnicodeSet *set = UnicodeSet::fromUSet(chars);
344 uspoof_setAllowedUnicodeSet(sc, set, status);
345 }
346
347
348 U_CAPI void U_EXPORT2
uspoof_setAllowedUnicodeSet(USpoofChecker * sc,const UnicodeSet * chars,UErrorCode * status)349 uspoof_setAllowedUnicodeSet(USpoofChecker *sc, const UnicodeSet *chars, UErrorCode *status) {
350 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
351 if (This == nullptr) {
352 return;
353 }
354 if (chars->isBogus()) {
355 *status = U_ILLEGAL_ARGUMENT_ERROR;
356 return;
357 }
358 UnicodeSet *clonedSet = chars->clone();
359 if (clonedSet == nullptr || clonedSet->isBogus()) {
360 *status = U_MEMORY_ALLOCATION_ERROR;
361 return;
362 }
363 clonedSet->freeze();
364 delete This->fAllowedCharsSet;
365 This->fAllowedCharsSet = clonedSet;
366 This->fChecks |= USPOOF_CHAR_LIMIT;
367 }
368
369
370 U_CAPI int32_t U_EXPORT2
uspoof_check(const USpoofChecker * sc,const char16_t * id,int32_t length,int32_t * position,UErrorCode * status)371 uspoof_check(const USpoofChecker *sc,
372 const char16_t *id, int32_t length,
373 int32_t *position,
374 UErrorCode *status) {
375
376 // Backwards compatibility:
377 if (position != nullptr) {
378 *position = 0;
379 }
380
381 // Delegate to uspoof_check2
382 return uspoof_check2(sc, id, length, nullptr, status);
383 }
384
385
386 U_CAPI int32_t U_EXPORT2
uspoof_check2(const USpoofChecker * sc,const char16_t * id,int32_t length,USpoofCheckResult * checkResult,UErrorCode * status)387 uspoof_check2(const USpoofChecker *sc,
388 const char16_t* id, int32_t length,
389 USpoofCheckResult* checkResult,
390 UErrorCode *status) {
391
392 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
393 if (This == nullptr) {
394 return 0;
395 }
396 if (length < -1) {
397 *status = U_ILLEGAL_ARGUMENT_ERROR;
398 return 0;
399 }
400 UnicodeString idStr((length == -1), id, length); // Aliasing constructor.
401 int32_t result = uspoof_check2UnicodeString(sc, idStr, checkResult, status);
402 return result;
403 }
404
405
406 U_CAPI int32_t U_EXPORT2
uspoof_checkUTF8(const USpoofChecker * sc,const char * id,int32_t length,int32_t * position,UErrorCode * status)407 uspoof_checkUTF8(const USpoofChecker *sc,
408 const char *id, int32_t length,
409 int32_t *position,
410 UErrorCode *status) {
411
412 // Backwards compatibility:
413 if (position != nullptr) {
414 *position = 0;
415 }
416
417 // Delegate to uspoof_check2
418 return uspoof_check2UTF8(sc, id, length, nullptr, status);
419 }
420
421
422 U_CAPI int32_t U_EXPORT2
uspoof_check2UTF8(const USpoofChecker * sc,const char * id,int32_t length,USpoofCheckResult * checkResult,UErrorCode * status)423 uspoof_check2UTF8(const USpoofChecker *sc,
424 const char *id, int32_t length,
425 USpoofCheckResult* checkResult,
426 UErrorCode *status) {
427
428 if (U_FAILURE(*status)) {
429 return 0;
430 }
431 UnicodeString idStr = UnicodeString::fromUTF8(StringPiece(id, length>=0 ? length : static_cast<int32_t>(uprv_strlen(id))));
432 int32_t result = uspoof_check2UnicodeString(sc, idStr, checkResult, status);
433 return result;
434 }
435
436
437 U_CAPI int32_t U_EXPORT2
uspoof_areConfusable(const USpoofChecker * sc,const char16_t * id1,int32_t length1,const char16_t * id2,int32_t length2,UErrorCode * status)438 uspoof_areConfusable(const USpoofChecker *sc,
439 const char16_t *id1, int32_t length1,
440 const char16_t *id2, int32_t length2,
441 UErrorCode *status) {
442 SpoofImpl::validateThis(sc, *status);
443 if (U_FAILURE(*status)) {
444 return 0;
445 }
446 if (length1 < -1 || length2 < -1) {
447 *status = U_ILLEGAL_ARGUMENT_ERROR;
448 return 0;
449 }
450
451 UnicodeString id1Str((length1==-1), id1, length1); // Aliasing constructor
452 UnicodeString id2Str((length2==-1), id2, length2); // Aliasing constructor
453 return uspoof_areConfusableUnicodeString(sc, id1Str, id2Str, status);
454 }
455
456
457 U_CAPI int32_t U_EXPORT2
uspoof_areConfusableUTF8(const USpoofChecker * sc,const char * id1,int32_t length1,const char * id2,int32_t length2,UErrorCode * status)458 uspoof_areConfusableUTF8(const USpoofChecker *sc,
459 const char *id1, int32_t length1,
460 const char *id2, int32_t length2,
461 UErrorCode *status) {
462 SpoofImpl::validateThis(sc, *status);
463 if (U_FAILURE(*status)) {
464 return 0;
465 }
466 if (length1 < -1 || length2 < -1) {
467 *status = U_ILLEGAL_ARGUMENT_ERROR;
468 return 0;
469 }
470 UnicodeString id1Str = UnicodeString::fromUTF8(StringPiece(id1, length1>=0? length1 : static_cast<int32_t>(uprv_strlen(id1))));
471 UnicodeString id2Str = UnicodeString::fromUTF8(StringPiece(id2, length2>=0? length2 : static_cast<int32_t>(uprv_strlen(id2))));
472 int32_t results = uspoof_areConfusableUnicodeString(sc, id1Str, id2Str, status);
473 return results;
474 }
475
476
477 U_CAPI int32_t U_EXPORT2
uspoof_areConfusableUnicodeString(const USpoofChecker * sc,const icu::UnicodeString & id1,const icu::UnicodeString & id2,UErrorCode * status)478 uspoof_areConfusableUnicodeString(const USpoofChecker *sc,
479 const icu::UnicodeString &id1,
480 const icu::UnicodeString &id2,
481 UErrorCode *status) {
482 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
483 if (U_FAILURE(*status)) {
484 return 0;
485 }
486 //
487 // See section 4 of UAX 39 for the algorithm for checking whether two strings are confusable,
488 // and for definitions of the types (single, whole, mixed-script) of confusables.
489
490 // We only care about a few of the check flags. Ignore the others.
491 // If no tests relevant to this function have been specified, return an error.
492 // TODO: is this really the right thing to do? It's probably an error on the caller's part,
493 // but logically we would just return 0 (no error).
494 if ((This->fChecks & USPOOF_CONFUSABLE) == 0) {
495 *status = U_INVALID_STATE_ERROR;
496 return 0;
497 }
498
499 // Compute the skeletons and check for confusability.
500 UnicodeString id1Skeleton;
501 uspoof_getSkeletonUnicodeString(sc, 0 /* deprecated */, id1, id1Skeleton, status);
502 UnicodeString id2Skeleton;
503 uspoof_getSkeletonUnicodeString(sc, 0 /* deprecated */, id2, id2Skeleton, status);
504 if (U_FAILURE(*status)) { return 0; }
505 if (id1Skeleton != id2Skeleton) {
506 return 0;
507 }
508
509 // If we get here, the strings are confusable. Now we just need to set the flags for the appropriate classes
510 // of confusables according to UTS 39 section 4.
511 // Start by computing the resolved script sets of id1 and id2.
512 ScriptSet id1RSS;
513 This->getResolvedScriptSet(id1, id1RSS, *status);
514 ScriptSet id2RSS;
515 This->getResolvedScriptSet(id2, id2RSS, *status);
516
517 // Turn on all applicable flags
518 int32_t result = 0;
519 if (id1RSS.intersects(id2RSS)) {
520 result |= USPOOF_SINGLE_SCRIPT_CONFUSABLE;
521 } else {
522 result |= USPOOF_MIXED_SCRIPT_CONFUSABLE;
523 if (!id1RSS.isEmpty() && !id2RSS.isEmpty()) {
524 result |= USPOOF_WHOLE_SCRIPT_CONFUSABLE;
525 }
526 }
527
528 // Turn off flags that the user doesn't want
529 if ((This->fChecks & USPOOF_SINGLE_SCRIPT_CONFUSABLE) == 0) {
530 result &= ~USPOOF_SINGLE_SCRIPT_CONFUSABLE;
531 }
532 if ((This->fChecks & USPOOF_MIXED_SCRIPT_CONFUSABLE) == 0) {
533 result &= ~USPOOF_MIXED_SCRIPT_CONFUSABLE;
534 }
535 if ((This->fChecks & USPOOF_WHOLE_SCRIPT_CONFUSABLE) == 0) {
536 result &= ~USPOOF_WHOLE_SCRIPT_CONFUSABLE;
537 }
538
539 return result;
540 }
541
uspoof_areBidiConfusable(const USpoofChecker * sc,UBiDiDirection direction,const char16_t * id1,int32_t length1,const char16_t * id2,int32_t length2,UErrorCode * status)542 U_CAPI uint32_t U_EXPORT2 uspoof_areBidiConfusable(const USpoofChecker *sc, UBiDiDirection direction,
543 const char16_t *id1, int32_t length1,
544 const char16_t *id2, int32_t length2,
545 UErrorCode *status) {
546 UnicodeString id1Str((length1 == -1), id1, length1); // Aliasing constructor
547 UnicodeString id2Str((length2 == -1), id2, length2); // Aliasing constructor
548 if (id1Str.isBogus() || id2Str.isBogus()) {
549 *status = U_ILLEGAL_ARGUMENT_ERROR;
550 return 0;
551 }
552 return uspoof_areBidiConfusableUnicodeString(sc, direction, id1Str, id2Str, status);
553 }
554
uspoof_areBidiConfusableUTF8(const USpoofChecker * sc,UBiDiDirection direction,const char * id1,int32_t length1,const char * id2,int32_t length2,UErrorCode * status)555 U_CAPI uint32_t U_EXPORT2 uspoof_areBidiConfusableUTF8(const USpoofChecker *sc, UBiDiDirection direction,
556 const char *id1, int32_t length1, const char *id2,
557 int32_t length2, UErrorCode *status) {
558 if (length1 < -1 || length2 < -1) {
559 *status = U_ILLEGAL_ARGUMENT_ERROR;
560 return 0;
561 }
562 UnicodeString id1Str = UnicodeString::fromUTF8(
563 StringPiece(id1, length1 >= 0 ? length1 : static_cast<int32_t>(uprv_strlen(id1))));
564 UnicodeString id2Str = UnicodeString::fromUTF8(
565 StringPiece(id2, length2 >= 0 ? length2 : static_cast<int32_t>(uprv_strlen(id2))));
566 return uspoof_areBidiConfusableUnicodeString(sc, direction, id1Str, id2Str, status);
567 }
568
uspoof_areBidiConfusableUnicodeString(const USpoofChecker * sc,UBiDiDirection direction,const icu::UnicodeString & id1,const icu::UnicodeString & id2,UErrorCode * status)569 U_CAPI uint32_t U_EXPORT2 uspoof_areBidiConfusableUnicodeString(const USpoofChecker *sc,
570 UBiDiDirection direction,
571 const icu::UnicodeString &id1,
572 const icu::UnicodeString &id2,
573 UErrorCode *status) {
574 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
575 if (U_FAILURE(*status)) {
576 return 0;
577 }
578 //
579 // See section 4 of UTS 39 for the algorithm for checking whether two strings are confusable,
580 // and for definitions of the types (single, whole, mixed-script) of confusables.
581
582 // We only care about a few of the check flags. Ignore the others.
583 // If no tests relevant to this function have been specified, return an error.
584 // TODO: is this really the right thing to do? It's probably an error on the caller's part,
585 // but logically we would just return 0 (no error).
586 if ((This->fChecks & USPOOF_CONFUSABLE) == 0) {
587 *status = U_INVALID_STATE_ERROR;
588 return 0;
589 }
590
591 // Compute the skeletons and check for confusability.
592 UnicodeString id1Skeleton;
593 uspoof_getBidiSkeletonUnicodeString(sc, direction, id1, id1Skeleton, status);
594 UnicodeString id2Skeleton;
595 uspoof_getBidiSkeletonUnicodeString(sc, direction, id2, id2Skeleton, status);
596 if (U_FAILURE(*status)) {
597 return 0;
598 }
599 if (id1Skeleton != id2Skeleton) {
600 return 0;
601 }
602
603 // If we get here, the strings are confusable. Now we just need to set the flags for the appropriate
604 // classes of confusables according to UTS 39 section 4. Start by computing the resolved script sets
605 // of id1 and id2.
606 ScriptSet id1RSS;
607 This->getResolvedScriptSet(id1, id1RSS, *status);
608 ScriptSet id2RSS;
609 This->getResolvedScriptSet(id2, id2RSS, *status);
610
611 // Turn on all applicable flags
612 uint32_t result = 0;
613 if (id1RSS.intersects(id2RSS)) {
614 result |= USPOOF_SINGLE_SCRIPT_CONFUSABLE;
615 } else {
616 result |= USPOOF_MIXED_SCRIPT_CONFUSABLE;
617 if (!id1RSS.isEmpty() && !id2RSS.isEmpty()) {
618 result |= USPOOF_WHOLE_SCRIPT_CONFUSABLE;
619 }
620 }
621
622 // Turn off flags that the user doesn't want
623 return result & This->fChecks;
624 }
625
626
627 U_CAPI int32_t U_EXPORT2
uspoof_checkUnicodeString(const USpoofChecker * sc,const icu::UnicodeString & id,int32_t * position,UErrorCode * status)628 uspoof_checkUnicodeString(const USpoofChecker *sc,
629 const icu::UnicodeString &id,
630 int32_t *position,
631 UErrorCode *status) {
632
633 // Backwards compatibility:
634 if (position != nullptr) {
635 *position = 0;
636 }
637
638 // Delegate to uspoof_check2
639 return uspoof_check2UnicodeString(sc, id, nullptr, status);
640 }
641
642 namespace {
643
checkImpl(const SpoofImpl * This,const UnicodeString & id,CheckResult * checkResult,UErrorCode * status)644 int32_t checkImpl(const SpoofImpl* This, const UnicodeString& id, CheckResult* checkResult, UErrorCode* status) {
645 U_ASSERT(This != nullptr);
646 U_ASSERT(checkResult != nullptr);
647 checkResult->clear();
648 int32_t result = 0;
649
650 if (0 != (This->fChecks & USPOOF_RESTRICTION_LEVEL)) {
651 URestrictionLevel idRestrictionLevel = This->getRestrictionLevel(id, *status);
652 if (idRestrictionLevel > This->fRestrictionLevel) {
653 result |= USPOOF_RESTRICTION_LEVEL;
654 }
655 checkResult->fRestrictionLevel = idRestrictionLevel;
656 }
657
658 if (0 != (This->fChecks & USPOOF_MIXED_NUMBERS)) {
659 UnicodeSet numerics;
660 This->getNumerics(id, numerics, *status);
661 if (numerics.size() > 1) {
662 result |= USPOOF_MIXED_NUMBERS;
663 }
664 checkResult->fNumerics = numerics; // UnicodeSet::operator=
665 }
666
667 if (0 != (This->fChecks & USPOOF_HIDDEN_OVERLAY)) {
668 int32_t index = This->findHiddenOverlay(id, *status);
669 if (index != -1) {
670 result |= USPOOF_HIDDEN_OVERLAY;
671 }
672 }
673
674
675 if (0 != (This->fChecks & USPOOF_CHAR_LIMIT)) {
676 int32_t i;
677 UChar32 c;
678 int32_t length = id.length();
679 for (i=0; i<length ;) {
680 c = id.char32At(i);
681 i += U16_LENGTH(c);
682 if (!This->fAllowedCharsSet->contains(c)) {
683 result |= USPOOF_CHAR_LIMIT;
684 break;
685 }
686 }
687 }
688
689 if (0 != (This->fChecks & USPOOF_INVISIBLE)) {
690 // This check needs to be done on NFD input
691 UnicodeString nfdText;
692 gNfdNormalizer->normalize(id, nfdText, *status);
693 int32_t nfdLength = nfdText.length();
694
695 // scan for more than one occurrence of the same non-spacing mark
696 // in a sequence of non-spacing marks.
697 int32_t i;
698 UChar32 c;
699 UChar32 firstNonspacingMark = 0;
700 UBool haveMultipleMarks = false;
701 UnicodeSet marksSeenSoFar; // Set of combining marks in a single combining sequence.
702
703 for (i=0; i<nfdLength ;) {
704 c = nfdText.char32At(i);
705 i += U16_LENGTH(c);
706 if (u_charType(c) != U_NON_SPACING_MARK) {
707 firstNonspacingMark = 0;
708 if (haveMultipleMarks) {
709 marksSeenSoFar.clear();
710 haveMultipleMarks = false;
711 }
712 continue;
713 }
714 if (firstNonspacingMark == 0) {
715 firstNonspacingMark = c;
716 continue;
717 }
718 if (!haveMultipleMarks) {
719 marksSeenSoFar.add(firstNonspacingMark);
720 haveMultipleMarks = true;
721 }
722 if (marksSeenSoFar.contains(c)) {
723 // report the error, and stop scanning.
724 // No need to find more than the first failure.
725 result |= USPOOF_INVISIBLE;
726 break;
727 }
728 marksSeenSoFar.add(c);
729 }
730 }
731
732 checkResult->fChecks = result;
733 return checkResult->toCombinedBitmask(This->fChecks);
734 }
735
736 } // namespace
737
738 U_CAPI int32_t U_EXPORT2
uspoof_check2UnicodeString(const USpoofChecker * sc,const icu::UnicodeString & id,USpoofCheckResult * checkResult,UErrorCode * status)739 uspoof_check2UnicodeString(const USpoofChecker *sc,
740 const icu::UnicodeString &id,
741 USpoofCheckResult* checkResult,
742 UErrorCode *status) {
743 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
744 if (This == nullptr) {
745 return false;
746 }
747
748 if (checkResult != nullptr) {
749 CheckResult* ThisCheckResult = CheckResult::validateThis(checkResult, *status);
750 if (ThisCheckResult == nullptr) {
751 return false;
752 }
753 return checkImpl(This, id, ThisCheckResult, status);
754 } else {
755 // Stack-allocate the checkResult since this method doesn't return it
756 CheckResult stackCheckResult;
757 return checkImpl(This, id, &stackCheckResult, status);
758 }
759 }
760
761
762 U_CAPI int32_t U_EXPORT2
uspoof_getSkeleton(const USpoofChecker * sc,uint32_t type,const char16_t * id,int32_t length,char16_t * dest,int32_t destCapacity,UErrorCode * status)763 uspoof_getSkeleton(const USpoofChecker *sc,
764 uint32_t type,
765 const char16_t *id, int32_t length,
766 char16_t *dest, int32_t destCapacity,
767 UErrorCode *status) {
768
769 SpoofImpl::validateThis(sc, *status);
770 if (U_FAILURE(*status)) {
771 return 0;
772 }
773 if (length<-1 || destCapacity<0 || (destCapacity==0 && dest!=nullptr)) {
774 *status = U_ILLEGAL_ARGUMENT_ERROR;
775 return 0;
776 }
777
778 UnicodeString idStr((length==-1), id, length); // Aliasing constructor
779 UnicodeString destStr;
780 uspoof_getSkeletonUnicodeString(sc, type, idStr, destStr, status);
781 destStr.extract(dest, destCapacity, *status);
782 return destStr.length();
783 }
784
uspoof_getBidiSkeleton(const USpoofChecker * sc,UBiDiDirection direction,const UChar * id,int32_t length,UChar * dest,int32_t destCapacity,UErrorCode * status)785 U_CAPI int32_t U_EXPORT2 uspoof_getBidiSkeleton(const USpoofChecker *sc, UBiDiDirection direction,
786 const UChar *id, int32_t length, UChar *dest,
787 int32_t destCapacity, UErrorCode *status) {
788 UnicodeString idStr((length == -1), id, length); // Aliasing constructor
789 if (idStr.isBogus()) {
790 *status = U_ILLEGAL_ARGUMENT_ERROR;
791 return 0;
792 }
793 UnicodeString destStr;
794 uspoof_getBidiSkeletonUnicodeString(sc, direction, idStr, destStr, status);
795 return destStr.extract(dest, destCapacity, *status);
796 }
797
798
799
uspoof_getBidiSkeletonUnicodeString(const USpoofChecker * sc,UBiDiDirection direction,const UnicodeString & id,UnicodeString & dest,UErrorCode * status)800 U_I18N_API UnicodeString &U_EXPORT2 uspoof_getBidiSkeletonUnicodeString(const USpoofChecker *sc,
801 UBiDiDirection direction,
802 const UnicodeString &id,
803 UnicodeString &dest,
804 UErrorCode *status) {
805 dest.remove();
806 if (direction != UBIDI_LTR && direction != UBIDI_RTL) {
807 *status = U_ILLEGAL_ARGUMENT_ERROR;
808 return dest;
809 }
810 UBiDi *bidi = ubidi_open();
811 ubidi_setPara(bidi, id.getBuffer(), id.length(), direction,
812 /*embeddingLevels*/ nullptr, status);
813 if (U_FAILURE(*status)) {
814 ubidi_close(bidi);
815 return dest;
816 }
817 UnicodeString reordered;
818 int32_t const size = ubidi_getProcessedLength(bidi);
819 UChar* const reorderedBuffer = reordered.getBuffer(size);
820 if (reorderedBuffer == nullptr) {
821 *status = U_MEMORY_ALLOCATION_ERROR;
822 ubidi_close(bidi);
823 return dest;
824 }
825 ubidi_writeReordered(bidi, reorderedBuffer, size,
826 UBIDI_KEEP_BASE_COMBINING | UBIDI_DO_MIRRORING, status);
827 reordered.releaseBuffer(size);
828 ubidi_close(bidi);
829
830 if (U_FAILURE(*status)) {
831 return dest;
832 }
833
834 // The type parameter is deprecated since ICU 58; any number may be passed.
835 constexpr uint32_t deprecatedType = 58;
836 return uspoof_getSkeletonUnicodeString(sc, deprecatedType, reordered, dest, status);
837 }
838
839
840
841 U_I18N_API UnicodeString & U_EXPORT2
uspoof_getSkeletonUnicodeString(const USpoofChecker * sc,uint32_t,const UnicodeString & id,UnicodeString & dest,UErrorCode * status)842 uspoof_getSkeletonUnicodeString(const USpoofChecker *sc,
843 uint32_t /*type*/,
844 const UnicodeString &id,
845 UnicodeString &dest,
846 UErrorCode *status) {
847 const SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
848 if (U_FAILURE(*status)) {
849 return dest;
850 }
851
852 UnicodeString nfdId;
853 gNfdNormalizer->normalize(id, nfdId, *status);
854
855 // Apply the skeleton mapping to the NFD normalized input string
856 // Accumulate the skeleton, possibly unnormalized, in a UnicodeString.
857 int32_t inputIndex = 0;
858 UnicodeString skelStr;
859 int32_t normalizedLen = nfdId.length();
860 for (inputIndex=0; inputIndex < normalizedLen; ) {
861 UChar32 c = nfdId.char32At(inputIndex);
862 inputIndex += U16_LENGTH(c);
863 if (!u_hasBinaryProperty(c, UCHAR_DEFAULT_IGNORABLE_CODE_POINT)) {
864 This->fSpoofData->confusableLookup(c, skelStr);
865 }
866 }
867
868 gNfdNormalizer->normalize(skelStr, dest, *status);
869 return dest;
870 }
871
uspoof_getSkeletonUTF8(const USpoofChecker * sc,uint32_t type,const char * id,int32_t length,char * dest,int32_t destCapacity,UErrorCode * status)872 U_CAPI int32_t U_EXPORT2 uspoof_getSkeletonUTF8(const USpoofChecker *sc, uint32_t type, const char *id,
873 int32_t length, char *dest, int32_t destCapacity,
874 UErrorCode *status) {
875 SpoofImpl::validateThis(sc, *status);
876 if (U_FAILURE(*status)) {
877 return 0;
878 }
879 if (length<-1 || destCapacity<0 || (destCapacity==0 && dest!=nullptr)) {
880 *status = U_ILLEGAL_ARGUMENT_ERROR;
881 return 0;
882 }
883
884 UnicodeString srcStr = UnicodeString::fromUTF8(
885 StringPiece(id, length >= 0 ? length : static_cast<int32_t>(uprv_strlen(id))));
886 UnicodeString destStr;
887 uspoof_getSkeletonUnicodeString(sc, type, srcStr, destStr, status);
888 if (U_FAILURE(*status)) {
889 return 0;
890 }
891
892 int32_t lengthInUTF8 = 0;
893 u_strToUTF8(dest, destCapacity, &lengthInUTF8, destStr.getBuffer(), destStr.length(), status);
894 return lengthInUTF8;
895 }
896
uspoof_getBidiSkeletonUTF8(const USpoofChecker * sc,UBiDiDirection direction,const char * id,int32_t length,char * dest,int32_t destCapacity,UErrorCode * status)897 U_CAPI int32_t U_EXPORT2 uspoof_getBidiSkeletonUTF8(const USpoofChecker *sc, UBiDiDirection direction,
898 const char *id, int32_t length, char *dest,
899 int32_t destCapacity, UErrorCode *status) {
900 if (length < -1) {
901 *status = U_ILLEGAL_ARGUMENT_ERROR;
902 return 0;
903 }
904
905 UnicodeString srcStr = UnicodeString::fromUTF8(
906 StringPiece(id, length >= 0 ? length : static_cast<int32_t>(uprv_strlen(id))));
907 UnicodeString destStr;
908 uspoof_getBidiSkeletonUnicodeString(sc, direction, srcStr, destStr, status);
909 if (U_FAILURE(*status)) {
910 return 0;
911 }
912
913 int32_t lengthInUTF8 = 0;
914 u_strToUTF8(dest, destCapacity, &lengthInUTF8, destStr.getBuffer(), destStr.length(), status);
915 return lengthInUTF8;
916 }
917
918
919 U_CAPI int32_t U_EXPORT2
uspoof_serialize(USpoofChecker * sc,void * buf,int32_t capacity,UErrorCode * status)920 uspoof_serialize(USpoofChecker *sc,void *buf, int32_t capacity, UErrorCode *status) {
921 SpoofImpl *This = SpoofImpl::validateThis(sc, *status);
922 if (This == nullptr) {
923 U_ASSERT(U_FAILURE(*status));
924 return 0;
925 }
926
927 return This->fSpoofData->serialize(buf, capacity, *status);
928 }
929
930 U_CAPI const USet * U_EXPORT2
uspoof_getInclusionSet(UErrorCode * status)931 uspoof_getInclusionSet(UErrorCode *status) {
932 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
933 return gInclusionSet->toUSet();
934 }
935
936 U_CAPI const USet * U_EXPORT2
uspoof_getRecommendedSet(UErrorCode * status)937 uspoof_getRecommendedSet(UErrorCode *status) {
938 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
939 return gRecommendedSet->toUSet();
940 }
941
942 U_I18N_API const UnicodeSet * U_EXPORT2
uspoof_getInclusionUnicodeSet(UErrorCode * status)943 uspoof_getInclusionUnicodeSet(UErrorCode *status) {
944 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
945 return gInclusionSet;
946 }
947
948 U_I18N_API const UnicodeSet * U_EXPORT2
uspoof_getRecommendedUnicodeSet(UErrorCode * status)949 uspoof_getRecommendedUnicodeSet(UErrorCode *status) {
950 umtx_initOnce(gSpoofInitStaticsOnce, &initializeStatics, *status);
951 return gRecommendedSet;
952 }
953
954 //------------------
955 // CheckResult APIs
956 //------------------
957
958 U_CAPI USpoofCheckResult* U_EXPORT2
uspoof_openCheckResult(UErrorCode * status)959 uspoof_openCheckResult(UErrorCode *status) {
960 CheckResult* checkResult = new CheckResult();
961 if (checkResult == nullptr) {
962 *status = U_MEMORY_ALLOCATION_ERROR;
963 return nullptr;
964 }
965 return checkResult->asUSpoofCheckResult();
966 }
967
968 U_CAPI void U_EXPORT2
uspoof_closeCheckResult(USpoofCheckResult * checkResult)969 uspoof_closeCheckResult(USpoofCheckResult* checkResult) {
970 UErrorCode status = U_ZERO_ERROR;
971 CheckResult* This = CheckResult::validateThis(checkResult, status);
972 delete This;
973 }
974
975 U_CAPI int32_t U_EXPORT2
uspoof_getCheckResultChecks(const USpoofCheckResult * checkResult,UErrorCode * status)976 uspoof_getCheckResultChecks(const USpoofCheckResult *checkResult, UErrorCode *status) {
977 const CheckResult* This = CheckResult::validateThis(checkResult, *status);
978 if (U_FAILURE(*status)) { return 0; }
979 return This->fChecks;
980 }
981
982 U_CAPI URestrictionLevel U_EXPORT2
uspoof_getCheckResultRestrictionLevel(const USpoofCheckResult * checkResult,UErrorCode * status)983 uspoof_getCheckResultRestrictionLevel(const USpoofCheckResult *checkResult, UErrorCode *status) {
984 const CheckResult* This = CheckResult::validateThis(checkResult, *status);
985 if (U_FAILURE(*status)) { return USPOOF_UNRESTRICTIVE; }
986 return This->fRestrictionLevel;
987 }
988
989 U_CAPI const USet* U_EXPORT2
uspoof_getCheckResultNumerics(const USpoofCheckResult * checkResult,UErrorCode * status)990 uspoof_getCheckResultNumerics(const USpoofCheckResult *checkResult, UErrorCode *status) {
991 const CheckResult* This = CheckResult::validateThis(checkResult, *status);
992 if (U_FAILURE(*status)) { return nullptr; }
993 return This->fNumerics.toUSet();
994 }
995
996
997
998 #endif // !UCONFIG_NO_NORMALIZATION
999