summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/lib-sieve/sieve-binary.h
blob: 0b8b66e8efd5927984edcdc16c1169333c735341 (plain)
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
#ifndef SIEVE_BINARY_H
#define SIEVE_BINARY_H

#include "lib.h"

#include "sieve-common.h"

/*
 * Config
 */

#define SIEVE_BINARY_VERSION_MAJOR     2
#define SIEVE_BINARY_VERSION_MINOR     0

#define SIEVE_BINARY_BASE_HEADER_SIZE  20

/*
 * Binary object
 */

struct sieve_binary;

struct sieve_binary *sieve_binary_create_new(struct sieve_script *script);
void sieve_binary_ref(struct sieve_binary *sbin);
void sieve_binary_unref(struct sieve_binary **_sbin);

void sieve_binary_close(struct sieve_binary **_sbin);

/*
 * Resource usage
 */

void sieve_binary_get_resource_usage(struct sieve_binary *sbin,
				     struct sieve_resource_usage *rusage_r);
bool sieve_binary_record_resource_usage(
	struct sieve_binary *sbin, const struct sieve_resource_usage *rusage)
	ATTR_NULL(1);
void sieve_binary_set_resource_usage(struct sieve_binary *sbin,
				     const struct sieve_resource_usage *rusage);
/*
 * Accessors
 */

pool_t sieve_binary_pool(struct sieve_binary *sbin);
struct sieve_instance *sieve_binary_svinst(struct sieve_binary *sbin);
const char *sieve_binary_path(struct sieve_binary *sbin);
struct sieve_script *sieve_binary_script(struct sieve_binary *sbin);

time_t sieve_binary_mtime(struct sieve_binary *sbin);
const struct stat *sieve_binary_stat(struct sieve_binary *sbin);

const char *sieve_binary_script_name(struct sieve_binary *sbin);
const char *sieve_binary_script_location(struct sieve_binary *sbin);

const char *sieve_binary_source(struct sieve_binary *sbin);
bool sieve_binary_loaded(struct sieve_binary *sbin);
bool sieve_binary_saved(struct sieve_binary *sbin);

/*
 * Utility
 */

const char *sieve_binfile_from_name(const char *name);

/*
 * Activation after code generation
 */

void sieve_binary_activate(struct sieve_binary *sbin);

/*
 * Saving the binary
 */

int sieve_binary_save(struct sieve_binary *sbin, const char *path, bool update,
		      mode_t save_mode, enum sieve_error *error_r);

/*
 * Loading the binary
 */

struct sieve_binary *
sieve_binary_open(struct sieve_instance *svinst, const char *path,
		  struct sieve_script *script, enum sieve_error *error_r);
bool sieve_binary_up_to_date(struct sieve_binary *sbin,
			     enum sieve_compile_flags cpflags);

int sieve_binary_check_executable(struct sieve_binary *sbin,
				  enum sieve_error *error_r,
				  const char **client_error_r);

/*
 * Block management
 */

enum sieve_binary_system_block {
	SBIN_SYSBLOCK_SCRIPT_DATA,
	SBIN_SYSBLOCK_EXTENSIONS,
	SBIN_SYSBLOCK_MAIN_PROGRAM,
	SBIN_SYSBLOCK_LAST
};

struct sieve_binary_block *sieve_binary_block_create(struct sieve_binary *sbin);

unsigned int sieve_binary_block_count(struct sieve_binary *sbin);

struct sieve_binary_block *
sieve_binary_block_get(struct sieve_binary *sbin, unsigned int id);

void sieve_binary_block_clear(struct sieve_binary_block *sblock);

size_t sieve_binary_block_get_size(const struct sieve_binary_block *sblock);

struct sieve_binary *
sieve_binary_block_get_binary(const struct sieve_binary_block *sblock);

unsigned int sieve_binary_block_get_id(const struct sieve_binary_block *sblock);

/*
 * Extension support
 */

struct sieve_binary_extension {
	const struct sieve_extension_def *extension;

	bool (*binary_pre_save)(const struct sieve_extension *ext,
				struct sieve_binary *sbin, void *context,
				enum sieve_error *error_r);
	bool (*binary_post_save)(const struct sieve_extension *ext,
				 struct sieve_binary *sbin, void *context,
				 enum sieve_error *error_r);
	bool (*binary_open)(const struct sieve_extension *ext,
			    struct sieve_binary *sbin, void *context);

	void (*binary_free)(const struct sieve_extension *ext,
			    struct sieve_binary *sbin, void *context);

	bool (*binary_up_to_date)(const struct sieve_extension *ext,
				  struct sieve_binary *sbin, void *context,
				  enum sieve_compile_flags cpflags);
};

void sieve_binary_extension_set_context(struct sieve_binary *sbin,
					const struct sieve_extension *ext,
					void *context);
const void *
sieve_binary_extension_get_context(struct sieve_binary *sbin,
				   const struct sieve_extension *ext);

