miketsai 8fb28087c5 feat(ble): add DrawBoxEnable SET cmd; per-board handshake key; move headers
- event_recorder.c: add SET DRAWBOXENABLE 0/1 BLE command (runtime toggle,
  INI-persistent); expose g_dwDrawBoxEnable extern
- kdp2_host_stream.c: guard draw-box and seg-overlay rendering behind
  g_dwDrawBoxEnable so SET DRAWBOXENABLE 0 cleanly suppresses both
- handshake.c: derive key from runtime VMF_NNM_Get_Kn_Number() instead of
  compile-time constant; each board gets a unique handshake key
- kp_firmware.c: remove startup handshake key print (key is now per-board,
  fixed expected value is no longer meaningful)
- move base64.h / sha256.h / handshake.h from src/host_stream/ to
  include/host_stream/ (proper include path for shared headers)
2026-08-04 18:43:54 +08:00

97 lines
3.6 KiB
C

/*
* base64.h — Standalone RFC 4648 Base64 encode/decode, header-only (static inline).
* No external dependencies.
*/
#ifndef BASE64_H
#define BASE64_H
#include <stdint.h>
#include <stddef.h>
#include <string.h>
static const char B64_ENC[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/*
* base64_encode - Encode src_len bytes to a NUL-terminated Base64 string.
* dst must be at least ((src_len + 2) / 3) * 4 + 1 bytes.
*/
static inline void base64_encode(const uint8_t *src, size_t src_len, char *dst)
{
size_t i = 0, j = 0;
while (i + 2 < src_len) {
uint32_t v = ((uint32_t)src[i] << 16)
| ((uint32_t)src[i+1] << 8)
| (uint32_t)src[i+2];
dst[j++] = B64_ENC[(v >> 18) & 0x3F];
dst[j++] = B64_ENC[(v >> 12) & 0x3F];
dst[j++] = B64_ENC[(v >> 6) & 0x3F];
dst[j++] = B64_ENC[ v & 0x3F];
i += 3;
}
if (i < src_len) {
uint32_t v = (uint32_t)src[i] << 16;
if (i + 1 < src_len) v |= (uint32_t)src[i+1] << 8;
dst[j++] = B64_ENC[(v >> 18) & 0x3F];
dst[j++] = B64_ENC[(v >> 12) & 0x3F];
dst[j++] = (i + 1 < src_len) ? B64_ENC[(v >> 6) & 0x3F] : '=';
dst[j++] = '=';
}
dst[j] = '\0';
}
/*
* base64_decode - Decode a Base64 string to bytes.
* Returns number of decoded bytes, or -1 on invalid input.
* dst must be at least (strlen(src) / 4) * 3 bytes.
*/
static inline int base64_decode(const char *src, uint8_t *dst)
{
/* Lookup table: 0-63=value, 64='=' padding, -1=invalid */
static const int8_t LUT[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 64, -1, -1, /* 0x30 */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0 */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0 */
};
int len = (int)strlen(src);
if (len % 4 != 0) return -1;
int out = 0;
for (int i = 0; i < len; i += 4) {
int8_t a = LUT[(uint8_t)src[i]];
int8_t b = LUT[(uint8_t)src[i+1]];
int8_t c = LUT[(uint8_t)src[i+2]];
int8_t d = LUT[(uint8_t)src[i+3]];
if (a < 0 || b < 0 || c < 0 || d < 0) return -1;
uint32_t v = ((uint32_t)a << 18)
| ((uint32_t)b << 12)
| ((c == 64) ? 0 : ((uint32_t)c << 6))
| ((d == 64) ? 0 : (uint32_t)d);
dst[out++] = (uint8_t)(v >> 16);
if (c != 64) dst[out++] = (uint8_t)(v >> 8);
if (d != 64) dst[out++] = (uint8_t)v;
}
return out;
}
#endif /* BASE64_H */