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