summaryrefslogtreecommitdiff
path: root/src/mime-parsefull.cc
blob: faf02f00cbd3317c11d214a0e5748d360ded7073 (plain)
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
/**
 *  @file mime-parsefull.cc
 *  @brief Implementation of main mime parser components
 *  @author Andreas Aardal Hanssen
 *  @date 2002-2005
 */

#include "convert.h"
#include "mime-inputsource.h"
#include "mime-utils.h"
#include "mime.h"

#include <exception>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

Binc::MimeInputSource *mimeSource = nullptr;

using std::string;
using std::vector;

void Binc::MimeDocument::parseFull(int fd) const
{
  if (allIsParsed) return;

  allIsParsed = true;

  if (!mimeSource || mimeSource->getFileDescriptor() != fd) {
    delete mimeSource;
    mimeSource = new MimeInputSource(fd);
  } else {
    mimeSource->reset();
  }

  headerstartoffsetcrlf = 0;
  headerlength = 0;
  bodystartoffsetcrlf = 0;
  bodylength = 0;
  size = 0;
  messagerfc822 = false;
  multipart = false;

  int bsize = 0;
  string bound;
  MimePart::parseFull(bound, bsize);

  // eat any trailing junk to get the correct size
  char c;
  while (mimeSource->getChar(&c))
    ;

  size = mimeSource->getOffset();
}

static bool parseOneHeaderLine(Binc::Header *header, unsigned int *nlines)
{
  using namespace ::Binc;
  char c;
  bool eof = false;
  char cqueue[4];
  string name;
  string content;

  while (mimeSource->getChar(&c)) {
    // If we encounter a \r before we got to the first ':', then
    // rewind back to the start of the line and assume we're at the
    // start of the body.
    if (c == '\r') {
      for (int i = 0; i < (int)name.length() + 1; ++i)
        mimeSource->ungetChar();
      return false;
    }

    // A colon marks the end of the header name
    if (c == ':') break;

    // Otherwise add to the header name
    name += c;
  }

  cqueue[0] = '\0';
  cqueue[1] = '\0';
  cqueue[2] = '\0';
  cqueue[3] = '\0';

  // Read until the end of the header.
  bool endOfHeaders = false;
  while (!endOfHeaders) {
    if (!mimeSource->getChar(&c)) {
      eof = true;
      break;
    }

    if (c == '\n') ++*nlines;

    for (int i = 0; i < 3; ++i)
      cqueue[i] = cqueue[i + 1];
    cqueue[3] = c;

    if (strncmp(cqueue, "\r\n\r\n", 4) == 0) {
      endOfHeaders = true;
      break;
    }

    // If the last character was a newline, and the first now is not
    // whitespace, then rewind one character and store the current
    // key,value pair.
    if (cqueue[2] == '\n' && c != ' ' && c != '\t') {
      if (content.length() > 2) content.resize(content.length() - 2);

      trim(content);
      header->add(name, content);

      if (c != '\r') {
        mimeSource->ungetChar();
        if (c == '\n') --*nlines;
        return true;
      }

      mimeSource->getChar(&c);
      return false;
    }

    content += c;
  }

  if (name != "") {
    if (content.length() > 2) content.resize(content.length() - 2);
    header->add(name, content);
  }

  return !(eof || endOfHeaders);
}

static void parseHeader(Binc::Header *header, unsigned int *nlines)
{
  while (parseOneHeaderLine(header, nlines)) {
  }
}

static void analyzeHeader(Binc::Header *header,
                          bool *multipart,
                          bool *messagerfc822,
                          string *subtype,
                          string *boundary)
{
  using namespace ::Binc;

  // Do simple parsing of headers to determine the
  // type of message (multipart,messagerfc822 etc)
  HeaderItem ctype;
  if (header->getFirstHeader("content-type", ctype)) {
    vector<string> types;
    split(ctype.getValue(), ";", types);

    if (types.size() > 0) {
      // first element should describe content type
      string tmp = types[0];
      trim(tmp);
      vector<string> v;
      split(tmp, "/", v);
      string key, value;

      key = (v.size() > 0) ? v[0] : "text";
      value = (v.size() > 1) ? v[1] : "plain";
      lowercase(key);

      if (key == "multipart") {
        *multipart = true;
        lowercase(value);
        *subtype = value;
      } else if (key == "message") {
        lowercase(value);
        if (value == "rfc822") *messagerfc822 = true;
      }
    }

    for (vector<string>::const_iterator i = types.begin(); i != types.end(); ++i) {
      string element = *i;
      trim(element);

      if (element.find("=") != string::npos) {
        string::size_type pos = element.find('=');
        string key = element.substr(0, pos);
        string value = element.substr(pos + 1);

        lowercase(key);
        trim(key);

        if (key == "boundary") {
          trim(value, " \"");
          *boundary = value;
        }
      }
    }
  }
}

