Base Module¶
This module contains common functions and classes used throughout the rest of the library
-
C2P_PROTOCOL_MAJOR_VERSION
¶ This macro defines the major version number. A change here indicates a major change or release, and may be breaking. In a scheme x.y.z, it would be x
-
C2P_PROTOCOL_MINOR_VERSION
¶ This macro defines the minor version number. It refers specifically to minor protocol revisions, and all changes here are API compatible (after 1.0), but not compatbile with other nodes. In a scheme x.y.z, it would be y
-
C2P_NODE_VERSION
¶ This macro defines the patch version number. It refers specifically to node policies, and all changes here are backwards compatible. In a scheme x.y.z, it would be z
-
C2P_VERSION
¶ This macro is a string literal. It combines all the above macros into a single string. It will generate whatever a string literal would normally be interpreted as in that context.
-
C2P_DEBUG_FLAG
¶ This macro indicates whether cp2p should generate debug prints. If you define this as anything it will print
-
unsigned long
getUTC
()¶ Returns the current UNIX second in UTC
-
static void
get_user_salt
(char result[36])¶ This generates a uuid4 for use in this library.
result
should be of length 36
-
static unsigned long long
unpack_value
(const char *str, size_t len)¶ Unpacks a big-endian binary value into an unsigned long long
Parameters: - str – The value you’d like to unpack
- len – The length of this value
Returns: The value this string contained
Warning
Integer overflow will not be accounted for
-
static void
pack_value
(size_t len, char *arr, unsigned long long i)¶ Packs an unsigned long long into a big-endian binary buffer of length len
Parameters: - len – The length of the string you’d like to produce
- arr – The buffer you would like to fill
- i – The value you’d like to pack
Warning
Integer overflow will not be accounted for
-
static int
sanitize_string
(char *str, size_t *len, int sizeless)¶ Mutates str to be clean for processing by process_string.
Parameters: - str – The string you wish to mutate
- len – The length of said string
- sizeless – A boolean which indicates whether the string has a standard size header
Returns: -1
if str was invalid for processing,0
if all went well
-
static int
decompress_string
(char *str, size_t len, char *result, size_t *res_len, char **compressions, size_t *compression_sizes, size_t num_compressions)¶ Puts a decompressed copy of str into result, and updates res_len to contain its length.
Parameters: - str – The string you wish to decompress
- len – The length of this string
- result – A pointer to the resulting string
- res_len – A pointer to the length of the result
- compressions – The list of possible compression methods
- compression_sizes – The length of each compression method
- num_compressions – The number of compression methods
Returns: -1
if decompression failed,0
if all went wellWarning
If you do not
free()
result
you will develop a memory leak
-
static int
process_string
(const char *str, size_t len, char **packets, size_t **lens, size_t *num_packets)¶ Transforms a serialized string into an array of packets. This is formatted as an array of strings, an array of lengths, and a number of packets. You must provide a pointer to these. Packets must be initialized as an array of
char *
.Parameters: - str – The string to deserialize
- len – The length of this string
- packets – A pointer to the returned array of packets. This will be initialized for you
- lens – A pointer to the returned array of packet lengths. This will be initiaized for you
Num_packets: A pointer to the number of packets. This will be initialized for you
Returns: 0
if successful, any other number if not. If it returns non-0
,packets
andlens
will point toNULL
Warning
If you do not
free()
packets
andlens
you will develop a memory leak