Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: rstzip2c.c | |
5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
7 | * | |
8 | * The above named program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public | |
10 | * License version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * The above named program is distributed in the hope that it will be | |
13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public | |
18 | * License along with this work; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | * | |
21 | * ========== Copyright Header End ============================================ | |
22 | */ | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | ||
27 | #include "rstzip2if.H" | |
28 | ||
29 | enum { | |
30 | BUFFERSIZE = 40000 | |
31 | }; | |
32 | ||
33 | static char zipname[] = "rstzip2"; | |
34 | static char unzipname[] = "rstunzip2"; | |
35 | static char* exename = zipname; | |
36 | ||
37 | void fprintUsage(FILE* fp, int major, int minor) { | |
38 | fprintf(fp, | |
39 | "Usage: %s [flags] [infile]\n" | |
40 | "\n" | |
41 | "(version %d.%02d, compiled %s) Compress / Decompress RST trace files.\n" | |
42 | "\n" | |
43 | "Flags:\n" | |
44 | " -d decompress\n" | |
45 | " -h give this help\n" | |
46 | " -n # decompress # records and exit (0=all, default=0)\n" | |
47 | " -up compress as uniprocessor trace (for old RST format)\n" | |
48 | " -mp compress as multiprocessor trace (for new RST format)\n" | |
49 | " -s print compression statistics\n" | |
50 | " -nogz do not apply gzip to the rstzip2 (de)compression\n" | |
51 | " -o outfile write output to outfile (Default: stdout)\n" | |
52 | " infile file to (de)compress (Default: stdin)\n" | |
53 | "\n" | |
54 | "NOTES: The 'old' RST format refers to RST versions 1.09 and below.\n" | |
55 | " Rstzip2 is incompatible with rstzip compressed files.\n" | |
56 | "\n" | |
57 | "Example 1: rstzip2 -o file.rz2.gz file.rst\n" | |
58 | "Example 2: rstunzip2 file.rz2.gz | trconv | less\n" | |
59 | "Example 3: rstzip2 file.rst | rstunzip2 | trconv | less\n" | |
60 | "\n" | |
61 | "Uses libz.a (version 1.1.3) written by Jean-loup Gailly and Mark Adler.\n" | |
62 | "\n", | |
63 | exename, major, minor, __DATE__); | |
64 | } | |
65 | ||
66 | #ifdef __cplusplus | |
67 | ||
68 | int main(int argc, char* argv[]) { | |
69 | Rstzip2if* rstzip = new Rstzip2if; | |
70 | char* infile = NULL; | |
71 | char* outfile = NULL; | |
72 | FILE* infp = stdin; | |
73 | FILE* outfp = stdout; | |
74 | rstf_unionT* rstbuf = NULL; | |
75 | long nrecs = 0; | |
76 | int numcpus = -1; | |
77 | int gzip = 1; | |
78 | int stats = 0; | |
79 | int decompress = 0; | |
80 | uint64_t totalrecs = 0; | |
81 | uint64_t decompress_nrecs = 0; | |
82 | int i; | |
83 | ||
84 | // Parse arguments. | |
85 | char* pname = strrchr(argv[0], '/'); | |
86 | if (pname == NULL) { | |
87 | if (strcmp(argv[0], unzipname) == 0) { | |
88 | exename = unzipname; | |
89 | } | |
90 | } else { | |
91 | if (strcmp(pname + 1, unzipname) == 0) { | |
92 | exename = unzipname; | |
93 | } | |
94 | } | |
95 | ||
96 | for (i = 1; i < argc; i++) { | |
97 | if (strcmp(argv[i], "-d") == 0) { | |
98 | decompress = 1; | |
99 | } else if (strcmp(argv[i], "-n") == 0) { | |
100 | i++; | |
101 | decompress_nrecs = strtoull(argv[i], NULL, 10); | |
102 | } else if (strcmp(argv[i], "-up") == 0) { | |
103 | numcpus = 1; | |
104 | } else if (strcmp(argv[i], "-mp") == 0) { | |
105 | numcpus = 0; | |
106 | } else if (strcmp(argv[i], "-s") == 0) { | |
107 | stats = 1; | |
108 | } else if (strcmp(argv[i], "-nogz") == 0) { | |
109 | gzip = 0; | |
110 | } else if (strcmp(argv[i], "-h") == 0 || | |
111 | strcmp(argv[i], "-help") == 0) { | |
112 | fprintUsage(stdout, rstzip->getMajorVersion(), rstzip->getMinorVersion()); | |
113 | exit(0); | |
114 | } else if (strcmp(argv[i], "-o") == 0) { | |
115 | i++; | |
116 | outfile = argv[i]; | |
117 | } else if (i == argc - 1) { | |
118 | infile = argv[i]; | |
119 | } else { | |
120 | fprintUsage(stderr, rstzip->getMajorVersion(), rstzip->getMinorVersion()); | |
121 | fprintf(stderr, "Error: unknown input parameter %s\n", argv[i]); | |
122 | exit(1); | |
123 | } | |
124 | } | |
125 | ||
126 | if (exename == unzipname) { | |
127 | decompress = 1; | |
128 | } | |
129 | ||
130 | if (decompress == 1) { | |
131 | if (outfile != NULL) { | |
132 | outfp = fopen(outfile, "w"); | |
133 | if (outfp == NULL) { | |
134 | fprintf(stderr, "\nError: unable to open %s for writing.\n\n", outfile); | |
135 | exit(1); | |
136 | } | |
137 | } | |
138 | ||
139 | if (infile == NULL) { | |
140 | infile = (char*) malloc(2); | |
141 | strcpy(infile, "-"); | |
142 | } | |
143 | } else { | |
144 | if (infile != NULL) { | |
145 | infp = fopen(infile, "r"); | |
146 | if (infp == NULL) { | |
147 | fprintf(stderr, "\nError: unable to open %s for reading.\n\n", infile); | |
148 | fprintUsage(stderr, rstzip->getMajorVersion(), rstzip->getMinorVersion()); | |
149 | exit(1); | |
150 | } | |
151 | } | |
152 | ||
153 | if (outfile == NULL) { | |
154 | outfile = (char*) malloc(2); | |
155 | strcpy(outfile, "-"); | |
156 | } | |
157 | } | |
158 | ||
159 | rstbuf = (rstf_unionT*) malloc(BUFFERSIZE * sizeof(rstf_unionT)); | |
160 | ||
161 | if (decompress == 0) { | |
162 | // Compression | |
163 | nrecs = fread(rstbuf, sizeof(rstf_unionT), BUFFERSIZE, infp); | |
164 | ||
165 | if (numcpus == -1) { | |
166 | if (rstbuf[0].proto.rtype == RSTHEADER_T) { | |
167 | if ((rstbuf[0].header.majorVer * 100) + rstbuf[0].header.minorVer < 110) { | |
168 | numcpus = 1; | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | rstzip->openRstzip(outfile, BUFFERSIZE, gzip, stats, numcpus); | |
174 | ||
175 | while (nrecs > 0) { | |
176 | rstzip->compress(rstbuf, nrecs); | |
177 | nrecs = fread(rstbuf, sizeof(rstf_unionT), BUFFERSIZE, infp); | |
178 | } | |
179 | ||
180 | rstzip->closeRstzip(); | |
181 | } else { | |
182 | // Decompression | |
183 | rstzip->openRstunzip(infile, BUFFERSIZE, gzip, stats); | |
184 | ||
185 | nrecs = rstzip->decompress(rstbuf, BUFFERSIZE); | |
186 | while (nrecs > 0) { | |
187 | if (decompress_nrecs > 0 && totalrecs + nrecs > decompress_nrecs) { | |
188 | totalrecs += fwrite(rstbuf, sizeof(rstf_unionT), decompress_nrecs - totalrecs, outfp); | |
189 | break; | |
190 | } else { | |
191 | totalrecs += fwrite(rstbuf, sizeof(rstf_unionT), nrecs, outfp); | |
192 | } | |
193 | nrecs = rstzip->decompress(rstbuf, BUFFERSIZE); | |
194 | } | |
195 | ||
196 | rstzip->closeRstunzip(); | |
197 | } | |
198 | ||
199 | delete rstzip; | |
200 | free(rstbuf); | |
201 | ||
202 | fclose(infp); | |
203 | fclose(outfp); | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
208 | #else // __cplusplus | |
209 | ||
210 | int main(int argc, char* argv[]) { | |
211 | Rstzip2if* rstzip = NULL; | |
212 | char* infile = NULL; | |
213 | char* outfile = NULL; | |
214 | FILE* infp = stdin; | |
215 | FILE* outfp = stdout; | |
216 | rstf_unionT* rstbuf = NULL; | |
217 | int nrecs = 0; | |
218 | int numcpus = -1; | |
219 | int gzip = 1; | |
220 | int stats = 0; | |
221 | int decompress = 0; | |
222 | uint64_t totalrecs = 0; | |
223 | uint64_t decompress_nrecs = 0; | |
224 | int i; | |
225 | ||
226 | // Parse arguments. | |
227 | char* pname = strrchr(argv[0], '/'); | |
228 | if (pname == NULL) { | |
229 | if (strcmp(argv[0], unzipname) == 0) { | |
230 | exename = unzipname; | |
231 | } | |
232 | } else { | |
233 | if (strcmp(pname + 1, unzipname) == 0) { | |
234 | exename = unzipname; | |
235 | } | |
236 | } | |
237 | ||
238 | for (i = 1; i < argc; i++) { | |
239 | if (strcmp(argv[i], "-d") == 0) { | |
240 | decompress = 1; | |
241 | } else if (strcmp(argv[i], "-n") == 0) { | |
242 | i++; | |
243 | decompress_nrecs = strtoull(argv[i], NULL, 10); | |
244 | } else if (strcmp(argv[i], "-up") == 0) { | |
245 | numcpus = 1; | |
246 | } else if (strcmp(argv[i], "-mp") == 0) { | |
247 | numcpus = 0; | |
248 | } else if (strcmp(argv[i], "-s") == 0) { | |
249 | stats = 1; | |
250 | } else if (strcmp(argv[i], "-nogz") == 0) { | |
251 | gzip = 0; | |
252 | } else if (strcmp(argv[i], "-h") == 0) { | |
253 | fprintUsage(stdout, rz2_getMajorVersion(rstzip), rz2_getMinorVersion(rstzip)); | |
254 | exit(0); | |
255 | } else if (strcmp(argv[i], "-o") == 0) { | |
256 | i++; | |
257 | outfile = argv[i]; | |
258 | } else if (i == argc - 1) { | |
259 | infile = argv[i]; | |
260 | } else { | |
261 | fprintUsage(stderr, rz2_getMajorVersion(rstzip), rz2_getMinorVersion(rstzip)); | |
262 | fprintf(stderr, "Error: unknown input parameter %s\n", argv[i]); | |
263 | exit(1); | |
264 | } | |
265 | } | |
266 | ||
267 | if (exename == unzipname) { | |
268 | decompress = 1; | |
269 | } | |
270 | ||
271 | if (decompress == 1) { | |
272 | if (outfile != NULL) { | |
273 | outfp = fopen(outfile, "w"); | |
274 | if (outfp == NULL) { | |
275 | fprintf(stderr, "Error: unable to open %s for writing.\n", outfile); | |
276 | exit(1); | |
277 | } | |
278 | } | |
279 | ||
280 | if (infile == NULL) { | |
281 | infile = (char*) malloc(2); | |
282 | strcpy(infile, "-"); | |
283 | } | |
284 | } else { | |
285 | if (infile != NULL) { | |
286 | infp = fopen(infile, "r"); | |
287 | if (infp == NULL) { | |
288 | fprintf(stderr, "Error: unable to open %s for reading.\n", infile); | |
289 | exit(1); | |
290 | } | |
291 | } | |
292 | ||
293 | if (outfile == NULL) { | |
294 | outfile = (char*) malloc(2); | |
295 | strcpy(outfile, "-"); | |
296 | } | |
297 | } | |
298 | ||
299 | rstbuf = (rstf_unionT*) malloc(BUFFERSIZE * sizeof(rstf_unionT)); | |
300 | ||
301 | if (decompress == 0) { | |
302 | // Compression | |
303 | nrecs = fread(rstbuf, sizeof(rstf_unionT), BUFFERSIZE, infp); | |
304 | ||
305 | if (numcpus == -1) { | |
306 | if (rstbuf[0].proto.rtype == RSTHEADER_T) { | |
307 | if ((rstbuf[0].header.majorVer * 100) + rstbuf[0].header.minorVer < 110) { | |
308 | numcpus = 1; | |
309 | } | |
310 | } | |
311 | } | |
312 | ||
313 | rstzip = rz2_openRstzip(outfile, BUFFERSIZE, gzip, stats, numcpus); | |
314 | while (nrecs > 0) { | |
315 | rz2_compress(rstzip, rstbuf, nrecs); | |
316 | nrecs = fread(rstbuf, sizeof(rstf_unionT), BUFFERSIZE, infp); | |
317 | } | |
318 | ||
319 | rz2_closeRstzip(rstzip); | |
320 | } else { | |
321 | // Decompression | |
322 | rstzip = rz2_openRstunzip(infile, BUFFERSIZE, gzip, stats); | |
323 | ||
324 | nrecs = rz2_decompress(rstzip, rstbuf, BUFFERSIZE); | |
325 | while (nrecs > 0) { | |
326 | if (decompress_nrecs > 0 && totalrecs + nrecs > decompress_nrecs) { | |
327 | totalrecs += fwrite(rstbuf, sizeof(rstf_unionT), decompress_nrecs - totalrecs, outfp); | |
328 | break; | |
329 | } else { | |
330 | totalrecs += fwrite(rstbuf, sizeof(rstf_unionT), nrecs, outfp); | |
331 | } | |
332 | nrecs = rz2_decompress(rstzip, rstbuf, BUFFERSIZE); | |
333 | } | |
334 | ||
335 | rz2_closeRstunzip(rstzip); | |
336 | } | |
337 | ||
338 | free(rstbuf); | |
339 | ||
340 | fclose(infp); | |
341 | fclose(outfp); | |
342 | ||
343 | return 0; | |
344 | } | |
345 | ||
346 | #endif |