Artifact 32f0ad6f5da4aef614aee4e715ebf37db76b627e45ccca3bc6da82c243134547:
- File
src/image.h
— part of check-in
[0cc6f109a2]
at
2026-11-20 00:08:59
on branch images
— Images: validate at entry, cap per request, prune by retention
Every image is checked from its header (PNG IHDR with CRC, JPEG frame header, GIF screen, WebP) as Fossil sniffs types: an invalid header, a type that isn't the declared one, more than 8000 px a side or over 4.5 MB is refused; the stored type is the sniffed one, with width and height. RPC image errors are reported, never dropped. A request carries the newest 20 images up to 20 MB; older ones become Pi's placeholder. Image bytes are dropped once compacted away or after imageRetentionDays (default 30; 0 keeps them), leaving hash, type and size and an "[image removed]" placeholder; pic prune [--dry-run] runs it, and it also runs at open at most hourly. pic check re-hashes images. Imports keep images. Journal version 10. (user: dmestas, size: 4039) [annotate] [blame] [check-ins using] [more...]
/* Images as Pi takes them (image.c): which bytes are a supported image, * the inline size limit, and base64. No database and no I/O here, so tools * and the provider adapters share one set of rules. */ #ifndef PIC_IMAGE_H #define PIC_IMAGE_H #include <stddef.h> /* Pi's DEFAULT_MAX_BYTES: 4.5MB of base64 payload, under Anthropic's 5MB. */ #define PIC_IMAGE_MAX_BASE64 ((size_t)(4.5 * 1024 * 1024)) /* Pi's notes when an image cannot go inline (image-process.ts). pic does not * resize or convert, so an image over the limit, or a BMP, gets these. */ #define PIC_IMAGE_TOO_BIG "[Image omitted: could not be resized below the inline image size limit.]" #define PIC_IMAGE_UNCONVERTIBLE "[Image omitted: could not be converted to a supported inline image format.]" /* Pi's placeholders for a model without image input (transform-messages.ts). */ #define PIC_IMAGE_USER_PLACEHOLDER "(image omitted: model does not support images)" #define PIC_IMAGE_TOOL_PLACEHOLDER "(tool image omitted: model does not support images)" /* An image whose bytes retention pruned (pic prune). */ #define PIC_IMAGE_REMOVED "[image removed]" #define PIC_IMAGE_MAX_SIDE 8000 /* pixels: what the providers take at all (Anthropic's limit) */ /* An image to attach: its bytes and MIME type; width and height (pixels) * once checked. */ typedef struct { const char *mime; const unsigned char *data; size_t len; long width, height; } pic_image; enum { PIC_IMAGE_OK, PIC_IMAGE_INVALID, /* not an image, or its header is broken */ PIC_IMAGE_MISMATCH, /* the declared type is not what the data is */ PIC_IMAGE_UNSUPPORTED, /* a valid BMP: Pi converts it, pic cannot */ PIC_IMAGE_TOO_LARGE, /* over 8000 px a side or the 4.5MB base64 limit */ }; /* Reads IMG's header without decoding it: PNG (IHDR and its CRC), JPEG * (the first frame header), GIF (the logical screen), WebP (VP8, VP8L or * VP8X). On PIC_IMAGE_OK mime is the sniffed type and width and height are * set. DECLARED, when not NULL, is a type the sender claimed, which must be * the sniffed one. Else why (when not NULL) says what is wrong. */ int pic_image_inspect(pic_image *img, const char *declared, char *why, size_t cap); /* Makes an inspected IMG fit what providers take. Pi shrinks to 2000x2000 * here; without a decoder pic only checks: PIC_IMAGE_TOO_LARGE past 8000 px * a side or the base64 limit. The seam for resizing. */ int pic_image_fit(pic_image *img, char *why, size_t cap); /* pic_image_inspect, then pic_image_fit. */ int pic_image_check(pic_image *img, const char *declared, char *why, size_t cap); /* Pi's note for an image that cannot go inline (the read tool's). */ const char *pic_image_note(int rc); /* Pi's _normalizePromptImages for a message: checks IMAGES[0..*N) (each * mime the declared type, or NULL to take the sniffed one). A BMP becomes * Pi's note after the text, after a blank line; any other failure is an * error (NULL, err set) naming the image. The rest stay in order (*N their * count, each with its sniffed type and size). The text is malloc'd. */ char *pic_image_prepare(const char *text, pic_image *images, size_t *n, char *err, size_t errcap); /* Pi's detectSupportedImageMimeType: image/jpeg, png (not animated), gif, * webp or bmp from the leading bytes, else NULL. */ const char *pic_image_sniff(const unsigned char *b, size_t n); /* Pi's normalizeSupportedImageMimeType: the inline type a declared MIME type * names (image/png, image/jpeg, image/gif, image/webp), else NULL. */ const char *pic_image_inline_mime(const char *declared); /* Standard base64 of N bytes into out (4 * ((n + 2) / 3) + 1 bytes, NUL * terminated); the length written. */ size_t pic_base64_encode(const unsigned char *in, size_t n, char *out); /* Decodes standard or URL-safe base64 (padding optional, whitespace * skipped) into out (at least 3 * (n / 4) + 3 bytes). The length, or * (size_t)-1 when the text is not base64. */ size_t pic_base64_decode(const char *in, size_t n, unsigned char *out); #endif