/src/pigeonhole/src/lib-sieve/sieve-binary-dumper.c
Line | Count | Source |
1 | | /* Copyright (c) Pigeonhole authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "str.h" |
5 | | #include "ostream.h" |
6 | | #include "array.h" |
7 | | #include "buffer.h" |
8 | | #include "time-util.h" |
9 | | |
10 | | #include "sieve-common.h" |
11 | | #include "sieve-extensions.h" |
12 | | #include "sieve-dump.h" |
13 | | #include "sieve-script.h" |
14 | | |
15 | | #include "sieve-binary-private.h" |
16 | | |
17 | | /* |
18 | | * Binary dumper object |
19 | | */ |
20 | | |
21 | | struct sieve_binary_dumper { |
22 | | pool_t pool; |
23 | | |
24 | | /* Dumptime environment */ |
25 | | struct sieve_dumptime_env dumpenv; |
26 | | }; |
27 | | |
28 | | struct sieve_binary_dumper * |
29 | | sieve_binary_dumper_create(struct sieve_binary *sbin) |
30 | 0 | { |
31 | 0 | pool_t pool; |
32 | 0 | struct sieve_binary_dumper *dumper; |
33 | |
|
34 | 0 | pool = pool_alloconly_create("sieve_binary_dumper", 4096); |
35 | 0 | dumper = p_new(pool, struct sieve_binary_dumper, 1); |
36 | 0 | dumper->pool = pool; |
37 | 0 | dumper->dumpenv.dumper = dumper; |
38 | |
|
39 | 0 | dumper->dumpenv.sbin = sbin; |
40 | 0 | sieve_binary_ref(sbin); |
41 | |
|
42 | 0 | dumper->dumpenv.svinst = sieve_binary_svinst(sbin); |
43 | |
|
44 | 0 | return dumper; |
45 | 0 | } |
46 | | |
47 | | void sieve_binary_dumper_free(struct sieve_binary_dumper **dumper) |
48 | 0 | { |
49 | 0 | sieve_binary_unref(&(*dumper)->dumpenv.sbin); |
50 | 0 | pool_unref(&((*dumper)->pool)); |
51 | |
|
52 | 0 | *dumper = NULL; |
53 | 0 | } |
54 | | |
55 | | pool_t sieve_binary_dumper_pool(struct sieve_binary_dumper *dumper) |
56 | 0 | { |
57 | 0 | return dumper->pool; |
58 | 0 | } |
59 | | |
60 | | /* |
61 | | * Formatted output |
62 | | */ |
63 | | |
64 | | void sieve_binary_dumpf(const struct sieve_dumptime_env *denv, |
65 | | const char *fmt, ...) |
66 | 0 | { |
67 | 0 | string_t *outbuf = t_str_new(128); |
68 | 0 | va_list args; |
69 | |
|
70 | 0 | va_start(args, fmt); |
71 | 0 | str_vprintfa(outbuf, fmt, args); |
72 | 0 | va_end(args); |
73 | |
|
74 | 0 | o_stream_nsend(denv->stream, str_data(outbuf), str_len(outbuf)); |
75 | 0 | } |
76 | | |
77 | | void sieve_binary_dump_sectionf(const struct sieve_dumptime_env *denv, |
78 | | const char *fmt, ...) |
79 | 0 | { |
80 | 0 | string_t *outbuf = t_str_new(128); |
81 | 0 | va_list args; |
82 | |
|
83 | 0 | va_start(args, fmt); |
84 | 0 | str_printfa(outbuf, "\n* "); |
85 | 0 | str_vprintfa(outbuf, fmt, args); |
86 | 0 | str_printfa(outbuf, ":\n\n"); |
87 | 0 | va_end(args); |
88 | |
|
89 | 0 | o_stream_nsend(denv->stream, str_data(outbuf), str_len(outbuf)); |
90 | 0 | } |
91 | | |
92 | | /* |
93 | | * Dumping the binary |
94 | | */ |
95 | | |
96 | | bool sieve_binary_dumper_run(struct sieve_binary_dumper *dumper, |
97 | | struct ostream *stream, bool verbose) |
98 | 0 | { |
99 | 0 | struct sieve_binary *sbin = dumper->dumpenv.sbin; |
100 | 0 | struct sieve_script *script = sieve_binary_script(sbin); |
101 | 0 | struct sieve_dumptime_env *denv = &(dumper->dumpenv); |
102 | 0 | const struct sieve_binary_header *header = &sbin->header; |
103 | 0 | struct sieve_binary_block *sblock; |
104 | 0 | bool success = TRUE; |
105 | 0 | sieve_size_t offset; |
106 | 0 | int count, i; |
107 | |
|
108 | 0 | dumper->dumpenv.stream = stream; |
109 | | |
110 | | /* Dump header */ |
111 | |
|
112 | 0 | sieve_binary_dump_sectionf(denv, "Header"); |
113 | |
|
114 | 0 | sieve_binary_dumpf(denv, |
115 | 0 | "version = %"PRIu16".%"PRIu16"\n" |
116 | 0 | "flags = 0x%08"PRIx32"\n", |
117 | 0 | header->version_major, header->version_minor, |
118 | 0 | header->flags); |
119 | 0 | if (header->resource_usage.update_time != 0) { |
120 | 0 | time_t update_time = |
121 | 0 | (time_t)header->resource_usage.update_time; |
122 | 0 | sieve_binary_dumpf(denv, |
123 | 0 | "resource usage:\n" |
124 | 0 | " update time = %s\n" |
125 | 0 | " cpu time = %"PRIu32" ms\n", |
126 | 0 | t_strflocaltime("%Y-%m-%d %H:%M:%S", |
127 | 0 | update_time), |
128 | 0 | header->resource_usage.cpu_time_msecs); |
129 | 0 | } |
130 | | |
131 | | /* Dump list of binary blocks */ |
132 | |
|
133 | 0 | if (verbose) { |
134 | 0 | count = sieve_binary_block_count(sbin); |
135 | |
|
136 | 0 | sieve_binary_dump_sectionf(denv, "Binary blocks (count: %d)", |
137 | 0 | count); |
138 | |
|
139 | 0 | for (i = 0; i < count; i++) { |
140 | 0 | struct sieve_binary_block *sblock = |
141 | 0 | sieve_binary_block_get(sbin, i); |
142 | |
|
143 | 0 | if (sblock == NULL) |
144 | 0 | return FALSE; |
145 | | |
146 | 0 | sieve_binary_dumpf( |
147 | 0 | denv, "%3d: size: %zu bytes\n", |
148 | 0 | i, sieve_binary_block_get_size(sblock)); |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | | /* Dump script metadata */ |
153 | | |
154 | 0 | sieve_binary_dump_sectionf(denv, "Script metadata (block: %d)", |
155 | 0 | SBIN_SYSBLOCK_SCRIPT_DATA); |
156 | 0 | sblock = sieve_binary_block_get(sbin, SBIN_SYSBLOCK_SCRIPT_DATA); |
157 | 0 | if (sblock == NULL) |
158 | 0 | return FALSE; |
159 | | |
160 | 0 | T_BEGIN { |
161 | 0 | offset = 0; |
162 | 0 | success = sieve_script_binary_dump_metadata( |
163 | 0 | script, denv, sblock, &offset); |
164 | 0 | } T_END; |
165 | | |
166 | 0 | if (!success) |
167 | 0 | return FALSE; |
168 | | |
169 | | /* Dump list of used extensions */ |
170 | | |
171 | 0 | count = sieve_binary_extensions_count(sbin); |
172 | 0 | if (count > 0) { |
173 | 0 | sieve_binary_dump_sectionf( |
174 | 0 | denv, "Required extensions (block: %d)", |
175 | 0 | SBIN_SYSBLOCK_EXTENSIONS); |
176 | |
|
177 | 0 | for (i = 0; i < count; i++) { |
178 | 0 | const struct sieve_extension *ext = |
179 | 0 | sieve_binary_extension_get_by_index(sbin, i); |
180 | |
|
181 | 0 | sblock = sieve_binary_extension_get_block(sbin, ext); |
182 | |
|
183 | 0 | if (sblock == NULL) { |
184 | 0 | sieve_binary_dumpf( |
185 | 0 | denv, "%3d: %s (id: %d)\n", |
186 | 0 | i, sieve_extension_name(ext), ext->id); |
187 | 0 | } else { |
188 | 0 | sieve_binary_dumpf( |
189 | 0 | denv, "%3d: %s (id: %d; block: %d)\n", |
190 | 0 | i, sieve_extension_name(ext), ext->id, |
191 | 0 | sieve_binary_block_get_id(sblock)); |
192 | 0 | } |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | | /* Dump extension-specific elements of the binary */ |
197 | |
|
198 | 0 | count = sieve_binary_extensions_count(sbin); |
199 | 0 | if (count > 0) { |
200 | 0 | for (i = 0; i < count; i++) { |
201 | 0 | success = TRUE; |
202 | |
|
203 | 0 | T_BEGIN { |
204 | 0 | const struct sieve_extension *ext = |
205 | 0 | sieve_binary_extension_get_by_index( |
206 | 0 | sbin, i); |
207 | |
|
208 | 0 | if (ext->def != NULL && |
209 | 0 | ext->def->binary_dump != NULL) { |
210 | 0 | success = ext->def->binary_dump( |
211 | 0 | ext, denv); |
212 | 0 | } |
213 | 0 | } T_END; |
214 | | |
215 | 0 | if (!success) |
216 | 0 | return FALSE; |
217 | 0 | } |
218 | 0 | } |
219 | | |
220 | | /* Dump main program */ |
221 | | |
222 | 0 | sieve_binary_dump_sectionf(denv, "Main program (block: %d)", |
223 | 0 | SBIN_SYSBLOCK_MAIN_PROGRAM); |
224 | |
|
225 | 0 | dumper->dumpenv.sblock = |
226 | 0 | sieve_binary_block_get(sbin, SBIN_SYSBLOCK_MAIN_PROGRAM); |
227 | 0 | if (dumper->dumpenv.sblock == NULL) |
228 | 0 | return FALSE; |
229 | | |
230 | 0 | dumper->dumpenv.cdumper = sieve_code_dumper_create(&(dumper->dumpenv)); |
231 | |
|
232 | 0 | if (dumper->dumpenv.cdumper != NULL) { |
233 | 0 | sieve_code_dumper_run(dumper->dumpenv.cdumper); |
234 | |
|
235 | 0 | sieve_code_dumper_free(&dumper->dumpenv.cdumper); |
236 | 0 | } |
237 | | |
238 | | /* Finish with empty line */ |
239 | 0 | sieve_binary_dumpf(denv, "\n"); |
240 | |
|
241 | 0 | return TRUE; |
242 | 0 | } |
243 | | |
244 | | /* |
245 | | * Hexdump production |
246 | | */ |
247 | | |
248 | | bool sieve_binary_dumper_hexdump(struct sieve_binary_dumper *dumper, |
249 | | struct ostream *stream) |
250 | 0 | { |
251 | 0 | struct sieve_binary *sbin = dumper->dumpenv.sbin; |
252 | 0 | struct sieve_dumptime_env *denv = &(dumper->dumpenv); |
253 | 0 | int count, i; |
254 | |
|
255 | 0 | dumper->dumpenv.stream = stream; |
256 | |
|
257 | 0 | count = sieve_binary_block_count(sbin); |
258 | | |
259 | | /* Block overview */ |
260 | |
|
261 | 0 | sieve_binary_dump_sectionf(denv, "Binary blocks (count: %d)", count); |
262 | |
|
263 | 0 | for (i = 0; i < count; i++) { |
264 | 0 | struct sieve_binary_block *sblock = |
265 | 0 | sieve_binary_block_get(sbin, i); |
266 | |
|
267 | 0 | if (sblock == NULL) |
268 | 0 | return FALSE; |
269 | | |
270 | 0 | sieve_binary_dumpf(denv, "%3d: size: %zu bytes\n", |
271 | 0 | i, sieve_binary_block_get_size(sblock)); |
272 | 0 | } |
273 | | |
274 | | /* Hexdump for each block */ |
275 | | |
276 | 0 | for (i = 0; i < count; i++) { |
277 | 0 | struct sieve_binary_block *sblock = |
278 | 0 | sieve_binary_block_get(sbin, i); |
279 | 0 | buffer_t *blockbuf = sieve_binary_block_get_buffer(sblock); |
280 | 0 | string_t *line; |
281 | 0 | size_t data_size; |
282 | 0 | const unsigned char *data; |
283 | 0 | size_t offset; |
284 | |
|
285 | 0 | data = buffer_get_data(blockbuf, &data_size); |
286 | | |
287 | | // FIXME: calculate offset more nicely. |
288 | 0 | sieve_binary_dump_sectionf( |
289 | 0 | denv, "Block %d (%zu bytes, file offset %08llx)", i, data_size, |
290 | 0 | (unsigned long long int)sblock->offset + 8); |
291 | |
|
292 | 0 | line = t_str_new(128); |
293 | 0 | offset = 0; |
294 | 0 | while (offset < data_size) { |
295 | 0 | size_t len = (data_size - offset >= 16 ? |
296 | 0 | 16 : data_size - offset); |
297 | 0 | size_t b; |
298 | |
|
299 | 0 | str_printfa(line, "%08llx ", |
300 | 0 | (unsigned long long)offset); |
301 | |
|
302 | 0 | for (b = 0; b < len; b++) { |
303 | 0 | str_printfa(line, "%02x ", data[offset+b]); |
304 | 0 | if (b == 7) |
305 | 0 | str_append_c(line, ' '); |
306 | 0 | } |
307 | |
|
308 | 0 | if (len < 16) { |
309 | 0 | if (len <= 7) |
310 | 0 | str_append_c(line, ' '); |
311 | |
|
312 | 0 | for (b = len; b < 16; b++) |
313 | 0 | str_append(line, " "); |
314 | 0 | } |
315 | |
|
316 | 0 | str_append(line, " |"); |
317 | |
|
318 | 0 | for (b = 0; b < len; b++) { |
319 | 0 | const unsigned char c = data[offset+b]; |
320 | |
|
321 | 0 | if (c >= 32 && c <= 126) |
322 | 0 | str_append_c(line, (const char)c); |
323 | 0 | else |
324 | 0 | str_append_c(line, '.'); |
325 | 0 | } |
326 | |
|
327 | 0 | str_append(line, "|\n"); |
328 | 0 | o_stream_nsend(stream, str_data(line), str_len(line)); |
329 | 0 | str_truncate(line, 0); |
330 | 0 | offset += len; |
331 | 0 | } |
332 | |
|
333 | 0 | str_printfa(line, "%08llx\n", (unsigned long long)offset); |
334 | 0 | o_stream_nsend(stream, str_data(line), str_len(line)); |
335 | 0 | } |
336 | 0 | return TRUE; |
337 | 0 | } |