examples/sfexamples/oggvorbiscodec/src/libogg/src/bitwise.c

00001 /********************************************************************
00002  *                                                                  *
00003  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
00004  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
00005  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
00006  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
00007  *                                                                  *
00008  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002             *
00009  * by the Xiph.Org Foundation http://www.xiph.org/                  *
00010  *                                                                  *
00011  ********************************************************************
00012 
00013   function: packing variable sized words into an octet stream
00014   last mod: $Id: bitwise.c,v 1.17 2003/11/10 13:06:08 xiphmont Exp $
00015 
00016  ********************************************************************/
00017 
00018 /* We're 'LSb' endian; if we write a word but read individual bits,
00019    then we'll read the lsb first */
00020 
00021 #include <string.h>
00022 #include <stdlib.h>
00023 #include "ogg/ogg.h"
00024 
00025 #define BUFFER_INCREMENT 256
00026 
00027 const static unsigned long mask[]=
00028 {0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
00029  0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
00030  0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
00031  0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
00032  0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
00033  0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
00034  0x3fffffff,0x7fffffff,0xffffffff };
00035 
00036 const static unsigned int mask8B[]=
00037 {0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};
00038 
00039 void oggpack_writeinit(oggpack_buffer *b){
00040   memset(b,0,sizeof(*b));
00041   b->ptr=b->buffer=(unsigned char*)_ogg_malloc(BUFFER_INCREMENT);
00042   b->buffer[0]='\0';
00043   b->storage=BUFFER_INCREMENT;
00044 }
00045 
00046 void oggpackB_writeinit(oggpack_buffer *b){
00047   oggpack_writeinit(b);
00048 }
00049 
00050 void oggpack_writetrunc(oggpack_buffer *b,long bits){
00051   long bytes=bits>>3;
00052   bits-=bytes*8;
00053   b->ptr=b->buffer+bytes;
00054   b->endbit=bits;
00055   b->endbyte=bytes;
00056   *b->ptr&=mask[bits];
00057 }
00058 
00059 void oggpackB_writetrunc(oggpack_buffer *b,long bits){
00060   long bytes=bits>>3;
00061   bits-=bytes*8;
00062   b->ptr=b->buffer+bytes;
00063   b->endbit=bits;
00064   b->endbyte=bytes;
00065   *b->ptr&=mask8B[bits];
00066 }
00067 
00068 /* Takes only up to 32 bits. */
00069 void oggpack_write(oggpack_buffer *b,unsigned long value,int bits){
00070   if(b->endbyte+4>=b->storage){
00071     b->buffer=(unsigned char*)_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
00072     b->storage+=BUFFER_INCREMENT;
00073     b->ptr=b->buffer+b->endbyte;
00074   }
00075 
00076   value&=mask[bits]; 
00077   bits+=b->endbit;
00078 
00079   b->ptr[0]|=value<<b->endbit;  
00080   
00081   if(bits>=8){
00082     b->ptr[1]=value>>(8-b->endbit);  
00083     if(bits>=16){
00084       b->ptr[2]=value>>(16-b->endbit);  
00085       if(bits>=24){
00086         b->ptr[3]=value>>(24-b->endbit);  
00087         if(bits>=32){
00088           if(b->endbit)
00089             b->ptr[4]=value>>(32-b->endbit);
00090           else
00091             b->ptr[4]=0;
00092         }
00093       }
00094     }
00095   }
00096 
00097   b->endbyte+=bits/8;
00098   b->ptr+=bits/8;
00099   b->endbit=bits&7;
00100 }
00101 
00102 /* Takes only up to 32 bits. */
00103 void oggpackB_write(oggpack_buffer *b,unsigned long value,int bits){
00104   if(b->endbyte+4>=b->storage){
00105     b->buffer=(unsigned char*)_ogg_realloc(b->buffer,b->storage+BUFFER_INCREMENT);
00106     b->storage+=BUFFER_INCREMENT;
00107     b->ptr=b->buffer+b->endbyte;
00108   }
00109 
00110   value=(value&mask[bits])<<(32-bits); 
00111   bits+=b->endbit;
00112 
00113   b->ptr[0]|=value>>(24+b->endbit);  
00114   
00115   if(bits>=8){
00116     b->ptr[1]=value>>(16+b->endbit);  
00117     if(bits>=16){
00118       b->ptr[2]=value>>(8+b->endbit);  
00119       if(bits>=24){
00120         b->ptr[3]=value>>(b->endbit);  
00121         if(bits>=32){
00122           if(b->endbit)
00123             b->ptr[4]=value<<(8-b->endbit);
00124           else
00125             b->ptr[4]=0;
00126         }
00127       }
00128     }
00129   }
00130 
00131   b->endbyte+=bits/8;
00132   b->ptr+=bits/8;
00133   b->endbit=bits&7;
00134 }
00135 
00136 void oggpack_writealign(oggpack_buffer *b){
00137   int bits=8-b->endbit;
00138   if(bits<8)
00139     oggpack_write(b,0,bits);
00140 }
00141 
00142 void oggpackB_writealign(oggpack_buffer *b){
00143   int bits=8-b->endbit;
00144   if(bits<8)
00145     oggpackB_write(b,0,bits);
00146 }
00147 
00148 static void oggpack_writecopy_helper(oggpack_buffer *b,
00149                                      void *source,
00150                                      long bits,
00151                                      void (*w)(oggpack_buffer *,
00152                                                unsigned long,
00153                                                int),
00154                                      int msb){
00155   unsigned char *ptr=(unsigned char *)source;
00156 
00157   long bytes=bits/8;
00158   bits-=bytes*8;
00159 
00160   if(b->endbit){
00161     int i;
00162     /* unaligned copy.  Do it the hard way. */
00163     for(i=0;i<bytes;i++)
00164       w(b,(unsigned long)(ptr[i]),8);    
00165   }else{
00166     /* aligned block copy */
00167     if(b->endbyte+bytes+1>=b->storage){
00168       b->storage=b->endbyte+bytes+BUFFER_INCREMENT;
00169       b->buffer=(unsigned char*)_ogg_realloc(b->buffer,b->storage);
00170       b->ptr=b->buffer+b->endbyte;
00171     }
00172 
00173     memmove(b->ptr,source,bytes);
00174     b->ptr+=bytes;
00175     b->buffer+=bytes;
00176     *b->ptr=0;
00177 
00178   }
00179   if(bits){
00180     if(msb)
00181       w(b,(unsigned long)(ptr[bytes]>>(8-bits)),bits);    
00182     else
00183       w(b,(unsigned long)(ptr[bytes]),bits);    
00184   }
00185 }
00186 
00187 void oggpack_writecopy(oggpack_buffer *b,void *source,long bits){
00188   oggpack_writecopy_helper(b,source,bits,oggpack_write,0);
00189 }
00190 
00191 void oggpackB_writecopy(oggpack_buffer *b,void *source,long bits){
00192   oggpack_writecopy_helper(b,source,bits,oggpackB_write,1);
00193 }
00194 
00195 void oggpack_reset(oggpack_buffer *b){
00196   b->ptr=b->buffer;
00197   b->buffer[0]=0;
00198   b->endbit=b->endbyte=0;
00199 }
00200 
00201 void oggpackB_reset(oggpack_buffer *b){
00202   oggpack_reset(b);
00203 }
00204 
00205 void oggpack_writeclear(oggpack_buffer *b){
00206   _ogg_free(b->buffer);
00207   memset(b,0,sizeof(*b));
00208 }
00209 
00210 void oggpackB_writeclear(oggpack_buffer *b){
00211   oggpack_writeclear(b);
00212 }
00213 
00214 void oggpack_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
00215   memset(b,0,sizeof(*b));
00216   b->buffer=b->ptr=buf;
00217   b->storage=bytes;
00218 }
00219 
00220 void oggpackB_readinit(oggpack_buffer *b,unsigned char *buf,int bytes){
00221   oggpack_readinit(b,buf,bytes);
00222 }
00223 
00224 /* Read in bits without advancing the bitptr; bits <= 32 */
00225 long oggpack_look(oggpack_buffer *b,int bits){
00226   unsigned long ret;
00227   unsigned long m=mask[bits];
00228 
00229   bits+=b->endbit;
00230 
00231   if(b->endbyte+4>=b->storage){
00232     /* not the main path */
00233     if(b->endbyte*8+bits>b->storage*8)return(-1);
00234   }
00235   
00236   ret=b->ptr[0]>>b->endbit;
00237   if(bits>8){
00238     ret|=b->ptr[1]<<(8-b->endbit);  
00239     if(bits>16){
00240       ret|=b->ptr[2]<<(16-b->endbit);  
00241       if(bits>24){
00242         ret|=b->ptr[3]<<(24-b->endbit);  
00243         if(bits>32 && b->endbit)
00244           ret|=b->ptr[4]<<(32-b->endbit);
00245       }
00246     }
00247   }
00248   return(m&ret);
00249 }
00250 
00251 /* Read in bits without advancing the bitptr; bits <= 32 */
00252 long oggpackB_look(oggpack_buffer *b,int bits){
00253   unsigned long ret;
00254   int m=32-bits;
00255 
00256   bits+=b->endbit;
00257 
00258   if(b->endbyte+4>=b->storage){
00259     /* not the main path */
00260     if(b->endbyte*8+bits>b->storage*8)return(-1);
00261   }
00262   
00263   ret=b->ptr[0]<<(24+b->endbit);
00264   if(bits>8){
00265     ret|=b->ptr[1]<<(16+b->endbit);  
00266     if(bits>16){
00267       ret|=b->ptr[2]<<(8+b->endbit);  
00268       if(bits>24){
00269         ret|=b->ptr[3]<<(b->endbit);  
00270         if(bits>32 && b->endbit)
00271           ret|=b->ptr[4]>>(8-b->endbit);
00272       }
00273     }
00274   }
00275   return (ret>>(m>>1))>>((m+1)>>1);
00276 }
00277 
00278 long oggpack_look1(oggpack_buffer *b){
00279   if(b->endbyte>=b->storage)return(-1);
00280   return((b->ptr[0]>>b->endbit)&1);
00281 }
00282 
00283 long oggpackB_look1(oggpack_buffer *b){
00284   if(b->endbyte>=b->storage)return(-1);
00285   return((b->ptr[0]>>(7-b->endbit))&1);
00286 }
00287 
00288 void oggpack_adv(oggpack_buffer *b,int bits){
00289   bits+=b->endbit;
00290   b->ptr+=bits/8;
00291   b->endbyte+=bits/8;
00292   b->endbit=bits&7;
00293 }
00294 
00295 void oggpackB_adv(oggpack_buffer *b,int bits){
00296   oggpack_adv(b,bits);
00297 }
00298 
00299 void oggpack_adv1(oggpack_buffer *b){
00300   if(++(b->endbit)>7){
00301     b->endbit=0;
00302     b->ptr++;
00303     b->endbyte++;
00304   }
00305 }
00306 
00307 void oggpackB_adv1(oggpack_buffer *b){
00308   oggpack_adv1(b);
00309 }
00310 
00311 /* bits <= 32 */
00312 long oggpack_read(oggpack_buffer *b,int bits){
00313   unsigned long ret;
00314   unsigned long m=mask[bits];
00315 
00316   bits+=b->endbit;
00317 
00318   if(b->endbyte+4>=b->storage){
00319     /* not the main path */
00320     ret=-1UL;
00321     if(b->endbyte*8+bits>b->storage*8)goto overflow;
00322   }
00323   
00324   ret=b->ptr[0]>>b->endbit;
00325   if(bits>8){
00326     ret|=b->ptr[1]<<(8-b->endbit);  
00327     if(bits>16){
00328       ret|=b->ptr[2]<<(16-b->endbit);  
00329       if(bits>24){
00330         ret|=b->ptr[3]<<(24-b->endbit);  
00331         if(bits>32 && b->endbit){
00332           ret|=b->ptr[4]<<(32-b->endbit);
00333         }
00334       }
00335     }
00336   }
00337   ret&=m;
00338   
00339  overflow:
00340 
00341   b->ptr+=bits/8;
00342   b->endbyte+=bits/8;
00343   b->endbit=bits&7;
00344   return(ret);
00345 }
00346 
00347 /* bits <= 32 */
00348 long oggpackB_read(oggpack_buffer *b,int bits){
00349   unsigned long ret;
00350   long m=32-bits;
00351   
00352   bits+=b->endbit;
00353 
00354   if(b->endbyte+4>=b->storage){
00355     /* not the main path */
00356     ret=-1UL;
00357     if(b->endbyte*8+bits>b->storage*8)goto overflow;
00358   }
00359   
00360   ret=b->ptr[0]<<(24+b->endbit);
00361   if(bits>8){
00362     ret|=b->ptr[1]<<(16+b->endbit);  
00363     if(bits>16){
00364       ret|=b->ptr[2]<<(8+b->endbit);  
00365       if(bits>24){
00366         ret|=b->ptr[3]<<(b->endbit);  
00367         if(bits>32 && b->endbit)
00368           ret|=b->ptr[4]>>(8-b->endbit);
00369       }
00370     }
00371   }
00372   ret=(ret>>(m>>1))>>((m+1)>>1);
00373   
00374  overflow:
00375 
00376   b->ptr+=bits/8;
00377   b->endbyte+=bits/8;
00378   b->endbit=bits&7;
00379   return(ret);
00380 }
00381 
00382 long oggpack_read1(oggpack_buffer *b){
00383   unsigned long ret;
00384   
00385   if(b->endbyte>=b->storage){
00386     /* not the main path */
00387     ret=-1UL;
00388     goto overflow;
00389   }
00390 
00391   ret=(b->ptr[0]>>b->endbit)&1;
00392   
00393  overflow:
00394 
00395   b->endbit++;
00396   if(b->endbit>7){
00397     b->endbit=0;
00398     b->ptr++;
00399     b->endbyte++;
00400   }
00401   return(ret);
00402 }
00403 
00404 long oggpackB_read1(oggpack_buffer *b){
00405   unsigned long ret;
00406   
00407   if(b->endbyte>=b->storage){
00408     /* not the main path */
00409     ret=-1UL;
00410     goto overflow;
00411   }
00412 
00413   ret=(b->ptr[0]>>(7-b->endbit))&1;
00414   
00415  overflow:
00416 
00417   b->endbit++;
00418   if(b->endbit>7){
00419     b->endbit=0;
00420     b->ptr++;
00421     b->endbyte++;
00422   }
00423   return(ret);
00424 }
00425 
00426 long oggpack_bytes(oggpack_buffer *b){
00427   return(b->endbyte+(b->endbit+7)/8);
00428 }
00429 
00430 long oggpack_bits(oggpack_buffer *b){
00431   return(b->endbyte*8+b->endbit);
00432 }
00433 
00434 long oggpackB_bytes(oggpack_buffer *b){
00435   return oggpack_bytes(b);
00436 }
00437 
00438 long oggpackB_bits(oggpack_buffer *b){
00439   return oggpack_bits(b);
00440 }
00441   
00442 unsigned char *oggpack_get_buffer(oggpack_buffer *b){
00443   return(b->buffer);
00444 }
00445 
00446 unsigned char *oggpackB_get_buffer(oggpack_buffer *b){
00447   return oggpack_get_buffer(b);
00448 }
00449 
00450 /* Self test of the bitwise routines; everything else is based on
00451    them, so they damned well better be solid. */
00452 
00453 #ifdef _V_SELFTEST
00454 #include <stdio.h>
00455 
00456 static int ilog(unsigned int v){
00457   int ret=0;
00458   while(v){
00459     ret++;
00460     v>>=1;
00461   }
00462   return(ret);
00463 }
00464       
00465 oggpack_buffer o;
00466 oggpack_buffer r;
00467 
00468 void report(char *in){
00469   fprintf(stderr,"%s",in);
00470   exit(1);
00471 }
00472 
00473 void cliptest(unsigned long *b,int vals,int bits,int *comp,int compsize){
00474   long bytes,i;
00475   unsigned char *buffer;
00476 
00477   oggpack_reset(&o);
00478   for(i=0;i<vals;i++)
00479     oggpack_write(&o,b[i],bits?bits:ilog(b[i]));
00480   buffer=oggpack_get_buffer(&o);
00481   bytes=oggpack_bytes(&o);
00482   if(bytes!=compsize)report("wrong number of bytes!\n");
00483   for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
00484     for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
00485     report("wrote incorrect value!\n");
00486   }
00487   oggpack_readinit(&r,buffer,bytes);
00488   for(i=0;i<vals;i++){
00489     int tbit=bits?bits:ilog(b[i]);
00490     if(oggpack_look(&r,tbit)==-1)
00491       report("out of data!\n");
00492     if(oggpack_look(&r,tbit)!=(b[i]&mask[tbit]))
00493       report("looked at incorrect value!\n");
00494     if(tbit==1)
00495       if(oggpack_look1(&r)!=(b[i]&mask[tbit]))
00496         report("looked at single bit incorrect value!\n");
00497     if(tbit==1){
00498       if(oggpack_read1(&r)!=(b[i]&mask[tbit]))
00499         report("read incorrect single bit value!\n");
00500     }else{
00501     if(oggpack_read(&r,tbit)!=(b[i]&mask[tbit]))
00502       report("read incorrect value!\n");
00503     }
00504   }
00505   if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
00506 }
00507 
00508 void cliptestB(unsigned long *b,int vals,int bits,int *comp,int compsize){
00509   long bytes,i;
00510   unsigned char *buffer;
00511   
00512   oggpackB_reset(&o);
00513   for(i=0;i<vals;i++)
00514     oggpackB_write(&o,b[i],bits?bits:ilog(b[i]));
00515   buffer=oggpackB_get_buffer(&o);
00516   bytes=oggpackB_bytes(&o);
00517   if(bytes!=compsize)report("wrong number of bytes!\n");
00518   for(i=0;i<bytes;i++)if(buffer[i]!=comp[i]){
00519     for(i=0;i<bytes;i++)fprintf(stderr,"%x %x\n",(int)buffer[i],(int)comp[i]);
00520     report("wrote incorrect value!\n");
00521   }
00522   oggpackB_readinit(&r,buffer,bytes);
00523   for(i=0;i<vals;i++){
00524     int tbit=bits?bits:ilog(b[i]);
00525     if(oggpackB_look(&r,tbit)==-1)
00526       report("out of data!\n");
00527     if(oggpackB_look(&r,tbit)!=(b[i]&mask[tbit]))
00528       report("looked at incorrect value!\n");
00529     if(tbit==1)
00530       if(oggpackB_look1(&r)!=(b[i]&mask[tbit]))
00531         report("looked at single bit incorrect value!\n");
00532     if(tbit==1){
00533       if(oggpackB_read1(&r)!=(b[i]&mask[tbit]))
00534         report("read incorrect single bit value!\n");
00535     }else{
00536     if(oggpackB_read(&r,tbit)!=(b[i]&mask[tbit]))
00537       report("read incorrect value!\n");
00538     }
00539   }
00540   if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
00541 }
00542 
00543 int main(void){
00544   unsigned char *buffer;
00545   long bytes,i;
00546   static unsigned long testbuffer1[]=
00547     {18,12,103948,4325,543,76,432,52,3,65,4,56,32,42,34,21,1,23,32,546,456,7,
00548        567,56,8,8,55,3,52,342,341,4,265,7,67,86,2199,21,7,1,5,1,4};
00549   int test1size=43;
00550 
00551   static unsigned long testbuffer2[]=
00552     {216531625L,1237861823,56732452,131,3212421,12325343,34547562,12313212,
00553        1233432,534,5,346435231,14436467,7869299,76326614,167548585,
00554        85525151,0,12321,1,349528352};
00555   int test2size=21;
00556 
00557   static unsigned long testbuffer3[]=
00558     {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,
00559        0,1,30,1,1,1,0,0,1,0,0,0,12,0,11,0,1,0,0,1};
00560   int test3size=56;
00561 
00562   static unsigned long large[]=
00563     {2136531625L,2137861823,56732452,131,3212421,12325343,34547562,12313212,
00564        1233432,534,5,2146435231,14436467,7869299,76326614,167548585,
00565        85525151,0,12321,1,2146528352};
00566 
00567   int onesize=33;
00568   static int one[33]={146,25,44,151,195,15,153,176,233,131,196,65,85,172,47,40,
00569                     34,242,223,136,35,222,211,86,171,50,225,135,214,75,172,
00570                     223,4};
00571   static int oneB[33]={150,101,131,33,203,15,204,216,105,193,156,65,84,85,222,
00572                        8,139,145,227,126,34,55,244,171,85,100,39,195,173,18,
00573                        245,251,128};
00574 
00575   int twosize=6;
00576   static int two[6]={61,255,255,251,231,29};
00577   static int twoB[6]={247,63,255,253,249,120};
00578 
00579   int threesize=54;
00580   static int three[54]={169,2,232,252,91,132,156,36,89,13,123,176,144,32,254,
00581                       142,224,85,59,121,144,79,124,23,67,90,90,216,79,23,83,
00582                       58,135,196,61,55,129,183,54,101,100,170,37,127,126,10,
00583                       100,52,4,14,18,86,77,1};
00584   static int threeB[54]={206,128,42,153,57,8,183,251,13,89,36,30,32,144,183,
00585                          130,59,240,121,59,85,223,19,228,180,134,33,107,74,98,
00586                          233,253,196,135,63,2,110,114,50,155,90,127,37,170,104,
00587                          200,20,254,4,58,106,176,144,0};
00588 
00589   int foursize=38;
00590   static int four[38]={18,6,163,252,97,194,104,131,32,1,7,82,137,42,129,11,72,
00591                      132,60,220,112,8,196,109,64,179,86,9,137,195,208,122,169,
00592                      28,2,133,0,1};
00593   static int fourB[38]={36,48,102,83,243,24,52,7,4,35,132,10,145,21,2,93,2,41,
00594                         1,219,184,16,33,184,54,149,170,132,18,30,29,98,229,67,
00595                         129,10,4,32};
00596 
00597   int fivesize=45;
00598   static int five[45]={169,2,126,139,144,172,30,4,80,72,240,59,130,218,73,62,
00599                      241,24,210,44,4,20,0,248,116,49,135,100,110,130,181,169,
00600                      84,75,159,2,1,0,132,192,8,0,0,18,22};
00601   static int fiveB[45]={1,84,145,111,245,100,128,8,56,36,40,71,126,78,213,226,
00602                         124,105,12,0,133,128,0,162,233,242,67,152,77,205,77,
00603                         172,150,169,129,79,128,0,6,4,32,0,27,9,0};
00604 
00605   int sixsize=7;
00606   static int six[7]={17,177,170,242,169,19,148};
00607   static int sixB[7]={136,141,85,79,149,200,41};
00608 
00609   /* Test read/write together */
00610   /* Later we test against pregenerated bitstreams */
00611   oggpack_writeinit(&o);
00612 
00613   fprintf(stderr,"\nSmall preclipped packing (LSb): ");
00614   cliptest(testbuffer1,test1size,0,one,onesize);
00615   fprintf(stderr,"ok.");
00616 
00617   fprintf(stderr,"\nNull bit call (LSb): ");
00618   cliptest(testbuffer3,test3size,0,two,twosize);
00619   fprintf(stderr,"ok.");
00620 
00621   fprintf(stderr,"\nLarge preclipped packing (LSb): ");
00622   cliptest(testbuffer2,test2size,0,three,threesize);
00623   fprintf(stderr,"ok.");
00624 
00625   fprintf(stderr,"\n32 bit preclipped packing (LSb): ");
00626   oggpack_reset(&o);
00627   for(i=0;i<test2size;i++)
00628     oggpack_write(&o,large[i],32);
00629   buffer=oggpack_get_buffer(&o);
00630   bytes=oggpack_bytes(&o);
00631   oggpack_readinit(&r,buffer,bytes);
00632   for(i=0;i<test2size;i++){
00633     if(oggpack_look(&r,32)==-1)report("out of data. failed!");
00634     if(oggpack_look(&r,32)!=large[i]){
00635       fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpack_look(&r,32),large[i],
00636               oggpack_look(&r,32),large[i]);
00637       report("read incorrect value!\n");
00638     }
00639     oggpack_adv(&r,32);
00640   }
00641   if(oggpack_bytes(&r)!=bytes)report("leftover bytes after read!\n");
00642   fprintf(stderr,"ok.");
00643 
00644   fprintf(stderr,"\nSmall unclipped packing (LSb): ");
00645   cliptest(testbuffer1,test1size,7,four,foursize);
00646   fprintf(stderr,"ok.");
00647 
00648   fprintf(stderr,"\nLarge unclipped packing (LSb): ");
00649   cliptest(testbuffer2,test2size,17,five,fivesize);
00650   fprintf(stderr,"ok.");
00651 
00652   fprintf(stderr,"\nSingle bit unclipped packing (LSb): ");
00653   cliptest(testbuffer3,test3size,1,six,sixsize);
00654   fprintf(stderr,"ok.");
00655 
00656   fprintf(stderr,"\nTesting read past end (LSb): ");
00657   oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8);
00658   for(i=0;i<64;i++){
00659     if(oggpack_read(&r,1)!=0){
00660       fprintf(stderr,"failed; got -1 prematurely.\n");
00661       exit(1);
00662     }
00663   }
00664   if(oggpack_look(&r,1)!=-1 ||
00665      oggpack_read(&r,1)!=-1){
00666       fprintf(stderr,"failed; read past end without -1.\n");
00667       exit(1);
00668   }
00669   oggpack_readinit(&r,"\0\0\0\0\0\0\0\0",8);
00670   if(oggpack_read(&r,30)!=0 || oggpack_read(&r,16)!=0){
00671       fprintf(stderr,"failed 2; got -1 prematurely.\n");
00672       exit(1);
00673   }
00674 
00675   if(oggpack_look(&r,18)!=0 ||
00676      oggpack_look(&r,18)!=0){
00677     fprintf(stderr,"failed 3; got -1 prematurely.\n");
00678       exit(1);
00679   }
00680   if(oggpack_look(&r,19)!=-1 ||
00681      oggpack_look(&r,19)!=-1){
00682     fprintf(stderr,"failed; read past end without -1.\n");
00683       exit(1);
00684   }
00685   if(oggpack_look(&r,32)!=-1 ||
00686      oggpack_look(&r,32)!=-1){
00687     fprintf(stderr,"failed; read past end without -1.\n");
00688       exit(1);
00689   }
00690   fprintf(stderr,"ok.\n");
00691 
00692   /********** lazy, cut-n-paste retest with MSb packing ***********/
00693 
00694   /* Test read/write together */
00695   /* Later we test against pregenerated bitstreams */
00696   oggpackB_writeinit(&o);
00697 
00698   fprintf(stderr,"\nSmall preclipped packing (MSb): ");
00699   cliptestB(testbuffer1,test1size,0,oneB,onesize);
00700   fprintf(stderr,"ok.");
00701 
00702   fprintf(stderr,"\nNull bit call (MSb): ");
00703   cliptestB(testbuffer3,test3size,0,twoB,twosize);
00704   fprintf(stderr,"ok.");
00705 
00706   fprintf(stderr,"\nLarge preclipped packing (MSb): ");
00707   cliptestB(testbuffer2,test2size,0,threeB,threesize);
00708   fprintf(stderr,"ok.");
00709 
00710   fprintf(stderr,"\n32 bit preclipped packing (MSb): ");
00711   oggpackB_reset(&o);
00712   for(i=0;i<test2size;i++)
00713     oggpackB_write(&o,large[i],32);
00714   buffer=oggpackB_get_buffer(&o);
00715   bytes=oggpackB_bytes(&o);
00716   oggpackB_readinit(&r,buffer,bytes);
00717   for(i=0;i<test2size;i++){
00718     if(oggpackB_look(&r,32)==-1)report("out of data. failed!");
00719     if(oggpackB_look(&r,32)!=large[i]){
00720       fprintf(stderr,"%ld != %ld (%lx!=%lx):",oggpackB_look(&r,32),large[i],
00721               oggpackB_look(&r,32),large[i]);
00722       report("read incorrect value!\n");
00723     }
00724     oggpackB_adv(&r,32);
00725   }
00726   if(oggpackB_bytes(&r)!=bytes)report("leftover bytes after read!\n");
00727   fprintf(stderr,"ok.");
00728 
00729   fprintf(stderr,"\nSmall unclipped packing (MSb): ");
00730   cliptestB(testbuffer1,test1size,7,fourB,foursize);
00731   fprintf(stderr,"ok.");
00732 
00733   fprintf(stderr,"\nLarge unclipped packing (MSb): ");
00734   cliptestB(testbuffer2,test2size,17,fiveB,fivesize);
00735   fprintf(stderr,"ok.");
00736 
00737   fprintf(stderr,"\nSingle bit unclipped packing (MSb): ");
00738   cliptestB(testbuffer3,test3size,1,sixB,sixsize);
00739   fprintf(stderr,"ok.");
00740 
00741   fprintf(stderr,"\nTesting read past end (MSb): ");
00742   oggpackB_readinit(&r,"\0\0\0\0\0\0\0\0",8);
00743   for(i=0;i<64;i++){
00744     if(oggpackB_read(&r,1)!=0){
00745       fprintf(stderr,"failed; got -1 prematurely.\n");
00746       exit(1);
00747     }
00748   }
00749   if(oggpackB_look(&r,1)!=-1 ||
00750      oggpackB_read(&r,1)!=-1){
00751       fprintf(stderr,"failed; read past end without -1.\n");
00752       exit(1);
00753   }
00754   oggpackB_readinit(&r,"\0\0\0\0\0\0\0\0",8);
00755   if(oggpackB_read(&r,30)!=0 || oggpackB_read(&r,16)!=0){
00756       fprintf(stderr,"failed 2; got -1 prematurely.\n");
00757       exit(1);
00758   }
00759 
00760   if(oggpackB_look(&r,18)!=0 ||
00761      oggpackB_look(&r,18)!=0){
00762     fprintf(stderr,"failed 3; got -1 prematurely.\n");
00763       exit(1);
00764   }
00765   if(oggpackB_look(&r,19)!=-1 ||
00766      oggpackB_look(&r,19)!=-1){
00767     fprintf(stderr,"failed; read past end without -1.\n");
00768       exit(1);
00769   }
00770   if(oggpackB_look(&r,32)!=-1 ||
00771      oggpackB_look(&r,32)!=-1){
00772     fprintf(stderr,"failed; read past end without -1.\n");
00773       exit(1);
00774   }
00775   fprintf(stderr,"ok.\n\n");
00776 
00777 
00778   return(0);
00779 }  
00780 #endif  /* _V_SELFTEST */
00781 
00782 #undef BUFFER_INCREMENT

Generated by  doxygen 1.6.2