framing.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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