home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / macutils.lzh / MACUTILS / MACUNPACK / cpt.c < prev    next >
C/C++ Source or Header  |  1995-09-18  |  18KB  |  720 lines

  1. #include "macunpack.h"
  2. #ifdef DD
  3. #ifndef CPT
  4. #define CPT
  5. #endif /* CPT */
  6. #endif /* DD */
  7. #ifdef CPT
  8. #include "globals.h"
  9. #include "cpt.h"
  10. #include "crc.h"
  11. #include "../util/util.h"
  12. #include "../fileio/machdr.h"
  13. #include "../fileio/wrfile.h"
  14. #include "../fileio/kind.h"
  15. #include "../util/masks.h"
  16. #include "huffman.h"
  17.  
  18. #define    ESC1        0x81
  19. #define    ESC2        0x82
  20. #define NONESEEN    0
  21. #define ESC1SEEN    1
  22. #define ESC2SEEN    2
  23.  
  24. extern char *malloc();
  25. extern char *realloc();
  26. extern int free();
  27.  
  28. static void cpt_uncompact();
  29. static unsigned char *cpt_data;
  30. static unsigned long cpt_datamax;
  31. static unsigned long cpt_datasize;
  32. static unsigned char cpt_LZbuff[CIRCSIZE];
  33. static unsigned int cpt_LZptr;
  34. static unsigned char *cpt_char;
  35. static unsigned long cpt_crc;
  36. static unsigned long cpt_inlength;
  37. static unsigned long cpt_outlength;
  38. static int cpt_outstat;
  39. static unsigned char cpt_savechar;
  40. static unsigned long cpt_newbits;
  41. static int cpt_bitsavail;
  42. static int cpt_blocksize;
  43. /* Lengths is twice the max number of entries, and include slack. */
  44. #define SLACK    6
  45. static node cpt_Hufftree[512 + SLACK], cpt_LZlength[128 + SLACK],
  46.         cpt_LZoffs[256 + SLACK];
  47.  
  48. static int readcpthdr();
  49. static int cpt_filehdr();
  50. static void cpt_folder();
  51. static void cpt_uncompact();
  52. static void cpt_wrfile();
  53. void cpt_wrfile1();
  54. static void cpt_outch();
  55. static void cpt_rle();
  56. static void cpt_rle_lzh();
  57. static void cpt_readHuff();
  58. static int cpt_get6bits();
  59. static int cpt_getbit();
  60.  
  61. void cpt()
  62. {
  63.     struct cptHdr cpthdr;
  64.     struct fileHdr filehdr;
  65.     char *cptindex;
  66.     int cptindsize;
  67.     char *cptptr;
  68.     int i;
  69.  
  70.     updcrc = zip_updcrc;
  71.     crcinit = zip_crcinit;
  72.     cpt_crc = INIT_CRC;
  73.     if(readcpthdr(&cpthdr) == 0) {
  74.     (void)fprintf(stderr, "Can't read archive header\n");
  75. #ifdef SCAN
  76.     do_error("macunpack: Can't read archive header");
  77. #endif /* SCAN */
  78.     exit(1);
  79.     }
  80.  
  81.     cptindsize = cpthdr.entries * FILEHDRSIZE;
  82.     if(cpthdr.commentsize > cptindsize) {
  83.     cptindsize = cpthdr.commentsize;
  84.     }
  85.     cptindex = malloc((unsigned)cptindsize);
  86.     if(cptindex == NULL) {
  87.     (void)fprintf(stderr, "Insufficient memory, aborting\n");
  88.     exit(1);
  89.     }
  90.     cptptr = cptindex;
  91.     if(fread(cptptr, 1, (int)cpthdr.commentsize, infp) != cpthdr.commentsize) {
  92.     (void)fprintf(stderr, "Can't read comment.\n");
  93. #ifdef SCAN
  94.     do_error("macunpack: Can't read comment");
  95. #endif /* SCAN */
  96.     exit(1);
  97.     }
  98.     cpt_crc = (*updcrc)(cpt_crc, cptptr, cpthdr.commentsize);
  99.  
  100.     for(i = 0; i < cpthdr.entries; i++) {
  101.     *cptptr = getc(infp);
  102.     cpt_crc = (*updcrc)(cpt_crc, cptptr, 1);
  103.     if(*cptptr & 0x80) {
  104.         cptptr[F_FOLDER] = 1;
  105.         *cptptr &= 0x3f;
  106.     } else {
  107.         cptptr[F_FOLDER] = 0;
  108.     }
  109.     if(fread(cptptr + 1, 1, *cptptr, infp) != *cptptr) {
  110.         (void)fprintf(stderr, "Can't read file header #%d\n", i+1);
  111. #ifdef SCAN
  112.         do_error("macunpack: Can't read file header");
  113. #endif /* SCAN */
  114.         exit(1);
  115.     }
  116.     cpt_crc = (*updcrc)(cpt_crc, cptptr + 1, *cptptr);
  117.     if(cptptr[F_FOLDER]) {
  118.         if(fread(cptptr + F_FOLDERSIZE, 1, 2, infp) != 2) {
  119.         (void)fprintf(stderr, "Can't read file header #%d\n", i+1);
  120. #ifdef SCAN
  121.         do_error("macunpack: Can't read file header");
  122. #endif /* SCAN */
  123.         exit(1);
  124.         }
  125.         cpt_crc = (*updcrc)(cpt_crc, cptptr + F_FOLDERSIZE, 2);
  126.     } else {
  127.         if(fread(cptptr + F_VOLUME, 1, FILEHDRSIZE - F_VOLUME, infp) !=
  128.         FILEHDRSIZE - F_VOLUME) {
  129.         (void)fprintf(stderr, "Can't read file header #%d\n", i+1);
  130. #ifdef SCAN
  131.         do_error("macunpack: Can't read file header");
  132. #endif /* SCAN */
  133.         exit(1);
  134.         }
  135.         cpt_crc = (*updcrc)(cpt_crc, cptptr + F_VOLUME,
  136.                 FILEHDRSIZE - F_VOLUME);
  137.     }
  138.     cptptr += FILEHDRSIZE;
  139.     }
  140.     if(cpt_crc != cpthdr.hdrcrc) {
  141.     (void)fprintf(stderr, "Header CRC mismatch: got 0x%08x, need 0x%08x\n",
  142.         (int)cpthdr.hdrcrc, (int)cpt_crc);
  143. #ifdef SCAN
  144.     do_error("macunpack: Header CRC mismatch");
  145. #endif /* SCAN */
  146.     exit(1);
  147.     }
  148.  
  149.     cptptr = cptindex;
  150.     for(i = 0; i < cpthdr.entries; i++) {
  151.     if(cpt_filehdr(&filehdr, cptptr) == -1) {
  152.         (void)fprintf(stderr, "Can't read file header #%d\n", i+1);
  153. #ifdef SCAN
  154.         do_error("macunpack: Can't read file header");
  155. #endif /* SCAN */
  156.         exit(1);
  157.     }
  158.     if(filehdr.folder) {
  159.         cpt_folder(text, filehdr, cptptr);
  160.         i += filehdr.foldersize;
  161.         cptptr += filehdr.foldersize * FILEHDRSIZE;
  162.     } else {
  163.         cpt_uncompact(filehdr);
  164.     }
  165.     cptptr += FILEHDRSIZE;
  166.     }
  167.     (void)free(cptindex);
  168. }
  169.  
  170. static int readcpthdr(s)
  171. struct cptHdr *s;
  172. {
  173.     char temp[CHDRSIZE];
  174.  
  175.     if(fread(temp, 1, CPTHDRSIZE, infp) != CPTHDRSIZE) {
  176.     return 0;
  177.     }
  178.  
  179.     if(temp[C_SIGNATURE] != 1) {
  180.     (void)fprintf(stderr, "Not a Compactor file\n");
  181.     return 0;
  182.     }
  183.  
  184.     cpt_datasize = get4(temp + C_IOFFSET);
  185.     s->offset = cpt_datasize;
  186.     if(cpt_datasize > cpt_datamax) {
  187.     if(cpt_datamax == 0) {
  188.         cpt_data = (unsigned char *)malloc((unsigned)cpt_datasize);
  189.     } else {
  190.         cpt_data = (unsigned char *)realloc((char *)cpt_data,
  191.                         (unsigned)cpt_datasize);
  192.     }
  193.     cpt_datamax = cpt_datasize;
  194.     }
  195.     if(cpt_data == NULL) {
  196.     (void)fprintf(stderr, "Insufficient memory, aborting\n");
  197.     exit(1);
  198.     }
  199.  
  200.     if(fread((char *)(cpt_data + CPTHDRSIZE), 1,
  201.     (int)s->offset - CPTHDRSIZE, infp) != s->offset - CPTHDRSIZE) {
  202.     return 0;
  203.     }
  204.  
  205.     if(fread(temp + CPTHDRSIZE, 1, CPTHDR2SIZE, infp) != CPTHDR2SIZE) {
  206.     return 0;
  207.     }
  208.  
  209.     cpt_crc = (*updcrc)(cpt_crc, temp + CPTHDRSIZE + C_ENTRIES, 3);
  210.     s->hdrcrc = get4(temp + CPTHDRSIZE + C_HDRCRC);
  211.     s->entries = get2(temp + CPTHDRSIZE + C_ENTRIES);
  212.     s->commentsize = temp[CPTHDRSIZE + C_COMMENT];
  213.  
  214.     return 1;
  215. }
  216.  
  217. static int cpt_filehdr(f, hdr)
  218. struct fileHdr *f;
  219. char *hdr;
  220. {
  221.     register int i;
  222.     int n;
  223.     char ftype[5], fauth[5];
  224.  
  225.     for(i = 0; i < INFOBYTES; i++) {
  226.     info[i] = '\0';
  227.     }
  228.  
  229.     n = hdr[F_FNAME] & BYTEMASK;
  230.     if(n > F_NAMELEN) {
  231.     n = F_NAMELEN;
  232.     }
  233.     info[I_NAMEOFF] = n;
  234.     copy(info + I_NAMEOFF + 1, hdr + F_FNAME + 1, n);
  235.     transname(hdr + F_FNAME + 1, text, n);
  236.  
  237.     f->folder = hdr[F_FOLDER];
  238.     if(f->folder) {
  239.     f->foldersize = get2(hdr + F_FOLDERSIZE);
  240.     } else {
  241.     f->cptFlag = get2(hdr + F_CPTFLAG);
  242.     f->rsrcLength = get4(hdr + F_RSRCLENGTH);
  243.     f->dataLength = get4(hdr + F_DATALENGTH);
  244.     f->compRLength = get4(hdr + F_COMPRLENGTH);
  245.     f->compDLength = get4(hdr + F_COMPDLENGTH);
  246.     f->fileCRC = get4(hdr + F_FILECRC);
  247.     f->FndrFlags = get2(hdr + F_FNDRFLAGS);
  248.     f->filepos = get4(hdr + F_FILEPOS);
  249.     f->volume = hdr[F_VOLUME];
  250.     }
  251.  
  252.     write_it = 1;
  253.     if(list) {
  254.     do_indent(indent);
  255.     if(f->folder) {
  256.         (void)fprintf(stderr, "folder=\"%s\"", text);
  257.     } else {
  258.         transname(hdr + F_FTYPE, ftype, 4);
  259.         transname(hdr + F_CREATOR, fauth, 4);
  260.         (void)fprintf(stderr,
  261.             "name=\"%s\", type=%4.4s, author=%4.4s, data=%ld, rsrc=%ld",
  262.             text, ftype, fauth,
  263.             (long)f->dataLength, (long)f->rsrcLength);
  264.     }
  265.     if(info_only) {
  266.         write_it = 0;
  267.     }
  268.     if(query) {
  269.         write_it = do_query();
  270.     } else {
  271.         (void)fputc('\n', stderr);
  272.     }
  273.     }
  274.  
  275.  
  276.     if(write_it) {
  277.     define_name(text);
  278.  
  279.     if(!f->folder) {
  280.         copy(info + I_TYPEOFF, hdr + F_FTYPE, 4);
  281.         copy(info + I_AUTHOFF, hdr + F_CREATOR, 4);
  282.         copy(info + I_FLAGOFF, hdr + F_FNDRFLAGS, 2);
  283.         copy(info + I_DLENOFF, hdr + F_DATALENGTH, 4);
  284.         copy(info + I_RLENOFF, hdr + F_RSRCLENGTH, 4);
  285.         copy(info + I_CTIMOFF, hdr + F_CREATIONDATE, 4);
  286.         copy(info + I_MTIMOFF, hdr + F_MODDATE, 4);
  287.     }
  288.     }
  289.     return 1;
  290. }
  291.  
  292. static void cpt_folder(name, fileh, cptptr)
  293. char *name;
  294. struct fileHdr fileh;
  295. char *cptptr;
  296. {
  297.     int i, nfiles;
  298.     char loc_name[64];
  299.     struct fileHdr filehdr;
  300.  
  301.     for(i = 0; i < 64; i++) {
  302.     loc_name[i] = name[i];
  303.     }
  304.     if(write_it || info_only) {
  305.     cptptr += FILEHDRSIZE;
  306.     nfiles = fileh.foldersize;
  307.     if(write_it) {
  308.         do_mkdir(text, info);
  309.     }
  310.     indent++;
  311.     for(i = 0; i < nfiles; i++) {
  312.         if(cpt_filehdr(&filehdr, cptptr) == -1) {
  313.         (void)fprintf(stderr, "Can't read file header #%d\n", i+1);
  314. #ifdef SCAN
  315.         do_error("macunpack: Can't read file header");
  316. #endif /* SCAN */
  317.         exit(1);
  318.         }
  319.         if(filehdr.folder) {
  320.         cpt_folder(text, filehdr, cptptr);
  321.         i += filehdr.foldersize;
  322.         cptptr += filehdr.foldersize * FILEHDRSIZE;
  323.         } else {
  324.         cpt_uncompact(filehdr);
  325.         }
  326.         cptptr += FILEHDRSIZE;
  327.     }
  328.     if(write_it) {
  329.         enddir();
  330.     }
  331.     indent--;
  332.     if(list) {
  333.         do_indent(indent);
  334.         (void)fprintf(stderr, "leaving folder \"%s\"\n", loc_name);
  335.     }
  336.     }
  337. }
  338.  
  339. static void cpt_uncompact(filehdr)
  340. struct fileHdr filehdr;
  341. {
  342.     if(filehdr.cptFlag & 1) {
  343.     (void)fprintf(stderr, "\tFile is password protected, skipping file\n");
  344. #ifdef SCAN
  345.     do_idf("", PROTECTED);
  346. #endif /* SCAN */
  347.     return;
  348.     }
  349.     if(write_it) {
  350.     start_info(info, filehdr.rsrcLength, filehdr.dataLength);
  351.     cpt_crc = INIT_CRC;
  352.     cpt_char = cpt_data + filehdr.filepos;
  353.     }
  354.     if(verbose) {
  355.     (void)fprintf(stderr, "\tRsrc: ");
  356.     if(filehdr.compRLength == 0) {
  357.         (void)fprintf(stderr, "empty");
  358.     } else if(filehdr.cptFlag & 2) {
  359.         (void)fprintf(stderr, "RLE/LZH compressed (%4.1f%%)",
  360.             100.0 * filehdr.compRLength / filehdr.rsrcLength);
  361.     } else {
  362.         (void)fprintf(stderr, "RLE compressed (%4.1f%%)",
  363.             100.0 * filehdr.compRLength / filehdr.rsrcLength);
  364.     }
  365.     }
  366.     if(write_it) {
  367.     start_rsrc();
  368.     cpt_wrfile(filehdr.compRLength, filehdr.rsrcLength,
  369.            filehdr.cptFlag & 2);
  370.     cpt_char = cpt_data + filehdr.filepos + filehdr.compRLength;
  371.     }
  372.     if(verbose) {
  373.     (void)fprintf(stderr, ", Data: ");
  374.     if(filehdr.compDLength == 0) {
  375.         (void)fprintf(stderr, "empty");
  376.     } else if(filehdr.cptFlag & 4) {
  377.         (void)fprintf(stderr, "RLE/LZH compressed (%4.1f%%)",
  378.             100.0 * filehdr.compDLength / filehdr.dataLength);
  379.     } else {
  380.         (void)fprintf(stderr, "RLE compressed (%4.1f%%)",
  381.             100.0 * filehdr.compDLength / filehdr.dataLength);
  382.     }
  383.     }
  384.     if(write_it) {
  385.     start_data();
  386.     cpt_wrfile(filehdr.compDLength, filehdr.dataLength,
  387.            filehdr.cptFlag & 4);
  388.     if(filehdr.fileCRC != cpt_crc) {
  389.         (void)fprintf(stderr,
  390.         "CRC error on file: need 0x%08lx, got 0x%08lx\n",
  391.         (long)filehdr.fileCRC, (long)cpt_crc);
  392. #ifdef SCAN
  393.         do_error("macunpack: CRC error on file");
  394. #endif /* SCAN */
  395.         exit(1);
  396.     }
  397.     end_file();
  398.     }
  399.     if(verbose) {
  400.     (void)fprintf(stderr, ".\n");
  401.     }
  402. }
  403.  
  404. static void cpt_wrfile(ibytes, obytes, type)
  405. unsigned long ibytes, obytes;
  406. unsigned short type;
  407. {
  408.     if(ibytes == 0) {
  409.     return;
  410.     }
  411.     cpt_outstat = NONESEEN;
  412.     cpt_inlength = ibytes;
  413.     cpt_outlength = obytes;
  414.     cpt_LZptr = 0;
  415.     cpt_blocksize = 0x1fff0;
  416.     if(type == 0) {
  417.     cpt_rle();
  418.     } else {
  419.     cpt_rle_lzh();
  420.     }
  421.     cpt_crc = (*updcrc)(cpt_crc, out_buffer, obytes);
  422. }
  423.  
  424. void cpt_wrfile1(in_char, ibytes, obytes, type, blocksize)
  425. unsigned char *in_char;
  426. unsigned long ibytes, obytes, blocksize;
  427. int type;
  428. {
  429.     cpt_char = in_char;
  430.     if(ibytes == 0) {
  431.     return;
  432.     }
  433.     cpt_outstat = NONESEEN;
  434.     cpt_inlength = ibytes;
  435.     cpt_outlength = obytes;
  436.     cpt_LZptr = 0;
  437.     cpt_blocksize = blocksize;
  438.     if(type == 0) {
  439.     cpt_rle();
  440.     } else {
  441.     cpt_rle_lzh();
  442.     }
  443. }
  444.  
  445. static void cpt_outch(ch)
  446. unsigned char ch;
  447. {
  448.     cpt_LZbuff[cpt_LZptr++ & (CIRCSIZE - 1)] = ch;
  449.     switch(cpt_outstat) {
  450.     case NONESEEN:
  451.     if(ch == ESC1 && cpt_outlength != 1) {
  452.         cpt_outstat = ESC1SEEN;
  453.     } else {
  454.         cpt_savechar = ch;
  455.         *out_ptr++ = ch;
  456.         cpt_outlength--;
  457.     }
  458.     break;
  459.     case ESC1SEEN:
  460.     if(ch == ESC2) {
  461.         cpt_outstat = ESC2SEEN;
  462.     } else {
  463.         cpt_savechar = ESC1;
  464.         *out_ptr++ = ESC1;
  465.         cpt_outlength--;
  466.         if(cpt_outlength == 0) {
  467.         return;
  468.         }
  469.         if(ch == ESC1 && cpt_outlength != 1) {
  470.         return;
  471.         }
  472.         cpt_outstat = NONESEEN;
  473.         cpt_savechar = ch;
  474.         *out_ptr++ = ch;
  475.         cpt_outlength--;
  476.     }
  477.     break;
  478.     case ESC2SEEN:
  479.     cpt_outstat = NONESEEN;
  480.     if(ch != 0) {
  481.         while(--ch != 0) {
  482.         *out_ptr++ = cpt_savechar;
  483.         cpt_outlength--;
  484.         if(cpt_outlength == 0) {
  485.             return;
  486.         }
  487.         }
  488.     } else {
  489.         *out_ptr++ = ESC1;
  490.         cpt_outlength--;
  491.         if(cpt_outlength == 0) {
  492.         return;
  493.         }
  494.         cpt_savechar = ESC2;
  495.         *out_ptr++ = cpt_savechar;
  496.         cpt_outlength--;
  497.     }
  498.     }
  499. }
  500.  
  501. /*---------------------------------------------------------------------------*/
  502. /*    Run length encoding                             */
  503. /*---------------------------------------------------------------------------*/
  504. static void cpt_rle()
  505. {
  506.     while(cpt_inlength-- > 0) {
  507.     cpt_outch(*cpt_char++);
  508.     }
  509. }
  510.  
  511. /*---------------------------------------------------------------------------*/
  512. /*    Run length encoding plus LZ compression plus Huffman encoding         */
  513. /*---------------------------------------------------------------------------*/
  514. static void cpt_rle_lzh()
  515. {
  516.     int block_count;
  517.     unsigned int bptr;
  518.     int Huffchar, LZlength, LZoffs;
  519.  
  520.     get_bit = cpt_getbit;
  521.     cpt_LZbuff[CIRCSIZE - 3] = 0;
  522.     cpt_LZbuff[CIRCSIZE - 2] = 0;
  523.     cpt_LZbuff[CIRCSIZE - 1] = 0;
  524.     cpt_LZptr = 0;
  525.     while(cpt_outlength != 0) {
  526.     cpt_readHuff(256, cpt_Hufftree);
  527.     cpt_readHuff(64, cpt_LZlength);
  528.     cpt_readHuff(128, cpt_LZoffs);
  529.     block_count = 0;
  530.     cpt_newbits = (*cpt_char++ << 8);
  531.     cpt_newbits = cpt_newbits | *cpt_char++;
  532.     cpt_newbits = cpt_newbits << 16;
  533.     cpt_bitsavail = 16;
  534.     while(block_count < cpt_blocksize && cpt_outlength != 0) {
  535.         if(cpt_getbit()) {
  536.         Huffchar = gethuffbyte(cpt_Hufftree);
  537.         cpt_outch((unsigned char)Huffchar);
  538.         block_count += 2;
  539.         } else {
  540.         LZlength = gethuffbyte(cpt_LZlength);
  541.         LZoffs = gethuffbyte(cpt_LZoffs);
  542.         LZoffs = (LZoffs << 6) | cpt_get6bits();
  543.         bptr = cpt_LZptr - LZoffs;
  544.         while(LZlength-- > 0) {
  545.             cpt_outch(cpt_LZbuff[bptr++ & (CIRCSIZE - 1)]);
  546.         }
  547.         block_count += 3;
  548.         }
  549.     }
  550.     }
  551. }
  552.  
  553. /* Based on unimplod from unzip; difference are noted below. */
  554. typedef struct sf_entry {
  555.     int Value;
  556.     int BitLength;
  557. } sf_entry;
  558.  
  559. /* See routine LoadTree.  The parameter tree (actually an array and
  560.    two integers) are only used locally in this version and hence locally
  561.    declared.  The parameter nodes has been renamed Hufftree.... */
  562. static void cpt_readHuff(size, Hufftree)
  563. int size;
  564. struct node *Hufftree;
  565. {
  566.     sf_entry tree_entry[256 + SLACK]; /* maximal number of elements */
  567.     int tree_entries;
  568.     int tree_MaxLength; /* finishes local declaration of tree */
  569.  
  570.     int treeBytes, i, len;  /* declarations from ReadLengths */
  571.  
  572.     /* declarations from SortLengths */
  573.     sf_entry *ejm1;
  574.     int j;
  575.     sf_entry *entry;
  576. /*  int i already above */
  577.     sf_entry tmp;
  578.     int entries;
  579.     unsigned a, b;
  580.  
  581.     /* declarations from GenerateTrees */
  582.     int codelen, lvlstart, next, parents;
  583. /*  int i, j already above */
  584.  
  585.     /* for Compactor */
  586.     int tree_count[32];
  587.     /* end declarations */
  588.  
  589.     /* next paraphrased from ReadLengths with adaption for Compactor. */
  590.     treeBytes = *cpt_char++;
  591.     if(size < treeBytes * 2) { /* too many entries, something is wrong! */
  592.     (void)fprintf(stderr, "Bytes is: %d, expected: %d\n", treeBytes,
  593.         size / 2);
  594. #ifdef SCAN
  595.     do_error("macunpack: error in coding tree");
  596. #endif /* SCAN */
  597.     exit(1);
  598.     }
  599.     for(i = 0; i < 32; i++) {
  600.     tree_count[i] = 0;
  601.     }
  602.     i = 0;
  603.     tree_MaxLength = 0;
  604.     tree_entries = 0;
  605.     while(treeBytes-- > 0) { /* adaption for Compactor */
  606.     len = (*cpt_char) >> 4;
  607.     if(len != 0) { /* only if length unequal zero */
  608.         if(len > tree_MaxLength) {
  609.         tree_MaxLength = len;
  610.         }
  611.         tree_count[len]++;
  612.         tree_entry[tree_entries].Value = i;
  613.         tree_entry[tree_entries++].BitLength = len;
  614.     }
  615.     i++;
  616.     len = *cpt_char++ & NIBBLEMASK;
  617.     if(len != 0) { /* only if length unequal zero */
  618.         if(len > tree_MaxLength) {
  619.         tree_MaxLength = len;
  620.         }
  621.         tree_count[len]++;
  622.         tree_entry[tree_entries].Value = i;
  623.         tree_entry[tree_entries++].BitLength = len;
  624.     }
  625.     i++;
  626.     }
  627.  
  628.     /* Compactor allows unused trailing codes in its Huffman tree! */
  629.     j = 0;
  630.     for(i = 0; i <= tree_MaxLength; i++) {
  631.     j = (j << 1) + tree_count[i];
  632.     }
  633.     j = (1 <<tree_MaxLength) - j;
  634.     /* Insert the unused entries for sorting purposes. */
  635.     for(i = 0; i < j; i++) {
  636.     tree_entry[tree_entries].Value = size;
  637.     tree_entry[tree_entries++].BitLength = tree_MaxLength;
  638.     }
  639.  
  640.     /* adaption from SortLengths */
  641.     entry = &(tree_entry[0]);
  642.     entries = tree_entries;
  643.     for(i = 0; ++i < entries;) {
  644.     tmp = entry[i];
  645.     b = tmp.BitLength;
  646.     j = i;
  647.     while((j > 0) && ((a = (ejm1 = &(entry[j - 1]))->BitLength) >= b)) {
  648.         if((a == b) && (ejm1->Value <= tmp.Value)) {
  649.         break;
  650.         }
  651.         *(ejm1 + 1) = *ejm1;
  652.         --j;
  653.     }
  654.     entry[j] = tmp;
  655.     }
  656.  
  657.     /* Adapted from GenerateTrees */
  658.     i = tree_entries - 1;
  659.     /* starting at the upper end (and reversing loop) because of Compactor */
  660.     lvlstart = next = size * 2 + SLACK - 1;
  661.     /* slight adaption because of different node format used */
  662.     for(codelen = tree_MaxLength; codelen >= 1; --codelen) {
  663.     while((i >= 0) && (tree_entry[i].BitLength == codelen)) {
  664.         Hufftree[next].byte = tree_entry[i].Value;
  665.         Hufftree[next].flag = 1;
  666.         next--;
  667.         i--;
  668.     }
  669.     parents = next;
  670.     if(codelen > 1) {
  671.         /* reversed loop */
  672.         for(j = lvlstart; j > parents + 1; j-= 2) {
  673.         Hufftree[next].one = &(Hufftree[j]);
  674.         Hufftree[next].zero = &(Hufftree[j - 1]);
  675.         Hufftree[next].flag = 0;
  676.         next--;
  677.         }
  678.     }
  679.     lvlstart = parents;
  680.     }
  681.     Hufftree[0].one = &(Hufftree[next + 2]);
  682.     Hufftree[0].zero = &(Hufftree[next + 1]);
  683.     Hufftree[0].flag = 0;
  684. }
  685.  
  686. static int cpt_get6bits()
  687. {
  688. int b = 0, cn;
  689.  
  690.     b = (cpt_newbits >> 26) & 0x3f;
  691.     cpt_bitsavail -= 6;
  692.     cpt_newbits <<= 6;
  693.     if(cpt_bitsavail < 16) {
  694.     cn = (*cpt_char++ << 8);
  695.     cn |= *cpt_char++;
  696.     cpt_newbits |= (cn << (16 - cpt_bitsavail));
  697.     cpt_bitsavail += 16;
  698.     }
  699.     return b;
  700. }
  701.  
  702. static int cpt_getbit()
  703. {
  704. int b;
  705.  
  706.     b = (cpt_newbits >> 31) & 1;
  707.     cpt_bitsavail--;
  708.     if(cpt_bitsavail < 16) {
  709.     cpt_newbits |= (*cpt_char++ << 8);
  710.     cpt_newbits |= *cpt_char++;
  711.     cpt_bitsavail += 16;
  712.     }
  713.     cpt_newbits <<= 1;
  714.     return b;
  715. }
  716. #else /* CPT */
  717. int cpt; /* keep lint and some compilers happy */
  718. #endif /* CPT */
  719.  
  720.