1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is Mozilla Communicator client code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
38
#ifndef CharDistribution_h__
39
#define CharDistribution_h__
43
#define ENOUGH_DATA_THRESHOLD 1024
45
class CharDistributionAnalysis
48
CharDistributionAnalysis() {Reset();}
50
//feed a block of data and do distribution analysis
51
void HandleData(const char* aBuf, PRUint32 aLen) {}
53
//Feed a character with known length
54
void HandleOneChar(const char* aStr, PRUint32 aCharLen)
58
//we only care about 2-bytes character in our distribution analysis
59
order = (aCharLen == 2) ? GetOrder(aStr) : -1;
65
if ((PRUint32)order < mTableSize)
67
if (512 > mCharToFreqOrder[order])
73
//return confidence base on existing data
74
float GetConfidence(PRBool aIsPreferredLanguage);
76
//Reset analyser, clear any state
84
//This function is for future extension. Caller can use this function to control
88
//It is not necessary to receive all data to draw conclusion. For charset detection,
89
// certain amount of data is enough
90
PRBool GotEnoughData() {return mTotalChars > ENOUGH_DATA_THRESHOLD;}
93
//we do not handle character base on its original encoding string, but
94
//convert this encoding string to a number, here called order.
95
//This allow multiple encoding of a language to share one frequency table
96
virtual PRInt32 GetOrder(const char* str) {return -1;}
98
//If this flag is set to PR_TRUE, detection is done and conclusion has been made
101
//The number of characters whose frequency order is less than 512
104
//Total character encounted.
105
PRUint32 mTotalChars;
107
//Mapping table to get frequency order from char order (get from GetOrder())
108
const PRInt16 *mCharToFreqOrder;
110
//Size of above table
113
//This is a constant value varies from language to language, it is used in
114
//calculating confidence. See my paper for further detail.
115
float mTypicalDistributionRatio;
119
class EUCTWDistributionAnalysis: public CharDistributionAnalysis
122
EUCTWDistributionAnalysis();
125
//for euc-TW encoding, we are interested
126
// first byte range: 0xc4 -- 0xfe
127
// second byte range: 0xa1 -- 0xfe
128
//no validation needed here. State machine has done that
129
PRInt32 GetOrder(const char* str)
130
{ if ((unsigned char)*str >= (unsigned char)0xc4)
131
return 94*((unsigned char)str[0]-(unsigned char)0xc4) + (unsigned char)str[1] - (unsigned char)0xa1;
138
class EUCKRDistributionAnalysis : public CharDistributionAnalysis
141
EUCKRDistributionAnalysis();
143
//for euc-KR encoding, we are interested
144
// first byte range: 0xb0 -- 0xfe
145
// second byte range: 0xa1 -- 0xfe
146
//no validation needed here. State machine has done that
147
PRInt32 GetOrder(const char* str)
148
{ if ((unsigned char)*str >= (unsigned char)0xb0)
149
return 94*((unsigned char)str[0]-(unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
155
class GB2312DistributionAnalysis : public CharDistributionAnalysis
158
GB2312DistributionAnalysis();
160
//for GB2312 encoding, we are interested
161
// first byte range: 0xb0 -- 0xfe
162
// second byte range: 0xa1 -- 0xfe
163
//no validation needed here. State machine has done that
164
PRInt32 GetOrder(const char* str)
165
{ if ((unsigned char)*str >= (unsigned char)0xb0 && (unsigned char)str[1] >= (unsigned char)0xa1)
166
return 94*((unsigned char)str[0]-(unsigned char)0xb0) + (unsigned char)str[1] - (unsigned char)0xa1;
173
class Big5DistributionAnalysis : public CharDistributionAnalysis
176
Big5DistributionAnalysis();
178
//for big5 encoding, we are interested
179
// first byte range: 0xa4 -- 0xfe
180
// second byte range: 0x40 -- 0x7e , 0xa1 -- 0xfe
181
//no validation needed here. State machine has done that
182
PRInt32 GetOrder(const char* str)
183
{ if ((unsigned char)*str >= (unsigned char)0xa4)
184
if ((unsigned char)str[1] >= (unsigned char)0xa1)
185
return 157*((unsigned char)str[0]-(unsigned char)0xa4) + (unsigned char)str[1] - (unsigned char)0xa1 +63;
187
return 157*((unsigned char)str[0]-(unsigned char)0xa4) + (unsigned char)str[1] - (unsigned char)0x40;
193
class SJISDistributionAnalysis : public CharDistributionAnalysis
196
SJISDistributionAnalysis();
198
//for sjis encoding, we are interested
199
// first byte range: 0x81 -- 0x9f , 0xe0 -- 0xfe
200
// second byte range: 0x40 -- 0x7e, 0x81 -- oxfe
201
//no validation needed here. State machine has done that
202
PRInt32 GetOrder(const char* str)
205
if ((unsigned char)*str >= (unsigned char)0x81 && (unsigned char)*str <= (unsigned char)0x9f)
206
order = 188 * ((unsigned char)str[0]-(unsigned char)0x81);
207
else if ((unsigned char)*str >= (unsigned char)0xe0 && (unsigned char)*str <= (unsigned char)0xef)
208
order = 188 * ((unsigned char)str[0]-(unsigned char)0xe0 + 31);
211
order += (unsigned char)*(str+1) - 0x40;
212
if ((unsigned char)str[1] > (unsigned char)0x7f)
218
class EUCJPDistributionAnalysis : public CharDistributionAnalysis
221
EUCJPDistributionAnalysis();
223
//for euc-JP encoding, we are interested
224
// first byte range: 0xa0 -- 0xfe
225
// second byte range: 0xa1 -- 0xfe
226
//no validation needed here. State machine has done that
227
PRInt32 GetOrder(const char* str)
228
{ if ((unsigned char)*str >= (unsigned char)0xa0)
229
return 94*((unsigned char)str[0]-(unsigned char)0xa1) + (unsigned char)str[1] - (unsigned char)0xa1;
235
#endif //CharDistribution_h__