Debian Codegraph @

faketape/bswap_header.h

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 #pragma once
31
32 #include <inttypes.h>
33 #include <cstddef>
34
35 /* Record types */
36 #define TS_TAPE 1 /* dump tape header */
37 #define TS_INODE 2 /* beginning of file record */
38 #define TS_BITS 3 /* map of inodes on tape */
39 #define TS_ADDR 4 /* continuation of file record */
40 #define TS_END 5 /* end of volume marker */
41 #define TS_CLRI 6 /* map of inodes deleted since last dump */
42
43 /* flags */
44 #define DR_NEWHEADER 0x0001 /* new format tape header */
45 #define DR_NEWINODEFMT 0x0002 /* new format inodes on tape */
46 #define DR_COMPRESSED 0x0080 /* dump tape is compressed */
47 #define DR_METAONLY 0x0100 /* only the metadata of the inode has
48 * been dumped */
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]; /* 1 => data; 0 => hole in inode */
89 dump_ino_t s_inos[TP_NINOS]; /* table of first inode on each volume */
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; /* record type (see below) */
98 int32_t c_date; /* date of this dump */
99 int32_t c_ddate; /* date of previous dump */
100 int32_t c_volume; /* dump volume number */
101 uint32_t c_tapea_lo; /* logical block of this record */
102 dump_ino_t c_inumber; /* number of inode */
103 int32_t c_magic; /* magic number (see above) */
104 int32_t c_checksum; /* record checksum */
105 new_bsd_inode c_dinode;
106 int32_t c_count; /* number of valid c_addr entries */
107 union u_data c_data; /* see above */
108 char c_label[LBLSIZE]; /* dump label */
109 int32_t c_level; /* level of this dump */
110 char c_filesys[NAMELEN]; /* name of dumpped file system */
111 char c_dev[NAMELEN]; /* name of dumpped device */
112 char c_host[NAMELEN]; /* name of dumpped host */
113 int32_t c_flags; /* additional information */
114 uint32_t c_firstrec_lo; /* first record on volume */
115 int32_t c_ntrec; /* blocksize on volume */
116 int32_t c_extattributes; /* additional inode info */
117 uint32_t c_tapea_hi; /* top 32 bits of c_tapea */
118 uint32_t c_firstrec_hi; /* top 32 bits of c_firstrec */
119 int32_t c_spare[28]; /* reserved for future uses */
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 /* vim: set sw=8 sts=8 ts=8 noexpandtab: */