Artifact a02be3405f44576f894e092aa75927ef350e80aa687f004a6b21dd576c04e931:
- File
src/image.c
— part of check-in
[c55dfbb163]
at
2026-11-19 23:39:49
on branch images
— Images, as Pi's
User messages, steering and follow-ups, and tool results can carry images. Journal version 9 keeps their bytes in SQLite (pic_image, deduplicated by SHA3-256, deflated only when smaller) linked by pic_attachment, written in the same transaction as the entry; pic check covers them. Each adapter sends its native shape (Anthropic base64 blocks, Chat image_url data URLs, Responses input_image); a model whose catalog input lacks images gets Pi's placeholders. pic ask --image PATH, the RPC images field and the read tool on image files add them; the Pi and HTML exports include them. (user: dmestas, size: 7001) [annotate] [blame] [check-ins using]
/* Images as Pi takes them: mime.ts's detection, image-process.ts's inline * types and limit, and base64. */ #include <stdlib.h> #include <string.h> #include <strings.h> #include "image.h" static int starts(const unsigned char *b, size_t n, size_t at, const char *text) { size_t k = strlen(text); return n >= at + k && memcmp(b + at, text, k) == 0; } static unsigned long be32(const unsigned char *b, size_t at) { return (unsigned long)b[at] << 24 | (unsigned long)b[at + 1] << 16 | (unsigned long)b[at + 2] << 8 | b[at + 3]; } static unsigned long le32(const unsigned char *b, size_t at) { return (unsigned long)b[at] | (unsigned long)b[at + 1] << 8 | (unsigned long)b[at + 2] << 16 | (unsigned long)b[at + 3] << 24; } static unsigned le16(const unsigned char *b, size_t at) { return (unsigned)b[at] | (unsigned)b[at + 1] << 8; } /* An acTL chunk before the first IDAT: an animated PNG. */ static int animated_png(const unsigned char *b, size_t n) { size_t at = 8; while (at + 8 <= n) { unsigned long len = be32(b, at); if (starts(b, n, at + 4, "acTL")) { return 1; } if (starts(b, n, at + 4, "IDAT")) { return 0; } if (len > n || at + 12 + len > n) { return 0; } at += 12 + len; } return 0; } static int bmp(const unsigned char *b, size_t n) { unsigned long size, offset, dib; unsigned planes, bits; if (n < 26) { return 0; } size = le32(b, 2); offset = le32(b, 10); dib = le32(b, 14); if ((size != 0 && size < 26) || offset < 14 + dib || (size != 0 && offset >= size)) { return 0; } if (dib == 12) { planes = le16(b, 22); bits = le16(b, 24); } else if (dib >= 40 && dib <= 124) { if (n < 30) { return 0; } planes = le16(b, 26); bits = le16(b, 28); } else { return 0; } return planes == 1 && (bits == 1 || bits == 4 || bits == 8 || bits == 16 || bits == 24 || bits == 32); } const char *pic_image_sniff(const unsigned char *b, size_t n) { static const unsigned char png[8] = {0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a}; if (n >= 3 && b[0] == 0xff && b[1] == 0xd8 && b[2] == 0xff) { return n > 3 && b[3] == 0xf7 ? NULL : "image/jpeg"; } if (n >= 8 && memcmp(b, png, 8) == 0) { return n >= 16 && be32(b, 8) == 13 && starts(b, n, 12, "IHDR") && !animated_png(b, n) ? "image/png" : NULL; } if (starts(b, n, 0, "GIF87a") || starts(b, n, 0, "GIF89a")) { return "image/gif"; } if (starts(b, n, 0, "RIFF") && starts(b, n, 8, "WEBP")) { return "image/webp"; } if (starts(b, n, 0, "BM") && bmp(b, n)) { return "image/bmp"; } return NULL; } const char *pic_image_inline_mime(const char *declared) { size_t n = 0; if (!declared) { return NULL; } while (declared[n] && declared[n] != ';') { n++; } while (n > 0 && (declared[n - 1] == ' ' || declared[n - 1] == '\t')) { n--; } while (n > 0 && (*declared == ' ' || *declared == '\t')) { declared++; n--; } if (n == 9 && strncasecmp(declared, "image/png", 9) == 0) { return "image/png"; } if ((n == 10 && strncasecmp(declared, "image/jpeg", 10) == 0) || (n == 9 && strncasecmp(declared, "image/jpg", 9) == 0)) { return "image/jpeg"; } if (n == 9 && strncasecmp(declared, "image/gif", 9) == 0) { return "image/gif"; } if (n == 10 && strncasecmp(declared, "image/webp", 10) == 0) { return "image/webp"; } return NULL; } const char *pic_image_refusal(const char *mime, size_t n) { if (!pic_image_inline_mime(mime)) { return PIC_IMAGE_UNCONVERTIBLE; } /* Pi resizes from here on; pic sends what fits and refuses the rest. */ return (n + 2) / 3 * 4 >= PIC_IMAGE_MAX_BASE64 ? PIC_IMAGE_TOO_BIG : NULL; } char *pic_image_prepare(const char *text, pic_image *images, size_t *n) { size_t kept = 0, cap = strlen(text) + 1, i; int noted = 0; char *out; for (i = 0; i < *n; i++) { const char *note = pic_image_refusal(images[i].mime, images[i].len); cap += note ? strlen(note) + 2 : 0; } if (!(out = malloc(cap))) { return NULL; } memcpy(out, text, strlen(text) + 1); for (i = 0; i < *n; i++) { const char *note = pic_image_refusal(images[i].mime, images[i].len); if (note) { /* Pi: the text, a blank line, then the notes one per line */ strcat(out, noted++ ? "\n" : "\n\n"); strcat(out, note); } else { images[kept] = images[i]; images[kept].mime = pic_image_inline_mime(images[i].mime); kept++; } } *n = kept; return out; } static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; size_t pic_base64_encode(const unsigned char *in, size_t n, char *out) { size_t o = 0, i = 0; for (; i + 3 <= n; i += 3) { unsigned long v = (unsigned long)in[i] << 16 | (unsigned long)in[i + 1] << 8 | in[i + 2]; out[o++] = b64[v >> 18 & 63]; out[o++] = b64[v >> 12 & 63]; out[o++] = b64[v >> 6 & 63]; out[o++] = b64[v & 63]; } if (i < n) { unsigned long v = (unsigned long)in[i] << 16 | (i + 1 < n ? (unsigned long)in[i + 1] << 8 : 0); out[o++] = b64[v >> 18 & 63]; out[o++] = b64[v >> 12 & 63]; out[o++] = i + 1 < n ? b64[v >> 6 & 63] : '='; out[o++] = '='; } out[o] = '\0'; return o; } static int b64_value(char c) { if (c >= 'A' && c <= 'Z') { return c - 'A'; } if (c >= 'a' && c <= 'z') { return c - 'a' + 26; } if (c >= '0' && c <= '9') { return c - '0' + 52; } return c == '+' || c == '-' ? 62 : c == '/' || c == '_' ? 63 : -1; } size_t pic_base64_decode(const char *in, size_t n, unsigned char *out) { unsigned long acc = 0; size_t o = 0, k = 0, pad = 0; for (size_t i = 0; i < n; i++) { char c = in[i]; int v; if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { continue; } if (c == '=') { pad++; continue; } if (pad || (v = b64_value(c)) < 0) { return (size_t)-1; /* data after padding, or not base64 */ } acc = acc << 6 | (unsigned long)v; if (++k == 4) { out[o++] = (unsigned char)(acc >> 16); out[o++] = (unsigned char)(acc >> 8); out[o++] = (unsigned char)acc; acc = 0; k = 0; } } if (k == 1 || pad > 2) { return (size_t)-1; } if (k == 2) { out[o++] = (unsigned char)(acc >> 4); } else if (k == 3) { out[o++] = (unsigned char)(acc >> 10); out[o++] = (unsigned char)(acc >> 2); } return o; }