Debian Codegraph @

external/vorbis/ogg_framing.cpp

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