File: ext-variables-common.c

package info (click to toggle)
dovecot 1%3A2.2.13-11~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 38,468 kB
  • sloc: ansic: 341,153; sh: 16,920; makefile: 5,381; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (795 lines) | stat: -rw-r--r-- 20,116 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/* 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-ast.h"
#include "sieve-binary.h"
#include "sieve-code.h"
#include "sieve-objects.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-modifiers.h"

/*
 * Limits
 */

unsigned int sieve_variables_get_max_scope_size(void)
{
	return EXT_VARIABLES_MAX_SCOPE_SIZE;
}

/*
 * Variable scope
 */

struct sieve_variable_scope {
	pool_t pool;
	int refcount;

	struct sieve_instance *svinst;
	const struct sieve_extension *ext;

	struct sieve_variable *error_var;

	HASH_TABLE(const char *, struct sieve_variable *) variables;
	ARRAY(struct sieve_variable *) variable_index;
};

struct sieve_variable_scope_binary {
	struct sieve_variable_scope *scope;

	unsigned int size;
	struct sieve_binary_block *sblock;
	sieve_size_t address;
};

struct sieve_variable_scope_iter {
	struct sieve_variable_scope *scope;
	struct hash_iterate_context *hctx;
};

struct sieve_variable_scope *sieve_variable_scope_create
(struct sieve_instance *svinst, const struct sieve_extension *ext)
{
	struct sieve_variable_scope *scope;
	pool_t pool;

	pool = pool_alloconly_create("sieve_variable_scope", 4096);
	scope = p_new(pool, struct sieve_variable_scope, 1);
	scope->pool = pool;
	scope->refcount = 1;

	scope->svinst = svinst;
	scope->ext = ext;

	hash_table_create(&scope->variables, pool, 0, strcase_hash, strcasecmp);
	p_array_init(&scope->variable_index, pool, 128);

	return scope;
}

void sieve_variable_scope_ref(struct sieve_variable_scope *scope)
{
	scope->refcount++;
}

void sieve_variable_scope_unref(struct sieve_variable_scope **_scope)
{
	struct sieve_variable_scope *scope = *_scope;

	i_assert(scope->refcount > 0);

	if (--scope->refcount != 0)
		return;

	hash_table_destroy(&scope->variables);

	*_scope = NULL;
	pool_unref(&scope->pool);
}

pool_t sieve_variable_scope_pool(struct sieve_variable_scope *scope)
{
	return scope->pool;
}

struct sieve_variable *sieve_variable_scope_declare
(struct sieve_variable_scope *scope, const char *identifier)
{
	struct sieve_variable *new_var;

	new_var = p_new(scope->pool, struct sieve_variable, 1);
	new_var->ext = scope->ext;

	if ( array_count(&scope->variable_index) >= EXT_VARIABLES_MAX_SCOPE_SIZE ) {
		if ( scope->error_var == NULL ) {
			new_var->identifier = "@ERROR@";
			new_var->index = 0;

			scope->error_var = new_var;
			return NULL;
		}

		return scope->error_var;
	}

	new_var->identifier = p_strdup(scope->pool, identifier);
	new_var->index = array_count(&scope->variable_index);

	hash_table_insert(scope->variables, new_var->identifier, new_var);
	array_append(&scope->variable_index, &new_var, 1);

	return new_var;
}

struct sieve_variable *sieve_variable_scope_get_variable
(struct sieve_variable_scope *scope, const char *identifier, bool declare)
{
	struct sieve_variable *var;

	var = hash_table_lookup(scope->variables, identifier);

	if ( var == NULL && declare ) {
		var = sieve_variable_scope_declare(scope, identifier);
	}

	return var;
}

struct sieve_variable *sieve_variable_scope_import
(struct sieve_variable_scope *scope, struct sieve_variable *var)
{
	struct sieve_variable *new_var;

	new_var = p_new(scope->pool, struct sieve_variable, 1);
	memcpy(new_var, var, sizeof(struct sieve_variable));

	hash_table_insert(scope->variables, new_var->identifier, new_var);

	/* Not entered into the index because it is an external variable
	 * (This can be done unlimited; only limited by the size of the external scope)
	 */

	return new_var;
}

struct sieve_variable_scope_iter *sieve_variable_scope_iterate_init
(struct sieve_variable_scope *scope)
{
	struct sieve_variable_scope_iter *iter;

	iter = t_new(struct sieve_variable_scope_iter, 1);
	iter->scope = scope;
	iter->hctx = hash_table_iterate_init(scope->variables);

	return iter;
}

