jpeginfo: clean up

This commit is contained in:
Přemysl Eric Janouch 2021-12-03 14:19:01 +01:00
parent 38427ff88e
commit 46c46ac093
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 50 additions and 44 deletions

View File

@ -23,14 +23,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h> #include <stdarg.h>
// --- TIFF -------------------------------------------------------------------- // --- Utilities ---------------------------------------------------------------
// https://www.adobe.io/content/dam/udp/en/open/standards/tiff/TIFF6.pdf
// https://www.adobe.io/content/dam/udp/en/open/standards/tiff/TIFFPM6.pdf
// https://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf
//
// libtiff is a mess, and the format is not particularly complicated.
// Also, we'd still want to duplicate its tag tables.
// Exif libraries are senselessly copylefted.
static uint32_t static uint32_t
u32be(const uint8_t *p) u32be(const uint8_t *p)
@ -56,13 +49,20 @@ u16le(const uint8_t *p)
return (uint16_t) p[1] << 8 | p[0]; return (uint16_t) p[1] << 8 | p[0];
} }
// --- TIFF --------------------------------------------------------------------
// https://www.adobe.io/content/dam/udp/en/open/standards/tiff/TIFF6.pdf
// https://www.adobe.io/content/dam/udp/en/open/standards/tiff/TIFFPM6.pdf
// https://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf
//
// libtiff is a mess, and the format is not particularly complicated.
// Also, we'd still want to duplicate its tag tables.
// Exif libraries are senselessly copylefted.
static struct un { static struct un {
uint32_t (*u32) (const uint8_t *); uint32_t (*u32) (const uint8_t *);
uint16_t (*u16) (const uint8_t *); uint16_t (*u16) (const uint8_t *);
} unbe = {u32be, u16be}, unle = {u32le, u16le}; } unbe = {u32be, u16be}, unle = {u32le, u16le};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct tiffer { struct tiffer {
struct un *un; struct un *un;
const uint8_t *begin, *p, *end; const uint8_t *begin, *p, *end;
@ -467,7 +467,7 @@ parse_icc(jv o, const uint8_t *profile, size_t profile_len)
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/ // https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/
static jv static jv
parse_psir_block(jv o, uint16_t resource_id, const char *name, process_psir(jv o, uint16_t resource_id, const char *name,
const uint8_t *data, size_t len) const uint8_t *data, size_t len)
{ {
// TODO(p): These is more to extract here. The name is most often empty. // TODO(p): These is more to extract here. The name is most often empty.
@ -478,12 +478,9 @@ parse_psir_block(jv o, uint16_t resource_id, const char *name,
} }
static jv static jv
parse_psir(jv o, const uint8_t *p, size_t len) parse_psir_block(jv o, const uint8_t *p, size_t len, size_t *advance)
{ {
if (len == 0) *advance = 0;
return add_warning(o, "empty PSIR data");
while (len) {
if (len < 8 || memcmp(p, "8BIM", 4)) if (len < 8 || memcmp(p, "8BIM", 4))
return add_warning(o, "bad PSIR block header"); return add_warning(o, "bad PSIR block header");
@ -506,16 +503,25 @@ parse_psir(jv o, const uint8_t *p, size_t len)
len < header_len + resource_len_padded) len < header_len + resource_len_padded)
return add_warning(o, "runaway PSIR block"); return add_warning(o, "runaway PSIR block");
p += header_len;
len -= header_len;
char *cname = calloc(1, name_len_full); char *cname = calloc(1, name_len_full);
strncpy(cname, (const char *) name, name_len); strncpy(cname, (const char *) name, name_len);
o = parse_psir_block(o, resource_id, cname, p, resource_len); o = process_psir(o, resource_id, cname, p + header_len, resource_len);
free(cname); free(cname);
p += resource_len_padded; *advance = header_len + resource_len_padded;
len -= resource_len_padded; return o;
}
static jv
parse_psir(jv o, const uint8_t *p, size_t len)
{
if (len == 0)
return add_warning(o, "empty PSIR data");
size_t advance = 0;
while (len && (o = parse_psir_block(o, p, len, &advance), advance)) {
p += advance;
len -= advance;
} }
return o; return o;
} }