| 1 |
/* Copyright (C) 2008 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
#ifdef HAVE_CONFIG_H |
| 11 |
#include "config.h" |
| 12 |
#endif |
| 13 |
#ifdef HAVE_UNISTD_H |
| 14 |
#include <unistd.h> |
| 15 |
#endif |
| 16 |
#if defined WIN32 || defined _WIN32 || defined MSDOS || defined __CYGWIN__ || defined __EMX__ || defined OS2 |
| 17 |
#include <io.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#endif |
| 20 |
#if defined WIN32 || defined _WIN32 |
| 21 |
#include <process.h> |
| 22 |
#endif |
| 23 |
#include <stdarg.h> |
| 24 |
#include <stdio.h> |
| 25 |
#ifdef HAVE_STDLIB_H |
| 26 |
#include <stdlib.h> |
| 27 |
#endif |
| 28 |
#include <errno.h> |
| 29 |
#ifdef HAVE_STRING_H |
| 30 |
#include <string.h> |
| 31 |
#endif |
| 32 |
#include <ogg/ogg.h> |
| 33 |
#include "kate/oggkate.h" |
| 34 |
#include "kate_internal.h" |
| 35 |
#include "ksrt.h" |
| 36 |
#include "klrc.h" |
| 37 |
#include "kkate.h" |
| 38 |
#include "kfuzz.h" |
| 39 |
#include "kstream.h" |
| 40 |
#include "kutil.h" |
| 41 |
#include "kread.h" |
| 42 |
|
| 43 |
static int read_raw_packet(FILE *f,char **buffer,ogg_int64_t bytes) |
| 44 |
{ |
| 45 |
size_t ret; |
| 46 |
|
| 47 |
*buffer=(char*)kate_realloc(*buffer,bytes); |
| 48 |
if (!*buffer) return -1; |
| 49 |
|
| 50 |
ret=read(fileno(f),*buffer,bytes); |
| 51 |
if (ret<(size_t)bytes) return -1; |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
int read_raw_size_and_packet(FILE *f,char **buffer,ogg_int64_t *bytes) |
| 56 |
{ |
| 57 |
size_t bytes_read; |
| 58 |
int ret; |
| 59 |
|
| 60 |
/* all subsequent packets are prefixed with 64 bits (signed) of the packet length in bytes */ |
| 61 |
bytes_read=read(fileno(f),bytes,8); |
| 62 |
if (bytes_read!=8 || *bytes<=0) { |
| 63 |
fprintf(stderr,"failed to read raw kate packet size (read %lu, bytes %"PRId64")\n",(unsigned long)bytes_read,*bytes); |
| 64 |
return -1; |
| 65 |
} |
| 66 |
ret=read_raw_packet(f,buffer,*bytes); |
| 67 |
if (ret<0) { |
| 68 |
fprintf(stderr,"failed to read raw kate packet (%"PRId64" bytes)\n",*bytes); |
| 69 |
return -1; |
| 70 |
} |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
FILE *open_and_probe_stream(const char *filename) |
| 76 |
{ |
| 77 |
FILE *fin; |
| 78 |
|
| 79 |
if (!filename || !strcmp(filename,"-")) { |
| 80 |
#if defined WIN32 || defined _WIN32 |
| 81 |
_setmode(_fileno(stdin),_O_BINARY); |
| 82 |
#else |
| 83 |
#if defined MSDOS || defined __CYGWIN__ || defined __EMX__ || defined OS2 || defined __BORLANDC__ |
| 84 |
setmode(fileno(stdin),_O_BINARY); |
| 85 |
#endif |
| 86 |
#endif |
| 87 |
fin=stdin; |
| 88 |
} |
| 89 |
else { |
| 90 |
fin=fopen(filename,"rb"); |
| 91 |
if (!fin) { |
| 92 |
fprintf(stderr,"%s: %s\n",filename,strerror(errno)); |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
setvbuf(fin,NULL,_IONBF,0); |
| 96 |
} |
| 97 |
|
| 98 |
return fin; |
| 99 |
} |
| 100 |
|
| 101 |
int parse_ogg_stream(FILE *f,const char *pre_buffer,size_t pre_bytes,ogg_parser_funcs funcs,kate_uintptr_t data) |
| 102 |
{ |
| 103 |
char *ptr=NULL; |
| 104 |
ogg_sync_state oy; |
| 105 |
ogg_page og; |
| 106 |
ogg_int64_t bytes_read; |
| 107 |
static const size_t buffer_size=4096; |
| 108 |
size_t offset=0; |
| 109 |
int ret; |
| 110 |
long last_returned; |
| 111 |
long leftover_size=0; |
| 112 |
int in_hole=0; |
| 113 |
long first_hole_size=0; |
| 114 |
long hole_offset; |
| 115 |
|
| 116 |
ogg_sync_init(&oy); |
| 117 |
|
| 118 |
/* first add the buffer, if any */ |
| 119 |
if (pre_buffer && pre_bytes) { |
| 120 |
ptr=ogg_sync_buffer(&oy,pre_bytes); |
| 121 |
if (!ptr) { |
| 122 |
fprintf(stderr,"Failed to get sync buffer for %lu bytes\n",(unsigned long)pre_bytes); |
| 123 |
goto error; |
| 124 |
} |
| 125 |
memcpy(ptr,pre_buffer,pre_bytes); |
| 126 |
ogg_sync_wrote(&oy,pre_bytes); |
| 127 |
} |
| 128 |
|
| 129 |
while (1) { |
| 130 |
ptr=ogg_sync_buffer(&oy,buffer_size); |
| 131 |
if (!ptr) { |
| 132 |
fprintf(stderr,"Failed to get sync buffer for %lu bytes\n",(unsigned long)buffer_size); |
| 133 |
goto error; |
| 134 |
} |
| 135 |
bytes_read=read(fileno(f),ptr,buffer_size); |
| 136 |
if (bytes_read==0) { |
| 137 |
break; |
| 138 |
} |
| 139 |
ogg_sync_wrote(&oy,bytes_read); |
| 140 |
|
| 141 |
last_returned=0; |
| 142 |
while ((ret=ogg_sync_pageout(&oy,&og))!=0) { |
| 143 |
if (ret>0) { |
| 144 |
/* new page */ |
| 145 |
if (in_hole) { |
| 146 |
long hole_size=first_hole_size+leftover_size+oy.returned-(og.header_len+og.body_len); |
| 147 |
if (funcs.on_hole) { |
| 148 |
ret=(*funcs.on_hole)(data,hole_offset,hole_size); |
| 149 |
if (ret<0) goto error; |
| 150 |
} |
| 151 |
offset+=hole_size; |
| 152 |
} |
| 153 |
if (funcs.on_page) { |
| 154 |
ret=(*funcs.on_page)(data,offset,&og); |
| 155 |
if (ret<0) goto error; |
| 156 |
} |
| 157 |
in_hole=0; |
| 158 |
offset+=og.header_len+og.body_len; |
| 159 |
} |
| 160 |
else { |
| 161 |
/* hole in data */ |
| 162 |
first_hole_size=oy.returned-last_returned; |
| 163 |
hole_offset=offset; |
| 164 |
in_hole=1; |
| 165 |
} |
| 166 |
leftover_size=0; |
| 167 |
last_returned=oy.returned; |
| 168 |
} |
| 169 |
leftover_size+=oy.fill-last_returned; |
| 170 |
} |
| 171 |
|
| 172 |
/* detect holes at end of file */ |
| 173 |
if (leftover_size>0) { |
| 174 |
if (funcs.on_hole) { |
| 175 |
ret=(*funcs.on_hole)(data,offset,leftover_size); |
| 176 |
if (ret<0) goto error; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
ogg_sync_clear(&oy); |
| 181 |
return 0; |
| 182 |
|
| 183 |
error: |
| 184 |
ogg_sync_clear(&oy); |
| 185 |
return -1; |
| 186 |
} |
| 187 |
|