bool sieve_variable_scope_iterate
(struct sieve_variable_scope_iter *iter, struct sieve_variable **var_r)
{
	const char *key;

	return hash_table_iterate
		(iter->hctx, iter->scope->variables, &key, var_r);
}

void sieve_variable_scope_iterate_deinit
(struct sieve_variable_scope_iter **iter)
{
	hash_table_iterate_deinit(&(*iter)->hctx);
	*iter = NULL;
}

unsigned int sieve_variable_scope_declarations
(struct sieve_variable_scope *scope)
{
	return hash_table_count(scope->variables);
}

unsigned int sieve_variable_scope_size
(struct sieve_variable_scope *scope)
{
	return array_count(&scope->variable_index);
}

struct sieve_variable * const *sieve_variable_scope_get_variables
(struct sieve_variable_scope *scope, unsigned int *size_r)
{
	return array_get(&scope->variable_index, size_r);
}

struct sieve_variable *sieve_variable_scope_get_indexed
(struct sieve_variable_scope *scope, unsigned int index)
{
	struct sieve_variable * const *var;

	if ( index >= array_count(&scope->variable_index) )
		return NULL;

	var = array_idx(&scope->variable_index, index);

	return *var;
}

/* Scope binary */

struct sieve_variable_scope *sieve_variable_scope_binary_dump
(struct sieve_instance *svinst, const struct sieve_extension *ext,
	const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
	struct sieve_variable_scope *local_scope;
	unsigned int i, scope_size;
	sieve_size_t pc;
	sieve_offset_t end_offset;

	/* Read scope size */
	sieve_code_mark(denv);
	if ( !sieve_binary_read_unsigned(denv->sblock, address, &scope_size) )
		return FALSE;

	/* Read offset */
	pc = *address;
	if ( !sieve_binary_read_offset(denv->sblock, address, &end_offset) )
		return FALSE;

	/* Create scope */
	local_scope = sieve_variable_scope_create(svinst, ext);

	/* Read and dump scope itself */

	sieve_code_dumpf(denv, "VARIABLES SCOPE [%u] (end: %08x)",
		scope_size, (unsigned int) (pc + end_offset));

	for ( i = 0; i < scope_size; i++ ) {
		string_t *identifier;

		sieve_code_mark(denv);
		if (!sieve_binary_read_string(denv->sblock, address, &identifier) ) {
			return FALSE;
		}

		sieve_code_dumpf(denv, "%3d: '%s'", i, str_c(identifier));

		(void) sieve_variable_scope_declare(local_scope, str_c(identifier));
	}

	return local_scope;
}

struct sieve_variable_scope_binary *sieve_variable_scope_binary_create
(struct sieve_variable_scope *scope)
{
	struct sieve_variable_scope_binary *scpbin;

	scpbin = p_new(scope->pool, struct sieve_variable_scope_binary, 1);
	scpbin->scope = scope;

	return scpbin;
}

void sieve_variable_scope_binary_ref
(struct sieve_variable_scope_binary *scpbin)
{
	sieve_variable_scope_ref(scpbin->scope);
}

void sieve_variable_scope_binary_unref
(struct sieve_variable_scope_binary **scpbin)
{
	sieve_variable_scope_unref(&(*scpbin)->scope);
	*scpbin = NULL;
}

struct sieve_variable_scope_binary *sieve_variable_scope_binary_read
(struct sieve_instance *svinst, const struct sieve_extension *ext,
	struct sieve_binary_block *sblock, sieve_size_t *address)
{
	struct sieve_variable_scope *scope;
	struct sieve_variable_scope_binary *scpbin;
	unsigned int scope_size;
	const char *ext_name =
		( ext == NULL ? "variables" : sieve_extension_name(ext) );
	sieve_size_t pc;
	sieve_offset_t end_offset;

	/* Read scope size */
	if ( !sieve_binary_read_unsigned(sblock, address, &scope_size) ) {
		sieve_sys_error
			(svinst, "%s: variable scope: failed to read size", ext_name);
		return NULL;
	}

	/* Check size limit */
	if ( scope_size > EXT_VARIABLES_MAX_SCOPE_SIZE ) {
		sieve_sys_error(svinst,
			"%s: variable scope: size exceeds the limit (%u > %u)",
			ext_name, scope_size, EXT_VARIABLES_MAX_SCOPE_SIZE );
		return NULL;
	}

	/* Read offset */
	pc = *address;
	if ( !sieve_binary_read_offset(sblock, address, &end_offset) ) {
		sieve_sys_error(svinst,
			"%s: variable scope: failed to read end offset", ext_name);
		return NULL;
	}

