From 68959447b0c5addf4fb975d26650351adc3a0293 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Wed, 14 Jan 2009 16:24:48 +0000 Subject: [PATCH] New header files with most of the inline functions required. --- configure.ac | 4 + portablexdr-5/rpc/rpc.h | 27 +++ portablexdr-5/rpc/types.h | 48 +++++ portablexdr-5/rpc/xdr.h | 366 +++++++++++++++++++++++++++++++++++++++ portablexdr-5/rpc/xdr_internal.h | 201 +++++++++++++++++++++ rpc/rpc.h | 3 - 6 files changed, 646 insertions(+), 3 deletions(-) create mode 100644 portablexdr-5/rpc/rpc.h create mode 100644 portablexdr-5/rpc/types.h create mode 100644 portablexdr-5/rpc/xdr.h create mode 100644 portablexdr-5/rpc/xdr_internal.h delete mode 100644 rpc/rpc.h diff --git a/configure.ac b/configure.ac index d252bd1..fed9614 100644 --- a/configure.ac +++ b/configure.ac @@ -15,6 +15,10 @@ AC_PROG_LIBTOOL AC_PROG_LEX AC_PROG_YACC +AC_TYPE_SIZE_T +AC_TYPE_SSIZE_T +AC_TYPE_OFF_T + AC_CHECK_PROGS(AR, ar) dnl Look for an external 'cpp' program which can run on a file with any diff --git a/portablexdr-5/rpc/rpc.h b/portablexdr-5/rpc/rpc.h new file mode 100644 index 0000000..68a5136 --- /dev/null +++ b/portablexdr-5/rpc/rpc.h @@ -0,0 +1,27 @@ +/* PortableXDR - a free, portable XDR implementation. + * Copyright (C) 2009 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PORTABLEXDR_RPC_H +#define PORTABLEXDR_RPC_H + +/* This header file is just for compatibility with SunRPC. */ + +#include +#include + +#endif /* PORTABLEXDR_RPC_H */ diff --git a/portablexdr-5/rpc/types.h b/portablexdr-5/rpc/types.h new file mode 100644 index 0000000..c0a29b2 --- /dev/null +++ b/portablexdr-5/rpc/types.h @@ -0,0 +1,48 @@ +/* PortableXDR - a free, portable XDR implementation. + * Copyright (C) 2009 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PORTABLEXDR_TYPES_H +#define PORTABLEXDR_TYPES_H + +/* Note that unlike Sun's RPC, we don't define all sorts of 'unusual' + * types like u_char, quad_t, etc. Instead we always use the + * C99 types from such as uint8_t and int64_t. + */ +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Define the boolean type in the wire protocol. */ +#ifndef TRUE +#define TRUE 1 +#endif +#ifndef FALSE +#define FALSE 0 +#endif +typedef int32_t bool_t; + +/* Define the enumerated type in the wire protocol. */ +typedef int32_t enum_t; + +#ifdef __cplusplus +} +#endif + +#endif /* PORTABLEXDR_TYPES_H */ diff --git a/portablexdr-5/rpc/xdr.h b/portablexdr-5/rpc/xdr.h new file mode 100644 index 0000000..4f59fb1 --- /dev/null +++ b/portablexdr-5/rpc/xdr.h @@ -0,0 +1,366 @@ +/* PortableXDR - a free, portable XDR implementation. + * Copyright (C) 2009 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* xdr_int8_t, xdr_uint8_t, xdr_int16_t, xdr_uint16_t, xdr_int32_t, + * xdr_uint32_t, xdr_int64_t and xdr_uint64_t functions: + * + * Copyright (c) 1998, 1999, 2000, 2004, 2005 Free Software Foundation, Inc. + * This file is part of the GNU C Library. + * Contributed by Thorsten Kukuk , 1998. + * + * (Under the same license as above). + */ + +#ifndef PORTABLEXDR_XDR_H +#define PORTABLEXDR_XDR_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Please read the documentation that comes with PortableXDR, + * otherwise you won't understand how to use the xdr_* conversion + * functions. + */ +typedef bool_t (*xdrproc_t) (); +#define NULL_xdrproc_t NULL + +/* XDR procedures for the basic types. */ +static inline bool_t +xdr_void (XDR *xdrs ATTRIBUTE_UNUSED, void *vp ATTRIBUTE_UNUSED) +{ + return TRUE; +} + +static inline bool_t +xdr_bool (XDR *xdrs, bool_t *p) +{ + int32_t i; + switch (xdrs->x_op) { + case XDR_ENCODE: + i = *p ? TRUE : FALSE; + return xdr_putlong (xdrs, &i); + case XDR_DECODE: + if (!xdr_getlong (xdrs, &i)) return FALSE; + *p = i ? TRUE : FALSE; + return TRUE; + case XDR_FREE: + return TRUE; + default: + return FALSE; + } +} + +/* XDR 64bit integers */ +bool_t +xdr_int64_t (XDR *xdrs, int64_t *ip) +{ + int32_t t1, t2; + + switch (xdrs->x_op) + { + case XDR_ENCODE: + t1 = (int32_t) ((*ip) >> 32); + t2 = (int32_t) (*ip); + return (XDR_PUTINT32(xdrs, &t1) && XDR_PUTINT32(xdrs, &t2)); + case XDR_DECODE: + if (!XDR_GETINT32(xdrs, &t1) || !XDR_GETINT32(xdrs, &t2)) + return FALSE; + *ip = ((int64_t) t1) << 32; + *ip |= (uint32_t) t2; /* Avoid sign extension. */ + return TRUE; + case XDR_FREE: + return TRUE; + default: + return FALSE; + } +} + +/* XDR 64bit unsigned integers */ +bool_t +xdr_uint64_t (XDR *xdrs, uint64_t *uip) +{ + uint32_t t1; + uint32_t t2; + + switch (xdrs->x_op) + { + case XDR_ENCODE: + t1 = (uint32_t) ((*uip) >> 32); + t2 = (uint32_t) (*uip); + return (XDR_PUTINT32 (xdrs, (int32_t *) &t1) && + XDR_PUTINT32(xdrs, (int32_t *) &t2)); + case XDR_DECODE: + if (!XDR_GETINT32(xdrs, (int32_t *) &t1) || + !XDR_GETINT32(xdrs, (int32_t *) &t2)) + return FALSE; + *uip = ((uint64_t) t1) << 32; + *uip |= t2; + return TRUE; + case XDR_FREE: + return TRUE; + default: + return FALSE; + } +} + +/* XDR 32bit integers */ +bool_t +xdr_int32_t (XDR *xdrs, int32_t *lp) +{ + switch (xdrs->x_op) + { + case XDR_ENCODE: + return XDR_PUTINT32 (xdrs, lp); + case XDR_DECODE: + return XDR_GETINT32 (xdrs, lp); + case XDR_FREE: + return TRUE; + default: + return FALSE; + } +} + +/* XDR 32bit unsigned integers */ +bool_t +xdr_uint32_t (XDR *xdrs, uint32_t *ulp) +{ + switch (xdrs->x_op) + { + case XDR_ENCODE: + return XDR_PUTINT32 (xdrs, (int32_t *) ulp); + case XDR_DECODE: + return XDR_GETINT32 (xdrs, (int32_t *) ulp); + case XDR_FREE: + return TRUE; + default: + return FALSE; + } +} + +/* XDR 16bit integers */ +bool_t +xdr_int16_t (XDR *xdrs, int16_t *ip) +{ + int32_t t; + + switch (xdrs->x_op) + { + case XDR_ENCODE: + t = (int32_t) *ip; + return XDR_PUTINT32 (xdrs, &t); + case XDR_DECODE: + if (!XDR_GETINT32 (xdrs, &t)) + return FALSE; + *ip = (int16_t) t; + return TRUE; + case XDR_FREE: + return TRUE; + default: + return FALSE; + } +} + +/* XDR 16bit unsigned integers */ +bool_t +xdr_uint16_t (XDR *xdrs, uint16_t *uip) +{ + uint32_t ut; + + switch (xdrs->x_op) + { + case XDR_ENCODE: + ut = (uint32_t) *uip; + return XDR_PUTINT32 (xdrs, (int32_t *) &ut); + case XDR_DECODE: + if (!XDR_GETINT32 (xdrs, (int32_t *) &ut)) + return FALSE; + *uip = (uint16_t) ut; + return TRUE; + case XDR_FREE: + return TRUE; + default: + return FALSE; + } +} + +/* XDR 8bit integers */ +bool_t +xdr_int8_t (XDR *xdrs, int8_t *ip) +{ + int32_t t; + + switch (xdrs->x_op) + { + case XDR_ENCODE: + t = (int32_t) *ip; + return XDR_PUTINT32 (xdrs, &t); + case XDR_DECODE: + if (!XDR_GETINT32 (xdrs, &t)) + return FALSE; + *ip = (int8_t) t; + return TRUE; + case XDR_FREE: + return TRUE; + default: + return FALSE; + } +} + +/* XDR 8bit unsigned integers */ +bool_t +xdr_uint8_t (XDR *xdrs, uint8_t *uip) +{ + uint32_t ut; + + switch (xdrs->x_op) + { + case XDR_ENCODE: + ut = (uint32_t) *uip; + return XDR_PUTINT32 (xdrs, (int32_t *) &ut); + case XDR_DECODE: + if (!XDR_GETINT32 (xdrs, (int32_t *) &ut)) + return FALSE; + *uip = (uint8_t) ut; + return TRUE; + case XDR_FREE: + return TRUE; + default: + return FALSE; + } +} + +#if 0 +static inline bool_t +xdr_float (XDR *xdrs, float *p) +{ + XXX; +} + +static inline bool_t +xdr_double (XDR *xdrs, double *p) +{ + XXX; +} +#endif + +/* Some very common aliases for the basic integer functions. */ +#define xdr_int xdr_int32_t +#define xdr_u_int xdr_uint32_t +#define xdr_long xdr_int32_t +#define xdr_u_long xdr_uint32_t + +#define xdr_short xdr_int16_t +#define xdr_u_short xdr_uint16_t + + /* NB: In PortableXDR, char is ALWAYS treated as signed octet. */ +#define xdr_char xdr_int8_t +#define xdr_u_char xdr_uint8_t + +/* +#define xdr_hyper xdr_int64_t +#define xdr_u_hyper xdr_uint64_t +#define xdr_quad xdr_int64_t +#define xdr_u_quad xdr_uint64_t +*/ + +/* Enumerations. */ +static inline bool_t +xdr_enum (XDR *xdrs, enum_t *ep) +{ + return xdr_int32_t (xdrs, ep); +} + +/* Union with discriminator. */ +extern bool_t xdr_union (XDR *xdrs, enum_t *discrim, void *p, struct xdr_discrim *choices, xdrproc_t default_proc); + +/* Variable-size array of arbitrary elements. */ +extern bool_t xdr_array (XDR *xdrs, void *p, size_t num_elements, size_t max_elements, size_t element_size, xdrproc_t element_proc); + +/* Fixed-size array of arbitrary elements. */ +extern bool_t xdr_vector (XDR *xdrs, void *p, size_t num_elements, size_t element_size, xdrproc_t element_size); + +/* Variable-size array of bytes. */ +extern bool_t xdr_bytes (XDR *xdrs, char **bytes, size_t num_bytes, size_t max_bytes); + +/* Fixed-size array of bytes. */ +extern bool_t xdr_opaque (XDR *xdrs, void *p, size_t num_bytes); + +/* Variable-size string (handled as normal C string in the program, but + * serialized as length + bytes). + */ +extern bool_t xdr_string (XDR *xdrs, char **str, size_t max_bytes); + +#define xdr_wrapstring(xdrs,str) (xdr_string ((xdrs),(str),~0)) + +#if 0 +/* A reference is a pointer which cannot be NULL. */ +static inline bool_t +xdr_reference (XDR *xdrs, void **p, size_t size, xdrproc_t proc) +{ + XXX +} + +/* A pointer is a pointer to an object that can be NULL. It is + * serialized as a boolean (TRUE = not null, FALSE = null), followed + * by the object. + */ +static inline bool_t +xdr_pointer (XDR *xdrs, void **p, size_t size, xdrproc_t proc) +{ + XXX +} +#endif + +/* Free an XDR object (recursively). */ +extern void xdr_free (xdrproc_t, void *); + +/* Construct an XDR stream from an in-memory buffer. */ +extern void xdrmem_create (XDR *xdrs, void *p, size_t size, enum xdr_op); + +/* Construct an XDR stream from a FILE* or file descriptor. Note + * that neither of these will close the underlying file. + */ +extern void xdrstdio_create (XDR *xdrs, FILE *, enum xdr_op); +extern void xdrfd_create (XDR *xdrs, int fd, enum xdr_op); + +/* PortableXDR variations which allow you to close the underlying + * file after use. + */ +#define XDR_CLOSE_FILE 1 + +extern void xdrstdio_create2 (XDR *xdrs, FILE *, enum xdr_op, uint32_t flags); +extern void xdrfd_create2 (XDR *xdrs, int fd, enum xdr_op, uint32_t flags); + +/* + Does anyone ever use these? Contributions welcome. +extern void xdrrec_create (...); +extern bool_t xdrrec_endofrecord (...); +extern bool_t xdrrec_skiprecord (...); +extern bool_t xdrrec_eof (...); +*/ + +#ifdef __cplusplus +} +#endif + +#endif /* PORTABLEXDR_XDR_H */ diff --git a/portablexdr-5/rpc/xdr_internal.h b/portablexdr-5/rpc/xdr_internal.h new file mode 100644 index 0000000..1670168 --- /dev/null +++ b/portablexdr-5/rpc/xdr_internal.h @@ -0,0 +1,201 @@ +/* PortableXDR - a free, portable XDR implementation. + * Copyright (C) 2009 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PORTABLEXDR_XDR_INTERNAL_H +#define PORTABLEXDR_XDR_INTERNAL_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __GNUC__ +#ifndef __GNUC_PREREQ +#define __GNUC_PREREQ(maj,min) 0 +#endif +#ifndef ATTRIBUTE_UNUSED +#define ATTRIBUTE_UNUSED __attribute__((__unused__)) +#endif +#else +#define ATTRIBUTE_UNUSED +#endif /* __GNUC__ */ + +/* Here we get into what could be termed the "internals" of XDR. Most + * callers can get away with only ever using the basic XDR types and + * functions (see . Sometimes, particularly for + * compatibility with Sun XDR code, you need to delve into the + * internals. + */ + +enum xdr_op { + XDR_ENCODE, + XDR_DECODE, + XDR_FREE +}; + +/* Operations available on an XDR data stream (file, socket or memory + * area). Callers shouldn't normally use these, but Sun's XDR + * implementation exposes these operations, so we do too. + */ +struct xdr_ops { + /* Get/put "long" (ie. 32 bit quantity). */ + bool_t (*x_getlong) (XDR *, int32_t *); + bool_t (*x_putlong) (XDR *, int32_t *); + /* Get/put bytes. */ + bool_t (*x_getbytes) (XDR *, void *, size_t); + bool_t (*x_putbytes) (XDR *, void *, size_t); + /* Get or seek within the stream (offsets from beginning of stream). */ + off_t (*x_getpostn) (XDR *); + bool_t (*x_setpostn) (XDR *, off_t); + /* Returns a pointer to the next n bytes in the stream. */ + void * (*x_inline) (XDR *, size_t); + /* Free the stream. */ + void (*x_destroy) (XDR *); +}; + +struct xdr { + /* Calling code can read the operation field, but should not update it. */ + enum xdr_op x_op; /* operation (encode/decode/free) */ + + /* Calling code may use this pointer for any purpose, eg. + * to point to their own data. + */ + void *x_public; + + struct xdr_ops x_ops; + + /* The remaining fields are private and could change in any + * future release. Calling code should not use or modify them. + */ + void *x__private; +}; + +/* Define wrapper functions around the x_ops. */ +static inline bool_t +xdr_getlong (XDR *xdrs, int32_t *v) +{ + return xdrs->x_ops->x_getlong (xdrs, v); +} +static inline bool_t +xdr_putlong (XDR *xdrs, int32_t *v) +{ + return xdrs->x_ops->x_putlong (xdrs, v); +} +static inline bool_t +xdr_getbytes (XDR *xdrs, void *p, size_t len) +{ + return xdrs->x_ops->x_getbytes (xdrs, p, len); +} +static inline bool_t +xdr_putbytes (XDR *xdrs, void *p, size_t len) +{ + return xdrs->x_ops->x_putbytes (xdrs, p, len); +} +static inline off_t +xdr_getpos (XDR *xdrs) +{ + return xdrs->x_ops->x_getpostn (xdrs); +} +static inline bool_t +xdr_setpos (XDR *xdrs, off_t v) +{ + return xdrs->x_ops->x_setpostn (xdrs, v); +} +static inline void +xdr_inline (XDR *xdrs, size_t len) +{ + return xdrs->x_ops->x_inline (xdrs, len); +} +static inline void +xdr_destroy (XDR *xdrs) +{ + return xdrs->x_ops->x_destroy (xdrs); +} + +/* For compatibility with Sun XDR. */ +#define XDR_GETLONG xdr_getlong +#define XDR_PUTLONG xdr_putlong +#define XDR_GETBYTES xdr_getbytes +#define XDR_PUTBYTES xdr_putbytes +#define XDR_GETPOS xdr_getpos +#define XDR_SETPOS xdr_setpos +#define XDR_INLINE xdr_inline +#define XDR_DESTROY xdr_destroy + +/* Also seen in the wild ... */ +#define XDR_GETINT32 xdr_getlong +#define XDR_PUTINT32 xdr_putlong + +/* These are the "inline" versions of the macros. These are used + * in SunRPC for some primitive optimizations. For example, suppose + * you have to parse 4 integers from the stream. You could + * optimize using these macros by doing: + * + * if (xdr->x_op == XDR_DECODE) { + * void *buf = xdr_inline (xdr, 4 * BYTES_PER_XDR_UNIT); + * if (buf) { + * i0 = IXDR_GET_LONG (buf); // NB. Macro autoincrements buf. + * i1 = IXDR_GET_LONG (buf); + * i2 = IXDR_GET_LONG (buf); + * i3 = IXDR_GET_LONG (buf); + * return TRUE; + * } + * } + * else if (xdr->x_op == XDR_ENCODE) { + * // Similar code required for encoding. + * } + * // Fallback code + * if (!xdr_int (xdr, &i0)) return FALSE; + * if (!xdr_int (xdr, &i1)) return FALSE; + * if (!xdr_int (xdr, &i2)) return FALSE; + * if (!xdr_int (xdr, &i3)) return FALSE; + * return TRUE; + * + * Note that you have to fallback in the case when xdr_inline + * returns NULL. This is NOT an error case. + * + * Whether this optimization is really worth it or not is left as + * an exercise in benchmarking. In any case, PortableXDR's rpcgen + * does NOT perform this optimization. + */ + +#define BYTES_PER_XDR_UNIT 4 + +#define IXDR_GET_LONG(buf) ((int32_t) ntohl (*((int32_t *)(buf))++)) +#define IXDR_GET_BOOL(buf) ((bool_t) IXDR_GET_LONG ((buf))) +#define IXDR_GET_ENUM(buf,type) ((type) IXDR_GET_LONG ((buf))) +#define IXDR_GET_U_LONG(buf) ((uint32_t) IXDR_GET_LONG ((buf))) +#define IXDR_GET_SHORT(buf) ((int16_t) IXDR_GET_LONG ((buf))) +#define IXDR_GET_U_SHORT(buf) ((uint16_t) IXDR_GET_LONG ((buf))) +#define IXDR_GET_INT32 IXDR_GET_LONG + +#define IXDR_PUT_LONG(buf,v) ((*((int32_t *)(buf))++) = htonl ((v))) +#define IXDR_PUT_BOOL(buf,v) IXDR_PUT_LONG((buf), (int32_t) (v)) +#define IXDR_PUT_ENUM(buf,v) IXDR_PUT_LONG((buf), (int32_t) (v)) +#define IXDR_PUT_U_LONG(buf,v) IXDR_PUT_LONG((buf), (int32_t) (v)) +#define IXDR_PUT_SHORT(buf,v) IXDR_PUT_LONG((buf), (int32_t) (v)) +#define IXDR_PUT_U_SHORT(buf,v) IXDR_PUT_LONG((buf), (int32_t) (v)) +#define IXDR_PUT_INT32 IXDR_PUT_LONG + +#ifdef __cplusplus +} +#endif + +#endif /* PORTABLEXDR_XDR_INTERNAL_H */ diff --git a/rpc/rpc.h b/rpc/rpc.h deleted file mode 100644 index ff3d05a..0000000 --- a/rpc/rpc.h +++ /dev/null @@ -1,3 +0,0 @@ -#include "config.h" -#include -#include -- 1.8.3.1