n_icc: embed a small profile in JPEG, PNG, WebP and TIFF shaped buffers and files, find it again, and refuse broken ones.
n_icc: embed a small profile in JPEG, PNG, WebP and TIFF shaped buffers and files, find it again, and refuse broken ones. With a file argument, report that file's profile instead.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <zlib.h>
static void check(
const char* what,
int ok) {
if (!ok) {
fprintf(stderr, "FAIL %s\n", what);
}
}
#define PROFILE_LEN 301
int i = 0;
out[i] = (unsigned char)(i * 7 + 3);
out[0] = 0;
out[1] = 0;
memcpy(out + 36, ok ? "acsp" : "nope", 4);
}
static void add(
unsigned char* buf,
size_t* len,
const void* data,
size_t n) {
memcpy(buf + *len, data, n);
*len += n;
}
static void segment(
unsigned char* buf,
size_t* len,
unsigned marker,
const unsigned char* body,
size_t n) {
unsigned char head[4];
head[0] = 0xFF;
head[1] = (unsigned char)marker;
head[2] = (unsigned char)((n + 2) >> 8);
head[3] = (unsigned char)((n + 2) & 0xff);
}
static void icc_chunk(
unsigned char* buf,
size_t* len,
int seq,
int of,
const unsigned char* part,
size_t n) {
memcpy(body, "ICC_PROFILE", 12);
body[12] = (unsigned char)seq;
body[13] = (unsigned char)of;
memcpy(body + 14, part, n);
segment(buf, len, 0xE2, body, n + 14);
}
static size_t make_jpeg(
unsigned char* buf,
const unsigned char* profile,
int chunks,
int padding) {
static unsigned char filler[60000];
const unsigned char soi[2] = {0xFF, 0xD8}, sos[4] = {0xFF, 0xDA, 0x00, 0x02};
size_t len = 0;
int i = 0;
segment(buf, &len, 0xE0, (
const unsigned char*)
"JFIF\0\1\1\0\0\1\0\1\0\0", 14);
for (i = 0; i < padding; i++)
segment(buf, &len, 0xE1, filler,
sizeof(filler));
if (profile) {
if (chunks == 2)
}
return len;
}
static void be32(
unsigned char* p,
size_t v) {
p[0] = (unsigned char)(v >> 24);
p[1] = (unsigned char)(v >> 16);
p[2] = (unsigned char)(v >> 8);
p[3] = (unsigned char)v;
}
static void png_chunk(
unsigned char* buf,
size_t* len,
const char*
type,
const unsigned char* body,
size_t n) {
unsigned char head[8], crc[4] = {0, 0, 0, 0};
memcpy(head + 4,
type, 4);
}
static size_t make_png(
unsigned char* buf,
const unsigned char* profile,
int after) {
static const unsigned char sig[8] = {0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A};
unsigned char ihdr[13], iccp[64 + 2 *
PROFILE_LEN], idat[4] = {0, 0, 0, 0};
size_t len = 0;
memset(ihdr, 0, sizeof(ihdr));
memcpy(iccp, "Test profile", 13);
iccp[13] = 0;
compress2(iccp + 14, &packed, profile,
PROFILE_LEN, 9);
if (!after)
png_chunk(buf, &len,
"iCCP", iccp, 14 + packed);
if (after)
png_chunk(buf, &len,
"iCCP", iccp, 14 + packed);
return len;
}
static void le32(
unsigned char* p,
size_t v) {
p[0] = (unsigned char)v;
p[1] = (unsigned char)(v >> 8);
p[2] = (unsigned char)(v >> 16);
p[3] = (unsigned char)(v >> 24);
}
static size_t make_webp(
unsigned char* buf,
const unsigned char* profile) {
unsigned char head[8], vp8x[10], pad = 0;
size_t len = 0;
memset(vp8x, 0, sizeof(vp8x));
add(buf, &len,
"RIFF\0\0\0\0WEBP", 12);
memcpy(head, "VP8X", 4);
le32(head + 4,
sizeof(vp8x));
add(buf, &len, vp8x,
sizeof(vp8x));
memcpy(head, "ICCP", 4);
memcpy(head, "VP8L", 4);
return len;
}
static size_t make_tiff(
unsigned char* buf,
const unsigned char* profile,
int be) {
size_t len = 8 + 2 + 12 + 4;
int i = 0;
memset(buf, 0, len);
buf[0] = buf[1] = be ? 'M' : 'I';
if (be) {
buf[3] = 42;
buf[9] = 1;
buf[10] = 0x87;
buf[11] = 0x73;
buf[13] = 7;
} else {
buf[2] = 42;
buf[8] = 1;
buf[10] = 0x73;
buf[11] = 0x87;
buf[12] = 7;
}
buf[len + (size_t)i] = profile[i];
}
static void expect(
const char* what,
const unsigned char* buf,
size_t len,
const unsigned char* want,
int expect_result) {
unsigned char* icc = NULL;
size_t icc_len = 0;
char label[160];
FILE* file = NULL;
snprintf(label, sizeof(label), "%s: from a buffer", what);
check(label, got == expect_result &&
(want || !icc));
free(icc);
icc = NULL;
file = fopen("ex_icc.tmp", "wb");
if (file) {
fwrite(buf, 1, len, file);
fclose(file);
snprintf(label, sizeof(label), "%s: from a file", what);
check(label, got == expect_result &&
free(icc);
remove("ex_icc.tmp");
}
}
int main(
int argc,
char** argv) {
unsigned char* buf = malloc(1024 * 1024);
size_t len = 0;
int opt = 0;
if (!buf)
return 1;
while ((opt = getopt(argc, argv, "V:")) != -1) {
if (opt != 'V') {
fprintf(stderr, "Usage: %s [-V LOG_LEVEL] [file]\n", argv[0]);
free(buf);
return 2;
}
}
if (optind < argc) {
const char* path = argv[optind];
unsigned char* icc = NULL;
size_t icc_len = 0;
if (got == 1)
printf("%s: a profile of %zu bytes\n", path, icc_len);
else
printf("%s: %s\n", path, got == 0 ? "no profile" : "a broken profile, or unreadable");
free(icc);
free(buf);
return got < 0 ? 1 : 0;
}
expect(
"JPEG, chunks out of order", buf, len, profile, 1);
expect(
"JPEG, the profile past the first read", buf, len, profile, 1);
expect(
"JPEG without a profile", buf, len, NULL, 0);
expect(
"JPEG with a chunk missing", buf, len, NULL, -1);
expect(
"a JPEG cut short", buf, len - 60, NULL, 0);
expect(
"PNG", buf, len, profile, 1);
expect(
"PNG with iCCP after the image data", buf, len, NULL, 0);
expect(
"WebP", buf, len, profile, 1);
expect(
"TIFF little endian", buf, len, profile, 1);
expect(
"TIFF big endian", buf, len, profile, 1);
expect(
"TIFF with a broken profile", buf, len, NULL, -1);
check(
"a buffer of another kind has none",
n_icc_from_buffer((
const unsigned char*)
"GIF89a..", 8, &(
unsigned char*){NULL}, &(size_t){0}) == 0);
check(
"a missing file is an error",
n_icc_from_file(
"/nonexistent/ex_icc", &(
unsigned char*){NULL}, &(size_t){0}) == -1);
free(buf);
fprintf(stderr,
"ex_icc: %d failure(s)\n",
failures);
return 1;
}
printf("ex_icc: ok\n");
return 0;
}
static int expect(BITMAP *bmp, int x, int y, int r, int g, int b, const char *what)
static unsigned char * make_png(int w, int h, size_t *out_len)
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
static void make_profile(unsigned char *out, int ok)
A profile shaped buffer: its size, "acsp" at 36, a pattern after.
static size_t make_tiff(unsigned char *buf, const unsigned char *profile, int be)
A TIFF shaped buffer with the profile in tag 34675.
static void png_chunk(unsigned char *buf, size_t *len, const char *type, const unsigned char *body, size_t n)
A PNG chunk, CRC left zero: nothing here checks it.
static void segment(unsigned char *buf, size_t *len, unsigned marker, const unsigned char *body, size_t n)
A JPEG APP segment.
static void icc_chunk(unsigned char *buf, size_t *len, int seq, int of, const unsigned char *part, size_t n)
One ICC chunk of a JPEG.
static void be32(unsigned char *p, size_t v)
A big endian 32 bit value.
#define PROFILE_LEN
the test profile's length, odd so the WebP padding is exercised
static size_t make_webp(unsigned char *buf, const unsigned char *profile)
A WebP shaped buffer: VP8X, ICCP (odd length, padded), VP8L.
static void add(unsigned char *buf, size_t *len, const void *data, size_t n)
Append bytes to a growing buffer.
static void check(const char *what, int ok)
Count and print a failed condition.
static size_t make_jpeg(unsigned char *buf, const unsigned char *profile, int chunks, int padding)
A JPEG shaped buffer: SOI, an APP0, padding segments, the profile in two chunks, second first,...
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_NULL
no log output
int n_icc_from_file(const char *path, unsigned char **icc, size_t *icc_len)
Find the profile embedded in an image file.
int n_icc_from_buffer(const unsigned char *data, size_t len, unsigned char **icc, size_t *icc_len)
Find the profile embedded in an image file held in memory.
int n_icc_is_profile(const unsigned char *data, size_t len)
Does a buffer look like an ICC profile: at least a header, a size field no larger than the buffer,...
Common headers and low-level functions & define.
Finding the ICC colour profile embedded in an image file.
static uint32_t le32(const unsigned char *p)
helper: a little endian 32 bit value