1.\" $OpenBSD: xdr.3,v 1.20 2008/09/07 14:59:41 marco Exp $ 2.\" Mostly converted to mandoc by Theo de Raadt, Tue Feb 24 04:04:46 MST 1998 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.Dd $Mdocdate: September 7 2008 $ 32.Dt XDR 3 33.Os 34.Sh NAME 35.Nm xdr_array , 36.Nm xdr_bool , 37.Nm xdr_bytes , 38.Nm xdr_char , 39.Nm xdr_destroy , 40.Nm xdr_double , 41.Nm xdr_enum , 42.Nm xdr_float , 43.Nm xdr_free , 44.Nm xdr_getpos , 45.Nm xdr_inline , 46.Nm xdr_int , 47.Nm xdr_long , 48.Nm xdr_int64_t , 49.Nm xdrmem_create , 50.Nm xdr_opaque , 51.Nm xdr_pointer , 52.Nm xdrrec_create , 53.Nm xdrrec_endofrecord , 54.Nm xdrrec_eof , 55.Nm xdrrec_skiprecord , 56.Nm xdr_reference , 57.Nm xdr_setpos , 58.Nm xdr_short , 59.Nm xdrstdio_create , 60.Nm xdr_string , 61.Nm xdr_u_char , 62.Nm xdr_u_int , 63.Nm xdr_u_long , 64.Nm xdr_u_int64_t , 65.Nm xdr_u_short , 66.Nm xdr_union , 67.Nm xdr_vector , 68.Nm xdr_void , 69.Nm xdr_wrapstring 70.Nd library routines for external data representation 71.Sh SYNOPSIS 72.Fd #include <sys/types.h> 73.Fd #include <rpc/xdr.h> 74.Ft int 75.Fn xdr_array "XDR *xdrs" "char **arrp" "u_int *sizep" "u_int maxsize" "u_int elsize" "xdrproc_t elproc" 76.Ft int 77.Fn xdr_bool "XDR *xdrs" "bool_t *bp" 78.Ft int 79.Fn xdr_bytes "XDR *xdrs" "char **sp" "u_int *sizep" "u_int maxsize" 80.Ft int 81.Fn xdr_char "XDR *xdrs" "char *cp" 82.Ft void 83.Fn xdr_destroy "XDR *xdrs" 84.Ft int 85.Fn xdr_double "XDR *xdrs" "double *dp" 86.Ft int 87.Fn xdr_enum "XDR *xdrs" "enum_t *ep" 88.Ft int 89.Fn xdr_float "XDR *xdrs" "float *fp" 90.Ft void 91.Fn xdr_free "xdrproc_t proc" "char *objp" 92.Ft u_int 93.Fn xdr_getpos "XDR *xdrs" 94.Ft long * 95.Fn xdr_inline "XDR *xdrs" "int len" 96.Ft int 97.Fn xdr_int "XDR *xdrs" "int *ip" 98.Ft int 99.Fn xdr_long "XDR *xdrs" "long *lp" 100.Ft int 101.Fn xdr_int64_t "XDR *xdrs" "int64_t *lp" 102.Ft void 103.Fn xdrmem_create "XDR *xdrs" "char *addr" "u_int size" "enum xdr_op op" 104.Ft int 105.Fn xdr_opaque "XDR *xdrs" "char *cp" "u_int cnt" 106.Ft int 107.Fn xdr_pointer "XDR *xdrs" "char **objpp" "u_int objsize" "xdrproc_t xdrobj" 108.Ft void 109.Fn xdrrec_create "XDR *xdrs" "u_int sendsize" "u_int recvsize" "char *handle" "int (*readit)()" "int (*writeit)()" 110.Ft int 111.Fn xdrrec_endofrecord "XDR *xdrs" "int sendnow" 112.Ft int 113.Fn xdrrec_eof "XDR *xdrs" 114.Ft int 115.Fn xdrrec_skiprecord "XDR *xdrs" 116.Ft int 117.Fn xdr_reference "XDR *xdrs" "char **pp" "u_int size" "xdrproc_t proc" 118.Ft int 119.Fn xdr_setpos "XDR *xdrs" "u_int pos" 120.Ft int 121.Fn xdr_short "XDR *xdrs" "short *sp" 122.Ft void 123.Fn xdrstdio_create "XDR *xdrs" "FILE *file" "enum xdr_op op" 124.Ft int 125.Fn xdr_string "XDR *xdrs" "char **sp" "u_int maxsize" 126.Ft int 127.Fn xdr_u_char "XDR *xdrs" "unsigned char *ucp" 128.Ft int 129.Fn xdr_u_int "XDR *xdrs" "unsigned int *up" 130.Ft int 131.Fn xdr_u_long "XDR *xdrs" "unsigned long *ulp" 132.Ft int 133.Fn xdr_u_int64_t "XDR *xdrs" "u_int64_t *ullp" 134.Ft int 135.Fn xdr_u_short "XDR *xdrs" "unsigned short *usp" 136.Ft int 137.Fn xdr_union "XDR *xdrs" "int *dscmp" "char *unp" "struct xdr_discrim *choices" "bool_t (*defaultarm)()" 138.Ft int 139.Fn xdr_vector "XDR *xdrs" "char *arrp" "u_int size" "u_int elsize" "xdrproc_t elproc" 140.Ft int 141.Fn xdr_void "void" 142.Ft int 143.Fn xdr_wrapstring "XDR *xdrs" "char **sp" 144.Sh DESCRIPTION 145These routines allow C programmers to describe 146arbitrary data structures in a machine-independent fashion. 147Data for remote procedure calls are transmitted using these 148routines. 149.Pp 150.Fn xdr_array 151is a filter primitive that translates between variable-length arrays 152and their corresponding external representations. 153The parameter 154.Fa arrp 155is the address of the pointer to the array, while 156.Fa sizep 157is the address of the element count of the array; 158this element count cannot exceed 159.Fa maxsize . 160The parameter 161.Fa elsize 162is the size of each of the array's elements, and 163.Fa elproc 164is an 165.Tn XDR 166filter that translates between 167the array elements' C form, and their external 168representation. 169This routine returns one if it succeeds, zero otherwise. 170.Pp 171.Fn xdr_bool 172is a filter primitive that translates between booleans (C integers) 173and their external representations. 174When encoding data, this 175filter produces values of either one or zero. 176This routine returns one if it succeeds, zero otherwise. 177.Pp 178.Fn xdr_bytes 179is a filter primitive that translates between counted byte 180strings and their external representations. 181The parameter 182.Fa sp 183is the address of the string pointer. 184The length of the string is located at address 185.Fa sizep ; 186strings cannot be longer than 187.Fa maxsize . 188This routine returns one if it succeeds, zero otherwise. 189.Pp 190.Fn xdr_char 191is a filter primitive that translates between C characters 192and their external representations. 193This routine returns one if it succeeds, zero otherwise. 194Note: encoded characters are not packed, and occupy 4 bytes each. 195For arrays of characters, it is worthwhile to consider 196.Fn xdr_bytes , 197.Fn xdr_opaque , 198or 199.Fn xdr_string . 200.Pp 201.Fn xdr_destroy 202is a macro that invokes the destroy routine associated with the 203.Tn XDR 204stream 205.Fa xdrs . 206Destruction usually involves freeing private data structures 207associated with the stream. 208Using 209.Fa xdrs 210after invoking 211.Fn xdr_destroy 212is undefined. 213.Pp 214.Fn xdr_double 215is a filter primitive that translates between C 216.Li double 217precision numbers and their external representations. 218This routine returns one if it succeeds, zero otherwise. 219.Pp 220.Fn xdr_enum 221is a filter primitive that translates between the C 222.Li enum 223type (actually an integer) and its external representations. 224This routine returns one if it succeeds, zero otherwise. 225.Pp 226.Fn xdr_float 227is a filter primitive that translates between the C 228.Li float 229type and its external representations. 230This routine returns one if it succeeds, zero otherwise. 231.Pp 232.Fn xdr_free 233is a generic freeing routine. 234The first argument is the 235.Tn XDR 236routine for the object being freed. 237The second argument 238is a pointer to the object itself. 239Note: the pointer passed to this routine is 240.Fa not 241freed, but what it points to is freed (recursively). 242.Pp 243.Fn xdr_getpos 244is a macro that invokes the get-position routine associated with the 245.Tn XDR 246stream 247.Fa xdrs . 248The routine returns an unsigned integer, 249which indicates the position of the 250.Tn XDR 251byte stream. 252A desirable feature of 253.Tn XDR 254streams is that simple arithmetic works with this number, 255although the 256.Tn XDR 257stream instances need not guarantee this. 258.Pp 259.Fn xdr_inline 260is a macro that invokes the in-line routine associated with the 261.Tn XDR 262stream 263.Fa xdrs . 264The routine returns a pointer 265to a contiguous piece of the stream's buffer; 266.Fa len 267is the byte length of the desired buffer. 268Note: pointer is cast to 269.Li long * . 270Warning: 271.Fn xdr_inline 272may return 273.Dv NULL 274if it cannot allocate a contiguous piece of a buffer. 275Therefore the behavior may vary among stream instances; 276it exists for the sake of efficiency. 277.Pp 278.Fn xdr_int 279is a filter primitive that translates between C integers 280and their external representations. 281This routine returns one if it succeeds, zero otherwise. 282.Pp 283.Fn xdr_long 284is a filter primitive that translates between C 285.Li long 286integers and their external representations. 287This routine returns one if it succeeds, zero otherwise. 288.Pp 289.Fn xdr_int64_t 290is a filter primitive that translates between C 291.Li int64_t 292integers and their external representations. 293This routine returns one if it succeeds, zero otherwise. 294.Pp 295.Fn xdrmem_create 296is a routine which initializes the 297.Tn XDR 298stream object pointed to by 299.Fa xdrs . 300The stream's data is written to, or read from, 301a chunk of memory at location 302.Fa addr 303whose length is no more than 304.Fa size 305bytes long. 306The 307.Fa op 308determines the direction of the 309.Tn XDR 310stream 311(either 312.Dv XDR_ENCODE , 313.Dv XDR_DECODE , 314or 315.Dv XDR_FREE ) . 316.Pp 317.Fn xdr_opaque 318is a filter primitive that translates between fixed size opaque 319data 320and its external representation. 321The parameter 322.Fa cp 323is the address of the opaque object, and 324.Fa cnt 325is its size in bytes. 326This routine returns one if it succeeds, zero otherwise. 327.Pp 328.Fn xdr_pointer 329is like 330.Fn xdr_reference 331except that it serializes 332.Dv NULL 333pointers, whereas 334.Fn xdr_reference 335does not. 336Thus, 337.Fn xdr_pointer 338can represent 339recursive data structures, such as binary trees or 340linked lists. 341.Pp 342.Fn xdrrec_create 343is a routine which initializes the 344.Tn XDR 345stream object pointed to by 346.Fa xdrs . 347The stream's data is written to a buffer of size 348.Fa sendsize ; 349a value of zero indicates the system should use a suitable 350default. 351The stream's data is read from a buffer of size 352.Fa recvsize ; 353it too can be set to a suitable default by passing a zero 354value. 355When a stream's output buffer is full, 356.Fn (*writeit) 357is called. 358Similarly, when a stream's input buffer is empty, 359.Fn (*readit) 360is called. 361The behavior of these two routines is similar to the system calls 362.Fn read 363and 364.Fn write , 365except that 366.Fa handle 367is passed to the former routines as the first parameter. 368Note: the 369.Tn XDR 370stream's 371.Fa op 372field must be set by the caller. 373Warning: this 374.Tn XDR 375stream implements an intermediate record stream. 376Therefore there are additional bytes in the stream 377to provide record boundary information. 378.Pp 379.Fn xdrrec_endofrecord 380is a routine which can be invoked only on 381streams created by 382.Fn xdrrec_create . 383The data in the output buffer is marked as a completed record, 384and the output buffer is optionally written out if 385.Fa sendnow 386is non-zero. 387This routine returns one if it succeeds, zero otherwise. 388.Pp 389.Fn xdrrec_eof 390is a routine which can be invoked only on 391streams created by 392.Fn xdrrec_create . 393After consuming the rest of the current record in the stream, 394this routine returns one if the stream has no more input, 395zero otherwise. 396.Pp 397.Fn xdrrec_skiprecord 398is a routine which can be invoked only on 399streams created by 400.Fn xdrrec_create . 401It tells the 402.Tn XDR 403implementation that the rest of the current record 404in the stream's input buffer should be discarded. 405This routine returns one if it succeeds, zero otherwise. 406.Pp 407.Fn xdr_reference 408is a primitive that provides pointer chasing within structures. 409The parameter 410.Fa pp 411is the address of the pointer; 412.Fa size 413is the size of the structure that 414.Fa *pp 415points to; and 416.Fa proc 417is an 418.Tn XDR 419procedure that filters the structure 420between its C form and its external representation. 421This routine returns one if it succeeds, zero otherwise. 422Warning: this routine does not understand 423.Dv NULL 424pointers. 425Use 426.Fn xdr_pointer 427instead. 428.Pp 429.Fn xdr_setpos 430is a macro that invokes the set position routine associated with the 431.Tn XDR 432stream 433.Fa xdrs . 434The parameter 435.Fa pos 436is a position value obtained from 437.Fn xdr_getpos . 438This routine returns one if the 439.Tn XDR 440stream could be repositioned, 441and zero otherwise. 442Warning: it is difficult to reposition some types of 443.Tn XDR 444streams, so this routine may fail with one 445type of stream and succeed with another. 446.Pp 447.Fn xdr_short 448is a filter primitive that translates between C 449.Li short 450integers and their external representations. 451This routine returns one if it succeeds, zero otherwise. 452.Pp 453.Fn xdrstdio_create 454is a routine which initializes the 455.Tn XDR 456stream object pointed to by 457.Fa xdrs . 458The 459.Tn XDR 460stream data is written to, or read from, the Standard I/O 461stream 462.Fa file . 463The parameter 464.Fa op 465determines the direction of the 466.Tn XDR 467stream (either 468.Dv XDR_ENCODE , 469.Dv XDR_DECODE , 470or 471.Dv XDR_FREE ) . 472Warning: the destroy routine associated with such 473.Tn XDR 474streams calls 475.Fn fflush 476on the 477.Fa file 478stream, but never 479.Fn fclose . 480.Pp 481.Fn xdr_string 482is a filter primitive that translates between C strings and their 483corresponding external representations. 484Strings cannot be longer than 485.Fa maxsize . 486Note: 487.Fa sp 488is the address of the string's pointer. 489This routine returns one if it succeeds, zero otherwise. 490.Pp 491.Fn xdr_u_char 492is a filter primitive that translates between 493.Li unsigned 494C characters and their external representations. 495This routine returns one if it succeeds, zero otherwise. 496.Pp 497.Fn xdr_u_int 498is a filter primitive that translates between C 499.Li unsigned 500integers and their external representations. 501This routine returns one if it succeeds, zero otherwise. 502.Pp 503.Fn xdr_u_long 504is a filter primitive that translates between C 505.Li unsigned long 506integers and their external representations. 507This routine returns one if it succeeds, zero otherwise. 508.Pp 509.Fn xdr_u_int64_t 510is a filter primitive that translates between C 511.Li u_int64_t 512integers and their external representations. 513This routine returns one if it succeeds, zero otherwise. 514.Pp 515.Fn xdr_u_short 516is a filter primitive that translates between C 517.Li unsigned short 518integers and their external representations. 519This routine returns one if it succeeds, zero otherwise. 520.Pp 521.Fn xdr_union 522is a filter primitive that translates between a discriminated C 523.Li union 524and its corresponding external representation. 525It first translates the discriminant of the union located at 526.Fa dscmp . 527This discriminant is always an 528.Li enum_t . 529Next the union located at 530.Fa unp 531is translated. 532The parameter 533.Fa choices 534is a pointer to an array of 535.Li struct xdr_discrim 536structures. 537Each structure contains an ordered pair of 538.Ft [ value , proc ]. 539If the union's discriminant is equal to the associated 540.Fa value , 541then the 542.Fa proc 543is called to translate the union. 544The end of the 545.Li struct xdr_discrim 546structure array is denoted by a routine of value 547.Dv NULL . 548If the discriminant is not found in the 549.Fa choices 550array, then the 551.Fn (*defaultarm) 552procedure is called (if it is not 553.Dv NULL ) . 554Returns one if it succeeds, zero otherwise. 555.Pp 556.Fn xdr_vector 557is a filter primitive that translates between fixed-length arrays 558and their corresponding external representations. 559The parameter 560.Fa arrp 561is the address of the pointer to the array, while 562.Fa size 563is the element count of the array. 564The parameter 565.Fa elsize 566is the size of each of the array's elements, and 567.Fa elproc 568is an 569.Tn XDR 570filter that translates between the array elements' C form, and their 571external representation. 572This routine returns one if it succeeds, zero otherwise. 573.Pp 574.Fn xdr_void 575is a routine which always returns one. 576It may be passed to 577.Tn RPC 578routines that require a function parameter, but where nothing is to be done. 579.Pp 580.Fn xdr_wrapstring 581is a primitive that calls 582.Pa "xdr_string(xdrs, sp, MAXUN.UNSIGNED );" 583where 584.Tn MAXUN.UNSIGNED 585is the maximum value of an unsigned integer. 586.Fn xdr_wrapstring 587is handy because the 588.Tn RPC 589package passes a maximum of two 590.Tn XDR 591routines as parameters, and 592.Fn xdr_string , 593one of the most frequently used primitives, requires three. 594Returns one if it succeeds, zero otherwise. 595.Sh SEE ALSO 596.Xr rpc 3 597.Rs 598.%T eXternal Data Representation Standard: Protocol Specification 599.Re 600.Rs 601.%T eXternal Data Representation: Sun Technical Notes 602.Re 603.Rs 604.%A "Sun Microsystems, Inc." 605.%T "XDR: External Data Representation Standard" 606.%R "RFC 1014, USC-ISI" 607.Re 608