00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 typedef struct bits_buffer_s
00026 {
00027 int i_size;
00028
00029 int i_data;
00030 uint8_t i_mask;
00031 uint8_t *p_data;
00032
00033 } bits_buffer_t;
00034
00035 static inline int bits_initwrite( bits_buffer_t *p_buffer,
00036 int i_size, void *p_data )
00037 {
00038 p_buffer->i_size = i_size;
00039 p_buffer->i_data = 0;
00040 p_buffer->i_mask = 0x80;
00041 p_buffer->p_data = p_data;
00042 p_buffer->p_data[0] = 0;
00043 if( !p_buffer->p_data )
00044 {
00045 if( !( p_buffer->p_data = malloc( i_size ) ) )
00046 return -1;
00047 }
00048 return 0;
00049 }
00050
00051 static inline void bits_align( bits_buffer_t *p_buffer )
00052 {
00053 if( p_buffer->i_mask != 0x80 && p_buffer->i_data < p_buffer->i_size )
00054 {
00055 p_buffer->i_mask = 0x80;
00056 p_buffer->i_data++;
00057 p_buffer->p_data[p_buffer->i_data] = 0x00;
00058 }
00059 }
00060
00061 static inline void bits_write( bits_buffer_t *p_buffer,
00062 int i_count, uint64_t i_bits )
00063 {
00064 while( i_count > 0 )
00065 {
00066 i_count--;
00067
00068 if( ( i_bits >> i_count )&0x01 )
00069 {
00070 p_buffer->p_data[p_buffer->i_data] |= p_buffer->i_mask;
00071 }
00072 else
00073 {
00074 p_buffer->p_data[p_buffer->i_data] &= ~p_buffer->i_mask;
00075 }
00076 p_buffer->i_mask >>= 1;
00077 if( p_buffer->i_mask == 0 )
00078 {
00079 p_buffer->i_data++;
00080 p_buffer->i_mask = 0x80;
00081 }
00082 }
00083 }
00084
00085