/* ARC - Archive utility - ARCSQ (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED By: Thom Henderson Description: This file contains the routines used to squeeze a file when placing it in an archive. Language: Computer Innovations Optimizing C86 Programming notes: Most of the routines used for the Huffman squeezing algorithm were lifted from the SQ program by Dick Greenlaw, as adapted to CI-C86 by Robert J. Beilstein. */ #include #define int16size 16 /* 16 bits */ /* stuff for Huffman squeezing */ #define TRUE 1 #define FALSE 0 #define ERROR (-1) #define SPEOF 256 /* special endfile token */ #define NOCHILD -1 /* marks end of path through tree */ #define NUMVALS 257 /* 256 data values plus SPEOF*/ #define NUMNODES (NUMVALS+NUMVALS-1) /* number of nodes */ #define MAXCOUNT (unsigned) 65535 /* biggest unsigned integer */ /* The following array of structures are the nodes of the binary trees. The first NUMVALS nodes become the leaves of the final tree and represent the values of the data bytes being encoded and the special endfile, SPEOF. The remaining nodes become the internal nodes of the final tree. */ struct nd /* shared by unsqueezer */ { unsigned weight; /* number of appearances */ int tdepth; /* length on longest path in tree */ int lchild, rchild; /* indices to next level */ } node[NUMNODES]; /* use large buffer */ static int dctreehd; /* index to head of final tree */ /* This is the encoding table: The bit strings have first bit in low bit. Note that counts were scaled so code fits unsigned integer. */ static int codelen[NUMVALS]; /* number of bits in code */ static unsigned code[NUMVALS]; /* code itself, right adjusted */ static unsigned tcode; /* temporary code value */ static long valcount[NUMVALS]; /* actual count of times seen */ /* Variables used by encoding process */ static int curin; /* value currently being encoded */ static int cbitsrem; /* # of code string bits left */ static unsigned ccode; /* current code right justified */ init_sq() /* prepare for scanning pass */ { int i; /* node index */ /* Initialize all nodes to single element binary trees with zero weight and depth. */ for(i=0; i 16 bits long. */ } while(buildenc(0,dctreehd) == ERROR); /* Initialize encoding variables */ cbitsrem = 0; /* force initial read */ curin = 0; /* anything but endfile */ for(i=0; i (ceil-sum)) ++ovflw; sum += node[i].weight; } divisor = ovflw + 1; /* Ensure no non-zero values are lost */ increased = FALSE; for(i=0; i1) for(i=0; i=0; --i) adjust(list,i,length-1); } /* Make a heap from a heap with a new top */ static adjust(list,top,bottom) int list[], top, bottom; { register int k, temp; k = 2 * top + 1; /* left child of top */ temp = list[top]; /* remember root node of top tree */ if(k<=bottom) { if(k b return true, else return false. Note comparison rules in previous comments. */ static cmptrees(a,b) int a, b; /* root nodes of trees */ { if(node[a].weight > node[b].weight) return TRUE; if(node[a].weight == node[b].weight) if(node[a].tdepth > node[b].tdepth) return TRUE; return FALSE; } /* HUFFMAN ALGORITHM: develops the single element trees into a single binary tree by forming subtrees rooted in interior nodes having weights equal to the sum of weights of all their descendents and having depth counts indicating the depth of their longest paths. When all trees have been formed into a single tree satisfying the heap property (on weight, with depth as a tie breaker) then the binary code assigned to a leaf (value to be encoded) is then the series of left (0) and right (1) paths leading from the root to the leaf. Note that trees are removed from the heaped list by moving the last element over the top element and reheaping the shorter list. */ static bld_tree(list,len) int list[]; int len; { register int freenode; /* next free node in tree */ register struct nd *frnp; /* free node pointer */ int lch, rch; /* temps for left, right children */ int i; /* Initialize index to next available (non-leaf) node. Lower numbered nodes correspond to leaves (data values). */ freenode = NUMVALS; while(len>1) { /* Take from list two btrees with least weight and build an interior node pointing to them. This forms a new tree. */ lch = list[0]; /* This one will be left child */ /* delete top (least) tree from the list of trees */ list[0] = list[--len]; adjust(list,0,len-1); /* Take new top (least) tree. Reuse list slot later */ rch = list[0]; /* This one will be right child */ /* Form new tree from the two least trees using a free node as root. Put the new tree in the list. */ frnp = &node[freenode]; /* address of next free node */ list[0] = freenode++; /* put at top for now */ frnp->lchild = lch; frnp->rchild = rch; frnp->weight = node[lch].weight + node[rch].weight; frnp->tdepth = 1 + maxchar(node[lch].tdepth, node[rch].tdepth); /* reheap list to get least tree at top */ adjust(list,0,len-1); } dctreehd = list[0]; /* head of final tree */ } static maxchar(a,b) { return a>b ? a : b; } static init_enc() { register int i; /* Initialize encoding table */ for(i=0; i> (16-level)); return (level>16) ? ERROR : NULL; } else { if(l!=NOCHILD) { /* Clear path bit and continue deeper */ tcode &= ~(1 << level); if(buildenc(level+1,l)==ERROR) return ERROR; /* pass back bad statuses */ } if(r!=NOCHILD) { /* Set path bit and continue deeper */ tcode |= 1 << level; if(buildenc(level+1,r)==ERROR) return ERROR; /* pass back bad statuses */ } } return NULL; /* it worked if we reach here */ } static put_int(n,f) /* output an integer */ int n; /* integer to output */ FILE *f; /* file to put it to */ { putc_pak(n&0xff,f); /* first the low byte */ putc_pak(n>>8,f); /* then the high byte */ } /* Write out the header of the compressed file */ static long wrt_head(ob) FILE *ob; { register int l,r; int i, k; int numnodes; /* # of nodes in simplified tree */ /* Write out a simplified decoding tree. Only the interior nodes are written. When a child is a leaf index (representing a data value) it is recoded as -(index + 1) to distinguish it from interior indexes which are recoded as positive indexes in the new tree. Note that this tree will be empty for an empty file. */ numnodes = dctreehd=need) /* if current code is big enough */ { if(need==0) return rbyte; rbyte |= ccode << (8-need); /* take what we need */ ccode >>= need; /* and leave the rest */ cbitsrem -= need; return rbyte & 0xff; } /* We need more than current code */ if(cbitsrem>0) { rbyte |= ccode << (8-need); /* take what there is */ need -= cbitsrem; } /* No more bits in current code string */ if(curin==SPEOF) { /* The end of file token has been encoded. If result byte has data return it and do EOF next time. */ cbitsrem = 0; return (need==8) ? EOF : rbyte + 0; } /* Get an input byte */ if((curin=getc_ncr(ib)) == EOF) curin = SPEOF; /* convenient for encoding */ ccode = code[curin]; /* get the new byte's code */ cbitsrem = codelen[curin]; goto loop; } /* This routine is used to perform the actual squeeze operation. It can only be called after the file has been scanned. It returns the true length of the squeezed entry. */ long file_sq(f,t) /* squeeze a file into an archive */ FILE *f; /* file to squeeze */ FILE *t; /* archive to receive file */ { int c; /* one byte of squeezed data */ long size; /* size after squeezing */ size = wrt_head(t); /* write out the decode tree */ while((c=gethuff(f))!=EOF) { putc_pak(c,t); size++; } return size; /* report true size */ }