⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipex.h

📁 本程序是基于Hufuman编码压缩算法的成熟代码实现
💻 H
📖 第 1 页 / 共 2 页
字号:
  OutputBits( output,( unsigned long )string_code,
              current_code_bits,outstream,flag );
  for( k=1l;k<=(*flag-j);k++ ) *outstream++;
  OutputBits( output,( unsigned long )END_OF_STREAM,
              current_code_bits,outstream,flag );
  for( k=0;k<TABLE_BANKS;k++ )
      free( (struct dictionary *) dict[k] );
}

unsigned long  find_child_node( long  parent_code,long  child_character )
{
  unsigned long  index;
  long  offset;

  index=( child_character<<( BITS-8 ) )^parent_code;
  if( index==0 )
      offset=1;
  else
      offset=TABLE_SIZE-index;
  for( ; ; ) {
       if( DICT( index ).code_value==NOUSED1 )
           return( ( unsigned long  )index );
       if( DICT( index ).parent_code==parent_code &&
           DICT( index ).character==( char )child_character )
           return( index );
       if( ( long  )index>=offset )
           index-=offset;
       else
           index+=TABLE_SIZE-offset;
       }
}


unsigned long  decode_string( unsigned long  count,unsigned long  code )
{
  while( code>256 ){
         decode_stack[count++]=DICT( code ).character;
         code=DICT( code ).parent_code;
         }
  decode_stack[count++]=( char )code;
  return( count );
}

void OutputBits( BIT_STREAM * bit_stream,unsigned long code,long  count,
                 unsigned char * outstream,long * flag )
{
  unsigned long mask;

  mask=1l<<( count-1 );
  while( mask!=0 ) {
     if( mask & code )
         bit_stream->rack|=bit_stream->mask;
     bit_stream->mask>>=1;
     if( bit_stream->mask==0 )  {
          * outstream=(unsigned char)(bit_stream->rack);
		  (*flag)++;
          outstream++;
          if( ( bit_stream->pacifier_counter++ & PACIFIER_COUNT )==0 );  //这是故意设定的空执行,不要理会警告信息
         bit_stream->rack=0;
         bit_stream->mask=0x80;
        }
     mask >>= 1;
  }
}


BIT_STREAM * OpenOutputBitStream( )
{
  BIT_STREAM * bit_stream;

  bit_stream=( BIT_STREAM * )calloc( 1,sizeof( BIT_STREAM ) );
  if( bit_stream==NULL )
    return( bit_stream );
  bit_stream->rack=0;
  bit_stream->mask=0x80;
  bit_stream->pacifier_counter=0;
  return( bit_stream );
}

long  code( unsigned char * mediastream,
           long length,unsigned char * outstream )
{

  unsigned char rackin,rackout,out_char;
  long  i,j,flag;
  short mask;
  long number;
  unsigned char bit[8];


  if( length/3*3==length ) number=length*8/6;
  else number=(length*8/6+1);
  flag=2;
  for( i=1l;i<=number;i++ ){
          if( flag==10 ) flag=2;
	  mask=0x01;
          rackout=0;
          if( (flag==8)||(i==number) ) rackin=0;
          else{ rackin=* mediastream;
                mediastream++; }
	  rackout=(rackin>>flag);
          for( j=(9-flag);j<=6;j++ )
               rackout |= bit[j];
          out_char=(unsigned char)(array[rackout]);
          *outstream++=out_char;
          for( j=(7-flag);j<=6;j++ ){
               bit[j]=rackin & mask;
               bit[j]<<=(6-flag);
	       mask<<=1;}
          flag+=2;
         }
  return( number );
}


