static int ebml_parse_elem(MatroskaDemuxContext *matroska,

                           EbmlSyntax *syntax, void *data)

{

    static const uint64_t max_lengths[EBML_TYPE_COUNT] = {

        [EBML_UINT]  = 8,

        [EBML_FLOAT] = 8,

        // max. 16 MB for strings

        [EBML_STR]   = 0x1000000,

        [EBML_UTF8]  = 0x1000000,

        // max. 256 MB for binary data

        [EBML_BIN]   = 0x10000000,

        // no limits for anything else

    };

    AVIOContext *pb = matroska->ctx->pb;

    uint32_t id = syntax->id;

    uint64_t length;

    int res;



    data = (char *)data + syntax->data_offset;

    if (syntax->list_elem_size) {

        EbmlList *list = data;

        list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);

        data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;

        memset(data, 0, syntax->list_elem_size);

        list->nb_elem++;

    }



    if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {

        matroska->current_id = 0;

        if ((res = ebml_read_length(matroska, pb, &length)) < 0)

            return res;

        if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {

            av_log(matroska->ctx, AV_LOG_ERROR,

                   "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n",

                   length, max_lengths[syntax->type], syntax->type);

            return AVERROR_INVALIDDATA;

        }

    }



    switch (syntax->type) {

    case EBML_UINT:  res = ebml_read_uint  (pb, length, data);  break;

    case EBML_FLOAT: res = ebml_read_float (pb, length, data);  break;

    case EBML_STR:

    case EBML_UTF8:  res = ebml_read_ascii (pb, length, data);  break;

    case EBML_BIN:   res = ebml_read_binary(pb, length, data);  break;

    case EBML_NEST:  if ((res=ebml_read_master(matroska, length)) < 0)

                         return res;

                     if (id == MATROSKA_ID_SEGMENT)

                         matroska->segment_start = avio_tell(matroska->ctx->pb);

                     return ebml_parse_nest(matroska, syntax->def.n, data);

    case EBML_PASS:  return ebml_parse_id(matroska, syntax->def.n, id, data);

    case EBML_STOP:  return 1;

    default:         return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0;

    }

    if (res == AVERROR_INVALIDDATA)

        av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");

    else if (res == AVERROR(EIO))

        av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");

    return res;

}

static int ebml_parse_elem(MatroskaDemuxContext *matroska,

                           EbmlSyntax *syntax, void *data)

{

    static const uint64_t max_lengths[EBML_TYPE_COUNT] = {

        [EBML_UINT]  = 8,

        [EBML_FLOAT] = 8,

        

        [EBML_STR]   = 0x1000000,

        [EBML_UTF8]  = 0x1000000,

        

        [EBML_BIN]   = 0x10000000,

        

    };

    AVIOContext *pb = matroska->ctx->pb;

    uint32_t id = syntax->id;

    uint64_t length;

    int res;



    data = (char *)data + syntax->data_offset;

    if (syntax->list_elem_size) {

        EbmlList *list = data;

        list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);

        data = (char*)list->elem + list->nb_elem*syntax->list_elem_size;

        memset(data, 0, syntax->list_elem_size);

        list->nb_elem++;

    }



    if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) {

        matroska->current_id = 0;

        if ((res = ebml_read_length(matroska, pb, &length)) < 0)

            return res;

        if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) {

            av_log(matroska->ctx, AV_LOG_ERROR,

                   "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n",

                   length, max_lengths[syntax->type], syntax->type);

            return AVERROR_INVALIDDATA;

        }

    }



    switch (syntax->type) {

    case EBML_UINT:  res = ebml_read_uint  (pb, length, data);  break;

    case EBML_FLOAT: res = ebml_read_float (pb, length, data);  break;

    case EBML_STR:

    case EBML_UTF8:  res = ebml_read_ascii (pb, length, data);  break;

    case EBML_BIN:   res = ebml_read_binary(pb, length, data);  break;

    case EBML_NEST:  if ((res=ebml_read_master(matroska, length)) < 0)

                         return res;

                     if (id == MATROSKA_ID_SEGMENT)

                         matroska->segment_start = avio_tell(matroska->ctx->pb);

                     return ebml_parse_nest(matroska, syntax->def.n, data);

    case EBML_PASS:  return ebml_parse_id(matroska, syntax->def.n, id, data);

    case EBML_STOP:  return 1;

    default:         return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0;

    }

    if (res == AVERROR_INVALIDDATA)

        av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n");

    else if (res == AVERROR(EIO))

        av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n");

    return res;

}

        list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size);
static int FUNC_0(MatroskaDemuxContext *VAR_0,

                           EbmlSyntax *VAR_1, void *VAR_2)

