bitwise.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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-2014 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: packing variable sized words into an octet stream
  13. last mod: $Id: bitwise.c 19149 2014-05-27 16:26:23Z giles $
  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 <limits.h>
  20. #include <ogg/ogg.h>
  21. #define BUFFER_INCREMENT 256
  22. static const unsigned long mask[]=
  23. {0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
  24. 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
  25. 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
  26. 0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
  27. 0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
  28. 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
  29. 0x3fffffff,0x7fffffff,0xffffffff };
  30. static const unsigned int mask8B[]=
  31. {0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};
  32. void oggpack_writeinit(oggpack_buffer *b){
  33. memset(b,0,sizeof(*b));
  34. b->ptr=b->buffer=_ogg_malloc(BUFFER_INCREMENT);
  35. b->buffer[0]='\0';
  36. b->storage=BUFFER_INCREMENT;
  37. }
  38. void oggpackB_writeinit(oggpack_buffer *b){
  39. oggpack_writeinit(b);
  40. }
  41. int oggpack_writecheck(oggpack_buffer *b){
  42. if(!b->ptr || !b->storage)return -1;
  43. return 0;
  44. }
  45. int oggpackB_writecheck(oggpack_buffer *b){
  46. return oggpack_writecheck(b);
  47. }
  48. void oggpack_writetrunc(oggpack_buffer *b,long bits){
  49. long bytes=bits>>3;
  50. if(b->ptr){
  51. bits-=bytes*8;
  52. b->ptr=b->buffer+bytes;
  53. b->endbit=bits;
  54. b->endbyte=bytes;
  55. *b->ptr&=mask[bits];
  56. }
  57. }
  58. void oggpackB_writetrunc(oggpack_buffer *b,long bits){
  59. long bytes=bits>>3;
  60. if(b->ptr){
  61. bits-=bytes*8;
  62. b->ptr=b->buffer+bytes;
  63. b->endbit=bits;
  64. b->endbyte=bytes;
  65. *b->ptr&=mask8B[bits];
  66. }
  67. }
  68. /* Takes only up to 32 bits. */
  69. void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){
  70. if(bits<0 || bits>32) goto err;
  71. if(b->endbyte>=b->storage-4){
  72. void *ret;
  73. if(!b->ptr)return;
  74. if(b->storage>LONG_MAX-BUFFER_INCREMENT) goto err;
  75. ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
  76. if(!ret) goto err;
  77. b->buffer=ret;
  78. b->storage+=BUFFER_INCREMENT;
  79. b->ptr=b->buffer+b->endbyte;
  80. }
  81. value&=mask[bits];
  82. bits+=b->endbit;
  83. b->ptr[0]|=value<<b->endbit;
  84. if(bits>=8){
  85. b->ptr[1]=(unsigned char)(value>>(8-b->endbit));
  86. if(bits>=16){
  87. b->ptr[2]=(unsigned char)(value>>(16-b->endbit));
  88. if(bits>=24){
  89. b->ptr[3]=(unsigned char)(value>>(24-b->endbit));
  90. if(bits>=32){
  91. if(b->endbit)
  92. b->ptr[4]=(unsigned char)(value>>(32-b->endbit));
  93. else
  94. b->ptr[4]=0;
  95. }
  96. }
  97. }
  98. }
  99. b->endbyte+=bits/8;
  100. b->ptr+=bits/8;
  101. b->endbit=bits&7;
  102. return;
  103. err:
  104. oggpack_writeclear(b);
  105. }
  106. /* Takes only up to 32 bits. */
  107. void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits){
  108. if(bits<0 || bits>32) goto err;
  109. if(b->endbyte>=b->storage-4){
  110. void *ret;
  111. if(!b->ptr)return;
  112. if(b->storage>LONG_MAX-BUFFER_INCREMENT) goto err;
  113. ret=_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
  114. if(!ret) goto err;
  115. b->buffer=ret;
  116. b->storage+=BUFFER_INCREMENT;
  117. b->ptr=b->buffer+b->endbyte;
  118. }
  119. value=(value&mask[bits])<<(32-bits);
  120. bits+=b->endbit;
  121. b->ptr[0]|=value>>(24+b->endbit);
  122. if(bits>=8){
  123. b->ptr[1]=(unsigned char)(value>>(16+b->endbit));
  124. if(bits>=16){
  125. b->ptr[2]=(unsigned char)(value>>(8+b->endbit));
  126. if(bits>=24){
  127. b->ptr[3]=(unsigned char)(value>>(b->endbit));
  128. if(bits>=32){
  129. if(b->endbit)
  130. b->ptr[4]=(unsigned char)(value<<(8-b->endbit));
  131. else
  132. b->ptr[4]=0;
  133. }
  134. }
  135. }
  136. }
  137. b->endbyte+=bits/8;
  138. b->ptr+=bits/8;
  139. b->endbit=bits&7;
  140. return;
  141. err:
  142. oggpack_writeclear(b);
  143. }
  144. void oggpack_writealign(oggpack_buffer *b){
  145. int bits=8-b->endbit;
  146. if(bits<8)
  147. oggpack_write(b,0,bits);
  148. }
  149. void oggpackB_writealign(oggpack_buffer *b){
  150. int bits=8-b->endbit;
  151. if(bits<8)
  152. oggpackB_write(b,0,bits);
  153. }
  154. static void oggpack_writecopy_helper(oggpack_buffer *b,
  155. void *source,
  156. long bits,
  157. void (*w)(oggpack_buffer *,
  158. unsigned long,
  159. int),
  160. int msb){
  161. unsigned char *ptr=(unsigned char *)source;
  162. long bytes=bits/8;
  163. long pbytes=(b->endbit+bits)/8;
  164. bits-=bytes*8;
  165. /* expand storage up-front */
  166. if(b->endbyte+pbytes>=b->storage){
  167. void *ret;
  168. if(!b->ptr) goto err;
  169. if(b->storage>b->endbyte+pbytes+BUFFER_INCREMENT) goto err;
  170. b->storage=b->endbyte+pbytes+BUFFER_INCREMENT;
  171. ret=_ogg_realloc(b->buffer,b->storage);
  172. if(!ret) goto err;
  173. b->buffer=ret;
  174. b->ptr=b->buffer+b->endbyte;
  175. }
  176. /* copy whole octets */
  177. if(b->endbit){
  178. int i;
  179. /* unaligned copy. Do it the hard way. */
  180. for(i=0;i<bytes;i++)
  181. w(b,(unsigned long)(ptr[i]),8);
  182. }else{
  183. /* aligned block copy */
  184. memmove(b->ptr,source,bytes);
  185. b->ptr+=bytes;
  186. b->endbyte+=bytes;
  187. *b->ptr=0;
  188. }
  189. /* copy trailing bits */
  190. if(bits){
  191. if(msb)
  192. w(b,(unsigned long)(ptr[bytes]>>(8-bits)),bits);
  193. else
  194. w(b,(unsigned long)(ptr[bytes]),bits);
  195. }
  196. return;
  197. err:
  198. oggpack_writeclear(b);
  199. }
  200. void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){
  201. oggpack_writecopy_helper(b,source,bits,oggpack_write,0);
  202. }
  203. void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits){
  204. oggpack_writecopy_helper(b,source,bits,oggpackB_write,1);
  205. }
  206. void oggpack_reset(oggpack_buffer *b){
  207. if(!b->ptr)return;
  208. b->ptr=b->buffer;
  209. b->buffer[0]=0;
  210. b->endbit=b->endbyte=0;
  211. }
  212. void oggpackB_reset(oggpack_buffer *b){
  213. oggpack_reset(b);
  214. }
  215. void oggpack_writeclear(oggpack_buffer *b){
  216. if(b->buffer)_ogg_free(b->buffer);
  217. memset(b,0,sizeof(*b));
  218. }
  219. void oggpackB_writeclear(oggpack_buffer *b){
  220. oggpack_writeclear(b);
  221. }
  222. void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
  223. memset(b,0,sizeof(*b));
  224. b->buffer=b->ptr=buf;
  225. b->storage=bytes;
  226. }
  227. void oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
  228. oggpack_readinit(b,buf,bytes);
  229. }
  230. /* Read in bits without advancing the bitptr; bits <= 32 */
  231. long oggpack_look(oggpack_buffer *b,int bits){
  232. unsigned long ret;
  233. unsigned long m;
  234. if(bits<0 || bits>32) return -1;
  235. m=mask[bits];
  236. bits+=b->endbit;
  237. if(b->endbyte >= b->storage-4){
  238. /* not the main path */
  239. if(b->endbyte > b->storage-((bits+7)>>3)) return -1;
  240. /* special case to avoid reading b->ptr[0], which might be past the end of
  241. the buffer; also skips some useless accounting */
  242. else if(!bits)return(0L);
  243. }
  244. ret=b->ptr[0]>>b->endbit;
  245. if(bits>8){
  246. ret|=b->ptr[1]<<(8-b->endbit);
  247. if(bits>16){
  248. ret|=b->ptr[2]<<(16-b->endbit);
  249. if(bits>24){
  250. ret|=b->ptr[3]<<(24-b->endbit);
  251. if(bits>32 && b->endbit)
  252. ret|=b->ptr[4]<<(32-b->endbit);
  253. }
  254. }
  255. }
  256. return(m&ret);
  257. }
  258. /* Read in bits without advancing the bitptr; bits <= 32 */
  259. long oggpackB_look(oggpack_buffer *b,int bits){
  260. unsigned long ret;
  261. int m=32-bits;
  262. if(m<0 || m>32) return -1;
  263. bits+=b->endbit;
  264. if(b->endbyte >= b->storage-4){
  265. /* not the main path */
  266. if(b->endbyte > b->storage-((bits+7)>>3)) return -1;
  267. /* special case to avoid reading b->ptr[0], which might be past the end of
  268. the buffer; also skips some useless accounting */
  269. else if(!bits)return(0L);
  270. }
  271. ret=b->ptr[0]<<(24+b->endbit);
  272. if(bits>8){
  273. ret|=b->ptr[1]<<(16+b->endbit);
  274. if(bits>16){
  275. ret|=b->ptr[2]<<(8+b->endbit);
  276. if(bits>24){
  277. ret|=b->ptr[3]<<(b->endbit);
  278. if(bits>32 && b->endbit)
  279. ret|=b->ptr[4]>>(8-b->endbit);
  280. }
  281. }
  282. }
  283. return ((ret&0xffffffff)>>(m>>1))>>((m+1)>>1);
  284. }
  285. long oggpack_look1(oggpack_buffer *b){
  286. if(b->endbyte>=b->storage)return(-1);
  287. return((b->ptr[0]>>b->endbit)&1);
  288. }
  289. long oggpackB_look1(oggpack_buffer *b){
  290. if(b->endbyte>=b->storage)return(-1);
  291. return((b->ptr[0]>>(7-b->endbit))&1);
  292. }
  293. void oggpack_adv(oggpack_buffer *b,int bits){
  294. bits+=b->endbit;
  295. if(b->endbyte > b->storage-((bits+7)>>3)) goto overflow;
  296. b->ptr+=bits/8;
  297. b->endbyte+=bits/8;
  298. b->endbit=bits&7;
  299. return;
  300. overflow:
  301. b->ptr=NULL;
  302. b->endbyte=b->storage;
  303. b->endbit=1;
  304. }
  305. void oggpackB_adv(oggpack_buffer *b,int bits){
  306. oggpack_adv(b,bits);
  307. }
  308. void oggpack_adv1(oggpack_buffer *b){
  309. if(++(b->endbit)>7){
  310. b->endbit=0;
  311. b->ptr++;
  312. b->endbyte++;
  313. }
  314. }
  315. void oggpackB_adv1(oggpack_buffer *b){
  316. oggpack_adv1(b);
  317. }
  318. /* bits <= 32 */
  319. long oggpack_read(oggpack_buffer *b,int bits){
  320. long ret;
  321. unsigned long m;
  322. if(bits<0 || bits>32) goto err;
  323. m=mask[bits];
  324. bits+=b->endbit;
  325. if(b->endbyte >= b->storage-4){
  326. /* not the main path */
  327. if(b->endbyte > b->storage-((bits+7)>>3)) goto overflow;
  328. /* special case to avoid reading b->ptr[0], which might be past the end of
  329. the buffer; also skips some useless accounting */
  330. else if(!bits)return(0L);
  331. }
  332. ret=b->ptr[0]>>b->endbit;
  333. if(bits>8){
  334. ret|=b->ptr[1]<<(8-b->endbit);
  335. if(bits>16){
  336. ret|=b->ptr[2]<<(16-b->endbit);
  337. if(bits>24){
  338. ret|=b->ptr[3]<<(24-b->endbit);
  339. if(bits>32 && b->endbit){
  340. ret|=b->ptr[4]<<(32-b->endbit);
  341. }
  342. }
  343. }
  344. }
  345. ret&=m;
  346. b->ptr+=bits/8;
  347. b->endbyte+=bits/8;
  348. b->endbit=bits&7;
  349. return ret;
  350. overflow:
  351. err:
  352. b->ptr=NULL;
  353. b->endbyte=b->storage;
  354. b->endbit=1;
  355. return -1L;
  356. }
  357. /* bits <= 32 */
  358. long oggpackB_read(oggpack_buffer *b,int bits){
  359. long ret;
  360. long m=32-bits;
  361. if(m<0 || m>32) goto err;
  362. bits+=b->endbit;
  363. if(b->endbyte+4>=b->storage){
  364. /* not the main path */
  365. if(b->endbyte > b->storage-((bits+7)>>3)) goto overflow;
  366. /* special case to avoid reading b->ptr[0], which might be past the end of
  367. the buffer; also skips some useless accounting */
  368. else if(!bits)return(0L);
  369. }
  370. ret=b->ptr[0]<<(24+b->endbit);
  371. if(bits>8){
  372. ret|=b->ptr[1]<<(16+b->endbit);
  373. if(bits>16){
  374. ret|=b->ptr[2]<<(8+b->endbit);
  375. if(bits>24){
  376. ret|=b->ptr[3]<<(b->endbit);
  377. if(bits>32 && b->endbit)
  378. ret|=b->ptr[4]>>(8-b->endbit);
  379. }
  380. }
  381. }
  382. ret=((ret&0xffffffffUL)>>(m>>1))>>((m+1)>>1);
  383. b->ptr+=bits/8;
  384. b->endbyte+=bits/8;
  385. b->endbit=bits&7;
  386. return ret;
  387. overflow:
  388. err:
  389. b->ptr=NULL;
  390. b->endbyte=b->storage;
  391. b->endbit=1;
  392. return -1L;
  393. }
  394. long oggpack_read1(oggpack_buffer *b){
  395. long ret;
  396. if(b->endbyte >= b->storage) goto overflow;
  397. ret=(b->ptr[0]>>b->endbit)&1;
  398. b->endbit++;
  399. if(b->endbit>7){
  400. b->endbit=0;
  401. b->ptr++;
  402. b->endbyte++;
  403. }
  404. return ret;
  405. overflow:
  406. b->ptr=NULL;
  407. b->endbyte=b->storage;
  408. b->endbit=1;
  409. return -1L;
  410. }
  411. long oggpackB_read1(oggpack_buffer *b){
  412. long ret;
  413. if(b->endbyte >= b->storage) goto overflow;
  414. ret=(b->ptr[0]>>(7-b->endbit))&1;
  415. b->endbit++;
  416. if(b->endbit>7){
  417. b->endbit=0;
  418. b->ptr++;
  419. b->endbyte++;
  420. }
  421. return ret;
  422. overflow:
  423. b->ptr=NULL;
  424. b->endbyte=b->storage;
  425. b->endbit=1;
  426. return -1L;
  427. }
  428. long oggpack_bytes(oggpack_buffer *b){
  429. return(b->endbyte+(b->endbit+7)/8);
  430. }
  431. long oggpack_bits(oggpack_buffer *b){
  432. return(b->endbyte*8+b->endbit);
  433. }
  434. long oggpackB_bytes(oggpack_buffer *b){
  435. return oggpack_bytes(b);
  436. }
  437. long oggpackB_bits(oggpack_buffer *b){
  438. return oggpack_bits(b);
  439. }
  440. unsigned char *oggpack_get_buffer(oggpack_buffer *b){
  441. return(b->buffer);
  442. }
  443. unsigned char *oggpackB_get_buffer(oggpack_buffer *b){
  444. return oggpack_get_buffer(b);
  445. }
  446. /* Self test of the bitwise routines; everything else is based on
  447. them, so they damned well better be solid. */
  448. #ifdef _V_SELFTEST
  449. #include <stdio.h>
  450. static int ilog(unsigned int v){
  451. int ret=0;
  452. while(v){
  453. ret++;
  454. v>>=1;
  455. }
  456. return(ret);
  457. }
  458. oggpack_buffer o;
  459. oggpack_buffer r;
  460. void report(char *in){
  461. fprintf(stderr,"%s",in);
  462. exit(1);
  463. }
  464. void cliptest(unsigned long *b,int vals,int bits,int *comp,int compsize){
  465. long bytes,i;
  466. unsigned char *buffer;
  467. oggpack_reset(&o);
  468. for(i=0;i<vals;i++)
  469. oggpack_write(&o,b[i],bits?bits:ilog(b[i]));
  470. buffer=oggpack_get_buffer(&o);
  471. bytes=oggpack_bytes(&o);
  472. if(bytes!=compsize)report("wrong number of bytes!\n");
  473. for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
  474. for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
  475. report("wrote incorrect value!\n");
  476. }
  477. oggpack_readinit(&r,buffer,bytes);
  478. for(i=0;i<vals;i++){
  479. int tbit=bits?bits:ilog(b[i]);
  480. if(oggpack_look(&r,tbit)==-1)
  481. report("out of data!\n");
  482. if(oggpack_look(&r,tbit)!=(b[i]&mask[tbit]))
  483. report("looked at incorrect value!\n");
  484. if(tbit==1)
  485. if(oggpack_look1(&r)!=(b[i]&mask[tbit]))
  486. report("looked at single bit incorrect value!\n");
  487. if(tbit==1){
  488. if(oggpack_read1(&r)!=(b[i]&mask[tbit]))
  489. report("read incorrect single bit value!\n");
  490. }else{
  491. if(oggpack_read(&r,tbit)!=(b[i]&mask[tbit]))
  492. report("read incorrect value!\n");
  493. }
  494. }
  495. if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
  496. }
  497. void cliptestB(unsigned long *b,int vals,int bits,int *comp,int compsize){
  498. long bytes,i;
  499. unsigned char *buffer;
  500. oggpackB_reset(&o);
  501. for(i=0;i<vals;i++)
  502. oggpackB_write(&o,b[i],bits?bits:ilog(b[i]));
  503. buffer=oggpackB_get_buffer(&o);
  504. bytes=oggpackB_bytes(&o);
  505. if(bytes!=compsize)report("wrong number of bytes!\n");
  506. for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
  507. for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
  508. report("wrote incorrect value!\n");
  509. }
  510. oggpackB_readinit(&r,buffer,bytes);
  511. for(i=0;i<vals;i++){
  512. int tbit=bits?bits:ilog(b[i]);
  513. if(oggpackB_look(&r,tbit)==-1)
  514. report("out of data!\n");
  515. if(oggpackB_look(&r,tbit)!=(b[i]&mask[tbit]))
  516. report("looked at incorrect value!\n");
  517. if(tbit==1)
  518. if(oggpackB_look1(&r)!=(b[i]&mask[tbit]))
  519. report("looked at single bit incorrect value!\n");
  520. if(tbit==1){
  521. if(oggpackB_read1(&r)!=(b[i]&mask[tbit]))
  522. report("read incorrect single bit value!\n");
  523. }else{
  524. if(oggpackB_read(&r,tbit)!=(b[i]&mask[tbit]))
  525. report("read incorrect value!\n");
  526. }
  527. }
  528. if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
  529. }
  530. void copytest(int prefill, int copy){
  531. oggpack_buffer source_write;
  532. oggpack_buffer dest_write;
  533. oggpack_buffer source_read;
  534. oggpack_buffer dest_read;
  535. unsigned char *source;
  536. unsigned char *dest;
  537. long source_bytes,dest_bytes;
  538. int i;
  539. oggpack_writeinit(&source_write);
  540. oggpack_writeinit(&dest_write);
  541. for(i=0;i<(prefill+copy+7)/8;i++)
  542. oggpack_write(&source_write,(i^0x5a)&0xff,8);
  543. source=oggpack_get_buffer(&source_write);
  544. source_bytes=oggpack_bytes(&source_write);
  545. /* prefill */
  546. oggpack_writecopy(&dest_write,source,prefill);
  547. /* check buffers; verify end byte masking */
  548. dest=oggpack_get_buffer(&dest_write);
  549. dest_bytes=oggpack_bytes(&dest_write);
  550. if(dest_bytes!=(prefill+7)/8){
  551. fprintf(stderr,"wrong number of bytes after prefill! %ld!=%d\n",dest_bytes,(prefill+7)/8);
  552. exit(1);
  553. }
  554. oggpack_readinit(&source_read,source,source_bytes);
  555. oggpack_readinit(&dest_read,dest,dest_bytes);
  556. for(i=0;i<prefill;i+=8){
  557. int s=oggpack_read(&source_read,prefill-i<8?prefill-i:8);
  558. int d=oggpack_read(&dest_read,prefill-i<8?prefill-i:8);
  559. if(s!=d){
  560. fprintf(stderr,"prefill=%d mismatch! byte %d, %x!=%x\n",prefill,i/8,s,d);
  561. exit(1);
  562. }
  563. }
  564. if(prefill<dest_bytes){
  565. if(oggpack_read(&dest_read,dest_bytes-prefill)!=0){
  566. fprintf(stderr,"prefill=%d mismatch! trailing bits not zero\n",prefill);
  567. exit(1);
  568. }
  569. }
  570. /* second copy */
  571. oggpack_writecopy(&dest_write,source,copy);
  572. /* check buffers; verify end byte masking */
  573. dest=oggpack_get_buffer(&dest_write);
  574. dest_bytes=oggpack_bytes(&dest_write);
  575. if(dest_bytes!=(copy+prefill+7)/8){
  576. fprintf(stderr,"wrong number of bytes after prefill+copy! %ld!=%d\n",dest_bytes,(copy+prefill+7)/8);
  577. exit(1);
  578. }
  579. oggpack_readinit(&source_read,source,source_bytes);
  580. oggpack_readinit(&dest_read,dest,dest_bytes);
  581. for(i=0;i<prefill;i+=8){
  582. int s=oggpack_read(&source_read,prefill-i<8?prefill-i:8);
  583. int d=oggpack_read(&dest_read,prefill-i<8?prefill-i:8);
  584. if(s!=d){
  585. fprintf(stderr,"prefill=%d mismatch! byte %d, %x!=%x\n",prefill,i/8,s,d);
  586. exit(1);
  587. }
  588. }
  589. oggpack_readinit(&source_read,source,source_bytes);
  590. for(i=0;i<copy;i+=8){
  591. int s=oggpack_read(&source_read,copy-i<8?copy-i:8);
  592. int d=oggpack_read(&dest_read,copy-i<8?copy-i:8);
  593. if(s!=d){
  594. fprintf(stderr,"prefill=%d copy=%d mismatch! byte %d, %x!=%x\n",prefill,copy,i/8,s,d);
  595. exit(1);
  596. }
  597. }
  598. if(copy+prefill<dest_bytes){
  599. if(oggpack_read(&dest_read,dest_bytes-copy-prefill)!=0){
  600. fprintf(stderr,"prefill=%d copy=%d mismatch! trailing bits not zero\n",prefill,copy);
  601. exit(1);
  602. }
  603. }
  604. oggpack_writeclear(&source_write);
  605. oggpack_writeclear(&dest_write);
  606. }
  607. void copytestB(int prefill, int copy){
  608. oggpack_buffer source_write;
  609. oggpack_buffer dest_write;
  610. oggpack_buffer source_read;
  611. oggpack_buffer dest_read;
  612. unsigned char *source;
  613. unsigned char *dest;
  614. long source_bytes,dest_bytes;
  615. int i;
  616. oggpackB_writeinit(&source_write);
  617. oggpackB_writeinit(&dest_write);
  618. for(i=0;i<(prefill+copy+7)/8;i++)
  619. oggpackB_write(&source_write,(i^0x5a)&0xff,8);
  620. source=oggpackB_get_buffer(&source_write);
  621. source_bytes=oggpackB_bytes(&source_write);
  622. /* prefill */
  623. oggpackB_writecopy(&dest_write,source,prefill);
  624. /* check buffers; verify end byte masking */
  625. dest=oggpackB_get_buffer(&dest_write);
  626. dest_bytes=oggpackB_bytes(&dest_write);
  627. if(dest_bytes!=(prefill+7)/8){
  628. fprintf(stderr,"wrong number of bytes after prefill! %ld!=%d\n",dest_bytes,(prefill+7)/8);
  629. exit(1);
  630. }
  631. oggpackB_readinit(&source_read,source,source_bytes);
  632. oggpackB_readinit(&dest_read,dest,dest_bytes);
  633. for(i=0;i<prefill;i+=8){
  634. int s=oggpackB_read(&source_read,prefill-i<8?prefill-i:8);
  635. int d=oggpackB_read(&dest_read,prefill-i<8?prefill-i:8);
  636. if(s!=d){
  637. fprintf(stderr,"prefill=%d mismatch! byte %d, %x!=%x\n",prefill,i/8,s,d);
  638. exit(1);
  639. }
  640. }
  641. if(prefill<dest_bytes){
  642. if(oggpackB_read(&dest_read,dest_bytes-prefill)!=0){
  643. fprintf(stderr,"prefill=%d mismatch! trailing bits not zero\n",prefill);
  644. exit(1);
  645. }
  646. }
  647. /* second copy */
  648. oggpackB_writecopy(&dest_write,source,copy);
  649. /* check buffers; verify end byte masking */
  650. dest=oggpackB_get_buffer(&dest_write);
  651. dest_bytes=oggpackB_bytes(&dest_write);
  652. if(dest_bytes!=(copy+prefill+7)/8){
  653. fprintf(stderr,"wrong number of bytes after prefill+copy! %ld!=%d\n",dest_bytes,(copy+prefill+7)/8);
  654. exit(1);
  655. }
  656. oggpackB_readinit(&source_read,source,source_bytes);
  657. oggpackB_readinit(&dest_read,dest,dest_bytes);
  658. for(i=0;i<prefill;i+=8){
  659. int s=oggpackB_read(&source_read,prefill-i<8?prefill-i:8);
  660. int d=oggpackB_read(&dest_read,prefill-i<8?prefill-i:8);
  661. if(s!=d){
  662. fprintf(stderr,"prefill=%d mismatch! byte %d, %x!=%x\n",prefill,i/8,s,d);
  663. exit(1);
  664. }
  665. }
  666. oggpackB_readinit(&source_read,source,source_bytes);
  667. for(i=0;i<copy;i+=8){
  668. int s=oggpackB_read(&source_read,copy-i<8?copy-i:8);
  669. int d=oggpackB_read(&dest_read,copy-i<8?copy-i:8);
  670. if(s!=d){
  671. fprintf(stderr,"prefill=%d copy=%d mismatch! byte %d, %x!=%x\n",prefill,copy,i/8,s,d);
  672. exit(1);
  673. }
  674. }
  675. if(copy+prefill<dest_bytes){
  676. if(oggpackB_read(&dest_read,dest_bytes-copy-prefill)!=0){
  677. fprintf(stderr,"prefill=%d copy=%d mismatch! trailing bits not zero\n",prefill,copy);
  678. exit(1);
  679. }
  680. }
  681. oggpackB_writeclear(&source_write);
  682. oggpackB_writeclear(&dest_write);
  683. }
  684. int main(void){
  685. unsigned char *buffer;
  686. long bytes,i,j;
  687. static unsigned long testbuffer1[]=
  688. {18,12,103948,4325,543,76,432,52,3,65,4,56,32,42,34,21,1,23,32,546,456,7,
  689. 567,56,8,8,55,3,52,342,341,4,265,7,67,86,2199,21,7,1,5,1,4};
  690. int test1size=43;
  691. static unsigned long testbuffer2[]=
  692. {216531625L,1237861823,56732452,131,3212421,12325343,34547562,12313212,
  693. 1233432,534,5,346435231,14436467,7869299,76326614,167548585,
  694. 85525151,0,12321,1,349528352};
  695. int test2size=21;
  696. static unsigned long testbuffer3[]=
  697. {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,
  698. 0,1,30,1,1,1,0,0,1,0,0,0,12,0,11,0,1,0,0,1};
  699. int test3size=56;
  700. static unsigned long large[]=
  701. {2136531625L,2137861823,56732452,131,3212421,12325343,34547562,12313212,
  702. 1233432,534,5,2146435231,14436467,7869299,76326614,167548585,
  703. 85525151,0,12321,1,2146528352};
  704. int onesize=33;
  705. static int one[33]={146,25,44,151,195,15,153,176,233,131,196,65,85,172,47,40,
  706. 34,242,223,136,35,222,211,86,171,50,225,135,214,75,172,
  707. 223,4};
  708. static int oneB[33]={150,101,131,33,203,15,204,216,105,193,156,65,84,85,222,
  709. 8,139,145,227,126,34,55,244,171,85,100,39,195,173,18,
  710. 245,251,128};
  711. int twosize=6;
  712. static int two[6]={61,255,255,251,231,29};
  713. static int twoB[6]={247,63,255,253,249,120};
  714. int threesize=54;
  715. static int three[54]={169,2,232,252,91,132,156,36,89,13,123,176,144,32,254,
  716. 142,224,85,59,121,144,79,124,23,67,90,90,216,79,23,83,
  717. 58,135,196,61,55,129,183,54,101,100,170,37,127,126,10,
  718. 100,52,4,14,18,86,77,1};
  719. static int threeB[54]={206,128,42,153,57,8,183,251,13,89,36,30,32,144,183,
  720. 130,59,240,121,59,85,223,19,228,180,134,33,107,74,98,
  721. 233,253,196,135,63,2,110,114,50,155,90,127,37,170,104,
  722. 200,20,254,4,58,106,176,144,0};
  723. int foursize=38;
  724. static int four[38]={18,6,163,252,97,194,104,131,32,1,7,82,137,42,129,11,72,
  725. 132,60,220,112,8,196,109,64,179,86,9,137,195,208,122,169,
  726. 28,2,133,0,1};
  727. static int fourB[38]={36,48,102,83,243,24,52,7,4,35,132,10,145,21,2,93,2,41,
  728. 1,219,184,16,33,184,54,149,170,132,18,30,29,98,229,67,
  729. 129,10,4,32};
  730. int fivesize=45;
  731. static int five[45]={169,2,126,139,144,172,30,4,80,72,240,59,130,218,73,62,
  732. 241,24,210,44,4,20,0,248,116,49,135,100,110,130,181,169,
  733. 84,75,159,2,1,0,132,192,8,0,0,18,22};
  734. static int fiveB[45]={1,84,145,111,245,100,128,8,56,36,40,71,126,78,213,226,
  735. 124,105,12,0,133,128,0,162,233,242,67,152,77,205,77,
  736. 172,150,169,129,79,128,0,6,4,32,0,27,9,0};
  737. int sixsize=7;
  738. static int six[7]={17,177,170,242,169,19,148};
  739. static int sixB[7]={136,141,85,79,149,200,41};
  740. /* Test read/write together */
  741. /* Later we test against pregenerated bitstreams */
  742. oggpack_writeinit(&o);
  743. fprintf(stderr,"\nSmall preclipped packing (LSb): ");
  744. cliptest(testbuffer1,test1size,0,one,onesize);
  745. fprintf(stderr,"ok.");
  746. fprintf(stderr,"\nNull bit call (LSb): ");
  747. cliptest(testbuffer3,test3size,0,two,twosize);
  748. fprintf(stderr,"ok.");
  749. fprintf(stderr,"\nLarge preclipped packing (LSb): ");
  750. cliptest(testbuffer2,test2size,0,three,threesize);
  751. fprintf(stderr,"ok.");
  752. fprintf(stderr,"\n32 bit preclipped packing (LSb): ");
  753. oggpack_reset(&o);
  754. for(i=0;i<test2size;i++)
  755. oggpack_write(&o,large[i],32);
  756. buffer=oggpack_get_buffer(&o);
  757. bytes=oggpack_bytes(&o);
  758. oggpack_readinit(&r,buffer,bytes);
  759. for(i=0;i<test2size;i++){
  760. if(oggpack_look(&r,32)==-1)report("out of data. failed!");
  761. if(oggpack_look(&r,32)!=large[i]){
  762. fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpack_look(&r,32),large[i],
  763. oggpack_look(&r,32),large[i]);
  764. report("read incorrect value!\n");
  765. }
  766. oggpack_adv(&r,32);
  767. }
  768. if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
  769. fprintf(stderr,"ok.");
  770. fprintf(stderr,"\nSmall unclipped packing (LSb): ");
  771. cliptest(testbuffer1,test1size,7,four,foursize);
  772. fprintf(stderr,"ok.");
  773. fprintf(stderr,"\nLarge unclipped packing (LSb): ");
  774. cliptest(testbuffer2,test2size,17,five,fivesize);
  775. fprintf(stderr,"ok.");
  776. fprintf(stderr,"\nSingle bit unclipped packing (LSb): ");
  777. cliptest(testbuffer3,test3size,1,six,sixsize);
  778. fprintf(stderr,"ok.");
  779. fprintf(stderr,"\nTesting read past end (LSb): ");
  780. oggpack_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8);
  781. for(i=0;i<64;i++){
  782. if(oggpack_read(&r,1)!=0){
  783. fprintf(stderr,"failed; got -1 prematurely.\n");
  784. exit(1);
  785. }
  786. }
  787. if(oggpack_look(&r,1)!=-1 ||
  788. oggpack_read(&r,1)!=-1){
  789. fprintf(stderr,"failed; read past end without -1.\n");
  790. exit(1);
  791. }
  792. oggpack_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8);
  793. if(oggpack_read(&r,30)!=0 || oggpack_read(&r,16)!=0){
  794. fprintf(stderr,"failed 2; got -1 prematurely.\n");
  795. exit(1);
  796. }
  797. if(oggpack_look(&r,18)!=0 ||
  798. oggpack_look(&r,18)!=0){
  799. fprintf(stderr,"failed 3; got -1 prematurely.\n");
  800. exit(1);
  801. }
  802. if(oggpack_look(&r,19)!=-1 ||
  803. oggpack_look(&r,19)!=-1){
  804. fprintf(stderr,"failed; read past end without -1.\n");
  805. exit(1);
  806. }
  807. if(oggpack_look(&r,32)!=-1 ||
  808. oggpack_look(&r,32)!=-1){
  809. fprintf(stderr,"failed; read past end without -1.\n");
  810. exit(1);
  811. }
  812. oggpack_writeclear(&o);
  813. fprintf(stderr,"ok.");
  814. /* this is partly glassbox; we're mostly concerned about the allocation boundaries */
  815. fprintf(stderr,"\nTesting aligned writecopies (LSb): ");
  816. for(i=0;i<71;i++)
  817. for(j=0;j<5;j++)
  818. copytest(j*8,i);
  819. for(i=BUFFER_INCREMENT*8-71;i<BUFFER_INCREMENT*8+71;i++)
  820. for(j=0;j<5;j++)
  821. copytest(j*8,i);
  822. fprintf(stderr,"ok. ");
  823. fprintf(stderr,"\nTesting unaligned writecopies (LSb): ");
  824. for(i=0;i<71;i++)
  825. for(j=1;j<40;j++)
  826. if(j&0x7)
  827. copytest(j,i);
  828. for(i=BUFFER_INCREMENT*8-71;i<BUFFER_INCREMENT*8+71;i++)
  829. for(j=1;j<40;j++)
  830. if(j&0x7)
  831. copytest(j,i);
  832. fprintf(stderr,"ok. \n");
  833. /********** lazy, cut-n-paste retest with MSb packing ***********/
  834. /* Test read/write together */
  835. /* Later we test against pregenerated bitstreams */
  836. oggpackB_writeinit(&o);
  837. fprintf(stderr,"\nSmall preclipped packing (MSb): ");
  838. cliptestB(testbuffer1,test1size,0,oneB,onesize);
  839. fprintf(stderr,"ok.");
  840. fprintf(stderr,"\nNull bit call (MSb): ");
  841. cliptestB(testbuffer3,test3size,0,twoB,twosize);
  842. fprintf(stderr,"ok.");
  843. fprintf(stderr,"\nLarge preclipped packing (MSb): ");
  844. cliptestB(testbuffer2,test2size,0,threeB,threesize);
  845. fprintf(stderr,"ok.");
  846. fprintf(stderr,"\n32 bit preclipped packing (MSb): ");
  847. oggpackB_reset(&o);
  848. for(i=0;i<test2size;i++)
  849. oggpackB_write(&o,large[i],32);
  850. buffer=oggpackB_get_buffer(&o);
  851. bytes=oggpackB_bytes(&o);
  852. oggpackB_readinit(&r,buffer,bytes);
  853. for(i=0;i<test2size;i++){
  854. if(oggpackB_look(&r,32)==-1)report("out of data. failed!");
  855. if(oggpackB_look(&r,32)!=large[i]){
  856. fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpackB_look(&r,32),large[i],
  857. oggpackB_look(&r,32),large[i]);
  858. report("read incorrect value!\n");
  859. }
  860. oggpackB_adv(&r,32);
  861. }
  862. if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
  863. fprintf(stderr,"ok.");
  864. fprintf(stderr,"\nSmall unclipped packing (MSb): ");
  865. cliptestB(testbuffer1,test1size,7,fourB,foursize);
  866. fprintf(stderr,"ok.");
  867. fprintf(stderr,"\nLarge unclipped packing (MSb): ");
  868. cliptestB(testbuffer2,test2size,17,fiveB,fivesize);
  869. fprintf(stderr,"ok.");
  870. fprintf(stderr,"\nSingle bit unclipped packing (MSb): ");
  871. cliptestB(testbuffer3,test3size,1,sixB,sixsize);
  872. fprintf(stderr,"ok.");
  873. fprintf(stderr,"\nTesting read past end (MSb): ");
  874. oggpackB_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8);
  875. for(i=0;i<64;i++){
  876. if(oggpackB_read(&r,1)!=0){
  877. fprintf(stderr,"failed; got -1 prematurely.\n");
  878. exit(1);
  879. }
  880. }
  881. if(oggpackB_look(&r,1)!=-1 ||
  882. oggpackB_read(&r,1)!=-1){
  883. fprintf(stderr,"failed; read past end without -1.\n");
  884. exit(1);
  885. }
  886. oggpackB_readinit(&r,(unsigned char *)"\0\0\0\0\0\0\0\0",8);
  887. if(oggpackB_read(&r,30)!=0 || oggpackB_read(&r,16)!=0){
  888. fprintf(stderr,"failed 2; got -1 prematurely.\n");
  889. exit(1);
  890. }
  891. if(oggpackB_look(&r,18)!=0 ||
  892. oggpackB_look(&r,18)!=0){
  893. fprintf(stderr,"failed 3; got -1 prematurely.\n");
  894. exit(1);
  895. }
  896. if(oggpackB_look(&r,19)!=-1 ||
  897. oggpackB_look(&r,19)!=-1){
  898. fprintf(stderr,"failed; read past end without -1.\n");
  899. exit(1);
  900. }
  901. if(oggpackB_look(&r,32)!=-1 ||
  902. oggpackB_look(&r,32)!=-1){
  903. fprintf(stderr,"failed; read past end without -1.\n");
  904. exit(1);
  905. }
  906. fprintf(stderr,"ok.");
  907. oggpackB_writeclear(&o);
  908. /* this is partly glassbox; we're mostly concerned about the allocation boundaries */
  909. fprintf(stderr,"\nTesting aligned writecopies (MSb): ");
  910. for(i=0;i<71;i++)
  911. for(j=0;j<5;j++)
  912. copytestB(j*8,i);
  913. for(i=BUFFER_INCREMENT*8-71;i<BUFFER_INCREMENT*8+71;i++)
  914. for(j=0;j<5;j++)
  915. copytestB(j*8,i);
  916. fprintf(stderr,"ok. ");
  917. fprintf(stderr,"\nTesting unaligned writecopies (MSb): ");
  918. for(i=0;i<71;i++)
  919. for(j=1;j<40;j++)
  920. if(j&0x7)
  921. copytestB(j,i);
  922. for(i=BUFFER_INCREMENT*8-71;i<BUFFER_INCREMENT*8+71;i++)
  923. for(j=1;j<40;j++)
  924. if(j&0x7)
  925. copytestB(j,i);
  926. fprintf(stderr,"ok. \n\n");
  927. return(0);
  928. }
  929. #endif /* _V_SELFTEST */
  930. #undef BUFFER_INCREMENT