File: ext-variables-operands.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 (273 lines) | stat: -rw-r--r-- 7,933 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
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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "hash.h"
#include "str.h"
#include "array.h"

#include "sieve-common.h"
#include "sieve-extensions.h"
#include "sieve-ast.h"
#include "sieve-binary.h"
#include "sieve-code.h"
#include "sieve-match-types.h"
#include "sieve-commands.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-dump.h"
#include "sieve-interpreter.h"

#include "ext-variables-common.h"
#include "ext-variables-limits.h"
#include "ext-variables-name.h"
#include "ext-variables-dump.h"
#include "ext-variables-operands.h"

/*
 * Variable operand
 */

static bool opr_variable_dump
	(const struct sieve_dumptime_env *denv, const struct sieve_operand *oprnd,
		sieve_size_t *address);
static int opr_variable_read_value
	(const struct sieve_runtime_env *renv, const struct sieve_operand *oprnd,
		sieve_size_t *address, string_t **str_r);

const struct sieve_opr_string_interface variable_interface = {
	opr_variable_dump,
	opr_variable_read_value
};

const struct sieve_operand_def variable_operand = {
	"variable",
	&variables_extension,
	EXT_VARIABLES_OPERAND_VARIABLE,
	&string_class,
	&variable_interface
};

void sieve_variables_opr_variable_emit
(struct sieve_binary_block *sblock, const struct sieve_extension *var_ext,
	struct sieve_variable *var)
{
	if ( var->ext == NULL ) {
		/* Default variable storage */
		(void) sieve_operand_emit(sblock, var_ext, &variable_operand);
		(void) sieve_binary_emit_byte(sblock, 0); /* Default */
		(void) sieve_binary_emit_unsigned(sblock, var->index);
		return;
	}

	(void) sieve_operand_emit(sblock, var_ext, &variable_operand);
	(void) sieve_binary_emit_extension(sblock, var->ext, 1); /* Extension */
	(void) sieve_binary_emit_unsigned(sblock, var->index);
}

static bool opr_variable_dump
(const struct sieve_dumptime_env *denv, const struct sieve_operand *oprnd,
	sieve_size_t *address)
{
	const struct sieve_extension *this_ext = oprnd->ext;
	unsigned int index = 0;
	const struct sieve_extension *ext;
	unsigned int code = 1; /* Initially set to offset value */
	const char *identifier;

	if ( !sieve_binary_read_extension(denv->sblock, address, &code, &ext) )
		return FALSE;

	if ( !sieve_binary_read_unsigned(denv->sblock, address, &index) )
		return FALSE;

	identifier = ext_variables_dump_get_identifier(this_ext, denv, ext, index);
	identifier = identifier == NULL ? "??" : identifier;

	if ( oprnd->field_name != NULL )
		sieve_code_dumpf(denv, "%s: VAR[%s] ${%s}",
			oprnd->field_name, sieve_ext_variables_get_varid(ext, index), identifier);
	else
		sieve_code_dumpf(denv, "VAR[%s] ${%s}",
			sieve_ext_variables_get_varid(ext, index), identifier);

	return TRUE;
}

static int opr_variable_read_value
(const struct sieve_runtime_env *renv, const struct sieve_operand *oprnd,
	sieve_size_t *address, string_t **str_r)
{
	const struct sieve_extension *this_ext = oprnd->ext;
	const struct sieve_extension *ext;
	unsigned int code = 1; /* Initially set to offset value */
	struct sieve_variable_storage *storage;
	unsigned int index = 0;

	if ( !sieve_binary_read_extension(renv->sblock, address, &code, &ext) ) {
		sieve_runtime_trace_operand_error(renv, oprnd,
			"variable operand corrupt: invalid extension byte");
		return SIEVE_EXEC_BIN_CORRUPT;
	}

	storage = sieve_ext_variables_runtime_get_storage
		(this_ext, renv, ext);
	if ( storage == NULL ) {
		sieve_runtime_trace_operand_error(renv, oprnd,
			"variable operand corrupt: extension has no storage");
		return SIEVE_EXEC_BIN_CORRUPT;
	}

	if ( sieve_binary_read_unsigned(renv->sblock, address, &index) ) {
		/* Parameter str can be NULL if we are requested to only skip and not
		 * actually read the argument.
		 */
		if ( str_r != NULL ) {
			if ( !sieve_variable_get(storage, index, str_r) )
				return SIEVE_EXEC_FAILURE;

			if ( *str_r == NULL ) *str_r = t_str_new(0);
		}

		return SIEVE_EXEC_OK;
	}

	sieve_runtime_trace_operand_error(renv, oprnd,
		"variable operand corrupt: invalid variable index");
	return SIEVE_EXEC_BIN_CORRUPT;
}

