?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // this is not a code to be separately compiled!
  2. // instead, it is included in mhmt-depack.c several times and depends on existing at include moment #define's to generate some compiling code
  3. //
  4. //                   // example defines:
  5. //#define DPK_CHECK  // check input stream for consistency
  6. //#define DPK_DEPACK // do depacking
  7. //#define DPK_REPERR // report errors via printf
  8. {
  9.         ULONG check;
  10.         ULONG byte,bits/*,bitlen*/;
  11.         LONG disp;
  12.         ULONG length;
  13.         ULONG len_bits;
  14.  
  15.         ULONG stop;
  16.  
  17.  
  18.         ULONG success = 1;
  19.  
  20.  
  21.  
  22.  
  23.  
  24.         // rewind input stream
  25.         //
  26.         check = depack_getbyte(DEPACK_GETBYTE_REWIND);
  27. #ifdef DPK_CHECK
  28.         if( 0xFFFFFFFF == check )
  29.         {
  30.  #ifdef DPK_REPERR
  31.                 printf("mhmt-depack-zx7.c:{} - Can't rewind input stream!\n");
  32.  #endif
  33.                 return 0;
  34.         }
  35. #endif
  36.  
  37.  
  38.         // first byte of input stream goes to the output unchanged
  39.         //
  40.         byte = depack_getbyte(DEPACK_GETBYTE_NEXT);
  41. //printf("FIRST: #%02x\n\n",byte);
  42. #ifdef DPK_CHECK
  43.         if( 0xFFFFFFFF == byte )
  44.         {
  45. NO_BYTE:
  46.  #ifdef DPK_REPERR
  47.                 printf("mhmt-depack-zx7.c:{} - Can't get byte from input stream!\n");
  48.  #endif
  49.                 return 0;
  50.         }
  51. #endif
  52.  
  53. #ifdef DPK_DEPACK
  54.         success = success && depack_outbyte( (UBYTE)(0x00FF&byte), DEPACK_OUTBYTE_ADD );
  55. #endif
  56.  
  57.  
  58.         // next is byte to the bitstream
  59.         //
  60.         check = depack_getbits(8,DEPACK_GETBITS_FORCE);
  61. #ifdef DPK_CHECK
  62.         if( 0xFFFFFFFF == check )
  63.         {
  64. NO_BITS:
  65.  #ifdef DPK_REPERR
  66.                 printf("mhmt-depack-zx7.c:{} - Can't get bits from input stream!\n");
  67.  #endif
  68.                 return 0;
  69.         }
  70. #endif
  71.  
  72.  
  73.  
  74.         // now normal depacking loop
  75.         //
  76.         stop = 0;
  77.         while( (!stop) && success )
  78.         {
  79.                 bits = depack_getbits(1,DEPACK_GETBITS_NEXT);
  80. #ifdef DPK_CHECK
  81.                 if( 0xFFFFFFFF == bits ) goto NO_BITS;
  82. #endif
  83.  
  84.                 if( !(1&bits) ) // %0<byte>
  85.                 {
  86.                         byte = depack_getbyte(DEPACK_GETBYTE_NEXT);
  87. //printf("BITS: %%0\nBYTE: #%02x\n\n",byte);
  88. #ifdef DPK_CHECK
  89.                         if( 0xFFFFFFFF == byte ) goto NO_BYTE;
  90. #endif
  91.  
  92. #ifdef DPK_DEPACK
  93.                         success = success && depack_outbyte( (UBYTE)(0x00FF&byte), DEPACK_OUTBYTE_ADD );
  94. #endif
  95.                 }
  96.                 else // %100...01[len], 0 to 15 zeros, 16 zeros=exit, >16 zeros=error
  97.                 {
  98.                         len_bits = 0;
  99. //printf("BITS: %%1");
  100.                         while( 1 )
  101.                         {
  102.                                 bits = depack_getbits(1,DEPACK_GETBITS_NEXT);
  103. //printf("%01d",(bits&1)?1:0);
  104. #ifdef DPK_CHECK
  105.                                 if( 0xFFFFFFFF == bits ) goto NO_BITS;
  106. #endif
  107.                                 if( 1 & bits )
  108.                                         break;
  109.  
  110.                                 len_bits++;
  111.                         }
  112.  
  113.  
  114.                         if( len_bits >= 16 )
  115.                         {
  116.                                 stop = 1;
  117. #ifdef DPK_CHECK
  118.                                 if( len_bits > 16 )
  119.                                 {
  120.                                         success = 0;
  121.  #ifdef DPK_REPERR
  122.                                         printf("mhmt-depack-zx7.c:{} - Too big bit length in input bitstream!\n");
  123.  #endif
  124.                                 }
  125. #endif
  126.                                 break;
  127.                         }
  128.  
  129.                         // read length bits (0 to 15)
  130.                         if( len_bits > 0 )
  131.                         {
  132.                                 bits = depack_getbits(len_bits,DEPACK_GETBITS_NEXT);
  133. //int i;
  134. //for(i=len_bits;i>0;i--){
  135. //printf("%01d",(bits&(1<<(i-1)))?1:0);}
  136. #ifdef DPK_CHECK
  137.                                 if( 0xFFFFFFFF == bits ) goto NO_BITS;
  138. #endif
  139.                         }
  140.                         else
  141.                         {
  142.                                 bits = 0;
  143.                         }
  144.  
  145.                         length = 1 + (1<<len_bits) + ( bits & ((1<<len_bits)-1) );
  146. //printf("\n");
  147.  
  148.  
  149.                         // now read displacement
  150.                         byte = depack_getbyte(DEPACK_GETBYTE_NEXT);
  151. #ifdef DPK_CHECK
  152.                         if( 0xFFFFFFFF == byte ) goto NO_BYTE;
  153. #endif
  154. //printf("BYTE: #%02x\n",byte);
  155.                         disp = -(1 + byte);
  156.  
  157.                         if( 128 & byte ) // bigger displacement
  158.                         {
  159.                                 bits = depack_getbits(4,DEPACK_GETBITS_NEXT);
  160. //int i;
  161. //printf("BITS: %%");
  162. //for(i=4;i>0;i--){
  163. //printf("%01d",(bits&(1<<(i-1)))?1:0);}
  164. //printf("\n");
  165. #ifdef DPK_CHECK
  166.                                 if( 0xFFFFFFFF == bits ) goto NO_BITS;
  167. #endif
  168.                                 disp -= (bits&15)<<7;
  169.                         }
  170. //printf("\n");
  171.  
  172.  
  173. #ifdef DPK_CHECK
  174.                         // check disp/length
  175.                         if( (ULONG)(-disp) > wrk.maxwin )
  176.                         {
  177.  #ifdef DPK_REPERR
  178.                                 printf("mhmt-depack-zx7.c:{} - Wrong lookback displacement of %d, greater than maxwin\n",(-disp) );
  179.  #endif
  180.                                 return 0;
  181.                         }
  182. #endif
  183.  
  184. #ifdef DPK_DEPACK
  185.                         success = success && depack_repeat(disp,length);
  186. #endif
  187.  
  188.                 }
  189.  
  190.         }
  191.  
  192.         return success;
  193. }
  194.  
  195.