]>
Commit | Line | Data |
---|---|---|
1 | /* -*-mode:java; c-basic-offset:2; -*- */ | |
2 | /* JOrbis | |
3 | * Copyright (C) 2000 ymnk, JCraft,Inc. | |
4 | * | |
5 | * Written by: 2000 ymnk<ymnk@jcraft.com> | |
6 | * | |
7 | * Many thanks to | |
8 | * Monty <monty@xiph.org> and | |
9 | * The XIPHOPHORUS Company http://www.xiph.org/ . | |
10 | * JOrbis has been based on their awesome works, Vorbis codec. | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Library General Public License | |
14 | * as published by the Free Software Foundation; either version 2 of | |
15 | * the License, or (at your option) any later version. | |
16 | ||
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Library General Public | |
23 | * License along with this program; if not, write to the Free Software | |
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | */ | |
26 | ||
27 | package com.jcraft.jogg; | |
28 | ||
29 | public class Page{ | |
30 | private static int[] crc_lookup=new int[256]; | |
31 | static { | |
32 | for(int i=0; i<crc_lookup.length; i++){ | |
33 | crc_lookup[i]=crc_entry(i); | |
34 | } | |
35 | } | |
36 | ||
37 | private static int crc_entry(int index){ | |
38 | int r=index<<24; | |
39 | for(int i=0; i<8; i++){ | |
40 | if((r& 0x80000000)!=0){ | |
41 | r=(r << 1)^0x04c11db7; /* The same as the ethernet generator | |
42 | polynomial, although we use an | |
43 | unreflected alg and an init/final | |
44 | of 0, not 0xffffffff */ | |
45 | } | |
46 | else{ | |
47 | r<<=1; | |
48 | } | |
49 | } | |
50 | return(r&0xffffffff); | |
51 | } | |
52 | ||
53 | public byte[] header_base; | |
54 | public int header; | |
55 | public int header_len; | |
56 | public byte[] body_base; | |
57 | public int body; | |
58 | public int body_len; | |
59 | ||
60 | int version(){ | |
61 | return header_base[header+4]&0xff; | |
62 | } | |
63 | int continued(){ | |
64 | return (header_base[header+5]&0x01); | |
65 | } | |
66 | public int bos(){ | |
67 | return (header_base[header+5]&0x02); | |
68 | } | |
69 | public int eos(){ | |
70 | return (header_base[header+5]&0x04); | |
71 | } | |
72 | public long granulepos(){ | |
73 | long foo=header_base[header+13]&0xff; | |
74 | foo=(foo<<8)|(header_base[header+12]&0xff); | |
75 | foo=(foo<<8)|(header_base[header+11]&0xff); | |
76 | foo=(foo<<8)|(header_base[header+10]&0xff); | |
77 | foo=(foo<<8)|(header_base[header+9]&0xff); | |
78 | foo=(foo<<8)|(header_base[header+8]&0xff); | |
79 | foo=(foo<<8)|(header_base[header+7]&0xff); | |
80 | foo=(foo<<8)|(header_base[header+6]&0xff); | |
81 | return(foo); | |
82 | } | |
83 | public int serialno(){ | |
84 | return (header_base[header+14]&0xff)| | |
85 | ((header_base[header+15]&0xff)<<8)| | |
86 | ((header_base[header+16]&0xff)<<16)| | |
87 | ((header_base[header+17]&0xff)<<24); | |
88 | } | |
89 | int pageno(){ | |
90 | return (header_base[header+18]&0xff)| | |
91 | ((header_base[header+19]&0xff)<<8)| | |
92 | ((header_base[header+20]&0xff)<<16)| | |
93 | ((header_base[header+21]&0xff)<<24); | |
94 | } | |
95 | ||
96 | void checksum(){ | |
97 | int crc_reg=0; | |
98 | ||
99 | // for(int i=0;i<header_len;i++){ | |
100 | // System.err.println("chksum: "+Integer.toHexString(header_base[header+i]&0xff)); | |
101 | // } | |
102 | ||
103 | for(int i=0;i<header_len;i++){ | |
104 | crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg>>>24)&0xff)^(header_base[header+i]&0xff)]; | |
105 | } | |
106 | for(int i=0;i<body_len;i++){ | |
107 | crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg>>>24)&0xff)^(body_base[body+i]&0xff)]; | |
108 | } | |
109 | header_base[header+22]=(byte)crc_reg/*&0xff*/; | |
110 | header_base[header+23]=(byte)(crc_reg>>>8)/*&0xff*/; | |
111 | header_base[header+24]=(byte)(crc_reg>>>16)/*&0xff*/; | |
112 | header_base[header+25]=(byte)(crc_reg>>>24)/*&0xff*/; | |
113 | } | |
114 | public Page copy(){ | |
115 | return copy(new Page()); | |
116 | } | |
117 | public Page copy(Page p){ | |
118 | byte[] tmp=new byte[header_len]; | |
119 | System.arraycopy(header_base, header, tmp, 0, header_len); | |
120 | p.header_len=header_len; | |
121 | p.header_base=tmp; | |
122 | p.header=0; | |
123 | tmp=new byte[body_len]; | |
124 | System.arraycopy(body_base, body, tmp, 0, body_len); | |
125 | p.body_len=body_len; | |
126 | p.body_base=tmp; | |
127 | p.body=0; | |
128 | return p; | |
129 | } | |
130 | /* | |
131 | // TEST | |
132 | static StreamState os_en, os_de; | |
133 | static SyncState oy; | |
134 | void check_page(byte[] data_base, int data, int[] _header){ | |
135 | // Test data | |
136 | for(int j=0;j<body_len;j++) | |
137 | if(body_base[body+j]!=data_base[data+j]){ | |
138 | System.err.println("body data mismatch at pos "+j+": "+data_base[data+j]+"!="+body_base[body+j]+"!\n"); | |
139 | System.exit(1); | |
140 | } | |
141 | ||
142 | // Test header | |
143 | for(int j=0;j<header_len;j++){ | |
144 | if((header_base[header+j]&0xff)!=_header[j]){ | |
145 | System.err.println("header content mismatch at pos "+j); | |
146 | for(int jj=0;jj<_header[26]+27;jj++) | |
147 | System.err.print(" ("+jj+")"+Integer.toHexString(_header[jj])+":"+Integer.toHexString(header_base[header+jj])); | |
148 | System.err.println(""); | |
149 | System.exit(1); | |
150 | } | |
151 | } | |
152 | if(header_len!=_header[26]+27){ | |
153 | System.err.print("header length incorrect! ("+header_len+"!="+(_header[26]+27)+")"); | |
154 | System.exit(1); | |
155 | } | |
156 | } | |
157 | ||
158 | void print_header(){ | |
159 | System.err.println("\nHEADER:"); | |
160 | System.err.println(" capture: "+ | |
161 | (header_base[header+0]&0xff)+" "+ | |
162 | (header_base[header+1]&0xff)+" "+ | |
163 | (header_base[header+2]&0xff)+" "+ | |
164 | (header_base[header+3]&0xff)+" "+ | |
165 | " version: "+(header_base[header+4]&0xff)+" flags: "+ | |
166 | (header_base[header+5]&0xff)); | |
167 | System.err.println(" pcmpos: "+ | |
168 | (((header_base[header+9]&0xff)<<24)| | |
169 | ((header_base[header+8]&0xff)<<16)| | |
170 | ((header_base[header+7]&0xff)<<8)| | |
171 | ((header_base[header+6]&0xff)))+ | |
172 | " serialno: "+ | |
173 | (((header_base[header+17]&0xff)<<24)| | |
174 | ((header_base[header+16]&0xff)<<16)| | |
175 | ((header_base[header+15]&0xff)<<8)| | |
176 | ((header_base[header+14]&0xff)))+ | |
177 | " pageno: "+ | |
178 | (((header_base[header+21]&0xff)<<24)| | |
179 | ((header_base[header+20]&0xff)<<16)| | |
180 | ((header_base[header+19]&0xff)<<8)| | |
181 | ((header_base[header+18]&0xff)))); | |
182 | ||
183 | System.err.println(" checksum: "+ | |
184 | (header_base[header+22]&0xff)+":"+ | |
185 | (header_base[header+23]&0xff)+":"+ | |
186 | (header_base[header+24]&0xff)+":"+ | |
187 | (header_base[header+25]&0xff)+"\n segments: "+ | |
188 | (header_base[header+26]&0xff)+" ("); | |
189 | for(int j=27;j<header_len;j++){ | |
190 | System.err.println((header_base[header+j]&0xff)+" "); | |
191 | } | |
192 | System.err.println(")\n"); | |
193 | } | |
194 | ||
195 | void copy_page(){ | |
196 | byte[] tmp=new byte[header_len]; | |
197 | System.arraycopy(header_base, header, tmp, 0, header_len); | |
198 | header_base=tmp; | |
199 | header=0; | |
200 | tmp=new byte[body_len]; | |
201 | System.arraycopy(body_base, body, tmp, 0, body_len); | |
202 | body_base=tmp; | |
203 | body=0; | |
204 | } | |
205 | ||
206 | static void test_pack(int[] pl, int[][] headers){ | |
207 | byte[] data=new byte[1024*1024]; // for scripted test cases only | |
208 | int inptr=0; | |
209 | int outptr=0; | |
210 | int deptr=0; | |
211 | int depacket=0; | |
212 | int pcm_pos=7; | |
213 | int packets,pageno=0,pageout=0; | |
214 | int eosflag=0; | |
215 | int bosflag=0; | |
216 | ||
217 | os_en.reset(); | |
218 | os_de.reset(); | |
219 | oy.reset(); | |
220 | ||
221 | for(packets=0;;packets++){ | |
222 | if(pl[packets]==-1)break; | |
223 | } | |
224 | ||
225 | for(int i=0;i<packets;i++){ | |
226 | // construct a test packet | |
227 | Packet op=new Packet(); | |
228 | int len=pl[i]; | |
229 | op.packet_base=data; | |
230 | op.packet=inptr; | |
231 | op.bytes=len; | |
232 | op.e_o_s=(pl[i+1]<0?1:0); | |
233 | op.granulepos=pcm_pos; | |
234 | ||
235 | pcm_pos+=1024; | |
236 | ||
237 | for(int j=0;j<len;j++){ | |
238 | data[inptr++]=(byte)(i+j); | |
239 | } | |
240 | ||
241 | // submit the test packet | |
242 | os_en.packetin(op); | |
243 | ||
244 | // retrieve any finished pages | |
245 | { | |
246 | Page og=new Page(); | |
247 | ||
248 | while(os_en.pageout(og)!=0){ | |
249 | // We have a page. Check it carefully | |
250 | //System.err.print(pageno+", "); | |
251 | if(headers[pageno]==null){ | |
252 | System.err.println("coded too many pages!"); | |
253 | System.exit(1); | |
254 | } | |
255 | og.check_page(data, outptr, headers[pageno]); | |
256 | ||
257 | outptr+=og.body_len; | |
258 | pageno++; | |
259 | ||
260 | //System.err.println("1# pageno="+pageno+", pageout="+pageout); | |
261 | ||
262 | // have a complete page; submit it to sync/decode | |
263 | ||
264 | { | |
265 | Page og_de=new Page(); | |
266 | Packet op_de=new Packet(); | |
267 | int index=oy.buffer(og.header_len+og.body_len); | |
268 | byte[] buf=oy.data; | |
269 | System.arraycopy(og.header_base, og.header, buf, index, og.header_len); | |
270 | System.arraycopy(og.body_base, og.body, buf, index+og.header_len, og.body_len); | |
271 | oy.wrote(og.header_len+og.body_len); | |
272 | ||
273 | //System.err.println("2# pageno="+pageno+", pageout="+pageout); | |
274 | ||
275 | while(oy.pageout(og_de)>0){ | |
276 | // got a page. Happy happy. Verify that it's good. | |
277 | ||
278 | og_de.check_page(data, deptr, headers[pageout]); | |
279 | deptr+=og_de.body_len; | |
280 | pageout++; | |
281 | ||
282 | // submit it to deconstitution | |
283 | os_de.pagein(og_de); | |
284 | ||
285 | // packets out? | |
286 | while(os_de.packetout(op_de)>0){ | |
287 | ||
288 | // verify the packet! | |
289 | // check data | |
290 | boolean check=false; | |
291 | for(int ii=0; ii<op_de.bytes; ii++){ | |
292 | if(data[depacket+ii]!=op_de.packet_base[op_de.packet+ii]){ | |
293 | check=true; | |
294 | break; | |
295 | } | |
296 | } | |
297 | if(check){ | |
298 | System.err.println("packet data mismatch in decode! pos="+ | |
299 | depacket); | |
300 | System.exit(1); | |
301 | } | |
302 | ||
303 | // check bos flag | |
304 | if(bosflag==0 && op_de.b_o_s==0){ | |
305 | System.err.println("b_o_s flag not set on packet!"); | |
306 | System.exit(1); | |
307 | } | |
308 | if(bosflag!=0 && op_de.b_o_s!=0){ | |
309 | System.err.println("b_o_s flag incorrectly set on packet!"); | |
310 | System.exit(1); | |
311 | } | |
312 | ||
313 | bosflag=1; | |
314 | depacket+=op_de.bytes; | |
315 | ||
316 | // check eos flag | |
317 | if(eosflag!=0){ | |
318 | System.err.println("Multiple decoded packets with eos flag!"); | |
319 | System.exit(1); | |
320 | } | |
321 | ||
322 | if(op_de.e_o_s!=0)eosflag=1; | |
323 | ||
324 | // check pcmpos flag | |
325 | if(op_de.granulepos!=-1){ | |
326 | System.err.print(" pcm:"+op_de.granulepos+" "); | |
327 | } | |
328 | } | |
329 | } | |
330 | } | |
331 | } | |
332 | } | |
333 | } | |
334 | //free(data); | |
335 | if(headers[pageno]!=null){ | |
336 | System.err.println("did not write last page!"); | |
337 | System.exit(1); | |
338 | } | |
339 | if(headers[pageout]!=null){ | |
340 | System.err.println("did not decode last page!"); | |
341 | System.exit(1); | |
342 | } | |
343 | if(inptr!=outptr){ | |
344 | System.err.println("encoded page data incomplete!"); | |
345 | System.exit(1); | |
346 | } | |
347 | if(inptr!=deptr){ | |
348 | System.err.println("decoded page data incomplete!"); | |
349 | System.exit(1); | |
350 | } | |
351 | if(inptr!=depacket){ | |
352 | System.err.println("decoded packet data incomplete!"); | |
353 | System.exit(1); | |
354 | } | |
355 | if(eosflag==0){ | |
356 | System.err.println("Never got a packet with EOS set!"); | |
357 | } | |
358 | System.err.println("ok."); | |
359 | } | |
360 | ||
361 | static void error(){ | |
362 | System.err.println("error!"); | |
363 | System.exit(1); | |
364 | } | |
365 | public static void main(String[] arg){ | |
366 | ||
367 | os_en=new StreamState(0x04030201); | |
368 | os_de=new StreamState(0x04030201); | |
369 | ||
370 | oy=new SyncState(); | |
371 | ||
372 | // Exercise each code path in the framing code. Also verify that | |
373 | // the checksums are working. | |
374 | ||
375 | { | |
376 | // 17 only | |
377 | int[] packets={17, -1}; | |
378 | int[] head1={0x4f,0x67,0x67,0x53,0,0x06, | |
379 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
380 | 0x01,0x02,0x03,0x04,0,0,0,0, | |
381 | 0x15,0xed,0xec,0x91, | |
382 | 1, | |
383 | 17}; | |
384 | int[][] headret={head1, null}; | |
385 | ||
386 | System.err.print("testing single page encoding... "); | |
387 | test_pack(packets,headret); | |
388 | } | |
389 | ||
390 | { | |
391 | // 17, 254, 255, 256, 500, 510, 600 byte, pad | |
392 | int[] packets={17, 254, 255, 256, 500, 510, 600, -1}; | |
393 | int[] head1={0x4f,0x67,0x67,0x53,0,0x02, | |
394 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
395 | 0x01,0x02,0x03,0x04,0,0,0,0, | |
396 | 0x59,0x10,0x6c,0x2c, | |
397 | 1, | |
398 | 17}; | |
399 | int[] head2={0x4f,0x67,0x67,0x53,0,0x04, | |
400 | 0x07,0x18,0x00,0x00,0x00,0x00,0x00,0x00, | |
401 | 0x01,0x02,0x03,0x04,1,0,0,0, | |
402 | 0x89,0x33,0x85,0xce, | |
403 | 13, | |
404 | 254,255,0,255,1,255,245,255,255,0, | |
405 | 255,255,90}; | |
406 | int[][] headret={head1,head2,null}; | |
407 | ||
408 | System.err.print("testing basic page encoding... "); | |
409 | test_pack(packets,headret); | |
410 | } | |
411 | ||
412 | { | |
413 | // nil packets; beginning,middle,end | |
414 | int[] packets={0,17, 254, 255, 0, 256, 0, 500, 510, 600, 0, -1}; | |
415 | ||
416 | int[] head1={0x4f,0x67,0x67,0x53,0,0x02, | |
417 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
418 | 0x01,0x02,0x03,0x04,0,0,0,0, | |
419 | 0xff,0x7b,0x23,0x17, | |
420 | 1, | |
421 | 0}; | |
422 | int[] head2={0x4f,0x67,0x67,0x53,0,0x04, | |
423 | 0x07,0x28,0x00,0x00,0x00,0x00,0x00,0x00, | |
424 | 0x01,0x02,0x03,0x04,1,0,0,0, | |
425 | 0x5c,0x3f,0x66,0xcb, | |
426 | 17, | |
427 | 17,254,255,0,0,255,1,0,255,245,255,255,0, | |
428 | 255,255,90,0}; | |
429 | int[][] headret={head1,head2,null}; | |
430 | ||
431 | System.err.print("testing basic nil packets... "); | |
432 | test_pack(packets,headret); | |
433 | } | |
434 | ||
435 | { | |
436 | // large initial packet | |
437 | int[] packets={4345,259,255,-1}; | |
438 | ||
439 | int[] head1={0x4f,0x67,0x67,0x53,0,0x02, | |
440 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
441 | 0x01,0x02,0x03,0x04,0,0,0,0, | |
442 | 0x01,0x27,0x31,0xaa, | |
443 | 18, | |
444 | 255,255,255,255,255,255,255,255, | |
445 | 255,255,255,255,255,255,255,255,255,10}; | |
446 | ||
447 | int[] head2={0x4f,0x67,0x67,0x53,0,0x04, | |
448 | 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00, | |
449 | 0x01,0x02,0x03,0x04,1,0,0,0, | |
450 | 0x7f,0x4e,0x8a,0xd2, | |
451 | 4, | |
452 | 255,4,255,0}; | |
453 | int[][] headret={head1,head2,null}; | |
454 | ||
455 | System.err.print("testing initial-packet lacing > 4k... "); | |
456 | test_pack(packets,headret); | |
457 | } | |
458 | ||
459 | { | |
460 | // continuing packet test | |
461 | int[] packets={0,4345,259,255,-1}; | |
462 | ||
463 | int[] head1={0x4f,0x67,0x67,0x53,0,0x02, | |
464 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
465 | 0x01,0x02,0x03,0x04,0,0,0,0, | |
466 | 0xff,0x7b,0x23,0x17, | |
467 | 1, | |
468 | 0}; | |
469 | ||
470 | int[] head2={0x4f,0x67,0x67,0x53,0,0x00, | |
471 | 0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
472 | 0x01,0x02,0x03,0x04,1,0,0,0, | |
473 | 0x34,0x24,0xd5,0x29, | |
474 | 17, | |
475 | 255,255,255,255,255,255,255,255, | |
476 | 255,255,255,255,255,255,255,255,255}; | |
477 | ||
478 | int[] head3={0x4f,0x67,0x67,0x53,0,0x05, | |
479 | 0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, | |
480 | 0x01,0x02,0x03,0x04,2,0,0,0, | |
481 | 0xc8,0xc3,0xcb,0xed, | |
482 | 5, | |
483 | 10,255,4,255,0}; | |
484 | int[][] headret={head1,head2,head3,null}; | |
485 | ||
486 | System.err.print("testing single packet page span... "); | |
487 | test_pack(packets,headret); | |
488 | } | |
489 | ||
490 | // page with the 255 segment limit | |
491 | { | |
492 | ||
493 | int[] packets={0,10,10,10,10,10,10,10,10, | |
494 | 10,10,10,10,10,10,10,10, | |
495 | 10,10,10,10,10,10,10,10, | |
496 | 10,10,10,10,10,10,10,10, | |
497 | 10,10,10,10,10,10,10,10, | |
498 | 10,10,10,10,10,10,10,10, | |
499 | 10,10,10,10,10,10,10,10, | |
500 | 10,10,10,10,10,10,10,10, | |
501 | 10,10,10,10,10,10,10,10, | |
502 | 10,10,10,10,10,10,10,10, | |
503 | 10,10,10,10,10,10,10,10, | |
504 | 10,10,10,10,10,10,10,10, | |
505 | 10,10,10,10,10,10,10,10, | |
506 | 10,10,10,10,10,10,10,10, | |
507 | 10,10,10,10,10,10,10,10, | |
508 | 10,10,10,10,10,10,10,10, | |
509 | 10,10,10,10,10,10,10,10, | |
510 | 10,10,10,10,10,10,10,10, | |
511 | 10,10,10,10,10,10,10,10, | |
512 | 10,10,10,10,10,10,10,10, | |
513 | 10,10,10,10,10,10,10,10, | |
514 | 10,10,10,10,10,10,10,10, | |
515 | 10,10,10,10,10,10,10,10, | |
516 | 10,10,10,10,10,10,10,10, | |
517 | 10,10,10,10,10,10,10,10, | |
518 | 10,10,10,10,10,10,10,10, | |
519 | 10,10,10,10,10,10,10,10, | |
520 | 10,10,10,10,10,10,10,10, | |
521 | 10,10,10,10,10,10,10,10, | |
522 | 10,10,10,10,10,10,10,10, | |
523 | 10,10,10,10,10,10,10,10, | |
524 | 10,10,10,10,10,10,10,50,-1}; | |
525 | ||
526 | int[] head1={0x4f,0x67,0x67,0x53,0,0x02, | |
527 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
528 | 0x01,0x02,0x03,0x04,0,0,0,0, | |
529 | 0xff,0x7b,0x23,0x17, | |
530 | 1, | |
531 | 0}; | |
532 | ||
533 | int[] head2={0x4f,0x67,0x67,0x53,0,0x00, | |
534 | 0x07,0xfc,0x03,0x00,0x00,0x00,0x00,0x00, | |
535 | 0x01,0x02,0x03,0x04,1,0,0,0, | |
536 | 0xed,0x2a,0x2e,0xa7, | |
537 | 255, | |
538 | 10,10,10,10,10,10,10,10, | |
539 | 10,10,10,10,10,10,10,10, | |
540 | 10,10,10,10,10,10,10,10, | |
541 | 10,10,10,10,10,10,10,10, | |
542 | 10,10,10,10,10,10,10,10, | |
543 | 10,10,10,10,10,10,10,10, | |
544 | 10,10,10,10,10,10,10,10, | |
545 | 10,10,10,10,10,10,10,10, | |
546 | 10,10,10,10,10,10,10,10, | |
547 | 10,10,10,10,10,10,10,10, | |
548 | 10,10,10,10,10,10,10,10, | |
549 | 10,10,10,10,10,10,10,10, | |
550 | 10,10,10,10,10,10,10,10, | |
551 | 10,10,10,10,10,10,10,10, | |
552 | 10,10,10,10,10,10,10,10, | |
553 | 10,10,10,10,10,10,10,10, | |
554 | 10,10,10,10,10,10,10,10, | |
555 | 10,10,10,10,10,10,10,10, | |
556 | 10,10,10,10,10,10,10,10, | |
557 | 10,10,10,10,10,10,10,10, | |
558 | 10,10,10,10,10,10,10,10, | |
559 | 10,10,10,10,10,10,10,10, | |
560 | 10,10,10,10,10,10,10,10, | |
561 | 10,10,10,10,10,10,10,10, | |
562 | 10,10,10,10,10,10,10,10, | |
563 | 10,10,10,10,10,10,10,10, | |
564 | 10,10,10,10,10,10,10,10, | |
565 | 10,10,10,10,10,10,10,10, | |
566 | 10,10,10,10,10,10,10,10, | |
567 | 10,10,10,10,10,10,10,10, | |
568 | 10,10,10,10,10,10,10,10, | |
569 | 10,10,10,10,10,10,10}; | |
570 | ||
571 | int[] head3={0x4f,0x67,0x67,0x53,0,0x04, | |
572 | 0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00, | |
573 | 0x01,0x02,0x03,0x04,2,0,0,0, | |
574 | 0x6c,0x3b,0x82,0x3d, | |
575 | 1, | |
576 | 50}; | |
577 | int[][] headret={head1,head2,head3,null}; | |
578 | ||
579 | System.err.print("testing max packet segments... "); | |
580 | test_pack(packets,headret); | |
581 | } | |
582 | ||
583 | { | |
584 | // packet that overspans over an entire page | |
585 | ||
586 | int[] packets={0,100,9000,259,255,-1}; | |
587 | ||
588 | int[] head1={0x4f,0x67,0x67,0x53,0,0x02, | |
589 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
590 | 0x01,0x02,0x03,0x04,0,0,0,0, | |
591 | 0xff,0x7b,0x23,0x17, | |
592 | 1, | |
593 | 0}; | |
594 | ||
595 | int[] head2={0x4f,0x67,0x67,0x53,0,0x00, | |
596 | 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, | |
597 | 0x01,0x02,0x03,0x04,1,0,0,0, | |
598 | 0x3c,0xd9,0x4d,0x3f, | |
599 | 17, | |
600 | 100,255,255,255,255,255,255,255,255, | |
601 | 255,255,255,255,255,255,255,255}; | |
602 | ||
603 | int[] head3={0x4f,0x67,0x67,0x53,0,0x01, | |
604 | 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, | |
605 | 0x01,0x02,0x03,0x04,2,0,0,0, | |
606 | 0xbd,0xd5,0xb5,0x8b, | |
607 | 17, | |
608 | 255,255,255,255,255,255,255,255, | |
609 | 255,255,255,255,255,255,255,255,255}; | |
610 | ||
611 | int[] head4={0x4f,0x67,0x67,0x53,0,0x05, | |
612 | 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00, | |
613 | 0x01,0x02,0x03,0x04,3,0,0,0, | |
614 | 0xef,0xdd,0x88,0xde, | |
615 | 7, | |
616 | 255,255,75,255,4,255,0}; | |
617 | int[][] headret={head1,head2,head3,head4,null}; | |
618 | ||
619 | System.err.print("testing very large packets... "); | |
620 | test_pack(packets,headret); | |
621 | } | |
622 | ||
623 | { | |
624 | // term only page. why not? | |
625 | ||
626 | int[] packets={0,100,4080,-1}; | |
627 | ||
628 | int[] head1={0x4f,0x67,0x67,0x53,0,0x02, | |
629 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
630 | 0x01,0x02,0x03,0x04,0,0,0,0, | |
631 | 0xff,0x7b,0x23,0x17, | |
632 | 1, | |
633 | 0}; | |
634 | ||
635 | int[] head2={0x4f,0x67,0x67,0x53,0,0x00, | |
636 | 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, | |
637 | 0x01,0x02,0x03,0x04,1,0,0,0, | |
638 | 0x3c,0xd9,0x4d,0x3f, | |
639 | 17, | |
640 | 100,255,255,255,255,255,255,255,255, | |
641 | 255,255,255,255,255,255,255,255}; | |
642 | ||
643 | int[] head3={0x4f,0x67,0x67,0x53,0,0x05, | |
644 | 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00, | |
645 | 0x01,0x02,0x03,0x04,2,0,0,0, | |
646 | 0xd4,0xe0,0x60,0xe5, | |
647 | 1,0}; | |
648 | ||
649 | int[][] headret={head1,head2,head3,null}; | |
650 | ||
651 | System.err.print("testing zero data page (1 nil packet)... "); | |
652 | test_pack(packets,headret); | |
653 | } | |
654 | ||
655 | { | |
656 | // build a bunch of pages for testing | |
657 | byte[] data=new byte[1024*1024]; | |
658 | int[] pl={0,100,4079,2956,2057,76,34,912,0,234,1000,1000,1000,300,-1}; | |
659 | int inptr=0; | |
660 | Page[] og=new Page[5]; | |
661 | for(int i=0; i<5; i++){ | |
662 | og[i]=new Page(); | |
663 | } | |
664 | ||
665 | os_en.reset(); | |
666 | ||
667 | for(int i=0;pl[i]!=-1;i++){ | |
668 | Packet op=new Packet(); | |
669 | int len=pl[i]; | |
670 | ||
671 | op.packet_base=data; | |
672 | op.packet=inptr; | |
673 | op.bytes=len; | |
674 | op.e_o_s=(pl[i+1]<0?1:0); | |
675 | op.granulepos=(i+1)*1000; | |
676 | ||
677 | for(int j=0;j<len;j++)data[inptr++]=(byte)(i+j); | |
678 | os_en.packetin(op); | |
679 | } | |
680 | ||
681 | // free(data); | |
682 | ||
683 | // retrieve finished pages | |
684 | for(int i=0;i<5;i++){ | |
685 | if(os_en.pageout(og[i])==0){ | |
686 | System.err.print("Too few pages output building sync tests!\n"); | |
687 | System.exit(1); | |
688 | } | |
689 | og[i].copy_page(); | |
690 | } | |
691 | ||
692 | // Test lost pages on pagein/packetout: no rollback | |
693 | { | |
694 | Page temp=new Page(); | |
695 | Packet test=new Packet(); | |
696 | ||
697 | System.err.print("Testing loss of pages... "); | |
698 | ||
699 | oy.reset(); | |
700 | os_de.reset(); | |
701 | for(int i=0;i<5;i++){ | |
702 | int index=oy.buffer(og[i].header_len); | |
703 | System.arraycopy(og[i].header_base, og[i].header, | |
704 | oy.data, index, og[i].header_len); | |
705 | oy.wrote(og[i].header_len); | |
706 | index=oy.buffer(og[i].body_len); | |
707 | System.arraycopy(og[i].body_base, og[i].body, | |
708 | oy.data, index, og[i].body_len); | |
709 | oy.wrote(og[i].body_len); | |
710 | } | |
711 | ||
712 | oy.pageout(temp); | |
713 | os_de.pagein(temp); | |
714 | oy.pageout(temp); | |
715 | os_de.pagein(temp); | |
716 | oy.pageout(temp); | |
717 | ||
718 | // skip | |
719 | oy.pageout(temp); | |
720 | os_de.pagein(temp); | |
721 | ||
722 | // do we get the expected results/packets? | |
723 | ||
724 | if(os_de.packetout(test)!=1)error(); | |
725 | test.checkpacket(0,0,0); | |
726 | if(os_de.packetout(test)!=1)error(); | |
727 | test.checkpacket(100,1,-1); | |
728 | if(os_de.packetout(test)!=1)error(); | |
729 | test.checkpacket(4079,2,3000); | |
730 | if(os_de.packetout(test)!=-1){ | |
731 | System.err.println("Error: loss of page did not return error"); | |
732 | System.exit(1); | |
733 | } | |
734 | if(os_de.packetout(test)!=1)error(); | |
735 | test.checkpacket(76,5,-1); | |
736 | if(os_de.packetout(test)!=1)error(); | |
737 | test.checkpacket(34,6,-1); | |
738 | System.err.println("ok."); | |
739 | } | |
740 | ||
741 | // Test lost pages on pagein/packetout: rollback with continuation | |
742 | { | |
743 | Page temp=new Page(); | |
744 | Packet test=new Packet(); | |
745 | ||
746 | System.err.print("Testing loss of pages (rollback required)... "); | |
747 | ||
748 | oy.reset(); | |
749 | os_de.reset(); | |
750 | for(int i=0;i<5;i++){ | |
751 | int index=oy.buffer(og[i].header_len); | |
752 | System.arraycopy(og[i].header_base, og[i].header, | |
753 | oy.data, index, og[i].header_len); | |
754 | oy.wrote(og[i].header_len); | |
755 | index=oy.buffer(og[i].body_len); | |
756 | System.arraycopy(og[i].body_base, og[i].body, | |
757 | oy.data, index, og[i].body_len); | |
758 | oy.wrote(og[i].body_len); | |
759 | } | |
760 | ||
761 | oy.pageout(temp); | |
762 | os_de.pagein(temp); | |
763 | oy.pageout(temp); | |
764 | os_de.pagein(temp); | |
765 | oy.pageout(temp); | |
766 | os_de.pagein(temp); | |
767 | oy.pageout(temp); | |
768 | // skip | |
769 | oy.pageout(temp); | |
770 | os_de.pagein(temp); | |
771 | ||
772 | // do we get the expected results/packets? | |
773 | ||
774 | if(os_de.packetout(test)!=1)error(); | |
775 | test.checkpacket(0,0,0); | |
776 | if(os_de.packetout(test)!=1)error(); | |
777 | test.checkpacket(100,1,-1); | |
778 | if(os_de.packetout(test)!=1)error(); | |
779 | test.checkpacket(4079,2,3000); | |
780 | if(os_de.packetout(test)!=1)error(); | |
781 | test.checkpacket(2956,3,4000); | |
782 | if(os_de.packetout(test)!=-1){ | |
783 | System.err.println("Error: loss of page did not return error"); | |
784 | System.exit(1); | |
785 | } | |
786 | if(os_de.packetout(test)!=1)error(); | |
787 | test.checkpacket(300,13,14000); | |
788 | System.err.println("ok."); | |
789 | } | |
790 | ||
791 | // the rest only test sync | |
792 | { | |
793 | Page og_de=new Page(); | |
794 | // Test fractional page inputs: incomplete capture | |
795 | System.err.print("Testing sync on partial inputs... "); | |
796 | oy.reset(); | |
797 | int index=oy.buffer(og[1].header_len); | |
798 | System.arraycopy(og[1].header_base, og[1].header, | |
799 | oy.data, index, 3); | |
800 | oy.wrote(3); | |
801 | if(oy.pageout(og_de)>0)error(); | |
802 | ||
803 | // Test fractional page inputs: incomplete fixed header | |
804 | index=oy.buffer(og[1].header_len); | |
805 | System.arraycopy(og[1].header_base, og[1].header+3, | |
806 | oy.data, index, 20); | |
807 | ||
808 | oy.wrote(20); | |
809 | if(oy.pageout(og_de)>0)error(); | |
810 | ||
811 | // Test fractional page inputs: incomplete header | |
812 | index=oy.buffer(og[1].header_len); | |
813 | System.arraycopy(og[1].header_base, og[1].header+23, | |
814 | oy.data, index, 5); | |
815 | oy.wrote(5); | |
816 | if(oy.pageout(og_de)>0)error(); | |
817 | ||
818 | // Test fractional page inputs: incomplete body | |
819 | index=oy.buffer(og[1].header_len); | |
820 | System.arraycopy(og[1].header_base, og[1].header+28, | |
821 | oy.data, index, og[1].header_len-28); | |
822 | oy.wrote(og[1].header_len-28); | |
823 | if(oy.pageout(og_de)>0)error(); | |
824 | ||
825 | index=oy.buffer(og[1].body_len); | |
826 | System.arraycopy(og[1].body_base, og[1].body, | |
827 | oy.data, index, 1000); | |
828 | oy.wrote(1000); | |
829 | if(oy.pageout(og_de)>0)error(); | |
830 | ||
831 | index=oy.buffer(og[1].body_len); | |
832 | System.arraycopy(og[1].body_base, og[1].body+1000, | |
833 | oy.data, index, og[1].body_len-1000); | |
834 | oy.wrote(og[1].body_len-1000); | |
835 | if(oy.pageout(og_de)<=0)error(); | |
836 | System.err.println("ok."); | |
837 | } | |
838 | ||
839 | // Test fractional page inputs: page + incomplete capture | |
840 | { | |
841 | Page og_de=new Page(); | |
842 | System.err.print("Testing sync on 1+partial inputs... "); | |
843 | oy.reset(); | |
844 | ||
845 | int index=oy.buffer(og[1].header_len); | |
846 | System.arraycopy(og[1].header_base, og[1].header, | |
847 | oy.data, index, og[1].header_len); | |
848 | oy.wrote(og[1].header_len); | |
849 | ||
850 | index=oy.buffer(og[1].body_len); | |
851 | System.arraycopy(og[1].body_base, og[1].body, | |
852 | oy.data, index, og[1].body_len); | |
853 | oy.wrote(og[1].body_len); | |
854 | ||
855 | index=oy.buffer(og[1].header_len); | |
856 | System.arraycopy(og[1].header_base, og[1].header, | |
857 | oy.data, index, 20); | |
858 | oy.wrote(20); | |
859 | if(oy.pageout(og_de)<=0)error(); | |
860 | if(oy.pageout(og_de)>0)error(); | |
861 | ||
862 | index=oy.buffer(og[1].header_len); | |
863 | System.arraycopy(og[1].header_base, og[1].header+20, | |
864 | oy.data, index, og[1].header_len-20); | |
865 | oy.wrote(og[1].header_len-20); | |
866 | index=oy.buffer(og[1].body_len); | |
867 | System.arraycopy(og[1].body_base, og[1].body, | |
868 | oy.data, index, og[1].body_len); | |
869 | ||
870 | oy.wrote(og[1].body_len); | |
871 | if(oy.pageout(og_de)<=0)error(); | |
872 | ||
873 | System.err.println("ok."); | |
874 | } | |
875 | ||
876 | // // // // // // // // // | |
877 | // Test recapture: garbage + page | |
878 | { | |
879 | Page og_de=new Page(); | |
880 | System.err.print("Testing search for capture... "); | |
881 | oy.reset(); | |
882 | ||
883 | // 'garbage' | |
884 | int index=oy.buffer(og[1].body_len); | |
885 | System.arraycopy(og[1].body_base, og[1].body, | |
886 | oy.data, index, og[1].body_len); | |
887 | oy.wrote(og[1].body_len); | |
888 | ||
889 | index=oy.buffer(og[1].header_len); | |
890 | System.arraycopy(og[1].header_base, og[1].header, | |
891 | oy.data, index, og[1].header_len); | |
892 | oy.wrote(og[1].header_len); | |
893 | ||
894 | index=oy.buffer(og[1].body_len); | |
895 | System.arraycopy(og[1].body_base, og[1].body, | |
896 | oy.data, index, og[1].body_len); | |
897 | oy.wrote(og[1].body_len); | |
898 | ||
899 | index=oy.buffer(og[2].header_len); | |
900 | System.arraycopy(og[2].header_base, og[2].header, | |
901 | oy.data, index, 20); | |
902 | ||
903 | oy.wrote(20); | |
904 | if(oy.pageout(og_de)>0)error(); | |
905 | if(oy.pageout(og_de)<=0)error(); | |
906 | if(oy.pageout(og_de)>0)error(); | |
907 | ||
908 | index=oy.buffer(og[2].header_len); | |
909 | System.arraycopy(og[2].header_base, og[2].header+20, | |
910 | oy.data, index, og[2].header_len-20); | |
911 | oy.wrote(og[2].header_len-20); | |
912 | index=oy.buffer(og[2].body_len); | |
913 | System.arraycopy(og[2].body_base, og[2].body, | |
914 | oy.data, index, og[2].body_len); | |
915 | oy.wrote(og[2].body_len); | |
916 | if(oy.pageout(og_de)<=0)error(); | |
917 | ||
918 | System.err.println("ok."); | |
919 | } | |
920 | ||
921 | // Test recapture: page + garbage + page | |
922 | { | |
923 | Page og_de=new Page(); | |
924 | System.err.print("Testing recapture... "); | |
925 | oy.reset(); | |
926 | ||
927 | int index=oy.buffer(og[1].header_len); | |
928 | System.arraycopy(og[1].header_base, og[1].header, | |
929 | oy.data, index, og[1].header_len); | |
930 | oy.wrote(og[1].header_len); | |
931 | ||
932 | index=oy.buffer(og[1].body_len); | |
933 | System.arraycopy(og[1].body_base, og[1].body, | |
934 | oy.data, index, og[1].body_len); | |
935 | oy.wrote(og[1].body_len); | |
936 | ||
937 | index=oy.buffer(og[2].header_len); | |
938 | System.arraycopy(og[2].header_base, og[2].header, | |
939 | oy.data, index, og[2].header_len); | |
940 | oy.wrote(og[2].header_len); | |
941 | ||
942 | index=oy.buffer(og[2].header_len); | |
943 | System.arraycopy(og[2].header_base, og[2].header, | |
944 | oy.data, index, og[2].header_len); | |
945 | oy.wrote(og[2].header_len); | |
946 | ||
947 | if(oy.pageout(og_de)<=0)error(); | |
948 | ||
949 | index=oy.buffer(og[2].body_len); | |
950 | System.arraycopy(og[2].body_base, og[2].body, | |
951 | oy.data, index, og[2].body_len-5); | |
952 | oy.wrote(og[2].body_len-5); | |
953 | ||
954 | index=oy.buffer(og[3].header_len); | |
955 | System.arraycopy(og[3].header_base, og[3].header, | |
956 | oy.data, index, og[3].header_len); | |
957 | oy.wrote(og[3].header_len); | |
958 | ||
959 | index=oy.buffer(og[3].body_len); | |
960 | System.arraycopy(og[3].body_base, og[3].body, | |
961 | oy.data, index, og[3].body_len); | |
962 | oy.wrote(og[3].body_len); | |
963 | ||
964 | if(oy.pageout(og_de)>0)error(); | |
965 | if(oy.pageout(og_de)<=0)error(); | |
966 | ||
967 | System.err.println("ok."); | |
968 | } | |
969 | } | |
970 | //return(0); | |
971 | } | |
972 | */ | |
973 | } |