/* * Ipc.h -- dtchat's adapter contract: JSON lines, at most 1 MiB each * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * This header is also the contract for adapters written outside this tree. * The rules are in docs/reference/dtchat-adapter-protocol.txt: * - one JSON object per line, UTF-8, '\n' terminated * - at most DC_IPC_MAX_LINE bytes per line (without the '\n') * - no NUL byte, no pretty-printing, no multiple objects per line * - unknown fields are ignored; a breaking change raises DC_IPC_VERSION */ #ifndef _DtChat_Ipc_h #define _DtChat_Ipc_h #include #ifdef __cplusplus extern "C" { #endif #define DC_IPC_VERSION 1 #define DC_IPC_MAX_LINE (1024 * 1024) #define DC_JSON_MAX_DEPTH 32 /* * UTF-8 validation. Returns 1 for a valid sequence, 0 otherwise (overlong * forms, surrogates, values above U+10FFFF and truncated sequences are * invalid). N may be 0; S may be NULL only when N is 0. */ int dc_utf8_valid(const char *s, size_t n); /* * A parsed or built JSON value. Opaque; the accessors below are the API. */ typedef struct DcJson DcJson; typedef enum { DC_JSON_NULL, DC_JSON_BOOL, DC_JSON_INT, DC_JSON_DOUBLE, DC_JSON_STRING, DC_JSON_ARRAY, DC_JSON_OBJECT } DcJsonType; /* * Parse one JSON value from LEN bytes. On error returns NULL and writes a * short message (including the byte offset) into ERR when ERR is not NULL. * The parser is strict: it rejects trailing data, duplicate object keys, * unescaped control characters, invalid UTF-8, invalid escapes, a depth * above DC_JSON_MAX_DEPTH, and input larger than DC_IPC_MAX_LINE. */ DcJson *dc_json_parse(const char *text, size_t len, char *err, size_t errlen); void dc_json_free(DcJson *json); DcJsonType dc_json_type(const DcJson *json); const char *dc_json_type_name(DcJsonType type); /* Constructors; NULL on out of memory. */ DcJson *dc_json_new_null(void); DcJson *dc_json_new_bool(int value); DcJson *dc_json_new_int(long long value); DcJson *dc_json_new_double(double value); DcJson *dc_json_new_string(const char *value); /* value is UTF-8 */ DcJson *dc_json_new_array(void); DcJson *dc_json_new_object(void); /* * Object/array builders. Both take ownership of VALUE: on success it is * stored, on failure it is freed and -1 is returned. Setting an existing * object key replaces the old value. */ int dc_json_object_set(DcJson *object, const char *key, DcJson *value); int dc_json_array_append(DcJson *array, DcJson *value); /* * Accessors. They return NULL/0/-1 when JSON is NULL or the wrong type. * dc_json_string() returns a pointer into JSON, valid until it is freed. */ DcJson *dc_json_get(const DcJson *object, const char *key); const char *dc_json_string(const DcJson *json); long long dc_json_int(const DcJson *json); double dc_json_double(const DcJson *json); int dc_json_bool(const DcJson *json); size_t dc_json_array_size(const DcJson *array); DcJson *dc_json_array_get(const DcJson *array, size_t index); /* * Serialize to a newly allocated, NUL-terminated UTF-8 string without a * trailing newline. LEN, when not NULL, receives the string length. The * caller frees the result with free(3). NULL on out of memory. */ char *dc_json_write(const DcJson *json, size_t *len); /* * Line framing for a non-blocking adapter pipe. The buffer owns its * memory; dc_frame_next() returns a newly allocated line (without '\n' * and without a trailing '\r') that the caller frees with free(3), or * NULL when no complete line is buffered. */ typedef struct { char *buf; size_t len; size_t cap; } DcFrameBuf; void dc_frame_init(DcFrameBuf *frame); void dc_frame_reset(DcFrameBuf *frame); void dc_frame_free(DcFrameBuf *frame); /* * Append N bytes. Returns 0 on success, -1 when the data contains a NUL * byte or the buffered line would exceed DC_IPC_MAX_LINE. On -1 the * buffer content is undefined; the caller closes the connection. */ int dc_frame_feed(DcFrameBuf *frame, const void *data, size_t n); /* * Return the next complete line (without '\n' and without a trailing * '\r') or NULL when no line is buffered. The result is newly allocated; * the caller frees it with free(3). LEN, when not NULL, receives its * length. */ char *dc_frame_next(DcFrameBuf *frame, size_t *len); /* Write LINE plus '\n' to FD, handling short writes and EINTR. 0/-1. */ int dc_frame_send(int fd, const char *line, size_t len); #ifdef __cplusplus } #endif #endif /* _DtChat_Ipc_h */