static void parseMessageRFC822(vector<Binc::MimePart> *members,
                               bool *foundendofpart,
                               unsigned int *bodylength,
                               unsigned int *nbodylines,
                               const string &toboundary)
{
  using namespace Binc;

  // message rfc822 means a completely enclosed mime document. we
  // call the parser recursively, and pass on the boundary string
  // that we got. when parse() finds this boundary, it returns 0. if
  // it finds the end boundary (boundary + "--"), it returns != 0.
  MimePart m;

  unsigned int bodystartoffsetcrlf = mimeSource->getOffset();

  // parsefull returns the number of bytes that need to be removed
  // from the body because of the terminating boundary string.
  int bsize = 0;
  if (m.parseFull(toboundary, bsize)) *foundendofpart = true;

  // make sure bodylength doesn't overflow
  *bodylength = mimeSource->getOffset();
  if (*bodylength >= bodystartoffsetcrlf) {
    *bodylength -= bodystartoffsetcrlf;
    if (*bodylength >= (unsigned int)bsize)
      *bodylength -= (unsigned int)bsize;
    else
      *bodylength = 0;
  } else {
    *bodylength = 0;
  }

  *nbodylines += m.getNofLines();

  members->push_back(m);
}

static bool skipUntilBoundary(const string &delimiter, unsigned int *nlines, bool *eof)
{
  int endpos = delimiter.length();
  char *delimiterqueue = nullptr;
  int delimiterpos = 0;
  const char *delimiterStr = delimiter.c_str();
  if (delimiter != "") {
    delimiterqueue = new char[endpos];
    memset(delimiterqueue, 0, endpos);
  }

  // first, skip to the first delimiter string. Anything between the
  // header and the first delimiter string is simply ignored (it's
  // usually a text message intended for non-mime clients)
  char c;

  bool foundBoundary = false;
  for (;;) {
    if (!mimeSource->getChar(&c)) {
      *eof = true;
      break;
    }

    if (c == '\n') ++*nlines;

    // if there is no delimiter, we just read until the end of the
    // file.
    if (!delimiterqueue) continue;

    delimiterqueue[delimiterpos++ % endpos] = c;

    if (compareStringToQueue(delimiterStr, delimiterqueue, delimiterpos, endpos)) {
      foundBoundary = true;
      break;
    }
  }

  delete[] delimiterqueue;
  delimiterqueue = nullptr;

  return foundBoundary;
}