void sieve_binary_extension_set(struct sieve_binary *sbin,
				const struct sieve_extension *ext,
				const struct sieve_binary_extension *bext,
				void *context);

struct sieve_binary_block *
sieve_binary_extension_create_block(struct sieve_binary *sbin,
				    const struct sieve_extension *ext);
struct sieve_binary_block *
sieve_binary_extension_get_block(struct sieve_binary *sbin,
				 const struct sieve_extension *ext);

int sieve_binary_extension_link(struct sieve_binary *sbin,
				const struct sieve_extension *ext);
const struct sieve_extension *
sieve_binary_extension_get_by_index(struct sieve_binary *sbin, int index);
int sieve_binary_extension_get_index(struct sieve_binary *sbin,
				     const struct sieve_extension *ext);
int sieve_binary_extensions_count(struct sieve_binary *sbin);

/*
 * Code emission
 */

/* Low-level emission functions */

sieve_size_t sieve_binary_emit_data(struct sieve_binary_block *sblock,
				    const void *data, sieve_size_t size);
sieve_size_t sieve_binary_emit_byte(struct sieve_binary_block *sblock,
				    uint8_t byte);
void sieve_binary_update_data(struct sieve_binary_block *sblock,
			      sieve_size_t address, const void *data,
			      sieve_size_t size);

/* Offset emission functions */

sieve_size_t sieve_binary_emit_offset(struct sieve_binary_block *sblock,
				      sieve_offset_t offset);
void sieve_binary_resolve_offset(struct sieve_binary_block *sblock,
				 sieve_size_t address);

/* Literal emission functions */

sieve_size_t sieve_binary_emit_integer(struct sieve_binary_block *sblock,
				       sieve_number_t integer);
sieve_size_t sieve_binary_emit_string(struct sieve_binary_block *sblock,
				      const string_t *str);
sieve_size_t sieve_binary_emit_cstring(struct sieve_binary_block *sblock,
				       const char *str);

static inline sieve_size_t
sieve_binary_emit_unsigned(struct sieve_binary_block *sblock,
			   unsigned int count)
{
	return sieve_binary_emit_integer(sblock, count);
}

/* Extension emission functions */

sieve_size_t sieve_binary_emit_extension(struct sieve_binary_block *sblock,
					 const struct sieve_extension *ext,
					 unsigned int offset);
void sieve_binary_emit_extension_object(
	struct sieve_binary_block *sblock,
	const struct sieve_extension_objects *objs, unsigned int code);

/*
 * Code retrieval
 */

/* Literals */

bool sieve_binary_read_byte(struct sieve_binary_block *sblock,
			    sieve_size_t *address, unsigned int *byte_r)
			    ATTR_NULL(3);
bool sieve_binary_read_code(struct sieve_binary_block *sblock,
			    sieve_size_t *address, signed int *code_r)
			    ATTR_NULL(3);
bool sieve_binary_read_offset(struct sieve_binary_block *sblock,
			      sieve_size_t *address, sieve_offset_t *offset_r)
			      ATTR_NULL(3);
bool sieve_binary_read_integer(struct sieve_binary_block *sblock,
			       sieve_size_t *address, sieve_number_t *int_r)
			       ATTR_NULL(3);
bool sieve_binary_read_string(struct sieve_binary_block *sblock,
			      sieve_size_t *address, string_t **str_r)
			      ATTR_NULL(3);

static inline bool ATTR_NULL(3)
sieve_binary_read_unsigned(struct sieve_binary_block *sblock,
			   sieve_size_t *address, unsigned int *count_r)
{
	sieve_number_t integer = 0;

	if (!sieve_binary_read_integer(sblock, address, &integer))
		return FALSE;
	if (count_r != NULL)
		*count_r = integer;
	return TRUE;
}

/* Extensions */

bool sieve_binary_read_extension(struct sieve_binary_block *sblock,
				 sieve_size_t *address, unsigned int *offset_r,
				 const struct sieve_extension **ext_r);
const void *
sieve_binary_read_extension_object(struct sieve_binary_block *sblock,
				   sieve_size_t *address,
				   const struct sieve_extension_objects *objs);

/*
 * Debug info
 */

/* Writer */

struct sieve_binary_debug_writer;

struct sieve_binary_debug_writer *
sieve_binary_debug_writer_init(struct sieve_binary_block *sblock);
void sieve_binary_debug_writer_deinit(
	struct sieve_binary_debug_writer **dwriter);

void sieve_binary_debug_emit(struct sieve_binary_debug_writer *dwriter,
			     sieve_size_t code_address, unsigned int code_line,
			     unsigned int code_column);

/* Reader */

struct sieve_binary_debug_reader *
sieve_binary_debug_reader_init(struct sieve_binary_block *sblock);
void sieve_binary_debug_reader_deinit(
	struct sieve_binary_debug_reader **dreader);

void sieve_binary_debug_reader_reset(struct sieve_binary_debug_reader *dreader);

unsigned int
sieve_binary_debug_read_line(struct sieve_binary_debug_reader *dreader,
			     sieve_size_t code_address);

#endif