/* -*-mode:java; c-basic-offset:2; -*- */ /* JOrbis * Copyright (C) 2000 ymnk, JCraft,Inc. * * Written by: 2000 ymnk * * Many thanks to * Monty and * The XIPHOPHORUS Company http://www.xiph.org/ . * JOrbis has been based on their awesome works, Vorbis codec. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License * as published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package com.jcraft.jogg; public class Page{ private static int[] crc_lookup=new int[256]; static { for(int i=0; i>>24)&0xff)^(header_base[header+i]&0xff)]; } for(int i=0;i>>24)&0xff)^(body_base[body+i]&0xff)]; } header_base[header+22]=(byte)crc_reg/*&0xff*/; header_base[header+23]=(byte)(crc_reg>>>8)/*&0xff*/; header_base[header+24]=(byte)(crc_reg>>>16)/*&0xff*/; header_base[header+25]=(byte)(crc_reg>>>24)/*&0xff*/; } public Page copy(){ return copy(new Page()); } public Page copy(Page p){ byte[] tmp=new byte[header_len]; System.arraycopy(header_base, header, tmp, 0, header_len); p.header_len=header_len; p.header_base=tmp; p.header=0; tmp=new byte[body_len]; System.arraycopy(body_base, body, tmp, 0, body_len); p.body_len=body_len; p.body_base=tmp; p.body=0; return p; } /* // TEST static StreamState os_en, os_de; static SyncState oy; void check_page(byte[] data_base, int data, int[] _header){ // Test data for(int j=0;j 4k... "); test_pack(packets,headret); } { // continuing packet test int[] packets={0,4345,259,255,-1}; int[] head1={0x4f,0x67,0x67,0x53,0,0x02, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,0,0,0,0, 0xff,0x7b,0x23,0x17, 1, 0}; int[] head2={0x4f,0x67,0x67,0x53,0,0x00, 0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,1,0,0,0, 0x34,0x24,0xd5,0x29, 17, 255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255}; int[] head3={0x4f,0x67,0x67,0x53,0,0x05, 0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,2,0,0,0, 0xc8,0xc3,0xcb,0xed, 5, 10,255,4,255,0}; int[][] headret={head1,head2,head3,null}; System.err.print("testing single packet page span... "); test_pack(packets,headret); } // page with the 255 segment limit { int[] packets={0,10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,50,-1}; int[] head1={0x4f,0x67,0x67,0x53,0,0x02, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,0,0,0,0, 0xff,0x7b,0x23,0x17, 1, 0}; int[] head2={0x4f,0x67,0x67,0x53,0,0x00, 0x07,0xfc,0x03,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,1,0,0,0, 0xed,0x2a,0x2e,0xa7, 255, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10,10, 10,10,10,10,10,10,10}; int[] head3={0x4f,0x67,0x67,0x53,0,0x04, 0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,2,0,0,0, 0x6c,0x3b,0x82,0x3d, 1, 50}; int[][] headret={head1,head2,head3,null}; System.err.print("testing max packet segments... "); test_pack(packets,headret); } { // packet that overspans over an entire page int[] packets={0,100,9000,259,255,-1}; int[] head1={0x4f,0x67,0x67,0x53,0,0x02, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,0,0,0,0, 0xff,0x7b,0x23,0x17, 1, 0}; int[] head2={0x4f,0x67,0x67,0x53,0,0x00, 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,1,0,0,0, 0x3c,0xd9,0x4d,0x3f, 17, 100,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255}; int[] head3={0x4f,0x67,0x67,0x53,0,0x01, 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,2,0,0,0, 0xbd,0xd5,0xb5,0x8b, 17, 255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255,255}; int[] head4={0x4f,0x67,0x67,0x53,0,0x05, 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,3,0,0,0, 0xef,0xdd,0x88,0xde, 7, 255,255,75,255,4,255,0}; int[][] headret={head1,head2,head3,head4,null}; System.err.print("testing very large packets... "); test_pack(packets,headret); } { // term only page. why not? int[] packets={0,100,4080,-1}; int[] head1={0x4f,0x67,0x67,0x53,0,0x02, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,0,0,0,0, 0xff,0x7b,0x23,0x17, 1, 0}; int[] head2={0x4f,0x67,0x67,0x53,0,0x00, 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,1,0,0,0, 0x3c,0xd9,0x4d,0x3f, 17, 100,255,255,255,255,255,255,255,255, 255,255,255,255,255,255,255,255}; int[] head3={0x4f,0x67,0x67,0x53,0,0x05, 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00, 0x01,0x02,0x03,0x04,2,0,0,0, 0xd4,0xe0,0x60,0xe5, 1,0}; int[][] headret={head1,head2,head3,null}; System.err.print("testing zero data page (1 nil packet)... "); test_pack(packets,headret); } { // build a bunch of pages for testing byte[] data=new byte[1024*1024]; int[] pl={0,100,4079,2956,2057,76,34,912,0,234,1000,1000,1000,300,-1}; int inptr=0; Page[] og=new Page[5]; for(int i=0; i<5; i++){ og[i]=new Page(); } os_en.reset(); for(int i=0;pl[i]!=-1;i++){ Packet op=new Packet(); int len=pl[i]; op.packet_base=data; op.packet=inptr; op.bytes=len; op.e_o_s=(pl[i+1]<0?1:0); op.granulepos=(i+1)*1000; for(int j=0;j0)error(); // Test fractional page inputs: incomplete fixed header index=oy.buffer(og[1].header_len); System.arraycopy(og[1].header_base, og[1].header+3, oy.data, index, 20); oy.wrote(20); if(oy.pageout(og_de)>0)error(); // Test fractional page inputs: incomplete header index=oy.buffer(og[1].header_len); System.arraycopy(og[1].header_base, og[1].header+23, oy.data, index, 5); oy.wrote(5); if(oy.pageout(og_de)>0)error(); // Test fractional page inputs: incomplete body index=oy.buffer(og[1].header_len); System.arraycopy(og[1].header_base, og[1].header+28, oy.data, index, og[1].header_len-28); oy.wrote(og[1].header_len-28); if(oy.pageout(og_de)>0)error(); index=oy.buffer(og[1].body_len); System.arraycopy(og[1].body_base, og[1].body, oy.data, index, 1000); oy.wrote(1000); if(oy.pageout(og_de)>0)error(); index=oy.buffer(og[1].body_len); System.arraycopy(og[1].body_base, og[1].body+1000, oy.data, index, og[1].body_len-1000); oy.wrote(og[1].body_len-1000); if(oy.pageout(og_de)<=0)error(); System.err.println("ok."); } // Test fractional page inputs: page + incomplete capture { Page og_de=new Page(); System.err.print("Testing sync on 1+partial inputs... "); oy.reset(); int index=oy.buffer(og[1].header_len); System.arraycopy(og[1].header_base, og[1].header, oy.data, index, og[1].header_len); oy.wrote(og[1].header_len); index=oy.buffer(og[1].body_len); System.arraycopy(og[1].body_base, og[1].body, oy.data, index, og[1].body_len); oy.wrote(og[1].body_len); index=oy.buffer(og[1].header_len); System.arraycopy(og[1].header_base, og[1].header, oy.data, index, 20); oy.wrote(20); if(oy.pageout(og_de)<=0)error(); if(oy.pageout(og_de)>0)error(); index=oy.buffer(og[1].header_len); System.arraycopy(og[1].header_base, og[1].header+20, oy.data, index, og[1].header_len-20); oy.wrote(og[1].header_len-20); index=oy.buffer(og[1].body_len); System.arraycopy(og[1].body_base, og[1].body, oy.data, index, og[1].body_len); oy.wrote(og[1].body_len); if(oy.pageout(og_de)<=0)error(); System.err.println("ok."); } // // // // // // // // // // Test recapture: garbage + page { Page og_de=new Page(); System.err.print("Testing search for capture... "); oy.reset(); // 'garbage' int index=oy.buffer(og[1].body_len); System.arraycopy(og[1].body_base, og[1].body, oy.data, index, og[1].body_len); oy.wrote(og[1].body_len); index=oy.buffer(og[1].header_len); System.arraycopy(og[1].header_base, og[1].header, oy.data, index, og[1].header_len); oy.wrote(og[1].header_len); index=oy.buffer(og[1].body_len); System.arraycopy(og[1].body_base, og[1].body, oy.data, index, og[1].body_len); oy.wrote(og[1].body_len); index=oy.buffer(og[2].header_len); System.arraycopy(og[2].header_base, og[2].header, oy.data, index, 20); oy.wrote(20); if(oy.pageout(og_de)>0)error(); if(oy.pageout(og_de)<=0)error(); if(oy.pageout(og_de)>0)error(); index=oy.buffer(og[2].header_len); System.arraycopy(og[2].header_base, og[2].header+20, oy.data, index, og[2].header_len-20); oy.wrote(og[2].header_len-20); index=oy.buffer(og[2].body_len); System.arraycopy(og[2].body_base, og[2].body, oy.data, index, og[2].body_len); oy.wrote(og[2].body_len); if(oy.pageout(og_de)<=0)error(); System.err.println("ok."); } // Test recapture: page + garbage + page { Page og_de=new Page(); System.err.print("Testing recapture... "); oy.reset(); int index=oy.buffer(og[1].header_len); System.arraycopy(og[1].header_base, og[1].header, oy.data, index, og[1].header_len); oy.wrote(og[1].header_len); index=oy.buffer(og[1].body_len); System.arraycopy(og[1].body_base, og[1].body, oy.data, index, og[1].body_len); oy.wrote(og[1].body_len); index=oy.buffer(og[2].header_len); System.arraycopy(og[2].header_base, og[2].header, oy.data, index, og[2].header_len); oy.wrote(og[2].header_len); index=oy.buffer(og[2].header_len); System.arraycopy(og[2].header_base, og[2].header, oy.data, index, og[2].header_len); oy.wrote(og[2].header_len); if(oy.pageout(og_de)<=0)error(); index=oy.buffer(og[2].body_len); System.arraycopy(og[2].body_base, og[2].body, oy.data, index, og[2].body_len-5); oy.wrote(og[2].body_len-5); index=oy.buffer(og[3].header_len); System.arraycopy(og[3].header_base, og[3].header, oy.data, index, og[3].header_len); oy.wrote(og[3].header_len); index=oy.buffer(og[3].body_len); System.arraycopy(og[3].body_base, og[3].body, oy.data, index, og[3].body_len); oy.wrote(og[3].body_len); if(oy.pageout(og_de)>0)error(); if(oy.pageout(og_de)<=0)error(); System.err.println("ok."); } } //return(0); } */ }