| 1 |
|
| 2 |
#include "pplib.h" |
| 3 |
|
| 4 |
#define PPXREF_MAP_INIT 16 // number of xref sections |
| 5 |
|
| 6 |
ppxref * ppxref_create (ppdoc *pdf, size_t initsize, size_t xrefoffset) |
| 7 |
{ |
| 8 |
ppxref *xref; |
| 9 |
|
| 10 |
if (initsize == 0) // unknown |
| 11 |
initsize = PPXREF_MAP_INIT; |
| 12 |
xref = (ppxref *)ppstruct_take(&pdf->heap, sizeof(ppxref)); |
| 13 |
xref->sects = (ppxsec *)ppstruct_take(&pdf->heap, initsize * sizeof(ppxsec)); |
| 14 |
xref->size = 0; |
| 15 |
xref->space = initsize; |
| 16 |
xref->count = 0; |
| 17 |
xref->trailer.type = PPNONE; |
| 18 |
xref->trailer.dict = NULL; |
| 19 |
xref->prev = NULL; |
| 20 |
xref->pdf = pdf; |
| 21 |
xref->offset = xrefoffset; |
| 22 |
return xref; |
| 23 |
} |
| 24 |
|
| 25 |
ppxsec * ppxref_push_section (ppxref *xref, ppheap *heap) |
| 26 |
{ |
| 27 |
ppxsec *sects; |
| 28 |
if (xref->size < xref->space) |
| 29 |
return &xref->sects[xref->size++]; |
| 30 |
xref->space <<= 1; |
| 31 |
sects = xref->sects; |
| 32 |
xref->sects = (ppxsec *)ppstruct_take(heap, xref->space * sizeof(ppxsec)); // waste but rare |
| 33 |
memcpy(xref->sects, sects, xref->size * sizeof(ppxsec)); |
| 34 |
return &xref->sects[xref->size++]; |
| 35 |
} |
| 36 |
|
| 37 |
/* When loading xref table, we don't know how many sections is there. We assume 16, which is |
| 38 |
more than usual (waste). But if there is more, we double the size, wasting again. This |
| 39 |
could be made better with a dedicated allocator for xref sections (heap or generic malloc). |
| 40 |
Or an ephemeric malloced c-array stored in heap once ready (analogical to stack used for dicts/arrays). |
| 41 |
For xref streams we have explicit num of sections. */ |
| 42 |
|
| 43 |
/* |
| 44 |
void ppxref_done_sections (ppxref *xref, ppheap *heap) |
| 45 |
{ // if xref->sects was initialized with mallocted array we could do |
| 46 |
ppxsec *sects; |
| 47 |
size_t size; |
| 48 |
sects = xref->sects; |
| 49 |
size = xref->size * sizeof(ppxsec); |
| 50 |
xref->sects = (ppxsec *)ppstruct_take(heap, size); |
| 51 |
memcpy(xref->sects, sects, size); |
| 52 |
pp_free(sects); |
| 53 |
xref->space = xref->size; |
| 54 |
} |
| 55 |
*/ |
| 56 |
|
| 57 |
static void ppxref_sort_sects (ppxsec *left, ppxsec *right) |
| 58 |
{ |
| 59 |
ppxsec *l, *r, *m, t; |
| 60 |
ppuint first, last; |
| 61 |
l = left, r = right, m = l + ((r - l) / 2); |
| 62 |
first = m->first, last = m->last; |
| 63 |
do |
| 64 |
{ // don't take first/last from pointer |
| 65 |
while (l->first < first) ++l; |
| 66 |
while (r->first > last) --r; |
| 67 |
if (l <= r) |
| 68 |
{ |
| 69 |
t = *l; |
| 70 |
*l = *r; |
| 71 |
*r = t; |
| 72 |
++l, --r; |
| 73 |
} |
| 74 |
} while (l <= r); |
| 75 |
if (l < right) |
| 76 |
ppxref_sort_sects(l, right); |
| 77 |
if (r > left) |
| 78 |
ppxref_sort_sects(left, r); |
| 79 |
} |
| 80 |
|
| 81 |
int ppxref_sort (ppxref *xref) |
| 82 |
{ |
| 83 |
if (xref->size == 0) |
| 84 |
return 0; |
| 85 |
ppxref_sort_sects(xref->sects, xref->sects + xref->size - 1); |
| 86 |
return 1; |
| 87 |
} |
| 88 |
|
| 89 |
ppref * ppxref_find_local (ppxref *xref, ppuint refnumber) |
| 90 |
{ |
| 91 |
ppxsec *left, *right, *mid; |
| 92 |
//if (xref->size == 0) // we don't allow that |
| 93 |
// return NULL; |
| 94 |
left = xref->sects; |
| 95 |
right = xref->sects + xref->size - 1; |
| 96 |
do |
| 97 |
{ |
| 98 |
mid = left + ((right - left) / 2); |
| 99 |
if (refnumber > mid->last) |
| 100 |
left = mid + 1; |
| 101 |
else if (refnumber < mid->first) |
| 102 |
right = mid - 1; |
| 103 |
else |
| 104 |
return &mid->refs[refnumber - mid->first]; |
| 105 |
} while (left <= right); |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
PJ 20180910 |
| 111 |
|
| 112 |
So far we were resolving references in the context of the current xref: |
| 113 |
|
| 114 |
- if a given object is found in this xref, than this is the object |
| 115 |
- otherwise older xrefs are queried in order |
| 116 |
- only in linearized documents older body may refer to object from newer xref |
| 117 |
|
| 118 |
Hans sent a document where an incremental update (newer body) has only an updated page object |
| 119 |
(plus /Metadata and /Info), but /Root (catalog) and /Pages dict refs are defined only in the older body. |
| 120 |
If we resolve references using the approach so far, we actually drop the update; newer objects are parsed |
| 121 |
and linked to the newest xref, but never linked to objects tree. Assuming we will never need to interpret |
| 122 |
older versions, makes sense to assume, that the newest object version is always the correct version. |
| 123 |
|
| 124 |
*/ |
| 125 |
|
| 126 |
#if 0 |
| 127 |
|
| 128 |
ppref * ppxref_find (ppxref *xref, ppuint refnumber) |
| 129 |
{ |
| 130 |
ppref *ref; |
| 131 |
ppxref *other; |
| 132 |
|
| 133 |
if ((ref = ppxref_find_local(xref, refnumber)) != NULL) |
| 134 |
return ref; |
| 135 |
if (xref->pdf->flags & PPDOC_LINEARIZED) |
| 136 |
{ |
| 137 |
for (other = xref->pdf->xref; other != NULL; other = other->prev) |
| 138 |
if (other != xref && (ref = ppxref_find_local(other, refnumber)) != NULL) |
| 139 |
return ref; |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
for (other = xref->prev; other != NULL; other = other->prev) |
| 144 |
if ((ref = ppxref_find_local(other, refnumber)) != NULL) |
| 145 |
return ref; |
| 146 |
/* This shouldn't happen, but I've met documents that have no linearized dict, |
| 147 |
but their xrefs are prepared as for linearized; with "older" xrefs referring |
| 148 |
to "newer". */ |
| 149 |
for (other = xref->pdf->xref; other != NULL && other != xref; other = other->prev) |
| 150 |
if ((ref = ppxref_find_local(other, refnumber)) != NULL) |
| 151 |
return ref; |
| 152 |
} |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
#else |
| 157 |
|
| 158 |
ppref * ppxref_find (ppxref *xref, ppuint refnumber) |
| 159 |
{ |
| 160 |
ppref *ref; |
| 161 |
ppxref *other; |
| 162 |
|
| 163 |
for (other = xref->pdf->xref; other != NULL; other = other->prev) |
| 164 |
if ((ref = ppxref_find_local(other, refnumber)) != NULL) |
| 165 |
return ref; |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
|
| 169 |
#endif |
| 170 |
|
| 171 |
ppdict * ppxref_trailer (ppxref *xref) |
| 172 |
{ |
| 173 |
switch (xref->trailer.type) |
| 174 |
{ |
| 175 |
case PPDICT: |
| 176 |
return xref->trailer.dict; |
| 177 |
case PPSTREAM: |
| 178 |
return xref->trailer.stream->dict; |
| 179 |
default: |
| 180 |
break; |
| 181 |
} |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
ppxref * ppdoc_xref (ppdoc *pdf) |
| 186 |
{ |
| 187 |
return pdf->xref; |
| 188 |
} |
| 189 |
|
| 190 |
ppxref * ppxref_prev (ppxref *xref) |
| 191 |
{ |
| 192 |
return xref->prev; |
| 193 |
} |
| 194 |
|
| 195 |
ppdict * ppxref_catalog (ppxref *xref) |
| 196 |
{ |
| 197 |
ppdict *trailer; |
| 198 |
return (trailer = ppxref_trailer(xref)) != NULL ? ppdict_rget_dict(trailer, "Root") : NULL; |
| 199 |
} |
| 200 |
|
| 201 |
ppdict * ppxref_info (ppxref *xref) |
| 202 |
{ |
| 203 |
ppdict *trailer; |
| 204 |
return (trailer = ppxref_trailer(xref)) != NULL ? ppdict_rget_dict(trailer, "Info") : NULL; |
| 205 |
} |
| 206 |
|
| 207 |
ppref * ppxref_pages (ppxref *xref) |
| 208 |
{ |
| 209 |
ppdict *dict; |
| 210 |
ppref *ref; |
| 211 |
|
| 212 |
if ((dict = ppxref_catalog(xref)) == NULL || (ref = ppdict_get_ref(dict, "Pages")) == NULL) |
| 213 |
return NULL; |
| 214 |
return ref->object.type == PPDICT ? ref : NULL; |
| 215 |
} |