File: sieve-binary-private.h

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 (209 lines) | stat: -rw-r--r-- 5,342 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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#ifndef __SIEVE_BINARY_PRIVATE_H
#define __SIEVE_BINARY_PRIVATE_H

#include "sieve-common.h"
#include "sieve-binary.h"
#include "sieve-extensions.h"

#include <sys/stat.h>

/*
 * Binary file
 */

struct sieve_binary_file {
	pool_t pool;
	const char *path;
	struct sieve_instance *svinst;

	struct stat st;
	int fd;
	off_t offset;

	const void *(*load_data)
		(struct sieve_binary_file *file, off_t *offset, size_t size);
	buffer_t *(*load_buffer)
		(struct sieve_binary_file *file, off_t *offset, size_t size);
};

bool sieve_binary_file_open
	(struct sieve_binary_file *file, struct sieve_instance *svinst,
		const char *path, enum sieve_error *error_r);
void sieve_binary_file_close(struct sieve_binary_file **file);

/*
 * Internal structures
 */

/* Extension registration */

struct sieve_binary_extension_reg {
	/* The identifier of the extension within this binary */
	int index;

	/* Global extension object */
	const struct sieve_extension *extension;

	/* Extension to the binary; typically used to manage extension-specific blocks
	 * in the binary and as a means to get a binary_free notification to release
	 * references held by extensions.
	 */
	const struct sieve_binary_extension *binext;

	/* Context data associated to the binary by this extension */
	void *context;

	/* Main block for this extension */
	unsigned int block_id;
};

/* Block */

struct sieve_binary_block {
	struct sieve_binary *sbin;
	unsigned int id;
	int ext_index;

	buffer_t *data;

	uoff_t offset;
};

/*
 * Binary object
 */

struct sieve_binary {
	pool_t pool;
	int refcount;

	struct sieve_instance *svinst;

	struct sieve_script *script;

	struct sieve_binary_file *file;

	/* When the binary is loaded into memory or when it is being constructed by
	 * the generator, extensions can be associated to the binary. The extensions
	 * array is a sequential list of all linked extensions. The extension_index
	 * array is a mapping ext_id -> binary_extension. This is used to obtain the
	 * index code associated with an extension for this particular binary. The
	 * linked_extensions list all extensions linked to this binary object other
	 * than the preloaded language features implemented as 'extensions'.
	 *
	 * All arrays refer to the same extension registration objects. Upon loading
	 * a binary, the 'require'd extensions will sometimes need to associate
	 * context data to the binary object in memory. This is stored in these
	 * registration objects as well.
	 */
	ARRAY(struct sieve_binary_extension_reg *) extensions;
	ARRAY(struct sieve_binary_extension_reg *) extension_index;
	ARRAY(struct sieve_binary_extension_reg *) linked_extensions;

	/* Attributes of a loaded binary */
	const char *path;

	/* Blocks */
	ARRAY(struct sieve_binary_block *) blocks;
};

struct sieve_binary *sieve_binary_create
	(struct sieve_instance *svinst, struct sieve_script *script);

/* Blocks management */

static inline struct sieve_binary_block *sieve_binary_block_index
(struct sieve_binary *sbin, unsigned int id)
{
	struct sieve_binary_block * const *sblock;

	if  ( id >= array_count(&sbin->blocks) )
		return NULL;

	sblock = array_idx(&sbin->blocks, id);

	if ( *sblock == NULL ) {
		return NULL;
	}

	return *sblock;
}

static inline size_t _sieve_binary_block_get_size
(const struct sieve_binary_block *sblock)
{
	return buffer_get_used_size(sblock->data);
}

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

buffer_t *sieve_binary_block_get_buffer
	(struct sieve_binary_block *sblock);

/* Extension registration */

static inline struct sieve_binary_extension_reg *
	sieve_binary_extension_create_reg
(struct sieve_binary *sbin, const struct sieve_extension *ext)
{
	int index = array_count(&sbin->extensions);
	struct sieve_binary_extension_reg *ereg;

	if ( ext->id < 0 ) return NULL;

	ereg = p_new(sbin->pool, struct sieve_binary_extension_reg, 1);
	ereg->index = index;
	ereg->extension = ext;

	array_idx_set(&sbin->extensions, (unsigned int) index, &ereg);
	array_idx_set(&sbin->extension_index, (unsigned int) ext->id, &ereg);

	return ereg;
}

static inline struct sieve_binary_extension_reg *sieve_binary_extension_get_reg
(struct sieve_binary *sbin, const struct sieve_extension *ext, bool create)
{
	struct sieve_binary_extension_reg *reg = NULL;

	if ( ext->id >= 0 && ext->id < (int) array_count(&sbin->extension_index) ) {
		struct sieve_binary_extension_reg * const *ereg =
			array_idx(&sbin->extension_index, (unsigned int) ext->id);

		reg = *ereg;
	}

	/* Register if not known */
	if ( reg == NULL && create )
		return sieve_binary_extension_create_reg(sbin, ext);

	return reg;
}

static inline int sieve_binary_extension_register
(struct sieve_binary *sbin, const struct sieve_extension *ext,
	struct sieve_binary_extension_reg **reg_r)
{
	struct sieve_binary_extension_reg *ereg;

	if ( (ereg=sieve_binary_extension_get_reg(sbin, ext, FALSE)) == NULL ) {
		ereg = sieve_binary_extension_create_reg(sbin, ext);

		if ( ereg == NULL ) return -1;

		array_append(&sbin->linked_extensions, &ereg, 1);
	}

	if ( reg_r != NULL ) *reg_r = ereg;
	return ereg->index;
}

/* Load/Save */

bool sieve_binary_load_block(struct sieve_binary_block *);

#endif /* __SIEVE_BINARY_PRIVATE_H */