]>
git.saurik.com Git - apple/boot.git/blob - gen/rcz/rcz.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
27 #import <mach/mach_error.h>
28 #import <mach/mach_traps.h>
30 #import "rcz_compress_mem.h"
31 #import "rcz_decompress_mem.h"
36 fprintf(stderr
, "usage: rcz [-v] [-c | -d] [-o <ofile>] infile\n");
37 fprintf(stderr
, " -v: verbose mode\n");
38 fprintf(stderr
, " -c: compress\n");
39 fprintf(stderr
, " -d: decompress\n");
40 fprintf(stderr
, " use the special file name '-' to refer to stdin\n");
44 int main(int argc
, char *argv
[])
46 char *infile
= NULL
, *outfile
= NULL
;
49 FILE *inf
, *outf
= NULL
;
50 unsigned char *inbuf
, *outbuf
;
51 unsigned long length
, total
;
54 while ((++argv
)[0] != NULL
) {
55 if (*argv
[0] == '-') {
56 switch(*(argv
[0]+1)) {
95 if (strcmp(infile
, "-") == 0) {
98 inf
= fopen(infile
, "r");
105 if (strcmp(outfile
, "-") == 0) {
108 outf
= fopen(outfile
, "w+");
110 perror("open outfile");
116 fseek(inf
,0,2); length
= ftell(inf
); rewind(inf
);
117 if ((ret
= map_fd(fileno(inf
), 0, (vm_address_t
*)&inbuf
, TRUE
, length
)) != KERN_SUCCESS
) {
118 mach_error("map_fd", ret
);
121 outbuf
= (unsigned char *)malloc(length
+ (length
+ 15)/16);
122 total
= rcz_compress_memory(inbuf
, length
, outbuf
);
123 fwrite(outbuf
, 1, total
, outf
);
125 fprintf(stderr
, "%ld %ld\nCompression ratio: %f\n", length
, total
, total
/(1.0*length
));
127 vm_deallocate(mach_task_self(), (vm_address_t
)inbuf
, length
);
132 if (strcmp(infile
, "-") == 0) {
135 inf
= fopen(infile
, "r");
142 if (strcmp(outfile
, "-") == 0) {
145 outf
= fopen(outfile
, "w+");
147 perror("open outfile");
152 fseek(inf
,0,2); length
= ftell(inf
); rewind(inf
);
153 if ((ret
= map_fd(fileno(inf
), 0, (vm_address_t
*)&inbuf
, TRUE
, length
)) != KERN_SUCCESS
) {
154 mach_error("map_fd", ret
);
158 ptr
+= 4; /* Skip over version number, which is checked
159 later in ex_decompress(). */
160 /* Next, figure out malloc size using the next four bytes of the
164 total
= (total
<<8) | (*ptr
++);
165 total
= (total
<<8) | (*ptr
++);
166 total
= (total
<<8) | (*ptr
++);
168 /* Now total is the exact byte count of the final, decompressed stream. */
169 outbuf
= (unsigned char *)malloc(total
);
170 total
= rcz_decompress_memory(ptr
, outbuf
);
171 fwrite(outbuf
, 1, total
, outf
);
173 fprintf(stderr
, "%ld %ld\nCompression ratio: %f\n", length
, total
, total
/(1.0*length
));
175 vm_deallocate(mach_task_self(), (vm_address_t
)inbuf
, length
);