xiph.c 2.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2007  FFmpeg Project
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21
#include "libavutil/intreadwrite.h"
22 23
#include "xiph.h"

24
int avpriv_split_xiph_headers(uint8_t *extradata, int extradata_size,
25 26 27
                          int first_header_size, uint8_t *header_start[3],
                          int header_len[3])
{
28
    int i;
29

30 31
    if (extradata_size >= 6 && AV_RB16(extradata) == first_header_size) {
        int overall_len = 6;
32 33 34 35 36
        for (i=0; i<3; i++) {
            header_len[i] = AV_RB16(extradata);
            extradata += 2;
            header_start[i] = extradata;
            extradata += header_len[i];
37 38 39
            if (overall_len > extradata_size - header_len[i])
                return -1;
            overall_len += header_len[i];
40
        }
41 42
    } else if (extradata_size >= 3 && extradata_size < INT_MAX - 0x1ff && extradata[0] == 2) {
        int overall_len = 3;
43 44
        extradata++;
        for (i=0; i<2; i++, extradata++) {
45
            header_len[i] = 0;
46
            for (; overall_len < extradata_size && *extradata==0xff; extradata++) {
47
                header_len[i] += 0xff;
48
                overall_len   += 0xff + 1;
49
            }
50 51
            header_len[i] += *extradata;
            overall_len   += *extradata;
52
            if (overall_len > extradata_size)
53 54
                return -1;
        }
55
        header_len[2] = extradata_size - overall_len;
56 57 58 59 60 61 62 63
        header_start[0] = extradata;
        header_start[1] = header_start[0] + header_len[0];
        header_start[2] = header_start[1] + header_len[1];
    } else {
        return -1;
    }
    return 0;
}