forked from petere/pgemailaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailaddr.c
246 lines (202 loc) · 5.88 KB
/
emailaddr.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <postgres.h>
#include <fmgr.h>
#include <utils/builtins.h>
PG_MODULE_MAGIC;
/*
* Internal storage format for email addresses: The data field
* contains the domain followed by the local part. (The '@' is not
* stored.) The local_offset field records where the local part
* starts. So for example, '[email protected]' is stored
* as { 14, 'postgresql.orgpgsql-hackers' }.
*
* This takes the same amount of space as the original but gives us a
* preparsed form that is easier for sorting and processing. The
* local_offset only needs to be 1 byte because domain names are
* restricted to 255 bytes in length (RFC 1034, RFC 1035, RFC 1123).
*/
typedef struct emailadr {
char vl_len_[4];
uint8 local_offset;
char data[FLEXIBLE_ARRAY_MEMBER];
} emailaddr;
#define DatumGetEmailAddrP(X) ((emailaddr *) PG_DETOAST_DATUM(X))
#define DatumGetEmailAddrPP(X) ((emailaddr *) PG_DETOAST_DATUM_PACKED(X))
#define EmailAddrPGetDatum(X) PointerGetDatum(X)
#define PG_GETARG_EMAILADDR_P(n) DatumGetEmailAddrP(PG_GETARG_DATUM(n))
#define PG_GETARG_EMAILADDR_PP(n) DatumGetEmailAddrPP(PG_GETARG_DATUM(n))
#define PG_RETURN_EMAILADDR_P(x) PG_RETURN_POINTER(x)
static bool
is_atext(char c)
{
return ((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9')
|| c == '!' || c == '#'
|| c == '$' || c == '%'
|| c == '&' || c == '\''
|| c == '*' || c == '+'
|| c == '-' || c == '/'
|| c == '=' || c == '?'
|| c == '^' || c == '_'
|| c == '`' || c == '{'
|| c == '|' || c == '}'
|| c == '~'
);
}
static void
parse_dot_atom(const char *s, size_t len)
{
size_t i;
for (i = 0; i < len; i++)
if (!is_atext(s[i]) && s[i] != '.')
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type emailaddr: invalid character \"%c\"", s[i])));
}
static void
parse_domain_literal(const char *s, size_t len)
{
size_t i;
for (i = 1; i < len - 1; i++)
switch (s[i])
{
case '[':
case ']':
case '\\':
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type emailaddr: invalid character \"%c\"", s[i])));
}
if (s[len - 1] != ']')
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type emailaddr: missing matching \"]\"")));
}
PG_FUNCTION_INFO_V1(emailaddr_in);
Datum
emailaddr_in(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0);
emailaddr *result;
char *p;
size_t len;
int32 result_len;
size_t domain_len;
p = strchr(s, '@');
if (!p)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type emailaddr: missing \"@\"")));
len = strlen(s);
domain_len = len - (p - s) - 1;
if (domain_len > 255)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type emailaddr: domain too long")));
parse_dot_atom(s, (p - s));
if (*(p + 1) == '[')
parse_domain_literal(p + 1, domain_len);
else
parse_dot_atom(p + 1, domain_len);
result_len = offsetof(emailaddr, data) + len - 1;
result = palloc(result_len);
SET_VARSIZE(result, result_len);
result->local_offset = domain_len;
memcpy(result->data, p + 1, domain_len);
memcpy(result->data + domain_len, s, (p - s));
PG_RETURN_EMAILADDR_P(result);
}
PG_FUNCTION_INFO_V1(emailaddr_out);
Datum
emailaddr_out(PG_FUNCTION_ARGS)
{
emailaddr *arg = PG_GETARG_EMAILADDR_P(0);
char *result;
size_t result_len;
size_t local_len;
result_len = VARSIZE(arg) - offsetof(emailaddr, data) + 1 + 1;
result = palloc(result_len);
local_len = VARSIZE(arg) - offsetof(emailaddr, data) - arg->local_offset;
memcpy(result, arg->data + arg->local_offset, local_len);
result[local_len] = '@';
memcpy(result + local_len + 1, arg->data, arg->local_offset);
result[result_len - 1] = '\0';
PG_RETURN_CSTRING(result);
}
static int
strnncmp(const char *s1, size_t n1, const char *s2, size_t n2)
{
int res;
res = strncmp(s1, s2, Min(n1, n2));
if (res != 0)
return res;
res = n1 - n2;
return res;
}
static int
_emailaddr_cmp(emailaddr *a, emailaddr *b)
{
int res;
res = strnncmp(a->data, a->local_offset, b->data, b->local_offset);
if (res != 0)
return res;
res = strnncmp(a->data + a->local_offset, VARSIZE(a) - offsetof(emailaddr, data) - a->local_offset,
b->data + b->local_offset, VARSIZE(b) - offsetof(emailaddr, data) - b->local_offset);
return res;
}
PG_FUNCTION_INFO_V1(emailaddr_lt);
Datum
emailaddr_lt(PG_FUNCTION_ARGS)
{
emailaddr *arg1 = PG_GETARG_EMAILADDR_PP(0);
emailaddr *arg2 = PG_GETARG_EMAILADDR_PP(1);
PG_RETURN_BOOL(_emailaddr_cmp(arg1, arg2) < 0);
}
PG_FUNCTION_INFO_V1(emailaddr_le);
Datum
emailaddr_le(PG_FUNCTION_ARGS)
{
emailaddr *arg1 = PG_GETARG_EMAILADDR_PP(0);
emailaddr *arg2 = PG_GETARG_EMAILADDR_PP(1);
PG_RETURN_BOOL(_emailaddr_cmp(arg1, arg2) <= 0);
}
PG_FUNCTION_INFO_V1(emailaddr_eq);
Datum
emailaddr_eq(PG_FUNCTION_ARGS)
{
emailaddr *arg1 = PG_GETARG_EMAILADDR_PP(0);
emailaddr *arg2 = PG_GETARG_EMAILADDR_PP(1);
PG_RETURN_BOOL(_emailaddr_cmp(arg1, arg2) == 0);
}
PG_FUNCTION_INFO_V1(emailaddr_ne);
Datum
emailaddr_ne(PG_FUNCTION_ARGS)
{
emailaddr *arg1 = PG_GETARG_EMAILADDR_PP(0);
emailaddr *arg2 = PG_GETARG_EMAILADDR_PP(1);
PG_RETURN_BOOL(_emailaddr_cmp(arg1, arg2) != 0);
}
PG_FUNCTION_INFO_V1(emailaddr_ge);
Datum
emailaddr_ge(PG_FUNCTION_ARGS)
{
emailaddr *arg1 = PG_GETARG_EMAILADDR_PP(0);
emailaddr *arg2 = PG_GETARG_EMAILADDR_PP(1);
PG_RETURN_BOOL(_emailaddr_cmp(arg1, arg2) >= 0);
}
PG_FUNCTION_INFO_V1(emailaddr_gt);
Datum
emailaddr_gt(PG_FUNCTION_ARGS)
{
emailaddr *arg1 = PG_GETARG_EMAILADDR_PP(0);
emailaddr *arg2 = PG_GETARG_EMAILADDR_PP(1);
PG_RETURN_BOOL(_emailaddr_cmp(arg1, arg2) > 0);
}
PG_FUNCTION_INFO_V1(emailaddr_cmp);
Datum
emailaddr_cmp(PG_FUNCTION_ARGS)
{
emailaddr *arg1 = PG_GETARG_EMAILADDR_PP(0);
emailaddr *arg2 = PG_GETARG_EMAILADDR_PP(1);
PG_RETURN_INT32(_emailaddr_cmp(arg1, arg2));
}