|
|
1.1 root 1: #ifndef lint
2: static char sccsid[] = "@(#)uncompact.c 4.4 (Berkeley) 8/11/83";
3: #endif
4:
5: /*
6: * Uncompact adaptive Huffman code input to output
7: *
8: * On - line algorithm
9: *
10: * Input file does not contain decoding tree
11: *
12: * Written by Colin L. Mc Master (UCB) February 14, 1979
13: */
14:
15: #include "compact.h"
16:
17:
18: main (argc, argv)
19: short argc;
20: char *argv [ ];
21: {
22: register short i;
23: register struct node *p;
24: register short j;
25: register int m;
26: union cio c, d;
27: char b;
28: longint ic, n;
29: char fname [LNAME], *cp;
30:
31: dir [513] . next = NULL;
32: for (head = dir + (j = 513); j--; ) {
33: dirp = head--;
34: head -> next = dirp;
35: }
36: bottom = dirp -> pt = dict;
37: dict [0] . top [0] = dict [0] . top [1] = dirp;
38: dirq = dirp -> next;
39: in [EF] . flags = FBIT | SEEN;
40:
41: for (i = 1; ; i++) {
42: ic = oc = 0;
43: (bottom -> top [1]) -> next = flist;
44: bottom -> top [1] = dirp;
45: flist = dirq;
46: if (i >= argc) {
47: uncfp = stdout;
48: cfp = stdin;
49: }
50: else {
51: m = -1;
52: cp = fname;
53: for (j = 0; j < (LNAME - 3) && (*cp = argv [i][j]); j++)
54: if (*cp++ == '/') m = j;
55: if (cp [-1] == 'C' && cp [-2] == '.') cp [-2] = 0;
56: else {
57: fprintf (stderr, "%s: File name must end with \".C\"\n", argv [i]);
58: if (i == argc - 1) break;
59: continue;
60: }
61: if (j >= (LNAME - 3) || (j - m) > MAXNAMLEN) {
62: fprintf (stderr, "File name too long -- %s\n", argv [i]);
63: if (i == argc - 1) break;
64: continue;
65: }
66: if ((cfp = fopen (argv [i], "r")) == NULL) {
67: perror (argv [i]);
68: if (i == argc - 1) break;
69: continue;
70: }
71: if ((uncfp = fopen (fname, "w")) == NULL) {
72: perror (fname);
73: fclose (cfp);
74: if (i == argc - 1) break;
75: continue;
76: }
77: fstat (fileno (cfp), &status);
78: chmod (fname, status.st_mode & 07777);
79: }
80:
81: if ((c . integ = getc (cfp)) != EOF) {
82: if ((d . integ = getc (cfp)) != EOF) {
83: c . chars . hib = d . integ & 0377;
84: c . integ &= 0177777;
85: if (c . integ != COMPACTED) goto notcompact;
86: if ((c . integ = getc (cfp)) != EOF) {
87: putc (c . chars . lob, uncfp);
88: ic = 3;
89:
90: in [NC] . fp = in [EF] . fp = dict [0] . sp [0] . p = bottom = dict + 1;
91: bottom -> count [0] = bottom -> count [1] = dict [0] . count [1] = 1;
92: dirp -> next = dict [0] . top [1] = bottom -> top [0] = bottom -> top [1] = dirq = NEW;
93: dirq -> next = NULL;
94: dict [0] . fath . fp = NULL;
95: dirq -> pt = bottom -> fath . fp = in [c . integ] . fp = dict;
96: in [c . integ] . flags = (FBIT | SEEN);
97: in [NC] . flags = SEEN;
98: dict [0] . fath . flags = RLEAF;
99: bottom -> fath . flags = (LLEAF | RLEAF);
100: dict [0] . count [0] = 2;
101:
102: dict [0] . sp [1] . ch = c . integ;
103: bottom -> sp [0] . ch = NC;
104: bottom -> sp [1] . ch = EF;
105:
106: p = dict;
107: while ((c . integ = getc (cfp)) != EOF) {
108: ic++;
109: for (m = 0200; m; ) {
110: b = (m & c . integ ? 1 : 0);
111: m >>= 1;
112: if (p -> fath . flags & (b ? RLEAF : LLEAF)) {
113: d . integ = p -> sp [b] . ch;
114: if (d . integ == EF) break;
115: if (d . integ == NC) {
116: uptree (NC);
117: d . integ = 0;
118: for (j = 8; j--; m >>= 1) {
119: if (m == 0) {
120: c . integ = getc (cfp);
121: ic++;
122: m = 0200;
123: }
124: d . integ <<= 1;
125: if (m & c . integ) d . integ++;
126: }
127: insert (d . integ);
128: }
129: uptree (d . integ);
130: putc (d . chars . lob, uncfp);
131: oc++;
132: p = dict;
133: }
134: else p = p -> sp [b] . p;
135: }
136: }
137: }
138: }
139: else goto notcompact;
140: }
141: else {
142: notcompact : if (i < argc) {
143: fprintf (stderr, "%s: ", argv [i]);
144: unlink (fname);
145: }
146: if (c . integ == PACKED) fprintf (stderr, "File is packed. Use unpack.\n");
147: else fprintf (stderr, "Not a compacted file.\n");
148: if (i >= argc) break;
149: goto closeboth;
150: }
151:
152: if (ferror (uncfp) || ferror (cfp))
153: if (i < argc) {
154: if (ferror (uncfp))
155: perror (fname);
156: else
157: perror (argv [i]);
158: fprintf (stderr, "Unable to uncompact %s\n", argv [i]);
159: unlink (fname);
160: goto closeboth;
161: }
162: if (i >= argc) break;
163: fprintf (stderr, "%s uncompacted to %s\n", argv [i], fname);
164: unlink (argv [i]);
165: closeboth : fclose (cfp);
166: closein : fclose (uncfp);
167: if (i == argc - 1) break;
168: for (j = 256; j--; ) in [j] . flags = 0;
169: continue;
170: fail : fprintf (stderr, "Unsuccessful uncompact of standard input to standard output.\n");
171: break;
172: }
173: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.