summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/import/src/nsImportTranslator.cpp
blob: f988e7035df78a5e45b4bc6456eb8ce5e51b8086 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ImportOutFile.h"
#include "nsImportTranslator.h"

#include "ImportCharSet.h"

bool nsImportTranslator::ConvertToFile(const uint8_t* pIn, uint32_t inLen,
                                       ImportOutFile* pOutFile,
                                       uint32_t* pProcessed) {
  if (pProcessed) *pProcessed = inLen;
  return (pOutFile->WriteData(pIn, inLen));
}

void CMHTranslator::ConvertBuffer(const uint8_t* pIn, uint32_t inLen,
                                  uint8_t* pOut) {
  while (inLen) {
    if (!ImportCharSet::IsUSAscii(*pIn) ||
        ImportCharSet::Is822SpecialChar(*pIn) ||
        ImportCharSet::Is822CtlChar(*pIn) ||
        (*pIn == ImportCharSet::cSpaceChar) || (*pIn == '*') ||
        (*pIn == '\'') || (*pIn == '%')) {
      // needs to be encode as %hex val
      *pOut = '%';
      pOut++;
      ImportCharSet::ByteToHex(*pIn, pOut);
      pOut += 2;
    } else {
      *pOut = *pIn;
      pOut++;
    }
    pIn++;
    inLen--;
  }
  *pOut = 0;
}

bool CMHTranslator::ConvertToFile(const uint8_t* pIn, uint32_t inLen,
                                  ImportOutFile* pOutFile,
                                  uint32_t* pProcessed) {
  uint8_t hex[2];
  while (inLen) {
    if (!ImportCharSet::IsUSAscii(*pIn) ||
        ImportCharSet::Is822SpecialChar(*pIn) ||
        ImportCharSet::Is822CtlChar(*pIn) ||
        (*pIn == ImportCharSet::cSpaceChar) || (*pIn == '*') ||
        (*pIn == '\'') || (*pIn == '%')) {
      // needs to be encode as %hex val
      if (!pOutFile->WriteByte('%')) return false;
      ImportCharSet::ByteToHex(*pIn, hex);
      if (!pOutFile->WriteData(hex, 2)) return false;
    } else {
      if (!pOutFile->WriteByte(*pIn)) return false;
    }
    pIn++;
    inLen--;
  }

  if (pProcessed) *pProcessed = inLen;

  return true;
}

bool C2047Translator::ConvertToFileQ(const uint8_t* pIn, uint32_t inLen,
                                     ImportOutFile* pOutFile,
                                     uint32_t* pProcessed) {
  if (!inLen) return true;

  int maxLineLen = 64;
  int curLineLen = m_startLen;
  bool startLine = true;

  uint8_t hex[2];
  while (inLen) {
    if (startLine) {
      if (!pOutFile->WriteStr(" =?")) return false;
      if (!pOutFile->WriteStr(m_charset.get())) return false;
      if (!pOutFile->WriteStr("?q?")) return false;
      curLineLen += (6 + m_charset.Length());
      startLine = false;
    }

    if (!ImportCharSet::IsUSAscii(*pIn) ||
        ImportCharSet::Is822SpecialChar(*pIn) ||
        ImportCharSet::Is822CtlChar(*pIn) ||
        (*pIn == ImportCharSet::cSpaceChar) || (*pIn == '?') || (*pIn == '=')) {
      // needs to be encode as =hex val
      if (!pOutFile->WriteByte('=')) return false;
      ImportCharSet::ByteToHex(*pIn, hex);
      if (!pOutFile->WriteData(hex, 2)) return false;
      curLineLen += 3;
    } else {
      if (!pOutFile->WriteByte(*pIn)) return false;
      curLineLen++;
    }
    pIn++;
    inLen--;
    if (curLineLen > maxLineLen) {
      if (!pOutFile->WriteStr("?=")) return false;
      if (inLen) {
        if (!pOutFile->WriteStr("\x0D\x0A ")) return false;
      }

      startLine = true;
      curLineLen = 0;
    }
  }

  if (!startLine) {
    // end the encoding!
    if (!pOutFile->WriteStr("?=")) return false;
  }

  if (pProcessed) *pProcessed = inLen;

  return true;
}

