]> git.saurik.com Git - apple/boot.git/blame - gen/rcz/rcz_decompress_file.c
boot-80.1.tar.gz
[apple/boot.git] / gen / rcz / rcz_decompress_file.c
CommitLineData
14c7c974
A
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
36static unsigned short que[QLEN];
37
38#define REWIND -1
39
40extern int read(int fd, char *buf, int len);
41
42static unsigned char *buf;
43static int buf_count;
44
45#define BUF_SIZE 8192
46
47static void
48alloc_buf(int fd)
49{
50 buf = (unsigned char *)malloc(BUF_SIZE);
51 buf_count = 0;
52}
53
54static unsigned char
55get_byte(int fd)
56{
57 static unsigned char *ptr;
58
59 if (buf_count == 0) {
60 buf_count = read(fd, buf, BUF_SIZE);
61 ptr = buf;
62 if (buf_count <= 0)
63 return 0xFF;
64 }
65 buf_count--;
66 return *ptr++;
67}
68
69static void
70free_buf(void)
71{
72 buf_count = 0;
73 free(buf);
74}
75
76
77int
78rcz_file_size(
79 int in_fd
80)
81{
82 unsigned int version, length;
83
84 alloc_buf(in_fd);
85 b_lseek(in_fd, 0, 0);
86 version = get_byte(in_fd);
87 version = (version<<8) | (get_byte(in_fd));
88 version = (version<<8) | (get_byte(in_fd));
89 version = (version<<8) | (get_byte(in_fd));
90
91 if(version != METHOD_17_JUL_95) {
92 return (-1);
93// fprintf(stderr, "Incompatible version.\n");
94// return(0);
95 }
96
97 length = get_byte(in_fd);
98 length = (length<<8) | (get_byte(in_fd));
99 length = (length<<8) | (get_byte(in_fd));
100 length = (length<<8) | (get_byte(in_fd));
101 free_buf();
102 return length;
103}
104
105int
106rcz_decompress_file(
107 int in_fd,
108 unsigned char *out
109)
110/* Returns actual number of bytes emitted as decompressed stream 'out.'
111 Note that the 'in' stream contains this byte count already.
112
113 Returns -1 if the input stream was not in compressed format.
114 */
115{
116 unsigned int c, j, k, jmatch, jabove;
117 int length;
118 unsigned int even_length, word, token, version;
119 unsigned char *outorigin = out;
120
121 length = rcz_file_size(in_fd);
122 if (length < 0)
123 return length;
124
125 alloc_buf(in_fd);
126 b_lseek(in_fd, 8, 0);
127 for(c=0; c < QLEN; c++) que[c] = c;
128 even_length = 2*(length/2);
129 while((int)(out-outorigin) < even_length) {
130 token = get_byte(in_fd);
131 token = (token<<8) | (get_byte(in_fd));
132 token = (token<<8) | (get_byte(in_fd));
133 token = (token<<8) | (get_byte(in_fd));
134 c = 1<<31;
135 for(k = 0; k<32; k++) {
136 if(c & token) {
137 jmatch = get_byte(in_fd);
138 word = que[jmatch];
139 /* Next, dynamically process the queue for match. */
140 jabove = (F1*jmatch) >> 4;
141 for(j = jmatch; j > jabove; j--) {
142 que[j] = que[j-1];
143 }
144 que[jabove] = word;
145 } else {
146 /* Next, dynamically process the queue for unmatch. */
147 word = get_byte(in_fd);
148 word = (word << 8) | (get_byte(in_fd));
149 for(j=QLEN-1; j > ABOVE; j--) {
150 que[j] = que[j-1];
151 }
152 que[ABOVE] = word;
153 }
154 *out++ = (word >> 8) & 0xff;
155 *out++ = (word) & 0xff;
156 if((int)(out-outorigin) >= even_length) break;
157 c >>= 1;
158 }
159 }
160 if(even_length != length) *out++ = get_byte(in_fd);
161 free_buf();
162 return(length);
163}