The object at off (or off + hdr), a stream with its raw bytes when one * follows. NULL: no matching header there. */
| 1188 | /* The object at off (or off + hdr), a stream with its raw bytes when one |
| 1189 | * follows. NULL: no matching header there. */ |
| 1190 | static pdf_val_t *parse_indirect_at(pdf_doc_t *d, int64_t off, bool has_expect, int64_t expect, |
| 1191 | bool *ok) { |
| 1192 | size_t st; |
| 1193 | pdf_val_t *val = indirect_value_at(d, off, has_expect, expect, ok, &st); |
| 1194 | if (!val || !st) { |
| 1195 | return val; |
| 1196 | } |
| 1197 | const unsigned char *s = d->data; |
| 1198 | size_t len = d->len; |
| 1199 | if (st + 1 < len && s[st] == '\r' && s[st + 1] == '\n') { |
| 1200 | st += 2; |
| 1201 | } else if (st < len && (s[st] == '\n' || s[st] == '\r')) { |
| 1202 | st += 1; |
| 1203 | } |
| 1204 | pdf_val_t *ln = length_value(d, pdf_dget_raw(val, "Length")); |
| 1205 | const unsigned char *raw = NULL; |
| 1206 | size_t raw_len = 0; |
| 1207 | if (ln && ln->kind == PV_NUM && ln->is_int && ln->inum >= 0 && |
| 1208 | (uint64_t)ln->inum <= (uint64_t)(len - st)) { |
| 1209 | if (endstream_follows(d, st + (size_t)ln->inum)) { |
| 1210 | raw = s + st; |
| 1211 | raw_len = (size_t)ln->inum; |
| 1212 | } |
| 1213 | } |
| 1214 | if (!raw) { |
| 1215 | d->counts->stream_length_recovered++; |
| 1216 | const unsigned char *e = cbm_memmem(s + st, len - st, "endstream", 9); |
| 1217 | size_t end = e ? (size_t)(e - s) : len; |
| 1218 | raw = s + st; |
| 1219 | raw_len = end - st; |
| 1220 | if (raw_len >= 2 && raw[raw_len - 2] == '\r' && raw[raw_len - 1] == '\n') { |
| 1221 | raw_len -= 2; |
| 1222 | } else if (raw_len >= 1 && (raw[raw_len - 1] == '\n' || raw[raw_len - 1] == '\r')) { |
| 1223 | raw_len -= 1; |
| 1224 | } |
| 1225 | } |
| 1226 | pdf_stream_t *sm = (pdf_stream_t *)pdf_calloc(d, sizeof(*sm)); |
| 1227 | pdf_val_t *sv = new_val(d, PV_STREAM); |
| 1228 | if (!sm || !sv) { |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | sm->dict = val; |
| 1232 | sm->raw = raw; |
| 1233 | sm->raw_len = raw_len; |
| 1234 | sv->stream = sm; |
| 1235 | return sv; |
| 1236 | } |
| 1237 | |
| 1238 | /* (number, position) pairs, sorted to find the first of a number. */ |
| 1239 | static int numidx_cmp(const void *x, const void *y) { |
no test coverage detected