C language function collection
This article introduces the complete set of C language functions — functions starting with x
1. xdr_opaque
1.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_opaque(XDR *xdrs, char *buf, u_int len); |
Used to encode or decode binary data of any length |
Parameters:
- xdrs : Pointer to the
XDR
data structure representing the data stream to be encoded or decoded- buf: Pointer to the binary data to be encoded or decoded
- len : The length (in bytes) of the binary 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 <stdlib.h> #include <rpc/xdr.h> int main() {<!-- --> // encode data char data[] = "hello, world!"; XDR xdr; xdrmem_create( & xdr, NULL, 0, XDR_ENCODE); if (!xdr_opaque( & xdr, data, sizeof(data))) {<!-- --> printf("Error: failed to encode data.\\ "); exit(EXIT_FAILURE); } // Get encoded data and print char *encoded_data = malloc(xdr_getpos( & amp;xdr)); xdrmem_create( & amp; xdr, encoded_data, xdr_getpos( & amp; xdr), XDR_DECODE); printf("Encoded data: "); for (int i = 0; i < xdr_getpos( & xdr); + + i) {<!-- --> printf(" x ", encoded_data[i]); } printf("\\ "); // decode data char decoded_data[sizeof(data)]; if (!xdr_opaque( & xdr, decoded_data, sizeof(decoded_data))) {<!-- --> printf("Error: failed to decode data.\\ "); exit(EXIT_FAILURE); } printf("Decoded data: %s\\ ", decoded_data); return 0; }
In the sample code above,
- First, we define a string
"hello, world!"
as the data to be encoded, and create aXDR
structure; - Then, call the
xdrmem_create()
function to initialize theXDR
structure so that it can read and write data from the memory; - Next, use the
xdr_opaque()
function to encode the string and convert it to XDR format; - Then, get the encoded data and print it; among them,
- We first use the
xdr_getpos()
function to get the length of the encoded data; - Then use the
xdrmem_create()
function to create a newXDR
structure to decode it; - Finally, loop through the encoded data and print it out in hexadecimal.
- We first use the
- Then, use the
xdr_opaque()
function to decode the encoded data; - Finally, print the decoded data.
2. xdr_opaque_auth
2.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap); |
used Used to encode or decode authentication information |
Parameters:
- xdrs : A pointer to the
XDR
structure, used to specify the codec context- ap : Pointer to
opaque_auth
structure, used to specify authentication information
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
2.2 Demonstration example
2.2.1 opaque_auth
// defined in <rpc/auth.h> struct opaque_auth {<!-- --> int oa_flavor; // authentication method (authentication flavor) caddr_t oa_base; // opaque data u_int oa_length; // data length };
2.2.2 test.c
#include <stdio.h> #include <stdlib.h> #include <rpc/xdr.h> #include <rpc/auth.h> int main() {<!-- --> // encode data struct opaque_auth auth; auth.oa_flavor = AUTH_NONE; auth.oa_base = NULL; auth.oa_length = 0; XDR xdr; xdrmem_create( & xdr, NULL, 0, XDR_ENCODE); if (!xdr_opaque_auth( &xdr, &auth)) {<!-- --> printf("Error: failed to encode authentication information.\\ "); exit(EXIT_FAILURE); } // Get encoded data and print char *encoded_data = malloc(xdr_getpos( & amp;xdr)); xdrmem_create( & amp; xdr, encoded_data, xdr_getpos( & amp; xdr), XDR_DECODE); printf("Encoded data: "); for (int i = 0; i < xdr_getpos( & xdr); + + i) {<!-- --> printf(" x ", encoded_data[i]); } printf("\\ "); // Decode authentication information struct opaque_auth decoded_auth; xdrmem_create( & amp; xdr, encoded_data, xdr_getpos( & amp; xdr), XDR_DECODE); if (!xdr_opaque_auth( &xdr, &decoded_auth)) {<!-- --> printf("Error: failed to decode authentication information.\\ "); exit(EXIT_FAILURE); } // Print the decoded authentication information printf("Decoded authentication information:\\ "); printf("oa_flavor = %d\\ ", decoded_auth.oa_flavor); printf("oa_length = %u\\ ", decoded_auth.oa_length); return 0; }
In the example above,
- First, we create a
opaque_auth
structure and initialize its member variables.
Note: Since the authentication method used is
AUTH_NONE
, that is, no authentication information is required; therefore, theoa_length
member variable is set to0
. In actual development, we may need to use other authentication methods, and set the member variables of theopaque_auth
structure accordingly. 】
-
Then, use the
xdrmem_create()
function to create anXDR
structure and specify it as the encoding mode. -
Then, call the
xdr_opaque_auth()
function to encode the authentication information, obtain the encoded data and print it out [here is the same as in 1.2, no more details]. -
Then, use the
xdrmem_create()
function to create a newXDR
structure and specify it as the decoding mode. -
Then, call the
xdr_opaque_auth()
function to decode the encoded authentication information, and obtain the decoded authentication information by accessing the member variables of theopaque_auth
structure. -
Finally, print the decoded authentication information.
3. xdr_pointer
3.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_pointer(XDR *xdrs, char **objpp, u_int obj_size, xdrproc_t xdr_obj) |
Used to encode and decode pointer type data |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- objpp: The address of the data to be encoded or decoded
- obj_size: The size in bytes of the data to be encoded or decoded
- xdr_obj: Used to encode or decode the data pointed to by the pointer
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
For the use of the xdr_pointer()
function, refer to the following:
-
For encoding operations, the
xdr_pointer()
function encodes the data pointed to by the pointerobjpp
into theXDR
stream. Ifobjpp
is NULL, a special flag is written to represent a null pointer. Ifobjpp
is notNULL
, first write a non-zero value to represent a non-null pointer, and then call the specifiedXDR
encoding program to encode the data to encode. -
For decoding operations, the
xdr_pointer()
function reads some bytes from theXDR
stream and creates a new data object from those bytes. If the bytes read represent a null pointer, setobjpp
toNULL
. Otherwise, setobjpp
to a pointer to the new object and call the specifiedXDR
encoder to decode the data.
3.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> int main() {<!-- --> char buffer[1024]; XDR xdrs; xdrmem_create( & amp;xdr, buffer, sizeof(buffer), XDR_ENCODE); // create XDR stream // encode the pointer as an integer in the XDR stream int pointer_value = 42; xdr_pointer( &xdrs, (char **) &pointer_value, sizeof(int), (xdrproc_t)xdr_int); // print the encoded value printf("Encoded value: %d\\ ", pointer_value); // decode the integer, restore the pointer xdr_setpos( & xdrs, 0); xdr_pointer( &xdrs, (char **) &pointer_value, sizeof(int), (xdrproc_t)xdr_int); // print the decoded pointer value printf("Decoded value: %d\\ ", pointer_value); // close the stream xdr_destroy( & xdrs); return 0; }
In the sample code above,
- First, we create a
XDR
stream objectxdrs
and bind it to thebuffer
array; - Then, use the
xdr_pointer()
function to write the integer pointerpointer_value
into the stream; and output the value ofpointer_value
, which is the encoded integer; - Then, use the
xdr_setpos()
function to set the position of theXDR
stream to0
; - Then, use the
xdr_pointer()
function to decode the integer value from theXDR
stream and store it back into thepointer_value
variable; and output thepointer_value
, which is the decoded integer pointer; - Finally, use the
xdr_destroy()
function to close and release theXDR
stream object and end the program.
4. xdr_replymsg
4.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_replymsg(XDR *xdrs, struct rpc_msg *msg); |
used Used to encode or decode reply messages |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- msg: Pointer to enum 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
.
Before using the xdr_replymsg()
function, each field in the rpc_msg
structure needs to be set. The rpc_msg
structure is used to represent the RPC
message, which includes the following fields:
uint32_t xid
: Indicates the identifier of this message.enum msg_type msg_type
: Indicates the message type, which can beCALL
orREPLY
.union rpc_body
: Indicates the main part ofRPC
, including:call_body
: Indicates the body of theCALL
message, including the program number, version number, and process number ofRPC
, as well as the parameters passed to the process.reply_body
: Indicates the body of theREPLY
message, including the return value and possible error messages.
4.2 Demonstration example
#include <stdio.h> #include <stdlib.h> #include <rpc/xdr.h> int main() {<!-- --> // create XDR stream object char buffer[1024]; XDR xdrs; xdrmem_create( & xdrs, buffer, sizeof(buffer), XDR_ENCODE); // Set rpc_msg structure struct rpc_msg msg = {<!-- -->.xid = 12345, .msg_type = REPLY}; msg.acpted_rply.ar_verf = AUTH_NULL; msg.acpted_rply.ar_results.where = (char *)"Success"; msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_string; // Encode the reply message if (!xdr_replymsg( &xdrs, &msg)) {<!-- --> fprintf(stderr, "Failed to encode reply message\\ "); return 1; } // print the encoded result printf("Encoded message: "); for (int i = 0; i < xdr_getpos( & amp; xdrs); i ++ ) {<!-- --> printf("x", buffer[i] & 0xff); } printf("\\ "); // decode the reply message struct rpc_msg decoded_msg; if (!xdr_replymsg( &xdrs, &decoded_msg)) {<!-- --> fprintf(stderr, "Failed to decode reply message\\ "); return 1; } // print the decoded result printf("Decoded message:\\ "); printf("XID: %d\\ ", decoded_msg.xid); printf("Message Type: %d\\ ", decoded_msg.msg_type); printf("Verf Flavor: %d\\ ", decoded_msg.acpted_rply.ar_verf.oa_flavor); printf("Verf Length: %d\\ ", decoded_msg.acpted_rply.ar_verf.oa_length); printf("Result:\\ "); char *result; if (!xdr_string( &xdrs, &result, sizeof(result))) {<!-- --> fprintf(stderr, "Failed to decode result string\\ "); return 1; } printf("%s\\ ", result); return 0; }
In the example program above,
- First, create an
XDR
stream object and set up anrpc_msg
structure containing the required information. - Next, use the
xdr_replymsg()
function to encode therpc_msg
structure intoXDR
format. If encoding fails, output an error and exit the program. - Then, print the encoded result so you can see the serialized data.
- Next, use the
xdr_replymsg()
function to decode the encoded data back into therpc_msg
structure. If decoding fails, output an error and exit the program. - Then, print the decoded result, including transaction
ID
, message type, verification flag, result and other information; - Finally, the program exits normally.
5. xdr_reference
5.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_reference(XDR *xdrs, char **objp, u_int obj_size, xdrproc_t xdr_func); |
is used to encode or decode a reference to an object |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- objp: A reference to a pointer object to encode or decode
- obj_size : the size of the referenced pointer object
- xdr_func: Pointer to the XDR function used to encode or decode the data object pointed to by objp
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 <stdlib.h> #include <rpc/xdr.h> int main() {<!-- --> // create XDR stream object char buffer[1024]; XDR xdrs; xdrmem_create( & xdrs, buffer, sizeof(buffer), XDR_ENCODE); // set string pointer char *str = "Hello, world!"; // encoded string pointer if (!xdr_reference( &xdrs, &str, strlen(str) + 1, (xdrproc_t)xdr_string)) {<!-- --> fprintf(stderr, "Failed to encode string reference\\ "); return EXIT_FAILURE; } // print the encoded result printf("Encoded message: "); for (int i = 0; i < xdr_getpos( & amp; xdrs); i ++ ) {<!-- --> printf("x", buffer[i] & 0xff); } printf("\\ "); // Create a new XDR stream object and deserialize the string pointer XDR new_xdrs; xdrmem_create( & new_xdrs, buffer, sizeof(buffer), XDR_DECODE); char *decoded_str = NULL; if (!xdr_reference( &new_xdrs, &decoded_str, strlen(str) + 1, (xdrproc_t)xdr_string)) {<!-- --> fprintf(stderr, "Failed to decode string reference\\ "); return EXIT_FAILURE; } // print the decoded result printf("Decoded message: %s\\ ", decoded_str); return EXIT_SUCCESS; }
In the sample code above,
- First, we call the
xdrmem_create()
function to create aXDR
stream objectxdrs
, and bind it tobuffer
; - Then, a string pointer
str
is defined and initialized to"Hello, world!"
; - Then, call the
xdr_reference()
function to encodestr
and store the result inbuffer
. If the encoding is successful, print out the encoded message; - Then, call the
xdrmem_create()
function to create a newXDR
stream objectnew_xdrs
and bind it tobuffer
Certainly; - Then, call
xdr_reference()
function to decodedecoded_str
, and read encoded data fromnew_xdrs
object. If the decoding is successful, print out the decoded message. - Finally, the program exits normally.
6. xdr_rpc_gss_data
6.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_rpc_gss_data(XDR * xdrs, rpc_gss_data * gd); |
for Encode or decode RPC GSS data |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- gd : Pointer to rpc_gss_data structure
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
6.2 Demonstration example
#include <stdio.h> #include <stdlib.h> #include <rpc/rpc.h> #include <rpc/auth_gss.h> int main() {<!-- --> rpc_gss_data gd; XDR xdr; char buffer[1024]; bool_t status; memset( & gd, 0, sizeof(gd)); memset( & xdr, 0, sizeof(xdr)); xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); status = xdr_rpc_gss_data( &xdr, &gd); if (!status) {<!-- --> fprintf(stderr, "Serialization failed.\\ "); exit(1); } xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_DECODE); status = xdr_rpc_gss_data( &xdr, &gd); if (!status) {<!-- --> fprintf(stderr, "Deserialization failed.\\ "); exit(1); } return 0; }
7. xdr_rpcb
7.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_rpcb(XDR *xdrs, struct rpcb *objp); |
used For encoding and decoding RPCBIND program number and version number, as well as program address information |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- objp: A pointer to the rpcb structure
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
7.2 Demonstration example
// RPCBIND program number and version number, and program address struct rpcb {<!-- --> char *r_progname; u_long r_progvers; struct netbuf r_addr; char *r_netid; }; // network address information struct netbuf {<!-- --> u_int maxlen; u_int len; char *buf; };
#include <stdio.h> #include <rpc/rpc.h> int main() {<!-- --> struct rpcb rpcb_obj = {<!-- -->"my_program", 1, {<!-- -->0, 0, NULL}, "tcp"}; XDR xdr; char buffer[1024]; xdrmem_create( & xdr, buffer, sizeof(buffer), XDR_ENCODE); if (xdr_rpcb( &xdr, &rpcb_obj)) {<!-- --> printf("Serialization success!\\ "); } else {<!-- --> printf("Serialization failed!\\ "); } return 0; }
8. xdr_short
8.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_short(XDR *xdrs, short *sp); |
for Encode or decode short integer data |
Parameters:
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- sp: Pointer to data of type
short
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 <rpc/xdr.h> #include <stdio.h> int main() {<!-- --> XDR xdr; char buf[256]; short num = 42; // Initialize the XDR stream structure xdrmem_create( & xdr, buf, sizeof(buf), XDR_ENCODE); // marshal short type variable if (!xdr_short( &xdr, &num)) {<!-- --> fprintf(stderr, "Marshalling failed!\\ "); return 1; } // reset stream position xdr_setpos( & xdr, 0); // unmarshal short type variable short decoded_num; if (!xdr_short( &xdr, &decoded_num)) {<!-- --> fprintf(stderr, "Unmarshalling failed!\\ "); return 1; } printf("Marshaling value: %d, unmarshalling value: %d\\ ", num, decoded_num); return 0; }
9. xdr_string
9.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_string(XDR *xdrs, char **cpp, u_int maxsize); |
Used to encode or decode string data |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- cpp: Pointer to the string data to be encoded or decoded
- maxsize: the maximum allowed string length
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() {<!-- --> char *str = "hello world!"; char buf[1024]; XDR xdrs; xdrmem_create( & xdrs, buf, sizeof(buf), XDR_ENCODE); if (!xdr_string( &xdrs, &str, strlen(str))) {<!-- --> printf("Serialization failed.\\ "); } else {<!-- --> printf("Serialized data:\\ "); for (int i = 0; i < xdr_getpos( & amp; xdrs); i ++ ) {<!-- --> printf(" x ", buf[i]); } printf("\\ "); } return 0; }
10. xdr_string_ptr
10.1 Function Description
Function declaration | Function function |
---|---|
bool_t xdr_string_ptr(XDR *xdrs, char **cpp, const uint_t maxsize); |
Used to encode or decode pointers to strings |
Parameters:
- xdrs : A pointer to an
XDR
structure to encode or decode data- cpp: Pointer to the string data to be encoded or decoded
- maxsize: the maximum allowed string length
Return value:
- If the encoding or decoding is successful, the return value is
TRUE
;- Otherwise the return value is
FALSE
.
10.2 Demonstration example
#include <stdio.h> #include <rpc/xdr.h> int main() {<!-- --> char str[50] = "Hello, world!"; XDR xdrs; xdrmem_create( & xdrs, str, sizeof(str), XDR_ENCODE); xdr_string_ptr( &xdrs, &str, sizeof(str)); printf("Encoded string: %s\\ ", str); char decoded_str[50]; xdrmem_create( & xdrs, str, sizeof(str), XDR_DECODE); xdr_string_ptr( & amp; xdrs, & amp; decoded_str, sizeof(decoded_str)); printf("Decoded string: %s\\ ", decoded_str); return 0; }