Bug 1074004 - Update nestegg from upstream. r=giles

--HG--
rename : media/libnestegg/README => media/libnestegg/README.md
This commit is contained in:
Matthew Gregan 2014-09-29 13:13:10 +13:00
parent 2576f16ba0
commit 6c48290b50
6 changed files with 187 additions and 88 deletions

View File

@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/kinetiknz/nestegg.svg?branch=master)](https://travis-ci.org/kinetiknz/nestegg)
See INSTALL for build instructions.
Licensed under an ISC-style license. See LICENSE for details.

View File

@ -5,4 +5,4 @@ Makefile.in build files for the Mozilla build system.
The nestegg git repository is: git://github.com/kinetiknz/nestegg.git
The git commit ID used was 46ab96bcc8b099704cc8a15993f80fe0269a5284.
The git commit ID used was 59220ae3e801cbad0f8160129c4df315469af671.

View File

@ -381,8 +381,11 @@ int nestegg_sniff(unsigned char const * buffer, size_t length);
* Set the underlying allocation function for library allocations.
*
* @param realloc_func The desired function.
* @retval 0 realloc_func(p, 0) does not act as free()
* @retval 1 realloc_func(p, 0) acts as free()
* @retval -1 malloc failed during realloc_func test
*/
void nestegg_set_halloc_func(void * (* realloc_func)(void *, size_t));
int nestegg_set_halloc_func(void * (* realloc_func)(void *, size_t));
#if defined(__cplusplus)
}

View File

@ -46,7 +46,7 @@ realloc_t halloc_allocator = NULL;
/*
* static methods
*/
static void _set_allocator(void);
int halloc_set_allocator(realloc_t realloc_func);
static void * _realloc(void * ptr, size_t n);
static int _relate(hblock_t * b, hblock_t * p);
@ -62,7 +62,10 @@ void * halloc(void * ptr, size_t len)
/* set up default allocator */
if (! allocator)
{
_set_allocator();
if (halloc_set_allocator(realloc) == 0)
{
halloc_set_allocator(_realloc);
}
assert(allocator);
}
@ -172,7 +175,7 @@ char * h_strdup(const char * str)
/*
* static stuff
*/
static void _set_allocator(void)
int halloc_set_allocator(realloc_t realloc_func)
{
void * p;
assert(! allocator);
@ -187,17 +190,17 @@ static void _set_allocator(void)
*
* Thanks to Stan Tobias for pointing this tricky part out.
*/
allocator = realloc;
if (! (p = malloc(1)))
if (! (p = realloc_func(NULL, 1)))
/* hmm */
return;
return -1;
if ((p = realloc(p, 0)))
if ((p = realloc_func(p, 0)))
{
/* realloc cannot be used as free() */
allocator = _realloc;
free(p);
/* realloc_func cannot be used as free() */
return 0;
}
allocator = realloc_func;
return 1;
}
static void * _realloc(void * ptr, size_t n)

View File

