Files
ZephCore/zephcore/helpers/TxtDataHelpers.h
T

100 lines
3.1 KiB
C++

/*
* SPDX-License-Identifier: MIT
* ZephCore TxtDataHelpers - text message type constants and string helpers
*/
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#define TXT_TYPE_PLAIN 0 // a plain text message
#define TXT_TYPE_CLI_DATA 1 // a CLI command -or- reply
#define TXT_TYPE_SIGNED_PLAIN 2 // plain text, signed by sender
#define TXT_TYPE_CLI_COMMAND 3 // a CLI command (explicitly), v1.18+
#define DATA_TYPE_RESERVED 0x0000 // reserved for future use
#define DATA_TYPE_DEV 0xFFFF // developer namespace for experimenting with group/channel datagrams
class StrHelper {
public:
static void strncpy(char *dest, const char *src, size_t buf_sz) {
if (buf_sz == 0) return;
size_t i = 0;
while (i < buf_sz - 1 && src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
static void strzcpy(char *dest, const char *src, size_t buf_sz) {
if (buf_sz == 0) return;
size_t i = 0;
while (i < buf_sz - 1 && src[i] != '\0') {
dest[i] = src[i];
i++;
}
while (i < buf_sz) {
dest[i++] = '\0';
}
}
/* Trim a fixed-precision decimal down to its shortest exact form:
* "250.000" -> "250", "62.500" -> "62.5". Matches how Arduino MeshCore
* renders float CLI values, so app-side parsers see the same text.
* No-op on strings without a '.'. */
static void stripTrailingZeros(char *s) {
if (s == nullptr || strchr(s, '.') == nullptr) return;
size_t i = strlen(s);
while (i > 0 && s[i - 1] == '0') i--;
if (i > 0 && s[i - 1] == '.') i--;
s[i] = '\0';
}
/* A received text is its characters, then zero padding up to the end of the
* decrypted block. True when every byte before the first NUL is a printable
* character (0x20 and up, not 0xFF) or tab, LF, CR, and every byte from that
* NUL to `len` is zero. With `plain_tail` (a TXT_TYPE_PLAIN text) the NUL may
* be followed by the extended attempt number (4 to 254), or by that number or
* zero and then the padding hint 0x5A that zcpfs builds add; zeros after it. */
static bool isTextThenPadding(const uint8_t *text, size_t len, bool plain_tail = false) {
size_t i = 0;
for (; i < len && text[i] != 0; i++) {
uint8_t c = text[i];
if (c == 0xFF || (c < 0x20 && c != '\t' && c != '\n' && c != '\r')) return false;
}
if (plain_tail && i + 1 < len) {
bool attempt = text[i + 1] >= 4 && text[i + 1] != 0xFF;
bool hint = i + 2 < len && text[i + 2] == 0x5A && (attempt || text[i + 1] == 0);
if (hint) {
i += 3; /* the NUL, the attempt number or zero, the hint */
} else if (attempt) {
i += 2; /* the NUL and the attempt number */
}
}
for (; i < len; i++) {
if (text[i] != 0) return false;
}
return true;
}
/* True when every byte is zero: what follows a fixed-size field in a
* decrypted block is the cipher's padding. */
static bool isZeroPadding(const uint8_t *p, size_t len) {
for (size_t i = 0; i < len; i++) {
if (p[i] != 0) return false;
}
return true;
}
static bool isBlank(const char *str) {
if (str == nullptr) return true;
while (*str) {
if (*str != ' ' && *str != '\t' && *str != '\n' && *str != '\r') return false;
str++;
}
return true;
}
};