	/* Create scope */
	scope = sieve_variable_scope_create(svinst, ext);

	scpbin = sieve_variable_scope_binary_create(scope);
	scpbin->size = scope_size;
	scpbin->sblock = sblock;
	scpbin->address = *address;

	*address = pc + end_offset;

	return scpbin;
}

struct sieve_variable_scope *sieve_variable_scope_binary_get
(struct sieve_variable_scope_binary *scpbin)
{
	const struct sieve_extension *ext = scpbin->scope->ext;
	struct sieve_instance *svinst = scpbin->scope->svinst;
	const char *ext_name =
		( ext == NULL ? "variables" : sieve_extension_name(ext) );
	unsigned int i;

	if ( scpbin->sblock != NULL ) {
		sieve_size_t *address = &scpbin->address;

		/* Read scope itself */
		for ( i = 0; i < scpbin->size; i++ ) {
			struct sieve_variable *var;
			string_t *identifier;

			if (!sieve_binary_read_string(scpbin->sblock, address, &identifier) ) {
				sieve_sys_error(svinst,
					"%s: variable scope: failed to read variable name", ext_name);
				return NULL;
			}

			var = sieve_variable_scope_declare(scpbin->scope, str_c(identifier));

			i_assert( var != NULL );
			i_assert( var->index == i );
		}

		scpbin->sblock = NULL;
	}

	return scpbin->scope;
}

unsigned int sieve_variable_scope_binary_get_size
(struct sieve_variable_scope_binary *scpbin)
{
	if ( scpbin->sblock != NULL )
		return scpbin->size;

	return array_count(&scpbin->scope->variable_index);
}

/*
 * Variable storage
 */

struct sieve_variable_storage {
	pool_t pool;
	struct sieve_variable_scope *scope;
	struct sieve_variable_scope_binary *scope_bin;
	unsigned int max_size;
	ARRAY(string_t *) var_values;
};

struct sieve_variable_storage *sieve_variable_storage_create
(pool_t pool, struct sieve_variable_scope_binary *scpbin)
{
	struct sieve_variable_storage *storage;

	storage = p_new(pool, struct sieve_variable_storage, 1);
	storage->pool = pool;
	storage->scope_bin = scpbin;
	storage->scope = NULL;

	storage->max_size = sieve_variable_scope_binary_get_size(scpbin);

	p_array_init(&storage->var_values, pool, 4);

	return storage;
}

static inline bool sieve_variable_valid
(struct sieve_variable_storage *storage, unsigned int index)
{
	if ( storage->scope_bin == NULL )
		return TRUE;

	return ( index < storage->max_size );
}

bool sieve_variable_get_identifier
(struct sieve_variable_storage *storage, unsigned int index,
	const char **identifier)
{
	struct sieve_variable * const *var;

	*identifier = NULL;

	if ( storage->scope_bin == NULL )
		return TRUE;

	if ( storage->scope == NULL ) {
		storage->scope = sieve_variable_scope_binary_get(storage->scope_bin);
		if ( storage->scope == NULL ) return FALSE;
	}

	/* FIXME: direct invasion of the scope object is a bit ugly */
	if ( index >= array_count(&storage->scope->variable_index) )
		return FALSE;

	var = array_idx(&storage->scope->variable_index, index);

	if ( *var != NULL )
		*identifier = (*var)->identifier;

	return TRUE;
}

const char *sieve_variable_get_varid
(struct sieve_variable_storage *storage, unsigned int index)
{
	if ( storage->scope_bin == NULL )
		return t_strdup_printf("%ld", (long) index);

	if ( storage->scope == NULL ) {
		storage->scope = sieve_variable_scope_binary_get(storage->scope_bin);
		if ( storage->scope == NULL ) return NULL;
	}

	return sieve_ext_variables_get_varid(storage->scope->ext, index);
}

bool sieve_variable_get
(struct sieve_variable_storage *storage, unsigned int index, string_t **value)
{
	*value = NULL;

	if  ( index < array_count(&storage->var_values) ) {
		string_t * const *varent;

		varent = array_idx(&storage->var_values, index);

		*value = *varent;
	} else if ( !sieve_variable_valid(storage, index) )
		return FALSE;

	return TRUE;
}

bool sieve_variable_get_modifiable
(struct sieve_variable_storage *storage, unsigned int index, string_t **value)
{
	string_t *dummy;

	if ( value == NULL ) value = &dummy;

	if ( !sieve_variable_get(storage, index, value) )
		return FALSE;

	if ( *value == NULL ) {
		*value = str_new(storage->pool, 256);
		array_idx_set(&storage->var_values, index, value);
	}

	return TRUE;
}

