summaryrefslogtreecommitdiff
path: root/utf8.c
diff options
context:
space:
mode:
authorLars Schneider <larsxschneider@gmail.com>2018-04-15 18:16:05 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-04-16 02:40:56 (GMT)
commit10ecb82e4f1f507d5f122e00fd4829b30953f853 (patch)
tree58e2245d175a29290fc4405988ab650b5b669207 /utf8.c
parent2f0c4a362c5db677ee392a6a550632bfb22d8319 (diff)
downloadgit-10ecb82e4f1f507d5f122e00fd4829b30953f853.zip
git-10ecb82e4f1f507d5f122e00fd4829b30953f853.tar.gz
git-10ecb82e4f1f507d5f122e00fd4829b30953f853.tar.bz2
utf8: add function to detect prohibited UTF-16/32 BOM
Whenever a data stream is declared to be UTF-16BE, UTF-16LE, UTF-32BE or UTF-32LE a BOM must not be used [1]. The function returns true if this is the case. This function is used in a subsequent commit. [1] http://unicode.org/faq/utf_bom.html#bom10 Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'utf8.c')
-rw-r--r--utf8.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/utf8.c b/utf8.c
index 40f4b14..83af3a9 100644
--- a/utf8.c
+++ b/utf8.c
@@ -560,6 +560,32 @@ char *reencode_string_len(const char *in, int insz,
}
#endif
+static int has_bom_prefix(const char *data, size_t len,
+ const char *bom, size_t bom_len)
+{
+ return data && bom && (len >= bom_len) && !memcmp(data, bom, bom_len);
+}
+
+static const char utf16_be_bom[] = {0xFE, 0xFF};
+static const char utf16_le_bom[] = {0xFF, 0xFE};
+static const char utf32_be_bom[] = {0x00, 0x00, 0xFE, 0xFF};
+static const char utf32_le_bom[] = {0xFF, 0xFE, 0x00, 0x00};
+
+int has_prohibited_utf_bom(const char *enc, const char *data, size_t len)
+{
+ return (
+ (same_utf_encoding("UTF-16BE", enc) ||
+ same_utf_encoding("UTF-16LE", enc)) &&
+ (has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||
+ has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))
+ ) || (
+ (same_utf_encoding("UTF-32BE", enc) ||
+ same_utf_encoding("UTF-32LE", enc)) &&
+ (has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||
+ has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))
+ );
+}
+
/*
* Returns first character length in bytes for multi-byte `text` according to
* `encoding`.