Download this file

mime-printheader.cc    201 lines (174 with data), 5.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
/* -*- mode:c++;c-basic-offset:2 -*- */
/* --------------------------------------------------------------------
* Filename:
* mime-printheader.cc
*
* Description:
* Implementation of main mime parser components
* --------------------------------------------------------------------
* Copyright 2002-2005 Andreas Aardal Hanssen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
* --------------------------------------------------------------------
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "mime.h"
#include "mime-utils.h"
#include "mime-inputsource.h"
#include "convert.h"
#include "iodevice.h"
#include "iofactory.h"
#include <string>
#include <vector>
#include <map>
#include <exception>
#include <iostream>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#ifndef NO_NAMESPACES
using namespace ::std;
#endif /* NO_NAMESPACES */
//------------------------------------------------------------------------
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.clear();
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 (vector<string>::const_iterator i = headers.begin();
i != headers.end(); ++i) {
string nametmp = *i;
lowercase(nametmp);
if (nametmp == lowername) {
foundMatch = true;
break;
}
}
if (foundMatch == includeheaders || headers.size() == 0) {
string out = name + content;
for (string::const_iterator i = out.begin(); i != out.end(); ++i)
if (processedbytes >= startoffset && wrotebytes < length) {
if (processedbytes >= startoffset) {
store += *i;
++wrotebytes;
}
} else
++processedbytes;
}
// move on to the next header
content.clear();
name.clear();
break;
}
content += c;
}
}
if (name != "") {
string lowername = name;
lowercase(lowername);
trim(lowername, ": \t");
bool foundMatch = false;
for (vector<string>::const_iterator i = headers.begin();
i != headers.end(); ++i) {
string nametmp = *i;
lowercase(nametmp);
if (nametmp == lowername) {
foundMatch = true;
break;
}
}
if (hasHeaderSeparator || foundMatch == includeheaders || headers.size() == 0) {
string out = name + content;
for (string::const_iterator i = out.begin(); i != out.end(); ++i)
if (processedbytes >= startoffset && wrotebytes < length) {
store += *i;
++wrotebytes;
} else
++processedbytes;
}
}
}