summaryrefslogtreecommitdiffstats
path: root/pigeonhole/src/lib-sieve/sieve-objects.c
blob: 66fc9698f68820d01f6ec356b59a8c06db60c062 (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
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

#include "sieve-common.h"
#include "sieve-extensions.h"
#include "sieve-code.h"
#include "sieve-binary.h"
#include "sieve-dump.h"
#include "sieve-interpreter.h"

#include "sieve-objects.h"

/*
 * Object coding
 */

void sieve_opr_object_emit
(struct sieve_binary_block *sblock, const struct sieve_extension *ext,
	const struct sieve_object_def *obj_def)
{
	struct sieve_extension_objects *objs =
		(struct sieve_extension_objects *) obj_def->operand->interface;

	(void) sieve_operand_emit(sblock, ext, obj_def->operand);

	if ( objs->count > 1 ) {
		(void) sieve_binary_emit_byte(sblock, obj_def->code);
	}
}

bool sieve_opr_object_read_data
(struct sieve_binary_block *sblock, const struct sieve_operand *operand,
	const struct sieve_operand_class *opclass, sieve_size_t *address,
	struct sieve_object *obj)
{
	const struct sieve_extension_objects *objs;
	unsigned int obj_code;

	if ( operand == NULL || operand->def->class != opclass )
		return FALSE;

	objs = (struct sieve_extension_objects *) operand->def->interface;
	if ( objs == NULL )
		return FALSE;

	if ( objs->count > 1 ) {
		if ( !sieve_binary_read_byte(sblock, address, &obj_code) )
			return FALSE;

		if ( obj_code < objs->count ) {
			const struct sieve_object_def *const *objects =
				(const struct sieve_object_def *const *) objs->objects;

			obj->def = objects[obj_code];
			obj->ext = operand->ext;
			return TRUE;
		}
	}

	obj->def = (const struct sieve_object_def *) objs->objects;
	obj->ext = operand->ext;
	return TRUE;
}

bool sieve_opr_object_read
(const struct sieve_runtime_env *renv,
	const struct sieve_operand_class *opclass, sieve_size_t *address,
	struct sieve_object *obj)
{
	struct sieve_operand operand;

	if ( !sieve_operand_read(renv->sblock, address, NULL, &operand) ) {
		return FALSE;
	}

	return sieve_opr_object_read_data
		(renv->sblock, &operand, opclass, address, obj);
}

bool sieve_opr_object_dump
(const struct sieve_dumptime_env *denv,
	const struct sieve_operand_class *opclass, sieve_size_t *address,
	struct sieve_object *obj)
{
	struct sieve_operand operand;
	struct sieve_object obj_i;
	const char *class;

	if ( obj == NULL )
		obj = &obj_i;

	sieve_code_mark(denv);

	if ( !sieve_operand_read(denv->sblock, address, NULL, &operand) ) {
		return FALSE;
	}

	if ( !sieve_opr_object_read_data
		(denv->sblock, &operand, opclass, address, obj) )
		return FALSE;

	if ( operand.def->class == NULL )
		class = "OBJECT";
	else
		class = operand.def->class->name;

	sieve_code_dumpf(denv, "%s: %s", class, obj->def->identifier);

	return TRUE;
}