summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libopus/ogg/framing.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libopus/ogg/framing.c')
-rw-r--r--lib/rbcodec/codecs/libopus/ogg/framing.c1294
1 files changed, 1189 insertions, 105 deletions
diff --git a/lib/rbcodec/codecs/libopus/ogg/framing.c b/lib/rbcodec/codecs/libopus/ogg/framing.c
index f007de176a..a7032a6a35 100644
--- a/lib/rbcodec/codecs/libopus/ogg/framing.c
+++ b/lib/rbcodec/codecs/libopus/ogg/framing.c
@@ -5,14 +5,13 @@
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
7 * * 7 * *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * 8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2018 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ * 9 * by the Xiph.Org Foundation http://www.xiph.org/ *
10 * * 10 * *
11 ******************************************************************** 11 ********************************************************************
12 12
13 function: code raw packets into framed OggSquish stream and 13 function: code raw packets into framed OggSquish stream and
14 decode Ogg streams back into raw packets 14 decode Ogg streams back into raw packets
15 last mod: $Id: framing.c 18052 2011-08-04 17:57:02Z giles $
16 15
17 note: The CRC code is directly derived from public domain code by 16 note: The CRC code is directly derived from public domain code by
18 Ross Williams (ross@guest.adelaide.edu.au). See docs/framing.html 17 Ross Williams (ross@guest.adelaide.edu.au). See docs/framing.html
@@ -20,9 +19,14 @@
20 19
21 ********************************************************************/ 20 ********************************************************************/
22 21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
23#include <stdlib.h> 26#include <stdlib.h>
27#include <limits.h>
24#include <string.h> 28#include <string.h>
25#include <ogg/ogg.h> 29#include "ogg.h"
26 30
27/* A complete description of Ogg framing exists in docs/framing.html */ 31/* A complete description of Ogg framing exists in docs/framing.html */
28 32
@@ -98,90 +102,31 @@ int ogg_page_packets(const ogg_page *og){
98 102
99#if 0 103#if 0
100/* helper to initialize lookup for direct-table CRC (illustrative; we 104/* helper to initialize lookup for direct-table CRC (illustrative; we
101 use the static init below) */ 105 use the static init in crctable.h) */
102 106
103static ogg_uint32_t _ogg_crc_entry(unsigned long index){ 107static void _ogg_crc_init(){
104 int i; 108 int i, j;
105 unsigned long r; 109 ogg_uint32_t polynomial, crc;
106 110 polynomial = 0x04c11db7; /* The same as the ethernet generator
107 r = index << 24; 111 polynomial, although we use an
108 for (i=0; i<8; i++) 112 unreflected alg and an init/final
109 if (r & 0x80000000UL) 113 of 0, not 0xffffffff */
110 r = (r << 1) ^ 0x04c11db7; /* The same as the ethernet generator 114 for (i = 0; i <= 0xFF; i++){
111 polynomial, although we use an 115 crc = i << 24;
112 unreflected alg and an init/final 116
113 of 0, not 0xffffffff */ 117 for (j = 0; j < 8; j++)
114 else 118 crc = (crc << 1) ^ (crc & (1 << 31) ? polynomial : 0);
115 r<<=1; 119
116 return (r & 0xffffffffUL); 120 crc_lookup[0][i] = crc;
121 }
122
123 for (i = 0; i <= 0xFF; i++)
124 for (j = 1; j < 8; j++)
125 crc_lookup[j][i] = crc_lookup[0][(crc_lookup[j - 1][i] >> 24) & 0xFF] ^ (crc_lookup[j - 1][i] << 8);
117} 126}
118#endif 127#endif
119 128
120static const ogg_uint32_t crc_lookup[256]={ 129#include "crctable.h"
121 0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9,
122 0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005,
123 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61,
124 0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd,
125 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9,
126 0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75,
127 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011,
128 0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd,
129 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039,
130 0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5,
131 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81,
132 0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d,
133 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49,
134 0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95,
135 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1,
136 0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d,
137 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae,
138 0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072,
139 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16,
140 0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca,
141 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde,
142 0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02,
143 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066,
144 0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba,
145 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e,
146 0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692,
147 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6,
148 0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a,
149 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e,
150 0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2,
151 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686,
152 0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a,
153 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637,
154 0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb,
155 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f,
156 0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53,
157 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47,
158 0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b,
159 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff,
160 0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623,
161 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7,
162 0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b,
163 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f,
164 0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3,
165 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7,
166 0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b,
167 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f,
168 0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3,
169 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640,
170 0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c,
171 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8,
172 0x68860bfd,0x6c47164a,0x61043093,0x65c52d24,
173 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30,
174 0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec,
175 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088,
176 0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654,
177 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0,
178 0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c,
179 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18,
180 0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4,
181 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0,
182 0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c,
183 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668,
184 0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4};
185 130
186/* init the encode/decode logical stream state */ 131/* init the encode/decode logical stream state */
187 132
@@ -236,39 +181,51 @@ int ogg_stream_destroy(ogg_stream_state *os){
236/* Helpers for ogg_stream_encode; this keeps the structure and 181/* Helpers for ogg_stream_encode; this keeps the structure and
237 what's happening fairly clear */ 182 what's happening fairly clear */
238 183
239static int _os_body_expand(ogg_stream_state *os,int needed){ 184static int _os_body_expand(ogg_stream_state *os,long needed){
240 if(os->body_storage<=os->body_fill+needed){ 185 if(os->body_storage-needed<=os->body_fill){
186 long body_storage;
241 void *ret; 187 void *ret;
242 ret=_ogg_realloc(os->body_data,(os->body_storage+needed+1024)* 188 if(os->body_storage>LONG_MAX-needed){
243 sizeof(*os->body_data)); 189 ogg_stream_clear(os);
190 return -1;
191 }
192 body_storage=os->body_storage+needed;
193 if(body_storage<LONG_MAX-1024)body_storage+=1024;
194 ret=_ogg_realloc(os->body_data,body_storage*sizeof(*os->body_data));
244 if(!ret){ 195 if(!ret){
245 ogg_stream_clear(os); 196 ogg_stream_clear(os);
246 return -1; 197 return -1;
247 } 198 }
248 os->body_storage+=(needed+1024); 199 os->body_storage=body_storage;
249 os->body_data=ret; 200 os->body_data=ret;
250 } 201 }
251 return 0; 202 return 0;
252} 203}
253 204
254static int _os_lacing_expand(ogg_stream_state *os,int needed){ 205static int _os_lacing_expand(ogg_stream_state *os,long needed){
255 if(os->lacing_storage<=os->lacing_fill+needed){ 206 if(os->lacing_storage-needed<=os->lacing_fill){
207 long lacing_storage;
256 void *ret; 208 void *ret;
257 ret=_ogg_realloc(os->lacing_vals,(os->lacing_storage+needed+32)* 209 if(os->lacing_storage>LONG_MAX-needed){
258 sizeof(*os->lacing_vals)); 210 ogg_stream_clear(os);
211 return -1;
212 }
213 lacing_storage=os->lacing_storage+needed;
214 if(lacing_storage<LONG_MAX-32)lacing_storage+=32;
215 ret=_ogg_realloc(os->lacing_vals,lacing_storage*sizeof(*os->lacing_vals));
259 if(!ret){ 216 if(!ret){
260 ogg_stream_clear(os); 217 ogg_stream_clear(os);
261 return -1; 218 return -1;
262 } 219 }
263 os->lacing_vals=ret; 220 os->lacing_vals=ret;
264 ret=_ogg_realloc(os->granule_vals,(os->lacing_storage+needed+32)* 221 ret=_ogg_realloc(os->granule_vals,lacing_storage*
265 sizeof(*os->granule_vals)); 222 sizeof(*os->granule_vals));
266 if(!ret){ 223 if(!ret){
267 ogg_stream_clear(os); 224 ogg_stream_clear(os);
268 return -1; 225 return -1;
269 } 226 }
270 os->granule_vals=ret; 227 os->granule_vals=ret;
271 os->lacing_storage+=(needed+32); 228 os->lacing_storage=lacing_storage;
272 } 229 }
273 return 0; 230 return 0;
274} 231}
@@ -277,10 +234,27 @@ static int _os_lacing_expand(ogg_stream_state *os,int needed){
277/* Direct table CRC; note that this will be faster in the future if we 234/* Direct table CRC; note that this will be faster in the future if we
278 perform the checksum simultaneously with other copies */ 235 perform the checksum simultaneously with other copies */
279 236
237static ogg_uint32_t _os_update_crc(ogg_uint32_t crc, unsigned char *buffer, int size){
238 while (size>=8){
239 crc^=buffer[0]<<24|buffer[1]<<16|buffer[2]<<8|buffer[3];
240
241 crc=crc_lookup[7][ crc>>24 ]^crc_lookup[6][(crc>>16)&0xFF]^
242 crc_lookup[5][(crc>> 8)&0xFF]^crc_lookup[4][ crc &0xFF]^
243 crc_lookup[3][buffer[4] ]^crc_lookup[2][buffer[5] ]^
244 crc_lookup[1][buffer[6] ]^crc_lookup[0][buffer[7] ];
245
246 buffer+=8;
247 size-=8;
248 }
249
250 while (size--)
251 crc=(crc<<8)^crc_lookup[0][((crc >> 24)&0xff)^*buffer++];
252 return crc;
253}
254
280void ogg_page_checksum_set(ogg_page *og){ 255void ogg_page_checksum_set(ogg_page *og){
281 if(og){ 256 if(og){
282 ogg_uint32_t crc_reg=0; 257 ogg_uint32_t crc_reg=0;
283 int i;
284 258
285 /* safety; needed for API behavior, but not framing code */ 259 /* safety; needed for API behavior, but not framing code */
286 og->header[22]=0; 260 og->header[22]=0;
@@ -288,10 +262,8 @@ void ogg_page_checksum_set(ogg_page *og){
288 og->header[24]=0; 262 og->header[24]=0;
289 og->header[25]=0; 263 og->header[25]=0;
290 264
291 for(i=0;i<og->header_len;i++) 265 crc_reg=_os_update_crc(crc_reg,og->header,og->header_len);
292 crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->header[i]]; 266 crc_reg=_os_update_crc(crc_reg,og->body,og->body_len);
293 for(i=0;i<og->body_len;i++)
294 crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->body[i]];
295 267
296 og->header[22]=(unsigned char)(crc_reg&0xff); 268 og->header[22]=(unsigned char)(crc_reg&0xff);
297 og->header[23]=(unsigned char)((crc_reg>>8)&0xff); 269 og->header[23]=(unsigned char)((crc_reg>>8)&0xff);
@@ -304,12 +276,17 @@ void ogg_page_checksum_set(ogg_page *og){
304int ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov, int count, 276int ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov, int count,
305 long e_o_s, ogg_int64_t granulepos){ 277 long e_o_s, ogg_int64_t granulepos){
306 278
307 int bytes = 0, lacing_vals, i; 279 long bytes = 0, lacing_vals;
280 int i;
308 281
309 if(ogg_stream_check(os)) return -1; 282 if(ogg_stream_check(os)) return -1;
310 if(!iov) return 0; 283 if(!iov) return 0;
311 284
312 for (i = 0; i < count; ++i) bytes += (int)iov[i].iov_len; 285 for (i = 0; i < count; ++i){
286 if(iov[i].iov_len>LONG_MAX) return -1;
287 if(bytes>LONG_MAX-(long)iov[i].iov_len) return -1;
288 bytes += (long)iov[i].iov_len;
289 }
313 lacing_vals=bytes/255+1; 290 lacing_vals=bytes/255+1;
314 291
315 if(os->body_returned){ 292 if(os->body_returned){
@@ -396,9 +373,9 @@ static int ogg_stream_flush_i(ogg_stream_state *os,ogg_page *og, int force, int
396 }else{ 373 }else{
397 374
398 /* The extra packets_done, packet_just_done logic here attempts to do two things: 375 /* The extra packets_done, packet_just_done logic here attempts to do two things:
399 1) Don't unneccessarily span pages. 376 1) Don't unnecessarily span pages.
400 2) Unless necessary, don't flush pages if there are less than four packets on 377 2) Unless necessary, don't flush pages if there are less than four packets on
401 them; this expands page size to reduce unneccessary overhead if incoming packets 378 them; this expands page size to reduce unnecessary overhead if incoming packets
402 are large. 379 are large.
403 These are not necessary behaviors, just 'always better than naive flushing' 380 These are not necessary behaviors, just 'always better than naive flushing'
404 without requiring an application to explicitly request a specific optimized 381 without requiring an application to explicitly request a specific optimized
@@ -683,7 +660,7 @@ long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og){
683 if(oy->bodybytes+oy->headerbytes>bytes)return(0); 660 if(oy->bodybytes+oy->headerbytes>bytes)return(0);
684 661
685 /* The whole test page is buffered. Verify the checksum */ 662 /* The whole test page is buffered. Verify the checksum */
686 if (0) { 663 {
687 /* Grab the checksum bytes, set the header field to zero */ 664 /* Grab the checksum bytes, set the header field to zero */
688 char chksum[4]; 665 char chksum[4];
689 ogg_page log; 666 ogg_page log;
@@ -705,8 +682,10 @@ long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og){
705 /* replace the computed checksum with the one actually read in */ 682 /* replace the computed checksum with the one actually read in */
706 memcpy(page+22,chksum,4); 683 memcpy(page+22,chksum,4);
707 684
685#ifndef DISABLE_CRC
708 /* Bad checksum. Lose sync */ 686 /* Bad checksum. Lose sync */
709 goto sync_fail; 687 goto sync_fail;
688#endif
710 } 689 }
711 } 690 }
712 691
@@ -780,6 +759,7 @@ int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og){
780 } 759 }
781 760
782 /* loop. keep looking */ 761 /* loop. keep looking */
762
783 } 763 }
784} 764}
785 765
@@ -856,6 +836,7 @@ int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){
856 some segments */ 836 some segments */
857 if(continued){ 837 if(continued){
858 if(os->lacing_fill<1 || 838 if(os->lacing_fill<1 ||
839 (os->lacing_vals[os->lacing_fill-1]&0xff)<255 ||
859 os->lacing_vals[os->lacing_fill-1]==0x400){ 840 os->lacing_vals[os->lacing_fill-1]==0x400){
860 bos=0; 841 bos=0;
861 for(;segptr<segments;segptr++){ 842 for(;segptr<segments;segptr++){
@@ -1023,3 +1004,1106 @@ void ogg_packet_clear(ogg_packet *op) {
1023 memset(op, 0, sizeof(*op)); 1004 memset(op, 0, sizeof(*op));
1024} 1005}
1025 1006
1007#ifdef _V_SELFTEST
1008#include <stdio.h>
1009
1010ogg_stream_state os_en, os_de;
1011ogg_sync_state oy;
1012
1013void checkpacket(ogg_packet *op,long len, int no, long pos){
1014 long j;
1015 static int sequence=0;
1016 static int lastno=0;
1017
1018 if(op->bytes!=len){
1019 fprintf(stderr,"incorrect packet length (%ld != %ld)!\n",op->bytes,len);
1020 exit(1);
1021 }
1022 if(op->granulepos!=pos){
1023 fprintf(stderr,"incorrect packet granpos (%ld != %ld)!\n",(long)op->granulepos,pos);
1024 exit(1);
1025 }
1026
1027 /* packet number just follows sequence/gap; adjust the input number
1028 for that */
1029 if(no==0){
1030 sequence=0;
1031 }else{
1032 sequence++;
1033 if(no>lastno+1)
1034 sequence++;
1035 }
1036 lastno=no;
1037 if(op->packetno!=sequence){
1038 fprintf(stderr,"incorrect packet sequence %ld != %d\n",
1039 (long)(op->packetno),sequence);
1040 exit(1);
1041 }
1042
1043 /* Test data */
1044 for(j=0;j<op->bytes;j++)
1045 if(op->packet[j]!=((j+no)&0xff)){
1046 fprintf(stderr,"body data mismatch (1) at pos %ld: %x!=%lx!\n\n",
1047 j,op->packet[j],(j+no)&0xff);
1048 exit(1);
1049 }
1050}
1051
1052void check_page(unsigned char *data,const int *header,ogg_page *og){
1053 long j;
1054 /* Test data */
1055 for(j=0;j<og->body_len;j++)
1056 if(og->body[j]!=data[j]){
1057 fprintf(stderr,"body data mismatch (2) at pos %ld: %x!=%x!\n\n",
1058 j,data[j],og->body[j]);
1059 exit(1);
1060 }
1061
1062 /* Test header */
1063 for(j=0;j<og->header_len;j++){
1064 if(og->header[j]!=header[j]){
1065 fprintf(stderr,"header content mismatch at pos %ld:\n",j);
1066 for(j=0;j<header[26]+27;j++)
1067 fprintf(stderr," (%ld)%02x:%02x",j,header[j],og->header[j]);
1068 fprintf(stderr,"\n");
1069 exit(1);
1070 }
1071 }
1072 if(og->header_len!=header[26]+27){
1073 fprintf(stderr,"header length incorrect! (%ld!=%d)\n",
1074 og->header_len,header[26]+27);
1075 exit(1);
1076 }
1077}
1078
1079void print_header(ogg_page *og){
1080 int j;
1081 fprintf(stderr,"\nHEADER:\n");
1082 fprintf(stderr," capture: %c %c %c %c version: %d flags: %x\n",
1083 og->header[0],og->header[1],og->header[2],og->header[3],
1084 (int)og->header[4],(int)og->header[5]);
1085
1086 fprintf(stderr," granulepos: %d serialno: %d pageno: %ld\n",
1087 (og->header[9]<<24)|(og->header[8]<<16)|
1088 (og->header[7]<<8)|og->header[6],
1089 (og->header[17]<<24)|(og->header[16]<<16)|
1090 (og->header[15]<<8)|og->header[14],
1091 ((long)(og->header[21])<<24)|(og->header[20]<<16)|
1092 (og->header[19]<<8)|og->header[18]);
1093
1094 fprintf(stderr," checksum: %02x:%02x:%02x:%02x\n segments: %d (",
1095 (int)og->header[22],(int)og->header[23],
1096 (int)og->header[24],(int)og->header[25],
1097 (int)og->header[26]);
1098
1099 for(j=27;j<og->header_len;j++)
1100 fprintf(stderr,"%d ",(int)og->header[j]);
1101 fprintf(stderr,")\n\n");
1102}
1103
1104void copy_page(ogg_page *og){
1105 unsigned char *temp=_ogg_malloc(og->header_len);
1106 memcpy(temp,og->header,og->header_len);
1107 og->header=temp;
1108
1109 temp=_ogg_malloc(og->body_len);
1110 memcpy(temp,og->body,og->body_len);
1111 og->body=temp;
1112}
1113
1114void free_page(ogg_page *og){
1115 _ogg_free (og->header);
1116 _ogg_free (og->body);
1117}
1118
1119void error(void){
1120 fprintf(stderr,"error!\n");
1121 exit(1);
1122}
1123
1124/* 17 only */
1125const int head1_0[] = {0x4f,0x67,0x67,0x53,0,0x06,
1126 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1127 0x01,0x02,0x03,0x04,0,0,0,0,
1128 0x15,0xed,0xec,0x91,
1129 1,
1130 17};
1131
1132/* 17, 254, 255, 256, 500, 510, 600 byte, pad */
1133const int head1_1[] = {0x4f,0x67,0x67,0x53,0,0x02,
1134 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1135 0x01,0x02,0x03,0x04,0,0,0,0,
1136 0x59,0x10,0x6c,0x2c,
1137 1,
1138 17};
1139const int head2_1[] = {0x4f,0x67,0x67,0x53,0,0x04,
1140 0x07,0x18,0x00,0x00,0x00,0x00,0x00,0x00,
1141 0x01,0x02,0x03,0x04,1,0,0,0,
1142 0x89,0x33,0x85,0xce,
1143 13,
1144 254,255,0,255,1,255,245,255,255,0,
1145 255,255,90};
1146
1147/* nil packets; beginning,middle,end */
1148const int head1_2[] = {0x4f,0x67,0x67,0x53,0,0x02,
1149 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1150 0x01,0x02,0x03,0x04,0,0,0,0,
1151 0xff,0x7b,0x23,0x17,
1152 1,
1153 0};
1154const int head2_2[] = {0x4f,0x67,0x67,0x53,0,0x04,
1155 0x07,0x28,0x00,0x00,0x00,0x00,0x00,0x00,
1156 0x01,0x02,0x03,0x04,1,0,0,0,
1157 0x5c,0x3f,0x66,0xcb,
1158 17,
1159 17,254,255,0,0,255,1,0,255,245,255,255,0,
1160 255,255,90,0};
1161
1162/* large initial packet */
1163const int head1_3[] = {0x4f,0x67,0x67,0x53,0,0x02,
1164 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1165 0x01,0x02,0x03,0x04,0,0,0,0,
1166 0x01,0x27,0x31,0xaa,
1167 18,
1168 255,255,255,255,255,255,255,255,
1169 255,255,255,255,255,255,255,255,255,10};
1170
1171const int head2_3[] = {0x4f,0x67,0x67,0x53,0,0x04,
1172 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
1173 0x01,0x02,0x03,0x04,1,0,0,0,
1174 0x7f,0x4e,0x8a,0xd2,
1175 4,
1176 255,4,255,0};
1177
1178
1179/* continuing packet test */
1180const int head1_4[] = {0x4f,0x67,0x67,0x53,0,0x02,
1181 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1182 0x01,0x02,0x03,0x04,0,0,0,0,
1183 0xff,0x7b,0x23,0x17,
1184 1,
1185 0};
1186
1187const int head2_4[] = {0x4f,0x67,0x67,0x53,0,0x00,
1188 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
1189 0x01,0x02,0x03,0x04,1,0,0,0,
1190 0xf8,0x3c,0x19,0x79,
1191 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,255,
1223 255,255,255,255,255,255,255};
1224
1225const int head3_4[] = {0x4f,0x67,0x67,0x53,0,0x05,
1226 0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,
1227 0x01,0x02,0x03,0x04,2,0,0,0,
1228 0x38,0xe6,0xb6,0x28,
1229 6,
1230 255,220,255,4,255,0};
1231
1232
1233/* spill expansion test */
1234const int head1_4b[] = {0x4f,0x67,0x67,0x53,0,0x02,
1235 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1236 0x01,0x02,0x03,0x04,0,0,0,0,
1237 0xff,0x7b,0x23,0x17,
1238 1,
1239 0};
1240
1241const int head2_4b[] = {0x4f,0x67,0x67,0x53,0,0x00,
1242 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
1243 0x01,0x02,0x03,0x04,1,0,0,0,
1244 0xce,0x8f,0x17,0x1a,
1245 23,
1246 255,255,255,255,255,255,255,255,
1247 255,255,255,255,255,255,255,255,255,10,255,4,255,0,0};
1248
1249
1250const int head3_4b[] = {0x4f,0x67,0x67,0x53,0,0x04,
1251 0x07,0x14,0x00,0x00,0x00,0x00,0x00,0x00,
1252 0x01,0x02,0x03,0x04,2,0,0,0,
1253 0x9b,0xb2,0x50,0xa1,
1254 1,
1255 0};
1256
1257/* page with the 255 segment limit */
1258const int head1_5[] = {0x4f,0x67,0x67,0x53,0,0x02,
1259 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1260 0x01,0x02,0x03,0x04,0,0,0,0,
1261 0xff,0x7b,0x23,0x17,
1262 1,
1263 0};
1264
1265const int head2_5[] = {0x4f,0x67,0x67,0x53,0,0x00,
1266 0x07,0xfc,0x03,0x00,0x00,0x00,0x00,0x00,
1267 0x01,0x02,0x03,0x04,1,0,0,0,
1268 0xed,0x2a,0x2e,0xa7,
1269 255,
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,10,
1301 10,10,10,10,10,10,10};
1302
1303const int head3_5[] = {0x4f,0x67,0x67,0x53,0,0x04,
1304 0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00,
1305 0x01,0x02,0x03,0x04,2,0,0,0,
1306 0x6c,0x3b,0x82,0x3d,
1307 1,
1308 50};
1309
1310
1311/* packet that overspans over an entire page */
1312const int head1_6[] = {0x4f,0x67,0x67,0x53,0,0x02,
1313 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1314 0x01,0x02,0x03,0x04,0,0,0,0,
1315 0xff,0x7b,0x23,0x17,
1316 1,
1317 0};
1318
1319const int head2_6[] = {0x4f,0x67,0x67,0x53,0,0x00,
1320 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
1321 0x01,0x02,0x03,0x04,1,0,0,0,
1322 0x68,0x22,0x7c,0x3d,
1323 255,
1324 100,
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,255,255,
1356 255,255,255,255,255,255};
1357
1358const int head3_6[] = {0x4f,0x67,0x67,0x53,0,0x01,
1359 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
1360 0x01,0x02,0x03,0x04,2,0,0,0,
1361 0xf4,0x87,0xba,0xf3,
1362 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,255,
1394 255,255,255,255,255,255,255};
1395
1396const int head4_6[] = {0x4f,0x67,0x67,0x53,0,0x05,
1397 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
1398 0x01,0x02,0x03,0x04,3,0,0,0,
1399 0xf7,0x2f,0x6c,0x60,
1400 5,
1401 254,255,4,255,0};
1402
1403/* packet that overspans over an entire page */
1404const int head1_7[] = {0x4f,0x67,0x67,0x53,0,0x02,
1405 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1406 0x01,0x02,0x03,0x04,0,0,0,0,
1407 0xff,0x7b,0x23,0x17,
1408 1,
1409 0};
1410
1411const int head2_7[] = {0x4f,0x67,0x67,0x53,0,0x00,
1412 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
1413 0x01,0x02,0x03,0x04,1,0,0,0,
1414 0x68,0x22,0x7c,0x3d,
1415 255,
1416 100,
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,255,255,
1448 255,255,255,255,255,255};
1449
1450const int head3_7[] = {0x4f,0x67,0x67,0x53,0,0x05,
1451 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
1452 0x01,0x02,0x03,0x04,2,0,0,0,
1453 0xd4,0xe0,0x60,0xe5,
1454 1,
1455 0};
1456
1457int compare_packet(const ogg_packet *op1, const ogg_packet *op2){
1458 if(op1->packet!=op2->packet){
1459 fprintf(stderr,"op1->packet != op2->packet\n");
1460 return(1);
1461 }
1462 if(op1->bytes!=op2->bytes){
1463 fprintf(stderr,"op1->bytes != op2->bytes\n");
1464 return(1);
1465 }
1466 if(op1->b_o_s!=op2->b_o_s){
1467 fprintf(stderr,"op1->b_o_s != op2->b_o_s\n");
1468 return(1);
1469 }
1470 if(op1->e_o_s!=op2->e_o_s){
1471 fprintf(stderr,"op1->e_o_s != op2->e_o_s\n");
1472 return(1);
1473 }
1474 if(op1->granulepos!=op2->granulepos){
1475 fprintf(stderr,"op1->granulepos != op2->granulepos\n");
1476 return(1);
1477 }
1478 if(op1->packetno!=op2->packetno){
1479 fprintf(stderr,"op1->packetno != op2->packetno\n");
1480 return(1);
1481 }
1482 return(0);
1483}
1484
1485void test_pack(const int *pl, const int **headers, int byteskip,
1486 int pageskip, int packetskip){
1487 unsigned char *data=_ogg_malloc(1024*1024); /* for scripted test cases only */
1488 long inptr=0;
1489 long outptr=0;
1490 long deptr=0;
1491 long depacket=0;
1492 long granule_pos=7,pageno=0;
1493 int i,j,packets,pageout=pageskip;
1494 int eosflag=0;
1495 int bosflag=0;
1496
1497 int byteskipcount=0;
1498
1499 ogg_stream_reset(&os_en);
1500 ogg_stream_reset(&os_de);
1501 ogg_sync_reset(&oy);
1502
1503 for(packets=0;packets<packetskip;packets++)
1504 depacket+=pl[packets];
1505
1506 for(packets=0;;packets++)if(pl[packets]==-1)break;
1507
1508 for(i=0;i<packets;i++){
1509 /* construct a test packet */
1510 ogg_packet op;
1511 int len=pl[i];
1512
1513 op.packet=data+inptr;
1514 op.bytes=len;
1515 op.e_o_s=(pl[i+1]<0?1:0);
1516 op.granulepos=granule_pos;
1517
1518 granule_pos+=1024;
1519
1520 for(j=0;j<len;j++)data[inptr++]=i+j;
1521
1522 /* submit the test packet */
1523 ogg_stream_packetin(&os_en,&op);
1524
1525 /* retrieve any finished pages */
1526 {
1527 ogg_page og;
1528
1529 while(ogg_stream_pageout(&os_en,&og)){
1530 /* We have a page. Check it carefully */
1531
1532 fprintf(stderr,"%ld, ",pageno);
1533
1534 if(headers[pageno]==NULL){
1535 fprintf(stderr,"coded too many pages!\n");
1536 exit(1);
1537 }
1538
1539 check_page(data+outptr,headers[pageno],&og);
1540
1541 outptr+=og.body_len;
1542 pageno++;
1543 if(pageskip){
1544 bosflag=1;
1545 pageskip--;
1546 deptr+=og.body_len;
1547 }
1548
1549 /* have a complete page; submit it to sync/decode */
1550
1551 {
1552 ogg_page og_de;
1553 ogg_packet op_de,op_de2;
1554 char *buf=ogg_sync_buffer(&oy,og.header_len+og.body_len);
1555 char *next=buf;
1556 byteskipcount+=og.header_len;
1557 if(byteskipcount>byteskip){
1558 memcpy(next,og.header,byteskipcount-byteskip);
1559 next+=byteskipcount-byteskip;
1560 byteskipcount=byteskip;
1561 }
1562
1563 byteskipcount+=og.body_len;
1564 if(byteskipcount>byteskip){
1565 memcpy(next,og.body,byteskipcount-byteskip);
1566 next+=byteskipcount-byteskip;
1567 byteskipcount=byteskip;
1568 }
1569
1570 ogg_sync_wrote(&oy,next-buf);
1571
1572 while(1){
1573 int ret=ogg_sync_pageout(&oy,&og_de);
1574 if(ret==0)break;
1575 if(ret<0)continue;
1576 /* got a page. Happy happy. Verify that it's good. */
1577
1578 fprintf(stderr,"(%d), ",pageout);
1579
1580 check_page(data+deptr,headers[pageout],&og_de);
1581 deptr+=og_de.body_len;
1582 pageout++;
1583
1584 /* submit it to deconstitution */
1585 ogg_stream_pagein(&os_de,&og_de);
1586
1587 /* packets out? */
1588 while(ogg_stream_packetpeek(&os_de,&op_de2)>0){
1589 ogg_stream_packetpeek(&os_de,NULL);
1590 ogg_stream_packetout(&os_de,&op_de); /* just catching them all */
1591
1592 /* verify peek and out match */
1593 if(compare_packet(&op_de,&op_de2)){
1594 fprintf(stderr,"packetout != packetpeek! pos=%ld\n",
1595 depacket);
1596 exit(1);
1597 }
1598
1599 /* verify the packet! */
1600 /* check data */
1601 if(memcmp(data+depacket,op_de.packet,op_de.bytes)){
1602 fprintf(stderr,"packet data mismatch in decode! pos=%ld\n",
1603 depacket);
1604 exit(1);
1605 }
1606 /* check bos flag */
1607 if(bosflag==0 && op_de.b_o_s==0){
1608 fprintf(stderr,"b_o_s flag not set on packet!\n");
1609 exit(1);
1610 }
1611 if(bosflag && op_de.b_o_s){
1612 fprintf(stderr,"b_o_s flag incorrectly set on packet!\n");
1613 exit(1);
1614 }
1615 bosflag=1;
1616 depacket+=op_de.bytes;
1617
1618 /* check eos flag */
1619 if(eosflag){
1620 fprintf(stderr,"Multiple decoded packets with eos flag!\n");
1621 exit(1);
1622 }
1623
1624 if(op_de.e_o_s)eosflag=1;
1625
1626 /* check granulepos flag */
1627 if(op_de.granulepos!=-1){
1628 fprintf(stderr," granule:%ld ",(long)op_de.granulepos);
1629 }
1630 }
1631 }
1632 }
1633 }
1634 }
1635 }
1636 _ogg_free(data);
1637 if(headers[pageno]!=NULL){
1638 fprintf(stderr,"did not write last page!\n");
1639 exit(1);
1640 }
1641 if(headers[pageout]!=NULL){
1642 fprintf(stderr,"did not decode last page!\n");
1643 exit(1);
1644 }
1645 if(inptr!=outptr){
1646 fprintf(stderr,"encoded page data incomplete!\n");
1647 exit(1);
1648 }
1649 if(inptr!=deptr){
1650 fprintf(stderr,"decoded page data incomplete!\n");
1651 exit(1);
1652 }
1653 if(inptr!=depacket){
1654 fprintf(stderr,"decoded packet data incomplete!\n");
1655 exit(1);
1656 }
1657 if(!eosflag){
1658 fprintf(stderr,"Never got a packet with EOS set!\n");
1659 exit(1);
1660 }
1661 fprintf(stderr,"ok.\n");
1662}
1663
1664int main(void){
1665
1666 ogg_stream_init(&os_en,0x04030201);
1667 ogg_stream_init(&os_de,0x04030201);
1668 ogg_sync_init(&oy);
1669
1670 /* Exercise each code path in the framing code. Also verify that
1671 the checksums are working. */
1672
1673 {
1674 /* 17 only */
1675 const int packets[]={17, -1};
1676 const int *headret[]={head1_0,NULL};
1677
1678 fprintf(stderr,"testing single page encoding... ");
1679 test_pack(packets,headret,0,0,0);
1680 }
1681
1682 {
1683 /* 17, 254, 255, 256, 500, 510, 600 byte, pad */
1684 const int packets[]={17, 254, 255, 256, 500, 510, 600, -1};
1685 const int *headret[]={head1_1,head2_1,NULL};
1686
1687 fprintf(stderr,"testing basic page encoding... ");
1688 test_pack(packets,headret,0,0,0);
1689 }
1690
1691 {
1692 /* nil packets; beginning,middle,end */
1693 const int packets[]={0,17, 254, 255, 0, 256, 0, 500, 510, 600, 0, -1};
1694 const int *headret[]={head1_2,head2_2,NULL};
1695
1696 fprintf(stderr,"testing basic nil packets... ");
1697 test_pack(packets,headret,0,0,0);
1698 }
1699
1700 {
1701 /* large initial packet */
1702 const int packets[]={4345,259,255,-1};
1703 const int *headret[]={head1_3,head2_3,NULL};
1704
1705 fprintf(stderr,"testing initial-packet lacing > 4k... ");
1706 test_pack(packets,headret,0,0,0);
1707 }
1708
1709 {
1710 /* continuing packet test; with page spill expansion, we have to
1711 overflow the lacing table. */
1712 const int packets[]={0,65500,259,255,-1};
1713 const int *headret[]={head1_4,head2_4,head3_4,NULL};
1714
1715 fprintf(stderr,"testing single packet page span... ");
1716 test_pack(packets,headret,0,0,0);
1717 }
1718
1719 {
1720 /* spill expand packet test */
1721 const int packets[]={0,4345,259,255,0,0,-1};
1722 const int *headret[]={head1_4b,head2_4b,head3_4b,NULL};
1723
1724 fprintf(stderr,"testing page spill expansion... ");
1725 test_pack(packets,headret,0,0,0);
1726 }
1727
1728 /* page with the 255 segment limit */
1729 {
1730
1731 const int packets[]={0,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,10,
1734 10,10,10,10,10,10,10,10,
1735 10,10,10,10,10,10,10,10,
1736 10,10,10,10,10,10,10,10,
1737 10,10,10,10,10,10,10,10,
1738 10,10,10,10,10,10,10,10,
1739 10,10,10,10,10,10,10,10,
1740 10,10,10,10,10,10,10,10,
1741 10,10,10,10,10,10,10,10,
1742 10,10,10,10,10,10,10,10,
1743 10,10,10,10,10,10,10,10,
1744 10,10,10,10,10,10,10,10,
1745 10,10,10,10,10,10,10,10,
1746 10,10,10,10,10,10,10,10,
1747 10,10,10,10,10,10,10,10,
1748 10,10,10,10,10,10,10,10,
1749 10,10,10,10,10,10,10,10,
1750 10,10,10,10,10,10,10,10,
1751 10,10,10,10,10,10,10,10,
1752 10,10,10,10,10,10,10,10,
1753 10,10,10,10,10,10,10,10,
1754 10,10,10,10,10,10,10,10,
1755 10,10,10,10,10,10,10,10,
1756 10,10,10,10,10,10,10,10,
1757 10,10,10,10,10,10,10,10,
1758 10,10,10,10,10,10,10,10,
1759 10,10,10,10,10,10,10,10,
1760 10,10,10,10,10,10,10,10,
1761 10,10,10,10,10,10,10,10,
1762 10,10,10,10,10,10,10,50,-1};
1763 const int *headret[]={head1_5,head2_5,head3_5,NULL};
1764
1765 fprintf(stderr,"testing max packet segments... ");
1766 test_pack(packets,headret,0,0,0);
1767 }
1768
1769 {
1770 /* packet that overspans over an entire page */
1771 const int packets[]={0,100,130049,259,255,-1};
1772 const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL};
1773
1774 fprintf(stderr,"testing very large packets... ");
1775 test_pack(packets,headret,0,0,0);
1776 }
1777
1778#ifndef DISABLE_CRC
1779 {
1780 /* test for the libogg 1.1.1 resync in large continuation bug
1781 found by Josh Coalson) */
1782 const int packets[]={0,100,130049,259,255,-1};
1783 const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL};
1784
1785 fprintf(stderr,"testing continuation resync in very large packets... ");
1786 test_pack(packets,headret,100,2,3);
1787 }
1788#else
1789 fprintf(stderr,"Skipping continuation resync test due to --disable-crc\n");
1790#endif
1791
1792 {
1793 /* term only page. why not? */
1794 const int packets[]={0,100,64770,-1};
1795 const int *headret[]={head1_7,head2_7,head3_7,NULL};
1796
1797 fprintf(stderr,"testing zero data page (1 nil packet)... ");
1798 test_pack(packets,headret,0,0,0);
1799 }
1800
1801
1802
1803 {
1804 /* build a bunch of pages for testing */
1805 unsigned char *data=_ogg_malloc(1024*1024);
1806 int pl[]={0, 1,1,98,4079, 1,1,2954,2057, 76,34,912,0,234,1000,1000, 1000,300,-1};
1807 int inptr=0,i,j;
1808 ogg_page og[5];
1809
1810 ogg_stream_reset(&os_en);
1811
1812 for(i=0;pl[i]!=-1;i++){
1813 ogg_packet op;
1814 int len=pl[i];
1815
1816 op.packet=data+inptr;
1817 op.bytes=len;
1818 op.e_o_s=(pl[i+1]<0?1:0);
1819 op.granulepos=(i+1)*1000;
1820
1821 for(j=0;j<len;j++)data[inptr++]=i+j;
1822 ogg_stream_packetin(&os_en,&op);
1823 }
1824
1825 _ogg_free(data);
1826
1827 /* retrieve finished pages */
1828 for(i=0;i<5;i++){
1829 if(ogg_stream_pageout(&os_en,&og[i])==0){
1830 fprintf(stderr,"Too few pages output building sync tests!\n");
1831 exit(1);
1832 }
1833 copy_page(&og[i]);
1834 }
1835
1836 /* Test lost pages on pagein/packetout: no rollback */
1837 {
1838 ogg_page temp;
1839 ogg_packet test;
1840
1841 fprintf(stderr,"Testing loss of pages... ");
1842
1843 ogg_sync_reset(&oy);
1844 ogg_stream_reset(&os_de);
1845 for(i=0;i<5;i++){
1846 memcpy(ogg_sync_buffer(&oy,og[i].header_len),og[i].header,
1847 og[i].header_len);
1848 ogg_sync_wrote(&oy,og[i].header_len);
1849 memcpy(ogg_sync_buffer(&oy,og[i].body_len),og[i].body,og[i].body_len);
1850 ogg_sync_wrote(&oy,og[i].body_len);
1851 }
1852
1853 ogg_sync_pageout(&oy,&temp);
1854 ogg_stream_pagein(&os_de,&temp);
1855 ogg_sync_pageout(&oy,&temp);
1856 ogg_stream_pagein(&os_de,&temp);
1857 ogg_sync_pageout(&oy,&temp);
1858 /* skip */
1859 ogg_sync_pageout(&oy,&temp);
1860 ogg_stream_pagein(&os_de,&temp);
1861
1862 /* do we get the expected results/packets? */
1863
1864 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1865 checkpacket(&test,0,0,0);
1866 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1867 checkpacket(&test,1,1,-1);
1868 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1869 checkpacket(&test,1,2,-1);
1870 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1871 checkpacket(&test,98,3,-1);
1872 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1873 checkpacket(&test,4079,4,5000);
1874 if(ogg_stream_packetout(&os_de,&test)!=-1){
1875 fprintf(stderr,"Error: loss of page did not return error\n");
1876 exit(1);
1877 }
1878 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1879 checkpacket(&test,76,9,-1);
1880 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1881 checkpacket(&test,34,10,-1);
1882 fprintf(stderr,"ok.\n");
1883 }
1884
1885 /* Test lost pages on pagein/packetout: rollback with continuation */
1886 {
1887 ogg_page temp;
1888 ogg_packet test;
1889
1890 fprintf(stderr,"Testing loss of pages (rollback required)... ");
1891
1892 ogg_sync_reset(&oy);
1893 ogg_stream_reset(&os_de);
1894 for(i=0;i<5;i++){
1895 memcpy(ogg_sync_buffer(&oy,og[i].header_len),og[i].header,
1896 og[i].header_len);
1897 ogg_sync_wrote(&oy,og[i].header_len);
1898 memcpy(ogg_sync_buffer(&oy,og[i].body_len),og[i].body,og[i].body_len);
1899 ogg_sync_wrote(&oy,og[i].body_len);
1900 }
1901
1902 ogg_sync_pageout(&oy,&temp);
1903 ogg_stream_pagein(&os_de,&temp);
1904 ogg_sync_pageout(&oy,&temp);
1905 ogg_stream_pagein(&os_de,&temp);
1906 ogg_sync_pageout(&oy,&temp);
1907 ogg_stream_pagein(&os_de,&temp);
1908 ogg_sync_pageout(&oy,&temp);
1909 /* skip */
1910 ogg_sync_pageout(&oy,&temp);
1911 ogg_stream_pagein(&os_de,&temp);
1912
1913 /* do we get the expected results/packets? */
1914
1915 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1916 checkpacket(&test,0,0,0);
1917 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1918 checkpacket(&test,1,1,-1);
1919 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1920 checkpacket(&test,1,2,-1);
1921 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1922 checkpacket(&test,98,3,-1);
1923 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1924 checkpacket(&test,4079,4,5000);
1925 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1926 checkpacket(&test,1,5,-1);
1927 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1928 checkpacket(&test,1,6,-1);
1929 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1930 checkpacket(&test,2954,7,-1);
1931 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1932 checkpacket(&test,2057,8,9000);
1933 if(ogg_stream_packetout(&os_de,&test)!=-1){
1934 fprintf(stderr,"Error: loss of page did not return error\n");
1935 exit(1);
1936 }
1937 if(ogg_stream_packetout(&os_de,&test)!=1)error();
1938 checkpacket(&test,300,17,18000);
1939 fprintf(stderr,"ok.\n");
1940 }
1941
1942 /* the rest only test sync */
1943 {
1944 ogg_page og_de;
1945 /* Test fractional page inputs: incomplete capture */
1946 fprintf(stderr,"Testing sync on partial inputs... ");
1947 ogg_sync_reset(&oy);
1948 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
1949 3);
1950 ogg_sync_wrote(&oy,3);
1951 if(ogg_sync_pageout(&oy,&og_de)>0)error();
1952
1953 /* Test fractional page inputs: incomplete fixed header */
1954 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+3,
1955 20);
1956 ogg_sync_wrote(&oy,20);
1957 if(ogg_sync_pageout(&oy,&og_de)>0)error();
1958
1959 /* Test fractional page inputs: incomplete header */
1960 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+23,
1961 5);
1962 ogg_sync_wrote(&oy,5);
1963 if(ogg_sync_pageout(&oy,&og_de)>0)error();
1964
1965 /* Test fractional page inputs: incomplete body */
1966
1967 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+28,
1968 og[1].header_len-28);
1969 ogg_sync_wrote(&oy,og[1].header_len-28);
1970 if(ogg_sync_pageout(&oy,&og_de)>0)error();
1971
1972 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,1000);
1973 ogg_sync_wrote(&oy,1000);
1974 if(ogg_sync_pageout(&oy,&og_de)>0)error();
1975
1976 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body+1000,
1977 og[1].body_len-1000);
1978 ogg_sync_wrote(&oy,og[1].body_len-1000);
1979 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
1980
1981 fprintf(stderr,"ok.\n");
1982 }
1983
1984 /* Test fractional page inputs: page + incomplete capture */
1985 {
1986 ogg_page og_de;
1987 fprintf(stderr,"Testing sync on 1+partial inputs... ");
1988 ogg_sync_reset(&oy);
1989
1990 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
1991 og[1].header_len);
1992 ogg_sync_wrote(&oy,og[1].header_len);
1993
1994 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
1995 og[1].body_len);
1996 ogg_sync_wrote(&oy,og[1].body_len);
1997
1998 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
1999 20);
2000 ogg_sync_wrote(&oy,20);
2001 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
2002 if(ogg_sync_pageout(&oy,&og_de)>0)error();
2003
2004 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+20,
2005 og[1].header_len-20);
2006 ogg_sync_wrote(&oy,og[1].header_len-20);
2007 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
2008 og[1].body_len);
2009 ogg_sync_wrote(&oy,og[1].body_len);
2010 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
2011
2012 fprintf(stderr,"ok.\n");
2013 }
2014
2015 /* Test recapture: garbage + page */
2016 {
2017 ogg_page og_de;
2018 fprintf(stderr,"Testing search for capture... ");
2019 ogg_sync_reset(&oy);
2020
2021 /* 'garbage' */
2022 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
2023 og[1].body_len);
2024 ogg_sync_wrote(&oy,og[1].body_len);
2025
2026 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
2027 og[1].header_len);
2028 ogg_sync_wrote(&oy,og[1].header_len);
2029
2030 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
2031 og[1].body_len);
2032 ogg_sync_wrote(&oy,og[1].body_len);
2033
2034 memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
2035 20);
2036 ogg_sync_wrote(&oy,20);
2037 if(ogg_sync_pageout(&oy,&og_de)>0)error();
2038 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
2039 if(ogg_sync_pageout(&oy,&og_de)>0)error();
2040
2041 memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header+20,
2042 og[2].header_len-20);
2043 ogg_sync_wrote(&oy,og[2].header_len-20);
2044 memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body,
2045 og[2].body_len);
2046 ogg_sync_wrote(&oy,og[2].body_len);
2047 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
2048
2049 fprintf(stderr,"ok.\n");
2050 }
2051
2052#ifndef DISABLE_CRC
2053 /* Test recapture: page + garbage + page */
2054 {
2055 ogg_page og_de;
2056 fprintf(stderr,"Testing recapture... ");
2057 ogg_sync_reset(&oy);
2058
2059 memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
2060 og[1].header_len);
2061 ogg_sync_wrote(&oy,og[1].header_len);
2062
2063 memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
2064 og[1].body_len);
2065 ogg_sync_wrote(&oy,og[1].body_len);
2066
2067 memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
2068 og[2].header_len);
2069 ogg_sync_wrote(&oy,og[2].header_len);
2070
2071 memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
2072 og[2].header_len);
2073 ogg_sync_wrote(&oy,og[2].header_len);
2074
2075 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
2076
2077 memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body,
2078 og[2].body_len-5);
2079 ogg_sync_wrote(&oy,og[2].body_len-5);
2080
2081 memcpy(ogg_sync_buffer(&oy,og[3].header_len),og[3].header,
2082 og[3].header_len);
2083 ogg_sync_wrote(&oy,og[3].header_len);
2084
2085 memcpy(ogg_sync_buffer(&oy,og[3].body_len),og[3].body,
2086 og[3].body_len);
2087 ogg_sync_wrote(&oy,og[3].body_len);
2088
2089 if(ogg_sync_pageout(&oy,&og_de)>0)error();
2090 if(ogg_sync_pageout(&oy,&og_de)<=0)error();
2091
2092 fprintf(stderr,"ok.\n");
2093 }
2094#else
2095 fprintf(stderr,"Skipping recapture test due to --disable-crc\n");
2096#endif
2097
2098 /* Free page data that was previously copied */
2099 {
2100 for(i=0;i<5;i++){
2101 free_page(&og[i]);
2102 }
2103 }
2104 }
2105
2106 return(0);
2107}
2108
2109#endif