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