aboutsummaryrefslogtreecommitdiffstats
path: root/src/encoderUUE.cpp
blob: 16b5a7799cfcb93284f6e6a941762f8c4e0aa3e7 (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
//
// VMime library (http://vmime.sourceforge.net)
// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include "vmime/encoderUUE.hpp"
#include "vmime/parserHelpers.hpp"


namespace vmime
{


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


const std::vector <string> encoderUUE::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 const unsigned char UUENCODE(const unsigned char c)
{
	return ((c & 077) + ' ');
}

// Single character decoding
static inline const unsigned char UUDECODE(const unsigned char c)
{
	return ((c - ' ') & 077);
}


const utility::stream::size_type encoderUUE::encode(utility::inputStream& in, utility::outputStream& out)
{
	in.reset();  // may not work...

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

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

	utility::stream::size_type total = 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
	utility::stream::value_type inBuffer[64];
	utility::stream::value_type outBuffer[64];

	while (!in.eof())
	{
		// Process up to 45 characters per line
		std::fill(inBuffer, inBuffer + sizeof(inBuffer), 0);

		const utility::stream::size_type inLength = in.read(inBuffer, maxLineLength - 1);

		outBuffer[0] = UUENCODE(inLength); // Line length

		utility::stream::size_type j = 1;

		for (utility::stream::size_type i = 0 ; i < inLength ; i += 3, j += 4)
		{
			const unsigned char c1 = static_cast <unsigned char>(inBuffer[i]);
			const unsigned char c2 = static_cast <unsigned char>(inBuffer[i + 1]);
			const unsigned char c3 = static_cast <unsigned char>(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;
	}

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

	return (total);
}


const utility::stream::size_type encoderUUE::decode(utility::inputStream& in, utility::outputStream& out)
{
	in.reset();  // may not work...

	// Process the data
	utility::stream::value_type inBuffer[64];
	utility::stream::value_type outBuffer[64];

	utility::stream::size_type total = 0;

	bool stop = false;

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

	while (!stop && !in.eof())
	{
		// Get the line length
		utility::stream::value_type lengthChar;

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

		const utility::stream::size_type outLength = UUDECODE(lengthChar);
		const utility::stream::size_type inLength =
			std::min((outLength * 4) / 3, static_cast <utility::stream::size_type>(64));
		utility::stream::value_type 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]))
			{
				utility::stream::value_type c = 0;

				utility::stream::size_type count = 0;
				utility::stream::value_type buffer[512];

				while (count < sizeof(buffer) - 1 && in.read(&c, 1) == 1)
				{
					if (c == '\n')
						break;

					buffer[count++] = c;
				}

				if (c != '\n')
				{
					// OOPS! Weird line. Don't try to decode more...
					return (total);
				}

				// Parse filename and mode
				if (count > 0)
				{
					buffer[count] = '\0';

					utility::stream::value_type* p = buffer;

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

					utility::stream::value_type* modeStart = buffer;

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

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

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

					utility::stream::value_type* 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;
				continue;
			}

			break;
		}

		}

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

		// Decode data
		for (utility::stream::size_type i = 0, j = 0 ; i < inLength ; i += 4, j += 3)
		{
			const unsigned char c1 = static_cast <unsigned char>(inBuffer[i]);
			const unsigned char c2 = static_cast <unsigned char>(inBuffer[i + 1]);
			const unsigned char c3 = static_cast <unsigned char>(inBuffer[i + 2]);
			const unsigned char c4 = static_cast <unsigned char>(inBuffer[i + 3]);

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

			switch (n)
			{
			default:
			case 3: outBuffer[j + 2] = UUDECODE(c3) << 6 | UUDECODE(c4);
			case 2: outBuffer[j + 1] = UUDECODE(c2) << 4 | UUDECODE(c3) >> 2;
			case 1: outBuffer[j]     = UUDECODE(c1) << 2 | UUDECODE(c2) >> 4;
			case 0: break;
			}

			total += n;
		}

		out.write(outBuffer, outLength);

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

	return (total);
}


} // vmime