File: rfc2045cpp.C

package info (click to toggle)
maildrop 3.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,896 kB
  • sloc: ansic: 29,827; cpp: 18,027; sh: 5,993; makefile: 882; perl: 94
file content (606 lines) | stat: -rw-r--r-- 12,858 bytes parent folder | download | duplicates (3)
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
600
601
602
603
604
605
606
/*
** Copyright 2025 Double Precision, Inc.
** See COPYING for distribution information.
**
*/

#include	"rfc2045/rfc2045.h"

std::vector<std::string> rfc2045::entity::parsing_error::describe() const
{
	std::vector<std::string> errors;

	if (code & RFC2045_ERR8BITHEADER)
		errors.push_back("8-bit header content");
	if (code & RFC2045_ERR8BITHEADER)
		errors.push_back(
			"8-bit content without a Content-Transfer-Encoding "
			"declaration"
		);
	if (code & RFC2045_ERR2COMPLEX)
		errors.push_back("MIME complexity exceeds limits");

	if (code & RFC2045_ERRBADBOUNDARY)
		errors.push_back("Non-unique MIME boundary delimiters");

	if (code & RFC2045_ERR8BITINQP)
		errors.push_back(
			"Unencoded 8-bit content in quoted-printable content");
	if (code & RFC2045_ERRBADHEXINQP)
		errors.push_back(
			"Bad hexadecimal quoted-printable encoding"
		);
	if (code & RFC2045_ERRWRONGBOUNDARY)
		errors.push_back(
			"Unexpected MIME boundary delimiter"
		);
	if (code & RFC2045_ERRLONGUNFOLDEDHEADER)
		errors.push_back(
			"Unfolded quopted-printable line is excessive"
		);
	if (code & RFC2045_ERRUNKNOWNTE)
		errors.push_back(
			"Unknown Content-Transfer-Encoding"

		);
	if (code & RFC2045_ERRINVALIDBASE64)
		errors.push_back(
			"Invalid base64 encoding"
		);
	return errors;
}

void rfc2045::entity_parse_meta::report_error(
	rfc2045::entity_info::errors_t code
)
{
	for (auto &e:parsing_entities)
		e->errors.code |= code;
}

void rfc2045::entity_parse_meta::report_error_here(
	rfc2045::entity_info::errors_t code
)
{
	if (!parsing_entities.empty()) // Sanity check
		parsing_entities.back()->errors.code |= code;
}

bool rfc2045::entity_parse_meta::fatal_error()
{
	return !parsing_entities.empty() // Sanity check
		&& parsing_entities[0]->errors.fatal();
}

void rfc2045::entity_parse_meta::consumed_header_line(size_t c)
{
	if (parsing_entities.empty())
		abort();

	auto b=parsing_entities.begin();
	auto e=parsing_entities.end();

	--e;
	++(*e)->nlines;
	(*e)->endbody += c;

	// And this is the body of all of its parents.

	while (b != e)
	{
		++(*b)->nlines;
		++(*b)->nbodylines;
		(*b)->endbody += c;
		++b;
	}
}

void rfc2045::entity_parse_meta::consumed_body_line(size_t c)
{
	for (auto &ptr:parsing_entities)
	{
		++ptr->nlines;
		++ptr->nbodylines;
		ptr->endbody += c;
	}
}

std::string rfc2045::entity::header_parameter_value::value_in_charset() const
{
	return value_in_charset(rfc2045_getdefaultcharset());
}

std::string rfc2045::entity::header_parameter_value::value_in_charset(
	std::string_view dest_charset
) const
{
	return unicode::iconvert::convert(
		value,
		charset,
		{dest_charset.begin(), dest_charset.end()}
	);
}

rfc2045::entity::entity() noexcept=default;

rfc2045::entity::entity(const entity &o) noexcept
	: entity_info{o}
{
	update_parent_ptr();
}

rfc2045::entity::entity(entity &&o) noexcept
	: entity_info{std::move(o)}
{
	update_parent_ptr();
}

std::string_view rfc2045::entity_info::content_type_charset() const
{
	auto p=content_type.parameters.find("charset");

	return p == content_type.parameters.end()
		? std::string_view{rfc2045_getdefaultcharset()}
		: std::string_view{p->second.value};
}

std::string_view rfc2045::entity_info::content_type_boundary() const
{
	auto p=content_type.parameters.find("boundary");

	return p == content_type.parameters.end()
		? std::string_view{}
		: std::string_view{p->second.value};
}

rfc2045::entity &rfc2045::entity::operator=(const entity &o) noexcept
{
	entity_info::operator=(o);
	update_parent_ptr();
	return *this;
}

rfc2045::entity &rfc2045::entity::operator=(entity &&o) noexcept
{
	entity_info::operator=(std::move(o));
	update_parent_ptr();
	return *this;
}

void rfc2045::entity::update_parent_ptr()
{
	for (auto &e:subentities)
	{
		e.parent_entity=this;
	}
}

// The constructor takes the entire header value as a parameter and fully
// parses it.

