+/*
+ * 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;
+ }
+
+ return offset;
+}
+
+/*
+ * Seek to the given offset in the existing data in the XDR buffer.
+ */
+int
+xb_seek(struct xdrbuf *xbp, uint32_t offset)
+{
+ switch (xbp->xb_type) {
+ case XDRBUF_BUFFER:
+ xbp->xb_ptr = xbp->xb_u.xb_buffer.xbb_base + offset;
+ xbp->xb_left = xbp->xb_u.xb_buffer.xbb_len - offset;
+ break;
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+/*
+ * allocate memory
+ */
+void *
+xb_malloc(size_t size)
+{
+ void *buf = NULL;
+
+#ifdef KERNEL
+ MALLOC(buf, void *, size, M_TEMP, M_WAITOK);
+#else
+ buf = malloc(size);
+#endif
+ return buf;
+}
+/*
+ * free a chunk of memory allocated with xb_malloc()
+ */
+void
+xb_free(void *buf)
+{
+#ifdef KERNEL
+ FREE(buf, M_TEMP);
+#else
+ free(buf);
+#endif
+}
+
+/*
+ * Increase space available for new data in XDR buffer.
+ */
+int
+xb_grow(struct xdrbuf *xbp)
+{
+ char *newbuf, *oldbuf;
+ size_t newsize, oldsize;
+
+ switch (xbp->xb_type) {
+ case XDRBUF_BUFFER:
+ oldsize = xbp->xb_u.xb_buffer.xbb_size;
+ oldbuf = xbp->xb_u.xb_buffer.xbb_base;
+ newsize = oldsize + xbp->xb_growsize;
+ if (newsize < oldsize) {
+ return ENOMEM;
+ }
+ newbuf = xb_malloc(newsize);
+ if (newbuf == NULL) {
+ return ENOMEM;
+ }
+ if (oldbuf != NULL) {
+ bcopy(oldbuf, newbuf, oldsize);
+ xb_free(oldbuf);
+ }
+ xbp->xb_u.xb_buffer.xbb_base = newbuf;
+ xbp->xb_u.xb_buffer.xbb_size = newsize;
+ xbp->xb_ptr = newbuf + oldsize;
+ xbp->xb_left = xbp->xb_growsize;
+ break;
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+/*
+ * xb_add_bytes()
+ *
+ * Add "count" bytes of opaque data pointed to by "buf" to the given XDR buffer.
+ */
+int
+xb_add_bytes(struct xdrbuf *xbp, const char *buf, uint32_t count, int nopad)
+{
+ uint32_t len, tlen;
+ int error;
+
+ len = nopad ? count : xdr_rndup(count);
+
+ /* copy in "count" bytes and zero out any pad bytes */
+ while (len) {
+ if (xbp->xb_left <= 0) {
+ /* need more space */
+ if ((error = xb_grow(xbp))) {
+ return error;
+ }
+ if (xbp->xb_left <= 0) {
+ return ENOMEM;
+ }
+ }
+ tlen = MIN(xbp->xb_left, len);
+ if (tlen) {
+ if (count) {
+ if (tlen > count) {
+ tlen = count;
+ }
+ bcopy(buf, xbp->xb_ptr, tlen);
+ } else {
+ bzero(xbp->xb_ptr, tlen);
+ }
+ xbp->xb_ptr += tlen;
+ xbp->xb_left -= tlen;
+ len -= tlen;
+ if (count) {
+ buf += tlen;
+ count -= tlen;
+ }
+ }
+ }
+ return 0;
+}
+
+/*
+ * xb_get_bytes()
+ *
+ * Get "count" bytes of opaque data from the given XDR buffer.
+ */
+int
+xb_get_bytes(struct xdrbuf *xbp, char *buf, uint32_t count, int nopad)
+{
+ uint32_t len, tlen;
+
+ len = nopad ? count : xdr_rndup(count);
+
+ /* copy in "count" bytes and zero out any pad bytes */
+ while (len) {
+ if (xbp->xb_left <= 0) {
+ return ENOMEM;
+ }
+ tlen = MIN(xbp->xb_left, len);
+ if (tlen) {
+ if (count) {
+ if (tlen > count) {
+ tlen = count;
+ }
+ bcopy(xbp->xb_ptr, buf, tlen);
+ }
+ xbp->xb_ptr += tlen;
+ xbp->xb_left -= tlen;
+ len -= tlen;
+ if (count) {
+ buf += tlen;
+ count -= tlen;
+ }
+ }
+ }
+ return 0;
+}
+
+#endif /* _NFS_XDR_SUBS_FUNCS_ */
+
+
+/*
+ * macros for building XDR data
+ */
+
+/* finalize the data that has been added to the buffer */
+#define xb_build_done(E, XB) \
+ do { \
+ if (E) break; \
+ xb_set_cur_buf_len(XB); \
+ } while (0)
+
+/* add a 32-bit value */
+#define xb_add_32(E, XB, VAL) \
+ do { \
+ uint32_t __tmp; \
+ if (E) break; \
+ __tmp = txdr_unsigned(VAL); \
+ (E) = xb_add_bytes((XB), (void*)&__tmp, XDRWORD, 0); \
+ } while (0)
+
+/* add a 64-bit value */
+#define xb_add_64(E, XB, VAL) \
+ do { \
+ uint64_t __tmp1, __tmp2; \
+ if (E) break; \
+ __tmp1 = (VAL); \
+ txdr_hyper(&__tmp1, &__tmp2); \
+ (E) = xb_add_bytes((XB), (char*)&__tmp2, 2 * XDRWORD, 0); \
+ } while (0)
+
+/* add an array of XDR words */
+#define xb_add_word_array(E, XB, A, LEN) \
+ do { \
+ uint32_t __i; \
+ xb_add_32((E), (XB), (LEN)); \
+ for (__i=0; __i < (uint32_t)(LEN); __i++) \
+ xb_add_32((E), (XB), (A)[__i]); \
+ } while (0)
+#define xb_add_bitmap(E, XB, B, LEN) xb_add_word_array((E), (XB), (B), (LEN))
+
+/* add a file handle */
+#define xb_add_fh(E, XB, FHP, FHLEN) \
+ do { \
+ xb_add_32((E), (XB), (FHLEN)); \
+ if (E) break; \
+ (E) = xb_add_bytes((XB), (char*)(FHP), (FHLEN), 0); \
+ } while (0)
+
+/* add a string */
+#define xb_add_string(E, XB, S, LEN) \
+ do { \
+ xb_add_32((E), (XB), (LEN)); \
+ if (E) break; \
+ (E) = xb_add_bytes((XB), (const char*)(S), (LEN), 0); \
+ } while (0)
+
+
+/*
+ * macros for decoding XDR data
+ */
+
+/* skip past data in the buffer */
+#define xb_skip(E, XB, LEN) \
+ do { \
+ if (E) break; \
+ (E) = xb_advance((XB), (LEN)); \
+ } while (0)
+
+/* get a 32-bit value */
+#define xb_get_32(E, XB, LVAL) \
+ do { \
+ uint32_t __tmp; \
+ if (E) break; \
+ (E) = xb_get_bytes((XB), (char*)&__tmp, XDRWORD, 0); \
+ if (E) break; \
+ (LVAL) = fxdr_unsigned(uint32_t, __tmp); \
+ } while (0)
+
+/* get a 64-bit value */
+#define xb_get_64(E, XB, LVAL) \
+ do { \
+ uint64_t __tmp; \
+ if (E) break; \
+ (E) = xb_get_bytes((XB), (char*)&__tmp, 2 * XDRWORD, 0); \
+ if (E) break; \
+ fxdr_hyper(&__tmp, &(LVAL)); \
+ } while (0)
+
+/* get an array of XDR words (of a given expected/maximum length) */
+#define xb_get_word_array(E, XB, A, LEN) \
+ do { \
+ uint32_t __len = 0, __i; \
+ xb_get_32((E), (XB), __len); \
+ if (E) break; \
+ for (__i=0; __i < MIN(__len, (uint32_t)(LEN)); __i++) \
+ xb_get_32((E), (XB), (A)[__i]); \
+ if (E) break; \
+ for (; __i < __len; __i++) \
+ xb_skip((E), (XB), XDRWORD); \
+ for (; __i < (uint32_t)(LEN); __i++) \
+ (A)[__i] = 0; \
+ (LEN) = __len; \
+ } while (0)
+#define xb_get_bitmap(E, XB, B, LEN) xb_get_word_array((E), (XB), (B), (LEN))
+