Line data Source code
1 : #ifndef _SENTINEL_MINIPOT_UTILS_H_ 2 : #define _SENTINEL_MINIPOT_UTILS_H_ 3 : 4 : #include <string.h> 5 : #include <stdint.h> 6 : #include <logc.h> 7 : #include <minipot_sentinel_msg_packer.h> 8 : 9 : #define MINIPOT_MIN(a,b) \ 10 : ({ __typeof__ (a) _a = (a); \ 11 : __typeof__ (b) _b = (b); \ 12 : _a > _b ? _b : _a; }) 13 : 14 : #define MINIPOT_TRACE_FUNC trace(__func__) 15 : #define MINIPOT_TRACE_FUNC_FD(fd) trace("%s (FD: %d)", __func__, fd) 16 : #define MINIPOT_TRACE_FUNC_P(FORMAT, ...) trace("%s (" FORMAT ")", __func__, __VA_ARGS__) 17 : 18 : #define MINIPOT_FLOW_GUARD(cmd) do { \ 19 : if (cmd) \ 20 : return -1; \ 21 : } while (0) 22 : 23 : struct minipot_token { 24 : const uint8_t *start_ptr; 25 : size_t len; 26 : }; 27 : 28 : // Sets file descriptor as nonblocking. 29 : // Returns 0 if setting was sucessfull otherwise -1. 30 0 : int minipot_set_nonblock(int); 31 : 32 : // Sends data to a socket. If successful returns 0 othervise -1. 33 0 : int minipot_send_all(int fd, const void *data, size_t len); 34 : 35 : // Validates whether given buffer can be decoded as UTF-8 string NOT containing 36 : // NULL characters. Uses naive implementation of canonical UTF-8 automaton from: 37 : // https://bjoern.hoehrmann.de/utf-8/decoder/dfa/. 38 : // If data represents UTF-8 string and do NOT contain NULL byte(s) returns 0 39 : // otherwise -1. Data with length 0 are valid. 40 0 : int minipot_check_data(const uint8_t *buff, size_t len); 41 : 42 : // Writes data to FD. If successful returns 0 othervise -1. 43 0 : int minipot_write_all(int fd, const void *data, size_t len); 44 : 45 : // Packs a message with a packer and writes it to fd. Returns 0 if sucessfull 46 : // otherwise -1. 47 0 : int minipot_report(int, minipot_sentinel_msg_packer_t, 48 : struct minipot_sentinel_msg *); 49 : 50 : // Skips given bytes from given string starting at first byte of the string. 51 : // Returns pointer to first byte in string not contained in bytes to skip. 52 : // If str_len or to_skip_len is 0 str is returned. 53 : // If all bytes/chars are skipped it returns pointer to the fisrt byte after 54 : // the string. 55 0 : const uint8_t *minipot_skip_sel_bytes(const uint8_t *str, size_t str_len, 56 : const uint8_t *to_skip, size_t to_skip_len); 57 : 58 : // Finds first occurence of given bytes in given string. 59 : // Returns pointer to first character in the string matching any byte from find. 60 : // If str_len is 0 returns str. 61 : // If find_len is 0 or if finds no chars returns pointer to the first char 62 : // after the str. 63 0 : const uint8_t *minipot_find_first_occur(const uint8_t *str, size_t str_len, 64 : const uint8_t *find, size_t find_len); 65 : 66 : // Splits string (NOT ZERO TERMINATED C-STRING) into tokens according to given 67 : // separators. Each token in saved in output array of token structs. 68 : // There can be more separators between two tokens. Separators before first and 69 : // after last token are ignored. 70 : // It fills up maximum token_len tokens even if there are more tokens in the string. 71 : // Caller is responsible for allocation of enough tokens structs and their free. 72 : // If str_len is 0 returns 0 and fills no token struct. 73 : // If separator_len is 0 returns 1 - the whole string is one token. 74 : // Returns number of tokens found in the string. 75 0 : size_t minipot_tokenize(const uint8_t *str, size_t str_len, 76 : struct minipot_token *tokens, size_t tokens_len, const uint8_t *separators, 77 : size_t sep_len); 78 : 79 : #endif