Root/
| Source at commit cdb68f70a27bca539c67e04eabba307b0cf25330 created 7 years 4 months ago. By Werner Almesberger, eeshow/gfx/pdftoc.c (write_trailer): walking pointer is a bad idea if we realloc | |
|---|---|
| 1 | /* |
| 2 | * gfx/pdftoc.c - PDF writer with TOC generation |
| 3 | * |
| 4 | * Written 2016 by Werner Almesberger |
| 5 | * Copyright 2016 by Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Strongly influenced by https://neo900.org/git?p=misc;a=tree;f=schtoc |
| 15 | * |
| 16 | * PDF Reference: |
| 17 | * http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | #include <stdbool.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | |
| 27 | #include "misc/util.h" |
| 28 | #include "misc/diag.h" |
| 29 | #include "gfx/pdftoc.h" |
| 30 | |
| 31 | |
| 32 | struct title { |
| 33 | char *s; |
| 34 | struct title *next; |
| 35 | }; |
| 36 | |
| 37 | struct object { |
| 38 | int gen; |
| 39 | unsigned pos; |
| 40 | bool is_page; |
| 41 | }; |
| 42 | |
| 43 | struct pdftoc { |
| 44 | FILE *file; |
| 45 | |
| 46 | enum state { |
| 47 | idle, /* between objects */ |
| 48 | object, /* inside an object */ |
| 49 | catalog,/* inside the catalog object */ |
| 50 | xref, /* stopped at xref */ |
| 51 | trailer,/* going through the trailer */ |
| 52 | } state; |
| 53 | |
| 54 | struct title *titles; |
| 55 | struct title **next_title; |
| 56 | unsigned n_titles; |
| 57 | |
| 58 | char *buf; |
| 59 | unsigned left; /* bytes left in buffer */ |
| 60 | unsigned offset; /* offset into buffer */ |
| 61 | unsigned pos; /* position in file */ |
| 62 | |
| 63 | struct object *objs; /* object array */ |
| 64 | struct object *curr_obj; |
| 65 | int top; /* highest object number; -1 if no objects */ |
| 66 | |
| 67 | int root; /* catalog dict */ |
| 68 | int info; /* information dict, 0 if absent */ |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | struct pdftoc *pdftoc_begin(const char *file) |
| 73 | { |
| 74 | struct pdftoc *ctx; |
| 75 | |
| 76 | ctx = alloc_type(struct pdftoc); |
| 77 | if (file) { |
| 78 | ctx->file = fopen(file, "w"); |
| 79 | if (!ctx->file) |
| 80 | diag_pfatal(file); |
| 81 | } else { |
| 82 | ctx->file = stdout; |
| 83 | } |
| 84 | |
| 85 | ctx->state = idle; |
| 86 | |
| 87 | ctx->titles = NULL; |
| 88 | ctx->next_title = &ctx->titles; |
| 89 | ctx->n_titles = 0; |
| 90 | |
| 91 | ctx->buf = NULL; |
| 92 | ctx->left = 0; |
| 93 | ctx->offset = 0; |
| 94 | ctx->pos = 0; |
| 95 | |
| 96 | ctx->objs = NULL; |
| 97 | ctx->top = -1; |
| 98 | |
| 99 | ctx->root = 0; |
| 100 | ctx->info = 0; |
| 101 | |
| 102 | return ctx; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void add_object(struct pdftoc *ctx, int id, int gen, unsigned pos) |
| 107 | { |
| 108 | struct object *obj; |
| 109 | |
| 110 | if (id > ctx->top) { |
| 111 | ctx->objs = realloc_type_n(ctx->objs, struct object, id + 1); |
| 112 | memset(ctx->objs + ctx->top + 1 , 0, |
| 113 | (id - ctx->top) * sizeof(struct object)); |
| 114 | ctx->top = id; |
| 115 | } |
| 116 | |
| 117 | obj = ctx->objs + id; |
| 118 | ctx->curr_obj = obj; |
| 119 | obj->gen = gen; |
| 120 | obj->pos = pos; |
| 121 | obj->is_page = 0; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | static bool parse_object(struct pdftoc *ctx, const char *s) |
| 126 | { |
| 127 | int id, gen; |
| 128 | int n = 0; |
| 129 | |
| 130 | if (sscanf(s, "%d %d obj%n", &id, &gen, &n) != 2 || !n) |
| 131 | return 0; |
| 132 | add_object(ctx, id, gen, ctx->pos); |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | static void line(struct pdftoc *ctx, const char *s) |
| 138 | { |
| 139 | |
| 140 | switch (ctx->state) { |
| 141 | case idle: |
| 142 | if (parse_object(ctx, s)) { |
| 143 | ctx->state = object; |
| 144 | break; |
| 145 | } |
| 146 | if (strbegins(s, "xref")) { |
| 147 | ctx->state = xref; |
| 148 | break; |
| 149 | } |
| 150 | break; |
| 151 | case object: |
| 152 | if (strbegins(s, "endobj")) { |
| 153 | ctx->state = idle; |
| 154 | break; |
| 155 | } |
| 156 | if (strbegins(s, "<< /Type /Page")) { |
| 157 | ctx->curr_obj->is_page = 1; |
| 158 | break; |
| 159 | } |
| 160 | if (strbegins(s, "<< /Type /Catalog")) { |
| 161 | ctx->state = catalog; |
| 162 | break; |
| 163 | } |
| 164 | break; |
| 165 | case catalog: |
| 166 | if (strbegins(s, ">>")) { |
| 167 | ctx->state = object; |
| 168 | ctx->pos += fprintf(ctx->file, |
| 169 | " /Outlines %u 0 R\n", |
| 170 | ctx->top + 1); |
| 171 | break; |
| 172 | } |
| 173 | break; |
| 174 | case xref: |
| 175 | abort(); |
| 176 | case trailer: |
| 177 | if (sscanf(s, " /Root %d 0 R", &ctx->root) == 1) |
| 178 | break; |
| 179 | if (sscanf(s, " /Info %d 0 R", &ctx->info) == 1) |
| 180 | break; |
| 181 | break; |
| 182 | default: |
| 183 | abort(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | static void parse_buffer(struct pdftoc *ctx, bool do_write) |
| 189 | { |
| 190 | unsigned size, wrote; |
| 191 | char *nl; |
| 192 | |
| 193 | while (ctx->state != xref) { |
| 194 | nl = memchr(ctx->buf + ctx->offset, '\n', ctx->left); |
| 195 | if (!nl) |
| 196 | break; |
| 197 | *nl = 0; |
| 198 | size = nl - (ctx->buf + ctx->offset); |
| 199 | line(ctx, ctx->buf + ctx->offset); |
| 200 | *nl = '\n'; |
| 201 | if (ctx->state == xref) |
| 202 | break; |
| 203 | if (do_write) { |
| 204 | wrote = fwrite(ctx->buf + ctx->offset, 1, size + 1, |
| 205 | ctx->file); |
| 206 | if (wrote != size + 1) |
| 207 | diag_pfatal("fwrite"); |
| 208 | ctx->pos += size + 1; |
| 209 | } |
| 210 | ctx->offset += size + 1; |
| 211 | ctx->left -= size + 1; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | bool pdftoc_write(struct pdftoc *ctx, const void *data, unsigned length) |
| 217 | { |
| 218 | char *buf; |
| 219 | |
| 220 | buf = alloc_size(ctx->left + length + 1); |
| 221 | memcpy(buf, ctx->buf + ctx->offset, ctx->left); |
| 222 | memcpy(buf + ctx->left, data, length); |
| 223 | ctx->offset = 0; |
| 224 | ctx->left += length; |
| 225 | free(ctx->buf); |
| 226 | ctx->buf = buf; |
| 227 | |
| 228 | parse_buffer(ctx, 1); |
| 229 | |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | void pdftoc_title(struct pdftoc *ctx, const char *title) |
| 235 | { |
| 236 | struct title *t; |
| 237 | |
| 238 | t = alloc_type(struct title); |
| 239 | t->s = stralloc(title); |
| 240 | *ctx->next_title = t; |
| 241 | t->next = NULL; |
| 242 | ctx->next_title = &t->next; |
| 243 | ctx->n_titles++; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | static void write_trailer(struct pdftoc *ctx) |
| 248 | { |
| 249 | unsigned n = ctx->top + 1; |
| 250 | const struct object *obj = ctx->objs; |
| 251 | const struct title *t; |
| 252 | unsigned outline, tail; |
| 253 | int i; |
| 254 | |
| 255 | /* Outline root */ |
| 256 | |
| 257 | outline = n; |
| 258 | add_object(ctx, n, 0, ctx->pos); |
| 259 | tail = fprintf(ctx->file, |
| 260 | "%u 0 obj\n<<\n" |
| 261 | " /Count %u\n" |
| 262 | " /First %u 0 R\n" |
| 263 | " /Last %u 0 R\n" |
| 264 | ">>\nendobj\n", |
| 265 | n, ctx->n_titles, n + 1, n + ctx->n_titles); |
| 266 | |
| 267 | /* Outline items */ |
| 268 | |
| 269 | n++; |
| 270 | i = 0; |
| 271 | for (t = ctx->titles; t; t = t->next) { |
| 272 | assert(i <= ctx->top); |
| 273 | while (!ctx->objs[i].is_page) { |
| 274 | i++; |
| 275 | assert(i <= ctx->top); |
| 276 | } |
| 277 | add_object(ctx, n, 0, ctx->pos + tail); |
| 278 | tail += fprintf(ctx->file, |
| 279 | "%u 0 obj\n<<\n" |
| 280 | " /Title (%s)\n" |
| 281 | " /Parent %u 0 R\n", |
| 282 | n, t->s, outline); |
| 283 | if (t != ctx->titles) |
| 284 | tail += fprintf(ctx->file, |
| 285 | " /Prev %u 0 R\n", n - 1); |
| 286 | if (t->next) |
| 287 | tail += fprintf(ctx->file, |
| 288 | " /Next %u 0 R\n", n + 1); |
| 289 | tail += fprintf(ctx->file, |
| 290 | " /Dest [%d %u R /Fit]\n" |
| 291 | ">>\nendobj\n", |
| 292 | i, ctx->objs[i].gen); |
| 293 | n++; |
| 294 | i++; |
| 295 | } |
| 296 | |
| 297 | /* xref table */ |
| 298 | |
| 299 | fprintf(ctx->file, "xref\n0 %u\n", n); |
| 300 | for (obj = ctx->objs; obj != ctx->objs + ctx->top + 1; obj++) |
| 301 | fprintf(ctx->file, |
| 302 | "%010u %05u %c \n", |
| 303 | obj->pos, obj->pos ? 0 : 65535, obj->pos ? 'n' : 'f'); |
| 304 | |
| 305 | fprintf(ctx->file, |
| 306 | "trailer\n" |
| 307 | "<< /Size %u\n" |
| 308 | " /Root %u 0 R\n", |
| 309 | n, ctx->root); |
| 310 | if (ctx->info) |
| 311 | fprintf(ctx->file, " /Info %u 0 R\n", ctx->info); |
| 312 | fprintf(ctx->file, ">>\nstartxref\n%u\n%%%%EOF\n", ctx->pos + tail); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | void pdftoc_end(struct pdftoc *ctx) |
| 317 | { |
| 318 | struct title *next; |
| 319 | |
| 320 | assert(ctx->state == xref); |
| 321 | ctx->state = trailer; |
| 322 | parse_buffer(ctx, 0); |
| 323 | if (ctx->left) { |
| 324 | fatal("%u bytes left in buffer at end\n", ctx->left); |
| 325 | exit(1); |
| 326 | } |
| 327 | |
| 328 | write_trailer(ctx); |
| 329 | |
| 330 | if (fclose(ctx->file) < 0) |
| 331 | diag_pfatal("fclose"); |
| 332 | |
| 333 | while (ctx->titles) { |
| 334 | next = ctx->titles->next; |
| 335 | free(ctx->titles->s); |
| 336 | free(ctx->titles); |
| 337 | ctx->titles = next; |
| 338 | } |
| 339 | free(ctx->buf); |
| 340 | free(ctx); |
| 341 | } |
| 342 | |
Branches:
master
