Debian Codegraph @

faketape/mangle-tape.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2024
3 * Tim Woodall. All rights reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *
27 * SPDX short identifier: BSD-2-Clause
28 */
29
30 /*
31 * mangle-tape - tool to mangle the bitfields of a tape to test the different
32 * encodings that might have been created over the years.
33 */
34
35 #include "config.h" // IWYU pragma: keep
36 #include "faketape-lib.h"
37
38 #include "bswap_header.h"
39
40 #include <inttypes.h>
41 #include <iostream>
42 #include <vector>
43 #include <cstring>
44 #include <fcntl.h>
45 #include <err.h>
46 #include <unistd.h>
47 #include <getopt.h>
48 #include <zlib.h>
49 #include <bzlib.h>
50 #include <lzo/lzo1x.h>
51 #include <optional>
52 #include <lzo/lzoconf.h>
53 #include <stdlib.h>
54 #include <zconf.h>
55 #include <memory>
56 #include <string>
57
58
59 // Messy. Ideally we'd use readblock_tape from restore/readtape.c but there are
60 // too many issues with global variables that we don't use but need to link in.
61
62 // Manually copied from protocols/dumprestore.h
63
64 #define COMPRESS_ZLIB 0
65 #define COMPRESS_BZLIB 1
66 #define COMPRESS_LZO 2
67
68 static std::vector<uint8_t> databuffer;
69
70 [[noreturn]] void usage(const char* prog) {
71 std::cerr << "Usage " << prog << " in-tape out-tape" << std::endl;
72 exit(1);
73 }
74
75 [[noreturn]] void fatal(const char* msg, const char* prog) {
76 std::cerr << "FATAL: " << msg << std::endl << std::endl;
77 usage(prog);
78 }
79
80 int main(int argc, char* argv[]) {
81
82 FakeTape tapein;
83 FakeTape tapeout;
84
85 const char* prog = argv[0];
86
87 const char* opts = "dhv";
88 struct option lopts[] = {
89 { .name = "version", .has_arg = 0, .flag = nullptr, .val = 'v' },
90 { .name = "debug", .has_arg = 0, .flag = nullptr, .val = 'd' },
91 { .name = "mangle-bitfields", .has_arg = 0, .flag = nullptr, .val = 1 },
92 { .name = "byteswap-output", .has_arg = 0, .flag = nullptr, .val = 4 },
93 { .name = "zlib-compress", .has_arg = 1, .flag = nullptr, .val = 5 },
94 { .name = "bzlib-compress", .has_arg = 1, .flag = nullptr, .val = 6 },
95 { .name = "lzo-compress", .has_arg = 0, .flag = nullptr, .val = 7 },
96 {},
97 };
98 std::string tapedev;
99
100 int ch;
101
102 bool mangle_bitfields = false;
103 bool bswap_output = false;
104 bool zlib_compress = false;
105 bool bzlib_compress = false;
106 bool lzo_compress = false;
107 int compress_level = 0;
108 while (( ch = getopt_long(argc, argv, opts, lopts, nullptr)) != -1)
109 switch(ch) {
110 case 'd':
111 tapein.debug = std::make_unique<std::ostream>(std::cerr.rdbuf());
112 tapein.debug->copyfmt(std::cerr);
113 tapein.debug->clear(std::cerr.rdstate());
114 tapeout.debug = std::make_unique<std::ostream>(std::cerr.rdbuf());
115 tapeout.debug->copyfmt(std::cerr);
116 tapeout.debug->clear(std::cerr.rdstate());
117 break;
118 case 'v':
119 std::cerr << prog << " version 1.0" << std::endl;
120 break;
121 case 'h':
122 usage(prog);
123 case 1:
124 mangle_bitfields = true;
125 break;
126 case 4:
127 bswap_output = true;
128 break;
129 case 5:
130 if (zlib_compress || bzlib_compress || lzo_compress)
131 fatal("Only one compression method can be specified", prog);
132 zlib_compress = true;
133 compress_level = atoi(optarg);
134 break;
135 case 6:
136 if (zlib_compress || bzlib_compress || lzo_compress)
137 fatal("Only one compression method can be specified", prog);
138 bzlib_compress = true;
139 compress_level = atoi(optarg);
140 break;
141 case 7:
142 if (zlib_compress || bzlib_compress || lzo_compress)
143 fatal("Only one compression method can be specified", prog);
144 lzo_compress = true;
145 break;
146 default:
147 usage(prog);
148 }
149 argc -= optind;
150 argv += optind;
151
152 if (argc != 2)
153 usage(prog);
154
155 /* Now open the dump image */
156 warnx("Starting %s", prog);
157 if(tapein.open(argv[0], O_RDONLY) != FakeTape::Response::OK) {
158 err(1, "%s does not exist.", argv[0]);
159 }
160 if(tapeout.open(argv[1], O_WRONLY|O_CREAT) != FakeTape::Response::OK) {
161 err(1, "%s can't be opened.", argv[1]);
162 }
163
164 std::vector<uint8_t> buffer(FakeTape::getMaxBlockSize());
165
166 bswap_context inctx;
167 bswap_context outctx;
168 outctx.from_cpu = bswap_output;
169
170 while (true) {
171 bswap_header(nullptr, 0, &inctx);
172 bswap_header(nullptr, 0, &outctx);
173 /* Read the first block */
174 size_t readlen;
175 if(tapein.readData(buffer.data(), buffer.size(), &readlen) != FakeTape::Response::OK)
176 err(1, "Failed to read first block from dump image");
177 if (readlen == 0)
178 break;
179
180 header* hdr = reinterpret_cast<header*>(buffer.data());
181
182 if (!checksum(hdr))
183 errx(1, "This doesn't appear to be a dump tape, or was written on a system with byteswapped values");
184
185 if (hdr->c_magic != NFS_MAGIC) {
186 inctx.to_cpu = true;
187 warnx("Input tape appears to be byteswapped");
188 }
189 bswap_header(buffer.data(), readlen, &inctx);
190
191 if (hdr->c_magic != NFS_MAGIC)
192 errx(1, "magic failure");
193
194 bool compressed = hdr->c_flags & DR_COMPRESSED;
195 uint32_t ntrec = hdr->c_ntrec;
196
197 if (zlib_compress || bzlib_compress || lzo_compress)
198 hdr->c_flags |= DR_COMPRESSED;
199 else
200 hdr->c_flags &= ~DR_COMPRESSED;
201 setchecksum(hdr);
202
203 std::cerr << "Tape has ntrec of " << ntrec << std::endl;
204
205 bswap_header(buffer.data(), readlen, &outctx);
206
207 if(tapeout.writeData(buffer.data(), readlen) != FakeTape::Response::OK)
208 err(1, "Failed to write first block to dump image");
209
210 std::optional<bool> demangle_bitfields;
211 while(true) {
212
213 if(tapein.readData(buffer.data(), buffer.size(), &readlen) != FakeTape::Response::OK)
214 err(1, "Failed to read block from dump image");
215 if (readlen == 0)
216 break;
217 uint32_t bsize = readlen;
218 uint32_t ctype = 0;
219 uint32_t cflag = 0;
220
221 if (compressed) {
222 if (inctx.to_cpu) {
223 auto data = buffer.data();
224 swap4(1, data);
225 }
226 uint32_t clen = *reinterpret_cast<uint32_t*>(buffer.data());
227 if (!demangle_bitfields.has_value()) {
228 if(clen>>31)
229 // Case 2b. Only way mc can be set.
230 demangle_bitfields = true;
231 /* blocks written without compression must be exactly ntrec*TP_BSIZE in size */
232 else if ((clen & 0xfffffff) != ntrec * TP_BSIZE)
233 // This covers case 2a and 1a. 2a because length will be odd when read mangled, and 1a because length will be 16x too big.
234 demangle_bitfields = false;
235 else
236 // Only case 1b remains.
237 demangle_bitfields = true;
238 if (*demangle_bitfields)
239 std::cerr << "Warning: Tape appears to have been written with different bitfield ordering" << std::endl;
240 }
241 if (*demangle_bitfields) {
242 bsize = clen & 0xfffffff;
243 ctype = (clen&0x70000000)>>28;
244 cflag = clen>>31;
245 } else {
246 bsize = clen >> 4;
247 ctype = (clen&0xe)>>1;
248 cflag = clen&1;
249 }
250 if (bsize == readlen) {
251 size_t extra;
252 if(tapein.readData(buffer.data() + readlen, 4, &extra) != FakeTape::Response::OK)
253 err(1, "Failed to read continuation block from dump image");
254 readlen += extra;
255 bswap_header(buffer.data()+4, readlen-4, &inctx);
256 } else if (bsize != readlen - 4) {
257 err(1, "Unexpected size of compressed block");
258 }
259 /* TODO check ctype and use correct decompressor */
260 if (cflag) {
261 switch(ctype) {
262 case COMPRESS_ZLIB: {
263 std::vector<uint8_t> cmprbuf(FakeTape::getMaxBlockSize());
264 std::swap(cmprbuf, buffer);
265 uLongf decmprlen = buffer.size();
266 int status = uncompress(buffer.data()+4, &decmprlen, cmprbuf.data()+4, bsize);
267 switch (status) {
268 case Z_OK: break;
269 case Z_MEM_ERROR: errx(1, "Memory error while decompressing");
270 case Z_BUF_ERROR: errx(1, "BUF error while decompressing");;
271 case Z_DATA_ERROR: errx(1, "Corrupt compressed data while decompressing");
272 default: errx(1, "Unexpected return from uncompress");
273 }
274 bsize = decmprlen;
275 break;
276 }
277 case COMPRESS_BZLIB: {
278 std::vector<uint8_t> cmprbuf(FakeTape::getMaxBlockSize());
279 std::swap(cmprbuf, buffer);
280 unsigned int decmprlen = buffer.size();
281 int status = BZ2_bzBuffToBuffDecompress((char*)buffer.data()+4, &decmprlen, (char*)cmprbuf.data()+4, bsize, 0, 0);
282
283 switch (status) {
284 case BZ_OK: break;
285 case BZ_MEM_ERROR: errx(1, "Memory error while decompressing");
286 case BZ_OUTBUFF_FULL: errx(1, "BUF error while decompressing");;
287 case BZ_DATA_ERROR:
288 case BZ_DATA_ERROR_MAGIC:
289 case BZ_UNEXPECTED_EOF:
290 errx(1, "Corrupt compressed data while decompressing");
291 default: errx(1, "Unexpected return from uncompress");
292 }
293 bsize = decmprlen;
294 break;
295 }
296 case COMPRESS_LZO: {
297 std::vector<uint8_t> cmprbuf(FakeTape::getMaxBlockSize());
298 std::swap(cmprbuf, buffer);
299 lzo_uint decmprlen = buffer.size();
300 int status = lzo1x_decompress(cmprbuf.data()+4, bsize, buffer.data()+4, &decmprlen, nullptr);
301
302 switch (status) {
303 case LZO_E_OK: break;
304 case LZO_E_ERROR: errx(1, "LZO error while decompressing");
305 case LZO_E_EOF_NOT_FOUND: errx(1, "LZO No EOF error while decompressing");;
306 default: errx(1, "Unexpected return from LZO uncompress");
307 }
308 bsize = decmprlen;
309 break;
310 }
311 }
312 }
313 bswap_header(buffer.data()+4, bsize, &inctx);
314 readlen = bsize + 4;
315 } else {
316 bswap_header(buffer.data(), readlen, &inctx);
317 std::memmove(buffer.data()+4, buffer.data(), readlen);
318 readlen += 4;
319 }
320 /* At this point we have uncompressed data with a compression header (header contains garbage) */
321 cflag = 0;
322 ctype = 0;
323 bswap_header(buffer.data()+4, readlen-4, &outctx);
324 if (zlib_compress) {
325 std::vector<uint8_t> cmprbuf(FakeTape::getMaxBlockSize());
326 std::swap(cmprbuf, buffer);
327 uLongf cmprlen = buffer.size();
328 int status = compress2(buffer.data()+4, &cmprlen, cmprbuf.data()+4, bsize, compress_level);
329 switch (status) {
330 case Z_OK: break;
331 case Z_MEM_ERROR: errx(1, "Memory error while compressing");
332 case Z_BUF_ERROR: errx(1, "BUF error while compressing");;
333 case Z_STREAM_ERROR: errx(1, "Stream error while compressing");
334 default: errx(1, "Unexpected return from compress");
335 }
336 if (cmprlen < bsize) {
337 bsize = cmprlen;
338 cflag = 1;
339 readlen = bsize + 4;
340 } else
341 std::swap(cmprbuf, buffer);
342 }
343 uint32_t clen;
344 if (mangle_bitfields)
345 clen = (cflag<<31) | (ctype<<28) | bsize;
346 else
347 clen = (bsize<<4) | (ctype<<1) | cflag;
348 *reinterpret_cast<uint32_t*>(buffer.data()) = clen;
349 if (outctx.from_cpu) {
350 auto data = buffer.data();
351 swap4(1, data);
352 }
353 if (zlib_compress || bzlib_compress || lzo_compress) {
354 if(tapeout.writeData(buffer.data(), readlen) != FakeTape::Response::OK)
355 err(1, "Write compressed block failed");
356 } else {
357 if(tapeout.writeData(buffer.data()+4, readlen-4) != FakeTape::Response::OK)
358 err(1, "Write uncompressed block failed");
359 }
360 }
361 if(tapeout.writeData(buffer.data(), 0) != FakeTape::Response::OK)
362 err(1, "Write EOF failed");
363 }
364
365 warnx("Finished");
366 }
367
368 /* vim: set sw=8 sts=8 ts=8 noexpandtab: */