1999-11-16 22:12:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
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-11-16 22:12:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This file contains implementations of the nsIBinaryInputStream and
|
|
|
|
* nsIBinaryOutputStream interfaces. Together, these interfaces allows reading
|
|
|
|
* and writing of primitive data types (integers, floating-point values,
|
|
|
|
* booleans, etc.) to a stream in a binary, untagged, fixed-endianness format.
|
|
|
|
* This might be used, for example, to implement network protocols or to
|
|
|
|
* produce architecture-neutral binary disk files, i.e. ones that can be read
|
|
|
|
* and written by both big-endian and little-endian platforms. Output is
|
|
|
|
* written in big-endian order (high-order byte first), as this is traditional
|
|
|
|
* network order.
|
|
|
|
*
|
|
|
|
* @See nsIBinaryInputStream
|
|
|
|
* @See nsIBinaryOutputStream
|
|
|
|
*/
|
2001-07-31 23:05:34 +04:00
|
|
|
#include <string.h>
|
1999-11-16 22:12:41 +03:00
|
|
|
#include "nsBinaryStream.h"
|
2001-07-31 23:05:34 +04:00
|
|
|
#include "nsCRT.h"
|
2005-04-20 19:39:24 +04:00
|
|
|
#include "nsString.h"
|
2007-08-07 06:00:55 +04:00
|
|
|
#include "nsISerializable.h"
|
|
|
|
#include "nsIClassInfo.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2011-06-09 23:10:42 +04:00
|
|
|
#include "nsIURI.h" // for NS_IURI_IID
|
2013-04-24 11:40:33 +04:00
|
|
|
#include "mozilla/Endian.h"
|
1999-11-16 22:12:41 +03:00
|
|
|
|
2013-04-19 13:25:33 +04:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
|
2003-11-06 09:03:14 +03:00
|
|
|
NS_IMPL_ISUPPORTS3(nsBinaryOutputStream, nsIObjectOutputStream, nsIBinaryOutputStream, nsIOutputStream)
|
1999-11-16 22:12:41 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2005-12-14 19:51:38 +03:00
|
|
|
nsBinaryOutputStream::Flush()
|
|
|
|
{
|
|
|
|
NS_ENSURE_STATE(mOutputStream);
|
|
|
|
return mOutputStream->Flush();
|
|
|
|
}
|
1999-11-16 22:12:41 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2005-12-14 19:51:38 +03:00
|
|
|
nsBinaryOutputStream::Close()
|
|
|
|
{
|
|
|
|
NS_ENSURE_STATE(mOutputStream);
|
|
|
|
return mOutputStream->Close();
|
|
|
|
}
|
1999-11-16 22:12:41 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::Write(const char *aBuf, uint32_t aCount, uint32_t *aActualBytes)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
2005-12-14 19:51:38 +03:00
|
|
|
NS_ENSURE_STATE(mOutputStream);
|
1999-11-16 22:12:41 +03:00
|
|
|
return mOutputStream->Write(aBuf, aCount, aActualBytes);
|
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteFrom");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteSegments");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsBinaryOutputStream::IsNonBlocking(bool *aNonBlocking)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
2005-12-14 19:51:38 +03:00
|
|
|
NS_ENSURE_STATE(mOutputStream);
|
2002-03-12 03:59:06 +03:00
|
|
|
return mOutputStream->IsNonBlocking(aNonBlocking);
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|
|
|
|
|
1999-11-21 11:01:08 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::WriteFully(const char *aBuf, uint32_t aCount)
|
1999-11-21 11:01:08 +03:00
|
|
|
{
|
2005-12-14 19:51:38 +03:00
|
|
|
NS_ENSURE_STATE(mOutputStream);
|
|
|
|
|
1999-11-21 11:01:08 +03:00
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesWritten;
|
2001-07-31 23:05:34 +04:00
|
|
|
|
|
|
|
rv = mOutputStream->Write(aBuf, aCount, &bytesWritten);
|
1999-11-21 11:01:08 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-07-31 23:05:34 +04:00
|
|
|
if (bytesWritten != aCount)
|
1999-11-21 11:01:08 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-16 22:12:41 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::SetOutputStream(nsIOutputStream *aOutputStream)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aOutputStream);
|
|
|
|
mOutputStream = aOutputStream;
|
2003-03-14 00:23:18 +03:00
|
|
|
mBufferAccess = do_QueryInterface(aOutputStream);
|
1999-11-16 22:12:41 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsBinaryOutputStream::WriteBoolean(bool aBoolean)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
|
|
|
return Write8(aBoolean);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::Write8(uint8_t aByte)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
1999-11-21 11:01:08 +03:00
|
|
|
return WriteFully((const char*)&aByte, sizeof aByte);
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::Write16(uint16_t a16)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
2013-04-24 11:40:33 +04:00
|
|
|
a16 = mozilla::NativeEndian::swapToBigEndian(a16);
|
1999-11-21 11:01:08 +03:00
|
|
|
return WriteFully((const char*)&a16, sizeof a16);
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::Write32(uint32_t a32)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
2013-04-24 11:40:33 +04:00
|
|
|
a32 = mozilla::NativeEndian::swapToBigEndian(a32);
|
1999-11-21 11:01:08 +03:00
|
|
|
return WriteFully((const char*)&a32, sizeof a32);
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::Write64(uint64_t a64)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesWritten;
|
1999-11-16 22:12:41 +03:00
|
|
|
|
2013-04-24 11:40:33 +04:00
|
|
|
a64 = mozilla::NativeEndian::swapToBigEndian(a64);
|
2007-07-08 11:08:04 +04:00
|
|
|
rv = Write(reinterpret_cast<char*>(&a64), sizeof a64, &bytesWritten);
|
1999-11-16 22:12:41 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-07-31 23:05:34 +04:00
|
|
|
if (bytesWritten != sizeof a64)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteFloat(float aFloat)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_ASSERTION(sizeof(float) == sizeof (uint32_t),
|
1999-11-16 22:12:41 +03:00
|
|
|
"False assumption about sizeof(float)");
|
2012-08-22 19:56:38 +04:00
|
|
|
return Write32(*reinterpret_cast<uint32_t*>(&aFloat));
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteDouble(double aDouble)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_ASSERTION(sizeof(double) == sizeof(uint64_t),
|
1999-11-16 22:12:41 +03:00
|
|
|
"False assumption about sizeof(double)");
|
2012-08-22 19:56:38 +04:00
|
|
|
return Write64(*reinterpret_cast<uint64_t*>(&aDouble));
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteStringZ(const char *aString)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length;
|
2001-07-31 23:05:34 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
length = strlen(aString);
|
|
|
|
rv = Write32(length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return WriteFully(aString, length);
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteWStringZ(const PRUnichar* aString)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length, byteCount;
|
2001-07-31 23:05:34 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2012-05-30 21:43:39 +04:00
|
|
|
length = NS_strlen(aString);
|
2001-07-31 23:05:34 +04:00
|
|
|
rv = Write32(length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return NS_OK;
|
|
|
|
byteCount = length * sizeof(PRUnichar);
|
|
|
|
|
|
|
|
#ifdef IS_BIG_ENDIAN
|
2007-07-08 11:08:04 +04:00
|
|
|
rv = WriteBytes(reinterpret_cast<const char*>(aString), byteCount);
|
2001-07-31 23:05:34 +04:00
|
|
|
#else
|
2003-04-15 22:17:54 +04:00
|
|
|
// XXX use WriteSegments here to avoid copy!
|
2001-07-31 23:05:34 +04:00
|
|
|
PRUnichar *copy, temp[64];
|
|
|
|
if (length <= 64) {
|
|
|
|
copy = temp;
|
|
|
|
} else {
|
2012-02-07 22:06:05 +04:00
|
|
|
copy = reinterpret_cast<PRUnichar*>(moz_malloc(byteCount));
|
2001-07-31 23:05:34 +04:00
|
|
|
if (!copy)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-09-15 00:09:52 +04:00
|
|
|
NS_ASSERTION((uintptr_t(aString) & 0x1) == 0, "aString not properly aligned");
|
2013-04-24 11:40:33 +04:00
|
|
|
mozilla::NativeEndian::copyAndSwapToBigEndian(copy, aString, length);
|
2007-07-08 11:08:04 +04:00
|
|
|
rv = WriteBytes(reinterpret_cast<const char*>(copy), byteCount);
|
2001-07-31 23:05:34 +04:00
|
|
|
if (copy != temp)
|
2012-02-07 22:06:05 +04:00
|
|
|
moz_free(copy);
|
2001-07-31 23:05:34 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return rv;
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteUtf8Z(const PRUnichar* aString)
|
|
|
|
{
|
2005-04-20 19:39:24 +04:00
|
|
|
return WriteStringZ(NS_ConvertUTF16toUTF8(aString).get());
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::WriteBytes(const char *aString, uint32_t aLength)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesWritten;
|
1999-11-16 22:12:41 +03:00
|
|
|
|
|
|
|
rv = Write(aString, aLength, &bytesWritten);
|
2001-07-31 23:05:34 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-16 22:12:41 +03:00
|
|
|
if (bytesWritten != aLength)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-03-14 00:23:18 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::WriteByteArray(uint8_t *aBytes, uint32_t aLength)
|
2003-03-14 00:23:18 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
return WriteBytes(reinterpret_cast<char *>(aBytes), aLength);
|
2003-03-14 00:23:18 +03:00
|
|
|
}
|
|
|
|
|
1999-11-16 22:12:41 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsBinaryOutputStream::WriteObject(nsISupports* aObject, bool aIsStrongRef)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
2007-08-07 06:00:55 +04:00
|
|
|
return WriteCompoundObject(aObject, NS_GET_IID(nsISupports),
|
|
|
|
aIsStrongRef);
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteSingleRefObject(nsISupports* aObject)
|
|
|
|
{
|
2007-08-07 06:00:55 +04:00
|
|
|
return WriteCompoundObject(aObject, NS_GET_IID(nsISupports),
|
2011-10-17 18:59:28 +04:00
|
|
|
true);
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteCompoundObject(nsISupports* aObject,
|
|
|
|
const nsIID& aIID,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aIsStrongRef)
|
2001-07-31 23:05:34 +04:00
|
|
|
{
|
2007-08-07 06:00:55 +04:00
|
|
|
// Can't deal with weak refs
|
|
|
|
NS_ENSURE_TRUE(aIsStrongRef, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIClassInfo> classInfo = do_QueryInterface(aObject);
|
|
|
|
NS_ENSURE_TRUE(classInfo, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISerializable> serializable = do_QueryInterface(aObject);
|
|
|
|
NS_ENSURE_TRUE(serializable, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsCID cid;
|
|
|
|
classInfo->GetClassIDNoAlloc(&cid);
|
|
|
|
|
|
|
|
nsresult rv = WriteID(cid);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = WriteID(aIID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return serializable->Write(this);
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteID(const nsIID& aIID)
|
|
|
|
{
|
2007-08-07 06:00:55 +04:00
|
|
|
nsresult rv = Write32(aIID.m0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = Write16(aIID.m1);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = Write16(aIID.m2);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; ++i) {
|
|
|
|
rv = Write8(aIID.m3[i]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(char*)
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::GetBuffer(uint32_t aLength, uint32_t aAlignMask)
|
2001-07-31 23:05:34 +04:00
|
|
|
{
|
|
|
|
if (mBufferAccess)
|
|
|
|
return mBufferAccess->GetBuffer(aLength, aAlignMask);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryOutputStream::PutBuffer(char* aBuffer, uint32_t aLength)
|
2001-07-31 23:05:34 +04:00
|
|
|
{
|
|
|
|
if (mBufferAccess)
|
|
|
|
mBufferAccess->PutBuffer(aBuffer, aLength);
|
|
|
|
}
|
|
|
|
|
2004-04-03 21:09:07 +04:00
|
|
|
NS_IMPL_ISUPPORTS3(nsBinaryInputStream, nsIObjectInputStream, nsIBinaryInputStream, nsIInputStream)
|
1999-11-16 22:12:41 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::Available(uint64_t* aResult)
|
2001-07-31 23:05:34 +04:00
|
|
|
{
|
2005-12-14 19:51:38 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
2001-07-31 23:05:34 +04:00
|
|
|
return mInputStream->Available(aResult);
|
|
|
|
}
|
1999-11-16 22:12:41 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t *aNumRead)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
2005-12-14 19:51:38 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
2007-07-18 05:54:55 +04:00
|
|
|
|
|
|
|
// mInputStream might give us short reads, so deal with that.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t totalRead = 0;
|
2007-07-18 05:54:55 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead;
|
2007-07-18 05:54:55 +04:00
|
|
|
do {
|
|
|
|
nsresult rv = mInputStream->Read(aBuffer, aCount, &bytesRead);
|
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK && totalRead != 0) {
|
|
|
|
// We already read some data. Return it.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
totalRead += bytesRead;
|
|
|
|
aBuffer += bytesRead;
|
|
|
|
aCount -= bytesRead;
|
|
|
|
} while (aCount != 0 && bytesRead != 0);
|
|
|
|
|
|
|
|
*aNumRead = totalRead;
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
2002-11-14 21:16:31 +03:00
|
|
|
|
|
|
|
// when forwarding ReadSegments to mInputStream, we need to make sure
|
|
|
|
// 'this' is being passed to the writer each time. To do this, we need
|
|
|
|
// a thunking function which keeps the real input stream around.
|
|
|
|
|
|
|
|
// the closure wrapper
|
|
|
|
struct ReadSegmentsClosure {
|
|
|
|
nsIInputStream* mRealInputStream;
|
|
|
|
void* mRealClosure;
|
|
|
|
nsWriteSegmentFun mRealWriter;
|
2007-07-18 05:54:55 +04:00
|
|
|
nsresult mRealResult;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mBytesRead; // to properly implement aToOffset
|
2002-11-14 21:16:31 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// the thunking function
|
|
|
|
static NS_METHOD
|
|
|
|
ReadSegmentForwardingThunk(nsIInputStream* aStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char* aFromSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2002-11-14 21:16:31 +03:00
|
|
|
{
|
|
|
|
ReadSegmentsClosure* thunkClosure =
|
2007-07-08 11:08:04 +04:00
|
|
|
reinterpret_cast<ReadSegmentsClosure*>(aClosure);
|
2002-11-14 21:16:31 +03:00
|
|
|
|
2007-07-18 05:54:55 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(thunkClosure->mRealResult),
|
|
|
|
"How did this get to be a failure status?");
|
|
|
|
|
|
|
|
thunkClosure->mRealResult =
|
|
|
|
thunkClosure->mRealWriter(thunkClosure->mRealInputStream,
|
|
|
|
thunkClosure->mRealClosure,
|
|
|
|
aFromSegment,
|
|
|
|
thunkClosure->mBytesRead + aToOffset,
|
|
|
|
aCount, aWriteCount);
|
|
|
|
|
|
|
|
return thunkClosure->mRealResult;
|
2002-11-14 21:16:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, uint32_t count, uint32_t *_retval)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
2005-12-14 19:51:38 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
|
2007-07-18 05:54:55 +04:00
|
|
|
ReadSegmentsClosure thunkClosure = { this, closure, writer, NS_OK, 0 };
|
2002-11-14 21:16:31 +03:00
|
|
|
|
2007-07-18 05:54:55 +04:00
|
|
|
// mInputStream might give us short reads, so deal with that.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead;
|
2007-07-18 05:54:55 +04:00
|
|
|
do {
|
|
|
|
nsresult rv = mInputStream->ReadSegments(ReadSegmentForwardingThunk,
|
|
|
|
&thunkClosure,
|
|
|
|
count, &bytesRead);
|
|
|
|
|
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK && thunkClosure.mBytesRead != 0) {
|
|
|
|
// We already read some data. Return it.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
thunkClosure.mBytesRead += bytesRead;
|
|
|
|
count -= bytesRead;
|
|
|
|
} while (count != 0 && bytesRead != 0 &&
|
|
|
|
NS_SUCCEEDED(thunkClosure.mRealResult));
|
|
|
|
|
|
|
|
*_retval = thunkClosure.mBytesRead;
|
|
|
|
|
|
|
|
return NS_OK;
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsBinaryInputStream::IsNonBlocking(bool *aNonBlocking)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
2005-12-14 19:51:38 +03:00
|
|
|
NS_ENSURE_STATE(mInputStream);
|
2002-03-12 03:59:06 +03:00
|
|
|
return mInputStream->IsNonBlocking(aNonBlocking);
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|
|
|
|
|
1999-11-16 22:12:41 +03:00
|
|
|
NS_IMETHODIMP
|
2005-12-14 19:51:38 +03:00
|
|
|
nsBinaryInputStream::Close()
|
|
|
|
{
|
|
|
|
NS_ENSURE_STATE(mInputStream);
|
|
|
|
return mInputStream->Close();
|
|
|
|
}
|
1999-11-16 22:12:41 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::SetInputStream(nsIInputStream *aInputStream)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInputStream);
|
|
|
|
mInputStream = aInputStream;
|
2003-03-14 00:23:18 +03:00
|
|
|
mBufferAccess = do_QueryInterface(aInputStream);
|
1999-11-16 22:12:41 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsBinaryInputStream::ReadBoolean(bool* aBoolean)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t byteResult;
|
1999-11-16 22:12:41 +03:00
|
|
|
nsresult rv = Read8(&byteResult);
|
2009-10-01 02:15:51 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-10-20 03:26:52 +04:00
|
|
|
*aBoolean = !!byteResult;
|
1999-11-16 22:12:41 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::Read8(uint8_t* aByte)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead;
|
2001-07-31 23:05:34 +04:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
rv = Read(reinterpret_cast<char*>(aByte), sizeof(*aByte), &bytesRead);
|
1999-11-21 11:01:08 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-07-31 23:05:34 +04:00
|
|
|
if (bytesRead != 1)
|
1999-11-16 22:12:41 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::Read16(uint16_t* a16)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead;
|
1999-11-16 22:12:41 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
rv = Read(reinterpret_cast<char*>(a16), sizeof *a16, &bytesRead);
|
1999-11-21 11:01:08 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-16 22:12:41 +03:00
|
|
|
if (bytesRead != sizeof *a16)
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-04-24 11:40:33 +04:00
|
|
|
*a16 = mozilla::NativeEndian::swapFromBigEndian(*a16);
|
1999-11-16 22:12:41 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::Read32(uint32_t* a32)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead;
|
1999-11-16 22:12:41 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
rv = Read(reinterpret_cast<char*>(a32), sizeof *a32, &bytesRead);
|
1999-11-21 11:01:08 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-16 22:12:41 +03:00
|
|
|
if (bytesRead != sizeof *a32)
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-04-24 11:40:33 +04:00
|
|
|
*a32 = mozilla::NativeEndian::swapFromBigEndian(*a32);
|
1999-11-16 22:12:41 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::Read64(uint64_t* a64)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead;
|
1999-11-16 22:12:41 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
rv = Read(reinterpret_cast<char*>(a64), sizeof *a64, &bytesRead);
|
1999-11-16 22:12:41 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-10-31 11:29:25 +03:00
|
|
|
if (bytesRead != sizeof *a64)
|
2001-07-31 23:05:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2013-04-24 11:40:33 +04:00
|
|
|
*a64 = mozilla::NativeEndian::swapFromBigEndian(*a64);
|
2001-07-31 23:05:34 +04:00
|
|
|
return rv;
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::ReadFloat(float* aFloat)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_ASSERTION(sizeof(float) == sizeof (uint32_t),
|
2001-07-31 23:05:34 +04:00
|
|
|
"False assumption about sizeof(float)");
|
2012-08-22 19:56:38 +04:00
|
|
|
return Read32(reinterpret_cast<uint32_t*>(aFloat));
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::ReadDouble(double* aDouble)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_ASSERTION(sizeof(double) == sizeof(uint64_t),
|
2001-07-31 23:05:34 +04:00
|
|
|
"False assumption about sizeof(double)");
|
2012-08-22 19:56:38 +04:00
|
|
|
return Read64(reinterpret_cast<uint64_t*>(aDouble));
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
2002-11-14 21:16:31 +03:00
|
|
|
static NS_METHOD
|
|
|
|
WriteSegmentToCString(nsIInputStream* aStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char* aFromSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2002-11-14 21:16:31 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsACString* outString = static_cast<nsACString*>(aClosure);
|
2002-11-14 21:16:31 +03:00
|
|
|
|
|
|
|
outString->Append(aFromSegment, aCount);
|
|
|
|
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-16 22:12:41 +03:00
|
|
|
NS_IMETHODIMP
|
2002-11-14 21:16:31 +03:00
|
|
|
nsBinaryInputStream::ReadCString(nsACString& aString)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
1999-11-21 11:01:08 +03:00
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length, bytesRead;
|
1999-11-21 11:01:08 +03:00
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
rv = Read32(&length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-11-14 21:16:31 +03:00
|
|
|
aString.Truncate();
|
|
|
|
rv = ReadSegments(WriteSegmentToCString, &aString, length, &bytesRead);
|
2001-07-31 23:05:34 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-11-14 21:16:31 +03:00
|
|
|
|
|
|
|
if (bytesRead != length)
|
2001-07-31 23:05:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-11-21 11:01:08 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
2002-11-19 05:57:30 +03:00
|
|
|
|
|
|
|
// sometimes, WriteSegmentToString will be handed an odd-number of
|
|
|
|
// bytes, which means we only have half of the last PRUnichar
|
|
|
|
struct WriteStringClosure {
|
2003-04-15 22:17:54 +04:00
|
|
|
PRUnichar *mWriteCursor;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mHasCarryoverByte;
|
2002-11-19 05:57:30 +03:00
|
|
|
char mCarryoverByte;
|
|
|
|
};
|
|
|
|
|
|
|
|
// there are a few cases we have to account for here:
|
|
|
|
// * even length buffer, no carryover - easy, just append
|
|
|
|
// * odd length buffer, no carryover - the last byte needs to be saved
|
|
|
|
// for carryover
|
|
|
|
// * odd length buffer, with carryover - first byte needs to be used
|
|
|
|
// with the carryover byte, and
|
|
|
|
// the rest of the even length
|
|
|
|
// buffer is appended as normal
|
|
|
|
// * even length buffer, with carryover - the first byte needs to be
|
|
|
|
// used with the previous carryover byte.
|
|
|
|
// this gives you an odd length buffer,
|
|
|
|
// so you have to save the last byte for
|
|
|
|
// the next carryover
|
|
|
|
|
|
|
|
|
2002-11-14 21:16:31 +03:00
|
|
|
// same version of the above, but with correct casting and endian swapping
|
|
|
|
static NS_METHOD
|
|
|
|
WriteSegmentToString(nsIInputStream* aStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char* aFromSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2002-11-14 21:16:31 +03:00
|
|
|
{
|
2002-11-19 05:57:30 +03:00
|
|
|
NS_PRECONDITION(aCount > 0, "Why are we being told to write 0 bytes?");
|
|
|
|
NS_PRECONDITION(sizeof(PRUnichar) == 2, "We can't handle other sizes!");
|
2003-04-15 22:17:54 +04:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
WriteStringClosure* closure = static_cast<WriteStringClosure*>(aClosure);
|
2003-04-15 22:17:54 +04:00
|
|
|
PRUnichar *cursor = closure->mWriteCursor;
|
2002-11-19 05:57:30 +03:00
|
|
|
|
|
|
|
// we're always going to consume the whole buffer no matter what
|
|
|
|
// happens, so take care of that right now.. that allows us to
|
|
|
|
// tweak aCount later. Do NOT move this!
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
|
|
|
|
// if the last Write had an odd-number of bytes read, then
|
|
|
|
if (closure->mHasCarryoverByte) {
|
|
|
|
// re-create the two-byte sequence we want to work with
|
|
|
|
char bytes[2] = { closure->mCarryoverByte, *aFromSegment };
|
2003-04-15 22:17:54 +04:00
|
|
|
*cursor = *(PRUnichar*)bytes;
|
2002-11-19 05:57:30 +03:00
|
|
|
// Now the little endianness dance
|
2013-04-24 11:40:33 +04:00
|
|
|
mozilla::NativeEndian::swapToBigEndianInPlace(cursor, 1);
|
2003-04-15 22:17:54 +04:00
|
|
|
++cursor;
|
2002-11-19 05:57:30 +03:00
|
|
|
|
|
|
|
// now skip past the first byte of the buffer.. code from here
|
|
|
|
// can assume normal operations, but should not assume aCount
|
|
|
|
// is relative to the ORIGINAL buffer
|
|
|
|
++aFromSegment;
|
|
|
|
--aCount;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
closure->mHasCarryoverByte = false;
|
2002-11-19 05:57:30 +03:00
|
|
|
}
|
|
|
|
|
2003-04-15 22:17:54 +04:00
|
|
|
// this array is possibly unaligned... be careful how we access it!
|
2002-11-14 21:16:31 +03:00
|
|
|
const PRUnichar *unicodeSegment =
|
2007-07-08 11:08:04 +04:00
|
|
|
reinterpret_cast<const PRUnichar*>(aFromSegment);
|
2003-04-15 22:17:54 +04:00
|
|
|
|
|
|
|
// calculate number of full characters in segment (aCount could be odd!)
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t segmentLength = aCount / sizeof(PRUnichar);
|
2002-11-14 21:16:31 +03:00
|
|
|
|
2003-04-15 22:17:54 +04:00
|
|
|
// copy all data into our aligned buffer. byte swap if necessary.
|
2013-04-24 11:40:33 +04:00
|
|
|
// cursor may be unaligned, so we cannot use copyAndSwapToBigEndian directly
|
2003-04-15 22:17:54 +04:00
|
|
|
memcpy(cursor, unicodeSegment, segmentLength * sizeof(PRUnichar));
|
|
|
|
PRUnichar *end = cursor + segmentLength;
|
2013-04-24 11:40:33 +04:00
|
|
|
mozilla::NativeEndian::swapToBigEndianInPlace(cursor, segmentLength);
|
2003-04-15 22:17:54 +04:00
|
|
|
closure->mWriteCursor = end;
|
2002-11-14 21:16:31 +03:00
|
|
|
|
2002-11-19 05:57:30 +03:00
|
|
|
// remember this is the modifed aCount and aFromSegment,
|
|
|
|
// so that will take into account the fact that we might have
|
|
|
|
// skipped the first byte in the buffer
|
|
|
|
if (aCount % sizeof(PRUnichar) != 0) {
|
|
|
|
// we must have had a carryover byte, that we'll need the next
|
|
|
|
// time around
|
|
|
|
closure->mCarryoverByte = aFromSegment[aCount - 1];
|
2011-10-17 18:59:28 +04:00
|
|
|
closure->mHasCarryoverByte = true;
|
2002-11-19 05:57:30 +03:00
|
|
|
}
|
|
|
|
|
2002-11-14 21:16:31 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-16 22:12:41 +03:00
|
|
|
NS_IMETHODIMP
|
2002-11-14 21:16:31 +03:00
|
|
|
nsBinaryInputStream::ReadString(nsAString& aString)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
2001-07-31 23:05:34 +04:00
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length, bytesRead;
|
2001-07-31 23:05:34 +04:00
|
|
|
|
|
|
|
rv = Read32(&length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2007-11-30 21:05:54 +03:00
|
|
|
if (length == 0) {
|
|
|
|
aString.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-04-15 22:17:54 +04:00
|
|
|
// pre-allocate output buffer, and get direct access to buffer...
|
2013-05-07 19:52:52 +04:00
|
|
|
if (!aString.SetLength(length, mozilla::fallible_t()))
|
2006-06-22 23:07:30 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2003-04-15 22:17:54 +04:00
|
|
|
nsAString::iterator start;
|
|
|
|
aString.BeginWriting(start);
|
2002-11-19 05:57:30 +03:00
|
|
|
|
|
|
|
WriteStringClosure closure;
|
2003-04-15 22:17:54 +04:00
|
|
|
closure.mWriteCursor = start.get();
|
2011-10-17 18:59:28 +04:00
|
|
|
closure.mHasCarryoverByte = false;
|
2002-11-19 05:57:30 +03:00
|
|
|
|
|
|
|
rv = ReadSegments(WriteSegmentToString, &closure,
|
|
|
|
length*sizeof(PRUnichar), &bytesRead);
|
2001-07-31 23:05:34 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-11-19 05:57:30 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(!closure.mHasCarryoverByte, "some strange stream corruption!");
|
2002-11-14 21:16:31 +03:00
|
|
|
|
|
|
|
if (bytesRead != length*sizeof(PRUnichar))
|
2001-07-31 23:05:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::ReadBytes(uint32_t aLength, char* *_rval)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead;
|
1999-11-16 22:12:41 +03:00
|
|
|
char* s;
|
|
|
|
|
2012-02-07 22:06:05 +04:00
|
|
|
s = reinterpret_cast<char*>(moz_malloc(aLength));
|
1999-11-16 22:12:41 +03:00
|
|
|
if (!s)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = Read(s, aLength, &bytesRead);
|
2003-03-14 00:23:18 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-02-07 22:06:05 +04:00
|
|
|
moz_free(s);
|
2003-03-14 00:23:18 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2001-07-31 23:05:34 +04:00
|
|
|
if (bytesRead != aLength) {
|
2012-02-07 22:06:05 +04:00
|
|
|
moz_free(s);
|
2001-07-31 23:05:34 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-11-16 22:12:41 +03:00
|
|
|
|
2003-03-14 00:23:18 +03:00
|
|
|
*_rval = s;
|
1999-11-16 22:12:41 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-14 00:23:18 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::ReadByteArray(uint32_t aLength, uint8_t* *_rval)
|
2003-03-14 00:23:18 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
return ReadBytes(aLength, reinterpret_cast<char **>(_rval));
|
2003-03-14 00:23:18 +03:00
|
|
|
}
|
|
|
|
|
2013-04-19 13:25:33 +04:00
|
|
|
NS_IMETHODIMP
|
2013-04-23 01:33:55 +04:00
|
|
|
nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength, const JS::Value& aBuffer, JSContext* cx)
|
2013-04-19 13:25:33 +04:00
|
|
|
{
|
2013-04-23 01:33:55 +04:00
|
|
|
if (!aBuffer.isObject()) {
|
2013-04-19 13:25:33 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-04-23 01:33:55 +04:00
|
|
|
JS::RootedObject buffer(cx, &aBuffer.toObject());
|
|
|
|
if (!JS_IsArrayBufferObject(buffer) ||
|
|
|
|
JS_GetArrayBufferByteLength(buffer) < aLength) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
uint8_t* data = JS_GetArrayBufferData(&aBuffer.toObject());
|
2013-04-19 13:25:33 +04:00
|
|
|
if (!data) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t bytesRead;
|
|
|
|
nsresult rv = Read(reinterpret_cast<char*>(data), aLength, &bytesRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (bytesRead != aLength) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-16 22:12:41 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsBinaryInputStream::ReadObject(bool aIsStrongRef, nsISupports* *aObject)
|
2001-07-31 23:05:34 +04:00
|
|
|
{
|
2007-08-07 06:00:55 +04:00
|
|
|
nsCID cid;
|
|
|
|
nsIID iid;
|
|
|
|
nsresult rv = ReadID(&cid);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ReadID(&iid);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-06-09 23:10:42 +04:00
|
|
|
// HACK: Intercept old (pre-gecko6) nsIURI IID, and replace with
|
|
|
|
// the updated IID, so that we're QI'ing to an actual interface.
|
|
|
|
// (As soon as we drop support for upgrading from pre-gecko6, we can
|
|
|
|
// remove this chunk.)
|
|
|
|
static const nsIID oldURIiid =
|
|
|
|
{ 0x7a22cc0, 0xce5, 0x11d3,
|
|
|
|
{ 0x93, 0x31, 0x0, 0x10, 0x4b, 0xa0, 0xfd, 0x40 }};
|
|
|
|
|
2011-07-27 09:14:08 +04:00
|
|
|
// hackaround for bug 670542
|
|
|
|
static const nsIID oldURIiid2 =
|
|
|
|
{ 0xd6d04c36, 0x0fa4, 0x4db3,
|
|
|
|
{ 0xbe, 0x05, 0x4a, 0x18, 0x39, 0x71, 0x03, 0xe2 }};
|
|
|
|
|
2011-08-26 10:24:43 +04:00
|
|
|
// hackaround for bug 682031
|
|
|
|
static const nsIID oldURIiid3 =
|
|
|
|
{ 0x12120b20, 0x0929, 0x40e9,
|
|
|
|
{ 0x88, 0xcf, 0x6e, 0x08, 0x76, 0x6e, 0x8b, 0x23 }};
|
|
|
|
|
2011-07-27 09:14:08 +04:00
|
|
|
if (iid.Equals(oldURIiid) ||
|
2011-08-26 10:24:43 +04:00
|
|
|
iid.Equals(oldURIiid2) ||
|
|
|
|
iid.Equals(oldURIiid3)) {
|
2011-06-09 23:10:42 +04:00
|
|
|
const nsIID newURIiid = NS_IURI_IID;
|
|
|
|
iid = newURIiid;
|
|
|
|
}
|
|
|
|
// END HACK
|
|
|
|
|
2007-08-07 06:00:55 +04:00
|
|
|
nsCOMPtr<nsISupports> object = do_CreateInstance(cid, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISerializable> serializable = do_QueryInterface(object);
|
|
|
|
NS_ENSURE_TRUE(serializable, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
rv = serializable->Read(this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return object->QueryInterface(iid, reinterpret_cast<void**>(aObject));
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::ReadID(nsID *aResult)
|
1999-11-16 22:12:41 +03:00
|
|
|
{
|
2007-08-07 06:00:55 +04:00
|
|
|
nsresult rv = Read32(&aResult->m0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = Read16(&aResult->m1);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = Read16(&aResult->m2);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; ++i) {
|
|
|
|
rv = Read8(&aResult->m3[i]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-16 22:12:41 +03:00
|
|
|
}
|
|
|
|
|
2001-07-31 23:05:34 +04:00
|
|
|
NS_IMETHODIMP_(char*)
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::GetBuffer(uint32_t aLength, uint32_t aAlignMask)
|
2001-07-31 23:05:34 +04:00
|
|
|
{
|
|
|
|
if (mBufferAccess)
|
|
|
|
return mBufferAccess->GetBuffer(aLength, aAlignMask);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2001-07-31 23:05:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBinaryInputStream::PutBuffer(char* aBuffer, uint32_t aLength)
|
2001-07-31 23:05:34 +04:00
|
|
|
{
|
|
|
|
if (mBufferAccess)
|
|
|
|
mBufferAccess->PutBuffer(aBuffer, aLength);
|
|
|
|
}
|