| 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 |
#pragma once |
| 31 |
|
| 32 |
#include <inttypes.h> |
| 33 |
#include <cstddef> |
| 34 |
|
| 35 |
|
| 36 |
#define TS_TAPE 1 |
| 37 |
#define TS_INODE 2 |
| 38 |
#define TS_BITS 3 |
| 39 |
#define TS_ADDR 4 |
| 40 |
#define TS_END 5 |
| 41 |
#define TS_CLRI 6 |
| 42 |
|
| 43 |
|
| 44 |
#define DR_NEWHEADER 0x0001 |
| 45 |
#define DR_NEWINODEFMT 0x0002 |
| 46 |
#define DR_COMPRESSED 0x0080 |
| 47 |
#define DR_METAONLY 0x0100 |
| 48 |
|
| 49 |
#define DR_EXTATTRIBUTES 0x8000 |
| 50 |
|
| 51 |
typedef uint32_t dump_ino_t; |
| 52 |
|
| 53 |
struct bsdtimeval { |
| 54 |
uint32_t tv_sec; |
| 55 |
uint32_t tv_usec; |
| 56 |
}; |
| 57 |
|
| 58 |
#define NDADDR 12 |
| 59 |
#define NIADDR 3 |
| 60 |
|
| 61 |
struct new_bsd_inode { |
| 62 |
uint16_t di_mode; |
| 63 |
int16_t di_nlink; |
| 64 |
union { |
| 65 |
uint16_t oldids[2]; |
| 66 |
uint32_t inumber; |
| 67 |
} di_u; |
| 68 |
uint64_t di_size; |
| 69 |
bsdtimeval di_atime; |
| 70 |
bsdtimeval di_mtime; |
| 71 |
bsdtimeval di_ctime; |
| 72 |
uint32_t di_db[NDADDR]; |
| 73 |
uint32_t di_ib[NIADDR]; |
| 74 |
uint32_t di_flags; |
| 75 |
int32_t di_blocks; |
| 76 |
int32_t di_gen; |
| 77 |
uint32_t di_uid; |
| 78 |
uint32_t di_gid; |
| 79 |
int32_t di_spare[2]; |
| 80 |
}; |
| 81 |
static_assert(sizeof(new_bsd_inode) == 128); |
| 82 |
|
| 83 |
#define TP_BSIZE 1024 |
| 84 |
#define TP_NINDIR (TP_BSIZE/2) |
| 85 |
#define TP_NINOS (TP_NINDIR / sizeof (dump_ino_t)) |
| 86 |
|
| 87 |
extern union u_data { |
| 88 |
char s_addrs[TP_NINDIR]; |
| 89 |
dump_ino_t s_inos[TP_NINOS]; |
| 90 |
} u_data; |
| 91 |
static_assert(sizeof(u_data) == 512); |
| 92 |
|
| 93 |
#define LBLSIZE 16 |
| 94 |
#define NAMELEN 64 |
| 95 |
|
| 96 |
struct header { |
| 97 |
int32_t c_type; |
| 98 |
int32_t c_date; |
| 99 |
int32_t c_ddate; |
| 100 |
int32_t c_volume; |
| 101 |
uint32_t c_tapea_lo; |
| 102 |
dump_ino_t c_inumber; |
| 103 |
int32_t c_magic; |
| 104 |
int32_t c_checksum; |
| 105 |
new_bsd_inode c_dinode; |
| 106 |
int32_t c_count; |
| 107 |
union u_data c_data; |
| 108 |
char c_label[LBLSIZE]; |
| 109 |
int32_t c_level; |
| 110 |
char c_filesys[NAMELEN]; |
| 111 |
char c_dev[NAMELEN]; |
| 112 |
char c_host[NAMELEN]; |
| 113 |
int32_t c_flags; |
| 114 |
uint32_t c_firstrec_lo; |
| 115 |
int32_t c_ntrec; |
| 116 |
int32_t c_extattributes; |
| 117 |
uint32_t c_tapea_hi; |
| 118 |
uint32_t c_firstrec_hi; |
| 119 |
int32_t c_spare[28]; |
| 120 |
}; |
| 121 |
static_assert(sizeof(header) == 1024); |
| 122 |
|
| 123 |
struct direct { |
| 124 |
uint32_t d_ino; |
| 125 |
uint16_t d_reclen; |
| 126 |
uint8_t d_type; |
| 127 |
uint8_t d_namlen; |
| 128 |
char d_name[0]; |
| 129 |
}; |
| 130 |
static_assert(sizeof(direct) == 8); |
| 131 |
|
| 132 |
#define NFS_MAGIC 60012 |
| 133 |
|
| 134 |
struct bswap_context { |
| 135 |
bool to_cpu = false; |
| 136 |
bool from_cpu = false; |
| 137 |
bool to_host = false; |
| 138 |
uint8_t s_addr[512] = {0}; |
| 139 |
uint8_t* s_addr_ptr = 0; |
| 140 |
uint32_t subcount = 0; |
| 141 |
int state = 0; |
| 142 |
size_t count = 0; |
| 143 |
bool inode_is_directory = false; |
| 144 |
uint64_t inode_data_size = 0; |
| 145 |
bool seen_end = true; |
| 146 |
bool new_vol = false; |
| 147 |
bool oldinofmt = false; |
| 148 |
dump_ino_t inode_num; |
| 149 |
}; |
| 150 |
|
| 151 |
extern uint32_t get_s_addr_length(const uint8_t* ptr); |
| 152 |
extern bool checksum(header* hdr); |
| 153 |
extern void setchecksum(header* hdr); |
| 154 |
extern void bswap_header(uint8_t* data, size_t bytes, bswap_context* context); |
| 155 |
extern void swap1(int count, uint8_t*& data); |
| 156 |
extern void swap2(int count, uint8_t*& data); |
| 157 |
extern void swap4(int count, uint8_t*& data); |
| 158 |
extern void swap8(int count, uint8_t*& data); |
| 159 |
|
| 160 |
extern void do_bswap_hdr(header* hdr); |
| 161 |
|
| 162 |
template<typename T> T tohost(const T& v) { |
| 163 |
T n(v); |
| 164 |
#if BYTE_ORDER == BIG_ENDIAN |
| 165 |
auto np = reinterpret_cast<uint8_t*>(&n); |
| 166 |
if constexpr (sizeof(T) == 8) { |
| 167 |
swap8(1, np); |
| 168 |
} else if constexpr (sizeof(T) == 4) { |
| 169 |
swap4(1, np); |
| 170 |
} else if constexpr (sizeof(T) == 2) { |
| 171 |
swap2(1, np); |
| 172 |
} else if constexpr (sizeof(T) == 1) { |
| 173 |
swap1(1, np); |
| 174 |
} |
| 175 |
#endif |
| 176 |
return n; |
| 177 |
} |
| 178 |
|
| 179 |
|