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