bool sieve_variable_assign
(struct sieve_variable_storage *storage, unsigned int index,
	const string_t *value)
{
	string_t *varval;

	if ( !sieve_variable_get_modifiable(storage, index, &varval) )
		return FALSE;

	str_truncate(varval, 0);
	str_append_str(varval, value);

	/* Just a precaution, caller should prevent this in the first place */
	if ( str_len(varval) > EXT_VARIABLES_MAX_VARIABLE_SIZE )
		str_truncate(varval, EXT_VARIABLES_MAX_VARIABLE_SIZE);

	return TRUE;
}

/*
 * AST Context
 */

static void ext_variables_ast_free
(const struct sieve_extension *ext ATTR_UNUSED,
	struct sieve_ast *ast ATTR_UNUSED, void *context)
{
	struct sieve_variable_scope *local_scope =
		(struct sieve_variable_scope *) context;

	/* Unreference main variable scope */
	sieve_variable_scope_unref(&local_scope);
}

static const struct sieve_ast_extension variables_ast_extension = {
    &variables_extension,
    ext_variables_ast_free
};

static struct sieve_variable_scope *ext_variables_create_local_scope
(const struct sieve_extension *this_ext, struct sieve_ast *ast)
{
	struct sieve_variable_scope *scope;

	scope = sieve_variable_scope_create(this_ext->svinst, NULL);

	sieve_ast_extension_register
		(ast, this_ext, &variables_ast_extension, (void *) scope);

	return scope;
}

static struct sieve_variable_scope *ext_variables_ast_get_local_scope
(const struct sieve_extension *this_ext, struct sieve_ast *ast)
{
	struct sieve_variable_scope *local_scope = (struct sieve_variable_scope *)
		sieve_ast_extension_get_context(ast, this_ext);

	return local_scope;
}

/*
 * Validator context
 */

static struct ext_variables_validator_context *
ext_variables_validator_context_create
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr)
{
	pool_t pool = sieve_validator_pool(valdtr);
	struct ext_variables_validator_context *ctx;
	struct sieve_ast *ast = sieve_validator_ast(valdtr);

	ctx = p_new(pool, struct ext_variables_validator_context, 1);
	ctx->modifiers = sieve_validator_object_registry_create(valdtr);
	ctx->namespaces = sieve_validator_object_registry_create(valdtr);
	ctx->local_scope = ext_variables_create_local_scope(this_ext, ast);

	sieve_validator_extension_set_context(valdtr, this_ext, (void *) ctx);
	return ctx;
}

struct ext_variables_validator_context *ext_variables_validator_context_get
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr)
{
	struct ext_variables_validator_context *ctx =
		(struct ext_variables_validator_context *)
		sieve_validator_extension_get_context(valdtr, this_ext);

	if ( ctx == NULL ) {
		ctx = ext_variables_validator_context_create(this_ext, valdtr);
	}

	return ctx;
}

void ext_variables_validator_initialize
(const struct sieve_extension *this_ext, struct sieve_validator *valdtr)
{
	struct ext_variables_validator_context *ctx;

	/* Create our context */
	ctx = ext_variables_validator_context_get(this_ext, valdtr);

	ext_variables_register_core_modifiers(this_ext, ctx);

	ctx->active = TRUE;
}

struct sieve_variable *ext_variables_validator_get_variable
(const struct sieve_extension *this_ext, struct sieve_validator *validator,
	const char *variable, bool declare)
{
	struct ext_variables_validator_context *ctx =
		ext_variables_validator_context_get(this_ext, validator);

	return sieve_variable_scope_get_variable(ctx->local_scope, variable, declare);
}

struct sieve_variable_scope *sieve_ext_variables_get_local_scope
(const struct sieve_extension *var_ext, struct sieve_validator *validator)
{
	struct ext_variables_validator_context *ctx =
		ext_variables_validator_context_get(var_ext, validator);

	return ctx->local_scope;
}

bool sieve_ext_variables_is_active
(const struct sieve_extension *var_ext, struct sieve_validator *valdtr)
{
	struct ext_variables_validator_context *ctx =
		ext_variables_validator_context_get(var_ext, valdtr);

	return ( ctx != NULL && ctx->active );
}

/*
 * Code generation
 */

