00001
00002 #include "config.h"
00003
00004 #if OASYS_ZLIB_ENABLED
00005
00006 #include <sys/errno.h>
00007 #include <zlib.h>
00008
00009 #include "../debug/Log.h"
00010 #include "../io/FileUtils.h"
00011 #include "../io/MmapFile.h"
00012 #include "../util/Getopt.h"
00013 #include "../util/MD5.h"
00014
00015
00016 using namespace oasys;
00017
00018 int
00019 main(int argc, char* const argv[])
00020 {
00021 const char* LOG = "/zlibsize";
00022
00023 std::string filename = "";
00024 u_int len = 0;
00025 u_int offset = 0;
00026
00027 oasys::Getopt opts;
00028 opts.addopt(new UIntOpt('L', "length", &len));
00029 opts.addopt(new UIntOpt('o', "offset", &offset));
00030
00031 int remainder = opts.getopt(argv[0], argc, argv);
00032 if (remainder != argc - 1) {
00033 fprintf(stderr, "invalid argument '%s'\n", argv[remainder]);
00034 opts.usage(argv[0], "filename");
00035 exit(1);
00036 }
00037
00038 filename = argv[remainder];
00039
00040 Log::init();
00041
00042 if (filename == "") {
00043 log_err_p(LOG, "filename must be set");
00044 exit(1);
00045 }
00046
00047 if (len == 0) {
00048 len = FileUtils::size(filename.c_str());
00049 }
00050
00051 MmapFile mm("/zlibsize/mmap");
00052 const u_char* src = (const u_char*)mm.map(filename.c_str(), PROT_READ, 0,
00053 len, offset);
00054 if (src == NULL) {
00055 log_err_p(LOG, "error mmap'ing file: %s", strerror(errno));
00056 exit(1);
00057 }
00058
00059 unsigned long zlen = compressBound(len);
00060 void* dst = malloc(zlen);
00061 log_debug_p(LOG, "calling compress on %lu byte buffer: src len %u",
00062 zlen, len);
00063
00064 int cc = compress((Bytef*)dst, &zlen, src, len);
00065 if (cc != Z_OK) {
00066 log_err_p(LOG, "error in compress: %s", zError(cc));
00067 exit(1);
00068 }
00069 free(dst);
00070
00071 printf("%s\t%u\t%lu\n", filename.c_str(), len, zlen);
00072 fflush(stdout);
00073 }
00074
00075 #else // OASYS_ZLIB_ENABLED
00076
00077 #include <stdio.h>
00078 #include <stdlib.h>
00079
00080 int
00081 main(int argc, char* const argv[])
00082 {
00083 (void)argc;
00084 fprintf(stderr, "%s: zlib required\n", argv[0]);
00085 exit(1);
00086 }
00087
00088 #endif // OASYS_ZLIB_ENABLED