]> git.saurik.com Git - apple/boot.git/blob - i386/rcz/rcz_compress_file.c
boot-132.tar.gz
[apple/boot.git] / i386 / rcz / rcz_compress_file.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 Library: compressor for executable files.
26
27 R. E. Crandall, July 1995
28
29 Copyright 1995 NeXT Computer, Inc.
30 All rights reserved.
31
32 */
33
34 #import "rcz_common.h"
35
36 static unsigned short que[QLEN];
37
38 unsigned int
39 rcz_compress_file(
40 int in_fd,
41 unsigned int inbytes,
42 unsigned char *out
43 )
44 /* Returns actual number of bytes emitted as compressed stream 'out.' */
45 {
46 unsigned int c, ct, j, jmatch, jabove, match;
47 unsigned int data[32], version;
48 unsigned int word, token, tokenct, total;
49 unsigned char *outorigin = out;
50
51 /* First, put version number into stream. */
52 *out++ = (METHOD_17_JUL_95>>24) & 0xff;
53 *out++ = (METHOD_17_JUL_95>>16) & 0xff;
54 *out++ = (METHOD_17_JUL_95>>8) & 0xff;
55 *out++ = (METHOD_17_JUL_95) & 0xff;
56 /* Next, put the initial size into stream. */
57 *out++ = (inbytes>>24) & 0xff;
58 *out++ = (inbytes>>16) & 0xff;
59 *out++ = (inbytes>>8) & 0xff;
60 *out++ = (inbytes) & 0xff;
61
62 for(ct=0; ct < QLEN; ct++) que[ct] = ct;
63 word = token = tokenct = 0;
64 for(ct = 0; ct < inbytes; ct++) {
65 /* Next, update bucket-brigade register. */
66 word = (word << 8) | (*in++);
67 if(ct % 2 == 1) {
68 word &= 0xffff;
69 match = 0;
70 for(j=0; j < QLEN; j++) {
71 if(que[j] == word) {
72 match = 1;
73 jmatch = j;
74 break;
75 }
76 }
77 token = (token<<1) | match;
78 if(match) { /* 16-bit symbol is in queue. */
79 c = que[jmatch];
80 jabove = (F1 * jmatch) >> 4;
81 for(j = jmatch; j > jabove; j--) {
82 que[j] = que[j-1];
83 }
84 que[jabove] = c;
85 data[tokenct++] = jmatch;
86 } else { /* 16-bit symbol is not in queue. */
87 for(j=QLEN-1; j > ABOVE; j--) {
88 que[j] = que[j-1];
89 }
90 que[ABOVE] = word;
91 data[tokenct++] = word;
92 }
93 if(tokenct == 32) { /* Unload tokens and data. */
94 *out++ = (token>>24) & 0xff;
95 *out++ = (token>>16) & 0xff;
96 *out++ = (token>>8) & 0xff;
97 *out++ = (token) & 0xff;
98 c = (1<<31);
99 for(j = 0; j < tokenct; j++) {
100 if(token & c) *out++ = data[j] & 0xff;
101 else {
102 *out++ = (data[j] >> 8) & 0xff;
103 *out++ = (data[j]) & 0xff;
104 }
105 c >>= 1;
106 }
107 token = tokenct = 0;
108 }
109 }
110 }
111 if(tokenct > 0) { /* Flush final token and data. */
112 token <<= (32-tokenct);
113 *out++ = (token>>24) & 0xff;
114 *out++ = (token>>16) & 0xff;
115 *out++ = (token>>8) & 0xff;
116 *out++ = (token) & 0xff;
117 c = (1<<31);
118 for(j = 0; j < tokenct; j++) {
119 if(token & c) *out++ = data[j] & 0xff;
120 else {
121 *out++ = (data[j] >> 8) & 0xff;
122 *out++ = (data[j]) & 0xff;
123 }
124 c >>= 1;
125 }
126 }
127 if(ct % 2 == 1) *out++ = (word) & 0xff;
128 return((int)(out-outorigin));
129 }