1999-11-16 19:12:41 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
2001-07-31 19:05:34 +00:00
|
|
|
*
|
1999-11-16 19:12:41 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
2001-07-31 19:05:34 +00:00
|
|
|
*
|
1999-11-16 19:12:41 +00:00
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
2001-07-31 19:05:34 +00:00
|
|
|
*
|
1999-11-16 19:12:41 +00:00
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 19:05:34 +00:00
|
|
|
#include <string.h>
|
1999-11-16 19:12:41 +00:00
|
|
|
#include "nsBinaryStream.h"
|
2001-07-31 19:05:34 +00:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsIStreamBufferAccess.h"
|
2000-06-03 09:46:12 +00:00
|
|
|
#include "nsMemory.h"
|
2001-07-31 19:05:34 +00:00
|
|
|
#include "prlong.h"
|
2003-03-13 21:23:18 +00:00
|
|
|
#include "nsGenericFactory.h"
|
1999-11-16 19:12:41 +00:00
|
|
|
|
2000-11-17 03:41:41 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsBinaryOutputStream, nsIBinaryOutputStream)
|
1999-11-16 19:12:41 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::Flush() { return mOutputStream->Flush(); }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::Close() { return mOutputStream->Close(); }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::Write(const char *aBuf, PRUint32 aCount, PRUint32 *aActualBytes)
|
|
|
|
{
|
|
|
|
return mOutputStream->Write(aBuf, aCount, aActualBytes);
|
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_IMETHODIMP
|
2000-08-22 07:03:33 +00:00
|
|
|
nsBinaryOutputStream::WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteFrom");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_IMETHODIMP
|
2000-08-22 07:03:33 +00:00
|
|
|
nsBinaryOutputStream::WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteSegments");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-12 00:59:06 +00:00
|
|
|
nsBinaryOutputStream::IsNonBlocking(PRBool *aNonBlocking)
|
2000-08-22 07:03:33 +00:00
|
|
|
{
|
2002-03-12 00:59:06 +00:00
|
|
|
return mOutputStream->IsNonBlocking(aNonBlocking);
|
2000-08-22 07:03:33 +00:00
|
|
|
}
|
|
|
|
|
1999-11-21 08:01:08 +00:00
|
|
|
nsresult
|
|
|
|
nsBinaryOutputStream::WriteFully(const char *aBuf, PRUint32 aCount)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2001-07-31 19:05:34 +00:00
|
|
|
PRUint32 bytesWritten;
|
|
|
|
|
|
|
|
rv = mOutputStream->Write(aBuf, aCount, &bytesWritten);
|
1999-11-21 08:01:08 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-07-31 19:05:34 +00:00
|
|
|
if (bytesWritten != aCount)
|
1999-11-21 08:01:08 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-16 19:12:41 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::SetOutputStream(nsIOutputStream *aOutputStream)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aOutputStream);
|
|
|
|
mOutputStream = aOutputStream;
|
2003-03-13 21:23:18 +00:00
|
|
|
mBufferAccess = do_QueryInterface(aOutputStream);
|
1999-11-16 19:12:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteBoolean(PRBool aBoolean)
|
|
|
|
{
|
|
|
|
return Write8(aBoolean);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::Write8(PRUint8 aByte)
|
|
|
|
{
|
1999-11-21 08:01:08 +00:00
|
|
|
return WriteFully((const char*)&aByte, sizeof aByte);
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::Write16(PRUint16 a16)
|
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
a16 = NS_SWAP16(a16);
|
1999-11-21 08:01:08 +00:00
|
|
|
return WriteFully((const char*)&a16, sizeof a16);
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::Write32(PRUint32 a32)
|
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
a32 = NS_SWAP32(a32);
|
1999-11-21 08:01:08 +00:00
|
|
|
return WriteFully((const char*)&a32, sizeof a32);
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::Write64(PRUint64 a64)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2001-07-31 19:05:34 +00:00
|
|
|
PRUint32 bytesWritten;
|
1999-11-16 19:12:41 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
a64 = NS_SWAP64(a64);
|
|
|
|
rv = Write(NS_REINTERPRET_CAST(char*, &a64), sizeof a64, &bytesWritten);
|
1999-11-16 19:12:41 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-07-31 19:05:34 +00:00
|
|
|
if (bytesWritten != sizeof a64)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteFloat(float aFloat)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(sizeof(float) == sizeof (PRUint32),
|
|
|
|
"False assumption about sizeof(float)");
|
|
|
|
return Write32(*NS_REINTERPRET_CAST(PRUint32*, &aFloat));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteDouble(double aDouble)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(sizeof(double) == sizeof(PRUint64),
|
|
|
|
"False assumption about sizeof(double)");
|
2001-07-31 19:05:34 +00:00
|
|
|
return Write64(*NS_REINTERPRET_CAST(PRUint64*, &aDouble));
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteStringZ(const char *aString)
|
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
PRUint32 length;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
length = strlen(aString);
|
|
|
|
rv = Write32(length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return WriteFully(aString, length);
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteWStringZ(const PRUnichar* aString)
|
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
PRUint32 length, byteCount;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
length = nsCRT::strlen(aString);
|
|
|
|
rv = Write32(length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return NS_OK;
|
|
|
|
byteCount = length * sizeof(PRUnichar);
|
|
|
|
|
|
|
|
#ifdef IS_BIG_ENDIAN
|
|
|
|
rv = WriteBytes(NS_REINTERPRET_CAST(const char*, aString), byteCount);
|
|
|
|
#else
|
|
|
|
PRUnichar *copy, temp[64];
|
|
|
|
if (length <= 64) {
|
|
|
|
copy = temp;
|
|
|
|
} else {
|
|
|
|
copy = NS_REINTERPRET_CAST(PRUnichar*, nsMemory::Alloc(byteCount));
|
|
|
|
if (!copy)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
for (PRUint32 i = 0; i < length; i++)
|
|
|
|
copy[i] = NS_SWAP16(aString[i]);
|
|
|
|
rv = WriteBytes(NS_REINTERPRET_CAST(const char*, copy), byteCount);
|
|
|
|
if (copy != temp)
|
|
|
|
nsMemory::Free(copy);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return rv;
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteUtf8Z(const PRUnichar* aString)
|
|
|
|
{
|
2000-08-22 07:03:33 +00:00
|
|
|
NS_NOTREACHED("WriteUtf8Z");
|
1999-11-16 19:12:41 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteBytes(const char *aString, PRUint32 aLength)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRUint32 bytesWritten;
|
|
|
|
|
|
|
|
rv = Write(aString, aLength, &bytesWritten);
|
2001-07-31 19:05:34 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-16 19:12:41 +00:00
|
|
|
if (bytesWritten != aLength)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-03-13 21:23:18 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteByteArray(PRUint8 *aBytes, PRUint32 aLength)
|
|
|
|
{
|
|
|
|
return WriteBytes(NS_REINTERPRET_CAST(char *, aBytes), aLength);
|
|
|
|
}
|
|
|
|
|
1999-11-16 19:12:41 +00:00
|
|
|
NS_IMETHODIMP
|
2001-07-31 19:05:34 +00:00
|
|
|
nsBinaryOutputStream::WriteObject(nsISupports* aObject, PRBool aIsStrongRef)
|
1999-11-16 19:12:41 +00:00
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_NOTREACHED("WriteObject");
|
1999-11-16 19:12:41 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteSingleRefObject(nsISupports* aObject)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteSingleRefObject");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteCompoundObject(nsISupports* aObject,
|
|
|
|
const nsIID& aIID,
|
|
|
|
PRBool aIsStrongRef)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteCompoundObject");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryOutputStream::WriteID(const nsIID& aIID)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("WriteID");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(char*)
|
|
|
|
nsBinaryOutputStream::GetBuffer(PRUint32 aLength, PRUint32 aAlignMask)
|
|
|
|
{
|
|
|
|
if (mBufferAccess)
|
|
|
|
return mBufferAccess->GetBuffer(aLength, aAlignMask);
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsBinaryOutputStream::PutBuffer(char* aBuffer, PRUint32 aLength)
|
|
|
|
{
|
|
|
|
if (mBufferAccess)
|
|
|
|
mBufferAccess->PutBuffer(aBuffer, aLength);
|
|
|
|
}
|
|
|
|
|
2000-11-17 03:41:41 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsBinaryInputStream, nsIBinaryInputStream)
|
1999-11-16 19:12:41 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-07-31 19:05:34 +00:00
|
|
|
nsBinaryInputStream::Available(PRUint32* aResult)
|
|
|
|
{
|
|
|
|
return mInputStream->Available(aResult);
|
|
|
|
}
|
1999-11-16 19:12:41 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::Read(char* aBuffer, PRUint32 aCount, PRUint32 *aNumRead)
|
|
|
|
{
|
|
|
|
return mInputStream->Read(aBuffer, aCount, aNumRead);
|
|
|
|
}
|
|
|
|
|
2002-11-14 18:16:31 +00: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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// the thunking function
|
|
|
|
static NS_METHOD
|
|
|
|
ReadSegmentForwardingThunk(nsIInputStream* aStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char* aFromSegment,
|
|
|
|
PRUint32 aToOffset,
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32 *aWriteCount)
|
|
|
|
{
|
|
|
|
ReadSegmentsClosure* thunkClosure =
|
|
|
|
NS_REINTERPRET_CAST(ReadSegmentsClosure*, aClosure);
|
|
|
|
|
|
|
|
return thunkClosure->mRealWriter(thunkClosure->mRealInputStream,
|
|
|
|
thunkClosure->mRealClosure,
|
|
|
|
aFromSegment, aToOffset,
|
|
|
|
aCount, aWriteCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_IMETHODIMP
|
2000-08-22 07:03:33 +00:00
|
|
|
nsBinaryInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval)
|
|
|
|
{
|
2002-11-14 18:16:31 +00:00
|
|
|
ReadSegmentsClosure thunkClosure = { this, closure, writer };
|
|
|
|
|
|
|
|
return mInputStream->ReadSegments(ReadSegmentForwardingThunk, &thunkClosure, count, _retval);
|
2000-08-22 07:03:33 +00:00
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-12 00:59:06 +00:00
|
|
|
nsBinaryInputStream::IsNonBlocking(PRBool *aNonBlocking)
|
2000-08-22 07:03:33 +00:00
|
|
|
{
|
2002-03-12 00:59:06 +00:00
|
|
|
return mInputStream->IsNonBlocking(aNonBlocking);
|
2000-08-22 07:03:33 +00:00
|
|
|
}
|
|
|
|
|
1999-11-16 19:12:41 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::Close() { return mInputStream->Close(); }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::SetInputStream(nsIInputStream *aInputStream)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInputStream);
|
|
|
|
mInputStream = aInputStream;
|
2003-03-13 21:23:18 +00:00
|
|
|
mBufferAccess = do_QueryInterface(aInputStream);
|
1999-11-16 19:12:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::ReadBoolean(PRBool* aBoolean)
|
|
|
|
{
|
|
|
|
PRUint8 byteResult;
|
|
|
|
nsresult rv = Read8(&byteResult);
|
|
|
|
*aBoolean = byteResult;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::Read8(PRUint8* aByte)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRUint32 bytesRead;
|
2001-07-31 19:05:34 +00:00
|
|
|
|
|
|
|
rv = Read(NS_REINTERPRET_CAST(char*, aByte), sizeof(*aByte), &bytesRead);
|
1999-11-21 08:01:08 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-07-31 19:05:34 +00:00
|
|
|
if (bytesRead != 1)
|
1999-11-16 19:12:41 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::Read16(PRUint16* a16)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRUint32 bytesRead;
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
rv = Read(NS_REINTERPRET_CAST(char*, a16), sizeof *a16, &bytesRead);
|
1999-11-21 08:01:08 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-16 19:12:41 +00:00
|
|
|
if (bytesRead != sizeof *a16)
|
|
|
|
return NS_ERROR_FAILURE;
|
2001-07-31 19:05:34 +00:00
|
|
|
*a16 = NS_SWAP16(*a16);
|
1999-11-16 19:12:41 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::Read32(PRUint32* a32)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRUint32 bytesRead;
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
rv = Read(NS_REINTERPRET_CAST(char*, a32), sizeof *a32, &bytesRead);
|
1999-11-21 08:01:08 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-16 19:12:41 +00:00
|
|
|
if (bytesRead != sizeof *a32)
|
|
|
|
return NS_ERROR_FAILURE;
|
2001-07-31 19:05:34 +00:00
|
|
|
*a32 = NS_SWAP32(*a32);
|
1999-11-16 19:12:41 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::Read64(PRUint64* a64)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2001-07-31 19:05:34 +00:00
|
|
|
PRUint32 bytesRead;
|
1999-11-16 19:12:41 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
rv = Read(NS_REINTERPRET_CAST(char*, a64), sizeof *a64, &bytesRead);
|
1999-11-16 19:12:41 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-10-31 08:29:25 +00:00
|
|
|
if (bytesRead != sizeof *a64)
|
2001-07-31 19:05:34 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
*a64 = NS_SWAP64(*a64);
|
|
|
|
return rv;
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::ReadFloat(float* aFloat)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(sizeof(float) == sizeof (PRUint32),
|
2001-07-31 19:05:34 +00:00
|
|
|
"False assumption about sizeof(float)");
|
1999-11-16 19:12:41 +00:00
|
|
|
return Read32(NS_REINTERPRET_CAST(PRUint32*, aFloat));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::ReadDouble(double* aDouble)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(sizeof(double) == sizeof(PRUint64),
|
2001-07-31 19:05:34 +00:00
|
|
|
"False assumption about sizeof(double)");
|
1999-11-16 19:12:41 +00:00
|
|
|
return Read64(NS_REINTERPRET_CAST(PRUint64*, aDouble));
|
|
|
|
}
|
|
|
|
|
2002-11-14 18:16:31 +00:00
|
|
|
static NS_METHOD
|
|
|
|
WriteSegmentToCString(nsIInputStream* aStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char* aFromSegment,
|
|
|
|
PRUint32 aToOffset,
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32 *aWriteCount)
|
|
|
|
{
|
|
|
|
nsACString* outString = NS_STATIC_CAST(nsACString*,aClosure);
|
|
|
|
|
|
|
|
outString->Append(aFromSegment, aCount);
|
|
|
|
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-16 19:12:41 +00:00
|
|
|
NS_IMETHODIMP
|
2002-11-14 18:16:31 +00:00
|
|
|
nsBinaryInputStream::ReadCString(nsACString& aString)
|
1999-11-16 19:12:41 +00:00
|
|
|
{
|
1999-11-21 08:01:08 +00:00
|
|
|
nsresult rv;
|
2001-07-31 19:05:34 +00:00
|
|
|
PRUint32 length, bytesRead;
|
1999-11-21 08:01:08 +00:00
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
rv = Read32(&length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-11-14 18:16:31 +00:00
|
|
|
aString.Truncate();
|
|
|
|
rv = ReadSegments(WriteSegmentToCString, &aString, length, &bytesRead);
|
2001-07-31 19:05:34 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-11-14 18:16:31 +00:00
|
|
|
|
|
|
|
if (bytesRead != length)
|
2001-07-31 19:05:34 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-11-21 08:01:08 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
2002-11-19 02:57:30 +00:00
|
|
|
|
|
|
|
// sometimes, WriteSegmentToString will be handed an odd-number of
|
|
|
|
// bytes, which means we only have half of the last PRUnichar
|
|
|
|
struct WriteStringClosure {
|
|
|
|
nsAString* mString;
|
|
|
|
PRPackedBool mHasCarryoverByte;
|
|
|
|
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 18:16:31 +00:00
|
|
|
// same version of the above, but with correct casting and endian swapping
|
|
|
|
static NS_METHOD
|
|
|
|
WriteSegmentToString(nsIInputStream* aStream,
|
|
|
|
void *aClosure,
|
|
|
|
const char* aFromSegment,
|
|
|
|
PRUint32 aToOffset,
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32 *aWriteCount)
|
|
|
|
{
|
2002-11-19 02:57:30 +00: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!");
|
|
|
|
|
|
|
|
WriteStringClosure* closure = NS_STATIC_CAST(WriteStringClosure*,aClosure);
|
|
|
|
nsAString* outString = closure->mString;
|
|
|
|
|
|
|
|
// 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 };
|
|
|
|
PRUnichar unichar = *(PRUnichar*)bytes;
|
|
|
|
// Now the little endianness dance
|
|
|
|
#ifdef IS_LITTLE_ENDIAN
|
|
|
|
outString->Append(PRUnichar(NS_SWAP16(unichar)));
|
|
|
|
#else
|
2002-11-19 03:43:01 +00:00
|
|
|
outString->Append(unichar);
|
2002-11-19 02:57:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
closure->mHasCarryoverByte = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2002-11-14 18:16:31 +00:00
|
|
|
const PRUnichar *unicodeSegment =
|
|
|
|
NS_REINTERPRET_CAST(const PRUnichar*, aFromSegment);
|
|
|
|
PRUint32 segmentLength = aCount / sizeof(PRUnichar);
|
|
|
|
|
|
|
|
// this sucks. we have to swap every 2 bytes on some machines
|
|
|
|
#ifdef IS_LITTLE_ENDIAN
|
|
|
|
for (PRUint32 i = 0; i < segmentLength; i++)
|
|
|
|
outString->Append(PRUnichar(NS_SWAP16(unicodeSegment[i])));
|
|
|
|
#else
|
|
|
|
outString->Append(unicodeSegment, segmentLength);
|
|
|
|
#endif
|
|
|
|
|
2002-11-19 02:57:30 +00: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];
|
|
|
|
closure->mHasCarryoverByte = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2002-11-14 18:16:31 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-16 19:12:41 +00:00
|
|
|
NS_IMETHODIMP
|
2002-11-14 18:16:31 +00:00
|
|
|
nsBinaryInputStream::ReadString(nsAString& aString)
|
1999-11-16 19:12:41 +00:00
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
nsresult rv;
|
2002-11-14 18:16:31 +00:00
|
|
|
PRUint32 length, bytesRead;
|
2001-07-31 19:05:34 +00:00
|
|
|
|
|
|
|
rv = Read32(&length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-11-14 18:16:31 +00:00
|
|
|
aString.Truncate();
|
2002-11-19 02:57:30 +00:00
|
|
|
|
|
|
|
WriteStringClosure closure;
|
|
|
|
closure.mString = &aString;
|
|
|
|
closure.mHasCarryoverByte = PR_FALSE;
|
|
|
|
|
|
|
|
rv = ReadSegments(WriteSegmentToString, &closure,
|
|
|
|
length*sizeof(PRUnichar), &bytesRead);
|
2001-07-31 19:05:34 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-11-19 02:57:30 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(!closure.mHasCarryoverByte, "some strange stream corruption!");
|
2002-11-14 18:16:31 +00:00
|
|
|
|
|
|
|
if (bytesRead != length*sizeof(PRUnichar))
|
2001-07-31 19:05:34 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-03-13 21:23:18 +00:00
|
|
|
nsBinaryInputStream::ReadBytes(PRUint32 aLength, char* *_rval)
|
1999-11-16 19:12:41 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRUint32 bytesRead;
|
|
|
|
char* s;
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
s = NS_REINTERPRET_CAST(char*, nsMemory::Alloc(aLength));
|
1999-11-16 19:12:41 +00:00
|
|
|
if (!s)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = Read(s, aLength, &bytesRead);
|
2003-03-13 21:23:18 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsMemory::Free(s);
|
|
|
|
return rv;
|
|
|
|
}
|
2001-07-31 19:05:34 +00:00
|
|
|
if (bytesRead != aLength) {
|
|
|
|
nsMemory::Free(s);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-11-16 19:12:41 +00:00
|
|
|
|
2003-03-13 21:23:18 +00:00
|
|
|
*_rval = s;
|
1999-11-16 19:12:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-13 21:23:18 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::ReadByteArray(PRUint32 aLength, PRUint8* *_rval)
|
|
|
|
{
|
|
|
|
return ReadBytes(aLength, NS_REINTERPRET_CAST(char **, _rval));
|
|
|
|
}
|
|
|
|
|
1999-11-16 19:12:41 +00:00
|
|
|
NS_IMETHODIMP
|
2001-07-31 19:05:34 +00:00
|
|
|
nsBinaryInputStream::ReadObject(PRBool aIsStrongRef, nsISupports* *aObject)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("ReadObject");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinaryInputStream::ReadID(nsID *aResult)
|
1999-11-16 19:12:41 +00:00
|
|
|
{
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_NOTREACHED("ReadID");
|
1999-11-16 19:12:41 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-07-31 19:05:34 +00:00
|
|
|
NS_IMETHODIMP_(char*)
|
|
|
|
nsBinaryInputStream::GetBuffer(PRUint32 aLength, PRUint32 aAlignMask)
|
|
|
|
{
|
|
|
|
if (mBufferAccess)
|
|
|
|
return mBufferAccess->GetBuffer(aLength, aAlignMask);
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsBinaryInputStream::PutBuffer(char* aBuffer, PRUint32 aLength)
|
|
|
|
{
|
|
|
|
if (mBufferAccess)
|
|
|
|
mBufferAccess->PutBuffer(aBuffer, aLength);
|
|
|
|
}
|
|
|
|
|