From 540eae22a6b42a3985e36f0f1d068e0ba748a41a Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Wed, 10 Aug 2022 20:58:19 +0200 Subject: Fix parsing of SSA colour tags which have their leading zeros omitted. --- src/ssa_reader.cc | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/ssa_reader.cc b/src/ssa_reader.cc index 8632251..07d592a 100644 --- a/src/ssa_reader.cc +++ b/src/ssa_reader.cc @@ -25,6 +25,7 @@ #include "compose.hpp" #include #include +#include #include #include @@ -55,17 +56,24 @@ SSAReader::SSAReader (FILE* f) Colour h_colour (string s) { - /* There are both BGR and ABGR versions of these colours */ - if ((s.length() != 8 && s.length() != 10) || s[0] != '&' || s[1] != 'H') { + if (s.empty() || s[0] != '&' || s[1] != 'H') { throw SSAError(String::compose("Badly formatted colour tag %1", s)); } - int ir, ig, ib; - /* XXX: ignoring alpha channel here; note that 00 is opaque and FF is transparent */ - int const off = s.length() == 10 ? 4 : 2; - if (sscanf(s.c_str() + off, "%2x%2x%2x", &ib, &ig, &ir) < 3) { - throw SSAError(String::compose("Badly formatted colour tag %1", s)); + + auto start = s.c_str(); + auto const end = start + s.length(); + while (start < end && (*start == '&' || *start == 'H')) { + ++start; } - return sub::Colour(ir / 255.0, ig / 255.0, ib / 255.0); + + auto const colour = strtoll(start, nullptr, 16); + + /* XXX: ignoring alpha channel here; note that 00 is opaque and FF is transparent */ + return sub::Colour( + ((colour & 0x000000ff) >> 0) / 255.0, + ((colour & 0x0000ff00) >> 8) / 255.0, + ((colour & 0x00ff0000) >> 16) / 255.0 + ); } class Style -- cgit v1.2.3