nsBinaryStream.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
893N/A/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3909N/A * ***** BEGIN LICENSE BLOCK *****
893N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
893N/A *
893N/A * The contents of this file are subject to the Mozilla Public License Version
893N/A * 1.1 (the "License"); you may not use this file except in compliance with
2362N/A * the License. You may obtain a copy of the License at
893N/A * http://www.mozilla.org/MPL/
2362N/A *
893N/A * Software distributed under the License is distributed on an "AS IS" basis,
893N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
893N/A * for the specific language governing rights and limitations under the
893N/A * License.
893N/A *
893N/A * The Original Code is Mozilla Communicator client code, released
893N/A * March 31, 1998.
893N/A *
893N/A * The Initial Developer of the Original Code is
893N/A * Netscape Communications Corporation.
893N/A * Portions created by the Initial Developer are Copyright (C) 1998-1999
2362N/A * the Initial Developer. All Rights Reserved.
2362N/A *
2362N/A * Contributor(s):
893N/A *
893N/A * Alternatively, the contents of this file may be used under the terms of
893N/A * either of the GNU General Public License Version 2 or later (the "GPL"),
893N/A * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
893N/A * in which case the provisions of the GPL or the LGPL are applicable instead
893N/A * of those above. If you wish to allow use of your version of this file only
893N/A * under the terms of either the GPL or the LGPL, and not to allow others to
893N/A * use your version of this file under the terms of the MPL, indicate your
893N/A * decision by deleting the provisions above and replace them with the notice
893N/A * and other provisions required by the GPL or the LGPL. If you do not delete
893N/A * the provisions above, a recipient may use your version of this file under
893N/A * the terms of any one of the MPL, the GPL or the LGPL.
893N/A *
893N/A * ***** END LICENSE BLOCK ***** */
893N/A
893N/A/**
893N/A * This file contains implementations of the nsIBinaryInputStream and
893N/A * nsIBinaryOutputStream interfaces. Together, these interfaces allows reading
893N/A * and writing of primitive data types (integers, floating-point values,
893N/A * booleans, etc.) to a stream in a binary, untagged, fixed-endianness format.
893N/A * This might be used, for example, to implement network protocols or to
893N/A * produce architecture-neutral binary disk files, i.e. ones that can be read
893N/A * and written by both big-endian and little-endian platforms. Output is
893N/A * written in big-endian order (high-order byte first), as this is traditional
893N/A * network order.
893N/A *
893N/A * @See nsIBinaryInputStream
893N/A * @See nsIBinaryOutputStream
893N/A */
893N/A#include <string.h>
893N/A#include "nsBinaryStream.h"
893N/A#include "nsCRT.h"
893N/A#include "nsIStreamBufferAccess.h"
893N/A#include "nsMemory.h"
893N/A#include "prlong.h"
893N/A#include "nsGenericFactory.h"
893N/A
893N/ANS_IMPL_ISUPPORTS3(nsBinaryOutputStream, nsIObjectOutputStream, nsIBinaryOutputStream, nsIOutputStream)
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::Flush() { return mOutputStream->Flush(); }
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::Close() { return mOutputStream->Close(); }
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::Write(const char *aBuf, PRUint32 aCount, PRUint32 *aActualBytes)
893N/A{
893N/A return mOutputStream->Write(aBuf, aCount, aActualBytes);
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::WriteFrom(nsIInputStream *inStr, PRUint32 count, PRUint32 *_retval)
893N/A{
893N/A NS_NOTREACHED("WriteFrom");
893N/A return NS_ERROR_NOT_IMPLEMENTED;
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval)
893N/A{
893N/A NS_NOTREACHED("WriteSegments");
893N/A return NS_ERROR_NOT_IMPLEMENTED;
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::IsNonBlocking(PRBool *aNonBlocking)
893N/A{
893N/A return mOutputStream->IsNonBlocking(aNonBlocking);
893N/A}
893N/A
893N/Ansresult
893N/AnsBinaryOutputStream::WriteFully(const char *aBuf, PRUint32 aCount)
893N/A{
893N/A nsresult rv;
893N/A PRUint32 bytesWritten;
893N/A
893N/A rv = mOutputStream->Write(aBuf, aCount, &bytesWritten);
893N/A if (NS_FAILED(rv)) return rv;
893N/A if (bytesWritten != aCount)
893N/A return NS_ERROR_FAILURE;
893N/A return NS_OK;
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::SetOutputStream(nsIOutputStream *aOutputStream)
893N/A{
893N/A NS_ENSURE_ARG_POINTER(aOutputStream);
893N/A mOutputStream = aOutputStream;
893N/A mBufferAccess = do_QueryInterface(aOutputStream);
893N/A return NS_OK;
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::WriteBoolean(PRBool aBoolean)
893N/A{
893N/A return Write8(aBoolean);
893N/A}
893N/A
5022N/ANS_IMETHODIMP
5022N/AnsBinaryOutputStream::Write8(PRUint8 aByte)
5022N/A{
5022N/A return WriteFully((const char*)&aByte, sizeof aByte);
5022N/A}
5022N/A
5022N/ANS_IMETHODIMP
5022N/AnsBinaryOutputStream::Write16(PRUint16 a16)
893N/A{
893N/A a16 = NS_SWAP16(a16);
893N/A return WriteFully((const char*)&a16, sizeof a16);
5022N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::Write32(PRUint32 a32)
893N/A{
893N/A a32 = NS_SWAP32(a32);
893N/A return WriteFully((const char*)&a32, sizeof a32);
893N/A}
3471N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::Write64(PRUint64 a64)
893N/A{
893N/A nsresult rv;
893N/A PRUint32 bytesWritten;
893N/A
893N/A a64 = NS_SWAP64(a64);
893N/A rv = Write(NS_REINTERPRET_CAST(char*, &a64), sizeof a64, &bytesWritten);
893N/A if (NS_FAILED(rv)) return rv;
893N/A if (bytesWritten != sizeof a64)
893N/A return NS_ERROR_FAILURE;
893N/A return rv;
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::WriteFloat(float aFloat)
893N/A{
893N/A NS_ASSERTION(sizeof(float) == sizeof (PRUint32),
893N/A "False assumption about sizeof(float)");
893N/A return Write32(*NS_REINTERPRET_CAST(PRUint32*, &aFloat));
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::WriteDouble(double aDouble)
893N/A{
893N/A NS_ASSERTION(sizeof(double) == sizeof(PRUint64),
893N/A "False assumption about sizeof(double)");
893N/A return Write64(*NS_REINTERPRET_CAST(PRUint64*, &aDouble));
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::WriteStringZ(const char *aString)
893N/A{
893N/A PRUint32 length;
893N/A nsresult rv;
893N/A
893N/A length = strlen(aString);
893N/A rv = Write32(length);
893N/A if (NS_FAILED(rv)) return rv;
893N/A return WriteFully(aString, length);
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::WriteWStringZ(const PRUnichar* aString)
893N/A{
893N/A PRUint32 length, byteCount;
893N/A nsresult rv;
893N/A
893N/A length = nsCRT::strlen(aString);
893N/A rv = Write32(length);
893N/A if (NS_FAILED(rv)) return rv;
893N/A
893N/A if (length == 0)
893N/A return NS_OK;
893N/A byteCount = length * sizeof(PRUnichar);
893N/A
893N/A#ifdef IS_BIG_ENDIAN
893N/A rv = WriteBytes(NS_REINTERPRET_CAST(const char*, aString), byteCount);
893N/A#else
893N/A // XXX use WriteSegments here to avoid copy!
893N/A PRUnichar *copy, temp[64];
893N/A if (length <= 64) {
893N/A copy = temp;
893N/A } else {
893N/A copy = NS_REINTERPRET_CAST(PRUnichar*, nsMemory::Alloc(byteCount));
893N/A if (!copy)
893N/A return NS_ERROR_OUT_OF_MEMORY;
893N/A }
893N/A NS_ASSERTION((PRUptrdiff(aString) & 0x1) == 0, "aString not properly aligned");
893N/A for (PRUint32 i = 0; i < length; i++)
893N/A copy[i] = NS_SWAP16(aString[i]);
893N/A rv = WriteBytes(NS_REINTERPRET_CAST(const char*, copy), byteCount);
893N/A if (copy != temp)
893N/A nsMemory::Free(copy);
893N/A#endif
893N/A
893N/A return rv;
893N/A}
893N/A
893N/ANS_IMETHODIMP
893N/AnsBinaryOutputStream::WriteUtf8Z(const PRUnichar* aString)
893N/A{
893N/A NS_NOTREACHED("WriteUtf8Z");
893N/A return NS_ERROR_NOT_IMPLEMENTED;
893N/A}
893N/A
893N/ANS_IMETHODIMP
nsBinaryOutputStream::WriteBytes(const char *aString, PRUint32 aLength)
{
nsresult rv;
PRUint32 bytesWritten;
rv = Write(aString, aLength, &bytesWritten);
if (NS_FAILED(rv)) return rv;
if (bytesWritten != aLength)
return NS_ERROR_FAILURE;
return rv;
}
NS_IMETHODIMP
nsBinaryOutputStream::WriteByteArray(PRUint8 *aBytes, PRUint32 aLength)
{
return WriteBytes(NS_REINTERPRET_CAST(char *, aBytes), aLength);
}
NS_IMETHODIMP
nsBinaryOutputStream::WriteObject(nsISupports* aObject, PRBool aIsStrongRef)
{
NS_NOTREACHED("WriteObject");
return NS_ERROR_NOT_IMPLEMENTED;
}
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);
}
NS_IMPL_ISUPPORTS3(nsBinaryInputStream, nsIObjectInputStream, nsIBinaryInputStream, nsIInputStream)
NS_IMETHODIMP
nsBinaryInputStream::Available(PRUint32* aResult)
{
return mInputStream->Available(aResult);
}
NS_IMETHODIMP
nsBinaryInputStream::Read(char* aBuffer, PRUint32 aCount, PRUint32 *aNumRead)
{
return mInputStream->Read(aBuffer, aCount, aNumRead);
}
// 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);
}
NS_IMETHODIMP
nsBinaryInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval)
{
ReadSegmentsClosure thunkClosure = { this, closure, writer };
return mInputStream->ReadSegments(ReadSegmentForwardingThunk, &thunkClosure, count, _retval);
}
NS_IMETHODIMP
nsBinaryInputStream::IsNonBlocking(PRBool *aNonBlocking)
{
return mInputStream->IsNonBlocking(aNonBlocking);
}
NS_IMETHODIMP
nsBinaryInputStream::Close() { return mInputStream->Close(); }
NS_IMETHODIMP
nsBinaryInputStream::SetInputStream(nsIInputStream *aInputStream)
{
NS_ENSURE_ARG_POINTER(aInputStream);
mInputStream = aInputStream;
mBufferAccess = do_QueryInterface(aInputStream);
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;
rv = Read(NS_REINTERPRET_CAST(char*, aByte), sizeof(*aByte), &bytesRead);
if (NS_FAILED(rv)) return rv;
if (bytesRead != 1)
return NS_ERROR_FAILURE;
return rv;
}
NS_IMETHODIMP
nsBinaryInputStream::Read16(PRUint16* a16)
{
nsresult rv;
PRUint32 bytesRead;
rv = Read(NS_REINTERPRET_CAST(char*, a16), sizeof *a16, &bytesRead);
if (NS_FAILED(rv)) return rv;
if (bytesRead != sizeof *a16)
return NS_ERROR_FAILURE;
*a16 = NS_SWAP16(*a16);
return rv;
}
NS_IMETHODIMP
nsBinaryInputStream::Read32(PRUint32* a32)
{
nsresult rv;
PRUint32 bytesRead;
rv = Read(NS_REINTERPRET_CAST(char*, a32), sizeof *a32, &bytesRead);
if (NS_FAILED(rv)) return rv;
if (bytesRead != sizeof *a32)
return NS_ERROR_FAILURE;
*a32 = NS_SWAP32(*a32);
return rv;
}
NS_IMETHODIMP
nsBinaryInputStream::Read64(PRUint64* a64)
{
nsresult rv;
PRUint32 bytesRead;
rv = Read(NS_REINTERPRET_CAST(char*, a64), sizeof *a64, &bytesRead);
if (NS_FAILED(rv)) return rv;
if (bytesRead != sizeof *a64)
return NS_ERROR_FAILURE;
*a64 = NS_SWAP64(*a64);
return rv;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadFloat(float* aFloat)
{
NS_ASSERTION(sizeof(float) == sizeof (PRUint32),
"False assumption about sizeof(float)");
return Read32(NS_REINTERPRET_CAST(PRUint32*, aFloat));
}
NS_IMETHODIMP
nsBinaryInputStream::ReadDouble(double* aDouble)
{
NS_ASSERTION(sizeof(double) == sizeof(PRUint64),
"False assumption about sizeof(double)");
return Read64(NS_REINTERPRET_CAST(PRUint64*, aDouble));
}
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;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadCString(nsACString& aString)
{
nsresult rv;
PRUint32 length, bytesRead;
rv = Read32(&length);
if (NS_FAILED(rv)) return rv;
aString.Truncate();
rv = ReadSegments(WriteSegmentToCString, &aString, length, &bytesRead);
if (NS_FAILED(rv)) return rv;
if (bytesRead != length)
return NS_ERROR_FAILURE;
return NS_OK;
}
// sometimes, WriteSegmentToString will be handed an odd-number of
// bytes, which means we only have half of the last PRUnichar
struct WriteStringClosure {
PRUnichar *mWriteCursor;
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
// 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)
{
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);
PRUnichar *cursor = closure->mWriteCursor;
// 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 };
*cursor = *(PRUnichar*)bytes;
// Now the little endianness dance
#ifdef IS_LITTLE_ENDIAN
*cursor = (PRUnichar) NS_SWAP16(*cursor);
#endif
++cursor;
// 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;
}
// this array is possibly unaligned... be careful how we access it!
const PRUnichar *unicodeSegment =
NS_REINTERPRET_CAST(const PRUnichar*, aFromSegment);
// calculate number of full characters in segment (aCount could be odd!)
PRUint32 segmentLength = aCount / sizeof(PRUnichar);
// copy all data into our aligned buffer. byte swap if necessary.
memcpy(cursor, unicodeSegment, segmentLength * sizeof(PRUnichar));
PRUnichar *end = cursor + segmentLength;
#ifdef IS_LITTLE_ENDIAN
for (; cursor < end; ++cursor)
*cursor = (PRUnichar) NS_SWAP16(*cursor);
#endif
closure->mWriteCursor = end;
// 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;
}
return NS_OK;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadString(nsAString& aString)
{
nsresult rv;
PRUint32 length, bytesRead;
rv = Read32(&length);
if (NS_FAILED(rv)) return rv;
// pre-allocate output buffer, and get direct access to buffer...
aString.SetLength(length);
nsAString::iterator start;
aString.BeginWriting(start);
WriteStringClosure closure;
closure.mWriteCursor = start.get();
closure.mHasCarryoverByte = PR_FALSE;
rv = ReadSegments(WriteSegmentToString, &closure,
length*sizeof(PRUnichar), &bytesRead);
if (NS_FAILED(rv)) return rv;
NS_ASSERTION(!closure.mHasCarryoverByte, "some strange stream corruption!");
if (bytesRead != length*sizeof(PRUnichar))
return NS_ERROR_FAILURE;
return NS_OK;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadBytes(PRUint32 aLength, char* *_rval)
{
nsresult rv;
PRUint32 bytesRead;
char* s;
s = NS_REINTERPRET_CAST(char*, nsMemory::Alloc(aLength));
if (!s)
return NS_ERROR_OUT_OF_MEMORY;
rv = Read(s, aLength, &bytesRead);
if (NS_FAILED(rv)) {
nsMemory::Free(s);
return rv;
}
if (bytesRead != aLength) {
nsMemory::Free(s);
return NS_ERROR_FAILURE;
}
*_rval = s;
return NS_OK;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadByteArray(PRUint32 aLength, PRUint8* *_rval)
{
return ReadBytes(aLength, NS_REINTERPRET_CAST(char **, _rval));
}
NS_IMETHODIMP
nsBinaryInputStream::ReadObject(PRBool aIsStrongRef, nsISupports* *aObject)
{
NS_NOTREACHED("ReadObject");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadID(nsID *aResult)
{
NS_NOTREACHED("ReadID");
return NS_ERROR_NOT_IMPLEMENTED;
}
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);
}