static void parseMultipart(const string &boundary,
                           const string &toboundary,
                           bool *eof,
                           unsigned int *nlines,
                           int *boundarysize,
                           bool *foundendofpart,
                           unsigned int *bodylength,
                           vector<Binc::MimePart> *members)
{
  using namespace ::Binc;
  unsigned int bodystartoffsetcrlf = mimeSource->getOffset();

  // multipart parsing starts with skipping to the first
  // boundary. then we call parse() for all parts. the last parse()
  // command will return a code indicating that it found the last
  // boundary of this multipart. Note that the first boundary does
  // not have to start with CRLF.
  string delimiter = "--" + boundary;

  skipUntilBoundary(delimiter, nlines, eof);

  if (!eof) *boundarysize = delimiter.size();

  // Read two more characters. This may be CRLF, it may be "--" and
  // it may be any other two characters.

  char a;
  if (!mimeSource->getChar(&a)) *eof = true;
  if (a == '\n') ++*nlines;

  char b;
  if (!mimeSource->getChar(&b)) *eof = true;
  if (b == '\n') ++*nlines;

  // If we find two dashes after the boundary, then this is the end
  // of boundary marker.
  if (!*eof) {
    if (a == '-' && b == '-') {
      *foundendofpart = true;
      *boundarysize += 2;

      if (!mimeSource->getChar(&a)) *eof = true;
      if (a == '\n') ++*nlines;
      if (!mimeSource->getChar(&b)) *eof = true;
      if (b == '\n') ++*nlines;
    }

    if (a == '\r' && b == '\n') {
      // This exception is to handle a special case where the
      // delimiter of one part is not followed by CRLF, but
      // immediately followed by a CRLF prefixed delimiter.
      if (!mimeSource->getChar(&a) || !mimeSource->getChar(&b)) {
        *eof = true;
      } else if (a == '-' && b == '-') {
        mimeSource->ungetChar();
        mimeSource->ungetChar();
        mimeSource->ungetChar();
        mimeSource->ungetChar();
      } else {
        mimeSource->ungetChar();
        mimeSource->ungetChar();
      }

      *boundarysize += 2;
    } else {
      mimeSource->ungetChar();
      mimeSource->ungetChar();
    }
  }

  // read all mime parts.
  if (!*foundendofpart && !*eof) {
    bool quit = false;
    do {
      MimePart m;

      // If parseFull returns != 0, then it encountered the multipart's
      // final boundary.
      int bsize = 0;
      if (m.parseFull(boundary, bsize)) {
        quit = true;
        *boundarysize = bsize;
      }

      members->push_back(m);

    } while (!quit);
  }

  if (!*foundendofpart && !*eof) {
    // multipart parsing starts with skipping to the first
    // boundary. then we call parse() for all parts. the last parse()
    // command will return a code indicating that it found the last
    // boundary of this multipart. Note that the first boundary does
    // not have to start with CRLF.
    string delimiter = "\r\n--" + toboundary;

    skipUntilBoundary(delimiter, nlines, eof);

    if (!*eof) *boundarysize = delimiter.size();

    // Read two more characters. This may be CRLF, it may be "--" and
    // it may be any other two characters.

    char a = '\0';
    if (!mimeSource->getChar(&a)) *eof = true;
    if (a == '\n') ++*nlines;

    char b = '\0';
    if (!mimeSource->getChar(&b)) *eof = true;
    if (b == '\n') ++*nlines;

    // If we find two dashes after the boundary, then this is the end
    // of boundary marker.
    if (!*eof) {
      if (a == '-' && b == '-') {
        *foundendofpart = true;
        *boundarysize += 2;
        if (!mimeSource->getChar(&a)) *eof = true;
        if (a == '\n') ++*nlines;
        if (!mimeSource->getChar(&b)) *eof = true;
        if (b == '\n') ++*nlines;
      }

      if (a == '\r' && b == '\n') {
        // This exception is to handle a special case where the
        // delimiter of one part is not followed by CRLF, but
        // immediately followed by a CRLF prefixed delimiter.
        if (!mimeSource->getChar(&a) || !mimeSource->getChar(&b)) {
          *eof = true;
        } else if (a == '-' && b == '-') {
          mimeSource->ungetChar();
          mimeSource->ungetChar();
          mimeSource->ungetChar();
          mimeSource->ungetChar();
        } else {
          mimeSource->ungetChar();
          mimeSource->ungetChar();
        }

        *boundarysize += 2;
      } else {
        mimeSource->ungetChar();
        mimeSource->ungetChar();
      }
    }
  }

  // make sure bodylength doesn't overflow
  *bodylength = mimeSource->getOffset();
  if (*bodylength >= bodystartoffsetcrlf) {
    *bodylength -= bodystartoffsetcrlf;
    if (*bodylength >= (unsigned int)*boundarysize)
      *bodylength -= (unsigned int)*boundarysize;
    else
      *bodylength = 0;
  } else {
    *bodylength = 0;
  }
}

