/* * XDR an indirect pointer * xdr_reference is for recursively translating a structure that is * referenced by a pointer inside the structure that is currently being * translated. pp references a pointer to storage. If *pp is null * the necessary storage is allocated. * size is the sizeof the referneced structure. * proc is the routine to handle the referenced structure. */ bool_t xdr_referenceEx(XDR *xdrs, caddr_t *pp, /* the pointer to work on */ u_int size, /* size of the object pointed to */ xdrproc_t proc, xdrproc_t xdr_user_obj) { /* xdr routine to handle the object */ caddr_t loc = *pp; bool_t stat; if (loc == NULL) switch (xdrs->x_op) { case XDR_FREE: return TRUE; case XDR_DECODE: *pp = loc = (caddr_t) XDR_ALLOC(xdrs, size); if (loc == NULL) { /*warnx("xdr_reference: out of memory"); */ return FALSE; } memset(loc, 0, size); break; case XDR_ENCODE: break; } stat = (*proc) (xdrs, loc, xdr_user_obj); if (xdrs->x_op == XDR_FREE) { XDR_DEALLOC(xdrs, loc, size); *pp = NULL; } return stat; }
/* * XDR an array of arbitrary elements * *addrp is a pointer to the array, *sizep is the number of elements. * If addrp is NULL (*sizep * elsize) bytes are allocated. * elsize is the size (in bytes) of each element, and elproc is the * xdr procedure to call to handle each element of the array. */ bool_t xdr_array(XDR *xdrs, caddr_t *addrp, u_int *sizep, u_int maxsize, u_int elsize, xdrproc_t elproc) { u_int i; caddr_t target = *addrp; u_int c; /* the actual element count */ bool_t stat = TRUE; u_int nodesize; /* like strings, arrays are really counted arrays */ if (!xdr_u_int(xdrs, sizep)) return (FALSE); c = *sizep; if ((c > maxsize || UINT_MAX/elsize < c) && (xdrs->x_op != XDR_FREE)) return (FALSE); nodesize = c * elsize; /* * if we are deserializing, we may need to allocate an array. * We also save time by checking for a null array if we are freeing. */ if (target == NULL) switch (xdrs->x_op) { case XDR_DECODE: if (c == 0) return (TRUE); *addrp = target = (caddr_t)XDR_ALLOC(xdrs, nodesize); if (target == NULL) { // warnx("xdr_array: out of memory"); return (FALSE); } memset(target, 0, nodesize); break; case XDR_FREE: return (TRUE); case XDR_ENCODE: break; } /* * now we xdr each element of array */ for (i = 0; (i < c) && stat; i++) { stat = (*elproc)(xdrs, target); target += elsize; } /* * the array may need freeing */ if (xdrs->x_op == XDR_FREE) { XDR_DEALLOC(xdrs, *addrp, nodesize); *addrp = NULL; } return (stat); }