/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla 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
*
* 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.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/**
* 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
*/
#include <string.h>
#include "nsBinaryStream.h"
#include "nsCRT.h"
#include "nsIStreamBufferAccess.h"
#include "nsMemory.h"
#include "prlong.h"
#include "nsGenericFactory.h"
NS_IMPL_ISUPPORTS3(nsBinaryOutputStream, nsIObjectOutputStream, nsIBinaryOutputStream, nsIOutputStream)
{
}
{
NS_NOTREACHED("WriteFrom");
return NS_ERROR_NOT_IMPLEMENTED;
}
nsBinaryOutputStream::WriteSegments(nsReadSegmentFun reader, void * closure, PRUint32 count, PRUint32 *_retval)
{
NS_NOTREACHED("WriteSegments");
return NS_ERROR_NOT_IMPLEMENTED;
}
{
}
{
if (bytesWritten != aCount)
return NS_ERROR_FAILURE;
return NS_OK;
}
{
return NS_OK;
}
{
}
{
}
{
}
{
}
{
if (bytesWritten != sizeof a64)
return NS_ERROR_FAILURE;
return rv;
}
{
NS_ASSERTION(sizeof(float) == sizeof (PRUint32),
"False assumption about sizeof(float)");
}
{
NS_ASSERTION(sizeof(double) == sizeof(PRUint64),
"False assumption about sizeof(double)");
}
{
}
{
if (length == 0)
return NS_OK;
#ifdef IS_BIG_ENDIAN
#else
// XXX use WriteSegments here to avoid copy!
if (length <= 64) {
} else {
if (!copy)
return NS_ERROR_OUT_OF_MEMORY;
}
#endif
return rv;
}
{
NS_NOTREACHED("WriteUtf8Z");
return NS_ERROR_NOT_IMPLEMENTED;
}
{
if (bytesWritten != aLength)
return NS_ERROR_FAILURE;
return rv;
}
{
}
{
NS_NOTREACHED("WriteObject");
return NS_ERROR_NOT_IMPLEMENTED;
}
{
NS_NOTREACHED("WriteSingleRefObject");
return NS_ERROR_NOT_IMPLEMENTED;
}
{
NS_NOTREACHED("WriteCompoundObject");
return NS_ERROR_NOT_IMPLEMENTED;
}
{
NS_NOTREACHED("WriteID");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP_(char*)
{
if (mBufferAccess)
return nsnull;
}
NS_IMETHODIMP_(void)
{
if (mBufferAccess)
}
{
}
{
}
// 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 {
void* mRealClosure;
};
// the thunking function
static NS_METHOD
void *aClosure,
const char* aFromSegment,
{
}
nsBinaryInputStream::ReadSegments(nsWriteSegmentFun writer, void * closure, PRUint32 count, PRUint32 *_retval)
{
}
{
}
{
return NS_OK;
}
{
*aBoolean = byteResult;
return rv;
}
{
if (bytesRead != 1)
return NS_ERROR_FAILURE;
return rv;
}
{
return NS_ERROR_FAILURE;
return rv;
}
{
return NS_ERROR_FAILURE;
return rv;
}
{
return NS_ERROR_FAILURE;
return rv;
}
{
NS_ASSERTION(sizeof(float) == sizeof (PRUint32),
"False assumption about sizeof(float)");
}
{
NS_ASSERTION(sizeof(double) == sizeof(PRUint64),
"False assumption about sizeof(double)");
}
static NS_METHOD
void *aClosure,
const char* aFromSegment,
{
*aWriteCount = aCount;
return NS_OK;
}
{
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 {
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
void *aClosure,
const char* aFromSegment,
{
// 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
// Now the little endianness dance
#ifdef IS_LITTLE_ENDIAN
#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;
}
// this array is possibly unaligned... be careful how we access it!
// calculate number of full characters in segment (aCount could be odd!)
// copy all data into our aligned buffer. byte swap if necessary.
#ifdef IS_LITTLE_ENDIAN
#endif
// 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
// we must have had a carryover byte, that we'll need the next
// time around
}
return NS_OK;
}
{
// pre-allocate output buffer, and get direct access to buffer...
return NS_ERROR_FAILURE;
return NS_OK;
}
{
char* s;
if (!s)
return NS_ERROR_OUT_OF_MEMORY;
return rv;
}
return NS_ERROR_FAILURE;
}
*_rval = s;
return NS_OK;
}
{
}
{
NS_NOTREACHED("ReadObject");
return NS_ERROR_NOT_IMPLEMENTED;
}
{
NS_NOTREACHED("ReadID");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP_(char*)
{
if (mBufferAccess)
return nsnull;
}
NS_IMETHODIMP_(void)
{
if (mBufferAccess)
}