?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.         LONG i;
  10.  
  11.         ULONG check;
  12.         ULONG byte,bits;//,bitlen;
  13.         LONG  disp;
  14.         ULONG length;
  15.  
  16.         ULONG stop;
  17.  
  18.  
  19.         ULONG success = 1;
  20.  
  21.  
  22.         // rewind input stream
  23.         //
  24.         check = depack_getbyte(DEPACK_GETBYTE_REWIND);
  25. #ifdef DPK_CHECK
  26.         if( 0xFFFFFFFF == check )
  27.         {
  28.  #ifdef DPK_REPERR
  29.                 printf("mhmt-depack-hrum.c:{} - Can't rewind input stream!\n");
  30.  #endif
  31.                 return 0;
  32.         }
  33. #endif
  34.  
  35.  
  36.         // manage zx header if needed
  37.         if( wrk.zxheader )
  38.         {
  39.                 // skip 5 bytes (they will go to the end of output file)
  40.                 for(i=0;i<5;i++)
  41.                 {
  42.                         check = depack_getbyte(DEPACK_GETBYTE_NEXT);
  43. #ifdef DPK_CHECK
  44.                         if( 0xFFFFFFFF == check ) goto NO_BYTE_HRM;
  45. #endif
  46.                 }
  47.  
  48.                 // next 2 bytes must be 0x10
  49.                 for(i=0;i<2;i++)
  50.                 {
  51.                         check = depack_getbyte(DEPACK_GETBYTE_NEXT);
  52. #ifdef DPK_CHECK
  53.                         if( 0xFFFFFFFF == check ) goto NO_BYTE_HRM;
  54.                         if( check != 0x0010 )
  55.                         {
  56.  #ifdef DPK_REPERR
  57.                                 printf("mhmt-depack-hrum.c:{} - Wrong ZX-header!\n");
  58.  #endif
  59.                                 return 0;
  60.                         }
  61. #endif
  62.                 }
  63.         }
  64.  
  65.  
  66.  
  67.         // initialize bitstream first
  68.         //
  69.         check = depack_getbits(16,DEPACK_GETBITS_FORCE); // number 16 is ignored! - just for convenience here...
  70. #ifdef DPK_CHECK
  71.         if( 0xFFFFFFFF == check )
  72.         {
  73. NO_BITS_HRM:
  74.  #ifdef DPK_REPERR
  75.                 printf("mhmt-depack-hrum.c:{} - Can't get bits from input stream!\n");
  76.  #endif
  77.                 return 0;
  78.         }
  79. #endif
  80.  
  81.  
  82.  
  83.         // then byte of input stream goes to the output unchanged
  84.         //
  85.         byte = depack_getbyte(DEPACK_GETBYTE_NEXT);
  86. #ifdef DPK_CHECK
  87.         if( 0xFFFFFFFF == byte )
  88.         {
  89. NO_BYTE_HRM:
  90.  #ifdef DPK_REPERR
  91.                 printf("mhmt-depack-hrum.c:{} - Can't get byte from input stream!\n");
  92.  #endif
  93.                 return 0;
  94.         }
  95. #endif
  96.  
  97. #ifdef DPK_DEPACK
  98.         success = success && depack_outbyte( (UBYTE)(0x00FF&byte), DEPACK_OUTBYTE_ADD );
  99. #endif
  100.  
  101.  
  102.  
  103.         // now normal depacking loop
  104.         //
  105.         stop = 0;
  106.         while( (!stop) && success )
  107.         {
  108.                 bits = depack_getbits(1,DEPACK_GETBITS_NEXT);
  109. #ifdef DPK_CHECK
  110.                 if( 0xFFFFFFFF == bits ) goto NO_BITS_HRM;
  111. #endif
  112.  
  113.                 if( 1&bits ) // %1<byte>
  114.                 {
  115.                         byte = depack_getbyte(DEPACK_GETBYTE_NEXT);
  116. #ifdef DPK_CHECK
  117.                         if( 0xFFFFFFFF == byte ) goto NO_BYTE_HRM;
  118. #endif
  119.  
  120. #ifdef DPK_DEPACK
  121.                         success = success && depack_outbyte( (UBYTE)(0x00FF&byte), DEPACK_OUTBYTE_ADD );
  122. #endif
  123.                 }
  124.                 else // %0xx
  125.                 {
  126.                         bits = depack_getbits(2,DEPACK_GETBITS_NEXT);
  127. #ifdef DPK_CHECK
  128.                         if( 0xFFFFFFFF == bits ) goto NO_BITS_HRM;
  129. #endif
  130.  
  131.                         switch( 0x03 & bits )
  132.                         {
  133.                         case 0x00: // %000xxx
  134.  
  135.                                 bits = depack_getbits(3,DEPACK_GETBITS_NEXT);
  136. #ifdef DPK_CHECK
  137.                                 if( 0xFFFFFFFF == bits ) goto NO_BITS_HRM;
  138. #endif
  139.  
  140.                                 disp = (-8) | (bits&0x07); // FFFFFFF8..FFFFFFFF (-8..-1)
  141. #ifdef DPK_CHECK
  142.                                 if( (ULONG)(-disp) > wrk.maxwin )
  143.                                 {
  144. WRONG_DISP_HRM:
  145.  #ifdef DPK_REPERR
  146.                                         printf("mhmt-depack-hrum.c:{} - Wrong lookback displacement of %d, greater than maxwin\n",(-disp) );
  147.  #endif
  148.                                         return 0;
  149.                                 }
  150. #endif
  151.  
  152. #ifdef DPK_DEPACK
  153.                                 success = success && depack_repeat(disp,1);
  154. #endif
  155.                                 break;
  156.  
  157.  
  158.                         case 0x01: // %001
  159.  
  160.                                 byte = depack_getbyte(DEPACK_GETBYTE_NEXT);
  161. #ifdef DPK_CHECK
  162.                                 if( 0xFFFFFFFF == byte ) goto NO_BYTE_HRM;
  163. #endif
  164.  
  165.                                 disp = (-256) | (0x00FF&byte); // -1..-256
  166. #ifdef DPK_CHECK
  167.                                 if( (ULONG)(-disp) > wrk.maxwin ) goto WRONG_DISP_HRM;
  168. #endif
  169.  
  170. #ifdef DPK_DEPACK
  171.                                 success = success && depack_repeat(disp,2);
  172. #endif
  173.                                 break;
  174.  
  175.                         default: // %010 or %011
  176.  
  177.                                 if( (bits&3)==2 ) // %010 - 3 bytes
  178.                                 {
  179.                                         length = 3;
  180.                                 }
  181.                                 else // %011 - varlen
  182.                                 {
  183.                                         bits = depack_getbits(2,DEPACK_GETBITS_NEXT);
  184. #ifdef DPK_CHECK
  185.                                         if( 0xFFFFFFFF == bits ) goto NO_BITS_HRM;
  186. #endif
  187.                                         //  fetch len
  188.                                         if( bits == 0x00 ) // %01100<len>, if <len>==0 - stop
  189.                                         {
  190.                                                 length = depack_getbyte(DEPACK_GETBYTE_NEXT);
  191. #ifdef DPK_CHECK
  192.                                                 if( 0xFFFFFFFF == length ) goto NO_BYTE_HRM;
  193. #endif
  194.                                                 if( length == 0 )
  195.                                                         stop = 1;
  196.                                         }
  197.                                         else if( bits == 0x01 ) // %01101 - len=4
  198.                                         {
  199.                                                 length = 4;
  200.                                         }
  201.                                         else if( bits == 0x02 ) // %01110 - len=5
  202.                                         {
  203.                                                 length = 5;
  204.                                         }
  205.                                         else // %01111
  206.                                         {
  207.                                                 bits = depack_getbits(2,DEPACK_GETBITS_NEXT);
  208. #ifdef DPK_CHECK
  209.                                                 if( 0xFFFFFFFF == bits ) goto NO_BITS_HRM;
  210. #endif
  211.                                                 if( bits == 0x00 ) // %0111100
  212.                                                 {
  213.                                                         length = 6;
  214.                                                 }
  215.                                                 else if( bits == 0x01 ) // %0111101
  216.                                                 {
  217.                                                         length = 7;
  218.                                                 }
  219.                                                 else if( bits == 0x02 ) // %0111110
  220.                                                 {
  221.                                                         length = 8;
  222.                                                 }
  223.                                                 else // %0111111
  224.                                                 {
  225.                                                         bits = depack_getbits(2,DEPACK_GETBITS_NEXT);
  226. #ifdef DPK_CHECK
  227.                                                         if( 0xFFFFFFFF == bits ) goto NO_BITS_HRM;
  228. #endif
  229.                                                         if( bits == 0x00 ) // %011111100
  230.                                                         {
  231.                                                                 length = 9;
  232.                                                         }
  233.                                                         else if( bits == 0x01 ) // %011111101
  234.                                                         {
  235.                                                                 length = 10;
  236.                                                         }
  237.                                                         else if( bits == 0x02 ) // %011111110
  238.                                                         {
  239.                                                                 length = 11;
  240.                                                         }
  241.                                                         else // %011111111
  242.                                                         {
  243.                                                                 bits = depack_getbits(2,DEPACK_GETBITS_NEXT);
  244. #ifdef DPK_CHECK
  245.                                                                 if( 0xFFFFFFFF == bits ) goto NO_BITS_HRM;
  246. #endif
  247.                                                                 if( bits == 0x00 ) // %01111111100
  248.                                                                 {
  249.                                                                         length = 12;
  250.                                                                 }
  251.                                                                 else if( bits == 0x01 ) // %01111111101
  252.                                                                 {
  253.                                                                         length = 13;
  254.                                                                 }
  255.                                                                 else if( bits == 0x02 ) // %01111111110
  256.                                                                 {
  257.                                                                         length = 14;
  258.                                                                 }
  259.                                                                 else // %01111111111
  260.                                                                 {
  261.                                                                         length = 15;
  262.                                                                 }
  263.                                                         }
  264.                                                 }
  265.                                         }
  266.                                 }
  267.  
  268.  
  269.                                 // fetch disp and depack
  270.                                 if( !stop )
  271.                                 {
  272.                                         bits = depack_getbits(1,DEPACK_GETBITS_NEXT);
  273. #ifdef DPK_CHECK
  274.                                         if( 0xFFFFFFFF == bits ) goto NO_BITS_HRM;
  275. #endif
  276.                                         if( bits == 0x00 ) // %0<disp>
  277.                                         {
  278.                                                 byte = depack_getbyte(DEPACK_GETBYTE_NEXT);
  279. #ifdef DPK_CHECK
  280.                                                 if( 0xFFFFFFFF == byte ) goto NO_BYTE_HRM;
  281. #endif
  282.                                                 disp = (-256) | (0x00FF&byte); // -1..-256
  283. #ifdef DPK_CHECK
  284.                                                 if( (ULONG)(-disp) > wrk.maxwin ) goto WRONG_DISP_HRM;
  285. #endif
  286. #ifdef DPK_DEPACK
  287.                                                 success = success && depack_repeat(disp,length);
  288. #endif
  289.                                         }
  290.                                         else // %1abcd<disp>
  291.                                         {
  292.                                                 bits = depack_getbits(4,DEPACK_GETBITS_NEXT);
  293. #ifdef DPK_CHECK
  294.                                                 if( 0xFFFFFFFF == bits ) goto NO_BITS_HRM;
  295. #endif
  296.                                                 byte = depack_getbyte(DEPACK_GETBYTE_NEXT);
  297. #ifdef DPK_CHECK
  298.                                                 if( 0xFFFFFFFF == byte ) goto NO_BYTE_HRM;
  299. #endif
  300.                                                 disp = (-4096) | (0x0F00&(bits<<8)) | (0x00FF&byte); // -1..-4096
  301. #ifdef DPK_CHECK
  302.                                                 if( (ULONG)(-disp) > wrk.maxwin ) goto WRONG_DISP_HRM;
  303. #endif
  304. #ifdef DPK_DEPACK
  305.                                                 success = success && depack_repeat(disp,length);
  306. #endif
  307.                                         }
  308.                                 }
  309.  
  310.                                 break;
  311.                         }
  312.                 }
  313.         }
  314.  
  315.         //manage zxheader again (copy to the end of output)
  316. #ifdef DPK_DEPACK
  317.         if( wrk.zxheader )
  318.         {
  319.                 check = depack_getbyte(DEPACK_GETBYTE_REWIND);
  320.  #ifdef DPK_CHECK
  321.                 if( 0xFFFFFFFF == check )
  322.                 {
  323.   #ifdef DPK_REPERR
  324.                         printf("mhmt-depack-hrum.c:{} - Can't rewind input stream!\n");
  325.   #endif
  326.                         return 0;
  327.                 }
  328.  #endif
  329.  
  330.                 // place 5 bytes of header to the end
  331.                 for(i=0;i<5;i++)
  332.                 {
  333.                         byte = depack_getbyte(DEPACK_GETBYTE_NEXT);
  334.  #ifdef DPK_CHECK
  335.                         if( 0xFFFFFFFF == check ) goto NO_BYTE_HRM:
  336.  #endif
  337.                         success = success && depack_outbyte( (UBYTE)(0x00FF&byte), DEPACK_OUTBYTE_ADD );
  338.                 }
  339.         }
  340. #endif
  341.  
  342.         return success;
  343. }
  344.  
  345.