From 98994138395c2ce9b72ac194dbdfb5799e696c23 Mon Sep 17 00:00:00 2001 From: Joseph Wallace Date: Fri, 20 Nov 2015 13:33:41 -0500 Subject: [PATCH] Add parser for bodies of Integer elements. --- src/format_ebml.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/format_ebml.c b/src/format_ebml.c index 4c4ec51f..d10fc2f4 100644 --- a/src/format_ebml.c +++ b/src/format_ebml.c @@ -111,6 +111,11 @@ static int ebml_parse_tag(unsigned char *buffer, static int ebml_parse_var_int(unsigned char *buffer, unsigned char *buffer_end, unsigned long long *out_value); +static int ebml_parse_sized_int(unsigned char *buffer, + unsigned char *buffer_end, + int len, + int is_signed, + unsigned long long *out_value); int format_ebml_get_plugin(source_t *source) { @@ -769,3 +774,42 @@ static int ebml_parse_var_int(unsigned char *buffer, return size; } + +/* Parse a normal int that may be from 1-8 bytes long. + * Returns 0 if there's not enough space to read the number; + * Returns -1 if the number is mis-sized. + * Else, returns the length of the number in bytes and writes the + * value to *out_value. + */ +static int ebml_parse_sized_int(unsigned char *buffer, + unsigned char *buffer_end, + int len, + int is_signed, + unsigned long long *out_value) +{ + long long value; + int i; + + if (len < 1 || len > 8) { + ICECAST_LOG_DEBUG("Sized int of %i bytes", len); + return -1; + } + + if (buffer + len >= buffer_end) { + return 0; + } + + if (is_signed && ((signed char) buffer[0]) < 0) { + value = -1; + } else { + value = 0; + } + + for (i = 0; i < len; i++) { + value = (value << 8) + ((unsigned char) buffer[i]); + } + + *out_value = value; + + return len; +}