src/nsUniversalDetector.cpp

filter: Statementstmt 53.1% 76 / 143 Branchbran 33.5% 43 / 128 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 43.9% 119 / 271 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/nsHebrewProber.cpp ↑ Summary src/nsEUCJPProber.cpp »
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is Mozilla Universal charset detector code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Shy Shalom <shooshX@gmail.com>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38  
39 #include "nscore.h"
40  
41 #include "nsUniversalDetector.h"
42  
43 #include "nsMBCSGroupProber.h"
44 #include "nsSBCSGroupProber.h"
45 #include "nsEscCharsetProber.h"
46 #include "nsLatin1Prober.h"
47  
48 4 nsUniversalDetector::nsUniversalDetector()
49 {
50 4 mDone = PR_FALSE;
51 4 mBestGuess = -1; //illegal value as signal
52 4 mInTag = PR_FALSE;
53 4 mEscCharSetProber = nsnull;
54  
55 4 mStart = PR_TRUE;
56 4 mDetectedCharset = nsnull;
57 4 mGotData = PR_FALSE;
58 4 mInputState = ePureAscii;
59 4 mLastChar = '\0';
60  
61 PRUint32 i;
62 16 ▶ for (i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < NUM_OF_CHARSET_PROBERS; i++) 12 4 16
63 12 mCharSetProbers[i] = nsnull;
64 4 }
65  
66 0 nsUniversalDetector::~nsUniversalDetector()
67 {
68 16 ▶ for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++) 12 4 16
69 12 ▶ if (mCharSetProbers[i])
Branch: 1 branch
BranchTrueFalseTotal
if (mCharSetProbers[i]) 12 0 12
70 12 ▶ delete mCharSetProbers[i];
Branch: 1 branch
BranchTrueFalseTotal
delete mCharSetProbers[i]; 12 0 12
71 4 ▶ if (mEscCharSetProber)
Branch: 1 branch
BranchTrueFalseTotal
if (mEscCharSetProber) 0 4 4
72 0 ▶ delete mEscCharSetProber;
Branch: 1 branch
BranchTrueFalseTotal
delete mEscCharSetProber; 0 0 0
73 0 }
74  
75 void
76 0 nsUniversalDetector::Reset()
77 {
78 0 mDone = PR_FALSE;
79 0 mBestGuess = -1; //illegal value as signal
80 0 mInTag = PR_FALSE;
81  
82 0 mStart = PR_TRUE;
83 0 mDetectedCharset = nsnull;
84 0 mGotData = PR_FALSE;
85 0 mInputState = ePureAscii;
86 0 mLastChar = '\0';
87  
88 0 ▶ if (mEscCharSetProber)
Branch: 1 branch
BranchTrueFalseTotal
if (mEscCharSetProber) 0 0 0
89 0 mEscCharSetProber->Reset();
90  
91 PRUint32 i;
92 0 ▶ for (i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < NUM_OF_CHARSET_PROBERS; i++) 0 0 0
93 0 ▶ if (mCharSetProbers[i])
Branch: 1 branch
BranchTrueFalseTotal
if (mCharSetProbers[i]) 0 0 0
94 0 mCharSetProbers[i]->Reset();
95 0 }
96  
97 //---------------------------------------------------------------------
98 #define SHORTCUT_THRESHOLD (float)0.95
99 #define MINIMUM_THRESHOLD (float)0.20
100  
101 4 nsresult nsUniversalDetector::HandleData(const char* aBuf, PRUint32 aLen)
102 {
103 4 ▶ if(mDone)
Branch: 1 branch
BranchTrueFalseTotal
if(mDone) 0 4 4
104 0 return NS_OK;
105  
106 4 ▶ if (aLen > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (aLen > 0) 4 0 4
107 4 mGotData = PR_TRUE;
108  
109 //If the data starts with BOM, we know it is UTF
110 4 ▶ if (mStart)
Branch: 1 branch
BranchTrueFalseTotal
if (mStart) 4 0 4
111 {
112 4 mStart = PR_FALSE;
113 4 ▶ if (aLen > 3)
Branch: 1 branch
BranchTrueFalseTotal
if (aLen > 3) 4 0 4
114 4 switch (aBuf[0])
115 {
116 0 case '\xEF':
117 0 ▶ if (('\xBB' == aBuf[1]) && ('\xBF' == aBuf[2]))
Branch: 2 branches
BranchTrueFalseTotal
if (('\xBB' == aBuf[1]) && ('\xBF' == aBuf[2])) 0 0 0
if (('\xBB' == aBuf[1]) && ('\xBF' == aBuf[2])) 0 0 0
118 // EF BB BF UTF-8 encoded BOM
119 0 mDetectedCharset = "UTF-8";
120 0 break;
121 0 case '\xFE':
122 0 ▶ if (('\xFF' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3]))
Branch: 3 branches
BranchTrueFalseTotal
if (('\xFF' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3])) 0 0 0
if (('\xFF' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3])) 0 0 0
if (('\xFF' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3])) 0 0 0
123 // FE FF 00 00 UCS-4, unusual octet order BOM (3412)
124 0 mDetectedCharset = "X-ISO-10646-UCS-4-3412";
125 0 ▶ else if ('\xFF' == aBuf[1])
Branch: 1 branch
BranchTrueFalseTotal
else if ('\xFF' == aBuf[1]) 0 0 0
126 // FE FF UTF-16, big endian BOM
127 0 mDetectedCharset = "UTF-16BE";
128 0 break;
129 0 case '\x00':
130 0 ▶ if (('\x00' == aBuf[1]) && ('\xFE' == aBuf[2]) && ('\xFF' == aBuf[3]))
Branch: 3 branches
BranchTrueFalseTotal
if (('\x00' == aBuf[1]) && ('\xFE' == aBuf[2]) && ('\xFF' == aBuf[3])) 0 0 0
if (('\x00' == aBuf[1]) && ('\xFE' == aBuf[2]) && ('\xFF' == aBuf[3])) 0 0 0
if (('\x00' == aBuf[1]) && ('\xFE' == aBuf[2]) && ('\xFF' == aBuf[3])) 0 0 0
131 // 00 00 FE FF UTF-32, big-endian BOM
132 0 mDetectedCharset = "UTF-32BE";
133 0 ▶ else if (('\x00' == aBuf[1]) && ('\xFF' == aBuf[2]) && ('\xFE' == aBuf[3]))
Branch: 3 branches
BranchTrueFalseTotal
else if (('\x00' == aBuf[1]) && ('\xFF' == aBuf[2]) && ('\xFE' == aBuf[3])) 0 0 0
else if (('\x00' == aBuf[1]) && ('\xFF' == aBuf[2]) && ('\xFE' == aBuf[3])) 0 0 0
else if (('\x00' == aBuf[1]) && ('\xFF' == aBuf[2]) && ('\xFE' == aBuf[3])) 0 0 0
134 // 00 00 FF FE UCS-4, unusual octet order BOM (2143)
135 0 mDetectedCharset = "X-ISO-10646-UCS-4-2143";
136 0 break;
137 0 case '\xFF':
138 0 ▶ if (('\xFE' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3]))
Branch: 3 branches
BranchTrueFalseTotal
if (('\xFE' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3])) 0 0 0
if (('\xFE' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3])) 0 0 0
if (('\xFE' == aBuf[1]) && ('\x00' == aBuf[2]) && ('\x00' == aBuf[3])) 0 0 0
139 // FF FE 00 00 UTF-32, little-endian BOM
140 0 mDetectedCharset = "UTF-32LE";
141 0 ▶ else if ('\xFE' == aBuf[1])
Branch: 1 branch
BranchTrueFalseTotal
else if ('\xFE' == aBuf[1]) 0 0 0
142 // FF FE UTF-16, little endian BOM
143 0 mDetectedCharset = "UTF-16LE";
144 0 break;
145 } // switch
146  
147 4 ▶ if (mDetectedCharset)
Branch: 1 branch
BranchTrueFalseTotal
if (mDetectedCharset) 0 4 4
148 {
149 0 mDone = PR_TRUE;
150 0 return NS_OK;
151 }
152 }
153
154 PRUint32 i;
155 50 ▶ for (i = 0; i < aLen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < aLen; i++) 46 4 50
156 {
157 //other than 0xa0, if every othe character is ascii, the page is ascii
158 46 ▶ if (aBuf[i] & '\x80' && aBuf[i] != '\xA0') //Since many Ascii only page contains NBSP
Branch: 2 branches
BranchTrueFalseTotal
if (aBuf[i] & '\x80' && aBuf[i] != '\xA0') //Since many Ascii only page contains NBSP 28 18 46
if (aBuf[i] & '\x80' && aBuf[i] != '\xA0') //Since many Ascii only page contains NBSP 28 0 28
159 {
160 //we got a non-ascii byte (high-byte)
161 28 ▶ if (mInputState != eHighbyte)
Branch: 1 branch
BranchTrueFalseTotal
if (mInputState != eHighbyte) 4 24 28
162 {
163 //adjust state
164 4 mInputState = eHighbyte;
165  
166 //kill mEscCharSetProber if it is active
167 4 ▶ if (mEscCharSetProber) {
Branch: 1 branch
BranchTrueFalseTotal
if (mEscCharSetProber) { 0 4 4
168 0 ▶ delete mEscCharSetProber;
Branch: 1 branch
BranchTrueFalseTotal
delete mEscCharSetProber; 0 0 0
169 0 mEscCharSetProber = nsnull;
170 }
171  
172 //start multibyte and singlebyte charset prober
173 4 ▶ if (nsnull == mCharSetProbers[0])
Branch: 1 branch
BranchTrueFalseTotal
if (nsnull == mCharSetProbers[0]) 4 0 4
174 4 ▶ mCharSetProbers[0] = new nsMBCSGroupProber;
Branch: 2 branches
BranchTrueFalseTotal
mCharSetProbers[0] = new nsMBCSGroupProber; 4 0 4
mCharSetProbers[0] = new nsMBCSGroupProber; 0 0 0
175 4 ▶ if (nsnull == mCharSetProbers[1])
Branch: 1 branch
BranchTrueFalseTotal
if (nsnull == mCharSetProbers[1]) 4 0 4
176 4 ▶ mCharSetProbers[1] = new nsSBCSGroupProber;
Branch: 2 branches
BranchTrueFalseTotal
mCharSetProbers[1] = new nsSBCSGroupProber; 4 0 4
mCharSetProbers[1] = new nsSBCSGroupProber; 0 0 0
177 4 ▶ if (nsnull == mCharSetProbers[2])
Branch: 1 branch
BranchTrueFalseTotal
if (nsnull == mCharSetProbers[2]) 4 0 4
178 4 ▶ mCharSetProbers[2] = new nsLatin1Prober;
Branch: 2 branches
BranchTrueFalseTotal
mCharSetProbers[2] = new nsLatin1Prober; 4 0 4
mCharSetProbers[2] = new nsLatin1Prober; 0 0 0
179  
180 4 ▶ if ((nsnull == mCharSetProbers[0]) ||
Branch: 1 branch
BranchTrueFalseTotal
if ((nsnull == mCharSetProbers[0]) || 4 0 4
181 4 ▶ (nsnull == mCharSetProbers[1]) ||
Branch: 1 branch
BranchTrueFalseTotal
(nsnull == mCharSetProbers[1]) || 4 0 4
182 4 ▶ (nsnull == mCharSetProbers[2]))
Branch: 1 branch
BranchTrueFalseTotal
(nsnull == mCharSetProbers[2])) 0 4 4
183 0 return NS_ERROR_OUT_OF_MEMORY;
184 }
185 }
186 else
187 {
188 //ok, just pure ascii so far
189 18 ▶ if ( ePureAscii == mInputState &&
Branch: 1 branch
BranchTrueFalseTotal
if ( ePureAscii == mInputState && 2 16 18
190 2 ▶ (aBuf[i] == '\033' || (aBuf[i] == '{' && mLastChar == '~')) )
Branch: 3 branches
BranchTrueFalseTotal
(aBuf[i] == '\033' || (aBuf[i] == '{' && mLastChar == '~')) ) 2 0 2
(aBuf[i] == '\033' || (aBuf[i] == '{' && mLastChar == '~')) ) 0 2 2
(aBuf[i] == '\033' || (aBuf[i] == '{' && mLastChar == '~')) ) 0 0 0
191 {
192 //found escape character or HZ "~{"
193 0 mInputState = eEscAscii;
194 }
195 18 mLastChar = aBuf[i];
196 }
197 }
198  
199 nsProbingState st;
200 4 switch (mInputState)
201 {
202 0 case eEscAscii:
203 0 ▶ if (nsnull == mEscCharSetProber) {
Branch: 1 branch
BranchTrueFalseTotal
if (nsnull == mEscCharSetProber) { 0 0 0
204 0 ▶ mEscCharSetProber = new nsEscCharSetProber;
Branch: 2 branches
BranchTrueFalseTotal
mEscCharSetProber = new nsEscCharSetProber; 0 0 0
mEscCharSetProber = new nsEscCharSetProber; 0 0 0
205 0 ▶ if (nsnull == mEscCharSetProber)
Branch: 1 branch
BranchTrueFalseTotal
if (nsnull == mEscCharSetProber) 0 0 0
206 0 return NS_ERROR_OUT_OF_MEMORY;
207 }
208 0 st = mEscCharSetProber->HandleData(aBuf, aLen);
209 0 ▶ if (st == eFoundIt)
Branch: 1 branch
BranchTrueFalseTotal
if (st == eFoundIt) 0 0 0
210 {
211 0 mDone = PR_TRUE;
212 0 mDetectedCharset = mEscCharSetProber->GetCharSetName();
213 }
214 0 break;
215 4 case eHighbyte:
216 16 ▶ for (i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < NUM_OF_CHARSET_PROBERS; i++) 12 4 16
217 {
218 12 st = mCharSetProbers[i]->HandleData(aBuf, aLen);
219 12 ▶ if (st == eFoundIt)
Branch: 1 branch
BranchTrueFalseTotal
if (st == eFoundIt) 0 12 12
220 {
221 0 mDone = PR_TRUE;
222 0 mDetectedCharset = mCharSetProbers[i]->GetCharSetName();
223 0 return NS_OK;
224 }
225 }
226 4 break;
227  
228 4 default: //pure ascii
229 ;//do nothing here
230 }
231 4 return NS_OK;
232 }
233  
234  
235 //---------------------------------------------------------------------
236 4 void nsUniversalDetector::DataEnd()
237 {
238 4 ▶ if (!mGotData)
Branch: 1 branch
BranchTrueFalseTotal
if (!mGotData) 0 4 4
239 {
240 // we haven't got any data yet, return immediately
241 // caller program sometimes call DataEnd before anything has been sent to detector
242 0 return;
243 }
244  
245 4 ▶ if (mDetectedCharset)
Branch: 1 branch
BranchTrueFalseTotal
if (mDetectedCharset) 0 4 4
246 {
247 0 mDone = PR_TRUE;
248 0 Report(mDetectedCharset);
249 0 return;
250 }
251
252 4 switch (mInputState)
253 {
254 4 case eHighbyte:
255 {
256 float proberConfidence;
257 4 float maxProberConfidence = (float)0.0;
258 4 PRInt32 maxProber = 0;
259  
260 16 ▶ for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (PRInt32 i = 0; i < NUM_OF_CHARSET_PROBERS; i++) 12 4 16
261 {
262 12 proberConfidence = mCharSetProbers[i]->GetConfidence();
263 12 ▶ if (proberConfidence > maxProberConfidence)
Branch: 1 branch
BranchTrueFalseTotal
if (proberConfidence > maxProberConfidence) 4 8 12
264 {
265 4 maxProberConfidence = proberConfidence;
266 4 maxProber = i;
267 }
268 }
269 //do not report anything because we are not confident of it, that's in fact a negative answer
270 4 ▶ if (maxProberConfidence > MINIMUM_THRESHOLD)
Branch: 1 branch
BranchTrueFalseTotal
if (maxProberConfidence > MINIMUM_THRESHOLD) 4 0 4
271 4 Report(mCharSetProbers[maxProber]->GetCharSetName());
272 }
273 4 break;
274 0 case eEscAscii:
275 0 break;
276 4 default:
277 ;
278 }
279 4 return;
280 }
« src/nsHebrewProber.cpp ↑ Summary src/nsEUCJPProber.cpp »