static void parseSinglePart(const string &toboundary,
                            int *boundarysize,
                            unsigned int *nbodylines,
                            unsigned int *nlines,
                            bool *eof,
                            bool *foundendofpart,
                            unsigned int *bodylength)
{
  using namespace ::Binc;
  unsigned int bodystartoffsetcrlf = mimeSource->getOffset();

  // If toboundary is empty, then we read until the end of the
  // file. Otherwise we will read until we encounter toboundary.
  string _toboundary;
  if (toboundary != "") {
    _toboundary = "\r\n--";
    _toboundary += toboundary;
  }

  //  if (skipUntilBoundary(_toboundary, nlines, eof))
  //    *boundarysize = _toboundary.length();

  char *boundaryqueue = nullptr;
  int endpos = _toboundary.length();
  if (toboundary != "") {
    boundaryqueue = new char[endpos];
    memset(boundaryqueue, 0, endpos);
  }
  int boundarypos = 0;

  *boundarysize = 0;

  const char *_toboundaryStr = _toboundary.c_str();
  string line;
  bool toboundaryIsEmpty = (toboundary == "");
  char c;
  while (mimeSource->getChar(&c)) {
    if (c == '\n') {
      ++*nbodylines;
      ++*nlines;
    }
    if (toboundaryIsEmpty) continue;

    // find boundary
    boundaryqueue[boundarypos++ % endpos] = c;

    if (compareStringToQueue(_toboundaryStr, boundaryqueue, boundarypos, endpos)) {
      *boundarysize = _toboundary.length();
      break;
    }
  }

  delete[] boundaryqueue;

  if (toboundary != "") {
    char a;
    if (!mimeSource->getChar(&a)) *eof = true;
    if (a == '\n') ++*nlines;

    char b;
    if (!mimeSource->getChar(&b)) *eof = true;
    if (b == '\n') ++*nlines;

    if (a == '-' && b == '-') {
      *boundarysize += 2;
      *foundendofpart = true;
      if (!mimeSource->getChar(&a)) *eof = true;
      if (a == '\n') ++*nlines;
      if (!mimeSource->getChar(&b)) *eof = true;
      if (b == '\n') ++*nlines;
    }

    if (a == '\r' && b == '\n') {
      // This exception is to handle a special case where the
      // delimiter of one part is not followed by CRLF, but
      // immediately followed by a CRLF prefixed delimiter.
      if (!mimeSource->getChar(&a) || !mimeSource->getChar(&b)) {
        *eof = true;
      } else if (a == '-' && b == '-') {
        mimeSource->ungetChar();
        mimeSource->ungetChar();
        mimeSource->ungetChar();
        mimeSource->ungetChar();
      } else {
        mimeSource->ungetChar();
        mimeSource->ungetChar();
      }

      *boundarysize += 2;
    } else {
      mimeSource->ungetChar();
      mimeSource->ungetChar();
    }
  }

  // make sure bodylength doesn't overflow
  *bodylength = mimeSource->getOffset();
  if (*bodylength >= bodystartoffsetcrlf) {
    *bodylength -= bodystartoffsetcrlf;
    if (*bodylength >= (unsigned int)*boundarysize)
      *bodylength -= (unsigned int)*boundarysize;
    else
      *bodylength = 0;
  } else {
    *bodylength = 0;
  }
}

int Binc::MimePart::parseFull(const string &toboundary, int &boundarysize) const
{
  headerstartoffsetcrlf = mimeSource->getOffset();

  // Parse the header of this mime part.
  parseHeader(&h, &nlines);

  // Headerlength includes the seperating CRLF. Body starts after the
  // CRLF.
  headerlength = mimeSource->getOffset() - headerstartoffsetcrlf;
  bodystartoffsetcrlf = mimeSource->getOffset();
  bodylength = 0;

  // Determine the type of mime part by looking at fields in the
  // header.
  analyzeHeader(&h, &multipart, &messagerfc822, &subtype, &boundary);

  bool eof = false;
  bool foundendofpart = false;

  if (messagerfc822) {
    parseMessageRFC822(&members, &foundendofpart, &bodylength, &nbodylines, toboundary);

  } else if (multipart) {
    parseMultipart(boundary,
                   toboundary,
                   &eof,
                   &nlines,
                   &boundarysize,
                   &foundendofpart,
                   &bodylength,
                   &members);
  } else {
    parseSinglePart(toboundary,
                    &boundarysize,
                    &nbodylines,
                    &nlines,
                    &eof,
                    &foundendofpart,
                    &bodylength);
  }

  return (eof || foundendofpart) ? 1 : 0;
}