fe3eb3ed5c
* ICU-13530 copy C/C++ files UTrie2 -> UTrie3 X-SVN-Rev: 40754 * ICU-13530 UTrie3 new files copied from UTrie2: rename types/functions/macros X-SVN-Rev: 40755 * ICU-13530 debug-print building each UTrie2 X-SVN-Rev: 40756 * ICU-13530 remove two-byte-UTF-8 errorValue block; move highValue from end of data array into header; add errorValue to header X-SVN-Rev: 40762 * ICU-13530 UTrie3 U16_NEXT/PREV: errorValue for unpaired surrogates X-SVN-Rev: 40763 * ICU-13530 no more separate values for lead surrogate code units X-SVN-Rev: 40764 * ICU-13530 change from 11:5 trie bits to 10:6 for simpler UTF-8 code X-SVN-Rev: 40766 * ICU-13530 UTrie2 build UTrie3 as well, print sizes X-SVN-Rev: 40767 * ICU-13530 debug-print countSame, sumOverlaps, countInitial X-SVN-Rev: 40768 * ICU-13530 debug-print whether trie is for CanonIterData X-SVN-Rev: 40769 * ICU-13530 no index-shift for BMP data, no separate index-2 for 2-byte UTF-8; builder changes incomplete X-SVN-Rev: 40777 * ICU-13530 remove errorValue and highStart from UNewTrie3 X-SVN-Rev: 40778 * ICU-13530 rewrite UTrie3 builder code X-SVN-Rev: 40783 * ICU-13530 UTrie3 bug fixes X-SVN-Rev: 40788 * ICU-13530 fully re-inline _UTRIE3_U8_NEXT() X-SVN-Rev: 40790 * ICU-13530 find most common all-same data block for dataNullBlock and initialValue X-SVN-Rev: 40792 * ICU-13530 UTrie3 iterator functions take start and return the end of a range, rather than callback call for each range X-SVN-Rev: 40800 * ICU-13530 mask off unused data value bits before building a UTrie3 with values less than 32 bits wide X-SVN-Rev: 40803 * ICU-13530 split utrie3builder.h out of utrie3.h X-SVN-Rev: 40804 * ICU-13530 separate types UTrie3 vs. UTrie3Builder, implement builder as wrapper over C++ class Trie3Builder in .cpp X-SVN-Rev: 40809 * ICU-13530 function to make a UTrie3Builder from a UTrie3 X-SVN-Rev: 40810 * ICU-13530 debug-print some data; some cleanup X-SVN-Rev: 40865 * ICU-13530 BMP 10:6 but supplementary 10:6:4 X-SVN-Rev: 40984 * ICU-13530 move errorValue & highValue to the end of the data table, minimal padding to 4 bytes X-SVN-Rev: 41011 * ICU-13530 index-1 table gap of index-2 null blocks X-SVN-Rev: 41018 * ICU-13530 test with more than 128k compacted data X-SVN-Rev: 41034 * ICU-13530 supplementary bits 11:5:4 saves a little space X-SVN-Rev: 41039 * ICU-13530 supplementary bits 6:5:5:4 instead of gap: about same size but simpler X-SVN-Rev: 41050 * ICU-13530 remove unnecessary utrie3_clone(built trie) X-SVN-Rev: 41058 * ICU-13530 remove unnecessary UTrie3StringIterator X-SVN-Rev: 41059 * ICU-13530 back to UTRIE3_GET...() macros *returning* data values X-SVN-Rev: 41060 * ICU-13530 fast vs. small X-SVN-Rev: 41066 * ICU-13530 always load NFC data, add simple normalization performance test X-SVN-Rev: 41110 * ICU-13530 change normalization main trie to UTrie3 with special values for lead surrogates; forbid non-inert surrogate code *points* because unable to store values different from code *units*; runtime code work around that for code point lookup and iteration; adjust UTS 46 for normalization no longer mapping unpaired surrogates to U+FFFD X-SVN-Rev: 41122 * ICU-13530 simplenormperf bug fix and NFC base line X-SVN-Rev: 41126 * ICU-13530 move normalization getRange skipping lead surrogates to API getRangeSkipLead() X-SVN-Rev: 41182 * ICU-13530 switch CanonIterData and gennorm2 Norms to UTrie3 X-SVN-Rev: 41183 * ICU-13530 remove unused overwrite parameter from setRange() X-SVN-Rev: 41184 * ICU-13530 getRange skip lead -> fixed surrogates X-SVN-Rev: 41219 * ICU-13530 minor cleanup X-SVN-Rev: 41221 * ICU-13530 UTS 46 code map unpaired surrogates to U+FFFD before normalization X-SVN-Rev: 41224 * ICU-13530 minor internal-docs cleanup X-SVN-Rev: 41225 * ICU-13530 rename UTrie3 to UCPTrie, and other name changes X-SVN-Rev: 41226 * ICU-13530 add 8-bit data option; add type-any & valueBits-any for fromBinary(); macros consistently source type then data width X-SVN-Rev: 41234 * ICU-13530 scrub the API docs for the proposal X-SVN-Rev: 41319 * ICU-13530 tag internal definitions as such, or move them to an internal header X-SVN-Rev: 41320 * ICU-13530 Java API skeleton X-SVN-Rev: 41326 * ICU-13530 API feedback: ValueWidth, MutableCodePointTrie, base CodePointMap, ... X-SVN-Rev: 41382 * ICU-13530 add UCPTrie valueWidth field and padding, and combine data pointers into a union X-SVN-Rev: 41408 * ICU-13530 switch some macros to using dataAccess parameter: separate index vs. data lookups, no macro variant for each value width X-SVN-Rev: 41409 * ICU-13530 StringIterator is no longer a java.util.Iterator (bad fit) X-SVN-Rev: 41455 * ICU-13530 CodePointTrie.java code complete X-SVN-Rev: 41518 * ICU-13530 finish Java port incl test; keep C++ parallel * ICU-13530 adjust API for feedback: rename HandleValue to FilterValue, change getRange+getRangeFixedSurr(bool allSurr) to enum RangeOption+getRange(enum option); change remaining C macros to use dataAccess for 16/32/8-bit value widths; fix/clarify some API docs * ICU-13530 add javadoc * ICU-13530 document UCPTrie binary data format * ICU-13530 update .nrm formatVersion 3->4, document change in surrogate handling with new trie * ICU-13530 re-hardcode NFC data * move trie swapper code into new file; add new files to Windows project files; turn off trie debugging * ICU-13530 minor cleanup * ICU-13530 test more range starts; fix a C test leak * ICU-13530 regenerate Java data from scratch * ICU-13530 review feedback changes: API docs typos, more @internal, C++11 field initializers, fix potential leak in MutableCodePointTrie::fromUCPTrie() * ICU-13530 rename interface FilterValue to ValueFilter
342 lines
14 KiB
C++
342 lines
14 KiB
C++
// © 2016 and later: Unicode, Inc. and others.
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
/*
|
|
***************************************************************************
|
|
* Copyright (C) 2008-2013, International Business Machines Corporation
|
|
* and others. All Rights Reserved.
|
|
***************************************************************************
|
|
*
|
|
* uspoof_impl.h
|
|
*
|
|
* Implemenation header for spoof detection
|
|
*
|
|
*/
|
|
|
|
#ifndef USPOOFIM_H
|
|
#define USPOOFIM_H
|
|
|
|
#include "uassert.h"
|
|
#include "unicode/utypes.h"
|
|
#include "unicode/uspoof.h"
|
|
#include "unicode/uscript.h"
|
|
#include "unicode/udata.h"
|
|
#include "udataswp.h"
|
|
#include "utrie2.h"
|
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
|
|
#ifdef __cplusplus
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
// The maximium length (in UTF-16 UChars) of the skeleton replacement string resulting from
|
|
// a single input code point. This is function of the unicode.org data.
|
|
#define USPOOF_MAX_SKELETON_EXPANSION 20
|
|
|
|
// The default stack buffer size for copies or conversions or normalizations
|
|
// of input strings being checked. (Used in multiple places.)
|
|
#define USPOOF_STACK_BUFFER_SIZE 100
|
|
|
|
// Magic number for sanity checking spoof data.
|
|
#define USPOOF_MAGIC 0x3845fdef
|
|
|
|
// Magic number for sanity checking spoof checkers.
|
|
#define USPOOF_CHECK_MAGIC 0x2734ecde
|
|
|
|
class ScriptSet;
|
|
class SpoofData;
|
|
struct SpoofDataHeader;
|
|
class ConfusableDataUtils;
|
|
|
|
/**
|
|
* Class SpoofImpl corresponds directly to the plain C API opaque type
|
|
* USpoofChecker. One can be cast to the other.
|
|
*/
|
|
class SpoofImpl : public UObject {
|
|
public:
|
|
SpoofImpl(SpoofData *data, UErrorCode& status);
|
|
SpoofImpl(UErrorCode& status);
|
|
SpoofImpl();
|
|
void construct(UErrorCode& status);
|
|
virtual ~SpoofImpl();
|
|
|
|
/** Copy constructor, used by the user level uspoof_clone() function.
|
|
*/
|
|
SpoofImpl(const SpoofImpl &src, UErrorCode &status);
|
|
|
|
USpoofChecker *asUSpoofChecker();
|
|
static SpoofImpl *validateThis(USpoofChecker *sc, UErrorCode &status);
|
|
static const SpoofImpl *validateThis(const USpoofChecker *sc, UErrorCode &status);
|
|
|
|
/** Set and Get AllowedLocales, implementations of the corresponding API */
|
|
void setAllowedLocales(const char *localesList, UErrorCode &status);
|
|
const char * getAllowedLocales(UErrorCode &status);
|
|
|
|
// Add (union) to the UnicodeSet all of the characters for the scripts used for
|
|
// the specified locale. Part of the implementation of setAllowedLocales.
|
|
void addScriptChars(const char *locale, UnicodeSet *allowedChars, UErrorCode &status);
|
|
|
|
// Functions implementing the features of UTS 39 section 5.
|
|
static void getAugmentedScriptSet(UChar32 codePoint, ScriptSet& result, UErrorCode& status);
|
|
void getResolvedScriptSet(const UnicodeString& input, ScriptSet& result, UErrorCode& status) const;
|
|
void getResolvedScriptSetWithout(const UnicodeString& input, UScriptCode script, ScriptSet& result, UErrorCode& status) const;
|
|
void getNumerics(const UnicodeString& input, UnicodeSet& result, UErrorCode& status) const;
|
|
URestrictionLevel getRestrictionLevel(const UnicodeString& input, UErrorCode& status) const;
|
|
|
|
int32_t findHiddenOverlay(const UnicodeString& input, UErrorCode& status) const;
|
|
bool isIllegalCombiningDotLeadCharacter(UChar32 cp) const;
|
|
|
|
/** parse a hex number. Untility used by the builders. */
|
|
static UChar32 ScanHex(const UChar *s, int32_t start, int32_t limit, UErrorCode &status);
|
|
|
|
static UClassID U_EXPORT2 getStaticClassID(void);
|
|
virtual UClassID getDynamicClassID(void) const;
|
|
|
|
//
|
|
// Data Members
|
|
//
|
|
|
|
int32_t fMagic; // Internal sanity check.
|
|
int32_t fChecks; // Bit vector of checks to perform.
|
|
|
|
SpoofData *fSpoofData;
|
|
|
|
const UnicodeSet *fAllowedCharsSet; // The UnicodeSet of allowed characters.
|
|
// for this Spoof Checker. Defaults to all chars.
|
|
|
|
const char *fAllowedLocales; // The list of allowed locales.
|
|
URestrictionLevel fRestrictionLevel; // The maximum restriction level for an acceptable identifier.
|
|
};
|
|
|
|
/**
|
|
* Class CheckResult corresponds directly to the plain C API opaque type
|
|
* USpoofCheckResult. One can be cast to the other.
|
|
*/
|
|
class CheckResult : public UObject {
|
|
public:
|
|
CheckResult();
|
|
virtual ~CheckResult();
|
|
|
|
USpoofCheckResult *asUSpoofCheckResult();
|
|
static CheckResult *validateThis(USpoofCheckResult *ptr, UErrorCode &status);
|
|
static const CheckResult *validateThis(const USpoofCheckResult *ptr, UErrorCode &status);
|
|
|
|
void clear();
|
|
|
|
// Used to convert this CheckResult to the older int32_t return value API
|
|
int32_t toCombinedBitmask(int32_t expectedChecks);
|
|
|
|
// Data Members
|
|
int32_t fMagic; // Internal sanity check.
|
|
int32_t fChecks; // Bit vector of checks that were failed.
|
|
UnicodeSet fNumerics; // Set of numerics found in the string.
|
|
URestrictionLevel fRestrictionLevel; // The restriction level of the string.
|
|
};
|
|
|
|
|
|
//
|
|
// Confusable Mappings Data Structures, version 2.0
|
|
//
|
|
// For the confusable data, we are essentially implementing a map,
|
|
// key: a code point
|
|
// value: a string. Most commonly one char in length, but can be more.
|
|
//
|
|
// The keys are stored as a sorted array of 32 bit ints.
|
|
// bits 0-23 a code point value
|
|
// bits 24-31 length of value string, in UChars (between 1 and 256 UChars).
|
|
// The key table is sorted in ascending code point order. (not on the
|
|
// 32 bit int value, the flag bits do not participate in the sorting.)
|
|
//
|
|
// Lookup is done by means of a binary search in the key table.
|
|
//
|
|
// The corresponding values are kept in a parallel array of 16 bit ints.
|
|
// If the value string is of length 1, it is literally in the value array.
|
|
// For longer strings, the value array contains an index into the strings table.
|
|
//
|
|
// String Table:
|
|
// The strings table contains all of the value strings (those of length two or greater)
|
|
// concatentated together into one long UChar (UTF-16) array.
|
|
//
|
|
// There is no nul character or other mark between adjacent strings.
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
//
|
|
// Changes from format version 1 to format version 2:
|
|
// 1) Removal of the whole-script confusable data tables.
|
|
// 2) Removal of the SL/SA/ML/MA and multi-table flags in the key bitmask.
|
|
// 3) Expansion of string length value in the key bitmask from 2 bits to 8 bits.
|
|
// 4) Removal of the string lengths table since 8 bits is sufficient for the
|
|
// lengths of all entries in confusables.txt.
|
|
|
|
|
|
|
|
// Internal functions for manipulating confusable data table keys
|
|
#define USPOOF_CONFUSABLE_DATA_FORMAT_VERSION 2 // version for ICU 58
|
|
class ConfusableDataUtils {
|
|
public:
|
|
inline static UChar32 keyToCodePoint(int32_t key) {
|
|
return key & 0x00ffffff;
|
|
}
|
|
inline static int32_t keyToLength(int32_t key) {
|
|
return ((key & 0xff000000) >> 24) + 1;
|
|
}
|
|
inline static int32_t codePointAndLengthToKey(UChar32 codePoint, int32_t length) {
|
|
U_ASSERT((codePoint & 0x00ffffff) == codePoint);
|
|
U_ASSERT(length <= 256);
|
|
return codePoint | ((length - 1) << 24);
|
|
}
|
|
};
|
|
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
//
|
|
// SpoofData
|
|
//
|
|
// A small class that wraps the raw (usually memory mapped) spoof data.
|
|
// Serves two primary functions:
|
|
// 1. Convenience. Contains real pointers to the data, to avoid dealing with
|
|
// the offsets in the raw data.
|
|
// 2. Reference counting. When a spoof checker is cloned, the raw data is shared
|
|
// and must be retained until all checkers using the data are closed.
|
|
// Nothing in this struct includes state that is specific to any particular
|
|
// USpoofDetector object.
|
|
//
|
|
//---------------------------------------------------------------------------------------
|
|
class SpoofData: public UMemory {
|
|
public:
|
|
static SpoofData* getDefault(UErrorCode &status); // Get standard ICU spoof data.
|
|
static void releaseDefault(); // Cleanup reference to default spoof data.
|
|
|
|
SpoofData(UErrorCode &status); // Create new spoof data wrapper.
|
|
// Only used when building new data from rules.
|
|
|
|
// Constructor for use when creating from prebuilt default data.
|
|
// A UDataMemory is what the ICU internal data loading functions provide.
|
|
// The udm is adopted by the SpoofData.
|
|
SpoofData(UDataMemory *udm, UErrorCode &status);
|
|
|
|
// Constructor for use when creating from serialized data.
|
|
//
|
|
SpoofData(const void *serializedData, int32_t length, UErrorCode &status);
|
|
|
|
// Check raw Spoof Data Version compatibility.
|
|
// Return TRUE it looks good.
|
|
UBool validateDataVersion(UErrorCode &status) const;
|
|
|
|
~SpoofData(); // Destructor not normally used.
|
|
// Use removeReference() instead.
|
|
// Reference Counting functions.
|
|
// Clone of a user-level spoof detector increments the ref count on the data.
|
|
// Close of a user-level spoof detector decrements the ref count.
|
|
// If the data is owned by us, it will be deleted when count goes to zero.
|
|
SpoofData *addReference();
|
|
void removeReference();
|
|
|
|
// Reset all fields to an initial state.
|
|
// Called from the top of all constructors.
|
|
void reset();
|
|
|
|
// Copy this instance's raw data buffer to the specified address.
|
|
int32_t serialize(void *buf, int32_t capacity, UErrorCode &status) const;
|
|
|
|
// Get the total number of bytes of data backed by this SpoofData.
|
|
// Not to be confused with length, which returns the number of confusable entries.
|
|
int32_t size() const;
|
|
|
|
// Get the confusable skeleton transform for a single code point.
|
|
// The result is a string with a length between 1 and 18 as of Unicode 9.
|
|
// This is the main public endpoint for this class.
|
|
// @return The length in UTF-16 code units of the substition string.
|
|
int32_t confusableLookup(UChar32 inChar, UnicodeString &dest) const;
|
|
|
|
// Get the number of confusable entries in this SpoofData.
|
|
int32_t length() const;
|
|
|
|
// Get the code point (key) at the specified index.
|
|
UChar32 codePointAt(int32_t index) const;
|
|
|
|
// Get the confusable skeleton (value) at the specified index.
|
|
// Append it to the specified UnicodeString&.
|
|
// @return The length in UTF-16 code units of the skeleton string.
|
|
int32_t appendValueTo(int32_t index, UnicodeString& dest) const;
|
|
|
|
private:
|
|
// Reserve space in the raw data. For use by builder when putting together a
|
|
// new set of data. Init the new storage to zero, to prevent inconsistent
|
|
// results if it is not all otherwise set by the requester.
|
|
// Return:
|
|
// pointer to the new space that was added by this function.
|
|
void *reserveSpace(int32_t numBytes, UErrorCode &status);
|
|
|
|
// initialize the pointers from this object to the raw data.
|
|
void initPtrs(UErrorCode &status);
|
|
|
|
SpoofDataHeader *fRawData; // Ptr to the raw memory-mapped data
|
|
UBool fDataOwned; // True if the raw data is owned, and needs
|
|
// to be deleted when refcount goes to zero.
|
|
UDataMemory *fUDM; // If not NULL, our data came from a
|
|
// UDataMemory, which we must close when
|
|
// we are done.
|
|
|
|
uint32_t fMemLimit; // Limit of available raw data space
|
|
u_atomic_int32_t fRefCount;
|
|
|
|
// Confusable data
|
|
int32_t *fCFUKeys;
|
|
uint16_t *fCFUValues;
|
|
UChar *fCFUStrings;
|
|
|
|
friend class ConfusabledataBuilder;
|
|
};
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
//
|
|
// Raw Binary Data Formats, as loaded from the ICU data file,
|
|
// or as built by the builder.
|
|
//
|
|
//---------------------------------------------------------------------------------------
|
|
struct SpoofDataHeader {
|
|
int32_t fMagic; // (0x3845fdef)
|
|
uint8_t fFormatVersion[4]; // Data Format. Same as the value in struct UDataInfo
|
|
// if there is one associated with this data.
|
|
int32_t fLength; // Total lenght in bytes of this spoof data,
|
|
// including all sections, not just the header.
|
|
|
|
// The following four sections refer to data representing the confusable data
|
|
// from the Unicode.org data from "confusables.txt"
|
|
|
|
int32_t fCFUKeys; // byte offset to Keys table (from SpoofDataHeader *)
|
|
int32_t fCFUKeysSize; // number of entries in keys table (32 bits each)
|
|
|
|
// TODO: change name to fCFUValues, for consistency.
|
|
int32_t fCFUStringIndex; // byte offset to String Indexes table
|
|
int32_t fCFUStringIndexSize; // number of entries in String Indexes table (16 bits each)
|
|
// (number of entries must be same as in Keys table
|
|
|
|
int32_t fCFUStringTable; // byte offset of String table
|
|
int32_t fCFUStringTableLen; // length of string table (in 16 bit UChars)
|
|
|
|
// The following sections are for data from xidmodifications.txt
|
|
|
|
int32_t unused[15]; // Padding, Room for Expansion
|
|
};
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
#endif /* __cplusplus */
|
|
|
|
/**
|
|
* Endianness swap function for binary spoof data.
|
|
* @internal
|
|
*/
|
|
U_CAPI int32_t U_EXPORT2
|
|
uspoof_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData,
|
|
UErrorCode *status);
|
|
|
|
|
|
#endif
|
|
|
|
#endif /* USPOOFIM_H */
|
|
|