Commit ab52d522 authored by Joseph Wallace's avatar Joseph Wallace Committed by Philipp Schafft
Browse files

Import sized-int parsing function

from Icecast.
parent b83c26b0
...@@ -109,6 +109,11 @@ static ssize_t ebml_parse_tag(unsigned char *buffer, ...@@ -109,6 +109,11 @@ static ssize_t ebml_parse_tag(unsigned char *buffer,
static ssize_t ebml_parse_var_int(unsigned char *buffer, static ssize_t ebml_parse_var_int(unsigned char *buffer,
unsigned char *buffer_end, unsigned char *buffer_end,
uint64_t *out_value); uint64_t *out_value);
static ssize_t ebml_parse_sized_int(unsigned char *buffer,
unsigned char *buffer_end,
size_t len,
bool is_signed,
uint64_t *out_value);
/* -- interface functions -- */ /* -- interface functions -- */
int shout_open_webm(shout_t *self) int shout_open_webm(shout_t *self)
...@@ -437,3 +442,45 @@ static ssize_t ebml_parse_var_int(unsigned char *buffer, ...@@ -437,3 +442,45 @@ static ssize_t ebml_parse_var_int(unsigned char *buffer,
return size; return size;
} }
/* Parse a big-endian 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.
* If is_signed is true, then the int is assumed to be two's complement
* signed, negative values will be correctly promoted, and the returned
* unsigned number can be safely cast to a signed number on systems using
* two's complement arithmatic.
*/
static ssize_t ebml_parse_sized_int(unsigned char *buffer,
unsigned char *buffer_end,
size_t len,
bool is_signed,
uint64_t *out_value)
{
uint64_t value;
size_t i;
if (len < 1 || len > 8) {
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;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment