/src/gdal/frmts/pdf/pdfio.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: PDF driver |
4 | | * Purpose: GDALDataset driver for PDF dataset. |
5 | | * Author: Even Rouault, <even dot rouault at spatialys.com> |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 2010-2013, Even Rouault <even dot rouault at spatialys.com> |
9 | | * |
10 | | * SPDX-License-Identifier: MIT |
11 | | ****************************************************************************/ |
12 | | |
13 | | #include "gdal_pdf.h" |
14 | | |
15 | | #ifdef HAVE_POPPLER |
16 | | |
17 | | #include "pdfio.h" |
18 | | |
19 | | #include "cpl_vsi.h" |
20 | | |
21 | | static vsi_l_offset VSIPDFFileStreamGetSize(VSILFILE *f) |
22 | 42.5k | { |
23 | 42.5k | VSIFSeekL(f, 0, SEEK_END); |
24 | 42.5k | vsi_l_offset nSize = VSIFTellL(f); |
25 | 42.5k | VSIFSeekL(f, 0, SEEK_SET); |
26 | 42.5k | return nSize; |
27 | 42.5k | } |
28 | | |
29 | | /************************************************************************/ |
30 | | /* VSIPDFFileStream() */ |
31 | | /************************************************************************/ |
32 | | |
33 | | VSIPDFFileStream::VSIPDFFileStream(VSILFILE *fIn, const char *pszFilename, |
34 | | Object &&dictA) |
35 | 42.5k | : BaseStream(std::move(dictA), |
36 | 42.5k | static_cast<Goffset>(VSIPDFFileStreamGetSize(fIn))), |
37 | 42.5k | poParent(nullptr), poFilename(new GooString(pszFilename)), f(fIn) |
38 | 42.5k | { |
39 | 42.5k | } |
40 | | |
41 | | /************************************************************************/ |
42 | | /* VSIPDFFileStream() */ |
43 | | /************************************************************************/ |
44 | | |
45 | | VSIPDFFileStream::VSIPDFFileStream(VSIPDFFileStream *poParentIn, |
46 | | vsi_l_offset startA, bool limitedA, |
47 | | vsi_l_offset lengthA, Object &&dictA) |
48 | 5.02M | : BaseStream(std::move(dictA), static_cast<Goffset>(lengthA)), |
49 | 5.02M | poParent(poParentIn), poFilename(poParentIn->poFilename), |
50 | 5.02M | f(poParentIn->f), nStart(startA), bLimited(limitedA), nLength(lengthA) |
51 | 5.02M | { |
52 | 5.02M | } |
53 | | |
54 | | /************************************************************************/ |
55 | | /* ~VSIPDFFileStream() */ |
56 | | /************************************************************************/ |
57 | | |
58 | | VSIPDFFileStream::~VSIPDFFileStream() |
59 | 5.07M | { |
60 | 5.07M | close(); |
61 | 5.07M | if (poParent == nullptr) |
62 | 42.5k | { |
63 | 42.5k | delete poFilename; |
64 | 42.5k | } |
65 | 5.07M | } |
66 | | |
67 | | /************************************************************************/ |
68 | | /* copy() */ |
69 | | /************************************************************************/ |
70 | | |
71 | | BaseStream *VSIPDFFileStream::copy() |
72 | 0 | { |
73 | 0 | return new VSIPDFFileStream(poParent, nStart, bLimited, nLength, |
74 | 0 | dict.copy()); |
75 | 0 | } |
76 | | |
77 | | /************************************************************************/ |
78 | | /* makeSubStream() */ |
79 | | /************************************************************************/ |
80 | | |
81 | | #if POPPLER_MAJOR_VERSION > 25 || \ |
82 | | (POPPLER_MAJOR_VERSION == 25 && POPPLER_MINOR_VERSION >= 5) |
83 | | std::unique_ptr<Stream> VSIPDFFileStream::makeSubStream(Goffset startA, |
84 | | bool limitedA, |
85 | | Goffset lengthA, |
86 | | Object &&dictA) |
87 | | { |
88 | | return std::make_unique<VSIPDFFileStream>(this, startA, limitedA, lengthA, |
89 | | std::move(dictA)); |
90 | | } |
91 | | #else |
92 | | Stream *VSIPDFFileStream::makeSubStream(Goffset startA, bool limitedA, |
93 | | Goffset lengthA, Object &&dictA) |
94 | 5.02M | { |
95 | 5.02M | return new VSIPDFFileStream(this, startA, limitedA, lengthA, |
96 | 5.02M | std::move(dictA)); |
97 | 5.02M | } |
98 | | #endif |
99 | | |
100 | | /************************************************************************/ |
101 | | /* getPos() */ |
102 | | /************************************************************************/ |
103 | | |
104 | | Goffset VSIPDFFileStream::getPos() |
105 | 933M | { |
106 | 933M | return static_cast<Goffset>(nCurrentPos); |
107 | 933M | } |
108 | | |
109 | | /************************************************************************/ |
110 | | /* getStart() */ |
111 | | /************************************************************************/ |
112 | | |
113 | | Goffset VSIPDFFileStream::getStart() |
114 | 90.9k | { |
115 | 90.9k | return static_cast<Goffset>(nStart); |
116 | 90.9k | } |
117 | | |
118 | | /************************************************************************/ |
119 | | /* getKind() */ |
120 | | /************************************************************************/ |
121 | | |
122 | | StreamKind VSIPDFFileStream::getKind() const |
123 | 151 | { |
124 | 151 | return strFile; |
125 | 151 | } |
126 | | |
127 | | /************************************************************************/ |
128 | | /* getFileName() */ |
129 | | /************************************************************************/ |
130 | | |
131 | | GooString *VSIPDFFileStream::getFileName() |
132 | 85.0k | { |
133 | 85.0k | return poFilename; |
134 | 85.0k | } |
135 | | |
136 | | /************************************************************************/ |
137 | | /* FillBuffer() */ |
138 | | /************************************************************************/ |
139 | | |
140 | | int VSIPDFFileStream::FillBuffer() |
141 | 53.9M | { |
142 | 53.9M | if (nBufferLength == 0) |
143 | 1.40M | return FALSE; |
144 | 52.5M | if (nBufferLength != -1 && nBufferLength < BUFFER_SIZE) |
145 | 36.3M | return FALSE; |
146 | | |
147 | 16.2M | nPosInBuffer = 0; |
148 | 16.2M | int nToRead; |
149 | 16.2M | if (!bLimited) |
150 | 13.7M | nToRead = BUFFER_SIZE; |
151 | 2.48M | else if (nCurrentPos + BUFFER_SIZE > nStart + nLength) |
152 | 1.26M | nToRead = static_cast<int>(nStart + nLength - nCurrentPos); |
153 | 1.21M | else |
154 | 1.21M | nToRead = BUFFER_SIZE; |
155 | 16.2M | if (nToRead < 0) |
156 | 0 | return FALSE; |
157 | 16.2M | nBufferLength = static_cast<int>(VSIFReadL(abyBuffer, 1, nToRead, f)); |
158 | 16.2M | if (nBufferLength == 0) |
159 | 78.8k | return FALSE; |
160 | | |
161 | | // Since we now report a non-zero length (as BaseStream::length member), |
162 | | // PDFDoc::getPage() can go to the linearized mode if the file is |
163 | | // linearized, and thus create a pageCache. If so, in PDFDoc::~PDFDoc(), if |
164 | | // pageCache is not null, it would try to access the stream (str) through |
165 | | // getPageCount(), but we have just freed and nullify str before in |
166 | | // PDFFreeDoc(). So make as if the file is not linearized to avoid those |
167 | | // issues... All this is due to our attempt of avoiding cross-heap issues |
168 | | // with allocation and liberation of VSIPDFFileStream as PDFDoc::str member. |
169 | 16.1M | if (nCurrentPos == 0 || nCurrentPos == VSI_L_OFFSET_MAX) |
170 | 172k | { |
171 | 172k | for (int i = 0; |
172 | 145M | i < nBufferLength - static_cast<int>(strlen("/Linearized ")); i++) |
173 | 145M | { |
174 | 145M | if (memcmp(abyBuffer + i, "/Linearized ", strlen("/Linearized ")) == |
175 | 145M | 0) |
176 | 22.0k | { |
177 | 22.0k | bFoundLinearizedHint = true; |
178 | 22.0k | memcpy(abyBuffer + i, "/XXXXXXXXXX ", strlen("/Linearized ")); |
179 | 22.0k | break; |
180 | 22.0k | } |
181 | 145M | } |
182 | 172k | } |
183 | | |
184 | 16.1M | return TRUE; |
185 | 16.2M | } |
186 | | |
187 | | /************************************************************************/ |
188 | | /* getChar() */ |
189 | | /************************************************************************/ |
190 | | |
191 | | /* The unoptimized version performs a bit less since we must go through */ |
192 | | /* the whole virtual I/O chain for each character reading. We save a few */ |
193 | | /* percent with this extra internal caching */ |
194 | | |
195 | | int VSIPDFFileStream::getChar() |
196 | 11.2G | { |
197 | | #ifdef unoptimized_version |
198 | | GByte chRead; |
199 | | if (bLimited && nCurrentPos >= nStart + nLength) |
200 | | return EOF; |
201 | | if (VSIFReadL(&chRead, 1, 1, f) == 0) |
202 | | return EOF; |
203 | | #else |
204 | 11.2G | if (nPosInBuffer == nBufferLength) |
205 | 53.6M | { |
206 | 53.6M | if (!FillBuffer() || nPosInBuffer >= nBufferLength) |
207 | 37.6M | return EOF; |
208 | 53.6M | } |
209 | | |
210 | 11.2G | GByte chRead = abyBuffer[nPosInBuffer]; |
211 | 11.2G | nPosInBuffer++; |
212 | 11.2G | #endif |
213 | 11.2G | nCurrentPos++; |
214 | 11.2G | return chRead; |
215 | 11.2G | } |
216 | | |
217 | | /************************************************************************/ |
218 | | /* getUnfilteredChar() */ |
219 | | /************************************************************************/ |
220 | | |
221 | | int VSIPDFFileStream::getUnfilteredChar() |
222 | 0 | { |
223 | 0 | return getChar(); |
224 | 0 | } |
225 | | |
226 | | /************************************************************************/ |
227 | | /* lookChar() */ |
228 | | /************************************************************************/ |
229 | | |
230 | | int VSIPDFFileStream::lookChar() |
231 | 56.2M | { |
232 | | #ifdef unoptimized_version |
233 | | int nPosBefore = nCurrentPos; |
234 | | int chRead = getChar(); |
235 | | if (chRead == EOF) |
236 | | return EOF; |
237 | | VSIFSeekL(f, nCurrentPos = nPosBefore, SEEK_SET); |
238 | | return chRead; |
239 | | #else |
240 | 56.2M | int chRead = getChar(); |
241 | 56.2M | if (chRead == EOF) |
242 | 74.8k | return EOF; |
243 | 56.2M | nPosInBuffer--; |
244 | 56.2M | nCurrentPos--; |
245 | 56.2M | return chRead; |
246 | 56.2M | #endif |
247 | 56.2M | } |
248 | | |
249 | | /************************************************************************/ |
250 | | /* reset() */ |
251 | | /************************************************************************/ |
252 | | |
253 | | #if POPPLER_MAJOR_VERSION > 25 || \ |
254 | | (POPPLER_MAJOR_VERSION == 25 && POPPLER_MINOR_VERSION >= 2) |
255 | | bool VSIPDFFileStream::reset() |
256 | | #else |
257 | | void VSIPDFFileStream::reset() |
258 | | #endif |
259 | 5.14M | { |
260 | 5.14M | nSavedPos = VSIFTellL(f); |
261 | 5.14M | bHasSavedPos = TRUE; |
262 | 5.14M | VSIFSeekL(f, nCurrentPos = nStart, SEEK_SET); |
263 | 5.14M | nPosInBuffer = -1; |
264 | 5.14M | nBufferLength = -1; |
265 | | #if POPPLER_MAJOR_VERSION > 25 || \ |
266 | | (POPPLER_MAJOR_VERSION == 25 && POPPLER_MINOR_VERSION >= 2) |
267 | | return true; |
268 | | #endif |
269 | 5.14M | } |
270 | | |
271 | | /************************************************************************/ |
272 | | /* unfilteredReset() */ |
273 | | /************************************************************************/ |
274 | | |
275 | | #if POPPLER_MAJOR_VERSION > 25 || \ |
276 | | (POPPLER_MAJOR_VERSION == 25 && POPPLER_MINOR_VERSION >= 3) |
277 | | bool VSIPDFFileStream::unfilteredReset() |
278 | | { |
279 | | return reset(); |
280 | | } |
281 | | #else |
282 | | void VSIPDFFileStream::unfilteredReset() |
283 | 0 | { |
284 | 0 | reset(); |
285 | 0 | } |
286 | | #endif |
287 | | |
288 | | /************************************************************************/ |
289 | | /* close() */ |
290 | | /************************************************************************/ |
291 | | |
292 | | void VSIPDFFileStream::close() |
293 | 10.0M | { |
294 | 10.0M | if (bHasSavedPos) |
295 | 5.01M | { |
296 | 5.01M | nCurrentPos = nSavedPos; |
297 | 5.01M | VSIFSeekL(f, nCurrentPos, SEEK_SET); |
298 | 5.01M | } |
299 | 10.0M | bHasSavedPos = FALSE; |
300 | 10.0M | nSavedPos = 0; |
301 | 10.0M | } |
302 | | |
303 | | /************************************************************************/ |
304 | | /* setPos() */ |
305 | | /************************************************************************/ |
306 | | |
307 | | void VSIPDFFileStream::setPos(Goffset pos, int dir) |
308 | 1.62M | { |
309 | 1.62M | if (dir >= 0) |
310 | 1.31M | { |
311 | 1.31M | VSIFSeekL(f, nCurrentPos = pos, SEEK_SET); |
312 | 1.31M | } |
313 | 308k | else |
314 | 308k | { |
315 | 308k | if (bLimited == false) |
316 | 308k | { |
317 | 308k | VSIFSeekL(f, 0, SEEK_END); |
318 | 308k | } |
319 | 0 | else |
320 | 0 | { |
321 | 0 | VSIFSeekL(f, nStart + nLength, SEEK_SET); |
322 | 0 | } |
323 | 308k | vsi_l_offset size = VSIFTellL(f); |
324 | 308k | vsi_l_offset newpos = static_cast<vsi_l_offset>(pos); |
325 | 308k | if (newpos > size) |
326 | 22.8k | newpos = size; |
327 | 308k | VSIFSeekL(f, nCurrentPos = size - newpos, SEEK_SET); |
328 | 308k | } |
329 | 1.62M | nPosInBuffer = -1; |
330 | 1.62M | nBufferLength = -1; |
331 | 1.62M | } |
332 | | |
333 | | /************************************************************************/ |
334 | | /* moveStart() */ |
335 | | /************************************************************************/ |
336 | | |
337 | | void VSIPDFFileStream::moveStart(Goffset delta) |
338 | 34.7k | { |
339 | 34.7k | nStart += delta; |
340 | 34.7k | nCurrentPos = nStart; |
341 | 34.7k | VSIFSeekL(f, nCurrentPos, SEEK_SET); |
342 | 34.7k | nPosInBuffer = -1; |
343 | 34.7k | nBufferLength = -1; |
344 | 34.7k | } |
345 | | |
346 | | /************************************************************************/ |
347 | | /* hasGetChars() */ |
348 | | /************************************************************************/ |
349 | | |
350 | | bool VSIPDFFileStream::hasGetChars() |
351 | 595k | { |
352 | 595k | return true; |
353 | 595k | } |
354 | | |
355 | | /************************************************************************/ |
356 | | /* getChars() */ |
357 | | /************************************************************************/ |
358 | | |
359 | | int VSIPDFFileStream::getChars(int nChars, unsigned char *buffer) |
360 | 595k | { |
361 | 595k | int nRead = 0; |
362 | 1.08M | while (nRead < nChars) |
363 | 702k | { |
364 | 702k | int nToRead = nChars - nRead; |
365 | 702k | if (nPosInBuffer == nBufferLength) |
366 | 317k | { |
367 | 317k | if (!bLimited && nToRead > BUFFER_SIZE) |
368 | 0 | { |
369 | 0 | int nJustRead = |
370 | 0 | static_cast<int>(VSIFReadL(buffer + nRead, 1, nToRead, f)); |
371 | 0 | nPosInBuffer = -1; |
372 | 0 | nBufferLength = -1; |
373 | 0 | nCurrentPos += nJustRead; |
374 | 0 | nRead += nJustRead; |
375 | 0 | break; |
376 | 0 | } |
377 | 317k | else if (!FillBuffer() || nPosInBuffer >= nBufferLength) |
378 | 215k | break; |
379 | 317k | } |
380 | 487k | if (nToRead > nBufferLength - nPosInBuffer) |
381 | 107k | nToRead = nBufferLength - nPosInBuffer; |
382 | | |
383 | 487k | memcpy(buffer + nRead, abyBuffer + nPosInBuffer, nToRead); |
384 | 487k | nPosInBuffer += nToRead; |
385 | 487k | nCurrentPos += nToRead; |
386 | 487k | nRead += nToRead; |
387 | 487k | } |
388 | 595k | return nRead; |
389 | 595k | } |
390 | | |
391 | | #endif |