rfc2045::entity::rfc2231_header::rfc2231_header(
	const std::string_view &s
)
{
	rfc822::tokens tokens{s};

	auto tb=tokens.begin(), te=tokens.end();

	// Value, up to the first semicolon.
	auto semicolon=std::find_if(tb, te,
				    []
				    (const auto &t)
				    {
					    return t.type == ';';
				    });

	rfc822::tokens::unquote(tb, semicolon,
				std::back_inserter(value));

	rfc2045::entity::tolowercase(value);

	// Value of a structured header parameter, parsed according to RFC 2231.
	// This is preliminary parsing, once it's parsed this'll get reassembled
	// into a single parsed string, representing the value.

	struct rfc2231_parsed_parameters {
		std::string charset{"utf-8"};
		std::string language{"en"};
		std::map<std::optional<int>, std::string> values;
	};

	// First, parse the parameters into a temporary structure.
	std::unordered_map<std::string,
			   rfc2231_parsed_parameters> parsed_parameters;

	while (semicolon != te)
	{
		if (++semicolon == te)
			break;
		tb=semicolon;

		// Find the next semicolon, for the next parameter
		semicolon=std::find_if(tb, te,
				       []
				       (const auto &t)
				       {
					       return t.type == ';';
				       });

		if (semicolon == tb)
			continue; // Multiple semicolons, quietly ignore.

		// = marks the end of the parameter name.
		auto equal_to=std::find_if(tb, semicolon,
				       []
				       (const auto &t)
				       {
					       return t.type == '=';
				       });

		// Grab the entire name=value

		std::string name, value;

		rfc822::tokens::unquote(tb, equal_to,
					std::back_inserter(name));

		// Everything after the = is the value.
		if (equal_to != semicolon)
		{
			++equal_to;
			rfc822::tokens::unquote(equal_to, semicolon,
						std::back_inserter(value));
		}

		rfc2045::entity::tolowercase(name);

		// Let's dig into the parameter name, to see if RFC 2231 is used
		// by checking for a trailing *, first.

		bool trailing_squid=false;
		std::optional<int> key;

		if (!name.empty() && name[name.size()-1] == '*')
		{
			trailing_squid=true;
			name.pop_back(); // Get rid of the trailing squid
		}

		// If there's a * in the name, we have a piece of the parameter

		auto delim=name.rfind('*');
		if (delim < name.size())
		{
			key=0;
			for (auto b=name.begin()+delim,
				     e=name.end(); ++b != e; )
			{
				if (*b >= '0' && *b <= '9')
					key= (*key)*10 + (*b-'0');
			}
			name.resize(delim);
		}
		else if (!trailing_squid)
		{
			// Non-standard: handle RFC2047-quoted content in
			// parameter values, but only for non-RFC2231
			// parameters.

			std::string buffer;

			rfc2047::decode(
				value.begin(), value.end(),
				[&]
				(const auto &charset, const auto &language,
				 auto &&callback)
				{
					auto striter=std::back_inserter(buffer);
					callback(striter);
				});
			value=std::move(buffer);

		}
		auto parameter=parsed_parameters.try_emplace(name).first;

		// If the key-less, or key=0 ends in a squid, extract the
		// charset and the language.

		if ((!key || *key == 0) && trailing_squid)
		{
			auto p=std::find(value.begin(), value.end(), '\'');
			auto q=p;
			if (q != value.end())
				++q;

			auto r=std::find(q, value.end(), '\'');
			auto s=r;
			if (s != value.end())
			{
				++s;

				rfc2045::entity::tolowercase(value.begin(), r);

				parameter->second.charset.assign(
					value.begin(), p
				);
				parameter->second.language.assign(q, r);

				value.erase(value.begin(), s);
			}
		}

		if (trailing_squid)
		{
			auto p=value.begin(), q=value.end(), r=p;
			while (p != q)
			{
				if (*p != '%')
				{
					*r++=*p++;
					continue;
				}

				// Poor man's hex decoder.

				if (++p != q)
				{
					unsigned char c=*p++;

					if (c > '9')
					{
						c += 10-('a' & 15);
					}
					c &= 15;

					if (p != q)
					{
						unsigned char d=*p++;

						if (d > '9')
						{
							d += 10-('a' & 15);
						}
						d &= 15;

						*r++ = static_cast<char>(
							(c << 4) | d
						);
					}
				}
			}
			value.erase(r, value.end());
		}

		parameter->second.values.emplace(key, value);
	}

	// Now, assemble the parsed parameters.

	std::string assembled_value;

	for (auto &[name, parsed_parameter_values] : parsed_parameters)
	{
		assembled_value.clear();

		// If the only value is the one that did NOT use RFC 2231
		// encoding, we'll take it.
		auto ptr=parsed_parameter_values.values.begin();

		if (auto ptr2=ptr; ptr2 != parsed_parameter_values.values.end()
		    && !ptr2->first &&
		    ++ptr2 == parsed_parameter_values.values.end())
		{
			assembled_value=ptr->second;
		}
		else
		{
			// Otherwise we will IGNORE the "legacy" value.

			for (auto &[key, value] :
				     parsed_parameter_values.values)
			{
				if (!key)
					continue; // Legacy
				assembled_value += value;
			}
		}

		parameters.emplace(
			std::piecewise_construct,
			std::forward_as_tuple(name),
			std::forward_as_tuple(
				std::move(parsed_parameter_values.charset),
				std::move(parsed_parameter_values.language),
				std::move(assembled_value)));
	}
}

