-
Notifications
You must be signed in to change notification settings - Fork 11
/
squat-dump.c
214 lines (177 loc) · 4.62 KB
/
squat-dump.c
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
/*
gcc -DHAVE_CONFIG_H -g -Wall squat-dump.c -o squat-dump ../../lib/liblib.a -I../../.. -I../../lib
*/
#include "lib.h"
#include "squat-trie-private.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define MAX_LEVEL 4
static void dump_header(const struct squat_trie_header *hdr)
{
printf("version: %u\n", hdr->version);
printf("uidvalidity: %u\n", hdr->uidvalidity);
printf("used_file_size: %u\n", hdr->used_file_size);
printf("deleted_space: %u\n", hdr->deleted_space);
printf("node_count: %u\n", hdr->node_count);
printf("modify_counter: %u\n", hdr->modify_counter);
printf("root_offset: %u\n", hdr->root_offset);
printf("\n");
}
static uint32_t unpack_num(const uint8_t **p, const uint8_t *end)
{
const uint8_t *c = *p;
uint32_t value = 0;
unsigned int bits = 0;
while (c != end && *c >= 0x80) {
value |= (*c & 0x7f) << bits;
bits += 7;
c++;
}
if (c == end) {
/* last number shouldn't end with high bit */
return 0;
}
if (bits > 32-7) {
/* we have only 32bit numbers */
return 0;
}
value |= (*c & 0x7f) << bits;
*p = c + 1;
return value;
}
static void indent(int level)
{
int i;
for (i = 0; i < (level-1) * 2; i++)
printf(" ");
}
static void iprintf(int level, const char *format, ...)
{
va_list args;
va_start(args, format);
indent(level);
vprintf(format, args);
va_end(args);
}
static const char *data_denormalize(int chr)
{
static char str[20];
if (chr == 0)
return " ";
str[1] = '\0';
chr += 32;
if (chr <= 'Z')
str[0] = chr;
else {
chr += 26;
if (chr < 128)
str[0] = chr;
else
snprintf(str, sizeof(str), "#%02x", chr);
}
return str;
}
static void dump_uidlist(const uint16_t *path, uint32_t uidlist)
{
int i;
iprintf(MAX_LEVEL+1, "path: ");
for (i = 0; i < MAX_LEVEL; i++)
printf("<%s>", data_denormalize(path[i]));
if (uidlist & 0x80000000)
printf(" => uid=%u\n", uidlist & ~0x80000000);
else
printf(" => uidlist=#%u\n", uidlist);
}
static void dump_tree(int fd, uoff_t offset, uint16_t *path, int level)
{
uint8_t buf[1 + 256*(sizeof(uint8_t) + sizeof(uint32_t)) + 8];
const uint8_t *p = buf, *end, *chars8;
const uint16_t *chars16 = NULL;
const uint32_t *idx8, *idx16 = NULL;
uint16_t my_path[MAX_LEVEL];
uint32_t num, chars8_count, chars16_count = 0, i;
ssize_t ret;
bool have_16bits, nonsorted;
if (level == MAX_LEVEL+1) {
/* uidlist */
dump_uidlist(path, offset);
return;
}
iprintf(level, "offset: %"PRIuUOFF_T"\n", offset);
iprintf(level, "path: [%d]: ", level);
for (i = 1; i < level; i++)
printf("<%s>", data_denormalize(path[i-1]));
printf("\n");
ret = pread(fd, buf, sizeof(buf), offset);
if (ret < 0)
i_fatal("read() failed at offset %"PRIuUOFF_T": %m", offset);
if (ret == 0)
i_fatal("ERROR: offset too large");
end = buf + ret;
num = unpack_num(&p, end);
have_16bits = (num & 1) != 0;
chars8_count = num >> 1;
if (chars8_count > 255)
i_fatal("ERROR: chars8_count too large");
iprintf(level, "chars8_count: %u\n", chars8_count);
if (p + chars8_count > end)
i_fatal("ERROR: chars8_count points outside file");
chars8 = p; nonsorted = FALSE;
iprintf(level, "chars8: ");
for (i = 0; i < chars8_count; i++) {
if (i > 0 && p[i-1] > p[i])
nonsorted = TRUE;
printf("%s", data_denormalize(p[i]));
}
if (nonsorted)
i_fatal("ERROR: chars8 not ordered");
printf("\n");
idx8 = (const uint32_t *)(p + chars8_count);
p = (const uint8_t *)(idx8 + chars8_count);
if (p > end)
i_fatal("ERROR: chars8_idx points outside file");
if (have_16bits) {
chars16_count = unpack_num(&p, end);
if ((size_t)p & 1) p++;
chars16 = (const uint16_t *)p;
iprintf(level, "chars16_count: %u\n", chars16_count);
if ((const uint8_t *)(chars16 + chars16_count) > end)
i_fatal("ERROR: chars16_count points outside file");
iprintf(level, "chars16: ");
for (i = 0; i < chars16_count; i++) {
printf("%s ", data_denormalize(p[i]));
}
printf("\n");
idx16 = (const uint32_t *)(chars16 + chars16_count);
}
for (i = 1; i < level; i++)
my_path[i-1] = path[i-1];
for (i = 0; i < chars8_count; i++) {
my_path[level-1] = chars8[i];
dump_tree(fd, idx8[i], my_path, level + 1);
}
for (i = 0; i < chars16_count; i++) {
my_path[level-1] = chars16[i];
dump_tree(fd, idx16[i], my_path, level + 1);
}
}
int main(int argc, char *argv[])
{
struct squat_trie_header hdr;
int fd;
ssize_t ret;
lib_init();
fd = open(argv[1], O_RDONLY);
if (fd == -1)
i_fatal("open(%s) failed: %m", argv[1]);
ret = read(fd, &hdr, sizeof(hdr));
if (ret < 0)
i_fatal("read() failed: %m");
if (ret != sizeof(hdr))
i_fatal("read(header) returned only %ld", ret);
dump_header(&hdr);
dump_tree(fd, hdr.root_offset, NULL, 1);
close(fd);
return 0;
}