█▀▀▄ ▄▀▀█ ▄▀▀▀▀▀▀▀▄ ▄▀▀▀▀▀▀▀▄ ▄▀▀▀▀▀▀▀▄ ▄▀▀▀▀▀▀▀▄ █▀▀▀█▀▀▀█ ▀▀▄ ▀ ▄▀▀ █ ▄▄ █ █ ▄ █ █ ▄ ▄ █ █ ▄ ▄ █ █ █ █ █ █ █ █████ █ █ █ █ █ █ █ █ █ █ █ █ █ ▄▄▀ ▄ ▀▄▄ █ ▀▀ █ █ ▀ █ █ █ █ █ █ █ █ █ █ █ █ █▄▄▀ ▀▄▄█ ██ ▀▄▄▄▄▄▄▄▀ ▀▄▄▄▄▄▄▄▀ █▄▄█▄█▄▄█ █▄▄█▄█▄▄█ ██ █▄▄▄█▄▄▄█
Cross platform C/C++ utilities for communication.
Table of Contents
Put x.comm.h
into your project.
x.comm.h
is a single header file following the style of the
stb libraries, which means users must define
the macro X_COMM_H_IMPLEMENTATION
in one and only one of their source files:
// in some foo.c file
#define X_COMM_H_IMPLEMENTATION
#include "x.comm.h"
// in some bar.h or bar.c file
#include "x.comm.h"
Calculating the CRC32 checksum of a data array.
uint32_t x_crc32(const void* data, const size_t size, const uint32_t* prev)
data
: The data array from which the checksum is calculated.size
: The length of the data array when it is casted touint8_t*
.prev
: Previous CRC32 result. PassNULL
/nullptr
to this argument if no previous CRC32 result is available.
- The CRC32 checksum.
Calculating the Internet checksum (following RFC1071) of a data array.
uint16_t x_rfc1071(const void* data, const size_t size)
data
: The data array from which the checksum is calculated.size
: The length of the data array when it is casted touint8_t*
.
- The Internet checksum.
Calculating the XOR checksum of a data array.
uint8_t x_xor(const void* data, const size_t size)
data
: The data array from which the checksum is calculated.size
: The length of the data array when it is casted touint8_t*
.
- The XOR checksum.
Header of a packet.
typedef struct _x_hdr_
{
uint16_t sof; // start of frame
uint16_t ctl; // control code
uint32_t opt; // option, just use it freely
uint64_t cnt; // total count of packets
uint64_t idx; // index of current packet
uint64_t dsz; // size of the data chunk
uint64_t cks; // checksum of packet
} x_hdr;
#ifndef X_HDR_SOF
#define X_HDR_SOF (0x55AA) // A commonly used start of frame.
#endif
#ifndef X_HDR_CNT_INF
#define X_HDR_CNT_INF UINT64_MAX // Can be used for x_hdr::cnt.
#endif
#define X_HDR_INIT {X_HDR_SOF, 0, 0, 0, 0, 0, 0} // Can be used to initialize x_hdr.
Normally the header and body of a packet can be used separately, but providing a simple encapsulation of them does no harm.
typedef struct _x_pkt_
{
x_hdr head;
void* body;
} x_pkt;
#define X_PKT_SOF X_HDR_SOF
#define X_PKT_CNT_INF X_HDR_CNT_INF
#define X_PKT_INIT {X_HDR_INIT, NULL} // Can be used to initialize x_pkt.
Distributed under the Mulan PSL v2 license. See LICENSE for more information.