Debian Codegraph @

components/chsdet/src/nsCore.pas

raw
1 // +----------------------------------------------------------------------+
2 // | chsdet - Charset Detector Library |
3 // +----------------------------------------------------------------------+
4 // | Copyright (C) 2006, Nick Yakowlew http://chsdet.sourceforge.net |
5 // +----------------------------------------------------------------------+
6 // | Based on Mozilla sources http://www.mozilla.org/projects/intl/ |
7 // +----------------------------------------------------------------------+
8 // | This library is free software; you can redistribute it and/or modify |
9 // | it under the terms of the GNU General Public License as published by |
10 // | the Free Software Foundation; either version 2 of the License, or |
11 // | (at your option) any later version. |
12 // | This library is distributed in the hope that it will be useful |
13 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
15 // | See the GNU Lesser General Public License for more details. |
16 // | http://www.opensource.org/licenses/lgpl-license.php |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id: nsCore.pas,v 1.5 2013/04/23 19:47:10 ya_nick Exp $
20
21 unit nsCore;
22
23 interface
24
25 type
26 int16 = smallint;
27 int32 = integer;
28 uInt32 = cardinal;
29
30 aByteArray = array of Byte;
31 puInt32 = ^uInt32;
32 auInt32 = array of uInt32;
33
34 pInt16 = ^int16;
35 aInt16 = array of int16;
36
37 const
38 SURE_YES: double = 0.99;
39 SURE_NO: double = 0.01;
40 const
41 ENOUGH_DATA_THRESHOLD: cardinal = 1024;
42 SHORTCUT_THRESHOLD = 0.95;
43
44 type
45 eProbingState = (
46 psDetecting = 0, //We are still detecting, no sure answer yet, but caller can ask for confidence.
47 psFoundIt = 1, //That's a positive answer
48 psNotMe = 2 //Negative answer
49 );
50
51 type
52 nsResult = uInt32;
53 const
54 NS_OK = 0;
55 NS_ERROR_OUT_OF_MEMORY = $8007000e;
56
57 type
58 float = double;
59
60 rAboutHolder = record
61 MajorVersionNr: Cardinal;
62 MinorVersionNr: Cardinal;
63 BuildVersionNr: Cardinal;
64 About: pChar;
65 end;
66
67 eBOMKind = (
68 BOM_Not_Found,
69 BOM_UCS4_BE, // 00 00 FE FF UCS-4, big-endian machine (1234 order)
70 BOM_UCS4_LE, // FF FE 00 00 UCS-4, little-endian machine (4321 order)
71 BOM_UCS4_2143, // 00 00 FF FE UCS-4, unusual octet order (2143)
72 BOM_UCS4_3412, // FE FF 00 00 UCS-4, unusual octet order (3412)
73 BOM_UTF16_BE, // FE FF ## ## UTF-16, big-endian
74 BOM_UTF16_LE, // FF FE ## ## UTF-16, little-endian
75 BOM_UTF8 // EF BB BF UTF-8
76 );
77
78 rBOMDef = record
79 Length: integer;
80 BOM: array [0..3] of AnsiChar;
81 end;
82 const
83 KNOWN_BOM: array [eBOMKind] of rBOMDef = (
84 (Length: 00; BOM: (#$00, #$00, #$00, #$00)),
85 (Length: 04; BOM: (#$00, #$00, #$FE, #$FF)),
86 (Length: 04; BOM: (#$FF, #$FE, #$00, #$00)),
87 (Length: 04; BOM: (#$00, #$00, #$FF, #$FE)),
88 (Length: 04; BOM: (#$FE, #$FF, #$00, #$00)),
89 (Length: 02; BOM: (#$FE, #$FF, #$00, #$00)),
90 (Length: 02; BOM: (#$FF, #$FE, #$00, #$00)),
91 (Length: 03; BOM: (#$EF, #$BB, #$BF, #$00))
92 );
93
94 // "extended" charset info
95 type
96 rCharsetInfo = record
97 Name: PAnsiChar;
98 CodePage: Integer;
99 Language: PAnsiChar;
100 end;
101
102 eInternalCharsetID = (
103 UNKNOWN_CHARSET = 000,
104 PURE_ASCII_CHARSET = 001,
105 UTF8_CHARSET = 002,
106 UCS4_BE_CHARSET = 003,
107 UTF16_BE_CHARSET = 004,
108 UTF32_BE_CHARSET = 005,
109 UCS4_LE_CHARSET = 006,
110 UTF32_LE_CHARSET = 007,
111 UTF16_LE_CHARSET = 008,
112 LATIN5_BULGARIAN_CHARSET = 009,
113 WINDOWS_BULGARIAN_CHARSET = 010,
114 KOI8_R_CHARSET = 011,
115 WINDOWS_1251_CHARSET = 012,
116 ISO_8859_5_CHARSET = 013,
117 X_MAC_CYRILLIC_CHARSET = 014,
118 IBM866_CHARSET = 015,
119 IBM855_CHARSET = 016,
120 ISO_8859_7_CHARSET = 017,
121 WINDOWS_1253_CHARSET = 018,
122 ISO_8859_8_CHARSET = 019,
123 WINDOWS_1255_CHARSET = 020,
124 BIG5_CHARSET = 021,
125 ISO_2022_CN_CHARSET = 022,
126 ISO_2022_JP_CHARSET = 023,
127 ISO_2022_KR_CHARSET = 024,
128 EUC_JP_CHARSET = 025,
129 EUC_KR_CHARSET = 026,
130 X_EUC_TW_CHARSET = 027,
131 SHIFT_JIS_CHARSET = 028,
132 GB18030_CHARSET = 029,
133 HZ_GB_2312_CHARSET = 030,
134 WINDOWS_1252_CHARSET = 031
135 );
136
137 const
138 KNOWN_CHARSETS: array [eInternalCharsetID] of rCharsetInfo = (
139 // UNKNOWN_CHARSET
140 (
141 Name: 'Unknown';
142 CodePage: -1;
143 Language: 'Unknown'
144 ),
145 // PURE_ASCII_CHARSET
146 (
147 Name: 'ASCII';
148 CodePage: 0;
149 Language: 'ASCII'
150 ),
151 // UTF8_CHARSET
152 (
153 Name: 'UTF-8';
154 CodePage: 65001;
155 Language: 'Unicode'
156 ),
157 // UCS4_BE_CHARSET
158 (
159 Name: 'X-ISO-10646-UCS-4-3412';
160 CodePage: 12001;
161 Language: 'Unicode'
162 ),
163 // UTF16_BE_CHARSET
164 (
165 Name: 'UTF-16BE';
166 CodePage: 1201;
167 Language: 'Unicode'
168 ),
169 // UTF32_BE_CHARSET
170 (
171 Name: 'UTF-32BE';
172 CodePage: 12001;
173 Language: 'Unicode'
174 ),
175 // UCS4_LE_CHARSET
176 (
177 Name: 'X-ISO-10646-UCS-4-2143';
178 CodePage: 12000;
179 Language: 'Unicode'
180 ),
181 // UTF32_LE_CHARSET
182 (
183 Name: 'UTF-32LE';
184 CodePage: 12000;
185 Language: 'Unicode'
186 ),
187 // UTF16_LE_CHARSET
188 (
189 Name: 'UTF-16LE';
190 CodePage: 1200;
191 Language: 'Unicode'
192 ),
193 // LATIN5_BULGARIAN_CHARSET
194 (
195 Name: 'ISO-8859-5';
196 CodePage: 28595;
197 Language: 'Bulgarian'
198 ),
199 // WINDOWS_BULGARIAN_CHARSET
200 (
201 Name: 'windows-1251';
202 CodePage: 1251;
203 Language: 'Bulgarian'
204 ),
205 // KOI8_R_CHARSET
206 (
207 Name: 'KOI8-R';
208 CodePage: 20866;
209 Language: 'russian'
210 ),
211 // WINDOWS_1251_CHARSET
212 (
213 Name: 'windows-1251';
214 CodePage: 1251;
215 Language: 'russian'
216 ),
217 // ISO_8859_5_CHARSET
218 (
219 Name: 'ISO-8859-5';
220 CodePage: 28595;
221 Language: 'russian'
222 ),
223 // X_MAC_CYRILLIC_CHARSET
224 (
225 Name: 'x-mac-cyrillic';
226 CodePage: 10007;
227 Language: 'russian'
228 ),
229 // IBM866_CHARSET
230 (
231 Name: 'IBM866';
232 CodePage: 866;
233 Language: 'russian'
234 ),
235 // IBM855_CHARSET
236 (
237 Name: 'IBM855';
238 CodePage: 855;
239 Language: 'russian'
240 ),
241 // ISO_8859_7_CHARSET
242 (
243 Name: 'ISO-8859-7';
244 CodePage: 28597;
245 Language: 'greek'
246 ),
247 // WINDOWS_1253_CHARSET
248 (
249 Name: 'windows-1253';
250 CodePage: 1253;
251 Language: 'greek'
252 ),
253 // ISO_8859_8_CHARSET
254 (
255 Name: 'ISO-8859-8';
256 CodePage: 28598;
257 Language: 'hebrew'
258 ),
259 // WINDOWS_1255_CHARSET
260 (
261 Name: 'windows-1255';
262 CodePage: 1255;
263 Language: 'hebrew'
264 ),
265 // BIG5_CHARSET
266 (
267 Name: 'Big5';
268 CodePage: 950;
269 Language: 'ch'
270 ),
271 // ISO_2022_CN_CHARSET
272 (
273 Name: 'ISO-2022-CN';
274 CodePage: 50227;
275 Language: 'ch';
276 ),
277 // ISO_2022_JP_CHARSET
278 (
279 Name: 'ISO-2022-JP';
280 CodePage: 50222;
281 Language: 'japanese';
282 ),
283 // ISO_2022_KR_CHARSET
284 (
285 Name: 'ISO-2022-KR';
286 CodePage: 50225;
287 Language: 'kr';
288 ),
289 // EUC_JP_CHARSET
290 (
291 Name: 'EUC-JP';
292 CodePage: 51932;
293 Language: 'japanese';
294 ),
295 // EUC_KR_CHARSET
296 (
297 Name: 'EUC-KR';
298 CodePage: 51949;
299 Language: 'kr';
300 ),
301 // X_EUC_TW_CHARSET
302 (
303 Name: 'x-euc-tw';
304 CodePage: 51936;
305 Language: 'ch';
306 ),
307 // SHIFT_JIS_CHARSET
308 (
309 Name: 'Shift_JIS';
310 CodePage: 932;
311 Language: 'japanese';
312 ),
313 // GB18030_CHARSET
314 (
315 Name: 'GB18030';
316 CodePage: 54936;
317 Language: 'ch';
318 ),
319 // HZ_GB_2312_CHARSET
320 (
321 Name: 'HZ-GB-2312';
322 CodePage: 52936;
323 Language: 'ch';
324 ),
325 // WINDOWS_1252_CHARSET
326 (
327 Name: 'windows-1252';
328 CodePage: 1252;
329 Language: 'eu';
330 )
331
332 );
333
334 (* Helper functions used in the Latin1 and Group probers.*)
335 (* both functions Allocate a new buffer for newBuf. This buffer should be *)
336 (* freed by the caller using PR_FREEIF.*)
337 (* Both functions return PR_FALSE in case of memory allocation failure.*)
338 function FilterWithoutEnglishLetters(aBuf: pAnsiChar; aLen: integer; var newBuf: pAnsiChar; var newLen: integer): Boolean;
339 function FilterWithEnglishLetters(aBuf: pAnsiChar; aLen: integer; var newBuf: pAnsiChar; var newLen: integer): Boolean;
340 implementation
341
342 function FilterWithEnglishLetters(aBuf: pAnsiChar;
343 aLen: integer; var newBuf: pAnsiChar; var newLen: integer): Boolean;
344 var
345 newptr: pAnsiChar;
346 prevPtr: pAnsiChar;
347 curPtr: pAnsiChar;
348 isInTag: Boolean;
349 begin
350 //do filtering to reduce load to probers
351 isInTag := FALSE;
352 newLen := 0;
353
354 newptr := newBuf;
355 if (newptr = nil) then
356 begin
357 Result := FALSE;
358 exit;
359 end;
360
361 prevPtr := aBuf;
362 curPtr := prevPtr;
363 while (curPtr < aBuf+aLen) do
364 begin
365 if (curPtr^ = '>') then
366 isInTag := FALSE
367 else
368 if (curPtr^ = '<') then
369 isInTag := TRUE;
370
371 if ((curPtr^ < #$80) and
372 ((curPtr^ < 'A') or ((curPtr^ > 'Z') and (curPtr^ < 'a')) or (curPtr^ > 'z')) ) then
373 begin
374 if ((curPtr > prevPtr) and (not isInTag)) then // Current segment contains more than just a symbol
375 // and it is not inside a tag, keep it.
376 begin
377 while (prevPtr < curPtr) do
378 begin
379 newptr^ := prevPtr^;
380 inc(newptr);
381 inc(prevPtr);
382 end;
383 inc(prevPtr);
384 newptr^ := ' ';
385 inc(newptr);
386 end
387 else
388 prevPtr := curPtr+1;
389 end;
390 inc(curPtr);
391 end;
392
393 // If the current segment contains more than just a symbol
394 // and it is not inside a tag then keep it.
395 if ( not isInTag) then
396 while (prevPtr < curPtr) do
397 begin
398 newptr^ := prevPtr^;
399 inc(newptr);
400 inc(prevPtr);
401 end;
402
403 newLen := newptr - newBuf;
404
405 Result := TRUE;
406 end;
407
408 function FilterWithoutEnglishLetters(aBuf: pAnsiChar;
409 aLen: integer; var newBuf: pAnsiChar; var newLen: integer): Boolean;
410 var
411 newPtr: pAnsiChar;
412 prevPtr: pAnsiChar;
413 curPtr: pAnsiChar;
414 meetMSB: Boolean;
415 begin
416 (*This filter applies to all scripts which do not use English characters*)
417 Result := FALSE;
418 newLen := 0;
419 meetMSB:= FALSE;
420
421 if newBuf = nil then
422 exit;
423
424 newPtr := newBuf;
425 curPtr := aBuf;
426 prevPtr := curPtr;
427
428 while curPtr < aBuf+aLen do
429 begin
430 if curPtr^ > #$80 then
431 meetMSB := TRUE
432 else
433 if ((curPtr^ < 'A') or ((curPtr^ > 'Z') and (curPtr^ < 'a')) or (curPtr^ > 'z')) then
434 begin
435 //current char is a symbol, most likely a punctuation. we treat it as segment delimiter
436 if (meetMSB and (curPtr > prevPtr)) then
437 //this segment contains more than single symbol, and it has upper ASCII, we need to keep it
438 begin
439 while (prevPtr < curPtr) do
440 begin
441 newptr^ := prevPtr^;
442 inc(newptr);
443 inc(prevPtr);
444 end;
445 inc(prevPtr);
446 newptr^ := ' ';
447 inc(newptr);
448 meetMSB := FALSE;
449 end
450 else //ignore current segment. (either because it is just a symbol or just an English word)
451 prevPtr := curPtr+1;
452 end;
453 inc(curPtr);
454 end;
455 if (meetMSB and (curPtr > prevPtr)) then
456 while (prevPtr < curPtr) do
457 begin
458 newptr^ := prevPtr^;
459 inc(newptr);
460 inc(prevPtr);
461 end;
462
463 newLen := newptr - newBuf;
464
465 Result := TRUE;
466 end;
467
468 end.