C language function collection
This article introduces the complete set of C language functions — functions starting with x
1. xdr_char
1.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_char(XDR *xdrs, char *cp); |
for Encode a char type data into XDR stream or decode a char type data from XDR stream |
Parameters:
- xdrs : Pointer to the XDR structure to encode or decode data
- cp: Pointer to char type data to be encoded or decoded
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
1.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> int main() {<!-- --> // create XDR stream XDR xdr; xdrmem_create( & xdr, NULL, 0, XDR_ENCODE); // write char value char c = 'A'; if (!xdr_char( &xdr, &c)) {<!-- --> printf("Write failed"); return 1; } // reset XDR stream to decoding mode xdrrec_endofrecord( & xdr, true); // read char value from stream char result; if (!xdr_char( &xdr, &result)) {<!-- --> printf("Failed to read"); return 1; } // output result printf("Read result: %c\\ ", result); return 0; }
In the sample code above,
- First, we call the
xdrmem_create()
function to create aXDR
stream; - Then, use the
xdr_char()
function to write thechar
value to the stream; - Next, reset the stream to decode mode by calling the
xdrrec_endofrecord()
function; - Then, use the
xdr_char()
function to read thechar
value from the stream; - Finally, output the
char
value read from the stream.
2. xdr_callmsg
2.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_callmsg(XDR *xdrs, struct rpc_msg *cmsg); |
used Used to encode a structure representing a RPC message into an XDR stream or decode a RPC stream from a XDR stream The structure of the message |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- cmsg : Pointer to a structure representing the
RPC
message
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
2.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> #include <rpc/rpc.h> int main() {<!-- --> // Create a structure representing the RPC message struct rpc_msg cmsg = {<!-- --> .rm_xid = 1234567890, .rm_direction = CALL, .rm_call.cb_rpcvers = 2, .rm_call.cb_prog = 100001, .rm_call.cb_vers = 1, .rm_call.cb_proc = 4 }; // create XDR stream XDR xdr; char buffer[1024]; xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); // encode the message into the XDR stream if (!xdr_callmsg( &xdr, &cmsg)) {<!-- --> printf("Encoding failed"); return 1; } // reset XDR stream to decoding mode xdrrec_endofrecord( & xdr, true); // read message from stream struct rpc_msg result; if (!xdr_callmsg( &xdr, &result)) {<!-- --> printf("Decoding failed"); return 1; } // output result printf("Read result:\\ "); printf(" XID: %u\\ ", result.rm_xid); printf("Direction: %d\\ ", result.rm_direction); printf("RPC Version: %u\\ ", result.rm_call.cb_rpcvers); printf("Program: %u\\ ", result.rm_call.cb_prog); printf("Program Version: %u\\ ", result.rm_call.cb_vers); printf(" Procedure: %u\\ ", result.rm_call.cb_proc); return 0; }
In the example above,
- First, we define a structure
cmsg
representing theRPC
message, which contains the XID of the message, the direction, Fields such as RPC version number, program number, program version number, and procedure number. - Next, use the
xdrmem_create()
function to create an in-memoryXDR
stream for encoding messages intoXDR
format; - Then, call the
xdr_callmsg()
function to encode the message into theXDR
stream. If the encoding is successful, the return value isTRUE
, otherwise the return value isFALSE
. - Next, call the
xdrrec_endofrecord()
function to reset the stream to decode mode so that encoded messages can be read from the stream. - Then, call the
xdr_callmsg()
function again to decode a structure representing theRPC
message from the stream, and save it in theresult
variable middle. - Finally, use the
printf()
function to output the decoded message fields.
3. xdr_double
3.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_authunix_parms(XDR *xdrs, struct authunix_parms *objp); |
use Used to encode double type data into XDR stream or decode a double type data from XDR stream td> |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- dp : Pointer to
double
type data to encode or decode
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
3.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> int main() {<!-- --> // create XDR stream XDR xdr; char buffer[1024]; xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); // write double value double d = 3.1415926; if (!xdr_double( &xdr, &d)) {<!-- --> printf("Write failed"); return 1; } // reset XDR stream to decoding mode xdrrec_endofrecord( & xdr, true); // read a double value from the stream double result; if (!xdr_double( &xdr, &result)) {<!-- --> printf("Failed to read"); return 1; } // output result printf("Read result: %f\\ ", result); return 0; }
In the sample code above,
- First, we call the
xdrmem_create()
function to create aXDR
stream; - Then, use the
xdr_double()
function to write thedouble
value to the stream; - Next, reset the stream to decode mode by calling the
xdrrec_endofrecord()
function; - Then, use the
xdr_double()
function to read thedouble
value from the stream; - Finally, outputs the
double
value read from the stream.
Note: Extra care must be taken when using the
xdr_double
function, as different system architectures and endianness may affect how floating-point numbers are represented. It is recommended to only transfer between the same system and language when using this function.
4. xdr_enum
4.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_enum(XDR *xdrs, enum_t *ep); |
for Encode enumeration type data into XDR stream or decode enumeration type data from XDR stream |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- ep: A pointer to the enumerated type data to be encoded or decoded
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
4.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> enum Color {<!-- --> RED, GREEN, BLUE }; int main() {<!-- --> // create XDR stream XDR xdr; char buffer[1024]; xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); // write enum type value enum Color c = GREEN; if (!xdr_enum( & amp;xdr, (enum_t *) & amp;c)) {<!-- --> printf("Write failed"); return 1; } // reset XDR stream to decoding mode xdrrec_endofrecord( & xdr, true); // read the enum type value from the stream enum Color result; if (!xdr_enum( & amp;xdr, (enum_t *) & amp;result)) {<!-- --> printf("Failed to read"); return 1; } // output result printf("Read result: %d\\ ", result); return 0; }
In the example program above,
- First, we call the
xdrmem_create()
function to create aXDR
stream; - Then, use the
xdr_enum()
function to write the enum type valueGREEN
to the stream; - Then, call the
xdrrec_endofrecord()
function to reset the stream to decoding mode; - Then, use the
xdr_enum()
function to read the enumeration type value from the stream; - Finally, the
printf()
function is called to output the Boolean value read from the stream.
5. xdr_float
5.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_float(XDR *xdrs, float *fp); |
for Encode float type data into XDR stream or decode a float type data | XDR stream >
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- fp : Pointer to
float
type data to encode or decode
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
5.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> int main() {<!-- --> // create XDR stream XDR xdr; char buffer[1024]; xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); // write float value float f = 3.14; if (!xdr_float( &xdr, &f)) {<!-- --> printf("Write failed"); return 1; } // reset XDR stream to decoding mode xdrrec_endofrecord( & xdr, true); // read float value from stream float result; if (!xdr_float( &xdr, &result)) {<!-- --> printf("Failed to read"); return 1; } // output result printf("Read result: %f\\ ", result); return 0; }
In the sample code above,
- First, we call the
xdrmem_create()
function to create aXDR
stream; - Then, use the
xdr_float()
function to write thefloat
value to the stream; - Next, reset the stream to decode mode by calling the
xdrrec_endofrecord()
function; - Then, use the
xdr_float()
function to read thefloat
value from the stream; - Finally, output the
float
value read from the stream.
6. xdr_free
6.1 Function Description
Function declaration | Function function |
---|---|
void xdr_free(xdrproc_t proc, char* objp); |
for release Dynamic memory space allocated by xdr series functions |
Parameters:
- proc : The pointer type of the serialized or deserialized object
- objp: A pointer to dynamically allocated memory that needs to be freed
6.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> struct Person {<!-- --> char *name; int age; }; int main() {<!-- --> // create XDR stream XDR xdr; char buffer[1024]; xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); // allocate memory and encode data into XDR stream struct Person *p = (struct Person *)xdr_malloc( & xdr, sizeof(struct Person)); p->name = "Huazie"; p->age = 18; xdr_struct( & amp; xdr, "Person", (xdrproc_t)xdr_person, (char *)p); xdr_free((xdrproc_t)xdr_person, (char *)p); // reset XDR stream to decoding mode xdrrec_endofrecord( & xdr, true); // decode data and free memory struct Person result; if (xdr_struct( &xdr, "Person", (xdrproc_t)xdr_person, (char *) &result)) {<!-- --> printf("Read result: name = %s, age = %d\\ ", result.name, result.age); xdr_free((xdrproc_t)xdr_person, (char *) &result); } else {<!-- --> printf("Failed to read"); } return 0; } // Encode and decode Person type data bool_t xdr_person(XDR *xdrs, struct Person *p) {<!-- --> return xdr_string(xdrs, & amp;p->name, ~0) & amp; & amp; xdr_int(xdrs, & amp;p->age); }
In the sample code above,
- First, a structure named
Person
is defined, which contains aname
field of string type and anage
field of integer type ; - Then, we use the
xdrmem_create()
function to create aXDR
stream and bind it to thebuffer
buffer; - Next, we call the
xdr_malloc()
function to allocate the memory space of a structurePerson
, and assignname
to"Huazie\ "
,age
is18
; - Then, call the
xdr_struct()
function to encode the above structure data into theXDR
stream. After the encoding is completed, call thexdr_free()
function to release the allocated memory space, so that the dynamically allocated memory space used in the serialization process can be released. - Next, we reset the
XDR
stream to decode mode; - Then, use the
xdr_struct()
function to read and decode data from the stream. If the decoding is successful, the result is stored in aPerson
structure calledresult
. After decoding, we use thexdr_free()
function to release the dynamically allocated memory space. - Finally, output the
name
andage
field values in thePerson
structure.
7. xdr_hyper
7.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_hyper(XDR *xdrs, hyper *hp); |
for Encode and decode data of super long integer type |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- hp: Pointer to the long integer type to encode or decode
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
7.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> int main() {<!-- --> // create XDR stream XDR xdr; char buffer[1024]; xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); // Encode data of super long integer type hyper h = 1234567890123456789; if (xdr_hyper( &xdr, &h)) {<!-- --> printf("encoded successfully\\ "); } // reset XDR stream to decoding mode xdrrec_endofrecord( & xdr, true); // Decode data of super long integer type hyper result; if (xdr_hyper( &xdr, &result)) {<!-- --> printf("Decoding succeeded, result = %lld\\ ", result); } return 0; }
In the sample code above,
- First, we use the
xdrmem_create()
function to create aXDR
stream and bind it to thebuffer
buffer; - Then, define a variable
h
of super long integer type, and assign it as1234567890123456789
; - Then, use the
xdr_hyper()
function to encode the data of the super long integer type into theXDR
stream, and check whether the encoding is successful; if the encoding is successful, output\ "Encoding successful"
; - Then, call the
xdrrec_endofrecord()
function to reset theXDR
stream to decoding mode; - Finally, call the
xdr_hyper()
function to decode the data read from theXDR
stream into a super long integer type, and check whether the decoding is successful; if the decoding is successful, output the decoded result.
8. xdr_int
8.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_int(XDR *xdrs, int *ip); |
for Encode int type data into XDR stream or decode a int type data | XDR stream >
Parameters:
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- ip : Pointer to
int
type data to encode or decode
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
8.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> int main() {<!-- --> // create XDR stream XDR xdr; char buffer[1024]; xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); // Encode integer type data int i = 12345; if (xdr_int( &xdr, &i)) {<!-- --> printf("encoded successfully\\ "); } // reset XDR stream to decoding mode xdrrec_endofrecord( & xdr, true); // decode integer type data int result; if (xdr_int( &xdr, &result)) {<!-- --> printf("Decoding succeeded, result = %d\\ ", result); } return 0; }
In the sample code above,
- First, we call the
xdrmem_create()
function to create aXDR
stream; - Then, use the
xdr_int()
function to write the value12345
of typeint
to the stream; if it returnsTRUE
, Then it means that the encoding is successful, output"encoding success"
; - Next, reset the stream to decode mode by calling the
xdrrec_endofrecord()
function; - Finally, use the
xdr_int()
function to read the value of theint
type from the stream; if it returns TRUE, it means that the decoding is successful, and theread from the stream is output A value of type int
.
9. xdr_long
9.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_long(XDR *xdrs, long *lp); |
for Encode long type data into XDR stream or decode a long type data | XDR stream >
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- lp: Pointer to data of type
long
to encode or decode
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
9.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> int main() {<!-- --> // create XDR stream XDR xdr; char buffer[1024]; xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); // Encode data of long integer type long l = 1234567890; if (xdr_long( &xdr, &l)) {<!-- --> printf("encoded successfully\\ "); } // reset XDR stream to decoding mode xdrrec_endofrecord( & xdr, true); // Decode data of long integer type long result; if (xdr_long( &xdr, &result)) {<!-- --> printf("Decoding succeeded, result = %ld\\ ", result); } return 0; }
In the sample code above,
- First, we call the
xdrmem_create()
function to create aXDR
stream; - Then, use the
xdr_long()
function to write the value1234567890
of typelong
to the stream; if it returnsTRUE
, Then it means that the encoding is successful, output"encoding success"
; - Then, call the
xdrrec_endofrecord()
function to reset the stream to decoding mode; - Finally, use the
xdr_long()
function to read the value of thelong
type from the stream; if it returns TRUE, it means that the decoding is successful, and theread from the stream is output A value of type long
.