Debian Codegraph @

mednafen/tremor/framing.c

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