bool ext_variables_generator_load
(const struct sieve_extension *ext, const struct sieve_codegen_env *cgenv)
{
	struct sieve_variable_scope *local_scope =
		ext_variables_ast_get_local_scope(ext, cgenv->ast);
	unsigned int count = sieve_variable_scope_size(local_scope);
	sieve_size_t jump;

	sieve_binary_emit_unsigned(cgenv->sblock, count);

	jump = sieve_binary_emit_offset(cgenv->sblock, 0);

	if ( count > 0 ) {
		unsigned int size, i;
		struct sieve_variable *const *vars =
			sieve_variable_scope_get_variables(local_scope, &size);

		for ( i = 0; i < size; i++ ) {
			sieve_binary_emit_cstring(cgenv->sblock, vars[i]->identifier);
		}
	}

	sieve_binary_resolve_offset(cgenv->sblock, jump);

	return TRUE;
}

/*
 * Interpreter context
 */

struct ext_variables_interpreter_context {
	pool_t pool;

	struct sieve_variable_scope *local_scope;
	struct sieve_variable_scope_binary *local_scope_bin;

	struct sieve_variable_storage *local_storage;
	ARRAY(struct sieve_variable_storage *) ext_storages;
};

static void ext_variables_interpreter_free
(const struct sieve_extension *ext ATTR_UNUSED,
	struct sieve_interpreter *interp ATTR_UNUSED, void *context)
{
	struct ext_variables_interpreter_context *ctx =
		(struct ext_variables_interpreter_context *)context;

	sieve_variable_scope_binary_unref(&ctx->local_scope_bin);
}

static struct sieve_interpreter_extension variables_interpreter_extension = {
	&variables_extension,
	NULL,
	ext_variables_interpreter_free
};

static struct ext_variables_interpreter_context *
ext_variables_interpreter_context_create
(const struct sieve_extension *this_ext, struct sieve_interpreter *interp,
	struct sieve_variable_scope_binary *scpbin)
{
	pool_t pool = sieve_interpreter_pool(interp);
	struct ext_variables_interpreter_context *ctx;

	ctx = p_new(pool, struct ext_variables_interpreter_context, 1);
	ctx->pool = pool;
	ctx->local_scope = NULL;
	ctx->local_scope_bin = scpbin;
	ctx->local_storage = sieve_variable_storage_create(pool, scpbin);
	p_array_init(&ctx->ext_storages, pool,
		sieve_extensions_get_count(this_ext->svinst));

	sieve_interpreter_extension_register
		(interp, this_ext, &variables_interpreter_extension, (void *) ctx);

	return ctx;
}

bool ext_variables_interpreter_load
(const struct sieve_extension *ext, const struct sieve_runtime_env *renv,
	sieve_size_t *address)
{
	struct sieve_variable_scope_binary *scpbin;

	scpbin = sieve_variable_scope_binary_read
		(renv->svinst, NULL, renv->sblock, address);
	if ( scpbin == NULL )
		return FALSE;

	/* Create our context */
	(void)ext_variables_interpreter_context_create
		(ext, renv->interp, scpbin);

	/* Enable support for match values */
	(void) sieve_match_values_set_enabled(renv, TRUE);

	return TRUE;
}

static inline struct ext_variables_interpreter_context *
ext_variables_interpreter_context_get
	(const struct sieve_extension *this_ext, struct sieve_interpreter *interp)
{
	return (struct ext_variables_interpreter_context *)
		sieve_interpreter_extension_get_context(interp, this_ext);
}

struct sieve_variable_storage *sieve_ext_variables_runtime_get_storage
(const struct sieve_extension *var_ext, const struct sieve_runtime_env *renv,
	const struct sieve_extension *ext)
{
	struct ext_variables_interpreter_context *ctx =
		ext_variables_interpreter_context_get(var_ext, renv->interp);
	struct sieve_variable_storage * const *storage;

	if ( ext == NULL )
		return ctx->local_storage;

	if ( ext->id >= (int) array_count(&ctx->ext_storages) ) {
		storage = NULL;
	} else {
		storage = array_idx(&ctx->ext_storages, ext->id);
	}

	if ( storage == NULL ) return NULL;

	return *storage;
}

void sieve_ext_variables_runtime_set_storage
(const struct sieve_extension *var_ext, const struct sieve_runtime_env *renv,
	const struct sieve_extension *ext, struct sieve_variable_storage *storage)
{
	struct ext_variables_interpreter_context *ctx =
		ext_variables_interpreter_context_get(var_ext, renv->interp);

	if ( ctx == NULL || ext == NULL || storage == NULL )
		return;

	if ( ext->id < 0 ) return;

	array_idx_set(&ctx->ext_storages, (unsigned int) ext->id, &storage);
}