CHCODE.H
上传用户:zdlsnail
上传日期:2007-01-06
资源大小:16k
文件大小:14k
源码类别:

压缩解压

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define BITS            15
  5. #define MAX_CODE        ( ( 1<<BITS )-1 )
  6. #define TABLE_SIZE      35023l
  7. #define TABLE_BANKS     ( ( TABLE_SIZE>>8 )+1 )
  8. #define END_OF_STREAM   256
  9. #define BUMP_CODE       257
  10. #define FLUSH_CODE      258
  11. #define FIRST_CODE      259
  12. #define UNUSED          -1
  13. #define PACIFIER_COUNT  2047
  14. /*long  array[128]={6,33,34,35,36,37,38,39,5,41,42,43,44,45,46,47,
  15. // 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,7,65,66,67,
  16. // 68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,
  17. // 88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,
  18. // 106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,
  19. // 121,122,123,124,125,126,127,3,129,130,131,132,133,134,4,
  20. // 136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,
  21. // 151,152,153,154,155,156,157,158,159};
  22. */long  array[64]={48,49,50,51,52,53,54,55,56,57,
  23.                     65,66,67,68,69,70,71,72,73,74,75,76,
  24.                     77,78,79,80,81,82,83,84,85,86,87,88,89,90,
  25.                     97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,
  26.                     117,118,119,120,121,122,40,41};
  27. struct dictionary{
  28.    long  code_value;
  29.    long  parent_code;
  30.    char character;
  31.    } * dict[TABLE_BANKS];
  32. typedef struct bit_stream{
  33.   unsigned char mask;
  34.   long  rack;
  35.   long  pacifier_counter;
  36.   }BIT_STREAM;
  37. void InitializeDictionary();
  38. void InitializeStorage();
  39. unsigned long  find_child_node( long  parent_code,long  child_character );
  40. unsigned long  decode_string( unsigned long  offset,unsigned long  code );
  41. void OutputBits( BIT_STREAM * bit_stream,
  42.                  unsigned long code,
  43.                  long  count,
  44.                  unsigned char * outstream,
  45.                  long * flag );
  46. unsigned long InputBits( BIT_STREAM  * bit_stream,
  47.                          long bit_count,
  48.  unsigned char * instream,
  49.  long length,
  50.                          long * flag );
  51. long code( unsigned char * instream,long length,unsigned char * outstream );
  52. long uncode( unsigned char * instream,long length,unsigned char * outstream );
  53. BIT_STREAM * OpenOutputBitStream( void );
  54. BIT_STREAM * OpenInputBitStream( void );
  55. void CompressStream( BIT_STREAM * output,
  56.    unsigned char * instream,
  57.    long length,
  58.    unsigned char * outstream ,
  59.                    long * flag);
  60. long ExpandStream( BIT_STREAM * input,
  61.                    unsigned char * instream,
  62.                    long length,
  63.                    unsigned char * outstream);
  64. long compress_code( unsigned char * instream,long length );
  65. long uncode_expand( unsigned char * instream,long length );
  66. #define DICT(i) dict[i>>8][i&0xff]
  67. char decode_stack[TABLE_SIZE];
  68. unsigned long  next_code;
  69. long  current_code_bits;
  70. unsigned long  next_bump_code;
  71. long compress_code( unsigned char * instream,long length )
  72. {
  73.   BIT_STREAM * output;
  74.   unsigned char * outstream,* copy_outstream;
  75.   unsigned char * mediastream,* copy_mediastream;
  76.   long * flag,i;
  77.   if(length==0) return(length);  
  78.   outstream=(unsigned char *)malloc(2*length+1);
  79.   copy_outstream=outstream;
  80.   mediastream=(unsigned char *)malloc(2*length);
  81.   copy_mediastream=mediastream;
  82.   flag=(long *)malloc(8);
  83.   *flag=0;
  84.   setbuf( stdout,NULL );
  85.   output=OpenOutputBitStream();
  86.   CompressStream( output,instream,length,outstream,flag );
  87.   for( i=1l;i<=*flag;i++ )  * mediastream++=*outstream++;
  88.   mediastream-=*flag;
  89.   outstream-=*flag;
  90.   length=code( mediastream,*flag,outstream );
  91.   for( i=1l;i<=length;i++ ) *instream++=*outstream++;
  92.   *instream='';
  93.   instream-=length;
  94.   outstream-=length;
  95.   free((unsigned char *)copy_outstream);
  96.   free((unsigned char *)copy_mediastream);
  97.   free((long *)flag);
  98.   free((BIT_STREAM *)output);
  99.   return(length);
  100. }
  101. void InitializeDictionary()
  102. {
  103.   unsigned long  i;
  104.   for( i=0;i<TABLE_SIZE;i++ )
  105.        DICT(i).code_value=UNUSED;
  106.   next_code=FIRST_CODE;
  107.   current_code_bits=9;
  108.   next_bump_code=511;
  109. }
  110. void InitializeStorage()
  111. {
  112.   long  i;
  113.   for( i=0;i<TABLE_BANKS;i++ )
  114.        dict[i]=( struct dictionary * )calloc( 256,sizeof( struct dictionary ) );
  115.   return;
  116. }
  117. void CompressStream( BIT_STREAM * output,
  118.                      unsigned char * instream,
  119.                      long length,
  120.                      unsigned char * outstream,
  121.                      long * flag)
  122. {
  123.   long  character;
  124.   long  string_code;
  125.   unsigned long  index;
  126.   long i,j,k;
  127.   InitializeStorage();
  128.   InitializeDictionary();
  129.   string_code=*instream; instream++;
  130.   for( i=1l;i<=(length+1);i++ ){
  131.          character= * instream;
  132.          if(i==length) character=13;
  133.          if(i==(length+1)) character=10;
  134.          instream++;
  135.          index=find_child_node( string_code,character );
  136.          if( DICT( index ).code_value!=-1 )
  137.              string_code=DICT( index ).code_value;
  138.           else {
  139.              DICT( index ).code_value=next_code++;
  140.              DICT( index ).parent_code=string_code;
  141.              DICT( index ).character=( char )character;
  142.              j=*flag;
  143.              OutputBits( output,( unsigned long )string_code,
  144.                          current_code_bits,outstream,flag );
  145.              for( k=1l;k<=(*flag-j);k++ ) *outstream++;
  146.              string_code=character;
  147.              if( next_code>MAX_CODE ) {
  148.                  j=*flag;
  149.                  OutputBits( output,( unsigned long )FLUSH_CODE,
  150.                              current_code_bits,outstream,flag );
  151.                  for( k=1l;k<=(*flag-j);k++ ) *outstream++;
  152.                  InitializeDictionary();
  153.                }
  154.              else if( next_code>next_bump_code ){
  155.                       j=*flag;
  156.                       OutputBits( output,( unsigned long )BUMP_CODE,
  157.                                   current_code_bits,outstream,flag);
  158.                       for( k=1l;k<=(*flag-j);k++ ) *outstream++;
  159.                       current_code_bits++;
  160.                       next_bump_code<<=1;
  161.                       next_bump_code|=1;
  162.                   }
  163.                }
  164.               }
  165.   j=*flag;
  166.   OutputBits( output,( unsigned long )string_code,
  167.               current_code_bits,outstream,flag );
  168.   for( k=1l;k<=(*flag-j);k++ ) *outstream++;
  169.   OutputBits( output,( unsigned long )END_OF_STREAM,
  170.               current_code_bits,outstream,flag );
  171.   for( k=0;k<TABLE_BANKS;k++ )
  172.       free( (struct dictionary *) dict[k] );
  173. }
  174. unsigned long  find_child_node( long  parent_code,long  child_character )
  175. {
  176.   unsigned long  index;
  177.   long  offset;
  178.   index=( child_character<<( BITS-8 ) )^parent_code;
  179.   if( index==0 )
  180.       offset=1;
  181.   else
  182.       offset=TABLE_SIZE-index;
  183.   for( ; ; ) {
  184.        if( DICT( index ).code_value==UNUSED )
  185.            return( ( unsigned long  )index );
  186.        if( DICT( index ).parent_code==parent_code &&
  187.            DICT( index ).character==( char )child_character )
  188.            return( index );
  189.        if( ( long  )index>=offset )
  190.            index-=offset;
  191.        else
  192.            index+=TABLE_SIZE-offset;
  193.        }
  194. }
  195. unsigned long  decode_string( unsigned long  count,unsigned long  code )
  196. {
  197.   while( code>256 ){
  198.          decode_stack[count++]=DICT( code ).character;
  199.          code=DICT( code ).parent_code;
  200.          }
  201.   decode_stack[count++]=( char )code;
  202.   return( count );
  203. }
  204. void OutputBits( BIT_STREAM * bit_stream,unsigned long code,long  count,
  205.                  unsigned char * outstream,long * flag )
  206. {
  207.   unsigned long mask;
  208.   mask=1l<<( count-1 );
  209.   while( mask!=0 ) {
  210.      if( mask & code )
  211.          bit_stream->rack|=bit_stream->mask;
  212.      bit_stream->mask>>=1;
  213.      if( bit_stream->mask==0 )  {
  214.           * outstream=(unsigned char)(bit_stream->rack);
  215.   (*flag)++;
  216.           outstream++;
  217.           if( ( bit_stream->pacifier_counter++ & PACIFIER_COUNT )==0 );
  218.          bit_stream->rack=0;
  219.          bit_stream->mask=0x80;
  220.         }
  221.      mask >>= 1;
  222.   }
  223. }
  224. BIT_STREAM * OpenOutputBitStream( )
  225. {
  226.   BIT_STREAM * bit_stream;
  227.   bit_stream=( BIT_STREAM * )calloc( 1,sizeof( BIT_STREAM ) );
  228.   if( bit_stream==NULL )
  229.     return( bit_stream );
  230.   bit_stream->rack=0;
  231.   bit_stream->mask=0x80;
  232.   bit_stream->pacifier_counter=0;
  233.   return( bit_stream );
  234. }
  235. long  code( unsigned char * mediastream,
  236.            long length,unsigned char * outstream )
  237. {
  238.   unsigned char rackin,rackout,out_char;
  239.   long  i,j,flag;
  240.   short mask;
  241.   long number;
  242.   unsigned char bit[8];
  243.   if( length/3*3==length ) number=length*8/6;
  244.   else number=(length*8/6+1);
  245.   flag=2;
  246.   for( i=1l;i<=number;i++ ){
  247.           if( flag==10 ) flag=2;
  248.   mask=0x01;
  249.           rackout=0;
  250.           if( (flag==8)||(i==number) ) rackin=0;
  251.           else{ rackin=* mediastream;
  252.                 mediastream++; }
  253.   rackout=(rackin>>flag);
  254.           for( j=(9-flag);j<=6;j++ )
  255.                rackout |= bit[j];
  256.           out_char=(unsigned char)(array[rackout]);
  257.           *outstream++=out_char;
  258.           for( j=(7-flag);j<=6;j++ ){
  259.                bit[j]=rackin & mask;
  260.                bit[j]<<=(6-flag);
  261.        mask<<=1;}
  262.           flag+=2;
  263.          }
  264.   return( number );
  265. }
  266. long uncode_expand( unsigned char * instream,long length )
  267. {
  268.   BIT_STREAM * input;
  269.   unsigned char * outstream,* copy_outstream;
  270.   unsigned char * mediastream,* copy_mediastream;
  271.   long i;
  272.   if(length==0) return(length);  
  273.   outstream=(unsigned char *)malloc(4*length);
  274.   copy_outstream=outstream;
  275.   mediastream=(unsigned char *)malloc(2*length);
  276.   copy_mediastream=mediastream;
  277.   setbuf( stdout,NULL );
  278.   input=OpenInputBitStream( );
  279.   length=uncode( instream,length,outstream);
  280.   for( i=1l;i<=length;i++ ) *mediastream++=*outstream++;
  281.   mediastream-=length;
  282.   outstream-=length;
  283.   length=ExpandStream( input,mediastream,length,outstream);
  284.   for( i=1l;i<=length;i++ ) *instream++=*outstream++;
  285.   *instream='';
  286.   instream-=length;
  287.   outstream-=length;
  288.   free((unsigned char *)copy_outstream );
  289.   free((unsigned char *)copy_mediastream);
  290.   free((BIT_STREAM *)input);
  291.   return( length-2);
  292. }
  293. long ExpandStream( BIT_STREAM * input,unsigned char * instream,long length,
  294.                    unsigned char * outstream )
  295. {
  296.   unsigned long new_code;
  297.   unsigned long old_code;
  298.   long character;
  299.   unsigned long count;
  300.   long k,j,ff=0;
  301.   long * flag;
  302.   flag=(long *)malloc(sizeof(long));
  303.   *flag=0l;
  304.   InitializeStorage();
  305.   for( ; ; ) {
  306.         InitializeDictionary();
  307.         k=*flag;
  308.         old_code=( unsigned long )InputBits( input,current_code_bits,
  309.                                  instream,length,flag );
  310.         for( j=1l;j<=(*flag-k);j++ ) instream++;
  311.         /*************/
  312.         if( old_code==(unsigned long)(length|0x10000000) )  return( ff );
  313.         /*************/
  314.         if( old_code==END_OF_STREAM )
  315.             { instream-=*flag;free((long*)flag); return( ff );}
  316.         character=old_code;
  317.         *outstream++=(unsigned char)(character);ff++;
  318.         for( ; ; ) {
  319.              k=*flag;
  320.              new_code=( unsigned long )InputBits( input,current_code_bits ,
  321.                                          instream,length,flag );
  322.              for( j=1l;j<=(*flag-k);j++ ) instream++;
  323.         /*************/
  324.         if( new_code==(unsigned long)(length|0x10000000) )
  325.    { return(ff);}
  326.         /*************/
  327.         if( new_code==END_OF_STREAM )
  328.             { instream-=*flag;free((long *)flag); return( ff );}
  329.         if( new_code==FLUSH_CODE )
  330.             break;
  331.         if( new_code==BUMP_CODE ) {
  332.             current_code_bits++;
  333.             continue;
  334.         }
  335.         if( new_code>=next_code ) {
  336.             decode_stack[0]=( char )character;
  337.             count=decode_string( 1,old_code );
  338.             }
  339.         else
  340.             count=decode_string( 0,new_code );
  341.         character=decode_stack[count-1];
  342.         while( count>0 )
  343.              {  * outstream++=decode_stack[--count];ff++;}
  344.         DICT( next_code ).parent_code=old_code;
  345.         DICT( next_code ).character=( char )character;
  346.         next_code++;
  347.         old_code=new_code;
  348.         }
  349.      }
  350. }
  351. unsigned long InputBits( BIT_STREAM * bit_stream,long bit_count,
  352.  unsigned char * instream,long length,long * flag )
  353. {
  354.   unsigned long mask;
  355.   unsigned long return_value;
  356.   mask=1l<<( bit_count-1 );
  357.   return_value=0;
  358.   while( mask!=0 ) {
  359.      if( bit_stream->mask==0x80 ) {
  360.          bit_stream->rack=* instream++; (*flag)++;
  361.          /****************/
  362.          if( *flag==length ) return( length|0x10000000 );
  363.          /****************/
  364.          if( ( bit_stream->pacifier_counter++ & PACIFIER_COUNT )==0 );
  365.          }
  366.      if( bit_stream->rack & bit_stream->mask )
  367.          return_value|=mask;
  368.      mask>>=1;
  369.      bit_stream->mask>>=1;
  370.      if( bit_stream->mask==0 )
  371.          bit_stream->mask=0x80;
  372.    }
  373.   return( return_value );
  374. }
  375. BIT_STREAM * OpenInputBitStream( )
  376. {
  377.   BIT_STREAM * bit_stream;
  378.   bit_stream=( BIT_STREAM * )calloc( 1,sizeof( BIT_STREAM ) );
  379.   if( bit_stream==NULL )
  380.     return( bit_stream );
  381.   bit_stream->rack=0;
  382.   bit_stream->mask=0x80;
  383.   bit_stream->pacifier_counter=0;
  384.   return( bit_stream );
  385. }
  386. long uncode( unsigned char * instream,long length,unsigned char * outstream )
  387. {
  388.   unsigned char rackin;
  389.   unsigned char rackout;
  390.   short mask;
  391.   long j,flag;
  392.   unsigned char bit[8];
  393.   long i,k=0;
  394.   
  395.   for( i=1l;i<=length;i++ ){
  396.        rackin=* instream++;
  397.        for( j=0;j<=63;j++ )
  398.     if( array[j]==rackin )  break;
  399.        rackin=(unsigned char)(j);
  400.        rackout=( rackin<<2 ) ;
  401.        for( flag=1;flag<=5;flag+=2 ){
  402.             mask=0x20;
  403.     rackin=*instream++;
  404.             i++;if(i>length) break;
  405.             for( j=0;j<=63;j++ )
  406.  if( array[j]==rackin ) break;
  407.             rackin=(unsigned char)(j);
  408.             for( j=flag;j>=0;j-- ){
  409.                  bit[j]=rackin & mask;
  410.                  bit[j]>>=(5-flag);
  411.                  rackout |= bit[j];
  412.                  mask>>=1;
  413.                  }
  414.        
  415.        *outstream++=rackout; k++;
  416.        rackout=( rackin<<(flag+3) );
  417.        }
  418.   }
  419.   * outstream++=0xfe;k+=1;
  420.   return( k );
  421. }