nuclear@1: /******************************************************************** nuclear@1: * * nuclear@1: * THIS FILE IS PART OF THE Ogg CONTAINER SOURCE CODE. * nuclear@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * nuclear@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * nuclear@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * nuclear@1: * * nuclear@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 * nuclear@1: * by the Xiph.Org Foundation http://www.xiph.org/ * nuclear@1: * * nuclear@1: ******************************************************************** nuclear@1: nuclear@1: function: code raw packets into framed OggSquish stream and nuclear@1: decode Ogg streams back into raw packets nuclear@1: last mod: $Id: framing.c 18758 2013-01-08 16:29:56Z tterribe $ nuclear@1: nuclear@1: note: The CRC code is directly derived from public domain code by nuclear@1: Ross Williams (ross@guest.adelaide.edu.au). See docs/framing.html nuclear@1: for details. nuclear@1: nuclear@1: ********************************************************************/ nuclear@1: nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: nuclear@1: /* A complete description of Ogg framing exists in docs/framing.html */ nuclear@1: nuclear@1: int ogg_page_version(const ogg_page *og){ nuclear@1: return((int)(og->header[4])); nuclear@1: } nuclear@1: nuclear@1: int ogg_page_continued(const ogg_page *og){ nuclear@1: return((int)(og->header[5]&0x01)); nuclear@1: } nuclear@1: nuclear@1: int ogg_page_bos(const ogg_page *og){ nuclear@1: return((int)(og->header[5]&0x02)); nuclear@1: } nuclear@1: nuclear@1: int ogg_page_eos(const ogg_page *og){ nuclear@1: return((int)(og->header[5]&0x04)); nuclear@1: } nuclear@1: nuclear@1: ogg_int64_t ogg_page_granulepos(const ogg_page *og){ nuclear@1: unsigned char *page=og->header; nuclear@1: ogg_int64_t granulepos=page[13]&(0xff); nuclear@1: granulepos= (granulepos<<8)|(page[12]&0xff); nuclear@1: granulepos= (granulepos<<8)|(page[11]&0xff); nuclear@1: granulepos= (granulepos<<8)|(page[10]&0xff); nuclear@1: granulepos= (granulepos<<8)|(page[9]&0xff); nuclear@1: granulepos= (granulepos<<8)|(page[8]&0xff); nuclear@1: granulepos= (granulepos<<8)|(page[7]&0xff); nuclear@1: granulepos= (granulepos<<8)|(page[6]&0xff); nuclear@1: return(granulepos); nuclear@1: } nuclear@1: nuclear@1: int ogg_page_serialno(const ogg_page *og){ nuclear@1: return(og->header[14] | nuclear@1: (og->header[15]<<8) | nuclear@1: (og->header[16]<<16) | nuclear@1: (og->header[17]<<24)); nuclear@1: } nuclear@1: nuclear@1: long ogg_page_pageno(const ogg_page *og){ nuclear@1: return(og->header[18] | nuclear@1: (og->header[19]<<8) | nuclear@1: (og->header[20]<<16) | nuclear@1: (og->header[21]<<24)); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: nuclear@1: /* returns the number of packets that are completed on this page (if nuclear@1: the leading packet is begun on a previous page, but ends on this nuclear@1: page, it's counted */ nuclear@1: nuclear@1: /* NOTE: nuclear@1: If a page consists of a packet begun on a previous page, and a new nuclear@1: packet begun (but not completed) on this page, the return will be: nuclear@1: ogg_page_packets(page) ==1, nuclear@1: ogg_page_continued(page) !=0 nuclear@1: nuclear@1: If a page happens to be a single packet that was begun on a nuclear@1: previous page, and spans to the next page (in the case of a three or nuclear@1: more page packet), the return will be: nuclear@1: ogg_page_packets(page) ==0, nuclear@1: ogg_page_continued(page) !=0 nuclear@1: */ nuclear@1: nuclear@1: int ogg_page_packets(const ogg_page *og){ nuclear@1: int i,n=og->header[26],count=0; nuclear@1: for(i=0;iheader[27+i]<255)count++; nuclear@1: return(count); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: #if 0 nuclear@1: /* helper to initialize lookup for direct-table CRC (illustrative; we nuclear@1: use the static init below) */ nuclear@1: nuclear@1: static ogg_uint32_t _ogg_crc_entry(unsigned long index){ nuclear@1: int i; nuclear@1: unsigned long r; nuclear@1: nuclear@1: r = index << 24; nuclear@1: for (i=0; i<8; i++) nuclear@1: if (r & 0x80000000UL) nuclear@1: r = (r << 1) ^ 0x04c11db7; /* The same as the ethernet generator nuclear@1: polynomial, although we use an nuclear@1: unreflected alg and an init/final nuclear@1: of 0, not 0xffffffff */ nuclear@1: else nuclear@1: r<<=1; nuclear@1: return (r & 0xffffffffUL); nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: static const ogg_uint32_t crc_lookup[256]={ nuclear@1: 0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9, nuclear@1: 0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005, nuclear@1: 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61, nuclear@1: 0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd, nuclear@1: 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9, nuclear@1: 0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75, nuclear@1: 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011, nuclear@1: 0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd, nuclear@1: 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039, nuclear@1: 0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5, nuclear@1: 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81, nuclear@1: 0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d, nuclear@1: 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49, nuclear@1: 0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95, nuclear@1: 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1, nuclear@1: 0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d, nuclear@1: 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae, nuclear@1: 0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072, nuclear@1: 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16, nuclear@1: 0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca, nuclear@1: 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde, nuclear@1: 0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02, nuclear@1: 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066, nuclear@1: 0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba, nuclear@1: 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e, nuclear@1: 0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692, nuclear@1: 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6, nuclear@1: 0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a, nuclear@1: 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e, nuclear@1: 0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2, nuclear@1: 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686, nuclear@1: 0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a, nuclear@1: 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637, nuclear@1: 0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb, nuclear@1: 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f, nuclear@1: 0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53, nuclear@1: 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47, nuclear@1: 0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b, nuclear@1: 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff, nuclear@1: 0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623, nuclear@1: 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7, nuclear@1: 0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b, nuclear@1: 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f, nuclear@1: 0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3, nuclear@1: 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7, nuclear@1: 0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b, nuclear@1: 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f, nuclear@1: 0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3, nuclear@1: 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640, nuclear@1: 0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c, nuclear@1: 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8, nuclear@1: 0x68860bfd,0x6c47164a,0x61043093,0x65c52d24, nuclear@1: 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30, nuclear@1: 0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec, nuclear@1: 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088, nuclear@1: 0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654, nuclear@1: 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0, nuclear@1: 0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c, nuclear@1: 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18, nuclear@1: 0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4, nuclear@1: 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0, nuclear@1: 0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c, nuclear@1: 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668, nuclear@1: 0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4}; nuclear@1: nuclear@1: /* init the encode/decode logical stream state */ nuclear@1: nuclear@1: int ogg_stream_init(ogg_stream_state *os,int serialno){ nuclear@1: if(os){ nuclear@1: memset(os,0,sizeof(*os)); nuclear@1: os->body_storage=16*1024; nuclear@1: os->lacing_storage=1024; nuclear@1: nuclear@1: os->body_data=_ogg_malloc(os->body_storage*sizeof(*os->body_data)); nuclear@1: os->lacing_vals=_ogg_malloc(os->lacing_storage*sizeof(*os->lacing_vals)); nuclear@1: os->granule_vals=_ogg_malloc(os->lacing_storage*sizeof(*os->granule_vals)); nuclear@1: nuclear@1: if(!os->body_data || !os->lacing_vals || !os->granule_vals){ nuclear@1: ogg_stream_clear(os); nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: os->serialno=serialno; nuclear@1: nuclear@1: return(0); nuclear@1: } nuclear@1: return(-1); nuclear@1: } nuclear@1: nuclear@1: /* async/delayed error detection for the ogg_stream_state */ nuclear@1: int ogg_stream_check(ogg_stream_state *os){ nuclear@1: if(!os || !os->body_data) return -1; nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: /* _clear does not free os, only the non-flat storage within */ nuclear@1: int ogg_stream_clear(ogg_stream_state *os){ nuclear@1: if(os){ nuclear@1: if(os->body_data)_ogg_free(os->body_data); nuclear@1: if(os->lacing_vals)_ogg_free(os->lacing_vals); nuclear@1: if(os->granule_vals)_ogg_free(os->granule_vals); nuclear@1: nuclear@1: memset(os,0,sizeof(*os)); nuclear@1: } nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: int ogg_stream_destroy(ogg_stream_state *os){ nuclear@1: if(os){ nuclear@1: ogg_stream_clear(os); nuclear@1: _ogg_free(os); nuclear@1: } nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* Helpers for ogg_stream_encode; this keeps the structure and nuclear@1: what's happening fairly clear */ nuclear@1: nuclear@1: static int _os_body_expand(ogg_stream_state *os,long needed){ nuclear@1: if(os->body_storage-needed<=os->body_fill){ nuclear@1: long body_storage; nuclear@1: void *ret; nuclear@1: if(os->body_storage>LONG_MAX-needed){ nuclear@1: ogg_stream_clear(os); nuclear@1: return -1; nuclear@1: } nuclear@1: body_storage=os->body_storage+needed; nuclear@1: if(body_storagebody_data,body_storage*sizeof(*os->body_data)); nuclear@1: if(!ret){ nuclear@1: ogg_stream_clear(os); nuclear@1: return -1; nuclear@1: } nuclear@1: os->body_storage=body_storage; nuclear@1: os->body_data=ret; nuclear@1: } nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: static int _os_lacing_expand(ogg_stream_state *os,long needed){ nuclear@1: if(os->lacing_storage-needed<=os->lacing_fill){ nuclear@1: long lacing_storage; nuclear@1: void *ret; nuclear@1: if(os->lacing_storage>LONG_MAX-needed){ nuclear@1: ogg_stream_clear(os); nuclear@1: return -1; nuclear@1: } nuclear@1: lacing_storage=os->lacing_storage+needed; nuclear@1: if(lacing_storagelacing_vals,lacing_storage*sizeof(*os->lacing_vals)); nuclear@1: if(!ret){ nuclear@1: ogg_stream_clear(os); nuclear@1: return -1; nuclear@1: } nuclear@1: os->lacing_vals=ret; nuclear@1: ret=_ogg_realloc(os->granule_vals,lacing_storage* nuclear@1: sizeof(*os->granule_vals)); nuclear@1: if(!ret){ nuclear@1: ogg_stream_clear(os); nuclear@1: return -1; nuclear@1: } nuclear@1: os->granule_vals=ret; nuclear@1: os->lacing_storage=lacing_storage; nuclear@1: } nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: /* checksum the page */ nuclear@1: /* Direct table CRC; note that this will be faster in the future if we nuclear@1: perform the checksum simultaneously with other copies */ nuclear@1: nuclear@1: void ogg_page_checksum_set(ogg_page *og){ nuclear@1: if(og){ nuclear@1: ogg_uint32_t crc_reg=0; nuclear@1: int i; nuclear@1: nuclear@1: /* safety; needed for API behavior, but not framing code */ nuclear@1: og->header[22]=0; nuclear@1: og->header[23]=0; nuclear@1: og->header[24]=0; nuclear@1: og->header[25]=0; nuclear@1: nuclear@1: for(i=0;iheader_len;i++) nuclear@1: crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->header[i]]; nuclear@1: for(i=0;ibody_len;i++) nuclear@1: crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->body[i]]; nuclear@1: nuclear@1: og->header[22]=(unsigned char)(crc_reg&0xff); nuclear@1: og->header[23]=(unsigned char)((crc_reg>>8)&0xff); nuclear@1: og->header[24]=(unsigned char)((crc_reg>>16)&0xff); nuclear@1: og->header[25]=(unsigned char)((crc_reg>>24)&0xff); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* submit data to the internal buffer of the framing engine */ nuclear@1: int ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov, int count, nuclear@1: long e_o_s, ogg_int64_t granulepos){ nuclear@1: nuclear@1: long bytes = 0, lacing_vals; nuclear@1: int i; nuclear@1: nuclear@1: if(ogg_stream_check(os)) return -1; nuclear@1: if(!iov) return 0; nuclear@1: nuclear@1: for (i = 0; i < count; ++i){ nuclear@1: if(iov[i].iov_len>LONG_MAX) return -1; nuclear@1: if(bytes>LONG_MAX-(long)iov[i].iov_len) return -1; nuclear@1: bytes += (long)iov[i].iov_len; nuclear@1: } nuclear@1: lacing_vals=bytes/255+1; nuclear@1: nuclear@1: if(os->body_returned){ nuclear@1: /* advance packet data according to the body_returned pointer. We nuclear@1: had to keep it around to return a pointer into the buffer last nuclear@1: call */ nuclear@1: nuclear@1: os->body_fill-=os->body_returned; nuclear@1: if(os->body_fill) nuclear@1: memmove(os->body_data,os->body_data+os->body_returned, nuclear@1: os->body_fill); nuclear@1: os->body_returned=0; nuclear@1: } nuclear@1: nuclear@1: /* make sure we have the buffer storage */ nuclear@1: if(_os_body_expand(os,bytes) || _os_lacing_expand(os,lacing_vals)) nuclear@1: return -1; nuclear@1: nuclear@1: /* Copy in the submitted packet. Yes, the copy is a waste; this is nuclear@1: the liability of overly clean abstraction for the time being. It nuclear@1: will actually be fairly easy to eliminate the extra copy in the nuclear@1: future */ nuclear@1: nuclear@1: for (i = 0; i < count; ++i) { nuclear@1: memcpy(os->body_data+os->body_fill, iov[i].iov_base, iov[i].iov_len); nuclear@1: os->body_fill += (int)iov[i].iov_len; nuclear@1: } nuclear@1: nuclear@1: /* Store lacing vals for this packet */ nuclear@1: for(i=0;ilacing_vals[os->lacing_fill+i]=255; nuclear@1: os->granule_vals[os->lacing_fill+i]=os->granulepos; nuclear@1: } nuclear@1: os->lacing_vals[os->lacing_fill+i]=bytes%255; nuclear@1: os->granulepos=os->granule_vals[os->lacing_fill+i]=granulepos; nuclear@1: nuclear@1: /* flag the first segment as the beginning of the packet */ nuclear@1: os->lacing_vals[os->lacing_fill]|= 0x100; nuclear@1: nuclear@1: os->lacing_fill+=lacing_vals; nuclear@1: nuclear@1: /* for the sake of completeness */ nuclear@1: os->packetno++; nuclear@1: nuclear@1: if(e_o_s)os->e_o_s=1; nuclear@1: nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: int ogg_stream_packetin(ogg_stream_state *os,ogg_packet *op){ nuclear@1: ogg_iovec_t iov; nuclear@1: iov.iov_base = op->packet; nuclear@1: iov.iov_len = op->bytes; nuclear@1: return ogg_stream_iovecin(os, &iov, 1, op->e_o_s, op->granulepos); nuclear@1: } nuclear@1: nuclear@1: /* Conditionally flush a page; force==0 will only flush nominal-size nuclear@1: pages, force==1 forces us to flush a page regardless of page size nuclear@1: so long as there's any data available at all. */ nuclear@1: static int ogg_stream_flush_i(ogg_stream_state *os,ogg_page *og, int force, int nfill){ nuclear@1: int i; nuclear@1: int vals=0; nuclear@1: int maxvals=(os->lacing_fill>255?255:os->lacing_fill); nuclear@1: int bytes=0; nuclear@1: long acc=0; nuclear@1: ogg_int64_t granule_pos=-1; nuclear@1: nuclear@1: if(ogg_stream_check(os)) return(0); nuclear@1: if(maxvals==0) return(0); nuclear@1: nuclear@1: /* construct a page */ nuclear@1: /* decide how many segments to include */ nuclear@1: nuclear@1: /* If this is the initial header case, the first page must only include nuclear@1: the initial header packet */ nuclear@1: if(os->b_o_s==0){ /* 'initial header page' case */ nuclear@1: granule_pos=0; nuclear@1: for(vals=0;valslacing_vals[vals]&0x0ff)<255){ nuclear@1: vals++; nuclear@1: break; nuclear@1: } nuclear@1: } nuclear@1: }else{ nuclear@1: nuclear@1: /* The extra packets_done, packet_just_done logic here attempts to do two things: nuclear@1: 1) Don't unneccessarily span pages. nuclear@1: 2) Unless necessary, don't flush pages if there are less than four packets on nuclear@1: them; this expands page size to reduce unneccessary overhead if incoming packets nuclear@1: are large. nuclear@1: These are not necessary behaviors, just 'always better than naive flushing' nuclear@1: without requiring an application to explicitly request a specific optimized nuclear@1: behavior. We'll want an explicit behavior setup pathway eventually as well. */ nuclear@1: nuclear@1: int packets_done=0; nuclear@1: int packet_just_done=0; nuclear@1: for(vals=0;valsnfill && packet_just_done>=4){ nuclear@1: force=1; nuclear@1: break; nuclear@1: } nuclear@1: acc+=os->lacing_vals[vals]&0x0ff; nuclear@1: if((os->lacing_vals[vals]&0xff)<255){ nuclear@1: granule_pos=os->granule_vals[vals]; nuclear@1: packet_just_done=++packets_done; nuclear@1: }else nuclear@1: packet_just_done=0; nuclear@1: } nuclear@1: if(vals==255)force=1; nuclear@1: } nuclear@1: nuclear@1: if(!force) return(0); nuclear@1: nuclear@1: /* construct the header in temp storage */ nuclear@1: memcpy(os->header,"OggS",4); nuclear@1: nuclear@1: /* stream structure version */ nuclear@1: os->header[4]=0x00; nuclear@1: nuclear@1: /* continued packet flag? */ nuclear@1: os->header[5]=0x00; nuclear@1: if((os->lacing_vals[0]&0x100)==0)os->header[5]|=0x01; nuclear@1: /* first page flag? */ nuclear@1: if(os->b_o_s==0)os->header[5]|=0x02; nuclear@1: /* last page flag? */ nuclear@1: if(os->e_o_s && os->lacing_fill==vals)os->header[5]|=0x04; nuclear@1: os->b_o_s=1; nuclear@1: nuclear@1: /* 64 bits of PCM position */ nuclear@1: for(i=6;i<14;i++){ nuclear@1: os->header[i]=(unsigned char)(granule_pos&0xff); nuclear@1: granule_pos>>=8; nuclear@1: } nuclear@1: nuclear@1: /* 32 bits of stream serial number */ nuclear@1: { nuclear@1: long serialno=os->serialno; nuclear@1: for(i=14;i<18;i++){ nuclear@1: os->header[i]=(unsigned char)(serialno&0xff); nuclear@1: serialno>>=8; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* 32 bits of page counter (we have both counter and page header nuclear@1: because this val can roll over) */ nuclear@1: if(os->pageno==-1)os->pageno=0; /* because someone called nuclear@1: stream_reset; this would be a nuclear@1: strange thing to do in an nuclear@1: encode stream, but it has nuclear@1: plausible uses */ nuclear@1: { nuclear@1: long pageno=os->pageno++; nuclear@1: for(i=18;i<22;i++){ nuclear@1: os->header[i]=(unsigned char)(pageno&0xff); nuclear@1: pageno>>=8; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* zero for computation; filled in later */ nuclear@1: os->header[22]=0; nuclear@1: os->header[23]=0; nuclear@1: os->header[24]=0; nuclear@1: os->header[25]=0; nuclear@1: nuclear@1: /* segment table */ nuclear@1: os->header[26]=(unsigned char)(vals&0xff); nuclear@1: for(i=0;iheader[i+27]=(unsigned char)(os->lacing_vals[i]&0xff); nuclear@1: nuclear@1: /* set pointers in the ogg_page struct */ nuclear@1: og->header=os->header; nuclear@1: og->header_len=os->header_fill=vals+27; nuclear@1: og->body=os->body_data+os->body_returned; nuclear@1: og->body_len=bytes; nuclear@1: nuclear@1: /* advance the lacing data and set the body_returned pointer */ nuclear@1: nuclear@1: os->lacing_fill-=vals; nuclear@1: memmove(os->lacing_vals,os->lacing_vals+vals,os->lacing_fill*sizeof(*os->lacing_vals)); nuclear@1: memmove(os->granule_vals,os->granule_vals+vals,os->lacing_fill*sizeof(*os->granule_vals)); nuclear@1: os->body_returned+=bytes; nuclear@1: nuclear@1: /* calculate the checksum */ nuclear@1: nuclear@1: ogg_page_checksum_set(og); nuclear@1: nuclear@1: /* done */ nuclear@1: return(1); nuclear@1: } nuclear@1: nuclear@1: /* This will flush remaining packets into a page (returning nonzero), nuclear@1: even if there is not enough data to trigger a flush normally nuclear@1: (undersized page). If there are no packets or partial packets to nuclear@1: flush, ogg_stream_flush returns 0. Note that ogg_stream_flush will nuclear@1: try to flush a normal sized page like ogg_stream_pageout; a call to nuclear@1: ogg_stream_flush does not guarantee that all packets have flushed. nuclear@1: Only a return value of 0 from ogg_stream_flush indicates all packet nuclear@1: data is flushed into pages. nuclear@1: nuclear@1: since ogg_stream_flush will flush the last page in a stream even if nuclear@1: it's undersized, you almost certainly want to use ogg_stream_pageout nuclear@1: (and *not* ogg_stream_flush) unless you specifically need to flush nuclear@1: a page regardless of size in the middle of a stream. */ nuclear@1: nuclear@1: int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){ nuclear@1: return ogg_stream_flush_i(os,og,1,4096); nuclear@1: } nuclear@1: nuclear@1: /* Like the above, but an argument is provided to adjust the nominal nuclear@1: page size for applications which are smart enough to provide their nuclear@1: own delay based flushing */ nuclear@1: nuclear@1: int ogg_stream_flush_fill(ogg_stream_state *os,ogg_page *og, int nfill){ nuclear@1: return ogg_stream_flush_i(os,og,1,nfill); nuclear@1: } nuclear@1: nuclear@1: /* This constructs pages from buffered packet segments. The pointers nuclear@1: returned are to static buffers; do not free. The returned buffers are nuclear@1: good only until the next call (using the same ogg_stream_state) */ nuclear@1: nuclear@1: int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og){ nuclear@1: int force=0; nuclear@1: if(ogg_stream_check(os)) return 0; nuclear@1: nuclear@1: if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */ nuclear@1: (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */ nuclear@1: force=1; nuclear@1: nuclear@1: return(ogg_stream_flush_i(os,og,force,4096)); nuclear@1: } nuclear@1: nuclear@1: /* Like the above, but an argument is provided to adjust the nominal nuclear@1: page size for applications which are smart enough to provide their nuclear@1: own delay based flushing */ nuclear@1: nuclear@1: int ogg_stream_pageout_fill(ogg_stream_state *os, ogg_page *og, int nfill){ nuclear@1: int force=0; nuclear@1: if(ogg_stream_check(os)) return 0; nuclear@1: nuclear@1: if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */ nuclear@1: (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */ nuclear@1: force=1; nuclear@1: nuclear@1: return(ogg_stream_flush_i(os,og,force,nfill)); nuclear@1: } nuclear@1: nuclear@1: int ogg_stream_eos(ogg_stream_state *os){ nuclear@1: if(ogg_stream_check(os)) return 1; nuclear@1: return os->e_o_s; nuclear@1: } nuclear@1: nuclear@1: /* DECODING PRIMITIVES: packet streaming layer **********************/ nuclear@1: nuclear@1: /* This has two layers to place more of the multi-serialno and paging nuclear@1: control in the application's hands. First, we expose a data buffer nuclear@1: using ogg_sync_buffer(). The app either copies into the nuclear@1: buffer, or passes it directly to read(), etc. We then call nuclear@1: ogg_sync_wrote() to tell how many bytes we just added. nuclear@1: nuclear@1: Pages are returned (pointers into the buffer in ogg_sync_state) nuclear@1: by ogg_sync_pageout(). The page is then submitted to nuclear@1: ogg_stream_pagein() along with the appropriate nuclear@1: ogg_stream_state* (ie, matching serialno). We then get raw nuclear@1: packets out calling ogg_stream_packetout() with a nuclear@1: ogg_stream_state. */ nuclear@1: nuclear@1: /* initialize the struct to a known state */ nuclear@1: int ogg_sync_init(ogg_sync_state *oy){ nuclear@1: if(oy){ nuclear@1: oy->storage = -1; /* used as a readiness flag */ nuclear@1: memset(oy,0,sizeof(*oy)); nuclear@1: } nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* clear non-flat storage within */ nuclear@1: int ogg_sync_clear(ogg_sync_state *oy){ nuclear@1: if(oy){ nuclear@1: if(oy->data)_ogg_free(oy->data); nuclear@1: memset(oy,0,sizeof(*oy)); nuclear@1: } nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: int ogg_sync_destroy(ogg_sync_state *oy){ nuclear@1: if(oy){ nuclear@1: ogg_sync_clear(oy); nuclear@1: _ogg_free(oy); nuclear@1: } nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: int ogg_sync_check(ogg_sync_state *oy){ nuclear@1: if(oy->storage<0) return -1; nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: char *ogg_sync_buffer(ogg_sync_state *oy, long size){ nuclear@1: if(ogg_sync_check(oy)) return NULL; nuclear@1: nuclear@1: /* first, clear out any space that has been previously returned */ nuclear@1: if(oy->returned){ nuclear@1: oy->fill-=oy->returned; nuclear@1: if(oy->fill>0) nuclear@1: memmove(oy->data,oy->data+oy->returned,oy->fill); nuclear@1: oy->returned=0; nuclear@1: } nuclear@1: nuclear@1: if(size>oy->storage-oy->fill){ nuclear@1: /* We need to extend the internal buffer */ nuclear@1: long newsize=size+oy->fill+4096; /* an extra page to be nice */ nuclear@1: void *ret; nuclear@1: nuclear@1: if(oy->data) nuclear@1: ret=_ogg_realloc(oy->data,newsize); nuclear@1: else nuclear@1: ret=_ogg_malloc(newsize); nuclear@1: if(!ret){ nuclear@1: ogg_sync_clear(oy); nuclear@1: return NULL; nuclear@1: } nuclear@1: oy->data=ret; nuclear@1: oy->storage=newsize; nuclear@1: } nuclear@1: nuclear@1: /* expose a segment at least as large as requested at the fill mark */ nuclear@1: return((char *)oy->data+oy->fill); nuclear@1: } nuclear@1: nuclear@1: int ogg_sync_wrote(ogg_sync_state *oy, long bytes){ nuclear@1: if(ogg_sync_check(oy))return -1; nuclear@1: if(oy->fill+bytes>oy->storage)return -1; nuclear@1: oy->fill+=bytes; nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* sync the stream. This is meant to be useful for finding page nuclear@1: boundaries. nuclear@1: nuclear@1: return values for this: nuclear@1: -n) skipped n bytes nuclear@1: 0) page not ready; more data (no bytes skipped) nuclear@1: n) page synced at current location; page length n bytes nuclear@1: nuclear@1: */ nuclear@1: nuclear@1: long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og){ nuclear@1: unsigned char *page=oy->data+oy->returned; nuclear@1: unsigned char *next; nuclear@1: long bytes=oy->fill-oy->returned; nuclear@1: nuclear@1: if(ogg_sync_check(oy))return 0; nuclear@1: nuclear@1: if(oy->headerbytes==0){ nuclear@1: int headerbytes,i; nuclear@1: if(bytes<27)return(0); /* not enough for a header */ nuclear@1: nuclear@1: /* verify capture pattern */ nuclear@1: if(memcmp(page,"OggS",4))goto sync_fail; nuclear@1: nuclear@1: headerbytes=page[26]+27; nuclear@1: if(bytesbodybytes+=page[27+i]; nuclear@1: oy->headerbytes=headerbytes; nuclear@1: } nuclear@1: nuclear@1: if(oy->bodybytes+oy->headerbytes>bytes)return(0); nuclear@1: nuclear@1: /* The whole test page is buffered. Verify the checksum */ nuclear@1: { nuclear@1: /* Grab the checksum bytes, set the header field to zero */ nuclear@1: char chksum[4]; nuclear@1: ogg_page log; nuclear@1: nuclear@1: memcpy(chksum,page+22,4); nuclear@1: memset(page+22,0,4); nuclear@1: nuclear@1: /* set up a temp page struct and recompute the checksum */ nuclear@1: log.header=page; nuclear@1: log.header_len=oy->headerbytes; nuclear@1: log.body=page+oy->headerbytes; nuclear@1: log.body_len=oy->bodybytes; nuclear@1: ogg_page_checksum_set(&log); nuclear@1: nuclear@1: /* Compare */ nuclear@1: if(memcmp(chksum,page+22,4)){ nuclear@1: /* D'oh. Mismatch! Corrupt page (or miscapture and not a page nuclear@1: at all) */ nuclear@1: /* replace the computed checksum with the one actually read in */ nuclear@1: memcpy(page+22,chksum,4); nuclear@1: nuclear@1: /* Bad checksum. Lose sync */ nuclear@1: goto sync_fail; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* yes, have a whole page all ready to go */ nuclear@1: { nuclear@1: unsigned char *page=oy->data+oy->returned; nuclear@1: long bytes; nuclear@1: nuclear@1: if(og){ nuclear@1: og->header=page; nuclear@1: og->header_len=oy->headerbytes; nuclear@1: og->body=page+oy->headerbytes; nuclear@1: og->body_len=oy->bodybytes; nuclear@1: } nuclear@1: nuclear@1: oy->unsynced=0; nuclear@1: oy->returned+=(bytes=oy->headerbytes+oy->bodybytes); nuclear@1: oy->headerbytes=0; nuclear@1: oy->bodybytes=0; nuclear@1: return(bytes); nuclear@1: } nuclear@1: nuclear@1: sync_fail: nuclear@1: nuclear@1: oy->headerbytes=0; nuclear@1: oy->bodybytes=0; nuclear@1: nuclear@1: /* search for possible capture */ nuclear@1: next=memchr(page+1,'O',bytes-1); nuclear@1: if(!next) nuclear@1: next=oy->data+oy->fill; nuclear@1: nuclear@1: oy->returned=(int)(next-oy->data); nuclear@1: return((long)-(next-page)); nuclear@1: } nuclear@1: nuclear@1: /* sync the stream and get a page. Keep trying until we find a page. nuclear@1: Suppress 'sync errors' after reporting the first. nuclear@1: nuclear@1: return values: nuclear@1: -1) recapture (hole in data) nuclear@1: 0) need more data nuclear@1: 1) page returned nuclear@1: nuclear@1: Returns pointers into buffered data; invalidated by next call to nuclear@1: _stream, _clear, _init, or _buffer */ nuclear@1: nuclear@1: int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og){ nuclear@1: nuclear@1: if(ogg_sync_check(oy))return 0; nuclear@1: nuclear@1: /* all we need to do is verify a page at the head of the stream nuclear@1: buffer. If it doesn't verify, we look for the next potential nuclear@1: frame */ nuclear@1: nuclear@1: for(;;){ nuclear@1: long ret=ogg_sync_pageseek(oy,og); nuclear@1: if(ret>0){ nuclear@1: /* have a page */ nuclear@1: return(1); nuclear@1: } nuclear@1: if(ret==0){ nuclear@1: /* need more data */ nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* head did not start a synced page... skipped some bytes */ nuclear@1: if(!oy->unsynced){ nuclear@1: oy->unsynced=1; nuclear@1: return(-1); nuclear@1: } nuclear@1: nuclear@1: /* loop. keep looking */ nuclear@1: nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* add the incoming page to the stream state; we decompose the page nuclear@1: into packet segments here as well. */ nuclear@1: nuclear@1: int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){ nuclear@1: unsigned char *header=og->header; nuclear@1: unsigned char *body=og->body; nuclear@1: long bodysize=og->body_len; nuclear@1: int segptr=0; nuclear@1: nuclear@1: int version=ogg_page_version(og); nuclear@1: int continued=ogg_page_continued(og); nuclear@1: int bos=ogg_page_bos(og); nuclear@1: int eos=ogg_page_eos(og); nuclear@1: ogg_int64_t granulepos=ogg_page_granulepos(og); nuclear@1: int serialno=ogg_page_serialno(og); nuclear@1: long pageno=ogg_page_pageno(og); nuclear@1: int segments=header[26]; nuclear@1: nuclear@1: if(ogg_stream_check(os)) return -1; nuclear@1: nuclear@1: /* clean up 'returned data' */ nuclear@1: { nuclear@1: long lr=os->lacing_returned; nuclear@1: long br=os->body_returned; nuclear@1: nuclear@1: /* body data */ nuclear@1: if(br){ nuclear@1: os->body_fill-=br; nuclear@1: if(os->body_fill) nuclear@1: memmove(os->body_data,os->body_data+br,os->body_fill); nuclear@1: os->body_returned=0; nuclear@1: } nuclear@1: nuclear@1: if(lr){ nuclear@1: /* segment table */ nuclear@1: if(os->lacing_fill-lr){ nuclear@1: memmove(os->lacing_vals,os->lacing_vals+lr, nuclear@1: (os->lacing_fill-lr)*sizeof(*os->lacing_vals)); nuclear@1: memmove(os->granule_vals,os->granule_vals+lr, nuclear@1: (os->lacing_fill-lr)*sizeof(*os->granule_vals)); nuclear@1: } nuclear@1: os->lacing_fill-=lr; nuclear@1: os->lacing_packet-=lr; nuclear@1: os->lacing_returned=0; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* check the serial number */ nuclear@1: if(serialno!=os->serialno)return(-1); nuclear@1: if(version>0)return(-1); nuclear@1: nuclear@1: if(_os_lacing_expand(os,segments+1)) return -1; nuclear@1: nuclear@1: /* are we in sequence? */ nuclear@1: if(pageno!=os->pageno){ nuclear@1: int i; nuclear@1: nuclear@1: /* unroll previous partial packet (if any) */ nuclear@1: for(i=os->lacing_packet;ilacing_fill;i++) nuclear@1: os->body_fill-=os->lacing_vals[i]&0xff; nuclear@1: os->lacing_fill=os->lacing_packet; nuclear@1: nuclear@1: /* make a note of dropped data in segment table */ nuclear@1: if(os->pageno!=-1){ nuclear@1: os->lacing_vals[os->lacing_fill++]=0x400; nuclear@1: os->lacing_packet++; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: /* are we a 'continued packet' page? If so, we may need to skip nuclear@1: some segments */ nuclear@1: if(continued){ nuclear@1: if(os->lacing_fill<1 || nuclear@1: os->lacing_vals[os->lacing_fill-1]==0x400){ nuclear@1: bos=0; nuclear@1: for(;segptrbody_data+os->body_fill,body,bodysize); nuclear@1: os->body_fill+=bodysize; nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: int saved=-1; nuclear@1: while(segptrlacing_vals[os->lacing_fill]=val; nuclear@1: os->granule_vals[os->lacing_fill]=-1; nuclear@1: nuclear@1: if(bos){ nuclear@1: os->lacing_vals[os->lacing_fill]|=0x100; nuclear@1: bos=0; nuclear@1: } nuclear@1: nuclear@1: if(val<255)saved=os->lacing_fill; nuclear@1: nuclear@1: os->lacing_fill++; nuclear@1: segptr++; nuclear@1: nuclear@1: if(val<255)os->lacing_packet=os->lacing_fill; nuclear@1: } nuclear@1: nuclear@1: /* set the granulepos on the last granuleval of the last full packet */ nuclear@1: if(saved!=-1){ nuclear@1: os->granule_vals[saved]=granulepos; nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: nuclear@1: if(eos){ nuclear@1: os->e_o_s=1; nuclear@1: if(os->lacing_fill>0) nuclear@1: os->lacing_vals[os->lacing_fill-1]|=0x200; nuclear@1: } nuclear@1: nuclear@1: os->pageno=pageno+1; nuclear@1: nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: /* clear things to an initial state. Good to call, eg, before seeking */ nuclear@1: int ogg_sync_reset(ogg_sync_state *oy){ nuclear@1: if(ogg_sync_check(oy))return -1; nuclear@1: nuclear@1: oy->fill=0; nuclear@1: oy->returned=0; nuclear@1: oy->unsynced=0; nuclear@1: oy->headerbytes=0; nuclear@1: oy->bodybytes=0; nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: int ogg_stream_reset(ogg_stream_state *os){ nuclear@1: if(ogg_stream_check(os)) return -1; nuclear@1: nuclear@1: os->body_fill=0; nuclear@1: os->body_returned=0; nuclear@1: nuclear@1: os->lacing_fill=0; nuclear@1: os->lacing_packet=0; nuclear@1: os->lacing_returned=0; nuclear@1: nuclear@1: os->header_fill=0; nuclear@1: nuclear@1: os->e_o_s=0; nuclear@1: os->b_o_s=0; nuclear@1: os->pageno=-1; nuclear@1: os->packetno=0; nuclear@1: os->granulepos=0; nuclear@1: nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: int ogg_stream_reset_serialno(ogg_stream_state *os,int serialno){ nuclear@1: if(ogg_stream_check(os)) return -1; nuclear@1: ogg_stream_reset(os); nuclear@1: os->serialno=serialno; nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: static int _packetout(ogg_stream_state *os,ogg_packet *op,int adv){ nuclear@1: nuclear@1: /* The last part of decode. We have the stream broken into packet nuclear@1: segments. Now we need to group them into packets (or return the nuclear@1: out of sync markers) */ nuclear@1: nuclear@1: int ptr=os->lacing_returned; nuclear@1: nuclear@1: if(os->lacing_packet<=ptr)return(0); nuclear@1: nuclear@1: if(os->lacing_vals[ptr]&0x400){ nuclear@1: /* we need to tell the codec there's a gap; it might need to nuclear@1: handle previous packet dependencies. */ nuclear@1: os->lacing_returned++; nuclear@1: os->packetno++; nuclear@1: return(-1); nuclear@1: } nuclear@1: nuclear@1: if(!op && !adv)return(1); /* just using peek as an inexpensive way nuclear@1: to ask if there's a whole packet nuclear@1: waiting */ nuclear@1: nuclear@1: /* Gather the whole packet. We'll have no holes or a partial packet */ nuclear@1: { nuclear@1: int size=os->lacing_vals[ptr]&0xff; nuclear@1: long bytes=size; nuclear@1: int eos=os->lacing_vals[ptr]&0x200; /* last packet of the stream? */ nuclear@1: int bos=os->lacing_vals[ptr]&0x100; /* first packet of the stream? */ nuclear@1: nuclear@1: while(size==255){ nuclear@1: int val=os->lacing_vals[++ptr]; nuclear@1: size=val&0xff; nuclear@1: if(val&0x200)eos=0x200; nuclear@1: bytes+=size; nuclear@1: } nuclear@1: nuclear@1: if(op){ nuclear@1: op->e_o_s=eos; nuclear@1: op->b_o_s=bos; nuclear@1: op->packet=os->body_data+os->body_returned; nuclear@1: op->packetno=os->packetno; nuclear@1: op->granulepos=os->granule_vals[ptr]; nuclear@1: op->bytes=bytes; nuclear@1: } nuclear@1: nuclear@1: if(adv){ nuclear@1: os->body_returned+=bytes; nuclear@1: os->lacing_returned=ptr+1; nuclear@1: os->packetno++; nuclear@1: } nuclear@1: } nuclear@1: return(1); nuclear@1: } nuclear@1: nuclear@1: int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op){ nuclear@1: if(ogg_stream_check(os)) return 0; nuclear@1: return _packetout(os,op,1); nuclear@1: } nuclear@1: nuclear@1: int ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op){ nuclear@1: if(ogg_stream_check(os)) return 0; nuclear@1: return _packetout(os,op,0); nuclear@1: } nuclear@1: nuclear@1: void ogg_packet_clear(ogg_packet *op) { nuclear@1: _ogg_free(op->packet); nuclear@1: memset(op, 0, sizeof(*op)); nuclear@1: } nuclear@1: nuclear@1: #ifdef _V_SELFTEST nuclear@1: #include nuclear@1: nuclear@1: ogg_stream_state os_en, os_de; nuclear@1: ogg_sync_state oy; nuclear@1: nuclear@1: void checkpacket(ogg_packet *op,long len, int no, long pos){ nuclear@1: long j; nuclear@1: static int sequence=0; nuclear@1: static int lastno=0; nuclear@1: nuclear@1: if(op->bytes!=len){ nuclear@1: fprintf(stderr,"incorrect packet length (%ld != %ld)!\n",op->bytes,len); nuclear@1: exit(1); nuclear@1: } nuclear@1: if(op->granulepos!=pos){ nuclear@1: fprintf(stderr,"incorrect packet granpos (%ld != %ld)!\n",(long)op->granulepos,pos); nuclear@1: exit(1); nuclear@1: } nuclear@1: nuclear@1: /* packet number just follows sequence/gap; adjust the input number nuclear@1: for that */ nuclear@1: if(no==0){ nuclear@1: sequence=0; nuclear@1: }else{ nuclear@1: sequence++; nuclear@1: if(no>lastno+1) nuclear@1: sequence++; nuclear@1: } nuclear@1: lastno=no; nuclear@1: if(op->packetno!=sequence){ nuclear@1: fprintf(stderr,"incorrect packet sequence %ld != %d\n", nuclear@1: (long)(op->packetno),sequence); nuclear@1: exit(1); nuclear@1: } nuclear@1: nuclear@1: /* Test data */ nuclear@1: for(j=0;jbytes;j++) nuclear@1: if(op->packet[j]!=((j+no)&0xff)){ nuclear@1: fprintf(stderr,"body data mismatch (1) at pos %ld: %x!=%lx!\n\n", nuclear@1: j,op->packet[j],(j+no)&0xff); nuclear@1: exit(1); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: void check_page(unsigned char *data,const int *header,ogg_page *og){ nuclear@1: long j; nuclear@1: /* Test data */ nuclear@1: for(j=0;jbody_len;j++) nuclear@1: if(og->body[j]!=data[j]){ nuclear@1: fprintf(stderr,"body data mismatch (2) at pos %ld: %x!=%x!\n\n", nuclear@1: j,data[j],og->body[j]); nuclear@1: exit(1); nuclear@1: } nuclear@1: nuclear@1: /* Test header */ nuclear@1: for(j=0;jheader_len;j++){ nuclear@1: if(og->header[j]!=header[j]){ nuclear@1: fprintf(stderr,"header content mismatch at pos %ld:\n",j); nuclear@1: for(j=0;jheader[j]); nuclear@1: fprintf(stderr,"\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: } nuclear@1: if(og->header_len!=header[26]+27){ nuclear@1: fprintf(stderr,"header length incorrect! (%ld!=%d)\n", nuclear@1: og->header_len,header[26]+27); nuclear@1: exit(1); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: void print_header(ogg_page *og){ nuclear@1: int j; nuclear@1: fprintf(stderr,"\nHEADER:\n"); nuclear@1: fprintf(stderr," capture: %c %c %c %c version: %d flags: %x\n", nuclear@1: og->header[0],og->header[1],og->header[2],og->header[3], nuclear@1: (int)og->header[4],(int)og->header[5]); nuclear@1: nuclear@1: fprintf(stderr," granulepos: %d serialno: %d pageno: %ld\n", nuclear@1: (og->header[9]<<24)|(og->header[8]<<16)| nuclear@1: (og->header[7]<<8)|og->header[6], nuclear@1: (og->header[17]<<24)|(og->header[16]<<16)| nuclear@1: (og->header[15]<<8)|og->header[14], nuclear@1: ((long)(og->header[21])<<24)|(og->header[20]<<16)| nuclear@1: (og->header[19]<<8)|og->header[18]); nuclear@1: nuclear@1: fprintf(stderr," checksum: %02x:%02x:%02x:%02x\n segments: %d (", nuclear@1: (int)og->header[22],(int)og->header[23], nuclear@1: (int)og->header[24],(int)og->header[25], nuclear@1: (int)og->header[26]); nuclear@1: nuclear@1: for(j=27;jheader_len;j++) nuclear@1: fprintf(stderr,"%d ",(int)og->header[j]); nuclear@1: fprintf(stderr,")\n\n"); nuclear@1: } nuclear@1: nuclear@1: void copy_page(ogg_page *og){ nuclear@1: unsigned char *temp=_ogg_malloc(og->header_len); nuclear@1: memcpy(temp,og->header,og->header_len); nuclear@1: og->header=temp; nuclear@1: nuclear@1: temp=_ogg_malloc(og->body_len); nuclear@1: memcpy(temp,og->body,og->body_len); nuclear@1: og->body=temp; nuclear@1: } nuclear@1: nuclear@1: void free_page(ogg_page *og){ nuclear@1: _ogg_free (og->header); nuclear@1: _ogg_free (og->body); nuclear@1: } nuclear@1: nuclear@1: void error(void){ nuclear@1: fprintf(stderr,"error!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: nuclear@1: /* 17 only */ nuclear@1: const int head1_0[] = {0x4f,0x67,0x67,0x53,0,0x06, nuclear@1: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,0,0,0,0, nuclear@1: 0x15,0xed,0xec,0x91, nuclear@1: 1, nuclear@1: 17}; nuclear@1: nuclear@1: /* 17, 254, 255, 256, 500, 510, 600 byte, pad */ nuclear@1: const int head1_1[] = {0x4f,0x67,0x67,0x53,0,0x02, nuclear@1: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,0,0,0,0, nuclear@1: 0x59,0x10,0x6c,0x2c, nuclear@1: 1, nuclear@1: 17}; nuclear@1: const int head2_1[] = {0x4f,0x67,0x67,0x53,0,0x04, nuclear@1: 0x07,0x18,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,1,0,0,0, nuclear@1: 0x89,0x33,0x85,0xce, nuclear@1: 13, nuclear@1: 254,255,0,255,1,255,245,255,255,0, nuclear@1: 255,255,90}; nuclear@1: nuclear@1: /* nil packets; beginning,middle,end */ nuclear@1: const int head1_2[] = {0x4f,0x67,0x67,0x53,0,0x02, nuclear@1: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,0,0,0,0, nuclear@1: 0xff,0x7b,0x23,0x17, nuclear@1: 1, nuclear@1: 0}; nuclear@1: const int head2_2[] = {0x4f,0x67,0x67,0x53,0,0x04, nuclear@1: 0x07,0x28,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,1,0,0,0, nuclear@1: 0x5c,0x3f,0x66,0xcb, nuclear@1: 17, nuclear@1: 17,254,255,0,0,255,1,0,255,245,255,255,0, nuclear@1: 255,255,90,0}; nuclear@1: nuclear@1: /* large initial packet */ nuclear@1: const int head1_3[] = {0x4f,0x67,0x67,0x53,0,0x02, nuclear@1: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,0,0,0,0, nuclear@1: 0x01,0x27,0x31,0xaa, nuclear@1: 18, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255,255,10}; nuclear@1: nuclear@1: const int head2_3[] = {0x4f,0x67,0x67,0x53,0,0x04, nuclear@1: 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,1,0,0,0, nuclear@1: 0x7f,0x4e,0x8a,0xd2, nuclear@1: 4, nuclear@1: 255,4,255,0}; nuclear@1: nuclear@1: nuclear@1: /* continuing packet test */ nuclear@1: const int head1_4[] = {0x4f,0x67,0x67,0x53,0,0x02, nuclear@1: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,0,0,0,0, nuclear@1: 0xff,0x7b,0x23,0x17, nuclear@1: 1, nuclear@1: 0}; nuclear@1: nuclear@1: const int head2_4[] = {0x4f,0x67,0x67,0x53,0,0x00, nuclear@1: 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, nuclear@1: 0x01,0x02,0x03,0x04,1,0,0,0, nuclear@1: 0xf8,0x3c,0x19,0x79, nuclear@1: 255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255}; nuclear@1: nuclear@1: const int head3_4[] = {0x4f,0x67,0x67,0x53,0,0x05, nuclear@1: 0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,2,0,0,0, nuclear@1: 0x38,0xe6,0xb6,0x28, nuclear@1: 6, nuclear@1: 255,220,255,4,255,0}; nuclear@1: nuclear@1: nuclear@1: /* spill expansion test */ nuclear@1: const int head1_4b[] = {0x4f,0x67,0x67,0x53,0,0x02, nuclear@1: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,0,0,0,0, nuclear@1: 0xff,0x7b,0x23,0x17, nuclear@1: 1, nuclear@1: 0}; nuclear@1: nuclear@1: const int head2_4b[] = {0x4f,0x67,0x67,0x53,0,0x00, nuclear@1: 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,1,0,0,0, nuclear@1: 0xce,0x8f,0x17,0x1a, nuclear@1: 23, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255,255,10,255,4,255,0,0}; nuclear@1: nuclear@1: nuclear@1: const int head3_4b[] = {0x4f,0x67,0x67,0x53,0,0x04, nuclear@1: 0x07,0x14,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,2,0,0,0, nuclear@1: 0x9b,0xb2,0x50,0xa1, nuclear@1: 1, nuclear@1: 0}; nuclear@1: nuclear@1: /* page with the 255 segment limit */ nuclear@1: const int head1_5[] = {0x4f,0x67,0x67,0x53,0,0x02, nuclear@1: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,0,0,0,0, nuclear@1: 0xff,0x7b,0x23,0x17, nuclear@1: 1, nuclear@1: 0}; nuclear@1: nuclear@1: const int head2_5[] = {0x4f,0x67,0x67,0x53,0,0x00, nuclear@1: 0x07,0xfc,0x03,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,1,0,0,0, nuclear@1: 0xed,0x2a,0x2e,0xa7, nuclear@1: 255, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10}; nuclear@1: nuclear@1: const int head3_5[] = {0x4f,0x67,0x67,0x53,0,0x04, nuclear@1: 0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,2,0,0,0, nuclear@1: 0x6c,0x3b,0x82,0x3d, nuclear@1: 1, nuclear@1: 50}; nuclear@1: nuclear@1: nuclear@1: /* packet that overspans over an entire page */ nuclear@1: const int head1_6[] = {0x4f,0x67,0x67,0x53,0,0x02, nuclear@1: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,0,0,0,0, nuclear@1: 0xff,0x7b,0x23,0x17, nuclear@1: 1, nuclear@1: 0}; nuclear@1: nuclear@1: const int head2_6[] = {0x4f,0x67,0x67,0x53,0,0x00, nuclear@1: 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,1,0,0,0, nuclear@1: 0x68,0x22,0x7c,0x3d, nuclear@1: 255, nuclear@1: 100, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255}; nuclear@1: nuclear@1: const int head3_6[] = {0x4f,0x67,0x67,0x53,0,0x01, nuclear@1: 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, nuclear@1: 0x01,0x02,0x03,0x04,2,0,0,0, nuclear@1: 0xf4,0x87,0xba,0xf3, nuclear@1: 255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255}; nuclear@1: nuclear@1: const int head4_6[] = {0x4f,0x67,0x67,0x53,0,0x05, nuclear@1: 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,3,0,0,0, nuclear@1: 0xf7,0x2f,0x6c,0x60, nuclear@1: 5, nuclear@1: 254,255,4,255,0}; nuclear@1: nuclear@1: /* packet that overspans over an entire page */ nuclear@1: const int head1_7[] = {0x4f,0x67,0x67,0x53,0,0x02, nuclear@1: 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,0,0,0,0, nuclear@1: 0xff,0x7b,0x23,0x17, nuclear@1: 1, nuclear@1: 0}; nuclear@1: nuclear@1: const int head2_7[] = {0x4f,0x67,0x67,0x53,0,0x00, nuclear@1: 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,1,0,0,0, nuclear@1: 0x68,0x22,0x7c,0x3d, nuclear@1: 255, nuclear@1: 100, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255,255,255, nuclear@1: 255,255,255,255,255,255}; nuclear@1: nuclear@1: const int head3_7[] = {0x4f,0x67,0x67,0x53,0,0x05, nuclear@1: 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00, nuclear@1: 0x01,0x02,0x03,0x04,2,0,0,0, nuclear@1: 0xd4,0xe0,0x60,0xe5, nuclear@1: 1, nuclear@1: 0}; nuclear@1: nuclear@1: void test_pack(const int *pl, const int **headers, int byteskip, nuclear@1: int pageskip, int packetskip){ nuclear@1: unsigned char *data=_ogg_malloc(1024*1024); /* for scripted test cases only */ nuclear@1: long inptr=0; nuclear@1: long outptr=0; nuclear@1: long deptr=0; nuclear@1: long depacket=0; nuclear@1: long granule_pos=7,pageno=0; nuclear@1: int i,j,packets,pageout=pageskip; nuclear@1: int eosflag=0; nuclear@1: int bosflag=0; nuclear@1: nuclear@1: int byteskipcount=0; nuclear@1: nuclear@1: ogg_stream_reset(&os_en); nuclear@1: ogg_stream_reset(&os_de); nuclear@1: ogg_sync_reset(&oy); nuclear@1: nuclear@1: for(packets=0;packetsbyteskip){ nuclear@1: memcpy(next,og.header,byteskipcount-byteskip); nuclear@1: next+=byteskipcount-byteskip; nuclear@1: byteskipcount=byteskip; nuclear@1: } nuclear@1: nuclear@1: byteskipcount+=og.body_len; nuclear@1: if(byteskipcount>byteskip){ nuclear@1: memcpy(next,og.body,byteskipcount-byteskip); nuclear@1: next+=byteskipcount-byteskip; nuclear@1: byteskipcount=byteskip; nuclear@1: } nuclear@1: nuclear@1: ogg_sync_wrote(&oy,next-buf); nuclear@1: nuclear@1: while(1){ nuclear@1: int ret=ogg_sync_pageout(&oy,&og_de); nuclear@1: if(ret==0)break; nuclear@1: if(ret<0)continue; nuclear@1: /* got a page. Happy happy. Verify that it's good. */ nuclear@1: nuclear@1: fprintf(stderr,"(%d), ",pageout); nuclear@1: nuclear@1: check_page(data+deptr,headers[pageout],&og_de); nuclear@1: deptr+=og_de.body_len; nuclear@1: pageout++; nuclear@1: nuclear@1: /* submit it to deconstitution */ nuclear@1: ogg_stream_pagein(&os_de,&og_de); nuclear@1: nuclear@1: /* packets out? */ nuclear@1: while(ogg_stream_packetpeek(&os_de,&op_de2)>0){ nuclear@1: ogg_stream_packetpeek(&os_de,NULL); nuclear@1: ogg_stream_packetout(&os_de,&op_de); /* just catching them all */ nuclear@1: nuclear@1: /* verify peek and out match */ nuclear@1: if(memcmp(&op_de,&op_de2,sizeof(op_de))){ nuclear@1: fprintf(stderr,"packetout != packetpeek! pos=%ld\n", nuclear@1: depacket); nuclear@1: exit(1); nuclear@1: } nuclear@1: nuclear@1: /* verify the packet! */ nuclear@1: /* check data */ nuclear@1: if(memcmp(data+depacket,op_de.packet,op_de.bytes)){ nuclear@1: fprintf(stderr,"packet data mismatch in decode! pos=%ld\n", nuclear@1: depacket); nuclear@1: exit(1); nuclear@1: } nuclear@1: /* check bos flag */ nuclear@1: if(bosflag==0 && op_de.b_o_s==0){ nuclear@1: fprintf(stderr,"b_o_s flag not set on packet!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: if(bosflag && op_de.b_o_s){ nuclear@1: fprintf(stderr,"b_o_s flag incorrectly set on packet!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: bosflag=1; nuclear@1: depacket+=op_de.bytes; nuclear@1: nuclear@1: /* check eos flag */ nuclear@1: if(eosflag){ nuclear@1: fprintf(stderr,"Multiple decoded packets with eos flag!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: nuclear@1: if(op_de.e_o_s)eosflag=1; nuclear@1: nuclear@1: /* check granulepos flag */ nuclear@1: if(op_de.granulepos!=-1){ nuclear@1: fprintf(stderr," granule:%ld ",(long)op_de.granulepos); nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: _ogg_free(data); nuclear@1: if(headers[pageno]!=NULL){ nuclear@1: fprintf(stderr,"did not write last page!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: if(headers[pageout]!=NULL){ nuclear@1: fprintf(stderr,"did not decode last page!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: if(inptr!=outptr){ nuclear@1: fprintf(stderr,"encoded page data incomplete!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: if(inptr!=deptr){ nuclear@1: fprintf(stderr,"decoded page data incomplete!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: if(inptr!=depacket){ nuclear@1: fprintf(stderr,"decoded packet data incomplete!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: if(!eosflag){ nuclear@1: fprintf(stderr,"Never got a packet with EOS set!\n"); nuclear@1: exit(1); nuclear@1: } nuclear@1: fprintf(stderr,"ok.\n"); nuclear@1: } nuclear@1: nuclear@1: int main(void){ nuclear@1: nuclear@1: ogg_stream_init(&os_en,0x04030201); nuclear@1: ogg_stream_init(&os_de,0x04030201); nuclear@1: ogg_sync_init(&oy); nuclear@1: nuclear@1: /* Exercise each code path in the framing code. Also verify that nuclear@1: the checksums are working. */ nuclear@1: nuclear@1: { nuclear@1: /* 17 only */ nuclear@1: const int packets[]={17, -1}; nuclear@1: const int *headret[]={head1_0,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing single page encoding... "); nuclear@1: test_pack(packets,headret,0,0,0); nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: /* 17, 254, 255, 256, 500, 510, 600 byte, pad */ nuclear@1: const int packets[]={17, 254, 255, 256, 500, 510, 600, -1}; nuclear@1: const int *headret[]={head1_1,head2_1,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing basic page encoding... "); nuclear@1: test_pack(packets,headret,0,0,0); nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: /* nil packets; beginning,middle,end */ nuclear@1: const int packets[]={0,17, 254, 255, 0, 256, 0, 500, 510, 600, 0, -1}; nuclear@1: const int *headret[]={head1_2,head2_2,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing basic nil packets... "); nuclear@1: test_pack(packets,headret,0,0,0); nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: /* large initial packet */ nuclear@1: const int packets[]={4345,259,255,-1}; nuclear@1: const int *headret[]={head1_3,head2_3,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing initial-packet lacing > 4k... "); nuclear@1: test_pack(packets,headret,0,0,0); nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: /* continuing packet test; with page spill expansion, we have to nuclear@1: overflow the lacing table. */ nuclear@1: const int packets[]={0,65500,259,255,-1}; nuclear@1: const int *headret[]={head1_4,head2_4,head3_4,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing single packet page span... "); nuclear@1: test_pack(packets,headret,0,0,0); nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: /* spill expand packet test */ nuclear@1: const int packets[]={0,4345,259,255,0,0,-1}; nuclear@1: const int *headret[]={head1_4b,head2_4b,head3_4b,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing page spill expansion... "); nuclear@1: test_pack(packets,headret,0,0,0); nuclear@1: } nuclear@1: nuclear@1: /* page with the 255 segment limit */ nuclear@1: { nuclear@1: nuclear@1: const int packets[]={0,10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,10, nuclear@1: 10,10,10,10,10,10,10,50,-1}; nuclear@1: const int *headret[]={head1_5,head2_5,head3_5,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing max packet segments... "); nuclear@1: test_pack(packets,headret,0,0,0); nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: /* packet that overspans over an entire page */ nuclear@1: const int packets[]={0,100,130049,259,255,-1}; nuclear@1: const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing very large packets... "); nuclear@1: test_pack(packets,headret,0,0,0); nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: /* test for the libogg 1.1.1 resync in large continuation bug nuclear@1: found by Josh Coalson) */ nuclear@1: const int packets[]={0,100,130049,259,255,-1}; nuclear@1: const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing continuation resync in very large packets... "); nuclear@1: test_pack(packets,headret,100,2,3); nuclear@1: } nuclear@1: nuclear@1: { nuclear@1: /* term only page. why not? */ nuclear@1: const int packets[]={0,100,64770,-1}; nuclear@1: const int *headret[]={head1_7,head2_7,head3_7,NULL}; nuclear@1: nuclear@1: fprintf(stderr,"testing zero data page (1 nil packet)... "); nuclear@1: test_pack(packets,headret,0,0,0); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: nuclear@1: { nuclear@1: /* build a bunch of pages for testing */ nuclear@1: unsigned char *data=_ogg_malloc(1024*1024); nuclear@1: int pl[]={0, 1,1,98,4079, 1,1,2954,2057, 76,34,912,0,234,1000,1000, 1000,300,-1}; nuclear@1: int inptr=0,i,j; nuclear@1: ogg_page og[5]; nuclear@1: nuclear@1: ogg_stream_reset(&os_en); nuclear@1: nuclear@1: for(i=0;pl[i]!=-1;i++){ nuclear@1: ogg_packet op; nuclear@1: int len=pl[i]; nuclear@1: nuclear@1: op.packet=data+inptr; nuclear@1: op.bytes=len; nuclear@1: op.e_o_s=(pl[i+1]<0?1:0); nuclear@1: op.granulepos=(i+1)*1000; nuclear@1: nuclear@1: for(j=0;j0)error(); nuclear@1: nuclear@1: /* Test fractional page inputs: incomplete fixed header */ nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+3, nuclear@1: 20); nuclear@1: ogg_sync_wrote(&oy,20); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)>0)error(); nuclear@1: nuclear@1: /* Test fractional page inputs: incomplete header */ nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+23, nuclear@1: 5); nuclear@1: ogg_sync_wrote(&oy,5); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)>0)error(); nuclear@1: nuclear@1: /* Test fractional page inputs: incomplete body */ nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+28, nuclear@1: og[1].header_len-28); nuclear@1: ogg_sync_wrote(&oy,og[1].header_len-28); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)>0)error(); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,1000); nuclear@1: ogg_sync_wrote(&oy,1000); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)>0)error(); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body+1000, nuclear@1: og[1].body_len-1000); nuclear@1: ogg_sync_wrote(&oy,og[1].body_len-1000); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)<=0)error(); nuclear@1: nuclear@1: fprintf(stderr,"ok.\n"); nuclear@1: } nuclear@1: nuclear@1: /* Test fractional page inputs: page + incomplete capture */ nuclear@1: { nuclear@1: ogg_page og_de; nuclear@1: fprintf(stderr,"Testing sync on 1+partial inputs... "); nuclear@1: ogg_sync_reset(&oy); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header, nuclear@1: og[1].header_len); nuclear@1: ogg_sync_wrote(&oy,og[1].header_len); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, nuclear@1: og[1].body_len); nuclear@1: ogg_sync_wrote(&oy,og[1].body_len); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header, nuclear@1: 20); nuclear@1: ogg_sync_wrote(&oy,20); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)<=0)error(); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)>0)error(); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+20, nuclear@1: og[1].header_len-20); nuclear@1: ogg_sync_wrote(&oy,og[1].header_len-20); nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, nuclear@1: og[1].body_len); nuclear@1: ogg_sync_wrote(&oy,og[1].body_len); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)<=0)error(); nuclear@1: nuclear@1: fprintf(stderr,"ok.\n"); nuclear@1: } nuclear@1: nuclear@1: /* Test recapture: garbage + page */ nuclear@1: { nuclear@1: ogg_page og_de; nuclear@1: fprintf(stderr,"Testing search for capture... "); nuclear@1: ogg_sync_reset(&oy); nuclear@1: nuclear@1: /* 'garbage' */ nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, nuclear@1: og[1].body_len); nuclear@1: ogg_sync_wrote(&oy,og[1].body_len); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header, nuclear@1: og[1].header_len); nuclear@1: ogg_sync_wrote(&oy,og[1].header_len); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, nuclear@1: og[1].body_len); nuclear@1: ogg_sync_wrote(&oy,og[1].body_len); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header, nuclear@1: 20); nuclear@1: ogg_sync_wrote(&oy,20); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)>0)error(); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)<=0)error(); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)>0)error(); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header+20, nuclear@1: og[2].header_len-20); nuclear@1: ogg_sync_wrote(&oy,og[2].header_len-20); nuclear@1: memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body, nuclear@1: og[2].body_len); nuclear@1: ogg_sync_wrote(&oy,og[2].body_len); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)<=0)error(); nuclear@1: nuclear@1: fprintf(stderr,"ok.\n"); nuclear@1: } nuclear@1: nuclear@1: /* Test recapture: page + garbage + page */ nuclear@1: { nuclear@1: ogg_page og_de; nuclear@1: fprintf(stderr,"Testing recapture... "); nuclear@1: ogg_sync_reset(&oy); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header, nuclear@1: og[1].header_len); nuclear@1: ogg_sync_wrote(&oy,og[1].header_len); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body, nuclear@1: og[1].body_len); nuclear@1: ogg_sync_wrote(&oy,og[1].body_len); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header, nuclear@1: og[2].header_len); nuclear@1: ogg_sync_wrote(&oy,og[2].header_len); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header, nuclear@1: og[2].header_len); nuclear@1: ogg_sync_wrote(&oy,og[2].header_len); nuclear@1: nuclear@1: if(ogg_sync_pageout(&oy,&og_de)<=0)error(); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body, nuclear@1: og[2].body_len-5); nuclear@1: ogg_sync_wrote(&oy,og[2].body_len-5); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[3].header_len),og[3].header, nuclear@1: og[3].header_len); nuclear@1: ogg_sync_wrote(&oy,og[3].header_len); nuclear@1: nuclear@1: memcpy(ogg_sync_buffer(&oy,og[3].body_len),og[3].body, nuclear@1: og[3].body_len); nuclear@1: ogg_sync_wrote(&oy,og[3].body_len); nuclear@1: nuclear@1: if(ogg_sync_pageout(&oy,&og_de)>0)error(); nuclear@1: if(ogg_sync_pageout(&oy,&og_de)<=0)error(); nuclear@1: nuclear@1: fprintf(stderr,"ok.\n"); nuclear@1: } nuclear@1: nuclear@1: /* Free page data that was previously copied */ nuclear@1: { nuclear@1: for(i=0;i<5;i++){ nuclear@1: free_page(&og[i]); nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: return(0); nuclear@1: } nuclear@1: nuclear@1: #endif