intl/icu/source/i18n/uspoof_impl.h

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 ***************************************************************************
michael@0 3 * Copyright (C) 2008-2013, International Business Machines Corporation
michael@0 4 * and others. All Rights Reserved.
michael@0 5 ***************************************************************************
michael@0 6 *
michael@0 7 * uspoof_impl.h
michael@0 8 *
michael@0 9 * Implemenation header for spoof detection
michael@0 10 *
michael@0 11 */
michael@0 12
michael@0 13 #ifndef USPOOFIM_H
michael@0 14 #define USPOOFIM_H
michael@0 15
michael@0 16 #include "unicode/utypes.h"
michael@0 17 #include "unicode/uspoof.h"
michael@0 18 #include "unicode/uscript.h"
michael@0 19 #include "unicode/udata.h"
michael@0 20
michael@0 21 #include "utrie2.h"
michael@0 22
michael@0 23 #if !UCONFIG_NO_NORMALIZATION
michael@0 24
michael@0 25 #ifdef __cplusplus
michael@0 26
michael@0 27 U_NAMESPACE_BEGIN
michael@0 28
michael@0 29 // The maximium length (in UTF-16 UChars) of the skeleton replacement string resulting from
michael@0 30 // a single input code point. This is function of the unicode.org data.
michael@0 31 #define USPOOF_MAX_SKELETON_EXPANSION 20
michael@0 32
michael@0 33 // The default stack buffer size for copies or conversions or normalizations
michael@0 34 // of input strings being checked. (Used in multiple places.)
michael@0 35 #define USPOOF_STACK_BUFFER_SIZE 100
michael@0 36
michael@0 37 // Magic number for sanity checking spoof data.
michael@0 38 #define USPOOF_MAGIC 0x3845fdef
michael@0 39
michael@0 40 class IdentifierInfo;
michael@0 41 class ScriptSet;
michael@0 42 class SpoofData;
michael@0 43 struct SpoofDataHeader;
michael@0 44 struct SpoofStringLengthsElement;
michael@0 45
michael@0 46 /**
michael@0 47 * Class SpoofImpl corresponds directly to the plain C API opaque type
michael@0 48 * USpoofChecker. One can be cast to the other.
michael@0 49 */
michael@0 50 class SpoofImpl : public UObject {
michael@0 51 public:
michael@0 52 SpoofImpl(SpoofData *data, UErrorCode &status);
michael@0 53 SpoofImpl();
michael@0 54 virtual ~SpoofImpl();
michael@0 55
michael@0 56 /** Copy constructor, used by the user level uspoof_clone() function.
michael@0 57 */
michael@0 58 SpoofImpl(const SpoofImpl &src, UErrorCode &status);
michael@0 59
michael@0 60 static SpoofImpl *validateThis(USpoofChecker *sc, UErrorCode &status);
michael@0 61 static const SpoofImpl *validateThis(const USpoofChecker *sc, UErrorCode &status);
michael@0 62
michael@0 63 /** Get the confusable skeleton transform for a single code point.
michael@0 64 * The result is a string with a length between 1 and 18.
michael@0 65 * @param tableMask bit flag specifying which confusable table to use.
michael@0 66 * One of USPOOF_SL_TABLE_FLAG, USPOOF_MA_TABLE_FLAG, etc.
michael@0 67 * @return The length in UTF-16 code units of the substition string.
michael@0 68 */
michael@0 69 int32_t confusableLookup(UChar32 inChar, int32_t tableMask, UnicodeString &destBuf) const;
michael@0 70
michael@0 71 /** Set and Get AllowedLocales, implementations of the corresponding API */
michael@0 72 void setAllowedLocales(const char *localesList, UErrorCode &status);
michael@0 73 const char * getAllowedLocales(UErrorCode &status);
michael@0 74
michael@0 75 // Add (union) to the UnicodeSet all of the characters for the scripts used for
michael@0 76 // the specified locale. Part of the implementation of setAllowedLocales.
michael@0 77 void addScriptChars(const char *locale, UnicodeSet *allowedChars, UErrorCode &status);
michael@0 78
michael@0 79
michael@0 80 /** parse a hex number. Untility used by the builders. */
michael@0 81 static UChar32 ScanHex(const UChar *s, int32_t start, int32_t limit, UErrorCode &status);
michael@0 82
michael@0 83 // Implementation for Whole Script tests.
michael@0 84 // Return the test bit flag to be ORed into the eventual user return value
michael@0 85 // if a Spoof opportunity is detected.
michael@0 86 void wholeScriptCheck(
michael@0 87 const UnicodeString &text, ScriptSet *result, UErrorCode &status) const;
michael@0 88
michael@0 89 static UClassID U_EXPORT2 getStaticClassID(void);
michael@0 90 virtual UClassID getDynamicClassID(void) const;
michael@0 91
michael@0 92 // IdentifierInfo Cache. IdentifierInfo objects are somewhat expensive to create.
michael@0 93 // Maintain a one-element cache, which is sufficient to avoid repeatedly
michael@0 94 // creating new ones unless we get multi-thread concurrency in spoof
michael@0 95 // check operations, which should be statistically uncommon.
michael@0 96 IdentifierInfo *getIdentifierInfo(UErrorCode &status) const;
michael@0 97 void releaseIdentifierInfo(IdentifierInfo *idInfo) const;
michael@0 98
michael@0 99 //
michael@0 100 // Data Members
michael@0 101 //
michael@0 102
michael@0 103 int32_t fMagic; // Internal sanity check.
michael@0 104 int32_t fChecks; // Bit vector of checks to perform.
michael@0 105
michael@0 106 SpoofData *fSpoofData;
michael@0 107
michael@0 108 const UnicodeSet *fAllowedCharsSet; // The UnicodeSet of allowed characters.
michael@0 109 // for this Spoof Checker. Defaults to all chars.
michael@0 110
michael@0 111 const char *fAllowedLocales; // The list of allowed locales.
michael@0 112 URestrictionLevel fRestrictionLevel; // The maximum restriction level for an acceptable identifier.
michael@0 113
michael@0 114 IdentifierInfo *fCachedIdentifierInfo; // Do not use directly. See getIdentifierInfo().:w
michael@0 115 };
michael@0 116
michael@0 117
michael@0 118
michael@0 119 //
michael@0 120 // Confusable Mappings Data Structures
michael@0 121 //
michael@0 122 // For the confusable data, we are essentially implementing a map,
michael@0 123 // key: a code point
michael@0 124 // value: a string. Most commonly one char in length, but can be more.
michael@0 125 //
michael@0 126 // The keys are stored as a sorted array of 32 bit ints.
michael@0 127 // bits 0-23 a code point value
michael@0 128 // bits 24-31 flags
michael@0 129 // 24: 1 if entry applies to SL table
michael@0 130 // 25: 1 if entry applies to SA table
michael@0 131 // 26: 1 if entry applies to ML table
michael@0 132 // 27: 1 if entry applies to MA table
michael@0 133 // 28: 1 if there are multiple entries for this code point.
michael@0 134 // 29-30: length of value string, in UChars.
michael@0 135 // values are (1, 2, 3, other)
michael@0 136 // The key table is sorted in ascending code point order. (not on the
michael@0 137 // 32 bit int value, the flag bits do not participate in the sorting.)
michael@0 138 //
michael@0 139 // Lookup is done by means of a binary search in the key table.
michael@0 140 //
michael@0 141 // The corresponding values are kept in a parallel array of 16 bit ints.
michael@0 142 // If the value string is of length 1, it is literally in the value array.
michael@0 143 // For longer strings, the value array contains an index into the strings table.
michael@0 144 //
michael@0 145 // String Table:
michael@0 146 // The strings table contains all of the value strings (those of length two or greater)
michael@0 147 // concatentated together into one long UChar (UTF-16) array.
michael@0 148 //
michael@0 149 // The array is arranged by length of the strings - all strings of the same length
michael@0 150 // are stored together. The sections are ordered by length of the strings -
michael@0 151 // all two char strings first, followed by all of the three Char strings, etc.
michael@0 152 //
michael@0 153 // There is no nul character or other mark between adjacent strings.
michael@0 154 //
michael@0 155 // String Lengths table
michael@0 156 // The length of strings from 1 to 3 is flagged in the key table.
michael@0 157 // For strings of length 4 or longer, the string length table provides a
michael@0 158 // mapping between an index into the string table and the corresponding length.
michael@0 159 // Strings of these lengths are rare, so lookup time is not an issue.
michael@0 160 // Each entry consists of
michael@0 161 // uint16_t index of the _last_ string with this length
michael@0 162 // uint16_t the length
michael@0 163 //
michael@0 164
michael@0 165 // Flag bits in the Key entries
michael@0 166 #define USPOOF_SL_TABLE_FLAG (1<<24)
michael@0 167 #define USPOOF_SA_TABLE_FLAG (1<<25)
michael@0 168 #define USPOOF_ML_TABLE_FLAG (1<<26)
michael@0 169 #define USPOOF_MA_TABLE_FLAG (1<<27)
michael@0 170 #define USPOOF_KEY_MULTIPLE_VALUES (1<<28)
michael@0 171 #define USPOOF_KEY_LENGTH_SHIFT 29
michael@0 172 #define USPOOF_KEY_LENGTH_FIELD(x) (((x)>>29) & 3)
michael@0 173
michael@0 174
michael@0 175 struct SpoofStringLengthsElement {
michael@0 176 uint16_t fLastString; // index in string table of last string with this length
michael@0 177 uint16_t fStrLength; // Length of strings
michael@0 178 };
michael@0 179
michael@0 180
michael@0 181
michael@0 182 //-------------------------------------------------------------------------------------
michael@0 183 //
michael@0 184 // SpoofData
michael@0 185 //
michael@0 186 // A small class that wraps the raw (usually memory mapped) spoof data.
michael@0 187 // Serves two primary functions:
michael@0 188 // 1. Convenience. Contains real pointers to the data, to avoid dealing with
michael@0 189 // the offsets in the raw data.
michael@0 190 // 2. Reference counting. When a spoof checker is cloned, the raw data is shared
michael@0 191 // and must be retained until all checkers using the data are closed.
michael@0 192 // Nothing in this struct includes state that is specific to any particular
michael@0 193 // USpoofDetector object.
michael@0 194 //
michael@0 195 //---------------------------------------------------------------------------------------
michael@0 196 class SpoofData: public UMemory {
michael@0 197 public:
michael@0 198 static SpoofData *getDefault(UErrorCode &status); // Load standard ICU spoof data.
michael@0 199 SpoofData(UErrorCode &status); // Create new spoof data wrapper.
michael@0 200 // Only used when building new data from rules.
michael@0 201
michael@0 202 // Constructor for use when creating from prebuilt default data.
michael@0 203 // A UDataMemory is what the ICU internal data loading functions provide.
michael@0 204 // The udm is adopted by the SpoofData.
michael@0 205 SpoofData(UDataMemory *udm, UErrorCode &status);
michael@0 206
michael@0 207 // Constructor for use when creating from serialized data.
michael@0 208 //
michael@0 209 SpoofData(const void *serializedData, int32_t length, UErrorCode &status);
michael@0 210
michael@0 211 // Check raw Spoof Data Version compatibility.
michael@0 212 // Return TRUE it looks good.
michael@0 213 static UBool validateDataVersion(const SpoofDataHeader *rawData, UErrorCode &status);
michael@0 214 ~SpoofData(); // Destructor not normally used.
michael@0 215 // Use removeReference() instead.
michael@0 216 // Reference Counting functions.
michael@0 217 // Clone of a user-level spoof detector increments the ref count on the data.
michael@0 218 // Close of a user-level spoof detector decrements the ref count.
michael@0 219 // If the data is owned by us, it will be deleted when count goes to zero.
michael@0 220 SpoofData *addReference();
michael@0 221 void removeReference();
michael@0 222
michael@0 223 // Reserve space in the raw data. For use by builder when putting together a
michael@0 224 // new set of data. Init the new storage to zero, to prevent inconsistent
michael@0 225 // results if it is not all otherwise set by the requester.
michael@0 226 // Return:
michael@0 227 // pointer to the new space that was added by this function.
michael@0 228 void *reserveSpace(int32_t numBytes, UErrorCode &status);
michael@0 229
michael@0 230 // initialize the pointers from this object to the raw data.
michael@0 231 void initPtrs(UErrorCode &status);
michael@0 232
michael@0 233 // Reset all fields to an initial state.
michael@0 234 // Called from the top of all constructors.
michael@0 235 void reset();
michael@0 236
michael@0 237 SpoofDataHeader *fRawData; // Ptr to the raw memory-mapped data
michael@0 238 UBool fDataOwned; // True if the raw data is owned, and needs
michael@0 239 // to be deleted when refcount goes to zero.
michael@0 240 UDataMemory *fUDM; // If not NULL, our data came from a
michael@0 241 // UDataMemory, which we must close when
michael@0 242 // we are done.
michael@0 243
michael@0 244 uint32_t fMemLimit; // Limit of available raw data space
michael@0 245 u_atomic_int32_t fRefCount;
michael@0 246
michael@0 247 // Confusable data
michael@0 248 int32_t *fCFUKeys;
michael@0 249 uint16_t *fCFUValues;
michael@0 250 SpoofStringLengthsElement *fCFUStringLengths;
michael@0 251 UChar *fCFUStrings;
michael@0 252
michael@0 253 // Whole Script Confusable Data
michael@0 254 UTrie2 *fAnyCaseTrie;
michael@0 255 UTrie2 *fLowerCaseTrie;
michael@0 256 ScriptSet *fScriptSets;
michael@0 257 };
michael@0 258
michael@0 259
michael@0 260 //---------------------------------------------------------------------------------------
michael@0 261 //
michael@0 262 // Raw Binary Data Formats, as loaded from the ICU data file,
michael@0 263 // or as built by the builder.
michael@0 264 //
michael@0 265 //---------------------------------------------------------------------------------------
michael@0 266 struct SpoofDataHeader {
michael@0 267 int32_t fMagic; // (0x3845fdef)
michael@0 268 uint8_t fFormatVersion[4]; // Data Format. Same as the value in struct UDataInfo
michael@0 269 // if there is one associated with this data.
michael@0 270 int32_t fLength; // Total lenght in bytes of this spoof data,
michael@0 271 // including all sections, not just the header.
michael@0 272
michael@0 273 // The following four sections refer to data representing the confusable data
michael@0 274 // from the Unicode.org data from "confusables.txt"
michael@0 275
michael@0 276 int32_t fCFUKeys; // byte offset to Keys table (from SpoofDataHeader *)
michael@0 277 int32_t fCFUKeysSize; // number of entries in keys table (32 bits each)
michael@0 278
michael@0 279 // TODO: change name to fCFUValues, for consistency.
michael@0 280 int32_t fCFUStringIndex; // byte offset to String Indexes table
michael@0 281 int32_t fCFUStringIndexSize; // number of entries in String Indexes table (16 bits each)
michael@0 282 // (number of entries must be same as in Keys table
michael@0 283
michael@0 284 int32_t fCFUStringTable; // byte offset of String table
michael@0 285 int32_t fCFUStringTableLen; // length of string table (in 16 bit UChars)
michael@0 286
michael@0 287 int32_t fCFUStringLengths; // byte offset to String Lengths table
michael@0 288 int32_t fCFUStringLengthsSize; // number of entries in lengths table. (2 x 16 bits each)
michael@0 289
michael@0 290
michael@0 291 // The following sections are for data from confusablesWholeScript.txt
michael@0 292
michael@0 293 int32_t fAnyCaseTrie; // byte offset to the serialized Any Case Trie
michael@0 294 int32_t fAnyCaseTrieLength; // Length (bytes) of the serialized Any Case Trie
michael@0 295
michael@0 296 int32_t fLowerCaseTrie; // byte offset to the serialized Lower Case Trie
michael@0 297 int32_t fLowerCaseTrieLength; // Length (bytes) of the serialized Lower Case Trie
michael@0 298
michael@0 299 int32_t fScriptSets; // byte offset to array of ScriptSets
michael@0 300 int32_t fScriptSetsLength; // Number of ScriptSets (24 bytes each)
michael@0 301
michael@0 302
michael@0 303 // The following sections are for data from xidmodifications.txt
michael@0 304
michael@0 305
michael@0 306 int32_t unused[15]; // Padding, Room for Expansion
michael@0 307
michael@0 308 };
michael@0 309
michael@0 310
michael@0 311
michael@0 312
michael@0 313 //
michael@0 314 // Structure for the Whole Script Confusable Data
michael@0 315 // See Unicode UAX-39, Unicode Security Mechanisms, for a description of the
michael@0 316 // Whole Script confusable data
michael@0 317 //
michael@0 318 // The data provides mappings from code points to a set of scripts
michael@0 319 // that contain characters that might be confused with the code point.
michael@0 320 // There are two mappings, one for lower case only, and one for characters
michael@0 321 // of any case.
michael@0 322 //
michael@0 323 // The actual data consists of a utrie2 to map from a code point to an offset,
michael@0 324 // and an array of UScriptSets (essentially bit maps) that is indexed
michael@0 325 // by the offsets obtained from the Trie.
michael@0 326 //
michael@0 327 //
michael@0 328
michael@0 329
michael@0 330 U_NAMESPACE_END
michael@0 331 #endif /* __cplusplus */
michael@0 332
michael@0 333 /**
michael@0 334 * Endianness swap function for binary spoof data.
michael@0 335 * @internal
michael@0 336 */
michael@0 337 U_CAPI int32_t U_EXPORT2
michael@0 338 uspoof_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData,
michael@0 339 UErrorCode *status);
michael@0 340
michael@0 341
michael@0 342 #endif
michael@0 343
michael@0 344 #endif /* USPOOFIM_H */
michael@0 345

mercurial