/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 "nsISupports.idl"
#include "nsrootidl.idl"

/**
 * An interface for access to a buffering stream implementation's underlying
 * memory buffer.
 *
 * Stream implementations that QueryInterface to nsIStreamBufferAccess must
 * ensure that all buffers are aligned on the most restrictive type size for
 * the current architecture (e.g., sizeof(double) for RISCy CPUs).  malloc(3)
 * satisfies this requirement.
 */
[scriptable, uuid(ac923b72-ac87-4892-ac7a-ca385d429435)]
interface nsIStreamBufferAccess : nsISupports
{
    /**
     * Get access to a contiguous, aligned run of bytes in the stream's buffer.
     * Exactly one successful getBuffer call must occur before a putBuffer call
     * taking the non-null pointer returned by the successful getBuffer.
     *
     * The run of bytes are the next bytes (modulo alignment padding) to read
     * for an input stream, and the next bytes (modulo alignment padding) to
     * store before (eventually) writing buffered data to an output stream.
     * There can be space beyond this run of bytes in the buffer for further
     * accesses before the fill or flush point is reached.
     *
     * @param aLength
     *    Count of contiguous bytes requested at the address A that satisfies
     *    (A & aAlignMask) == 0 in the buffer, starting from the current stream
     *    position, mapped to a buffer address B.  The stream implementation
     *    must pad from B to A by skipping bytes (if input stream) or storing
     *    zero bytes (if output stream).
     *
     * @param aAlignMask
     *    Bit-mask computed by subtracting 1 from the power-of-two alignment
     *    modulus (e.g., 3 or sizeof(uint32_t)-1 for uint32_t alignment).
     *
     * @return
     *    The aligned pointer to aLength bytes in the buffer, or null if the
     *    buffer has no room for aLength bytes starting at the next address A
     *    after the current position that satisfies (A & aAlignMask) == 0.
     */
    [notxpcom,noscript] charPtr getBuffer(in uint32_t aLength, in uint32_t aAlignMask);

    /**
     * Relinquish access to the stream's buffer, filling if at end of an input
     * buffer, flushing if completing an output buffer.  After a getBuffer call
     * that returns non-null, putBuffer must be called.
     *
     * @param aBuffer
     *    A non-null pointer returned by getBuffer on the same stream buffer
     *    access object.
     *
     * @param aLength
     *    The same count of contiguous bytes passed to the getBuffer call that
     *    returned aBuffer.
     */
    [notxpcom,noscript] void putBuffer(in charPtr aBuffer, in uint32_t aLength);

    /**
     * Disable and enable buffering on the stream implementing this interface.
     * DisableBuffering flushes an output stream's buffer, and invalidates an
     * input stream's buffer.
     */
    void disableBuffering();
    void enableBuffering();

    /**
     * The underlying, unbuffered input or output stream.
     */
    readonly attribute nsISupports unbufferedStream;
};

%{C++

// Swap macros, used to convert to/from canonical (big-endian) format
#if defined IS_LITTLE_ENDIAN

# define NS_SWAP16(x) ((((x) & 0xff) << 8) | (((x) >> 8) & 0xff))
# define NS_SWAP32(x) ((NS_SWAP16((x) & 0xffff) << 16) | (NS_SWAP16((x) >> 16)))

// We want to avoid casting to 32-bit types if possible, since that violates
// aliasing rules (a standard compiler may assume that pointers of two types
// do not address overlapping storage).
//
// XXX What if we have a compiler that follows aliasing rules strictly but
// doesn't have a 64-bit int type?
//
// XXXbe shouldn't NSPR's LL_INIT work for non-constant arguments in all cases?

# if defined HAVE_LONG_LONG
#  if PR_BYTES_PER_LONG == 8
#   define ULL_(x)     x ## UL
#  elif defined WIN32 && defined _MSC_VER
#   define ULL_(x)     x ## ui64
#  else
#   define ULL_(x)     x ## ULL
#  endif

#  define NS_SWAP64(x) ((((x)    & ULL_(0xff00000000000000))    >> 56) |      \
                        (((x)    & ULL_(0x00ff000000000000))    >> 40) |      \
                        (((x)    & ULL_(0x0000ff0000000000))    >> 24) |      \
                        (((x)    & ULL_(0x000000ff00000000))    >> 8)  |      \
                        (((x)    & ULL_(0x00000000ff000000))    << 8)  |      \
                        (((x)    & ULL_(0x0000000000ff0000))    << 24) |      \
                        (((x)    & ULL_(0x000000000000ff00))    << 40) |      \
                        (((x) /* & ULL_(0x00000000000000ff) */) << 56))
# else
#  define NS_SWAP64(x) LL_INIT((((x).lo /* & 0xff000000ul */) >> 24) |        \
                               (((x).lo    & 0x00ff0000ul)    >> 8)  |        \
                               (((x).lo    & 0x0000ff00ul)    << 8)  |        \
                               (((x).lo /* & 0x000000fful */) << 24),         \
                               (((x).hi /* & 0xff000000ul */) >> 24) |        \
                               (((x).hi    & 0x00ff0000ul)    >> 8)  |        \
                               (((x).hi    & 0x0000ff00ul)    << 8)  |        \
                               (((x).hi /* & 0x000000fful */) << 24))
# endif

#elif defined IS_BIG_ENDIAN

# define NS_SWAP16(x) (x)
# define NS_SWAP32(x) (x)
# define NS_SWAP64(x) (x)

#else

# error "Unknown byte order"

#endif

/**
 * These macros get and put a buffer given either an sba parameter that may
 * point to an object implementing nsIStreamBufferAccess, nsIObjectInputStream,
 * or nsIObjectOutputStream.
 */
#define NS_GET_BUFFER(sba,n,a)  ((sba)->GetBuffer(n, a))
#define NS_PUT_BUFFER(sba,p,n)  ((sba)->PutBuffer(p, n))

#define NS_GET8(p)              (*(uint8_t*)(p))
#define NS_GET16(p)             NS_SWAP16(*(uint16_t*)(p))
#define NS_GET32(p)             NS_SWAP32(*(uint32_t*)(p))
#define NS_GET64(p)             NS_SWAP64(*(uint64_t*)(p))
#define NS_GET_FLOAT(p)         ((float)NS_SWAP32(*(uint32_t*)(p)))
#define NS_GET_DOUBLE(p)        ((double)NS_SWAP64(*(uint64_t*)(p)))

#define NS_PUT8(p,x)            (*(uint8_t*)(p) = (x))
#define NS_PUT16(p,x)           (*(uint16_t*)(p) = NS_SWAP16(x))
#define NS_PUT32(p,x)           (*(uint32_t*)(p) = NS_SWAP32(x))
#define NS_PUT64(p,x)           (*(uint64_t*)(p) = NS_SWAP64(x))
#define NS_PUT_FLOAT(p,x)       (*(uint32_t*)(p) = NS_SWAP32(*(uint32_t*)&(x)))
#define NS_PUT_DOUBLE(p,x)      (*(uint64_t*)(p) = NS_SWAP64(*(uint64_t*)&(x)))

%}
