File handling: Do not abort on seeing a BOM in input files, just warn and ignore the BOM
This commit is contained in:
+64
-55
@@ -388,7 +388,7 @@ bool hc_path_has_bom (const char *path)
|
||||
|
||||
HCFILE fp;
|
||||
|
||||
if (hc_fopen (&fp, path, "rb") == false) return false;
|
||||
if (hc_fopen_raw (&fp, path, "rb") == false) return false;
|
||||
|
||||
const size_t nread = hc_fread (buf, 1, sizeof (buf), &fp);
|
||||
|
||||
@@ -396,95 +396,104 @@ bool hc_path_has_bom (const char *path)
|
||||
|
||||
if (nread < 1) return false;
|
||||
|
||||
const int bom_size = hc_string_bom_size (buf);
|
||||
|
||||
const bool has_bom = bom_size > 0;
|
||||
|
||||
return has_bom;
|
||||
}
|
||||
|
||||
int hc_string_bom_size (const u8 *s)
|
||||
{
|
||||
/* signatures from https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding */
|
||||
|
||||
// utf-8
|
||||
|
||||
if ((buf[0] == 0xef)
|
||||
&& (buf[1] == 0xbb)
|
||||
&& (buf[2] == 0xbf)) return true;
|
||||
if ((s[0] == 0xef)
|
||||
&& (s[1] == 0xbb)
|
||||
&& (s[2] == 0xbf)) return 3;
|
||||
|
||||
// utf-16
|
||||
|
||||
if ((buf[0] == 0xfe)
|
||||
&& (buf[1] == 0xff)) return true;
|
||||
if ((s[0] == 0xfe)
|
||||
&& (s[1] == 0xff)) return 2;
|
||||
|
||||
if ((buf[0] == 0xff)
|
||||
&& (buf[1] == 0xfe)) return true;
|
||||
if ((s[0] == 0xff)
|
||||
&& (s[1] == 0xfe)) return 2;
|
||||
|
||||
// utf-32
|
||||
|
||||
if ((buf[0] == 0x00)
|
||||
&& (buf[1] == 0x00)
|
||||
&& (buf[2] == 0xfe)
|
||||
&& (buf[3] == 0xff)) return true;
|
||||
if ((s[0] == 0x00)
|
||||
&& (s[1] == 0x00)
|
||||
&& (s[2] == 0xfe)
|
||||
&& (s[3] == 0xff)) return 4;
|
||||
|
||||
if ((buf[0] == 0xff)
|
||||
&& (buf[1] == 0xfe)
|
||||
&& (buf[2] == 0x00)
|
||||
&& (buf[3] == 0x00)) return true;
|
||||
if ((s[0] == 0xff)
|
||||
&& (s[1] == 0xfe)
|
||||
&& (s[2] == 0x00)
|
||||
&& (s[3] == 0x00)) return 4;
|
||||
|
||||
// utf-7
|
||||
|
||||
if ((buf[0] == 0x2b)
|
||||
&& (buf[1] == 0x2f)
|
||||
&& (buf[2] == 0x76)
|
||||
&& (buf[3] == 0x38)) return true;
|
||||
if ((s[0] == 0x2b)
|
||||
&& (s[1] == 0x2f)
|
||||
&& (s[2] == 0x76)
|
||||
&& (s[3] == 0x38)) return 4;
|
||||
|
||||
if ((buf[0] == 0x2b)
|
||||
&& (buf[1] == 0x2f)
|
||||
&& (buf[2] == 0x76)
|
||||
&& (buf[3] == 0x39)) return true;
|
||||
if ((s[0] == 0x2b)
|
||||
&& (s[1] == 0x2f)
|
||||
&& (s[2] == 0x76)
|
||||
&& (s[3] == 0x39)) return 4;
|
||||
|
||||
if ((buf[0] == 0x2b)
|
||||
&& (buf[1] == 0x2f)
|
||||
&& (buf[2] == 0x76)
|
||||
&& (buf[3] == 0x2b)) return true;
|
||||
if ((s[0] == 0x2b)
|
||||
&& (s[1] == 0x2f)
|
||||
&& (s[2] == 0x76)
|
||||
&& (s[3] == 0x2b)) return 4;
|
||||
|
||||
if ((buf[0] == 0x2b)
|
||||
&& (buf[1] == 0x2f)
|
||||
&& (buf[2] == 0x76)
|
||||
&& (buf[3] == 0x2f)) return true;
|
||||
if ((s[0] == 0x2b)
|
||||
&& (s[1] == 0x2f)
|
||||
&& (s[2] == 0x76)
|
||||
&& (s[3] == 0x2f)) return 4;
|
||||
|
||||
if ((buf[0] == 0x2b)
|
||||
&& (buf[1] == 0x2f)
|
||||
&& (buf[2] == 0x76)
|
||||
&& (buf[3] == 0x38)
|
||||
&& (buf[4] == 0x2d)) return true;
|
||||
if ((s[0] == 0x2b)
|
||||
&& (s[1] == 0x2f)
|
||||
&& (s[2] == 0x76)
|
||||
&& (s[3] == 0x38)
|
||||
&& (s[4] == 0x2d)) return 5;
|
||||
|
||||
// utf-1
|
||||
|
||||
if ((buf[0] == 0xf7)
|
||||
&& (buf[1] == 0x64)
|
||||
&& (buf[2] == 0x4c)) return true;
|
||||
if ((s[0] == 0xf7)
|
||||
&& (s[1] == 0x64)
|
||||
&& (s[2] == 0x4c)) return 3;
|
||||
|
||||
// utf-ebcdic
|
||||
|
||||
if ((buf[0] == 0xdd)
|
||||
&& (buf[1] == 0x73)
|
||||
&& (buf[2] == 0x66)
|
||||
&& (buf[3] == 0x73)) return true;
|
||||
if ((s[0] == 0xdd)
|
||||
&& (s[1] == 0x73)
|
||||
&& (s[2] == 0x66)
|
||||
&& (s[3] == 0x73)) return 4;
|
||||
|
||||
// scsu
|
||||
|
||||
if ((buf[0] == 0x0e)
|
||||
&& (buf[1] == 0xfe)
|
||||
&& (buf[2] == 0xff)) return true;
|
||||
if ((s[0] == 0x0e)
|
||||
&& (s[1] == 0xfe)
|
||||
&& (s[2] == 0xff)) return 3;
|
||||
|
||||
// bocu-1
|
||||
|
||||
if ((buf[0] == 0xfb)
|
||||
&& (buf[1] == 0xee)
|
||||
&& (buf[2] == 0x28)) return true;
|
||||
if ((s[0] == 0xfb)
|
||||
&& (s[1] == 0xee)
|
||||
&& (s[2] == 0x28)) return 3;
|
||||
|
||||
// gb-18030
|
||||
|
||||
if ((buf[0] == 0x84)
|
||||
&& (buf[1] == 0x31)
|
||||
&& (buf[2] == 0x95)
|
||||
&& (buf[3] == 0x33)) return true;
|
||||
if ((s[0] == 0x84)
|
||||
&& (s[1] == 0x31)
|
||||
&& (s[2] == 0x95)
|
||||
&& (s[3] == 0x33)) return 4;
|
||||
|
||||
return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool hc_string_is_digit (const char *s)
|
||||
|
||||
Reference in New Issue
Block a user