2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2009-03-09 23:40:15 +00:00
|
|
|
/*
|
|
|
|
***************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2008-2013, International Business Machines Corporation
|
|
|
|
* and others. All Rights Reserved.
|
2009-03-09 23:40:15 +00:00
|
|
|
***************************************************************************
|
|
|
|
*
|
|
|
|
* uspoof_impl.h
|
|
|
|
*
|
|
|
|
* Implemenation header for spoof detection
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef USPOOFIM_H
|
|
|
|
#define USPOOFIM_H
|
|
|
|
|
2016-09-13 22:15:13 +00:00
|
|
|
#include "uassert.h"
|
2009-03-09 23:40:15 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/uspoof.h"
|
|
|
|
#include "unicode/uscript.h"
|
|
|
|
#include "unicode/udata.h"
|
|
|
|
|
2013-02-11 04:51:14 +00:00
|
|
|
#include "utrie2.h"
|
2009-03-09 23:40:15 +00:00
|
|
|
|
2009-05-05 02:03:27 +00:00
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
|
|
|
2011-07-23 00:10:30 +00:00
|
|
|
#ifdef __cplusplus
|
2010-12-02 19:17:46 +00:00
|
|
|
|
2009-03-09 23:40:15 +00:00
|
|
|
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
|
|
|
|
|
2016-09-13 22:15:13 +00:00
|
|
|
// Magic number for sanity checking spoof checkers.
|
|
|
|
#define USPOOF_CHECK_MAGIC 0x2734ecde
|
|
|
|
|
2013-02-11 04:51:14 +00:00
|
|
|
class ScriptSet;
|
2009-03-09 23:40:15 +00:00
|
|
|
class SpoofData;
|
|
|
|
struct SpoofDataHeader;
|
2016-09-13 22:15:13 +00:00
|
|
|
class ConfusableDataUtils;
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class SpoofImpl corresponds directly to the plain C API opaque type
|
|
|
|
* USpoofChecker. One can be cast to the other.
|
|
|
|
*/
|
|
|
|
class SpoofImpl : public UObject {
|
|
|
|
public:
|
2016-09-13 22:15:13 +00:00
|
|
|
SpoofImpl(SpoofData *data, UErrorCode& status);
|
|
|
|
SpoofImpl(UErrorCode& status);
|
|
|
|
SpoofImpl();
|
|
|
|
void construct(UErrorCode& status);
|
|
|
|
virtual ~SpoofImpl();
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
/** Copy constructor, used by the user level uspoof_clone() function.
|
|
|
|
*/
|
|
|
|
SpoofImpl(const SpoofImpl &src, UErrorCode &status);
|
|
|
|
|
2016-09-13 22:15:13 +00:00
|
|
|
USpoofChecker *asUSpoofChecker();
|
2009-03-30 05:08:00 +00:00
|
|
|
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);
|
2009-03-09 23:40:15 +00:00
|
|
|
|
2016-09-13 22:15:13 +00:00
|
|
|
// 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;
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
/** parse a hex number. Untility used by the builders. */
|
2009-03-30 05:08:00 +00:00
|
|
|
static UChar32 ScanHex(const UChar *s, int32_t start, int32_t limit, UErrorCode &status);
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
static UClassID U_EXPORT2 getStaticClassID(void);
|
|
|
|
virtual UClassID getDynamicClassID(void) const;
|
|
|
|
|
2009-03-30 05:08:00 +00:00
|
|
|
//
|
|
|
|
// Data Members
|
|
|
|
//
|
2009-03-09 23:40:15 +00:00
|
|
|
|
2009-03-30 05:08:00 +00:00
|
|
|
int32_t fMagic; // Internal sanity check.
|
|
|
|
int32_t fChecks; // Bit vector of checks to perform.
|
2009-03-09 23:40:15 +00:00
|
|
|
|
2009-03-30 05:08:00 +00:00
|
|
|
SpoofData *fSpoofData;
|
|
|
|
|
2009-03-09 23:40:15 +00:00
|
|
|
const UnicodeSet *fAllowedCharsSet; // The UnicodeSet of allowed characters.
|
2016-09-13 22:15:13 +00:00
|
|
|
// for this Spoof Checker. Defaults to all chars.
|
2009-03-09 23:40:15 +00:00
|
|
|
|
2009-03-30 05:08:00 +00:00
|
|
|
const char *fAllowedLocales; // The list of allowed locales.
|
2013-02-11 04:51:14 +00:00
|
|
|
URestrictionLevel fRestrictionLevel; // The maximum restriction level for an acceptable identifier.
|
2009-03-09 23:40:15 +00:00
|
|
|
};
|
|
|
|
|
2016-09-13 22:15:13 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2016-09-20 21:06:55 +00:00
|
|
|
// Data Members
|
2016-09-13 22:15:13 +00:00
|
|
|
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.
|
|
|
|
};
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
2016-09-27 17:46:10 +00:00
|
|
|
// Confusable Mappings Data Structures, version 2.0
|
2009-03-09 23:40:15 +00:00
|
|
|
//
|
|
|
|
// 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
|
2016-09-13 22:15:13 +00:00
|
|
|
// bits 24-31 length of value string, in UChars (between 1 and 256 UChars).
|
2009-03-09 23:40:15 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2016-09-27 17:46:10 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
2009-03-09 23:40:15 +00:00
|
|
|
|
2016-09-13 22:15:13 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2009-03-09 23:40:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// 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:
|
2016-09-13 22:15:13 +00:00
|
|
|
static SpoofData* getDefault(UErrorCode &status); // Get standard ICU spoof data.
|
|
|
|
static void releaseDefault(); // Cleanup reference to default spoof data.
|
|
|
|
|
2009-03-09 23:40:15 +00:00
|
|
|
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.
|
2016-09-13 22:15:13 +00:00
|
|
|
UBool validateDataVersion(UErrorCode &status) const;
|
|
|
|
|
2009-03-09 23:40:15 +00:00
|
|
|
~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();
|
|
|
|
|
2016-09-13 22:15:13 +00:00
|
|
|
// 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:
|
2009-03-09 23:40:15 +00:00
|
|
|
// 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
|
2011-05-04 22:34:13 +00:00
|
|
|
// we are done.
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
uint32_t fMemLimit; // Limit of available raw data space
|
2013-08-30 01:23:42 +00:00
|
|
|
u_atomic_int32_t fRefCount;
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
// Confusable data
|
|
|
|
int32_t *fCFUKeys;
|
|
|
|
uint16_t *fCFUValues;
|
|
|
|
UChar *fCFUStrings;
|
|
|
|
|
2016-09-13 22:15:13 +00:00
|
|
|
friend class ConfusabledataBuilder;
|
|
|
|
};
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Raw Binary Data Formats, as loaded from the ICU data file,
|
|
|
|
// or as built by the builder.
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
struct SpoofDataHeader {
|
2010-12-02 19:17:46 +00:00
|
|
|
int32_t fMagic; // (0x3845fdef)
|
2009-03-09 23:40:15 +00:00
|
|
|
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
|
|
|
|
|
2016-09-13 22:15:13 +00:00
|
|
|
int32_t unused[15]; // Padding, Room for Expansion
|
|
|
|
};
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
2011-07-23 00:10:30 +00:00
|
|
|
#endif /* __cplusplus */
|
2009-03-09 23:40:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
|
2009-05-05 02:03:27 +00:00
|
|
|
#endif
|
|
|
|
|
2009-03-09 23:40:15 +00:00
|
|
|
#endif /* USPOOFIM_H */
|
|
|
|
|