diff mbox series

[FFmpeg-devel] avformat/subtitles: check for double BOM in UTF-16 files

Message ID 20230923232049.14119-1-llyyr.public@gmail.com
State New
Headers show
Series [FFmpeg-devel] avformat/subtitles: check for double BOM in UTF-16 files | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

llyyr Sept. 23, 2023, 11:20 p.m. UTC
While these files certainly aren't the norm, and might not even be
considered valid by many programs, there are plenty of older ASS tracks
in UTF-16 LE/BE encoding that contain double BOMs.

This patch teaches ff_text_init_avio about double BOMs and makes it
check for them in UTF-16 LE/BE files. This works by reading two more
bytes after the first BOM check, and seeking back if a second BOM
doesn't exist. If it does exist, we simply procede with buf_pos two
bytes ahead.

While this hack could certainly live in assdec.c, and would be much
simpler that way, there certainly isn't any harm in allowing other
subtitle format readers to be aware of double BOMs too.
---
 libavformat/subtitles.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
diff mbox series

Patch

diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index 3413763c7b..a7b83cbb69 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -44,6 +44,20 @@  void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
             r->buf_pos += 3;
         }
     }
+    if (r->type != FF_UTF_8) {
+        // Check for double BOM in UTF-16 LE/BE files
+        for (i = 0; i < 2; i++)
+            r->buf[r->buf_len++] = avio_r8(r->pb);
+        if (strncmp("\xFF\xFE\xFF\xFE", r->buf, 4) == 0 ||
+            strncmp("\xFE\xFF\xFE\xFF", r->buf, 4) == 0) {
+            // We did find a second BOM, so move buf_pos two bytes ahead
+            r->buf_pos += 2;
+        } else {
+            // We did not find a second BOM, undo the seek
+            r->buf_len -= 2;
+            avio_seek(r->pb, -2, SEEK_CUR); // Seek back two bytes
+        }
+    }
     if (s && (r->type == FF_UTF16LE || r->type == FF_UTF16BE))
         av_log(s, AV_LOG_INFO,
                "UTF16 is automatically converted to UTF8, do not specify a character encoding\n");