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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/* Copyright (c) 2002-2016 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "str.h"
#include "ostream.h"
#include "array.h"
#include "buffer.h"
#include "sieve-common.h"
#include "sieve-extensions.h"
#include "sieve-dump.h"
#include "sieve-script.h"
#include "sieve-binary-private.h"
/*
* Binary dumper object
*/
struct sieve_binary_dumper {
pool_t pool;
/* Dumptime environment */
struct sieve_dumptime_env dumpenv;
};
struct sieve_binary_dumper *sieve_binary_dumper_create
(struct sieve_binary *sbin)
{
pool_t pool;
struct sieve_binary_dumper *dumper;
pool = pool_alloconly_create("sieve_binary_dumper", 4096);
dumper = p_new(pool, struct sieve_binary_dumper, 1);
dumper->pool = pool;
dumper->dumpenv.dumper = dumper;
dumper->dumpenv.sbin = sbin;
sieve_binary_ref(sbin);
dumper->dumpenv.svinst = sieve_binary_svinst(sbin);
return dumper;
}
void sieve_binary_dumper_free(struct sieve_binary_dumper **dumper)
{
sieve_binary_unref(&(*dumper)->dumpenv.sbin);
pool_unref(&((*dumper)->pool));
*dumper = NULL;
}
pool_t sieve_binary_dumper_pool(struct sieve_binary_dumper *dumper)
{
return dumper->pool;
}
/*
* Formatted output
*/
void sieve_binary_dumpf
(const struct sieve_dumptime_env *denv, const char *fmt, ...)
{
string_t *outbuf = t_str_new(128);
va_list args;
va_start(args, fmt);
str_vprintfa(outbuf, fmt, args);
va_end(args);
o_stream_send(denv->stream, str_data(outbuf), str_len(outbuf));
}
void sieve_binary_dump_sectionf
(const struct sieve_dumptime_env *denv, const char *fmt, ...)
{
string_t *outbuf = t_str_new(128);
va_list args;
va_start(args, fmt);
str_printfa(outbuf, "\n* ");
str_vprintfa(outbuf, fmt, args);
str_printfa(outbuf, ":\n\n");
va_end(args);
o_stream_send(denv->stream, str_data(outbuf), str_len(outbuf));
}
/*
* Dumping the binary
*/
bool sieve_binary_dumper_run
(struct sieve_binary_dumper *dumper, struct ostream *stream, bool verbose)
{
struct sieve_binary *sbin = dumper->dumpenv.sbin;
struct sieve_script *script = sieve_binary_script(sbin);
struct sieve_dumptime_env *denv = &(dumper->dumpenv);
struct sieve_binary_block *sblock;
bool success = TRUE;
sieve_size_t offset;
int count, i;
dumper->dumpenv.stream = stream;
/* Dump list of binary blocks */
if ( verbose ) {
count = sieve_binary_block_count(sbin);
sieve_binary_dump_sectionf
(denv, "Binary blocks (count: %d)", count);
for ( i = 0; i < count; i++ ) {
struct sieve_binary_block *sblock = sieve_binary_block_get(sbin, i);
sieve_binary_dumpf(denv,
"%3d: size: %"PRIuSIZE_T" bytes\n", i,
sieve_binary_block_get_size(sblock));
}
}
/* Dump script metadata */
sieve_binary_dump_sectionf
(denv, "Script metadata (block: %d)", SBIN_SYSBLOCK_SCRIPT_DATA);
sblock = sieve_binary_block_get(sbin, SBIN_SYSBLOCK_SCRIPT_DATA);
T_BEGIN {
offset = 0;
success = sieve_script_binary_dump_metadata
(script, denv, sblock, &offset);
} T_END;
if ( !success ) return FALSE;
/* Dump list of used extensions */
count = sieve_binary_extensions_count(sbin);
if ( count > 0 ) {
sieve_binary_dump_sectionf
(denv, "Required extensions (block: %d)", SBIN_SYSBLOCK_EXTENSIONS);
for ( i = 0; i < count; i++ ) {
const struct sieve_extension *ext = sieve_binary_extension_get_by_index
(sbin, i);
sblock = sieve_binary_extension_get_block(sbin, ext);
if ( sblock == NULL ) {
sieve_binary_dumpf(denv, "%3d: %s (id: %d)\n",
i, sieve_extension_name(ext), ext->id);
} else {
sieve_binary_dumpf(denv, "%3d: %s (id: %d; block: %d)\n",
i, sieve_extension_name(ext), ext->id,
sieve_binary_block_get_id(sblock));
}
}
}
/* Dump extension-specific elements of the binary */
count = sieve_binary_extensions_count(sbin);
if ( count > 0 ) {
for ( i = 0; i < count; i++ ) {
success = TRUE;
T_BEGIN {
const struct sieve_extension *ext = sieve_binary_extension_get_by_index
(sbin, i);
if ( ext->def != NULL && ext->def->binary_dump != NULL ) {
success = ext->def->binary_dump(ext, denv);
}
} T_END;
if ( !success ) return FALSE;
}
}
/* Dump main program */
sieve_binary_dump_sectionf
(denv, "Main program (block: %d)", SBIN_SYSBLOCK_MAIN_PROGRAM);
dumper->dumpenv.sblock =
sieve_binary_block_get(sbin, SBIN_SYSBLOCK_MAIN_PROGRAM);
dumper->dumpenv.cdumper = sieve_code_dumper_create(&(dumper->dumpenv));
if ( dumper->dumpenv.cdumper != NULL ) {
sieve_code_dumper_run(dumper->dumpenv.cdumper);
sieve_code_dumper_free(&dumper->dumpenv.cdumper);
}
/* Finish with empty line */
sieve_binary_dumpf(denv, "\n");
return TRUE;
}
/*
* Hexdump production
*/
void sieve_binary_dumper_hexdump
(struct sieve_binary_dumper *dumper, struct ostream *stream)
{
struct sieve_binary *sbin = dumper->dumpenv.sbin;
struct sieve_dumptime_env *denv = &(dumper->dumpenv);
int count, i;
dumper->dumpenv.stream = stream;
count = sieve_binary_block_count(sbin);
/* Block overview */
sieve_binary_dump_sectionf
(denv, "Binary blocks (count: %d)", count);
for ( i = 0; i < count; i++ ) {
struct sieve_binary_block *sblock = sieve_binary_block_get(sbin, i);
sieve_binary_dumpf(denv,
"%3d: size: %"PRIuSIZE_T" bytes\n", i,
sieve_binary_block_get_size(sblock));
}
/* Hexdump for each block */
for ( i = 0; i < count; i++ ) {
struct sieve_binary_block *sblock = sieve_binary_block_get(sbin, i);
buffer_t *blockbuf = sieve_binary_block_get_buffer(sblock);
string_t *line;
size_t data_size;
const unsigned char *data;
size_t offset;
data = buffer_get_data(blockbuf, &data_size);
// FIXME: calculate offset more nicely.
sieve_binary_dump_sectionf
(denv, "Block %d (%"PRIuSIZE_T" bytes, file offset %08llx)", i,
data_size, (unsigned long long int) sblock->offset + 8);
line = t_str_new(128);
offset = 0;
while ( offset < data_size ) {
size_t len = ( data_size - offset >= 16 ? 16 : data_size - offset );
size_t b;
str_printfa(line, "%08llx ", (unsigned long long) offset);
for ( b = 0; b < len; b++ ) {
str_printfa(line, "%02x ", data[offset+b]);
if ( b == 7 ) str_append_c(line, ' ');
}
if ( len < 16 ) {
if ( len <= 7 ) str_append_c(line, ' ');
for ( b = len; b < 16; b++ ) {
str_append(line, " ");
}
}
str_append(line, " |");
for ( b = 0; b < len; b++ ) {
const unsigned char c = data[offset+b];
if ( c >= 32 && c <= 126 )
str_append_c(line, (const char)c);
else
str_append_c(line, '.');
}
str_append(line, "|\n");
o_stream_send(stream, str_data(line), str_len(line));
str_truncate(line, 0);
offset += len;
}
str_printfa(line, "%08llx\n", (unsigned long long) offset);
o_stream_send(stream, str_data(line), str_len(line));
}
}
|