CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_str_conv.cpp
Go to the documentation of this file.
1
5
6#include "host_str_conv.h"
7
10#include "cdc_core/Cp437.h"
11
12#include <cstring>
13
14namespace cdc::plugin_manager {
15
16std::string toDisplay(const char* utf8)
17{
18 if (!utf8 || !*utf8) return std::string();
19 size_t cap = std::strlen(utf8) + 1;
20 std::string out;
21 out.resize(cap);
23 out.resize(std::strlen(out.c_str()));
24 return out;
25}
26
27int copyUtf8(const char* cp437, char* out, size_t out_size)
28{
29 if (!out || out_size == 0) return HOST_ERR_INVALID_ARG;
30 std::string u = cp437 ? cdc::core::cp437::toUtf8(cp437) : std::string();
31 size_t n = u.size();
32 if (n >= out_size) {
33 n = out_size - 1;
34 // Back off so the buffer never ends inside a multi-byte UTF-8 sequence.
35 while (n > 0 && (static_cast<unsigned char>(u[n]) & 0xC0) == 0x80) --n;
36 }
37 std::memcpy(out, u.data(), n);
38 out[n] = '\0';
39 return static_cast<int>(n);
40}
41
42} // namespace cdc::plugin_manager
Canonical CP437 <-> Unicode/UTF-8 codec.
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_ERR_INVALID_ARG
Definition host_api.h:39
Internal UTF-8 <-> CP437 helpers for the plugin host API boundary.
std::string toUtf8(const char *s)
Convert CP437 bytes to a UTF-8 string.
Definition Cp437.cpp:86
std::string toDisplay(const char *utf8)
Decode a UTF-8 (with optional HTML entities) string into CP437 bytes.
int copyUtf8(const char *cp437, char *out, size_t out_size)
Encode a CP437 string into a caller buffer as UTF-8.
void decodeWebText(const char *in, char *out, size_t out_size, DisplayTarget target=DisplayTarget::Cp437)
Normalises a web payload for display: HTML named/numeric entities decode first, then UTF-8 multibyte ...
@ Cp437
GFX builtin glcdfont (default after setFont(nullptr)).