summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/quoted_printable.cc
blob: b61246b2fbb4c44d1551fc815ad668d9ac37ec36 (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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/net/quoted_printable.h"

#include "base/logging.h"
#include "base/string_util.h"

namespace {

const int kMaxCharPerLine = 76;
const char* const kEOL = "\r\n";

const char kHexTable[] = "0123456789ABCDEF";

}  // namespace

namespace chrome {
namespace browser {
namespace net {

void QuotedPrintableEncode(const std::string& input, std::string* output) {
  // The number of characters in the current line.
  int char_count = 0;
  for (std::string::const_iterator iter = input.begin();
       iter != input.end(); ++iter) {
    bool last_char = (iter + 1 == input.end());
    char c = *iter;
    // Whether this character can be inserted without encoding.
    bool as_is = false;
    // All printable ASCII characters can be included as is (but for =).
    if (c >= '!' && c <= '~' && c != '=') {
      as_is = true;
    }

    // Space and tab characters can be included as is if they don't appear at
    // the end of a line or at then end of the input.
    if (!as_is && (c == '\t' || c == ' ') && !last_char &&
        !IsEOL(iter + 1, input)) {
      as_is = true;
    }

    // End of line should be converted to CR-LF sequences.
    if (!last_char) {
      int eol_len = IsEOL(iter, input);
      if (eol_len > 0) {
        output->append(kEOL);
        char_count = 0;
        iter += (eol_len - 1);  // -1 because we'll ++ in the for() above.
        continue;
      }
    }

    // Insert a soft line break if necessary.
    int min_chars_needed = as_is ? kMaxCharPerLine - 2 : kMaxCharPerLine - 4;
    if (!last_char && char_count > min_chars_needed) {
      output->append("=");
      output->append(kEOL);
      char_count = 0;
    }

    // Finally, insert the actual character(s).
    if (as_is) {
      output->append(1, c);
      char_count++;
    } else {
      output->append("=");
      output->append(1, kHexTable[static_cast<int>((c >> 4) & 0xF)]);
      output->append(1, kHexTable[static_cast<int>(c & 0x0F)]);
      char_count += 3;
    }
  }
}

bool QuotedPrintableDecode(const std::string& input, std::string* output) {
  bool success = true;
  for (std::string::const_iterator iter = input.begin();
       iter!= input.end(); ++iter) {
    char c = *iter;
    if (c != '=') {
      output->append(1, c);
      continue;
    }
    if (input.end() - iter < 3) {
      LOG(ERROR) << "unfinished = sequence in input string.";
      success = false;
      output->append(1, c);
      continue;
    }
    char c2 = *(++iter);
    char c3 = *(++iter);
    if (c2 == '\r' && c3 == '\n') {
      // Soft line break, ignored.
      continue;
    }

    if (!IsHexDigit(c2) || !IsHexDigit(c3)) {
      LOG(ERROR) << "invalid = sequence, = followed by non hexa digit " <<
          "chars: " << c2 << " " << c3;
      success = false;
      // Just insert the chars as is.
      output->append("=");
      output->append(1, c2);
      output->append(1, c3);
      continue;
    }

    int i1 = HexDigitToInt(c2);
    int i2 = HexDigitToInt(c3);
    char r = static_cast<char>(((i1 << 4) & 0xF0) | (i2 & 0x0F));
    output->append(1, r);
  }
  return success;
}

int IsEOL(const std::string::const_iterator& iter, const std::string& input) {
  if (*iter == '\n')
    return 1;  // Single LF.

  if (*iter == '\r') {
    if ((iter + 1) == input.end() || *(iter + 1) != '\n')
      return 1;  // Single CR (Commodore and Old Macs).
    return 2;  // CR-LF.
  }

  return 0;
}

}  // namespace net
}  // namespace browser
}  // namespace chrome