File: mcht-value.c

package info (click to toggle)
dovecot 1%3A2.3.4.1-5%2Bdeb10u6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 48,648 kB
  • sloc: ansic: 500,433; makefile: 7,372; sh: 5,592; cpp: 1,555; perl: 303; python: 73; xml: 44; pascal: 27
file content (80 lines) | stat: -rw-r--r-- 2,134 bytes parent folder | download | duplicates (5)
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
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

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

#include "sieve-common.h"

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

#include "ext-relational-common.h"

/*
 * Match-type objects
 */

const struct sieve_match_type_def value_match_type = {
	SIEVE_OBJECT("value",
		&rel_match_type_operand, RELATIONAL_VALUE),
	.validate = mcht_relational_validate
};

#define VALUE_MATCH_TYPE(name, rel_match)                       \
const struct sieve_match_type_def rel_match_value_ ## name = {  \
	SIEVE_OBJECT("value-" #name,                                  \
		&rel_match_type_operand,                                    \
		REL_MATCH_INDEX(RELATIONAL_VALUE, rel_match)),              \
	.match_key = mcht_value_match_key,                            \
}

VALUE_MATCH_TYPE(gt, REL_MATCH_GREATER);
VALUE_MATCH_TYPE(ge, REL_MATCH_GREATER_EQUAL);
VALUE_MATCH_TYPE(lt, REL_MATCH_LESS);
VALUE_MATCH_TYPE(le, REL_MATCH_LESS_EQUAL);
VALUE_MATCH_TYPE(eq, REL_MATCH_EQUAL);
VALUE_MATCH_TYPE(ne, REL_MATCH_NOT_EQUAL);

/*
 * Match-type implementation
 */

int mcht_value_match_key
(struct sieve_match_context *mctx, const char *val, size_t val_size,
	const char *key, size_t key_size)
{
	const struct sieve_match_type *mtch = mctx->match_type;
	unsigned int rel_match = REL_MATCH(mtch->object.def->code);
	int cmp_result;

	cmp_result = mctx->comparator->def->
		compare(mctx->comparator, val, val_size, key, key_size);

	switch ( rel_match ) {
	case REL_MATCH_GREATER:
		return ( cmp_result > 0 ? 1 : 0 );
	case REL_MATCH_GREATER_EQUAL:
		return ( cmp_result >= 0 ? 1 : 0 );
	case REL_MATCH_LESS:
		return ( cmp_result < 0 ? 1 : 0 );
	case REL_MATCH_LESS_EQUAL:
		return ( cmp_result <= 0 ? 1 : 0 );
	case REL_MATCH_EQUAL:
		return ( cmp_result == 0 ? 1 : 0);
	case REL_MATCH_NOT_EQUAL:
		return ( cmp_result != 0 ? 1 : 0);
	}

	i_unreached();
}