framing.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg CONTAINER SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2010 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: code raw packets into framed OggSquish stream and
  13. decode Ogg streams back into raw packets
  14. last mod: $Id: framing.c 18758 2013-01-08 16:29:56Z tterribe $
  15. note: The CRC code is directly derived from public domain code by
  16. Ross Williams (ross@guest.adelaide.edu.au). See docs/framing.html
  17. for details.
  18. ********************************************************************/
  19. #include <stdlib.h>
  20. #include <limits.h>
  21. #include <string.h>
  22. #include <ogg/ogg.h>
  23. /* A complete description of Ogg framing exists in docs/framing.html */
  24. int ogg_page_version(const ogg_page *og){
  25. return((int)(og->header[4]));
  26. }
  27. int ogg_page_continued(const ogg_page *og){
  28. return((int)(og->header[5]&0x01));
  29. }
  30. int ogg_page_bos(const ogg_page *og){
  31. return((int)(og->header[5]&0x02));
  32. }
  33. int ogg_page_eos(const ogg_page *og){
  34. return((int)(og->header[5]&0x04));
  35. }
  36. ogg_int64_t ogg_page_granulepos(const ogg_page *og){
  37. unsigned char *page=og->header;
  38. ogg_int64_t granulepos=page[13]&(0xff);
  39. granulepos= (granulepos<<8)|(page[12]&0xff);
  40. granulepos= (granulepos<<8)|(page[11]&0xff);
  41. granulepos= (granulepos<<8)|(page[10]&0xff);
  42. granulepos= (granulepos<<8)|(page[9]&0xff);
  43. granulepos= (granulepos<<8)|(page[8]&0xff);
  44. granulepos= (granulepos<<8)|(page[7]&0xff);
  45. granulepos= (granulepos<<8)|(page[6]&0xff);
  46. return(granulepos);
  47. }
  48. int ogg_page_serialno(const ogg_page *og){
  49. return(og->header[14] |
  50. (og->header[15]<<8) |
  51. (og->header[16]<<16) |
  52. (og->header[17]<<24));
  53. }
  54. long ogg_page_pageno(const ogg_page *og){
  55. return(og->header[18] |
  56. (og->header[19]<<8) |
  57. (og->header[20]<<16) |
  58. (og->header[21]<<24));
  59. }
  60. /* returns the number of packets that are completed on this page (if
  61. the leading packet is begun on a previous page, but ends on this
  62. page, it's counted */
  63. /* NOTE:
  64. If a page consists of a packet begun on a previous page, and a new
  65. packet begun (but not completed) on this page, the return will be:
  66. ogg_page_packets(page) ==1,
  67. ogg_page_continued(page) !=0
  68. If a page happens to be a single packet that was begun on a
  69. previous page, and spans to the next page (in the case of a three or
  70. more page packet), the return will be:
  71. ogg_page_packets(page) ==0,
  72. ogg_page_continued(page) !=0
  73. */
  74. int ogg_page_packets(const ogg_page *og){
  75. int i,n=og->header[26],count=0;
  76. for(i=0;i<n;i++)
  77. if(og->header[27+i]<255)count++;
  78. return(count);
  79. }
  80. #if 0
  81. /* helper to initialize lookup for direct-table CRC (illustrative; we
  82. use the static init below) */
  83. static ogg_uint32_t _ogg_crc_entry(unsigned long index){
  84. int i;
  85. unsigned long r;
  86. r = index << 24;
  87. for (i=0; i<8; i++)
  88. if (r & 0x80000000UL)
  89. r = (r << 1) ^ 0x04c11db7; /* The same as the ethernet generator
  90. polynomial, although we use an
  91. unreflected alg and an init/final
  92. of 0, not 0xffffffff */
  93. else
  94. r<<=1;
  95. return (r & 0xffffffffUL);
  96. }
  97. #endif
  98. static const ogg_uint32_t crc_lookup[256]={
  99. 0x00000000,0x04c11db7,0x09823b6e,0x0d4326d9,
  100. 0x130476dc,0x17c56b6b,0x1a864db2,0x1e475005,
  101. 0x2608edb8,0x22c9f00f,0x2f8ad6d6,0x2b4bcb61,
  102. 0x350c9b64,0x31cd86d3,0x3c8ea00a,0x384fbdbd,
  103. 0x4c11db70,0x48d0c6c7,0x4593e01e,0x4152fda9,
  104. 0x5f15adac,0x5bd4b01b,0x569796c2,0x52568b75,
  105. 0x6a1936c8,0x6ed82b7f,0x639b0da6,0x675a1011,
  106. 0x791d4014,0x7ddc5da3,0x709f7b7a,0x745e66cd,
  107. 0x9823b6e0,0x9ce2ab57,0x91a18d8e,0x95609039,
  108. 0x8b27c03c,0x8fe6dd8b,0x82a5fb52,0x8664e6e5,
  109. 0xbe2b5b58,0xbaea46ef,0xb7a96036,0xb3687d81,
  110. 0xad2f2d84,0xa9ee3033,0xa4ad16ea,0xa06c0b5d,
  111. 0xd4326d90,0xd0f37027,0xddb056fe,0xd9714b49,
  112. 0xc7361b4c,0xc3f706fb,0xceb42022,0xca753d95,
  113. 0xf23a8028,0xf6fb9d9f,0xfbb8bb46,0xff79a6f1,
  114. 0xe13ef6f4,0xe5ffeb43,0xe8bccd9a,0xec7dd02d,
  115. 0x34867077,0x30476dc0,0x3d044b19,0x39c556ae,
  116. 0x278206ab,0x23431b1c,0x2e003dc5,0x2ac12072,
  117. 0x128e9dcf,0x164f8078,0x1b0ca6a1,0x1fcdbb16,
  118. 0x018aeb13,0x054bf6a4,0x0808d07d,0x0cc9cdca,
  119. 0x7897ab07,0x7c56b6b0,0x71159069,0x75d48dde,
  120. 0x6b93dddb,0x6f52c06c,0x6211e6b5,0x66d0fb02,
  121. 0x5e9f46bf,0x5a5e5b08,0x571d7dd1,0x53dc6066,
  122. 0x4d9b3063,0x495a2dd4,0x44190b0d,0x40d816ba,
  123. 0xaca5c697,0xa864db20,0xa527fdf9,0xa1e6e04e,
  124. 0xbfa1b04b,0xbb60adfc,0xb6238b25,0xb2e29692,
  125. 0x8aad2b2f,0x8e6c3698,0x832f1041,0x87ee0df6,
  126. 0x99a95df3,0x9d684044,0x902b669d,0x94ea7b2a,
  127. 0xe0b41de7,0xe4750050,0xe9362689,0xedf73b3e,
  128. 0xf3b06b3b,0xf771768c,0xfa325055,0xfef34de2,
  129. 0xc6bcf05f,0xc27dede8,0xcf3ecb31,0xcbffd686,
  130. 0xd5b88683,0xd1799b34,0xdc3abded,0xd8fba05a,
  131. 0x690ce0ee,0x6dcdfd59,0x608edb80,0x644fc637,
  132. 0x7a089632,0x7ec98b85,0x738aad5c,0x774bb0eb,
  133. 0x4f040d56,0x4bc510e1,0x46863638,0x42472b8f,
  134. 0x5c007b8a,0x58c1663d,0x558240e4,0x51435d53,
  135. 0x251d3b9e,0x21dc2629,0x2c9f00f0,0x285e1d47,
  136. 0x36194d42,0x32d850f5,0x3f9b762c,0x3b5a6b9b,
  137. 0x0315d626,0x07d4cb91,0x0a97ed48,0x0e56f0ff,
  138. 0x1011a0fa,0x14d0bd4d,0x19939b94,0x1d528623,
  139. 0xf12f560e,0xf5ee4bb9,0xf8ad6d60,0xfc6c70d7,
  140. 0xe22b20d2,0xe6ea3d65,0xeba91bbc,0xef68060b,
  141. 0xd727bbb6,0xd3e6a601,0xdea580d8,0xda649d6f,
  142. 0xc423cd6a,0xc0e2d0dd,0xcda1f604,0xc960ebb3,
  143. 0xbd3e8d7e,0xb9ff90c9,0xb4bcb610,0xb07daba7,
  144. 0xae3afba2,0xaafbe615,0xa7b8c0cc,0xa379dd7b,
  145. 0x9b3660c6,0x9ff77d71,0x92b45ba8,0x9675461f,
  146. 0x8832161a,0x8cf30bad,0x81b02d74,0x857130c3,
  147. 0x5d8a9099,0x594b8d2e,0x5408abf7,0x50c9b640,
  148. 0x4e8ee645,0x4a4ffbf2,0x470cdd2b,0x43cdc09c,
  149. 0x7b827d21,0x7f436096,0x7200464f,0x76c15bf8,
  150. 0x68860bfd,0x6c47164a,0x61043093,0x65c52d24,
  151. 0x119b4be9,0x155a565e,0x18197087,0x1cd86d30,
  152. 0x029f3d35,0x065e2082,0x0b1d065b,0x0fdc1bec,
  153. 0x3793a651,0x3352bbe6,0x3e119d3f,0x3ad08088,
  154. 0x2497d08d,0x2056cd3a,0x2d15ebe3,0x29d4f654,
  155. 0xc5a92679,0xc1683bce,0xcc2b1d17,0xc8ea00a0,
  156. 0xd6ad50a5,0xd26c4d12,0xdf2f6bcb,0xdbee767c,
  157. 0xe3a1cbc1,0xe760d676,0xea23f0af,0xeee2ed18,
  158. 0xf0a5bd1d,0xf464a0aa,0xf9278673,0xfde69bc4,
  159. 0x89b8fd09,0x8d79e0be,0x803ac667,0x84fbdbd0,
  160. 0x9abc8bd5,0x9e7d9662,0x933eb0bb,0x97ffad0c,
  161. 0xafb010b1,0xab710d06,0xa6322bdf,0xa2f33668,
  162. 0xbcb4666d,0xb8757bda,0xb5365d03,0xb1f740b4};
  163. /* init the encode/decode logical stream state */
  164. int ogg_stream_init(ogg_stream_state *os,int serialno){
  165. if(os){
  166. memset(os,0,sizeof(*os));
  167. os->body_storage=16*1024;
  168. os->lacing_storage=1024;
  169. os->body_data=_ogg_malloc(os->body_storage*sizeof(*os->body_data));
  170. os->lacing_vals=_ogg_malloc(os->lacing_storage*sizeof(*os->lacing_vals));
  171. os->granule_vals=_ogg_malloc(os->lacing_storage*sizeof(*os->granule_vals));
  172. if(!os->body_data || !os->lacing_vals || !os->granule_vals){
  173. ogg_stream_clear(os);
  174. return -1;
  175. }
  176. os->serialno=serialno;
  177. return(0);
  178. }
  179. return(-1);
  180. }
  181. /* async/delayed error detection for the ogg_stream_state */
  182. int ogg_stream_check(ogg_stream_state *os){
  183. if(!os || !os->body_data) return -1;
  184. return 0;
  185. }
  186. /* _clear does not free os, only the non-flat storage within */
  187. int ogg_stream_clear(ogg_stream_state *os){
  188. if(os){
  189. if(os->body_data)_ogg_free(os->body_data);
  190. if(os->lacing_vals)_ogg_free(os->lacing_vals);
  191. if(os->granule_vals)_ogg_free(os->granule_vals);
  192. memset(os,0,sizeof(*os));
  193. }
  194. return(0);
  195. }
  196. int ogg_stream_destroy(ogg_stream_state *os){
  197. if(os){
  198. ogg_stream_clear(os);
  199. _ogg_free(os);
  200. }
  201. return(0);
  202. }
  203. /* Helpers for ogg_stream_encode; this keeps the structure and
  204. what's happening fairly clear */
  205. static int _os_body_expand(ogg_stream_state *os,long needed){
  206. if(os->body_storage-needed<=os->body_fill){
  207. long body_storage;
  208. void *ret;
  209. if(os->body_storage>LONG_MAX-needed){
  210. ogg_stream_clear(os);
  211. return -1;
  212. }
  213. body_storage=os->body_storage+needed;
  214. if(body_storage<LONG_MAX-1024)body_storage+=1024;
  215. ret=_ogg_realloc(os->body_data,body_storage*sizeof(*os->body_data));
  216. if(!ret){
  217. ogg_stream_clear(os);
  218. return -1;
  219. }
  220. os->body_storage=body_storage;
  221. os->body_data=ret;
  222. }
  223. return 0;
  224. }
  225. static int _os_lacing_expand(ogg_stream_state *os,long needed){
  226. if(os->lacing_storage-needed<=os->lacing_fill){
  227. long lacing_storage;
  228. void *ret;
  229. if(os->lacing_storage>LONG_MAX-needed){
  230. ogg_stream_clear(os);
  231. return -1;
  232. }
  233. lacing_storage=os->lacing_storage+needed;
  234. if(lacing_storage<LONG_MAX-32)lacing_storage+=32;
  235. ret=_ogg_realloc(os->lacing_vals,lacing_storage*sizeof(*os->lacing_vals));
  236. if(!ret){
  237. ogg_stream_clear(os);
  238. return -1;
  239. }
  240. os->lacing_vals=ret;
  241. ret=_ogg_realloc(os->granule_vals,lacing_storage*
  242. sizeof(*os->granule_vals));
  243. if(!ret){
  244. ogg_stream_clear(os);
  245. return -1;
  246. }
  247. os->granule_vals=ret;
  248. os->lacing_storage=lacing_storage;
  249. }
  250. return 0;
  251. }
  252. /* checksum the page */
  253. /* Direct table CRC; note that this will be faster in the future if we
  254. perform the checksum simultaneously with other copies */
  255. void ogg_page_checksum_set(ogg_page *og){
  256. if(og){
  257. ogg_uint32_t crc_reg=0;
  258. int i;
  259. /* safety; needed for API behavior, but not framing code */
  260. og->header[22]=0;
  261. og->header[23]=0;
  262. og->header[24]=0;
  263. og->header[25]=0;
  264. for(i=0;i<og->header_len;i++)
  265. crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->header[i]];
  266. for(i=0;i<og->body_len;i++)
  267. crc_reg=(crc_reg<<8)^crc_lookup[((crc_reg >> 24)&0xff)^og->body[i]];
  268. og->header[22]=(unsigned char)(crc_reg&0xff);
  269. og->header[23]=(unsigned char)((crc_reg>>8)&0xff);
  270. og->header[24]=(unsigned char)((crc_reg>>16)&0xff);
  271. og->header[25]=(unsigned char)((crc_reg>>24)&0xff);
  272. }
  273. }
  274. /* submit data to the internal buffer of the framing engine */
  275. int ogg_stream_iovecin(ogg_stream_state *os, ogg_iovec_t *iov, int count,
  276. long e_o_s, ogg_int64_t granulepos){
  277. long bytes = 0, lacing_vals;
  278. int i;
  279. if(ogg_stream_check(os)) return -1;
  280. if(!iov) return 0;
  281. for (i = 0; i < count; ++i){
  282. if(iov[i].iov_len>LONG_MAX) return -1;
  283. if(bytes>LONG_MAX-(long)iov[i].iov_len) return -1;
  284. bytes += (long)iov[i].iov_len;
  285. }
  286. lacing_vals=bytes/255+1;
  287. if(os->body_returned){
  288. /* advance packet data according to the body_returned pointer. We
  289. had to keep it around to return a pointer into the buffer last
  290. call */
  291. os->body_fill-=os->body_returned;
  292. if(os->body_fill)
  293. memmove(os->body_data,os->body_data+os->body_returned,
  294. os->body_fill);
  295. os->body_returned=0;
  296. }
  297. /* make sure we have the buffer storage */
  298. if(_os_body_expand(os,bytes) || _os_lacing_expand(os,lacing_vals))
  299. return -1;
  300. /* Copy in the submitted packet. Yes, the copy is a waste; this is
  301. the liability of overly clean abstraction for the time being. It
  302. will actually be fairly easy to eliminate the extra copy in the
  303. future */
  304. for (i = 0; i < count; ++i) {
  305. memcpy(os->body_data+os->body_fill, iov[i].iov_base, iov[i].iov_len);
  306. os->body_fill += (int)iov[i].iov_len;
  307. }
  308. /* Store lacing vals for this packet */
  309. for(i=0;i<lacing_vals-1;i++){
  310. os->lacing_vals[os->lacing_fill+i]=255;
  311. os->granule_vals[os->lacing_fill+i]=os->granulepos;
  312. }
  313. os->lacing_vals[os->lacing_fill+i]=bytes%255;
  314. os->granulepos=os->granule_vals[os->lacing_fill+i]=granulepos;
  315. /* flag the first segment as the beginning of the packet */
  316. os->lacing_vals[os->lacing_fill]|= 0x100;
  317. os->lacing_fill+=lacing_vals;
  318. /* for the sake of completeness */
  319. os->packetno++;
  320. if(e_o_s)os->e_o_s=1;
  321. return(0);
  322. }
  323. int ogg_stream_packetin(ogg_stream_state *os,ogg_packet *op){
  324. ogg_iovec_t iov;
  325. iov.iov_base = op->packet;
  326. iov.iov_len = op->bytes;
  327. return ogg_stream_iovecin(os, &iov, 1, op->e_o_s, op->granulepos);
  328. }
  329. /* Conditionally flush a page; force==0 will only flush nominal-size
  330. pages, force==1 forces us to flush a page regardless of page size
  331. so long as there's any data available at all. */
  332. static int ogg_stream_flush_i(ogg_stream_state *os,ogg_page *og, int force, int nfill){
  333. int i;
  334. int vals=0;
  335. int maxvals=(os->lacing_fill>255?255:os->lacing_fill);
  336. int bytes=0;
  337. long acc=0;
  338. ogg_int64_t granule_pos=-1;
  339. if(ogg_stream_check(os)) return(0);
  340. if(maxvals==0) return(0);
  341. /* construct a page */
  342. /* decide how many segments to include */
  343. /* If this is the initial header case, the first page must only include
  344. the initial header packet */
  345. if(os->b_o_s==0){ /* 'initial header page' case */
  346. granule_pos=0;
  347. for(vals=0;vals<maxvals;vals++){
  348. if((os->lacing_vals[vals]&0x0ff)<255){
  349. vals++;
  350. break;
  351. }
  352. }
  353. }else{
  354. /* The extra packets_done, packet_just_done logic here attempts to do two things:
  355. 1) Don't unneccessarily span pages.
  356. 2) Unless necessary, don't flush pages if there are less than four packets on
  357. them; this expands page size to reduce unneccessary overhead if incoming packets
  358. are large.
  359. These are not necessary behaviors, just 'always better than naive flushing'
  360. without requiring an application to explicitly request a specific optimized
  361. behavior. We'll want an explicit behavior setup pathway eventually as well. */
  362. int packets_done=0;
  363. int packet_just_done=0;
  364. for(vals=0;vals<maxvals;vals++){
  365. if(acc>nfill && packet_just_done>=4){
  366. force=1;
  367. break;
  368. }
  369. acc+=os->lacing_vals[vals]&0x0ff;
  370. if((os->lacing_vals[vals]&0xff)<255){
  371. granule_pos=os->granule_vals[vals];
  372. packet_just_done=++packets_done;
  373. }else
  374. packet_just_done=0;
  375. }
  376. if(vals==255)force=1;
  377. }
  378. if(!force) return(0);
  379. /* construct the header in temp storage */
  380. memcpy(os->header,"OggS",4);
  381. /* stream structure version */
  382. os->header[4]=0x00;
  383. /* continued packet flag? */
  384. os->header[5]=0x00;
  385. if((os->lacing_vals[0]&0x100)==0)os->header[5]|=0x01;
  386. /* first page flag? */
  387. if(os->b_o_s==0)os->header[5]|=0x02;
  388. /* last page flag? */
  389. if(os->e_o_s && os->lacing_fill==vals)os->header[5]|=0x04;
  390. os->b_o_s=1;
  391. /* 64 bits of PCM position */
  392. for(i=6;i<14;i++){
  393. os->header[i]=(unsigned char)(granule_pos&0xff);
  394. granule_pos>>=8;
  395. }
  396. /* 32 bits of stream serial number */
  397. {
  398. long serialno=os->serialno;
  399. for(i=14;i<18;i++){
  400. os->header[i]=(unsigned char)(serialno&0xff);
  401. serialno>>=8;
  402. }
  403. }
  404. /* 32 bits of page counter (we have both counter and page header
  405. because this val can roll over) */
  406. if(os->pageno==-1)os->pageno=0; /* because someone called
  407. stream_reset; this would be a
  408. strange thing to do in an
  409. encode stream, but it has
  410. plausible uses */
  411. {
  412. long pageno=os->pageno++;
  413. for(i=18;i<22;i++){
  414. os->header[i]=(unsigned char)(pageno&0xff);
  415. pageno>>=8;
  416. }
  417. }
  418. /* zero for computation; filled in later */
  419. os->header[22]=0;
  420. os->header[23]=0;
  421. os->header[24]=0;
  422. os->header[25]=0;
  423. /* segment table */
  424. os->header[26]=(unsigned char)(vals&0xff);
  425. for(i=0;i<vals;i++)
  426. bytes+=os->header[i+27]=(unsigned char)(os->lacing_vals[i]&0xff);
  427. /* set pointers in the ogg_page struct */
  428. og->header=os->header;
  429. og->header_len=os->header_fill=vals+27;
  430. og->body=os->body_data+os->body_returned;
  431. og->body_len=bytes;
  432. /* advance the lacing data and set the body_returned pointer */
  433. os->lacing_fill-=vals;
  434. memmove(os->lacing_vals,os->lacing_vals+vals,os->lacing_fill*sizeof(*os->lacing_vals));
  435. memmove(os->granule_vals,os->granule_vals+vals,os->lacing_fill*sizeof(*os->granule_vals));
  436. os->body_returned+=bytes;
  437. /* calculate the checksum */
  438. ogg_page_checksum_set(og);
  439. /* done */
  440. return(1);
  441. }
  442. /* This will flush remaining packets into a page (returning nonzero),
  443. even if there is not enough data to trigger a flush normally
  444. (undersized page). If there are no packets or partial packets to
  445. flush, ogg_stream_flush returns 0. Note that ogg_stream_flush will
  446. try to flush a normal sized page like ogg_stream_pageout; a call to
  447. ogg_stream_flush does not guarantee that all packets have flushed.
  448. Only a return value of 0 from ogg_stream_flush indicates all packet
  449. data is flushed into pages.
  450. since ogg_stream_flush will flush the last page in a stream even if
  451. it's undersized, you almost certainly want to use ogg_stream_pageout
  452. (and *not* ogg_stream_flush) unless you specifically need to flush
  453. a page regardless of size in the middle of a stream. */
  454. int ogg_stream_flush(ogg_stream_state *os,ogg_page *og){
  455. return ogg_stream_flush_i(os,og,1,4096);
  456. }
  457. /* Like the above, but an argument is provided to adjust the nominal
  458. page size for applications which are smart enough to provide their
  459. own delay based flushing */
  460. int ogg_stream_flush_fill(ogg_stream_state *os,ogg_page *og, int nfill){
  461. return ogg_stream_flush_i(os,og,1,nfill);
  462. }
  463. /* This constructs pages from buffered packet segments. The pointers
  464. returned are to static buffers; do not free. The returned buffers are
  465. good only until the next call (using the same ogg_stream_state) */
  466. int ogg_stream_pageout(ogg_stream_state *os, ogg_page *og){
  467. int force=0;
  468. if(ogg_stream_check(os)) return 0;
  469. if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */
  470. (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */
  471. force=1;
  472. return(ogg_stream_flush_i(os,og,force,4096));
  473. }
  474. /* Like the above, but an argument is provided to adjust the nominal
  475. page size for applications which are smart enough to provide their
  476. own delay based flushing */
  477. int ogg_stream_pageout_fill(ogg_stream_state *os, ogg_page *og, int nfill){
  478. int force=0;
  479. if(ogg_stream_check(os)) return 0;
  480. if((os->e_o_s&&os->lacing_fill) || /* 'were done, now flush' case */
  481. (os->lacing_fill&&!os->b_o_s)) /* 'initial header page' case */
  482. force=1;
  483. return(ogg_stream_flush_i(os,og,force,nfill));
  484. }
  485. int ogg_stream_eos(ogg_stream_state *os){
  486. if(ogg_stream_check(os)) return 1;
  487. return os->e_o_s;
  488. }
  489. /* DECODING PRIMITIVES: packet streaming layer **********************/
  490. /* This has two layers to place more of the multi-serialno and paging
  491. control in the application's hands. First, we expose a data buffer
  492. using ogg_sync_buffer(). The app either copies into the
  493. buffer, or passes it directly to read(), etc. We then call
  494. ogg_sync_wrote() to tell how many bytes we just added.
  495. Pages are returned (pointers into the buffer in ogg_sync_state)
  496. by ogg_sync_pageout(). The page is then submitted to
  497. ogg_stream_pagein() along with the appropriate
  498. ogg_stream_state* (ie, matching serialno). We then get raw
  499. packets out calling ogg_stream_packetout() with a
  500. ogg_stream_state. */
  501. /* initialize the struct to a known state */
  502. int ogg_sync_init(ogg_sync_state *oy){
  503. if(oy){
  504. oy->storage = -1; /* used as a readiness flag */
  505. memset(oy,0,sizeof(*oy));
  506. }
  507. return(0);
  508. }
  509. /* clear non-flat storage within */
  510. int ogg_sync_clear(ogg_sync_state *oy){
  511. if(oy){
  512. if(oy->data)_ogg_free(oy->data);
  513. memset(oy,0,sizeof(*oy));
  514. }
  515. return(0);
  516. }
  517. int ogg_sync_destroy(ogg_sync_state *oy){
  518. if(oy){
  519. ogg_sync_clear(oy);
  520. _ogg_free(oy);
  521. }
  522. return(0);
  523. }
  524. int ogg_sync_check(ogg_sync_state *oy){
  525. if(oy->storage<0) return -1;
  526. return 0;
  527. }
  528. char *ogg_sync_buffer(ogg_sync_state *oy, long size){
  529. if(ogg_sync_check(oy)) return NULL;
  530. /* first, clear out any space that has been previously returned */
  531. if(oy->returned){
  532. oy->fill-=oy->returned;
  533. if(oy->fill>0)
  534. memmove(oy->data,oy->data+oy->returned,oy->fill);
  535. oy->returned=0;
  536. }
  537. if(size>oy->storage-oy->fill){
  538. /* We need to extend the internal buffer */
  539. long newsize=size+oy->fill+4096; /* an extra page to be nice */
  540. void *ret;
  541. if(oy->data)
  542. ret=_ogg_realloc(oy->data,newsize);
  543. else
  544. ret=_ogg_malloc(newsize);
  545. if(!ret){
  546. ogg_sync_clear(oy);
  547. return NULL;
  548. }
  549. oy->data=ret;
  550. oy->storage=newsize;
  551. }
  552. /* expose a segment at least as large as requested at the fill mark */
  553. return((char *)oy->data+oy->fill);
  554. }
  555. int ogg_sync_wrote(ogg_sync_state *oy, long bytes){
  556. if(ogg_sync_check(oy))return -1;
  557. if(oy->fill+bytes>oy->storage)return -1;
  558. oy->fill+=bytes;
  559. return(0);
  560. }
  561. /* sync the stream. This is meant to be useful for finding page
  562. boundaries.
  563. return values for this:
  564. -n) skipped n bytes
  565. 0) page not ready; more data (no bytes skipped)
  566. n) page synced at current location; page length n bytes
  567. */
  568. long ogg_sync_pageseek(ogg_sync_state *oy,ogg_page *og){
  569. unsigned char *page=oy->data+oy->returned;
  570. unsigned char *next;
  571. long bytes=oy->fill-oy->returned;
  572. if(ogg_sync_check(oy))return 0;
  573. if(oy->headerbytes==0){
  574. int headerbytes,i;
  575. if(bytes<27)return(0); /* not enough for a header */
  576. /* verify capture pattern */
  577. if(memcmp(page,"OggS",4))goto sync_fail;
  578. headerbytes=page[26]+27;
  579. if(bytes<headerbytes)return(0); /* not enough for header + seg table */
  580. /* count up body length in the segment table */
  581. for(i=0;i<page[26];i++)
  582. oy->bodybytes+=page[27+i];
  583. oy->headerbytes=headerbytes;
  584. }
  585. if(oy->bodybytes+oy->headerbytes>bytes)return(0);
  586. /* The whole test page is buffered. Verify the checksum */
  587. {
  588. /* Grab the checksum bytes, set the header field to zero */
  589. char chksum[4];
  590. ogg_page log;
  591. memcpy(chksum,page+22,4);
  592. memset(page+22,0,4);
  593. /* set up a temp page struct and recompute the checksum */
  594. log.header=page;
  595. log.header_len=oy->headerbytes;
  596. log.body=page+oy->headerbytes;
  597. log.body_len=oy->bodybytes;
  598. ogg_page_checksum_set(&log);
  599. /* Compare */
  600. if(memcmp(chksum,page+22,4)){
  601. /* D'oh. Mismatch! Corrupt page (or miscapture and not a page
  602. at all) */
  603. /* replace the computed checksum with the one actually read in */
  604. memcpy(page+22,chksum,4);
  605. /* Bad checksum. Lose sync */
  606. goto sync_fail;
  607. }
  608. }
  609. /* yes, have a whole page all ready to go */
  610. {
  611. unsigned char *page=oy->data+oy->returned;
  612. long bytes;
  613. if(og){
  614. og->header=page;
  615. og->header_len=oy->headerbytes;
  616. og->body=page+oy->headerbytes;
  617. og->body_len=oy->bodybytes;
  618. }
  619. oy->unsynced=0;
  620. oy->returned+=(bytes=oy->headerbytes+oy->bodybytes);
  621. oy->headerbytes=0;
  622. oy->bodybytes=0;
  623. return(bytes);
  624. }
  625. sync_fail:
  626. oy->headerbytes=0;
  627. oy->bodybytes=0;
  628. /* search for possible capture */
  629. next=memchr(page+1,'O',bytes-1);
  630. if(!next)
  631. next=oy->data+oy->fill;
  632. oy->returned=(int)(next-oy->data);
  633. return((long)-(next-page));
  634. }
  635. /* sync the stream and get a page. Keep trying until we find a page.
  636. Suppress 'sync errors' after reporting the first.
  637. return values:
  638. -1) recapture (hole in data)
  639. 0) need more data
  640. 1) page returned
  641. Returns pointers into buffered data; invalidated by next call to
  642. _stream, _clear, _init, or _buffer */
  643. int ogg_sync_pageout(ogg_sync_state *oy, ogg_page *og){
  644. if(ogg_sync_check(oy))return 0;
  645. /* all we need to do is verify a page at the head of the stream
  646. buffer. If it doesn't verify, we look for the next potential
  647. frame */
  648. for(;;){
  649. long ret=ogg_sync_pageseek(oy,og);
  650. if(ret>0){
  651. /* have a page */
  652. return(1);
  653. }
  654. if(ret==0){
  655. /* need more data */
  656. return(0);
  657. }
  658. /* head did not start a synced page... skipped some bytes */
  659. if(!oy->unsynced){
  660. oy->unsynced=1;
  661. return(-1);
  662. }
  663. /* loop. keep looking */
  664. }
  665. }
  666. /* add the incoming page to the stream state; we decompose the page
  667. into packet segments here as well. */
  668. int ogg_stream_pagein(ogg_stream_state *os, ogg_page *og){
  669. unsigned char *header=og->header;
  670. unsigned char *body=og->body;
  671. long bodysize=og->body_len;
  672. int segptr=0;
  673. int version=ogg_page_version(og);
  674. int continued=ogg_page_continued(og);
  675. int bos=ogg_page_bos(og);
  676. int eos=ogg_page_eos(og);
  677. ogg_int64_t granulepos=ogg_page_granulepos(og);
  678. int serialno=ogg_page_serialno(og);
  679. long pageno=ogg_page_pageno(og);
  680. int segments=header[26];
  681. if(ogg_stream_check(os)) return -1;
  682. /* clean up 'returned data' */
  683. {
  684. long lr=os->lacing_returned;
  685. long br=os->body_returned;
  686. /* body data */
  687. if(br){
  688. os->body_fill-=br;
  689. if(os->body_fill)
  690. memmove(os->body_data,os->body_data+br,os->body_fill);
  691. os->body_returned=0;
  692. }
  693. if(lr){
  694. /* segment table */
  695. if(os->lacing_fill-lr){
  696. memmove(os->lacing_vals,os->lacing_vals+lr,
  697. (os->lacing_fill-lr)*sizeof(*os->lacing_vals));
  698. memmove(os->granule_vals,os->granule_vals+lr,
  699. (os->lacing_fill-lr)*sizeof(*os->granule_vals));
  700. }
  701. os->lacing_fill-=lr;
  702. os->lacing_packet-=lr;
  703. os->lacing_returned=0;
  704. }
  705. }
  706. /* check the serial number */
  707. if(serialno!=os->serialno)return(-1);
  708. if(version>0)return(-1);
  709. if(_os_lacing_expand(os,segments+1)) return -1;
  710. /* are we in sequence? */
  711. if(pageno!=os->pageno){
  712. int i;
  713. /* unroll previous partial packet (if any) */
  714. for(i=os->lacing_packet;i<os->lacing_fill;i++)
  715. os->body_fill-=os->lacing_vals[i]&0xff;
  716. os->lacing_fill=os->lacing_packet;
  717. /* make a note of dropped data in segment table */
  718. if(os->pageno!=-1){
  719. os->lacing_vals[os->lacing_fill++]=0x400;
  720. os->lacing_packet++;
  721. }
  722. }
  723. /* are we a 'continued packet' page? If so, we may need to skip
  724. some segments */
  725. if(continued){
  726. if(os->lacing_fill<1 ||
  727. os->lacing_vals[os->lacing_fill-1]==0x400){
  728. bos=0;
  729. for(;segptr<segments;segptr++){
  730. int val=header[27+segptr];
  731. body+=val;
  732. bodysize-=val;
  733. if(val<255){
  734. segptr++;
  735. break;
  736. }
  737. }
  738. }
  739. }
  740. if(bodysize){
  741. if(_os_body_expand(os,bodysize)) return -1;
  742. memcpy(os->body_data+os->body_fill,body,bodysize);
  743. os->body_fill+=bodysize;
  744. }
  745. {
  746. int saved=-1;
  747. while(segptr<segments){
  748. int val=header[27+segptr];
  749. os->lacing_vals[os->lacing_fill]=val;
  750. os->granule_vals[os->lacing_fill]=-1;
  751. if(bos){
  752. os->lacing_vals[os->lacing_fill]|=0x100;
  753. bos=0;
  754. }
  755. if(val<255)saved=os->lacing_fill;
  756. os->lacing_fill++;
  757. segptr++;
  758. if(val<255)os->lacing_packet=os->lacing_fill;
  759. }
  760. /* set the granulepos on the last granuleval of the last full packet */
  761. if(saved!=-1){
  762. os->granule_vals[saved]=granulepos;
  763. }
  764. }
  765. if(eos){
  766. os->e_o_s=1;
  767. if(os->lacing_fill>0)
  768. os->lacing_vals[os->lacing_fill-1]|=0x200;
  769. }
  770. os->pageno=pageno+1;
  771. return(0);
  772. }
  773. /* clear things to an initial state. Good to call, eg, before seeking */
  774. int ogg_sync_reset(ogg_sync_state *oy){
  775. if(ogg_sync_check(oy))return -1;
  776. oy->fill=0;
  777. oy->returned=0;
  778. oy->unsynced=0;
  779. oy->headerbytes=0;
  780. oy->bodybytes=0;
  781. return(0);
  782. }
  783. int ogg_stream_reset(ogg_stream_state *os){
  784. if(ogg_stream_check(os)) return -1;
  785. os->body_fill=0;
  786. os->body_returned=0;
  787. os->lacing_fill=0;
  788. os->lacing_packet=0;
  789. os->lacing_returned=0;
  790. os->header_fill=0;
  791. os->e_o_s=0;
  792. os->b_o_s=0;
  793. os->pageno=-1;
  794. os->packetno=0;
  795. os->granulepos=0;
  796. return(0);
  797. }
  798. int ogg_stream_reset_serialno(ogg_stream_state *os,int serialno){
  799. if(ogg_stream_check(os)) return -1;
  800. ogg_stream_reset(os);
  801. os->serialno=serialno;
  802. return(0);
  803. }
  804. static int _packetout(ogg_stream_state *os,ogg_packet *op,int adv){
  805. /* The last part of decode. We have the stream broken into packet
  806. segments. Now we need to group them into packets (or return the
  807. out of sync markers) */
  808. int ptr=os->lacing_returned;
  809. if(os->lacing_packet<=ptr)return(0);
  810. if(os->lacing_vals[ptr]&0x400){
  811. /* we need to tell the codec there's a gap; it might need to
  812. handle previous packet dependencies. */
  813. os->lacing_returned++;
  814. os->packetno++;
  815. return(-1);
  816. }
  817. if(!op && !adv)return(1); /* just using peek as an inexpensive way
  818. to ask if there's a whole packet
  819. waiting */
  820. /* Gather the whole packet. We'll have no holes or a partial packet */
  821. {
  822. int size=os->lacing_vals[ptr]&0xff;
  823. long bytes=size;
  824. int eos=os->lacing_vals[ptr]&0x200; /* last packet of the stream? */
  825. int bos=os->lacing_vals[ptr]&0x100; /* first packet of the stream? */
  826. while(size==255){
  827. int val=os->lacing_vals[++ptr];
  828. size=val&0xff;
  829. if(val&0x200)eos=0x200;
  830. bytes+=size;
  831. }
  832. if(op){
  833. op->e_o_s=eos;
  834. op->b_o_s=bos;
  835. op->packet=os->body_data+os->body_returned;
  836. op->packetno=os->packetno;
  837. op->granulepos=os->granule_vals[ptr];
  838. op->bytes=bytes;
  839. }
  840. if(adv){
  841. os->body_returned+=bytes;
  842. os->lacing_returned=ptr+1;
  843. os->packetno++;
  844. }
  845. }
  846. return(1);
  847. }
  848. int ogg_stream_packetout(ogg_stream_state *os,ogg_packet *op){
  849. if(ogg_stream_check(os)) return 0;
  850. return _packetout(os,op,1);
  851. }
  852. int ogg_stream_packetpeek(ogg_stream_state *os,ogg_packet *op){
  853. if(ogg_stream_check(os)) return 0;
  854. return _packetout(os,op,0);
  855. }
  856. void ogg_packet_clear(ogg_packet *op) {
  857. _ogg_free(op->packet);
  858. memset(op, 0, sizeof(*op));
  859. }
  860. #ifdef _V_SELFTEST
  861. #include <stdio.h>
  862. ogg_stream_state os_en, os_de;
  863. ogg_sync_state oy;
  864. void checkpacket(ogg_packet *op,long len, int no, long pos){
  865. long j;
  866. static int sequence=0;
  867. static int lastno=0;
  868. if(op->bytes!=len){
  869. fprintf(stderr,"incorrect packet length (%ld != %ld)!\n",op->bytes,len);
  870. exit(1);
  871. }
  872. if(op->granulepos!=pos){
  873. fprintf(stderr,"incorrect packet granpos (%ld != %ld)!\n",(long)op->granulepos,pos);
  874. exit(1);
  875. }
  876. /* packet number just follows sequence/gap; adjust the input number
  877. for that */
  878. if(no==0){
  879. sequence=0;
  880. }else{
  881. sequence++;
  882. if(no>lastno+1)
  883. sequence++;
  884. }
  885. lastno=no;
  886. if(op->packetno!=sequence){
  887. fprintf(stderr,"incorrect packet sequence %ld != %d\n",
  888. (long)(op->packetno),sequence);
  889. exit(1);
  890. }
  891. /* Test data */
  892. for(j=0;j<op->bytes;j++)
  893. if(op->packet[j]!=((j+no)&0xff)){
  894. fprintf(stderr,"body data mismatch (1) at pos %ld: %x!=%lx!\n\n",
  895. j,op->packet[j],(j+no)&0xff);
  896. exit(1);
  897. }
  898. }
  899. void check_page(unsigned char *data,const int *header,ogg_page *og){
  900. long j;
  901. /* Test data */
  902. for(j=0;j<og->body_len;j++)
  903. if(og->body[j]!=data[j]){
  904. fprintf(stderr,"body data mismatch (2) at pos %ld: %x!=%x!\n\n",
  905. j,data[j],og->body[j]);
  906. exit(1);
  907. }
  908. /* Test header */
  909. for(j=0;j<og->header_len;j++){
  910. if(og->header[j]!=header[j]){
  911. fprintf(stderr,"header content mismatch at pos %ld:\n",j);
  912. for(j=0;j<header[26]+27;j++)
  913. fprintf(stderr," (%ld)%02x:%02x",j,header[j],og->header[j]);
  914. fprintf(stderr,"\n");
  915. exit(1);
  916. }
  917. }
  918. if(og->header_len!=header[26]+27){
  919. fprintf(stderr,"header length incorrect! (%ld!=%d)\n",
  920. og->header_len,header[26]+27);
  921. exit(1);
  922. }
  923. }
  924. void print_header(ogg_page *og){
  925. int j;
  926. fprintf(stderr,"\nHEADER:\n");
  927. fprintf(stderr," capture: %c %c %c %c version: %d flags: %x\n",
  928. og->header[0],og->header[1],og->header[2],og->header[3],
  929. (int)og->header[4],(int)og->header[5]);
  930. fprintf(stderr," granulepos: %d serialno: %d pageno: %ld\n",
  931. (og->header[9]<<24)|(og->header[8]<<16)|
  932. (og->header[7]<<8)|og->header[6],
  933. (og->header[17]<<24)|(og->header[16]<<16)|
  934. (og->header[15]<<8)|og->header[14],
  935. ((long)(og->header[21])<<24)|(og->header[20]<<16)|
  936. (og->header[19]<<8)|og->header[18]);
  937. fprintf(stderr," checksum: %02x:%02x:%02x:%02x\n segments: %d (",
  938. (int)og->header[22],(int)og->header[23],
  939. (int)og->header[24],(int)og->header[25],
  940. (int)og->header[26]);
  941. for(j=27;j<og->header_len;j++)
  942. fprintf(stderr,"%d ",(int)og->header[j]);
  943. fprintf(stderr,")\n\n");
  944. }
  945. void copy_page(ogg_page *og){
  946. unsigned char *temp=_ogg_malloc(og->header_len);
  947. memcpy(temp,og->header,og->header_len);
  948. og->header=temp;
  949. temp=_ogg_malloc(og->body_len);
  950. memcpy(temp,og->body,og->body_len);
  951. og->body=temp;
  952. }
  953. void free_page(ogg_page *og){
  954. _ogg_free (og->header);
  955. _ogg_free (og->body);
  956. }
  957. void error(void){
  958. fprintf(stderr,"error!\n");
  959. exit(1);
  960. }
  961. /* 17 only */
  962. const int head1_0[] = {0x4f,0x67,0x67,0x53,0,0x06,
  963. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  964. 0x01,0x02,0x03,0x04,0,0,0,0,
  965. 0x15,0xed,0xec,0x91,
  966. 1,
  967. 17};
  968. /* 17, 254, 255, 256, 500, 510, 600 byte, pad */
  969. const int head1_1[] = {0x4f,0x67,0x67,0x53,0,0x02,
  970. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  971. 0x01,0x02,0x03,0x04,0,0,0,0,
  972. 0x59,0x10,0x6c,0x2c,
  973. 1,
  974. 17};
  975. const int head2_1[] = {0x4f,0x67,0x67,0x53,0,0x04,
  976. 0x07,0x18,0x00,0x00,0x00,0x00,0x00,0x00,
  977. 0x01,0x02,0x03,0x04,1,0,0,0,
  978. 0x89,0x33,0x85,0xce,
  979. 13,
  980. 254,255,0,255,1,255,245,255,255,0,
  981. 255,255,90};
  982. /* nil packets; beginning,middle,end */
  983. const int head1_2[] = {0x4f,0x67,0x67,0x53,0,0x02,
  984. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  985. 0x01,0x02,0x03,0x04,0,0,0,0,
  986. 0xff,0x7b,0x23,0x17,
  987. 1,
  988. 0};
  989. const int head2_2[] = {0x4f,0x67,0x67,0x53,0,0x04,
  990. 0x07,0x28,0x00,0x00,0x00,0x00,0x00,0x00,
  991. 0x01,0x02,0x03,0x04,1,0,0,0,
  992. 0x5c,0x3f,0x66,0xcb,
  993. 17,
  994. 17,254,255,0,0,255,1,0,255,245,255,255,0,
  995. 255,255,90,0};
  996. /* large initial packet */
  997. const int head1_3[] = {0x4f,0x67,0x67,0x53,0,0x02,
  998. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  999. 0x01,0x02,0x03,0x04,0,0,0,0,
  1000. 0x01,0x27,0x31,0xaa,
  1001. 18,
  1002. 255,255,255,255,255,255,255,255,
  1003. 255,255,255,255,255,255,255,255,255,10};
  1004. const int head2_3[] = {0x4f,0x67,0x67,0x53,0,0x04,
  1005. 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
  1006. 0x01,0x02,0x03,0x04,1,0,0,0,
  1007. 0x7f,0x4e,0x8a,0xd2,
  1008. 4,
  1009. 255,4,255,0};
  1010. /* continuing packet test */
  1011. const int head1_4[] = {0x4f,0x67,0x67,0x53,0,0x02,
  1012. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1013. 0x01,0x02,0x03,0x04,0,0,0,0,
  1014. 0xff,0x7b,0x23,0x17,
  1015. 1,
  1016. 0};
  1017. const int head2_4[] = {0x4f,0x67,0x67,0x53,0,0x00,
  1018. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  1019. 0x01,0x02,0x03,0x04,1,0,0,0,
  1020. 0xf8,0x3c,0x19,0x79,
  1021. 255,
  1022. 255,255,255,255,255,255,255,255,
  1023. 255,255,255,255,255,255,255,255,
  1024. 255,255,255,255,255,255,255,255,
  1025. 255,255,255,255,255,255,255,255,
  1026. 255,255,255,255,255,255,255,255,
  1027. 255,255,255,255,255,255,255,255,
  1028. 255,255,255,255,255,255,255,255,
  1029. 255,255,255,255,255,255,255,255,
  1030. 255,255,255,255,255,255,255,255,
  1031. 255,255,255,255,255,255,255,255,
  1032. 255,255,255,255,255,255,255,255,
  1033. 255,255,255,255,255,255,255,255,
  1034. 255,255,255,255,255,255,255,255,
  1035. 255,255,255,255,255,255,255,255,
  1036. 255,255,255,255,255,255,255,255,
  1037. 255,255,255,255,255,255,255,255,
  1038. 255,255,255,255,255,255,255,255,
  1039. 255,255,255,255,255,255,255,255,
  1040. 255,255,255,255,255,255,255,255,
  1041. 255,255,255,255,255,255,255,255,
  1042. 255,255,255,255,255,255,255,255,
  1043. 255,255,255,255,255,255,255,255,
  1044. 255,255,255,255,255,255,255,255,
  1045. 255,255,255,255,255,255,255,255,
  1046. 255,255,255,255,255,255,255,255,
  1047. 255,255,255,255,255,255,255,255,
  1048. 255,255,255,255,255,255,255,255,
  1049. 255,255,255,255,255,255,255,255,
  1050. 255,255,255,255,255,255,255,255,
  1051. 255,255,255,255,255,255,255,255,
  1052. 255,255,255,255,255,255,255,255,
  1053. 255,255,255,255,255,255,255};
  1054. const int head3_4[] = {0x4f,0x67,0x67,0x53,0,0x05,
  1055. 0x07,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,
  1056. 0x01,0x02,0x03,0x04,2,0,0,0,
  1057. 0x38,0xe6,0xb6,0x28,
  1058. 6,
  1059. 255,220,255,4,255,0};
  1060. /* spill expansion test */
  1061. const int head1_4b[] = {0x4f,0x67,0x67,0x53,0,0x02,
  1062. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1063. 0x01,0x02,0x03,0x04,0,0,0,0,
  1064. 0xff,0x7b,0x23,0x17,
  1065. 1,
  1066. 0};
  1067. const int head2_4b[] = {0x4f,0x67,0x67,0x53,0,0x00,
  1068. 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
  1069. 0x01,0x02,0x03,0x04,1,0,0,0,
  1070. 0xce,0x8f,0x17,0x1a,
  1071. 23,
  1072. 255,255,255,255,255,255,255,255,
  1073. 255,255,255,255,255,255,255,255,255,10,255,4,255,0,0};
  1074. const int head3_4b[] = {0x4f,0x67,0x67,0x53,0,0x04,
  1075. 0x07,0x14,0x00,0x00,0x00,0x00,0x00,0x00,
  1076. 0x01,0x02,0x03,0x04,2,0,0,0,
  1077. 0x9b,0xb2,0x50,0xa1,
  1078. 1,
  1079. 0};
  1080. /* page with the 255 segment limit */
  1081. const int head1_5[] = {0x4f,0x67,0x67,0x53,0,0x02,
  1082. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1083. 0x01,0x02,0x03,0x04,0,0,0,0,
  1084. 0xff,0x7b,0x23,0x17,
  1085. 1,
  1086. 0};
  1087. const int head2_5[] = {0x4f,0x67,0x67,0x53,0,0x00,
  1088. 0x07,0xfc,0x03,0x00,0x00,0x00,0x00,0x00,
  1089. 0x01,0x02,0x03,0x04,1,0,0,0,
  1090. 0xed,0x2a,0x2e,0xa7,
  1091. 255,
  1092. 10,10,10,10,10,10,10,10,
  1093. 10,10,10,10,10,10,10,10,
  1094. 10,10,10,10,10,10,10,10,
  1095. 10,10,10,10,10,10,10,10,
  1096. 10,10,10,10,10,10,10,10,
  1097. 10,10,10,10,10,10,10,10,
  1098. 10,10,10,10,10,10,10,10,
  1099. 10,10,10,10,10,10,10,10,
  1100. 10,10,10,10,10,10,10,10,
  1101. 10,10,10,10,10,10,10,10,
  1102. 10,10,10,10,10,10,10,10,
  1103. 10,10,10,10,10,10,10,10,
  1104. 10,10,10,10,10,10,10,10,
  1105. 10,10,10,10,10,10,10,10,
  1106. 10,10,10,10,10,10,10,10,
  1107. 10,10,10,10,10,10,10,10,
  1108. 10,10,10,10,10,10,10,10,
  1109. 10,10,10,10,10,10,10,10,
  1110. 10,10,10,10,10,10,10,10,
  1111. 10,10,10,10,10,10,10,10,
  1112. 10,10,10,10,10,10,10,10,
  1113. 10,10,10,10,10,10,10,10,
  1114. 10,10,10,10,10,10,10,10,
  1115. 10,10,10,10,10,10,10,10,
  1116. 10,10,10,10,10,10,10,10,
  1117. 10,10,10,10,10,10,10,10,
  1118. 10,10,10,10,10,10,10,10,
  1119. 10,10,10,10,10,10,10,10,
  1120. 10,10,10,10,10,10,10,10,
  1121. 10,10,10,10,10,10,10,10,
  1122. 10,10,10,10,10,10,10,10,
  1123. 10,10,10,10,10,10,10};
  1124. const int head3_5[] = {0x4f,0x67,0x67,0x53,0,0x04,
  1125. 0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00,
  1126. 0x01,0x02,0x03,0x04,2,0,0,0,
  1127. 0x6c,0x3b,0x82,0x3d,
  1128. 1,
  1129. 50};
  1130. /* packet that overspans over an entire page */
  1131. const int head1_6[] = {0x4f,0x67,0x67,0x53,0,0x02,
  1132. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1133. 0x01,0x02,0x03,0x04,0,0,0,0,
  1134. 0xff,0x7b,0x23,0x17,
  1135. 1,
  1136. 0};
  1137. const int head2_6[] = {0x4f,0x67,0x67,0x53,0,0x00,
  1138. 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
  1139. 0x01,0x02,0x03,0x04,1,0,0,0,
  1140. 0x68,0x22,0x7c,0x3d,
  1141. 255,
  1142. 100,
  1143. 255,255,255,255,255,255,255,255,
  1144. 255,255,255,255,255,255,255,255,
  1145. 255,255,255,255,255,255,255,255,
  1146. 255,255,255,255,255,255,255,255,
  1147. 255,255,255,255,255,255,255,255,
  1148. 255,255,255,255,255,255,255,255,
  1149. 255,255,255,255,255,255,255,255,
  1150. 255,255,255,255,255,255,255,255,
  1151. 255,255,255,255,255,255,255,255,
  1152. 255,255,255,255,255,255,255,255,
  1153. 255,255,255,255,255,255,255,255,
  1154. 255,255,255,255,255,255,255,255,
  1155. 255,255,255,255,255,255,255,255,
  1156. 255,255,255,255,255,255,255,255,
  1157. 255,255,255,255,255,255,255,255,
  1158. 255,255,255,255,255,255,255,255,
  1159. 255,255,255,255,255,255,255,255,
  1160. 255,255,255,255,255,255,255,255,
  1161. 255,255,255,255,255,255,255,255,
  1162. 255,255,255,255,255,255,255,255,
  1163. 255,255,255,255,255,255,255,255,
  1164. 255,255,255,255,255,255,255,255,
  1165. 255,255,255,255,255,255,255,255,
  1166. 255,255,255,255,255,255,255,255,
  1167. 255,255,255,255,255,255,255,255,
  1168. 255,255,255,255,255,255,255,255,
  1169. 255,255,255,255,255,255,255,255,
  1170. 255,255,255,255,255,255,255,255,
  1171. 255,255,255,255,255,255,255,255,
  1172. 255,255,255,255,255,255,255,255,
  1173. 255,255,255,255,255,255,255,255,
  1174. 255,255,255,255,255,255};
  1175. const int head3_6[] = {0x4f,0x67,0x67,0x53,0,0x01,
  1176. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  1177. 0x01,0x02,0x03,0x04,2,0,0,0,
  1178. 0xf4,0x87,0xba,0xf3,
  1179. 255,
  1180. 255,255,255,255,255,255,255,255,
  1181. 255,255,255,255,255,255,255,255,
  1182. 255,255,255,255,255,255,255,255,
  1183. 255,255,255,255,255,255,255,255,
  1184. 255,255,255,255,255,255,255,255,
  1185. 255,255,255,255,255,255,255,255,
  1186. 255,255,255,255,255,255,255,255,
  1187. 255,255,255,255,255,255,255,255,
  1188. 255,255,255,255,255,255,255,255,
  1189. 255,255,255,255,255,255,255,255,
  1190. 255,255,255,255,255,255,255,255,
  1191. 255,255,255,255,255,255,255,255,
  1192. 255,255,255,255,255,255,255,255,
  1193. 255,255,255,255,255,255,255,255,
  1194. 255,255,255,255,255,255,255,255,
  1195. 255,255,255,255,255,255,255,255,
  1196. 255,255,255,255,255,255,255,255,
  1197. 255,255,255,255,255,255,255,255,
  1198. 255,255,255,255,255,255,255,255,
  1199. 255,255,255,255,255,255,255,255,
  1200. 255,255,255,255,255,255,255,255,
  1201. 255,255,255,255,255,255,255,255,
  1202. 255,255,255,255,255,255,255,255,
  1203. 255,255,255,255,255,255,255,255,
  1204. 255,255,255,255,255,255,255,255,
  1205. 255,255,255,255,255,255,255,255,
  1206. 255,255,255,255,255,255,255,255,
  1207. 255,255,255,255,255,255,255,255,
  1208. 255,255,255,255,255,255,255,255,
  1209. 255,255,255,255,255,255,255,255,
  1210. 255,255,255,255,255,255,255,255,
  1211. 255,255,255,255,255,255,255};
  1212. const int head4_6[] = {0x4f,0x67,0x67,0x53,0,0x05,
  1213. 0x07,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
  1214. 0x01,0x02,0x03,0x04,3,0,0,0,
  1215. 0xf7,0x2f,0x6c,0x60,
  1216. 5,
  1217. 254,255,4,255,0};
  1218. /* packet that overspans over an entire page */
  1219. const int head1_7[] = {0x4f,0x67,0x67,0x53,0,0x02,
  1220. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1221. 0x01,0x02,0x03,0x04,0,0,0,0,
  1222. 0xff,0x7b,0x23,0x17,
  1223. 1,
  1224. 0};
  1225. const int head2_7[] = {0x4f,0x67,0x67,0x53,0,0x00,
  1226. 0x07,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
  1227. 0x01,0x02,0x03,0x04,1,0,0,0,
  1228. 0x68,0x22,0x7c,0x3d,
  1229. 255,
  1230. 100,
  1231. 255,255,255,255,255,255,255,255,
  1232. 255,255,255,255,255,255,255,255,
  1233. 255,255,255,255,255,255,255,255,
  1234. 255,255,255,255,255,255,255,255,
  1235. 255,255,255,255,255,255,255,255,
  1236. 255,255,255,255,255,255,255,255,
  1237. 255,255,255,255,255,255,255,255,
  1238. 255,255,255,255,255,255,255,255,
  1239. 255,255,255,255,255,255,255,255,
  1240. 255,255,255,255,255,255,255,255,
  1241. 255,255,255,255,255,255,255,255,
  1242. 255,255,255,255,255,255,255,255,
  1243. 255,255,255,255,255,255,255,255,
  1244. 255,255,255,255,255,255,255,255,
  1245. 255,255,255,255,255,255,255,255,
  1246. 255,255,255,255,255,255,255,255,
  1247. 255,255,255,255,255,255,255,255,
  1248. 255,255,255,255,255,255,255,255,
  1249. 255,255,255,255,255,255,255,255,
  1250. 255,255,255,255,255,255,255,255,
  1251. 255,255,255,255,255,255,255,255,
  1252. 255,255,255,255,255,255,255,255,
  1253. 255,255,255,255,255,255,255,255,
  1254. 255,255,255,255,255,255,255,255,
  1255. 255,255,255,255,255,255,255,255,
  1256. 255,255,255,255,255,255,255,255,
  1257. 255,255,255,255,255,255,255,255,
  1258. 255,255,255,255,255,255,255,255,
  1259. 255,255,255,255,255,255,255,255,
  1260. 255,255,255,255,255,255,255,255,
  1261. 255,255,255,255,255,255,255,255,
  1262. 255,255,255,255,255,255};
  1263. const int head3_7[] = {0x4f,0x67,0x67,0x53,0,0x05,
  1264. 0x07,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
  1265. 0x01,0x02,0x03,0x04,2,0,0,0,
  1266. 0xd4,0xe0,0x60,0xe5,
  1267. 1,
  1268. 0};
  1269. void test_pack(const int *pl, const int **headers, int byteskip,
  1270. int pageskip, int packetskip){
  1271. unsigned char *data=_ogg_malloc(1024*1024); /* for scripted test cases only */
  1272. long inptr=0;
  1273. long outptr=0;
  1274. long deptr=0;
  1275. long depacket=0;
  1276. long granule_pos=7,pageno=0;
  1277. int i,j,packets,pageout=pageskip;
  1278. int eosflag=0;
  1279. int bosflag=0;
  1280. int byteskipcount=0;
  1281. ogg_stream_reset(&os_en);
  1282. ogg_stream_reset(&os_de);
  1283. ogg_sync_reset(&oy);
  1284. for(packets=0;packets<packetskip;packets++)
  1285. depacket+=pl[packets];
  1286. for(packets=0;;packets++)if(pl[packets]==-1)break;
  1287. for(i=0;i<packets;i++){
  1288. /* construct a test packet */
  1289. ogg_packet op;
  1290. int len=pl[i];
  1291. op.packet=data+inptr;
  1292. op.bytes=len;
  1293. op.e_o_s=(pl[i+1]<0?1:0);
  1294. op.granulepos=granule_pos;
  1295. granule_pos+=1024;
  1296. for(j=0;j<len;j++)data[inptr++]=i+j;
  1297. /* submit the test packet */
  1298. ogg_stream_packetin(&os_en,&op);
  1299. /* retrieve any finished pages */
  1300. {
  1301. ogg_page og;
  1302. while(ogg_stream_pageout(&os_en,&og)){
  1303. /* We have a page. Check it carefully */
  1304. fprintf(stderr,"%ld, ",pageno);
  1305. if(headers[pageno]==NULL){
  1306. fprintf(stderr,"coded too many pages!\n");
  1307. exit(1);
  1308. }
  1309. check_page(data+outptr,headers[pageno],&og);
  1310. outptr+=og.body_len;
  1311. pageno++;
  1312. if(pageskip){
  1313. bosflag=1;
  1314. pageskip--;
  1315. deptr+=og.body_len;
  1316. }
  1317. /* have a complete page; submit it to sync/decode */
  1318. {
  1319. ogg_page og_de;
  1320. ogg_packet op_de,op_de2;
  1321. char *buf=ogg_sync_buffer(&oy,og.header_len+og.body_len);
  1322. char *next=buf;
  1323. byteskipcount+=og.header_len;
  1324. if(byteskipcount>byteskip){
  1325. memcpy(next,og.header,byteskipcount-byteskip);
  1326. next+=byteskipcount-byteskip;
  1327. byteskipcount=byteskip;
  1328. }
  1329. byteskipcount+=og.body_len;
  1330. if(byteskipcount>byteskip){
  1331. memcpy(next,og.body,byteskipcount-byteskip);
  1332. next+=byteskipcount-byteskip;
  1333. byteskipcount=byteskip;
  1334. }
  1335. ogg_sync_wrote(&oy,next-buf);
  1336. while(1){
  1337. int ret=ogg_sync_pageout(&oy,&og_de);
  1338. if(ret==0)break;
  1339. if(ret<0)continue;
  1340. /* got a page. Happy happy. Verify that it's good. */
  1341. fprintf(stderr,"(%d), ",pageout);
  1342. check_page(data+deptr,headers[pageout],&og_de);
  1343. deptr+=og_de.body_len;
  1344. pageout++;
  1345. /* submit it to deconstitution */
  1346. ogg_stream_pagein(&os_de,&og_de);
  1347. /* packets out? */
  1348. while(ogg_stream_packetpeek(&os_de,&op_de2)>0){
  1349. ogg_stream_packetpeek(&os_de,NULL);
  1350. ogg_stream_packetout(&os_de,&op_de); /* just catching them all */
  1351. /* verify peek and out match */
  1352. if(memcmp(&op_de,&op_de2,sizeof(op_de))){
  1353. fprintf(stderr,"packetout != packetpeek! pos=%ld\n",
  1354. depacket);
  1355. exit(1);
  1356. }
  1357. /* verify the packet! */
  1358. /* check data */
  1359. if(memcmp(data+depacket,op_de.packet,op_de.bytes)){
  1360. fprintf(stderr,"packet data mismatch in decode! pos=%ld\n",
  1361. depacket);
  1362. exit(1);
  1363. }
  1364. /* check bos flag */
  1365. if(bosflag==0 && op_de.b_o_s==0){
  1366. fprintf(stderr,"b_o_s flag not set on packet!\n");
  1367. exit(1);
  1368. }
  1369. if(bosflag && op_de.b_o_s){
  1370. fprintf(stderr,"b_o_s flag incorrectly set on packet!\n");
  1371. exit(1);
  1372. }
  1373. bosflag=1;
  1374. depacket+=op_de.bytes;
  1375. /* check eos flag */
  1376. if(eosflag){
  1377. fprintf(stderr,"Multiple decoded packets with eos flag!\n");
  1378. exit(1);
  1379. }
  1380. if(op_de.e_o_s)eosflag=1;
  1381. /* check granulepos flag */
  1382. if(op_de.granulepos!=-1){
  1383. fprintf(stderr," granule:%ld ",(long)op_de.granulepos);
  1384. }
  1385. }
  1386. }
  1387. }
  1388. }
  1389. }
  1390. }
  1391. _ogg_free(data);
  1392. if(headers[pageno]!=NULL){
  1393. fprintf(stderr,"did not write last page!\n");
  1394. exit(1);
  1395. }
  1396. if(headers[pageout]!=NULL){
  1397. fprintf(stderr,"did not decode last page!\n");
  1398. exit(1);
  1399. }
  1400. if(inptr!=outptr){
  1401. fprintf(stderr,"encoded page data incomplete!\n");
  1402. exit(1);
  1403. }
  1404. if(inptr!=deptr){
  1405. fprintf(stderr,"decoded page data incomplete!\n");
  1406. exit(1);
  1407. }
  1408. if(inptr!=depacket){
  1409. fprintf(stderr,"decoded packet data incomplete!\n");
  1410. exit(1);
  1411. }
  1412. if(!eosflag){
  1413. fprintf(stderr,"Never got a packet with EOS set!\n");
  1414. exit(1);
  1415. }
  1416. fprintf(stderr,"ok.\n");
  1417. }
  1418. int main(void){
  1419. ogg_stream_init(&os_en,0x04030201);
  1420. ogg_stream_init(&os_de,0x04030201);
  1421. ogg_sync_init(&oy);
  1422. /* Exercise each code path in the framing code. Also verify that
  1423. the checksums are working. */
  1424. {
  1425. /* 17 only */
  1426. const int packets[]={17, -1};
  1427. const int *headret[]={head1_0,NULL};
  1428. fprintf(stderr,"testing single page encoding... ");
  1429. test_pack(packets,headret,0,0,0);
  1430. }
  1431. {
  1432. /* 17, 254, 255, 256, 500, 510, 600 byte, pad */
  1433. const int packets[]={17, 254, 255, 256, 500, 510, 600, -1};
  1434. const int *headret[]={head1_1,head2_1,NULL};
  1435. fprintf(stderr,"testing basic page encoding... ");
  1436. test_pack(packets,headret,0,0,0);
  1437. }
  1438. {
  1439. /* nil packets; beginning,middle,end */
  1440. const int packets[]={0,17, 254, 255, 0, 256, 0, 500, 510, 600, 0, -1};
  1441. const int *headret[]={head1_2,head2_2,NULL};
  1442. fprintf(stderr,"testing basic nil packets... ");
  1443. test_pack(packets,headret,0,0,0);
  1444. }
  1445. {
  1446. /* large initial packet */
  1447. const int packets[]={4345,259,255,-1};
  1448. const int *headret[]={head1_3,head2_3,NULL};
  1449. fprintf(stderr,"testing initial-packet lacing > 4k... ");
  1450. test_pack(packets,headret,0,0,0);
  1451. }
  1452. {
  1453. /* continuing packet test; with page spill expansion, we have to
  1454. overflow the lacing table. */
  1455. const int packets[]={0,65500,259,255,-1};
  1456. const int *headret[]={head1_4,head2_4,head3_4,NULL};
  1457. fprintf(stderr,"testing single packet page span... ");
  1458. test_pack(packets,headret,0,0,0);
  1459. }
  1460. {
  1461. /* spill expand packet test */
  1462. const int packets[]={0,4345,259,255,0,0,-1};
  1463. const int *headret[]={head1_4b,head2_4b,head3_4b,NULL};
  1464. fprintf(stderr,"testing page spill expansion... ");
  1465. test_pack(packets,headret,0,0,0);
  1466. }
  1467. /* page with the 255 segment limit */
  1468. {
  1469. const int packets[]={0,10,10,10,10,10,10,10,10,
  1470. 10,10,10,10,10,10,10,10,
  1471. 10,10,10,10,10,10,10,10,
  1472. 10,10,10,10,10,10,10,10,
  1473. 10,10,10,10,10,10,10,10,
  1474. 10,10,10,10,10,10,10,10,
  1475. 10,10,10,10,10,10,10,10,
  1476. 10,10,10,10,10,10,10,10,
  1477. 10,10,10,10,10,10,10,10,
  1478. 10,10,10,10,10,10,10,10,
  1479. 10,10,10,10,10,10,10,10,
  1480. 10,10,10,10,10,10,10,10,
  1481. 10,10,10,10,10,10,10,10,
  1482. 10,10,10,10,10,10,10,10,
  1483. 10,10,10,10,10,10,10,10,
  1484. 10,10,10,10,10,10,10,10,
  1485. 10,10,10,10,10,10,10,10,
  1486. 10,10,10,10,10,10,10,10,
  1487. 10,10,10,10,10,10,10,10,
  1488. 10,10,10,10,10,10,10,10,
  1489. 10,10,10,10,10,10,10,10,
  1490. 10,10,10,10,10,10,10,10,
  1491. 10,10,10,10,10,10,10,10,
  1492. 10,10,10,10,10,10,10,10,
  1493. 10,10,10,10,10,10,10,10,
  1494. 10,10,10,10,10,10,10,10,
  1495. 10,10,10,10,10,10,10,10,
  1496. 10,10,10,10,10,10,10,10,
  1497. 10,10,10,10,10,10,10,10,
  1498. 10,10,10,10,10,10,10,10,
  1499. 10,10,10,10,10,10,10,10,
  1500. 10,10,10,10,10,10,10,50,-1};
  1501. const int *headret[]={head1_5,head2_5,head3_5,NULL};
  1502. fprintf(stderr,"testing max packet segments... ");
  1503. test_pack(packets,headret,0,0,0);
  1504. }
  1505. {
  1506. /* packet that overspans over an entire page */
  1507. const int packets[]={0,100,130049,259,255,-1};
  1508. const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL};
  1509. fprintf(stderr,"testing very large packets... ");
  1510. test_pack(packets,headret,0,0,0);
  1511. }
  1512. {
  1513. /* test for the libogg 1.1.1 resync in large continuation bug
  1514. found by Josh Coalson) */
  1515. const int packets[]={0,100,130049,259,255,-1};
  1516. const int *headret[]={head1_6,head2_6,head3_6,head4_6,NULL};
  1517. fprintf(stderr,"testing continuation resync in very large packets... ");
  1518. test_pack(packets,headret,100,2,3);
  1519. }
  1520. {
  1521. /* term only page. why not? */
  1522. const int packets[]={0,100,64770,-1};
  1523. const int *headret[]={head1_7,head2_7,head3_7,NULL};
  1524. fprintf(stderr,"testing zero data page (1 nil packet)... ");
  1525. test_pack(packets,headret,0,0,0);
  1526. }
  1527. {
  1528. /* build a bunch of pages for testing */
  1529. unsigned char *data=_ogg_malloc(1024*1024);
  1530. int pl[]={0, 1,1,98,4079, 1,1,2954,2057, 76,34,912,0,234,1000,1000, 1000,300,-1};
  1531. int inptr=0,i,j;
  1532. ogg_page og[5];
  1533. ogg_stream_reset(&os_en);
  1534. for(i=0;pl[i]!=-1;i++){
  1535. ogg_packet op;
  1536. int len=pl[i];
  1537. op.packet=data+inptr;
  1538. op.bytes=len;
  1539. op.e_o_s=(pl[i+1]<0?1:0);
  1540. op.granulepos=(i+1)*1000;
  1541. for(j=0;j<len;j++)data[inptr++]=i+j;
  1542. ogg_stream_packetin(&os_en,&op);
  1543. }
  1544. _ogg_free(data);
  1545. /* retrieve finished pages */
  1546. for(i=0;i<5;i++){
  1547. if(ogg_stream_pageout(&os_en,&og[i])==0){
  1548. fprintf(stderr,"Too few pages output building sync tests!\n");
  1549. exit(1);
  1550. }
  1551. copy_page(&og[i]);
  1552. }
  1553. /* Test lost pages on pagein/packetout: no rollback */
  1554. {
  1555. ogg_page temp;
  1556. ogg_packet test;
  1557. fprintf(stderr,"Testing loss of pages... ");
  1558. ogg_sync_reset(&oy);
  1559. ogg_stream_reset(&os_de);
  1560. for(i=0;i<5;i++){
  1561. memcpy(ogg_sync_buffer(&oy,og[i].header_len),og[i].header,
  1562. og[i].header_len);
  1563. ogg_sync_wrote(&oy,og[i].header_len);
  1564. memcpy(ogg_sync_buffer(&oy,og[i].body_len),og[i].body,og[i].body_len);
  1565. ogg_sync_wrote(&oy,og[i].body_len);
  1566. }
  1567. ogg_sync_pageout(&oy,&temp);
  1568. ogg_stream_pagein(&os_de,&temp);
  1569. ogg_sync_pageout(&oy,&temp);
  1570. ogg_stream_pagein(&os_de,&temp);
  1571. ogg_sync_pageout(&oy,&temp);
  1572. /* skip */
  1573. ogg_sync_pageout(&oy,&temp);
  1574. ogg_stream_pagein(&os_de,&temp);
  1575. /* do we get the expected results/packets? */
  1576. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1577. checkpacket(&test,0,0,0);
  1578. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1579. checkpacket(&test,1,1,-1);
  1580. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1581. checkpacket(&test,1,2,-1);
  1582. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1583. checkpacket(&test,98,3,-1);
  1584. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1585. checkpacket(&test,4079,4,5000);
  1586. if(ogg_stream_packetout(&os_de,&test)!=-1){
  1587. fprintf(stderr,"Error: loss of page did not return error\n");
  1588. exit(1);
  1589. }
  1590. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1591. checkpacket(&test,76,9,-1);
  1592. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1593. checkpacket(&test,34,10,-1);
  1594. fprintf(stderr,"ok.\n");
  1595. }
  1596. /* Test lost pages on pagein/packetout: rollback with continuation */
  1597. {
  1598. ogg_page temp;
  1599. ogg_packet test;
  1600. fprintf(stderr,"Testing loss of pages (rollback required)... ");
  1601. ogg_sync_reset(&oy);
  1602. ogg_stream_reset(&os_de);
  1603. for(i=0;i<5;i++){
  1604. memcpy(ogg_sync_buffer(&oy,og[i].header_len),og[i].header,
  1605. og[i].header_len);
  1606. ogg_sync_wrote(&oy,og[i].header_len);
  1607. memcpy(ogg_sync_buffer(&oy,og[i].body_len),og[i].body,og[i].body_len);
  1608. ogg_sync_wrote(&oy,og[i].body_len);
  1609. }
  1610. ogg_sync_pageout(&oy,&temp);
  1611. ogg_stream_pagein(&os_de,&temp);
  1612. ogg_sync_pageout(&oy,&temp);
  1613. ogg_stream_pagein(&os_de,&temp);
  1614. ogg_sync_pageout(&oy,&temp);
  1615. ogg_stream_pagein(&os_de,&temp);
  1616. ogg_sync_pageout(&oy,&temp);
  1617. /* skip */
  1618. ogg_sync_pageout(&oy,&temp);
  1619. ogg_stream_pagein(&os_de,&temp);
  1620. /* do we get the expected results/packets? */
  1621. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1622. checkpacket(&test,0,0,0);
  1623. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1624. checkpacket(&test,1,1,-1);
  1625. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1626. checkpacket(&test,1,2,-1);
  1627. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1628. checkpacket(&test,98,3,-1);
  1629. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1630. checkpacket(&test,4079,4,5000);
  1631. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1632. checkpacket(&test,1,5,-1);
  1633. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1634. checkpacket(&test,1,6,-1);
  1635. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1636. checkpacket(&test,2954,7,-1);
  1637. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1638. checkpacket(&test,2057,8,9000);
  1639. if(ogg_stream_packetout(&os_de,&test)!=-1){
  1640. fprintf(stderr,"Error: loss of page did not return error\n");
  1641. exit(1);
  1642. }
  1643. if(ogg_stream_packetout(&os_de,&test)!=1)error();
  1644. checkpacket(&test,300,17,18000);
  1645. fprintf(stderr,"ok.\n");
  1646. }
  1647. /* the rest only test sync */
  1648. {
  1649. ogg_page og_de;
  1650. /* Test fractional page inputs: incomplete capture */
  1651. fprintf(stderr,"Testing sync on partial inputs... ");
  1652. ogg_sync_reset(&oy);
  1653. memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
  1654. 3);
  1655. ogg_sync_wrote(&oy,3);
  1656. if(ogg_sync_pageout(&oy,&og_de)>0)error();
  1657. /* Test fractional page inputs: incomplete fixed header */
  1658. memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+3,
  1659. 20);
  1660. ogg_sync_wrote(&oy,20);
  1661. if(ogg_sync_pageout(&oy,&og_de)>0)error();
  1662. /* Test fractional page inputs: incomplete header */
  1663. memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+23,
  1664. 5);
  1665. ogg_sync_wrote(&oy,5);
  1666. if(ogg_sync_pageout(&oy,&og_de)>0)error();
  1667. /* Test fractional page inputs: incomplete body */
  1668. memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+28,
  1669. og[1].header_len-28);
  1670. ogg_sync_wrote(&oy,og[1].header_len-28);
  1671. if(ogg_sync_pageout(&oy,&og_de)>0)error();
  1672. memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,1000);
  1673. ogg_sync_wrote(&oy,1000);
  1674. if(ogg_sync_pageout(&oy,&og_de)>0)error();
  1675. memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body+1000,
  1676. og[1].body_len-1000);
  1677. ogg_sync_wrote(&oy,og[1].body_len-1000);
  1678. if(ogg_sync_pageout(&oy,&og_de)<=0)error();
  1679. fprintf(stderr,"ok.\n");
  1680. }
  1681. /* Test fractional page inputs: page + incomplete capture */
  1682. {
  1683. ogg_page og_de;
  1684. fprintf(stderr,"Testing sync on 1+partial inputs... ");
  1685. ogg_sync_reset(&oy);
  1686. memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
  1687. og[1].header_len);
  1688. ogg_sync_wrote(&oy,og[1].header_len);
  1689. memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
  1690. og[1].body_len);
  1691. ogg_sync_wrote(&oy,og[1].body_len);
  1692. memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
  1693. 20);
  1694. ogg_sync_wrote(&oy,20);
  1695. if(ogg_sync_pageout(&oy,&og_de)<=0)error();
  1696. if(ogg_sync_pageout(&oy,&og_de)>0)error();
  1697. memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header+20,
  1698. og[1].header_len-20);
  1699. ogg_sync_wrote(&oy,og[1].header_len-20);
  1700. memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
  1701. og[1].body_len);
  1702. ogg_sync_wrote(&oy,og[1].body_len);
  1703. if(ogg_sync_pageout(&oy,&og_de)<=0)error();
  1704. fprintf(stderr,"ok.\n");
  1705. }
  1706. /* Test recapture: garbage + page */
  1707. {
  1708. ogg_page og_de;
  1709. fprintf(stderr,"Testing search for capture... ");
  1710. ogg_sync_reset(&oy);
  1711. /* 'garbage' */
  1712. memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
  1713. og[1].body_len);
  1714. ogg_sync_wrote(&oy,og[1].body_len);
  1715. memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
  1716. og[1].header_len);
  1717. ogg_sync_wrote(&oy,og[1].header_len);
  1718. memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
  1719. og[1].body_len);
  1720. ogg_sync_wrote(&oy,og[1].body_len);
  1721. memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
  1722. 20);
  1723. ogg_sync_wrote(&oy,20);
  1724. if(ogg_sync_pageout(&oy,&og_de)>0)error();
  1725. if(ogg_sync_pageout(&oy,&og_de)<=0)error();
  1726. if(ogg_sync_pageout(&oy,&og_de)>0)error();
  1727. memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header+20,
  1728. og[2].header_len-20);
  1729. ogg_sync_wrote(&oy,og[2].header_len-20);
  1730. memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body,
  1731. og[2].body_len);
  1732. ogg_sync_wrote(&oy,og[2].body_len);
  1733. if(ogg_sync_pageout(&oy,&og_de)<=0)error();
  1734. fprintf(stderr,"ok.\n");
  1735. }
  1736. /* Test recapture: page + garbage + page */
  1737. {
  1738. ogg_page og_de;
  1739. fprintf(stderr,"Testing recapture... ");
  1740. ogg_sync_reset(&oy);
  1741. memcpy(ogg_sync_buffer(&oy,og[1].header_len),og[1].header,
  1742. og[1].header_len);
  1743. ogg_sync_wrote(&oy,og[1].header_len);
  1744. memcpy(ogg_sync_buffer(&oy,og[1].body_len),og[1].body,
  1745. og[1].body_len);
  1746. ogg_sync_wrote(&oy,og[1].body_len);
  1747. memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
  1748. og[2].header_len);
  1749. ogg_sync_wrote(&oy,og[2].header_len);
  1750. memcpy(ogg_sync_buffer(&oy,og[2].header_len),og[2].header,
  1751. og[2].header_len);
  1752. ogg_sync_wrote(&oy,og[2].header_len);
  1753. if(ogg_sync_pageout(&oy,&og_de)<=0)error();
  1754. memcpy(ogg_sync_buffer(&oy,og[2].body_len),og[2].body,
  1755. og[2].body_len-5);
  1756. ogg_sync_wrote(&oy,og[2].body_len-5);
  1757. memcpy(ogg_sync_buffer(&oy,og[3].header_len),og[3].header,
  1758. og[3].header_len);
  1759. ogg_sync_wrote(&oy,og[3].header_len);
  1760. memcpy(ogg_sync_buffer(&oy,og[3].body_len),og[3].body,
  1761. og[3].body_len);
  1762. ogg_sync_wrote(&oy,og[3].body_len);
  1763. if(ogg_sync_pageout(&oy,&og_de)>0)error();
  1764. if(ogg_sync_pageout(&oy,&og_de)<=0)error();
  1765. fprintf(stderr,"ok.\n");
  1766. }
  1767. /* Free page data that was previously copied */
  1768. {
  1769. for(i=0;i<5;i++){
  1770. free_page(&og[i]);
  1771. }
  1772. }
  1773. }
  1774. return(0);
  1775. }
  1776. #endif