/* * json.c -- the small, strict JSON reader/writer of dtchat's IPC * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The parser is deliberately strict: adapters are other programs, and a * malformed line must be rejected, not guessed around. The writer builds * the same subset (null, bool, int, double, string, array, object). */ #include "DtChat/Ipc.h" #include #include #include #include #include #include #include struct DcJson { DcJsonType type; union { int boolean; long long integer; double dbl; char *string; struct { DcJson **items; size_t n, cap; } array; struct { char **keys; DcJson **values; size_t n, cap; } object; } u; }; /* ------------------------------------------------------------------ */ /* UTF-8 */ /* * Decode one UTF-8 sequence. Returns 0 on success (and sets *CP and * *USED), -1 on an invalid, overlong, surrogate or truncated sequence. */ static int utf8_decode_one(const unsigned char *s, size_t n, unsigned *cp, size_t *used) { unsigned c, v, min; size_t need, i; if (n == 0) return -1; c = s[0]; if (c < 0x80) { *cp = c; *used = 1; return 0; } if (c >= 0xC2 && c <= 0xDF) { need = 1; v = c & 0x1F; min = 0x80; } else if (c >= 0xE0 && c <= 0xEF) { need = 2; v = c & 0x0F; min = 0x800; } else if (c >= 0xF0 && c <= 0xF4) { need = 3; v = c & 0x07; min = 0x10000; } else { return -1; } if (1 + need > n) return -1; for (i = 1; i <= need; i++) { if ((s[i] & 0xC0) != 0x80) return -1; v = (v << 6) | (s[i] & 0x3F); } if (v < min || v > 0x10FFFF || (v >= 0xD800 && v <= 0xDFFF)) return -1; *cp = v; *used = 1 + need; return 0; } int dc_utf8_valid(const char *s, size_t n) { const unsigned char *p = (const unsigned char *)s; size_t used; unsigned cp; if (s == NULL) return n == 0; while (n > 0) { if (utf8_decode_one(p, n, &cp, &used) != 0) return 0; p += used; n -= used; } return 1; } static int utf8_encode(unsigned cp, char out[4]) { if (cp < 0x80) { out[0] = (char)cp; return 1; } if (cp < 0x800) { out[0] = (char)(0xC0 | (cp >> 6)); out[1] = (char)(0x80 | (cp & 0x3F)); return 2; } if (cp < 0x10000) { if (cp >= 0xD800 && cp <= 0xDFFF) return -1; out[0] = (char)(0xE0 | (cp >> 12)); out[1] = (char)(0x80 | ((cp >> 6) & 0x3F)); out[2] = (char)(0x80 | (cp & 0x3F)); return 3; } if (cp <= 0x10FFFF) { out[0] = (char)(0xF0 | (cp >> 18)); out[1] = (char)(0x80 | ((cp >> 12) & 0x3F)); out[2] = (char)(0x80 | ((cp >> 6) & 0x3F)); out[3] = (char)(0x80 | (cp & 0x3F)); return 4; } return -1; } /* ------------------------------------------------------------------ */ /* growable string buffer */ typedef struct { char *p; size_t len, cap; } StrBuf; static void sb_init(StrBuf *sb) { sb->p = NULL; sb->len = 0; sb->cap = 0; } static void sb_free(StrBuf *sb) { free(sb->p); sb->p = NULL; sb->len = sb->cap = 0; } static int sb_reserve(StrBuf *sb, size_t extra) { size_t need, cap; char *np; if (extra > SIZE_MAX - sb->len - 1) return -1; need = sb->len + extra + 1; if (need <= sb->cap) return 0; cap = sb->cap ? sb->cap : 64; while (cap < need) { if (cap > SIZE_MAX / 2) return -1; cap *= 2; } np = realloc(sb->p, cap); if (np == NULL) return -1; sb->p = np; sb->cap = cap; return 0; } static int sb_putc(StrBuf *sb, char c) { if (sb_reserve(sb, 1) != 0) return -1; sb->p[sb->len++] = c; sb->p[sb->len] = '\0'; return 0; } static int sb_putn(StrBuf *sb, const char *s, size_t n) { if (sb_reserve(sb, n) != 0) return -1; memcpy(sb->p + sb->len, s, n); sb->len += n; sb->p[sb->len] = '\0'; return 0; } /* ------------------------------------------------------------------ */ /* constructors and accessors */ static DcJson * json_new(DcJsonType type) { DcJson *j = calloc(1, sizeof(*j)); if (j != NULL) j->type = type; return j; } DcJson * dc_json_new_null(void) { return json_new(DC_JSON_NULL); } DcJson * dc_json_new_bool(int value) { DcJson *j = json_new(DC_JSON_BOOL); if (j != NULL) j->u.boolean = value != 0; return j; } DcJson * dc_json_new_int(long long value) { DcJson *j = json_new(DC_JSON_INT); if (j != NULL) j->u.integer = value; return j; } DcJson * dc_json_new_double(double value) { DcJson *j; if (!isfinite(value)) return NULL; j = json_new(DC_JSON_DOUBLE); if (j != NULL) j->u.dbl = value; return j; } DcJson * dc_json_new_string(const char *value) { DcJson *j; if (value == NULL || !dc_utf8_valid(value, strlen(value))) return NULL; j = json_new(DC_JSON_STRING); if (j == NULL) return NULL; j->u.string = strdup(value); if (j->u.string == NULL) { free(j); return NULL; } return j; } DcJson * dc_json_new_array(void) { return json_new(DC_JSON_ARRAY); } DcJson * dc_json_new_object(void) { return json_new(DC_JSON_OBJECT); } DcJsonType dc_json_type(const DcJson *json) { return json ? json->type : DC_JSON_NULL; } const char * dc_json_type_name(DcJsonType type) { switch (type) { case DC_JSON_NULL: return "null"; case DC_JSON_BOOL: return "bool"; case DC_JSON_INT: return "int"; case DC_JSON_DOUBLE: return "double"; case DC_JSON_STRING: return "string"; case DC_JSON_ARRAY: return "array"; case DC_JSON_OBJECT: return "object"; } return "?"; } void dc_json_free(DcJson *json) { size_t i; if (json == NULL) return; switch (json->type) { case DC_JSON_STRING: free(json->u.string); break; case DC_JSON_ARRAY: for (i = 0; i < json->u.array.n; i++) dc_json_free(json->u.array.items[i]); free(json->u.array.items); break; case DC_JSON_OBJECT: for (i = 0; i < json->u.object.n; i++) { free(json->u.object.keys[i]); dc_json_free(json->u.object.values[i]); } free(json->u.object.keys); free(json->u.object.values); break; default: break; } free(json); } DcJson * dc_json_get(const DcJson *json, const char *key) { size_t i; if (json == NULL || json->type != DC_JSON_OBJECT || key == NULL) return NULL; for (i = 0; i < json->u.object.n; i++) if (strcmp(json->u.object.keys[i], key) == 0) return json->u.object.values[i]; return NULL; } const char * dc_json_string(const DcJson *json) { if (json == NULL || json->type != DC_JSON_STRING) return NULL; return json->u.string; } long long dc_json_int(const DcJson *json) { return (json && json->type == DC_JSON_INT) ? json->u.integer : 0; } double dc_json_double(const DcJson *json) { if (json == NULL) return 0.0; if (json->type == DC_JSON_DOUBLE) return json->u.dbl; if (json->type == DC_JSON_INT) return (double)json->u.integer; return 0.0; } int dc_json_bool(const DcJson *json) { return (json && json->type == DC_JSON_BOOL) ? json->u.boolean : 0; } size_t dc_json_array_size(const DcJson *json) { return (json && json->type == DC_JSON_ARRAY) ? json->u.array.n : 0; } DcJson * dc_json_array_get(const DcJson *json, size_t index) { if (json == NULL || json->type != DC_JSON_ARRAY || index >= json->u.array.n) return NULL; return json->u.array.items[index]; } static int array_grow(DcJson *array) { size_t cap; DcJson **items; if (array->u.array.n < array->u.array.cap) return 0; cap = array->u.array.cap ? array->u.array.cap * 2 : 8; items = realloc(array->u.array.items, cap * sizeof(*items)); if (items == NULL) return -1; array->u.array.items = items; array->u.array.cap = cap; return 0; } int dc_json_array_append(DcJson *array, DcJson *value) { if (array == NULL || array->type != DC_JSON_ARRAY || value == NULL) { dc_json_free(value); return -1; } if (array_grow(array) != 0) { dc_json_free(value); return -1; } array->u.array.items[array->u.array.n++] = value; return 0; } static int object_grow(DcJson *object) { size_t cap; char **keys; DcJson **values; if (object->u.object.n < object->u.object.cap) return 0; cap = object->u.object.cap ? object->u.object.cap * 2 : 8; keys = realloc(object->u.object.keys, cap * sizeof(*keys)); if (keys == NULL) return -1; object->u.object.keys = keys; values = realloc(object->u.object.values, cap * sizeof(*values)); if (values == NULL) return -1; object->u.object.values = values; object->u.object.cap = cap; return 0; } int dc_json_object_set(DcJson *object, const char *key, DcJson *value) { size_t i; char *dup; if (object == NULL || object->type != DC_JSON_OBJECT || key == NULL || value == NULL || !dc_utf8_valid(key, strlen(key))) { dc_json_free(value); return -1; } for (i = 0; i < object->u.object.n; i++) { if (strcmp(object->u.object.keys[i], key) == 0) { dc_json_free(object->u.object.values[i]); object->u.object.values[i] = value; return 0; } } if (object_grow(object) != 0) { dc_json_free(value); return -1; } dup = strdup(key); if (dup == NULL) { dc_json_free(value); return -1; } object->u.object.keys[object->u.object.n] = dup; object->u.object.values[object->u.object.n] = value; object->u.object.n++; return 0; } /* ------------------------------------------------------------------ */ /* parser */ typedef struct { const char *start; const char *p; const char *end; int depth; int failed; char *err; size_t errlen; } Parser; static void perr(Parser *ps, const char *msg) { if (ps->failed) return; ps->failed = 1; if (ps->err != NULL && ps->errlen > 0) snprintf(ps->err, ps->errlen, "%s at byte %zu", msg, (size_t)(ps->p - ps->start)); } static void skip_ws(Parser *ps) { while (ps->p < ps->end) { char c = *ps->p; if (c != ' ' && c != '\t' && c != '\n' && c != '\r') break; ps->p++; } } static DcJson *parse_value(Parser *ps); static int hexval(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; return -1; } static int parse_hex4(Parser *ps, unsigned *out) { unsigned v = 0; int i, h; if ((size_t)(ps->end - ps->p) < 4) { perr(ps, "short \\u escape"); return -1; } for (i = 0; i < 4; i++) { h = hexval(ps->p[i]); if (h < 0) { perr(ps, "bad hex digit"); return -1; } v = (v << 4) | (unsigned)h; } ps->p += 4; *out = v; return 0; } static char * parse_string_raw(Parser *ps) { StrBuf sb; unsigned cp, low; char utf8[4]; int n; sb_init(&sb); if (ps->p >= ps->end || *ps->p != '"') { perr(ps, "expected string"); return NULL; } ps->p++; while (ps->p < ps->end) { unsigned char c = (unsigned char)*ps->p; if (c == '"') { ps->p++; if (sb.p == NULL) return strdup(""); return sb.p; } if (c == '\\') { ps->p++; if (ps->p >= ps->end) { perr(ps, "short escape"); goto bad; } switch (*ps->p++) { case '"': c = '"'; break; case '\\': c = '\\'; break; case '/': c = '/'; break; case 'b': c = '\b'; break; case 'f': c = '\f'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; case 'u': if (parse_hex4(ps, &cp) != 0) goto bad; if (cp >= 0xD800 && cp <= 0xDBFF) { if ((size_t)(ps->end - ps->p) < 6 || ps->p[0] != '\\' || ps->p[1] != 'u') { perr(ps, "missing low surrogate"); goto bad; } ps->p += 2; if (parse_hex4(ps, &low) != 0) goto bad; if (low < 0xDC00 || low > 0xDFFF) { perr(ps, "bad low surrogate"); goto bad; } cp = 0x10000 + ((cp - 0xD800) << 10) + (low - 0xDC00); } else if (cp >= 0xDC00 && cp <= 0xDFFF) { perr(ps, "lone low surrogate"); goto bad; } if (cp == 0) { perr(ps, "NUL in string"); goto bad; } n = utf8_encode(cp, utf8); if (n < 0 || sb_putn(&sb, utf8, (size_t)n) != 0) { perr(ps, "bad code point"); goto bad; } continue; default: perr(ps, "bad escape"); goto bad; } if (sb_putc(&sb, (char)c) != 0) { perr(ps, "out of memory"); goto bad; } continue; } if (c < 0x20) { perr(ps, "control character in string"); goto bad; } if (c < 0x80) { if (sb_putc(&sb, (char)c) != 0) { perr(ps, "out of memory"); goto bad; } ps->p++; continue; } { size_t used; const char *q = ps->p; if (utf8_decode_one((const unsigned char *)q, (size_t)(ps->end - q), &cp, &used) != 0) { perr(ps, "invalid UTF-8"); goto bad; } if (sb_putn(&sb, q, used) != 0) { perr(ps, "out of memory"); goto bad; } ps->p += used; } } perr(ps, "unterminated string"); bad: sb_free(&sb); return NULL; } static DcJson * parse_number(Parser *ps) { const char *start = ps->p; char *tmp; char *endptr; DcJson *j; int is_int = 1; if (ps->p < ps->end && *ps->p == '-') ps->p++; if (ps->p >= ps->end) { perr(ps, "short number"); return NULL; } if (*ps->p == '0') { ps->p++; if (ps->p < ps->end && *ps->p >= '0' && *ps->p <= '9') { perr(ps, "leading zero"); return NULL; } } else if (*ps->p >= '1' && *ps->p <= '9') { while (ps->p < ps->end && *ps->p >= '0' && *ps->p <= '9') ps->p++; } else { perr(ps, "bad number"); return NULL; } if (ps->p < ps->end && *ps->p == '.') { is_int = 0; ps->p++; if (ps->p >= ps->end || *ps->p < '0' || *ps->p > '9') { perr(ps, "bad fraction"); return NULL; } while (ps->p < ps->end && *ps->p >= '0' && *ps->p <= '9') ps->p++; } if (ps->p < ps->end && (*ps->p == 'e' || *ps->p == 'E')) { is_int = 0; ps->p++; if (ps->p < ps->end && (*ps->p == '+' || *ps->p == '-')) ps->p++; if (ps->p >= ps->end || *ps->p < '0' || *ps->p > '9') { perr(ps, "bad exponent"); return NULL; } while (ps->p < ps->end && *ps->p >= '0' && *ps->p <= '9') ps->p++; } tmp = malloc((size_t)(ps->p - start) + 1); if (tmp == NULL) { perr(ps, "out of memory"); return NULL; } memcpy(tmp, start, (size_t)(ps->p - start)); tmp[ps->p - start] = '\0'; errno = 0; if (is_int) { long long v = strtoll(tmp, &endptr, 10); if (errno == 0 && endptr != tmp && *endptr == '\0') { free(tmp); return dc_json_new_int(v); } } errno = 0; j = dc_json_new_double(strtod(tmp, &endptr)); if (errno != 0 || endptr == tmp || *endptr != '\0' || j == NULL) { perr(ps, "bad number"); free(tmp); dc_json_free(j); return NULL; } free(tmp); return j; } static DcJson * parse_array(Parser *ps) { DcJson *array; if (ps->depth >= DC_JSON_MAX_DEPTH) { perr(ps, "depth limit"); return NULL; } ps->depth++; ps->p++; /* '[' */ array = dc_json_new_array(); if (array == NULL) { perr(ps, "out of memory"); ps->depth--; return NULL; } skip_ws(ps); if (ps->p < ps->end && *ps->p == ']') { ps->p++; ps->depth--; return array; } for (;;) { DcJson *item; skip_ws(ps); item = parse_value(ps); if (item == NULL || dc_json_array_append(array, item) != 0) { if (item == NULL) perr(ps, "bad array item"); dc_json_free(array); ps->depth--; return NULL; } skip_ws(ps); if (ps->p >= ps->end) { perr(ps, "unterminated array"); dc_json_free(array); ps->depth--; return NULL; } if (*ps->p == ',') { ps->p++; skip_ws(ps); if (ps->p < ps->end && *ps->p == ']') { perr(ps, "trailing comma"); dc_json_free(array); ps->depth--; return NULL; } continue; } if (*ps->p == ']') { ps->p++; ps->depth--; return array; } perr(ps, "expected ',' or ']'"); dc_json_free(array); ps->depth--; return NULL; } } static DcJson * parse_object(Parser *ps) { DcJson *object; if (ps->depth >= DC_JSON_MAX_DEPTH) { perr(ps, "depth limit"); return NULL; } ps->depth++; ps->p++; /* '{' */ object = dc_json_new_object(); if (object == NULL) { perr(ps, "out of memory"); ps->depth--; return NULL; } skip_ws(ps); if (ps->p < ps->end && *ps->p == '}') { ps->p++; ps->depth--; return object; } for (;;) { char *key; DcJson *value; skip_ws(ps); key = parse_string_raw(ps); if (key == NULL) { perr(ps, "bad object key"); dc_json_free(object); ps->depth--; return NULL; } skip_ws(ps); if (ps->p >= ps->end || *ps->p != ':') { perr(ps, "expected ':'"); free(key); dc_json_free(object); ps->depth--; return NULL; } ps->p++; skip_ws(ps); value = parse_value(ps); if (value == NULL) { perr(ps, "bad object value"); free(key); dc_json_free(object); ps->depth--; return NULL; } if (dc_json_get(object, key) != NULL) { perr(ps, "duplicate object key"); free(key); dc_json_free(value); dc_json_free(object); ps->depth--; return NULL; } if (dc_json_object_set(object, key, value) != 0) { perr(ps, "out of memory"); free(key); dc_json_free(object); ps->depth--; return NULL; } free(key); skip_ws(ps); if (ps->p >= ps->end) { perr(ps, "unterminated object"); dc_json_free(object); ps->depth--; return NULL; } if (*ps->p == ',') { ps->p++; skip_ws(ps); if (ps->p < ps->end && *ps->p == '}') { perr(ps, "trailing comma"); dc_json_free(object); ps->depth--; return NULL; } continue; } if (*ps->p == '}') { ps->p++; ps->depth--; return object; } perr(ps, "expected ',' or '}'"); dc_json_free(object); ps->depth--; return NULL; } } static DcJson * parse_value(Parser *ps) { skip_ws(ps); if (ps->p >= ps->end) { perr(ps, "unexpected end"); return NULL; } switch (*ps->p) { case '{': return parse_object(ps); case '[': return parse_array(ps); case '"': { char *s = parse_string_raw(ps); DcJson *j; if (s == NULL) return NULL; j = dc_json_new_string(s); free(s); if (j == NULL) perr(ps, "out of memory or invalid UTF-8"); return j; } case 't': if ((size_t)(ps->end - ps->p) >= 4 && memcmp(ps->p, "true", 4) == 0) { ps->p += 4; return dc_json_new_bool(1); } break; case 'f': if ((size_t)(ps->end - ps->p) >= 5 && memcmp(ps->p, "false", 5) == 0) { ps->p += 5; return dc_json_new_bool(0); } break; case 'n': if ((size_t)(ps->end - ps->p) >= 4 && memcmp(ps->p, "null", 4) == 0) { ps->p += 4; return dc_json_new_null(); } break; default: if (*ps->p == '-' || (*ps->p >= '0' && *ps->p <= '9')) return parse_number(ps); break; } perr(ps, "unexpected token"); return NULL; } DcJson * dc_json_parse(const char *text, size_t len, char *err, size_t errlen) { Parser ps; DcJson *j; if (err != NULL && errlen > 0) err[0] = '\0'; if (text == NULL) return NULL; if (len > DC_IPC_MAX_LINE) { if (err != NULL && errlen > 0) snprintf(err, errlen, "line too long (%zu bytes)", len); return NULL; } memset(&ps, 0, sizeof(ps)); ps.start = text; ps.p = text; ps.end = text + len; ps.err = err; ps.errlen = errlen; j = parse_value(&ps); if (j == NULL) return NULL; skip_ws(&ps); if (ps.p != ps.end) { perr(&ps, "trailing data"); dc_json_free(j); return NULL; } return j; } /* ------------------------------------------------------------------ */ /* writer */ static int write_string(StrBuf *sb, const char *s) { const unsigned char *p = (const unsigned char *)s; size_t n = strlen(s); size_t used; unsigned cp; char esc[8]; if (!dc_utf8_valid(s, n)) return -1; if (sb_putc(sb, '"') != 0) return -1; while (n > 0) { unsigned char c = *p; if (c == '"' || c == '\\') { if (sb_putc(sb, '\\') != 0 || sb_putc(sb, (char)c) != 0) return -1; p++; n--; continue; } if (c == '\b') { if (sb_putn(sb, "\\b", 2) != 0) return -1; p++; n--; continue; } if (c == '\f') { if (sb_putn(sb, "\\f", 2) != 0) return -1; p++; n--; continue; } if (c == '\n') { if (sb_putn(sb, "\\n", 2) != 0) return -1; p++; n--; continue; } if (c == '\r') { if (sb_putn(sb, "\\r", 2) != 0) return -1; p++; n--; continue; } if (c == '\t') { if (sb_putn(sb, "\\t", 2) != 0) return -1; p++; n--; continue; } if (c < 0x20) { snprintf(esc, sizeof(esc), "\\u%04x", c); if (sb_putn(sb, esc, 6) != 0) return -1; p++; n--; continue; } if (c >= 0x80) { if (utf8_decode_one(p, n, &cp, &used) != 0) return -1; if (sb_putn(sb, (const char *)p, used) != 0) return -1; p += used; n -= used; continue; } if (sb_putc(sb, (char)c) != 0) return -1; p++; n--; } return sb_putc(sb, '"'); } static int write_value(StrBuf *sb, const DcJson *json, int depth) { char num[64]; size_t i; if (json == NULL || depth > DC_JSON_MAX_DEPTH) return -1; switch (json->type) { case DC_JSON_NULL: return sb_putn(sb, "null", 4); case DC_JSON_BOOL: return sb_putn(sb, json->u.boolean ? "true" : "false", json->u.boolean ? 4 : 5); case DC_JSON_INT: snprintf(num, sizeof(num), "%lld", json->u.integer); return sb_putn(sb, num, strlen(num)); case DC_JSON_DOUBLE: if (!isfinite(json->u.dbl)) return -1; snprintf(num, sizeof(num), "%.17g", json->u.dbl); return sb_putn(sb, num, strlen(num)); case DC_JSON_STRING: return write_string(sb, json->u.string); case DC_JSON_ARRAY: if (sb_putc(sb, '[') != 0) return -1; for (i = 0; i < json->u.array.n; i++) { if (i > 0 && sb_putc(sb, ',') != 0) return -1; if (write_value(sb, json->u.array.items[i], depth + 1) != 0) return -1; } return sb_putc(sb, ']'); case DC_JSON_OBJECT: if (sb_putc(sb, '{') != 0) return -1; for (i = 0; i < json->u.object.n; i++) { if (i > 0 && sb_putc(sb, ',') != 0) return -1; if (write_string(sb, json->u.object.keys[i]) != 0) return -1; if (sb_putc(sb, ':') != 0) return -1; if (write_value(sb, json->u.object.values[i], depth + 1) != 0) return -1; } return sb_putc(sb, '}'); } return -1; } char * dc_json_write(const DcJson *json, size_t *len) { StrBuf sb; sb_init(&sb); if (write_value(&sb, json, 0) != 0) { sb_free(&sb); return NULL; } if (len != NULL) *len = sb.len; return sb.p; } /* ------------------------------------------------------------------ */ /* line framing */ void dc_frame_init(DcFrameBuf *frame) { frame->buf = NULL; frame->len = 0; frame->cap = 0; } void dc_frame_reset(DcFrameBuf *frame) { frame->len = 0; if (frame->buf != NULL) frame->buf[0] = '\0'; } void dc_frame_free(DcFrameBuf *frame) { free(frame->buf); frame->buf = NULL; frame->len = 0; frame->cap = 0; } static int frame_reserve(DcFrameBuf *frame, size_t extra) { size_t need, cap; char *np; if (extra > SIZE_MAX - frame->len - 1) return -1; need = frame->len + extra + 1; if (need <= frame->cap) return 0; cap = frame->cap ? frame->cap : 1024; while (cap < need) { if (cap > SIZE_MAX / 2) return -1; cap *= 2; } np = realloc(frame->buf, cap); if (np == NULL) return -1; frame->buf = np; frame->cap = cap; return 0; } int dc_frame_feed(DcFrameBuf *frame, const void *data, size_t n) { const char *p = data; const char *nl; size_t first; if (n == 0) return 0; if (memchr(p, '\0', n) != NULL) return -1; if (frame_reserve(frame, n) != 0) return -1; memcpy(frame->buf + frame->len, p, n); frame->len += n; frame->buf[frame->len] = '\0'; /* * A newline may only appear within the first DC_IPC_MAX_LINE bytes. * If there is none yet, the buffered line must still fit. */ nl = memchr(frame->buf, '\n', frame->len); first = nl != NULL ? (size_t)(nl - frame->buf) : frame->len; if (first > DC_IPC_MAX_LINE) return -1; return 0; } char * dc_frame_next(DcFrameBuf *frame, size_t *len) { char *nl; size_t line_len, consume; char *line; if (frame->len == 0) return NULL; nl = memchr(frame->buf, '\n', frame->len); if (nl == NULL) return NULL; line_len = (size_t)(nl - frame->buf); if (line_len > 0 && frame->buf[line_len - 1] == '\r') line_len--; line = malloc(line_len + 1); if (line == NULL) return NULL; memcpy(line, frame->buf, line_len); line[line_len] = '\0'; consume = (size_t)(nl - frame->buf) + 1; memmove(frame->buf, frame->buf + consume, frame->len - consume); frame->len -= consume; frame->buf[frame->len] = '\0'; if (len != NULL) *len = line_len; return line; } int dc_frame_send(int fd, const char *line, size_t len) { size_t off = 0; ssize_t w; if (line == NULL) return -1; while (off < len) { w = write(fd, line + off, len - off); if (w < 0) { if (errno == EINTR) continue; return -1; } if (w == 0) return -1; off += (size_t)w; } for (;;) { ssize_t n = write(fd, "\n", 1); if (n == 1) break; if (n < 0 && errno == EINTR) continue; return -1; } return 0; }