File: ext-cmp-i-ascii-numeric.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 (160 lines) | stat: -rw-r--r-- 3,339 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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

/* Extension comparator-i;ascii-numeric
 * ------------------------------------
 *
 * Author: Stephan Bosch
 * Specification: RFC 2244
 * Implementation: full
 * Status: testing
 *
 */

#include "sieve-common.h"

#include "sieve-code.h"
#include "sieve-extensions.h"
#include "sieve-comparators.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-interpreter.h"

#include <ctype.h>

/*
 * Forward declarations
 */

static const struct sieve_operand_def my_comparator_operand;

const struct sieve_comparator_def i_ascii_numeric_comparator;

/*
 * Extension
 */

static bool ext_cmp_i_ascii_numeric_validator_load
	(const struct sieve_extension *ext, struct sieve_validator *validator);

const struct sieve_extension_def comparator_i_ascii_numeric_extension = {
	.name = "comparator-i;ascii-numeric",
	.validator_load = ext_cmp_i_ascii_numeric_validator_load,
	SIEVE_EXT_DEFINE_OPERAND(my_comparator_operand)
};

static bool ext_cmp_i_ascii_numeric_validator_load
(const struct sieve_extension *ext, struct sieve_validator *validator)
{
	sieve_comparator_register(validator, ext, &i_ascii_numeric_comparator);
	return TRUE;
}

/*
 * Operand
 */

static const struct sieve_extension_objects ext_comparators =
	SIEVE_EXT_DEFINE_COMPARATOR(i_ascii_numeric_comparator);

static const struct sieve_operand_def my_comparator_operand = {
	"comparator-i;ascii-numeric",
	&comparator_i_ascii_numeric_extension,
	0,
	&sieve_comparator_operand_class,
	&ext_comparators
};

/*
 * Comparator
 */

/* Forward declarations */

static int cmp_i_ascii_numeric_compare
	(const struct sieve_comparator *cmp,
		const char *val1, size_t val1_size, const char *val2, size_t val2_size);

/* Comparator object */

const struct sieve_comparator_def i_ascii_numeric_comparator = {
	SIEVE_OBJECT("i;ascii-numeric", &my_comparator_operand, 0),
	SIEVE_COMPARATOR_FLAG_ORDERING | SIEVE_COMPARATOR_FLAG_EQUALITY,
	cmp_i_ascii_numeric_compare,
	NULL,
	NULL
};

/* Comparator implementation */

static int cmp_i_ascii_numeric_compare
	(const struct sieve_comparator *cmp ATTR_UNUSED,
		const char *val, size_t val_size, const char *key, size_t key_size)
{
	const char *vend = val + val_size;
	const char *kend = key + key_size;
	const char *vp = val;
	const char *kp = key;
	int digits, i;

	/* RFC 4790: All input is valid; strings that do not start with a digit
	 * represent positive infinity.
	 */
	if ( !i_isdigit(*vp) ) {
		if ( i_isdigit(*kp) ) {
			/* Value is greater */
			return 1;
		}
	} else {
		if ( !i_isdigit(*kp) ) {
			/* Value is less */
			return -1;
		}
	}

	/* Ignore leading zeros */

	while ( *vp == '0' && vp < vend )
		vp++;

	while ( *kp == '0' && kp < kend )
		kp++;

	/* Check whether both numbers are equally long in terms of digits */

	digits = 0;
	while ( vp < vend && kp < kend && i_isdigit(*vp) && i_isdigit(*kp) ) {
		vp++;
		kp++;
		digits++;
	}

	if ( vp == vend || !i_isdigit(*vp) ) {
		if ( kp != kend && i_isdigit(*kp) ) {
			/* Value is less */
			return -1;
		}
	} else {
		/* Value is greater */
		return 1;
	}

	/* Equally long: compare digits */

	vp -= digits;
	kp -= digits;
	i = 0;
	while ( i < digits ) {
		if ( *vp > *kp )
			return 1;
		else if ( *vp < *kp )
			return -1;

		kp++;
		vp++;
		i++;
	}

	return 0;
}