summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/src/nsMsgLineBuffer.cpp
blob: 60d38b5b8ffaad7db7054ab32e01803804e00f37 (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
/* -*- 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 "msgCore.h"
#include "prlog.h"
#include "prmem.h"
#include "nsMsgLineBuffer.h"
#include "nsMsgUtils.h"
#include "nsIInputStream.h"  // used by nsMsgLineStreamBuffer

nsByteArray::nsByteArray() {
  MOZ_COUNT_CTOR(nsByteArray);
  m_buffer = NULL;
  m_bufferSize = 0;
  m_bufferPos = 0;
}

nsByteArray::~nsByteArray() {
  MOZ_COUNT_DTOR(nsByteArray);
  PR_FREEIF(m_buffer);
}

nsresult nsByteArray::GrowBuffer(uint64_t desired_size, uint32_t quantum) {
  if (desired_size > PR_UINT32_MAX) {
    return NS_ERROR_OUT_OF_MEMORY;
  }
  if (m_bufferSize < desired_size) {
    char* new_buf;
    uint32_t increment = desired_size - m_bufferSize;
    if (increment < quantum) /* always grow by a minimum of N bytes */
      increment = quantum;

    new_buf =
        (m_buffer ? (char*)PR_REALLOC(m_buffer, (m_bufferSize + increment))
                  : (char*)PR_MALLOC(m_bufferSize + increment));
    if (!new_buf) return NS_ERROR_OUT_OF_MEMORY;
    m_buffer = new_buf;
    m_bufferSize += increment;
  }
  return NS_OK;
}

nsresult nsByteArray::AppendString(const char* string) {
  uint32_t strLength = (string) ? PL_strlen(string) : 0;
  return AppendBuffer(string, strLength);
}

nsresult nsByteArray::AppendBuffer(const char* buffer, uint32_t length) {
  nsresult ret = NS_OK;
  if (m_bufferPos + length > m_bufferSize)
    ret = GrowBuffer(m_bufferPos + length, 1024);
  if (NS_SUCCEEDED(ret)) {
    memcpy(m_buffer + m_bufferPos, buffer, length);
    m_bufferPos += length;
  }
  return ret;
}

nsMsgLineBuffer::nsMsgLineBuffer() { MOZ_COUNT_CTOR(nsMsgLineBuffer); }

nsMsgLineBuffer::~nsMsgLineBuffer() { MOZ_COUNT_DTOR(nsMsgLineBuffer); }

nsresult nsMsgLineBuffer::BufferInput(const char* net_buffer,
                                      int32_t net_buffer_size) {
  if (net_buffer_size < 0) {
    return NS_ERROR_INVALID_ARG;
  }
  nsresult status = NS_OK;
  if (m_bufferPos > 0 && m_buffer && m_buffer[m_bufferPos - 1] == '\r' &&
      net_buffer_size > 0 && net_buffer[0] != '\n') {
    /* The last buffer ended with a CR.  The new buffer does not start
       with a LF.  This old buffer should be shipped out and discarded. */
    PR_ASSERT(m_bufferSize > m_bufferPos);
    if (m_bufferSize <= m_bufferPos) {
      return NS_ERROR_UNEXPECTED;
    }
    if (NS_FAILED(HandleLine(m_buffer, m_bufferPos))) {
      return NS_ERROR_FAILURE;
    }
    m_bufferPos = 0;
  }
  while (net_buffer_size > 0) {
    const char* net_buffer_end = net_buffer + net_buffer_size;
    const char* newline = 0;
    const char* s;

    for (s = net_buffer; s < net_buffer_end; s++) {
      /* Move forward in the buffer until the first newline.
         Stop when we see CRLF, CR, or LF, or the end of the buffer.
         *But*, if we see a lone CR at the *very end* of the buffer,
         treat this as if we had reached the end of the buffer without
         seeing a line terminator.  This is to catch the case of the
         buffers splitting a CRLF pair, as in "FOO\r\nBAR\r" "\nBAZ\r\n".
      */
      if (*s == '\r' || *s == '\n') {
        newline = s;
        if (newline[0] == '\r') {
          if (s == net_buffer_end - 1) {
            /* CR at end - wait for the next character. */
            newline = 0;
            break;
          } else if (newline[1] == '\n') {
            /* CRLF seen; swallow both. */
            newline++;
          }
        }
        newline++;
        break;
      }
    }

    /* Ensure room in the net_buffer and append some or all of the current
       chunk of data to it. */
    {
      const char* end = (newline ? newline : net_buffer_end);
      uint64_t desired_size = (end - net_buffer) + (uint64_t)m_bufferPos + 1;
      if (desired_size >= PR_INT32_MAX) {
        // We're not willing to buffer more than 2GB data without seeing
        // a newline, something is wrong with the input.
        // Using this limit prevents us from overflowing.
        return NS_ERROR_UNEXPECTED;
      }

      if (desired_size >= m_bufferSize) {
        status = GrowBuffer(desired_size, 1024);
        if (NS_FAILED(status)) return status;
      }
      memcpy(m_buffer + m_bufferPos, net_buffer, (end - net_buffer));
      m_bufferPos += (end - net_buffer);
    }

    /* Now m_buffer contains either a complete line, or as complete
       a line as we have read so far.

       If we have a line, process it, and then remove it from `m_buffer'.
       Then go around the loop again, until we drain the incoming data.
     */
    if (!newline) return NS_OK;

    if (NS_FAILED(HandleLine(m_buffer, m_bufferPos))) {
      return NS_ERROR_FAILURE;
    }

    net_buffer_size -= (newline - net_buffer);
    net_buffer = newline;
    m_bufferPos = 0;
  }
  return NS_OK;
}

