]>
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@
26 #import <mach/mach_error.h>
27 #import <mach/mach_traps.h>
29 #import "rcz_compress_mem.h"
30 #import "rcz_decompress_mem.h"
35 fprintf(stderr
, "usage: rcz [-v] [-c | -d] [-o <ofile>] infile\n");
36 fprintf(stderr
, " -v: verbose mode\n");
37 fprintf(stderr
, " -c: compress\n");
38 fprintf(stderr
, " -d: decompress\n");
39 fprintf(stderr
, " use the special file name '-' to refer to stdin\n");
43 main(int argc
, char *argv
[])
45 char *infile
= NULL
, *outfile
= NULL
;
49 unsigned char *inbuf
, *outbuf
;
50 unsigned long length
, total
;
53 while ((++argv
)[0] != NULL
) {
54 if (*argv
[0] == '-') {
55 switch(*(argv
[0]+1)) {
94 if (strcmp(infile
, "-") == 0) {
97 inf
= fopen(infile
, "r");
104 if (strcmp(outfile
, "-") == 0) {
107 outf
= fopen(outfile
, "w+");
109 perror("open outfile");
115 fseek(inf
,0,2); length
= ftell(inf
); rewind(inf
);
116 if ((ret
= map_fd(fileno(inf
), 0, &inbuf
, TRUE
, length
)) != KERN_SUCCESS
) {
117 mach_error("map_fd", ret
);
120 outbuf
= (unsigned char *)malloc(length
+ (length
+ 15)/16);
121 total
= rcz_compress_memory(inbuf
, length
, outbuf
);
122 fwrite(outbuf
, 1, total
, outf
);
124 fprintf(stderr
, "%ld %ld\nCompression ratio: %f\n", length
, total
, total
/(1.0*length
));
126 vm_deallocate(mach_task_self(), inbuf
, length
);
131 if (strcmp(infile
, "-") == 0) {
134 inf
= fopen(infile
, "r");
141 if (strcmp(outfile
, "-") == 0) {
144 outf
= fopen(outfile
, "w+");
146 perror("open outfile");
151 fseek(inf
,0,2); length
= ftell(inf
); rewind(inf
);
152 if ((ret
= map_fd(fileno(inf
), 0, &inbuf
, TRUE
, length
)) != KERN_SUCCESS
) {
153 mach_error("map_fd", ret
);
157 ptr
+= 4; /* Skip over version number, which is checked
158 later in ex_decompress(). */
159 /* Next, figure out malloc size using the next four bytes of the
163 total
= (total
<<8) | (*ptr
++);
164 total
= (total
<<8) | (*ptr
++);
165 total
= (total
<<8) | (*ptr
++);
167 /* Now total is the exact byte count of the final, decompressed stream. */
168 outbuf
= (unsigned char *)malloc(total
);
169 total
= rcz_decompress_memory(ptr
, outbuf
);
170 fwrite(outbuf
, 1, total
, outf
);
172 fprintf(stderr
, "%ld %ld\nCompression ratio: %f\n", length
, total
, total
/(1.0*length
));
174 vm_deallocate(mach_task_self(), inbuf
, length
);