gecko-dev/xpcom/io/nsUnicharInputStream.cpp

399 строки
10 KiB
C++
Исходник Обычный вид История

/* -*- 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/. */
#include "nsUnicharInputStream.h"
#include "nsIInputStream.h"
#include "nsIServiceManager.h"
1998-04-14 00:24:54 +04:00
#include "nsString.h"
#include "nsTArray.h"
#include "nsAutoPtr.h"
1998-04-14 00:24:54 +04:00
#include "nsCRT.h"
#include "nsStreamUtils.h"
#include "nsUTF8Utils.h"
#include "mozilla/Attributes.h"
1998-04-14 00:24:54 +04:00
#include <fcntl.h>
#if defined(XP_WIN)
1998-04-14 00:24:54 +04:00
#include <io.h>
#else
#include <unistd.h>
#endif
#define STRING_BUFFER_SIZE 8192
class StringUnicharInputStream final : public nsIUnicharInputStream
{
1998-04-14 00:24:54 +04:00
public:
explicit StringUnicharInputStream(const nsAString& aString) :
mString(aString), mPos(0), mLen(aString.Length()) { }
1998-04-14 00:24:54 +04:00
NS_DECL_ISUPPORTS
NS_DECL_NSIUNICHARINPUTSTREAM
1998-04-14 00:24:54 +04:00
nsString mString;
uint32_t mPos;
uint32_t mLen;
private:
~StringUnicharInputStream() { }
1998-04-14 00:24:54 +04:00
};
NS_IMETHODIMP
StringUnicharInputStream::Read(char16_t* aBuf,
uint32_t aCount,
uint32_t* aReadCount)
1998-04-14 00:24:54 +04:00
{
if (mPos >= mLen) {
*aReadCount = 0;
return NS_OK;
1998-04-14 00:24:54 +04:00
}
nsAString::const_iterator iter;
mString.BeginReading(iter);
const char16_t* us = iter.get();
uint32_t amount = mLen - mPos;
1998-04-14 00:24:54 +04:00
if (amount > aCount) {
amount = aCount;
}
memcpy(aBuf, us + mPos, sizeof(char16_t) * amount);
1998-04-14 00:24:54 +04:00
mPos += amount;
*aReadCount = amount;
return NS_OK;
1998-04-14 00:24:54 +04:00
}
NS_IMETHODIMP
StringUnicharInputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter,
void* aClosure,
uint32_t aCount, uint32_t* aReadCount)
{
uint32_t bytesWritten;
uint32_t totalBytesWritten = 0;
nsresult rv;
aCount = XPCOM_MIN(mString.Length() - mPos, aCount);
nsAString::const_iterator iter;
mString.BeginReading(iter);
while (aCount) {
rv = aWriter(this, aClosure, iter.get() + mPos,
totalBytesWritten, aCount, &bytesWritten);
if (NS_FAILED(rv)) {
// don't propagate errors to the caller
break;
}
aCount -= bytesWritten;
totalBytesWritten += bytesWritten;
mPos += bytesWritten;
}
*aReadCount = totalBytesWritten;
return NS_OK;
}
NS_IMETHODIMP
StringUnicharInputStream::ReadString(uint32_t aCount, nsAString& aString,
uint32_t* aReadCount)
{
if (mPos >= mLen) {
*aReadCount = 0;
return NS_OK;
}
uint32_t amount = mLen - mPos;
if (amount > aCount) {
amount = aCount;
}
aString = Substring(mString, mPos, amount);
mPos += amount;
*aReadCount = amount;
return NS_OK;
}
nsresult
StringUnicharInputStream::Close()
1998-04-14 00:24:54 +04:00
{
mPos = mLen;
return NS_OK;
1998-04-14 00:24:54 +04:00
}
NS_IMPL_ISUPPORTS(StringUnicharInputStream, nsIUnicharInputStream)
1998-04-14 00:24:54 +04:00
//----------------------------------------------------------------------
class UTF8InputStream final : public nsIUnicharInputStream
{
1998-04-14 00:24:54 +04:00
public:
UTF8InputStream();
nsresult Init(nsIInputStream* aStream);
1998-04-14 00:24:54 +04:00
NS_DECL_ISUPPORTS
NS_DECL_NSIUNICHARINPUTSTREAM
1998-04-14 00:24:54 +04:00
private:
~UTF8InputStream();
1998-04-14 00:24:54 +04:00
protected:
int32_t Fill(nsresult* aErrorCode);
1998-04-14 00:24:54 +04:00
static void CountValidUTF8Bytes(const char* aBuf, uint32_t aMaxBytes,
uint32_t& aValidUTF8bytes,
uint32_t& aValidUTF16CodeUnits);
nsCOMPtr<nsIInputStream> mInput;
FallibleTArray<char> mByteData;
FallibleTArray<char16_t> mUnicharData;
uint32_t mByteDataOffset;
uint32_t mUnicharDataOffset;
uint32_t mUnicharDataLength;
1998-04-14 00:24:54 +04:00
};
UTF8InputStream::UTF8InputStream() :
mByteDataOffset(0),
mUnicharDataOffset(0),
mUnicharDataLength(0)
1998-04-14 00:24:54 +04:00
{
}
nsresult
UTF8InputStream::Init(nsIInputStream* aStream)
{
if (!mByteData.SetCapacity(STRING_BUFFER_SIZE, mozilla::fallible) ||
!mUnicharData.SetCapacity(STRING_BUFFER_SIZE, mozilla::fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
mInput = aStream;
return NS_OK;
1998-04-14 00:24:54 +04:00
}
NS_IMPL_ISUPPORTS(UTF8InputStream, nsIUnicharInputStream)
1998-04-14 00:24:54 +04:00
UTF8InputStream::~UTF8InputStream()
1998-04-14 00:24:54 +04:00
{
Close();
}
nsresult
UTF8InputStream::Close()
1998-04-14 00:24:54 +04:00
{
mInput = nullptr;
mByteData.Clear();
mUnicharData.Clear();
return NS_OK;
1998-04-14 00:24:54 +04:00
}
nsresult
UTF8InputStream::Read(char16_t* aBuf, uint32_t aCount, uint32_t* aReadCount)
1998-04-14 00:24:54 +04:00
{
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
uint32_t readCount = mUnicharDataLength - mUnicharDataOffset;
nsresult errorCode;
if (0 == readCount) {
1998-04-14 00:24:54 +04:00
// Fill the unichar buffer
int32_t bytesRead = Fill(&errorCode);
if (bytesRead <= 0) {
*aReadCount = 0;
return errorCode;
1998-04-14 00:24:54 +04:00
}
readCount = bytesRead;
1998-04-14 00:24:54 +04:00
}
if (readCount > aCount) {
readCount = aCount;
1998-04-14 00:24:54 +04:00
}
memcpy(aBuf, mUnicharData.Elements() + mUnicharDataOffset,
readCount * sizeof(char16_t));
mUnicharDataOffset += readCount;
*aReadCount = readCount;
return NS_OK;
1998-04-14 00:24:54 +04:00
}
NS_IMETHODIMP
UTF8InputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter,
void* aClosure,
uint32_t aCount, uint32_t* aReadCount)
{
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
uint32_t bytesToWrite = mUnicharDataLength - mUnicharDataOffset;
nsresult rv = NS_OK;
if (0 == bytesToWrite) {
// Fill the unichar buffer
int32_t bytesRead = Fill(&rv);
if (bytesRead <= 0) {
*aReadCount = 0;
return rv;
}
bytesToWrite = bytesRead;
}
if (bytesToWrite > aCount) {
bytesToWrite = aCount;
}
uint32_t bytesWritten;
uint32_t totalBytesWritten = 0;
while (bytesToWrite) {
rv = aWriter(this, aClosure,
mUnicharData.Elements() + mUnicharDataOffset,
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;
}
NS_IMETHODIMP
UTF8InputStream::ReadString(uint32_t aCount, nsAString& aString,
uint32_t* aReadCount)
{
NS_ASSERTION(mUnicharDataLength >= mUnicharDataOffset, "unsigned madness");
uint32_t readCount = mUnicharDataLength - mUnicharDataOffset;
nsresult errorCode;
if (0 == readCount) {
// Fill the unichar buffer
int32_t bytesRead = Fill(&errorCode);
if (bytesRead <= 0) {
*aReadCount = 0;
return errorCode;
}
readCount = bytesRead;
}
if (readCount > aCount) {
readCount = aCount;
}
const char16_t* buf = mUnicharData.Elements() + mUnicharDataOffset;
aString.Assign(buf, readCount);
mUnicharDataOffset += readCount;
*aReadCount = readCount;
return NS_OK;
}
int32_t
UTF8InputStream::Fill(nsresult* aErrorCode)
1998-04-14 00:24:54 +04:00
{
if (!mInput) {
1998-04-14 00:24:54 +04:00
// We already closed the stream!
*aErrorCode = NS_BASE_STREAM_CLOSED;
1998-04-14 00:24:54 +04:00
return -1;
}
NS_ASSERTION(mByteData.Length() >= mByteDataOffset, "unsigned madness");
uint32_t remainder = mByteData.Length() - mByteDataOffset;
mByteDataOffset = remainder;
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
// 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;
}
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
uint32_t srcLen, dstLen;
CountValidUTF8Bytes(mByteData.Elements(), remainder + nb, srcLen, dstLen);
// the number of UCS2 characters should always be <= the number of
// UTF8 chars
NS_ASSERTION(remainder + nb >= srcLen, "cannot be longer than out buffer");
NS_ASSERTION(dstLen <= mUnicharData.Capacity(),
"Ouch. I would overflow my buffer if I wasn't so careful.");
if (dstLen > mUnicharData.Capacity()) {
return 0;
}
ConvertUTF8toUTF16 converter(mUnicharData.Elements());
nsASingleFragmentCString::const_char_iterator start = mByteData.Elements();
nsASingleFragmentCString::const_char_iterator end = mByteData.Elements() + srcLen;
copy_string(start, end, converter);
if (converter.Length() != dstLen) {
*aErrorCode = NS_BASE_STREAM_BAD_CONVERSION;
return -1;
}
1998-04-14 00:24:54 +04:00
mUnicharDataOffset = 0;
mUnicharDataLength = dstLen;
mByteDataOffset = srcLen;
1998-04-14 00:24:54 +04:00
return dstLen;
}
void
UTF8InputStream::CountValidUTF8Bytes(const char* aBuffer, uint32_t aMaxBytes,
uint32_t& aValidUTF8bytes,
uint32_t& aValidUTF16CodeUnits)
1998-04-14 00:24:54 +04:00
{
const char* c = aBuffer;
const char* end = aBuffer + aMaxBytes;
const char* lastchar = c; // pre-initialize in case of 0-length buffer
uint32_t utf16length = 0;
while (c < end && *c) {
lastchar = c;
utf16length++;
if (UTF8traits::isASCII(*c)) {
c++;
} else if (UTF8traits::is2byte(*c)) {
c += 2;
} else if (UTF8traits::is3byte(*c)) {
c += 3;
} else if (UTF8traits::is4byte(*c)) {
c += 4;
utf16length++; // add 1 more because this will be converted to a
// surrogate pair.
} else if (UTF8traits::is5byte(*c)) {
c += 5;
} else if (UTF8traits::is6byte(*c)) {
c += 6;
} else {
NS_WARNING("Unrecognized UTF8 string in UTF8InputStream::CountValidUTF8Bytes()");
break; // Otherwise we go into an infinite loop. But what happens now?
}
1998-04-14 00:24:54 +04:00
}
if (c > end) {
c = lastchar;
utf16length--;
}
1998-04-14 00:24:54 +04:00
aValidUTF8bytes = c - aBuffer;
aValidUTF16CodeUnits = utf16length;
}
1998-04-14 00:24:54 +04:00
nsresult
NS_NewUnicharInputStream(nsIInputStream* aStreamToWrap,
nsIUnicharInputStream** aResult)
{
*aResult = nullptr;
// Create converter input stream
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<UTF8InputStream> it = new UTF8InputStream();
nsresult rv = it->Init(aStreamToWrap);
if (NS_FAILED(rv)) {
return rv;
}
it.forget(aResult);
return NS_OK;
}