1.\" $OpenBSD: xdr.3,v 1.28 2022/09/11 06:38:11 jmc Exp $ 2.\" Mostly converted to mandoc by Theo de Raadt, Tue Feb 24 04:04:46 MST 1998 3.\" 4.\" Copyright (c) 2010, Oracle America, Inc. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions are 8.\" met: 9.\" 10.\" * Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" * Redistributions in binary form must reproduce the above 13.\" copyright notice, this list of conditions and the following 14.\" disclaimer in the documentation and/or other materials 15.\" provided with the distribution. 16.\" * Neither the name of the "Oracle America, Inc." nor the names of its 17.\" contributors may be used to endorse or promote products derived 18.\" from this software without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21.\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24.\" COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25.\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30.\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31.\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32.\" 33.Dd $Mdocdate: September 11 2022 $ 34.Dt XDR_ARRAY 3 35.Os 36.Sh NAME 37.Nm xdr_array , 38.Nm xdr_bool , 39.Nm xdr_bytes , 40.Nm xdr_char , 41.Nm xdr_destroy , 42.Nm xdr_double , 43.Nm xdr_enum , 44.Nm xdr_float , 45.Nm xdr_free , 46.Nm xdr_getpos , 47.Nm xdr_inline , 48.Nm xdr_int , 49.Nm xdr_long , 50.Nm xdr_int64_t , 51.Nm xdrmem_create , 52.Nm xdr_opaque , 53.Nm xdr_pointer , 54.Nm xdrrec_create , 55.Nm xdrrec_endofrecord , 56.Nm xdrrec_eof , 57.Nm xdrrec_skiprecord , 58.Nm xdr_reference , 59.Nm xdr_setpos , 60.Nm xdr_short , 61.Nm xdrstdio_create , 62.Nm xdr_string , 63.Nm xdr_u_char , 64.Nm xdr_u_int , 65.Nm xdr_u_long , 66.Nm xdr_u_int64_t , 67.Nm xdr_u_short , 68.Nm xdr_union , 69.Nm xdr_vector , 70.Nm xdr_void , 71.Nm xdr_wrapstring 72.Nd library routines for external data representation 73.Sh SYNOPSIS 74.In sys/types.h 75.In rpc/xdr.h 76.Ft int 77.Fn xdr_array "XDR *xdrs" "char **arrp" "u_int *sizep" "u_int maxsize" "u_int elsize" "xdrproc_t elproc" 78.Ft int 79.Fn xdr_bool "XDR *xdrs" "bool_t *bp" 80.Ft int 81.Fn xdr_bytes "XDR *xdrs" "char **sp" "u_int *sizep" "u_int maxsize" 82.Ft int 83.Fn xdr_char "XDR *xdrs" "char *cp" 84.Ft void 85.Fn xdr_destroy "XDR *xdrs" 86.Ft int 87.Fn xdr_double "XDR *xdrs" "double *dp" 88.Ft int 89.Fn xdr_enum "XDR *xdrs" "enum_t *ep" 90.Ft int 91.Fn xdr_float "XDR *xdrs" "float *fp" 92.Ft void 93.Fn xdr_free "xdrproc_t proc" "char *objp" 94.Ft u_int 95.Fn xdr_getpos "XDR *xdrs" 96.Ft long * 97.Fn xdr_inline "XDR *xdrs" "int len" 98.Ft int 99.Fn xdr_int "XDR *xdrs" "int *ip" 100.Ft int 101.Fn xdr_long "XDR *xdrs" "long *lp" 102.Ft int 103.Fn xdr_int64_t "XDR *xdrs" "int64_t *lp" 104.Ft void 105.Fn xdrmem_create "XDR *xdrs" "char *addr" "u_int size" "enum xdr_op op" 106.Ft int 107.Fn xdr_opaque "XDR *xdrs" "char *cp" "u_int cnt" 108.Ft int 109.Fn xdr_pointer "XDR *xdrs" "char **objpp" "u_int objsize" "xdrproc_t xdrobj" 110.Ft void 111.Fn xdrrec_create "XDR *xdrs" "u_int sendsize" "u_int recvsize" "char *handle" "int (*readit)()" "int (*writeit)()" 112.Ft int 113.Fn xdrrec_endofrecord "XDR *xdrs" "int sendnow" 114.Ft int 115.Fn xdrrec_eof "XDR *xdrs" 116.Ft int 117.Fn xdrrec_skiprecord "XDR *xdrs" 118.Ft int 119.Fn xdr_reference "XDR *xdrs" "char **pp" "u_int size" "xdrproc_t proc" 120.Ft int 121.Fn xdr_setpos "XDR *xdrs" "u_int pos" 122.Ft int 123.Fn xdr_short "XDR *xdrs" "short *sp" 124.Ft void 125.Fn xdrstdio_create "XDR *xdrs" "FILE *file" "enum xdr_op op" 126.Ft int 127.Fn xdr_string "XDR *xdrs" "char **sp" "u_int maxsize" 128.Ft int 129.Fn xdr_u_char "XDR *xdrs" "unsigned char *ucp" 130.Ft int 131.Fn xdr_u_int "XDR *xdrs" "unsigned int *up" 132.Ft int 133.Fn xdr_u_long "XDR *xdrs" "unsigned long *ulp" 134.Ft int 135.Fn xdr_u_int64_t "XDR *xdrs" "u_int64_t *ullp" 136.Ft int 137.Fn xdr_u_short "XDR *xdrs" "unsigned short *usp" 138.Ft int 139.Fn xdr_union "XDR *xdrs" "int *dscmp" "char *unp" "struct xdr_discrim *choices" "bool_t (*defaultarm)()" 140.Ft int 141.Fn xdr_vector "XDR *xdrs" "char *arrp" "u_int size" "u_int elsize" "xdrproc_t elproc" 142.Ft int 143.Fn xdr_void "void" 144.Ft int 145.Fn xdr_wrapstring "XDR *xdrs" "char **sp" 146.Sh DESCRIPTION 147These routines allow C programmers to describe 148arbitrary data structures in a machine-independent fashion. 149Data for remote procedure calls are transmitted using these 150routines. 151.Pp 152.Fn xdr_array 153is a filter primitive that translates between variable-length arrays 154and their corresponding external representations. 155The parameter 156.Fa arrp 157is the address of the pointer to the array, while 158.Fa sizep 159is the address of the element count of the array; 160this element count cannot exceed 161.Fa maxsize . 162The parameter 163.Fa elsize 164is the size of each of the array's elements, and 165.Fa elproc 166is an XDR filter 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 XDR stream 203.Fa xdrs . 204Destruction usually involves freeing private data structures 205associated with the stream. 206Using 207.Fa xdrs 208after invoking 209.Fn xdr_destroy 210is undefined. 211.Pp 212.Fn xdr_double 213is a filter primitive that translates between C 214.Vt double 215precision numbers and their external representations. 216This routine returns one if it succeeds, zero otherwise. 217.Pp 218.Fn xdr_enum 219is a filter primitive that translates between the C 220.Vt enum 221type (actually an integer) and its external representations. 222This routine returns one if it succeeds, zero otherwise. 223.Pp 224.Fn xdr_float 225is a filter primitive that translates between the C 226.Vt float 227type and its external representations. 228This routine returns one if it succeeds, zero otherwise. 229.Pp 230.Fn xdr_free 231is a generic freeing routine. 232The first argument is the XDR routine for the object being freed. 233The second argument 234is a pointer to the object itself. 235Note: the pointer passed to this routine is 236.Fa not 237freed, but what it points to is freed (recursively). 238.Pp 239.Fn xdr_getpos 240is a macro that invokes the get-position routine associated with the XDR 241stream 242.Fa xdrs . 243The routine returns an unsigned integer, 244which indicates the position of the XDR byte stream. 245A desirable feature of XDR 246streams is that simple arithmetic works with this number, 247although the XDR stream instances need not guarantee this. 248.Pp 249.Fn xdr_inline 250is a macro that invokes the in-line routine associated with the XDR stream 251.Fa xdrs . 252The routine returns a pointer 253to a contiguous piece of the stream's buffer; 254.Fa len 255is the byte length of the desired buffer. 256Note: pointer is cast to 257.Vt long * . 258Warning: 259.Fn xdr_inline 260may return 261.Dv NULL 262if it cannot allocate a contiguous piece of a buffer. 263Therefore the behavior may vary among stream instances; 264it exists for the sake of efficiency. 265.Pp 266.Fn xdr_int 267is a filter primitive that translates between C integers 268and their external representations. 269This routine returns one if it succeeds, zero otherwise. 270.Pp 271.Fn xdr_long 272is a filter primitive that translates between C 273.Vt long 274integers and their external representations. 275This routine returns one if it succeeds, zero otherwise. 276.Pp 277.Fn xdr_int64_t 278is a filter primitive that translates between C 279.Vt int64_t 280integers and their external representations. 281This routine returns one if it succeeds, zero otherwise. 282.Pp 283.Fn xdrmem_create 284is a routine which initializes the XDR stream object pointed to by 285.Fa xdrs . 286The stream's data is written to, or read from, 287a chunk of memory at location 288.Fa addr 289whose length is no more than 290.Fa size 291bytes long. 292The 293.Fa op 294determines the direction of the XDR stream 295(either 296.Dv XDR_ENCODE , 297.Dv XDR_DECODE , 298or 299.Dv XDR_FREE ) . 300.Pp 301.Fn xdr_opaque 302is a filter primitive that translates between fixed size opaque 303data 304and its external representation. 305The parameter 306.Fa cp 307is the address of the opaque object, and 308.Fa cnt 309is its size in bytes. 310This routine returns one if it succeeds, zero otherwise. 311.Pp 312.Fn xdr_pointer 313is like 314.Fn xdr_reference 315except that it serializes 316.Dv NULL 317pointers, whereas 318.Fn xdr_reference 319does not. 320Thus, 321.Fn xdr_pointer 322can represent 323recursive data structures, such as binary trees or 324linked lists. 325.Pp 326.Fn xdrrec_create 327is a routine which initializes the XDR stream object pointed to by 328.Fa xdrs . 329The stream's data is written to a buffer of size 330.Fa sendsize ; 331a value of zero indicates the system should use a suitable 332default. 333The stream's data is read from a buffer of size 334.Fa recvsize ; 335it too can be set to a suitable default by passing a zero 336value. 337When a stream's output buffer is full, 338.Fn (*writeit) 339is called. 340Similarly, when a stream's input buffer is empty, 341.Fn (*readit) 342is called. 343The behavior of these two routines is similar to the system calls 344.Fn read 345and 346.Fn write , 347except that 348.Fa handle 349is passed to the former routines as the first parameter. 350Note: the XDR stream's 351.Fa op 352field must be set by the caller. 353Warning: this XDR stream implements an intermediate record stream. 354Therefore there are additional bytes in the stream 355to provide record boundary information. 356.Pp 357.Fn xdrrec_endofrecord 358is a routine which can be invoked only on 359streams created by 360.Fn xdrrec_create . 361The data in the output buffer is marked as a completed record, 362and the output buffer is optionally written out if 363.Fa sendnow 364is non-zero. 365This routine returns one if it succeeds, zero otherwise. 366.Pp 367.Fn xdrrec_eof 368is a routine which can be invoked only on 369streams created by 370.Fn xdrrec_create . 371After consuming the rest of the current record in the stream, 372this routine returns one if the stream has no more input, 373zero otherwise. 374.Pp 375.Fn xdrrec_skiprecord 376is a routine which can be invoked only on 377streams created by 378.Fn xdrrec_create . 379It tells the XDR implementation that the rest of the current record 380in the stream's input buffer should be discarded. 381This routine returns one if it succeeds, zero otherwise. 382.Pp 383.Fn xdr_reference 384is a primitive that provides pointer chasing within structures. 385The parameter 386.Fa pp 387is the address of the pointer; 388.Fa size 389is the size of the structure that 390.Fa *pp 391points to; and 392.Fa proc 393is an XDR procedure that filters the structure 394between its C form and its external representation. 395This routine returns one if it succeeds, zero otherwise. 396Warning: this routine does not understand 397.Dv NULL 398pointers. 399Use 400.Fn xdr_pointer 401instead. 402.Pp 403.Fn xdr_setpos 404is a macro that invokes the set position routine associated with the XDR 405stream 406.Fa xdrs . 407The parameter 408.Fa pos 409is a position value obtained from 410.Fn xdr_getpos . 411This routine returns one if the XDR stream could be repositioned, 412and zero otherwise. 413Warning: it is difficult to reposition some types of XDR 414streams, so this routine may fail with one 415type of stream and succeed with another. 416.Pp 417.Fn xdr_short 418is a filter primitive that translates between C 419.Vt short 420integers and their external representations. 421This routine returns one if it succeeds, zero otherwise. 422.Pp 423.Fn xdrstdio_create 424is a routine which initializes the XDR stream object pointed to by 425.Fa xdrs . 426The XDR stream data is written to, or read from, the Standard I/O 427stream 428.Fa file . 429The parameter 430.Fa op 431determines the direction of the XDR stream (either 432.Dv XDR_ENCODE , 433.Dv XDR_DECODE , 434or 435.Dv XDR_FREE ) . 436Warning: the destroy routine associated with such XDR streams calls 437.Fn fflush 438on the 439.Fa file 440stream, but never 441.Fn fclose . 442.Pp 443.Fn xdr_string 444is a filter primitive that translates between C strings and their 445corresponding external representations. 446Strings cannot be longer than 447.Fa maxsize . 448Note: 449.Fa sp 450is the address of the string's pointer. 451This routine returns one if it succeeds, zero otherwise. 452.Pp 453.Fn xdr_u_char 454is a filter primitive that translates between 455.Vt unsigned 456C characters and their external representations. 457This routine returns one if it succeeds, zero otherwise. 458.Pp 459.Fn xdr_u_int 460is a filter primitive that translates between C 461.Vt unsigned 462integers and their external representations. 463This routine returns one if it succeeds, zero otherwise. 464.Pp 465.Fn xdr_u_long 466is a filter primitive that translates between C 467.Vt unsigned long 468integers and their external representations. 469This routine returns one if it succeeds, zero otherwise. 470.Pp 471.Fn xdr_u_int64_t 472is a filter primitive that translates between C 473.Vt u_int64_t 474integers and their external representations. 475This routine returns one if it succeeds, zero otherwise. 476.Pp 477.Fn xdr_u_short 478is a filter primitive that translates between C 479.Vt unsigned short 480integers and their external representations. 481This routine returns one if it succeeds, zero otherwise. 482.Pp 483.Fn xdr_union 484is a filter primitive that translates between a discriminated C 485.Vt union 486and its corresponding external representation. 487It first translates the discriminant of the union located at 488.Fa dscmp . 489This discriminant is always an 490.Vt enum_t . 491Next the union located at 492.Fa unp 493is translated. 494The parameter 495.Fa choices 496is a pointer to an array of 497.Vt struct xdr_discrim 498structures, containing the two members 499.Fa value 500and 501.Fa proc . 502If the union's discriminant is equal to the associated 503.Fa value , 504then the 505.Fa proc 506is called to translate the union. 507The end of the 508.Vt struct xdr_discrim 509structure array is denoted by a routine of value 510.Dv NULL . 511If the discriminant is not found in the 512.Fa choices 513array, then the 514.Fn (*defaultarm) 515procedure is called (if it is not 516.Dv NULL ) . 517Returns one if it succeeds, zero otherwise. 518.Pp 519.Fn xdr_vector 520is a filter primitive that translates between fixed-length arrays 521and their corresponding external representations. 522The parameter 523.Fa arrp 524is the address of the pointer to the array, while 525.Fa size 526is the element count of the array. 527The parameter 528.Fa elsize 529is the size of each of the array's elements, and 530.Fa elproc 531is an XDR 532filter that translates between the array elements' C form, and their 533external representation. 534This routine returns one if it succeeds, zero otherwise. 535.Pp 536.Fn xdr_void 537is a routine which always returns one. 538It may be passed to RPC 539routines that require a function parameter, but where nothing is to be done. 540.Pp 541.Fn xdr_wrapstring 542is a primitive that calls 543.Nm xdr_string Ns Pq Fa xdrs , sp , Dv MAXUN.UNSIGNED 544where 545.Dv MAXUN.UNSIGNED 546is the maximum value of an unsigned integer. 547.Fn xdr_wrapstring 548is handy because the RPC package passes a maximum of two XDR 549routines as parameters, and 550.Fn xdr_string , 551one of the most frequently used primitives, requires three. 552Returns one if it succeeds, zero otherwise. 553.Sh SEE ALSO 554.Xr rpc 3 555.Rs 556.%T eXternal Data Representation Standard: Protocol Specification 557.Re 558.Rs 559.%T eXternal Data Representation: Sun Technical Notes 560.Re 561.Sh STANDARDS 562.Rs 563.%D June 1987 564.%Q Sun Microsystems, Inc. 565.%R RFC 1014 566.%T XDR: External Data Representation Standard 567.Re 568