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