File: ext-ihave-binary.c

package info (click to toggle)
dovecot 1:2.2.13-12~deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 38,792 kB
  • sloc: ansic: 341,472; sh: 16,920; makefile: 5,393; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (245 lines) | stat: -rw-r--r-- 6,179 bytes parent folder | download | duplicates (3)
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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "str.h"

#include "sieve-common.h"
#include "sieve-extensions.h"
#include "sieve-error.h"
#include "sieve-script.h"
#include "sieve-binary.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"
#include "sieve-dump.h"

#include "ext-ihave-common.h"
#include "ext-ihave-binary.h"

/*
 * Forward declarations
 */

static bool ext_ihave_binary_save
	(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context);
static bool ext_ihave_binary_open
	(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context);
static bool ext_ihave_binary_up_to_date
	(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context,
		enum sieve_compile_flags cpflags);

/*
 * Binary include extension
 */

const struct sieve_binary_extension ihave_binary_ext = {
	&ihave_extension,
	ext_ihave_binary_save,
	ext_ihave_binary_open,
	NULL,
	ext_ihave_binary_up_to_date
};

/*
 * Binary context management
 */

struct ext_ihave_binary_context {
	struct sieve_binary *binary;
	struct sieve_binary_block *block;

	ARRAY(const char *) missing_extensions;
};

static struct ext_ihave_binary_context *ext_ihave_binary_create_context
(const struct sieve_extension *this_ext, struct sieve_binary *sbin)
{
	pool_t pool = sieve_binary_pool(sbin);

	struct ext_ihave_binary_context *ctx =
		p_new(pool, struct ext_ihave_binary_context, 1);

	ctx->binary = sbin;
	p_array_init(&ctx->missing_extensions, pool, 64);

	sieve_binary_extension_set(sbin, this_ext, &ihave_binary_ext, ctx);
	return ctx;
}

struct ext_ihave_binary_context *ext_ihave_binary_get_context
(const struct sieve_extension *this_ext, struct sieve_binary *sbin)
{
	struct ext_ihave_binary_context *ctx = (struct ext_ihave_binary_context *)
		sieve_binary_extension_get_context(sbin, this_ext);

	if ( ctx == NULL )
		ctx = ext_ihave_binary_create_context(this_ext, sbin);

	return ctx;
}

struct ext_ihave_binary_context *ext_ihave_binary_init
(const struct sieve_extension *this_ext, struct sieve_binary *sbin,
	struct sieve_ast *ast)
{
	struct ext_ihave_ast_context *ast_ctx =
		ext_ihave_get_ast_context(this_ext, ast);
	struct ext_ihave_binary_context *binctx;
	const char *const *exts;
	unsigned int i, count;

	binctx = ext_ihave_binary_get_context(this_ext, sbin);

	exts = array_get(&ast_ctx->missing_extensions, &count);

	if ( count > 0 ) {
		pool_t pool = sieve_binary_pool(sbin);

		if ( binctx->block == NULL )
			binctx->block = sieve_binary_extension_create_block(sbin, this_ext);

		for ( i = 0; i < count; i++ ) {
			const char *ext_name = p_strdup(pool, exts[i]);

			array_append(&binctx->missing_extensions, &ext_name, 1);
		}
	}

	return binctx;
}

/*
 * Binary extension
 */

static bool ext_ihave_binary_save
(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context)
{
	struct ext_ihave_binary_context *binctx =
		(struct ext_ihave_binary_context *) context;
	const char *const *exts;
	unsigned int count, i;

	exts = array_get(&binctx->missing_extensions, &count);

	if ( binctx->block != NULL )
		sieve_binary_block_clear(binctx->block);

	if ( count > 0 ) {
		if ( binctx->block == NULL )
			binctx->block = sieve_binary_extension_create_block(sbin, ext);

		sieve_binary_emit_unsigned(binctx->block, count);

		for ( i = 0; i < count; i++ ) {
			sieve_binary_emit_cstring(binctx->block, exts[i]);
		}
	}

	return TRUE;
}

static bool ext_ihave_binary_open
(const struct sieve_extension *ext, struct sieve_binary *sbin, void *context)
{
	struct sieve_instance *svinst = ext->svinst;
	struct ext_ihave_binary_context *binctx =
		(struct ext_ihave_binary_context *) context;
	struct sieve_binary_block *sblock;
	unsigned int i, count, block_id;
	sieve_size_t offset;

	sblock = sieve_binary_extension_get_block(sbin, ext);

	if ( sblock != NULL ) {
		binctx->block = sblock;
		block_id = sieve_binary_block_get_id(sblock);

		offset = 0;

		/* Read number of missing extensions to read subsequently */
		if ( !sieve_binary_read_unsigned(sblock, &offset, &count) ) {
			sieve_sys_error(svinst,
				"ihave: failed to read missing extension count "
				"from block %d of binary %s", block_id, sieve_binary_path(sbin));
			return FALSE;
		}

		/* Read dependencies */
		for ( i = 0; i < count; i++ ) {
			string_t *ext_name;
			const char *name;

			if ( !sieve_binary_read_string(sblock, &offset, &ext_name) ) {
				/* Binary is corrupt, recompile */
				sieve_sys_error(svinst,
					"ihave: failed to read missing extension name "
					"from block %d of binary %s", block_id, sieve_binary_path(sbin));
				return FALSE;
			}

			name = str_c(ext_name);
			array_append(&binctx->missing_extensions, &name, 1);
		}
	}

	return TRUE;
}

static bool ext_ihave_binary_up_to_date
(const struct sieve_extension *ext, struct sieve_binary *sbin ATTR_UNUSED,
	void *context, enum sieve_compile_flags cpflags)
{
	struct ext_ihave_binary_context *binctx =
		(struct ext_ihave_binary_context *) context;
	const struct sieve_extension *mext;
	const char *const *mexts;
	unsigned int count, i;

	mexts = array_get(&binctx->missing_extensions, &count);
	for ( i = 0; i < count; i++ ) {
		if ( (mext=sieve_extension_get_by_name(ext->svinst, mexts[i])) != NULL &&
			((cpflags & SIEVE_COMPILE_FLAG_NOGLOBAL) == 0 || !mext->global) )
			return FALSE;
	}

	return TRUE;
}

/*
 * Main extension interface
 */

bool ext_ihave_binary_load
(const struct sieve_extension *ext, struct sieve_binary *sbin)
{
	(void)ext_ihave_binary_get_context(ext, sbin);

	return TRUE;
}

bool ext_ihave_binary_dump
(const struct sieve_extension *ext, struct sieve_dumptime_env *denv)
{
	struct sieve_binary *sbin = denv->sbin;
	struct ext_ihave_binary_context *binctx =
		ext_ihave_binary_get_context(ext, sbin);
	const char *const *exts;
	unsigned int count, i;

	exts = array_get(&binctx->missing_extensions, &count);

	if ( count > 0 ) {
		sieve_binary_dump_sectionf(denv,
			"Extensions missing at compile (block: %d)",
			sieve_binary_block_get_id(binctx->block));

		for ( i = 0; i < count; i++ ) {
			sieve_binary_dumpf(denv, "  -  %s\n", exts[i]);
		}
	}

	return TRUE;
}