demux_mkv.c, ebml.c: Reformat to K&R style

This commit is contained in:
Uoti Urpala 2009-12-29 21:06:21 +02:00
parent c447ef4e74
commit 9fe4f64782
2 changed files with 2217 additions and 2430 deletions

File diff suppressed because it is too large Load Diff

View File

@ -39,161 +39,151 @@
* Read: the element content data ID. * Read: the element content data ID.
* Return: the ID. * Return: the ID.
*/ */
uint32_t uint32_t ebml_read_id(stream_t *s, int *length)
ebml_read_id (stream_t *s, int *length)
{ {
int i, len_mask = 0x80; int i, len_mask = 0x80;
uint32_t id; uint32_t id;
for (i=0, id=stream_read_char (s); i<4 && !(id & len_mask); i++) for (i = 0, id = stream_read_char(s); i < 4 && !(id & len_mask); i++)
len_mask >>= 1; len_mask >>= 1;
if (i >= 4) if (i >= 4)
return EBML_ID_INVALID; return EBML_ID_INVALID;
if (length) if (length)
*length = i + 1; *length = i + 1;
while (i--) while (i--)
id = (id << 8) | stream_read_char (s); id = (id << 8) | stream_read_char(s);
return id; return id;
} }
/* /*
* Read a variable length unsigned int. * Read a variable length unsigned int.
*/ */
uint64_t uint64_t ebml_read_vlen_uint(uint8_t *buffer, int *length)
ebml_read_vlen_uint (uint8_t *buffer, int *length)
{ {
int i, j, num_ffs = 0, len_mask = 0x80; int i, j, num_ffs = 0, len_mask = 0x80;
uint64_t num; uint64_t num;
for (i=0, num=*buffer++; i<8 && !(num & len_mask); i++) for (i = 0, num = *buffer++; i < 8 && !(num & len_mask); i++)
len_mask >>= 1; len_mask >>= 1;
if (i >= 8) if (i >= 8)
return EBML_UINT_INVALID; return EBML_UINT_INVALID;
j = i+1; j = i + 1;
if (length) if (length)
*length = j; *length = j;
if ((int)(num &= (len_mask - 1)) == len_mask - 1) if ((int) (num &= (len_mask - 1)) == len_mask - 1)
num_ffs++;
while (i--)
{
num = (num << 8) | *buffer++;
if ((num & 0xFF) == 0xFF)
num_ffs++; num_ffs++;
while (i--) {
num = (num << 8) | *buffer++;
if ((num & 0xFF) == 0xFF)
num_ffs++;
} }
if (j == num_ffs) if (j == num_ffs)
return EBML_UINT_INVALID; return EBML_UINT_INVALID;
return num; return num;
} }
/* /*
* Read a variable length signed int. * Read a variable length signed int.
*/ */
int64_t int64_t ebml_read_vlen_int(uint8_t *buffer, int *length)
ebml_read_vlen_int (uint8_t *buffer, int *length)
{ {
uint64_t unum; uint64_t unum;
int l; int l;
/* read as unsigned number first */ /* read as unsigned number first */
unum = ebml_read_vlen_uint (buffer, &l); unum = ebml_read_vlen_uint(buffer, &l);
if (unum == EBML_UINT_INVALID) if (unum == EBML_UINT_INVALID)
return EBML_INT_INVALID; return EBML_INT_INVALID;
if (length) if (length)
*length = l; *length = l;
return unum - ((1 << ((7 * l) - 1)) - 1); return unum - ((1 << ((7 * l) - 1)) - 1);
} }
/* /*
* Read: element content length. * Read: element content length.
*/ */
uint64_t uint64_t ebml_read_length(stream_t *s, int *length)
ebml_read_length (stream_t *s, int *length)
{ {
int i, j, num_ffs = 0, len_mask = 0x80; int i, j, num_ffs = 0, len_mask = 0x80;
uint64_t len; uint64_t len;
for (i=0, len=stream_read_char (s); i<8 && !(len & len_mask); i++) for (i = 0, len = stream_read_char(s); i < 8 && !(len & len_mask); i++)
len_mask >>= 1; len_mask >>= 1;
if (i >= 8) if (i >= 8)
return EBML_UINT_INVALID; return EBML_UINT_INVALID;
j = i+1; j = i + 1;
if (length) if (length)
*length = j; *length = j;
if ((int)(len &= (len_mask - 1)) == len_mask - 1) if ((int) (len &= (len_mask - 1)) == len_mask - 1)
num_ffs++;
while (i--)
{
len = (len << 8) | stream_read_char (s);
if ((len & 0xFF) == 0xFF)
num_ffs++; num_ffs++;
while (i--) {
len = (len << 8) | stream_read_char(s);
if ((len & 0xFF) == 0xFF)
num_ffs++;
} }
if (j == num_ffs) if (j == num_ffs)
return EBML_UINT_INVALID; return EBML_UINT_INVALID;
return len; return len;
} }
/* /*
* Read the next element as an unsigned int. * Read the next element as an unsigned int.
*/ */
uint64_t uint64_t ebml_read_uint(stream_t *s, uint64_t *length)
ebml_read_uint (stream_t *s, uint64_t *length)
{ {
uint64_t len, value = 0; uint64_t len, value = 0;
int l; int l;
len = ebml_read_length (s, &l); len = ebml_read_length(s, &l);
if (len == EBML_UINT_INVALID || len < 1 || len > 8) if (len == EBML_UINT_INVALID || len < 1 || len > 8)
return EBML_UINT_INVALID; return EBML_UINT_INVALID;
if (length) if (length)
*length = len + l; *length = len + l;
while (len--) while (len--)
value = (value << 8) | stream_read_char (s); value = (value << 8) | stream_read_char(s);
return value; return value;
} }
/* /*
* Read the next element as a signed int. * Read the next element as a signed int.
*/ */
int64_t int64_t ebml_read_int(stream_t *s, uint64_t *length)
ebml_read_int (stream_t *s, uint64_t *length)
{ {
int64_t value = 0; int64_t value = 0;
uint64_t len; uint64_t len;
int l; int l;
len = ebml_read_length (s, &l); len = ebml_read_length(s, &l);
if (len == EBML_UINT_INVALID || len < 1 || len > 8) if (len == EBML_UINT_INVALID || len < 1 || len > 8)
return EBML_INT_INVALID; return EBML_INT_INVALID;
if (length) if (length)
*length = len + l; *length = len + l;
len--; len--;
l = stream_read_char (s); l = stream_read_char(s);
if (l & 0x80) if (l & 0x80)
value = -1; value = -1;
value = (value << 8) | l; value = (value << 8) | l;
while (len--) while (len--)
value = (value << 8) | stream_read_char (s); value = (value << 8) | stream_read_char(s);
return value; return value;
} }
/* /*
* Read the next element as a float. * Read the next element as a float.
*/ */
long double long double ebml_read_float(stream_t *s, uint64_t *length)
ebml_read_float (stream_t *s, uint64_t *length)
{ {
long double value; long double value;
uint64_t len; uint64_t len;
int l; int l;
len = ebml_read_length (s, &l); len = ebml_read_length(s, &l);
switch (len) switch (len) {
{
case 4: case 4:
value = av_int2flt(stream_read_dword(s)); value = av_int2flt(stream_read_dword(s));
break; break;
@ -203,168 +193,160 @@ ebml_read_float (stream_t *s, uint64_t *length)
break; break;
default: default:
return EBML_FLOAT_INVALID; return EBML_FLOAT_INVALID;
} }
if (length) if (length)
*length = len + l; *length = len + l;
return value; return value;
} }
/* /*
* Read the next element as an ASCII string. * Read the next element as an ASCII string.
*/ */
char * char *ebml_read_ascii(stream_t *s, uint64_t *length)
ebml_read_ascii (stream_t *s, uint64_t *length)
{ {
uint64_t len; uint64_t len;
char *str; char *str;
int l; int l;
len = ebml_read_length (s, &l); len = ebml_read_length(s, &l);
if (len == EBML_UINT_INVALID) if (len == EBML_UINT_INVALID)
return NULL; return NULL;
if (len > SIZE_MAX - 1) if (len > SIZE_MAX - 1)
return NULL; return NULL;
if (length) if (length)
*length = len + l; *length = len + l;
str = (char *) malloc (len+1); str = (char *) malloc(len + 1);
if (stream_read(s, str, len) != (int) len) if (stream_read(s, str, len) != (int) len) {
{ free(str);
free (str); return NULL;
return NULL;
} }
str[len] = '\0'; str[len] = '\0';
return str; return str;
} }
/* /*
* Read the next element as a UTF-8 string. * Read the next element as a UTF-8 string.
*/ */
char * char *ebml_read_utf8(stream_t *s, uint64_t *length)
ebml_read_utf8 (stream_t *s, uint64_t *length)
{ {
return ebml_read_ascii (s, length); return ebml_read_ascii(s, length);
} }
/* /*
* Skip the next element. * Skip the next element.
*/ */
int int ebml_read_skip(stream_t *s, uint64_t *length)
ebml_read_skip (stream_t *s, uint64_t *length)
{ {
uint64_t len; uint64_t len;
int l; int l;
len = ebml_read_length (s, &l); len = ebml_read_length(s, &l);
if (len == EBML_UINT_INVALID) if (len == EBML_UINT_INVALID)
return 1; return 1;
if (length) if (length)
*length = len + l; *length = len + l;
stream_skip(s, len); stream_skip(s, len);
return 0; return 0;
} }
/* /*
* Read the next element, but only the header. The contents * Read the next element, but only the header. The contents
* are supposed to be sub-elements which can be read separately. * are supposed to be sub-elements which can be read separately.
*/ */
uint32_t uint32_t ebml_read_master(stream_t *s, uint64_t *length)
ebml_read_master (stream_t *s, uint64_t *length)
{ {
uint64_t len; uint64_t len;
uint32_t id; uint32_t id;
id = ebml_read_id(s, NULL);
if (id == EBML_ID_INVALID)
return id;
len = ebml_read_length(s, NULL);
if (len == EBML_UINT_INVALID)
return EBML_ID_INVALID;
if (length)
*length = len;
id = ebml_read_id (s, NULL);
if (id == EBML_ID_INVALID)
return id; return id;
len = ebml_read_length (s, NULL);
if (len == EBML_UINT_INVALID)
return EBML_ID_INVALID;
if (length)
*length = len;
return id;
} }
/* /*
* Read an EBML header. * Read an EBML header.
*/ */
char * char *ebml_read_header(stream_t *s, int *version)
ebml_read_header (stream_t *s, int *version)
{ {
uint64_t length, l, num; uint64_t length, l, num;
uint32_t id; uint32_t id;
char *str = NULL; char *str = NULL;
if (ebml_read_master (s, &length) != EBML_ID_HEADER) if (ebml_read_master(s, &length) != EBML_ID_HEADER)
return 0; return 0;
if (version) if (version)
*version = 1; *version = 1;
while (length > 0) while (length > 0) {
{ id = ebml_read_id(s, NULL);
id = ebml_read_id (s, NULL); if (id == EBML_ID_INVALID)
if (id == EBML_ID_INVALID) return NULL;
return NULL; length -= 2;
length -= 2;
switch (id) switch (id) {
{ /* is our read version uptodate? */
/* is our read version uptodate? */
case EBML_ID_EBMLREADVERSION: case EBML_ID_EBMLREADVERSION:
num = ebml_read_uint (s, &l); num = ebml_read_uint(s, &l);
if (num != EBML_VERSION) if (num != EBML_VERSION)
return NULL; return NULL;
break; break;
/* we only handle 8 byte lengths at max */ /* we only handle 8 byte lengths at max */
case EBML_ID_EBMLMAXSIZELENGTH: case EBML_ID_EBMLMAXSIZELENGTH:
num = ebml_read_uint (s, &l); num = ebml_read_uint(s, &l);
if (num != sizeof (uint64_t)) if (num != sizeof(uint64_t))
return NULL; return NULL;
break; break;
/* we handle 4 byte IDs at max */ /* we handle 4 byte IDs at max */
case EBML_ID_EBMLMAXIDLENGTH: case EBML_ID_EBMLMAXIDLENGTH:
num = ebml_read_uint (s, &l); num = ebml_read_uint(s, &l);
if (num != sizeof (uint32_t)) if (num != sizeof(uint32_t))
return NULL; return NULL;
break; break;
case EBML_ID_DOCTYPE: case EBML_ID_DOCTYPE:
str = ebml_read_ascii (s, &l); str = ebml_read_ascii(s, &l);
if (str == NULL) if (str == NULL)
return NULL; return NULL;
break; break;
case EBML_ID_DOCTYPEREADVERSION: case EBML_ID_DOCTYPEREADVERSION:
num = ebml_read_uint (s, &l); num = ebml_read_uint(s, &l);
if (num == EBML_UINT_INVALID) if (num == EBML_UINT_INVALID)
return NULL; return NULL;
if (version) if (version)
*version = num; *version = num;
break; break;
/* we ignore these two, they don't tell us anything we care about */ /* we ignore these two, they don't tell us anything we care about */
case EBML_ID_VOID: case EBML_ID_VOID:
case EBML_ID_EBMLVERSION: case EBML_ID_EBMLVERSION:
case EBML_ID_DOCTYPEVERSION: case EBML_ID_DOCTYPEVERSION:
default: default:
if (ebml_read_skip (s, &l)) if (ebml_read_skip(s, &l))
return NULL; return NULL;
break; break;
} }
length -= l; length -= l;
} }
return str; return str;
} }