bitwise.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:7k
源码类别:

Windows CE

开发平台:

C/C++

  1. /********************************************************************
  2.  *                                                                  *
  3.  * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
  4.  *                                                                  *
  5.  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
  6.  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  7.  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
  8.  *                                                                  *
  9.  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2002    *
  10.  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
  11.  *                                                                  *
  12.  ********************************************************************
  13.   function: packing variable sized words into an octet stream
  14.  ********************************************************************/
  15. /* We're 'LSb' endian; if we write a word but read individual bits,
  16.    then we'll read the lsb first */
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include "ogg.h"
  20. static const unsigned long mask[]=
  21. {0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
  22.  0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
  23.  0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
  24.  0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
  25.  0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
  26.  0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
  27.  0x3fffffff,0x7fffffff,0xffffffff };
  28. /* mark read process as having run off the end */
  29. static void _adv_halt(oggpack_buffer *b){
  30.   b->headptr=b->head->buffer->data+b->head->begin+b->head->length;
  31.   b->headend=-1;
  32.   b->headbit=0;
  33. }
  34. /* spans forward, skipping as many bytes as headend is negative; if
  35.    headend is zero, simply finds next byte.  If we're up to the end
  36.    of the buffer, leaves headend at zero.  If we've read past the end,
  37.    halt the decode process. */
  38. static void _span(oggpack_buffer *b){
  39.   while(b->headend<1){
  40.     if(b->head->next){
  41.       b->count+=b->head->length;
  42.       b->head=b->head->next;
  43.       b->headptr=b->head->buffer->data+b->head->begin-b->headend; 
  44.       b->headend+=b->head->length;      
  45.     }else{
  46.       /* we've either met the end of decode, or gone past it. halt
  47.          only if we're past */
  48.       if(b->headend<0 || b->headbit)
  49.         /* read has fallen off the end */
  50.         _adv_halt(b);
  51.       break;
  52.     }
  53.   }
  54. }
  55. void oggpack_readinit(oggpack_buffer *b,ogg_reference *r){
  56.   memset(b,0,sizeof(*b));
  57.   b->tail=b->head=r;
  58.   b->count=0;
  59.   b->headptr=b->head->buffer->data+b->head->begin;
  60.   b->headend=b->head->length;
  61.   _span(b);
  62. }
  63. #define _lookspan()   while(!end){
  64.                         head=head->next;
  65.                         if(!head) return -1;
  66.                         ptr=head->buffer->data + head->begin;
  67.                         end=head->length;
  68.                       }
  69. /* Read in bits without advancing the bitptr; bits <= 32 */
  70. long oggpack_look(oggpack_buffer *b,int bits){
  71.   unsigned long m=mask[bits];
  72.   unsigned long ret;
  73.   bits+=b->headbit;
  74.   if(bits >= b->headend<<3){
  75.     int            end=b->headend;
  76.     const unsigned char *ptr=b->headptr;
  77.     ogg_reference *head=b->head;
  78.     if(end<0)return -1;
  79.     
  80.     if(bits){
  81.       _lookspan();
  82.       ret=*ptr++>>b->headbit;
  83.       if(bits>8){
  84.         --end;
  85.         _lookspan();
  86.         ret|=*ptr++<<(8-b->headbit);  
  87.         if(bits>16){
  88.           --end;
  89.           _lookspan();
  90.           ret|=*ptr++<<(16-b->headbit);  
  91.           if(bits>24){
  92.             --end;
  93.             _lookspan();
  94.             ret|=*ptr++<<(24-b->headbit);  
  95.             if(bits>32 && b->headbit){
  96.               --end;
  97.               _lookspan();
  98.               ret|=*ptr<<(32-b->headbit);
  99.             }
  100.           }
  101.         }
  102.       }
  103.     }
  104.   }else{
  105.     /* make this a switch jump-table */
  106.     ret=b->headptr[0]>>b->headbit;
  107.     if(bits>8){
  108.       ret|=b->headptr[1]<<(8-b->headbit);  
  109.       if(bits>16){
  110.         ret|=b->headptr[2]<<(16-b->headbit);  
  111.         if(bits>24){
  112.           ret|=b->headptr[3]<<(24-b->headbit);  
  113.           if(bits>32 && b->headbit)
  114.             ret|=b->headptr[4]<<(32-b->headbit);
  115.         }
  116.       }
  117.     }
  118.   }
  119.   ret&=m;
  120.   return ret;
  121. }
  122. /* limited to 32 at a time */
  123. void oggpack_adv(oggpack_buffer *b,int bits){
  124.   bits+=b->headbit;
  125.   b->headbit=bits&7;
  126.   b->headptr+=bits/8;
  127.   if((b->headend-=bits/8)<1)_span(b);
  128. }
  129. /* spans forward and finds next byte.  Never halts */
  130. static void _span_one(oggpack_buffer *b){
  131.   while(b->headend<1){
  132.     if(b->head->next){
  133.       b->count+=b->head->length;
  134.       b->head=b->head->next;
  135.       b->headptr=b->head->buffer->data+b->head->begin; 
  136.       b->headend=b->head->length;      
  137.     }else
  138.       break;
  139.   }
  140. }
  141. static int _halt_one(oggpack_buffer *b){
  142.   if(b->headend<1){
  143.     _adv_halt(b);
  144.     return -1;
  145.   }
  146.   return 0;
  147. }
  148. int oggpack_eop(oggpack_buffer *b){
  149.   if(b->headend<0)return -1;
  150.   return 0;
  151. }
  152. /* bits <= 32 */
  153. long oggpack_read(oggpack_buffer *b,int bits){
  154.   unsigned long m=mask[bits];
  155.   ogg_uint32_t ret;
  156.   bits+=b->headbit;
  157.   if(bits >= b->headend<<3){
  158.     if(b->headend<0)return -1;
  159.     
  160.     if(bits){
  161.       if (_halt_one(b)) return -1;
  162.       ret=*b->headptr>>b->headbit;
  163.       
  164.       if(bits>=8){
  165.         ++b->headptr;
  166.         --b->headend;
  167.         _span_one(b);
  168.         if(bits>8){
  169.           if (_halt_one(b)) return -1;
  170.           ret|=*b->headptr<<(8-b->headbit);   
  171.           
  172.           if(bits>=16){
  173.             ++b->headptr;
  174.             --b->headend;
  175.             _span_one(b);
  176.             if(bits>16){
  177.               if (_halt_one(b)) return -1;
  178.               ret|=*b->headptr<<(16-b->headbit);  
  179.               
  180.               if(bits>=24){
  181.                 ++b->headptr;
  182.                 --b->headend;
  183.                 _span_one(b);
  184.                 if(bits>24){
  185.                   if (_halt_one(b)) return -1;
  186.                   ret|=*b->headptr<<(24-b->headbit);
  187.                   
  188.                   if(bits>=32){
  189.                     ++b->headptr;
  190.                     --b->headend;
  191.                     _span_one(b);
  192.                     if(bits>32){
  193.                       if (_halt_one(b)) return -1;
  194.                       if(b->headbit)ret|=*b->headptr<<(32-b->headbit);
  195.                       
  196.                     }
  197.                   }
  198.                 }
  199.               }
  200.             }
  201.           }
  202.         }
  203.       }
  204.     }
  205.   }else{
  206.   
  207.     ret=b->headptr[0]>>b->headbit;
  208.     if(bits>8){
  209.       ret|=b->headptr[1]<<(8-b->headbit);  
  210.       if(bits>16){
  211.         ret|=b->headptr[2]<<(16-b->headbit);  
  212.         if(bits>24){
  213.           ret|=b->headptr[3]<<(24-b->headbit);  
  214.           if(bits>32 && b->headbit){
  215.             ret|=b->headptr[4]<<(32-b->headbit);
  216.           }
  217.         }
  218.       }
  219.     }
  220.     
  221.     b->headptr+=bits/8;
  222.     b->headend-=bits/8;
  223.   }
  224.   ret&=m;
  225.   b->headbit=bits&7;   
  226.   return ret;
  227. }
  228. long oggpack_bytes(oggpack_buffer *b){
  229.   return(b->count+b->headptr-b->head->buffer->data-b->head->begin+
  230.          (b->headbit+7)/8);
  231. }
  232. long oggpack_bits(oggpack_buffer *b){
  233.   return((b->count+b->headptr-b->head->buffer->data-b->head->begin)*8+
  234.          b->headbit);
  235. }