extensions/universalchardet/src/base/CharDistribution.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef CharDistribution_h__
michael@0 7 #define CharDistribution_h__
michael@0 8
michael@0 9 #include "nscore.h"
michael@0 10
michael@0 11 #define ENOUGH_DATA_THRESHOLD 1024
michael@0 12
michael@0 13 #define MINIMUM_DATA_THRESHOLD 4
michael@0 14
michael@0 15 class CharDistributionAnalysis
michael@0 16 {
michael@0 17 public:
michael@0 18 CharDistributionAnalysis() {Reset(false);}
michael@0 19
michael@0 20 //feed a block of data and do distribution analysis
michael@0 21 void HandleData(const char* aBuf, uint32_t aLen) {}
michael@0 22
michael@0 23 //Feed a character with known length
michael@0 24 void HandleOneChar(const char* aStr, uint32_t aCharLen)
michael@0 25 {
michael@0 26 int32_t order;
michael@0 27
michael@0 28 //we only care about 2-bytes character in our distribution analysis
michael@0 29 order = (aCharLen == 2) ? GetOrder(aStr) : -1;
michael@0 30
michael@0 31 if (order >= 0)
michael@0 32 {
michael@0 33 mTotalChars++;
michael@0 34 //order is valid
michael@0 35 if ((uint32_t)order < mTableSize)
michael@0 36 {
michael@0 37 if (512 > mCharToFreqOrder[order])
michael@0 38 mFreqChars++;
michael@0 39 }
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 //return confidence base on existing data
michael@0 44 float GetConfidence(void);
michael@0 45
michael@0 46 //Reset analyser, clear any state
michael@0 47 void Reset(bool aIsPreferredLanguage)
michael@0 48 {
michael@0 49 mDone = false;
michael@0 50 mTotalChars = 0;
michael@0 51 mFreqChars = 0;
michael@0 52 mDataThreshold = aIsPreferredLanguage ? 0 : MINIMUM_DATA_THRESHOLD;
michael@0 53 }
michael@0 54
michael@0 55 //It is not necessary to receive all data to draw conclusion. For charset detection,
michael@0 56 // certain amount of data is enough
michael@0 57 bool GotEnoughData() {return mTotalChars > ENOUGH_DATA_THRESHOLD;}
michael@0 58
michael@0 59 protected:
michael@0 60 //we do not handle character base on its original encoding string, but
michael@0 61 //convert this encoding string to a number, here called order.
michael@0 62 //This allow multiple encoding of a language to share one frequency table
michael@0 63 virtual int32_t GetOrder(const char* str) {return -1;}
michael@0 64
michael@0 65 //If this flag is set to true, detection is done and conclusion has been made
michael@0 66 bool mDone;
michael@0 67
michael@0 68 //The number of characters whose frequency order is less than 512
michael@0 69 uint32_t mFreqChars;
michael@0 70
michael@0 71 //Total character encounted.
michael@0 72 uint32_t mTotalChars;
michael@0 73
michael@0 74 //Number of hi-byte characters needed to trigger detection
michael@0 75 uint32_t mDataThreshold;
michael@0 76
michael@0 77 //Mapping table to get frequency order from char order (get from GetOrder())
michael@0 78 const int16_t *mCharToFreqOrder;
michael@0 79
michael@0 80 //Size of above table
michael@0 81 uint32_t mTableSize;
michael@0 82
michael@0 83 //This is a constant value varies from language to language, it is used in
michael@0 84 //calculating confidence. See my paper for further detail.
michael@0 85 float mTypicalDistributionRatio;
michael@0 86 };
michael@0 87
michael@0 88
michael@0 89 class EUCTWDistributionAnalysis: public CharDistributionAnalysis
michael@0 90 {
michael@0 91 public:
michael@0 92 EUCTWDistributionAnalysis();
michael@0 93 protected:
michael@0 94
michael@0 95 //for euc-TW encoding, we are interested
michael@0 96 // first byte range: 0xc4 -- 0xfe
michael@0 97 // second byte range: 0xa1 -- 0xfe
michael@0 98 //no validation needed here. State machine has done that
michael@0 99 int32_t GetOrder(const char* str)
michael@0 100 { if ((unsigned char)*str >= (unsigned char)0xc4)
michael@0 101 return 94*((unsigned char)str[0]-(unsigned char)0xc4) + (unsigned char)str[1] - (unsigned char)0xa1;
michael@0 102 else
michael@0 103 return -1;
michael@0 104 }
michael@0 105 };
michael@0 106
michael@0 107
michael@0 108 class EUCKRDistributionAnalysis : public CharDistributionAnalysis
michael@0 109 {
michael@0 110 public:
michael@0 111 EUCKRDistributionAnalysis();
michael@0 112 protected:
michael@0 113 //for euc-KR encoding, we are interested
michael@0 114 // first byte range: 0xb0 -- 0xfe
michael@0 115 // second byte range: 0xa1 -- 0xfe
michael@0 116 //no validation needed here. State machine has done that
michael@0 117 int32_t GetOrder(const char* str)
michael@0 118 { if ((unsigned char)*str >= (unsigned char)0xb0)
michael@0 119 return 94*((unsigned char)str[0]-(unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
michael@0 120 else
michael@0 121 return -1;
michael@0 122 }
michael@0 123 };
michael@0 124
michael@0 125 class GB2312DistributionAnalysis : public CharDistributionAnalysis
michael@0 126 {
michael@0 127 public:
michael@0 128 GB2312DistributionAnalysis();
michael@0 129 protected:
michael@0 130 //for GB2312 encoding, we are interested
michael@0 131 // first byte range: 0xb0 -- 0xfe
michael@0 132 // second byte range: 0xa1 -- 0xfe
michael@0 133 //no validation needed here. State machine has done that
michael@0 134 int32_t GetOrder(const char* str)
michael@0 135 { if ((unsigned char)*str >= (unsigned char)0xb0 && (unsigned char)str[1] >= (unsigned char)0xa1)
michael@0 136 return 94*((unsigned char)str[0]-(unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
michael@0 137 else
michael@0 138 return -1;
michael@0 139 }
michael@0 140 };
michael@0 141
michael@0 142
michael@0 143 class Big5DistributionAnalysis : public CharDistributionAnalysis
michael@0 144 {
michael@0 145 public:
michael@0 146 Big5DistributionAnalysis();
michael@0 147 protected:
michael@0 148 //for big5 encoding, we are interested
michael@0 149 // first byte range: 0xa4 -- 0xfe
michael@0 150 // second byte range: 0x40 -- 0x7e , 0xa1 -- 0xfe
michael@0 151 //no validation needed here. State machine has done that
michael@0 152 int32_t GetOrder(const char* str)
michael@0 153 { if ((unsigned char)*str >= (unsigned char)0xa4)
michael@0 154 if ((unsigned char)str[1] >= (unsigned char)0xa1)
michael@0 155 return 157*((unsigned char)str[0]-(unsigned char)0xa4) + (unsigned char)str[1] - (unsigned char)0xa1 +63;
michael@0 156 else
michael@0 157 return 157*((unsigned char)str[0]-(unsigned char)0xa4) + (unsigned char)str[1] - (unsigned char)0x40;
michael@0 158 else
michael@0 159 return -1;
michael@0 160 }
michael@0 161 };
michael@0 162
michael@0 163 class SJISDistributionAnalysis : public CharDistributionAnalysis
michael@0 164 {
michael@0 165 public:
michael@0 166 SJISDistributionAnalysis();
michael@0 167 protected:
michael@0 168 //for sjis encoding, we are interested
michael@0 169 // first byte range: 0x81 -- 0x9f , 0xe0 -- 0xfe
michael@0 170 // second byte range: 0x40 -- 0x7e, 0x81 -- oxfe
michael@0 171 //no validation needed here. State machine has done that
michael@0 172 int32_t GetOrder(const char* str)
michael@0 173 {
michael@0 174 int32_t order;
michael@0 175 if ((unsigned char)*str >= (unsigned char)0x81 && (unsigned char)*str <= (unsigned char)0x9f)
michael@0 176 order = 188 * ((unsigned char)str[0]-(unsigned char)0x81);
michael@0 177 else if ((unsigned char)*str >= (unsigned char)0xe0 && (unsigned char)*str <= (unsigned char)0xef)
michael@0 178 order = 188 * ((unsigned char)str[0]-(unsigned char)0xe0 + 31);
michael@0 179 else
michael@0 180 return -1;
michael@0 181 order += (unsigned char)*(str+1) - 0x40;
michael@0 182 if ((unsigned char)str[1] > (unsigned char)0x7f)
michael@0 183 order--;
michael@0 184 return order;
michael@0 185 }
michael@0 186 };
michael@0 187
michael@0 188 class EUCJPDistributionAnalysis : public CharDistributionAnalysis
michael@0 189 {
michael@0 190 public:
michael@0 191 EUCJPDistributionAnalysis();
michael@0 192 protected:
michael@0 193 //for euc-JP encoding, we are interested
michael@0 194 // first byte range: 0xa0 -- 0xfe
michael@0 195 // second byte range: 0xa1 -- 0xfe
michael@0 196 //no validation needed here. State machine has done that
michael@0 197 int32_t GetOrder(const char* str)
michael@0 198 { if ((unsigned char)*str >= (unsigned char)0xa0)
michael@0 199 return 94*((unsigned char)str[0]-(unsigned char)0xa1) + (unsigned char)str[1] - (unsigned char)0xa1;
michael@0 200 else
michael@0 201 return -1;
michael@0 202 }
michael@0 203 };
michael@0 204
michael@0 205 #endif //CharDistribution_h__
michael@0 206

mercurial