+#define fxdr_unsigned(t, v) ((t)ntohl((uint32_t)(v)))
+#define txdr_unsigned(v) (htonl((uint32_t)(v)))
+
+#define fxdr_hyper(f, t) { \
+ ((uint32_t *)(t))[_QUAD_HIGHWORD] = ntohl(((uint32_t *)(f))[0]); \
+ ((uint32_t *)(t))[_QUAD_LOWWORD] = ntohl(((uint32_t *)(f))[1]); \
+}
+#define txdr_hyper(f, t) { \
+ ((uint32_t *)(t))[0] = htonl(((uint32_t *)(f))[_QUAD_HIGHWORD]); \
+ ((uint32_t *)(t))[1] = htonl(((uint32_t *)(f))[_QUAD_LOWWORD]); \
+}
+
+
+/*
+ * xdrbuf
+ *
+ * generalized functionality for managing the building/dissecting of XDR data
+ */
+typedef enum xdrbuf_type {
+ XDRBUF_NONE = 0,
+ XDRBUF_BUFFER = 1,
+} xdrbuf_type;
+
+struct xdrbuf {
+ union {
+ struct {
+ char * xbb_base; /* base address of buffer */
+ uint32_t xbb_size; /* size of buffer */
+ uint32_t xbb_len; /* length of data in buffer */
+ } xb_buffer;
+ } xb_u;
+ char * xb_ptr; /* pointer to current position */
+ size_t xb_left; /* bytes remaining in current buffer */
+ size_t xb_growsize; /* bytes to allocate when growing */
+ xdrbuf_type xb_type; /* type of xdr buffer */
+ uint32_t xb_flags; /* XB_* (see below) */
+};
+
+#define XB_CLEANUP 0x0001 /* needs cleanup */
+
+#define XDRWORD 4 /* the basic XDR building block is a 4 byte (32 bit) word */
+#define xdr_rndup(a) (((a)+3)&(~0x3)) /* round up to XDRWORD size */
+#define xdr_pad(a) (xdr_rndup(a) - (a)) /* calculate round up padding */
+
+void xb_init(struct xdrbuf *, xdrbuf_type);
+void xb_init_buffer(struct xdrbuf *, char *, size_t);
+void xb_cleanup(struct xdrbuf *);
+void *xb_malloc(size_t);
+void xb_free(void *);
+int xb_grow(struct xdrbuf *);
+void xb_set_cur_buf_len(struct xdrbuf *);
+char *xb_buffer_base(struct xdrbuf *);
+int xb_advance(struct xdrbuf *, uint32_t);
+int xb_offset(struct xdrbuf *);
+int xb_seek(struct xdrbuf *, uint32_t);
+int xb_add_bytes(struct xdrbuf *, const char *, uint32_t, int);
+int xb_get_bytes(struct xdrbuf *, char *, uint32_t, int);
+
+#ifdef _NFS_XDR_SUBS_FUNCS_
+
+/*
+ * basic initialization of xdrbuf structure
+ */
+void
+xb_init(struct xdrbuf *xbp, xdrbuf_type type)
+{
+ bzero(xbp, sizeof(*xbp));
+ xbp->xb_type = type;
+ xbp->xb_flags |= XB_CLEANUP;
+}
+
+/*
+ * initialize a single-buffer xdrbuf
+ */
+void
+xb_init_buffer(struct xdrbuf *xbp, char *buf, size_t buflen)
+{
+ xb_init(xbp, XDRBUF_BUFFER);
+ xbp->xb_u.xb_buffer.xbb_base = buf;
+ xbp->xb_u.xb_buffer.xbb_size = buflen;
+ xbp->xb_u.xb_buffer.xbb_len = buflen;
+ xbp->xb_growsize = 512;
+ xbp->xb_ptr = buf;
+ xbp->xb_left = buflen;
+ if (buf) { /* when using an existing buffer, xb code should skip cleanup */
+ xbp->xb_flags &= ~XB_CLEANUP;
+ }
+}
+
+/*
+ * get the pointer to the single-buffer xdrbuf's buffer
+ */
+char *
+xb_buffer_base(struct xdrbuf *xbp)
+{
+ return xbp->xb_u.xb_buffer.xbb_base;
+}
+
+/*
+ * clean up any resources held by an xdrbuf
+ */
+void
+xb_cleanup(struct xdrbuf *xbp)
+{
+ if (!(xbp->xb_flags & XB_CLEANUP)) {
+ return;
+ }
+ switch (xbp->xb_type) {
+ case XDRBUF_BUFFER:
+ if (xbp->xb_u.xb_buffer.xbb_base) {
+ xb_free(xbp->xb_u.xb_buffer.xbb_base);
+ }
+ break;
+ default:
+ break;
+ }
+ xbp->xb_flags &= ~XB_CLEANUP;
+}
+
+/*
+ * set the length of valid data in the current buffer to
+ * be up to the current location within the buffer
+ */
+void
+xb_set_cur_buf_len(struct xdrbuf *xbp)
+{
+ switch (xbp->xb_type) {
+ case XDRBUF_BUFFER:
+ xbp->xb_u.xb_buffer.xbb_len = xbp->xb_ptr - xbp->xb_u.xb_buffer.xbb_base;
+ break;
+ default:
+ break;
+ }
+}
+
+/*
+ * advance forward through existing data in xdrbuf
+ */
+int
+xb_advance(struct xdrbuf *xbp, uint32_t len)
+{
+ uint32_t tlen;
+
+ while (len) {
+ if (xbp->xb_left <= 0) {
+ return EBADRPC;
+ }
+ tlen = MIN(xbp->xb_left, len);
+ if (tlen) {
+ xbp->xb_ptr += tlen;
+ xbp->xb_left -= tlen;
+ len -= tlen;
+ }
+ }
+ return 0;
+}
+
+/*
+ * Calculate the current offset in the XDR buffer.
+ */
+int
+xb_offset(struct xdrbuf *xbp)
+{
+ uint32_t offset = 0;
+
+ switch (xbp->xb_type) {
+ case XDRBUF_BUFFER:
+ offset = xbp->xb_ptr - xbp->xb_u.xb_buffer.xbb_base;
+ break;
+ default:
+ break;
+ }