long ExpandStream( BIT_STREAM * input,unsigned char * instream,long length,
                   unsigned char * outstream )
{
  unsigned long new_code;
  unsigned long old_code;
  long character;
  unsigned long count;
  long k,j,ff=0;
  long * flag;

  flag=(long *)malloc(sizeof(long));
  *flag=0l;
  InitializeStorage();
  for( ; ; ) {
        InitializeDictionary();
        k=*flag;
        old_code=( unsigned long )InputBits( input,current_code_bits,
                                 instream,length,flag );
        for( j=1l;j<=(*flag-k);j++ ) instream++;
        /*************/
        if( old_code==(unsigned long)(length|0x10000000) )  return( ff );
        /*************/
        if( old_code==END_OF_STREAM )
            { instream-=*flag;free((long*)flag); return( ff );}
        character=old_code;
        *outstream++=(unsigned char)(character);ff++;
        for( ; ; ) {
             k=*flag;
             new_code=( unsigned long )InputBits( input,current_code_bits ,
                                         instream,length,flag );
             for( j=1l;j<=(*flag-k);j++ ) instream++;
        /*************/
        if( new_code==(unsigned long)(length|0x10000000) )
		   { return(ff);}
        /*************/
        if( new_code==END_OF_STREAM )
            { instream-=*flag;free((long *)flag); return( ff );}
        if( new_code==FLUSH_CODE )
            break;
        if( new_code==BUMP_CODE ) {
            current_code_bits++;
            continue;
        }
        if( new_code>=next_code ) {
            decode_stack[0]=( char )character;
            count=decode_string( 1,old_code );
            }
        else
            count=decode_string( 0,new_code );
        character=decode_stack[count-1];
        while( count>0 )
             {  * outstream++=decode_stack[--count];ff++;}

        DICT( next_code ).parent_code=old_code;
        DICT( next_code ).character=( char )character;
        next_code++;
        old_code=new_code;
        }
     }

     free((long *)flag);

//    for( int k1=0;k1<TABLE_BANKS;k1++ )
//      free( (struct dictionary *) dict[k1] );

}


unsigned long InputBits( BIT_STREAM * bit_stream,long bit_count,
			 unsigned char * instream,long length,long * flag )
{
  unsigned long mask;
  unsigned long return_value;

  mask=1l<<( bit_count-1 );
  return_value=0;
  while( mask!=0 ) {
     if( bit_stream->mask==0x80 ) {
         bit_stream->rack=* instream++; (*flag)++;
         /****************/
         if( *flag==length ) return( length|0x10000000 );
         /****************/
         if( ( bit_stream->pacifier_counter++ & PACIFIER_COUNT )==0 ); //这是故意设定的空执行,不要理会警告信息
         }
     if( bit_stream->rack & bit_stream->mask )
         return_value|=mask;
     mask>>=1;
     bit_stream->mask>>=1;
     if( bit_stream->mask==0 )
         bit_stream->mask=0x80;
   }
  return( return_value );
}

BIT_STREAM * OpenInputBitStream( )
{
  BIT_STREAM * bit_stream;

  bit_stream=( BIT_STREAM * )calloc( 1,sizeof( BIT_STREAM ) );
  if( bit_stream==NULL )
    return( bit_stream );
  bit_stream->rack=0;
  bit_stream->mask=0x80;
  bit_stream->pacifier_counter=0;
  return( bit_stream );
}

long uncode( unsigned char * instream,long length,unsigned char * outstream )
{
  unsigned char rackin;
  unsigned char rackout;
  short mask;
  long j,flag;
  unsigned char bit[8];
  long i,k=0;
  
  for( i=1l;i<=length;i++ ){
       rackin=* instream++;
       for( j=0;j<=63;j++ )
	    if( array[j]==rackin )  break;
       rackin=(unsigned char)(j);
       rackout=( rackin<<2 ) ;
       for( flag=1;flag<=5;flag+=2 ){
            mask=0x20;
	    rackin=*instream++;
            i++;if(i>length) break;
            for( j=0;j<=63;j++ )
		 if( array[j]==rackin ) break;
            rackin=(unsigned char)(j);
            for( j=flag;j>=0;j-- ){
                 bit[j]=rackin & mask;
                 bit[j]>>=(5-flag);
                 rackout |= bit[j];
                 mask>>=1;
                 }
       
       *outstream++=rackout; k++;
       rackout=( rackin<<(flag+3) );
       }
  }
  * outstream++=0xfe;k+=1;
  return( k );
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -