Encyclopedia of C language functions — functions starting with x (4)

C language function collection

This article introduces the complete set of C language functions — functions starting with x

1. xdr_struct

1.1 Function Description

Function Declaration Function Function
bool_t xdr_struct(XDR *xdrs, void *addr, ...); for encoding or decoding structures Body data

Parameters:

  • xdrs : Pointer to the XDR data structure representing the data stream to be encoded or decoded
  • addr: Pointer to the structure variable to be encoded or decoded
  • … : Arbitrary number and type of parameters used to describe the members of the structure and their serialization format

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>

struct person
{<!-- -->
    char name[20];
    int age;
};

int main()
{<!-- -->
    struct person p1 = {<!-- -->"Huazie", 18};
    struct person p2;

    XDR xdrs;
    char buffer[100];

    // Initialize the xdr stream
    xdrmem_create( & xdrs, buffer, sizeof(buffer), XDR_ENCODE);

    // encode the p1 structure
    if (!xdr_struct( & amp;xdrs, "person", (char *) & amp;p1, sizeof(p1))) {<!-- -->
        printf("Encoding failed\
");
        return -1;
    }

    // reset xdr stream
    xdr_destroy( & xdrs);
    xdrmem_create( & xdrs, buffer, sizeof(buffer), XDR_DECODE);

    // decode structure
    if (!xdr_struct( & amp;xdrs, "person", (char *) & amp;p2, sizeof(p2))) {<!-- -->
        printf("Decoding failed\
");
        return -1;
    }

    // output result
    printf("p1: name=%s, age=%d\
", p1.name, p1.age);
    printf("p2: name=%s, age=%d\
", p2.name, p2.age);

    return 0;
}

In the sample code above,
-First, we define two variables p1 and p2 of type person, where p1 is initialized to {name="Huazie", age=18}.

  • Then, use the xdrmem_create() function to create a xdr stream and combine it with a buffer with a size of 100 bytes code> bind;
  • Next, use the xdr_struct() function to encode the p1 structure;
  • Then, the xdr_destroy() function is called to reset the xdr stream, and the xdrmem_create() function is used to rebind it to the buffer on;
  • Then, use the xdr_struct() function to decode the encoded data into the p2 variable, and check whether the decoding is successful.
  • Finally, the values of the member variables corresponding to the p1 and p2 structures are output.

2. xdr_u_char

2.1 Function Description

Function Declaration Function Function
bool_t xdr_u_char(XDR *xdrs, u_char *up); Used to encode or decode unsigned char type value

Parameters:

  • xdrs : A pointer to the XDR structure
  • up : pointer to a variable of type unsigned char

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>

int main()
{<!-- -->
    XDR xdrs;
    u_char val = 'a';
    u_char result;

    char buf[1024];
    xdrmem_create( & xdrs, buf, sizeof(buf), XDR_ENCODE);
    if (!xdr_u_char( &xdrs, &val))
    {<!-- -->
        printf("Encoding failed!");
        return 1;
    }
    // Get the length of the encoded data
    int len = xdr_getpos( & xdrs);

    xdrmem_create( & xdrs, buf, len, XDR_DECODE);
    if (!xdr_u_char( &xdrs, &result))
    {<!-- -->
        printf("Decoding failed!");
        return 1;
    }

    printf("Value before encoding: %c\
", val);
    printf("Encoded length: %d\
", len);
    printf("Decoded value: %c\
", result);
    return 0;
}

In the example above,

  • First, we define a variable val of type unsigned char and initialize it to the character 'a';
  • Then, use the xdrmem_create() function to create a XDR stream object xdrs, and specify the encoding method of the stream as XDR_ENCODE
  • Then, call the xdr_u_char() function to encode val, if the encoding is successful, call the xdr_getpos() function to get the length of the encoded data, The length will be used as a parameter when decoding;
  • Then, call the xdrmem_create() function again to create a XDR stream object xdrs and specify the encoding method of the stream as XDR_DECODE
  • Then, call the xdr_u_char() function to decode the encoded data and store the result in the variable result;
  • Finally, the value before encoding, the length after encoding and the value after decoding are output.

3. xdr_u_hyper

3.1 Function Description

Function Declaration Function Function
bool_t xdr_u_hyper(XDR *xdrs, u_hyper *uhp); for encoding and decoding u_hyper type of data

