Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_tiff.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
30
31#include "nilorea/n_common.h"
32#include "nilorea/n_files.h"
33#include "nilorea/n_log.h"
34#include "nilorea/n_tiff.h"
35
36#include <math.h>
37#include <stdio.h>
38#include <string.h>
39#include <stdlib.h>
40#include <unistd.h>
41
43static int failures = 0;
44
48static void check(const char* what, int ok) {
49 if (!ok) {
50 failures++;
51 fprintf(stderr, "FAIL %s\n", what);
52 }
53}
54
59static void put16(unsigned char* p, uint16_t v, int be) {
60 p[be ? 0 : 1] = (unsigned char)(v >> 8);
61 p[be ? 1 : 0] = (unsigned char)(v & 0xff);
62}
63
68static void put32(unsigned char* p, uint32_t v, int be) {
69 int i = 0;
70
71 for (i = 0; i < 4; i++)
72 p[be ? i : 3 - i] = (unsigned char)(v >> (24 - 8 * i));
73}
74
82static void put_entry(unsigned char* e, uint16_t tag, uint16_t type, uint32_t count, uint32_t value, int be) {
83 put16(e, tag, be);
84 put16(e + 2, type, be);
85 put32(e + 4, count, be);
86 put32(e + 8, value, be);
87}
88
94static size_t build(unsigned char* buf, int be) {
95 const float floats[2] = {1.5f, -0.25f};
96 int i = 0;
97
98 memset(buf, 0, 256);
99 buf[0] = buf[1] = (unsigned char)(be ? 'M' : 'I');
100 put16(buf + 2, N_TIFF_MAGIC_DCP, be);
101 put32(buf + 4, 8, be);
102 put16(buf + 8, 5, be);
103 /* five entries of 12 bytes from 10, values from 80 */
104 put_entry(buf + 10, 50708, 2, 12, 80, be); /* ASCII, at 80 */
105 put_entry(buf + 22, 50778, 3, 1, 0, be); /* SHORT, in the entry */
106 put16(buf + 22 + 8, 21, be);
107 put_entry(buf + 34, 50721, 10, 3, 96, be); /* SRATIONAL x3, at 96 */
108 put_entry(buf + 46, 50938, 11, 2, 120, be); /* FLOAT x2, at 120 */
109 put_entry(buf + 58, 50939, 11, 64, 200, be); /* past the end */
110 memcpy(buf + 80, "Test Camera", 12);
111 put32(buf + 96, (uint32_t)8309, be);
112 put32(buf + 100, 10000, be);
113 put32(buf + 104, (uint32_t)-1786, be);
114 put32(buf + 108, 10000, be);
115 put32(buf + 112, 5, be);
116 put32(buf + 116, 0, be);
117 for (i = 0; i < 2; i++) {
118 uint32_t bits = 0;
119
120 memcpy(&bits, &floats[i], sizeof(bits));
121 put32(buf + 120 + 4 * i, bits, be);
122 }
123 return 256;
124}
125
128static void test_order(int be) {
129 unsigned char buf[256];
130 size_t len = build(buf, be);
131 N_TIFF* tiff = n_tiff_parse(buf, len);
132 double v[4] = {0, 0, 0, 0};
133 char text[64];
134
135 check("the buffer parses", tiff != NULL);
136 if (!tiff)
137 return;
138 check("the byte order is read", tiff->big_endian == be);
139 check("the magic is reported", tiff->magic == N_TIFF_MAGIC_DCP);
140 check("the entry past the end is skipped", tiff->count == 4 && !n_tiff_find(tiff, 50939));
141 check("ASCII reads", n_tiff_get_ascii(tiff, 50708, text, sizeof(text)) == 0 &&
142 strcmp(text, "Test Camera") == 0);
143 check("ASCII is cut to the room given",
144 n_tiff_get_ascii(tiff, 50708, text, 5) == 0 && strcmp(text, "Test") == 0);
145 check("a short in the entry reads", n_tiff_get_doubles(tiff, 50778, v, 4) == 1 && v[0] == 21.0);
146 check("signed rationals read", n_tiff_get_doubles(tiff, 50721, v, 4) == 3 &&
147 fabs(v[0] - 0.8309) < 1e-9 && fabs(v[1] + 0.1786) < 1e-9 &&
148 v[2] == 0.0);
149 check("floats read", n_tiff_get_doubles(tiff, 50938, v, 4) == 2 && v[0] == 1.5 && v[1] == -0.25);
150 check("a count is reported", n_tiff_count(tiff, 50721) == 3 && n_tiff_count(tiff, 1) == 0);
151 check("ASCII is not a number", n_tiff_get_doubles(tiff, 50708, v, 4) == 0);
152 check("a number is not ASCII", n_tiff_get_ascii(tiff, 50721, text, sizeof(text)) == -1 &&
153 text[0] == '\0');
154 n_tiff_free(&tiff);
155 check("free clears the pointer", tiff == NULL);
156}
157
159static void test_broken(void) {
160 unsigned char buf[256];
161 size_t len = build(buf, 0);
162
163 check("a short buffer is refused", n_tiff_parse(buf, 6) == NULL);
164 buf[0] = 'X';
165 check("a bad byte order is refused", n_tiff_parse(buf, len) == NULL);
166 build(buf, 0);
167 put32(buf + 4, 250, 0);
168 check("a directory past the end is refused", n_tiff_parse(buf, len) == NULL);
169 build(buf, 0);
170 put16(buf + 8, 60, 0);
171 check("a directory longer than the buffer is refused", n_tiff_parse(buf, len) == NULL);
172}
173
177static int list_file(const char* path) {
178 unsigned char* data = NULL;
179 size_t len = 0;
180 N_TIFF* tiff = NULL;
181 size_t i = 0;
182
183 if (n_file_read_bounded(path, 64 * 1024 * 1024, &data, &len) != TRUE || !data) {
184 fprintf(stderr, "cannot read %s\n", path);
185 return 1;
186 }
187 tiff = n_tiff_parse(data, len);
188 if (!tiff) {
189 fprintf(stderr, "%s is not TIFF structured\n", path);
190 free(data);
191 return 1;
192 }
193 printf("%s: %s endian, magic 0x%04x, %zu entries\n", path, tiff->big_endian ? "big" : "little",
194 tiff->magic, tiff->count);
195 for (i = 0; i < tiff->count; i++)
196 printf(" tag %5u type %2u count %u\n", tiff->entries[i].tag, tiff->entries[i].type,
197 tiff->entries[i].count);
198 n_tiff_free(&tiff);
199 free(data);
200 return 0;
201}
202
214static void test_sub_ifd(int be) {
215 unsigned char buf[64];
216 N_TIFF* tiff = NULL;
217 N_TIFF* exif = NULL;
218 double offset = 0.0;
219 char text[8];
220
221 memset(buf, 0, sizeof(buf));
222 buf[0] = buf[1] = (unsigned char)(be ? 'M' : 'I');
223 put16(buf + 2, N_TIFF_MAGIC, be);
224 put32(buf + 4, 8, be);
225 /* IFD0: one entry, the EXIF pointer, a LONG holding 26. */
226 put16(buf + 8, 1, be);
227 put_entry(buf + 10, 0x8769, 4, 1, 26, be);
228 /* At 26: one entry, SubSecTimeOriginal, "45" and its terminator, which
229 * fit in the entry itself. */
230 put16(buf + 26, 1, be);
231 put_entry(buf + 28, 0x9291, 2, 3, 0, be);
232 memcpy(buf + 28 + 8, "45", 3);
233
234 tiff = n_tiff_parse(buf, sizeof(buf));
235 check("sub-directory: IFD0 parses", tiff != NULL);
236 check("sub-directory: the pointer reads", tiff && n_tiff_get_doubles(tiff, 0x8769, &offset, 1) == 1 &&
237 offset == 26.0);
238 check("sub-directory: its tag is not in IFD0", tiff && n_tiff_find(tiff, 0x9291) == NULL);
239 exif = n_tiff_parse_ifd(buf, sizeof(buf), (size_t)offset);
240 check("sub-directory: it parses at the offset", exif != NULL);
241 check("sub-directory: its ASCII reads", exif && n_tiff_get_ascii(exif, 0x9291, text, sizeof(text)) == 0 &&
242 strcmp(text, "45") == 0);
243 check("sub-directory: it takes the header's byte order", exif && exif->big_endian == be);
244 n_tiff_free(&exif);
245 n_tiff_free(&tiff);
246 check("sub-directory: an offset past the end is refused", n_tiff_parse_ifd(buf, sizeof(buf), 63) == NULL);
247 check("sub-directory: an offset inside the header is refused", n_tiff_parse_ifd(buf, sizeof(buf), 4) == NULL);
248 /* A count that runs past the end. */
249 put16(buf + 26, 9, be);
250 check("sub-directory: a directory longer than the buffer is refused",
251 n_tiff_parse_ifd(buf, sizeof(buf), 26) == NULL);
252}
253
254int main(int argc, char** argv) {
255 int opt = 0, log_level = LOG_ERR;
256
257 while ((opt = getopt(argc, argv, "V:")) != -1) {
258 if (opt != 'V') {
259 fprintf(stderr, "Usage: %s [-V LOG_LEVEL] [file]\n", argv[0]);
260 return 2;
261 }
262 log_level = atoi(optarg);
263 }
265 if (optind < argc)
266 return list_file(argv[optind]);
267 test_order(0);
268 test_order(1);
269 test_sub_ifd(0);
270 test_sub_ifd(1);
271 test_broken();
272 printf("%s: %d failure(s)\n", failures ? "FAILED" : "ok", failures);
273 return failures ? 1 : 0;
274}
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static int log_level
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
static N_HTTP_RESP * text(int status, const char *body)
text response
static void check(const char *what, int ok)
Count and print a failed condition.
Definition ex_icc.c:46
static void test_sub_ifd(int be)
Run the checks, or list a file given as the argument.
Definition ex_tiff.c:214
static size_t build(unsigned char *buf, int be)
Build a DCP shaped buffer: a name, a short, three signed rationals, two floats, and one entry pointin...
Definition ex_tiff.c:94
static void put16(unsigned char *p, uint16_t v, int be)
Write a 16 bit value in a byte order.
Definition ex_tiff.c:59
static void put32(unsigned char *p, uint32_t v, int be)
Write a 32 bit value in a byte order.
Definition ex_tiff.c:68
static void test_order(int be)
Parse a built buffer and check every value.
Definition ex_tiff.c:128
static void test_broken(void)
Broken buffers are refused, never read out of bounds.
Definition ex_tiff.c:159
static int list_file(const char *path)
List the tags of a file's first directory.
Definition ex_tiff.c:177
static void put_entry(unsigned char *e, uint16_t tag, uint16_t type, uint32_t count, uint32_t value, int be)
Write one directory entry.
Definition ex_tiff.c:82
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
int n_file_read_bounded(const char *path, size_t max_size, unsigned char **out, size_t *out_len)
read a whole file into a freshly allocated, NUL-terminated buffer, refusing a file larger than max_si...
Definition n_files.c:270
uint16_t tag
tag number
Definition n_tiff.h:70
N_TIFF_ENTRY * entries
the entries whose values lie inside the buffer, in file order
Definition n_tiff.h:90
uint16_t type
TIFF field type, 1 BYTE to 12 DOUBLE.
Definition n_tiff.h:72
uint32_t count
number of values
Definition n_tiff.h:74
int big_endian
1 for "MM" (Motorola, big endian), 0 for "II"
Definition n_tiff.h:86
size_t count
how many
Definition n_tiff.h:92
uint16_t magic
the 16 bit value after the byte order mark
Definition n_tiff.h:88
#define N_TIFF_MAGIC
The magic of a plain TIFF file.
Definition n_tiff.h:63
N_TIFF * n_tiff_parse(const unsigned char *data, size_t len)
Parse the first directory of a TIFF structured buffer.
Definition n_tiff.c:158
size_t n_tiff_count(const N_TIFF *tiff, uint16_t tag)
How many values a tag has.
Definition n_tiff.c:188
size_t n_tiff_get_doubles(const N_TIFF *tiff, uint16_t tag, double *out, size_t max)
Read a numeric tag as doubles.
Definition n_tiff.c:194
#define N_TIFF_MAGIC_DCP
The magic of an Adobe DCP camera profile, "RC" read little endian.
Definition n_tiff.h:65
const N_TIFF_ENTRY * n_tiff_find(const N_TIFF *tiff, uint16_t tag)
Find an entry by tag.
Definition n_tiff.c:177
int n_tiff_get_ascii(const N_TIFF *tiff, uint16_t tag, char *out, size_t out_len)
Read an ASCII tag.
Definition n_tiff.c:264
N_TIFF * n_tiff_parse_ifd(const unsigned char *data, size_t len, size_t ifd)
Parse a directory at a given offset of a TIFF structured buffer: a sub-directory such as EXIF's,...
Definition n_tiff.c:94
void n_tiff_free(N_TIFF **tiff)
Release a parse, and set the pointer to NULL.
Definition n_tiff.c:170
The first directory of a TIFF structured buffer.
Definition n_tiff.h:80
Common headers and low-level functions & define.
Files configuration header.
Generic log system.
Reading the tags of a TIFF structured buffer.