-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmfs_imap_parse.c
More file actions
672 lines (535 loc) · 12.2 KB
/
mfs_imap_parse.c
File metadata and controls
672 lines (535 loc) · 12.2 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/kref.h>
#include <linux/ctype.h>
#include "mfs_imap_parse.h"
#ifndef DEBUG
#define IMAP_DBG(...)
#else
#define IMAP_DBG(...) pr_err("[MFS/IMAP_PARSE]: " __VA_ARGS__)
#endif
#define assert(...)
/**
* TODO: one mfs_imap_elt_new_* create for each type
*/
/**
* Parsing syntax
*/
#define IS_CTL(c) (((c) >= 0 && (c) <= 31) || (c) == 127)
#define IS_LISTWC(c) ((c) == '%' || (c) == '*')
#define IS_QUOTEDSPE(c) ((c) == '"')
#define IS_RESPSPE(c) ((c) == ']')
#define IS_CR(c) ((c) == '\r')
#define IS_LF(c) ((c) == '\n')
#define IS_WHITESPE(c) ((c) == ' ' || IS_CTL(c) || IS_RESPSPE(c))
#define IS_NIL(s, l) \
(((l) > strlen("NIL")) && (memcmp(s, "NIL", strlen("NIL")) == 0))
#define IS_ATOMSPE(c) \
((c) == '(' || (c) == ')' || (c) == '{' || (c) == ' ' || \
IS_CTL(c)|| IS_QUOTEDSPE(c))
/**
* Stack helper to avoid recursive functions while parsing imap lists
*/
#define DEFINE_ISTACK(name) \
struct imap_parse_stack name = { \
.idx = 0, \
.st = {} \
}
#define INIT_ISTACK(s) do { \
(s)->idx = 0; \
(s)->st[0] = NULL; \
} while(/*CONSTCOND*/0)
#define POP_ISTACK(s) ((s)->st[(s)->idx--])
#define SPACE_ISTACK(s) ((s)->idx < ISTACK_MAX - 1)
#define EMPTY_ISTACK(s) ((s)->idx == 0)
#define PUSH_ISTACK(s, p) ((s)->st[++(s)->idx] = p)
#define IMAP_BEGIN_LIST(c, im, p, l) do { \
if(SPACE_ISTACK(&(c)->st)) { \
struct imap_elt *__in = mfs_imap_elt_new_list(); \
if(__in != NULL) { \
PUSH_ISTACK(&(c)->st, im); \
list_add_tail(&__in->next, &im->elt); \
im = IMAP_ELT_MSG(__in); \
} \
} \
++(*(p)); \
--(*(l)); \
} while(/*CONSTCOND*/0)
#define IMAP_END_LIST(c, im, p, l) do { \
if(!EMPTY_ISTACK(&(c)->st)) \
im = POP_ISTACK(&(c)->st); \
++(*(p)); \
--(*(l)); \
} while(/*CONSTCOND*/0)
static inline struct imap_elt *mfs_imap_elt_new(enum imap_elt_type type,
size_t len)
{
struct imap_elt *res;
res = kmalloc(sizeof(*res) + len, GFP_KERNEL);
if(res == NULL)
return NULL;
res->type = type;
return res;
}
static inline struct imap_elt *mfs_imap_elt_resize(struct imap_elt *e,
size_t size)
{
return krealloc(e, sizeof(*e) + size, GFP_KERNEL);
}
static inline void mfs_imap_elt_free(struct imap_elt *m)
{
kfree(m);
}
static inline void mfs_imap_msg_init(struct imap_msg *m)
{
INIT_LIST_HEAD(&m->elt);
kref_init(&m->refcnt);
}
static inline struct imap_msg *mfs_imap_msg_new(void)
{
struct imap_msg *m;
m = kmalloc(sizeof(*m), GFP_KERNEL);
if(m == NULL)
goto out;
mfs_imap_msg_init(m);
out:
return m;
}
static inline void mfs_imap_msg_free(struct imap_msg *m)
{
kfree(m);
}
static inline void mfs_imap_msg_destroy(struct kref *k)
{
struct imap_msg *m = container_of(k, struct imap_msg, refcnt);
struct imap_msg *p = m;
struct imap_elt *elt;
DEFINE_ISTACK(st);
while(!list_empty(&p->elt) || !EMPTY_ISTACK(&st)) {
if(list_empty(&p->elt))
p = POP_ISTACK(&st);
elt = list_first_entry(&p->elt, struct imap_elt, next);
if(elt->type == IET_LIST &&
!list_empty(&IMAP_ELT_MSG(elt)->elt)) {
assert(SPACE_ISTACK(&st));
PUSH_ISTACK(&st, p);
p = IMAP_ELT_MSG(elt);
} else {
list_del(&elt->next);
mfs_imap_elt_free(elt);
}
}
assert(p == m);
mfs_imap_msg_free(m);
}
static inline struct imap_elt *mfs_imap_elt_new_list(void)
{
struct imap_elt *m;
m = mfs_imap_elt_new(IET_LIST, sizeof(struct imap_msg));
if(m == NULL)
goto out;
mfs_imap_msg_init(IMAP_ELT_MSG(m));
out:
return m;
}
static inline struct imap_elt *mfs_imap_parse_number(struct imap_parse_ctx *c,
char const **p, size_t *len)
{
struct imap_elt *new;
int nb = 0, *ptr;
new = mfs_imap_elt_new(IET_NUMBER, sizeof(unsigned int));
if(new == NULL)
goto out;
while(*len != 0 && isdigit(**p)) {
nb = nb * 10 + **p - '0';
++(*p);
--(*len);
}
IMAP_DBG("NUMBER %u\n", nb);
ptr = IMAP_ELT_NUM(new);
*ptr = nb;
out:
if(*len == 0) {
c->elt = new;
new = NULL;
}
return new;
}
static inline
struct imap_elt *mfs_imap_parse_number_cont(struct imap_parse_ctx *c,
char const **p, size_t *len)
{
struct imap_elt *new = c->elt;
int nb = *IMAP_ELT_NUM(new), *ptr;
while(*len != 0 && isdigit(**p)) {
nb = nb * 10 + **p - '0';
++(*p);
--(*len);
}
IMAP_DBG("NUMBER - CONT %u\n", nb);
ptr = IMAP_ELT_NUM(new);
*ptr = nb;
if(*len == 0) {
c->elt = new;
new = NULL;
}
return new;
}
static inline struct imap_elt *mfs_imap_parse_string(struct imap_parse_ctx *c,
char const **p, size_t *len)
{
struct imap_elt *new = NULL;
char const *s;
size_t l = 0;
assert(**p == '"');
/**
* Skip first quote
*/
++(*p);
--(*len);
s = *p;
/**
* Find ending quote
*/
for(l = 0; l < *len && s[l] != '"'; ++l);
new = mfs_imap_elt_new(IET_STRING, l + 1);
if(new == NULL)
goto skip_quote;
IMAP_DBG("STRING %.*s\n", (int)l, s);
memcpy(IMAP_ELT_STR(new), s, l);
IMAP_ELT_STR(new)[l] = '\0';
skip_quote:
if(s[l] != '"') {
c->elt = new;
new = NULL;
} else {
++l;
}
(*len) -= l;
(*p) += l;
return new;
}
static inline struct imap_elt *
mfs_imap_parse_string_cont(struct imap_parse_ctx *c, char const **p,
size_t *len)
{
struct imap_elt *new = c->elt;
char const *s = *p;
size_t l = 0, curlen = strlen(IMAP_ELT_STR(new));
/**
* Find ending quote
*/
for(l = 0; l < *len && s[l] != '"'; ++l);
new = mfs_imap_elt_resize(new, curlen + l + 1);
if(new == NULL) {
kfree(c->elt);
goto skip_quote;
}
IMAP_DBG("STRING - CONT %.*s\n", (int)l, s);
memcpy(IMAP_ELT_STR(new) + curlen, s, l);
IMAP_ELT_STR(new)[curlen + l] = '\0';
skip_quote:
if(s[l] != '"') {
c->elt = new;
new = NULL;
} else {
++l;
}
(*len) -= l;
(*p) += l;
return new;
}
static inline struct imap_elt *mfs_imap_parse_literal(struct imap_parse_ctx *c,
char const **p, size_t *len)
{
struct imap_elt *new = NULL;
char const *s = *p;
size_t nb = 0, l;
for(l = 1; l < *len && isdigit(s[l]); ++l)
nb = nb * 10 + (*p)[l] - '0';
if(l > *len - 3 || s[l] != '}' || !IS_CR(s[l + 1]) || !IS_LF(s[l + 2]))
goto out;
(*len) -= l + 3;
(*p) += l + 3;
l = min(*len, nb);
if(l == 0)
goto out;
new = mfs_imap_elt_new(IET_STRING, nb + 1);
if(new == NULL)
goto out;
IMAP_DBG("LITERAL {%lu/%lu} - %.*s\n", l, nb, (int)l, *p);
memcpy(IMAP_ELT_STR(new), *p, l);
IMAP_ELT_STR(new)[l] = '\0';
if(l < nb) {
c->more = nb - l;
c->elt = new;
new = NULL;
}
out:
(*len) -= l;
(*p) += l;
return new;
}
static inline struct imap_elt *
mfs_imap_parse_literal_cont(struct imap_parse_ctx *c, char const **p,
size_t *len)
{
struct imap_elt *new = c->elt;
size_t l = 0, curlen;
l = min(*len, c->more);
curlen = strlen(IMAP_ELT_STR(new));
IMAP_DBG("LITERAL CONT {%lu/%lu} - %.*s\n", l, c->more, (int)l, *p);
memcpy(IMAP_ELT_STR(new) + curlen, *p, l);
IMAP_ELT_STR(new)[curlen + l] = '\0';
c->more -= l;
if(c->more)
new = NULL;
(*len) -= l;
(*p) += l;
return new;
}
static inline struct imap_elt *mfs_imap_parse_nil(struct imap_parse_ctx *c,
char const **p, size_t *len)
{
struct imap_elt *new = NULL;
size_t l = strlen("NIL");
assert(IS_NIL(*p, *len));
new = mfs_imap_elt_new(IET_NIL, 0);
IMAP_DBG("NIL\n");
(*len) -= l;
(*p) += l;
return new;
}
static inline struct imap_elt *mfs_imap_parse_atom(struct imap_parse_ctx *c,
char const **p, size_t *len)
{
struct imap_elt *new = NULL;
char const *s = *p;
size_t l;
for(l = 0; l < *len && !IS_ATOMSPE(s[l]); ++l);
new = mfs_imap_elt_new(IET_ATOM, l + 1);
if(new == NULL)
goto out;
new->type = IET_ATOM;
IMAP_DBG("ATOM %.*s\n", (int)l, *p);
memcpy(IMAP_ELT_ATOM(new), *p, l);
IMAP_ELT_ATOM(new)[l] = '\0';
if(l == *len) {
c->elt = new;
new = NULL;
}
out:
(*len) -= l;
(*p) += l;
return new;
}
static inline struct imap_elt *
mfs_imap_parse_atom_cont(struct imap_parse_ctx *c, char const **p, size_t *len)
{
struct imap_elt *new = c->elt;
char const *s = *p;
size_t l, curlen = strlen(IMAP_ELT_ATOM(new));
for(l = 0; l < *len && !IS_ATOMSPE(s[l]); ++l);
new = mfs_imap_elt_resize(new, curlen + l + 1);
if(new == NULL) {
kfree(c->elt);
goto out;
}
IMAP_DBG("ATOM - CONT %.*s\n", (int)l, *p);
memcpy(IMAP_ELT_ATOM(new) + curlen, *p, l);
IMAP_ELT_ATOM(new)[curlen + l] = '\0';
if(l == *len) {
c->elt = new;
new = NULL;
}
out:
(*len) -= l;
(*p) += l;
return new;
}
static inline void mfs_imap_skip_atomspe(struct imap_parse_ctx *c,
char const **p, size_t *len)
{
pr_err("[MFS/IMAP_PARSE]: %c is not supported\n", (*p)[0]);
++(*p);
--(*len);
}
static inline void mfs_imap_parse_reset_ctx(struct imap_parse_ctx *c)
{
INIT_ISTACK(&c->st);
c->first = NULL;
c->msg = NULL;
c->elt = NULL;
c->more = 0;
c->cr = 0;
}
static inline struct imap_elt *mfs_imap_parse_cont(struct imap_parse_ctx *c,
char const **p, size_t *len)
{
struct imap_elt *ret = NULL;
if(c->elt == NULL)
return ret;
switch(c->elt->type) {
case IET_ATOM:
ret = mfs_imap_parse_atom_cont(c, p, len);
break;
case IET_STRING:
if(c->more)
ret = mfs_imap_parse_literal_cont(c, p, len);
else
ret = mfs_imap_parse_string_cont(c, p, len);
break;
case IET_NIL:
case IET_LIST:
case IET_NUMBER:
ret = mfs_imap_parse_number_cont(c, p, len);
break;
default:
IMAP_DBG("Wrong elt type\n");
mfs_imap_parse_reset_ctx(c);
}
return ret;
}
static inline int mfs_imap_parse_end(struct imap_parse_ctx *c,
char const **msg, size_t *len)
{
char const *p = *msg;
char const *end = p + *len;
size_t l = *len;
int lf = 0;
/**
* Skip aditional whitespaces
*/
while((p < end) && (IS_WHITESPE(*p)) && (c->cr == 0 || lf == 0)) {
/**
* CRLF is new message so finish this message
*/
if(c->cr == 1 && IS_LF(*p)) {
lf = 1;
c->cr = 0;
} else if(IS_CR(*p)) {
c->cr = 1;
} else if(c->cr == 1) {
c->cr = 0;
}
++p;
--l;
}
*msg = p;
*len = l;
if(l)
c->cr = 0;
return lf == 1;
}
/**
* Create a new context for imap parsing
*/
struct imap_parse_ctx *mfs_imap_parse_new_ctx(void)
{
struct imap_parse_ctx *ctx;
ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
if(ctx == NULL)
return ctx;
INIT_ISTACK(&ctx->st);
ctx->first = NULL;
ctx->msg = NULL;
ctx->elt = NULL;
ctx->more = 0;
ctx->cr = 0;
return ctx;
}
/**
* Delete a imap parsing context
*/
void mfs_imap_parse_del_ctx(struct imap_parse_ctx *ctx)
{
if(ctx->first)
mfs_imap_msg_put(ctx->first);
kfree(ctx);
}
/**
* Transform received message into structured imap message
*/
struct imap_msg *mfs_imap_parse_msg(struct imap_parse_ctx *ctx,
char const **msg, size_t *len)
{
struct imap_msg *im = ctx->msg, *first = ctx->first;
struct imap_elt *new;
char const *p = *msg, *end = *msg + *len;
size_t l = *len;
int msgend = 0;
if(im == NULL)
im = mfs_imap_msg_new();
if(im == NULL)
goto out;
/**
* If this is a multipacket message continue to fill old one
*/
if(first == NULL) {
first = im;
}
if(ctx->elt) {
new = mfs_imap_parse_cont(ctx, &p, &l);
if(new != NULL) {
list_add_tail(&new->next, &im->elt);
ctx->elt = NULL;
}
}
msgend = mfs_imap_parse_end(ctx, &p, &l);
while(p < end && !msgend) {
new = NULL;
if(isdigit(p[0]))
new = mfs_imap_parse_number(ctx, &p, &l);
else if(p[0] == '(')
IMAP_BEGIN_LIST(ctx, im, &p, &l);
else if(p[0] == ')')
IMAP_END_LIST(ctx, im, &p, &l);
else if(p[0] == '"')
new = mfs_imap_parse_string(ctx, &p, &l);
else if(p[0] == '{')
new = mfs_imap_parse_literal(ctx, &p, &l);
else if(IS_NIL(p, l))
new = mfs_imap_parse_nil(ctx, &p, &l);
else if(IS_ATOMSPE(p[0]))
mfs_imap_skip_atomspe(ctx, &p, &l);
else
new = mfs_imap_parse_atom(ctx, &p, &l);
msgend = mfs_imap_parse_end(ctx, &p, &l);
/**
* Append new imap element
*/
if(new != NULL)
list_add_tail(&new->next, &im->elt);
}
out:
/**
* Do garbage collecting
* XXX in well formed imap msg this should not happen
*/
if(!EMPTY_ISTACK(&ctx->st) && msgend) {
mfs_imap_msg_put(first);
im = ERR_PTR(-EINVAL);
}
/**
* Imap message is complete
*/
if(msgend) {
mfs_imap_parse_reset_ctx(ctx);
} else {
ctx->first = first;
ctx->msg = im;
im = NULL;
}
*msg = p;
*len = l;
return im;
}
int __must_check mfs_imap_msg_get(struct imap_msg *im)
{
return kref_get_unless_zero(&im->refcnt);
}
int mfs_imap_msg_put(struct imap_msg *im)
{
return kref_put(&im->refcnt, mfs_imap_msg_destroy);
}