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

#include "convert.h"
#include "iodevice.h"
#include "iofactory.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>

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

void Binc::MimePart::printHeader(int fd,
                                 IODevice &output,
                                 vector<string> headers,
                                 bool includeheaders,
                                 unsigned int startoffset,
                                 unsigned int length,
                                 string &store) const
{
  if (!mimeSource || mimeSource->getFileDescriptor() != fd) {
    delete mimeSource;
    mimeSource = new MimeInputSource(fd);
  }

  mimeSource->seek(headerstartoffsetcrlf);

  string name;
  string content;
  char cqueue[2];
  memset(cqueue, 0, sizeof(cqueue));

  bool quit = false;
  char c = '\0';

  unsigned int wrotebytes = 0;
  unsigned int processedbytes = 0;
  bool hasHeaderSeparator = false;

  while (!quit) {
    // read name
    while (1) {
      // allow EOF to end the header
      if (!mimeSource->getChar(&c)) {
        quit = true;
        break;
      }

      // assume this character is part of the header name.
      name += c;

      // break on the first colon
      if (c == ':') break;

      // break if a '\n' turned up.
      if (c == '\n') {
        // end of headers detected
        if (name == "\r\n") {
          hasHeaderSeparator = true;
          quit = true;
          break;
        }

        // put all data back in the buffer to the beginning of this
        // line.
        for (int i = name.length(); i >= 0; --i)
          mimeSource->ungetChar();

        // abort printing of header. note that in this case, the
        // headers will not end with a seperate \r\n.
        quit = true;
        name = "";
        break;
      }
    }
    if (quit) break;

    // at this point, we have a name, that is - the start of a
    // header. we'll read until the end of the header.
    while (!quit) {
      // allow EOF to end the header.
      if (!mimeSource->getChar(&c)) {
        quit = true;
        break;
      }
      if (c == '\n') ++nlines;

      // make a fifo queue of the last 4 characters.
      cqueue[0] = cqueue[1];
      cqueue[1] = c;

      // print header
      if (cqueue[0] == '\n' && cqueue[1] != '\t' && cqueue[1] != ' ') {
        // it wasn't a space, so put it back as it is most likely
        // the start of a header name. in any case it terminates the
        // content part of this header.
        mimeSource->ungetChar();

        string lowername = name;
        lowercase(lowername);
        trim(lowername, ": \t");
        bool foundMatch = false;

        for (auto nametmp : headers) {
          lowercase(nametmp);
          if (nametmp == lowername) {
            foundMatch = true;
            break;
          }
        }

        if (foundMatch == includeheaders || headers.size() == 0) {
          string out = name + content;
          for (char i : out) {
            if (processedbytes >= startoffset && wrotebytes < length) {
              if (processedbytes >= startoffset) {
                store += i;
                ++wrotebytes;
              }
            } else {
              ++processedbytes;
            }
          }
        }

        // move on to the next header
        content = "";
        name = "";
        break;
      }
      content += c;
    }
  }  // end while loop

  if (name != "") {
    string lowername = name;
    lowercase(lowername);
    trim(lowername, ": \t");
    bool foundMatch = false;
    for (auto nametmp : headers) {
      lowercase(nametmp);
      if (nametmp == lowername) {
        foundMatch = true;
        break;
      }
    }

    if (hasHeaderSeparator || foundMatch == includeheaders || headers.size() == 0) {
      string out = name + content;
      for (char i : out) {
        if (processedbytes >= startoffset && wrotebytes < length) {
          store += i;
          ++wrotebytes;
        } else {
          ++processedbytes;
        }
      }
    }
  }
}