void rfc2045::entity::rfc2231_header::lowercase_value(const char *n)
{
	auto v=parameters.find(n);
	if (v != parameters.end())
		rfc2045::entity::tolowercase(v->second.value);
}

rfc2045::entity_parse_meta::scope::scope(entity_parse_meta &info,
					 entity *e)
	: info{info}
{
	info.parsing_entities.push_back(e);
}

rfc2045::entity_parse_meta::scope::~scope()
{
	auto last=info.parsing_entities.back();

	if (last->has8bitheader || last->has8bitbody)
	{
		last->has8bitcontentchar=true;
	}

	if (last->has8bitbody)
	{
		if (last->multipart() ||
		    rfc2045_message_content_type(
			    last->content_type.value.c_str()
		    )
		)
		{
			last->content_transfer_encoding=cte::eightbit;
		}
		else
		{
			if (last->content_transfer_encoding != cte::eightbit)
			{
				info.report_error(RFC2045_ERR8BITCONTENT);
			}
		}
	}

	info.parsing_entities.pop_back();

	if (!info.parsing_entities.empty())
	{
		auto parent=info.parsing_entities.back();

		if (last->has8bitbody || last->has8bitheader)
			parent->has8bitbody=true;

		if (last->has8bitcontentchar)
			parent->has8bitcontentchar=true;
	}
}

rfc2045::entity &&rfc2045::entity_parser_base::parsed_entity()
{
	std::unique_lock lock{m};

	// Wait until either the end_of_parse flag is already set, or
	// the execution thread hasn't picked up the previous content, yet.

	c.wait(lock,
	       [this]
	       {
		       return end_of_parse || !has_content_to_parse;
	       });

	// Set the end of parse flag

	RFC2045_ENTITY_PARSER_DECL(bool was_end_of_parse=end_of_parse);

	if (!end_of_parse)
	{
		RFC2045_ENTITY_PARSER_TEST("end_of_parse set");
	}
	end_of_parse=true;
	c.notify_all();

	// And wait until this has been received
	c.wait(lock,
	       [this]
	       {
		       return thread_finished;
	       });

	RFC2045_ENTITY_PARSER_DECL(
		if (!was_end_of_parse) {);
		RFC2045_ENTITY_PARSER_TEST("thread finished");
		RFC2045_ENTITY_PARSER_DECL(});
	return std::move(entity_getting_parsed);
}

// Called by execution thread to get the next chunk to parse, it is copied into
// the chunk parameter. Returns false if there are no more chunks.

bool rfc2045::entity_parser_base::get_next_chunk(
	std::unique_lock<std::mutex> &lock,
	std::string &chunk)
{
	c.wait(lock,
	       [this]
	       {
		       return end_of_parse || has_content_to_parse;
	       });

	if (end_of_parse && !has_content_to_parse)
	{
		if (!thread_finished)
		{
			RFC2045_ENTITY_PARSER_TEST("end_of_parse received");
		}
		thread_finished=true;
		c.notify_all();
		return false;
	}
	chunk=content_to_parse;

	RFC2045_ENTITY_PARSER_TEST("retrieved next chunk");
	has_content_to_parse=false;
	c.notify_all();
	return true;
}

rfc2045::headers_base::headers_base(size_t empty_line_size)
	: empty_line_size{empty_line_size}
{
}

std::string_view rfc2045::headers_base::current_header()
{
	// If a line was read in next(), header_line is always
	// non-empty except if left=0. So if it is empty, then
	// this is either the first header, or at the end, and
	// it does no harm to next() again.

	if (header_line.empty())
		next();

	return header_line;
}

std::tuple<std::string_view, std::string_view>
rfc2045::headers_base::name_content()
{
	auto sv=current_header();

	auto b=sv.begin(), e=sv.end();
	auto p=std::find(b, e, ':');

	auto q=p;

	if (q < e) ++q;

	while (q < e)
	{
		if (*q != ' ' && *q != '\t')
			break;
		++q;
	}

	return { {b, static_cast<std::string_view::size_type>(p-b)},
		 {q, static_cast<std::string_view::size_type>(e-q)} };
}

std::tuple<std::string, bool> rfc2045::headers_base::convert_name_check_empty()
{
	const auto &[name, contentx] = name_content();

	std::string name_lc{name.begin(), name.end()};
	rfc2045::entity::tolowercase(name_lc);

	return {
		std::move(name_lc),
		current_header().size() <= empty_line_size
	};
}

rfc2045::entity::errors_t rfc2045::entity_info::all_errors() const
{
	auto code=errors.code;

	for (auto &se:subentities)
		code |= se.all_errors();

	return code;
}