Parameters:

  • xdrs : A pointer to an XDR structure to encode or decode data
  • uhp: pointer to a variable of type u_hyper

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()
{<!-- -->
    XDR xdrs;
    u_hyper value = 12345678901234567890ULL; // define a 64-bit unsigned integer
    char buffer[16];
    char *bufptr = buffer;

    xdrmem_create( & xdrs, buffer, sizeof(buffer), XDR_ENCODE);
    if (!xdr_u_hyper( &xdrs, &value))
    {<!-- -->
        printf("Serialization error");
        return 1;
    }

    // print the encoded data
    for (int i = 0; i < sizeof(buffer); i ++ ) {<!-- -->
        printf("x", buffer[i]);
    }
    printf("\
");

    xdrmem_create( & xdrs, buffer, sizeof(buffer), XDR_DECODE);
    if (!xdr_u_hyper( &xdrs, &value))
    {<!-- -->
        printf("Deserialization error");
        return 1;
    }

    printf("%llu\
", value); // print the decoded value

    return 0;
}

In the sample code above,

  • First, we define a 64 bit unsigned integer variable value and assign it the value 12345678901234567890ULL;
  • Then, use the xdrmem_create() function to create a XDR stream object xdrs, and specify the encoding method of the stream as XDR_ENCODE
  • Then, call the xdr_u_hyper() function to perform the encoding operation; if the encoding is successful, the encoded data will be printed out;
  • Then, call the xdrmem_create() function again to create a XDR stream object xdrs and specify the encoding method of the stream as XDR_DECODE
  • Then, call the xdr_u_hyper() function to decode the encoded data and store the result in the variable value;
  • Finally, output the decoded value.

4. xdr_u_int

4.1 Function Description

Function Declaration Function Function
bool_t xdr_u_int(XDR *xdrs, u_int *a); for encoding or decoding unsigned integer

Parameters:

  • xdrs : A pointer to an XDR structure to encode or decode data
  • a: A pointer to an unsigned integer to encode or decode

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>

int main(void)
{<!-- -->
    // Encode unsigned integer to XDR format byte stream
    unsigned int num = 123456789;
    XDR xdr;
    char buf[4];
    xdrmem_create( & xdr, buf, sizeof(buf), XDR_ENCODE);
    if (!xdr_u_int( &xdr, &num))
    {<!-- -->
        printf("Failed to encode unsigned int.\
");
        return 1;
    }
    printf("Encoded bytes: x x x x\
", buf[0], buf[1], buf[2], buf[3]);

    // Decode unsigned integer from XDR format byte stream
    unsigned int decoded_num;
    XDR xdr2;
    xdrmem_create( & xdr2, buf, sizeof(buf), XDR_DECODE);
    if (!xdr_u_int( &xdr2, &decoded_num))
    {<!-- -->
        printf("Failed to decode unsigned int.\
");
        return 1;
    }
    printf("Decoded number: %u\
", decoded_num);

    return 0;
}

In the example program above,

  • First, we first create an unsigned integer variable num and set it to 123456789;
  • Then, use the xdrmem_create() function to create an XDR object xdr and bind it to buf;
  • Next, use the xdr_u_int() function to encode the unsigned integer num into a XDR format byte stream and write it to the bufferbuf in. If an error occurs during the encoding process, output an error message and return 1; if the encoding is successful, output the encoded byte stream;
  • Then, use the xdrmem_create() function to create another XDR object xdr2;
  • Then, call the xdr_u_int() function to decode the unsigned integer in the XDR format byte stream. If an error occurs during decoding, output an error message and return 1;
  • Finally, if the decoding is successful, the decoded unsigned integer is output.

5. xdr_u_long

5.1 Function Description

Function Declaration Function Function
bool_t xdr_u_long(XDR *xdrs, u_long *objp); Used to encode or decode unsigned long integer value

Parameters:

  • xdrs : A pointer to an XDR structure to encode or decode data
  • objp: Pointer to the unsigned long value 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()
{<!-- -->
    XDR xdr;
    u_long value = 12345;

    // Open the XDR data stream
    xdrmem_create( & xdr, NULL, 0, XDR_ENCODE);

    // Encode an unsigned long value into the XDR data stream
    if (!xdr_u_long( & amp;xdr, & amp;value)) {<!-- -->
        printf("Unable to encode value.\
");
        return 1;
    }

    // Close the XDR data stream
    xdr_destroy( & xdr);

    return 0;
}

6. xdr_u_short

6.1 Function Description

Function Declaration Function Function
bool_t xdr_u_short(XDR *xdrs, unsigned short *objp); Used to encode or decode unsigned short type data

Parameters:

  • xdrs : A pointer to an XDR structure to encode or decode data
  • objp: pointer to unsigned short

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 <rpc/rpc.h>

