xref: /freebsd-13-stable/sys/xdr/xdr.c (revision b59d578945807e487a2ed20ff6387f40c89db47d)
1 /*	$NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)xdr.c 1.35 87/08/12";
34 static char *sccsid = "@(#)xdr.c	2.1 88/07/29 4.0 RPCSRC";
35 #endif
36 #include <sys/cdefs.h>
37 /*
38  * xdr.c, Generic XDR routines implementation.
39  *
40  * Copyright (C) 1986, Sun Microsystems, Inc.
41  *
42  * These are the "generic" xdr routines used to serialize and de-serialize
43  * most common data items.  See xdr.h for more info on the interface to
44  * xdr.
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 
53 #include <rpc/rpc.h>
54 #include <rpc/rpc_com.h>
55 #include <rpc/types.h>
56 #include <rpc/xdr.h>
57 
58 typedef quad_t          longlong_t;     /* ANSI long long type */
59 typedef u_quad_t        u_longlong_t;   /* ANSI unsigned long long type */
60 
61 /*
62  * constants specific to the xdr "protocol"
63  */
64 #define XDR_FALSE	((long) 0)
65 #define XDR_TRUE	((long) 1)
66 
67 MALLOC_DEFINE(M_RPC, "rpc", "Remote Procedure Call");
68 
69 /*
70  * for unit alignment
71  */
72 static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
73 
74 /*
75  * Free a data structure using XDR
76  * Not a filter, but a convenient utility nonetheless
77  */
78 void
xdr_free(xdrproc_t proc,void * objp)79 xdr_free(xdrproc_t proc, void *objp)
80 {
81 	XDR x;
82 
83 	x.x_op = XDR_FREE;
84 	(*proc)(&x, objp);
85 }
86 
87 /*
88  * XDR nothing
89  */
90 bool_t
xdr_void(XDR * xrds __unused,void * ptr __unused)91 xdr_void(XDR *xrds __unused, void *ptr __unused)
92 {
93 	return (TRUE);
94 }
95 
96 /*
97  * XDR integers
98  */
99 bool_t
xdr_int(XDR * xdrs,int * ip)100 xdr_int(XDR *xdrs, int *ip)
101 {
102 	long l;
103 
104 	switch (xdrs->x_op) {
105 	case XDR_ENCODE:
106 		l = (long) *ip;
107 		return (XDR_PUTLONG(xdrs, &l));
108 
109 	case XDR_DECODE:
110 		if (!XDR_GETLONG(xdrs, &l)) {
111 			return (FALSE);
112 		}
113 		*ip = (int) l;
114 		return (TRUE);
115 
116 	case XDR_FREE:
117 		return (TRUE);
118 	}
119 	/* NOTREACHED */
120 	return (FALSE);
121 }
122 
123 /*
124  * XDR unsigned integers
125  */
126 bool_t
xdr_u_int(XDR * xdrs,u_int * up)127 xdr_u_int(XDR *xdrs, u_int *up)
128 {
129 	u_long l;
130 
131 	switch (xdrs->x_op) {
132 	case XDR_ENCODE:
133 		l = (u_long) *up;
134 		return (XDR_PUTLONG(xdrs, (long *)&l));
135 
136 	case XDR_DECODE:
137 		if (!XDR_GETLONG(xdrs, (long *)&l)) {
138 			return (FALSE);
139 		}
140 		*up = (u_int) l;
141 		return (TRUE);
142 
143 	case XDR_FREE:
144 		return (TRUE);
145 	}
146 	/* NOTREACHED */
147 	return (FALSE);
148 }
149 
150 /*
151  * XDR long integers
152  * same as xdr_u_long - open coded to save a proc call!
153  */
154 bool_t
xdr_long(XDR * xdrs,long * lp)155 xdr_long(XDR *xdrs, long *lp)
156 {
157 	switch (xdrs->x_op) {
158 	case XDR_ENCODE:
159 		return (XDR_PUTLONG(xdrs, lp));
160 	case XDR_DECODE:
161 		return (XDR_GETLONG(xdrs, lp));
162 	case XDR_FREE:
163 		return (TRUE);
164 	}
165 	/* NOTREACHED */
166 	return (FALSE);
167 }
168 
169 /*
170  * XDR unsigned long integers
171  * same as xdr_long - open coded to save a proc call!
172  */
173 bool_t
xdr_u_long(XDR * xdrs,u_long * ulp)174 xdr_u_long(XDR *xdrs, u_long *ulp)
175 {
176 	switch (xdrs->x_op) {
177 	case XDR_ENCODE:
178 		return (XDR_PUTLONG(xdrs, (long *)ulp));
179 	case XDR_DECODE:
180 		return (XDR_GETLONG(xdrs, (long *)ulp));
181 	case XDR_FREE:
182 		return (TRUE);
183 	}
184 	/* NOTREACHED */
185 	return (FALSE);
186 }
187 
188 /*
189  * XDR 32-bit integers
190  * same as xdr_uint32_t - open coded to save a proc call!
191  */
192 bool_t
xdr_int32_t(XDR * xdrs,int32_t * int32_p)193 xdr_int32_t(XDR *xdrs, int32_t *int32_p)
194 {
195 	long l;
196 
197 	switch (xdrs->x_op) {
198 	case XDR_ENCODE:
199 		l = (long) *int32_p;
200 		return (XDR_PUTLONG(xdrs, &l));
201 
202 	case XDR_DECODE:
203 		if (!XDR_GETLONG(xdrs, &l)) {
204 			return (FALSE);
205 		}
206 		*int32_p = (int32_t) l;
207 		return (TRUE);
208 
209 	case XDR_FREE:
210 		return (TRUE);
211 	}
212 	/* NOTREACHED */
213 	return (FALSE);
214 }
215 
216 /*
217  * XDR unsigned 32-bit integers
218  * same as xdr_int32_t - open coded to save a proc call!
219  */
220 bool_t
xdr_uint32_t(XDR * xdrs,uint32_t * uint32_p)221 xdr_uint32_t(XDR *xdrs, uint32_t *uint32_p)
222 {
223 	u_long l;
224 
225 	switch (xdrs->x_op) {
226 	case XDR_ENCODE:
227 		l = (u_long) *uint32_p;
228 		return (XDR_PUTLONG(xdrs, (long *)&l));
229 
230 	case XDR_DECODE:
231 		if (!XDR_GETLONG(xdrs, (long *)&l)) {
232 			return (FALSE);
233 		}
234 		*uint32_p = (uint32_t) l;
235 		return (TRUE);
236 
237 	case XDR_FREE:
238 		return (TRUE);
239 	}
240 	/* NOTREACHED */
241 	return (FALSE);
242 }
243 
244 /*
245  * XDR short integers
246  */
247 bool_t
xdr_short(XDR * xdrs,short * sp)248 xdr_short(XDR *xdrs, short *sp)
249 {
250 	long l;
251 
252 	switch (xdrs->x_op) {
253 	case XDR_ENCODE:
254 		l = (long) *sp;
255 		return (XDR_PUTLONG(xdrs, &l));
256 
257 	case XDR_DECODE:
258 		if (!XDR_GETLONG(xdrs, &l)) {
259 			return (FALSE);
260 		}
261 		*sp = (short) l;
262 		return (TRUE);
263 
264 	case XDR_FREE:
265 		return (TRUE);
266 	}
267 	/* NOTREACHED */
268 	return (FALSE);
269 }
270 
271 /*
272  * XDR unsigned short integers
273  */
274 bool_t
xdr_u_short(XDR * xdrs,u_short * usp)275 xdr_u_short(XDR *xdrs, u_short *usp)
276 {
277 	u_long l;
278 
279 	switch (xdrs->x_op) {
280 	case XDR_ENCODE:
281 		l = (u_long) *usp;
282 		return (XDR_PUTLONG(xdrs, (long *)&l));
283 
284 	case XDR_DECODE:
285 		if (!XDR_GETLONG(xdrs, (long *)&l)) {
286 			return (FALSE);
287 		}
288 		*usp = (u_short) l;
289 		return (TRUE);
290 
291 	case XDR_FREE:
292 		return (TRUE);
293 	}
294 	/* NOTREACHED */
295 	return (FALSE);
296 }
297 
298 /*
299  * XDR 16-bit integers
300  */
301 bool_t
xdr_int16_t(XDR * xdrs,int16_t * int16_p)302 xdr_int16_t(XDR *xdrs, int16_t *int16_p)
303 {
304 	long l;
305 
306 	switch (xdrs->x_op) {
307 	case XDR_ENCODE:
308 		l = (long) *int16_p;
309 		return (XDR_PUTLONG(xdrs, &l));
310 
311 	case XDR_DECODE:
312 		if (!XDR_GETLONG(xdrs, &l)) {
313 			return (FALSE);
314 		}
315 		*int16_p = (int16_t) l;
316 		return (TRUE);
317 
318 	case XDR_FREE:
319 		return (TRUE);
320 	}
321 	/* NOTREACHED */
322 	return (FALSE);
323 }
324 
325 /*
326  * XDR unsigned 16-bit integers
327  */
328 bool_t
xdr_uint16_t(XDR * xdrs,uint16_t * uint16_p)329 xdr_uint16_t(XDR *xdrs, uint16_t *uint16_p)
330 {
331 	u_long l;
332 
333 	switch (xdrs->x_op) {
334 	case XDR_ENCODE:
335 		l = (u_long) *uint16_p;
336 		return (XDR_PUTLONG(xdrs, (long *)&l));
337 
338 	case XDR_DECODE:
339 		if (!XDR_GETLONG(xdrs, (long *)&l)) {
340 			return (FALSE);
341 		}
342 		*uint16_p = (uint16_t) l;
343 		return (TRUE);
344 
345 	case XDR_FREE:
346 		return (TRUE);
347 	}
348 	/* NOTREACHED */
349 	return (FALSE);
350 }
351 
352 /*
353  * XDR a char
354  */
355 bool_t
xdr_char(XDR * xdrs,char * cp)356 xdr_char(XDR *xdrs, char *cp)
357 {
358 	u_int i;
359 
360 	i = *((unsigned char *)cp);
361 	if (!xdr_u_int(xdrs, &i)) {
362 		return (FALSE);
363 	}
364 	*((unsigned char *)cp) = i;
365 	return (TRUE);
366 }
367 
368 /*
369  * XDR an unsigned char
370  */
371 bool_t
xdr_u_char(XDR * xdrs,u_char * cp)372 xdr_u_char(XDR *xdrs, u_char *cp)
373 {
374 	u_int u;
375 
376 	u = (*cp);
377 	if (!xdr_u_int(xdrs, &u)) {
378 		return (FALSE);
379 	}
380 	*cp = u;
381 	return (TRUE);
382 }
383 
384 /*
385  * XDR booleans
386  */
387 bool_t
xdr_bool(XDR * xdrs,bool_t * bp)388 xdr_bool(XDR *xdrs, bool_t *bp)
389 {
390 	long lb;
391 
392 	switch (xdrs->x_op) {
393 	case XDR_ENCODE:
394 		lb = *bp ? XDR_TRUE : XDR_FALSE;
395 		return (XDR_PUTLONG(xdrs, &lb));
396 
397 	case XDR_DECODE:
398 		if (!XDR_GETLONG(xdrs, &lb)) {
399 			return (FALSE);
400 		}
401 		*bp = (lb == XDR_FALSE) ? FALSE : TRUE;
402 		return (TRUE);
403 
404 	case XDR_FREE:
405 		return (TRUE);
406 	}
407 	/* NOTREACHED */
408 	return (FALSE);
409 }
410 
411 /*
412  * XDR enumerations
413  */
414 bool_t
xdr_enum(XDR * xdrs,enum_t * ep)415 xdr_enum(XDR *xdrs, enum_t *ep)
416 {
417 	enum sizecheck { SIZEVAL };	/* used to find the size of an enum */
418 
419 	/*
420 	 * enums are treated as ints
421 	 */
422 	/* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
423 		return (xdr_long(xdrs, (long *)(void *)ep));
424 	} else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
425 		return (xdr_int(xdrs, (int *)(void *)ep));
426 	} else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
427 		return (xdr_short(xdrs, (short *)(void *)ep));
428 	} else {
429 		return (FALSE);
430 	}
431 }
432 
433 /*
434  * XDR opaque data
435  * Allows the specification of a fixed size sequence of opaque bytes.
436  * cp points to the opaque object and cnt gives the byte length.
437  */
438 bool_t
xdr_opaque(XDR * xdrs,caddr_t cp,u_int cnt)439 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
440 {
441 	u_int rndup;
442 	static int crud[BYTES_PER_XDR_UNIT];
443 
444 	/*
445 	 * if no data we are done
446 	 */
447 	if (cnt == 0)
448 		return (TRUE);
449 
450 	/*
451 	 * round byte count to full xdr units
452 	 */
453 	rndup = cnt % BYTES_PER_XDR_UNIT;
454 	if (rndup > 0)
455 		rndup = BYTES_PER_XDR_UNIT - rndup;
456 
457 	if (xdrs->x_op == XDR_DECODE) {
458 		if (!XDR_GETBYTES(xdrs, cp, cnt)) {
459 			return (FALSE);
460 		}
461 		if (rndup == 0)
462 			return (TRUE);
463 		return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
464 	}
465 
466 	if (xdrs->x_op == XDR_ENCODE) {
467 		if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
468 			return (FALSE);
469 		}
470 		if (rndup == 0)
471 			return (TRUE);
472 		return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
473 	}
474 
475 	if (xdrs->x_op == XDR_FREE) {
476 		return (TRUE);
477 	}
478 
479 	return (FALSE);
480 }
481 
482 /*
483  * XDR counted bytes
484  * *cpp is a pointer to the bytes, *sizep is the count.
485  * If *cpp is NULL maxsize bytes are allocated
486  */
487 bool_t
xdr_bytes(XDR * xdrs,char ** cpp,u_int * sizep,u_int maxsize)488 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
489 {
490 	char *sp = *cpp;  /* sp is the actual string pointer */
491 	u_int nodesize;
492 	bool_t ret, allocated = FALSE;
493 
494 	/*
495 	 * first deal with the length since xdr bytes are counted
496 	 */
497 	if (! xdr_u_int(xdrs, sizep)) {
498 		return (FALSE);
499 	}
500 	nodesize = *sizep;
501 	if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
502 		return (FALSE);
503 	}
504 
505 	/*
506 	 * now deal with the actual bytes
507 	 */
508 	switch (xdrs->x_op) {
509 	case XDR_DECODE:
510 		if (nodesize == 0) {
511 			return (TRUE);
512 		}
513 		if (sp == NULL) {
514 			*cpp = sp = mem_alloc(nodesize);
515 			allocated = TRUE;
516 		}
517 		if (sp == NULL) {
518 			printf("xdr_bytes: out of memory");
519 			return (FALSE);
520 		}
521 		/* FALLTHROUGH */
522 
523 	case XDR_ENCODE:
524 		ret = xdr_opaque(xdrs, sp, nodesize);
525 		if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
526 			if (allocated == TRUE) {
527 				mem_free(sp, nodesize);
528 				*cpp = NULL;
529 			}
530 		}
531 		return (ret);
532 
533 	case XDR_FREE:
534 		if (sp != NULL) {
535 			mem_free(sp, nodesize);
536 			*cpp = NULL;
537 		}
538 		return (TRUE);
539 	}
540 	/* NOTREACHED */
541 	return (FALSE);
542 }
543 
544 /*
545  * Implemented here due to commonality of the object.
546  */
547 bool_t
xdr_netobj(XDR * xdrs,struct netobj * np)548 xdr_netobj(XDR *xdrs, struct netobj *np)
549 {
550 
551 	return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
552 }
553 
554 /*
555  * XDR a descriminated union
556  * Support routine for discriminated unions.
557  * You create an array of xdrdiscrim structures, terminated with
558  * an entry with a null procedure pointer.  The routine gets
559  * the discriminant value and then searches the array of xdrdiscrims
560  * looking for that value.  It calls the procedure given in the xdrdiscrim
561  * to handle the discriminant.  If there is no specific routine a default
562  * routine may be called.
563  * If there is no specific or default routine an error is returned.
564  */
565 bool_t
xdr_union(XDR * xdrs,enum_t * dscmp,char * unp,const struct xdr_discrim * choices,xdrproc_t dfault)566 xdr_union(XDR *xdrs,
567     enum_t *dscmp,		/* enum to decide which arm to work on */
568     char *unp,				/* the union itself */
569     const struct xdr_discrim *choices,	/* [value, xdr proc] for each arm */
570     xdrproc_t dfault)			/* default xdr routine */
571 {
572 	enum_t dscm;
573 
574 	/*
575 	 * we deal with the discriminator;  it's an enum
576 	 */
577 	if (! xdr_enum(xdrs, dscmp)) {
578 		return (FALSE);
579 	}
580 	dscm = *dscmp;
581 
582 	/*
583 	 * search choices for a value that matches the discriminator.
584 	 * if we find one, execute the xdr routine for that value.
585 	 */
586 	for (; choices->proc != NULL_xdrproc_t; choices++) {
587 		if (choices->value == dscm)
588 			return ((*(choices->proc))(xdrs, unp));
589 	}
590 
591 	/*
592 	 * no match - execute the default xdr routine if there is one
593 	 */
594 	return ((dfault == NULL_xdrproc_t) ? FALSE :
595 	    (*dfault)(xdrs, unp));
596 }
597 
598 /*
599  * Non-portable xdr primitives.
600  * Care should be taken when moving these routines to new architectures.
601  */
602 
603 /*
604  * XDR null terminated ASCII strings
605  * xdr_string deals with "C strings" - arrays of bytes that are
606  * terminated by a NULL character.  The parameter cpp references a
607  * pointer to storage; If the pointer is null, then the necessary
608  * storage is allocated.  The last parameter is the max allowed length
609  * of the string as specified by a protocol.
610  */
611 bool_t
xdr_string(XDR * xdrs,char ** cpp,u_int maxsize)612 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
613 {
614 	char *sp = *cpp;  /* sp is the actual string pointer */
615 	u_int size;
616 	u_int nodesize;
617 	bool_t ret, allocated = FALSE;
618 
619 	/*
620 	 * first deal with the length since xdr strings are counted-strings
621 	 */
622 	switch (xdrs->x_op) {
623 	case XDR_FREE:
624 		if (sp == NULL) {
625 			return(TRUE);	/* already free */
626 		}
627 		/* FALLTHROUGH */
628 	case XDR_ENCODE:
629 		size = strlen(sp);
630 		break;
631 	case XDR_DECODE:
632 		break;
633 	}
634 	if (! xdr_u_int(xdrs, &size)) {
635 		return (FALSE);
636 	}
637 	if (size > maxsize) {
638 		return (FALSE);
639 	}
640 	nodesize = size + 1;
641 
642 	/*
643 	 * now deal with the actual bytes
644 	 */
645 	switch (xdrs->x_op) {
646 	case XDR_DECODE:
647 		if (nodesize == 0) {
648 			return (TRUE);
649 		}
650 		if (sp == NULL) {
651 			*cpp = sp = mem_alloc(nodesize);
652 			allocated = TRUE;
653 		}
654 		if (sp == NULL) {
655 			printf("xdr_string: out of memory");
656 			return (FALSE);
657 		}
658 		sp[size] = 0;
659 		/* FALLTHROUGH */
660 
661 	case XDR_ENCODE:
662 		ret = xdr_opaque(xdrs, sp, size);
663 		if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
664 			if (allocated == TRUE) {
665 				mem_free(sp, nodesize);
666 				*cpp = NULL;
667 			}
668 		}
669 		return (ret);
670 
671 	case XDR_FREE:
672 		mem_free(sp, nodesize);
673 		*cpp = NULL;
674 		return (TRUE);
675 	}
676 	/* NOTREACHED */
677 	return (FALSE);
678 }
679 
680 /*
681  * Wrapper for xdr_string that can be called directly from
682  * routines like clnt_call
683  */
684 bool_t
xdr_wrapstring(XDR * xdrs,char ** cpp)685 xdr_wrapstring(XDR *xdrs, char **cpp)
686 {
687 	return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
688 }
689 
690 /*
691  * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
692  * are in the "non-portable" section because they require that a `long long'
693  * be a 64-bit type.
694  *
695  *	--thorpej@netbsd.org, November 30, 1999
696  */
697 
698 /*
699  * XDR 64-bit integers
700  */
701 bool_t
xdr_int64_t(XDR * xdrs,int64_t * llp)702 xdr_int64_t(XDR *xdrs, int64_t *llp)
703 {
704 	u_long ul[2];
705 
706 	switch (xdrs->x_op) {
707 	case XDR_ENCODE:
708 		ul[0] = (u_long)((uint64_t)*llp >> 32) & 0xffffffff;
709 		ul[1] = (u_long)((uint64_t)*llp) & 0xffffffff;
710 		if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
711 			return (FALSE);
712 		return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
713 	case XDR_DECODE:
714 		if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
715 			return (FALSE);
716 		if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
717 			return (FALSE);
718 		*llp = (int64_t)
719 		    (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
720 		return (TRUE);
721 	case XDR_FREE:
722 		return (TRUE);
723 	}
724 	/* NOTREACHED */
725 	return (FALSE);
726 }
727 
728 /*
729  * XDR unsigned 64-bit integers
730  */
731 bool_t
xdr_uint64_t(XDR * xdrs,uint64_t * ullp)732 xdr_uint64_t(XDR *xdrs, uint64_t *ullp)
733 {
734 	u_long ul[2];
735 
736 	switch (xdrs->x_op) {
737 	case XDR_ENCODE:
738 		ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
739 		ul[1] = (u_long)(*ullp) & 0xffffffff;
740 		if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
741 			return (FALSE);
742 		return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
743 	case XDR_DECODE:
744 		if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
745 			return (FALSE);
746 		if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
747 			return (FALSE);
748 		*ullp = (uint64_t)
749 		    (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
750 		return (TRUE);
751 	case XDR_FREE:
752 		return (TRUE);
753 	}
754 	/* NOTREACHED */
755 	return (FALSE);
756 }
757 
758 /*
759  * XDR hypers
760  */
761 bool_t
xdr_hyper(XDR * xdrs,longlong_t * llp)762 xdr_hyper(XDR *xdrs, longlong_t *llp)
763 {
764 
765 	/*
766 	 * Don't bother open-coding this; it's a fair amount of code.  Just
767 	 * call xdr_int64_t().
768 	 */
769 	return (xdr_int64_t(xdrs, (int64_t *)llp));
770 }
771 
772 /*
773  * XDR unsigned hypers
774  */
775 bool_t
xdr_u_hyper(XDR * xdrs,u_longlong_t * ullp)776 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
777 {
778 
779 	/*
780 	 * Don't bother open-coding this; it's a fair amount of code.  Just
781 	 * call xdr_uint64_t().
782 	 */
783 	return (xdr_uint64_t(xdrs, (uint64_t *)ullp));
784 }
785 
786 /*
787  * XDR longlong_t's
788  */
789 bool_t
xdr_longlong_t(XDR * xdrs,longlong_t * llp)790 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
791 {
792 
793 	/*
794 	 * Don't bother open-coding this; it's a fair amount of code.  Just
795 	 * call xdr_int64_t().
796 	 */
797 	return (xdr_int64_t(xdrs, (int64_t *)llp));
798 }
799 
800 /*
801  * XDR u_longlong_t's
802  */
803 bool_t
xdr_u_longlong_t(XDR * xdrs,u_longlong_t * ullp)804 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
805 {
806 
807 	/*
808 	 * Don't bother open-coding this; it's a fair amount of code.  Just
809 	 * call xdr_uint64_t().
810 	 */
811 	return (xdr_uint64_t(xdrs, (uint64_t *)ullp));
812 }
813 
814 /*
815  * Kernel module glue
816  */
817 static int
xdr_modevent(module_t mod,int type,void * data)818 xdr_modevent(module_t mod, int type, void *data)
819 {
820 
821         return (0);
822 }
823 static moduledata_t xdr_mod = {
824         "xdr",
825         xdr_modevent,
826         NULL,
827 };
828 DECLARE_MODULE(xdr, xdr_mod, SI_SUB_VFS, SI_ORDER_ANY);
829 MODULE_VERSION(xdr, 1);
830