home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 June / Chip_2002-06_cd1.bin / zkuste / wincom / download / unlzx.2.12.ME-src.cab / unlzx-src / unlzx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-03  |  36.4 KB  |  1,328 lines

  1. /*
  2. **  LZX Extract in (supposedly) portable C.
  3. **
  4. **  Based on unlzx 1.0 by David Tritscher.
  5. **  Rewritten by Oliver Gantert <lucyg@t-online.de>
  6. **
  7. **  Compiled with vbcc/Amiga and lcc/Win32
  8. */
  9. /*
  10.  Added some support for using with WinCommander MultiArc.
  11.  Siarzhuk Zharski - 2.10.2k
  12. */
  13.  
  14. #include "unlzx.h"
  15.  
  16. #ifdef AMIGA
  17. static unsigned char *version_string = "$VER: UnLZX " UNLZX_VERSION " (" UNLZX_VERDATE ")";
  18. #endif
  19.  
  20. #ifndef PMATCH_MAXSTRLEN
  21. #define PMATCH_MAXSTRLEN  1024    /*  max string length  */
  22. #endif
  23.  
  24. unsigned char match_pattern[256] = { '*', '\0' };
  25. signed long use_outdir = 0;
  26. unsigned char output_dir[768] = { '\0' };
  27. unsigned char work_buffer[1024];
  28.  
  29. signed long mode;
  30.  
  31. unsigned char info_header[10];
  32. unsigned char archive_header[31];
  33. unsigned char header_filename[256];
  34. unsigned char header_comment[256];
  35.  
  36. unsigned long pack_size;
  37. unsigned long unpack_size;
  38.  
  39. unsigned long crc;
  40. unsigned long year, month, day;
  41. unsigned long hour, minute, second;
  42. unsigned char attributes;
  43. unsigned char pack_mode;
  44.  
  45. struct filename_node *filename_list;
  46.  
  47. unsigned char read_buffer[16384];
  48. unsigned char decrunch_buffer[66560];
  49.  
  50. unsigned char *source;
  51. unsigned char *destination;
  52. unsigned char *source_end;
  53. unsigned char *destination_end;
  54.  
  55. unsigned long decrunch_method;
  56. unsigned long decrunch_length;
  57. unsigned long last_offset;
  58. unsigned long global_control;
  59. signed long global_shift;
  60.  
  61. unsigned char offset_len[8];
  62. unsigned short offset_table[128];
  63. unsigned char huffman20_len[20];
  64. unsigned short huffman20_table[96];
  65. unsigned char literal_len[768];
  66. unsigned short literal_table[5120];
  67.  
  68. const unsigned char *month_str[16] =
  69. {
  70.   "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug",
  71.   "sep", "oct", "nov", "dec", "?13", "?14", "?15", "?16"
  72. };
  73.  
  74. unsigned long sum;
  75.  
  76. const unsigned long crc_table[256] =
  77. {
  78.   0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
  79.   0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  80.   0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
  81.   0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  82.   0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  83.   0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  84.   0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
  85.   0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  86.   0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
  87.   0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  88.   0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
  89.   0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  90.   0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
  91.   0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  92.   0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
  93.   0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  94.   0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
  95.   0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  96.   0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
  97.   0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  98.   0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
  99.   0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  100.   0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
  101.   0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  102.   0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  103.   0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  104.   0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
  105.   0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  106.   0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
  107.   0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  108.   0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
  109.   0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  110.   0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
  111.   0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  112.   0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
  113.   0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  114.   0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
  115.   0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  116.   0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
  117.   0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  118.   0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
  119.   0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  120.   0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
  121. };
  122.  
  123. const unsigned char table_one[32] =
  124. {
  125.   0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02,
  126.   0x03, 0x03, 0x04, 0x04, 0x05, 0x05, 0x06, 0x06,
  127.   0x07, 0x07, 0x08, 0x08, 0x09, 0x09, 0x0a, 0x0a,
  128.   0x0b, 0x0b, 0x0c, 0x0c, 0x0d, 0x0d, 0x0e, 0x0e
  129. };
  130.  
  131. const unsigned long table_two[32]=
  132. {
  133.   0,    1,    2,    3,
  134.   4,    6,    8,    12,
  135.   16,   24,   32,   48,
  136.   64,   96,   128,  192,
  137.   256,  384,  512,  768,
  138.   1024, 1536, 2048, 3072,
  139.   4096, 6144, 8192, 12288,
  140.   16384,24576,32768,49152
  141. };
  142.  
  143. const unsigned long table_three[16]=
  144. {
  145.   0,    1,    3,    7,
  146.   15,   31,   63,   127,
  147.   255,  511,  1023, 2047,
  148.   4095, 8191, 16383,32767
  149. };
  150.  
  151. const unsigned char table_four[34]=
  152. {
  153.   0,    1,    2,    3,
  154.   4,    5,    6,    7,
  155.   8,    9,    10,   11,
  156.   12,   13,   14,   15,
  157.   16,   0,    1,    2,
  158.   3,    4,    5,    6,
  159.   7,    8,    9,    10,
  160.   11,   12,   13,   14,
  161.   15,   16
  162. };
  163.  
  164. /* -------------------------------------------------------------------------- */
  165.  
  166. int pmatch(const char *mask, const char *name)
  167. {
  168.   int           calls = 0,
  169.                 wild  = 0,
  170.                 q     = 0;
  171.   const char  * m     = mask,
  172.               * n     = name,
  173.               * ma    = mask,
  174.               * na    = name;
  175.  
  176.   for(;;)
  177.   {
  178.     if (++calls > PMATCH_MAXSTRLEN) return(1);
  179.     if (*m == '*')
  180.     {
  181.       while (*m == '*') ++m;
  182.       wild = 1;
  183.       ma = m;
  184.       na = n;
  185.     }
  186.     if (!*m)
  187.     {
  188.       if (!*n) return(0);
  189.       for (--m; (m > mask) && (*m == '?'); --m);
  190.       if ((*m == '*') && (m > mask) && (m[-1] != '\\')) return(0);
  191.       if (!wild) return(1);
  192.       m = ma;
  193.     }
  194.     else if (!*n)
  195.     {
  196.       while(*m == '*') ++m;
  197.       return(*m != 0);
  198.     }
  199.     if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
  200.     {
  201.       ++m;
  202.       q = 1;
  203.     }
  204.     else
  205.     {
  206.       q = 0;
  207.     }
  208.     if ((tolower(*m) != tolower(*n)) && ((*m != '?') || q))
  209.     {
  210.       if (!wild) return(1);
  211.       m = ma;
  212.       n = ++na;
  213.     }
  214.     else
  215.     {
  216.       if (*m) ++m;
  217.       if (*n) ++n;
  218.     }
  219.   }
  220. }
  221.  
  222. void just_wait(void)
  223. {
  224.   /*
  225.       Under certain conditions UnLZX needs to wait
  226.       some cycles for disk access to finish. This
  227.       only seems to happen on fast CPUs, but it
  228.       doesn't hurt anyway...
  229.   */
  230. }
  231.  
  232. unsigned long argopt(unsigned char * ao_strg, unsigned long ao_argc, unsigned char **ao_argv)
  233. {
  234.   while(ao_argc > 1)
  235.   {
  236.     if (!strcmp(ao_strg, ao_argv[ao_argc - 1]))
  237.       return(ao_argc - 1);
  238.     ao_argc--;
  239.   }
  240.   return(0);
  241. }
  242.  
  243. void crc_calc(unsigned char *memory, unsigned long length)
  244. {
  245.   unsigned long temp;
  246.  
  247.   if (length)
  248.   {
  249.     temp = ~sum;
  250.     do
  251.     {
  252.       temp = crc_table[(*memory++ ^ temp) & 255] ^ (temp >> 8);
  253.     } while(--length);
  254.     sum = ~temp;
  255.   }
  256. }
  257.  
  258. signed long make_decode_table(signed long number_symbols, signed long table_size, unsigned char *length, unsigned short *table)
  259. {
  260.   unsigned char bit_num = 0;
  261.   signed long symbol, abort = 0;
  262.   unsigned long leaf, table_mask, bit_mask, pos, fill, next_symbol, reverse;
  263.  
  264.   pos = 0;
  265.   table_mask = 1 << table_size;
  266.   bit_mask = table_mask >> 1;
  267.   bit_num++;
  268.   while ((!abort) && (bit_num <= table_size))
  269.   {
  270.     for (symbol = 0; symbol < number_symbols; symbol++)
  271.     {
  272.       if (length[symbol] == bit_num)
  273.       {
  274.         reverse = pos;
  275.         leaf = 0;
  276.         fill = table_size;
  277.         do
  278.         {
  279.           leaf = (leaf << 1)+(reverse & 1);
  280.           reverse >>= 1;
  281.         } while (--fill);
  282.         if ((pos += bit_mask) > table_mask)
  283.         {
  284.           abort = 1;
  285.           break;
  286.         }
  287.         fill = bit_mask;
  288.         next_symbol = 1 << bit_num;
  289.         do
  290.         {
  291.           table[leaf] = symbol;
  292.           leaf += next_symbol;
  293.         } while (--fill);
  294.       }
  295.     }
  296.     bit_mask >>= 1;
  297.     bit_num++;
  298.   }
  299.   if ((!abort) && (pos != table_mask))
  300.   {
  301.     for (symbol = pos; symbol < table_mask; symbol++)
  302.     {
  303.       reverse = symbol;
  304.       leaf = 0;
  305.       fill = table_size;
  306.       do
  307.       {
  308.         leaf = (leaf << 1)+(reverse & 1);
  309.         reverse >>= 1;
  310.       } while (--fill);
  311.       table[leaf] = 0;
  312.     }
  313.     next_symbol = table_mask >> 1;
  314.     pos <<= 16;
  315.     table_mask <<= 16;
  316.     bit_mask = 32768;
  317.     while((!abort) && (bit_num <= 16))
  318.     {
  319.       for(symbol = 0; symbol < number_symbols; symbol++)
  320.       {
  321.         if (length[symbol] == bit_num)
  322.         {
  323.           reverse = pos >> 16;
  324.           leaf = 0;
  325.           fill = table_size;
  326.           do
  327.           {
  328.             leaf = (leaf << 1)+(reverse & 1);
  329.             reverse >>= 1;
  330.           } while (--fill);
  331.           for (fill = 0; fill < bit_num - table_size; fill++)
  332.           {
  333.             if (!table[leaf])
  334.             {
  335.               table[(next_symbol << 1)] = 0;
  336.               table[(next_symbol << 1) + 1] = 0;
  337.               table[leaf] = next_symbol++;
  338.             }
  339.             leaf = table[leaf] << 1;
  340.             leaf += (pos >> (15 - fill)) & 1;
  341.           }
  342.           table[leaf] = symbol;
  343.           if ((pos += bit_mask) > table_mask)
  344.           {
  345.             abort = 1;
  346.             break;
  347.           }
  348.         }
  349.       }
  350.       bit_mask >>= 1;
  351.       bit_num++;
  352.     }
  353.   }
  354.   if (pos != table_mask) abort = 1;
  355.   return(abort);
  356. }
  357.  
  358. signed long read_literal_table(void)
  359. {
  360.   signed long shift = global_shift, abort = 0;
  361.   unsigned long control = global_control, temp, symbol, pos, count, fix, max_symbol;
  362.  
  363.   if (shift < 0)
  364.   {
  365.     shift += 16;
  366.     control += *source++ << (8 + shift);
  367.     control += *source++ << shift;
  368.   }
  369.   decrunch_method = control & 7;
  370.   control >>= 3;
  371.   if ((shift -= 3) < 0)
  372.   {
  373.     shift += 16;
  374.     control += *source++ << (8 + shift);
  375.     control += *source++ << shift;
  376.   }
  377.   if ((!abort) && (decrunch_method == 3))
  378.   {
  379.     for (temp = 0; temp < 8; temp++)
  380.     {
  381.       offset_len[temp] = control & 7;
  382.       control >>= 3;
  383.       if ((shift -= 3) < 0)
  384.       {
  385.         shift += 16;
  386.         control += *source++ << (8 + shift);
  387.         control += *source++ << shift;
  388.       }
  389.     }
  390.     abort = make_decode_table(8, 7, offset_len, offset_table);
  391.   }
  392.   if (!abort)
  393.   {
  394.     decrunch_length = (control & 255) << 16;
  395.     control >>= 8;
  396.     if ((shift -= 8) < 0)
  397.     {
  398.       shift += 16;
  399.       control += *source++ << (8 + shift);
  400.       control += *source++ << shift;
  401.     }
  402.     decrunch_length += (control & 255) << 8;
  403.     control >>= 8;
  404.     if ((shift -= 8) < 0)
  405.     {
  406.       shift += 16;
  407.       control += *source++ << (8 + shift);
  408.       control += *source++ << shift;
  409.     }
  410.     decrunch_length += (control & 255);
  411.     control >>= 8;
  412.     if ((shift -= 8) < 0)
  413.     {
  414.       shift += 16;
  415.       control += *source++ << (8 + shift);
  416.       control += *source++ << shift;
  417.     }
  418.   }
  419.   if ((!abort) && (decrunch_method != 1))
  420.   {
  421.     pos = 0;
  422.     fix = 1;
  423.     max_symbol = 256;
  424.     do
  425.     {
  426.       for (temp = 0; temp < 20; temp++)
  427.       {
  428.         huffman20_len[temp] = control & 15;
  429.         control >>= 4;
  430.         if ((shift -= 4) < 0)
  431.         {
  432.           shift += 16;
  433.           control += *source++ << (8 + shift);
  434.           control += *source++ << shift;
  435.         }
  436.       }
  437.       if (abort = make_decode_table(20, 6, huffman20_len, huffman20_table)) break;
  438.       do
  439.       {
  440.         if ((symbol = huffman20_table[control & 63]) >= 20)
  441.         {
  442.           do
  443.           {
  444.             symbol = huffman20_table[((control >> 6) & 1) + (symbol << 1)];
  445.             if (!shift--)
  446.             {
  447.               shift += 16;
  448.               control += *source++ << 24;
  449.               control += *source++ << 16;
  450.             }
  451.             control >>= 1;
  452.           } while(symbol >= 20);
  453.           temp = 6;
  454.         }
  455.         else
  456.         {
  457.           temp = huffman20_len[symbol];
  458.         }
  459.         control >>= temp;
  460.         if ((shift -= temp) < 0)
  461.         {
  462.           shift += 16;
  463.           control += *source++ << (8 + shift);
  464.           control += *source++ << shift;
  465.         }
  466.         switch(symbol)
  467.         {
  468.           case 17:
  469.           case 18:
  470.             if(symbol == 17)
  471.             {
  472.               temp = 4;
  473.               count = 3;
  474.             }
  475.             else
  476.             {
  477.               temp = 6 - fix;
  478.               count = 19;
  479.             }
  480.             count += (control & table_three[temp]) + fix;
  481.             control >>= temp;
  482.             if ((shift -= temp) < 0)
  483.             {
  484.               shift += 16;
  485.               control += *source++ << (8 + shift);
  486.               control += *source++ << shift;
  487.             }
  488.             while ((pos < max_symbol) && (count--)) literal_len[pos++] = 0;
  489.           break;
  490.           case 19:
  491.             count = (control & 1) + 3 + fix;
  492.             if (!shift--)
  493.             {
  494.               shift += 16;
  495.               control += *source++ << 24;
  496.               control += *source++ << 16;
  497.             }
  498.             control >>= 1;
  499.             if ((symbol = huffman20_table[control & 63]) >= 20)
  500.             {
  501.               do
  502.               {
  503.                 symbol = huffman20_table[((control >> 6) & 1) + (symbol << 1)];
  504.                 if (!shift--)
  505.                 {
  506.                   shift += 16;
  507.                   control += *source++ << 24;
  508.                   control += *source++ << 16;
  509.                 }
  510.                 control >>= 1;
  511.               } while(symbol >= 20);
  512.               temp = 6;
  513.             }
  514.             else
  515.             {
  516.               temp = huffman20_len[symbol];
  517.             }
  518.             control >>= temp;
  519.             if ((shift -= temp) < 0)
  520.             {
  521.               shift += 16;
  522.               control += *source++ << (8 + shift);
  523.               control += *source++ << shift;
  524.             }
  525.             symbol = table_four[literal_len[pos] + 17 - symbol];
  526.             while ((pos < max_symbol) && (count--)) literal_len[pos++] = symbol;
  527.           break;
  528.           default:
  529.             symbol = table_four[literal_len[pos] + 17 - symbol];
  530.             literal_len[pos++] = symbol;
  531.           break;
  532.         }
  533.       } while(pos < max_symbol);
  534.       fix--;
  535.       max_symbol += 512;
  536.     } while(max_symbol == 768);
  537.     if(!abort) abort = make_decode_table(768, 12, literal_len, literal_table);
  538.   }
  539.   global_control = control;
  540.   global_shift = shift;
  541.   return(abort);
  542. }
  543.  
  544. void decrunch(void)
  545. {
  546.   unsigned long control = global_control, temp, symbol, count;
  547.   signed long shift = global_shift;
  548.   unsigned char *string;
  549.  
  550.   do
  551.   {
  552.     if ((symbol = literal_table[control & 4095]) >= 768)
  553.     {
  554.       control >>= 12;
  555.       if ((shift -= 12) < 0)
  556.       {
  557.         shift += 16;
  558.         control += *source++ << (8 + shift);
  559.         control += *source++ << shift;
  560.       }
  561.       do
  562.       {
  563.         symbol = literal_table[(control & 1) + (symbol << 1)];
  564.         if (!shift--)
  565.         {
  566.           shift += 16;
  567.           control += *source++ << 24;
  568.           control += *source++ << 16;
  569.         }
  570.         control >>= 1;
  571.       } while(symbol >= 768);
  572.     }
  573.     else
  574.     {
  575.       temp = literal_len[symbol];
  576.       control >>= temp;
  577.       if ((shift -= temp) < 0)
  578.       {
  579.         shift += 16;
  580.         control += *source++ << (8 + shift);
  581.         control += *source++ << shift;
  582.       }
  583.     }
  584.     if (symbol < 256)
  585.     {
  586.       *destination++ = symbol;
  587.     }
  588.     else
  589.     {
  590.       symbol -= 256;
  591.       count = table_two[temp = symbol & 31];
  592.       temp = table_one[temp];
  593.       if ((temp >= 3) && (decrunch_method == 3))
  594.       {
  595.         temp -= 3;
  596.         count += ((control & table_three[temp]) << 3);
  597.         control >>= temp;
  598.         if ((shift -= temp) < 0)
  599.         {
  600.           shift += 16;
  601.           control += *source++ << (8 + shift);
  602.           control += *source++ << shift;
  603.         }
  604.         count += (temp = offset_table[control & 127]);
  605.         temp = offset_len[temp];
  606.       }
  607.       else
  608.       {
  609.         count += control & table_three[temp];
  610.         if (!count) count = last_offset;
  611.       }
  612.       control >>= temp;
  613.       if ((shift -= temp) < 0)
  614.       {
  615.         shift += 16;
  616.         control += *source++ << (8 + shift);
  617.         control += *source++ << shift;
  618.       }
  619.       last_offset = count;
  620.       count = table_two[temp = (symbol >> 5) & 15] + 3;
  621.       temp = table_one[temp];
  622.       count += (control & table_three[temp]);
  623.       control >>= temp;
  624.       if ((shift -= temp) < 0)
  625.       {
  626.         shift += 16;
  627.         control += *source++ << (8 + shift);
  628.         control += *source++ << shift;
  629.       }
  630.       string = (decrunch_buffer + last_offset < destination) ? destination - last_offset : destination + 65536 - last_offset;
  631.       do
  632.       {
  633.         *destination++ = *string++;
  634.       } while(--count);
  635.     }
  636.   } while((destination < destination_end) && (source < source_end));
  637.   global_control = control;
  638.   global_shift = shift;
  639. }
  640.  
  641. FILE *open_output(unsigned char * filename)
  642. {
  643.   unsigned long temp,  /*SZ*/ last_dir_sep=0;
  644.   FILE *file;
  645.  
  646.   if (!(file = fopen(filename, "wb")))
  647.   {
  648.     for(temp = 0; filename[temp]; temp++)
  649.     {
  650.       if (filename[temp] == '/')
  651.       {
  652.         last_dir_sep = temp;
  653.         /*SZ - skip directories creating
  654.         filename[temp] = 0;
  655.         mkdir(filename, 511);
  656.         filename[temp] = '/';
  657.         */
  658.       }
  659.     }
  660.     //SZ
  661.     //file = fopen(filename, "wb");
  662.     file = fopen(&filename[last_dir_sep+1], "wb");
  663.   }
  664.   return(file);
  665. }
  666.  
  667. signed long extract_normal(FILE *in_file)
  668. {
  669.   struct filename_node *node;
  670.   FILE *out_file = 0;
  671.   unsigned char *pos, *temp;
  672.   unsigned long count;
  673.   signed long abort = 0;
  674.  
  675.   global_control = 0;
  676.   global_shift = -16;
  677.   last_offset = 1;
  678.   unpack_size = 0;
  679.   decrunch_length = 0;
  680.   for(count = 0; count < 8; count++) offset_len[count] = 0;
  681.   for(count = 0; count < 768; count ++) literal_len[count] = 0;
  682.   source_end = (source = read_buffer + 16384) - 1024;
  683.   pos = destination_end = destination = decrunch_buffer + 65794;
  684.   for (node = filename_list; (!abort) && node; node = node->next)
  685.   {
  686.     sum = 0;
  687.     printf("Extracting \"%s\"...", node->filename);
  688.     if (use_outdir)
  689.     {
  690.       strcpy(work_buffer, output_dir);
  691.       strcat(work_buffer, node->filename);
  692.     }
  693.     else
  694.     {
  695.       strcpy(work_buffer, node->filename);
  696.     }
  697.     fflush(stdout);
  698.     if (!pmatch(match_pattern, node->filename))
  699.       out_file = open_output(work_buffer);
  700.     else
  701.       out_file = 0;
  702.     unpack_size = node->length;
  703.     while(unpack_size > 0)
  704.     {
  705.       if (pos == destination)
  706.       {
  707.         if(source >= source_end)
  708.         {
  709.           temp = read_buffer;
  710.           if (count = temp - source + 16384)
  711.           {
  712.             do
  713.             {
  714.               *temp++ = *source++;
  715.             } while(--count);
  716.           }
  717.           source = read_buffer;
  718.           count = source - temp + 16384;
  719.           if (pack_size < count) count = pack_size;
  720.           if (fread(temp, 1, count, in_file) != count)
  721.           {
  722.             printf("\n");
  723. #ifdef UNLZX_DEBUG
  724.             if (ferror(in_file))
  725.             {
  726.               perror("FRead(Data)");
  727.             }
  728.             else
  729.             {
  730.               fprintf(stderr, "EOF: Data\n");
  731.             }
  732. #endif
  733.             abort = 1;
  734.             break;
  735.           }
  736.           pack_size -= count;
  737.           temp += count;
  738.           if (source >= temp) break;
  739.         }
  740.         if (decrunch_length <= 0)
  741.         {
  742.           if (read_literal_table()) break;
  743.         }
  744.         if (destination >= decrunch_buffer + 65794)
  745.         {
  746.           if (count = destination - decrunch_buffer - 65536)
  747.           {
  748.             temp = (destination = decrunch_buffer) + 65536;
  749.             do
  750.             {
  751.               *destination++ = *temp++;
  752.             } while(--count);
  753.           }
  754.           pos = destination;
  755.         }
  756.         destination_end = destination + decrunch_length;
  757.         if(destination_end > decrunch_buffer + 65794) destination_end = decrunch_buffer + 65794;
  758.         temp = destination;
  759.         decrunch();
  760.         decrunch_length -= (destination - temp);
  761.       }
  762.       count = destination - pos;
  763.       if (count > unpack_size) count = unpack_size;
  764.       crc_calc(pos, count);
  765.       if (out_file)
  766.       {
  767.         if (fwrite(pos, 1, count, out_file) != count)
  768.         {
  769. #ifdef UNLZX_DEBUG
  770.           perror("FWrite");
  771. #endif
  772.           fclose(out_file);
  773.           out_file = 0;
  774.         }
  775.       }
  776.       unpack_size -= count;
  777.       pos += count;
  778.     }
  779.     if (out_file)
  780.     {
  781.       fclose(out_file);
  782.       if (!abort) printf(" crc %s\n", (node->crc == sum) ? "good" : "bad");
  783.     }
  784.   }
  785.   return(abort);
  786. }
  787.  
  788. signed long extract_store(FILE *in_file)
  789. {
  790.   struct filename_node *node;
  791.   FILE *out_file;
  792.   unsigned long count;
  793.   signed long abort = 0;
  794.  
  795.   for (node = filename_list; (!abort) && (node); node = node->next)
  796.   {
  797.     sum = 0;
  798.     printf("Storing \"%s\"...", node->filename);
  799.     if (use_outdir)
  800.     {
  801.       strcpy(work_buffer, output_dir);
  802.       strcat(work_buffer, node->filename);
  803.     }
  804.     else
  805.     {
  806.       strcpy(work_buffer, node->filename);
  807.     }
  808.     fflush(stdout);
  809.     if (!pmatch(match_pattern, node->filename))
  810.       out_file = open_output(work_buffer);
  811.     else
  812.       out_file = 0;
  813.     unpack_size = node->length;
  814.     if (unpack_size > pack_size) unpack_size = pack_size;
  815.     while (unpack_size > 0)
  816.     {
  817.       count = (unpack_size > 16384) ? 16384 : unpack_size;
  818.       if (fread(read_buffer, 1, count, in_file) != count)
  819.       {
  820.         printf("\n");
  821. #ifdef UNLZX_DEBUG
  822.         if (ferror(in_file))
  823.         {
  824.           perror("FRead(Data)");
  825.         }
  826.         else
  827.         {
  828.           fprintf(stderr, "EOF: Data\n");
  829.         }
  830. #endif
  831.         abort = 1;
  832.         break;
  833.       }
  834.       pack_size -= count;
  835.       crc_calc(read_buffer, count);
  836.       if (out_file)
  837.       {
  838.         if (fwrite(read_buffer, 1, count, out_file) != count)
  839.         {
  840. #ifdef UNLZX_DEBUG
  841.           perror("FWrite");
  842. #endif
  843.           fclose(out_file);
  844.           out_file = 0;
  845.         }
  846.       }
  847.       unpack_size -= count;
  848.     }
  849.     if (out_file)
  850.     {
  851.       fclose(out_file);
  852.       if (!abort) printf(" crc %s\n", (node->crc == sum) ? "good" : "bad");
  853.     }
  854.   }
  855.   return(abort);
  856. }
  857.  
  858. signed long extract_unknown(FILE *in_file)
  859. {
  860.   struct filename_node *node;
  861.  
  862.   for (node = filename_list; (node); node = node->next)
  863.   {
  864.     printf("Unknown \"%s\"\n", node->filename);
  865.   }
  866.   return(0);
  867. }
  868.  
  869. signed long extract_archive(FILE *in_file)
  870. {
  871.   unsigned long temp;
  872.   struct filename_node **filename_next;
  873.   struct filename_node *node;
  874.   struct filename_node *temp_node;
  875.   signed long actual, abort, result = 1;
  876.  
  877.   filename_list = 0;
  878.   filename_next = &filename_list;
  879.   do
  880.   {
  881.     abort = 1;
  882.     actual = fread(archive_header, 1, 31, in_file);
  883.     if (!ferror(in_file))
  884.     {
  885.       if (actual)
  886.       {
  887.         if (actual == 31)
  888.         {
  889.           sum = 0;
  890.           crc = (archive_header[29] << 24) + (archive_header[28] << 16) + (archive_header[27] << 8) + archive_header[26];
  891.           archive_header[29] =
  892.           archive_header[28] =
  893.           archive_header[27] =
  894.           archive_header[26] = 0;
  895.           crc_calc(archive_header, 31);
  896.           temp = archive_header[30];
  897.           actual = fread(header_filename, 1, temp, in_file);
  898.           if (!ferror(in_file))
  899.           {
  900.             if (actual == temp)
  901.             {
  902.               header_filename[temp] = 0;
  903.               crc_calc(header_filename, temp);
  904.               temp = archive_header[14];
  905.               actual = fread(header_comment, 1, temp, in_file);
  906.               if (!ferror(in_file))
  907.               {
  908.                 if (actual == temp)
  909.                 {
  910.                   header_comment[temp] = 0;
  911.                   crc_calc(header_comment, temp);
  912.                   if (sum == crc)
  913.                   {
  914.                     unpack_size = (archive_header[5] << 24) + (archive_header[4] << 16) + (archive_header[3] << 8) + archive_header[2];
  915.                     pack_size = (archive_header[9] << 24) + (archive_header[8] << 16) + (archive_header[7] << 8) + archive_header[6];
  916.                     pack_mode = archive_header[11];
  917.                     crc = (archive_header[25] << 24) + (archive_header[24] << 16) + (archive_header[23] << 8) + archive_header[22];
  918.                     if (node = (struct filename_node *)malloc(sizeof(struct filename_node)))
  919.                     {
  920.                       *filename_next = node;
  921.                       filename_next = &(node->next);
  922.                       node->next = 0;
  923.                       node->length = unpack_size;
  924.                       node->crc = crc;
  925.                       for (temp = 0; node->filename[temp] = header_filename[temp]; temp++);
  926.                       if (pack_size)
  927.                       {
  928.                         switch(pack_mode)
  929.                         {
  930.                           case 0:
  931.                             abort = extract_store(in_file);
  932.                           break;
  933.                           case 2:
  934.                             abort = extract_normal(in_file);
  935.                           break;
  936.                           default:
  937.                             abort = extract_unknown(in_file);
  938.                           break;
  939.                         }
  940.                         if (abort) break;
  941.                         temp_node = filename_list;
  942.                         while (node = temp_node)
  943.                         {
  944.                           temp_node = node->next;
  945.                           free(node);
  946.                         }
  947.                         filename_list = 0;
  948.                         filename_next = &filename_list;
  949.                         if (fseek(in_file, pack_size, SEEK_CUR))
  950.                         {
  951. #ifdef UNLZX_DEBUG
  952.                           perror("FSeek(Data)");
  953. #endif
  954.                           break;
  955.                         }
  956.                       }
  957.                       else
  958.                       {
  959.                         abort = 0;
  960.                       }
  961.                     }
  962. #ifdef UNLZX_DEBUG
  963.                     else
  964.                     {
  965.                       fprintf(stderr, "MAlloc(Filename_node)\n");
  966.                     }
  967. #endif
  968.                   }
  969. #ifdef UNLZX_DEBUG
  970.                   else
  971.                   {
  972.                     fprintf(stderr, "CRC: Archive_Header\n");
  973.                   }
  974. #endif
  975.                 }
  976. #ifdef UNLZX_DEBUG
  977.                 else
  978.                 {
  979.                   fprintf(stderr, "EOF: Header_Comment\n");
  980.                 }
  981. #endif
  982.               }
  983. #ifdef UNLZX_DEBUG
  984.               else
  985.               {
  986.                 perror("FRead(Header_Comment)");
  987.               }
  988. #endif
  989.             }
  990. #ifdef UNLZX_DEBUG
  991.             else
  992.             {
  993.               fprintf(stderr, "EOF: Header_Filename\n");
  994.             }
  995. #endif
  996.           }
  997. #ifdef UNLZX_DEBUG
  998.           else
  999.           {
  1000.             perror("FRead(Header_Filename)");
  1001.           }
  1002. #endif
  1003.         }
  1004. #ifdef UNLZX_DEBUG
  1005.         else
  1006.         {
  1007.           fprintf(stderr, "EOF: Archive_Header\n");
  1008.         }
  1009. #endif
  1010.       }
  1011.       else
  1012.       {
  1013.         result = 0;
  1014.       }
  1015.     }
  1016. #ifdef UNLZX_DEBUG
  1017.     else
  1018.     {
  1019.       perror("FRead(Archive_Header)");
  1020.     }
  1021. #endif
  1022.   } while(!abort);
  1023.   temp_node = filename_list;
  1024.   while(node = temp_node)
  1025.   {
  1026.     temp_node = node->next;
  1027.     free(node);
  1028.   }
  1029.   return(result);
  1030. }
  1031.  
  1032. signed long view_archive(FILE *in_file)
  1033. {
  1034.   unsigned long temp, total_pack = 0, total_unpack = 0, total_files = 0, merge_size = 0;
  1035.   signed long actual, abort, result = 1;
  1036.  
  1037.   printf("Unpacked   Packed Time     Date        Attrib   Name\n");
  1038.   // SZ
  1039.   //printf("-------- -------- -------- ----------- -------- ----\n");
  1040.   printf("BEGIN -- -------- -------- ----------- -------- ----\n");
  1041.   do
  1042.   {
  1043.     abort = 1;
  1044.     actual = fread(archive_header, 1, 31, in_file);
  1045.     if (!ferror(in_file))
  1046.     {
  1047.       if (actual)
  1048.       {
  1049.         if (actual == 31)
  1050.         {
  1051.           sum = 0;
  1052.           crc = (archive_header[29] << 24) + (archive_header[28] << 16) + (archive_header[27] << 8) + archive_header[26];
  1053.           archive_header[29] =
  1054.           archive_header[28] =
  1055.           archive_header[27] =
  1056.           archive_header[26] = 0;
  1057.           crc_calc(archive_header, 31);
  1058.           temp = archive_header[30];
  1059.           actual = fread(header_filename, 1, temp, in_file);
  1060.           if (!ferror(in_file))
  1061.           {
  1062.             if (actual == temp)
  1063.             {
  1064.               header_filename[temp] = 0;
  1065.               crc_calc(header_filename, temp);
  1066.               temp = archive_header[14];
  1067.               actual = fread(header_comment, 1, temp, in_file);
  1068.               if (!ferror(in_file))
  1069.               {
  1070.                 if (actual == temp)
  1071.                 {
  1072.                   header_comment[temp] = 0;
  1073.                   crc_calc(header_comment, temp);
  1074.                   if (sum == crc)
  1075.                   {
  1076.                     attributes = archive_header[0];
  1077.                     unpack_size = (archive_header[5] << 24) + (archive_header[4] << 16) + (archive_header[3] << 8) + archive_header[2];
  1078.                     pack_size = (archive_header[9] << 24) + (archive_header[8] << 16) + (archive_header[7] << 8) + archive_header[6];
  1079.                     temp = (archive_header[18] << 24) + (archive_header[19] << 16) + (archive_header[20] << 8) + archive_header[21];
  1080.                     year = ((temp >> 17) & 63) + 1970;
  1081.                     month = (temp >> 23) & 15;
  1082.                     day = (temp >> 27) & 31;
  1083.                     hour = (temp >> 12) & 31;
  1084.                     minute = (temp >> 6) & 63;
  1085.                     second = temp & 63;
  1086.                     total_pack += pack_size;
  1087.                     total_unpack += unpack_size;
  1088.                     total_files++;
  1089.                     merge_size += unpack_size;
  1090.                     printf("%8ld ", unpack_size);
  1091.                     if (archive_header[12] & 1)
  1092.                     {
  1093.                       printf("     n/a ");
  1094.                     }
  1095.                     else
  1096.                     {
  1097.                       printf("%8ld ", pack_size);
  1098.                     }
  1099.                     printf("%02ld:%02ld:%02ld ", hour, minute, second);
  1100.                     printf("%2ld-%s-%4ld ", day, month_str[month], year);
  1101.                     printf("%c%c%c%c%c%c%c%c ",
  1102.                       (attributes & 32) ? 'h' : '-',
  1103.                       (attributes & 64) ? 's' : '-',
  1104.                       (attributes & 128) ? 'p' : '-',
  1105.                       (attributes & 16) ? 'a' : '-',
  1106.                       (attributes & 1) ? 'r' : '-',
  1107.                       (attributes & 2) ? 'w' : '-',
  1108.                       (attributes & 8) ? 'e' : '-',
  1109.                       (attributes & 4) ? 'd' : '-');
  1110.                     //SZ
  1111.                     //printf("\"%s\"\n", header_filename);
  1112.                     printf("%s\n", header_filename);
  1113.                     //SZ
  1114.                     //if(header_comment[0]) printf(": \"%s\"\n", header_comment);
  1115.                     if ((archive_header[12] & 1) && pack_size)
  1116.                     {
  1117.                       //SZ
  1118.                       //printf("%8ld %8ld Merged\n", merge_size, pack_size);
  1119.                     }
  1120.                     if (pack_size)
  1121.                     {
  1122.                       merge_size = 0;
  1123.                       if (!fseek(in_file, pack_size, SEEK_CUR))
  1124.                       {
  1125.                         abort = 0;
  1126.                         just_wait();
  1127.                       }
  1128. #ifdef UNLZX_DEBUG
  1129.                       else
  1130.                       {
  1131.                         perror("FSeek()");
  1132.                       }
  1133. #endif
  1134.                     }
  1135.                     else
  1136.                     {
  1137.                       abort = 0;
  1138.                     }
  1139.                   }
  1140. #ifdef UNLZX_DEBUG
  1141.                   else
  1142.                   {
  1143.                     fprintf(stderr, "CRC: Archive_Header\n");
  1144.                   }
  1145. #endif
  1146.                 }
  1147. #ifdef UNLZX_DEBUG
  1148.                 else
  1149.                 {
  1150.                   fprintf(stderr, "EOF: Header_Comment\n");
  1151.                 }
  1152. #endif
  1153.               }
  1154. #ifdef UNLZX_DEBUG
  1155.               else
  1156.               {
  1157.                 perror("FRead(Header_Comment)");
  1158.               }
  1159. #endif
  1160.             }
  1161. #ifdef UNLZX_DEBUG
  1162.             else
  1163.             {
  1164.               fprintf(stderr, "EOF: Header_Filename\n");
  1165.             }
  1166. #endif
  1167.           }
  1168. #ifdef UNLZX_DEBUG
  1169.           else
  1170.           {
  1171.             perror("FRead(Header_Filename)");
  1172.           }
  1173. #endif
  1174.         }
  1175. #ifdef UNLZX_DEBUG
  1176.         else
  1177.         {
  1178.           fprintf(stderr, "EOF: Archive_Header\n");
  1179.         }
  1180. #endif
  1181.       }
  1182.       else
  1183.       {
  1184.         //SZ
  1185.         //printf("-------- -------- -------- ----------- -------- ----\n");
  1186.         printf("END ---- -------- -------- ----------- -------- ----\n");
  1187.         printf("%8ld %8ld ", total_unpack, total_pack);
  1188.         printf("%ld file%s\n", total_files, ((total_files == 1) ? "" : "s"));
  1189.         result = 0;
  1190.       }
  1191.     }
  1192. #ifdef UNLZX_DEBUG
  1193.     else
  1194.     {
  1195.       perror("FRead(Archive_Header)");
  1196.     }
  1197. #endif
  1198.   } while(!abort);
  1199.   return(result);
  1200. }
  1201.  
  1202. signed long process_archive(unsigned char * filename)
  1203. {
  1204.   signed long result = 1, actual;
  1205.   FILE *in_file;
  1206.  
  1207.   if (in_file = fopen(filename,"rb"))
  1208.   {
  1209.     actual = fread(info_header, 1, 10, in_file);
  1210.     if (!ferror(in_file))
  1211.     {
  1212.       if (actual == 10)
  1213.       {
  1214.         if ((info_header[0] == 76) && (info_header[1] == 90) && (info_header[2] == 88))
  1215.         {
  1216.           switch (mode)
  1217.           {
  1218.             case 1:
  1219.               result = extract_archive(in_file);
  1220.             break;
  1221.             case 2:
  1222.               result = view_archive(in_file);
  1223.             break;
  1224.           }
  1225.         }
  1226. #ifdef UNLZX_DEBUG
  1227.         else
  1228.         {
  1229.           fprintf(stderr, "Info_Header: Bad ID\n");
  1230.         }
  1231. #endif
  1232.       }
  1233. #ifdef UNLZX_DEBUG
  1234.       else
  1235.       {
  1236.         fprintf(stderr, "EOF: Info_Header\n");
  1237.       }
  1238. #endif
  1239.     }
  1240. #ifdef UNLZX_DEBUG
  1241.     else
  1242.     {
  1243.       perror("FRead(Info_Header)");
  1244.     }
  1245. #endif
  1246.     fclose(in_file);
  1247.   }
  1248. #ifdef UNLZX_DEBUG
  1249.   else
  1250.   {
  1251.     perror("FOpen(Archive)");
  1252.   }
  1253. #endif
  1254.   return(result);
  1255. }
  1256.  
  1257. int main(int argc, char **argv)
  1258. {
  1259.   int result = 0;
  1260.   unsigned long option;
  1261.   unsigned char tmp;
  1262.  
  1263.   if (option = argopt("-p", (unsigned long)argc, (unsigned char **)argv))
  1264.   {
  1265.     if (++option < argc)
  1266.     {
  1267.       
  1268.       strcpy(match_pattern, argv[option]);
  1269.       //SZ begin
  1270.       {
  1271.         char *p;
  1272.         for(p=match_pattern;*p;p++)
  1273.           if(*p == '\\') *p = '/';
  1274.       }
  1275.       //SZ ende
  1276.     }
  1277.   }
  1278.   if (option = argopt("-o", (unsigned long)argc, (unsigned char **)argv))
  1279.   {
  1280.     if (++option < argc)
  1281.     {
  1282.       strcpy(output_dir, argv[option]);
  1283.       use_outdir = 1;
  1284.  
  1285.       tmp = strlen(output_dir) - 1;
  1286. #ifdef AMIGA
  1287.       if ((output_dir[tmp] != '/') && (output_dir[tmp] != ':'))
  1288.       {
  1289.         output_dir[tmp+1] = '/';
  1290. #else
  1291.       if (output_dir[tmp] != '\\')
  1292.       {
  1293.         output_dir[tmp+1] = '\\';
  1294. #endif
  1295.         output_dir[tmp+2] = '\0';
  1296.       }
  1297.     }
  1298.   }
  1299.  
  1300.   if (option = argopt("-v", (unsigned long)argc, (unsigned char **)argv))
  1301.   {
  1302.     mode = 2;
  1303.   }
  1304.   else if (option = argopt("-x", (unsigned long)argc, (unsigned char **)argv))
  1305.   {
  1306.     mode = 1;
  1307.   }
  1308.   else
  1309.   {
  1310.     result = 1;
  1311.   }
  1312.   if ((!result) && (++option < argc))
  1313.   {
  1314.     result = process_archive(argv[option]);
  1315.   }
  1316.   else
  1317.   {
  1318.     printf( "Usage: %s <options>\n", argv[0] );
  1319.     printf( "\t-v <archive> : list archive\n"
  1320.             "\t-x <archive> : extract archive\n"
  1321.             "\t-p <pattern> : only matching files\n"
  1322.             "\t-o <outpath> : destination path\n" );
  1323.     result = 2;
  1324.   }
  1325.   return(result);
  1326. }
  1327.  
  1328.