media/libogg/src/ogg_framing.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /********************************************************************
michael@0 2 * *
michael@0 3 * THIS FILE IS PART OF THE Ogg CONTAINER SOURCE CODE. *
michael@0 4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
michael@0 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
michael@0 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
michael@0 7 * *
michael@0 8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 *
michael@0 9 * by the Xiph.Org Foundation http://www.xiph.org/ *
michael@0 10 * *
michael@0 11 ********************************************************************
michael@0 12
michael@0 13 function: code raw packets into framed OggSquish stream and
michael@0 14 decode Ogg streams back into raw packets
michael@0 15 last mod: $Id: framing.c 18052 2011-08-04 17:57:02Z giles $
michael@0 16
michael@0 17 note: The CRC code is directly derived from public domain code by
michael@0 18 Ross Williams (ross@guest.adelaide.edu.au). See docs/framing.html
michael@0 19 for details.
michael@0 20
michael@0 21 ********************************************************************/
michael@0 22
michael@0 23 #include <stdlib.h>
michael@0 24 #include <string.h>
michael@0 25 #include <ogg/ogg.h>
michael@0 26
michael@0 27 /* A complete description of Ogg framing exists in docs/framing.html */
michael@0 28
michael@0 29 int ogg_page_version(const ogg_page *og){
michael@0 30 return((int)(og->header[4]));
michael@0 31 }
michael@0 32
michael@0 33 int ogg_page_continued(const ogg_page *og){
michael@0 34 return((int)(og->header[5]&0x01));
michael@0 35 }
michael@0 36
michael@0 37 int ogg_page_bos(const ogg_page *og){
michael@0 38 return((int)(og->header[5]&0x02));
michael@0 39 }
michael@0 40
michael@0 41 int ogg_page_eos(const ogg_page *og){
michael@0 42 return((int)(og->header[5]&0x04));
michael@0 43 }
michael@0 44
michael@0 45 ogg_int64_t ogg_page_granulepos(const ogg_page *og){
michael@0 46 unsigned char *page=og->header;
michael@0 47 ogg_int64_t granulepos=page[13]&(0xff);
michael@0 48 granulepos= (granulepos<<8)|(page[12]&0xff);
michael@0 49 granulepos= (granulepos<<8)|(page[11]&0xff);
michael@0 50 granulepos= (granulepos<<8)|(page[10]&0xff);
michael@0 51 granulepos= (granulepos<<8)|(page[9]&0xff);
michael@0 52 granulepos= (granulepos<<8)|(page[8]&0xff);
michael@0 53 granulepos= (granulepos<<8)|(page[7]&0xff);
michael@0 54 granulepos= (granulepos<<8)|(page[6]&0xff);
michael@0 55 return(granulepos);
michael@0 56 }
michael@0 57
michael@0 58 int ogg_page_serialno(const ogg_page *og){
michael@0 59 return(og->header[14] |
michael@0 60 (og->header[15]<<8) |
michael@0 61 (og->header[16]<<16) |
michael@0 62 (og->header[17]<<24));
michael@0 63 }
michael@0 64
michael@0 65 long ogg_page_pageno(const ogg_page *og){
michael@0 66 return(og->header[18] |
michael@0 67 (og->header[19]<<8) |
michael@0 68 (og->header[20]<<16) |
michael@0 69 (og->header[21]<<24));
michael@0 70 }
michael@0 71
michael@0 72
michael@0 73
michael@0 74 /* returns the number of packets that are completed on this page (if
michael@0 75 the leading packet is begun on a previous page, but ends on this
michael@0 76 page, it's counted */
michael@0 77
michael@0 78 /* NOTE:
michael@0 79 If a page consists of a packet begun on a previous page, and a new
michael@0 80 packet begun (but not completed) on this page, the return will be:
michael@0 81 ogg_page_packets(page) ==1,
michael@0 82 ogg_page_continued(page) !=0
michael@0 83
michael@0 84 If a page happens to be a single packet that was begun on a
michael@0 85 previous page, and spans to the next page (in the case of a three or
michael@0 86 more page packet), the return will be:
michael@0 87 ogg_page_packets(page) ==0,
michael@0 88 ogg_page_continued(page) !=0
michael@0 89 */
michael@0 90
michael@0 91 int ogg_page_packets(const ogg_page *og){
michael@0 92 int i,n=og->header[26],count=0;
michael@0 93 for(i=0;i<n;i++)
michael@0 94 if(og->header[27+i]<255)count++;
michael@0 95 return(count);
michael@0 96 }
michael@0 97
michael@0 98
michael@0 99 #if 0
michael@0 100 /* helper to initialize lookup for direct-table CRC (illustrative; we
michael@0 101 use the static init below) */
michael@0 102
michael@0 103 static ogg_uint32_t _ogg_crc_entry(unsigned long index){
michael@0 104 int i;
michael@0 105 unsigned long r;
michael@0 106
michael@0 107 r = index << 24;
michael@0 108 for (i=0; i<8; i++)
michael@0 109 if (r & 0x80000000UL)
michael@0 110 r = (r << 1) ^ 0x04c11db7; /* The same as the ethernet generator
michael@0 111 polynomial, although we use an
michael@0 112 unreflected alg and an init/final
michael@0 113 of 0, not 0xffffffff */
michael@0 114 else
michael@0 115 r<<=1;
michael@0 116 return (r & 0xffffffffUL);
michael@0 117 }
michael@0 118 #endif
michael@0 119
michael@0 120 static const ogg_uint32_t crc_lookup[256]={
michael@0 121 0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9,
michael@0 122 0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005,
michael@0 123 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61,
michael@0 124 0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd,
michael@0 125 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9,
michael@0 126 0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75,
michael@0 127 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011,
michael@0 128 0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd,
michael@0 129 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039,
michael@0 130 0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5,
michael@0 131 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81,
michael@0 132 0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d,
michael@0 133 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49,
michael@0 134 0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95,
michael@0 135 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1,
michael@0 136 0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d,
michael@0 137 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae,
michael@0 138 0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072,
michael@0 139 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16,
michael@0 140 0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca,
michael@0 141 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde,
michael@0 142 0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02,
michael@0 143 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066,
michael@0 144 0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba,
michael@0 145 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e,
michael@0 146 0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692,
michael@0 147 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6,
michael@0 148 0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a,
michael@0 149 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e,
michael@0 150 0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2,
michael@0 151 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686,
michael@0 152 0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a,
michael@0 153 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637,
michael@0 154 0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb,
michael@0 155 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f,
michael@0 156 0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53,
michael@0 157 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47,
michael@0 158 0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b,
michael@0 159 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff,
michael@0 160 0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623,
michael@0 161 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7,
michael@0 162 0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b,
michael@0 163 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f,
michael@0 164 0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3,
michael@0 165 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7,
michael@0 166 0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b,
michael@0 167 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f,
michael@0 168 0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3,
michael@0 169 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640,
michael@0 170 0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c,
michael@0 171 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8,
michael@0 172 0x68860bfd,0x6c47164a,0x61043093,0x65c52d24,
michael@0 173 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30,
michael@0 174 0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec,
michael@0 175 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088,
michael@0 176 0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654,
michael@0 177 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0,
michael@0 178 0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c,
michael@0 179 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18,
michael@0 180 0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4,
michael@0 181 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0,
michael@0 182 0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c,
michael@0 183 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668,
michael@0 184 0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4};
michael@0 185
michael@0 186 /* init the encode/decode logical stream state */
michael@0 187
michael@0 188 int ogg_stream_init(ogg_stream_state *os,int serialno){
michael@0 189 if(os){
michael@0 190 memset(os,0,sizeof(*os));
michael@0 191 os->body_storage=16*1024;
michael@0 192 os->lacing_storage=1024;
michael@0 193
michael@0 194 os->body_data=_ogg_malloc(os->body_storage*sizeof(*os->body_data));
michael@0 195 os->lacing_vals=_ogg_malloc(os->lacing_storage*sizeof(*os->lacing_vals));
michael@0 196 os->granule_vals=_ogg_malloc(os->lacing_storage*sizeof(*os->granule_vals));
michael@0 197
michael@0 198 if(!os->body_data || !os->lacing_vals || !os->granule_vals){
michael@0 199 ogg_stream_clear(os);
michael@0 200 return -1;
michael@0 201 }
michael@0 202
michael@0 203 os->serialno=serialno;
michael@0 204
michael@0 205 return(0);
michael@0 206 }
michael@0 207 return(-1);
michael@0 208 }
michael@0 209
michael@0 210 /* async/delayed error detection for the ogg_stream_state */
michael@0 211 int ogg_stream_check(ogg_stream_state *os){
michael@0 212 if(!os || !os->body_data) return -1;
michael@0 213 return 0;
michael@0 214 }
michael@0 215
michael@0 216 /* _clear does not free os, only the non-flat storage within */
michael@0 217 int ogg_stream_clear(ogg_stream_state *os){
michael@0 218 if(os){
michael@0 219 if(os->body_data)_ogg_free(os->body_data);
michael@0 220 if(os->lacing_vals)_ogg_free(os->lacing_vals);
michael@0 221 if(os->granule_vals)_ogg_free(os->granule_vals);
michael@0 222
michael@0 223 memset(os,0,sizeof(*os));
michael@0 224 }
michael@0 225 return(0);
michael@0 226 }
michael@0 227
michael@0 228 int ogg_stream_destroy(ogg_stream_state *os){
michael@0 229 if(os){
michael@0 230 ogg_stream_clear(os);
michael@0 231 _ogg_free(os);
michael@0 232 }
michael@0 233 return(0);
michael@0 234 }
michael@0 235
michael@0 236 /* Helpers for ogg_stream_encode; this keeps the structure and
michael@0 237 what's happening fairly clear */
michael@0 238
michael@0 239 static int _os_body_expand(ogg_stream_state *os,int needed){
michael@0 240 if(os->body_storage<=os->body_fill+needed){
michael@0 241 void *ret;
michael@0 242 ret=_ogg_realloc(os->body_data,(os->body_storage+needed+1024)*
michael@0 243 sizeof(*os->body_data));
michael@0 244 if(!ret){
michael@0 245 ogg_stream_clear(os);
michael@0 246 return -1;
michael@0 247 }
michael@0 248 os->body_storage+=(needed+1024);
michael@0 249 os->body_data=ret;
michael@0 250 }
michael@0 251 return 0;
michael@0 252 }
michael@0 253
michael@0 254 static int _os_lacing_expand(ogg_stream_state *os,int needed){
michael@0 255 if(os->lacing_storage<=os->lacing_fill+needed){
michael@0 256 void *ret;
michael@0 257 ret=_ogg_realloc(os->lacing_vals,(os->lacing_storage+needed+32)*
michael@0 258 sizeof(*os->lacing_vals));
michael@0 259 if(!ret){
michael@0 260 ogg_stream_clear(os);
michael@0 261 return -1;
michael@0 262 }
michael@0 263 os->lacing_vals=ret;
michael@0 264 ret=_ogg_realloc(os->granule_vals,(os->lacing_storage+needed+32)*
michael@0 265 sizeof(*os->granule_vals));
michael@0 266 if(!ret){
michael@0 267 ogg_stream_clear(os);
michael@0 268 return -1;
michael@0 269 }
michael@0 270 os->granule_vals=ret;
michael@0 271 os->lacing_storage+=(needed+32);
michael@0 272 }
michael@0 273 return 0;
michael@0 274 }
michael@0 275
michael@0 276 /* checksum the page */
michael@0 277 /* Direct table CRC; note that this will be faster in the future if we
michael@0 278 perform the checksum simultaneously with other copies */
michael@0 279
michael@0 280 void ogg_page_checksum_set(ogg_page *og){
michael@0 281 if(og){
michael@0 282 ogg_uint32_t crc_reg=0;
michael@0 283 int i;
michael@0 284
michael@0 285 /* safety; needed for API behavior, but not framing code */
michael@0 286 og->header[22]=0;
michael@0 287 og->header[23]=0;
michael@0 288 og->header[24]=0;
michael@0 289 og->header[25]=0;
michael@0 290
michael@0 291 for(i=0;i<og->header_len;i++)
michael@0 292 crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->header[i]];
michael@0 293 for(i=0;i<og->body_len;i++)
michael@0 294 crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->body[i]];
michael@0 295
michael@0 296 og->header[22]=(unsigned char)(crc_reg&0xff);
michael@0 297 og->header[23]=(unsigned char)((crc_reg>>8)&0xff);
michael@0 298 og->header[24]=(unsigned char)((crc_reg>>16)&0xff);
michael@0 299 og->header[25]=(unsigned char)((crc_reg>>24)&0xff);
michael@0 300 }
michael@0 301 }
michael@0 302
michael@0 303 /* submit data to the internal buffer of the framing engine */
michael@0 304 int ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov, int count,
michael@0 305 long e_o_s, ogg_int64_t granulepos){
michael@0 306
michael@0 307 int bytes = 0, lacing_vals, i;
michael@0 308
michael@0 309 if(ogg_stream_check(os)) return -1;
michael@0 310 if(!iov) return 0;
michael@0 311
michael@0 312 for (i = 0; i < count; ++i) bytes += (int)iov[i].iov_len;
michael@0 313 lacing_vals=bytes/255+1;
michael@0 314
michael@0 315 if(os->body_returned){
michael@0 316 /* advance packet data according to the body_returned pointer. We
michael@0 317 had to keep it around to return a pointer into the buffer last
michael@0 318 call */
michael@0 319
michael@0 320 os->body_fill-=os->body_returned;
michael@0 321 if(os->body_fill)
michael@0 322 memmove(os->body_data,os->body_data+os->body_returned,
michael@0 323 os->body_fill);
michael@0 324 os->body_returned=0;
michael@0 325 }
michael@0 326
michael@0 327 /* make sure we have the buffer storage */
michael@0 328 if(_os_body_expand(os,bytes) || _os_lacing_expand(os,lacing_vals))
michael@0 329 return -1;
michael@0 330
michael@0 331 /* Copy in the submitted packet. Yes, the copy is a waste; this is
michael@0 332 the liability of overly clean abstraction for the time being. It
michael@0 333 will actually be fairly easy to eliminate the extra copy in the
michael@0 334 future */
michael@0 335
michael@0 336 for (i = 0; i < count; ++i) {
michael@0 337 memcpy(os->body_data+os->body_fill, iov[i].iov_base, iov[i].iov_len);
michael@0 338 os->body_fill += (int)iov[i].iov_len;
michael@0 339 }
michael@0 340
michael@0 341 /* Store lacing vals for this packet */
michael@0 342 for(i=0;i<lacing_vals-1;i++){
michael@0 343 os->lacing_vals[os->lacing_fill+i]=255;
michael@0 344 os->granule_vals[os->lacing_fill+i]=os->granulepos;
michael@0 345 }
michael@0 346 os->lacing_vals[os->lacing_fill+i]=bytes%255;
michael@0 347 os->granulepos=os->granule_vals[os->lacing_fill+i]=granulepos;
michael@0 348
michael@0 349 /* flag the first segment as the beginning of the packet */
michael@0 350 os->lacing_vals[os->lacing_fill]|= 0x100;
michael@0 351
michael@0 352 os->lacing_fill+=lacing_vals;
michael@0 353
michael@0 354 /* for the sake of completeness */
michael@0 355 os->packetno++;
michael@0 356
michael@0 357 if(e_o_s)os->e_o_s=1;
michael@0 358
michael@0 359 return(0);
michael@0 360 }
michael@0 361
michael@0 362 int ogg_stream_packetin(ogg_stream_state *os,ogg_packet *op){
michael@0 363 ogg_iovec_t iov;
michael@0 364 iov.iov_base = op->packet;
michael@0 365 iov.iov_len = op->bytes;
michael@0 366 return ogg_stream_iovecin(os, &iov, 1, op->e_o_s, op->granulepos);
michael@0 367 }
michael@0 368
michael@0 369 /* Conditionally flush a page; force==0 will only flush nominal-size
michael@0 370 pages, force==1 forces us to flush a page regardless of page size
michael@0 371 so long as there's any data available at all. */
michael@0 372 static int ogg_stream_flush_i(ogg_stream_state *os,ogg_page *og, int force, int nfill){
michael@0 373 int i;
michael@0 374 int vals=0;
michael@0 375 int maxvals=(os->lacing_fill>255?255:os->lacing_fill);
michael@0 376 int bytes=0;
michael@0 377 long acc=0;
michael@0 378 ogg_int64_t granule_pos=-1;
michael@0 379
michael@0 380 if(ogg_stream_check(os)) return(0);
michael@0 381 if(maxvals==0) return(0);
michael@0 382
michael@0 383 /* construct a page */
michael@0 384 /* decide how many segments to include */
michael@0 385
michael@0 386 /* If this is the initial header case, the first page must only include
michael@0 387 the initial header packet */
michael@0 388 if(os->b_o_s==0){ /* 'initial header page' case */
michael@0 389 granule_pos=0;
michael@0 390 for(vals=0;vals<maxvals;vals++){
michael@0 391 if((os->lacing_vals[vals]&0x0ff)<255){
michael@0 392 vals++;
michael@0 393 break;
michael@0 394 }
michael@0 395 }
michael@0 396 }else{
michael@0 397
michael@0 398 /* The extra packets_done, packet_just_done logic here attempts to do two things:
michael@0 399 1) Don't unneccessarily span pages.
michael@0 400 2) Unless necessary, don't flush pages if there are less than four packets on
michael@0 401 them; this expands page size to reduce unneccessary overhead if incoming packets
michael@0 402 are large.
michael@0 403 These are not necessary behaviors, just 'always better than naive flushing'
michael@0 404 without requiring an application to explicitly request a specific optimized
michael@0 405 behavior. We'll want an explicit behavior setup pathway eventually as well. */
michael@0 406
michael@0 407 int packets_done=0;
michael@0 408 int packet_just_done=0;
michael@0 409 for(vals=0;vals<maxvals;vals++){
michael@0 410 if(acc>nfill && packet_just_done>=4){
michael@0 411 force=1;
michael@0 412 break;
michael@0 413 }
michael@0 414 acc+=os->lacing_vals[vals]&0x0ff;
michael@0 415 if((os->lacing_vals[vals]&0xff)<255){
michael@0 416 granule_pos=os->granule_vals[vals];
michael@0 417 packet_just_done=++packets_done;
michael@0 418 }else
michael@0 419 packet_just_done=0;
michael@0 420 }
michael@0 421 if(vals==255)force=1;
michael@0 422 }
michael@0 423
michael@0 424 if(!force) return(0);
michael@0 425
michael@0 426 /* construct the header in temp storage */
michael@0 427 memcpy(os->header,"OggS",4);
michael@0 428
michael@0 429 /* stream structure version */
michael@0 430 os->header[4]=0x00;
michael@0 431
michael@0 432 /* continued packet flag? */
michael@0 433 os->header[5]=0x00;
michael@0 434 if((os->lacing_vals[0]&0x100)==0)os->header[5]|=0x01;
michael@0 435 /* first page flag? */
michael@0 436 if(os->b_o_s==0)os->header[5]|=0x02;
michael@0 437 /* last page flag? */
michael@0 438 if(os->e_o_s && os->lacing_fill==vals)os->header[5]|=0x04;
michael@0 439 os->b_o_s=1;
michael@0 440
michael@0 441 /* 64 bits of PCM position */
michael@0 442 for(i=6;i<14;i++){
michael@0 443 os->header[i]=(unsigned char)(granule_pos&0xff);
michael@0 444 granule_pos>>=8;
michael@0 445 }
michael@0 446
michael@0 447 /* 32 bits of stream serial number */
michael@0 448 {
michael@0 449 long serialno=os->serialno;
michael@0 450 for(i=14;i<18;i++){
michael@0 451 os->header[i]=(unsigned char)(serialno&0xff);
michael@0 452 serialno>>=8;
michael@0 453 }
michael@0 454 }
michael@0 455
michael@0 456 /* 32 bits of page counter (we have both counter and page header
michael@0 457 because this val can roll over) */
michael@0 458 if(os->pageno==-1)os->pageno=0; /* because someone called
michael@0 459 stream_reset; this would be a
michael@0 460 strange thing to do in an
michael@0 461 encode stream, but it has
michael@0 462 plausible uses */
michael@0 463 {
michael@0 464 long pageno=os->pageno++;
michael@0 465 for(i=18;i<22;i++){
michael@0 466 os->header[i]=(unsigned char)(pageno&0xff);
michael@0 467 pageno>>=8;
michael@0 468 }
michael@0 469 }
michael@0 470
michael@0 471 /* zero for computation; filled in later */
michael@0 472 os->header[22]=0;
michael@0 473 os->header[23]=0;
michael@0 474 os->header[24]=0;
michael@0 475 os->header[25]=0;
michael@0 476
michael@0 477 /* segment table */
michael@0 478 os->header[26]=(unsigned char)(vals&0xff);
michael@0 479 for(i=0;i<vals;i++)
michael@0 480 bytes+=os->header[i+27]=(unsigned char)(os->lacing_vals[i]&0xff);
michael@0 481
michael@0 482 /* set pointers in the ogg_page struct */
michael@0 483 og->header=os->header;
michael@0 484 og->header_len=os->header_fill=vals+27;
michael@0 485 og->body=os->body_data+os->body_returned;
michael@0 486 og->body_len=bytes;
michael@0 487
michael@0 488 /* advance the lacing data and set the body_returned pointer */
michael@0 489
michael@0 490 os->lacing_fill-=vals;
michael@0 491 memmove(os->lacing_vals,os->lacing_vals+vals,os->lacing_fill*sizeof(*os->lacing_vals));
michael@0 492 memmove(os->granule_vals,os->granule_vals+vals,os->lacing_fill*sizeof(*os->granule_vals));
michael@0 493 os->body_returned+=bytes;
michael@0 494
michael@0 495 /* calculate the checksum */
michael@0 496
michael@0 497 ogg_page_checksum_set(og);
michael@0 498
michael@0 499 /* done */
michael@0 500 return(1);
michael@0 501 }
michael@0 502
michael@0 503 /* This will flush remaining packets into a page (returning nonzero),
michael@0 504 even if there is not enough data to trigger a flush normally
michael@0 505 (undersized page). If there are no packets or partial packets to
michael@0 506 flush, ogg_stream_flush returns 0. Note that ogg_stream_flush will
michael@0 507 try to flush a normal sized page like ogg_stream_pageout; a call to
michael@0 508 ogg_stream_flush does not guarantee that all packets have flushed.
michael@0 509 Only a return value of 0 from ogg_stream_flush indicates all packet
michael@0 510 data is flushed into pages.
michael@0 511
michael@0 512 since ogg_stream_flush will flush the last page in a stream even if
michael@0 513 it's undersized, you almost certainly want to use ogg_stream_pageout
michael@0 514 (and *not* ogg_stream_flush) unless you specifically need to flush
michael@0 515 a page regardless of size in the middle of a stream. */
michael@0 516
michael@0 517 int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){
michael@0 518 return ogg_stream_flush_i(os,og,1,4096);
michael@0 519 }
michael@0 520
michael@0 521 /* Like the above, but an argument is provided to adjust the nominal
michael@0 522 page size for applications which are smart enough to provide their
michael@0 523 own delay based flushing */
michael@0 524
michael@0 525 int ogg_stream_flush_fill(ogg_stream_state *os,ogg_page *og, int nfill){
michael@0 526 return ogg_stream_flush_i(os,og,1,nfill);
michael@0 527 }
michael@0 528
michael@0 529 /* This constructs pages from buffered packet segments. The pointers
michael@0 530 returned are to static buffers; do not free. The returned buffers are
michael@0 531 good only until the next call (using the same ogg_stream_state) */
michael@0 532
michael@0 533 int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og){
michael@0 534 int force=0;
michael@0 535 if(ogg_stream_check(os)) return 0;
michael@0 536
michael@0 537 if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */
michael@0 538 (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */
michael@0 539 force=1;
michael@0 540
michael@0 541 return(ogg_stream_flush_i(os,og,force,4096));
michael@0 542 }
michael@0 543
michael@0 544 /* Like the above, but an argument is provided to adjust the nominal
michael@0 545 page size for applications which are smart enough to provide their
michael@0 546 own delay based flushing */
michael@0 547
michael@0 548 int ogg_stream_pageout_fill(ogg_stream_state *os, ogg_page *og, int nfill){
michael@0 549 int force=0;
michael@0 550 if(ogg_stream_check(os)) return 0;
michael@0 551
michael@0 552 if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */
michael@0 553 (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */
michael@0 554 force=1;
michael@0 555
michael@0 556 return(ogg_stream_flush_i(os,og,force,nfill));
michael@0 557 }
michael@0 558
michael@0 559 int ogg_stream_eos(ogg_stream_state *os){
michael@0 560 if(ogg_stream_check(os)) return 1;
michael@0 561 return os->e_o_s;
michael@0 562 }
michael@0 563
michael@0 564 /* DECODING PRIMITIVES: packet streaming layer **********************/
michael@0 565
michael@0 566 /* This has two layers to place more of the multi-serialno and paging
michael@0 567 control in the application's hands. First, we expose a data buffer
michael@0 568 using ogg_sync_buffer(). The app either copies into the
michael@0 569 buffer, or passes it directly to read(), etc. We then call
michael@0 570 ogg_sync_wrote() to tell how many bytes we just added.
michael@0 571
michael@0 572 Pages are returned (pointers into the buffer in ogg_sync_state)
michael@0 573 by ogg_sync_pageout(). The page is then submitted to
michael@0 574 ogg_stream_pagein() along with the appropriate
michael@0 575 ogg_stream_state* (ie, matching serialno). We then get raw
michael@0 576 packets out calling ogg_stream_packetout() with a
michael@0 577 ogg_stream_state. */
michael@0 578
michael@0 579 /* initialize the struct to a known state */
michael@0 580 int ogg_sync_init(ogg_sync_state *oy){
michael@0 581 if(oy){
michael@0 582 oy->storage = -1; /* used as a readiness flag */
michael@0 583 memset(oy,0,sizeof(*oy));
michael@0 584 }
michael@0 585 return(0);
michael@0 586 }
michael@0 587
michael@0 588 /* clear non-flat storage within */
michael@0 589 int ogg_sync_clear(ogg_sync_state *oy){
michael@0 590 if(oy){
michael@0 591 if(oy->data)_ogg_free(oy->data);
michael@0 592 memset(oy,0,sizeof(*oy));
michael@0 593 }
michael@0 594 return(0);
michael@0 595 }
michael@0 596
michael@0 597 int ogg_sync_destroy(ogg_sync_state *oy){
michael@0 598 if(oy){
michael@0 599 ogg_sync_clear(oy);
michael@0 600 _ogg_free(oy);
michael@0 601 }
michael@0 602 return(0);
michael@0 603 }
michael@0 604
michael@0 605 int ogg_sync_check(ogg_sync_state *oy){
michael@0 606 if(oy->storage<0) return -1;
michael@0 607 return 0;
michael@0 608 }
michael@0 609
michael@0 610 char *ogg_sync_buffer(ogg_sync_state *oy, long size){
michael@0 611 if(ogg_sync_check(oy)) return NULL;
michael@0 612
michael@0 613 /* first, clear out any space that has been previously returned */
michael@0 614 if(oy->returned){
michael@0 615 oy->fill-=oy->returned;
michael@0 616 if(oy->fill>0)
michael@0 617 memmove(oy->data,oy->data+oy->returned,oy->fill);
michael@0 618 oy->returned=0;
michael@0 619 }
michael@0 620
michael@0 621 if(size>oy->storage-oy->fill){
michael@0 622 /* We need to extend the internal buffer */
michael@0 623 long newsize=size+oy->fill+4096; /* an extra page to be nice */
michael@0 624 void *ret;
michael@0 625
michael@0 626 if(oy->data)
michael@0 627 ret=_ogg_realloc(oy->data,newsize);
michael@0 628 else
michael@0 629 ret=_ogg_malloc(newsize);
michael@0 630 if(!ret){
michael@0 631 ogg_sync_clear(oy);
michael@0 632 return NULL;
michael@0 633 }
michael@0 634 oy->data=ret;
michael@0 635 oy->storage=newsize;
michael@0 636 }
michael@0 637
michael@0 638 /* expose a segment at least as large as requested at the fill mark */
michael@0 639 return((char *)oy->data+oy->fill);
michael@0 640 }
michael@0 641
michael@0 642 int ogg_sync_wrote(ogg_sync_state *oy, long bytes){
michael@0 643 if(ogg_sync_check(oy))return -1;
michael@0 644 if(oy->fill+bytes>oy->storage)return -1;
michael@0 645 oy->fill+=bytes;
michael@0 646 return(0);
michael@0 647 }
michael@0 648
michael@0 649 /* sync the stream. This is meant to be useful for finding page
michael@0 650 boundaries.
michael@0 651
michael@0 652 return values for this:
michael@0 653 -n) skipped n bytes
michael@0 654 0) page not ready; more data (no bytes skipped)
michael@0 655 n) page synced at current location; page length n bytes
michael@0 656
michael@0 657 */
michael@0 658
michael@0 659 long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og){
michael@0 660 unsigned char *page=oy->data+oy->returned;
michael@0 661 unsigned char *next;
michael@0 662 long bytes=oy->fill-oy->returned;
michael@0 663
michael@0 664 if(ogg_sync_check(oy))return 0;
michael@0 665
michael@0 666 if(oy->headerbytes==0){
michael@0 667 int headerbytes,i;
michael@0 668 if(bytes<27)return(0); /* not enough for a header */
michael@0 669
michael@0 670 /* verify capture pattern */
michael@0 671 if(memcmp(page,"OggS",4))goto sync_fail;
michael@0 672
michael@0 673 headerbytes=page[26]+27;
michael@0 674 if(bytes<headerbytes)return(0); /* not enough for header + seg table */
michael@0 675
michael@0 676 /* count up body length in the segment table */
michael@0 677
michael@0 678 for(i=0;i<page[26];i++)
michael@0 679 oy->bodybytes+=page[27+i];
michael@0 680 oy->headerbytes=headerbytes;
michael@0 681 }
michael@0 682
michael@0 683 if(oy->bodybytes+oy->headerbytes>bytes)return(0);
michael@0 684
michael@0 685 /* The whole test page is buffered. Verify the checksum */
michael@0 686 {
michael@0 687 /* Grab the checksum bytes, set the header field to zero */
michael@0 688 char chksum[4];
michael@0 689 ogg_page log;
michael@0 690
michael@0 691 memcpy(chksum,page+22,4);
michael@0 692 memset(page+22,0,4);
michael@0 693
michael@0 694 /* set up a temp page struct and recompute the checksum */
michael@0 695 log.header=page;
michael@0 696 log.header_len=oy->headerbytes;
michael@0 697 log.body=page+oy->headerbytes;
michael@0 698 log.body_len=oy->bodybytes;
michael@0 699 ogg_page_checksum_set(&log);
michael@0 700
michael@0 701 /* Compare */
michael@0 702 if(memcmp(chksum,page+22,4)){
michael@0 703 /* D'oh. Mismatch! Corrupt page (or miscapture and not a page
michael@0 704 at all) */
michael@0 705 /* replace the computed checksum with the one actually read in */
michael@0 706 memcpy(page+22,chksum,4);
michael@0 707
michael@0 708 /* Bad checksum. Lose sync */
michael@0 709 goto sync_fail;
michael@0 710 }
michael@0 711 }
michael@0 712
michael@0 713 /* yes, have a whole page all ready to go */
michael@0 714 {
michael@0 715 unsigned char *page=oy->data+oy->returned;
michael@0 716 long bytes;
michael@0 717
michael@0 718 if(og){
michael@0 719 og->header=page;
michael@0 720 og->header_len=oy->headerbytes;
michael@0 721 og->body=page+oy->headerbytes;
michael@0 722 og->body_len=oy->bodybytes;
michael@0 723 }
michael@0 724
michael@0 725 oy->unsynced=0;
michael@0 726 oy->returned+=(bytes=oy->headerbytes+oy->bodybytes);
michael@0 727 oy->headerbytes=0;
michael@0 728 oy->bodybytes=0;
michael@0 729 return(bytes);
michael@0 730 }
michael@0 731
michael@0 732 sync_fail:
michael@0 733
michael@0 734 oy->headerbytes=0;
michael@0 735 oy->bodybytes=0;
michael@0 736
michael@0 737 /* search for possible capture */
michael@0 738 next=memchr(page+1,'O',bytes-1);
michael@0 739 if(!next)
michael@0 740 next=oy->data+oy->fill;
michael@0 741
michael@0 742 oy->returned=(int)(next-oy->data);
michael@0 743 return((long)-(next-page));
michael@0 744 }
michael@0 745
michael@0 746 /* sync the stream and get a page. Keep trying until we find a page.
michael@0 747 Suppress 'sync errors' after reporting the first.
michael@0 748
michael@0 749 return values:
michael@0 750 -1) recapture (hole in data)
michael@0 751 0) need more data
michael@0 752 1) page returned
michael@0 753
michael@0 754 Returns pointers into buffered data; invalidated by next call to
michael@0 755 _stream, _clear, _init, or _buffer */
michael@0 756
michael@0 757 int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og){
michael@0 758
michael@0 759 if(ogg_sync_check(oy))return 0;
michael@0 760
michael@0 761 /* all we need to do is verify a page at the head of the stream
michael@0 762 buffer. If it doesn't verify, we look for the next potential
michael@0 763 frame */
michael@0 764
michael@0 765 for(;;){
michael@0 766 long ret=ogg_sync_pageseek(oy,og);
michael@0 767 if(ret>0){
michael@0 768 /* have a page */
michael@0 769 return(1);
michael@0 770 }
michael@0 771 if(ret==0){
michael@0 772 /* need more data */
michael@0 773 return(0);
michael@0 774 }
michael@0 775
michael@0 776 /* head did not start a synced page... skipped some bytes */
michael@0 777 if(!oy->unsynced){
michael@0 778 oy->unsynced=1;
michael@0 779 return(-1);
michael@0 780 }
michael@0 781
michael@0 782 /* loop. keep looking */
michael@0 783
michael@0 784 }
michael@0 785 }
michael@0 786
michael@0 787 /* add the incoming page to the stream state; we decompose the page
michael@0 788 into packet segments here as well. */
michael@0 789
michael@0 790 int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){
michael@0 791 unsigned char *header=og->header;
michael@0 792 unsigned char *body=og->body;
michael@0 793 long bodysize=og->body_len;
michael@0 794 int segptr=0;
michael@0 795
michael@0 796 int version=ogg_page_version(og);
michael@0 797 int continued=ogg_page_continued(og);
michael@0 798 int bos=ogg_page_bos(og);
michael@0 799 int eos=ogg_page_eos(og);
michael@0 800 ogg_int64_t granulepos=ogg_page_granulepos(og);
michael@0 801 int serialno=ogg_page_serialno(og);
michael@0 802 long pageno=ogg_page_pageno(og);
michael@0 803 int segments=header[26];
michael@0 804
michael@0 805 if(ogg_stream_check(os)) return -1;
michael@0 806
michael@0 807 /* clean up 'returned data' */
michael@0 808 {
michael@0 809 long lr=os->lacing_returned;
michael@0 810 long br=os->body_returned;
michael@0 811
michael@0 812 /* body data */
michael@0 813 if(br){
michael@0 814 os->body_fill-=br;
michael@0 815 if(os->body_fill)
michael@0 816 memmove(os->body_data,os->body_data+br,os->body_fill);
michael@0 817 os->body_returned=0;
michael@0 818 }
michael@0 819
michael@0 820 if(lr){
michael@0 821 /* segment table */
michael@0 822 if(os->lacing_fill-lr){
michael@0 823 memmove(os->lacing_vals,os->lacing_vals+lr,
michael@0 824 (os->lacing_fill-lr)*sizeof(*os->lacing_vals));
michael@0 825 memmove(os->granule_vals,os->granule_vals+lr,
michael@0 826 (os->lacing_fill-lr)*sizeof(*os->granule_vals));
michael@0 827 }
michael@0 828 os->lacing_fill-=lr;
michael@0 829 os->lacing_packet-=lr;
michael@0 830 os->lacing_returned=0;
michael@0 831 }
michael@0 832 }
michael@0 833
michael@0 834 /* check the serial number */
michael@0 835 if(serialno!=os->serialno)return(-1);
michael@0 836 if(version>0)return(-1);
michael@0 837
michael@0 838 if(_os_lacing_expand(os,segments+1)) return -1;
michael@0 839
michael@0 840 /* are we in sequence? */
michael@0 841 if(pageno!=os->pageno){
michael@0 842 int i;
michael@0 843
michael@0 844 /* unroll previous partial packet (if any) */
michael@0 845 for(i=os->lacing_packet;i<os->lacing_fill;i++)
michael@0 846 os->body_fill-=os->lacing_vals[i]&0xff;
michael@0 847 os->lacing_fill=os->lacing_packet;
michael@0 848
michael@0 849 /* make a note of dropped data in segment table */
michael@0 850 if(os->pageno!=-1){
michael@0 851 os->lacing_vals[os->lacing_fill++]=0x400;
michael@0 852 os->lacing_packet++;
michael@0 853 }
michael@0 854 }
michael@0 855
michael@0 856 /* are we a 'continued packet' page? If so, we may need to skip
michael@0 857 some segments */
michael@0 858 if(continued){
michael@0 859 if(os->lacing_fill<1 ||
michael@0 860 os->lacing_vals[os->lacing_fill-1]==0x400){
michael@0 861 bos=0;
michael@0 862 for(;segptr<segments;segptr++){
michael@0 863 int val=header[27+segptr];
michael@0 864 body+=val;
michael@0 865 bodysize-=val;
michael@0 866 if(val<255){
michael@0 867 segptr++;
michael@0 868 break;
michael@0 869 }
michael@0 870 }
michael@0 871 }
michael@0 872 }
michael@0 873
michael@0 874 if(bodysize){
michael@0 875 if(_os_body_expand(os,bodysize)) return -1;
michael@0 876 memcpy(os->body_data+os->body_fill,body,bodysize);
michael@0 877 os->body_fill+=bodysize;
michael@0 878 }
michael@0 879
michael@0 880 {
michael@0 881 int saved=-1;
michael@0 882 while(segptr<segments){
michael@0 883 int val=header[27+segptr];
michael@0 884 os->lacing_vals[os->lacing_fill]=val;
michael@0 885 os->granule_vals[os->lacing_fill]=-1;
michael@0 886
michael@0 887 if(bos){
michael@0 888 os->lacing_vals[os->lacing_fill]|=0x100;
michael@0 889 bos=0;
michael@0 890 }
michael@0 891
michael@0 892 if(val<255)saved=os->lacing_fill;
michael@0 893
michael@0 894 os->lacing_fill++;
michael@0 895 segptr++;
michael@0 896
michael@0 897 if(val<255)os->lacing_packet=os->lacing_fill;
michael@0 898 }
michael@0 899
michael@0 900 /* set the granulepos on the last granuleval of the last full packet */
michael@0 901 if(saved!=-1){
michael@0 902 os->granule_vals[saved]=granulepos;
michael@0 903 }
michael@0 904
michael@0 905 }
michael@0 906
michael@0 907 if(eos){
michael@0 908 os->e_o_s=1;
michael@0 909 if(os->lacing_fill>0)
michael@0 910 os->lacing_vals[os->lacing_fill-1]|=0x200;
michael@0 911 }
michael@0 912
michael@0 913 os->pageno=pageno+1;
michael@0 914
michael@0 915 return(0);
michael@0 916 }
michael@0 917
michael@0 918 /* clear things to an initial state. Good to call, eg, before seeking */
michael@0 919 int ogg_sync_reset(ogg_sync_state *oy){
michael@0 920 if(ogg_sync_check(oy))return -1;
michael@0 921
michael@0 922 oy->fill=0;
michael@0 923 oy->returned=0;
michael@0 924 oy->unsynced=0;
michael@0 925 oy->headerbytes=0;
michael@0 926 oy->bodybytes=0;
michael@0 927 return(0);
michael@0 928 }
michael@0 929
michael@0 930 int ogg_stream_reset(ogg_stream_state *os){
michael@0 931 if(ogg_stream_check(os)) return -1;
michael@0 932
michael@0 933 os->body_fill=0;
michael@0 934 os->body_returned=0;
michael@0 935
michael@0 936 os->lacing_fill=0;
michael@0 937 os->lacing_packet=0;
michael@0 938 os->lacing_returned=0;
michael@0 939
michael@0 940 os->header_fill=0;
michael@0 941
michael@0 942 os->e_o_s=0;
michael@0 943 os->b_o_s=0;
michael@0 944 os->pageno=-1;
michael@0 945 os->packetno=0;
michael@0 946 os->granulepos=0;
michael@0 947
michael@0 948 return(0);
michael@0 949 }
michael@0 950
michael@0 951 int ogg_stream_reset_serialno(ogg_stream_state *os,int serialno){
michael@0 952 if(ogg_stream_check(os)) return -1;
michael@0 953 ogg_stream_reset(os);
michael@0 954 os->serialno=serialno;
michael@0 955 return(0);
michael@0 956 }
michael@0 957
michael@0 958 static int _packetout(ogg_stream_state *os,ogg_packet *op,int adv){
michael@0 959
michael@0 960 /* The last part of decode. We have the stream broken into packet
michael@0 961 segments. Now we need to group them into packets (or return the
michael@0 962 out of sync markers) */
michael@0 963
michael@0 964 int ptr=os->lacing_returned;
michael@0 965
michael@0 966 if(os->lacing_packet<=ptr)return(0);
michael@0 967
michael@0 968 if(os->lacing_vals[ptr]&0x400){
michael@0 969 /* we need to tell the codec there's a gap; it might need to
michael@0 970 handle previous packet dependencies. */
michael@0 971 os->lacing_returned++;
michael@0 972 os->packetno++;
michael@0 973 return(-1);
michael@0 974 }
michael@0 975
michael@0 976 if(!op && !adv)return(1); /* just using peek as an inexpensive way
michael@0 977 to ask if there's a whole packet
michael@0 978 waiting */
michael@0 979
michael@0 980 /* Gather the whole packet. We'll have no holes or a partial packet */
michael@0 981 {
michael@0 982 int size=os->lacing_vals[ptr]&0xff;
michael@0 983 long bytes=size;
michael@0 984 int eos=os->lacing_vals[ptr]&0x200; /* last packet of the stream? */
michael@0 985 int bos=os->lacing_vals[ptr]&0x100; /* first packet of the stream? */
michael@0 986
michael@0 987 while(size==255){
michael@0 988 int val=os->lacing_vals[++ptr];
michael@0 989 size=val&0xff;
michael@0 990 if(val&0x200)eos=0x200;
michael@0 991 bytes+=size;
michael@0 992 }
michael@0 993
michael@0 994 if(op){
michael@0 995 op->e_o_s=eos;
michael@0 996 op->b_o_s=bos;
michael@0 997 op->packet=os->body_data+os->body_returned;
michael@0 998 op->packetno=os->packetno;
michael@0 999 op->granulepos=os->granule_vals[ptr];
michael@0 1000 op->bytes=bytes;
michael@0 1001 }
michael@0 1002
michael@0 1003 if(adv){
michael@0 1004 os->body_returned+=bytes;
michael@0 1005 os->lacing_returned=ptr+1;
michael@0 1006 os->packetno++;
michael@0 1007 }
michael@0 1008 }
michael@0 1009 return(1);
michael@0 1010 }
michael@0 1011
michael@0 1012 int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op){
michael@0 1013 if(ogg_stream_check(os)) return 0;
michael@0 1014 return _packetout(os,op,1);
michael@0 1015 }
michael@0 1016
michael@0 1017 int ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op){
michael@0 1018 if(ogg_stream_check(os)) return 0;
michael@0 1019 return _packetout(os,op,0);
michael@0 1020 }
michael@0 1021
michael@0 1022 void ogg_packet_clear(ogg_packet *op) {
michael@0 1023 _ogg_free(op->packet);
michael@0 1024 memset(op, 0, sizeof(*op));
michael@0 1025 }
michael@0 1026
michael@0 1027 #ifdef _V_SELFTEST
michael@0 1028 #include <stdio.h>
michael@0 1029
michael@0 1030 ogg_stream_state os_en, os_de;
michael@0 1031 ogg_sync_state oy;
michael@0 1032
michael@0 1033 void checkpacket(ogg_packet *op,long len, int no, long pos){
michael@0 1034 long j;
michael@0 1035 static int sequence=0;
michael@0 1036 static int lastno=0;
michael@0 1037
michael@0 1038 if(op->bytes!=len){
michael@0 1039 fprintf(stderr,"incorrect packet length (%ld != %ld)!\n",op->bytes,len);
michael@0 1040 exit(1);
michael@0 1041 }
michael@0 1042 if(op->granulepos!=pos){
michael@0 1043 fprintf(stderr,"incorrect packet granpos (%ld != %ld)!\n",(long)op->granulepos,pos);
michael@0 1044 exit(1);
michael@0 1045 }
michael@0 1046
michael@0 1047 /* packet number just follows sequence/gap; adjust the input number
michael@0 1048 for that */
michael@0 1049 if(no==0){
michael@0 1050 sequence=0;
michael@0 1051 }else{
michael@0 1052 sequence++;
michael@0 1053 if(no>lastno+1)
michael@0 1054 sequence++;
michael@0 1055 }
michael@0 1056 lastno=no;
michael@0 1057 if(op->packetno!=sequence){
michael@0 1058 fprintf(stderr,"incorrect packet sequence %ld != %d\n",
michael@0 1059 (long)(op->packetno),sequence);
michael@0 1060 exit(1);
michael@0 1061 }
michael@0 1062
michael@0 1063 /* Test data */
michael@0 1064 for(j=0;j<op->bytes;j++)
michael@0 1065 if(op->packet[j]!=((j+no)&0xff)){
michael@0 1066 fprintf(stderr,"body data mismatch (1) at pos %ld: %x!=%lx!\n\n",
michael@0 1067 j,op->packet[j],(j+no)&0xff);
michael@0 1068 exit(1);
michael@0 1069 }
michael@0 1070 }
michael@0 1071
michael@0 1072 void check_page(unsigned char *data,const int *header,ogg_page *og){
michael@0 1073 long j;
michael@0 1074 /* Test data */
michael@0 1075 for(j=0;j<og->body_len;j++)
michael@0 1076 if(og->body[j]!=data[j]){
michael@0 1077 fprintf(stderr,"body data mismatch (2) at pos %ld: %x!=%x!\n\n",
michael@0 1078 j,data[j],og->body[j]);
michael@0 1079 exit(1);
michael@0 1080 }
michael@0 1081
michael@0 1082 /* Test header */
michael@0 1083 for(j=0;j<og->header_len;j++){
michael@0 1084 if(og->header[j]!=header[j]){
michael@0 1085 fprintf(stderr,"header content mismatch at pos %ld:\n",j);
michael@0 1086 for(j=0;j<header[26]+27;j++)
michael@0 1087 fprintf(stderr," (%ld)%02x:%02x",j,header[j],og->header[j]);
michael@0 1088 fprintf(stderr,"\n");
michael@0 1089 exit(1);
michael@0 1090 }
michael@0 1091 }
michael@0 1092 if(og->header_len!=header[26]+27){
michael@0 1093 fprintf(stderr,"header length incorrect! (%ld!=%d)\n",
michael@0 1094 og->header_len,header[26]+27);
michael@0 1095 exit(1);
michael@0 1096 }
michael@0 1097 }
michael@0 1098
michael@0 1099 void print_header(ogg_page *og){
michael@0 1100 int j;
michael@0 1101 fprintf(stderr,"\nHEADER:\n");
michael@0 1102 fprintf(stderr," capture: %c %c %c %c version: %d flags: %x\n",
michael@0 1103 og->header[0],og->header[1],og->header[2],og->header[3],
michael@0 1104 (int)og->header[4],(int)og->header[5]);
michael@0 1105
michael@0 1106 fprintf(stderr," granulepos: %d serialno: %d pageno: %ld\n",
michael@0 1107 (og->header[9]<<24)|(og->header[8]<<16)|
michael@0 1108 (og->header[7]<<8)|og->header[6],
michael@0 1109 (og->header[17]<<24)|(og->header[16]<<16)|
michael@0 1110 (og->header[15]<<8)|og->header[14],
michael@0 1111 ((long)(og->header[21])<<24)|(og->header[20]<<16)|
michael@0 1112 (og->header[19]<<8)|og->header[18]);
michael@0 1113
michael@0 1114 fprintf(stderr," checksum: %02x:%02x:%02x:%02x\n segments: %d (",
michael@0 1115 (int)og->header[22],(int)og->header[23],
michael@0 1116 (int)og->header[24],(int)og->header[25],
michael@0 1117 (int)og->header[26]);
michael@0 1118
michael@0 1119 for(j=27;j<og->header_len;j++)
michael@0 1120 fprintf(stderr,"%d ",(int)og->header[j]);
michael@0 1121 fprintf(stderr,")\n\n");
michael@0 1122 }
michael@0 1123
michael@0 1124 void copy_page(ogg_page *og){
michael@0 1125 unsigned char *temp=_ogg_malloc(og->header_len);
michael@0 1126 memcpy(temp,og->header,og->header_len);
michael@0 1127 og->header=temp;
michael@0 1128
michael@0 1129 temp=_ogg_malloc(og->body_len);
michael@0 1130 memcpy(temp,og->body,og->body_len);
michael@0 1131 og->body=temp;
michael@0 1132 }
michael@0 1133
michael@0 1134 void free_page(ogg_page *og){
michael@0 1135 _ogg_free (og->header);
michael@0 1136 _ogg_free (og->body);
michael@0 1137 }
michael@0 1138
michael@0 1139 void error(void){
michael@0 1140 fprintf(stderr,"error!\n");
michael@0 1141 exit(1);
michael@0 1142 }
michael@0 1143
michael@0 1144 /* 17 only */
michael@0 1145 const int head1_0[] = {0x4f,0x67,0x67,0x53,0,0x06,
michael@0 1146 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1147 0x01,0x02,0x03,0x04,0,0,0,0,
michael@0 1148 0x15,0xed,0xec,0x91,
michael@0 1149 1,
michael@0 1150 17};
michael@0 1151
michael@0 1152 /* 17, 254, 255, 256, 500, 510, 600 byte, pad */
michael@0 1153 const int head1_1[] = {0x4f,0x67,0x67,0x53,0,0x02,
michael@0 1154 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1155 0x01,0x02,0x03,0x04,0,0,0,0,
michael@0 1156 0x59,0x10,0x6c,0x2c,
michael@0 1157 1,
michael@0 1158 17};
michael@0 1159 const int head2_1[] = {0x4f,0x67,0x67,0x53,0,0x04,
michael@0 1160 0x07,0x18,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1161 0x01,0x02,0x03,0x04,1,0,0,0,
michael@0 1162 0x89,0x33,0x85,0xce,
michael@0 1163 13,
michael@0 1164 254,255,0,255,1,255,245,255,255,0,
michael@0 1165 255,255,90};
michael@0 1166
michael@0 1167 /* nil packets; beginning,middle,end */
michael@0 1168 const int head1_2[] = {0x4f,0x67,0x67,0x53,0,0x02,
michael@0 1169 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1170 0x01,0x02,0x03,0x04,0,0,0,0,
michael@0 1171 0xff,0x7b,0x23,0x17,
michael@0 1172 1,
michael@0 1173 0};
michael@0 1174 const int head2_2[] = {0x4f,0x67,0x67,0x53,0,0x04,
michael@0 1175 0x07,0x28,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1176 0x01,0x02,0x03,0x04,1,0,0,0,
michael@0 1177 0x5c,0x3f,0x66,0xcb,
michael@0 1178 17,
michael@0 1179 17,254,255,0,0,255,1,0,255,245,255,255,0,
michael@0 1180 255,255,90,0};
michael@0 1181
michael@0 1182 /* large initial packet */
michael@0 1183 const int head1_3[] = {0x4f,0x67,0x67,0x53,0,0x02,
michael@0 1184 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1185 0x01,0x02,0x03,0x04,0,0,0,0,
michael@0 1186 0x01,0x27,0x31,0xaa,
michael@0 1187 18,
michael@0 1188 255,255,255,255,255,255,255,255,
michael@0 1189 255,255,255,255,255,255,255,255,255,10};
michael@0 1190
michael@0 1191 const int head2_3[] = {0x4f,0x67,0x67,0x53,0,0x04,
michael@0 1192 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1193 0x01,0x02,0x03,0x04,1,0,0,0,
michael@0 1194 0x7f,0x4e,0x8a,0xd2,
michael@0 1195 4,
michael@0 1196 255,4,255,0};
michael@0 1197
michael@0 1198
michael@0 1199 /* continuing packet test */
michael@0 1200 const int head1_4[] = {0x4f,0x67,0x67,0x53,0,0x02,
michael@0 1201 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1202 0x01,0x02,0x03,0x04,0,0,0,0,
michael@0 1203 0xff,0x7b,0x23,0x17,
michael@0 1204 1,
michael@0 1205 0};
michael@0 1206
michael@0 1207 const int head2_4[] = {0x4f,0x67,0x67,0x53,0,0x00,
michael@0 1208 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
michael@0 1209 0x01,0x02,0x03,0x04,1,0,0,0,
michael@0 1210 0xf8,0x3c,0x19,0x79,
michael@0 1211 255,
michael@0 1212 255,255,255,255,255,255,255,255,
michael@0 1213 255,255,255,255,255,255,255,255,
michael@0 1214 255,255,255,255,255,255,255,255,
michael@0 1215 255,255,255,255,255,255,255,255,
michael@0 1216 255,255,255,255,255,255,255,255,
michael@0 1217 255,255,255,255,255,255,255,255,
michael@0 1218 255,255,255,255,255,255,255,255,
michael@0 1219 255,255,255,255,255,255,255,255,
michael@0 1220 255,255,255,255,255,255,255,255,
michael@0 1221 255,255,255,255,255,255,255,255,
michael@0 1222 255,255,255,255,255,255,255,255,
michael@0 1223 255,255,255,255,255,255,255,255,
michael@0 1224 255,255,255,255,255,255,255,255,
michael@0 1225 255,255,255,255,255,255,255,255,
michael@0 1226 255,255,255,255,255,255,255,255,
michael@0 1227 255,255,255,255,255,255,255,255,
michael@0 1228 255,255,255,255,255,255,255,255,
michael@0 1229 255,255,255,255,255,255,255,255,
michael@0 1230 255,255,255,255,255,255,255,255,
michael@0 1231 255,255,255,255,255,255,255,255,
michael@0 1232 255,255,255,255,255,255,255,255,
michael@0 1233 255,255,255,255,255,255,255,255,
michael@0 1234 255,255,255,255,255,255,255,255,
michael@0 1235 255,255,255,255,255,255,255,255,
michael@0 1236 255,255,255,255,255,255,255,255,
michael@0 1237 255,255,255,255,255,255,255,255,
michael@0 1238 255,255,255,255,255,255,255,255,
michael@0 1239 255,255,255,255,255,255,255,255,
michael@0 1240 255,255,255,255,255,255,255,255,
michael@0 1241 255,255,255,255,255,255,255,255,
michael@0 1242 255,255,255,255,255,255,255,255,
michael@0 1243 255,255,255,255,255,255,255};
michael@0 1244
michael@0 1245 const int head3_4[] = {0x4f,0x67,0x67,0x53,0,0x05,
michael@0 1246 0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1247 0x01,0x02,0x03,0x04,2,0,0,0,
michael@0 1248 0x38,0xe6,0xb6,0x28,
michael@0 1249 6,
michael@0 1250 255,220,255,4,255,0};
michael@0 1251
michael@0 1252
michael@0 1253 /* spill expansion test */
michael@0 1254 const int head1_4b[] = {0x4f,0x67,0x67,0x53,0,0x02,
michael@0 1255 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1256 0x01,0x02,0x03,0x04,0,0,0,0,
michael@0 1257 0xff,0x7b,0x23,0x17,
michael@0 1258 1,
michael@0 1259 0};
michael@0 1260
michael@0 1261 const int head2_4b[] = {0x4f,0x67,0x67,0x53,0,0x00,
michael@0 1262 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1263 0x01,0x02,0x03,0x04,1,0,0,0,
michael@0 1264 0xce,0x8f,0x17,0x1a,
michael@0 1265 23,
michael@0 1266 255,255,255,255,255,255,255,255,
michael@0 1267 255,255,255,255,255,255,255,255,255,10,255,4,255,0,0};
michael@0 1268
michael@0 1269
michael@0 1270 const int head3_4b[] = {0x4f,0x67,0x67,0x53,0,0x04,
michael@0 1271 0x07,0x14,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1272 0x01,0x02,0x03,0x04,2,0,0,0,
michael@0 1273 0x9b,0xb2,0x50,0xa1,
michael@0 1274 1,
michael@0 1275 0};
michael@0 1276
michael@0 1277 /* page with the 255 segment limit */
michael@0 1278 const int head1_5[] = {0x4f,0x67,0x67,0x53,0,0x02,
michael@0 1279 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1280 0x01,0x02,0x03,0x04,0,0,0,0,
michael@0 1281 0xff,0x7b,0x23,0x17,
michael@0 1282 1,
michael@0 1283 0};
michael@0 1284
michael@0 1285 const int head2_5[] = {0x4f,0x67,0x67,0x53,0,0x00,
michael@0 1286 0x07,0xfc,0x03,0x00,0x00,0x00,0x00,0x00,
michael@0 1287 0x01,0x02,0x03,0x04,1,0,0,0,
michael@0 1288 0xed,0x2a,0x2e,0xa7,
michael@0 1289 255,
michael@0 1290 10,10,10,10,10,10,10,10,
michael@0 1291 10,10,10,10,10,10,10,10,
michael@0 1292 10,10,10,10,10,10,10,10,
michael@0 1293 10,10,10,10,10,10,10,10,
michael@0 1294 10,10,10,10,10,10,10,10,
michael@0 1295 10,10,10,10,10,10,10,10,
michael@0 1296 10,10,10,10,10,10,10,10,
michael@0 1297 10,10,10,10,10,10,10,10,
michael@0 1298 10,10,10,10,10,10,10,10,
michael@0 1299 10,10,10,10,10,10,10,10,
michael@0 1300 10,10,10,10,10,10,10,10,
michael@0 1301 10,10,10,10,10,10,10,10,
michael@0 1302 10,10,10,10,10,10,10,10,
michael@0 1303 10,10,10,10,10,10,10,10,
michael@0 1304 10,10,10,10,10,10,10,10,
michael@0 1305 10,10,10,10,10,10,10,10,
michael@0 1306 10,10,10,10,10,10,10,10,
michael@0 1307 10,10,10,10,10,10,10,10,
michael@0 1308 10,10,10,10,10,10,10,10,
michael@0 1309 10,10,10,10,10,10,10,10,
michael@0 1310 10,10,10,10,10,10,10,10,
michael@0 1311 10,10,10,10,10,10,10,10,
michael@0 1312 10,10,10,10,10,10,10,10,
michael@0 1313 10,10,10,10,10,10,10,10,
michael@0 1314 10,10,10,10,10,10,10,10,
michael@0 1315 10,10,10,10,10,10,10,10,
michael@0 1316 10,10,10,10,10,10,10,10,
michael@0 1317 10,10,10,10,10,10,10,10,
michael@0 1318 10,10,10,10,10,10,10,10,
michael@0 1319 10,10,10,10,10,10,10,10,
michael@0 1320 10,10,10,10,10,10,10,10,
michael@0 1321 10,10,10,10,10,10,10};
michael@0 1322
michael@0 1323 const int head3_5[] = {0x4f,0x67,0x67,0x53,0,0x04,
michael@0 1324 0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00,
michael@0 1325 0x01,0x02,0x03,0x04,2,0,0,0,
michael@0 1326 0x6c,0x3b,0x82,0x3d,
michael@0 1327 1,
michael@0 1328 50};
michael@0 1329
michael@0 1330
michael@0 1331 /* packet that overspans over an entire page */
michael@0 1332 const int head1_6[] = {0x4f,0x67,0x67,0x53,0,0x02,
michael@0 1333 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1334 0x01,0x02,0x03,0x04,0,0,0,0,
michael@0 1335 0xff,0x7b,0x23,0x17,
michael@0 1336 1,
michael@0 1337 0};
michael@0 1338
michael@0 1339 const int head2_6[] = {0x4f,0x67,0x67,0x53,0,0x00,
michael@0 1340 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1341 0x01,0x02,0x03,0x04,1,0,0,0,
michael@0 1342 0x68,0x22,0x7c,0x3d,
michael@0 1343 255,
michael@0 1344 100,
michael@0 1345 255,255,255,255,255,255,255,255,
michael@0 1346 255,255,255,255,255,255,255,255,
michael@0 1347 255,255,255,255,255,255,255,255,
michael@0 1348 255,255,255,255,255,255,255,255,
michael@0 1349 255,255,255,255,255,255,255,255,
michael@0 1350 255,255,255,255,255,255,255,255,
michael@0 1351 255,255,255,255,255,255,255,255,
michael@0 1352 255,255,255,255,255,255,255,255,
michael@0 1353 255,255,255,255,255,255,255,255,
michael@0 1354 255,255,255,255,255,255,255,255,
michael@0 1355 255,255,255,255,255,255,255,255,
michael@0 1356 255,255,255,255,255,255,255,255,
michael@0 1357 255,255,255,255,255,255,255,255,
michael@0 1358 255,255,255,255,255,255,255,255,
michael@0 1359 255,255,255,255,255,255,255,255,
michael@0 1360 255,255,255,255,255,255,255,255,
michael@0 1361 255,255,255,255,255,255,255,255,
michael@0 1362 255,255,255,255,255,255,255,255,
michael@0 1363 255,255,255,255,255,255,255,255,
michael@0 1364 255,255,255,255,255,255,255,255,
michael@0 1365 255,255,255,255,255,255,255,255,
michael@0 1366 255,255,255,255,255,255,255,255,
michael@0 1367 255,255,255,255,255,255,255,255,
michael@0 1368 255,255,255,255,255,255,255,255,
michael@0 1369 255,255,255,255,255,255,255,255,
michael@0 1370 255,255,255,255,255,255,255,255,
michael@0 1371 255,255,255,255,255,255,255,255,
michael@0 1372 255,255,255,255,255,255,255,255,
michael@0 1373 255,255,255,255,255,255,255,255,
michael@0 1374 255,255,255,255,255,255,255,255,
michael@0 1375 255,255,255,255,255,255,255,255,
michael@0 1376 255,255,255,255,255,255};
michael@0 1377
michael@0 1378 const int head3_6[] = {0x4f,0x67,0x67,0x53,0,0x01,
michael@0 1379 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
michael@0 1380 0x01,0x02,0x03,0x04,2,0,0,0,
michael@0 1381 0xf4,0x87,0xba,0xf3,
michael@0 1382 255,
michael@0 1383 255,255,255,255,255,255,255,255,
michael@0 1384 255,255,255,255,255,255,255,255,
michael@0 1385 255,255,255,255,255,255,255,255,
michael@0 1386 255,255,255,255,255,255,255,255,
michael@0 1387 255,255,255,255,255,255,255,255,
michael@0 1388 255,255,255,255,255,255,255,255,
michael@0 1389 255,255,255,255,255,255,255,255,
michael@0 1390 255,255,255,255,255,255,255,255,
michael@0 1391 255,255,255,255,255,255,255,255,
michael@0 1392 255,255,255,255,255,255,255,255,
michael@0 1393 255,255,255,255,255,255,255,255,
michael@0 1394 255,255,255,255,255,255,255,255,
michael@0 1395 255,255,255,255,255,255,255,255,
michael@0 1396 255,255,255,255,255,255,255,255,
michael@0 1397 255,255,255,255,255,255,255,255,
michael@0 1398 255,255,255,255,255,255,255,255,
michael@0 1399 255,255,255,255,255,255,255,255,
michael@0 1400 255,255,255,255,255,255,255,255,
michael@0 1401 255,255,255,255,255,255,255,255,
michael@0 1402 255,255,255,255,255,255,255,255,
michael@0 1403 255,255,255,255,255,255,255,255,
michael@0 1404 255,255,255,255,255,255,255,255,
michael@0 1405 255,255,255,255,255,255,255,255,
michael@0 1406 255,255,255,255,255,255,255,255,
michael@0 1407 255,255,255,255,255,255,255,255,
michael@0 1408 255,255,255,255,255,255,255,255,
michael@0 1409 255,255,255,255,255,255,255,255,
michael@0 1410 255,255,255,255,255,255,255,255,
michael@0 1411 255,255,255,255,255,255,255,255,
michael@0 1412 255,255,255,255,255,255,255,255,
michael@0 1413 255,255,255,255,255,255,255,255,
michael@0 1414 255,255,255,255,255,255,255};
michael@0 1415
michael@0 1416 const int head4_6[] = {0x4f,0x67,0x67,0x53,0,0x05,
michael@0 1417 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1418 0x01,0x02,0x03,0x04,3,0,0,0,
michael@0 1419 0xf7,0x2f,0x6c,0x60,
michael@0 1420 5,
michael@0 1421 254,255,4,255,0};
michael@0 1422
michael@0 1423 /* packet that overspans over an entire page */
michael@0 1424 const int head1_7[] = {0x4f,0x67,0x67,0x53,0,0x02,
michael@0 1425 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1426 0x01,0x02,0x03,0x04,0,0,0,0,
michael@0 1427 0xff,0x7b,0x23,0x17,
michael@0 1428 1,
michael@0 1429 0};
michael@0 1430
michael@0 1431 const int head2_7[] = {0x4f,0x67,0x67,0x53,0,0x00,
michael@0 1432 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1433 0x01,0x02,0x03,0x04,1,0,0,0,
michael@0 1434 0x68,0x22,0x7c,0x3d,
michael@0 1435 255,
michael@0 1436 100,
michael@0 1437 255,255,255,255,255,255,255,255,
michael@0 1438 255,255,255,255,255,255,255,255,
michael@0 1439 255,255,255,255,255,255,255,255,
michael@0 1440 255,255,255,255,255,255,255,255,
michael@0 1441 255,255,255,255,255,255,255,255,
michael@0 1442 255,255,255,255,255,255,255,255,
michael@0 1443 255,255,255,255,255,255,255,255,
michael@0 1444 255,255,255,255,255,255,255,255,
michael@0 1445 255,255,255,255,255,255,255,255,
michael@0 1446 255,255,255,255,255,255,255,255,
michael@0 1447 255,255,255,255,255,255,255,255,
michael@0 1448 255,255,255,255,255,255,255,255,
michael@0 1449 255,255,255,255,255,255,255,255,
michael@0 1450 255,255,255,255,255,255,255,255,
michael@0 1451 255,255,255,255,255,255,255,255,
michael@0 1452 255,255,255,255,255,255,255,255,
michael@0 1453 255,255,255,255,255,255,255,255,
michael@0 1454 255,255,255,255,255,255,255,255,
michael@0 1455 255,255,255,255,255,255,255,255,
michael@0 1456 255,255,255,255,255,255,255,255,
michael@0 1457 255,255,255,255,255,255,255,255,
michael@0 1458 255,255,255,255,255,255,255,255,
michael@0 1459 255,255,255,255,255,255,255,255,
michael@0 1460 255,255,255,255,255,255,255,255,
michael@0 1461 255,255,255,255,255,255,255,255,
michael@0 1462 255,255,255,255,255,255,255,255,
michael@0 1463 255,255,255,255,255,255,255,255,
michael@0 1464 255,255,255,255,255,255,255,255,
michael@0 1465 255,255,255,255,255,255,255,255,
michael@0 1466 255,255,255,255,255,255,255,255,
michael@0 1467 255,255,255,255,255,255,255,255,
michael@0 1468 255,255,255,255,255,255};
michael@0 1469
michael@0 1470 const int head3_7[] = {0x4f,0x67,0x67,0x53,0,0x05,
michael@0 1471 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
michael@0 1472 0x01,0x02,0x03,0x04,2,0,0,0,
michael@0 1473 0xd4,0xe0,0x60,0xe5,
michael@0 1474 1,
michael@0 1475 0};
michael@0 1476
michael@0 1477 void test_pack(const int *pl, const int **headers, int byteskip,
michael@0 1478 int pageskip, int packetskip){
michael@0 1479 unsigned char *data=_ogg_malloc(1024*1024); /* for scripted test cases only */
michael@0 1480 long inptr=0;
michael@0 1481 long outptr=0;
michael@0 1482 long deptr=0;
michael@0 1483 long depacket=0;
michael@0 1484 long granule_pos=7,pageno=0;
michael@0 1485 int i,j,packets,pageout=pageskip;
michael@0 1486 int eosflag=0;
michael@0 1487 int bosflag=0;
michael@0 1488
michael@0 1489 int byteskipcount=0;
michael@0 1490
michael@0 1491 ogg_stream_reset(&os_en);
michael@0 1492 ogg_stream_reset(&os_de);
michael@0 1493 ogg_sync_reset(&oy);
michael@0 1494
michael@0 1495 for(packets=0;packets<packetskip;packets++)
michael@0 1496 depacket+=pl[packets];
michael@0 1497
michael@0 1498 for(packets=0;;packets++)if(pl[packets]==-1)break;
michael@0 1499
michael@0 1500 for(i=0;i<packets;i++){
michael@0 1501 /* construct a test packet */
michael@0 1502 ogg_packet op;
michael@0 1503 int len=pl[i];
michael@0 1504
michael@0 1505 op.packet=data+inptr;
michael@0 1506 op.bytes=len;
michael@0 1507 op.e_o_s=(pl[i+1]<0?1:0);
michael@0 1508 op.granulepos=granule_pos;
michael@0 1509
michael@0 1510 granule_pos+=1024;
michael@0 1511
michael@0 1512 for(j=0;j<len;j++)data[inptr++]=i+j;
michael@0 1513
michael@0 1514 /* submit the test packet */
michael@0 1515 ogg_stream_packetin(&os_en,&op);
michael@0 1516
michael@0 1517 /* retrieve any finished pages */
michael@0 1518 {
michael@0 1519 ogg_page og;
michael@0 1520
michael@0 1521 while(ogg_stream_pageout(&os_en,&og)){
michael@0 1522 /* We have a page. Check it carefully */
michael@0 1523
michael@0 1524 fprintf(stderr,"%ld, ",pageno);
michael@0 1525
michael@0 1526 if(headers[pageno]==NULL){
michael@0 1527 fprintf(stderr,"coded too many pages!\n");
michael@0 1528 exit(1);
michael@0 1529 }
michael@0 1530
michael@0 1531 check_page(data+outptr,headers[pageno],&og);
michael@0 1532
michael@0 1533 outptr+=og.body_len;
michael@0 1534 pageno++;
michael@0 1535 if(pageskip){
michael@0 1536 bosflag=1;
michael@0 1537 pageskip--;
michael@0 1538 deptr+=og.body_len;
michael@0 1539 }
michael@0 1540
michael@0 1541 /* have a complete page; submit it to sync/decode */
michael@0 1542
michael@0 1543 {
michael@0 1544 ogg_page og_de;
michael@0 1545 ogg_packet op_de,op_de2;
michael@0 1546 char *buf=ogg_sync_buffer(&oy,og.header_len+og.body_len);
michael@0 1547 char *next=buf;
michael@0 1548 byteskipcount+=og.header_len;
michael@0 1549 if(byteskipcount>byteskip){
michael@0 1550 memcpy(next,og.header,byteskipcount-byteskip);
michael@0 1551 next+=byteskipcount-byteskip;
michael@0 1552 byteskipcount=byteskip;
michael@0 1553 }
michael@0 1554
michael@0 1555 byteskipcount+=og.body_len;
michael@0 1556 if(byteskipcount>byteskip){
michael@0 1557 memcpy(next,og.body,byteskipcount-byteskip);
michael@0 1558 next+=byteskipcount-byteskip;
michael@0 1559 byteskipcount=byteskip;
michael@0 1560 }
michael@0 1561
michael@0 1562 ogg_sync_wrote(&oy,next-buf);
michael@0 1563
michael@0 1564 while(1){
michael@0 1565 int ret=ogg_sync_pageout(&oy,&og_de);
michael@0 1566 if(ret==0)break;
michael@0 1567 if(ret<0)continue;
michael@0 1568 /* got a page. Happy happy. Verify that it's good. */
michael@0 1569
michael@0 1570 fprintf(stderr,"(%d), ",pageout);
michael@0 1571
michael@0 1572 check_page(data+deptr,headers[pageout],&og_de);
michael@0 1573 deptr+=og_de.body_len;
michael@0 1574 pageout++;
michael@0 1575
michael@0 1576 /* submit it to deconstitution */
michael@0 1577 ogg_stream_pagein(&os_de,&og_de);
michael@0 1578
michael@0 1579 /* packets out? */
michael@0 1580 while(ogg_stream_packetpeek(&os_de,&op_de2)>0){
michael@0 1581 ogg_stream_packetpeek(&os_de,NULL);
michael@0 1582 ogg_stream_packetout(&os_de,&op_de); /* just catching them all */
michael@0 1583
michael@0 1584 /* verify peek and out match */
michael@0 1585 if(memcmp(&op_de,&op_de2,sizeof(op_de))){
michael@0 1586 fprintf(stderr,"packetout != packetpeek! pos=%ld\n",
michael@0 1587 depacket);
michael@0 1588 exit(1);
michael@0 1589 }
michael@0 1590
michael@0 1591 /* verify the packet! */
michael@0 1592 /* check data */
michael@0 1593 if(memcmp(data+depacket,op_de.packet,op_de.bytes)){
michael@0 1594 fprintf(stderr,"packet data mismatch in decode! pos=%ld\n",
michael@0 1595 depacket);
michael@0 1596 exit(1);
michael@0 1597 }
michael@0 1598 /* check bos flag */
michael@0 1599 if(bosflag==0 && op_de.b_o_s==0){
michael@0 1600 fprintf(stderr,"b_o_s flag not set on packet!\n");
michael@0 1601 exit(1);
michael@0 1602 }
michael@0 1603 if(bosflag && op_de.b_o_s){
michael@0 1604 fprintf(stderr,"b_o_s flag incorrectly set on packet!\n");
michael@0 1605 exit(1);
michael@0 1606 }
michael@0 1607 bosflag=1;
michael@0 1608 depacket+=op_de.bytes;
michael@0 1609
michael@0 1610 /* check eos flag */
michael@0 1611 if(eosflag){
michael@0 1612 fprintf(stderr,"Multiple decoded packets with eos flag!\n");
michael@0 1613 exit(1);
michael@0 1614 }
michael@0 1615
michael@0 1616 if(op_de.e_o_s)eosflag=1;
michael@0 1617
michael@0 1618 /* check granulepos flag */
michael@0 1619 if(op_de.granulepos!=-1){
michael@0 1620 fprintf(stderr," granule:%ld ",(long)op_de.granulepos);
michael@0 1621 }
michael@0 1622 }
michael@0 1623 }
michael@0 1624 }
michael@0 1625 }
michael@0 1626 }
michael@0 1627 }
michael@0 1628 _ogg_free(data);
michael@0 1629 if(headers[pageno]!=NULL){
michael@0 1630 fprintf(stderr,"did not write last page!\n");
michael@0 1631 exit(1);
michael@0 1632 }
michael@0 1633 if(headers[pageout]!=NULL){
michael@0 1634 fprintf(stderr,"did not decode last page!\n");
michael@0 1635 exit(1);
michael@0 1636 }
michael@0 1637 if(inptr!=outptr){
michael@0 1638 fprintf(stderr,"encoded page data incomplete!\n");
michael@0 1639 exit(1);
michael@0 1640 }
michael@0 1641 if(inptr!=deptr){
michael@0 1642 fprintf(stderr,"decoded page data incomplete!\n");
michael@0 1643 exit(1);
michael@0 1644 }
michael@0 1645 if(inptr!=depacket){
michael@0 1646 fprintf(stderr,"decoded packet data incomplete!\n");
michael@0 1647 exit(1);
michael@0 1648 }
michael@0 1649 if(!eosflag){
michael@0 1650 fprintf(stderr,"Never got a packet with EOS set!\n");
michael@0 1651 exit(1);
michael@0 1652 }
michael@0 1653 fprintf(stderr,"ok.\n");
michael@0 1654 }
michael@0 1655
michael@0 1656 int main(void){
michael@0 1657
michael@0 1658 ogg_stream_init(&os_en,0x04030201);
michael@0 1659 ogg_stream_init(&os_de,0x04030201);
michael@0 1660 ogg_sync_init(&oy);
michael@0 1661
michael@0 1662 /* Exercise each code path in the framing code. Also verify that
michael@0 1663 the checksums are working. */
michael@0 1664
michael@0 1665 {
michael@0 1666 /* 17 only */
michael@0 1667 const int packets[]={17, -1};
michael@0 1668 const int *headret[]={head1_0,NULL};
michael@0 1669
michael@0 1670 fprintf(stderr,"testing single page encoding... ");
michael@0 1671 test_pack(packets,headret,0,0,0);
michael@0 1672 }
michael@0 1673
michael@0 1674 {
michael@0 1675 /* 17, 254, 255, 256, 500, 510, 600 byte, pad */
michael@0 1676 const int packets[]={17, 254, 255, 256, 500, 510, 600, -1};
michael@0 1677 const int *headret[]={head1_1,head2_1,NULL};
michael@0 1678
michael@0 1679 fprintf(stderr,"testing basic page encoding... ");
michael@0 1680 test_pack(packets,headret,0,0,0);
michael@0 1681 }
michael@0 1682
michael@0 1683 {
michael@0 1684 /* nil packets; beginning,middle,end */
michael@0 1685 const int packets[]={0,17, 254, 255, 0, 256, 0, 500, 510, 600, 0, -1};
michael@0 1686 const int *headret[]={head1_2,head2_2,NULL};
michael@0 1687
michael@0 1688 fprintf(stderr,"testing basic nil packets... ");
michael@0 1689 test_pack(packets,headret,0,0,0);
michael@0 1690 }
michael@0 1691
michael@0 1692 {
michael@0 1693 /* large initial packet */
michael@0 1694 const int packets[]={4345,259,255,-1};
michael@0 1695 const int *headret[]={head1_3,head2_3,NULL};
michael@0 1696
michael@0 1697 fprintf(stderr,"testing initial-packet lacing > 4k... ");
michael@0 1698 test_pack(packets,headret,0,0,0);
michael@0 1699 }
michael@0 1700
michael@0 1701 {
michael@0 1702 /* continuing packet test; with page spill expansion, we have to
michael@0 1703 overflow the lacing table. */
michael@0 1704 const int packets[]={0,65500,259,255,-1};
michael@0 1705 const int *headret[]={head1_4,head2_4,head3_4,NULL};
michael@0 1706
michael@0 1707 fprintf(stderr,"testing single packet page span... ");
michael@0 1708 test_pack(packets,headret,0,0,0);
michael@0 1709 }
michael@0 1710
michael@0 1711 {
michael@0 1712 /* spill expand packet test */
michael@0 1713 const int packets[]={0,4345,259,255,0,0,-1};
michael@0 1714 const int *headret[]={head1_4b,head2_4b,head3_4b,NULL};
michael@0 1715
michael@0 1716 fprintf(stderr,"testing page spill expansion... ");
michael@0 1717 test_pack(packets,headret,0,0,0);
michael@0 1718 }
michael@0 1719
michael@0 1720 /* page with the 255 segment limit */
michael@0 1721 {
michael@0 1722
michael@0 1723 const int packets[]={0,10,10,10,10,10,10,10,10,
michael@0 1724 10,10,10,10,10,10,10,10,
michael@0 1725 10,10,10,10,10,10,10,10,
michael@0 1726 10,10,10,10,10,10,10,10,
michael@0 1727 10,10,10,10,10,10,10,10,
michael@0 1728 10,10,10,10,10,10,10,10,
michael@0 1729 10,10,10,10,10,10,10,10,
michael@0 1730 10,10,10,10,10,10,10,10,
michael@0 1731 10,10,10,10,10,10,10,10,
michael@0 1732 10,10,10,10,10,10,10,10,
michael@0 1733 10,10,10,10,10,10,10,10,
michael@0 1734 10,10,10,10,10,10,10,10,
michael@0 1735 10,10,10,10,10,10,10,10,
michael@0 1736 10,10,10,10,10,10,10,10,
michael@0 1737 10,10,10,10,10,10,10,10,
michael@0 1738 10,10,10,10,10,10,10,10,
michael@0 1739 10,10,10,10,10,10,10,10,
michael@0 1740 10,10,10,10,10,10,10,10,
michael@0 1741 10,10,10,10,10,10,10,10,
michael@0 1742 10,10,10,10,10,10,10,10,
michael@0 1743 10,10,10,10,10,10,10,10,
michael@0 1744 10,10,10,10,10,10,10,10,
michael@0 1745 10,10,10,10,10,10,10,10,
michael@0 1746 10,10,10,10,10,10,10,10,
michael@0 1747 10,10,10,10,10,10,10,10,
michael@0 1748 10,10,10,10,10,10,10,10,
michael@0 1749 10,10,10,10,10,10,10,10,
michael@0 1750 10,10,10,10,10,10,10,10,
michael@0 1751 10,10,10,10,10,10,10,10,
michael@0 1752 10,10,10,10,10,10,10,10,
michael@0 1753 10,10,10,10,10,10,10,10,
michael@0 1754 10,10,10,10,10,10,10,50,-1};
michael@0 1755 const int *headret[]={head1_5,head2_5,head3_5,NULL};
michael@0 1756
michael@0 1757 fprintf(stderr,"testing max packet segments... ");
michael@0 1758 test_pack(packets,headret,0,0,0);
michael@0 1759 }
michael@0 1760
michael@0 1761 {
michael@0 1762 /* packet that overspans over an entire page */
michael@0 1763 const int packets[]={0,100,130049,259,255,-1};
michael@0 1764 const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL};
michael@0 1765
michael@0 1766 fprintf(stderr,"testing very large packets... ");
michael@0 1767 test_pack(packets,headret,0,0,0);
michael@0 1768 }
michael@0 1769
michael@0 1770 {
michael@0 1771 /* test for the libogg 1.1.1 resync in large continuation bug
michael@0 1772 found by Josh Coalson) */
michael@0 1773 const int packets[]={0,100,130049,259,255,-1};
michael@0 1774 const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL};
michael@0 1775
michael@0 1776 fprintf(stderr,"testing continuation resync in very large packets... ");
michael@0 1777 test_pack(packets,headret,100,2,3);
michael@0 1778 }
michael@0 1779
michael@0 1780 {
michael@0 1781 /* term only page. why not? */
michael@0 1782 const int packets[]={0,100,64770,-1};
michael@0 1783 const int *headret[]={head1_7,head2_7,head3_7,NULL};
michael@0 1784
michael@0 1785 fprintf(stderr,"testing zero data page (1 nil packet)... ");
michael@0 1786 test_pack(packets,headret,0,0,0);
michael@0 1787 }
michael@0 1788
michael@0 1789
michael@0 1790
michael@0 1791 {
michael@0 1792 /* build a bunch of pages for testing */
michael@0 1793 unsigned char *data=_ogg_malloc(1024*1024);
michael@0 1794 int pl[]={0, 1,1,98,4079, 1,1,2954,2057, 76,34,912,0,234,1000,1000, 1000,300,-1};
michael@0 1795 int inptr=0,i,j;
michael@0 1796 ogg_page og[5];
michael@0 1797
michael@0 1798 ogg_stream_reset(&os_en);
michael@0 1799
michael@0 1800 for(i=0;pl[i]!=-1;i++){
michael@0 1801 ogg_packet op;
michael@0 1802 int len=pl[i];
michael@0 1803
michael@0 1804 op.packet=data+inptr;
michael@0 1805 op.bytes=len;
michael@0 1806 op.e_o_s=(pl[i+1]<0?1:0);
michael@0 1807 op.granulepos=(i+1)*1000;
michael@0 1808
michael@0 1809 for(j=0;j<len;j++)data[inptr++]=i+j;
michael@0 1810 ogg_stream_packetin(&os_en,&op);
michael@0 1811 }
michael@0 1812
michael@0 1813 _ogg_free(data);
michael@0 1814
michael@0 1815 /* retrieve finished pages */
michael@0 1816 for(i=0;i<5;i++){
michael@0 1817 if(ogg_stream_pageout(&os_en,&og[i])==0){
michael@0 1818 fprintf(stderr,"Too few pages output building sync tests!\n");
michael@0 1819 exit(1);
michael@0 1820 }
michael@0 1821 copy_page(&og[i]);
michael@0 1822 }
michael@0 1823
michael@0 1824 /* Test lost pages on pagein/packetout: no rollback */
michael@0 1825 {
michael@0 1826 ogg_page temp;
michael@0 1827 ogg_packet test;
michael@0 1828
michael@0 1829 fprintf(stderr,"Testing loss of pages... ");
michael@0 1830
michael@0 1831 ogg_sync_reset(&oy);
michael@0 1832 ogg_stream_reset(&os_de);
michael@0 1833 for(i=0;i<5;i++){
michael@0 1834 memcpy(ogg_sync_buffer(&oy,og[i].header_len),og[i].header,
michael@0 1835 og[i].header_len);
michael@0 1836 ogg_sync_wrote(&oy,og[i].header_len);
michael@0 1837 memcpy(ogg_sync_buffer(&oy,og[i].body_len),og[i].body,og[i].body_len);
michael@0 1838 ogg_sync_wrote(&oy,og[i].body_len);
michael@0 1839 }
michael@0 1840
michael@0 1841 ogg_sync_pageout(&oy,&temp);
michael@0 1842 ogg_stream_pagein(&os_de,&temp);
michael@0 1843 ogg_sync_pageout(&oy,&temp);
michael@0 1844 ogg_stream_pagein(&os_de,&temp);
michael@0 1845 ogg_sync_pageout(&oy,&temp);
michael@0 1846 /* skip */
michael@0 1847 ogg_sync_pageout(&oy,&temp);
michael@0 1848 ogg_stream_pagein(&os_de,&temp);
michael@0 1849
michael@0 1850 /* do we get the expected results/packets? */
michael@0 1851
michael@0 1852 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1853 checkpacket(&test,0,0,0);
michael@0 1854 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1855 checkpacket(&test,1,1,-1);
michael@0 1856 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1857 checkpacket(&test,1,2,-1);
michael@0 1858 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1859 checkpacket(&test,98,3,-1);
michael@0 1860 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1861 checkpacket(&test,4079,4,5000);
michael@0 1862 if(ogg_stream_packetout(&os_de,&test)!=-1){
michael@0 1863 fprintf(stderr,"Error: loss of page did not return error\n");
michael@0 1864 exit(1);
michael@0 1865 }
michael@0 1866 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1867 checkpacket(&test,76,9,-1);
michael@0 1868 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1869 checkpacket(&test,34,10,-1);
michael@0 1870 fprintf(stderr,"ok.\n");
michael@0 1871 }
michael@0 1872
michael@0 1873 /* Test lost pages on pagein/packetout: rollback with continuation */
michael@0 1874 {
michael@0 1875 ogg_page temp;
michael@0 1876 ogg_packet test;
michael@0 1877
michael@0 1878 fprintf(stderr,"Testing loss of pages (rollback required)... ");
michael@0 1879
michael@0 1880 ogg_sync_reset(&oy);
michael@0 1881 ogg_stream_reset(&os_de);
michael@0 1882 for(i=0;i<5;i++){
michael@0 1883 memcpy(ogg_sync_buffer(&oy,og[i].header_len),og[i].header,
michael@0 1884 og[i].header_len);
michael@0 1885 ogg_sync_wrote(&oy,og[i].header_len);
michael@0 1886 memcpy(ogg_sync_buffer(&oy,og[i].body_len),og[i].body,og[i].body_len);
michael@0 1887 ogg_sync_wrote(&oy,og[i].body_len);
michael@0 1888 }
michael@0 1889
michael@0 1890 ogg_sync_pageout(&oy,&temp);
michael@0 1891 ogg_stream_pagein(&os_de,&temp);
michael@0 1892 ogg_sync_pageout(&oy,&temp);
michael@0 1893 ogg_stream_pagein(&os_de,&temp);
michael@0 1894 ogg_sync_pageout(&oy,&temp);
michael@0 1895 ogg_stream_pagein(&os_de,&temp);
michael@0 1896 ogg_sync_pageout(&oy,&temp);
michael@0 1897 /* skip */
michael@0 1898 ogg_sync_pageout(&oy,&temp);
michael@0 1899 ogg_stream_pagein(&os_de,&temp);
michael@0 1900
michael@0 1901 /* do we get the expected results/packets? */
michael@0 1902
michael@0 1903 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1904 checkpacket(&test,0,0,0);
michael@0 1905 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1906 checkpacket(&test,1,1,-1);
michael@0 1907 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1908 checkpacket(&test,1,2,-1);
michael@0 1909 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1910 checkpacket(&test,98,3,-1);
michael@0 1911 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1912 checkpacket(&test,4079,4,5000);
michael@0 1913 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1914 checkpacket(&test,1,5,-1);
michael@0 1915 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1916 checkpacket(&test,1,6,-1);
michael@0 1917 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1918 checkpacket(&test,2954,7,-1);
michael@0 1919 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1920 checkpacket(&test,2057,8,9000);
michael@0 1921 if(ogg_stream_packetout(&os_de,&test)!=-1){
michael@0 1922 fprintf(stderr,"Error: loss of page did not return error\n");
michael@0 1923 exit(1);
michael@0 1924 }
michael@0 1925 if(ogg_stream_packetout(&os_de,&test)!=1)error();
michael@0 1926 checkpacket(&test,300,17,18000);
michael@0 1927 fprintf(stderr,"ok.\n");
michael@0 1928 }
michael@0 1929
michael@0 1930 /* the rest only test sync */
michael@0 1931 {
michael@0 1932 ogg_page og_de;
michael@0 1933 /* Test fractional page inputs: incomplete capture */
michael@0 1934 fprintf(stderr,"Testing sync on partial inputs... ");
michael@0 1935 ogg_sync_reset(&oy);
michael@0 1936 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
michael@0 1937 3);
michael@0 1938 ogg_sync_wrote(&oy,3);
michael@0 1939 if(ogg_sync_pageout(&oy,&og_de)>0)error();
michael@0 1940
michael@0 1941 /* Test fractional page inputs: incomplete fixed header */
michael@0 1942 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+3,
michael@0 1943 20);
michael@0 1944 ogg_sync_wrote(&oy,20);
michael@0 1945 if(ogg_sync_pageout(&oy,&og_de)>0)error();
michael@0 1946
michael@0 1947 /* Test fractional page inputs: incomplete header */
michael@0 1948 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+23,
michael@0 1949 5);
michael@0 1950 ogg_sync_wrote(&oy,5);
michael@0 1951 if(ogg_sync_pageout(&oy,&og_de)>0)error();
michael@0 1952
michael@0 1953 /* Test fractional page inputs: incomplete body */
michael@0 1954
michael@0 1955 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+28,
michael@0 1956 og[1].header_len-28);
michael@0 1957 ogg_sync_wrote(&oy,og[1].header_len-28);
michael@0 1958 if(ogg_sync_pageout(&oy,&og_de)>0)error();
michael@0 1959
michael@0 1960 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,1000);
michael@0 1961 ogg_sync_wrote(&oy,1000);
michael@0 1962 if(ogg_sync_pageout(&oy,&og_de)>0)error();
michael@0 1963
michael@0 1964 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body+1000,
michael@0 1965 og[1].body_len-1000);
michael@0 1966 ogg_sync_wrote(&oy,og[1].body_len-1000);
michael@0 1967 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
michael@0 1968
michael@0 1969 fprintf(stderr,"ok.\n");
michael@0 1970 }
michael@0 1971
michael@0 1972 /* Test fractional page inputs: page + incomplete capture */
michael@0 1973 {
michael@0 1974 ogg_page og_de;
michael@0 1975 fprintf(stderr,"Testing sync on 1+partial inputs... ");
michael@0 1976 ogg_sync_reset(&oy);
michael@0 1977
michael@0 1978 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
michael@0 1979 og[1].header_len);
michael@0 1980 ogg_sync_wrote(&oy,og[1].header_len);
michael@0 1981
michael@0 1982 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
michael@0 1983 og[1].body_len);
michael@0 1984 ogg_sync_wrote(&oy,og[1].body_len);
michael@0 1985
michael@0 1986 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
michael@0 1987 20);
michael@0 1988 ogg_sync_wrote(&oy,20);
michael@0 1989 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
michael@0 1990 if(ogg_sync_pageout(&oy,&og_de)>0)error();
michael@0 1991
michael@0 1992 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+20,
michael@0 1993 og[1].header_len-20);
michael@0 1994 ogg_sync_wrote(&oy,og[1].header_len-20);
michael@0 1995 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
michael@0 1996 og[1].body_len);
michael@0 1997 ogg_sync_wrote(&oy,og[1].body_len);
michael@0 1998 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
michael@0 1999
michael@0 2000 fprintf(stderr,"ok.\n");
michael@0 2001 }
michael@0 2002
michael@0 2003 /* Test recapture: garbage + page */
michael@0 2004 {
michael@0 2005 ogg_page og_de;
michael@0 2006 fprintf(stderr,"Testing search for capture... ");
michael@0 2007 ogg_sync_reset(&oy);
michael@0 2008
michael@0 2009 /* 'garbage' */
michael@0 2010 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
michael@0 2011 og[1].body_len);
michael@0 2012 ogg_sync_wrote(&oy,og[1].body_len);
michael@0 2013
michael@0 2014 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
michael@0 2015 og[1].header_len);
michael@0 2016 ogg_sync_wrote(&oy,og[1].header_len);
michael@0 2017
michael@0 2018 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
michael@0 2019 og[1].body_len);
michael@0 2020 ogg_sync_wrote(&oy,og[1].body_len);
michael@0 2021
michael@0 2022 memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
michael@0 2023 20);
michael@0 2024 ogg_sync_wrote(&oy,20);
michael@0 2025 if(ogg_sync_pageout(&oy,&og_de)>0)error();
michael@0 2026 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
michael@0 2027 if(ogg_sync_pageout(&oy,&og_de)>0)error();
michael@0 2028
michael@0 2029 memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header+20,
michael@0 2030 og[2].header_len-20);
michael@0 2031 ogg_sync_wrote(&oy,og[2].header_len-20);
michael@0 2032 memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body,
michael@0 2033 og[2].body_len);
michael@0 2034 ogg_sync_wrote(&oy,og[2].body_len);
michael@0 2035 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
michael@0 2036
michael@0 2037 fprintf(stderr,"ok.\n");
michael@0 2038 }
michael@0 2039
michael@0 2040 /* Test recapture: page + garbage + page */
michael@0 2041 {
michael@0 2042 ogg_page og_de;
michael@0 2043 fprintf(stderr,"Testing recapture... ");
michael@0 2044 ogg_sync_reset(&oy);
michael@0 2045
michael@0 2046 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
michael@0 2047 og[1].header_len);
michael@0 2048 ogg_sync_wrote(&oy,og[1].header_len);
michael@0 2049
michael@0 2050 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
michael@0 2051 og[1].body_len);
michael@0 2052 ogg_sync_wrote(&oy,og[1].body_len);
michael@0 2053
michael@0 2054 memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
michael@0 2055 og[2].header_len);
michael@0 2056 ogg_sync_wrote(&oy,og[2].header_len);
michael@0 2057
michael@0 2058 memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
michael@0 2059 og[2].header_len);
michael@0 2060 ogg_sync_wrote(&oy,og[2].header_len);
michael@0 2061
michael@0 2062 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
michael@0 2063
michael@0 2064 memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body,
michael@0 2065 og[2].body_len-5);
michael@0 2066 ogg_sync_wrote(&oy,og[2].body_len-5);
michael@0 2067
michael@0 2068 memcpy(ogg_sync_buffer(&oy,og[3].header_len),og[3].header,
michael@0 2069 og[3].header_len);
michael@0 2070 ogg_sync_wrote(&oy,og[3].header_len);
michael@0 2071
michael@0 2072 memcpy(ogg_sync_buffer(&oy,og[3].body_len),og[3].body,
michael@0 2073 og[3].body_len);
michael@0 2074 ogg_sync_wrote(&oy,og[3].body_len);
michael@0 2075
michael@0 2076 if(ogg_sync_pageout(&oy,&og_de)>0)error();
michael@0 2077 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
michael@0 2078
michael@0 2079 fprintf(stderr,"ok.\n");
michael@0 2080 }
michael@0 2081
michael@0 2082 /* Free page data that was previously copied */
michael@0 2083 {
michael@0 2084 for(i=0;i<5;i++){
michael@0 2085 free_page(&og[i]);
michael@0 2086 }
michael@0 2087 }
michael@0 2088 }
michael@0 2089
michael@0 2090 return(0);
michael@0 2091 }
michael@0 2092
michael@0 2093 #endif

mercurial