]> git.saurik.com Git - apple/boot.git/blob - gen/rcz/rcz.c
boot-83.2.tar.gz
[apple/boot.git] / gen / rcz / rcz.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 #import <stdio.h>
25 #import <mach/mach.h>
26 #import <mach/mach_error.h>
27 #import <mach/mach_traps.h>
28
29 #import "rcz_compress_mem.h"
30 #import "rcz_decompress_mem.h"
31
32 void
33 usage(void)
34 {
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");
40 exit(1);
41 }
42
43 main(int argc, char *argv[])
44 {
45 char *infile = NULL, *outfile = NULL;
46 int compress = 0;
47 int verbose = 0;
48 FILE *inf, *outf;
49 unsigned char *inbuf, *outbuf;
50 unsigned long length, total;
51 kern_return_t ret;
52
53 while ((++argv)[0] != NULL) {
54 if (*argv[0] == '-') {
55 switch(*(argv[0]+1)) {
56 case 'c':
57 compress = 1;
58 break;
59 case 'd':
60 compress = 0;
61 break;
62 case 'o':
63 if (argv[1]) {
64 outfile = argv[1];
65 argv++;
66 } else {
67 usage();
68 }
69 break;
70 case 'v':
71 verbose = 1;
72 break;
73 case '\0':
74 if (infile)
75 usage();
76 infile = "-";
77 break;
78 default:
79 usage();
80 break;
81 }
82 } else {
83 if (infile) {
84 usage();
85 } else {
86 infile = argv[0];
87 }
88 }
89 }
90 if (infile == NULL)
91 usage();
92
93 if (compress) {
94 if (strcmp(infile, "-") == 0) {
95 inf = stdin;
96 } else {
97 inf = fopen(infile, "r");
98 if (inf == NULL) {
99 perror("open");
100 exit(1);
101 }
102 }
103 if (*outfile) {
104 if (strcmp(outfile, "-") == 0) {
105 outf = stdout;
106 } else {
107 outf = fopen(outfile, "w+");
108 if (outf == NULL) {
109 perror("open outfile");
110 exit(1);
111 }
112 }
113 }
114
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);
118 exit(1);
119 }
120 outbuf = (unsigned char *)malloc(length + (length + 15)/16);
121 total = rcz_compress_memory(inbuf, length, outbuf);
122 fwrite(outbuf, 1, total, outf);
123 if (verbose)
124 fprintf(stderr, "%ld %ld\nCompression ratio: %f\n", length, total, total/(1.0*length));
125 fclose(inf);
126 vm_deallocate(mach_task_self(), inbuf, length);
127 fclose(outf);
128 } else {
129 unsigned char *ptr;
130
131 if (strcmp(infile, "-") == 0) {
132 inf = stdin;
133 } else {
134 inf = fopen(infile, "r");
135 if (inf == NULL) {
136 perror("open");
137 exit(1);
138 }
139 }
140 if (*outfile) {
141 if (strcmp(outfile, "-") == 0) {
142 outf = stdout;
143 } else {
144 outf = fopen(outfile, "w+");
145 if (outf == NULL) {
146 perror("open outfile");
147 exit(1);
148 }
149 }
150 }
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);
154 exit(1);
155 }
156 ptr = inbuf;
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
160 compressed stream.
161 */
162 total = *ptr++;
163 total = (total<<8) | (*ptr++);
164 total = (total<<8) | (*ptr++);
165 total = (total<<8) | (*ptr++);
166 ptr -= 8;
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);
171 if (verbose)
172 fprintf(stderr, "%ld %ld\nCompression ratio: %f\n", length, total, total/(1.0*length));
173 fclose(inf);
174 vm_deallocate(mach_task_self(), inbuf, length);
175 fclose(outf);
176 }
177 exit(0);
178 }