2001-12-01 01:48:47 +03:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2001-12-01 01:48:47 +03:00
|
|
|
|
|
|
|
#include "nsConverterInputStream.h"
|
2005-06-24 23:44:50 +04:00
|
|
|
#include "nsIInputStream.h"
|
2012-12-05 03:04:39 +04:00
|
|
|
#include "nsReadLine.h"
|
2013-08-20 15:03:50 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2014-05-08 13:32:00 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
|
|
|
|
|
|
using mozilla::dom::EncodingUtils;
|
2001-12-01 01:48:47 +03:00
|
|
|
|
|
|
|
#define CONVERTER_BUFFER_SIZE 8192
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsConverterInputStream, nsIConverterInputStream,
|
|
|
|
nsIUnicharInputStream, nsIUnicharLineInputStream)
|
2013-10-18 03:09:20 +04:00
|
|
|
|
2001-12-01 01:48:47 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConverterInputStream::Init(nsIInputStream* aStream,
|
2003-06-11 22:16:03 +04:00
|
|
|
const char *aCharset,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aBufferSize,
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t aReplacementChar)
|
2001-12-01 01:48:47 +03:00
|
|
|
{
|
2014-05-08 13:32:00 +04:00
|
|
|
nsAutoCString label;
|
|
|
|
if (!aCharset) {
|
|
|
|
label.AssignLiteral("UTF-8");
|
|
|
|
} else {
|
|
|
|
label = aCharset;
|
|
|
|
}
|
2001-12-01 01:48:47 +03:00
|
|
|
|
|
|
|
if (aBufferSize <=0) aBufferSize=CONVERTER_BUFFER_SIZE;
|
|
|
|
|
|
|
|
// get the decoder
|
2014-05-08 13:32:00 +04:00
|
|
|
nsAutoCString encoding;
|
|
|
|
if (label.EqualsLiteral("UTF-16")) {
|
|
|
|
// Compat with old test cases. Unclear if any extensions really care.
|
|
|
|
encoding.Assign(label);
|
|
|
|
} else if (!EncodingUtils::FindEncodingForLabelNoReplacement(label,
|
|
|
|
encoding)) {
|
|
|
|
return NS_ERROR_UCONV_NOCONV;
|
|
|
|
}
|
|
|
|
mConverter = EncodingUtils::DecoderForEncoding(encoding);
|
2001-12-01 01:48:47 +03:00
|
|
|
|
|
|
|
// set up our buffers
|
2015-05-18 23:50:34 +03:00
|
|
|
if (!mByteData.SetCapacity(aBufferSize, mozilla::fallible) ||
|
|
|
|
!mUnicharData.SetCapacity(aBufferSize, mozilla::fallible)) {
|
2013-08-20 15:03:50 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2001-12-01 01:48:47 +03:00
|
|
|
|
|
|
|
mInput = aStream;
|
2005-06-24 23:44:50 +04:00
|
|
|
mReplacementChar = aReplacementChar;
|
2012-12-10 18:11:15 +04:00
|
|
|
if (!aReplacementChar ||
|
|
|
|
aReplacementChar != mConverter->GetCharacterForUnMapped()) {
|
|
|
|
mConverter->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
|
|
|
|
}
|
|
|
|
|
2001-12-01 01:48:47 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConverterInputStream::Close()
|
|
|
|
{
|
2005-06-25 03:06:11 +04:00
|
|
|
nsresult rv = mInput ? mInput->Close() : NS_OK;
|
2012-12-05 03:04:39 +04:00
|
|
|
mLineBuffer = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mInput = nullptr;
|
|
|
|
mConverter = nullptr;
|
2013-08-20 15:03:50 +04:00
|
|
|
mByteData.Clear();
|
|
|
|
mUnicharData.Clear();
|
2005-06-24 23:44:50 +04:00
|
|
|
return rv;
|
2001-12-01 01:48:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsConverterInputStream::Read(char16_t* aBuf,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aReadCount)
|
2001-12-01 01:48:47 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t readCount = mUnicharDataLength - mUnicharDataOffset;
|
2005-06-24 23:44:50 +04:00
|
|
|
if (0 == readCount) {
|
2001-12-01 01:48:47 +03:00
|
|
|
// Fill the unichar buffer
|
2005-06-24 23:44:50 +04:00
|
|
|
readCount = Fill(&mLastErrorCode);
|
|
|
|
if (readCount == 0) {
|
2001-12-01 01:48:47 +03:00
|
|
|
*aReadCount = 0;
|
2002-08-28 03:47:25 +04:00
|
|
|
return mLastErrorCode;
|
2001-12-01 01:48:47 +03:00
|
|
|
}
|
|
|
|
}
|
2005-06-24 23:44:50 +04:00
|
|
|
if (readCount > aCount) {
|
|
|
|
readCount = aCount;
|
2001-12-01 01:48:47 +03:00
|
|
|
}
|
2013-08-20 15:03:50 +04:00
|
|
|
memcpy(aBuf, mUnicharData.Elements() + mUnicharDataOffset,
|
2014-01-04 19:02:17 +04:00
|
|
|
readCount * sizeof(char16_t));
|
2005-06-24 23:44:50 +04:00
|
|
|
mUnicharDataOffset += readCount;
|
|
|
|
*aReadCount = readCount;
|
2001-12-01 01:48:47 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConverterInputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter,
|
|
|
|
void* aClosure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount, uint32_t *aReadCount)
|
2003-03-06 22:54:51 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesToWrite = mUnicharDataLength - mUnicharDataOffset;
|
2003-03-06 22:54:51 +03:00
|
|
|
nsresult rv;
|
|
|
|
if (0 == bytesToWrite) {
|
|
|
|
// Fill the unichar buffer
|
|
|
|
bytesToWrite = Fill(&rv);
|
|
|
|
if (bytesToWrite <= 0) {
|
|
|
|
*aReadCount = 0;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytesToWrite > aCount)
|
|
|
|
bytesToWrite = aCount;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesWritten;
|
|
|
|
uint32_t totalBytesWritten = 0;
|
2003-03-06 22:54:51 +03:00
|
|
|
|
|
|
|
while (bytesToWrite) {
|
|
|
|
rv = aWriter(this, aClosure,
|
2013-08-20 15:03:50 +04:00
|
|
|
mUnicharData.Elements() + mUnicharDataOffset,
|
2003-03-06 22:54:51 +03:00
|
|
|
totalBytesWritten, bytesToWrite, &bytesWritten);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// don't propagate errors to the caller
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesToWrite -= bytesWritten;
|
|
|
|
totalBytesWritten += bytesWritten;
|
|
|
|
mUnicharDataOffset += bytesWritten;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*aReadCount = totalBytesWritten;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-06-24 23:44:50 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsConverterInputStream::ReadString(uint32_t aCount, nsAString& aString,
|
|
|
|
uint32_t* aReadCount)
|
2005-06-24 23:44:50 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t readCount = mUnicharDataLength - mUnicharDataOffset;
|
2005-06-24 23:44:50 +04:00
|
|
|
if (0 == readCount) {
|
|
|
|
// Fill the unichar buffer
|
|
|
|
readCount = Fill(&mLastErrorCode);
|
|
|
|
if (readCount == 0) {
|
|
|
|
*aReadCount = 0;
|
|
|
|
return mLastErrorCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (readCount > aCount) {
|
|
|
|
readCount = aCount;
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* buf = mUnicharData.Elements() + mUnicharDataOffset;
|
2005-06-24 23:44:50 +04:00
|
|
|
aString.Assign(buf, readCount);
|
|
|
|
mUnicharDataOffset += readCount;
|
|
|
|
*aReadCount = readCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2001-12-01 01:48:47 +03:00
|
|
|
nsConverterInputStream::Fill(nsresult * aErrorCode)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
if (nullptr == mInput) {
|
2001-12-01 01:48:47 +03:00
|
|
|
// We already closed the stream!
|
|
|
|
*aErrorCode = NS_BASE_STREAM_CLOSED;
|
2002-08-28 03:47:25 +04:00
|
|
|
return 0;
|
2001-12-01 01:48:47 +03:00
|
|
|
}
|
|
|
|
|
2002-08-28 03:47:25 +04:00
|
|
|
if (NS_FAILED(mLastErrorCode)) {
|
|
|
|
// We failed to completely convert last time, and error-recovery
|
|
|
|
// is disabled. We will fare no better this time, so...
|
|
|
|
*aErrorCode = mLastErrorCode;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We assume a many to one conversion and are using equal sizes for
|
|
|
|
// the two buffers. However if an error happens at the very start
|
|
|
|
// of a byte buffer we may end up in a situation where n bytes lead
|
|
|
|
// to n+1 unicode chars. Thus we need to keep track of the leftover
|
|
|
|
// bytes as we convert.
|
|
|
|
|
2013-08-20 15:03:50 +04:00
|
|
|
uint32_t nb;
|
|
|
|
*aErrorCode = NS_FillArray(mByteData, mInput, mLeftOverBytes, &nb);
|
|
|
|
if (nb == 0 && mLeftOverBytes == 0) {
|
2002-08-28 03:47:25 +04:00
|
|
|
// No more data
|
|
|
|
*aErrorCode = NS_OK;
|
|
|
|
return 0;
|
2001-12-01 01:48:47 +03:00
|
|
|
}
|
|
|
|
|
2013-08-20 15:03:50 +04:00
|
|
|
NS_ASSERTION(uint32_t(nb) + mLeftOverBytes == mByteData.Length(),
|
2002-08-28 03:47:25 +04:00
|
|
|
"mByteData is lying to us somewhere");
|
2013-08-20 15:03:50 +04:00
|
|
|
|
2001-12-01 01:48:47 +03:00
|
|
|
// Now convert as much of the byte buffer to unicode as possible
|
|
|
|
mUnicharDataOffset = 0;
|
2002-08-28 03:47:25 +04:00
|
|
|
mUnicharDataLength = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t srcConsumed = 0;
|
2002-08-28 03:47:25 +04:00
|
|
|
do {
|
2013-08-20 15:03:50 +04:00
|
|
|
int32_t srcLen = mByteData.Length() - srcConsumed;
|
|
|
|
int32_t dstLen = mUnicharData.Capacity() - mUnicharDataLength;
|
|
|
|
*aErrorCode = mConverter->Convert(mByteData.Elements()+srcConsumed,
|
2002-08-28 03:47:25 +04:00
|
|
|
&srcLen,
|
2013-08-20 15:03:50 +04:00
|
|
|
mUnicharData.Elements()+mUnicharDataLength,
|
2002-08-28 03:47:25 +04:00
|
|
|
&dstLen);
|
|
|
|
mUnicharDataLength += dstLen;
|
|
|
|
// XXX if srcLen is negative, we want to drop the _first_ byte in
|
|
|
|
// the erroneous byte sequence and try again. This is not quite
|
|
|
|
// possible right now -- see bug 160784
|
|
|
|
srcConsumed += srcLen;
|
2005-06-24 23:44:50 +04:00
|
|
|
if (NS_FAILED(*aErrorCode) && mReplacementChar) {
|
2013-08-20 15:03:50 +04:00
|
|
|
NS_ASSERTION(0 < mUnicharData.Capacity() - mUnicharDataLength,
|
2002-08-28 03:47:25 +04:00
|
|
|
"Decoder returned an error but filled the output buffer! "
|
|
|
|
"Should not happen.");
|
2013-08-20 15:03:50 +04:00
|
|
|
mUnicharData.Elements()[mUnicharDataLength++] = mReplacementChar;
|
2002-08-28 03:47:25 +04:00
|
|
|
++srcConsumed;
|
|
|
|
// XXX this is needed to make sure we don't underrun our buffer;
|
|
|
|
// bug 160784 again
|
2013-01-15 16:22:03 +04:00
|
|
|
srcConsumed = std::max<uint32_t>(srcConsumed, 0);
|
2002-08-28 03:47:25 +04:00
|
|
|
mConverter->Reset();
|
|
|
|
}
|
2013-08-20 15:03:50 +04:00
|
|
|
NS_ASSERTION(srcConsumed <= mByteData.Length(),
|
2002-08-28 03:47:25 +04:00
|
|
|
"Whoa. The converter should have returned NS_OK_UDEC_MOREINPUT before this point!");
|
2005-06-24 23:44:50 +04:00
|
|
|
} while (mReplacementChar &&
|
2011-03-03 08:01:20 +03:00
|
|
|
NS_FAILED(*aErrorCode) &&
|
2013-08-20 15:03:50 +04:00
|
|
|
mUnicharData.Capacity() > mUnicharDataLength);
|
2002-08-28 03:47:25 +04:00
|
|
|
|
2013-08-20 15:03:50 +04:00
|
|
|
mLeftOverBytes = mByteData.Length() - srcConsumed;
|
2002-08-28 03:47:25 +04:00
|
|
|
|
|
|
|
return mUnicharDataLength;
|
2001-12-01 01:48:47 +03:00
|
|
|
}
|
2005-06-24 23:44:50 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsConverterInputStream::ReadLine(nsAString& aLine, bool* aResult)
|
2005-06-24 23:44:50 +04:00
|
|
|
{
|
|
|
|
if (!mLineBuffer) {
|
2014-01-04 19:02:17 +04:00
|
|
|
mLineBuffer = new nsLineBuffer<char16_t>;
|
2005-06-24 23:44:50 +04:00
|
|
|
}
|
2012-12-05 03:04:39 +04:00
|
|
|
return NS_ReadLine(this, mLineBuffer.get(), aLine, aResult);
|
2005-06-24 23:44:50 +04:00
|
|
|
}
|