src/pdfmake_meta.c

filter: Statementstmt 58.6% 78 / 133 Branchbran 33.5% 45 / 134 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 46.0% 123 / 267 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/crypt.xs ↑ Summary xs/watermark.xs »
1 /*
2 * pdfmake_meta.c — Document Information Dictionary implementation.
3 */
4  
5 #include "pdfmake_meta.h"
6 #include "pdfmake_arena.h"
7 #include "pdfmake.h" /* PDFMAKE_VERSION, so Producer tracks the dist */
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <errno.h>
12  
13 /*----------------------------------------------------------------------------
14 * PDF date formatting (§7.9.4)
15 *
16 * Format: D:YYYYMMDDHHmmSSOHH'mm'
17 * YYYY = 4-digit year
18 * MM = month (01-12)
19 * DD = day (01-31)
20 * HH = hour (00-23)
21 * mm = minute (00-59)
22 * SS = second (00-59)
23 * O = relationship to UTC: +, -, or Z
24 * HH'mm' = timezone offset (only if O is + or -)
25 *--------------------------------------------------------------------------*/
26  
27 1196 char *pdfmake_format_date(time_t t, char *buf, size_t buflen) {
28 struct tm local_tm;
29 struct tm utc_tm;
30 time_t local_epoch;
31 time_t utc_epoch;
32 long offset_secs;
33 int offset_mins;
34 int n;
35 char sign;
36 int off_hours;
37 int off_mins;
38 int n2;
39  
40 1196 ▶ if (!buf || buflen < 24) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!buf || buflen < 24) return NULL; 1196 0 1196
if (!buf || buflen < 24) return NULL; 0 1196 1196
41  
42 #ifdef _WIN32
43 localtime_s(&local_tm, &t);
44 #else
45 1196 localtime_r(&t, &local_tm);
46 #endif
47  
48 /* Get UTC time to compute offset */
49 #ifdef _WIN32
50 gmtime_s(&utc_tm, &t);
51 #else
52 1196 gmtime_r(&t, &utc_tm);
53 #endif
54  
55 /* Compute timezone offset in minutes */
56 1196 local_epoch = mktime(&local_tm);
57 1196 utc_epoch = mktime(&utc_tm);
58 1196 offset_secs = (long)difftime(local_epoch, utc_epoch);
59 1196 offset_mins = (int)(offset_secs / 60);
60  
61 /* Format base date part */
62 1196 n = snprintf(buf, buflen, "D:%04d%02d%02d%02d%02d%02d",
63 1196 local_tm.tm_year + 1900,
64 1196 local_tm.tm_mon + 1,
65 local_tm.tm_mday,
66 local_tm.tm_hour,
67 local_tm.tm_min,
68 local_tm.tm_sec);
69  
70 1196 ▶ if (n < 0 || (size_t)n >= buflen) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || (size_t)n >= buflen) return NULL; 1196 0 1196
if (n < 0 || (size_t)n >= buflen) return NULL; 0 1196 1196
71  
72 /* Append timezone */
73 1196 ▶ if (offset_mins == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset_mins == 0) { 1196 0 1196
74 /* UTC */
75 1196 ▶ if ((size_t)n + 1 >= buflen) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)n + 1 >= buflen) return NULL; 0 1196 1196
76 1196 buf[n++] = 'Z';
77 1196 buf[n] = '\0';
78 } else {
79 /* Offset: +HH'mm' or -HH'mm' */
80 0 ▶ sign = (offset_mins >= 0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
sign = (offset_mins >= 0) ? '+' : '-'; 0 0 0
81 0 ▶ if (offset_mins < 0) offset_mins = -offset_mins;
Branch: 1 branch
BranchTrueFalseTotal
if (offset_mins < 0) offset_mins = -offset_mins; 0 0 0
82 0 off_hours = offset_mins / 60;
83 0 off_mins = offset_mins % 60;
84  
85 0 n2 = snprintf(buf + n, buflen - (size_t)n, "%c%02d'%02d'",
86 sign, off_hours, off_mins);
87 0 ▶ if (n2 < 0 || (size_t)(n + n2) >= buflen) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (n2 < 0 || (size_t)(n + n2) >= buflen) return NULL; 0 0 0
if (n2 < 0 || (size_t)(n + n2) >= buflen) return NULL; 0 0 0
88 }
89  
90 1196 return buf;
91 }
92  
93 0 char *pdfmake_format_date_utc(time_t t, char *buf, size_t buflen) {
94 struct tm utc_tm;
95 int n;
96  
97 0 ▶ if (!buf || buflen < 18) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!buf || buflen < 18) return NULL; 0 0 0
if (!buf || buflen < 18) return NULL; 0 0 0
98  
99 #ifdef _WIN32
100 gmtime_s(&utc_tm, &t);
101 #else
102 0 gmtime_r(&t, &utc_tm);
103 #endif
104  
105 0 n = snprintf(buf, buflen, "D:%04d%02d%02d%02d%02d%02dZ",
106 0 utc_tm.tm_year + 1900,
107 0 utc_tm.tm_mon + 1,
108 utc_tm.tm_mday,
109 utc_tm.tm_hour,
110 utc_tm.tm_min,
111 utc_tm.tm_sec);
112  
113 0 ▶ if (n < 0 || (size_t)n >= buflen) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || (size_t)n >= buflen) return NULL; 0 0 0
if (n < 0 || (size_t)n >= buflen) return NULL; 0 0 0
114  
115 0 return buf;
116 }
117  
118 /*----------------------------------------------------------------------------
119 * Internal: ensure doc has an Info dictionary
120 *--------------------------------------------------------------------------*/
121  
122 1977 static pdfmake_obj_t *ensure_info_dict(pdfmake_doc_t *doc) {
123 pdfmake_arena_t *arena;
124 pdfmake_obj_t info;
125 uint32_t num;
126 1977 ▶ if (!doc) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return NULL; 0 1977 1977
127  
128 /* If we already have an Info object, return its dict */
129 1977 ▶ if (doc->info_num != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (doc->info_num != 0) { 1363 614 1977
130 1363 pdfmake_obj_t *obj = pdfmake_doc_get(doc, doc->info_num);
131 1363 ▶ if (obj && obj->kind == PDFMAKE_DICT) {
Branch: 2 branches
BranchTrueFalseTotal
if (obj && obj->kind == PDFMAKE_DICT) { 1363 0 1363
if (obj && obj->kind == PDFMAKE_DICT) { 1362 1 1363
132 1362 return obj;
133 }
134 }
135  
136 /* Create a new Info dictionary */
137 615 arena = pdfmake_doc_arena(doc);
138 615 info = pdfmake_dict_new(arena);
139 615 ▶ if (info.kind != PDFMAKE_DICT) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (info.kind != PDFMAKE_DICT) return NULL; 0 615 615
140  
141 /* Add to document as indirect object */
142 615 num = pdfmake_doc_add(doc, info);
143 615 ▶ if (num == 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (num == 0) return NULL; 0 615 615
144  
145 /* Set as Info reference in trailer */
146 615 pdfmake_doc_set_info(doc, num, 0);
147  
148 /* Return pointer to the dict in the indirect object table */
149 615 return pdfmake_doc_get(doc, num);
150 }
151  
152 /*----------------------------------------------------------------------------
153 * Generic metadata set/get
154 *--------------------------------------------------------------------------*/
155  
156 1977 pdfmake_err_t pdfmake_meta_set(pdfmake_doc_t *doc, const char *key, const char *value) {
157 pdfmake_obj_t *info;
158 pdfmake_arena_t *arena;
159 uint32_t key_id;
160 pdfmake_obj_t val;
161  
162 1977 ▶ if (!doc || !key) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!doc || !key) return PDFMAKE_EINVAL; 1977 0 1977
if (!doc || !key) return PDFMAKE_EINVAL; 0 1977 1977
163  
164 1977 info = ensure_info_dict(doc);
165 1977 ▶ if (!info) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!info) return PDFMAKE_ENOMEM; 0 1977 1977
166  
167 1977 arena = pdfmake_doc_arena(doc);
168  
169 /* Intern the key name */
170 1977 key_id = pdfmake_arena_intern_name(arena, key, strlen(key));
171 1977 ▶ if (key_id == 0) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (key_id == 0) return PDFMAKE_ENOMEM; 0 1977 1977
172  
173 /* Create string value (or null if value is NULL) */
174 1977 ▶ if (value) {
Branch: 1 branch
BranchTrueFalseTotal
if (value) { 1977 0 1977
175 1977 val = pdfmake_str_cstr(arena, value);
176 1977 ▶ if (val.kind != PDFMAKE_STR) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (val.kind != PDFMAKE_STR) return PDFMAKE_ENOMEM; 0 1977 1977
177 } else {
178 0 val = pdfmake_null();
179 }
180  
181 /* Set in dict */
182 1977 ▶ if (!pdfmake_dict_set(arena, info, key_id, val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_dict_set(arena, info, key_id, val)) { 0 1977 1977
183 0 return PDFMAKE_ENOMEM;
184 }
185  
186 1977 return PDFMAKE_OK;
187 }
188  
189 1861 const char *pdfmake_meta_get(pdfmake_doc_t *doc, const char *key) {
190 pdfmake_obj_t *info;
191 pdfmake_arena_t *arena;
192 uint32_t key_id;
193 pdfmake_obj_t *val;
194 size_t len;
195 const uint8_t *bytes;
196  
197 1861 ▶ if (!doc || !key || doc->info_num == 0) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!doc || !key || doc->info_num == 0) return NULL; 1861 0 1861
if (!doc || !key || doc->info_num == 0) return NULL; 1861 0 1861
if (!doc || !key || doc->info_num == 0) return NULL; 583 1278 1861
198  
199 1278 info = pdfmake_doc_get(doc, doc->info_num);
200 1278 ▶ if (!info || info->kind != PDFMAKE_DICT) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!info || info->kind != PDFMAKE_DICT) return NULL; 1278 0 1278
if (!info || info->kind != PDFMAKE_DICT) return NULL; 1 1277 1278
201  
202 1277 arena = pdfmake_doc_arena(doc);
203  
204 /* Intern the key name to look it up */
205 1277 key_id = pdfmake_arena_intern_name(arena, key, strlen(key));
206 1277 ▶ if (key_id == 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (key_id == 0) return NULL; 0 1277 1277
207  
208 1277 val = pdfmake_dict_get(info, key_id);
209 1277 ▶ if (!val || val->kind != PDFMAKE_STR) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!val || val->kind != PDFMAKE_STR) return NULL; 64 1213 1277
if (!val || val->kind != PDFMAKE_STR) return NULL; 0 64 64
210  
211 /* Return the string bytes (null-terminated by str_cstr) */
212 64 bytes = pdfmake_get_str_bytes(val, &len);
213 (void)len;
214 64 return (const char *)bytes;
215 }
216  
217 /*----------------------------------------------------------------------------
218 * Date field setters
219 *--------------------------------------------------------------------------*/
220  
221 598 pdfmake_err_t pdfmake_meta_set_creation_date(pdfmake_doc_t *doc, time_t t) {
222 char buf[32];
223 598 ▶ if (!pdfmake_format_date(t, buf, sizeof(buf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_format_date(t, buf, sizeof(buf))) { 0 598 598
224 0 return PDFMAKE_EINVAL;
225 }
226 598 return pdfmake_meta_set(doc, PDFMAKE_META_CREATION_DATE, buf);
227 }
228  
229 598 pdfmake_err_t pdfmake_meta_set_mod_date(pdfmake_doc_t *doc, time_t t) {
230 char buf[32];
231 598 ▶ if (!pdfmake_format_date(t, buf, sizeof(buf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_format_date(t, buf, sizeof(buf))) { 0 598 598
232 0 return PDFMAKE_EINVAL;
233 }
234 598 return pdfmake_meta_set(doc, PDFMAKE_META_MOD_DATE, buf);
235 }
236  
237 /*----------------------------------------------------------------------------
238 * Trapped field (name value)
239 *--------------------------------------------------------------------------*/
240  
241 0 pdfmake_err_t pdfmake_meta_set_trapped(pdfmake_doc_t *doc, pdfmake_trapped_t trapped) {
242 pdfmake_obj_t *info;
243 pdfmake_arena_t *arena;
244 uint32_t key_id;
245 const char *name_str;
246 pdfmake_obj_t val;
247  
248 0 ▶ if (!doc) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return PDFMAKE_EINVAL; 0 0 0
249  
250 0 info = ensure_info_dict(doc);
251 0 ▶ if (!info) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!info) return PDFMAKE_ENOMEM; 0 0 0
252  
253 0 arena = pdfmake_doc_arena(doc);
254  
255 /* Intern the Trapped key */
256 0 key_id = pdfmake_arena_intern_name(arena, PDFMAKE_META_TRAPPED, 7);
257 0 ▶ if (key_id == 0) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (key_id == 0) return PDFMAKE_ENOMEM; 0 0 0
258  
259 /* Create name value */
260 0 switch (trapped) {
261 0 case PDFMAKE_TRAPPED_TRUE: name_str = "True"; break;
262 0 case PDFMAKE_TRAPPED_FALSE: name_str = "False"; break;
263 0 default: name_str = "Unknown"; break;
264 }
265  
266 0 val = pdfmake_name_cstr(arena, name_str);
267 0 ▶ if (val.kind != PDFMAKE_NAME) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (val.kind != PDFMAKE_NAME) return PDFMAKE_ENOMEM; 0 0 0
268  
269 0 ▶ if (!pdfmake_dict_set(arena, info, key_id, val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_dict_set(arena, info, key_id, val)) { 0 0 0
270 0 return PDFMAKE_ENOMEM;
271 }
272  
273 0 return PDFMAKE_OK;
274 }
275  
276 0 pdfmake_trapped_t pdfmake_meta_get_trapped(pdfmake_doc_t *doc) {
277 pdfmake_obj_t *info;
278 pdfmake_arena_t *arena;
279 uint32_t key_id;
280 pdfmake_obj_t *val;
281 const char *name;
282  
283 0 ▶ if (!doc || doc->info_num == 0) return PDFMAKE_TRAPPED_UNKNOWN;
Branch: 2 branches
BranchTrueFalseTotal
if (!doc || doc->info_num == 0) return PDFMAKE_TRAPPED_UNKNOWN; 0 0 0
if (!doc || doc->info_num == 0) return PDFMAKE_TRAPPED_UNKNOWN; 0 0 0
284  
285 0 info = pdfmake_doc_get(doc, doc->info_num);
286 0 ▶ if (!info || info->kind != PDFMAKE_DICT) return PDFMAKE_TRAPPED_UNKNOWN;
Branch: 2 branches
BranchTrueFalseTotal
if (!info || info->kind != PDFMAKE_DICT) return PDFMAKE_TRAPPED_UNKNOWN; 0 0 0
if (!info || info->kind != PDFMAKE_DICT) return PDFMAKE_TRAPPED_UNKNOWN; 0 0 0
287  
288 0 arena = pdfmake_doc_arena(doc);
289  
290 0 key_id = pdfmake_arena_intern_name(arena, PDFMAKE_META_TRAPPED, 7);
291 0 ▶ if (key_id == 0) return PDFMAKE_TRAPPED_UNKNOWN;
Branch: 1 branch
BranchTrueFalseTotal
if (key_id == 0) return PDFMAKE_TRAPPED_UNKNOWN; 0 0 0
292  
293 0 val = pdfmake_dict_get(info, key_id);
294 0 ▶ if (!val || val->kind != PDFMAKE_NAME) return PDFMAKE_TRAPPED_UNKNOWN;
Branch: 2 branches
BranchTrueFalseTotal
if (!val || val->kind != PDFMAKE_NAME) return PDFMAKE_TRAPPED_UNKNOWN; 0 0 0
if (!val || val->kind != PDFMAKE_NAME) return PDFMAKE_TRAPPED_UNKNOWN; 0 0 0
295  
296 0 name = pdfmake_get_name_bytes(arena, val);
297 0 ▶ if (!name) return PDFMAKE_TRAPPED_UNKNOWN;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return PDFMAKE_TRAPPED_UNKNOWN; 0 0 0
298  
299 0 ▶ if (strcmp(name, "True") == 0) return PDFMAKE_TRAPPED_TRUE;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "True") == 0) return PDFMAKE_TRAPPED_TRUE; 0 0 0
300 0 ▶ if (strcmp(name, "False") == 0) return PDFMAKE_TRAPPED_FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "False") == 0) return PDFMAKE_TRAPPED_FALSE; 0 0 0
301 0 return PDFMAKE_TRAPPED_UNKNOWN;
302 }
303  
304 /*----------------------------------------------------------------------------
305 * Auto-metadata (called during write)
306 *--------------------------------------------------------------------------*/
307  
308 /*
309 * SOURCE_DATE_EPOCH (the reproducible-builds convention): when set to a
310 * non-negative integer, it replaces the wall clock everywhere a timestamp
311 * would otherwise leak into the output. Two runs over the same input then
312 * produce the same bytes, which is what makes a golden corpus - and the
313 * promise that a customer's documents do not move - checkable at all.
314 *
315 * Anything unparseable is ignored rather than diagnosed: this is a build
316 * knob, and a document that fails to render because an environment
317 * variable had a typo would be a worse failure than a timestamp.
318 */
319 static pdfmake_source_date_hook_t source_date_hook = NULL;
320  
321 104 void pdfmake_set_source_date_hook(pdfmake_source_date_hook_t hook) {
322 104 source_date_hook = hook;
323 104 }
324  
325 1197 int pdfmake_source_date(time_t *out) {
326 const char *s;
327 char *end;
328 long long v;
329  
330 /* An embedding host may keep the variable somewhere getenv() cannot see;
331 * see the hook's comment in pdfmake_meta.h. */
332 1197 ▶ if (source_date_hook) {
Branch: 1 branch
BranchTrueFalseTotal
if (source_date_hook) { 1197 0 1197
333 time_t t;
334 1197 ▶ if (source_date_hook(&t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (source_date_hook(&t)) { 126 1071 1197
335 126 ▶ if (out) *out = t;
Branch: 1 branch
BranchTrueFalseTotal
if (out) *out = t; 126 0 126
336 126 return 1;
337 }
338 1071 return 0;
339 }
340  
341 0 s = getenv("SOURCE_DATE_EPOCH");
342 0 ▶ if (!s || !*s) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!s || !*s) return 0; 0 0 0
if (!s || !*s) return 0; 0 0 0
343 0 errno = 0;
344 0 v = strtoll(s, &end, 10);
345 0 ▶ if (errno || end == s || *end || v < 0) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (errno || end == s || *end || v < 0) return 0; 0 0 0
if (errno || end == s || *end || v < 0) return 0; 0 0 0
if (errno || end == s || *end || v < 0) return 0; 0 0 0
if (errno || end == s || *end || v < 0) return 0; 0 0 0
346 0 ▶ if (out) *out = (time_t)v;
Branch: 1 branch
BranchTrueFalseTotal
if (out) *out = (time_t)v; 0 0 0
347 0 return 1;
348 }
349  
350 598 void pdfmake_meta_auto_fill(pdfmake_doc_t *doc) {
351 time_t now;
352  
353 598 ▶ if (!doc) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return; 0 598 598
354  
355 598 ▶ if (!pdfmake_source_date(&now))
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_source_date(&now)) 535 63 598
356 535 now = time(NULL);
357  
358 /* Set Producer if not already set */
359 598 ▶ if (!pdfmake_meta_get(doc, PDFMAKE_META_PRODUCER)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_meta_get(doc, PDFMAKE_META_PRODUCER)) { 596 2 598
360 596 pdfmake_meta_set(doc, PDFMAKE_META_PRODUCER, "PDF-Make/" PDFMAKE_VERSION);
361 }
362  
363 /* Set CreationDate if not already set */
364 598 ▶ if (!pdfmake_meta_get(doc, PDFMAKE_META_CREATION_DATE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_meta_get(doc, PDFMAKE_META_CREATION_DATE)) { 598 0 598
365 598 pdfmake_meta_set_creation_date(doc, now);
366 }
367  
368 /* Set ModDate if not already set. Callers who need to refresh it
369 * (e.g. after an edit) should call pdfmake_meta_set_mod_date
370 * explicitly; leaving it untouched here keeps the serialized bytes
371 * deterministic across multiple writes of the same doc — required
372 * for the two-pass TSA signing flow where pass 1's RSA imprint must
373 * match pass 2's RSA bytes. */
374 598 ▶ if (!pdfmake_meta_get(doc, PDFMAKE_META_MOD_DATE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_meta_get(doc, PDFMAKE_META_MOD_DATE)) { 598 0 598
375 598 pdfmake_meta_set_mod_date(doc, now);
376 }
377 }
« xs/crypt.xs ↑ Summary xs/watermark.xs »