bitwise.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. *
  4. * *
  5. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  6. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  8. * *
  9. * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003 *
  10. * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: packing variable sized words into an octet stream
  14. ********************************************************************/
  15. /* We're 'LSb' endian; if we write a word but read individual bits,
  16. then we'll read the lsb first */
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include "misc.h"
  20. #include "ogg.h"
  21. static unsigned long mask[]=
  22. {0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
  23. 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
  24. 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
  25. 0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
  26. 0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
  27. 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
  28. 0x3fffffff,0x7fffffff,0xffffffff };
  29. /* spans forward, skipping as many bytes as headend is negative; if
  30. headend is zero, simply finds next byte. If we're up to the end
  31. of the buffer, leaves headend at zero. If we've read past the end,
  32. halt the decode process. */
  33. static void _span(oggpack_buffer *b){
  34. while(b->headend-(b->headbit>>3)<1){
  35. b->headend-=b->headbit>>3;
  36. b->headbit&=0x7;
  37. if(b->head->next){
  38. b->count+=b->head->length;
  39. b->head=b->head->next;
  40. if(b->headend+b->head->length>0)
  41. b->headptr=b->head->buffer->data+b->head->begin-b->headend;
  42. b->headend+=b->head->length;
  43. }else{
  44. /* we've either met the end of decode, or gone past it. halt
  45. only if we're past */
  46. if(b->headend*8<b->headbit)
  47. /* read has fallen off the end */
  48. b->headend=-1;
  49. break;
  50. }
  51. }
  52. }
  53. void oggpack_readinit(oggpack_buffer *b,ogg_reference *r){
  54. memset(b,0,sizeof(*b));
  55. b->tail=b->head=r;
  56. b->count=0;
  57. b->headptr=b->head->buffer->data+b->head->begin;
  58. b->headend=b->head->length;
  59. _span(b);
  60. }
  61. #define _lookspan() while(!end){\
  62. head=head->next;\
  63. if(!head) return -1;\
  64. ptr=head->buffer->data + head->begin;\
  65. end=head->length;\
  66. }
  67. /* Read in bits without advancing the bitptr; bits <= 32 */
  68. long oggpack_look(oggpack_buffer *b,int bits){
  69. unsigned long m=mask[bits];
  70. unsigned long ret;
  71. bits+=b->headbit;
  72. if(bits >= b->headend<<3){
  73. int end=b->headend;
  74. unsigned char *ptr=b->headptr;
  75. ogg_reference *head=b->head;
  76. if(end<0)return -1;
  77. if(bits){
  78. _lookspan();
  79. ret=*ptr++>>b->headbit;
  80. if(bits>8){
  81. --end;
  82. _lookspan();
  83. ret|=*ptr++<<(8-b->headbit);
  84. if(bits>16){
  85. --end;
  86. _lookspan();
  87. ret|=*ptr++<<(16-b->headbit);
  88. if(bits>24){
  89. --end;
  90. _lookspan();
  91. ret|=*ptr++<<(24-b->headbit);
  92. if(bits>32 && b->headbit){
  93. --end;
  94. _lookspan();
  95. ret|=*ptr<<(32-b->headbit);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }else{
  102. /* make this a switch jump-table */
  103. ret=b->headptr[0]>>b->headbit;
  104. if(bits>8){
  105. ret|=b->headptr[1]<<(8-b->headbit);
  106. if(bits>16){
  107. ret|=b->headptr[2]<<(16-b->headbit);
  108. if(bits>24){
  109. ret|=b->headptr[3]<<(24-b->headbit);
  110. if(bits>32 && b->headbit)
  111. ret|=b->headptr[4]<<(32-b->headbit);
  112. }
  113. }
  114. }
  115. }
  116. ret&=m;
  117. return ret;
  118. }
  119. /* limited to 32 at a time */
  120. void oggpack_adv(oggpack_buffer *b,int bits){
  121. bits+=b->headbit;
  122. b->headbit=bits&7;
  123. b->headend-=(bits>>3);
  124. b->headptr+=(bits>>3);
  125. if(b->headend<1)_span(b);
  126. }
  127. int oggpack_eop(oggpack_buffer *b){
  128. if(b->headend<0)return -1;
  129. return 0;
  130. }
  131. /* bits <= 32 */
  132. long oggpack_read(oggpack_buffer *b,int bits){
  133. long ret=oggpack_look(b,bits);
  134. oggpack_adv(b,bits);
  135. return(ret);
  136. }
  137. long oggpack_bytes(oggpack_buffer *b){
  138. if(b->headend<0)return b->count+b->head->length;
  139. return b->count + b->head->length-b->headend +
  140. (b->headbit+7)/8;
  141. }
  142. long oggpack_bits(oggpack_buffer *b){
  143. if(b->headend<0)return (b->count+b->head->length)*8;
  144. return (b->count + b->head->length-b->headend)*8 +
  145. b->headbit;
  146. }
  147. /* Self test of the bitwise routines; everything else is based on
  148. them, so they damned well better be solid. */
  149. #ifdef _V_BIT_TEST
  150. #include <string.h>
  151. #include <stdlib.h>
  152. #include <stdio.h>
  153. #include "framing.c"
  154. static int ilog(unsigned long v){
  155. int ret=0;
  156. while(v){
  157. ret++;
  158. v>>=1;
  159. }
  160. return(ret);
  161. }
  162. oggpack_buffer r;
  163. oggpack_buffer o;
  164. ogg_buffer_state *bs;
  165. ogg_reference *or;
  166. #define TESTWORDS 256
  167. void report(char *in){
  168. fprintf(stderr,"%s",in);
  169. exit(1);
  170. }
  171. int getbyte(ogg_reference *or,int position){
  172. while(or && position>=or->length){
  173. position-=or->length;
  174. or=or->next;
  175. if(or==NULL){
  176. fprintf(stderr,"\n\tERROR: getbyte ran off end of buffer.\n");
  177. exit(1);
  178. }
  179. }
  180. if((position+or->begin)&1)
  181. return (or->buffer->data[(position+or->begin)>>1])&0xff;
  182. else
  183. return (or->buffer->data[(position+or->begin)>>1]>>8)&0xff;
  184. }
  185. void cliptest(unsigned long *b,int vals,int bits,int *comp,int compsize){
  186. long i,bitcount=0;
  187. ogg_reference *or=ogg_buffer_alloc(bs,64);
  188. for(i=0;i<compsize;i++)
  189. or->buffer->data[i]= comp[i];
  190. or->length=i;
  191. oggpack_readinit(&r,or);
  192. for(i=0;i<vals;i++){
  193. unsigned long test;
  194. int tbit=bits?bits:ilog(b[i]);
  195. if((test=oggpack_look(&r,tbit))==0xffffffff)
  196. report("out of data!\n");
  197. if(test!=(b[i]&mask[tbit])){
  198. fprintf(stderr,"%ld) %lx %lx\n",i,(b[i]&mask[tbit]),test);
  199. report("looked at incorrect value!\n");
  200. }
  201. if((test=oggpack_read(&r,tbit))==0xffffffff){
  202. report("premature end of data when reading!\n");
  203. }
  204. if(test!=(b[i]&mask[tbit])){
  205. fprintf(stderr,"%ld) %lx %lx\n",i,(b[i]&mask[tbit]),test);
  206. report("read incorrect value!\n");
  207. }
  208. bitcount+=tbit;
  209. if(bitcount!=oggpack_bits(&r))
  210. report("wrong number of bits while reading!\n");
  211. if((bitcount+7)/8!=oggpack_bytes(&r))
  212. report("wrong number of bytes while reading!\n");
  213. }
  214. if(oggpack_bytes(&r)!=(bitcount+7)/8)report("leftover bytes after read!\n");
  215. ogg_buffer_release(or);
  216. }
  217. void _end_verify(int count){
  218. int i;
  219. /* are the proper number of bits left over? */
  220. int leftover=count*8-oggpack_bits(&o);
  221. if(leftover>7)
  222. report("\nERROR: too many bits reported left over.\n");
  223. /* does reading to exactly byte alignment *not* trip EOF? */
  224. if(oggpack_read(&o,leftover)==-1)
  225. report("\nERROR: read to but not past exact end tripped EOF.\n");
  226. if(oggpack_bits(&o)!=count*8)
  227. report("\nERROR: read to but not past exact end reported bad bitcount.\n");
  228. /* does EOF trip properly after a single additional bit? */
  229. if(oggpack_read(&o,1)!=-1)
  230. report("\nERROR: read past exact end did not trip EOF.\n");
  231. if(oggpack_bits(&o)!=count*8)
  232. report("\nERROR: read past exact end reported bad bitcount.\n");
  233. /* does EOF stay set over additional bit reads? */
  234. for(i=0;i<=32;i++){
  235. if(oggpack_read(&o,i)!=-1)
  236. report("\nERROR: EOF did not stay set on stream.\n");
  237. if(oggpack_bits(&o)!=count*8)
  238. report("\nERROR: read past exact end reported bad bitcount.\n");
  239. }
  240. }
  241. void _end_verify2(int count){
  242. int i;
  243. /* are the proper number of bits left over? */
  244. int leftover=count*8-oggpack_bits(&o);
  245. if(leftover>7)
  246. report("\nERROR: too many bits reported left over.\n");
  247. /* does reading to exactly byte alignment *not* trip EOF? */
  248. oggpack_adv(&o,leftover);
  249. if(o.headend!=0)
  250. report("\nERROR: read to but not past exact end tripped EOF.\n");
  251. if(oggpack_bits(&o)!=count*8)
  252. report("\nERROR: read to but not past exact end reported bad bitcount.\n");
  253. /* does EOF trip properly after a single additional bit? */
  254. oggpack_adv(&o,1);
  255. if(o.headend>=0)
  256. report("\nERROR: read past exact end did not trip EOF.\n");
  257. if(oggpack_bits(&o)!=count*8)
  258. report("\nERROR: read past exact end reported bad bitcount.\n");
  259. /* does EOF stay set over additional bit reads? */
  260. for(i=0;i<=32;i++){
  261. oggpack_adv(&o,i);
  262. if(o.headend>=0)
  263. report("\nERROR: EOF did not stay set on stream.\n");
  264. if(oggpack_bits(&o)!=count*8)
  265. report("\nERROR: read past exact end reported bad bitcount.\n");
  266. }
  267. }
  268. long ogg_buffer_length(ogg_reference *or){
  269. int count=0;
  270. while(or){
  271. count+=or->length;
  272. or=or->next;
  273. }
  274. return count;
  275. }
  276. ogg_reference *ogg_buffer_extend(ogg_reference *or,long bytes){
  277. if(or){
  278. while(or->next){
  279. or=or->next;
  280. }
  281. or->next=ogg_buffer_alloc(or->buffer->ptr.owner,bytes);
  282. return(or->next);
  283. }
  284. return 0;
  285. }
  286. void ogg_buffer_posttruncate(ogg_reference *or,long pos){
  287. /* walk to the point where we want to begin truncate */
  288. while(or && pos>or->length){
  289. pos-=or->length;
  290. or=or->next;
  291. }
  292. if(or){
  293. ogg_buffer_release(or->next);
  294. or->next=0;
  295. or->length=pos;
  296. }
  297. }
  298. int main(void){
  299. long i;
  300. static unsigned long testbuffer1[]=
  301. {18,12,103948,4325,543,76,432,52,3,65,4,56,32,42,34,21,1,23,32,546,456,7,
  302. 567,56,8,8,55,3,52,342,341,4,265,7,67,86,2199,21,7,1,5,1,4};
  303. int test1size=43;
  304. static unsigned long testbuffer2[]=
  305. {216531625L,1237861823,56732452,131,3212421,12325343,34547562,12313212,
  306. 1233432,534,5,346435231,14436467,7869299,76326614,167548585,
  307. 85525151,0,12321,1,349528352};
  308. int test2size=21;
  309. static unsigned long testbuffer3[]=
  310. {1,0,14,0,1,0,12,0,1,0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1,0,0,1,
  311. 0,1,30,1,1,1,0,0,1,0,0,0,12,0,11,0,1,0,0,1};
  312. int test3size=56;
  313. static unsigned long large[]=
  314. {2136531625L,2137861823,56732452,131,3212421,12325343,34547562,12313212,
  315. 1233432,534,5,2146435231,14436467,7869299,76326614,167548585,
  316. 85525151,0,12321,1,2146528352};
  317. int onesize=33;
  318. static int one[33]={146,25,44,151,195,15,153,176,233,131,196,65,85,172,47,40,
  319. 34,242,223,136,35,222,211,86,171,50,225,135,214,75,172,
  320. 223,4};
  321. int twosize=6;
  322. static int two[6]={61,255,255,251,231,29};
  323. int threesize=54;
  324. static int three[54]={169,2,232,252,91,132,156,36,89,13,123,176,144,32,254,
  325. 142,224,85,59,121,144,79,124,23,67,90,90,216,79,23,83,
  326. 58,135,196,61,55,129,183,54,101,100,170,37,127,126,10,
  327. 100,52,4,14,18,86,77,1};
  328. int foursize=38;
  329. static int four[38]={18,6,163,252,97,194,104,131,32,1,7,82,137,42,129,11,72,
  330. 132,60,220,112,8,196,109,64,179,86,9,137,195,208,122,169,
  331. 28,2,133,0,1};
  332. int fivesize=45;
  333. static int five[45]={169,2,126,139,144,172,30,4,80,72,240,59,130,218,73,62,
  334. 241,24,210,44,4,20,0,248,116,49,135,100,110,130,181,169,
  335. 84,75,159,2,1,0,132,192,8,0,0,18,22};
  336. int sixsize=7;
  337. static int six[7]={17,177,170,242,169,19,148};
  338. /* Test read/write together */
  339. /* Later we test against pregenerated bitstreams */
  340. bs=ogg_buffer_create();
  341. fprintf(stderr,"\nSmall preclipped packing (LSb): ");
  342. cliptest(testbuffer1,test1size,0,one,onesize);
  343. fprintf(stderr,"ok.");
  344. fprintf(stderr,"\nNull bit call (LSb): ");
  345. cliptest(testbuffer3,test3size,0,two,twosize);
  346. fprintf(stderr,"ok.");
  347. fprintf(stderr,"\nLarge preclipped packing (LSb): ");
  348. cliptest(testbuffer2,test2size,0,three,threesize);
  349. fprintf(stderr,"ok.");
  350. fprintf(stderr,"\n32 bit preclipped packing (LSb): ");
  351. or=ogg_buffer_alloc(bs,128);
  352. for(i=0;i<test2size;i++){
  353. or->buffer->data[i*4] = large[i]&0xff;
  354. or->buffer->data[i*4+1] = (large[i]>>8)&0xff;
  355. or->buffer->data[i*4+2] = (large[i]>>16)&0xff;
  356. or->buffer->data[i*4+3] = (large[i]>>24)&0xff;
  357. }
  358. or->length=test2size*4;
  359. oggpack_readinit(&r,or);
  360. for(i=0;i<test2size;i++){
  361. unsigned long test;
  362. if((test=oggpack_look(&r,32))==0xffffffffUL)report("out of data. failed!");
  363. if(test!=large[i]){
  364. fprintf(stderr,"%ld != %ld (%lx!=%lx):",test,large[i],
  365. test,large[i]);
  366. report("read incorrect value!\n");
  367. }
  368. oggpack_adv(&r,32);
  369. }
  370. ogg_buffer_release(or);
  371. if(oggpack_bytes(&r)!=test2size*4)report("leftover bytes after read!\n");
  372. fprintf(stderr,"ok.");
  373. fprintf(stderr,"\nSmall unclipped packing (LSb): ");
  374. cliptest(testbuffer1,test1size,7,four,foursize);
  375. fprintf(stderr,"ok.");
  376. fprintf(stderr,"\nLarge unclipped packing (LSb): ");
  377. cliptest(testbuffer2,test2size,17,five,fivesize);
  378. fprintf(stderr,"ok.");
  379. fprintf(stderr,"\nSingle bit unclipped packing (LSb): ");
  380. cliptest(testbuffer3,test3size,1,six,sixsize);
  381. fprintf(stderr,"ok.");
  382. fprintf(stderr,"\nTesting read past end (LSb): ");
  383. {
  384. unsigned char dda[]={0,0,0,0};
  385. ogg_buffer lob={dda,8,0,{0}};
  386. ogg_reference lor={&lob,0,8,0};
  387. oggpack_readinit(&r,&lor);
  388. for(i=0;i<64;i++){
  389. if(oggpack_read(&r,1)<0){
  390. fprintf(stderr,"failed; got -1 prematurely.\n");
  391. exit(1);
  392. }
  393. }
  394. if(oggpack_look(&r,1)!=-1 ||
  395. oggpack_read(&r,1)!=-1){
  396. fprintf(stderr,"failed; read past end without -1.\n");
  397. exit(1);
  398. }
  399. }
  400. {
  401. unsigned char dda[]={0,0,0,0};
  402. ogg_buffer lob={dda,8,0,{0}};
  403. ogg_reference lor={&lob,0,8,0};
  404. unsigned long test;
  405. oggpack_readinit(&r,&lor);
  406. if((test=oggpack_read(&r,30))==0xffffffffUL ||
  407. (test=oggpack_read(&r,16))==0xffffffffUL){
  408. fprintf(stderr,"failed 2; got -1 prematurely.\n");
  409. exit(1);
  410. }
  411. if((test=oggpack_look(&r,18))==0xffffffffUL){
  412. fprintf(stderr,"failed 3; got -1 prematurely.\n");
  413. exit(1);
  414. }
  415. if((test=oggpack_look(&r,19))!=0xffffffffUL){
  416. fprintf(stderr,"failed; read past end without -1.\n");
  417. exit(1);
  418. }
  419. if((test=oggpack_look(&r,32))!=0xffffffffUL){
  420. fprintf(stderr,"failed; read past end without -1.\n");
  421. exit(1);
  422. }
  423. }
  424. fprintf(stderr,"ok.\n");
  425. /* now the scary shit: randomized testing */
  426. for(i=0;i<10000;i++){
  427. long j,count=0,count2=0,bitcount=0;
  428. unsigned long values[TESTWORDS];
  429. int len[TESTWORDS];
  430. unsigned char flat[4*TESTWORDS]; /* max possible needed size */
  431. memset(flat,0,sizeof(flat));
  432. fprintf(stderr,"\rRandomized testing (LSb)... (%ld) ",10000-i);
  433. /* generate a list of words and lengths */
  434. /* write the required number of bits out to packbuffer */
  435. {
  436. long word=0;
  437. long bit=0;
  438. int k;
  439. for(j=0;j<TESTWORDS;j++){
  440. values[j]=rand();
  441. len[j]=(rand()%33);
  442. for(k=0;k<len[j];k++){
  443. flat[word] |= ((values[j]>>k)&0x1)<<bit;
  444. bit++;
  445. bitcount++;
  446. if(bit>7){
  447. bit=0;
  448. word++;
  449. }
  450. }
  451. }
  452. }
  453. count2=(bitcount+7)>>3;
  454. /* construct random-length buffer chain from flat vector; random
  455. byte starting offset within the length of the vector */
  456. {
  457. ogg_reference *or=NULL,*orl=NULL;
  458. long pos=0;
  459. /* build buffer chain */
  460. while(count2){
  461. int ilen=(rand()%32),k;
  462. int ibegin=(rand()%32);
  463. if(ilen>count2)ilen=count2;
  464. if(or)
  465. orl=ogg_buffer_extend(orl,64);
  466. else
  467. or=orl=ogg_buffer_alloc(bs,64);
  468. orl->length=ilen;
  469. orl->begin=ibegin;
  470. for(k=0;k<ilen;k++)
  471. orl->buffer->data[ibegin++]= flat[pos++];
  472. count2-=ilen;
  473. }
  474. if(ogg_buffer_length(or)!=(bitcount+7)/8){
  475. fprintf(stderr,"\nERROR: buffer length incorrect after build.\n");
  476. exit(1);
  477. }
  478. {
  479. int begin=0; //=(rand()%TESTWORDS);
  480. int ilen=(rand()%(TESTWORDS-begin));
  481. int bitoffset,bitcount=0;
  482. unsigned long temp;
  483. for(j=0;j<begin;j++)
  484. bitcount+=len[j];
  485. or=ogg_buffer_pretruncate(or,bitcount/8);
  486. bitoffset=bitcount%=8;
  487. for(;j<begin+ilen;j++)
  488. bitcount+=len[j];
  489. ogg_buffer_posttruncate(or,((bitcount+7)/8));
  490. if((count=ogg_buffer_length(or))!=(bitcount+7)/8){
  491. fprintf(stderr,"\nERROR: buffer length incorrect after truncate.\n");
  492. exit(1);
  493. }
  494. oggpack_readinit(&o,or);
  495. /* verify bit count */
  496. if(oggpack_bits(&o)!=0){
  497. fprintf(stderr,"\nERROR: Read bitcounter not zero!\n");
  498. exit(1);
  499. }
  500. if(oggpack_bytes(&o)!=0){
  501. fprintf(stderr,"\nERROR: Read bytecounter not zero!\n");
  502. exit(1);
  503. }
  504. bitcount=bitoffset;
  505. oggpack_read(&o,bitoffset);
  506. /* read and compare to original list */
  507. for(j=begin;j<begin+ilen;j++){
  508. temp=oggpack_read(&o,len[j]);
  509. if(temp==0xffffffffUL){
  510. fprintf(stderr,"\nERROR: End of stream too soon! word: %ld,%d\n",
  511. j-begin,ilen);
  512. exit(1);
  513. }
  514. if(temp!=(values[j]&mask[len[j]])){
  515. fprintf(stderr,"\nERROR: Incorrect read %lx != %lx, word %ld, len %d\n"
  516. ,
  517. values[j]&mask[len[j]],temp,j-begin,len[j]);
  518. exit(1);
  519. }
  520. bitcount+=len[j];
  521. if(oggpack_bits(&o)!=bitcount){
  522. fprintf(stderr,"\nERROR: Read bitcounter %d != %ld!\n",
  523. bitcount,oggpack_bits(&o));
  524. exit(1);
  525. }
  526. if(oggpack_bytes(&o)!=(bitcount+7)/8){
  527. fprintf(stderr,"\nERROR: Read bytecounter %d != %ld!\n",
  528. (bitcount+7)/8,oggpack_bytes(&o));
  529. exit(1);
  530. }
  531. }
  532. _end_verify(count);
  533. /* look/adv version */
  534. oggpack_readinit(&o,or);
  535. bitcount=bitoffset;
  536. oggpack_adv(&o,bitoffset);
  537. /* read and compare to original list */
  538. for(j=begin;j<begin+ilen;j++){
  539. temp=oggpack_look(&o,len[j]);
  540. if(temp==0xffffffffUL){
  541. fprintf(stderr,"\nERROR: End of stream too soon! word: %ld\n",
  542. j-begin);
  543. exit(1);
  544. }
  545. if(temp!=(values[j]&mask[len[j]])){
  546. fprintf(stderr,"\nERROR: Incorrect look %lx != %lx, word %ld, len %d\n"
  547. ,
  548. values[j]&mask[len[j]],temp,j-begin,len[j]);
  549. exit(1);
  550. }
  551. oggpack_adv(&o,len[j]);
  552. bitcount+=len[j];
  553. if(oggpack_bits(&o)!=bitcount){
  554. fprintf(stderr,"\nERROR: Look/Adv bitcounter %d != %ld!\n",
  555. bitcount,oggpack_bits(&o));
  556. exit(1);
  557. }
  558. if(oggpack_bytes(&o)!=(bitcount+7)/8){
  559. fprintf(stderr,"\nERROR: Look/Adv bytecounter %d != %ld!\n",
  560. (bitcount+7)/8,oggpack_bytes(&o));
  561. exit(1);
  562. }
  563. }
  564. _end_verify2(count);
  565. }
  566. ogg_buffer_release(or);
  567. }
  568. }
  569. fprintf(stderr,"\rRandomized testing (LSb)... ok. \n");
  570. return(0);
  571. }
  572. #endif