int sieve_variable_operand_read_data
(const struct sieve_runtime_env *renv, struct sieve_operand *oprnd,
	sieve_size_t *address, const char *field_name,
	struct sieve_variable_storage **storage_r, unsigned int *var_index_r)
{
	const struct sieve_extension *ext;
	unsigned int code = 1; /* Initially set to offset value */
	unsigned int idx = 0;

	oprnd->field_name = field_name;

	if ( !sieve_operand_is_variable(oprnd) ) {
		sieve_runtime_trace_operand_error(renv, oprnd,
			"expected variable operand but found %s",	sieve_operand_name(oprnd));
		return SIEVE_EXEC_BIN_CORRUPT;
	}

	if ( !sieve_binary_read_extension(renv->sblock, address, &code, &ext) ) {
		sieve_runtime_trace_operand_error(renv, oprnd,
			"variable operand corrupt: invalid extension byte");
		return SIEVE_EXEC_BIN_CORRUPT;
	}

	*storage_r = sieve_ext_variables_runtime_get_storage
		(oprnd->ext, renv, ext);
	if ( *storage_r == NULL )	{
		sieve_runtime_trace_operand_error(renv, oprnd,
			"variable operand corrupt: extension has no storage");
		return SIEVE_EXEC_BIN_CORRUPT;
	}

	if ( !sieve_binary_read_unsigned(renv->sblock, address, &idx) ) {
		sieve_runtime_trace_operand_error(renv, oprnd,
			"variable operand corrupt: invalid variable index");
		return SIEVE_EXEC_BIN_CORRUPT;
	}

	*var_index_r = idx;
	return SIEVE_EXEC_OK;
}

int sieve_variable_operand_read
(const struct sieve_runtime_env *renv, sieve_size_t *address,
	const char *field_name, struct sieve_variable_storage **storage_r,
	unsigned int *var_index_r)
{
	struct sieve_operand operand;
	int ret;

	if ( (ret=sieve_operand_runtime_read(renv, address, field_name, &operand))
		<= 0)
		return ret;

	return sieve_variable_operand_read_data
		(renv, &operand, address, field_name, storage_r, var_index_r);
}

/*
 * Match value operand
 */

static bool opr_match_value_dump
	(const struct sieve_dumptime_env *denv, const struct sieve_operand *oprnd,
		sieve_size_t *address);
static int opr_match_value_read
	(const struct sieve_runtime_env *renv, const struct sieve_operand *oprnd,
		sieve_size_t *address, string_t **str_r);

const struct sieve_opr_string_interface match_value_interface = {
	opr_match_value_dump,
	opr_match_value_read
};

const struct sieve_operand_def match_value_operand = {
	"match-value",
	&variables_extension,
	EXT_VARIABLES_OPERAND_MATCH_VALUE,
	&string_class,
	&match_value_interface
};

void sieve_variables_opr_match_value_emit
(struct sieve_binary_block *sblock, const struct sieve_extension *var_ext,
	unsigned int index)
{
	(void) sieve_operand_emit(sblock, var_ext, &match_value_operand);
	(void) sieve_binary_emit_unsigned(sblock, index);
}

static bool opr_match_value_dump
(const struct sieve_dumptime_env *denv,	const struct sieve_operand *oprnd,
	sieve_size_t *address)
{
	unsigned int index = 0;

	if (sieve_binary_read_unsigned(denv->sblock, address, &index) ) {
		if ( oprnd->field_name != NULL )
			sieve_code_dumpf
				(denv, "%s: MATCHVAL %lu", oprnd->field_name, (unsigned long) index);
		else
			sieve_code_dumpf(denv, "MATCHVAL %lu", (unsigned long) index);

		return TRUE;
	}

	return FALSE;
}

static int opr_match_value_read
(const struct sieve_runtime_env *renv, const struct sieve_operand *oprnd,
	sieve_size_t *address, string_t **str_r)
{
	unsigned int index = 0;

	if ( sieve_binary_read_unsigned(renv->sblock, address, &index) ) {
		/* Parameter str can be NULL if we are requested to only skip and not
		 * actually read the argument.
		 	*/
		if ( str_r != NULL ) {
			sieve_match_values_get(renv, index, str_r);

			if ( *str_r == NULL )
				*str_r = t_str_new(0);
			else if ( str_len(*str_r) > EXT_VARIABLES_MAX_VARIABLE_SIZE )
				str_truncate(*str_r, EXT_VARIABLES_MAX_VARIABLE_SIZE);
		}

		return SIEVE_EXEC_OK;
	}

	sieve_runtime_trace_operand_error(renv, oprnd,
		"match value operand corrupt: invalid index data");
	return SIEVE_EXEC_BIN_CORRUPT;
}