@ -321,9 +321,18 @@ struct block_additional {
struct block_additional * next;
};
#define NE_IO_BUFSZ 16384
struct nestegg_io_buf {
nestegg_io io;
unsigned char buffer[NE_IO_BUFSZ];
size_t bufsz;
int offset;
};
/* Public (opaque) Structures */
struct nestegg {
nestegg_io * io;
struct nestegg_io_buf * io;
nestegg_log log;
struct pool_ctx * alloc_pool;
uint64_t last_id;
@ -546,19 +555,99 @@ ne_alloc(size_t size)
}
static int
ne_io_read(nestegg_io * io, void * buffer, size_t length)
ne_io_read(struct nestegg_io_buf * io, void * buffer, size_t length)
{
return io->read(buffer, length, io->userdata);
int64_t off;
int r;
size_t avail;
assert(io->offset == -1 || (io->offset >= 0 && (unsigned int) io->offset < io->bufsz));
/* Too big to buffer, invalidate buffer and read through */
if (length > io->bufsz) {
if (io->offset != -1) {
r = io->io.seek(-(io->bufsz - io->offset), NESTEGG_SEEK_CUR, io->io.userdata);
if (r != 0) {
return -1;
}
}
io->offset = -1;
return io->io.read(buffer, length, io->io.userdata);
}
/* Buffer invalid */
if (io->offset == -1) {
off = io->io.tell(io->io.userdata);
if (off == -1) {
return -1;
}
/* Refill buffer */
r = io->io.read(io->buffer, io->bufsz, io->io.userdata);
if (r != 1) {
/* Read truncated due to being within io->bufsz of EOS, reset read
position and switch to read through mode */
io->offset = -1;
io->bufsz = 0;
if (r == 0) {
r = io->io.seek(off, NESTEGG_SEEK_SET, io->io.userdata);
}
if (r == 0) {
return io->io.read(buffer, length, io->io.userdata);
}
return -1;
}
if (r == 1) {
io->offset = 0;
}
}
/* Service request with what we have */
avail = length;
if (io->bufsz - io->offset < length) {
avail = io->bufsz - io->offset;
}
memcpy(buffer, io->buffer + io->offset, avail);
io->offset += avail;
if ((unsigned int) io->offset == io->bufsz) {
io->offset = -1;
}
/* Still more to read, invalidate buffer and read more */
if (length - avail > 0) {
return ne_io_read(io, (char *) buffer + avail, length - avail);
}
return 1;
}
static int
ne_io_seek(nestegg_io * io, int64_t offset, int whence)
ne_io_seek(struct nestegg_io_buf * io, int64_t offset, int whence)
{
return io->seek(offset, whence, io->userdata);
/* Invalidate buffer */
io->offset = -1;
return io->io.seek(offset, whence, io->io.userdata);
}
static int64_t
ne_io_tell(struct nestegg_io_buf * io)
{
int64_t off;
off = io->io.tell(io->io.userdata);
if (off == -1) {
return -1;
}
if (io->offset == -1) {
return off;
}
assert(off >= (int64_t) io->bufsz - io->offset);
return off - io->bufsz + (unsigned int) io->offset;
}
static int
ne_io_read_skip(nestegg_io * io, size_t length)
ne_io_read_skip(struct nestegg_io_buf * io, size_t length)
{
size_t get;
unsigned char buf[8192];
@ -575,14 +664,8 @@ ne_io_read_skip(nestegg_io * io, size_t length)
return r;
}
static int64_t
ne_io_tell(nestegg_io * io)
{
return io->tell(io->userdata);
}
static int
ne_bare_read_vint(nestegg_io * io, uint64_t * value, uint64_t * length, enum vint_mask maskflag)
ne_bare_read_vint(struct nestegg_io_buf * io, uint64_t * value, uint64_t * length, enum vint_mask maskflag)
{
int r;
unsigned char b;
@ -619,19 +702,19 @@ ne_bare_read_vint(nestegg_io * io, uint64_t * value, uint64_t * length, enum vin
}
static int
ne_read_id(nestegg_io * io, uint64_t * value, uint64_t * length)
ne_read_id(struct nestegg_io_buf * io, uint64_t * value, uint64_t * length)
{
return ne_bare_read_vint(io, value, length, MASK_NONE);
}
static int
ne_read_vint(nestegg_io * io, uint64_t * value, uint64_t * length)
ne_read_vint(struct nestegg_io_buf * io, uint64_t * value, uint64_t * length)
{
return ne_bare_read_vint(io, value, length, MASK_FIRST_BIT);
}
static int
ne_read_svint(nestegg_io * io, int64_t * value, uint64_t * length)
ne_read_svint(struct nestegg_io_buf * io, int64_t * value, uint64_t * length)
{
int r;
uint64_t uvalue;
@ -653,7 +736,7 @@ ne_read_svint(nestegg_io * io, int64_t * value, uint64_t * length)
}
static int
ne_read_uint(nestegg_io * io, uint64_t * val, uint64_t length)
ne_read_uint(struct nestegg_io_buf * io, uint64_t * val, uint64_t length)
{
unsigned char b;
int r;
@ -675,7 +758,7 @@ ne_read_uint(nestegg_io * io, uint64_t * val, uint64_t length)
}
static int
ne_read_int(nestegg_io * io, int64_t * val, uint64_t length)
ne_read_int(struct nestegg_io_buf * io, int64_t * val, uint64_t length)
{
int r;
uint64_t uval, base;
@ -688,8 +771,8 @@ ne_read_int(nestegg_io * io, int64_t * val, uint64_t length)
base = 1;
base <<= length * 8 - 1;
if (uval >= base) {
base = 1;
base <<= length * 8;
base = 1;
base <<= length * 8;
} else {
base = 0;
}
@ -702,7 +785,7 @@ ne_read_int(nestegg_io * io, int64_t * val, uint64_t length)
}
static int
ne_read_float(nestegg_io * io, double * val, uint64_t length)
ne_read_float(struct nestegg_io_buf * io, double * val, uint64_t length)
{
union {
uint64_t u;
@ -736,9 +819,9 @@ ne_read_string(nestegg * ctx, char ** val, uint64_t length)
if (!str)
return -1;
if (length) {
r = ne_io_read(ctx->io, (unsigned char *) str, length);
if (r != 1)
return r;
r = ne_io_read(ctx->io, (unsigned char *) str, length);
if (r != 1)
return r;
}
str[length] = '\0';
*val = str;
@ -1018,8 +1101,9 @@ ne_read_simple(nestegg * ctx, struct ebml_element_desc * desc, size_t length)
break;
case TYPE_MASTER:
case TYPE_UNKNOWN:
assert(0);
default:
r = 0;
assert(0);
break;
}
@ -1136,7 +1220,7 @@ ne_xiph_lace_value(unsigned char ** np)
}
static int
ne_read_xiph_lace_value(nestegg_io * io, uint64_t * value, size_t * consumed)
ne_read_xiph_lace_value(struct nestegg_io_buf * io, uint64_t * value, size_t * consumed)
{
int r;
uint64_t lace;
@ -1159,7 +1243,7 @@ ne_read_xiph_lace_value(nestegg_io * io, uint64_t * value, size_t * consumed)
}
static int
ne_read_xiph_lacing(nestegg_io * io, size_t block, size_t * read, uint64_t n, uint64_t * sizes)
ne_read_xiph_lacing(struct nestegg_io_buf * io, size_t block, size_t * read, uint64_t n, uint64_t * sizes)
{
int r;
size_t i = 0;
@ -1182,7 +1266,7 @@ ne_read_xiph_lacing(nestegg_io * io, size_t block, size_t * read, uint64_t n, ui
}
static int
ne_read_ebml_lacing(nestegg_io * io, size_t block, size_t * read, uint64_t n, uint64_t * sizes)
ne_read_ebml_lacing(struct nestegg_io_buf * io, size_t block, size_t * read, uint64_t n, uint64_t * sizes)
{
int r;
uint64_t lace, sum, length;
@ -1792,9 +1876,9 @@ struct sniff_buffer {
};
static int
ne_buffer_read(void * buffer, size_t length, void * user_data)
ne_buffer_read(void * buffer, size_t length, void * userdata)
{
struct sniff_buffer * sb = user_data;
struct sniff_buffer * sb = userdata;
int rv = 1;
size_t available = sb->length - sb->offset;
@ -1809,21 +1893,21 @@ ne_buffer_read(void * buffer, size_t length, void * user_data)
}
static int
ne_buffer_seek(int64_t offset, int whence, void * user_data)
ne_buffer_seek(int64_t offset, int whence, void * userdata)
{
struct sniff_buffer * sb = user_data;
struct sniff_buffer * sb = userdata;
int64_t o = sb->offset;
switch(whence) {
case NESTEGG_SEEK_SET:
o = offset;
break;
case NESTEGG_SEEK_CUR:
o += offset;
break;
case NESTEGG_SEEK_END:
o = sb->length + offset;
break;
case NESTEGG_SEEK_SET:
o = offset;
break;
case NESTEGG_SEEK_CUR:
o += offset;
break;
case NESTEGG_SEEK_END:
o = sb->length + offset;
break;
}
if (o < 0 || o > (int64_t) sb->length)
@ -1834,9 +1918,9 @@ ne_buffer_seek(int64_t offset, int whence, void * user_data)
}
static int64_t
ne_buffer_tell(void * user_data)
ne_buffer_tell(void * userdata)
{
struct sniff_buffer * sb = user_data;
struct sniff_buffer * sb = userdata;
return sb->offset;
}
@ -1860,7 +1944,9 @@ ne_match_webm(nestegg_io io, int64_t max_offset)
nestegg_destroy(ctx);
return -1;
}
*ctx->io = io;
ctx->io->io = io;
ctx->io->bufsz = NE_IO_BUFSZ;
ctx->io->offset = -1;
ctx->alloc_pool = ne_pool_init();
if (!ctx->alloc_pool) {
nestegg_destroy(ctx);
@ -1918,7 +2004,9 @@ nestegg_init(nestegg ** context, nestegg_io io, nestegg_log callback, int64_t ma
nestegg_destroy(ctx);
return -1;
}
*ctx->io = io;
ctx->io->io = io;
ctx->io->bufsz = NE_IO_BUFSZ;
ctx->io->offset = -1;
ctx->log = callback;
ctx->alloc_pool = ne_pool_init();
if (!ctx->alloc_pool) {
@ -2264,35 +2352,35 @@ nestegg_track_codec_data(nestegg * ctx, unsigned int track, unsigned int item,
return -1;
if (nestegg_track_codec_id(ctx, track) != NESTEGG_CODEC_VORBIS
&& nestegg_track_codec_id(ctx, track) != NESTEGG_CODEC_OPUS)
&& nestegg_track_codec_id(ctx, track) != NESTEGG_CODEC_OPUS)
return -1;
if (ne_get_binary(entry->codec_private, &codec_private) != 0)
return -1;
if (nestegg_track_codec_id(ctx, track) == NESTEGG_CODEC_VORBIS) {
p = codec_private.data;
count = *p++ + 1;
p = codec_private.data;
count = *p++ + 1;
if (count > 3)
if (count > 3)
return -1;
i = 0;
total = 0;
while (--count) {
sizes[i] = ne_xiph_lace_value(&p);
total += sizes[i];
i += 1;
}
sizes[i] = codec_private.length - total - (p - codec_private.data);
for (i = 0; i < item; ++i) {
if (sizes[i] > LIMIT_FRAME)
return -1;
i = 0;
total = 0;
while (--count) {
sizes[i] = ne_xiph_lace_value(&p);
total += sizes[i];
i += 1;
}
sizes[i] = codec_private.length - total - (p - codec_private.data);
for (i = 0; i < item; ++i) {
if (sizes[i] > LIMIT_FRAME)
return -1;
p += sizes[i];
}
*data = p;
*length = sizes[item];
p += sizes[i];
}
*data = p;
*length = sizes[item];
} else {
*data = codec_private.data;
*length = codec_private.length;
@ -2494,7 +2582,7 @@ nestegg_free_packet(nestegg_packet * pkt)
free(block_additional);
}
free(pkt);
free(pkt);
}
int
@ -2588,28 +2676,31 @@ int
nestegg_has_cues(nestegg * ctx)
{
return ctx->segment.cues.cue_point.head ||
ne_find_seek_for_id(ctx->segment.seek_head.head, ID_CUES);
ne_find_seek_for_id(ctx->segment.seek_head.head, ID_CUES);
}
int
nestegg_sniff(unsigned char const * buffer, size_t length)
{
nestegg_io io;
struct sniff_buffer user_data;
struct sniff_buffer userdata;
user_data.buffer = buffer;
user_data.length = length;
user_data.offset = 0;
userdata.buffer = buffer;
userdata.length = length;
userdata.offset = 0;
io.read = ne_buffer_read;
io.seek = ne_buffer_seek;
io.tell = ne_buffer_tell;
io.userdata = &user_data;
io.userdata = &userdata;
return ne_match_webm(io, length);
}
void
/* From halloc.c */
int halloc_set_allocator(realloc_t realloc_func);
int
nestegg_set_halloc_func(void * (* realloc_func)(void *, size_t))
{
halloc_allocator = realloc_func;
return halloc_set_allocator(realloc_func);
}

View File

@ -7,7 +7,7 @@ cp $1/halloc/src/halloc.c src
cp $1/halloc/src/hlist.h src
cp $1/halloc/src/macros.h src
cp $1/LICENSE .
cp $1/README .
cp $1/README.md .
cp $1/AUTHORS .
if [ -d $1/.git ]; then
rev=$(cd $1 && git rev-parse --verify HEAD)