2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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/. */
|
1999-02-03 21:55:10 +03:00
|
|
|
|
2006-07-21 20:28:51 +04:00
|
|
|
#include "nsUnicharInputStream.h"
|
2005-06-24 23:44:50 +04:00
|
|
|
#include "nsIInputStream.h"
|
1999-02-03 21:55:10 +03:00
|
|
|
#include "nsIServiceManager.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsString.h"
|
2013-08-20 15:03:50 +04:00
|
|
|
#include "nsTArray.h"
|
2006-07-21 20:28:51 +04:00
|
|
|
#include "nsAutoPtr.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsCRT.h"
|
2013-08-20 15:03:50 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2003-05-22 02:20:27 +04:00
|
|
|
#include "nsUTF8Utils.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
#include <fcntl.h>
|
2011-12-21 09:09:17 +04:00
|
|
|
#if defined(XP_WIN)
|
1998-04-14 00:24:54 +04:00
|
|
|
#include <io.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-02 13:27:29 +04:00
|
|
|
#define STRING_BUFFER_SIZE 8192
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class StringUnicharInputStream final : public nsIUnicharInputStream
|
2014-05-15 00:15:46 +04:00
|
|
|
{
|
1998-04-14 00:24:54 +04:00
|
|
|
public:
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit StringUnicharInputStream(const nsAString& aString) :
|
2006-07-21 20:28:51 +04:00
|
|
|
mString(aString), mPos(0), mLen(aString.Length()) { }
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2005-06-24 23:44:50 +04:00
|
|
|
NS_DECL_NSIUNICHARINPUTSTREAM
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2006-07-21 20:28:51 +04:00
|
|
|
nsString mString;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mPos;
|
|
|
|
uint32_t mLen;
|
2004-01-15 09:14:18 +03:00
|
|
|
|
|
|
|
private:
|
2006-07-21 20:28:51 +04:00
|
|
|
~StringUnicharInputStream() { }
|
1998-04-14 00:24:54 +04:00
|
|
|
};
|
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
StringUnicharInputStream::Read(char16_t* aBuf,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount,
|
2014-05-15 00:15:46 +04:00
|
|
|
uint32_t* aReadCount)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
|
|
|
if (mPos >= mLen) {
|
1998-07-24 00:34:01 +04:00
|
|
|
*aReadCount = 0;
|
2003-11-18 18:06:03 +03:00
|
|
|
return NS_OK;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2004-10-27 06:19:43 +04:00
|
|
|
nsAString::const_iterator iter;
|
2006-07-21 20:28:51 +04:00
|
|
|
mString.BeginReading(iter);
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* us = iter.get();
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t amount = mLen - mPos;
|
1998-04-14 00:24:54 +04:00
|
|
|
if (amount > aCount) {
|
|
|
|
amount = aCount;
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
memcpy(aBuf, us + mPos, sizeof(char16_t) * amount);
|
1998-04-14 00:24:54 +04:00
|
|
|
mPos += amount;
|
1998-07-24 00:34:01 +04:00
|
|
|
*aReadCount = amount;
|
|
|
|
return NS_OK;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
StringUnicharInputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter,
|
|
|
|
void* aClosure,
|
2014-05-15 00:15:46 +04:00
|
|
|
uint32_t aCount, uint32_t* aReadCount)
|
2003-03-06 22:54:51 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesWritten;
|
|
|
|
uint32_t totalBytesWritten = 0;
|
2003-03-06 22:54:51 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
2013-01-15 16:22:03 +04:00
|
|
|
aCount = XPCOM_MIN(mString.Length() - mPos, aCount);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2004-10-27 06:19:43 +04:00
|
|
|
nsAString::const_iterator iter;
|
2006-07-21 20:28:51 +04:00
|
|
|
mString.BeginReading(iter);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
while (aCount) {
|
2004-10-27 06:19:43 +04:00
|
|
|
rv = aWriter(this, aClosure, iter.get() + mPos,
|
2003-03-06 22:54:51 +03:00
|
|
|
totalBytesWritten, aCount, &bytesWritten);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// don't propagate errors to the caller
|
|
|
|
break;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
aCount -= bytesWritten;
|
|
|
|
totalBytesWritten += bytesWritten;
|
|
|
|
mPos += bytesWritten;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
*aReadCount = totalBytesWritten;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-06-24 23:44:50 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
StringUnicharInputStream::ReadString(uint32_t aCount, nsAString& aString,
|
|
|
|
uint32_t* aReadCount)
|
2005-06-24 23:44:50 +04:00
|
|
|
{
|
|
|
|
if (mPos >= mLen) {
|
|
|
|
*aReadCount = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t amount = mLen - mPos;
|
2005-06-24 23:44:50 +04:00
|
|
|
if (amount > aCount) {
|
|
|
|
amount = aCount;
|
|
|
|
}
|
2006-07-21 20:28:51 +04:00
|
|
|
aString = Substring(mString, mPos, amount);
|
2005-06-24 23:44:50 +04:00
|
|
|
mPos += amount;
|
|
|
|
*aReadCount = amount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult
|
|
|
|
StringUnicharInputStream::Close()
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
|
|
|
mPos = mLen;
|
1998-07-24 00:34:01 +04:00
|
|
|
return NS_OK;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(StringUnicharInputStream, nsIUnicharInputStream)
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class UTF8InputStream final : public nsIUnicharInputStream
|
2014-05-15 00:15:46 +04:00
|
|
|
{
|
1998-04-14 00:24:54 +04:00
|
|
|
public:
|
2002-11-14 00:23:58 +03:00
|
|
|
UTF8InputStream();
|
2006-07-21 20:28:51 +04:00
|
|
|
nsresult Init(nsIInputStream* aStream);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2005-06-24 23:44:50 +04:00
|
|
|
NS_DECL_NSIUNICHARINPUTSTREAM
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2004-01-15 09:14:18 +03:00
|
|
|
private:
|
|
|
|
~UTF8InputStream();
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
protected:
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t Fill(nsresult* aErrorCode);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
static void CountValidUTF8Bytes(const char* aBuf, uint32_t aMaxBytes,
|
|
|
|
uint32_t& aValidUTF8bytes,
|
|
|
|
uint32_t& aValidUTF16CodeUnits);
|
2001-12-04 04:10:43 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> mInput;
|
2013-08-20 15:03:50 +04:00
|
|
|
FallibleTArray<char> mByteData;
|
2014-01-04 19:02:17 +04:00
|
|
|
FallibleTArray<char16_t> mUnicharData;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mByteDataOffset;
|
|
|
|
uint32_t mUnicharDataOffset;
|
|
|
|
uint32_t mUnicharDataLength;
|
1998-04-14 00:24:54 +04:00
|
|
|
};
|
|
|
|
|
2002-11-14 00:23:58 +03:00
|
|
|
UTF8InputStream::UTF8InputStream() :
|
|
|
|
mByteDataOffset(0),
|
|
|
|
mUnicharDataOffset(0),
|
|
|
|
mUnicharDataLength(0)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2002-11-14 00:23:58 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult
|
2006-07-21 20:28:51 +04:00
|
|
|
UTF8InputStream::Init(nsIInputStream* aStream)
|
2002-11-14 00:23:58 +03:00
|
|
|
{
|
2015-05-18 23:50:34 +03:00
|
|
|
if (!mByteData.SetCapacity(STRING_BUFFER_SIZE, mozilla::fallible) ||
|
|
|
|
!mUnicharData.SetCapacity(STRING_BUFFER_SIZE, mozilla::fallible)) {
|
2013-08-20 15:03:50 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2002-11-14 00:23:58 +03:00
|
|
|
mInput = aStream;
|
|
|
|
|
|
|
|
return NS_OK;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_IMPL_ISUPPORTS(UTF8InputStream, nsIUnicharInputStream)
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2001-12-04 04:10:43 +03:00
|
|
|
UTF8InputStream::~UTF8InputStream()
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult
|
|
|
|
UTF8InputStream::Close()
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mInput = nullptr;
|
2013-08-20 15:03:50 +04:00
|
|
|
mByteData.Clear();
|
|
|
|
mUnicharData.Clear();
|
1998-07-24 00:34:01 +04:00
|
|
|
return NS_OK;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult
|
|
|
|
UTF8InputStream::Read(char16_t* aBuf, uint32_t aCount, uint32_t* aReadCount)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
1998-12-16 08:40:20 +03:00
|
|
|
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t readCount = mUnicharDataLength - mUnicharDataOffset;
|
1998-07-24 00:34:01 +04:00
|
|
|
nsresult errorCode;
|
2005-06-24 23:44:50 +04:00
|
|
|
if (0 == readCount) {
|
1998-04-14 00:24:54 +04:00
|
|
|
// Fill the unichar buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesRead = Fill(&errorCode);
|
2008-03-13 22:01:53 +03:00
|
|
|
if (bytesRead <= 0) {
|
1998-07-24 00:34:01 +04:00
|
|
|
*aReadCount = 0;
|
|
|
|
return errorCode;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2008-03-13 22:01:53 +03:00
|
|
|
readCount = bytesRead;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2005-06-24 23:44:50 +04:00
|
|
|
if (readCount > aCount) {
|
|
|
|
readCount = aCount;
|
1998-04-14 00:24:54 +04: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;
|
1998-07-24 00:34:01 +04:00
|
|
|
return NS_OK;
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
UTF8InputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter,
|
|
|
|
void* aClosure,
|
2014-05-15 00:15:46 +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 = NS_OK;
|
|
|
|
if (0 == bytesToWrite) {
|
|
|
|
// Fill the unichar buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesRead = Fill(&rv);
|
2008-03-13 22:01:53 +03:00
|
|
|
if (bytesRead <= 0) {
|
2003-03-06 22:54:51 +03:00
|
|
|
*aReadCount = 0;
|
|
|
|
return rv;
|
|
|
|
}
|
2008-03-13 22:01:53 +03:00
|
|
|
bytesToWrite = bytesRead;
|
2003-03-06 22:54:51 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (bytesToWrite > aCount) {
|
2003-03-06 22:54:51 +03:00
|
|
|
bytesToWrite = aCount;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
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;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
bytesToWrite -= bytesWritten;
|
|
|
|
totalBytesWritten += bytesWritten;
|
|
|
|
mUnicharDataOffset += bytesWritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aReadCount = totalBytesWritten;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2003-03-06 22:54:51 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-06-24 23:44:50 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
UTF8InputStream::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
|
|
|
nsresult errorCode;
|
|
|
|
if (0 == readCount) {
|
|
|
|
// Fill the unichar buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesRead = Fill(&errorCode);
|
2008-03-13 22:01:53 +03:00
|
|
|
if (bytesRead <= 0) {
|
2005-06-24 23:44:50 +04:00
|
|
|
*aReadCount = 0;
|
|
|
|
return errorCode;
|
|
|
|
}
|
2008-03-13 22:01:53 +03:00
|
|
|
readCount = bytesRead;
|
2005-06-24 23:44:50 +04:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
int32_t
|
|
|
|
UTF8InputStream::Fill(nsresult* aErrorCode)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!mInput) {
|
1998-04-14 00:24:54 +04:00
|
|
|
// We already closed the stream!
|
1998-07-24 00:34:01 +04:00
|
|
|
*aErrorCode = NS_BASE_STREAM_CLOSED;
|
1998-04-14 00:24:54 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-20 15:03:50 +04:00
|
|
|
NS_ASSERTION(mByteData.Length() >= mByteDataOffset, "unsigned madness");
|
|
|
|
uint32_t remainder = mByteData.Length() - mByteDataOffset;
|
2002-01-12 01:44:30 +03:00
|
|
|
mByteDataOffset = remainder;
|
2013-08-20 15:03:50 +04:00
|
|
|
uint32_t nb;
|
|
|
|
*aErrorCode = NS_FillArray(mByteData, mInput, remainder, &nb);
|
|
|
|
if (nb == 0) {
|
1998-04-14 00:24:54 +04:00
|
|
|
// Because we assume a many to one conversion, the lingering data
|
|
|
|
// in the byte buffer must be a partial conversion
|
2005-11-26 00:57:13 +03:00
|
|
|
// fragment. Because we know that we have received no more new
|
1998-04-14 00:24:54 +04:00
|
|
|
// data to add to it, we can't convert it. Therefore, we discard
|
|
|
|
// it.
|
|
|
|
return nb;
|
|
|
|
}
|
2013-08-20 15:03:50 +04:00
|
|
|
NS_ASSERTION(remainder + nb == mByteData.Length(), "bad nb");
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
// Now convert as much of the byte buffer to unicode as possible
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t srcLen, dstLen;
|
2014-05-15 00:15:46 +04:00
|
|
|
CountValidUTF8Bytes(mByteData.Elements(), remainder + nb, srcLen, dstLen);
|
2001-12-04 04:10:43 +03:00
|
|
|
|
|
|
|
// the number of UCS2 characters should always be <= the number of
|
|
|
|
// UTF8 chars
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(remainder + nb >= srcLen, "cannot be longer than out buffer");
|
2013-08-20 15:03:50 +04:00
|
|
|
NS_ASSERTION(dstLen <= mUnicharData.Capacity(),
|
2001-12-04 04:10:43 +03:00
|
|
|
"Ouch. I would overflow my buffer if I wasn't so careful.");
|
2014-05-15 00:15:46 +04:00
|
|
|
if (dstLen > mUnicharData.Capacity()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2013-08-20 15:03:50 +04:00
|
|
|
ConvertUTF8toUTF16 converter(mUnicharData.Elements());
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2013-08-20 15:03:50 +04:00
|
|
|
nsASingleFragmentCString::const_char_iterator start = mByteData.Elements();
|
|
|
|
nsASingleFragmentCString::const_char_iterator end = mByteData.Elements() + srcLen;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2002-01-25 02:53:53 +03:00
|
|
|
copy_string(start, end, converter);
|
2008-03-13 22:01:53 +03:00
|
|
|
if (converter.Length() != dstLen) {
|
|
|
|
*aErrorCode = NS_BASE_STREAM_BAD_CONVERSION;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
mUnicharDataOffset = 0;
|
|
|
|
mUnicharDataLength = dstLen;
|
2002-01-12 05:38:08 +03:00
|
|
|
mByteDataOffset = srcLen;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
return dstLen;
|
|
|
|
}
|
|
|
|
|
2002-01-25 02:53:53 +03:00
|
|
|
void
|
2014-05-15 00:15:46 +04:00
|
|
|
UTF8InputStream::CountValidUTF8Bytes(const char* aBuffer, uint32_t aMaxBytes,
|
|
|
|
uint32_t& aValidUTF8bytes,
|
|
|
|
uint32_t& aValidUTF16CodeUnits)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
const char* c = aBuffer;
|
|
|
|
const char* end = aBuffer + aMaxBytes;
|
|
|
|
const char* lastchar = c; // pre-initialize in case of 0-length buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t utf16length = 0;
|
2001-12-05 06:49:13 +03:00
|
|
|
while (c < end && *c) {
|
2002-01-12 05:38:08 +03:00
|
|
|
lastchar = c;
|
2005-08-22 14:50:49 +04:00
|
|
|
utf16length++;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (UTF8traits::isASCII(*c)) {
|
2001-12-04 04:10:43 +03:00
|
|
|
c++;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (UTF8traits::is2byte(*c)) {
|
2001-12-04 04:10:43 +03:00
|
|
|
c += 2;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (UTF8traits::is3byte(*c)) {
|
2001-12-04 04:10:43 +03:00
|
|
|
c += 3;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (UTF8traits::is4byte(*c)) {
|
2001-12-04 04:10:43 +03:00
|
|
|
c += 4;
|
2005-08-22 14:50:49 +04:00
|
|
|
utf16length++; // add 1 more because this will be converted to a
|
2014-05-15 00:15:46 +04:00
|
|
|
// surrogate pair.
|
|
|
|
} else if (UTF8traits::is5byte(*c)) {
|
2001-12-04 04:10:43 +03:00
|
|
|
c += 5;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (UTF8traits::is6byte(*c)) {
|
2001-12-05 06:49:13 +03:00
|
|
|
c += 6;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2001-12-04 04:10:43 +03:00
|
|
|
NS_WARNING("Unrecognized UTF8 string in UTF8InputStream::CountValidUTF8Bytes()");
|
2001-12-05 06:49:13 +03:00
|
|
|
break; // Otherwise we go into an infinite loop. But what happens now?
|
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
2002-01-25 02:53:53 +03:00
|
|
|
if (c > end) {
|
2002-01-12 05:38:08 +03:00
|
|
|
c = lastchar;
|
2005-08-22 14:50:49 +04:00
|
|
|
utf16length--;
|
2002-01-25 02:53:53 +03:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2002-01-25 02:53:53 +03:00
|
|
|
aValidUTF8bytes = c - aBuffer;
|
2005-08-22 14:50:49 +04:00
|
|
|
aValidUTF16CodeUnits = utf16length;
|
2001-12-04 04:10:43 +03:00
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2016-01-12 03:31:29 +03:00
|
|
|
nsresult
|
|
|
|
NS_NewUnicharInputStream(nsIInputStream* aStreamToWrap,
|
|
|
|
nsIUnicharInputStream** aResult)
|
2006-07-21 20:28:51 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2006-07-21 20:28:51 +04:00
|
|
|
|
|
|
|
// Create converter input stream
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UTF8InputStream> it = new UTF8InputStream();
|
2006-07-21 20:28:51 +04:00
|
|
|
nsresult rv = it->Init(aStreamToWrap);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2002-11-14 00:23:58 +03:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2002-11-14 00:23:58 +03:00
|
|
|
|
2015-07-01 19:27:43 +03:00
|
|
|
it.forget(aResult);
|
2006-07-21 20:28:51 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|