libavcodec/xiph.c
5bb127a9
 /*
6dfa70f2
  * Copyright (C) 2007 The FFmpeg Project
5bb127a9
  *
  * 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
  */
 
6a5d31ac
 #include "libavutil/intreadwrite.h"
5bb127a9
 #include "xiph.h"
 
e5916087
 int avpriv_split_xiph_headers(const uint8_t *extradata, int extradata_size,
ad2deb02
                           int first_header_size, const uint8_t *header_start[3],
5bb127a9
                           int header_len[3])
 {
b189e670
     int i;
5bb127a9
 
58720ebd
     if (extradata_size >= 6 && AV_RB16(extradata) == first_header_size) {
         int overall_len = 6;
5bb127a9
         for (i=0; i<3; i++) {
             header_len[i] = AV_RB16(extradata);
             extradata += 2;
             header_start[i] = extradata;
             extradata += header_len[i];
58720ebd
             if (overall_len > extradata_size - header_len[i])
                 return -1;
             overall_len += header_len[i];
5bb127a9
         }
58720ebd
     } else if (extradata_size >= 3 && extradata_size < INT_MAX - 0x1ff && extradata[0] == 2) {
         int overall_len = 3;
b189e670
         extradata++;
         for (i=0; i<2; i++, extradata++) {
5bb127a9
             header_len[i] = 0;
b189e670
             for (; overall_len < extradata_size && *extradata==0xff; extradata++) {
5bb127a9
                 header_len[i] += 0xff;
58720ebd
                 overall_len   += 0xff + 1;
5bb127a9
             }
b189e670
             header_len[i] += *extradata;
             overall_len   += *extradata;
58720ebd
             if (overall_len > extradata_size)
5bb127a9
                 return -1;
         }
b189e670
         header_len[2] = extradata_size - overall_len;
5bb127a9
         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;
 }