blob: 6411ac1cdd4ef7d2f8319f887efce3d19acb9f16 [file] [log] [blame]
José Fonsecaa3285532011-11-27 12:32:00 +00001/**************************************************************************
2 *
3 * Copyright 2011 Jose Fonseca
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
27#include <string.h>
José Fonsecab1b5a382012-04-20 22:49:20 +010028#include <getopt.h>
29
José Fonsecaa3285532011-11-27 12:32:00 +000030#include <iostream>
31
32#include "cli.hpp"
33
Jose Fonsecaa09b4172016-03-25 09:34:06 +000034#include <brotli/enc/encode.h>
35
José Fonsecaa3285532011-11-27 12:32:00 +000036#include "trace_file.hpp"
Jose Fonsecace2ed372015-11-07 23:01:11 +000037#include "trace_ostream.hpp"
José Fonsecaa3285532011-11-27 12:32:00 +000038
39
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000040static const char *synopsis = "Repack a trace file with different compression.";
José Fonsecaa3285532011-11-27 12:32:00 +000041
42static void
43usage(void)
44{
45 std::cout
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000046 << "usage: apitrace repack [options] <in-trace-file> <out-trace-file>\n"
José Fonsecaa3285532011-11-27 12:32:00 +000047 << synopsis << "\n"
48 << "\n"
49 << "Snappy compression allows for faster replay and smaller memory footprint,\n"
50 << "at the expense of a slightly smaller compression ratio than zlib\n"
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000051 << "\n"
Jose Fonsecaa09b4172016-03-25 09:34:06 +000052 << " -b,--brotli Use Brotli compression\n"
53 << " -z,--zlib Use ZLib compression\n"
José Fonsecaa3285532011-11-27 12:32:00 +000054 << "\n";
55}
56
José Fonsecab1b5a382012-04-20 22:49:20 +010057const static char *
Jose Fonsecaa09b4172016-03-25 09:34:06 +000058shortOptions = "hbz";
José Fonsecab1b5a382012-04-20 22:49:20 +010059
60const static struct option
61longOptions[] = {
62 {"help", no_argument, 0, 'h'},
Jose Fonseca796d3bc2016-03-25 22:32:25 +000063 {"brotli", optional_argument, 0, 'b'},
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000064 {"zlib", no_argument, 0, 'z'},
José Fonsecab1b5a382012-04-20 22:49:20 +010065 {0, 0, 0, 0}
66};
67
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000068enum Format {
69 FORMAT_SNAPPY = 0,
70 FORMAT_ZLIB,
Jose Fonsecaa09b4172016-03-25 09:34:06 +000071 FORMAT_BROTLI,
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000072};
73
Jose Fonsecaa09b4172016-03-25 09:34:06 +000074
75class BrotliTraceIn : public brotli::BrotliIn
José Fonsecaa3285532011-11-27 12:32:00 +000076{
Jose Fonsecaa09b4172016-03-25 09:34:06 +000077private:
78 trace::File *stream;
79 char buf[1 << 16];
80 bool eof = false;
81
82public:
83 BrotliTraceIn(trace::File *s) :
84 stream(s)
85 {
José Fonsecaa3285532011-11-27 12:32:00 +000086 }
87
Jose Fonsecaa09b4172016-03-25 09:34:06 +000088 ~BrotliTraceIn() {
José Fonsecaa3285532011-11-27 12:32:00 +000089 }
90
Jose Fonsecaa09b4172016-03-25 09:34:06 +000091 const void *
92 Read(size_t n, size_t* bytes_read) override
93 {
94 if (n > sizeof buf) {
95 n = sizeof buf;
96 } else if (n == 0) {
97 return eof ? nullptr : buf;
98 }
99 *bytes_read = stream->read(buf, n);
100 if (*bytes_read == 0) {
101 eof = true;
102 return nullptr;
103 }
104 return buf;
105 }
106};
107
108
109static int
110repack_generic(trace::File *inFile, trace::OutStream *outFile)
111{
112 const size_t size = 8192;
José Fonsecaa3285532011-11-27 12:32:00 +0000113 char *buf = new char[size];
114 size_t read;
115
116 while ((read = inFile->read(buf, size)) != 0) {
117 outFile->write(buf, read);
118 }
119
120 delete [] buf;
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000121
122 return EXIT_SUCCESS;
123}
124
125
126static int
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000127repack_brotli(trace::File *inFile, const char *outFileName, int quality)
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000128{
129 brotli::BrotliParams params;
130
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000131 if (quality > 0) {
132 params.quality = quality;
133 }
134
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000135 BrotliTraceIn in(inFile);
136 FILE *fout = fopen(outFileName, "wb");
137 if (!fout) {
138 return EXIT_FAILURE;
139 }
140 assert(fout);
141 brotli::BrotliFileOut out(fout);
142 if (!BrotliCompress(params, &in, &out)) {
143 std::cerr << "error: brotli compression failed\n";
144 return EXIT_FAILURE;
145 }
146 fclose(fout);
147
148 return EXIT_SUCCESS;
149}
150
151static int
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000152repack(const char *inFileName, const char *outFileName, Format format, int quality)
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000153{
154 int ret = EXIT_FAILURE;
155
156 trace::File *inFile = trace::File::createForRead(inFileName);
157 if (!inFile) {
158 return 1;
159 }
160
161 trace::OutStream *outFile = nullptr;
162 if (format == FORMAT_SNAPPY) {
163 outFile = trace::createSnappyStream(outFileName);
164 } else if (format == FORMAT_BROTLI) {
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000165 ret = repack_brotli(inFile, outFileName, quality);
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000166 delete inFile;
167 return ret;
168 } else if (format == FORMAT_ZLIB) {
169 outFile = trace::createZLibStream(outFileName);
170 }
171 if (outFile) {
172 ret = repack_generic(inFile, outFile);
173 delete outFile;
174 }
175
José Fonsecaa3285532011-11-27 12:32:00 +0000176 delete inFile;
177
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000178 return ret;
José Fonsecaa3285532011-11-27 12:32:00 +0000179}
180
181static int
182command(int argc, char *argv[])
183{
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000184 Format format = FORMAT_SNAPPY;
José Fonsecab1b5a382012-04-20 22:49:20 +0100185 int opt;
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000186 int quality = -1;
José Fonsecab1b5a382012-04-20 22:49:20 +0100187 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
188 switch (opt) {
189 case 'h':
José Fonsecaa3285532011-11-27 12:32:00 +0000190 usage();
191 return 0;
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000192 case 'b':
193 format = FORMAT_BROTLI;
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000194 if (optarg) {
195 quality = atoi(optarg);
196 }
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000197 break;
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000198 case 'z':
199 format = FORMAT_ZLIB;
200 break;
José Fonsecab1b5a382012-04-20 22:49:20 +0100201 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700202 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonsecaa3285532011-11-27 12:32:00 +0000203 usage();
204 return 1;
205 }
206 }
207
José Fonsecab1b5a382012-04-20 22:49:20 +0100208 if (argc != optind + 2) {
José Fonsecaa3285532011-11-27 12:32:00 +0000209 std::cerr << "error: insufficient number of arguments\n";
210 usage();
211 return 1;
212 }
213
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000214 return repack(argv[optind], argv[optind + 1], format, quality);
José Fonsecaa3285532011-11-27 12:32:00 +0000215}
216
217const Command repack_command = {
218 "repack",
219 synopsis,
220 usage,
221 command
222};