Artifact ffbadabbafa8ab891b57f0e1fb7b3bc71da413d35037da77f41ca63af97a0371:
- File
src/sqlar.c
— 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: 2698) [annotate] [blame] [check-ins using] [more...]
/* The SQLite Archive's compression functions (https://sqlite.org/sqlar.html), * as SQLite's ext/misc/sqlar.c defines them, over the vendored zlib: * sqlar_compress(X) X deflated (zlib format) when that is smaller, * else X unchanged * sqlar_uncompress(Y, SZ) Y inflated to SZ bytes, or Y itself when SZ is * its length (stored raw) * A row keeps sz = the original length beside the data, so sz = length(data) * marks raw content. */ #include <limits.h> #include <stdlib.h> #include "sqlar.h" #include "zlib.h" static void sqlar_compress(sqlite3_context *ctx, int argc, sqlite3_value **argv) { const Bytef *in; int n; uLongf out_n; Bytef *out; (void)argc; if (sqlite3_value_type(argv[0]) != SQLITE_BLOB && sqlite3_value_type(argv[0]) != SQLITE_TEXT) { sqlite3_result_value(ctx, argv[0]); return; } in = sqlite3_value_blob(argv[0]); n = sqlite3_value_bytes(argv[0]); out_n = compressBound((uLong)n); if (!(out = sqlite3_malloc64(out_n))) { sqlite3_result_error_nomem(ctx); return; } if (compress(out, &out_n, in, (uLong)n) == Z_OK && out_n < (uLongf)n) { sqlite3_result_blob(ctx, out, (int)out_n, sqlite3_free); } else { sqlite3_free(out); sqlite3_result_value(ctx, argv[0]); } } static void sqlar_uncompress(sqlite3_context *ctx, int argc, sqlite3_value **argv) { const Bytef *in = sqlite3_value_blob(argv[0]); int n = sqlite3_value_bytes(argv[0]); sqlite3_int64 sz = sqlite3_value_int64(argv[1]); uLongf out_n; Bytef *out; (void)argc; if (sqlite3_value_type(argv[0]) == SQLITE_NULL) { /* pruned content */ sqlite3_result_null(ctx); return; } if (sz == n || sz < 0 || sz > INT_MAX) { sqlite3_result_value(ctx, argv[0]); return; } out_n = (uLongf)sz; if (!(out = sqlite3_malloc64((sqlite3_uint64)sz + 1))) { sqlite3_result_error_nomem(ctx); return; } if (uncompress(out, &out_n, in, (uLong)n) != Z_OK || out_n != (uLongf)sz) { sqlite3_free(out); sqlite3_result_error(ctx, "sqlar_uncompress: corrupt data", -1); return; } sqlite3_result_blob(ctx, out, (int)out_n, sqlite3_free); } int pic_sqlar_register(sqlite3 *db) { int flags = SQLITE_UTF8 | SQLITE_DETERMINISTIC | SQLITE_INNOCUOUS; return sqlite3_create_function(db, "sqlar_compress", 1, flags, NULL, sqlar_compress, NULL, NULL) == SQLITE_OK && sqlite3_create_function(db, "sqlar_uncompress", 2, flags, NULL, sqlar_uncompress, NULL, NULL) == SQLITE_OK ? 0 : -1; }