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