// ========== Copyright Header Begin ==========================================
// OpenSPARC T2 Processor File: rstzip.C
// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
// The above named program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License version 2 as published by the Free Software Foundation.
// The above named program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public
// License along with this work; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
// ========== Copyright Header End ============================================
* compressor binary for rstzip (includes rstzip1, 2, 3 (and other versions)
#include "rstf/rstf_convert.h"
"rstzip [-h] [-v] [-verbose] [-d] [-n #] [-s] [-o outfile] [[-i] infile]\n"
" -v # print version number and exit\n"
" -h # print this help string and exit\n"
" -verbose # print verbose diagnostics\n"
" -d # decompress. default if invoked as rstunzip\n"
" -n <nrec> # (de)compress nrec records and exit\n"
" -up # obsolete option. ignored\n"
" -s # print compression statistics. verbose stats if -verbose\n"
" -o file # output file. default: stdout (only if non-tty)\n"
" [i] file # input file. default: stdin (only if non-tty)\n\n"
"Example 1: rstzip -i file.rst -o file.rz.gz\n"
"Example 2: rstunzip file.rz2.gz | trconv -x|less\n"
"Example 3: rstunzip2 file.rz2.gz | rstzip -o file.rz.gz\n";
int main(int argc, char **argv)
const char * infile = NULL;
const char * outfile = NULL;
int64_t record_count = (int64_t) ((~0ull)>>1); // some ridiculously large number
Rstzip * rz = new Rstzip;
char * cmd = strdup(argv[0]);
char * bn = basename(cmd);
if ((strcmp(bn, "rstunzip") == 0) || (strcmp(bn, "rstunzip2") == 0) || (strcmp(bn, "rstunzip3") == 0)) {
const char * arg = argv[i++];
if (strcmp(arg, "-h") == 0) {
printf("Usage: %s\n", usage);
} else if (strcmp(arg, "-v") == 0) { // print version number from the newest rstzip compressor
printf("rstzip version %s, build date %s\n", rz->getVersionStr(), __DATE__);
} else if (strcmp(arg, "-verbose") == 0) {
} else if (strcmp(arg, "-s") == 0) {
} else if (strcmp(arg, "-d") == 0) {
} else if (strcmp(arg, "-n") == 0) {
fprintf(stderr, "ERROR: %s requires an argument\nUsage: %s\n", arg, usage);
int rv = sscanf(argv[i], "%lld", &record_count);
fprintf(stderr, "ERROR parsing argument: %s %s\nUsage: %s\n", arg, argv[i], usage);
} else if (strcmp(arg, "-o") == 0) {
fprintf(stderr, "ERROR: %s requires an argument\nUsage; %s\n", arg, usage);
} else if (strcmp(arg, "-i") == 0) {
fprintf(stderr, "ERROR: input file %s already specified. Offending arg=%s\nUsage: %s\n",
fprintf(stderr, "ERROR: %s requires an argument\nUsage; %s\n", arg, usage);
fprintf(stderr, "ERROR: input file %s already specified. Offending arg=%s\nUsage: %s\n",
if ((infile == NULL) && isatty(STDIN_FILENO)) {
fprintf(stderr, "Error: rstzip cannot read binary input from tty\nUsage: %s\n", usage);
if ((outfile == NULL) && isatty(STDOUT_FILENO)) {
fprintf(stderr, "Error: rstzip cannot write binary output to tty\nUsage: %s", usage);
strcat(rz3_options, " verbose=1 ");
strcat(rz3_options, " stats=1 ");
rv = rz->open(outfile, "w", rz3_options);
fprintf(stderr, "ERROR: rstzip::open error writing %s\n", outfile);
rstf_unionT * rstbuf = new rstf_unionT [rstzip_opt_buffersize];
int64_t more = record_count;
int req = (more > rstzip_opt_buffersize) ? rstzip_opt_buffersize : more;
n = fread(rstbuf, sizeof(rstf_unionT), req, fp);
for (int i=0; i<n; i++) {
rstf_convertT::b2l((rstf_uint8T*)&rstbuf[i]);
int rv = rz->compress(rstbuf, n);
fprintf(stderr, "ERROR: rstzip: could not compress %d records (rz->compress() returned %d\n", n, rv);
delete []rstbuf; rstbuf = NULL;
fp = fopen(outfile, "w");
rv = rz->open(infile, "r", rz3_options);
fprintf(stderr, "ERROR: rstzip::open error reading %s\n", infile);
rstf_unionT * rstbuf = new rstf_unionT [rstzip_opt_buffersize];
int64_t more = record_count;
int req = (more > rstzip_opt_buffersize) ? rstzip_opt_buffersize : more;
int n = rz->decompress(rstbuf, req);
int rv = fwrite(rstbuf, sizeof(rstf_unionT), n, fp);
fprintf(stderr, "ERROR: rstzip: could not write %d decompressed records to ", n);
delete [] rstbuf; rstbuf = NULL;
} // compress or decompress?
} // int main(int argc, char **argv)