| 1490 | } |
| 1491 | |
| 1492 | static pdf_objstm_t *objstm_open(pdf_doc_t *d, int64_t stmnum) { |
| 1493 | pdf_xent_t *e = xent(d, stmnum); |
| 1494 | if (e && e->stm) { |
| 1495 | return e->stm; |
| 1496 | } |
| 1497 | pdf_val_t *st = pdf_get(d, stmnum); |
| 1498 | if (!st || st->kind != PV_STREAM) { |
| 1499 | return NULL; |
| 1500 | } |
| 1501 | size_t dlen = 0; |
| 1502 | const unsigned char *data = pdf_stream_decode(d, st->stream, &dlen); |
| 1503 | if (!data) { |
| 1504 | data = (const unsigned char *)""; |
| 1505 | dlen = 0; |
| 1506 | } |
| 1507 | pdf_val_t *nv = pdf_dget_raw(st->stream->dict, "N"); |
| 1508 | pdf_val_t *fv = pdf_dget_raw(st->stream->dict, "First"); |
| 1509 | int64_t n = (nv && nv->kind == PV_NUM && nv->is_int) ? nv->inum : 0; |
| 1510 | pdf_objstm_t *sm = (pdf_objstm_t *)pdf_calloc(d, sizeof(*sm)); |
| 1511 | if (!sm) { |
| 1512 | return NULL; |
| 1513 | } |
| 1514 | sm->data = data; |
| 1515 | sm->len = dlen; |
| 1516 | sm->first = (fv && fv->kind == PV_NUM && fv->is_int) ? fv->inum : 0; |
| 1517 | /* at most one pair per two tokens of the header: bounded by the data */ |
| 1518 | int64_t cap = n < 0 ? 0 : n; |
| 1519 | if (cap > (int64_t)(dlen / 2 + 1)) { |
| 1520 | cap = (int64_t)(dlen / 2 + 1); |
| 1521 | } |
| 1522 | sm->onum = (int64_t *)pdf_big(d, (size_t)(cap ? cap : 1) * sizeof(int64_t)); |
| 1523 | sm->ooff = (int64_t *)pdf_big(d, (size_t)(cap ? cap : 1) * sizeof(int64_t)); |
| 1524 | if (!sm->onum || !sm->ooff) { |
| 1525 | return NULL; |
| 1526 | } |
| 1527 | size_t p = 0; |
| 1528 | for (int64_t i = 0; i < n && sm->n < cap; i++) { |
| 1529 | pdf_tok_t t1; |
| 1530 | pdf_tok_t t2; |
| 1531 | lex_peek(d, data, dlen, &p, &t1); |
| 1532 | lex_peek(d, data, dlen, &p, &t2); |
| 1533 | if (t1.kind != PT_NUM || t2.kind != PT_NUM) { |
| 1534 | break; |
| 1535 | } |
| 1536 | sm->onum[sm->n] = t1.is_int ? t1.inum : pdf_d2i(t1.num); |
| 1537 | sm->ooff[sm->n] = t2.is_int ? t2.inum : pdf_d2i(t2.num); |
| 1538 | sm->n++; |
| 1539 | } |
| 1540 | e = xent(d, stmnum); |
| 1541 | if (e) { |
| 1542 | e->stm = sm; |
| 1543 | } |
| 1544 | return sm; |
| 1545 | } |
| 1546 | |
| 1547 | static pdf_objstm_t *objstm(pdf_doc_t *d, int64_t stmnum) { |
| 1548 | d->objstm_opening++; |
no test coverage detected