Fossil SCM

More simplification in UTF-16 bom detection

jan.nijtmans 2013-02-17 14:47 UTC trunk
Commit 1e70f211f9b37df4a8d9948b50f0585ca2ba7732
1 file changed +7 -12
+7 -12
--- src/diff.c
+++ src/diff.c
@@ -370,29 +370,24 @@
370370
int starts_with_utf16_bom(
371371
const Blob *pContent, /* IN: Blob content to perform BOM detection on. */
372372
int *pnByte, /* OUT: The number of bytes used for the BOM. */
373373
int *pbReverse /* OUT: Non-zero for BOM in reverse byte-order. */
374374
){
375
- const char *z = blob_buffer(pContent);
376
- int bomSize = 2;
375
+ const unsigned short *z = (unsigned short *)blob_buffer(pContent);
376
+ const int bomSize = 2;
377377
static const unsigned short bom = 0xfeff;
378378
static const unsigned short bom_reversed = 0xfffe;
379
- static const unsigned short null = 0;
380
- int size;
379
+ int size = blob_size(pContent);
381380
382381
if( pnByte ) *pnByte = bomSize;
383382
if( pbReverse ) *pbReverse = -1; /* Unknown. */
384
- size = blob_size(pContent);
385
- if( (size<bomSize) || (size%2) ) return 0;
386
- if( memcmp(z, &bom_reversed, bomSize)==0 ){
383
+ if( (size<bomSize) || (size%2)
384
+ || (size>=(2*bomSize) && z[1]==0) ) return 0;
385
+ if( z[0] == bom_reversed ){
387386
if( pbReverse ) *pbReverse = 1;
388
- if( size<(2*bomSize) ) return 1;
389
- if( memcmp(z+bomSize, &null, bomSize)!=0 ) return 1;
390
- }else if( memcmp(z, &bom, bomSize)==0 ){
387
+ }else if( z[0] == bom ){
391388
if( pbReverse ) *pbReverse = 0;
392
- if( size<(2*bomSize) ) return 1;
393
- if( memcmp(z+bomSize, &null, bomSize)!=0 ) return 1;
394389
}
395390
return 0;
396391
}
397392
398393
/*
399394
--- src/diff.c
+++ src/diff.c
@@ -370,29 +370,24 @@
370 int starts_with_utf16_bom(
371 const Blob *pContent, /* IN: Blob content to perform BOM detection on. */
372 int *pnByte, /* OUT: The number of bytes used for the BOM. */
373 int *pbReverse /* OUT: Non-zero for BOM in reverse byte-order. */
374 ){
375 const char *z = blob_buffer(pContent);
376 int bomSize = 2;
377 static const unsigned short bom = 0xfeff;
378 static const unsigned short bom_reversed = 0xfffe;
379 static const unsigned short null = 0;
380 int size;
381
382 if( pnByte ) *pnByte = bomSize;
383 if( pbReverse ) *pbReverse = -1; /* Unknown. */
384 size = blob_size(pContent);
385 if( (size<bomSize) || (size%2) ) return 0;
386 if( memcmp(z, &bom_reversed, bomSize)==0 ){
387 if( pbReverse ) *pbReverse = 1;
388 if( size<(2*bomSize) ) return 1;
389 if( memcmp(z+bomSize, &null, bomSize)!=0 ) return 1;
390 }else if( memcmp(z, &bom, bomSize)==0 ){
391 if( pbReverse ) *pbReverse = 0;
392 if( size<(2*bomSize) ) return 1;
393 if( memcmp(z+bomSize, &null, bomSize)!=0 ) return 1;
394 }
395 return 0;
396 }
397
398 /*
399
--- src/diff.c
+++ src/diff.c
@@ -370,29 +370,24 @@
370 int starts_with_utf16_bom(
371 const Blob *pContent, /* IN: Blob content to perform BOM detection on. */
372 int *pnByte, /* OUT: The number of bytes used for the BOM. */
373 int *pbReverse /* OUT: Non-zero for BOM in reverse byte-order. */
374 ){
375 const unsigned short *z = (unsigned short *)blob_buffer(pContent);
376 const int bomSize = 2;
377 static const unsigned short bom = 0xfeff;
378 static const unsigned short bom_reversed = 0xfffe;
379 int size = blob_size(pContent);
 
380
381 if( pnByte ) *pnByte = bomSize;
382 if( pbReverse ) *pbReverse = -1; /* Unknown. */
383 if( (size<bomSize) || (size%2)
384 || (size>=(2*bomSize) && z[1]==0) ) return 0;
385 if( z[0] == bom_reversed ){
386 if( pbReverse ) *pbReverse = 1;
387 }else if( z[0] == bom ){
 
 
388 if( pbReverse ) *pbReverse = 0;
 
 
389 }
390 return 0;
391 }
392
393 /*
394

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button