SDK สำหรับ TESAIoT Dev Kit
คู่มืออ้างอิง API และ Tutorial (MTB & µPython)
Loading...
Searching...
No Matches
nus_base64.h
Go to the documentation of this file.
1/*******************************************************************************
2 * File Name: nus_base64.h
3 *
4 * Description: Stateful base64 decoder for the Bento Desktop Buddy folder
5 * push protocol (Bento forked this protocol under its own
6 * branding — see Bento_Buddy/SPEC.md §3.1.1 Not
7 * Anthropic-compatible). Chunks arrive one "chunk" command at
8 * a time —
9 * each chunk carries a "d":"<b64>" field that may split a 4-char
10 * quantum across chunk boundaries. This decoder keeps the
11 * leftover 1..3 bytes between calls so the caller can feed each
12 * chunk as-received.
13 *
14 * Output goes through a consumer callback so the caller can
15 * stream to flash (LittleFS) without buffering the whole file
16 * in RAM — important for Bento's pack size limit of 1.8 MB.
17 *
18 ******************************************************************************/
19
20#ifndef NUS_BASE64_H
21#define NUS_BASE64_H
22
23#include <stdbool.h>
24#include <stddef.h>
25#include <stdint.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31typedef int (*nus_b64_sink_t)(const uint8_t *bytes, size_t len, void *ctx);
32
33typedef struct {
34 uint8_t carry[4]; /* up to 3 half-quantum bytes + 1 spare */
35 uint8_t carry_n; /* 0..3 */
36 uint32_t bytes_out; /* total decoded bytes written */
37 bool got_padding; /* '=' seen — no more input expected */
39 void *sink_ctx;
41
43
44/* Feed one chunk of base64 text. Returns 0 on success, negative on error
45 * (invalid char, sink failure, stray data after padding). */
46int nus_b64_feed(nus_b64_state_t *s, const uint8_t *b64, size_t len);
47
48/* Flush the last quantum (if any trailing carry exists). Called at file_end. */
50
51#ifdef __cplusplus
52}
53#endif
54
55#endif /* NUS_BASE64_H */
void nus_b64_init(nus_b64_state_t *s, nus_b64_sink_t sink, void *ctx)
รีเซ็ตสถานะ decoder ที่ผู้เรียกเป็นเจ้าของแล้วต่อ sink เข้าไป ไม่มีการพักข้อมูลทั้งก้อนไว้ใน RAM
int nus_b64_flush(nus_b64_state_t *s)
เขียนค่าที่ค้างท้ายลงจริง ให้เรียกตอน file_end
int nus_b64_feed(nus_b64_state_t *s, const uint8_t *b64, size_t len)
ป้อนข้อความ base64 ทีละ chunk สถานะจะพา quantum ขนาด 4 อักขระที่ถูกแบ่งข้าม chunk ไปด้วย
int(*) nus_b64_sink_t(const uint8_t *bytes, size_t len, void *ctx)
Definition nus_base64.h:31
Definition nus_base64.h:33
uint8_t carry_n
Definition nus_base64.h:35
uint32_t bytes_out
Definition nus_base64.h:36
void * sink_ctx
Definition nus_base64.h:39
uint8_t carry[4]
Definition nus_base64.h:34
nus_b64_sink_t sink
Definition nus_base64.h:38
bool got_padding
Definition nus_base64.h:37