aboutsummaryrefslogtreecommitdiffstats
path: root/src/text.cpp
blob: 5a5a12509ffb5f031337c2e54ee8829d08043ef7 (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
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
//
// VMime library (http://vmime.sourceforge.net)
// Copyright (C) 2002-2004 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 "text.hpp"

#include "utility/stringUtils.hpp"

#include "encoder.hpp"
#include "encoderB64.hpp"
#include "encoderQP.hpp"


namespace vmime
{


text::text()
{
}


text::text(const text& t)
	: component()
{
	copyFrom(t);
}


text::text(const string& t, const charset& ch)
{
	text::newFromString(t, ch, this);
}


text::text(const string& t)
{
	text::newFromString(t, charset::getLocaleCharset(), this);
}


text::text(const word& w)
{
	appendWord(new word(w));
}


text::~text()
{
	removeAllWords();
}


void text::parse(const string& buffer, const string::size_type position,
	const string::size_type end, string::size_type* newPosition)
{
	decodeAndUnfold(buffer.begin() + position, buffer.begin() + end, *this);

	if (newPosition)
		*newPosition = end;
}


void text::generate(utility::outputStream& os, const string::size_type maxLineLength,
	const string::size_type curLinePos, string::size_type* newLinePos) const
{
	encodeAndFold(os, maxLineLength, curLinePos, newLinePos, 0);
}


#if VMIME_WIDE_CHAR_SUPPORT

const wstring text::getDecodedText() const
{
	wstring out;

	for (std::vector <word*>::const_iterator i = m_words.begin() ; i != m_words.end() ; ++i)
		out += (*i)->getDecodedText();

	return (out);
}

#endif


void text::copyFrom(const component& other)
{
	const text& t = dynamic_cast <const text&>(other);

	removeAllWords();

	for (std::vector <word*>::const_iterator i = t.m_words.begin() ; i != t.m_words.end() ; ++i)
		m_words.push_back(new word(**i));
}


text& text::operator=(const component& other)
{
	copyFrom(other);
	return (*this);
}


text& text::operator=(const text& other)
{
	copyFrom(other);
	return (*this);
}


const bool text::operator==(const text& t) const
{
	if (getWordCount() == t.getWordCount())
	{
		bool equal = true;

		std::vector <word*>::const_iterator i = m_words.begin();
		std::vector <word*>::const_iterator j = t.m_words.begin();

		for ( ; equal && i != m_words.end() ; ++i, ++j)
			equal = (**i == **j);

		return (equal);
	}

	return (false);
}


const bool text::operator!=(const text& t) const
{
	return !(*this == t);
}


const string text::getConvertedText(const charset& dest) const
{
	string out;

	for (std::vector <word*>::const_iterator i = m_words.begin() ; i != m_words.end() ; ++i)
		out += (*i)->getConvertedText(dest);

	return (out);
}


void text::appendWord(word* w)
{
	m_words.push_back(w);
}


void text::insertWordBefore(const int pos, word* w)
{
	m_words.insert(m_words.begin() + pos, w);
}


void text::insertWordAfter(const int pos, word* w)
{
	m_words.insert(m_words.begin() + pos + 1, w);
}


void text::removeWord(const int pos)
{
	const std::vector <word*>::iterator it = m_words.begin() + pos;

	delete (*it);

	m_words.erase(it);
}


void text::removeAllWords()
{
	free_container(m_words);
}


const int text::getWordCount() const
{
	return (m_words.size());
}


const bool text::isEmpty() const
{
	return (m_words.empty());
}


word* text::getWordAt(const int pos)
{
	return (m_words[pos]);
}


const word* const text::getWordAt(const int pos) const
{
	return (m_words[pos]);
}


const std::vector <const word*> text::getWordList() const
{
	std::vector <const word*> list;

	list.reserve(m_words.size());

	for (std::vector <word*>::const_iterator it = m_words.begin() ;
	     it != m_words.end() ; ++it)
	{
		list.push_back(*it);
	}

	return (list);
}


const std::vector <word*> text::getWordList()
{
	return (m_words);
}


text* text::clone() const
{
	return new text(*this);
}


text* text::newFromString(const string& in, const charset& ch, text* generateInExisting)
{
	const string::const_iterator end = in.end();
	string::const_iterator p = in.begin();
	string::const_iterator start = in.begin();

	bool is8bit = false;     // is the current word 8-bit?
	bool prevIs8bit = false; // is previous word 8-bit?
	unsigned int count = 0;  // total number of words

	text* out = (generateInExisting != NULL) ? generateInExisting : new text();

	out->removeAllWords();

	for ( ; ; )
	{
		if (p == end || isspace(*p))
		{
			if (p != end)
				++p;

			if (is8bit)
			{
				if (count && prevIs8bit)
				{
					// No need to create a new encoded word, just append
					// the current word to the previous one.
					out->getWordAt(out->getWordCount() - 1)->
						getBuffer() += string(start, p);
				}
				else
				{
					out->appendWord(new word(string(start, p), ch));

					prevIs8bit = true;
					++count;
				}
			}
			else
			{
				if (count && !prevIs8bit)
				{
					out->getWordAt(out->getWordCount() - 1)->
						getBuffer() += string(start, p);
				}
				else
				{
					out->appendWord(new word
						(string(start, p), charset(charsets::US_ASCII)));

					prevIs8bit = false;
					++count;
				}
			}

			if (p == end)
				break;

			is8bit = false;
			start = p;
		}
		else if (!isascii(*p))
		{
			is8bit = true;
			++p;
		}
		else
		{
			++p;
		}
	}

	return (out);
}


void text::encodeAndFold(utility::outputStream& os, const string::size_type maxLineLength,
	const string::size_type firstLineOffset, string::size_type* lastLineLength, const int flags) const
{
	string::size_type curLineLength = firstLineOffset;

	for (int wi = 0 ; wi < getWordCount() ; ++wi)
	{
		const word& w = *getWordAt(wi);
		const string& buffer = w.getBuffer();

		// Calculate the number of ASCII chars to check whether encoding is needed
		// and _which_ encoding to use.
		const string::size_type asciiCount =
			stringUtils::countASCIIchars(buffer.begin(), buffer.end());

		bool noEncoding = (flags & FORCE_NO_ENCODING) ||
		    (!(flags & FORCE_ENCODING) && asciiCount == buffer.length());

		if (noEncoding)
		{
			// We will fold lines without encoding them.

			string::const_iterator lastWSpos = buffer.end(); // last white-space position
			string::const_iterator curLineStart = buffer.begin(); // current line start

			string::const_iterator p = buffer.begin();
			const string::const_iterator end = buffer.end();

			bool finished = false;
			bool newLine = false;

			while (!finished)
			{
				for ( ; p != end ; ++p, ++curLineLength)
				{
					// Exceeded maximum line length, but we have found a white-space
					// where we can cut the line...
					if (curLineLength >= maxLineLength && lastWSpos != end)
						break;

					if (*p == ' ' || *p == '\t')
					{
						// Remember the position of this white-space character
						lastWSpos = p;
					}
				}

				if (p != end)
					++curLineLength;

				//if (p == end || curLineLength >= maxLineLength)
				{
					if (p == end || lastWSpos == end)
					{
						// If we are here, it means that we have found no whitespace
						// before the first "maxLineLength" characters. In this case,
						// we write the full line no matter of the max line length...

						if (!newLine && p != end && lastWSpos == end &&
						    wi != 0 && curLineStart == buffer.begin())
						{
							// Here, we are continuing on the line of previous encoded
							// word, but there is not even enough space to put the
							// first word of this line, so we start a new line.
							if (flags & NO_NEW_LINE_SEQUENCE)
							{
								os << CRLF;
								curLineLength = 0;
							}
							else
							{
								os << NEW_LINE_SEQUENCE;
								curLineLength = NEW_LINE_SEQUENCE_LENGTH;
							}

							p = curLineStart;
							lastWSpos = end;
							newLine = true;
						}
						else
						{
							os << string(curLineStart, p);

							if (p == end)
							{
								finished = true;
							}
							else
							{
								if (flags & NO_NEW_LINE_SEQUENCE)
								{
									os << CRLF;
									curLineLength = 0;
								}
								else
								{
									os << NEW_LINE_SEQUENCE;
									curLineLength = NEW_LINE_SEQUENCE_LENGTH;
								}

								curLineStart = p;
								lastWSpos = end;
								newLine = true;
							}
						}
					}
					else
					{
						// In this case, there will not be enough space on the line for all the
						// characters _after_ the last white-space; so we cut the line at this
						// last white-space.

#if 1
						if (curLineLength != 1 && wi != 0)
							os << " "; // Separate from previous word
#endif

						os << string(curLineStart, lastWSpos);

						if (flags & NO_NEW_LINE_SEQUENCE)
						{
							os << CRLF;
							curLineLength = 0;
						}
						else
						{
							os << NEW_LINE_SEQUENCE;
							curLineLength = NEW_LINE_SEQUENCE_LENGTH;
						}

						curLineStart = lastWSpos + 1;

						p = lastWSpos + 1;
						lastWSpos = end;
						newLine = true;
					}
				}
			}
		}
		/*
			RFC #2047:
			4. Encodings

			Initially, the legal values for "encoding" are "Q" and "B".  These
			encodings are described below.  The "Q" encoding is recommended for
			use when most of the characters to be encoded are in the ASCII
			character set; otherwise, the "B" encoding should be used.
			Nevertheless, a mail reader which claims to recognize 'encoded-word's
			MUST be able to accept either encoding for any character set which it
			supports.
		*/
		else
		{
			// We will encode _AND_ fold lines

			/*
				RFC #2047:
				2. Syntax of encoded-words

				" While there is no limit to the length of a multiple-line header
				  field, each line of a header field that contains one or more
				  'encoded-word's is limited to 76 characters. "
			*/

			const string::size_type maxLineLength3 =
				(maxLineLength == lineLengthLimits::infinite)
					? maxLineLength
					: std::min(maxLineLength, (const string::size_type) 76);

			// Base64 if more than 60% non-ascii, quoted-printable else (default)
			const string::size_type asciiPercent = (100 * asciiCount) / buffer.length();
			const string::value_type encoding = (asciiPercent <= 40) ? 'B' : 'Q';

			string wordStart("=?" + w.getCharset().getName() + "?" + encoding + "?");
			string wordEnd("?=");

			const string::size_type minWordLength = wordStart.length() + wordEnd.length();
			const string::size_type maxLineLength2 = (maxLineLength3 < minWordLength + 1)
				? maxLineLength3 + minWordLength + 1 : maxLineLength3;

			// Checks whether remaining space on this line is usable. If too few
			// characters can be encoded, start a new line.
			bool startNewLine = true;

			if (curLineLength + 2 < maxLineLength2)
			{
				const string::size_type remainingSpaceOnLine = maxLineLength2 - curLineLength - 2;

				if (remainingSpaceOnLine < minWordLength + 10)
				{
					// Space for no more than 10 encoded chars!
					// It is not worth while to continue on this line...
					startNewLine = true;
				}
				else
				{
					// OK, there is enough usable space on the current line.
					startNewLine = false;
				}
			}

			if (startNewLine)
			{
				os << NEW_LINE_SEQUENCE;
				curLineLength = NEW_LINE_SEQUENCE_LENGTH;
			}

			// Encode and fold input buffer
			string::const_iterator pos = buffer.begin();
			string::size_type remaining = buffer.length();

			encoder* theEncoder = ((encoding == 'B')
				? ((encoder*) new encoderB64)
				: ((encoder*) new encoderQP));

			string qpEncodedBuffer;

			if (encoding == 'Q')
			{
				theEncoder->getProperties()["rfc2047"] = true;

				// In the case of Quoted-Printable encoding, we cannot simply encode input
				// buffer line by line. So, we encode the whole buffer and we will fold it
				// in the next loop...
				utility::inputStreamStringAdapter in(buffer);
				utility::outputStreamStringAdapter out(qpEncodedBuffer);

				theEncoder->encode(in, out);

				pos = qpEncodedBuffer.begin();
				remaining = qpEncodedBuffer.length();
			}

#if 1
			if (curLineLength != 1 && wi != 0)
			{
				os << " "; // Separate from previous word
				++curLineLength;
			}
#endif

			for ( ; remaining ; )
			{
				// Start a new encoded word
				os << wordStart;
				curLineLength += minWordLength;

				// Compute the number of encoded chars that will fit on this line
				const string::size_type fit = maxLineLength2 - curLineLength;

				// Base-64 encoding
				if (encoding == 'B')
				{
					// TODO: WARNING! "Any encoded word which encodes a non-integral
					// number of characters or octets is incorrectly formed."

					// Here, we have a formula to compute the maximum number of source
					// characters to encode knowing the maximum number of encoded chars
					// (with Base64, 3 bytes of input provide 4 bytes of output).
					string::size_type count = (fit > 1) ? ((fit - 1) * 3) / 4 : 1;
					if (count > remaining) count = remaining;

					utility::inputStreamStringAdapter in
						(buffer, pos - buffer.begin(), pos - buffer.begin() + count);

					curLineLength += theEncoder->encode(in, os);

					pos += count;
					remaining -= count;
				}
				// Quoted-Printable encoding
				else
				{
					// TODO: WARNING! "Any encoded word which encodes a non-integral
					// number of characters or octets is incorrectly formed."

					// All we have to do here is to take a certain number of character
					// (that is less than or equal to "fit") from the QP encoded buffer,
					// but we also make sure not to fold a "=XY" encoded char.
					const string::const_iterator qpEnd = qpEncodedBuffer.end();
					string::const_iterator lastFoldPos = pos;
					string::const_iterator p = pos;
					string::size_type n = 0;

					while (n < fit && p != qpEnd)
					{
						if (*p == '=')
						{
							if (n + 3 >= fit)
							{
								lastFoldPos = p;
								break;
							}

							p += 3;
							n += 3;
						}
						else
						{
							++p;
							++n;
						}
					}

					if (lastFoldPos == pos)
						lastFoldPos = p;

					os << string(pos, lastFoldPos);

					curLineLength += (lastFoldPos - pos) + 1;

					pos += n;
					remaining -= n;
				}

				// End of the encoded word
				os << wordEnd;

				if (remaining)
				{
					os << NEW_LINE_SEQUENCE;
					curLineLength = NEW_LINE_SEQUENCE_LENGTH;
				}
			}

			delete (theEncoder);
  		}
	}

	if (lastLineLength)
		*lastLineLength = curLineLength;
}


text* text::decodeAndUnfold(const string& in, text* generateInExisting)
{
	text* out = (generateInExisting != NULL) ? generateInExisting : new text();

	out->removeAllWords();

	decodeAndUnfold(in.begin(), in.end(), *out);

	return (out);
}


void text::decodeAndUnfold(const string::const_iterator& inStart, const string::const_iterator& inEnd, text& out)
{
	// NOTE: See RFC-2047, Pages 11-12 for knowing about handling
	// of white-spaces between encoded words.

	out.removeAllWords();

	string::const_iterator p = inStart;
	const string::const_iterator end = inEnd;

	const charset defaultCharset(charsets::US_ASCII);
	charset prevWordCharset(defaultCharset);

	bool prevIsEncoded = false;

	string::const_iterator prevPos = p;

	for ( ; ; )
	{
		if (p == end || *p == '\n')
		{
			string::const_iterator textEnd = p;

			if (textEnd != inStart && *(textEnd - 1) == '\r')
				--textEnd;

			if (textEnd != prevPos)
			{
				if (!out.isEmpty() && prevWordCharset == defaultCharset)
				{
					out.getWordAt(out.getWordCount() - 1)->getBuffer() += string(prevPos, textEnd);
				}
				else
				{
					prevWordCharset = defaultCharset;
					out.appendWord(new word(string(prevPos, textEnd), defaultCharset));
					prevIsEncoded = false;
				}
			}

			if (p == end)
			{
				// Finished
				break;
			}

			// Skip the new-line character
			prevPos = ++p;
		}
		else if (*p == '=' && (p + 1) != end && *(p + 1) == '?')
		{
			string::const_iterator wordPos = p;
			p += 2; // skip '=?'

			if (p != end)
			{
				const string::const_iterator charsetPos = p;

				for ( ; p != end && *p != '?' ; ++p);

				if (p != end) // a charset is specified
				{
					const string::const_iterator charsetEnd = p;
					const string::const_iterator encPos = ++p; // skip '?'

					for ( ; p != end && *p != '?' ; ++p);

					if (p != end) // an encoding is specified
					{
						//const string::const_iterator encEnd = p;
						const string::const_iterator dataPos = ++p; // skip '?'

						for ( ; p != end && !(*p == '?' && *(p + 1) == '=') ; ++p);

						if (p != end) // some data is specified
						{
							const string::const_iterator dataEnd = p;
							p += 2; // skip '?='

							encoder* theEncoder = NULL;

							// Base-64 encoding
							if (*encPos == 'B' || *encPos == 'b')
							{
								theEncoder = new encoderB64;
							}
							// Quoted-Printable encoding
							else if (*encPos == 'Q' || *encPos == 'q')
							{
								theEncoder = new encoderQP;
								theEncoder->getProperties()["rfc2047"] = true;
							}

							if (theEncoder)
							{
								// Decode text
								string decodedBuffer;

								utility::inputStreamStringAdapter ein(string(dataPos, dataEnd));
								utility::outputStreamStringAdapter eout(decodedBuffer);

								theEncoder->decode(ein, eout);
								delete (theEncoder);

								// Append all the unencoded text before this word
								if (prevPos != wordPos)
								{
									string::const_iterator p = prevPos;

									if (prevIsEncoded)
									{
										// Check whether there are only white-spaces between
										// the two encoded words
										for ( ; (p != wordPos) && isspace(*p) ; ++p);
									}

									if (p != wordPos) // if not empty
									{
										if (!out.isEmpty() && prevWordCharset == defaultCharset)
										{
											out.getWordAt(out.getWordCount() - 1)->
												getBuffer() += string(prevPos, wordPos);
										}
										else
										{
											out.appendWord(new word
												(string(prevPos, wordPos), defaultCharset));

											prevWordCharset = defaultCharset;
										}
									}
								}

								// Append this fresh decoded word to output text
								charset thisCharset(string(charsetPos, charsetEnd));

								if (!out.isEmpty() && prevWordCharset == thisCharset)
								{
									out.getWordAt(out.getWordCount() - 1)->
										getBuffer() += decodedBuffer;
								}
								else
								{
									prevWordCharset = thisCharset;
									out.appendWord(new word(decodedBuffer, thisCharset));
								}

								// This word has been decoded: we can advance in the input buffer
								prevPos = p;
								prevIsEncoded = true;
							}
							else
							{
								// Unknown encoding: can't decode this word, we will
								// treat this word as ordinary text (RFC-2047, Page 9).
							}
						}
					}
				}
			}
		}
		else
		{
			++p;
		}

		for ( ; p != end && *p != '=' && *p != '\n' ; ++p);
	}
}


} // vmime