aboutsummaryrefslogtreecommitdiff
path: root/vmime-master/src/vmime/utility/encoder/uuEncoder.cpp
blob: 24dcdc8bd2e6359c496c113cd6b138adf23dfae0 (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
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// 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 3 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.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library.  Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//

#include "vmime/utility/encoder/uuEncoder.hpp"
#include "vmime/parserHelpers.hpp"


namespace vmime {
namespace utility {
namespace encoder {


uuEncoder::uuEncoder() {

	getProperties()["mode"] = 644;
	getProperties()["filename"] = "no_name";
	getProperties()["maxlinelength"] = 46;
}


const std::vector <string> uuEncoder::getAvailableProperties() const {

	std::vector <string> list(encoder::getAvailableProperties());

	list.push_back("maxlinelength");

	list.push_back("mode");
	list.push_back("filename");

	return list;
}


// This is the character encoding function to make a character printable
static inline byte_t UUENCODE(const unsigned int c) {

	return static_cast <byte_t>((c & 077) + ' ');
}

// Single character decoding
static inline unsigned int UUDECODE(const unsigned int c) {

	return (c - ' ') & 077;
}


size_t uuEncoder::encode(
	utility::inputStream& in,
	utility::outputStream& out,
	utility::progressListener* progress
) {

	in.reset();  // may not work...

	const string propFilename = getProperties().getProperty <string>("filename", "");
	const string propMode = getProperties().getProperty <string>("mode", "644");

	const size_t maxLineLength =
		std::min(getProperties().getProperty <size_t>("maxlinelength", 46), static_cast <size_t>(46));

	size_t total = 0;
	size_t inTotal = 0;

	// Output the prelude text ("begin [mode] [filename]")
	out << "begin";

	if (!propFilename.empty()) {
		out << " " << propMode << " " << propFilename;
		total += 2 + propMode.length() + propFilename.length();
	}

	out << "\r\n";
	total += 7;

	// Process the data
	byte_t inBuffer[64];
	byte_t outBuffer[64];

	if (progress) {
		progress->start(0);
	}

	while (!in.eof()) {

		// Process up to 45 characters per line
		std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0);

		const size_t inLength = in.read(inBuffer, maxLineLength - 1);

		outBuffer[0] = UUENCODE(static_cast <unsigned int>(inLength)); // Line length

		size_t j = 1;

		for (size_t i = 0 ; i < inLength ; i += 3, j += 4) {

			const byte_t c1 = inBuffer[i];
			const byte_t c2 = inBuffer[i + 1];
			const byte_t c3 = inBuffer[i + 2];

			outBuffer[j]     = UUENCODE(c1 >> 2);
			outBuffer[j + 1] = UUENCODE(((c1 << 4) & 060) | ((c2 >> 4) & 017));
			outBuffer[j + 2] = UUENCODE(((c2 << 2) & 074) | ((c3 >> 6) & 03));
			outBuffer[j + 3] = UUENCODE(c3 & 077);
		}

		outBuffer[j] = '\r';
		outBuffer[j + 1] = '\n';

		out.write(outBuffer, j + 2);

		total += j + 2;
		inTotal += inLength;

		if (progress) {
			progress->progress(inTotal, inTotal);
		}
	}

	out << "end\r\n";
	total += 5;

	if (progress) {
		progress->stop(inTotal);
	}

	return total;
}


size_t uuEncoder::decode(
	utility::inputStream& in,
	utility::outputStream& out,
	utility::progressListener* progress
) {

	in.reset();  // may not work...

	// Process the data
	byte_t inBuffer[64];
	byte_t outBuffer[64];

	size_t total = 0;
	size_t inTotal = 0;

	bool stop = false;

	std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0);

	if (progress) {
		progress->start(0);
	}

	while (!stop && !in.eof()) {

		// Get the line length
		byte_t lengthChar;

		if (in.read(&lengthChar, 1) == 0) {
			break;
		}

		const size_t outLength = UUDECODE(lengthChar);
		const size_t inLength = std::min((outLength * 4) / 3, static_cast <size_t>(64));
		size_t inPos = 0;

		switch (lengthChar) {

			case ' ':
			case '\t':
			case '\r':
			case '\n': {

				// Ignore
				continue;
			}
			case 'b': {

				// Read 5 characters more to check for begin ("begin ...\r\n" or "begin ...\n")
				inPos = in.read(inBuffer, 5);

				if (inPos == 5 &&
				    inBuffer[0] == 'e' &&
				    inBuffer[1] == 'g' &&
				    inBuffer[2] == 'i' &&
				    inBuffer[3] == 'n' &&
				    parserHelpers::isSpace(inBuffer[4])) {

					inTotal += 5;

					byte_t c = 0;

					size_t count = 0;
					byte_t buffer[512];

					while (count < sizeof(buffer) - 1 && in.read(&c, 1) == 1) {

						if (c == '\n') {
							break;
						}

						buffer[count++] = c;
					}

					inTotal += count;

					if (c != '\n') {

						// OOPS! Weird line. Don't try to decode more...

						if (progress) {
							progress->stop(inTotal);
						}

						return total;
					}

					// Parse filename and mode
					if (count > 0) {

						buffer[count] = '\0';

						byte_t* p = buffer;

						while (*p && parserHelpers::isSpace(*p)) ++p;

						byte_t* modeStart = buffer;

						while (*p && !parserHelpers::isSpace(*p)) ++p;

						getResults()["mode"] = string(modeStart, p);

						while (*p && parserHelpers::isSpace(*p)) ++p;

						byte_t* filenameStart = buffer;

						while (*p && !(*p == '\r' || *p == '\n')) ++p;

						getResults()["filename"] = string(filenameStart, p);

					// No filename or mode specified
					} else {

						getResults()["filename"] = "untitled";
						getResults()["mode"] = 644;
					}

					continue;
				}

				break;
			}
			case 'e': {

				// Read 3 characters more to check for end ("end\r\n" or "end\n")
				inPos = in.read(inBuffer, 3);

				if (inPos == 3 &&
				    inBuffer[0] == 'n' &&
				    inBuffer[1] == 'd' &&
				    (inBuffer[2] == '\r' || inBuffer[2] == '\n')) {

					stop = true;
					inTotal += 3;
					continue;
				}

				break;
			}

		}

		// Read encoded data
		if (in.read(inBuffer + inPos, inLength - inPos) != inLength - inPos) {
			// Premature end of data
			break;
		}

		inTotal += (inLength - inPos);

		// Decode data
		for (size_t i = 0, j = 0 ; i < inLength ; i += 4, j += 3) {

			const byte_t c1 = inBuffer[i];
			const byte_t c2 = inBuffer[i + 1];
			const byte_t c3 = inBuffer[i + 2];
			const byte_t c4 = inBuffer[i + 3];

			const size_t n = std::min(inLength - i, static_cast <size_t>(3));

			if (n >= 3) {
				outBuffer[j + 2] = static_cast <byte_t>(UUDECODE(c3) << 6 | UUDECODE(c4));
			}
			if (n >= 2) {
				outBuffer[j + 1] = static_cast <byte_t>(UUDECODE(c2) << 4 | UUDECODE(c3) >> 2);
			}
			if (n >= 1) {
				outBuffer[j]     = static_cast <byte_t>(UUDECODE(c1) << 2 | UUDECODE(c2) >> 4);
			}

			total += n;
		}

		out.write(outBuffer, outLength);

		std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0);

		if (progress) {
			progress->progress(inTotal, inTotal);
		}
	}

	if (progress) {
		progress->stop(inTotal);
	}

	return total;
}


size_t uuEncoder::getEncodedSize(const size_t n) const {

	// 3 bytes of input provide 4 bytes of output.
	// Count CRLF (2 bytes) for each line of 45 characters.
	// Also reserve some space for header and footer.
	return 200 + n * 3 + (n / 45) * 2;
}


size_t uuEncoder::getDecodedSize(const size_t n) const {

	// 4 bytes of input provide 3 bytes of output
	return (n * 3) / 4;
}


} // encoder
} // utility
} // vmime