cryptnox-pos
1.0.0
Standalone USDC payment terminal firmware (ESP32 + Cryptnox smart card)
Toggle main menu visibility
Loading...
Searching...
No Matches
json_out.h
Go to the documentation of this file.
1
/*
2
* SPDX-License-Identifier: LGPL-3.0-or-later
3
* Copyright (c) 2026 Cryptnox SA
4
*/
5
22
23
#ifndef JSON_OUT_H
24
#define JSON_OUT_H
25
26
#include <stddef.h>
27
#include <stdio.h>
28
46
static
inline
size_t
json_escape
(
char
*out,
size_t
n,
const
char
*val)
47
{
48
if
((out == NULL) || (n == 0U)) {
return
0U; }
49
out[0] =
'\0'
;
50
if
(val == NULL) {
return
0U; }
51
52
size_t
w = 0U;
53
for
(
const
char
*p = val; *p !=
'\0'
; p++) {
54
const
unsigned
char
c = (
unsigned
char)*p;
55
/* Room for the worst case (6) plus the NUL before committing to anything,
56
* so the output never ends half way through an escape sequence. */
57
if
((w + 7U) > n) {
break
; }
58
if
((c ==
'"'
) || (c ==
'\\'
)) {
59
out[w++] =
'\\'
;
60
out[w++] = (char)c;
61
}
else
if
(c < 0x20U) {
62
const
int
k = snprintf(&out[w], n - w,
"\\u%04x"
, (
unsigned
)c);
63
if
(k < 0) {
break
; }
64
w += (size_t)k;
65
}
else
{
66
out[w++] = (char)c;
67
}
68
}
69
out[w] =
'\0'
;
70
return
w;
71
}
72
73
#endif
/* JSON_OUT_H */
json_escape
static size_t json_escape(char *out, size_t n, const char *val)
Copy val into out, escaping what JSON requires.
Definition
json_out.h:46
main
json_out.h
Generated by
1.17.0