// If there's still some data (non CRLF terminated) flush it out
nsresult nsMsgLineBuffer::Flush() {
  nsresult rv = NS_OK;
  if (m_bufferPos > 0) {
    rv = HandleLine(m_buffer, m_bufferPos);
    m_bufferPos = 0;
  }
  return rv;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// This is a utility class used to efficiently extract lines from an input
// stream by buffering read but unprocessed stream data in a buffer.
///////////////////////////////////////////////////////////////////////////////////////////////////

nsMsgLineStreamBuffer::nsMsgLineStreamBuffer(uint32_t aBufferSize,
                                             bool aAllocateNewLines,
                                             bool aEatCRLFs, char aLineToken)
    : m_eatCRLFs(aEatCRLFs),
      m_allocateNewLines(aAllocateNewLines),
      m_lineToken(aLineToken) {
  NS_ASSERTION(aBufferSize > 0, "invalid buffer size!!!");
  m_dataBuffer = nullptr;
  m_startPos = 0;
  m_numBytesInBuffer = 0;

  // used to buffer incoming data by ReadNextLineFromInput
  if (aBufferSize > 0) {
    m_dataBuffer = (char*)PR_CALLOC(sizeof(char) * aBufferSize);
  }

  m_dataBufferSize = aBufferSize;
}

nsMsgLineStreamBuffer::~nsMsgLineStreamBuffer() {
  PR_FREEIF(m_dataBuffer);  // release our buffer...
}

nsresult nsMsgLineStreamBuffer::GrowBuffer(uint32_t desiredSize) {
  char* newBuffer = (char*)PR_REALLOC(m_dataBuffer, desiredSize);
  NS_ENSURE_TRUE(newBuffer, NS_ERROR_OUT_OF_MEMORY);
  m_dataBuffer = newBuffer;
  m_dataBufferSize = desiredSize;
  return NS_OK;
}

void nsMsgLineStreamBuffer::ClearBuffer() {
  m_startPos = 0;
  m_numBytesInBuffer = 0;
}

// aInputStream - the input stream we want to read a line from
// aPauseForMoreData is returned as true if the stream does not yet contain a
// line and we must wait for more data to come into the stream. Note to people
// wishing to modify this function: Be *VERY CAREFUL* this is a critical
// function used by all of our mail protocols including imap, nntp, and pop. If
// you screw it up, you could break a lot of stuff.....

char* nsMsgLineStreamBuffer::ReadNextLine(nsIInputStream* aInputStream,
                                          uint32_t& aNumBytesInLine,
                                          bool& aPauseForMoreData,
                                          nsresult* prv,
                                          bool addLineTerminator) {
  // try to extract a line from m_inputBuffer. If we don't have an entire line,
  // then read more bytes out from the stream. If the stream is empty then wait
  // on the monitor for more data to come in.

  NS_ASSERTION(m_dataBuffer && m_dataBufferSize > 0,
               "invalid input arguments for read next line from input");

  if (prv) *prv = NS_OK;
  // initialize out values
  aPauseForMoreData = false;
  aNumBytesInLine = 0;
  char* endOfLine = nullptr;
  char* startOfLine = m_dataBuffer + m_startPos;

  if (m_numBytesInBuffer > 0)  // any data in our internal buffer?
    endOfLine = PL_strchr(startOfLine, m_lineToken);  // see if we already
                                                      // have a line ending...

  // it's possible that we got here before the first time we receive data from
  // the server so aInputStream will be nullptr...
  if (!endOfLine && aInputStream)  // get some more data from the server
  {
    nsresult rv;
    uint64_t numBytesInStream = 0;
    uint32_t numBytesCopied = 0;
    bool nonBlockingStream;
    aInputStream->IsNonBlocking(&nonBlockingStream);
    rv = aInputStream->Available(&numBytesInStream);
    if (NS_FAILED(rv)) {
      if (prv) *prv = rv;
      aNumBytesInLine = 0;
      return nullptr;
    }
    if (!nonBlockingStream && numBytesInStream == 0)  // if no data available,
      numBytesInStream = m_dataBufferSize / 2;        // ask for half the data
                                                      // buffer size.

    // if the number of bytes we want to read from the stream, is greater than
    // the number of bytes left in our buffer, then we need to shift the start
    // pos and its contents down to the beginning of m_dataBuffer...
    uint32_t numFreeBytesInBuffer =
        m_dataBufferSize - m_startPos - m_numBytesInBuffer;
    if (numBytesInStream >= numFreeBytesInBuffer) {
      if (m_startPos) {
        memmove(m_dataBuffer, startOfLine, m_numBytesInBuffer);
        // make sure the end of the buffer is terminated
        m_dataBuffer[m_numBytesInBuffer] = '\0';
        m_startPos = 0;
        startOfLine = m_dataBuffer;
        numFreeBytesInBuffer = m_dataBufferSize - m_numBytesInBuffer;
      }
      // If we didn't make enough space (or any), grow the buffer
      if (numBytesInStream >= numFreeBytesInBuffer) {
        int64_t growBy = (numBytesInStream - numFreeBytesInBuffer) * 2 + 1;
        // GrowBuffer cannot handle over 4GB size.
        if (m_dataBufferSize + growBy > PR_UINT32_MAX) return nullptr;
        // try growing buffer by twice as much as we need.
        nsresult rv = GrowBuffer(m_dataBufferSize + growBy);
        // if we can't grow the buffer, we have to bail.
        if (NS_FAILED(rv)) return nullptr;
        startOfLine = m_dataBuffer;
        numFreeBytesInBuffer += growBy;
      }
      NS_ASSERTION(m_startPos == 0, "m_startPos should be 0 .....");
    }

    uint32_t numBytesToCopy = /* leave one for a null terminator */
        std::min(uint64_t(numFreeBytesInBuffer - 1), numBytesInStream);
    if (numBytesToCopy > 0) {
      // read the data into the end of our data buffer
      char* startOfNewData = startOfLine + m_numBytesInBuffer;
      rv = aInputStream->Read(startOfNewData, numBytesToCopy, &numBytesCopied);
      if (prv) *prv = rv;
      uint32_t i;
      for (i = 0; i < numBytesCopied; i++)  // replace nulls with spaces
      {
        if (!startOfNewData[i]) startOfNewData[i] = ' ';
      }
      m_numBytesInBuffer += numBytesCopied;
      m_dataBuffer[m_startPos + m_numBytesInBuffer] = '\0';

      // okay, now that we've tried to read in more data from the stream,
      // look for another end of line character in the new data
      endOfLine = PL_strchr(startOfNewData, m_lineToken);
    }
  }

  // okay, now check again for endOfLine.
  if (endOfLine) {
    if (!m_eatCRLFs) endOfLine += 1;  // count for LF or CR

    aNumBytesInLine = endOfLine - startOfLine;

    if (m_eatCRLFs && aNumBytesInLine > 0 &&
        startOfLine[aNumBytesInLine - 1] == '\r')
      aNumBytesInLine--;  // Remove the CR in a CRLF sequence.

    // PR_CALLOC zeros out the allocated line
    char* newLine = (char*)PR_CALLOC(
        aNumBytesInLine + (addLineTerminator ? MSG_LINEBREAK_LEN : 0) + 1);
    if (!newLine) {
      aNumBytesInLine = 0;
      aPauseForMoreData = true;
      return nullptr;
    }

    memcpy(newLine, startOfLine,
           aNumBytesInLine);  // copy the string into the new line buffer
    if (addLineTerminator) {
      memcpy(newLine + aNumBytesInLine, MSG_LINEBREAK, MSG_LINEBREAK_LEN);
      aNumBytesInLine += MSG_LINEBREAK_LEN;
    }

    if (m_eatCRLFs)
      endOfLine += 1;  // advance past LF or CR if we haven't already done so...

    // now we need to update the data buffer to go past the line we just read
    // out.
    m_numBytesInBuffer -= (endOfLine - startOfLine);
    if (m_numBytesInBuffer)
      m_startPos = endOfLine - m_dataBuffer;
    else
      m_startPos = 0;

    return newLine;
  }

  aPauseForMoreData = true;
  return nullptr;  // if we somehow got here. we don't have another line in the
                   // buffer yet...need to wait for more data...
}

bool nsMsgLineStreamBuffer::NextLineAvailable() {
  return (m_numBytesInBuffer > 0 &&
          PL_strchr(m_dataBuffer + m_startPos, m_lineToken));
}