Artifact 0da281b831ebbf5939f7c121c0486baf7cd4059d2252db9d1754b284e879ae3c:
- File
src/image.c
— part of check-in
[9dabe49948]
at
2026-11-21 19:00:33
on branch amalgam
— Amalgamation: pic as one pic.c beside the vendored sources, built by one cc line
make amalgamation writes pic-amalgamation-VERSION/ and its tarball; make test-amal builds it from the README's own line and runs crash, print and tools against that binary, and is part of make test-pic. About 60 file-local names that collided in one translation unit take their file's prefix; test/pic/amalgam.sh fails on a new collision. (user: dmestas, size: 13211) [annotate] [blame] [check-ins using] [more...]
/* Images as Pi takes them: mime.ts's detection, image-process.ts's inline * types and limit, and base64. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include "image.h" static int img_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 (img_starts(b, n, at + 4, "acTL")) { return 1; } if (img_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 && img_starts(b, n, 12, "IHDR") && !animated_png(b, n) ? "image/png" : NULL; } if (img_starts(b, n, 0, "GIF87a") || img_starts(b, n, 0, "GIF89a")) { return "image/gif"; } if (img_starts(b, n, 0, "RIFF") && img_starts(b, n, 8, "WEBP")) { return "image/webp"; } if (img_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; } /* CRC-32 (ISO 3309, as PNG's chunks carry it). */ static unsigned long crc32_of(const unsigned char *b, size_t n) { unsigned long c = 0xffffffffUL; for (size_t i = 0; i < n; i++) { c ^= b[i]; for (int k = 0; k < 8; k++) { c = c & 1 ? 0xedb88320UL ^ (c >> 1) : c >> 1; } } return c ^ 0xffffffffUL; } /* PNG: the IHDR chunk first, its CRC right, a known bit depth and colour * type, no animation. */ static int png_size(const unsigned char *b, size_t n, long *w, long *h) { unsigned depth, color; if (n < 33 || be32(b, 8) != 13 || !img_starts(b, n, 12, "IHDR") || crc32_of(b + 12, 17) != be32(b, 29) || animated_png(b, n)) { return 0; } *w = (long)be32(b, 16); *h = (long)be32(b, 20); depth = b[24]; color = b[25]; return (color == 0 && (depth == 1 || depth == 2 || depth == 4 || depth == 8 || depth == 16)) || ((color == 2 || color == 4 || color == 6) && (depth == 8 || depth == 16)) || (color == 3 && (depth == 1 || depth == 2 || depth == 4 || depth == 8)); } /* JPEG: segments from SOI to the first start-of-frame marker, which holds * the size; a scan (SOS) or the end before any frame is not an image. */ static int jpeg_size(const unsigned char *b, size_t n, long *w, long *h) { size_t at = 2; while (at + 4 <= n) { unsigned m; size_t len; if (b[at] != 0xff) { return 0; } m = b[at + 1]; if (m == 0xff) { /* fill byte */ at++; continue; } if (m == 0x01 || (m >= 0xd0 && m <= 0xd7)) { /* no length */ at += 2; continue; } if (m == 0xd8 || m == 0xd9 || m == 0xda) { return 0; } len = (size_t)b[at + 2] << 8 | b[at + 3]; if (len < 2 || at + 2 + len > n) { return 0; } if (m >= 0xc0 && m <= 0xcf && m != 0xc4 && m != 0xc8 && m != 0xcc) { if (len < 8) { return 0; } *h = (long)((unsigned)b[at + 5] << 8 | b[at + 6]); *w = (long)((unsigned)b[at + 7] << 8 | b[at + 8]); return 1; } at += 2 + len; } return 0; } /* GIF: the logical screen descriptor. */ static int gif_size(const unsigned char *b, size_t n, long *w, long *h) { if (n < 13) { return 0; } *w = (long)le16(b, 6); *h = (long)le16(b, 8); return 1; } /* WebP: a RIFF whose size fits, then a VP8 (lossy key frame), VP8L * (lossless) or VP8X (extended) chunk. */ static int webp_size(const unsigned char *b, size_t n, long *w, long *h) { if (n < 30 || le32(b, 4) < 4 || le32(b, 4) > n - 8 || le32(b, 16) > n - 20) { return 0; } if (img_starts(b, n, 12, "VP8 ")) { if ((b[20] & 1) != 0 || b[23] != 0x9d || b[24] != 0x01 || b[25] != 0x2a) { return 0; /* not a key frame */ } *w = (long)(le16(b, 26) & 0x3fff); *h = (long)(le16(b, 28) & 0x3fff); return 1; } if (img_starts(b, n, 12, "VP8L")) { unsigned long bits = le32(b, 21); if (b[20] != 0x2f || (bits >> 29) != 0) { return 0; /* signature, version 0 */ } *w = (long)(bits & 0x3fff) + 1; *h = (long)(bits >> 14 & 0x3fff) + 1; return 1; } if (img_starts(b, n, 12, "VP8X")) { *w = (long)(b[24] | (unsigned long)b[25] << 8 | (unsigned long)b[26] << 16) + 1; *h = (long)(b[27] | (unsigned long)b[28] << 8 | (unsigned long)b[29] << 16) + 1; return 1; } return 0; } static int set_why(char *why, size_t cap, int rc, const char *fmt, long a, long b) { if (why && cap) { (void)snprintf(why, cap, fmt, a, b); } return rc; } int pic_image_inspect(pic_image *img, const char *declared, char *why, size_t cap) { const char *mime = pic_image_sniff(img->data, img->len < 4100 ? img->len : 4100), *want = NULL; long w = 0, h = 0; int ok; img->width = img->height = 0; if (!mime) { return set_why(why, cap, PIC_IMAGE_INVALID, "not a PNG, JPEG, GIF, WebP or BMP image", 0, 0); } if (declared) { want = pic_image_inline_mime(declared); if (!want && strncasecmp(declared, "image/bmp", 9) == 0) { want = "image/bmp"; } } if (declared && (!want || strcmp(want, mime) != 0)) { if (why && cap) { (void)snprintf(why, cap, "declared %.40s but the data is %s", declared, mime); } return PIC_IMAGE_MISMATCH; } img->mime = mime; if (strcmp(mime, "image/bmp") == 0) { return set_why(why, cap, PIC_IMAGE_UNSUPPORTED, "BMP is not an inline type", 0, 0); } ok = strcmp(mime, "image/png") == 0 ? png_size(img->data, img->len, &w, &h) : strcmp(mime, "image/jpeg") == 0 ? jpeg_size(img->data, img->len, &w, &h) : strcmp(mime, "image/gif") == 0 ? gif_size(img->data, img->len, &w, &h) : webp_size(img->data, img->len, &w, &h); if (!ok || w <= 0 || h <= 0) { if (why && cap) { (void)snprintf(why, cap, "the %s header is invalid", mime); } return PIC_IMAGE_INVALID; } img->width = w; img->height = h; return PIC_IMAGE_OK; } int pic_image_fit(pic_image *img, char *why, size_t cap) { /* Pi shrinks an image to 2000x2000 under the limit; pic has no decoder * yet, so it only refuses what providers would. */ if (img->width > PIC_IMAGE_MAX_SIDE || img->height > PIC_IMAGE_MAX_SIDE) { return set_why(why, cap, PIC_IMAGE_TOO_LARGE, "%ldx%ld pixels; at most 8000 a side", img->width, img->height); } if ((img->len + 2) / 3 * 4 >= PIC_IMAGE_MAX_BASE64) { return set_why(why, cap, PIC_IMAGE_TOO_LARGE, "over the 4.5MB inline limit", 0, 0); } return PIC_IMAGE_OK; } int pic_image_check(pic_image *img, const char *declared, char *why, size_t cap) { int rc = pic_image_inspect(img, declared, why, cap); return rc == PIC_IMAGE_OK ? pic_image_fit(img, why, cap) : rc; } const char *pic_image_note(int rc) { return rc == PIC_IMAGE_TOO_LARGE ? PIC_IMAGE_TOO_BIG : PIC_IMAGE_UNCONVERTIBLE; } char *pic_image_prepare(const char *text, pic_image *images, size_t *n, char *err, size_t errcap) { size_t kept = 0, cap = strlen(text) + 1, i; int noted = 0; char *out, why[160]; for (i = 0; i < *n; i++) { int rc = pic_image_check(&images[i], images[i].mime, why, sizeof why); if (rc != PIC_IMAGE_OK && rc != PIC_IMAGE_UNSUPPORTED) { (void)snprintf(err, errcap, "image %zu: %s", i + 1, why); return NULL; } cap += rc == PIC_IMAGE_UNSUPPORTED ? strlen(PIC_IMAGE_UNCONVERTIBLE) + 2 : 0; } if (!(out = malloc(cap))) { (void)snprintf(err, errcap, "out of memory"); return NULL; } memcpy(out, text, strlen(text) + 1); for (i = 0; i < *n; i++) { if (strcmp(images[i].mime, "image/bmp") == 0) { /* Pi converts it; pic notes it, after a blank line */ strcat(out, noted++ ? "\n" : "\n\n"); strcat(out, PIC_IMAGE_UNCONVERTIBLE); } else { images[kept++] = images[i]; } } *n = kept; return out; } static const char img_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++] = img_b64[v >> 18 & 63]; out[o++] = img_b64[v >> 12 & 63]; out[o++] = img_b64[v >> 6 & 63]; out[o++] = img_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++] = img_b64[v >> 18 & 63]; out[o++] = img_b64[v >> 12 & 63]; out[o++] = i + 1 < n ? img_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; }