Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | // ========== Copyright Header Begin ========================================== |
2 | // | |
3 | // OpenSPARC T2 Processor File: rstzip.C | |
4 | // Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
5 | // DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
6 | // | |
7 | // The above named program is free software; you can redistribute it and/or | |
8 | // modify it under the terms of the GNU General Public | |
9 | // License version 2 as published by the Free Software Foundation. | |
10 | // | |
11 | // The above named program is distributed in the hope that it will be | |
12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | // | |
16 | // You should have received a copy of the GNU General Public | |
17 | // License along with this work; if not, write to the Free Software | |
18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 | // | |
20 | // ========== Copyright Header End ============================================ | |
21 | /* rstzip.C | |
22 | * compressor binary for rstzip (includes rstzip1, 2, 3 (and other versions) | |
23 | */ | |
24 | ||
25 | ||
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <libgen.h> | |
29 | #include <string.h> | |
30 | #include <strings.h> | |
31 | #include <unistd.h> | |
32 | ||
33 | #include "rstf/rstf.h" | |
34 | ||
35 | #if defined(ARCH_AMD64) | |
36 | #include "rstf/rstf_convert.h" | |
37 | #endif | |
38 | ||
39 | #include "Rstzip.H" | |
40 | ||
41 | const char usage[] = | |
42 | "rstzip [-h] [-v] [-verbose] [-d] [-n #] [-s] [-o outfile] [[-i] infile]\n" | |
43 | " -v # print version number and exit\n" | |
44 | " -h # print this help string and exit\n" | |
45 | " -verbose # print verbose diagnostics\n" | |
46 | " -d # decompress. default if invoked as rstunzip\n" | |
47 | " -n <nrec> # (de)compress nrec records and exit\n" | |
48 | " -up # obsolete option. ignored\n" | |
49 | " -s # print compression statistics. verbose stats if -verbose\n" | |
50 | " -o file # output file. default: stdout (only if non-tty)\n" | |
51 | " [i] file # input file. default: stdin (only if non-tty)\n\n" | |
52 | "Example 1: rstzip -i file.rst -o file.rz.gz\n" | |
53 | "Example 2: rstunzip file.rz2.gz | trconv -x|less\n" | |
54 | "Example 3: rstunzip2 file.rz2.gz | rstzip -o file.rz.gz\n"; | |
55 | ||
56 | ||
57 | int main(int argc, char **argv) | |
58 | { | |
59 | const char * infile = NULL; | |
60 | const char * outfile = NULL; | |
61 | ||
62 | int64_t record_count = (int64_t) ((~0ull)>>1); // some ridiculously large number | |
63 | ||
64 | bool c_nd; | |
65 | ||
66 | ||
67 | bool verbose = false; | |
68 | bool stats = false; | |
69 | ||
70 | Rstzip * rz = new Rstzip; | |
71 | ||
72 | ||
73 | char * cmd = strdup(argv[0]); | |
74 | char * bn = basename(cmd); | |
75 | if ((strcmp(bn, "rstunzip") == 0) || (strcmp(bn, "rstunzip2") == 0) || (strcmp(bn, "rstunzip3") == 0)) { | |
76 | c_nd = false; | |
77 | } else { | |
78 | c_nd = true; | |
79 | } | |
80 | ||
81 | int i = 1; | |
82 | while(i < argc) { | |
83 | const char * arg = argv[i++]; | |
84 | if (strcmp(arg, "-h") == 0) { | |
85 | printf("Usage: %s\n", usage); | |
86 | exit(0); | |
87 | } else if (strcmp(arg, "-v") == 0) { // print version number from the newest rstzip compressor | |
88 | printf("rstzip version %s, build date %s\n", rz->getVersionStr(), __DATE__); | |
89 | exit(0); | |
90 | } else if (strcmp(arg, "-verbose") == 0) { | |
91 | verbose = true; | |
92 | } else if (strcmp(arg, "-s") == 0) { | |
93 | stats = true; | |
94 | } else if (strcmp(arg, "-d") == 0) { | |
95 | c_nd = false; | |
96 | } else if (strcmp(arg, "-n") == 0) { | |
97 | if (argv[i] == NULL) { | |
98 | fprintf(stderr, "ERROR: %s requires an argument\nUsage: %s\n", arg, usage); | |
99 | exit(1); | |
100 | } | |
101 | int rv = sscanf(argv[i], "%lld", &record_count); | |
102 | if (rv != 1) { | |
103 | fprintf(stderr, "ERROR parsing argument: %s %s\nUsage: %s\n", arg, argv[i], usage); | |
104 | exit(1); | |
105 | } | |
106 | i++; | |
107 | } else if (strcmp(arg, "-o") == 0) { | |
108 | if (argv[i] == NULL) { | |
109 | fprintf(stderr, "ERROR: %s requires an argument\nUsage; %s\n", arg, usage); | |
110 | exit(1); | |
111 | } | |
112 | outfile = argv[i++]; | |
113 | } else if (strcmp(arg, "-i") == 0) { | |
114 | if (infile != NULL) { | |
115 | fprintf(stderr, "ERROR: input file %s already specified. Offending arg=%s\nUsage: %s\n", | |
116 | infile, arg, usage); | |
117 | exit(1); | |
118 | } | |
119 | if (argv[i] == NULL) { | |
120 | fprintf(stderr, "ERROR: %s requires an argument\nUsage; %s\n", arg, usage); | |
121 | exit(1); | |
122 | } | |
123 | infile = argv[i++]; | |
124 | } else { | |
125 | if (infile != NULL) { | |
126 | fprintf(stderr, "ERROR: input file %s already specified. Offending arg=%s\nUsage: %s\n", | |
127 | infile, arg, usage); | |
128 | exit(1); | |
129 | } | |
130 | infile = arg; | |
131 | } | |
132 | } // while more args | |
133 | ||
134 | if ((infile == NULL) && isatty(STDIN_FILENO)) { | |
135 | fprintf(stderr, "Error: rstzip cannot read binary input from tty\nUsage: %s\n", usage); | |
136 | exit(0); | |
137 | } | |
138 | ||
139 | if ((outfile == NULL) && isatty(STDOUT_FILENO)) { | |
140 | fprintf(stderr, "Error: rstzip cannot write binary output to tty\nUsage: %s", usage); | |
141 | exit(1); | |
142 | } | |
143 | ||
144 | if (record_count == 0) { | |
145 | exit(0); | |
146 | } | |
147 | ||
148 | char rz3_options[256]; | |
149 | bzero(rz3_options, 256); | |
150 | if (verbose) { | |
151 | strcat(rz3_options, " verbose=1 "); | |
152 | } | |
153 | if (stats) { | |
154 | strcat(rz3_options, " stats=1 "); | |
155 | } | |
156 | ||
157 | int rv; | |
158 | ||
159 | if (c_nd) { | |
160 | // compress | |
161 | FILE * fp; | |
162 | if (infile != NULL) { | |
163 | fp = fopen(infile, "r"); | |
164 | if (fp == NULL) { | |
165 | perror(infile); | |
166 | exit(1); | |
167 | } | |
168 | } else { | |
169 | infile = "<STDIN>"; | |
170 | fp = stdin; | |
171 | } | |
172 | ||
173 | rv = rz->open(outfile, "w", rz3_options); | |
174 | if (outfile == NULL) { | |
175 | outfile = "<STDOUT>"; | |
176 | } | |
177 | if (rv != RSTZIP_OK) { | |
178 | fprintf(stderr, "ERROR: rstzip::open error writing %s\n", outfile); | |
179 | exit(1); | |
180 | } | |
181 | ||
182 | rstf_unionT * rstbuf = new rstf_unionT [rstzip_opt_buffersize]; | |
183 | ||
184 | int64_t more = record_count; | |
185 | ||
186 | int n; | |
187 | while(more) { | |
188 | int req = (more > rstzip_opt_buffersize) ? rstzip_opt_buffersize : more; | |
189 | n = fread(rstbuf, sizeof(rstf_unionT), req, fp); | |
190 | if (n == 0) break; | |
191 | #if defined(ARCH_AMD64) | |
192 | for (int i=0; i<n; i++) { | |
193 | rstf_convertT::b2l((rstf_uint8T*)&rstbuf[i]); | |
194 | } | |
195 | #endif | |
196 | int rv = rz->compress(rstbuf, n); | |
197 | if (rv != n) { | |
198 | fprintf(stderr, "ERROR: rstzip: could not compress %d records (rz->compress() returned %d\n", n, rv); | |
199 | break; | |
200 | } | |
201 | more -= n; | |
202 | } | |
203 | rz->close(); | |
204 | delete rz; rz=NULL; | |
205 | ||
206 | delete []rstbuf; rstbuf = NULL; | |
207 | ||
208 | if (fp != stdin) { | |
209 | fclose(fp); fp = NULL; | |
210 | } | |
211 | ||
212 | } else { | |
213 | // decompress | |
214 | FILE * fp; | |
215 | if (outfile != NULL) { | |
216 | fp = fopen(outfile, "w"); | |
217 | if (fp == NULL) { | |
218 | perror(outfile); | |
219 | exit(1); | |
220 | } | |
221 | } else { | |
222 | outfile = "<STDOUT>"; | |
223 | fp = stdout; | |
224 | } | |
225 | ||
226 | rv = rz->open(infile, "r", rz3_options); | |
227 | if (infile == NULL) { | |
228 | infile = "<STDIN>"; | |
229 | } | |
230 | if (rv != RSTZIP_OK) { | |
231 | fprintf(stderr, "ERROR: rstzip::open error reading %s\n", infile); | |
232 | exit(1); | |
233 | } | |
234 | ||
235 | rstf_unionT * rstbuf = new rstf_unionT [rstzip_opt_buffersize]; | |
236 | ||
237 | int64_t more = record_count; | |
238 | ||
239 | while(more) { | |
240 | int req = (more > rstzip_opt_buffersize) ? rstzip_opt_buffersize : more; | |
241 | int n = rz->decompress(rstbuf, req); | |
242 | if (n == 0) break; | |
243 | ||
244 | int rv = fwrite(rstbuf, sizeof(rstf_unionT), n, fp); | |
245 | if (rv != n) { | |
246 | fprintf(stderr, "ERROR: rstzip: could not write %d decompressed records to ", n); | |
247 | perror(outfile); | |
248 | break; | |
249 | } | |
250 | more -= n; | |
251 | } | |
252 | rz->close(); | |
253 | delete rz; rz = NULL; | |
254 | ||
255 | delete [] rstbuf; rstbuf = NULL; | |
256 | ||
257 | if (fp != stdout) { | |
258 | fclose(fp); fp = NULL; | |
259 | } | |
260 | ||
261 | } // compress or decompress? | |
262 | } // int main(int argc, char **argv) | |
263 |