/*
* Copyright (c) 2003-2015 Hypertriton, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Serialization of integers.
*/
#ifndef _LIBPERCGI_LOAD_INTEGRAL_H_
#define _LIBPERCGI_LOAD_INTEGRAL_H_
#include
__BEGIN_DECLS
#define SYS_ReadSint8(ds) ((Sint8)SYS_ReadUint8(ds))
#define SYS_ReadSint8v(ds,v) SYS_ReadUint8v(ds, (Sint8 *)(v))
#define SYS_WriteSint8(ds,v) SYS_WriteUint8((ds),(Uint8)(v))
#define SYS_WriteSint8v(ds,v) SYS_WriteUint8v((ds),(Uint8 *)(v))
#define SYS_ReadSint16(ds) ((Sint16)SYS_ReadUint16(ds))
#define SYS_ReadSint16v(ds,v) SYS_ReadUint16v(ds, (Sint16 *)(v))
#define SYS_WriteSint16(ds,v) SYS_WriteUint16((ds),(Uint16)(v))
#define SYS_WriteSint16v(ds,v) SYS_WriteUint16v((ds),(Uint16 *)(v))
#define SYS_ReadSint32(ds) ((Sint32)SYS_ReadUint32(ds))
#define SYS_ReadSint32v(ds,v) SYS_ReadUint32v(ds, (Sint32 *)(v))
#define SYS_WriteSint32(ds,v) SYS_WriteUint32((ds),(Uint32)(v))
#define SYS_WriteSint32v(ds,v) SYS_WriteUint32v((ds),(Uint32 *)(v))
#ifdef HAVE_64BIT
# define SYS_ReadSint64(ds) ((Sint64)SYS_ReadUint64(ds))
# define SYS_ReadSint64v(ds,v) SYS_ReadUint64v(ds, (Sint64 *)(v))
# define SYS_WriteSint64(ds,v) SYS_WriteUint64((ds),(Uint64)(v))
# define SYS_WriteSint64v(ds,v) SYS_WriteUint64v((ds),(Uint64 *)(v))
#endif
/*
* 8-bit integers
*/
static __inline__ Uint8
SYS_ReadUint8(int fd)
{
Uint8 i;
if (SYS_Read(fd, &i, sizeof(i)) != 0) {
CGI_Exit(CGI_EX_IOERR, NULL);
}
return (i);
}
static __inline__ int
SYS_ReadUint8v(int fd, Uint8 *v)
{
Uint8 i;
if (SYS_Read(fd, &i, sizeof(i)) != 0) {
return (-1);
}
*v = i;
return (0);
}
static __inline__ void
SYS_WriteUint8(int fd, Uint8 i)
{
if (SYS_Write(fd, &i, sizeof(i)) != 0)
CGI_Exit(CGI_EX_IOERR, NULL);
}
static __inline__ int
SYS_WriteUint8v(int fd, const Uint8 *i)
{
return SYS_Write(fd, i, sizeof(Uint8));
}
/*
* 16-bit integers
*/
static __inline__ Uint16
SYS_ReadUint16(int fd)
{
Uint16 i;
if (SYS_Read(fd, &i, sizeof(i)) != 0) {
CGI_Exit(CGI_EX_IOERR, NULL);
}
return CGI_SwapLE16(i);
}
static __inline__ int
SYS_ReadUint16v(int fd, Uint16 *v)
{
Uint16 i;
if (SYS_Read(fd, &i, sizeof(i)) != 0) {
return (-1);
}
*v = CGI_SwapLE16(i);
return (0);
}
static __inline__ void
SYS_WriteUint16(int fd, Uint16 u16)
{
Uint16 i = CGI_SwapLE16(u16);
if (SYS_Write(fd, &i, sizeof(i)) != 0)
CGI_Exit(CGI_EX_IOERR, NULL);
}
static __inline__ int
SYS_WriteUint16v(int fd, const Uint16 *u16)
{
Uint16 i = CGI_SwapLE16(*u16);
return SYS_Write(fd, &i, sizeof(i));
}
/*
* 32-bit integers
*/
static __inline__ Uint32
SYS_ReadUint32(int fd)
{
Uint32 i;
if (SYS_Read(fd, &i, sizeof(i)) != 0) {
CGI_Exit(CGI_EX_IOERR, NULL);
}
return CGI_SwapLE32(i);
}
static __inline__ int
SYS_ReadUint32v(int fd, Uint32 *v)
{
Uint32 i;
if (SYS_Read(fd, &i, sizeof(i)) != 0) {
return (-1);
}
*v = CGI_SwapLE32(i);
return (0);
}
static __inline__ void
SYS_WriteUint32(int fd, Uint32 u32)
{
Uint32 i = CGI_SwapLE32(u32);
if (SYS_Write(fd, &i, sizeof(i)) != 0)
CGI_Exit(CGI_EX_IOERR, NULL);
}
static __inline__ int
SYS_WriteUint32v(int fd, const Uint32 *u32)
{
Uint32 i = CGI_SwapLE32(*u32);
return SYS_Write(fd, &i, sizeof(i));
}
#ifdef HAVE_64BIT
/*
* 64-bit integers
*/
static __inline__ Uint64
SYS_ReadUint64(int fd)
{
Uint64 i;
if (SYS_Read(fd, &i, sizeof(i)) != 0) {
CGI_Exit(CGI_EX_IOERR, NULL);
}
return CGI_SwapLE64(i);
}
static __inline__ int
SYS_ReadUint64v(int fd, Uint64 *v)
{
Uint64 i;
if (SYS_Read(fd, &i, sizeof(i)) != 0) {
return (-1);
}
*v = CGI_SwapLE64(i);
return (0);
}
static __inline__ void
SYS_WriteUint64(int fd, Uint64 u64)
{
Uint64 i = CGI_SwapLE64(u64);
if (SYS_Write(fd, &i, sizeof(i)) != 0)
CGI_Exit(CGI_EX_IOERR, NULL);
}
static __inline__ int
SYS_WriteUint64v(int fd, const Uint64 *u64)
{
Uint64 i = CGI_SwapLE64(*u64);
return SYS_Write(fd, &i, sizeof(i));
}
#endif /* HAVE_64BIT */
__END_DECLS
#endif /* _LIBPERCGI_LOAD_INTEGRAL_H_ */