#define ARRAY_SIZE 3

int main(void)
{<!-- -->
    XDR xdrs;
    char buffer[1024];
    unsigned short source_array[ARRAY_SIZE] = {<!-- -->1, 2, 3};
    unsigned short dest_array[ARRAY_SIZE];

    xdrmem_create( & xdrs, buffer, sizeof(buffer), XDR_ENCODE);
    for (int i = 0; i < ARRAY_SIZE; + + i)
    {<!-- -->
        if (!xdr_u_short( &xdrs, &source_array[i]))
        {<!-- -->
            printf("Encoding failed\
");
            return 1;
        }
    }
    xdr_destroy( & xdrs);
    printf("Encoding result:\
");
    for (int i = 0; i < xdr_getpos( & xdrs); + + i)
    {<!-- -->
        printf(" X ", buffer[i]);
    }
    printf("\
");

    xdrmem_create( & xdrs, buffer, sizeof(buffer), XDR_DECODE);
    for (int i = 0; i < ARRAY_SIZE; + + i)
    {<!-- -->
        if (!xdr_u_short( &xdrs, &dest_array[i]))
        {<!-- -->
            printf("Decoding failed\
");
            return 1;
        }
    }
    xdr_destroy( & xdrs);
    printf("Decoding result:\
");
    for (int i = 0; i < ARRAY_SIZE; + + i)
    {<!-- -->
        printf("%u ", dest_array[i]);
    }
    printf("\
");

    return 0;
}

7. xdr_vector

7.1 Function Description

Function Declaration Function Function
bool_t xdr_short(XDR *xdrs, short *sp); Used to encode or decode a variable length vector

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.

7.2 Demonstration example

#include <stdio.h>
#include <stdlib.h>
#include <rpc/types.h>
#include <rpc/xdr.h>

#define MAXSIZE 100

void error(char *s)
{<!-- -->
    fprintf(stderr, "error: %s\
", s);
    exit(1);
}

int main()
{<!-- -->
    XDR xdrs;
    char buffer[MAXSIZE];
    int values[] = {<!-- -->1, 2, 3, 4, 5};
    u_int size = sizeof(values)/sizeof(int);

    xdrmem_create( & xdrs, buffer, MAXSIZE, XDR_ENCODE);
    if (!xdr_vector( &xdrs, (char *) values, &size, MAXSIZE, sizeof(int), (xdrproc_t) xdr_int))
    {<!-- -->
        error("serialization failed");
    }

    xdrmem_create( & xdrs, buffer, MAXSIZE, XDR_DECODE);
    if (!xdr_vector( &xdrs, (char *) values, &size, MAXSIZE, sizeof(int), (xdrproc_t) xdr_int))
    {<!-- -->
        error("deserialization failed");
    }

    for (int i = 0; i < size; i ++ )
    {<!-- -->
        printf("%d ", values[i]);
    }
    printf("\
");

    return 0;
}

8. xdr_void

8.1 Function Description

Function Declaration Function Function
bool_t xdr_void(void); Used to encode or decode empty values [void type]

Return value:
Always returns true since no value was encoded or decoded

8.2 Demonstration example

#include <stdio.h>
#include <stdlib.h>
#include <rpc/rpc.h>

int main()
{<!-- -->
    // Create an xdr_void data structure
    XDR xdr;
    xdrmem_create( & xdr, NULL, 0, XDR_ENCODE);

    // Serialize null values to xdr_void type
    if (!xdr_void( & amp;xdr, NULL))
    {<!-- -->
        fprintf(stderr, "Serialization failed\
");
        exit(1);
    }

    // print the serialized result
    printf("Serialization result:\
");
    int i;
    for (i = 0; i < xdr_getpos( & amp;xdr); i ++ ) {<!-- -->
        printf("x", ((unsigned char *)xdr_inline( & amp;xdr, i))[0]);
    }
    printf("\
");

    // deserialize xdr_void type
    xdr_destroy( & xdr);
    xdrmem_create( & amp; xdr, xdr_getpos( & amp; xdr), xdr_getpos( & amp; xdr), XDR_DECODE);

    // Attempt to deserialize non-null data to xdr_void type, expected to fail
    char data[] = {<!-- -->0x01};
    if (xdr_bytes( &xdr, &data, &i, sizeof(data)) != FALSE) {<!-- -->
        fprintf(stderr, "Attempt to deserialize non-null data to xdr_void type, but succeeded\
");
        exit(1);
    }

    // print deserialization result
    printf("Deserialization result: empty value\
");

    return 0;
}