dbf-halloween2015

annotate libs/ogg/framing.c @ 3:c37fe5d8a4ed

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