LCOV - code coverage report
Current view: directory - media/libogg/src - ogg_bitwise.c (source / functions) Found Hit Coverage
Test: app.info Lines: 302 0 0.0 %
Date: 2012-06-02 Functions: 37 0 0.0 %

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

Generated by: LCOV version 1.7