Skip to content

PDF Compress — C source

Shrink PDF file sizes in your browser — lossless optimization plus a JPEG quality slider for embedded images. Nothing uploads.

This is the C implementation — the same logic the interactive tool runs, in a shareable, citable form.

/* pdf-compress — two-pass, fully client-side PDF compression. C port
 * (canonical TS: src/lib/pdf-compress.ts; Go twin: cli/pdf-compress).
 * Honest scope note: C has no idiomatic PDF library to call, so like the
 * self-contained Go CLI this port ships the pure decision and error
 * classification helpers verbatim plus a raw-buffer load guard that refuses
 * garbage and /Encrypt-carrying inputs exactly like the TS load path. The
 * full object-graph walk (Resources -> XObject -> /Image with /Filter
 * /DCTDecode, re-encode at identical pixel dimensions) stays a documented
 * extension point: a PDF parser + JPEG re-encoder is its own project.
 * Caller owns nothing here — every function returns static strings. */
#include <ctype.h>
#include <stddef.h>
#include <string.h>

/* ---- pure helpers (unit-testable standalone, no library types) ---- */

/* ascii case-insensitive substring: the /encrypt|password/i heuristic. */
static int contains_ignore_case(const char *hay, const char *needle) {
    size_t n = strlen(needle);
    if (n == 0) return 1;
    for (; *hay; hay++) {
        size_t i = 0;
        while (i < n && hay[i] &&
               tolower((unsigned char)hay[i]) == tolower((unsigned char)needle[i])) i++;
        if (i == n) return 1;
    }
    return 0;
}

/* Pure classifier — encrypted vs invalid, from the raw error message.
 * Mirrors classifyLoadError() in src/lib/pdf-errors.ts. NULL/absent
 * messages classify as invalid. Returns "encrypted" or "invalid". */
const char *classify_load_error(const char *msg) {
    if (msg && (contains_ignore_case(msg, "encrypt") || contains_ignore_case(msg, "password")))
        return "encrypted";
    return "invalid";
}

/* Pure decision: only baseline single-stream JPEGs (/Filter /DCTDecode)
 * are recompressible — anything else (Flate, multi-filter chains like
 * DCTDecode,FlateDecode, no filter) stays byte-for-byte intact. Ported
 * verbatim from chooseImageAction() in src/lib/pdf-compress.ts. */
const char *choose_image_action(const char *filter) {
    return filter && strcmp(filter, "DCTDecode") == 0 ? "recompress" : "skip";
}

/* ---- raw-buffer load guard (the self-contained stand-in for load()) ---- */

typedef struct { const unsigned char *data; size_t len; } PdfBytes;

static int bytes_contains(const PdfBytes *b, const char *needle) {
    size_t n = strlen(needle);
    if (b->len < n) return 0;
    for (size_t i = 0; i + n <= b->len; i++)
        if (memcmp(b->data + i, needle, n) == 0) return 1;
    return 0;
}

/* Classify a raw input the way PDFDocument.load + mapError would: not a
 * %PDF header -> "invalid"; an /Encrypt entry anywhere in the document ->
 * "encrypted"; otherwise "ok". */
const char *classify_bytes(const PdfBytes *b) {
    if (b->len < 5 || memcmp(b->data, "%PDF-", 5) != 0) return "invalid";
    if (bytes_contains(b, "/Encrypt")) return "encrypted";
    return "ok";
}

/* Per-image outcome row the full walk would emit (display type):
 * index/before/after=-1 when kept/skipped "" | "grew" | "not-jpeg" |
 * "decode-failed". */
typedef struct { size_t index, before, after; const char *skipped; } ImageAccounting;

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →