Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / lib / libc / xdr / xdr.3
1 .\" @(#)xdr.3n  2.2 88/08/03 4.0 RPCSRC; from 1.16 88/03/14 SMI
2 .\" $FreeBSD: src/lib/libc/xdr/xdr.3,v 1.17 2005/11/24 07:12:01 ru Exp $
3 .\" $DragonFly: src/lib/libc/xdr/xdr.3,v 1.3 2007/11/23 23:16:36 swildner Exp $
4 .\"
5 .Dd February 16, 1988
6 .Dt XDR 3
7 .Os
8 .Sh NAME
9 .Nm xdr ,
10 .Nm xdr_array ,
11 .Nm xdr_bool ,
12 .Nm xdr_bytes ,
13 .Nm xdr_char ,
14 .Nm xdr_destroy ,
15 .Nm xdr_double ,
16 .Nm xdr_enum ,
17 .Nm xdr_float ,
18 .Nm xdr_free ,
19 .Nm xdr_getpos ,
20 .Nm xdr_hyper ,
21 .Nm xdr_inline ,
22 .Nm xdr_int ,
23 .Nm xdr_long ,
24 .Nm xdr_longlong_t ,
25 .Nm xdrmem_create ,
26 .Nm xdr_opaque ,
27 .Nm xdr_pointer ,
28 .Nm xdrrec_create ,
29 .Nm xdrrec_endofrecord ,
30 .Nm xdrrec_eof ,
31 .Nm xdrrec_skiprecord ,
32 .Nm xdr_reference ,
33 .Nm xdr_setpos ,
34 .Nm xdr_short ,
35 .Nm xdrstdio_create ,
36 .Nm xdr_string ,
37 .Nm xdr_u_char ,
38 .Nm xdr_u_hyper ,
39 .Nm xdr_u_int ,
40 .Nm xdr_u_long ,
41 .Nm xdr_u_longlong_t ,
42 .Nm xdr_u_short ,
43 .Nm xdr_union ,
44 .Nm xdr_vector ,
45 .Nm xdr_void ,
46 .Nm xdr_wrapstring
47 .Nd "library routines for external data representation"
48 .Sh LIBRARY
49 .Lb libc
50 .Sh SYNOPSIS
51 .In rpc/types.h
52 .In rpc/xdr.h
53 .Pp
54 See
55 .Sx DESCRIPTION
56 for function declarations.
57 .Sh DESCRIPTION
58 These routines allow C programmers to describe
59 arbitrary data structures in a machine-independent fashion.
60 Data for remote procedure calls are transmitted using these
61 routines.
62 .Pp
63 .Bl -tag -width indent -compact
64 .It Xo
65 .Ft int
66 .Xc
67 .It Xo
68 .Fo xdr_array
69 .Fa "XDR *xdrs"
70 .Fa "char **arrp"
71 .Fa "u_int *sizep"
72 .Fa "u_int maxsize"
73 .Fa "u_int elsize"
74 .Fa "xdrproc_t elproc"
75 .Fc
76 .Xc
77 .Pp
78 A filter primitive that translates between variable-length
79 arrays
80 and their corresponding external representations.
81 The
82 .Fa arrp
83 argument
84 is the address of the pointer to the array, while
85 .Fa sizep
86 is the address of the element count of the array;
87 this element count cannot exceed
88 .Fa maxsize .
89 The
90 .Fa elsize
91 argument
92 is the
93 .Ic sizeof
94 each of the array's elements, and
95 .Fa elproc
96 is an
97 .Tn XDR
98 filter that translates between
99 the array elements' C form, and their external
100 representation.
101 This routine returns one if it succeeds, zero otherwise.
102 .Pp
103 .It Xo
104 .Ft int
105 .Xc
106 .It Xo
107 .Fn xdr_bool "XDR *xdrs" "bool_t *bp"
108 .Xc
109 .Pp
110 A filter primitive that translates between booleans (C
111 integers)
112 and their external representations.
113 When encoding data, this
114 filter produces values of either one or zero.
115 This routine returns one if it succeeds, zero otherwise.
116 .Pp
117 .It Xo
118 .Ft int
119 .Xc
120 .It Xo
121 .Fn xdr_bytes "XDR *xdrs" "char **sp" "u_int *sizep" "u_int maxsize"
122 .Xc
123 .Pp
124 A filter primitive that translates between counted byte
125 strings and their external representations.
126 The
127 .Fa sp
128 argument
129 is the address of the string pointer.
130 The length of the
131 string is located at address
132 .Fa sizep ;
133 strings cannot be longer than
134 .Fa maxsize .
135 This routine returns one if it succeeds, zero otherwise.
136 .Pp
137 .It Xo
138 .Ft int
139 .Xc
140 .It Xo
141 .Fn xdr_char "XDR *xdrs" "char *cp"
142 .Xc
143 .Pp
144 A filter primitive that translates between C characters
145 and their external representations.
146 This routine returns one if it succeeds, zero otherwise.
147 Note: encoded characters are not packed, and occupy 4 bytes
148 each.
149 For arrays of characters, it is worthwhile to
150 consider
151 .Fn xdr_bytes ,
152 .Fn xdr_opaque
153 or
154 .Fn xdr_string .
155 .Pp
156 .It Xo
157 .Ft void
158 .Xc
159 .It Xo
160 .Fn xdr_destroy "XDR *xdrs"
161 .Xc
162 .Pp
163 A macro that invokes the destroy routine associated with the
164 .Tn XDR
165 stream,
166 .Fa xdrs .
167 Destruction usually involves freeing private data structures
168 associated with the stream.
169 Using
170 .Fa xdrs
171 after invoking
172 .Fn xdr_destroy
173 is undefined.
174 .Pp
175 .It Xo
176 .Ft int
177 .Xc
178 .It Xo
179 .Fn xdr_double "XDR *xdrs" "double *dp"
180 .Xc
181 .Pp
182 A filter primitive that translates between C
183 .Vt double
184 precision numbers and their external representations.
185 This routine returns one if it succeeds, zero otherwise.
186 .Pp
187 .It Xo
188 .Ft int
189 .Xc
190 .It Xo
191 .Fn xdr_enum "XDR *xdrs" "enum_t *ep"
192 .Xc
193 .Pp
194 A filter primitive that translates between C
195 .Vt enum Ns s
196 (actually integers) and their external representations.
197 This routine returns one if it succeeds, zero otherwise.
198 .Pp
199 .It Xo
200 .Ft int
201 .Xc
202 .It Xo
203 .Fn xdr_float "XDR *xdrs" "float *fp"
204 .Xc
205 .Pp
206 A filter primitive that translates between C
207 .Vt float Ns s
208 and their external representations.
209 This routine returns one if it succeeds, zero otherwise.
210 .Pp
211 .It Xo
212 .Ft void
213 .Xc
214 .It Xo
215 .Fn xdr_free "xdrproc_t proc" "void *objp"
216 .Xc
217 .Pp
218 Generic freeing routine.
219 The first argument is the
220 .Tn XDR
221 routine for the object being freed.
222 The second argument
223 is a pointer to the object itself.
224 Note: the pointer passed
225 to this routine is
226 .Em not
227 freed, but what it points to
228 .Em is
229 freed (recursively).
230 .Pp
231 .It Xo
232 .Ft u_int
233 .Xc
234 .It Xo
235 .Fn xdr_getpos "XDR *xdrs"
236 .Xc
237 .Pp
238 A macro that invokes the get\-position routine
239 associated with the
240 .Tn XDR
241 stream,
242 .Fa xdrs .
243 The routine returns an unsigned integer,
244 which indicates the position of the
245 .Tn XDR
246 byte stream.
247 A desirable feature of
248 .Tn XDR
249 streams is that simple arithmetic works with this number,
250 although the
251 .Tn XDR
252 stream instances need not guarantee this.
253 .Pp
254 .It Xo
255 .Ft int
256 .Xc
257 .It Xo
258 .Fn xdr_hyper "XDR *xdrs" "quad_t *llp"
259 .Xc
260 A filter primitive that translates between ANSI C
261 .Vt "long long"
262 integers and their external representations.
263 This routine returns one if it succeeds, zero otherwise.
264 .Pp
265 .It Xo
266 .Ft "long *"
267 .Xc
268 .It Xo
269 .Fn xdr_inline "XDR *xdrs" "int len"
270 .Xc
271 .Pp
272 A macro that invokes the in-line routine associated with the
273 .Tn XDR
274 stream,
275 .Fa xdrs .
276 The routine returns a pointer
277 to a contiguous piece of the stream's buffer;
278 .Fa len
279 is the byte length of the desired buffer.
280 Note: pointer is cast to
281 .Vt "long *" .
282 .Pp
283 Warning:
284 .Fn xdr_inline
285 may return
286 .Dv NULL
287 (0)
288 if it cannot allocate a contiguous piece of a buffer.
289 Therefore the behavior may vary among stream instances;
290 it exists for the sake of efficiency.
291 .Pp
292 .It Xo
293 .Ft int
294 .Xc
295 .It Xo
296 .Fn xdr_int "XDR *xdrs" "int *ip"
297 .Xc
298 .Pp
299 A filter primitive that translates between C integers
300 and their external representations.
301 This routine returns one if it succeeds, zero otherwise.
302 .Pp
303 .It Xo
304 .Ft int
305 .Xc
306 .It Xo
307 .Fn xdr_long "XDR *xdrs" "long *lp"
308 .Xc
309 .Pp
310 A filter primitive that translates between C
311 .Vt long
312 integers and their external representations.
313 This routine returns one if it succeeds, zero otherwise.
314 .Pp
315 .It Xo
316 .Ft int
317 .Xc
318 .It Xo
319 .Fn xdr_longlong_t "XDR *xdrs" "quad_t *llp"
320 .Xc
321 A filter primitive that translates between ANSI C
322 .Vt "long long"
323 integers and their external representations.
324 This routine returns one if it succeeds, zero otherwise.
325 .Pp
326 .It Xo
327 .Ft void
328 .Xc
329 .It Xo
330 .Fn xdrmem_create "XDR *xdrs" "char *addr" "u_int size" "enum xdr_op op"
331 .Xc
332 .Pp
333 This routine initializes the
334 .Tn XDR
335 stream object pointed to by
336 .Fa xdrs .
337 The stream's data is written to, or read from,
338 a chunk of memory at location
339 .Fa addr
340 whose length is no more than
341 .Fa size
342 bytes long.
343 The
344 .Fa op
345 argument
346 determines the direction of the
347 .Tn XDR
348 stream
349 (either
350 .Dv XDR_ENCODE ,
351 .Dv XDR_DECODE ,
352 or
353 .Dv XDR_FREE ) .
354 .Pp
355 .It Xo
356 .Ft int
357 .Xc
358 .It Xo
359 .Fn xdr_opaque "XDR *xdrs" "char *cp" "u_int cnt"
360 .Xc
361 .Pp
362 A filter primitive that translates between fixed size opaque
363 data
364 and its external representation.
365 The
366 .Fa cp
367 argument
368 is the address of the opaque object, and
369 .Fa cnt
370 is its size in bytes.
371 This routine returns one if it succeeds, zero otherwise.
372 .Pp
373 .It Xo
374 .Ft int
375 .Xc
376 .It Xo
377 .Fn xdr_pointer "XDR *xdrs" "char **objpp" "u_int objsize" "xdrproc_t xdrobj"
378 .Xc
379 .Pp
380 Like
381 .Fn xdr_reference
382 except that it serializes
383 .Dv NULL
384 pointers, whereas
385 .Fn xdr_reference
386 does not.
387 Thus,
388 .Fn xdr_pointer
389 can represent
390 recursive data structures, such as binary trees or
391 linked lists.
392 .Pp
393 .It Xo
394 .Ft void
395 .Xc
396 .It Xo
397 .Fo xdrrec_create
398 .Fa "XDR *xdrs"
399 .Fa "u_int sendsize"
400 .Fa "u_int recvsize"
401 .Fa "void *handle"
402 .Fa "int \*(lp*readit\*(rp\*(lp\*(rp"
403 .Fa "int \*(lp*writeit\*(rp\*(lp\*(rp"
404 .Fc
405 .Xc
406 .Pp
407 This routine initializes the
408 .Tn XDR
409 stream object pointed to by
410 .Fa xdrs .
411 The stream's data is written to a buffer of size
412 .Fa sendsize ;
413 a value of zero indicates the system should use a suitable
414 default.
415 The stream's data is read from a buffer of size
416 .Fa recvsize ;
417 it too can be set to a suitable default by passing a zero
418 value.
419 When a stream's output buffer is full,
420 .Fn writeit
421 is called.
422 Similarly, when a stream's input buffer is empty,
423 .Fn readit
424 is called.
425 The behavior of these two routines is similar to
426 the
427 system calls
428 .Xr read 2
429 and
430 .Xr write 2 ,
431 except that
432 .Fa handle
433 is passed to the former routines as the first argument.
434 Note: the
435 .Tn XDR
436 stream's
437 .Fa op
438 field must be set by the caller.
439 .Pp
440 Warning: this
441 .Tn XDR
442 stream implements an intermediate record stream.
443 Therefore there are additional bytes in the stream
444 to provide record boundary information.
445 .Pp
446 .It Xo
447 .Ft int
448 .Xc
449 .It Xo
450 .Fn xdrrec_endofrecord "XDR *xdrs" "int sendnow"
451 .Xc
452 .Pp
453 This routine can be invoked only on
454 streams created by
455 .Fn xdrrec_create .
456 The data in the output buffer is marked as a completed
457 record,
458 and the output buffer is optionally written out if
459 .Fa sendnow
460 is non-zero.
461 This routine returns one if it succeeds, zero
462 otherwise.
463 .Pp
464 .It Xo
465 .Ft int
466 .Xc
467 .It Xo
468 .Fn xdrrec_eof "XDR *xdrs"
469 .Xc
470 .Pp
471 This routine can be invoked only on
472 streams created by
473 .Fn xdrrec_create .
474 After consuming the rest of the current record in the stream,
475 this routine returns one if the stream has no more input,
476 zero otherwise.
477 .Pp
478 .It Xo
479 .Ft int
480 .Xc
481 .It Xo
482 .Fn xdrrec_skiprecord "XDR *xdrs"
483 .Xc
484 .Pp
485 This routine can be invoked only on
486 streams created by
487 .Fn xdrrec_create .
488 It tells the
489 .Tn XDR
490 implementation that the rest of the current record
491 in the stream's input buffer should be discarded.
492 This routine returns one if it succeeds, zero otherwise.
493 .Pp
494 .It Xo
495 .Ft int
496 .Xc
497 .It Xo
498 .Fn xdr_reference "XDR *xdrs" "char **pp" "u_int size" "xdrproc_t proc"
499 .Xc
500 .Pp
501 A primitive that provides pointer chasing within structures.
502 The
503 .Fa pp
504 argument
505 is the address of the pointer;
506 .Fa size
507 is the
508 .Ic sizeof
509 the structure that
510 .Fa *pp
511 points to; and
512 .Fa proc
513 is an
514 .Tn XDR
515 procedure that filters the structure
516 between its C form and its external representation.
517 This routine returns one if it succeeds, zero otherwise.
518 .Pp
519 Warning: this routine does not understand
520 .Dv NULL
521 pointers.
522 Use
523 .Fn xdr_pointer
524 instead.
525 .Pp
526 .It Xo
527 .Ft int
528 .Xc
529 .It Xo
530 .Fn xdr_setpos "XDR *xdrs" "u_int pos"
531 .Xc
532 .Pp
533 A macro that invokes the set position routine associated with
534 the
535 .Tn XDR
536 stream
537 .Fa xdrs .
538 The
539 .Fa pos
540 argument
541 is a position value obtained from
542 .Fn xdr_getpos .
543 This routine returns one if the
544 .Tn XDR
545 stream could be repositioned,
546 and zero otherwise.
547 .Pp
548 Warning: it is difficult to reposition some types of
549 .Tn XDR
550 streams, so this routine may fail with one
551 type of stream and succeed with another.
552 .Pp
553 .It Xo
554 .Ft int
555 .Xc
556 .It Xo
557 .Fn xdr_short "XDR *xdrs" "short *sp"
558 .Xc
559 .Pp
560 A filter primitive that translates between C
561 .Vt short
562 integers and their external representations.
563 This routine returns one if it succeeds, zero otherwise.
564 .Pp
565 .It Li "#ifdef _STDIO_H_"
566 .It Li "/* XDR using stdio library */"
567 .It Xo
568 .Ft void
569 .Xc
570 .It Xo
571 .Fn xdrstdio_create "XDR *xdrs" "FILE *file" "enum xdr_op op"
572 .Xc
573 .It Li "#endif"
574 .Pp
575 This routine initializes the
576 .Tn XDR
577 stream object pointed to by
578 .Fa xdrs .
579 The
580 .Tn XDR
581 stream data is written to, or read from, the Standard
582 .Tn I/O
583 stream
584 .Fa file .
585 The
586 .Fa op
587 argument
588 determines the direction of the
589 .Tn XDR
590 stream (either
591 .Dv XDR_ENCODE ,
592 .Dv XDR_DECODE ,
593 or
594 .Dv XDR_FREE ) .
595 .Pp
596 Warning: the destroy routine associated with such
597 .Tn XDR
598 streams calls
599 .Xr fflush 3
600 on the
601 .Fa file
602 stream, but never
603 .Xr fclose 3 .
604 .Pp
605 .It Xo
606 .Ft int
607 .Xc
608 .It Xo
609 .Fn xdr_string "XDR *xdrs" "char **sp" "u_int maxsize"
610 .Xc
611 .Pp
612 A filter primitive that translates between C strings and
613 their
614 corresponding external representations.
615 Strings cannot be longer than
616 .Fa maxsize .
617 Note:
618 .Fa sp
619 is the address of the string's pointer.
620 This routine returns one if it succeeds, zero otherwise.
621 .Pp
622 .It Xo
623 .Ft int
624 .Xc
625 .It Xo
626 .Fn xdr_u_char "XDR *xdrs" "unsigned char *ucp"
627 .Xc
628 .Pp
629 A filter primitive that translates between
630 .Vt unsigned
631 C characters and their external representations.
632 This routine returns one if it succeeds, zero otherwise.
633 .Pp
634 .It Xo
635 .Ft int
636 .Xc
637 .It Xo
638 .Fn xdr_u_hyper "XDR *xdrs" "u_quad_t *ullp"
639 .Xc
640 A filter primitive that translates between
641 .Vt unsigned
642 ANSI C
643 .Vt long long
644 integers and their external representations.
645 This routine returns one if it succeeds, zero otherwise.
646 .Pp
647 .It Xo
648 .Ft int
649 .Xc
650 .It Xo
651 .Fn xdr_u_int "XDR *xdrs" "unsigned *up"
652 .Xc
653 .Pp
654 A filter primitive that translates between C
655 .Vt unsigned
656 integers and their external representations.
657 This routine returns one if it succeeds, zero otherwise.
658 .Pp
659 .It Xo
660 .Ft int
661 .Xc
662 .It Xo
663 .Fn xdr_u_long "XDR *xdrs" "unsigned long *ulp"
664 .Xc
665 .Pp
666 A filter primitive that translates between C
667 .Vt "unsigned long"
668 integers and their external representations.
669 This routine returns one if it succeeds, zero otherwise.
670 .Pp
671 .It Xo
672 .Ft int
673 .Xc
674 .It Xo
675 .Fn xdr_u_longlong_t "XDR *xdrs" "u_quad_t *ullp"
676 .Xc
677 A filter primitive that translates between
678 .Vt unsigned
679 ANSI C
680 .Vt "long long"
681 integers and their external representations.
682 This routine returns one if it succeeds, zero otherwise.
683 .Pp
684 .It Xo
685 .Ft int
686 .Xc
687 .It Xo
688 .Fn xdr_u_short "XDR *xdrs" "unsigned short *usp"
689 .Xc
690 .Pp
691 A filter primitive that translates between C
692 .Vt "unsigned short"
693 integers and their external representations.
694 This routine returns one if it succeeds, zero otherwise.
695 .Pp
696 .It Xo
697 .Ft int
698 .Xc
699 .It Xo
700 .Fo xdr_union
701 .Fa "XDR *xdrs"
702 .Fa "enum_t *dscmp"
703 .Fa "char *unp"
704 .Fa "const struct xdr_discrim *choices"
705 .Fa "xdrproc_t defaultarm"
706 .Fc
707 .Xc
708 .Pp
709 A filter primitive that translates between a discriminated C
710 .Vt union
711 and its corresponding external representation.
712 It first
713 translates the discriminant of the union located at
714 .Fa dscmp .
715 This discriminant is always an
716 .Vt enum_t .
717 Next the union located at
718 .Fa unp
719 is translated.
720 The
721 .Fa choices
722 argument
723 is a pointer to an array of
724 .Vt xdr_discrim
725 structures.
726 Each structure contains an ordered pair of
727 .Bq Va value , proc .
728 If the union's discriminant is equal to the associated
729 .Va value ,
730 then the
731 .Fn proc
732 is called to translate the union.
733 The end of the
734 .Vt xdr_discrim
735 structure array is denoted by a routine of value
736 .Dv NULL .
737 If the discriminant is not found in the
738 .Fa choices
739 array, then the
740 .Fn defaultarm
741 procedure is called (if it is not
742 .Dv NULL ) .
743 Returns one if it succeeds, zero otherwise.
744 .Pp
745 .It Xo
746 .Ft int
747 .Xc
748 .It Xo
749 .Fo xdr_vector
750 .Fa "XDR *xdrs"
751 .Fa "char *arrp"
752 .Fa "u_int size"
753 .Fa "u_int elsize"
754 .Fa "xdrproc_t elproc"
755 .Fc
756 .Xc
757 .Pp
758 A filter primitive that translates between fixed-length
759 arrays
760 and their corresponding external representations.
761 The
762 .Fa arrp
763 argument
764 is the address of the pointer to the array, while
765 .Fa size
766 is the element count of the array.
767 The
768 .Fa elsize
769 argument
770 is the
771 .Ic sizeof
772 each of the array's elements, and
773 .Fa elproc
774 is an
775 .Tn XDR
776 filter that translates between
777 the array elements' C form, and their external
778 representation.
779 This routine returns one if it succeeds, zero otherwise.
780 .Pp
781 .It Xo
782 .Ft int
783 .Xc
784 .It Xo
785 .Fn xdr_void void
786 .Xc
787 .Pp
788 This routine always returns one.
789 It may be passed to
790 .Tn RPC
791 routines that require a function argument,
792 where nothing is to be done.
793 .Pp
794 .It Xo
795 .Ft int
796 .Xc
797 .It Xo
798 .Fn xdr_wrapstring "XDR *xdrs" "char **sp"
799 .Xc
800 .Pp
801 A primitive that calls
802 .Fn xdr_string xdrs sp MAXUN.UNSIGNED ;
803 where
804 .Dv MAXUN.UNSIGNED
805 is the maximum value of an unsigned integer.
806 The
807 .Fn xdr_wrapstring
808 function
809 is handy because the
810 .Tn RPC
811 package passes a maximum of two
812 .Tn XDR
813 routines as arguments, and
814 .Fn xdr_string ,
815 one of the most frequently used primitives, requires three.
816 Returns one if it succeeds, zero otherwise.
817 .El
818 .Sh SEE ALSO
819 .Xr rpc 3
820 .Rs
821 .%T "eXternal Data Representation Standard: Protocol Specification"
822 .Re
823 .Rs
824 .%T "eXternal Data Representation: Sun Technical Notes"
825 .Re
826 .Rs
827 .%T "XDR: External Data Representation Standard"
828 .%O RFC 1014
829 .%Q "Sun Microsystems, Inc., USC\-ISI"
830 .Re