bool C2047Translator::ConvertToFile(const uint8_t* pIn, uint32_t inLen,
                                    ImportOutFile* pOutFile,
                                    uint32_t* pProcessed) {
  if (m_useQuotedPrintable)
    return ConvertToFileQ(pIn, inLen, pOutFile, pProcessed);

  if (!inLen) return true;

  int maxLineLen = 64;
  int curLineLen = m_startLen;
  bool startLine = true;
  int encodeMax;
  uint8_t* pEncoded = new uint8_t[maxLineLen * 2];

  while (inLen) {
    if (startLine) {
      if (!pOutFile->WriteStr(" =?")) {
        delete[] pEncoded;
        return false;
      }
      if (!pOutFile->WriteStr(m_charset.get())) {
        delete[] pEncoded;
        return false;
      }
      if (!pOutFile->WriteStr("?b?")) {
        delete[] pEncoded;
        return false;
      }
      curLineLen += (6 + m_charset.Length());
      startLine = false;
    }
    encodeMax = maxLineLen - curLineLen;
    encodeMax *= 3;
    encodeMax /= 4;
    if ((uint32_t)encodeMax > inLen) encodeMax = (int)inLen;

    // encode the line, end the line
    // then continue. Update curLineLen, pIn, startLine, and inLen
    UMimeEncode::ConvertBuffer(pIn, encodeMax, pEncoded, maxLineLen, maxLineLen,
                               "\x0D\x0A");

    if (!pOutFile->WriteStr((const char*)pEncoded)) {
      delete[] pEncoded;
      return false;
    }

    pIn += encodeMax;
    inLen -= encodeMax;
    startLine = true;
    curLineLen = 0;
    if (!pOutFile->WriteStr("?=")) {
      delete[] pEncoded;
      return false;
    }
    if (inLen) {
      if (!pOutFile->WriteStr("\x0D\x0A ")) {
        delete[] pEncoded;
        return false;
      }
    }
  }

  delete[] pEncoded;

  if (pProcessed) *pProcessed = inLen;

  return true;
}

uint32_t UMimeEncode::GetBufferSize(uint32_t inBytes) {
  // it takes 4 base64 bytes to represent 3 regular bytes
  inBytes += 3;
  inBytes /= 3;
  inBytes *= 4;
  // This should be plenty, but just to be safe
  inBytes += 4;

  // now allow for end of line characters
  inBytes += ((inBytes + 39) / 40) * 4;

  return inBytes;
}

static uint8_t gBase64[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

uint32_t UMimeEncode::ConvertBuffer(const uint8_t* pIn, uint32_t inLen,
                                    uint8_t* pOut, uint32_t maxLen,
                                    uint32_t firstLineLen,
                                    const char* pEolStr) {
  uint32_t pos = 0;
  uint32_t len = 0;
  uint32_t lineLen = 0;
  uint32_t maxLine = firstLineLen;
  int eolLen = 0;
  if (pEolStr) eolLen = strlen(pEolStr);

  while ((pos + 2) < inLen) {
    // Encode 3 bytes
    *pOut = gBase64[*pIn >> 2];
    pOut++;
    len++;
    lineLen++;
    *pOut = gBase64[(((*pIn) & 0x3) << 4) | (((*(pIn + 1)) & 0xF0) >> 4)];
    pIn++;
    pOut++;
    len++;
    lineLen++;
    *pOut = gBase64[(((*pIn) & 0xF) << 2) | (((*(pIn + 1)) & 0xC0) >> 6)];
    pIn++;
    pOut++;
    len++;
    lineLen++;
    *pOut = gBase64[(*pIn) & 0x3F];
    pIn++;
    pOut++;
    len++;
    lineLen++;
    pos += 3;
    if (lineLen >= maxLine) {
      lineLen = 0;
      maxLine = maxLen;
      if (pEolStr) {
        memcpy(pOut, pEolStr, eolLen);
        pOut += eolLen;
        len += eolLen;
      }
    }
  }

  if ((pos < inLen) && ((lineLen + 3) > maxLine)) {
    lineLen = 0;
    maxLine = maxLen;
    if (pEolStr) {
      memcpy(pOut, pEolStr, eolLen);
      pOut += eolLen;
      len += eolLen;
    }
  }

  if (pos < inLen) {
    // Get the last few bytes!
    *pOut = gBase64[*pIn >> 2];
    pOut++;
    len++;
    pos++;
    if (pos < inLen) {
      *pOut = gBase64[(((*pIn) & 0x3) << 4) | (((*(pIn + 1)) & 0xF0) >> 4)];
      pIn++;
      pOut++;
      pos++;
      len++;
      if (pos < inLen) {
        // Should be dead code!! (Then why is it here doofus?)
        *pOut = gBase64[(((*pIn) & 0xF) << 2) | (((*(pIn + 1)) & 0xC0) >> 6)];
        pIn++;
        pOut++;
        len++;
        *pOut = gBase64[(*pIn) & 0x3F];
        pos++;
        pOut++;
        len++;
      } else {
        *pOut = gBase64[(((*pIn) & 0xF) << 2)];
        pOut++;
        len++;
        *pOut = '=';
        pOut++;
        len++;
      }
    } else {
      *pOut = gBase64[(((*pIn) & 0x3) << 4)];
      pOut++;
      len++;
      *pOut = '=';
      pOut++;
      len++;
      *pOut = '=';
      pOut++;
      len++;
    }
  }

  *pOut = 0;

  return len;
}