File: cmp-i-octet.c

package info (click to toggle)
dovecot 1%3A2.2.33.2-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports-sloppy
  • size: 50,420 kB
  • sloc: ansic: 449,977; sh: 9,653; makefile: 6,792; cpp: 1,557; perl: 295; python: 73; xml: 44; pascal: 27
file content (97 lines) | stat: -rw-r--r-- 2,006 bytes parent folder | download
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
/* Copyright (c) 2002-2017 Pigeonhole authors, see the included COPYING file
 */

/* Comparator 'i;octet':
 *
 */

#include "lib.h"

#include "sieve-common.h"
#include "sieve-comparators.h"

#include <string.h>
#include <stdio.h>

/*
 * Forward declarations
 */

static int cmp_i_octet_compare
	(const struct sieve_comparator *cmp,
		const char *val1, size_t val1_size, const char *val2, size_t val2_size);
static bool cmp_i_octet_char_match
	(const struct sieve_comparator *cmp, const char **val1, const char *val1_end,
		const char **val2, const char *val2_end);

/*
 * Comparator object
 */

const struct sieve_comparator_def i_octet_comparator = {
	SIEVE_OBJECT("i;octet",
		&comparator_operand, SIEVE_COMPARATOR_I_OCTET),
	.flags =
		SIEVE_COMPARATOR_FLAG_ORDERING |
		SIEVE_COMPARATOR_FLAG_EQUALITY |
		SIEVE_COMPARATOR_FLAG_SUBSTRING_MATCH |
		SIEVE_COMPARATOR_FLAG_PREFIX_MATCH,
	.compare = cmp_i_octet_compare,
	.char_match = cmp_i_octet_char_match,
	.char_skip = sieve_comparator_octet_skip
};

/*
 * Comparator implementation
 */

static int cmp_i_octet_compare(
	const struct sieve_comparator *cmp ATTR_UNUSED,
	const char *val1, size_t val1_size, const char *val2, size_t val2_size)
{
	int result;

	if ( val1_size == val2_size ) {
		return memcmp((void *) val1, (void *) val2, val1_size);
	}

	if ( val1_size > val2_size ) {
		result = memcmp((void *) val1, (void *) val2, val2_size);

		if ( result == 0 ) return 1;

		return result;
	}

	result = memcmp((void *) val1, (void *) val2, val1_size);

	if ( result == 0 ) return -1;

	return result;
}

static bool cmp_i_octet_char_match
	(const struct sieve_comparator *cmp ATTR_UNUSED,
		const char **val, const char *val_end,
		const char **key, const char *key_end)
{
	const char *val_begin = *val;
	const char *key_begin = *key;

	while ( **val == **key && *val < val_end && *key < key_end ) {
		(*val)++;
		(*key)++;
	}

	if ( *key < key_end ) {
		/* Reset */
		*val = val_begin;
		*key = key_begin;

		return FALSE;
	}

	return TRUE;
}