{

    static const uint64_t VAR_3[EBML_TYPE_COUNT] = {

        [EBML_UINT]  = 8,

        [EBML_FLOAT] = 8,

        

        [EBML_STR]   = 0x1000000,

        [EBML_UTF8]  = 0x1000000,

        

        [EBML_BIN]   = 0x10000000,

        

    };

    AVIOContext *pb = VAR_0->ctx->pb;

    uint32_t id = VAR_1->id;

    uint64_t length;

    int VAR_4;



    VAR_2 = (char *)VAR_2 + VAR_1->data_offset;

    if (VAR_1->list_elem_size) {

        EbmlList *list = VAR_2;

        list->elem = av_realloc(list->elem, (list->nb_elem+1)*VAR_1->list_elem_size);

        VAR_2 = (char*)list->elem + list->nb_elem*VAR_1->list_elem_size;

        memset(VAR_2, 0, VAR_1->list_elem_size);

        list->nb_elem++;

    }



    if (VAR_1->type != EBML_PASS && VAR_1->type != EBML_STOP) {

        VAR_0->current_id = 0;

        if ((VAR_4 = ebml_read_length(VAR_0, pb, &length)) < 0)

            return VAR_4;

        if (VAR_3[VAR_1->type] && length > VAR_3[VAR_1->type]) {

            av_log(VAR_0->ctx, AV_LOG_ERROR,

                   "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for VAR_1 element %i\n",

                   length, VAR_3[VAR_1->type], VAR_1->type);

            return AVERROR_INVALIDDATA;

        }

    }



    switch (VAR_1->type) {

    case EBML_UINT:  VAR_4 = ebml_read_uint  (pb, length, VAR_2);  break;

    case EBML_FLOAT: VAR_4 = ebml_read_float (pb, length, VAR_2);  break;

    case EBML_STR:

    case EBML_UTF8:  VAR_4 = ebml_read_ascii (pb, length, VAR_2);  break;

    case EBML_BIN:   VAR_4 = ebml_read_binary(pb, length, VAR_2);  break;

    case EBML_NEST:  if ((VAR_4=ebml_read_master(VAR_0, length)) < 0)

                         return VAR_4;

                     if (id == MATROSKA_ID_SEGMENT)

                         VAR_0->segment_start = avio_tell(VAR_0->ctx->pb);

                     return ebml_parse_nest(VAR_0, VAR_1->def.n, VAR_2);

    case EBML_PASS:  return ebml_parse_id(VAR_0, VAR_1->def.n, id, VAR_2);

    case EBML_STOP:  return 1;

    default:         return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0;

    }

    if (VAR_4 == AVERROR_INVALIDDATA)

        av_log(VAR_0->ctx, AV_LOG_ERROR, "Invalid element\n");

    else if (VAR_4 == AVERROR(EIO))

        av_log(VAR_0->ctx, AV_LOG_ERROR, "Read error\n");

    return VAR_4;

}

static int FUNC_0(MatroskaDemuxContext *VAR_0,
EbmlSyntax *VAR_1, void *VAR_2)
{ static const uint64_t VAR_3[EBML_TYPE_COUNT] = { [EBML_UINT]  = 8,
[EBML_FLOAT] = 8,
[EBML_STR]   = 0x1000000,
[EBML_UTF8]  = 0x1000000,
[EBML_BIN]   = 0x10000000,
}; AVIOContext *pb = VAR_0->ctx->pb; uint32_t id = VAR_1->id; uint64_t length; int VAR_4; VAR_2 = (char *)VAR_2 + VAR_1->data_offset; if (VAR_1->list_elem_size) { EbmlList *list = VAR_2; list->elem = av_realloc(list->elem, (list->nb_elem+1)*VAR_1->list_elem_size); VAR_2 = (char*)list->elem + list->nb_elem*VAR_1->list_elem_size; memset(VAR_2, 0, VAR_1->list_elem_size); list->nb_elem++; } if (VAR_1->type != EBML_PASS && VAR_1->type != EBML_STOP) { VAR_0->current_id = 0; if ((VAR_4 = ebml_read_length(VAR_0, pb, &length)) < 0)
return VAR_4; if (VAR_3[VAR_1->type] && length > VAR_3[VAR_1->type]) { av_log(VAR_0->ctx, AV_LOG_ERROR,
"Invalid length 0x%"PRIx64" > 0x%"PRIx64" for VAR_1 element %i\n",
length, VAR_3[VAR_1->type], VAR_1->type); return AVERROR_INVALIDDATA; } } switch (VAR_1->type) { case EBML_UINT:  VAR_4 = ebml_read_uint  (pb, length, VAR_2);  break; case EBML_FLOAT: VAR_4 = ebml_read_float (pb, length, VAR_2);  break; case EBML_STR:
case EBML_UTF8:  VAR_4 = ebml_read_ascii (pb, length, VAR_2);  break; case EBML_BIN:   VAR_4 = ebml_read_binary(pb, length, VAR_2);  break; case EBML_NEST:  if ((VAR_4=ebml_read_master(VAR_0, length)) < 0)
return VAR_4; if (id == MATROSKA_ID_SEGMENT)
VAR_0->segment_start = avio_tell(VAR_0->ctx->pb); return ebml_parse_nest(VAR_0, VAR_1->def.n, VAR_2); case EBML_PASS:  return ebml_parse_id(VAR_0, VAR_1->def.n, id, VAR_2); case EBML_STOP:  return 1; default:         return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0; } if (VAR_4 == AVERROR_INVALIDDATA)
av_log(VAR_0->ctx, AV_LOG_ERROR, "Invalid element\n"); else if (VAR_4 == AVERROR(EIO))
av_log(VAR_0->ctx, AV_LOG_ERROR, "Read error\n"); return VAR_4; }