| 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 | #include "config.h" // IWYU pragma: keep |
| 31 | #include "bswap_header.h" |
| 32 | |
| 33 | #include <inttypes.h> |
| 34 | #include <cstddef> |
| 35 | #include <cstring> |
| 36 | #include <iostream> |
| 37 | #include <err.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <endian.h> |
| 40 | #include <utility> |
| 41 | |
| 42 | #define CHECKSUM 84446 |
| 43 | |
| 44 | void swap1(int count, uint8_t*& data) { |
| 45 | data += count; |
| 46 | } |
| 47 | |
| 48 | void swap2(int count, uint8_t*& data) { |
| 49 | while(count--) { |
| 50 | std::swap(data[0], data[1]); |
| 51 | data += 2; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void swap4(int count, uint8_t*& data) { |
| 56 | while(count--) { |
| 57 | std::swap(data[0], data[3]); |
| 58 | std::swap(data[1], data[2]); |
| 59 | data += 4; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void swap8(int count, uint8_t*& data) { |
| 64 | while(count--) { |
| 65 | std::swap(data[0], data[7]); |
| 66 | std::swap(data[1], data[6]); |
| 67 | std::swap(data[2], data[5]); |
| 68 | std::swap(data[3], data[4]); |
| 69 | data += 8; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | uint32_t get_s_addr_length(const uint8_t* ptr) { |
| 74 | /* |
| 75 | * 1 - 64 encoded as 0-63 in n V[0] = (0nnnnnnf) (f is the data/sparse flag) |
| 76 | * if ( V[0] < 128 ) return (*V >> 1) + 1 ) |
| 77 | * 65 - 16384 encoded as 0-16319 in Nn V[0] = (1nnnnnnf) V[1] = (NNNNNNNN) |
| 78 | * if (V[0] >= 128) return V[1]*64 + ((V[0]>>1)&63) + 65; |
| 79 | * 128 values left unused for future special cases (V[1] = 255) 1nnnnnnn 11111111 |
| 80 | * if get_s_addr_length() >=65 then length is encoded in two bytes else in one. |
| 81 | */ |
| 82 | if (*ptr < 128) |
| 83 | return (*ptr>>1)+1; |
| 84 | return *(ptr+1)*64 + ((*ptr>>1)&63) + 65; |
| 85 | } |
| 86 | |
| 87 | void do_bswap_hdr(header* hdr) { |
| 88 | /** |
| 89 | * We are byteswapping a u_spcl.s_spcl here from dumprestore.h |
| 90 | 8 << swap 8 terms |
| 91 | i << c_type |
| 92 | c_date |
| 93 | c_ddate |
| 94 | c_volume |
| 95 | c_tapea_lo |
| 96 | c_inumber |
| 97 | c_magic |
| 98 | c_checksum |
| 99 | 4 << swap 4 terms |
| 100 | s di_mode (new_bsd_inode) |
| 101 | di_nlink |
| 102 | oldids[2] |
| 103 | 1 << swap 1 term |
| 104 | l di_size |
| 105 | 29 << swap 29 terms |
| 106 | i di_atime 0 |
| 107 | di_mtime 2 |
| 108 | di_ctime 4 |
| 109 | di_db[NDADDR] 6 |
| 110 | di_ib[NIADDR] 18 |
| 111 | di_flags 21 |
| 112 | di_blocks 22 |
| 113 | di_gen 23 |
| 114 | di_uid 24 |
| 115 | di_gid 25 |
| 116 | di_spare[2] 26 (end of new_bsd_inode) |
| 117 | c_count 29 |
| 118 | 528 << swap 528 terms |
| 119 | b c_data 0 |
| 120 | c_label 512 |
| 121 | i << Swap 1 term |
| 122 | c_level |
| 123 | 192 << swap 192 terms |
| 124 | b << c_filesys 0 |
| 125 | c_dev 64 |
| 126 | c_host 128 |
| 127 | 6 << swap 6 terms |
| 128 | i << c_flags |
| 129 | c_firstrec |
| 130 | c_ntrec |
| 131 | c_extattributes |
| 132 | c_tapea_hi |
| 133 | c_firstrec_hi |
| 134 | c_spare[28] not swapped. |
| 135 | */ |
| 136 | |
| 137 | uint8_t* data = reinterpret_cast<uint8_t*>(hdr); |
| 138 | swap4(8, data); //32 32 |
| 139 | swap2(4, data); //8 40 |
| 140 | swap8(1, data); //8 48 |
| 141 | swap4(29, data); //116 164 |
| 142 | swap1(528, data); //528 692 |
| 143 | swap4(1, data); //4 696 |
| 144 | swap1(192, data); //192 888 |
| 145 | swap4(6, data); //24 912 |
| 146 | //112 1024 |
| 147 | }; |
| 148 | |
| 149 | bool checksum(header* hdr) { |
| 150 | bool ok = false; |
| 151 | bool bswap = false; |
| 152 | if (hdr->c_magic != NFS_MAGIC) { |
| 153 | auto* data = reinterpret_cast<uint8_t*>(hdr); |
| 154 | swap4(sizeof *hdr / sizeof(uint32_t), data); |
| 155 | bswap = true; |
| 156 | } |
| 157 | if (hdr->c_magic == NFS_MAGIC) { |
| 158 | auto* lp = reinterpret_cast<const uint32_t *>(hdr); |
| 159 | uint32_t sum = 0; |
| 160 | uint32_t cnt = sizeof *hdr / sizeof *lp; |
| 161 | while (cnt--) |
| 162 | sum += *lp++; |
| 163 | ok = sum == CHECKSUM; |
| 164 | } |
| 165 | if(bswap) { |
| 166 | auto* data = reinterpret_cast<uint8_t*>(hdr); |
| 167 | swap4(sizeof *hdr / sizeof(uint32_t), data); |
| 168 | } |
| 169 | return ok; |
| 170 | } |
| 171 | |
| 172 | void setchecksum(header* hdr) { |
| 173 | bool bswap = false; |
| 174 | if (hdr->c_magic != NFS_MAGIC) { |
| 175 | auto* data = reinterpret_cast<uint8_t*>(hdr); |
| 176 | swap4(sizeof *hdr / sizeof(uint32_t), data); |
| 177 | bswap = true; |
| 178 | } |
| 179 | if (hdr->c_magic == NFS_MAGIC) { |
| 180 | uint32_t* lp = reinterpret_cast<uint32_t *>(hdr); |
| 181 | uint32_t sum = 0; |
| 182 | uint32_t cnt = sizeof *hdr / sizeof *lp; |
| 183 | hdr->c_checksum = 0; |
| 184 | while (cnt--) |
| 185 | sum += *lp++; |
| 186 | hdr->c_checksum = (uint32_t)CHECKSUM - sum; |
| 187 | } |
| 188 | if(bswap) { |
| 189 | auto* data = reinterpret_cast<uint8_t*>(hdr); |
| 190 | swap4(sizeof *hdr / sizeof(uint32_t), data); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void bswap_header(uint8_t* data, size_t bytes, bswap_context* ctx) { |
| 195 | if(!data || !bytes) { |
| 196 | if(ctx->seen_end) { |
| 197 | ctx->subcount = 0; |
| 198 | ctx->state = 0; |
| 199 | ctx->count = 0; |
| 200 | ctx->inode_is_directory = false; |
| 201 | ctx->inode_data_size = 0; |
| 202 | } else |
| 203 | ctx->new_vol = true; |
| 204 | return; |
| 205 | } |
| 206 | if (bytes % sizeof(header)) |
| 207 | errx(1, "Buffer is not a multiple of tape blocks in size"); |
| 208 | header* hdr = reinterpret_cast<header*>(data); |
| 209 | bytes /= sizeof *hdr; |
| 210 | |
| 211 | auto skip_holes = [&ctx]() { |
| 212 | while (ctx->count && !(*ctx->s_addr_ptr&1)) { |
| 213 | if(get_s_addr_length(ctx->s_addr_ptr) > 64) |
| 214 | ctx->s_addr_ptr += 2; |
| 215 | else |
| 216 | ctx->s_addr_ptr++; |
| 217 | ctx->count--; |
| 218 | } |
| 219 | if (!ctx->count) { |
| 220 | ctx->subcount = 0; |
| 221 | ctx->state = 0; |
| 222 | } else |
| 223 | ctx->subcount = get_s_addr_length(ctx->s_addr_ptr); |
| 224 | }; |
| 225 | |
| 226 | auto do_bswap_direct = [&]() { |
| 227 | uint8_t* data = reinterpret_cast<uint8_t*>(hdr); |
| 228 | size_t consumed = 0; |
| 229 | const direct* ptr = reinterpret_cast<const direct*>(data); |
| 230 | while(consumed < sizeof *hdr && ctx->inode_data_size) { |
| 231 | if (ctx->to_cpu) { |
| 232 | swap4(1, data); |
| 233 | swap2(1, data); |
| 234 | data -= 6; |
| 235 | } |
| 236 | // If to_cpu is set then the tape was written with a |
| 237 | // different endianness to us |
| 238 | // If tape was written in little endian then we have to |
| 239 | // byteswap the 2 byte length from oldinofmt |
| 240 | // TAPE HOST OUT to_cpu from_cpu |
| 241 | // L L L 10->01->10 L 0 0 |
| 242 | // L B L 10->01->10 L 1 1 |
| 243 | // L L B 10->01->01 S 0 1 |
| 244 | // L B B 10->01->01 S 1 0 |
| 245 | // B L L 01->01->10 S 1 0 |
| 246 | // B B L 01->01->10 S 0 1 |
| 247 | // B L B 01->01->01 L 1 1 |
| 248 | // B B B 01->01->01 L 0 0 |
| 249 | // |
| 250 | if (ctx->oldinofmt && ctx->to_cpu == (BYTE_ORDER == BIG_ENDIAN)) { |
| 251 | data += 6; |
| 252 | swap2(1, data); |
| 253 | data -= 8; |
| 254 | } |
| 255 | uint16_t reclen = ptr->d_reclen; |
| 256 | if (ctx->from_cpu) { |
| 257 | swap4(1, data); |
| 258 | swap2(1, data); |
| 259 | data -= 6; |
| 260 | } |
| 261 | if (ctx->oldinofmt && !ctx->to_host && ctx->from_cpu == (BYTE_ORDER == BIG_ENDIAN)) { |
| 262 | data += 6; |
| 263 | swap2(1, data); |
| 264 | data -= 8; |
| 265 | } |
| 266 | data += reclen; |
| 267 | consumed += reclen; |
| 268 | ctx->inode_data_size -= reclen; |
| 269 | ptr = reinterpret_cast<const direct*>(data); |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | auto process_block = [&]() { |
| 274 | switch(ctx->state) { |
| 275 | case 0: { |
| 276 | if (!checksum(hdr)) { |
| 277 | std::cerr << "SKIPPING UNKNOWN in bswap_header::process_block bad checksum" << std::endl; |
| 278 | hdr++; |
| 279 | bytes--; |
| 280 | ctx->subcount = 0; |
| 281 | ctx->count = 0; |
| 282 | ctx->inode_is_directory = false; |
| 283 | ctx->inode_data_size = 0; |
| 284 | break; |
| 285 | } |
| 286 | if (ctx->to_cpu) |
| 287 | do_bswap_hdr(hdr); |
| 288 | if (hdr->c_magic != NFS_MAGIC) { |
| 289 | std::cerr << "SKIPPING UNKNOWN in bswap_header::process_block bad magic" << std::endl; |
| 290 | hdr++; |
| 291 | bytes--; |
| 292 | break; |
| 293 | } |
| 294 | ctx->seen_end = false; |
| 295 | switch (hdr->c_type) { |
| 296 | case TS_END: |
| 297 | ctx->seen_end = true; |
| 298 | [[fallthrough]]; |
| 299 | case TS_TAPE: { |
| 300 | uint8_t* data = reinterpret_cast<uint8_t*>(hdr->c_data.s_inos); |
| 301 | if (ctx->to_cpu || ctx->from_cpu) |
| 302 | swap4(128, data); |
| 303 | break; |
| 304 | } |
| 305 | case TS_CLRI: |
| 306 | ctx->count = hdr->c_count; |
| 307 | ctx->state = ctx->count?TS_CLRI:0; |
| 308 | break; |
| 309 | case TS_BITS: |
| 310 | ctx->count = hdr->c_count; |
| 311 | ctx->state = ctx->count?TS_BITS:0; |
| 312 | break; |
| 313 | case TS_INODE: |
| 314 | ctx->inode_is_directory = !(hdr->c_flags & DR_EXTATTRIBUTES) && (hdr->c_dinode.di_mode & S_IFMT) == S_IFDIR; |
| 315 | ctx->inode_data_size = hdr->c_dinode.di_size; |
| 316 | ctx->oldinofmt = !(hdr->c_flags & DR_NEWINODEFMT); |
| 317 | [[fallthrough]]; |
| 318 | case TS_ADDR: |
| 319 | /* TODO - there was a problem with EAs sometimes not having s_addr populated properly */ |
| 320 | if (hdr->c_flags & DR_EXTATTRIBUTES) { |
| 321 | /* |
| 322 | * ugly hack: cope with invalid spcl.c_addr[] records written by |
| 323 | * old versions of dump (versions 0.4b40 through 0.4b42) |
| 324 | */ |
| 325 | for(int i = 0; i<hdr->c_count; ++i) |
| 326 | if(hdr->c_data.s_addrs[i] >= 2) |
| 327 | break; /* must be 0.4b49 or later */ |
| 328 | else if(hdr->c_data.s_addrs[i] == 0) { |
| 329 | std::cerr << "Fixing up invalid c_addr record for xattrs (v0.4b40-0.4b42 bug)\n"; |
| 330 | for(int i = 0; i<hdr->c_count; ++i) |
| 331 | hdr->c_data.s_addrs[i] = 1; |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | ctx->count = hdr->c_count; |
| 336 | std::memcpy(ctx->s_addr, hdr->c_data.s_addrs, 512); |
| 337 | ctx->s_addr_ptr = ctx->s_addr; |
| 338 | skip_holes(); |
| 339 | if (ctx->count) |
| 340 | ctx->state = TS_INODE; |
| 341 | break; |
| 342 | default: |
| 343 | errx(1, "unknown state on tape"); |
| 344 | break; |
| 345 | } |
| 346 | if (ctx->from_cpu) |
| 347 | do_bswap_hdr(hdr); |
| 348 | setchecksum(hdr); |
| 349 | hdr++; |
| 350 | bytes--; |
| 351 | break; |
| 352 | } |
| 353 | case TS_INODE: |
| 354 | if (ctx->inode_is_directory) |
| 355 | do_bswap_direct(); |
| 356 | hdr++; |
| 357 | bytes--; |
| 358 | if(--ctx->subcount == 0) { |
| 359 | if(--ctx->count == 0) |
| 360 | ctx->state = 0; |
| 361 | else { |
| 362 | if(get_s_addr_length(ctx->s_addr_ptr) > 64) |
| 363 | ctx->s_addr_ptr += 2; |
| 364 | else |
| 365 | ctx->s_addr_ptr++; |
| 366 | } |
| 367 | skip_holes(); |
| 368 | } |
| 369 | break; |
| 370 | case TS_BITS: |
| 371 | case TS_CLRI: |
| 372 | hdr++; |
| 373 | bytes--; |
| 374 | if(--ctx->count == 0) |
| 375 | ctx->state = 0; |
| 376 | break; |
| 377 | default: |
| 378 | errx(1, "unexpected state while reading tape"); |
| 379 | break; |
| 380 | } |
| 381 | }; |
| 382 | if (ctx->new_vol) { |
| 383 | auto save_hdr = hdr; |
| 384 | int saved_state = ctx->state; |
| 385 | ctx->state = 0; |
| 386 | if (ctx->to_cpu) |
| 387 | process_block(); |
| 388 | if (save_hdr->c_magic != NFS_MAGIC || !checksum(save_hdr) || save_hdr->c_type != TS_TAPE) { |
| 389 | std::cerr << "EXPECTED TS_TAPE at start of volume save_hdr->c_magic=" << std::hex << save_hdr->c_magic << std::endl; |
| 390 | } else if (!ctx->to_cpu) |
| 391 | process_block(); |
| 392 | ctx->state = saved_state; |
| 393 | ctx->new_vol = false; |
| 394 | } |
| 395 | while (bytes) |
| 396 | process_block(); |
| 397 | } |
| 398 | |
| 399 | /* vim: set sw=8 sts=8 ts=8 noexpandtab: */ |