| 1 |
|
| /* |
| 2 |
|
| * libpdfmake — object serializer implementation. |
| 3 |
|
| * |
| 4 |
|
| * Emits PDF objects per §7.3. Locale-independent number formatting. |
| 5 |
|
| */ |
| 6 |
|
| |
| 7 |
|
| #include "pdfmake_writer.h" |
| 8 |
|
| #include "pdfmake_filter.h" |
| 9 |
|
| #include "pdfmake_crypt.h" |
| 10 |
|
| #include <stdio.h> |
| 11 |
|
| #include <string.h> |
| 12 |
|
| #include <stdlib.h> |
| 13 |
|
| #include <math.h> |
| 14 |
|
| #include <float.h> |
| 15 |
|
| |
| 16 |
|
| /*---------------------------------------------------------------------------- |
| 17 |
|
| * Escape tables |
| 18 |
|
| *--------------------------------------------------------------------------*/ |
| 19 |
|
| |
| 20 |
|
| /* Name escape table: 1 = must escape as #XX. |
| 21 |
|
| * Per §7.3.5: escape NUL, whitespace, delimiters (()<>[]{}/%#), and |
| 22 |
|
| * any byte outside 0x21-0x7E (printable non-space ASCII). */ |
| 23 |
|
| static const uint8_t name_escape_table[256] = { |
| 24 |
|
| /* 0x00-0x0F: control chars - escape all */ |
| 25 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 26 |
|
| /* 0x10-0x1F: control chars - escape all */ |
| 27 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 28 |
|
| /* 0x20: space - escape */ |
| 29 |
|
| 1, |
| 30 |
|
| /* 0x21-0x22: !" - safe */ |
| 31 |
|
| 0,0, |
| 32 |
|
| /* 0x23: # - escape (escape char) */ |
| 33 |
|
| 1, |
| 34 |
|
| /* 0x24: $ - safe */ |
| 35 |
|
| 0, |
| 36 |
|
| /* 0x25: % - escape (comment) */ |
| 37 |
|
| 1, |
| 38 |
|
| /* 0x26-0x27: &' - safe */ |
| 39 |
|
| 0,0, |
| 40 |
|
| /* 0x28-0x29: () - escape (delimiters) */ |
| 41 |
|
| 1,1, |
| 42 |
|
| /* 0x2A-0x2E: *+,-. - safe */ |
| 43 |
|
| 0,0,0,0,0, |
| 44 |
|
| /* 0x2F: / - escape (name prefix) */ |
| 45 |
|
| 1, |
| 46 |
|
| /* 0x30-0x39: 0-9 - safe */ |
| 47 |
|
| 0,0,0,0,0,0,0,0,0,0, |
| 48 |
|
| /* 0x3A-0x3B: :; - safe */ |
| 49 |
|
| 0,0, |
| 50 |
|
| /* 0x3C-0x3E: <>= - escape < and > (delimiters), = is safe */ |
| 51 |
|
| 1,0,1, |
| 52 |
|
| /* 0x3F-0x5A: ?@A-Z - safe */ |
| 53 |
|
| 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 54 |
|
| /* 0x5B: [ - escape (delimiter) */ |
| 55 |
|
| 1, |
| 56 |
|
| /* 0x5C: \ - safe in names (only special in strings) */ |
| 57 |
|
| 0, |
| 58 |
|
| /* 0x5D: ] - escape (delimiter) */ |
| 59 |
|
| 1, |
| 60 |
|
| /* 0x5E-0x60: ^_` - safe */ |
| 61 |
|
| 0,0,0, |
| 62 |
|
| /* 0x61-0x7A: a-z - safe */ |
| 63 |
|
| 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 64 |
|
| /* 0x7B: { - escape (delimiter) */ |
| 65 |
|
| 1, |
| 66 |
|
| /* 0x7C: | - safe */ |
| 67 |
|
| 0, |
| 68 |
|
| /* 0x7D: } - escape (delimiter) */ |
| 69 |
|
| 1, |
| 70 |
|
| /* 0x7E: ~ - safe */ |
| 71 |
|
| 0, |
| 72 |
|
| /* 0x7F: DEL - escape */ |
| 73 |
|
| 1, |
| 74 |
|
| /* 0x80-0xFF: high bytes - escape all */ |
| 75 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 76 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 77 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 78 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 79 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 80 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 81 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 82 |
|
| 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 83 |
|
| }; |
| 84 |
|
| |
| 85 |
|
| /* Hex digits for encoding. */ |
| 86 |
|
| static const char hex_upper[16] = "0123456789ABCDEF"; |
| 87 |
|
| |
| 88 |
|
| /*---------------------------------------------------------------------------- |
| 89 |
|
| * Number formatting (locale-independent) |
| 90 |
|
| *--------------------------------------------------------------------------*/ |
| 91 |
|
| |
| 92 |
73247 |
| int pdfmake_format_int(char *buf, int64_t value) { |
| 93 |
|
| char tmp[24]; |
| 94 |
73247 |
| int pos = 0; |
| 95 |
73247 |
| int neg = 0; |
| 96 |
|
| uint64_t v; |
| 97 |
|
| int len; |
| 98 |
|
| |
| 99 |
73247 |
▶ | if (value < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value < 0) { |
86 |
73161 |
73247 |
|
| 100 |
86 |
| neg = 1; |
| 101 |
86 |
| v = (uint64_t)(-(value + 1)) + 1; /* Handle INT64_MIN safely */ |
| 102 |
|
| } else { |
| 103 |
73161 |
| v = (uint64_t)value; |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| /* Generate digits in reverse order. */ |
| 107 |
|
| do { |
| 108 |
108771 |
| tmp[pos++] = '0' + (v % 10); |
| 109 |
108771 |
| v /= 10; |
| 110 |
108771 |
▶ | } while (v > 0); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (v > 0); |
35524 |
73247 |
108771 |
|
| 111 |
|
| |
| 112 |
73247 |
| len = 0; |
| 113 |
73247 |
▶ | if (neg) buf[len++] = '-'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (neg) buf[len++] = '-'; |
86 |
73161 |
73247 |
|
| 114 |
|
| |
| 115 |
|
| /* Reverse the digits. */ |
| 116 |
182018 |
▶ | while (pos > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (pos > 0) { |
108771 |
73247 |
182018 |
|
| 117 |
108771 |
| buf[len++] = tmp[--pos]; |
| 118 |
|
| } |
| 119 |
73247 |
| buf[len] = '\0'; |
| 120 |
73247 |
| return len; |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
70975 |
| int pdfmake_format_real(char *buf, double value) { |
| 124 |
|
| int len; |
| 125 |
|
| |
| 126 |
|
| /* Handle special cases. */ |
| 127 |
70975 |
▶ | if (isnan(value)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (isnan(value)) { |
0 |
70975 |
70975 |
|
| 128 |
0 |
| memcpy(buf, "0", 2); |
| 129 |
0 |
| return 1; |
| 130 |
|
| } |
| 131 |
70975 |
▶ | if (isinf(value)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (isinf(value)) { |
0 |
70975 |
70975 |
|
| 132 |
0 |
▶ | if (value > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value > 0) { |
0 |
0 |
0 |
|
| 133 |
0 |
| memcpy(buf, "999999999", 10); |
| 134 |
0 |
| return 9; |
| 135 |
|
| } else { |
| 136 |
0 |
| memcpy(buf, "-999999999", 11); |
| 137 |
0 |
| return 10; |
| 138 |
|
| } |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
|
| /* If it's an integer, format as integer (no decimal point). */ |
| 142 |
70975 |
▶ | if (value == floor(value) && value >= -9007199254740992.0 && value <= 9007199254740992.0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (value == floor(value) && value >= -9007199254740992.0 && value <= 9007199254740992.0) { |
60568 |
10407 |
70975 |
| if (value == floor(value) && value >= -9007199254740992.0 && value <= 9007199254740992.0) { |
60568 |
0 |
60568 |
| if (value == floor(value) && value >= -9007199254740992.0 && value <= 9007199254740992.0) { |
60568 |
0 |
60568 |
|
| 143 |
60568 |
| return pdfmake_format_int(buf, (int64_t)value); |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| /* Format with 6 decimal places then strip trailing zeros. |
| 147 |
|
| * |
| 148 |
|
| * The previous hand-rolled digit-extraction loop multiplied the |
| 149 |
|
| * fractional part by 10 and took the floor repeatedly. For values |
| 150 |
|
| * like 0.3 (which is 0.2999999… in IEEE 754 double) the loop |
| 151 |
|
| * produced "299999999999999" — 15 nines — because the accumulated |
| 152 |
|
| * error never crossed the 1e-14 convergence threshold. On |
| 153 |
|
| * quadmath/longdouble builds the same problem surfaced, causing the |
| 154 |
|
| * CPAN-Testers failure: |
| 155 |
|
| * expected "0.1 0.2 0.3 0.4 K" |
| 156 |
|
| * got "0.1 0.2 0.299999999999999 0.4 K" |
| 157 |
|
| * |
| 158 |
|
| * sprintf("%.6f") lets the C runtime do the rounding correctly: |
| 159 |
|
| * 0.2999999… rounds to 0.300000, which strips to "0.3". Six |
| 160 |
|
| * decimal places give sub-micrometre precision for PDF coordinates |
| 161 |
|
| * and a millionth-unit resolution for colour components — both more |
| 162 |
|
| * than the PDF spec requires. */ |
| 163 |
10407 |
| len = sprintf(buf, "%.6f", value); |
| 164 |
|
| |
| 165 |
|
| /* Strip trailing zeros after the decimal point, and the point itself |
| 166 |
|
| * if no fractional digit remains. */ |
| 167 |
10407 |
▶ | if (memchr(buf, '.', (size_t)len)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memchr(buf, '.', (size_t)len)) { |
10407 |
0 |
10407 |
|
| 168 |
40629 |
▶ | while (len > 1 && buf[len - 1] == '0') len--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (len > 1 && buf[len - 1] == '0') len--; |
40629 |
0 |
40629 |
| while (len > 1 && buf[len - 1] == '0') len--; |
30222 |
10407 |
40629 |
|
| 169 |
10407 |
▶ | if (buf[len - 1] == '.') len--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf[len - 1] == '.') len--; |
2 |
10405 |
10407 |
|
| 170 |
|
| } |
| 171 |
10407 |
| buf[len] = '\0'; |
| 172 |
10407 |
| return len; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
|
| /*---------------------------------------------------------------------------- |
| 176 |
|
| * Per-kind writers |
| 177 |
|
| *--------------------------------------------------------------------------*/ |
| 178 |
|
| |
| 179 |
3 |
| pdfmake_err_t pdfmake_write_null(pdfmake_buf_t *buf) { |
| 180 |
3 |
| return pdfmake_buf_append(buf, "null", 4); |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
20 |
| pdfmake_err_t pdfmake_write_bool(pdfmake_buf_t *buf, int value) { |
| 184 |
20 |
▶ | if (value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) { |
9 |
11 |
20 |
|
| 185 |
9 |
| return pdfmake_buf_append(buf, "true", 4); |
| 186 |
|
| } else { |
| 187 |
11 |
| return pdfmake_buf_append(buf, "false", 5); |
| 188 |
|
| } |
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
5073 |
| pdfmake_err_t pdfmake_write_int(pdfmake_buf_t *buf, int64_t value) { |
| 192 |
|
| char tmp[24]; |
| 193 |
5073 |
| int len = pdfmake_format_int(tmp, value); |
| 194 |
5073 |
| return pdfmake_buf_append(buf, tmp, (size_t)len); |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
2092 |
| pdfmake_err_t pdfmake_write_real(pdfmake_buf_t *buf, double value) { |
| 198 |
|
| char tmp[32]; |
| 199 |
2092 |
| int len = pdfmake_format_real(tmp, value); |
| 200 |
2092 |
| return pdfmake_buf_append(buf, tmp, (size_t)len); |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
20059 |
| pdfmake_err_t pdfmake_write_name(pdfmake_buf_t *buf, const char *bytes, size_t len) { |
| 204 |
|
| pdfmake_err_t err; |
| 205 |
|
| size_t i; |
| 206 |
|
| uint8_t c; |
| 207 |
|
| |
| 208 |
|
| /* Names start with /. */ |
| 209 |
20059 |
| err = pdfmake_buf_append_byte(buf, '/'); |
| 210 |
20059 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
20059 |
20059 |
|
| 211 |
|
| |
| 212 |
|
| /* Emit each byte, escaping as needed. */ |
| 213 |
147826 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
127767 |
20059 |
147826 |
|
| 214 |
127767 |
| c = (uint8_t)bytes[i]; |
| 215 |
127767 |
▶ | if (name_escape_table[c]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (name_escape_table[c]) { |
4 |
127763 |
127767 |
|
| 216 |
|
| char esc[3]; |
| 217 |
4 |
| esc[0] = '#'; |
| 218 |
4 |
| esc[1] = hex_upper[c >> 4]; |
| 219 |
4 |
| esc[2] = hex_upper[c & 0x0F]; |
| 220 |
4 |
| err = pdfmake_buf_append(buf, esc, 3); |
| 221 |
|
| } else { |
| 222 |
127763 |
| err = pdfmake_buf_append_byte(buf, c); |
| 223 |
|
| } |
| 224 |
127767 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
127767 |
127767 |
|
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
20059 |
| return PDFMAKE_OK; |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
20071 |
| pdfmake_err_t pdfmake_write_name_id(pdfmake_buf_t *buf, pdfmake_arena_t *arena, |
| 231 |
|
| uint32_t name_id) { |
| 232 |
20071 |
| const char *bytes = pdfmake_arena_name_bytes(arena, name_id); |
| 233 |
20071 |
| size_t len = pdfmake_arena_name_len(arena, name_id); |
| 234 |
20071 |
▶ | if (!bytes) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) return PDFMAKE_EINVAL; |
12 |
20059 |
20071 |
|
| 235 |
20059 |
| return pdfmake_write_name(buf, bytes, len); |
| 236 |
|
| } |
| 237 |
|
| |
| 238 |
2042 |
| pdfmake_err_t pdfmake_write_string(pdfmake_buf_t *buf, const uint8_t *bytes, size_t len) { |
| 239 |
|
| pdfmake_err_t err; |
| 240 |
|
| size_t i; |
| 241 |
|
| uint8_t c; |
| 242 |
|
| |
| 243 |
2042 |
| err = pdfmake_buf_append_byte(buf, '('); |
| 244 |
2042 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
2042 |
2042 |
|
| 245 |
|
| |
| 246 |
32391 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
30349 |
2042 |
32391 |
|
| 247 |
30349 |
| c = bytes[i]; |
| 248 |
30349 |
| switch (c) { |
| 249 |
3 |
| case '\n': |
| 250 |
3 |
| err = pdfmake_buf_append(buf, "\\n", 2); |
| 251 |
3 |
| break; |
| 252 |
0 |
| case '\r': |
| 253 |
0 |
| err = pdfmake_buf_append(buf, "\\r", 2); |
| 254 |
0 |
| break; |
| 255 |
0 |
| case '\t': |
| 256 |
0 |
| err = pdfmake_buf_append(buf, "\\t", 2); |
| 257 |
0 |
| break; |
| 258 |
0 |
| case '\b': |
| 259 |
0 |
| err = pdfmake_buf_append(buf, "\\b", 2); |
| 260 |
0 |
| break; |
| 261 |
0 |
| case '\f': |
| 262 |
0 |
| err = pdfmake_buf_append(buf, "\\f", 2); |
| 263 |
0 |
| break; |
| 264 |
0 |
| case '(': |
| 265 |
0 |
| err = pdfmake_buf_append(buf, "\\(", 2); |
| 266 |
0 |
| break; |
| 267 |
0 |
| case ')': |
| 268 |
0 |
| err = pdfmake_buf_append(buf, "\\)", 2); |
| 269 |
0 |
| break; |
| 270 |
0 |
| case '\\': |
| 271 |
0 |
| err = pdfmake_buf_append(buf, "\\\\", 2); |
| 272 |
0 |
| break; |
| 273 |
30346 |
| default: |
| 274 |
|
| /* Emit as-is (including high bytes). PDF strings are 8-bit clean. */ |
| 275 |
30346 |
| err = pdfmake_buf_append_byte(buf, c); |
| 276 |
30346 |
| break; |
| 277 |
|
| } |
| 278 |
30349 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
30349 |
30349 |
|
| 279 |
|
| } |
| 280 |
|
| |
| 281 |
2042 |
| return pdfmake_buf_append_byte(buf, ')'); |
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
34 |
| pdfmake_err_t pdfmake_write_hexstring(pdfmake_buf_t *buf, const uint8_t *bytes, size_t len) { |
| 285 |
|
| pdfmake_err_t err; |
| 286 |
|
| size_t i; |
| 287 |
|
| uint8_t c; |
| 288 |
|
| char hex[2]; |
| 289 |
|
| |
| 290 |
34 |
| err = pdfmake_buf_append_byte(buf, '<'); |
| 291 |
34 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
34 |
34 |
|
| 292 |
|
| |
| 293 |
1590 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
1556 |
34 |
1590 |
|
| 294 |
1556 |
| c = bytes[i]; |
| 295 |
1556 |
| hex[0] = hex_upper[c >> 4]; |
| 296 |
1556 |
| hex[1] = hex_upper[c & 0x0F]; |
| 297 |
1556 |
| err = pdfmake_buf_append(buf, hex, 2); |
| 298 |
1556 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
1556 |
1556 |
|
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
34 |
| return pdfmake_buf_append_byte(buf, '>'); |
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
1729 |
| pdfmake_err_t pdfmake_write_array(pdfmake_buf_t *buf, pdfmake_arena_t *arena, |
| 305 |
|
| const pdfmake_array_t *arr) { |
| 306 |
|
| pdfmake_err_t err; |
| 307 |
|
| uint32_t i; |
| 308 |
|
| |
| 309 |
1729 |
▶ | if (!arr) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!arr) return PDFMAKE_EINVAL; |
0 |
1729 |
1729 |
|
| 310 |
|
| |
| 311 |
1729 |
| err = pdfmake_buf_append_byte(buf, '['); |
| 312 |
1729 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
1729 |
1729 |
|
| 313 |
|
| |
| 314 |
7974 |
▶ | for (i = 0; i < arr->len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < arr->len; i++) { |
6245 |
1729 |
7974 |
|
| 315 |
6245 |
▶ | if (i > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > 0) { |
4516 |
1729 |
6245 |
|
| 316 |
4516 |
| err = pdfmake_buf_append_byte(buf, ' '); |
| 317 |
4516 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
4516 |
4516 |
|
| 318 |
|
| } |
| 319 |
6245 |
| err = pdfmake_write_obj(buf, arena, &arr->items[i]); |
| 320 |
6245 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
6245 |
6245 |
|
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
1729 |
| return pdfmake_buf_append_byte(buf, ']'); |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
5763 |
| pdfmake_err_t pdfmake_write_dict(pdfmake_buf_t *buf, pdfmake_arena_t *arena, |
| 327 |
|
| const pdfmake_dict_t *dict) { |
| 328 |
|
| pdfmake_err_t err; |
| 329 |
|
| uint32_t order; |
| 330 |
|
| uint32_t i; |
| 331 |
|
| pdfmake_dict_entry_t *e; |
| 332 |
|
| |
| 333 |
5763 |
▶ | if (!dict) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dict) return PDFMAKE_EINVAL; |
0 |
5763 |
5763 |
|
| 334 |
|
| |
| 335 |
5763 |
| err = pdfmake_buf_append(buf, "<<", 2); |
| 336 |
5763 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
5763 |
5763 |
|
| 337 |
|
| |
| 338 |
|
| /* Iterate in insertion order. This is O(n²) but maintains PDF convention. */ |
| 339 |
20712 |
▶ | for (order = 0; order < dict->next_order; order++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (order = 0; order < dict->next_order; order++) { |
14961 |
5751 |
20712 |
|
| 340 |
|
| /* Find entry with this order. */ |
| 341 |
105948 |
▶ | for (i = 0; i < dict->cap; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < dict->cap; i++) { |
105846 |
102 |
105948 |
|
| 342 |
105846 |
| e = &dict->entries[i]; |
| 343 |
105846 |
▶ | if (e->key != 0 && !e->deleted && e->order == order) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (e->key != 0 && !e->deleted && e->order == order) { |
35456 |
70390 |
105846 |
| if (e->key != 0 && !e->deleted && e->order == order) { |
35456 |
0 |
35456 |
| if (e->key != 0 && !e->deleted && e->order == order) { |
14859 |
20597 |
35456 |
|
| 344 |
|
| /* Write key. */ |
| 345 |
14859 |
| err = pdfmake_write_name_id(buf, arena, e->key); |
| 346 |
14859 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
7 |
14852 |
14859 |
|
| 347 |
|
| |
| 348 |
|
| /* Space between key and value. */ |
| 349 |
14852 |
| err = pdfmake_buf_append_byte(buf, ' '); |
| 350 |
14852 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
14852 |
14852 |
|
| 351 |
|
| |
| 352 |
|
| /* Write value. */ |
| 353 |
14852 |
| err = pdfmake_write_obj(buf, arena, &e->value); |
| 354 |
14852 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
5 |
14847 |
14852 |
|
| 355 |
|
| |
| 356 |
14847 |
| break; |
| 357 |
|
| } |
| 358 |
|
| } |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
5751 |
| return pdfmake_buf_append(buf, ">>", 2); |
| 362 |
|
| } |
| 363 |
|
| |
| 364 |
3803 |
| pdfmake_err_t pdfmake_write_ref(pdfmake_buf_t *buf, uint32_t num, uint16_t gen) { |
| 365 |
|
| char tmp[32]; |
| 366 |
3803 |
| int len = pdfmake_format_int(tmp, num); |
| 367 |
3803 |
| tmp[len++] = ' '; |
| 368 |
3803 |
| len += pdfmake_format_int(tmp + len, gen); |
| 369 |
3803 |
| tmp[len++] = ' '; |
| 370 |
3803 |
| tmp[len++] = 'R'; |
| 371 |
3803 |
| return pdfmake_buf_append(buf, tmp, (size_t)len); |
| 372 |
|
| } |
| 373 |
|
| |
| 374 |
777 |
| pdfmake_err_t pdfmake_write_stream(pdfmake_buf_t *buf, pdfmake_arena_t *arena, |
| 375 |
|
| const pdfmake_stream_t *stream) { |
| 376 |
|
| pdfmake_err_t err; |
| 377 |
|
| uint32_t filter_key; |
| 378 |
|
| pdfmake_obj_t dict_obj; |
| 379 |
|
| pdfmake_obj_t *filter_val; |
| 380 |
|
| const uint8_t *output_data; |
| 381 |
|
| size_t output_len; |
| 382 |
777 |
| pdfmake_buf_t compressed = {0}; |
| 383 |
777 |
| int needs_free = 0; |
| 384 |
|
| const char *filter_name; |
| 385 |
|
| uint32_t length_key; |
| 386 |
|
| pdfmake_obj_t length_obj; |
| 387 |
|
| |
| 388 |
777 |
▶ | if (!stream || !stream->dict) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!stream || !stream->dict) return PDFMAKE_EINVAL; |
777 |
0 |
777 |
| if (!stream || !stream->dict) return PDFMAKE_EINVAL; |
0 |
777 |
777 |
|
| 389 |
|
| |
| 390 |
|
| /* Check if /Filter is set for compression */ |
| 391 |
777 |
| filter_key = pdfmake_arena_intern_name(arena, "Filter", 6); |
| 392 |
777 |
| dict_obj.kind = PDFMAKE_DICT; |
| 393 |
777 |
| dict_obj.as.dict = stream->dict; |
| 394 |
777 |
▶ | filter_val = filter_key ? pdfmake_dict_get(&dict_obj, filter_key) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| filter_val = filter_key ? pdfmake_dict_get(&dict_obj, filter_key) : NULL; |
777 |
0 |
777 |
|
| 395 |
|
| |
| 396 |
777 |
| output_data = stream->raw; |
| 397 |
777 |
| output_len = stream->raw_len; |
| 398 |
|
| |
| 399 |
|
| /* Apply compression if /Filter is set and stream is not already filtered */ |
| 400 |
777 |
▶ | if (filter_val && filter_val->kind == PDFMAKE_NAME && !stream->filtered) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (filter_val && filter_val->kind == PDFMAKE_NAME && !stream->filtered) { |
26 |
751 |
777 |
| if (filter_val && filter_val->kind == PDFMAKE_NAME && !stream->filtered) { |
26 |
0 |
26 |
| if (filter_val && filter_val->kind == PDFMAKE_NAME && !stream->filtered) { |
11 |
15 |
26 |
|
| 401 |
11 |
| filter_name = pdfmake_arena_name_bytes(arena, filter_val->as.name.id); |
| 402 |
|
| |
| 403 |
11 |
▶ | if (filter_name && strcmp(filter_name, "FlateDecode") == 0 && stream->raw && stream->raw_len > 0) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (filter_name && strcmp(filter_name, "FlateDecode") == 0 && stream->raw && stream->raw_len > 0) { |
11 |
0 |
11 |
| if (filter_name && strcmp(filter_name, "FlateDecode") == 0 && stream->raw && stream->raw_len > 0) { |
9 |
2 |
11 |
| if (filter_name && strcmp(filter_name, "FlateDecode") == 0 && stream->raw && stream->raw_len > 0) { |
9 |
0 |
9 |
| if (filter_name && strcmp(filter_name, "FlateDecode") == 0 && stream->raw && stream->raw_len > 0) { |
9 |
0 |
9 |
|
| 404 |
|
| /* Compress with FlateDecode */ |
| 405 |
9 |
▶ | if (pdfmake_buf_init(&compressed) == PDFMAKE_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pdfmake_buf_init(&compressed) == PDFMAKE_OK) { |
9 |
0 |
9 |
|
| 406 |
9 |
| err = pdfmake_flate_encode(stream->raw, stream->raw_len, NULL, &compressed); |
| 407 |
9 |
▶ | if (err == PDFMAKE_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err == PDFMAKE_OK) { |
9 |
0 |
9 |
|
| 408 |
9 |
| output_data = compressed.data; |
| 409 |
9 |
| output_len = compressed.len; |
| 410 |
9 |
| needs_free = 1; |
| 411 |
|
| } |
| 412 |
|
| } |
| 413 |
|
| } |
| 414 |
|
| } |
| 415 |
|
| |
| 416 |
|
| /* Set /Length in dictionary */ |
| 417 |
777 |
| length_key = pdfmake_arena_intern_name(arena, "Length", 6); |
| 418 |
777 |
▶ | if (length_key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (length_key) { |
777 |
0 |
777 |
|
| 419 |
777 |
| length_obj = pdfmake_int((int64_t)output_len); |
| 420 |
777 |
| pdfmake_dict_set(arena, &dict_obj, length_key, length_obj); |
| 421 |
|
| } |
| 422 |
|
| |
| 423 |
|
| /* Write the dictionary */ |
| 424 |
777 |
| err = pdfmake_write_dict(buf, arena, stream->dict); |
| 425 |
777 |
▶ | if (err != PDFMAKE_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) { |
3 |
774 |
777 |
|
| 426 |
3 |
▶ | if (needs_free) pdfmake_buf_free(&compressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_free) pdfmake_buf_free(&compressed); |
0 |
3 |
3 |
|
| 427 |
3 |
| return err; |
| 428 |
|
| } |
| 429 |
|
| |
| 430 |
|
| /* Stream framing */ |
| 431 |
774 |
| err = pdfmake_buf_append(buf, "\nstream\n", 8); |
| 432 |
774 |
▶ | if (err != PDFMAKE_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) { |
0 |
774 |
774 |
|
| 433 |
0 |
▶ | if (needs_free) pdfmake_buf_free(&compressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_free) pdfmake_buf_free(&compressed); |
0 |
0 |
0 |
|
| 434 |
0 |
| return err; |
| 435 |
|
| } |
| 436 |
|
| |
| 437 |
|
| /* Stream data */ |
| 438 |
774 |
▶ | if (output_data && output_len > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (output_data && output_len > 0) { |
753 |
21 |
774 |
| if (output_data && output_len > 0) { |
753 |
0 |
753 |
|
| 439 |
753 |
| err = pdfmake_buf_append(buf, output_data, output_len); |
| 440 |
753 |
▶ | if (err != PDFMAKE_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) { |
0 |
753 |
753 |
|
| 441 |
0 |
▶ | if (needs_free) pdfmake_buf_free(&compressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_free) pdfmake_buf_free(&compressed); |
0 |
0 |
0 |
|
| 442 |
0 |
| return err; |
| 443 |
|
| } |
| 444 |
|
| } |
| 445 |
|
| |
| 446 |
774 |
▶ | if (needs_free) pdfmake_buf_free(&compressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_free) pdfmake_buf_free(&compressed); |
9 |
765 |
774 |
|
| 447 |
|
| |
| 448 |
774 |
| return pdfmake_buf_append(buf, "\nendstream", 10); |
| 449 |
|
| } |
| 450 |
|
| |
| 451 |
|
| /*---------------------------------------------------------------------------- |
| 452 |
|
| * Object dispatcher |
| 453 |
|
| *--------------------------------------------------------------------------*/ |
| 454 |
|
| |
| 455 |
25423 |
| pdfmake_err_t pdfmake_write_obj(pdfmake_buf_t *buf, pdfmake_arena_t *arena, |
| 456 |
|
| const pdfmake_obj_t *obj) { |
| 457 |
25423 |
▶ | if (!buf || !obj) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!buf || !obj) return PDFMAKE_EINVAL; |
25423 |
0 |
25423 |
| if (!buf || !obj) return PDFMAKE_EINVAL; |
0 |
25423 |
25423 |
|
| 458 |
|
| |
| 459 |
25423 |
| switch (obj->kind) { |
| 460 |
3 |
| case PDFMAKE_NULL: |
| 461 |
3 |
| return pdfmake_write_null(buf); |
| 462 |
|
| |
| 463 |
20 |
| case PDFMAKE_BOOL: |
| 464 |
20 |
| return pdfmake_write_bool(buf, (int)obj->as.i); |
| 465 |
|
| |
| 466 |
5029 |
| case PDFMAKE_INT: |
| 467 |
5029 |
| return pdfmake_write_int(buf, obj->as.i); |
| 468 |
|
| |
| 469 |
2082 |
| case PDFMAKE_REAL: |
| 470 |
2082 |
| return pdfmake_write_real(buf, obj->as.r); |
| 471 |
|
| |
| 472 |
4962 |
| case PDFMAKE_NAME: |
| 473 |
4962 |
| return pdfmake_write_name_id(buf, arena, obj->as.name.id); |
| 474 |
|
| |
| 475 |
2057 |
| case PDFMAKE_STR: |
| 476 |
2057 |
▶ | if (obj->as.str.hex) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (obj->as.str.hex) { |
15 |
2042 |
2057 |
|
| 477 |
15 |
| return pdfmake_write_hexstring(buf, obj->as.str.bytes, obj->as.str.len); |
| 478 |
|
| } else { |
| 479 |
2042 |
| return pdfmake_write_string(buf, obj->as.str.bytes, obj->as.str.len); |
| 480 |
|
| } |
| 481 |
|
| |
| 482 |
1729 |
| case PDFMAKE_ARRAY: |
| 483 |
1729 |
| return pdfmake_write_array(buf, arena, obj->as.arr); |
| 484 |
|
| |
| 485 |
4986 |
| case PDFMAKE_DICT: |
| 486 |
4986 |
| return pdfmake_write_dict(buf, arena, obj->as.dict); |
| 487 |
|
| |
| 488 |
777 |
| case PDFMAKE_STREAM: |
| 489 |
777 |
| return pdfmake_write_stream(buf, arena, obj->as.stream); |
| 490 |
|
| |
| 491 |
3778 |
| case PDFMAKE_REF: |
| 492 |
3778 |
| return pdfmake_write_ref(buf, obj->as.ref.num, obj->as.ref.gen); |
| 493 |
|
| |
| 494 |
0 |
| default: |
| 495 |
0 |
| return PDFMAKE_EINVAL; |
| 496 |
|
| } |
| 497 |
|
| } |
| 498 |
|
| |
| 499 |
|
| /*---------------------------------------------------------------------------- |
| 500 |
|
| * Encrypted emission path — mirrors the non-encrypted helpers above but |
| 501 |
|
| * threads a crypt ctx + obj_num through recursion so every string/stream |
| 502 |
|
| * encountered inside an indirect object can be encrypted with the correct |
| 503 |
|
| * per-object key. The `skip_encrypt` flag is raised for objects that the |
| 504 |
|
| * PDF spec says must remain plaintext (the /Encrypt dict itself and its |
| 505 |
|
| * string children). |
| 506 |
|
| *--------------------------------------------------------------------------*/ |
| 507 |
|
| |
| 508 |
|
| static pdfmake_err_t write_obj_enc(pdfmake_buf_t *buf, pdfmake_arena_t *arena, |
| 509 |
|
| const pdfmake_crypt_ctx_t *crypt, |
| 510 |
|
| uint32_t obj_num, int skip_encrypt, |
| 511 |
|
| const pdfmake_obj_t *obj); |
| 512 |
|
| |
| 513 |
15 |
| static pdfmake_err_t write_literal_string_bytes(pdfmake_buf_t *buf, |
| 514 |
|
| const uint8_t *bytes, |
| 515 |
|
| size_t len) { |
| 516 |
15 |
| pdfmake_err_t err = pdfmake_buf_append_byte(buf, '('); |
| 517 |
|
| size_t i; |
| 518 |
|
| uint8_t c; |
| 519 |
15 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
15 |
15 |
|
| 520 |
574 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
559 |
15 |
574 |
|
| 521 |
559 |
| c = bytes[i]; |
| 522 |
559 |
| switch (c) { |
| 523 |
1 |
| case '\n': err = pdfmake_buf_append(buf, "\\n", 2); break; |
| 524 |
1 |
| case '\r': err = pdfmake_buf_append(buf, "\\r", 2); break; |
| 525 |
2 |
| case '\t': err = pdfmake_buf_append(buf, "\\t", 2); break; |
| 526 |
0 |
| case '\b': err = pdfmake_buf_append(buf, "\\b", 2); break; |
| 527 |
1 |
| case '\f': err = pdfmake_buf_append(buf, "\\f", 2); break; |
| 528 |
2 |
| case '(': err = pdfmake_buf_append(buf, "\\(", 2); break; |
| 529 |
1 |
| case ')': err = pdfmake_buf_append(buf, "\\)", 2); break; |
| 530 |
4 |
| case '\\': err = pdfmake_buf_append(buf, "\\\\", 2); break; |
| 531 |
547 |
| default: err = pdfmake_buf_append_byte(buf, c); break; |
| 532 |
|
| } |
| 533 |
559 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
559 |
559 |
|
| 534 |
|
| } |
| 535 |
15 |
| return pdfmake_buf_append_byte(buf, ')'); |
| 536 |
|
| } |
| 537 |
|
| |
| 538 |
34 |
| static pdfmake_err_t write_string_enc(pdfmake_buf_t *buf, |
| 539 |
|
| const pdfmake_crypt_ctx_t *crypt, |
| 540 |
|
| uint32_t obj_num, int skip_encrypt, |
| 541 |
|
| const pdfmake_string_t *s) { |
| 542 |
34 |
| const uint8_t *bytes = s->bytes; |
| 543 |
34 |
| size_t len = s->len; |
| 544 |
34 |
| uint8_t *enc = NULL; |
| 545 |
34 |
| size_t enc_len = 0; |
| 546 |
|
| size_t cap; |
| 547 |
|
| int n; |
| 548 |
|
| pdfmake_err_t err; |
| 549 |
|
| |
| 550 |
34 |
▶ | if (crypt && !skip_encrypt) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (crypt && !skip_encrypt) { |
34 |
0 |
34 |
| if (crypt && !skip_encrypt) { |
15 |
19 |
34 |
|
| 551 |
15 |
▶ | cap = (crypt->R >= 4) ? (len + 32 + 16) : (len + 1); |
Branch: 1 branch
| Branch | True | False | Total |
| cap = (crypt->R >= 4) ? (len + 32 + 16) : (len + 1); |
12 |
3 |
15 |
|
| 552 |
15 |
| enc = (uint8_t *)malloc(cap); |
| 553 |
15 |
▶ | if (!enc) return PDFMAKE_ENOMEM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!enc) return PDFMAKE_ENOMEM; |
0 |
15 |
15 |
|
| 554 |
15 |
| n = pdfmake_crypt_encrypt_string(crypt, (int)obj_num, 0, |
| 555 |
|
| bytes, len, enc); |
| 556 |
15 |
▶ | if (n < 0) { free(enc); return PDFMAKE_EINVAL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) { free(enc); return PDFMAKE_EINVAL; } |
0 |
15 |
15 |
|
| 557 |
15 |
| bytes = enc; |
| 558 |
15 |
| enc_len = (size_t)n; |
| 559 |
15 |
| len = enc_len; |
| 560 |
|
| } |
| 561 |
|
| |
| 562 |
34 |
▶ | if (s->hex) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->hex) { |
19 |
15 |
34 |
|
| 563 |
19 |
| err = pdfmake_write_hexstring(buf, bytes, len); |
| 564 |
|
| } else { |
| 565 |
15 |
| err = write_literal_string_bytes(buf, bytes, len); |
| 566 |
|
| } |
| 567 |
34 |
| free(enc); |
| 568 |
34 |
| return err; |
| 569 |
|
| } |
| 570 |
|
| |
| 571 |
10 |
| static pdfmake_err_t write_array_enc(pdfmake_buf_t *buf, pdfmake_arena_t *arena, |
| 572 |
|
| const pdfmake_crypt_ctx_t *crypt, |
| 573 |
|
| uint32_t obj_num, int skip_encrypt, |
| 574 |
|
| const pdfmake_array_t *arr) { |
| 575 |
|
| pdfmake_err_t err; |
| 576 |
|
| uint32_t i; |
| 577 |
10 |
▶ | if (!arr) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!arr) return PDFMAKE_EINVAL; |
0 |
10 |
10 |
|
| 578 |
10 |
| err = pdfmake_buf_append_byte(buf, '['); |
| 579 |
10 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
10 |
10 |
|
| 580 |
35 |
▶ | for (i = 0; i < arr->len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < arr->len; i++) { |
25 |
10 |
35 |
|
| 581 |
25 |
▶ | if (i > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > 0) { |
15 |
10 |
25 |
|
| 582 |
15 |
| err = pdfmake_buf_append_byte(buf, ' '); |
| 583 |
15 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
15 |
15 |
|
| 584 |
|
| } |
| 585 |
25 |
| err = write_obj_enc(buf, arena, crypt, obj_num, skip_encrypt, |
| 586 |
25 |
| &arr->items[i]); |
| 587 |
25 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
25 |
25 |
|
| 588 |
|
| } |
| 589 |
10 |
| return pdfmake_buf_append_byte(buf, ']'); |
| 590 |
|
| } |
| 591 |
|
| |
| 592 |
57 |
| static pdfmake_err_t write_dict_enc(pdfmake_buf_t *buf, pdfmake_arena_t *arena, |
| 593 |
|
| const pdfmake_crypt_ctx_t *crypt, |
| 594 |
|
| uint32_t obj_num, int skip_encrypt, |
| 595 |
|
| const pdfmake_dict_t *dict) { |
| 596 |
|
| pdfmake_err_t err; |
| 597 |
|
| uint32_t order; |
| 598 |
|
| uint32_t i; |
| 599 |
|
| pdfmake_dict_entry_t *e; |
| 600 |
57 |
▶ | if (!dict) return PDFMAKE_EINVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dict) return PDFMAKE_EINVAL; |
0 |
57 |
57 |
|
| 601 |
57 |
| err = pdfmake_buf_append(buf, "<<", 2); |
| 602 |
57 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
57 |
57 |
|
| 603 |
|
| |
| 604 |
240 |
▶ | for (order = 0; order < dict->next_order; order++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (order = 0; order < dict->next_order; order++) { |
183 |
57 |
240 |
|
| 605 |
1822 |
▶ | for (i = 0; i < dict->cap; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < dict->cap; i++) { |
1822 |
0 |
1822 |
|
| 606 |
1822 |
| e = &dict->entries[i]; |
| 607 |
1822 |
▶ | if (e->key != 0 && !e->deleted && e->order == order) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (e->key != 0 && !e->deleted && e->order == order) { |
619 |
1203 |
1822 |
| if (e->key != 0 && !e->deleted && e->order == order) { |
619 |
0 |
619 |
| if (e->key != 0 && !e->deleted && e->order == order) { |
183 |
436 |
619 |
|
| 608 |
183 |
| err = pdfmake_write_name_id(buf, arena, e->key); |
| 609 |
183 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
183 |
183 |
|
| 610 |
183 |
| err = pdfmake_buf_append_byte(buf, ' '); |
| 611 |
183 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
183 |
183 |
|
| 612 |
183 |
| err = write_obj_enc(buf, arena, crypt, obj_num, skip_encrypt, |
| 613 |
183 |
| &e->value); |
| 614 |
183 |
▶ | if (err != PDFMAKE_OK) return err; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) return err; |
0 |
183 |
183 |
|
| 615 |
183 |
| break; |
| 616 |
|
| } |
| 617 |
|
| } |
| 618 |
|
| } |
| 619 |
57 |
| return pdfmake_buf_append(buf, ">>", 2); |
| 620 |
|
| } |
| 621 |
|
| |
| 622 |
5 |
| static pdfmake_err_t write_stream_enc(pdfmake_buf_t *buf, |
| 623 |
|
| pdfmake_arena_t *arena, |
| 624 |
|
| const pdfmake_crypt_ctx_t *crypt, |
| 625 |
|
| uint32_t obj_num, int skip_encrypt, |
| 626 |
|
| const pdfmake_stream_t *stream) { |
| 627 |
|
| pdfmake_err_t err; |
| 628 |
|
| uint32_t filter_key; |
| 629 |
|
| pdfmake_obj_t dict_obj; |
| 630 |
|
| pdfmake_obj_t *filter_val; |
| 631 |
|
| const uint8_t *output_data; |
| 632 |
|
| size_t output_len; |
| 633 |
|
| pdfmake_buf_t compressed; |
| 634 |
|
| int needs_free; |
| 635 |
|
| uint8_t *enc; |
| 636 |
|
| size_t enc_len; |
| 637 |
|
| uint32_t length_key; |
| 638 |
|
| pdfmake_obj_t len_obj; |
| 639 |
|
| |
| 640 |
5 |
▶ | if (!stream || !stream->dict) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!stream || !stream->dict) return PDFMAKE_EINVAL; |
5 |
0 |
5 |
| if (!stream || !stream->dict) return PDFMAKE_EINVAL; |
0 |
5 |
5 |
|
| 641 |
|
| |
| 642 |
|
| /* Stage 1: apply /Filter compression if requested (mirrors |
| 643 |
|
| * pdfmake_write_stream's logic). */ |
| 644 |
5 |
| filter_key = pdfmake_arena_intern_name(arena, "Filter", 6); |
| 645 |
5 |
| dict_obj.kind = PDFMAKE_DICT; |
| 646 |
5 |
| dict_obj.as.dict = stream->dict; |
| 647 |
5 |
| filter_val = filter_key |
| 648 |
5 |
▶ | ? pdfmake_dict_get(&dict_obj, filter_key) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? pdfmake_dict_get(&dict_obj, filter_key) : NULL; |
5 |
0 |
5 |
|
| 649 |
|
| |
| 650 |
5 |
| output_data = stream->raw; |
| 651 |
5 |
| output_len = stream->raw_len; |
| 652 |
5 |
| memset(&compressed, 0, sizeof(compressed)); |
| 653 |
5 |
| needs_free = 0; |
| 654 |
|
| |
| 655 |
5 |
▶ | if (filter_val && filter_val->kind == PDFMAKE_NAME && !stream->filtered) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (filter_val && filter_val->kind == PDFMAKE_NAME && !stream->filtered) { |
0 |
5 |
5 |
| if (filter_val && filter_val->kind == PDFMAKE_NAME && !stream->filtered) { |
0 |
0 |
0 |
| if (filter_val && filter_val->kind == PDFMAKE_NAME && !stream->filtered) { |
0 |
0 |
0 |
|
| 656 |
|
| const char *filter_name = |
| 657 |
0 |
| pdfmake_arena_name_bytes(arena, filter_val->as.name.id); |
| 658 |
0 |
▶ | if (filter_name && strcmp(filter_name, "FlateDecode") == 0 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (filter_name && strcmp(filter_name, "FlateDecode") == 0 && |
0 |
0 |
0 |
| if (filter_name && strcmp(filter_name, "FlateDecode") == 0 && |
0 |
0 |
0 |
|
| 659 |
0 |
▶ | stream->raw && stream->raw_len > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| stream->raw && stream->raw_len > 0) { |
0 |
0 |
0 |
| stream->raw && stream->raw_len > 0) { |
0 |
0 |
0 |
|
| 660 |
0 |
▶ | if (pdfmake_buf_init(&compressed) == PDFMAKE_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pdfmake_buf_init(&compressed) == PDFMAKE_OK) { |
0 |
0 |
0 |
|
| 661 |
0 |
| err = pdfmake_flate_encode(stream->raw, stream->raw_len, |
| 662 |
|
| NULL, &compressed); |
| 663 |
0 |
▶ | if (err == PDFMAKE_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err == PDFMAKE_OK) { |
0 |
0 |
0 |
|
| 664 |
0 |
| output_data = compressed.data; |
| 665 |
0 |
| output_len = compressed.len; |
| 666 |
0 |
| needs_free = 1; |
| 667 |
|
| } |
| 668 |
|
| } |
| 669 |
|
| } |
| 670 |
|
| } |
| 671 |
|
| |
| 672 |
|
| /* Stage 2: apply stream encryption if a crypt ctx is active. */ |
| 673 |
5 |
| enc = NULL; |
| 674 |
5 |
| enc_len = 0; |
| 675 |
5 |
▶ | if (crypt && !skip_encrypt && output_data && output_len > 0) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (crypt && !skip_encrypt && output_data && output_len > 0) { |
5 |
0 |
5 |
| if (crypt && !skip_encrypt && output_data && output_len > 0) { |
5 |
0 |
5 |
| if (crypt && !skip_encrypt && output_data && output_len > 0) { |
5 |
0 |
5 |
| if (crypt && !skip_encrypt && output_data && output_len > 0) { |
5 |
0 |
5 |
|
| 676 |
5 |
▶ | if (pdfmake_crypt_encrypt_stream(crypt, (int)obj_num, 0, |
Branch: 1 branch
| Branch | True | False | Total |
| if (pdfmake_crypt_encrypt_stream(crypt, (int)obj_num, 0, |
0 |
5 |
5 |
|
| 677 |
|
| output_data, output_len, |
| 678 |
|
| &enc, &enc_len) != 0) { |
| 679 |
0 |
▶ | if (needs_free) pdfmake_buf_free(&compressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_free) pdfmake_buf_free(&compressed); |
0 |
0 |
0 |
|
| 680 |
0 |
| return PDFMAKE_EINVAL; |
| 681 |
|
| } |
| 682 |
5 |
| output_data = enc; |
| 683 |
5 |
| output_len = enc_len; |
| 684 |
|
| } |
| 685 |
|
| |
| 686 |
|
| /* /Length must reflect the final on-wire size */ |
| 687 |
5 |
| length_key = pdfmake_arena_intern_name(arena, "Length", 6); |
| 688 |
5 |
▶ | if (length_key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (length_key) { |
5 |
0 |
5 |
|
| 689 |
5 |
| len_obj = pdfmake_int((int64_t)output_len); |
| 690 |
5 |
| pdfmake_dict_set(arena, &dict_obj, length_key, len_obj); |
| 691 |
|
| } |
| 692 |
|
| |
| 693 |
|
| /* Emit dict with strings encrypted too (if applicable) */ |
| 694 |
5 |
| err = write_dict_enc(buf, arena, crypt, obj_num, skip_encrypt, stream->dict); |
| 695 |
5 |
▶ | if (err != PDFMAKE_OK) goto cleanup; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) goto cleanup; |
0 |
5 |
5 |
|
| 696 |
|
| |
| 697 |
5 |
| err = pdfmake_buf_append(buf, "\nstream\n", 8); |
| 698 |
5 |
▶ | if (err != PDFMAKE_OK) goto cleanup; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) goto cleanup; |
0 |
5 |
5 |
|
| 699 |
|
| |
| 700 |
5 |
▶ | if (output_data && output_len > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (output_data && output_len > 0) { |
5 |
0 |
5 |
| if (output_data && output_len > 0) { |
5 |
0 |
5 |
|
| 701 |
5 |
| err = pdfmake_buf_append(buf, output_data, output_len); |
| 702 |
5 |
▶ | if (err != PDFMAKE_OK) goto cleanup; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != PDFMAKE_OK) goto cleanup; |
0 |
5 |
5 |
|
| 703 |
|
| } |
| 704 |
|
| |
| 705 |
5 |
| err = pdfmake_buf_append(buf, "\nendstream", 10); |
| 706 |
|
| |
| 707 |
5 |
| cleanup: |
| 708 |
5 |
▶ | if (needs_free) pdfmake_buf_free(&compressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_free) pdfmake_buf_free(&compressed); |
0 |
5 |
5 |
|
| 709 |
5 |
| free(enc); |
| 710 |
5 |
| return err; |
| 711 |
|
| } |
| 712 |
|
| |
| 713 |
247 |
| static pdfmake_err_t write_obj_enc(pdfmake_buf_t *buf, pdfmake_arena_t *arena, |
| 714 |
|
| const pdfmake_crypt_ctx_t *crypt, |
| 715 |
|
| uint32_t obj_num, int skip_encrypt, |
| 716 |
|
| const pdfmake_obj_t *obj) { |
| 717 |
247 |
▶ | if (!buf || !obj) return PDFMAKE_EINVAL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!buf || !obj) return PDFMAKE_EINVAL; |
247 |
0 |
247 |
| if (!buf || !obj) return PDFMAKE_EINVAL; |
0 |
247 |
247 |
|
| 718 |
247 |
| switch (obj->kind) { |
| 719 |
0 |
| case PDFMAKE_NULL: return pdfmake_write_null(buf); |
| 720 |
0 |
| case PDFMAKE_BOOL: return pdfmake_write_bool(buf, (int)obj->as.i); |
| 721 |
44 |
| case PDFMAKE_INT: return pdfmake_write_int(buf, obj->as.i); |
| 722 |
10 |
| case PDFMAKE_REAL: return pdfmake_write_real(buf, obj->as.r); |
| 723 |
67 |
| case PDFMAKE_NAME: return pdfmake_write_name_id(buf, arena, |
| 724 |
67 |
| obj->as.name.id); |
| 725 |
34 |
| case PDFMAKE_STR: return write_string_enc(buf, crypt, obj_num, |
| 726 |
|
| skip_encrypt, &obj->as.str); |
| 727 |
10 |
| case PDFMAKE_ARRAY: return write_array_enc(buf, arena, crypt, obj_num, |
| 728 |
10 |
| skip_encrypt, obj->as.arr); |
| 729 |
52 |
| case PDFMAKE_DICT: return write_dict_enc(buf, arena, crypt, obj_num, |
| 730 |
52 |
| skip_encrypt, obj->as.dict); |
| 731 |
5 |
| case PDFMAKE_STREAM: return write_stream_enc(buf, arena, crypt, obj_num, |
| 732 |
5 |
| skip_encrypt, obj->as.stream); |
| 733 |
25 |
| case PDFMAKE_REF: return pdfmake_write_ref(buf, obj->as.ref.num, |
| 734 |
25 |
| obj->as.ref.gen); |
| 735 |
|
| } |
| 736 |
0 |
| return PDFMAKE_EINVAL; |
| 737 |
|
| } |
| 738 |
|
| |
| 739 |
39 |
| pdfmake_err_t pdfmake_write_obj_encrypted(pdfmake_buf_t *buf, |
| 740 |
|
| pdfmake_arena_t *arena, |
| 741 |
|
| const pdfmake_crypt_ctx_t *crypt, |
| 742 |
|
| uint32_t obj_num, |
| 743 |
|
| int skip_encrypt, |
| 744 |
|
| const pdfmake_obj_t *obj) { |
| 745 |
39 |
| return write_obj_enc(buf, arena, crypt, obj_num, skip_encrypt, obj); |
| 746 |
|
| } |