src/pdfmake_doc.c

filter: Statementstmt 91.5% 259 / 283 Branchbran 59.7% 104 / 174 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.4% 363 / 457 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/markup_style.xs ↑ Summary xs/writer.xs »
1 /*
2 * pdfmake_doc.c — PDF document structure and file emission.
3 */
4  
5 #include "pdfmake_doc.h"
6 #include "pdfmake_meta.h"
7 #include "pdfmake_page.h"
8 #include "pdfmake_writer.h"
9 #include "pdfmake_attach.h"
10 #include "pdfmake_tag.h"
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <time.h>
15  
16 /*----------------------------------------------------------------------------
17 * FNV-1a hash for ID generation
18 *--------------------------------------------------------------------------*/
19  
20 #define FNV_OFFSET_BASIS 0xcbf29ce484222325ULL
21 #define FNV_PRIME 0x100000001b3ULL
22  
23 1198 static uint64_t fnv1a_64(const void *data, size_t len) {
24 1198 uint64_t hash = FNV_OFFSET_BASIS;
25 1198 const uint8_t *p = (const uint8_t *)data;
26 size_t i;
27 39534 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 38336 1198 39534
28 38336 hash ^= p[i];
29 38336 hash *= FNV_PRIME;
30 }
31 1198 return hash;
32 }
33  
34 /*----------------------------------------------------------------------------
35 * Lifecycle
36 *--------------------------------------------------------------------------*/
37  
38 967 pdfmake_doc_t *pdfmake_doc_new(void) {
39 967 pdfmake_doc_t *doc = calloc(1, sizeof(pdfmake_doc_t));
40 967 ▶ if (!doc) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return NULL; 0 967 967
41  
42 967 doc->arena = pdfmake_arena_new();
43 967 ▶ if (!doc->arena) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->arena) { 0 967 967
44 0 free(doc);
45 0 return NULL;
46 }
47  
48 967 doc->objects = calloc(PDFMAKE_DOC_INIT_CAP, sizeof(pdfmake_indirect_t));
49 967 ▶ if (!doc->objects) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->objects) { 0 967 967
50 0 pdfmake_arena_free(doc->arena);
51 0 free(doc);
52 0 return NULL;
53 }
54 967 doc->obj_cap = PDFMAKE_DOC_INIT_CAP;
55 967 doc->obj_count = 0;
56  
57 967 doc->pages = NULL;
58 967 doc->page_count = 0;
59 967 doc->page_cap = 0;
60 967 doc->pages_num = 0;
61 967 doc->finalized = 0;
62  
63 967 return doc;
64 }
65  
66 810 void pdfmake_doc_free(pdfmake_doc_t *doc) {
67 size_t i;
68 810 ▶ if (!doc) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return; 0 810 810
69 /* Free page annotation arrays (malloc'd, not arena) before arena is freed */
70 1622 ▶ for (i = 0; i < doc->page_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < doc->page_count; i++) { 812 810 1622
71 812 ▶ if (doc->pages[i] && doc->pages[i]->annots) {
Branch: 2 branches
BranchTrueFalseTotal
if (doc->pages[i] && doc->pages[i]->annots) { 812 0 812
if (doc->pages[i] && doc->pages[i]->annots) { 26 786 812
72 26 free(doc->pages[i]->annots);
73 }
74 }
75 810 pdfmake_doc_free_struct_tree(doc); /* calloc'd, not from the arena */
76 810 pdfmake_arena_free(doc->arena);
77 810 free(doc->objects);
78 810 free(doc->pages); /* Page structs are in arena, just free the pointer array */
79 810 free(doc->ocgs); /* OCG pointers in arena, just free the pointer array */
80 /* Free individual attachments (data + struct) */
81 810 ▶ if (doc->attachments) {
Branch: 1 branch
BranchTrueFalseTotal
if (doc->attachments) { 6 804 810
82 31 ▶ for (i = 0; i < doc->attach_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < doc->attach_count; i++) { 25 6 31
83 25 pdfmake_attachment_t *att = doc->attachments[i];
84 25 ▶ if (att) {
Branch: 1 branch
BranchTrueFalseTotal
if (att) { 25 0 25
85 25 free(att->data);
86 25 free(att);
87 }
88 }
89 6 free(doc->attachments);
90 }
91 810 free(doc);
92 }
93  
94 6602 pdfmake_arena_t *pdfmake_doc_arena(pdfmake_doc_t *doc) {
95 6602 ▶ return doc ? doc->arena : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return doc ? doc->arena : NULL; 6602 0 6602
96 }
97  
98 /*----------------------------------------------------------------------------
99 * Indirect object management
100 *--------------------------------------------------------------------------*/
101  
102 0 static int doc_grow_objects(pdfmake_doc_t *doc) {
103 0 size_t new_cap = doc->obj_cap * 2;
104 0 pdfmake_indirect_t *new_arr = realloc(doc->objects,
105 new_cap * sizeof(pdfmake_indirect_t));
106 0 ▶ if (!new_arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!new_arr) return 0; 0 0 0
107  
108 /* Zero out new entries */
109 0 memset(new_arr + doc->obj_cap, 0,
110 0 (new_cap - doc->obj_cap) * sizeof(pdfmake_indirect_t));
111  
112 0 doc->objects = new_arr;
113 0 doc->obj_cap = new_cap;
114 0 return 1;
115 }
116  
117 4321 uint32_t pdfmake_doc_add(pdfmake_doc_t *doc, pdfmake_obj_t obj) {
118 uint32_t num;
119 size_t idx;
120 4321 ▶ if (!doc) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return 0; 0 4321 4321
121  
122 /* Grow if needed */
123 4321 ▶ if (doc->obj_count >= doc->obj_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (doc->obj_count >= doc->obj_cap) { 0 4321 4321
124 0 ▶ if (!doc_grow_objects(doc)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc_grow_objects(doc)) return 0; 0 0 0
125 }
126  
127 /* Object numbers are 1-based; index 0 corresponds to object 1 */
128 4321 num = (uint32_t)(doc->obj_count + 1);
129 4321 idx = doc->obj_count;
130  
131 4321 doc->objects[idx].num = num;
132 4321 doc->objects[idx].gen = 0;
133 4321 doc->objects[idx].byte_offset = 0;
134 4321 doc->objects[idx].obj = obj;
135 4321 doc->objects[idx].in_use = 1;
136 4321 doc->obj_count++;
137  
138 4321 return num;
139 }
140  
141 15759 pdfmake_obj_t *pdfmake_doc_get(pdfmake_doc_t *doc, uint32_t num) {
142 size_t idx;
143 15759 ▶ if (!doc || num == 0 || num > doc->obj_count) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!doc || num == 0 || num > doc->obj_count) return NULL; 15759 0 15759
if (!doc || num == 0 || num > doc->obj_count) return NULL; 15750 9 15759
if (!doc || num == 0 || num > doc->obj_count) return NULL; 3 15747 15750
144 15747 idx = num - 1;
145 15747 ▶ if (!doc->objects[idx].in_use) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->objects[idx].in_use) return NULL; 0 15747 15747
146 15747 return &doc->objects[idx].obj;
147 }
148  
149 0 pdfmake_obj_t pdfmake_doc_ref(pdfmake_doc_t *doc, uint32_t num) {
150 0 uint16_t gen = 0;
151 0 ▶ if (doc && num > 0 && num <= doc->obj_count) {
Branch: 3 branches
BranchTrueFalseTotal
if (doc && num > 0 && num <= doc->obj_count) { 0 0 0
if (doc && num > 0 && num <= doc->obj_count) { 0 0 0
if (doc && num > 0 && num <= doc->obj_count) { 0 0 0
152 0 gen = doc->objects[num - 1].gen;
153 }
154 0 return pdfmake_ref(num, gen);
155 }
156  
157 /*----------------------------------------------------------------------------
158 * Trailer setup
159 *--------------------------------------------------------------------------*/
160  
161 905 void pdfmake_doc_set_root(pdfmake_doc_t *doc, uint32_t num, uint16_t gen) {
162 905 ▶ if (!doc) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return; 0 905 905
163 905 doc->root_num = num;
164 905 doc->root_gen = gen;
165 }
166  
167 866 void pdfmake_doc_set_info(pdfmake_doc_t *doc, uint32_t num, uint16_t gen) {
168 866 ▶ if (!doc) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return; 0 866 866
169 866 doc->info_num = num;
170 866 doc->info_gen = gen;
171 }
172  
173 599 void pdfmake_doc_generate_id(pdfmake_doc_t *doc) {
174 /*
175 * Generate ID using FNV-1a hash of:
176 * - Current time
177 * - Document pointer (randomness)
178 * - Object count
179 */
180 struct {
181 time_t t;
182 void *ptr;
183 size_t count;
184 uint64_t counter;
185 } seed;
186 static uint64_t counter = 0;
187 time_t src;
188 uint64_t h1;
189 uint64_t h2;
190  
191 599 ▶ if (!doc) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return; 0 599 599
192  
193 /* Zero the whole struct: it is hashed by bytes, and padding between the
194 * members is otherwise uninitialised, which would make the ID vary run
195 * to run even with every field pinned. */
196 599 memset(&seed, 0, sizeof(seed));
197  
198 599 ▶ if (pdfmake_source_date(&src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_source_date(&src)) { 63 536 599
199 /* Reproducible mode: the pointer (ASLR) and the process-wide counter
200 * are exactly the two inputs that stop identical documents hashing
201 * alike, so both are dropped. Two renders of the same document then
202 * carry the same /ID, which is the intent - the ID identifies the
203 * document, not the run that produced it. */
204 63 seed.t = src;
205 63 seed.ptr = NULL;
206 63 seed.count = doc->obj_count;
207 63 seed.count += (size_t)doc->page_count * 1000003u;
208 63 seed.counter = 0;
209 63 h1 = fnv1a_64(&seed, sizeof(seed));
210 63 seed.counter = 1;
211 63 h2 = fnv1a_64(&seed, sizeof(seed));
212 } else {
213 536 seed.t = time(NULL);
214 536 seed.ptr = doc;
215 536 seed.count = doc->obj_count;
216 536 seed.counter = ++counter;
217  
218 536 h1 = fnv1a_64(&seed, sizeof(seed));
219 536 seed.counter = ++counter;
220 536 h2 = fnv1a_64(&seed, sizeof(seed));
221 }
222  
223 /* Pack two 64-bit hashes into two 16-byte IDs */
224 599 memcpy(doc->id1, &h1, 8);
225 599 memcpy(doc->id1 + 8, &h2, 8);
226  
227 /* For initial creation, id2 == id1 */
228 599 memcpy(doc->id2, doc->id1, 16);
229  
230 599 doc->id_set = 1;
231 }
232  
233 /*----------------------------------------------------------------------------
234 * Encryption request and /Encrypt dict construction
235 *--------------------------------------------------------------------------*/
236  
237 5 pdfmake_err_t pdfmake_doc_set_encryption(pdfmake_doc_t *doc,
238 pdfmake_crypt_algo_t algorithm,
239 const char *user_passwd,
240 const char *owner_passwd,
241 int32_t permissions)
242 {
243 const char *u;
244 const char *o;
245  
246 5 ▶ if (!doc) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return PDFMAKE_EINVAL; 0 5 5
247 5 ▶ if (algorithm < PDFMAKE_CRYPT_RC4_40 || algorithm > PDFMAKE_CRYPT_AES_256) {
Branch: 2 branches
BranchTrueFalseTotal
if (algorithm < PDFMAKE_CRYPT_RC4_40 || algorithm > PDFMAKE_CRYPT_AES_256) { 5 0 5
if (algorithm < PDFMAKE_CRYPT_RC4_40 || algorithm > PDFMAKE_CRYPT_AES_256) { 0 5 5
248 0 return PDFMAKE_EINVAL;
249 }
250  
251 5 ▶ u = user_passwd ? user_passwd : "";
Branch: 1 branch
BranchTrueFalseTotal
u = user_passwd ? user_passwd : ""; 5 0 5
252 5 ▶ o = owner_passwd ? owner_passwd : u;
Branch: 1 branch
BranchTrueFalseTotal
o = owner_passwd ? owner_passwd : u; 5 0 5
253  
254 5 doc->enc_algo = algorithm;
255 5 doc->enc_user_pw = pdfmake_arena_strdup(doc->arena, u);
256 5 doc->enc_owner_pw = pdfmake_arena_strdup(doc->arena, o);
257 5 doc->enc_permissions = permissions;
258 5 doc->enc_requested = 1;
259 5 doc->finalized = 0;
260  
261 5 return PDFMAKE_OK;
262 }
263  
264 /* Build the /Encrypt dict and add it as an indirect object. Assumes the
265 * doc has a generated /ID and that doc->encryption has been set up. */
266 5 static pdfmake_err_t build_encrypt_dict(pdfmake_doc_t *doc) {
267 5 pdfmake_arena_t *a = doc->arena;
268 5 pdfmake_crypt_ctx_t *ctx = doc->encryption;
269 pdfmake_obj_t d;
270 uint32_t k;
271 int ou_len;
272 pdfmake_obj_t std_cf;
273 uint32_t cfm_k;
274 uint32_t len_k;
275 uint32_t auth_k;
276 uint32_t type_k;
277 pdfmake_obj_t cf;
278 uint32_t stdcf_k;
279 uint32_t num;
280 5 ▶ if (!ctx) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx) return PDFMAKE_EINVAL; 0 5 5
281  
282 5 d = pdfmake_dict_new(a);
283 5 ▶ if (d.kind != PDFMAKE_DICT) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (d.kind != PDFMAKE_DICT) return PDFMAKE_ENOMEM; 0 5 5
284  
285 5 k = pdfmake_arena_intern_name(a, "Filter", 6);
286 5 pdfmake_dict_set(a, &d, k, pdfmake_name_cstr(a, "Standard"));
287  
288 5 k = pdfmake_arena_intern_name(a, "V", 1);
289 5 pdfmake_dict_set(a, &d, k, pdfmake_int(ctx->V));
290  
291 5 k = pdfmake_arena_intern_name(a, "R", 1);
292 5 pdfmake_dict_set(a, &d, k, pdfmake_int(ctx->R));
293  
294 5 k = pdfmake_arena_intern_name(a, "Length", 6);
295 5 pdfmake_dict_set(a, &d, k, pdfmake_int(ctx->key_length * 8));
296  
297 5 k = pdfmake_arena_intern_name(a, "P", 1);
298 5 pdfmake_dict_set(a, &d, k, pdfmake_int((int64_t)(int32_t)ctx->P));
299  
300 /* O and U keys as hex strings — binary bytes survive unchanged.
301 * (R2-R4: 32 bytes, R6: 48 bytes including salts.) */
302 5 ▶ ou_len = (ctx->R == 6) ? 48 : 32;
Branch: 1 branch
BranchTrueFalseTotal
ou_len = (ctx->R == 6) ? 48 : 32; 3 2 5
303 5 k = pdfmake_arena_intern_name(a, "O", 1);
304 5 pdfmake_dict_set(a, &d, k, pdfmake_hexstr(a, ctx->O, ou_len));
305 5 k = pdfmake_arena_intern_name(a, "U", 1);
306 5 pdfmake_dict_set(a, &d, k, pdfmake_hexstr(a, ctx->U, ou_len));
307  
308 5 ▶ if (ctx->R == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->R == 6) { 3 2 5
309 /* R6 extras: OE, UE, Perms (each 32/32/16 bytes) */
310 3 k = pdfmake_arena_intern_name(a, "OE", 2);
311 3 pdfmake_dict_set(a, &d, k, pdfmake_hexstr(a, ctx->OE, 32));
312 3 k = pdfmake_arena_intern_name(a, "UE", 2);
313 3 pdfmake_dict_set(a, &d, k, pdfmake_hexstr(a, ctx->UE, 32));
314 3 k = pdfmake_arena_intern_name(a, "Perms", 5);
315 3 pdfmake_dict_set(a, &d, k, pdfmake_hexstr(a, ctx->Perms, 16));
316 }
317  
318 /* V=4/5 need /CF + /StmF + /StrF */
319 5 ▶ if (ctx->V >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->V >= 4) { 4 1 5
320 4 std_cf = pdfmake_dict_new(a);
321 4 cfm_k = pdfmake_arena_intern_name(a, "CFM", 3);
322 4 len_k = pdfmake_arena_intern_name(a, "Length", 6);
323 4 auth_k = pdfmake_arena_intern_name(a, "AuthEvent", 9);
324 4 type_k = pdfmake_arena_intern_name(a, "Type", 4);
325 4 pdfmake_dict_set(a, &std_cf, type_k, pdfmake_name_cstr(a, "CryptFilter"));
326 4 pdfmake_dict_set(a, &std_cf, cfm_k,
327 4 ▶ pdfmake_name_cstr(a, ctx->V == 5 ? "AESV3" : "AESV2"));
Branch: 1 branch
BranchTrueFalseTotal
pdfmake_name_cstr(a, ctx->V == 5 ? "AESV3" : "AESV2")); 3 1 4
328 4 pdfmake_dict_set(a, &std_cf, auth_k, pdfmake_name_cstr(a, "DocOpen"));
329 4 pdfmake_dict_set(a, &std_cf, len_k, pdfmake_int(ctx->key_length));
330  
331 4 cf = pdfmake_dict_new(a);
332 4 stdcf_k = pdfmake_arena_intern_name(a, "StdCF", 5);
333 4 pdfmake_dict_set(a, &cf, stdcf_k, std_cf);
334  
335 4 k = pdfmake_arena_intern_name(a, "CF", 2);
336 4 pdfmake_dict_set(a, &d, k, cf);
337 4 k = pdfmake_arena_intern_name(a, "StmF", 4);
338 4 pdfmake_dict_set(a, &d, k, pdfmake_name_cstr(a, "StdCF"));
339 4 k = pdfmake_arena_intern_name(a, "StrF", 4);
340 4 pdfmake_dict_set(a, &d, k, pdfmake_name_cstr(a, "StdCF"));
341 }
342  
343 5 num = pdfmake_doc_add(doc, d);
344 5 ▶ if (num == 0) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (num == 0) return PDFMAKE_ENOMEM; 0 5 5
345 5 doc->encrypt_num = num;
346 5 return PDFMAKE_OK;
347 }
348  
349 /* Prepare encryption for write: generate ID if needed, allocate+setup the
350 * crypt ctx, add the /Encrypt indirect dict. */
351 598 pdfmake_err_t pdfmake_doc_prepare_encryption(pdfmake_doc_t *doc) {
352 598 ▶ if (!doc || !doc->enc_requested || doc->encrypt_num != 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (!doc || !doc->enc_requested || doc->encrypt_num != 0) { 598 0 598
if (!doc || !doc->enc_requested || doc->encrypt_num != 0) { 5 593 598
if (!doc || !doc->enc_requested || doc->encrypt_num != 0) { 0 5 5
353 593 return PDFMAKE_OK;
354 }
355 5 ▶ if (!doc->id_set) pdfmake_doc_generate_id(doc);
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->id_set) pdfmake_doc_generate_id(doc); 5 0 5
356  
357 5 doc->encryption = pdfmake_arena_calloc(doc->arena,
358 sizeof(pdfmake_crypt_ctx_t));
359 5 ▶ if (!doc->encryption) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->encryption) return PDFMAKE_ENOMEM; 0 5 5
360  
361 5 ▶ if (pdfmake_crypt_setup(doc->encryption, doc->enc_algo,
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_crypt_setup(doc->encryption, doc->enc_algo, 0 5 5
362 5 doc->enc_user_pw, doc->enc_owner_pw,
363 doc->enc_permissions,
364 5 doc->id1, 16) != 0) {
365 0 return PDFMAKE_EINVAL;
366 }
367  
368 5 return build_encrypt_dict(doc);
369 }
370  
371 /*----------------------------------------------------------------------------
372 * File emission - Header
373 *--------------------------------------------------------------------------*/
374  
375 598 static pdfmake_err_t emit_header(pdfmake_buf_t *out) {
376 /*
377 * §7.5.2: %PDF-x.y followed by a binary comment.
378 * The binary comment ensures file transfer programs treat the file
379 * as binary. It must contain at least 4 bytes, each >= 128.
380 */
381 598 return pdfmake_buf_append_cstr(out,
382 "%PDF-" PDFMAKE_PDF_VERSION "\n"
383 "%\xE2\xE3\xCF\xD3\n");
384 }
385  
386 /*----------------------------------------------------------------------------
387 * File emission - Body
388 *--------------------------------------------------------------------------*/
389  
390 598 static pdfmake_err_t emit_body(pdfmake_doc_t *doc, pdfmake_buf_t *out) {
391 pdfmake_err_t err;
392 598 const pdfmake_crypt_ctx_t *crypt =
393 598 ▶ (doc->enc_requested && doc->encryption) ? doc->encryption : NULL;
Branch: 2 branches
BranchTrueFalseTotal
(doc->enc_requested && doc->encryption) ? doc->encryption : NULL; 5 593 598
(doc->enc_requested && doc->encryption) ? doc->encryption : NULL; 5 0 5
394 size_t i;
395 pdfmake_indirect_t *ind;
396  
397 4831 ▶ for (i = 0; i < doc->obj_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < doc->obj_count; i++) { 4233 598 4831
398 4233 ind = &doc->objects[i];
399 4233 ▶ if (!ind->in_use) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ind->in_use) continue; 0 4233 4233
400  
401 /* Record byte offset of this object */
402 4233 ind->byte_offset = out->len;
403  
404 /* Emit: N G obj\n */
405 4233 err = pdfmake_buf_appendf(out, "%u %u obj\n",
406 4233 (unsigned)ind->num, (unsigned)ind->gen);
407 4233 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 4233 4233
408  
409 /* Emit the object value. The /Encrypt dict itself must remain
410 * plaintext (its strings are special-cased by the spec). */
411 4233 ▶ if (crypt) {
Branch: 1 branch
BranchTrueFalseTotal
if (crypt) { 39 4194 4233
412 39 int skip = (ind->num == doc->encrypt_num);
413 39 err = pdfmake_write_obj_encrypted(out, doc->arena, crypt,
414 39 ind->num, skip, &ind->obj);
415 } else {
416 4194 err = pdfmake_write_obj(out, doc->arena, &ind->obj);
417 }
418 4233 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 4233 4233
419  
420 /* Emit: \nendobj\n */
421 4233 err = pdfmake_buf_append_cstr(out, "\nendobj\n");
422 4233 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 4233 4233
423 }
424  
425 598 return PDFMAKE_OK;
426 }
427  
428 /*----------------------------------------------------------------------------
429 * File emission - Cross-reference table
430 *--------------------------------------------------------------------------*/
431  
432 598 static pdfmake_err_t emit_xref(pdfmake_doc_t *doc, pdfmake_buf_t *out) {
433 pdfmake_err_t err;
434 size_t n_entries;
435 size_t i;
436 pdfmake_indirect_t *ind;
437 char entry[21];
438  
439 /* Record xref offset for startxref */
440 598 doc->xref_offset = out->len;
441  
442 /*
443 * Classic xref table format (§7.5.4):
444 * xref
445 * 0 N
446 * 0000000000 65535 f
447 * nnnnnnnnnn ggggg n
448 * ...
449 *
450 * Each entry is exactly 20 bytes: 10-digit offset, space, 5-digit gen,
451 * space, 'f' or 'n', space, newline (we use space + \n = 2 chars).
452 * Actually the spec says "space or EOL" after the letter, and requires
453 * exactly 20 bytes including the line terminator. We emit:
454 * "OOOOOOOOOO GGGGG X \n" (space before \n for Windows compat).
455 */
456  
457 /* Number of entries = obj_count + 1 (for entry 0) */
458 598 n_entries = doc->obj_count + 1;
459  
460 598 err = pdfmake_buf_appendf(out, "xref\n0 %zu\n", n_entries);
461 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
462  
463 /* Entry 0: head of free list, gen 65535 */
464 598 err = pdfmake_buf_append_cstr(out, "0000000000 65535 f \n");
465 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
466  
467 /* Entries for each object */
468 4831 ▶ for (i = 0; i < doc->obj_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < doc->obj_count; i++) { 4233 598 4831
469 4233 ind = &doc->objects[i];
470  
471 4233 ▶ if (ind->in_use) {
Branch: 1 branch
BranchTrueFalseTotal
if (ind->in_use) { 4233 0 4233
472 4233 snprintf(entry, sizeof(entry), "%010lu %05u n \n",
473 4233 (unsigned long)ind->byte_offset,
474 4233 (unsigned)ind->gen);
475 } else {
476 /* Free entry: offset points to next free (0 = end of list) */
477 0 snprintf(entry, sizeof(entry), "%010u %05u f \n",
478 0 0, (unsigned)(ind->gen + 1));
479 }
480  
481 4233 err = pdfmake_buf_append(out, entry, 20);
482 4233 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 4233 4233
483 }
484  
485 598 return PDFMAKE_OK;
486 }
487  
488 /*----------------------------------------------------------------------------
489 * File emission - Trailer
490 *--------------------------------------------------------------------------*/
491  
492 598 static pdfmake_err_t emit_trailer(pdfmake_doc_t *doc, pdfmake_buf_t *out) {
493 pdfmake_err_t err;
494 int i;
495  
496 /* Generate ID if not already done */
497 598 ▶ if (!doc->id_set) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->id_set) { 589 9 598
498 589 pdfmake_doc_generate_id(doc);
499 }
500  
501 598 err = pdfmake_buf_append_cstr(out, "trailer\n<<");
502 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
503  
504 /* /Size: total number of entries in xref (obj_count + 1) */
505 598 err = pdfmake_buf_appendf(out, "/Size %zu", doc->obj_count + 1);
506 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
507  
508 /* /Root: required reference to document catalog */
509 598 ▶ if (doc->root_num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (doc->root_num > 0) { 598 0 598
510 598 err = pdfmake_buf_appendf(out, "/Root %u %u R",
511 598 (unsigned)doc->root_num,
512 598 (unsigned)doc->root_gen);
513 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
514 }
515  
516 /* /Info: optional reference to info dictionary */
517 598 ▶ if (doc->info_num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (doc->info_num > 0) { 598 0 598
518 598 err = pdfmake_buf_appendf(out, "/Info %u %u R",
519 598 (unsigned)doc->info_num,
520 598 (unsigned)doc->info_gen);
521 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
522 }
523  
524 /* /Encrypt: reference to the Standard security handler dict */
525 598 ▶ if (doc->encrypt_num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (doc->encrypt_num > 0) { 5 593 598
526 5 err = pdfmake_buf_appendf(out, "/Encrypt %u 0 R",
527 5 (unsigned)doc->encrypt_num);
528 5 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 5 5
529 }
530  
531 /* /ID: array of two byte strings */
532 598 err = pdfmake_buf_append_cstr(out, "/ID[<");
533 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
534  
535 /* Emit id1 as hex */
536 10166 ▶ for (i = 0; i < 16; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) { 9568 598 10166
537 9568 err = pdfmake_buf_appendf(out, "%02X", doc->id1[i]);
538 9568 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 9568 9568
539 }
540  
541 598 err = pdfmake_buf_append_cstr(out, "><");
542 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
543  
544 /* Emit id2 as hex */
545 10166 ▶ for (i = 0; i < 16; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) { 9568 598 10166
546 9568 err = pdfmake_buf_appendf(out, "%02X", doc->id2[i]);
547 9568 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 9568 9568
548 }
549  
550 598 err = pdfmake_buf_append_cstr(out, ">]");
551 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
552  
553 /* Close trailer dict */
554 598 err = pdfmake_buf_append_cstr(out, ">>\n");
555 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
556  
557 /* startxref */
558 598 err = pdfmake_buf_appendf(out, "startxref\n%lu\n",
559 598 (unsigned long)doc->xref_offset);
560 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
561  
562 /* %%EOF */
563 598 err = pdfmake_buf_append_cstr(out, "%%EOF\n");
564 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
565  
566 598 return PDFMAKE_OK;
567 }
568  
569 /*----------------------------------------------------------------------------
570 * File emission - Main entry point
571 *--------------------------------------------------------------------------*/
572  
573 598 pdfmake_err_t pdfmake_doc_write(pdfmake_doc_t *doc, pdfmake_buf_t *out) {
574 pdfmake_err_t err;
575 598 ▶ if (!doc || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!doc || !out) return PDFMAKE_EINVAL; 598 0 598
if (!doc || !out) return PDFMAKE_EINVAL; 0 598 598
576  
577 /* 0a. Finalize document structure if not already done */
578 598 ▶ if (doc->page_count > 0 && !doc->finalized) {
Branch: 2 branches
BranchTrueFalseTotal
if (doc->page_count > 0 && !doc->finalized) { 587 11 598
if (doc->page_count > 0 && !doc->finalized) { 587 0 587
579 587 err = pdfmake_doc_finalize(doc);
580 587 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 587 587
581 }
582  
583 /* 0b. Auto-fill metadata (Producer, CreationDate, ModDate) */
584 598 pdfmake_meta_auto_fill(doc);
585  
586 /* 0c. Materialize encryption ctx + /Encrypt dict (no-op if no
587 * encryption was requested). Must happen after all other indirects
588 * are in place so obj numbers for real content don't collide with
589 * the encrypt dict's number. */
590 598 err = pdfmake_doc_prepare_encryption(doc);
591 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
592  
593 /* 1. Header */
594 598 err = emit_header(out);
595 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
596  
597 /* 2. Body (indirect objects) */
598 598 err = emit_body(doc, out);
599 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
600  
601 /* 3. Cross-reference table */
602 598 err = emit_xref(doc, out);
603 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
604  
605 /* 4. Trailer */
606 598 err = emit_trailer(doc, out);
607 598 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 598 598
608  
609 598 return PDFMAKE_OK;
610 }
« xs/markup_style.xs ↑ Summary xs/writer.xs »