A PDF of objects 1..n (objs[i] is object i+1) with a classic xref table; * `trailer_extra` is added to the trailer dict. Free with free(). */
| 63 | /* A PDF of objects 1..n (objs[i] is object i+1) with a classic xref table; |
| 64 | * `trailer_extra` is added to the trailer dict. Free with free(). */ |
| 65 | static unsigned char *tp_pdf(const tp_obj_t *objs, int n, const char *trailer_extra, |
| 66 | size_t *out_len) { |
| 67 | tp_buf_t b = {0}; |
| 68 | tp_puts(&b, "%PDF-1.7\n%\xe2\xe3\xcf\xd3\n"); |
| 69 | size_t *off = (size_t *)calloc((size_t)n + 1, sizeof(size_t)); |
| 70 | char line[256]; |
| 71 | for (int i = 0; i < n; i++) { |
| 72 | off[i + 1] = b.n; |
| 73 | snprintf(line, sizeof(line), "%d 0 obj\n", i + 1); |
| 74 | tp_puts(&b, line); |
| 75 | if (objs[i].data) { |
| 76 | snprintf(line, sizeof(line), "<< /Length %zu ", objs[i].len); |
| 77 | tp_puts(&b, line); |
| 78 | tp_puts(&b, objs[i].dict); |
| 79 | tp_puts(&b, " >>\nstream\n"); |
| 80 | tp_put(&b, objs[i].data, objs[i].len); |
| 81 | tp_puts(&b, "\nendstream"); |
| 82 | } else { |
| 83 | tp_puts(&b, objs[i].dict); |
| 84 | } |
| 85 | tp_puts(&b, "\nendobj\n"); |
| 86 | } |
| 87 | size_t xref = b.n; |
| 88 | snprintf(line, sizeof(line), "xref\n0 %d\n0000000000 65535 f \n", n + 1); |
| 89 | tp_puts(&b, line); |
| 90 | for (int i = 1; i <= n; i++) { |
| 91 | snprintf(line, sizeof(line), "%010zu 00000 n \n", off[i]); |
| 92 | tp_puts(&b, line); |
| 93 | } |
| 94 | snprintf(line, sizeof(line), |
| 95 | "trailer\n<< /Size %d /Root 1 0 R %s >>\nstartxref\n%zu\n%%%%EOF\n", n + 1, |
| 96 | trailer_extra ? trailer_extra : "", xref); |
| 97 | tp_puts(&b, line); |
| 98 | free(off); |
| 99 | *out_len = b.n; |
| 100 | return b.p; |
| 101 | } |
| 102 | |
| 103 | static unsigned char *tp_flate(const void *src, size_t n, size_t *out_len) { |
| 104 | uLongf cap = compressBound((uLong)n); |
no test coverage detected