diff --git a/utils.cpp b/utils.cpp index 60c5e22..b82f7ed 100644 --- a/utils.cpp +++ b/utils.cpp @@ -697,18 +697,14 @@ char *get_file_contents (const char *filename) { // If there is a UTF-8 endian marker at the beginning of the file, skip it. - const unsigned char utf8_endian_mark[] = {0xEF, 0xBB, 0xBF}; - bool matched = true; - for (unsigned i = 0; matched && i < sizeof(utf8_endian_mark); i++) { - int c = fgetc(file); - if (c == EOF) { - return NULL; - } - matched &= (unsigned char)c == utf8_endian_mark[i]; - } - if (matched) { + const unsigned char utf8_endian_mark[] = {0xEF, 0xBB, 0xBF}; + unsigned char file_head[3]; + read_size = fread (file_head, 1, 3, file); + + if (read_size == 3 && memcmp(file_head, utf8_endian_mark, sizeof(utf8_endian_mark)) == 0) + // Skip UTF-8 BOM size -= sizeof(utf8_endian_mark); - } else { + else { rewind(file); }