/src/mozilla-central/intl/icu/source/i18n/uspoof_impl.h
Line | Count | Source (jump to first uncovered line) |
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-2013, International Business Machines Corporation |
6 | | * and others. All Rights Reserved. |
7 | | *************************************************************************** |
8 | | * |
9 | | * uspoof_impl.h |
10 | | * |
11 | | * Implemenation header for spoof detection |
12 | | * |
13 | | */ |
14 | | |
15 | | #ifndef USPOOFIM_H |
16 | | #define USPOOFIM_H |
17 | | |
18 | | #include "uassert.h" |
19 | | #include "unicode/utypes.h" |
20 | | #include "unicode/uspoof.h" |
21 | | #include "unicode/uscript.h" |
22 | | #include "unicode/udata.h" |
23 | | |
24 | | #include "utrie2.h" |
25 | | |
26 | | #if !UCONFIG_NO_NORMALIZATION |
27 | | |
28 | | #ifdef __cplusplus |
29 | | |
30 | | U_NAMESPACE_BEGIN |
31 | | |
32 | | // The maximium length (in UTF-16 UChars) of the skeleton replacement string resulting from |
33 | | // a single input code point. This is function of the unicode.org data. |
34 | | #define USPOOF_MAX_SKELETON_EXPANSION 20 |
35 | | |
36 | | // The default stack buffer size for copies or conversions or normalizations |
37 | | // of input strings being checked. (Used in multiple places.) |
38 | | #define USPOOF_STACK_BUFFER_SIZE 100 |
39 | | |
40 | | // Magic number for sanity checking spoof data. |
41 | 0 | #define USPOOF_MAGIC 0x3845fdef |
42 | | |
43 | | // Magic number for sanity checking spoof checkers. |
44 | 0 | #define USPOOF_CHECK_MAGIC 0x2734ecde |
45 | | |
46 | | class ScriptSet; |
47 | | class SpoofData; |
48 | | struct SpoofDataHeader; |
49 | | class ConfusableDataUtils; |
50 | | |
51 | | /** |
52 | | * Class SpoofImpl corresponds directly to the plain C API opaque type |
53 | | * USpoofChecker. One can be cast to the other. |
54 | | */ |
55 | | class SpoofImpl : public UObject { |
56 | | public: |
57 | | SpoofImpl(SpoofData *data, UErrorCode& status); |
58 | | SpoofImpl(UErrorCode& status); |
59 | | SpoofImpl(); |
60 | | void construct(UErrorCode& status); |
61 | | virtual ~SpoofImpl(); |
62 | | |
63 | | /** Copy constructor, used by the user level uspoof_clone() function. |
64 | | */ |
65 | | SpoofImpl(const SpoofImpl &src, UErrorCode &status); |
66 | | |
67 | | USpoofChecker *asUSpoofChecker(); |
68 | | static SpoofImpl *validateThis(USpoofChecker *sc, UErrorCode &status); |
69 | | static const SpoofImpl *validateThis(const USpoofChecker *sc, UErrorCode &status); |
70 | | |
71 | | /** Set and Get AllowedLocales, implementations of the corresponding API */ |
72 | | void setAllowedLocales(const char *localesList, UErrorCode &status); |
73 | | const char * getAllowedLocales(UErrorCode &status); |
74 | | |
75 | | // Add (union) to the UnicodeSet all of the characters for the scripts used for |
76 | | // the specified locale. Part of the implementation of setAllowedLocales. |
77 | | void addScriptChars(const char *locale, UnicodeSet *allowedChars, UErrorCode &status); |
78 | | |
79 | | // Functions implementing the features of UTS 39 section 5. |
80 | | static void getAugmentedScriptSet(UChar32 codePoint, ScriptSet& result, UErrorCode& status); |
81 | | void getResolvedScriptSet(const UnicodeString& input, ScriptSet& result, UErrorCode& status) const; |
82 | | void getResolvedScriptSetWithout(const UnicodeString& input, UScriptCode script, ScriptSet& result, UErrorCode& status) const; |
83 | | void getNumerics(const UnicodeString& input, UnicodeSet& result, UErrorCode& status) const; |
84 | | URestrictionLevel getRestrictionLevel(const UnicodeString& input, UErrorCode& status) const; |
85 | | |
86 | | int32_t findHiddenOverlay(const UnicodeString& input, UErrorCode& status) const; |
87 | | bool isIllegalCombiningDotLeadCharacter(UChar32 cp) const; |
88 | | |
89 | | /** parse a hex number. Untility used by the builders. */ |
90 | | static UChar32 ScanHex(const UChar *s, int32_t start, int32_t limit, UErrorCode &status); |
91 | | |
92 | | static UClassID U_EXPORT2 getStaticClassID(void); |
93 | | virtual UClassID getDynamicClassID(void) const; |
94 | | |
95 | | // |
96 | | // Data Members |
97 | | // |
98 | | |
99 | | int32_t fMagic; // Internal sanity check. |
100 | | int32_t fChecks; // Bit vector of checks to perform. |
101 | | |
102 | | SpoofData *fSpoofData; |
103 | | |
104 | | const UnicodeSet *fAllowedCharsSet; // The UnicodeSet of allowed characters. |
105 | | // for this Spoof Checker. Defaults to all chars. |
106 | | |
107 | | const char *fAllowedLocales; // The list of allowed locales. |
108 | | URestrictionLevel fRestrictionLevel; // The maximum restriction level for an acceptable identifier. |
109 | | }; |
110 | | |
111 | | /** |
112 | | * Class CheckResult corresponds directly to the plain C API opaque type |
113 | | * USpoofCheckResult. One can be cast to the other. |
114 | | */ |
115 | | class CheckResult : public UObject { |
116 | | public: |
117 | | CheckResult(); |
118 | | virtual ~CheckResult(); |
119 | | |
120 | | USpoofCheckResult *asUSpoofCheckResult(); |
121 | | static CheckResult *validateThis(USpoofCheckResult *ptr, UErrorCode &status); |
122 | | static const CheckResult *validateThis(const USpoofCheckResult *ptr, UErrorCode &status); |
123 | | |
124 | | void clear(); |
125 | | |
126 | | // Used to convert this CheckResult to the older int32_t return value API |
127 | | int32_t toCombinedBitmask(int32_t expectedChecks); |
128 | | |
129 | | // Data Members |
130 | | int32_t fMagic; // Internal sanity check. |
131 | | int32_t fChecks; // Bit vector of checks that were failed. |
132 | | UnicodeSet fNumerics; // Set of numerics found in the string. |
133 | | URestrictionLevel fRestrictionLevel; // The restriction level of the string. |
134 | | }; |
135 | | |
136 | | |
137 | | // |
138 | | // Confusable Mappings Data Structures, version 2.0 |
139 | | // |
140 | | // For the confusable data, we are essentially implementing a map, |
141 | | // key: a code point |
142 | | // value: a string. Most commonly one char in length, but can be more. |
143 | | // |
144 | | // The keys are stored as a sorted array of 32 bit ints. |
145 | | // bits 0-23 a code point value |
146 | | // bits 24-31 length of value string, in UChars (between 1 and 256 UChars). |
147 | | // The key table is sorted in ascending code point order. (not on the |
148 | | // 32 bit int value, the flag bits do not participate in the sorting.) |
149 | | // |
150 | | // Lookup is done by means of a binary search in the key table. |
151 | | // |
152 | | // The corresponding values are kept in a parallel array of 16 bit ints. |
153 | | // If the value string is of length 1, it is literally in the value array. |
154 | | // For longer strings, the value array contains an index into the strings table. |
155 | | // |
156 | | // String Table: |
157 | | // The strings table contains all of the value strings (those of length two or greater) |
158 | | // concatentated together into one long UChar (UTF-16) array. |
159 | | // |
160 | | // There is no nul character or other mark between adjacent strings. |
161 | | // |
162 | | //---------------------------------------------------------------------------- |
163 | | // |
164 | | // Changes from format version 1 to format version 2: |
165 | | // 1) Removal of the whole-script confusable data tables. |
166 | | // 2) Removal of the SL/SA/ML/MA and multi-table flags in the key bitmask. |
167 | | // 3) Expansion of string length value in the key bitmask from 2 bits to 8 bits. |
168 | | // 4) Removal of the string lengths table since 8 bits is sufficient for the |
169 | | // lengths of all entries in confusables.txt. |
170 | | |
171 | | |
172 | | |
173 | | // Internal functions for manipulating confusable data table keys |
174 | 0 | #define USPOOF_CONFUSABLE_DATA_FORMAT_VERSION 2 // version for ICU 58 |
175 | | class ConfusableDataUtils { |
176 | | public: |
177 | 0 | inline static UChar32 keyToCodePoint(int32_t key) { |
178 | 0 | return key & 0x00ffffff; |
179 | 0 | } |
180 | 0 | inline static int32_t keyToLength(int32_t key) { |
181 | 0 | return ((key & 0xff000000) >> 24) + 1; |
182 | 0 | } |
183 | 0 | inline static int32_t codePointAndLengthToKey(UChar32 codePoint, int32_t length) { |
184 | 0 | U_ASSERT((codePoint & 0x00ffffff) == codePoint); |
185 | 0 | U_ASSERT(length <= 256); |
186 | 0 | return codePoint | ((length - 1) << 24); |
187 | 0 | } |
188 | | }; |
189 | | |
190 | | |
191 | | //------------------------------------------------------------------------------------- |
192 | | // |
193 | | // SpoofData |
194 | | // |
195 | | // A small class that wraps the raw (usually memory mapped) spoof data. |
196 | | // Serves two primary functions: |
197 | | // 1. Convenience. Contains real pointers to the data, to avoid dealing with |
198 | | // the offsets in the raw data. |
199 | | // 2. Reference counting. When a spoof checker is cloned, the raw data is shared |
200 | | // and must be retained until all checkers using the data are closed. |
201 | | // Nothing in this struct includes state that is specific to any particular |
202 | | // USpoofDetector object. |
203 | | // |
204 | | //--------------------------------------------------------------------------------------- |
205 | | class SpoofData: public UMemory { |
206 | | public: |
207 | | static SpoofData* getDefault(UErrorCode &status); // Get standard ICU spoof data. |
208 | | static void releaseDefault(); // Cleanup reference to default spoof data. |
209 | | |
210 | | SpoofData(UErrorCode &status); // Create new spoof data wrapper. |
211 | | // Only used when building new data from rules. |
212 | | |
213 | | // Constructor for use when creating from prebuilt default data. |
214 | | // A UDataMemory is what the ICU internal data loading functions provide. |
215 | | // The udm is adopted by the SpoofData. |
216 | | SpoofData(UDataMemory *udm, UErrorCode &status); |
217 | | |
218 | | // Constructor for use when creating from serialized data. |
219 | | // |
220 | | SpoofData(const void *serializedData, int32_t length, UErrorCode &status); |
221 | | |
222 | | // Check raw Spoof Data Version compatibility. |
223 | | // Return TRUE it looks good. |
224 | | UBool validateDataVersion(UErrorCode &status) const; |
225 | | |
226 | | ~SpoofData(); // Destructor not normally used. |
227 | | // Use removeReference() instead. |
228 | | // Reference Counting functions. |
229 | | // Clone of a user-level spoof detector increments the ref count on the data. |
230 | | // Close of a user-level spoof detector decrements the ref count. |
231 | | // If the data is owned by us, it will be deleted when count goes to zero. |
232 | | SpoofData *addReference(); |
233 | | void removeReference(); |
234 | | |
235 | | // Reset all fields to an initial state. |
236 | | // Called from the top of all constructors. |
237 | | void reset(); |
238 | | |
239 | | // Copy this instance's raw data buffer to the specified address. |
240 | | int32_t serialize(void *buf, int32_t capacity, UErrorCode &status) const; |
241 | | |
242 | | // Get the total number of bytes of data backed by this SpoofData. |
243 | | // Not to be confused with length, which returns the number of confusable entries. |
244 | | int32_t size() const; |
245 | | |
246 | | // Get the confusable skeleton transform for a single code point. |
247 | | // The result is a string with a length between 1 and 18 as of Unicode 9. |
248 | | // This is the main public endpoint for this class. |
249 | | // @return The length in UTF-16 code units of the substition string. |
250 | | int32_t confusableLookup(UChar32 inChar, UnicodeString &dest) const; |
251 | | |
252 | | // Get the number of confusable entries in this SpoofData. |
253 | | int32_t length() const; |
254 | | |
255 | | // Get the code point (key) at the specified index. |
256 | | UChar32 codePointAt(int32_t index) const; |
257 | | |
258 | | // Get the confusable skeleton (value) at the specified index. |
259 | | // Append it to the specified UnicodeString&. |
260 | | // @return The length in UTF-16 code units of the skeleton string. |
261 | | int32_t appendValueTo(int32_t index, UnicodeString& dest) const; |
262 | | |
263 | | private: |
264 | | // Reserve space in the raw data. For use by builder when putting together a |
265 | | // new set of data. Init the new storage to zero, to prevent inconsistent |
266 | | // results if it is not all otherwise set by the requester. |
267 | | // Return: |
268 | | // pointer to the new space that was added by this function. |
269 | | void *reserveSpace(int32_t numBytes, UErrorCode &status); |
270 | | |
271 | | // initialize the pointers from this object to the raw data. |
272 | | void initPtrs(UErrorCode &status); |
273 | | |
274 | | SpoofDataHeader *fRawData; // Ptr to the raw memory-mapped data |
275 | | UBool fDataOwned; // True if the raw data is owned, and needs |
276 | | // to be deleted when refcount goes to zero. |
277 | | UDataMemory *fUDM; // If not NULL, our data came from a |
278 | | // UDataMemory, which we must close when |
279 | | // we are done. |
280 | | |
281 | | uint32_t fMemLimit; // Limit of available raw data space |
282 | | u_atomic_int32_t fRefCount; |
283 | | |
284 | | // Confusable data |
285 | | int32_t *fCFUKeys; |
286 | | uint16_t *fCFUValues; |
287 | | UChar *fCFUStrings; |
288 | | |
289 | | friend class ConfusabledataBuilder; |
290 | | }; |
291 | | |
292 | | //--------------------------------------------------------------------------------------- |
293 | | // |
294 | | // Raw Binary Data Formats, as loaded from the ICU data file, |
295 | | // or as built by the builder. |
296 | | // |
297 | | //--------------------------------------------------------------------------------------- |
298 | | struct SpoofDataHeader { |
299 | | int32_t fMagic; // (0x3845fdef) |
300 | | uint8_t fFormatVersion[4]; // Data Format. Same as the value in struct UDataInfo |
301 | | // if there is one associated with this data. |
302 | | int32_t fLength; // Total lenght in bytes of this spoof data, |
303 | | // including all sections, not just the header. |
304 | | |
305 | | // The following four sections refer to data representing the confusable data |
306 | | // from the Unicode.org data from "confusables.txt" |
307 | | |
308 | | int32_t fCFUKeys; // byte offset to Keys table (from SpoofDataHeader *) |
309 | | int32_t fCFUKeysSize; // number of entries in keys table (32 bits each) |
310 | | |
311 | | // TODO: change name to fCFUValues, for consistency. |
312 | | int32_t fCFUStringIndex; // byte offset to String Indexes table |
313 | | int32_t fCFUStringIndexSize; // number of entries in String Indexes table (16 bits each) |
314 | | // (number of entries must be same as in Keys table |
315 | | |
316 | | int32_t fCFUStringTable; // byte offset of String table |
317 | | int32_t fCFUStringTableLen; // length of string table (in 16 bit UChars) |
318 | | |
319 | | // The following sections are for data from xidmodifications.txt |
320 | | |
321 | | int32_t unused[15]; // Padding, Room for Expansion |
322 | | }; |
323 | | |
324 | | |
325 | | |
326 | | U_NAMESPACE_END |
327 | | #endif /* __cplusplus */ |
328 | | |
329 | | /** |
330 | | * Endianness swap function for binary spoof data. |
331 | | * @internal |
332 | | */ |
333 | | U_CAPI int32_t U_EXPORT2 |
334 | | uspoof_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData, |
335 | | UErrorCode *status); |
336 | | |
337 | | |
338 | | #endif |
339 | | |
340 | | #endif /* USPOOFIM_H */ |
341 | | |