130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
}
if( j>LENGTH_MASK ){
flags |= LOOK_LONG; /* Very long line -> binary */
}
return flags;
}
/*
** Checks for proper UTF-8. It uses the method described in:
** http://en.wikipedia.org/wiki/UTF-8#Invalid_byte_sequences
** except for the "overlong form" of \u0000 which is not considered invalid
** here: Some languages like Java and Tcl use it. For UTF-8 characters
** > 7f, the variable 'c2' not necessary means the previous character.
** It's number of higher 1-bits indicate the number of continuation bytes
** that are expected to be followed. E.g. when 'c2' has a value in the range
** 0xc0..0xdf it means that 'c' is expected to contain the last continuation
** byte of a UTF-8 character. A value 0xe0..0xef means that after 'c' one
** more continuation byte is expected.
*/
int invalid_utf8(const Blob *pContent){
const unsigned char *z = (unsigned char *) blob_buffer(pContent);
unsigned int n = blob_size(pContent);
unsigned char c, c2;
if( n==0 ) return 0; /* Empty file -> OK */
c = *z;
while( --n>0 ){
c2 = c;
c = *++z;
if( c2>=0x80 ){
if( ((c2<0xc2) || (c2>=0xf4) || ((c&0xc0)!=0x80)) &&
(((c2!=0xf4) || (c>=0x90)) && ((c2!=0xc0) || (c!=0x80))) ){
return LOOK_INVALID; /* Invalid UTF-8 */
}
c = (c2 >= 0xe0) ? (c2<<1)+1 : ' ';
}
}
return (c>=0x80) ? LOOK_INVALID : 0; /* Last byte must be ASCII. */
}
/*
** Define the type needed to represent a Unicode (UTF-16) character.
*/
#ifndef WCHAR_T
# ifdef _WIN32
# define WCHAR_T wchar_t
|
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
-
+
|
#define UTF16_LENGTH_MASK_SZ (LENGTH_MASK_SZ-(sizeof(WCHAR_T)-sizeof(char)))
#define UTF16_LENGTH_MASK ((1<<UTF16_LENGTH_MASK_SZ)-1)
/*
** This macro is used to swap the byte order of a UTF-16 character in the
** looks_like_utf16() function.
*/
#define UTF16_SWAP(ch) ((((ch) << 8) & 0xFF00) | (((ch) >> 8) & 0xFF))
#define UTF16_SWAP(ch) ((((ch) << 8) & 0xff00) | (((ch) >> 8) & 0xff))
#define UTF16_SWAP_IF(expr,ch) ((expr) ? UTF16_SWAP((ch)) : (ch))
/*
** This function attempts to scan each logical line within the blob to
** determine the type of content it appears to contain. The return value
** is a combination of one or more of the LOOK_XXX flags (see above):
**
|
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
-
+
|
/*
** This function returns an array of bytes representing the byte-order-mark
** for UTF-8.
*/
const unsigned char *get_utf8_bom(int *pnByte){
static const unsigned char bom[] = {
0xEF, 0xBB, 0xBF, 0x00, 0x00, 0x00
0xef, 0xbb, 0xbf, 0x00, 0x00, 0x00
};
if( pnByte ) *pnByte = 3;
return bom;
}
/*
** This function returns non-zero if the blob starts with a UTF-8
|
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
|
/*
** COMMAND: test-looks-like-utf
**
** Usage: %fossil test-looks-like-utf FILENAME
**
** Options:
** -n|--limit <num> Repeat looks-like function <num> times, for
** performance measurement. Default = 1;
** --utf8 Ignoring BOM and file size, force UTF-8 checking
** --utf16 Ignoring BOM and file size, force UTF-16 checking
**
** FILENAME is the name of a file to check for textual content in the UTF-8
** and/or UTF-16 encodings.
*/
void looks_like_utf_test_cmd(void){
Blob blob; /* the contents of the specified file */
int fUtf8; /* return value of starts_with_utf8_bom() */
int fUtf16; /* return value of starts_with_utf16_bom() */
int fUnicode; /* return value of could_be_utf16() */
int lookFlags; /* output flags from looks_like_utf8/utf16() */
int bRevUtf16 = 0; /* non-zero -> UTF-16 byte order reversed */
int bRevUnicode = 0; /* non-zero -> UTF-16 byte order reversed */
int fForceUtf8 = find_option("utf8",0,0)!=0;
int fForceUtf16 = find_option("utf16",0,0)!=0;
const char *zCount = find_option("limit","n",1);
int nRepeat = 1;
if( g.argc!=3 ) usage("FILENAME");
if( zCount ){
nRepeat = atoi(zCount);
}
blob_read_from_file(&blob, g.argv[2]);
while( --nRepeat >= 0 ){
fUtf8 = starts_with_utf8_bom(&blob, 0);
fUtf16 = starts_with_utf16_bom(&blob, 0, &bRevUtf16);
if( fForceUtf8 ){
fUnicode = 0;
}else{
fUnicode = could_be_utf16(&blob, &bRevUnicode) || fForceUtf16;
}
lookFlags = fUnicode ? looks_like_utf16(&blob, bRevUnicode, 0) :
looks_like_utf8(&blob, 0);
fUtf8 = starts_with_utf8_bom(&blob, 0);
fUtf16 = starts_with_utf16_bom(&blob, 0, &bRevUtf16);
if( fForceUtf8 ){
fUnicode = 0;
}else{
fUnicode = could_be_utf16(&blob, &bRevUnicode) || fForceUtf16;
}
if( fUnicode ){
lookFlags = looks_like_utf16(&blob, bRevUnicode, 0);
}else{
lookFlags = looks_like_utf8(&blob, 0)|invalid_utf8(&blob);
}
}
fossil_print("File \"%s\" has %d bytes.\n",g.argv[2],blob_size(&blob));
fossil_print("Starts with UTF-8 BOM: %s\n",fUtf8?"yes":"no");
fossil_print("Starts with UTF-16 BOM: %s\n",
fUtf16?(bRevUtf16?"reversed":"yes"):"no");
fossil_print("Looks like UTF-%s: %s\n",fUnicode?"16":"8",
(lookFlags&LOOK_BINARY)?"no":"yes");
fossil_print("Has flag LOOK_NUL: %s\n",(lookFlags&LOOK_NUL)?"yes":"no");
|