summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/import/src/ImportOutFile.cpp
blob: 3622b56ad7df3fe55ebf5ea66dee6a1c19d9814f (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
/* -*- 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 "nscore.h"
#include "nsString.h"
#include "prio.h"
#include "nsNetUtil.h"
#include "nsISeekableStream.h"
#include "nsMsgUtils.h"
#include "ImportOutFile.h"
#include "ImportCharSet.h"

#include "ImportDebug.h"

/*
#ifdef _MAC
#define  kMacNoCreator    '????'
#define kMacTextFile    'TEXT'
#else
#define  kMacNoCreator    0
#define kMacTextFile    0
#endif
*/

ImportOutFile::ImportOutFile() {
  m_ownsFileAndBuffer = false;
  m_pos = 0;
  m_pBuf = nullptr;
  m_bufSz = 0;
  m_pTrans = nullptr;
  m_pTransOut = nullptr;
  m_pTransBuf = nullptr;
}

ImportOutFile::ImportOutFile(nsIFile* pFile, uint8_t* pBuf, uint32_t sz) {
  m_pTransBuf = nullptr;
  m_pTransOut = nullptr;
  m_pTrans = nullptr;
  m_ownsFileAndBuffer = false;
  InitOutFile(pFile, pBuf, sz);
}

ImportOutFile::~ImportOutFile() {
  if (m_ownsFileAndBuffer) {
    Flush();
    delete[] m_pBuf;
  }

  delete m_pTrans;
  delete m_pTransOut;
  delete[] m_pTransBuf;
}

bool ImportOutFile::Set8bitTranslator(nsImportTranslator* pTrans) {
  if (!Flush()) return false;

  m_engaged = false;
  m_pTrans = pTrans;
  m_supports8to7 = pTrans->Supports8bitEncoding();

  return true;
}

bool ImportOutFile::End8bitTranslation(bool* pEngaged, nsCString& useCharset,
                                       nsCString& encoding) {
  if (!m_pTrans) return false;

  bool bResult = Flush();
  if (m_supports8to7 && m_pTransOut) {
    if (bResult) bResult = m_pTrans->FinishConvertToFile(m_pTransOut);
    if (bResult) bResult = Flush();
  }

  if (m_supports8to7) {
    m_pTrans->GetCharset(useCharset);
    m_pTrans->GetEncoding(encoding);
  } else
    useCharset.Truncate();
  *pEngaged = m_engaged;
  delete m_pTrans;
  m_pTrans = nullptr;
  delete m_pTransOut;
  m_pTransOut = nullptr;
  delete[] m_pTransBuf;
  m_pTransBuf = nullptr;

  return bResult;
}

bool ImportOutFile::InitOutFile(nsIFile* pFile, uint32_t bufSz) {
  if (!bufSz) bufSz = 32 * 1024;
  if (!m_pBuf) m_pBuf = new uint8_t[bufSz];

  if (!m_outputStream) {
    nsresult rv;
    rv = MsgNewBufferedFileOutputStream(
        getter_AddRefs(m_outputStream), pFile,
        PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE, 0644);

    if (NS_FAILED(rv)) {
      IMPORT_LOG0("Couldn't create outfile\n");
      delete[] m_pBuf;
      m_pBuf = nullptr;
      return false;
    }
  }
  m_pFile = pFile;
  m_ownsFileAndBuffer = true;
  m_pos = 0;
  m_bufSz = bufSz;
  return true;
}

void ImportOutFile::InitOutFile(nsIFile* pFile, uint8_t* pBuf, uint32_t sz) {
  m_ownsFileAndBuffer = false;
  m_pFile = pFile;
  m_pBuf = pBuf;
  m_bufSz = sz;
  m_pos = 0;
}

bool ImportOutFile::Flush(void) {
  if (!m_pos) return true;

  uint32_t transLen;
  bool duddleyDoWrite = false;

  // handle translations if appropriate
  if (m_pTrans) {
    if (m_engaged && m_supports8to7) {
      // Markers can get confused by this crap!!!
      // TLR: FIXME: Need to update the markers based on
      // the difference between the translated len and untranslated len

      if (!m_pTrans->ConvertToFile(m_pBuf, m_pos, m_pTransOut, &transLen))
        return false;
      if (!m_pTransOut->Flush()) return false;
      // now update our buffer...
      if (transLen < m_pos) {
        memcpy(m_pBuf, m_pBuf + transLen, m_pos - transLen);
      }
      m_pos -= transLen;
    } else if (m_engaged) {
      // does not actually support translation!
      duddleyDoWrite = true;
    } else {
      // should we engage?
      uint8_t* pChar = m_pBuf;
      uint32_t len = m_pos;
      while (len) {
        if (!ImportCharSet::IsUSAscii(*pChar)) break;
        pChar++;
        len--;
      }
      if (len) {
        m_engaged = true;
        if (m_supports8to7) {
          // allocate our translation output buffer and file...
          m_pTransBuf = new uint8_t[m_bufSz];
          m_pTransOut = new ImportOutFile(m_pFile, m_pTransBuf, m_bufSz);
          return Flush();
        } else
          duddleyDoWrite = true;
      } else {
        duddleyDoWrite = true;
      }
    }
  } else
    duddleyDoWrite = true;

  if (duddleyDoWrite) {
    uint32_t written = 0;
    nsresult rv =
        m_outputStream->Write((const char*)m_pBuf, (int32_t)m_pos, &written);
    if (NS_FAILED(rv) || ((uint32_t)written != m_pos)) return false;
    m_pos = 0;
  }

  return true;
}

bool ImportOutFile::WriteU8NullTerm(const uint8_t* pSrc, bool includeNull) {
  while (*pSrc) {
    if (m_pos >= m_bufSz) {
      if (!Flush()) return false;
    }
    *(m_pBuf + m_pos) = *pSrc;
    m_pos++;
    pSrc++;
  }
  if (includeNull) {
    if (m_pos >= m_bufSz) {
      if (!Flush()) return false;
    }
    *(m_pBuf + m_pos) = 0;
    m_pos++;
  }

  return true;
}

bool ImportOutFile::SetMarker(int markerID) {
  if (!Flush()) {
    return false;
  }

  if (markerID < kMaxMarkers) {
    int64_t pos = 0;
    if (m_outputStream) {
      // do we need to flush for the seek to give us the right pos?
      m_outputStream->Flush();
      nsresult rv;
      nsCOMPtr<nsISeekableStream> seekStream =
          do_QueryInterface(m_outputStream, &rv);
      NS_ENSURE_SUCCESS(rv, false);
      rv = seekStream->Tell(&pos);
      if (NS_FAILED(rv)) {
        IMPORT_LOG0("*** Error, Tell failed on output stream\n");
        return false;
      }
    }
    m_markers[markerID] = (uint32_t)pos + m_pos;
  }

  return true;
}

void ImportOutFile::ClearMarker(int markerID) {
  if (markerID < kMaxMarkers) m_markers[markerID] = 0;
}

bool ImportOutFile::WriteStrAtMarker(int markerID, const char* pStr) {
  if (markerID >= kMaxMarkers) return false;

  if (!Flush()) return false;
  int64_t pos;
  m_outputStream->Flush();
  nsresult rv;
  nsCOMPtr<nsISeekableStream> seekStream =
      do_QueryInterface(m_outputStream, &rv);
  NS_ENSURE_SUCCESS(rv, false);
  rv = seekStream->Tell(&pos);
  if (NS_FAILED(rv)) return false;
  rv = seekStream->Seek(nsISeekableStream::NS_SEEK_SET,
                        (int32_t)m_markers[markerID]);
  if (NS_FAILED(rv)) return false;
  uint32_t written;
  rv = m_outputStream->Write(pStr, strlen(pStr), &written);
  if (NS_FAILED(rv)) return false;

  rv = seekStream->Seek(nsISeekableStream::NS_SEEK_SET, pos);
  if (NS_FAILED(rv)) return false;

  return true;
}