+static bool
+is_encoding_utf16 (const char *encoding)
+{
+ return (!c_strcasecmp (encoding, "utf-16")
+ || !c_strcasecmp (encoding, "utf16"));
+}
+
+static bool
+is_encoding_utf32 (const char *encoding)
+{
+ return (!c_strcasecmp (encoding, "utf-32")
+ || !c_strcasecmp (encoding, "utf32"));
+}
+
+/* If ENCODING is the name of an encoding that could begin with a byte-order
+ mark, and in fact the N bytes in DATA do begin with a byte-order mark,
+ returns the number of bytes in the byte-order mark. Otherwise, returns 0.
+
+ N must be at least ENCODING_GUESS_MIN, unless the file is shorter than
+ that. */
+size_t
+encoding_guess_bom_length (const char *encoding,
+ const void *data_, size_t n)
+{
+ const uint8_t *data = data_;
+
+ return (is_utf8_bom (data, n) && is_encoding_utf8 (encoding) ? 3
+ : is_utf16le_bom (data, n) && is_encoding_utf16 (encoding) ? 2
+ : is_utf16be_bom (data, n) && is_encoding_utf16 (encoding) ? 2
+ : is_utf32le_bom (data, n) && is_encoding_utf32 (encoding) ? 4
+ : is_utf32be_bom (data, n) && is_encoding_utf32 (encoding) ? 4
+ : 0);
+}
+