diff --git a/src/zm_rtsp_server_stream_av1_source.cpp b/src/zm_rtsp_server_stream_av1_source.cpp index fa042519c2..bf2bb8b391 100644 --- a/src/zm_rtsp_server_stream_av1_source.cpp +++ b/src/zm_rtsp_server_stream_av1_source.cpp @@ -10,6 +10,7 @@ #include "zm_config.h" #include "zm_logger.h" +#include "zm_utils.h" #include "xop/AV1Source.h" #if HAVE_RTSP_SERVER @@ -26,21 +27,7 @@ AV1_ZoneMinderStreamSource::AV1_ZoneMinderStreamSource( // Parse LEB128 variable-length integer (used for OBU sizes in AV1) size_t AV1_ZoneMinderStreamSource::parseLEB128( unsigned char* data, size_t max_size, uint32_t& value) { - value = 0; - size_t bytes_read = 0; - - for (size_t i = 0; i < max_size && i < 8; i++) { - uint8_t byte = data[i]; - value |= ((byte & 0x7F) << (7 * i)); - bytes_read++; - - if ((byte & 0x80) == 0) { - // Last byte of LEB128 - break; - } - } - - return bytes_read; + return zm_parse_leb128(data, max_size, value); } // Parse OBU header diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp index c8de21e950..597ef29986 100644 --- a/src/zm_utils.cpp +++ b/src/zm_utils.cpp @@ -674,3 +674,21 @@ std::string escape_json_string(std::string input) { } return output; } + +size_t zm_parse_leb128(const unsigned char *data, size_t max_size, uint32_t &value) { + uint64_t result = 0; + size_t bytes_read = 0; + + for (size_t i = 0; i < max_size && i < 8; i++) { + uint8_t byte = data[i]; + result |= static_cast(byte & 0x7F) << (7 * i); + bytes_read++; + + if ((byte & 0x80) == 0) { + break; + } + } + + value = static_cast(result); + return bytes_read; +} diff --git a/src/zm_utils.h b/src/zm_utils.h index 5774188b64..d0cab23805 100644 --- a/src/zm_utils.h +++ b/src/zm_utils.h @@ -112,6 +112,9 @@ std::string stringtf(const char* format, ...); std::string ByteArrayToHexString(nonstd::span bytes); +// Parses an AV1 LEB128 value (at most 8 bytes) and returns the bytes consumed. +size_t zm_parse_leb128(const unsigned char *data, size_t max_size, uint32_t &value); + std::string Base64Encode(const std::string &str); std::string TimevalToString(timeval tv); diff --git a/tests/zm_utils.cpp b/tests/zm_utils.cpp index b328a4310a..0584375b39 100644 --- a/tests/zm_utils.cpp +++ b/tests/zm_utils.cpp @@ -582,3 +582,53 @@ TEST_CASE("url_for_log") { REQUIRE(out.find("eyJ") == std::string::npos); } } + +TEST_CASE("zm_parse_leb128", "[utils]") { + uint32_t value; + size_t n; + + SECTION("single byte") { + static const unsigned char data[] = {0x05}; + n = zm_parse_leb128(data, sizeof(data), value); + REQUIRE(n == 1); + REQUIRE(value == 5); + } + + SECTION("two bytes, 128") { + static const unsigned char data[] = {0x80, 0x01}; + n = zm_parse_leb128(data, sizeof(data), value); + REQUIRE(n == 2); + REQUIRE(value == 128); + } + + SECTION("max 5-byte 32-bit value") { + static const unsigned char data[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x0F}; + n = zm_parse_leb128(data, sizeof(data), value); + REQUIRE(n == 5); + REQUIRE(value == 4294967295u); + } + + SECTION("padded 8-byte encoding of 1") { + static const unsigned char data[] = { + 0x81, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00}; + n = zm_parse_leb128(data, sizeof(data), value); + REQUIRE(n == 8); + REQUIRE(value == 1); + } + + SECTION("max_size truncation") { + static const unsigned char data[] = {0xFF, 0xFF, 0xFF, 0xFF, 0x0F}; + // Only 3 bytes available: 0x7F | (0x7F << 7) | (0x7F << 14) = 0x1FFFFF, + // and the third byte still has the continuation bit set, so it stops + // without a terminator but still returns the bytes it consumed. + n = zm_parse_leb128(data, 3, value); + REQUIRE(n == 3); + REQUIRE(value == 0x1FFFFFu); + } + + SECTION("empty input") { + static const unsigned char data[] = {0x05}; + n = zm_parse_leb128(data, 0, value); + REQUIRE(n == 0); + } +}