Line data Source code
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 : *
9 : * Support for open-source PDFium library
10 : *
11 : * Copyright (C) 2015 Klokan Technologies GmbH (http://www.klokantech.com/)
12 : * Author: Martin Mikita <martin.mikita@klokantech.com>, xmikit00 @ FIT VUT Brno
13 : *
14 : ******************************************************************************
15 : * Copyright (c) 2011-2013, Even Rouault <even dot rouault at spatialys.com>
16 : *
17 : * SPDX-License-Identifier: MIT
18 : ****************************************************************************/
19 :
20 : #include "gdal_pdf.h"
21 :
22 : #include <cassert>
23 : #include <limits>
24 : #include <type_traits>
25 : #include <vector>
26 : #include "pdfobject.h"
27 :
28 : /************************************************************************/
29 : /* ROUND_IF_CLOSE() */
30 : /************************************************************************/
31 :
32 4541 : double ROUND_IF_CLOSE(double x, double eps)
33 : {
34 4541 : if (eps == 0.0)
35 3313 : eps = fabs(x) < 1 ? 1e-10 : 1e-8;
36 4541 : const double dfRounded = std::round(x);
37 4541 : if (fabs(x - dfRounded) < eps)
38 3531 : return dfRounded;
39 : else
40 1010 : return x;
41 : }
42 :
43 : /************************************************************************/
44 : /* GDALPDFGetPDFString() */
45 : /************************************************************************/
46 :
47 631 : static CPLString GDALPDFGetPDFString(const char *pszStr)
48 : {
49 631 : const GByte *pabyData = reinterpret_cast<const GByte *>(pszStr);
50 : GByte ch;
51 20934 : for (size_t i = 0; (ch = pabyData[i]) != '\0'; i++)
52 : {
53 20311 : if (ch < 32 || ch > 127 || ch == '(' || ch == ')' || ch == '\\' ||
54 20303 : ch == '%' || ch == '#')
55 : break;
56 : }
57 631 : CPLString osStr;
58 631 : if (ch == 0)
59 : {
60 623 : osStr = "(";
61 623 : osStr += pszStr;
62 623 : osStr += ")";
63 623 : return osStr;
64 : }
65 :
66 8 : wchar_t *pwszDest = CPLRecodeToWChar(pszStr, CPL_ENC_UTF8, CPL_ENC_UCS2);
67 8 : osStr = "<FEFF";
68 480 : for (size_t i = 0; pwszDest[i] != 0; i++)
69 : {
70 : #ifndef _WIN32
71 472 : if (pwszDest[i] >= 0x10000 /* && pwszDest[i] <= 0x10FFFF */)
72 : {
73 : /* Generate UTF-16 surrogate pairs (on Windows, CPLRecodeToWChar
74 : * does it for us) */
75 0 : int nHeadSurrogate = ((pwszDest[i] - 0x10000) >> 10) | 0xd800;
76 0 : int nTrailSurrogate = ((pwszDest[i] - 0x10000) & 0x3ff) | 0xdc00;
77 0 : osStr += CPLSPrintf("%02X", (nHeadSurrogate >> 8) & 0xff);
78 0 : osStr += CPLSPrintf("%02X", (nHeadSurrogate) & 0xff);
79 0 : osStr += CPLSPrintf("%02X", (nTrailSurrogate >> 8) & 0xff);
80 0 : osStr += CPLSPrintf("%02X", (nTrailSurrogate) & 0xff);
81 : }
82 : else
83 : #endif
84 : {
85 : osStr +=
86 472 : CPLSPrintf("%02X", static_cast<int>(pwszDest[i] >> 8) & 0xff);
87 472 : osStr += CPLSPrintf("%02X", static_cast<int>(pwszDest[i]) & 0xff);
88 : }
89 : }
90 8 : osStr += ">";
91 8 : CPLFree(pwszDest);
92 8 : return osStr;
93 : }
94 :
95 : #if defined(HAVE_POPPLER) || defined(HAVE_PDFIUM)
96 :
97 : /************************************************************************/
98 : /* GDALPDFGetUTF8StringFromBytes() */
99 : /************************************************************************/
100 :
101 436 : static std::string GDALPDFGetUTF8StringFromBytes(const GByte *pabySrc,
102 : size_t nLen)
103 : {
104 436 : const bool bLEUnicodeMarker =
105 436 : nLen >= 2 && pabySrc[0] == 0xFE && pabySrc[1] == 0xFF;
106 436 : const bool bBEUnicodeMarker =
107 436 : nLen >= 2 && pabySrc[0] == 0xFF && pabySrc[1] == 0xFE;
108 436 : if (!bLEUnicodeMarker && !bBEUnicodeMarker)
109 : {
110 0 : std::string osStr;
111 : try
112 : {
113 0 : osStr.reserve(nLen);
114 : }
115 0 : catch (const std::exception &e)
116 : {
117 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
118 0 : "GDALPDFGetUTF8StringFromBytes(): %s", e.what());
119 0 : return osStr;
120 : }
121 0 : osStr.assign(reinterpret_cast<const char *>(pabySrc), nLen);
122 0 : const char *pszStr = osStr.c_str();
123 0 : if (CPLIsUTF8(pszStr, -1))
124 0 : return osStr;
125 : else
126 : {
127 0 : char *pszUTF8 = CPLRecode(pszStr, CPL_ENC_ISO8859_1, CPL_ENC_UTF8);
128 0 : std::string osRet = pszUTF8;
129 0 : CPLFree(pszUTF8);
130 0 : return osRet;
131 : }
132 : }
133 :
134 : /* This is UTF-16 content */
135 436 : pabySrc += 2;
136 436 : nLen = (nLen - 2) / 2;
137 872 : std::wstring awszSource;
138 436 : awszSource.resize(nLen + 1);
139 436 : size_t j = 0;
140 6541 : for (size_t i = 0; i < nLen; i++, j++)
141 : {
142 6105 : if (!bBEUnicodeMarker)
143 5105 : awszSource[j] = (pabySrc[2 * i] << 8) + pabySrc[2 * i + 1];
144 : else
145 1000 : awszSource[j] = (pabySrc[2 * i + 1] << 8) + pabySrc[2 * i];
146 : #ifndef _WIN32
147 : /* Is there a surrogate pair ? See http://en.wikipedia.org/wiki/UTF-16
148 : */
149 : /* On Windows, CPLRecodeFromWChar does this for us, because wchar_t is
150 : * only */
151 : /* 2 bytes wide, whereas on Unix it is 32bits */
152 6105 : if (awszSource[j] >= 0xD800 && awszSource[j] <= 0xDBFF && i + 1 < nLen)
153 : {
154 : /* should be in the range 0xDC00... 0xDFFF */
155 : wchar_t nTrailSurrogate;
156 0 : if (!bBEUnicodeMarker)
157 0 : nTrailSurrogate =
158 0 : (pabySrc[2 * (i + 1)] << 8) + pabySrc[2 * (i + 1) + 1];
159 : else
160 0 : nTrailSurrogate =
161 0 : (pabySrc[2 * (i + 1) + 1] << 8) + pabySrc[2 * (i + 1)];
162 0 : if (nTrailSurrogate >= 0xDC00 && nTrailSurrogate <= 0xDFFF)
163 : {
164 0 : awszSource[j] = ((awszSource[j] - 0xD800) << 10) +
165 0 : (nTrailSurrogate - 0xDC00) + 0x10000;
166 0 : i++;
167 : }
168 : }
169 : #endif
170 : }
171 436 : awszSource[j] = 0;
172 :
173 : char *pszUTF8 =
174 436 : CPLRecodeFromWChar(awszSource.data(), CPL_ENC_UCS2, CPL_ENC_UTF8);
175 436 : awszSource.clear();
176 872 : std::string osStrUTF8(pszUTF8);
177 436 : CPLFree(pszUTF8);
178 436 : return osStrUTF8;
179 : }
180 :
181 : #endif // defined(HAVE_POPPLER) || defined(HAVE_PDFIUM)
182 :
183 : /************************************************************************/
184 : /* GDALPDFGetPDFName() */
185 : /************************************************************************/
186 :
187 2147 : static std::string GDALPDFGetPDFName(const std::string &osStr)
188 : {
189 2147 : std::string osRet;
190 18209 : for (const char ch : osStr)
191 : {
192 16062 : if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
193 12 : (ch >= '0' && ch <= '9') || ch == '-'))
194 0 : osRet += '_';
195 : else
196 16062 : osRet += ch;
197 : }
198 2147 : return osRet;
199 : }
200 :
201 : /************************************************************************/
202 : /* ==================================================================== */
203 : /* GDALPDFObject */
204 : /* ==================================================================== */
205 : /************************************************************************/
206 :
207 : /************************************************************************/
208 : /* ~GDALPDFObject() */
209 : /************************************************************************/
210 :
211 26436 : GDALPDFObject::~GDALPDFObject()
212 : {
213 26436 : }
214 :
215 : /************************************************************************/
216 : /* LookupObject() */
217 : /************************************************************************/
218 :
219 1226 : GDALPDFObject *GDALPDFObject::LookupObject(const char *pszPath)
220 : {
221 1226 : if (GetType() != PDFObjectType_Dictionary)
222 0 : return nullptr;
223 1226 : return GetDictionary()->LookupObject(pszPath);
224 : }
225 :
226 : /************************************************************************/
227 : /* GetTypeName() */
228 : /************************************************************************/
229 :
230 70 : const char *GDALPDFObject::GetTypeName()
231 : {
232 70 : switch (GetType())
233 : {
234 0 : case PDFObjectType_Unknown:
235 0 : return GetTypeNameNative();
236 0 : case PDFObjectType_Null:
237 0 : return "null";
238 0 : case PDFObjectType_Bool:
239 0 : return "bool";
240 0 : case PDFObjectType_Int:
241 0 : return "int";
242 0 : case PDFObjectType_Real:
243 0 : return "real";
244 0 : case PDFObjectType_String:
245 0 : return "string";
246 0 : case PDFObjectType_Name:
247 0 : return "name";
248 3 : case PDFObjectType_Array:
249 3 : return "array";
250 67 : case PDFObjectType_Dictionary:
251 67 : return "dictionary";
252 0 : default:
253 0 : return GetTypeNameNative();
254 : }
255 : }
256 :
257 : /************************************************************************/
258 : /* Serialize() */
259 : /************************************************************************/
260 :
261 10768 : void GDALPDFObject::Serialize(CPLString &osStr, bool bEmitRef)
262 : {
263 10768 : auto nRefNum = GetRefNum();
264 10768 : if (bEmitRef && nRefNum.toBool())
265 : {
266 2088 : int nRefGen = GetRefGen();
267 2088 : osStr.append(CPLSPrintf("%d %d R", nRefNum.toInt(), nRefGen));
268 2088 : return;
269 : }
270 :
271 8680 : switch (GetType())
272 : {
273 12 : case PDFObjectType_Null:
274 12 : osStr.append("null");
275 12 : return;
276 23 : case PDFObjectType_Bool:
277 23 : osStr.append(GetBool() ? "true" : "false");
278 23 : return;
279 2453 : case PDFObjectType_Int:
280 2453 : osStr.append(CPLSPrintf("%d", GetInt()));
281 2453 : return;
282 1653 : case PDFObjectType_Real:
283 : {
284 : char szReal[512];
285 1653 : double dfRealNonRounded = GetReal();
286 1653 : double dfReal = ROUND_IF_CLOSE(dfRealNonRounded);
287 1653 : if (dfReal >=
288 1653 : static_cast<double>(std::numeric_limits<GIntBig>::min()) &&
289 : dfReal <=
290 3306 : static_cast<double>(std::numeric_limits<GIntBig>::max()) &&
291 1653 : dfReal == static_cast<double>(static_cast<GIntBig>(dfReal)))
292 : {
293 1225 : snprintf(szReal, sizeof(szReal), CPL_FRMT_GIB,
294 : static_cast<GIntBig>(dfReal));
295 : }
296 428 : else if (CanRepresentRealAsString())
297 : {
298 : /* Used for OGC BP numeric values */
299 0 : CPLsnprintf(szReal, sizeof(szReal), "(%.*g)", GetPrecision(),
300 : dfReal);
301 : }
302 : else
303 : {
304 428 : CPLsnprintf(szReal, sizeof(szReal), "%.*f", GetPrecision(),
305 : dfReal);
306 :
307 : /* Remove non significant trailing zeroes */
308 428 : char *pszDot = strchr(szReal, '.');
309 428 : if (pszDot)
310 : {
311 428 : int iDot = static_cast<int>(pszDot - szReal);
312 428 : int nLen = static_cast<int>(strlen(szReal));
313 2174 : for (int i = nLen - 1; i > iDot; i--)
314 : {
315 2174 : if (szReal[i] == '0')
316 1746 : szReal[i] = '\0';
317 : else
318 428 : break;
319 : }
320 : }
321 : }
322 1653 : osStr.append(szReal);
323 1653 : return;
324 : }
325 631 : case PDFObjectType_String:
326 631 : osStr.append(GDALPDFGetPDFString(GetString().c_str()));
327 631 : return;
328 2147 : case PDFObjectType_Name:
329 2147 : osStr.append("/");
330 2147 : osStr.append(GDALPDFGetPDFName(GetName()));
331 2147 : return;
332 839 : case PDFObjectType_Array:
333 839 : GetArray()->Serialize(osStr);
334 839 : return;
335 922 : case PDFObjectType_Dictionary:
336 922 : GetDictionary()->Serialize(osStr);
337 922 : return;
338 0 : case PDFObjectType_Unknown:
339 : default:
340 0 : CPLError(CE_Warning, CPLE_AppDefined,
341 : "Serializing unknown object !");
342 0 : return;
343 : }
344 : }
345 :
346 : /************************************************************************/
347 : /* Clone() */
348 : /************************************************************************/
349 :
350 158 : GDALPDFObjectRW *GDALPDFObject::Clone()
351 : {
352 158 : auto nRefNum = GetRefNum();
353 158 : if (nRefNum.toBool())
354 : {
355 63 : int nRefGen = GetRefGen();
356 63 : return GDALPDFObjectRW::CreateIndirect(nRefNum, nRefGen);
357 : }
358 :
359 95 : switch (GetType())
360 : {
361 0 : case PDFObjectType_Null:
362 0 : return GDALPDFObjectRW::CreateNull();
363 0 : case PDFObjectType_Bool:
364 0 : return GDALPDFObjectRW::CreateBool(GetBool());
365 54 : case PDFObjectType_Int:
366 54 : return GDALPDFObjectRW::CreateInt(GetInt());
367 4 : case PDFObjectType_Real:
368 4 : return GDALPDFObjectRW::CreateReal(GetReal());
369 0 : case PDFObjectType_String:
370 0 : return GDALPDFObjectRW::CreateString(GetString().c_str());
371 15 : case PDFObjectType_Name:
372 15 : return GDALPDFObjectRW::CreateName(GetName().c_str());
373 22 : case PDFObjectType_Array:
374 22 : return GDALPDFObjectRW::CreateArray(GetArray()->Clone());
375 0 : case PDFObjectType_Dictionary:
376 0 : return GDALPDFObjectRW::CreateDictionary(GetDictionary()->Clone());
377 0 : case PDFObjectType_Unknown:
378 : default:
379 0 : CPLError(CE_Warning, CPLE_AppDefined, "Cloning unknown object !");
380 0 : return nullptr;
381 : }
382 : }
383 :
384 : /************************************************************************/
385 : /* ==================================================================== */
386 : /* GDALPDFDictionary */
387 : /* ==================================================================== */
388 : /************************************************************************/
389 :
390 : /************************************************************************/
391 : /* ~GDALPDFDictionary() */
392 : /************************************************************************/
393 :
394 6939 : GDALPDFDictionary::~GDALPDFDictionary()
395 : {
396 6939 : }
397 :
398 : /************************************************************************/
399 : /* LookupObject() */
400 : /************************************************************************/
401 :
402 1706 : GDALPDFObject *GDALPDFDictionary::LookupObject(const char *pszPath)
403 : {
404 1706 : GDALPDFObject *poCurObj = nullptr;
405 3412 : CPLStringList aosTokens(CSLTokenizeString2(pszPath, ".", 0));
406 4089 : for (int i = 0; i < aosTokens.size(); i++)
407 : {
408 3074 : int iElt = -1;
409 3074 : char *pszBracket = strchr(aosTokens[i], '[');
410 3074 : if (pszBracket != nullptr)
411 : {
412 0 : iElt = atoi(pszBracket + 1);
413 0 : *pszBracket = '\0';
414 : }
415 :
416 3074 : if (i == 0)
417 : {
418 1706 : poCurObj = Get(aosTokens[i]);
419 : }
420 : else
421 : {
422 1368 : if (poCurObj->GetType() != PDFObjectType_Dictionary)
423 : {
424 0 : return nullptr;
425 : }
426 1368 : auto poDict = poCurObj->GetDictionary();
427 1368 : assert(poDict);
428 1368 : poCurObj = poDict->Get(aosTokens[i]);
429 : }
430 :
431 3074 : if (poCurObj == nullptr)
432 : {
433 691 : return nullptr;
434 : }
435 :
436 2383 : if (iElt >= 0)
437 : {
438 0 : if (poCurObj->GetType() != PDFObjectType_Array)
439 : {
440 0 : return nullptr;
441 : }
442 0 : auto poArray = poCurObj->GetArray();
443 0 : assert(poArray);
444 : #if defined(__GNUC__)
445 : #pragma GCC diagnostic push
446 : #pragma GCC diagnostic ignored "-Wnull-dereference"
447 : #endif
448 0 : poCurObj = poArray->Get(iElt);
449 : #if defined(__GNUC__)
450 : #pragma GCC diagnostic pop
451 : #endif
452 : }
453 : }
454 1015 : return poCurObj;
455 : }
456 :
457 : /************************************************************************/
458 : /* Serialize() */
459 : /************************************************************************/
460 :
461 2204 : void GDALPDFDictionary::Serialize(CPLString &osStr)
462 : {
463 2204 : osStr.append("<< ");
464 9359 : for (const auto &oIter : GetValues())
465 : {
466 7155 : const char *pszKey = oIter.first.c_str();
467 7155 : GDALPDFObject *poObj = oIter.second;
468 7155 : osStr.append("/");
469 7155 : osStr.append(pszKey);
470 7155 : osStr.append(" ");
471 7155 : poObj->Serialize(osStr);
472 7155 : osStr.append(" ");
473 : }
474 2204 : osStr.append(">>");
475 2204 : }
476 :
477 : /************************************************************************/
478 : /* Clone() */
479 : /************************************************************************/
480 :
481 15 : GDALPDFDictionaryRW *GDALPDFDictionary::Clone()
482 : {
483 15 : GDALPDFDictionaryRW *poDict = new GDALPDFDictionaryRW();
484 115 : for (const auto &oIter : GetValues())
485 : {
486 100 : const char *pszKey = oIter.first.c_str();
487 100 : GDALPDFObject *poObj = oIter.second;
488 100 : poDict->Add(pszKey, poObj->Clone());
489 : }
490 15 : return poDict;
491 : }
492 :
493 : /************************************************************************/
494 : /* ==================================================================== */
495 : /* GDALPDFArray */
496 : /* ==================================================================== */
497 : /************************************************************************/
498 :
499 : /************************************************************************/
500 : /* ~GDALPDFArray() */
501 : /************************************************************************/
502 :
503 2217 : GDALPDFArray::~GDALPDFArray()
504 : {
505 2217 : }
506 :
507 : /************************************************************************/
508 : /* Serialize() */
509 : /************************************************************************/
510 :
511 914 : void GDALPDFArray::Serialize(CPLString &osStr)
512 : {
513 914 : int nLength = GetLength();
514 : int i;
515 :
516 914 : osStr.append("[ ");
517 4059 : for (i = 0; i < nLength; i++)
518 : {
519 3145 : Get(i)->Serialize(osStr);
520 3145 : osStr.append(" ");
521 : }
522 914 : osStr.append("]");
523 914 : }
524 :
525 : /************************************************************************/
526 : /* Clone() */
527 : /************************************************************************/
528 :
529 22 : GDALPDFArrayRW *GDALPDFArray::Clone()
530 : {
531 22 : GDALPDFArrayRW *poArray = new GDALPDFArrayRW();
532 22 : int nLength = GetLength();
533 : int i;
534 80 : for (i = 0; i < nLength; i++)
535 : {
536 58 : poArray->Add(Get(i)->Clone());
537 : }
538 22 : return poArray;
539 : }
540 :
541 : /************************************************************************/
542 : /* ==================================================================== */
543 : /* GDALPDFStream */
544 : /* ==================================================================== */
545 : /************************************************************************/
546 :
547 : /************************************************************************/
548 : /* ~GDALPDFStream() */
549 : /************************************************************************/
550 :
551 : GDALPDFStream::~GDALPDFStream() = default;
552 :
553 : /************************************************************************/
554 : /* ==================================================================== */
555 : /* GDALPDFObjectRW */
556 : /* ==================================================================== */
557 : /************************************************************************/
558 :
559 : /************************************************************************/
560 : /* GDALPDFObjectRW() */
561 : /************************************************************************/
562 :
563 10851 : GDALPDFObjectRW::GDALPDFObjectRW(GDALPDFObjectType eType) : m_eType(eType)
564 : {
565 10851 : }
566 :
567 : /************************************************************************/
568 : /* ~GDALPDFObjectRW() */
569 : /************************************************************************/
570 :
571 21702 : GDALPDFObjectRW::~GDALPDFObjectRW()
572 : {
573 10851 : delete m_poDict;
574 10851 : delete m_poArray;
575 21702 : }
576 :
577 : /************************************************************************/
578 : /* CreateIndirect() */
579 : /************************************************************************/
580 :
581 2125 : GDALPDFObjectRW *GDALPDFObjectRW::CreateIndirect(const GDALPDFObjectNum &nNum,
582 : int nGen)
583 : {
584 2125 : GDALPDFObjectRW *poObj = new GDALPDFObjectRW(PDFObjectType_Unknown);
585 2125 : poObj->m_nNum = nNum;
586 2125 : poObj->m_nGen = nGen;
587 2125 : return poObj;
588 : }
589 :
590 : /************************************************************************/
591 : /* CreateNull() */
592 : /************************************************************************/
593 :
594 12 : GDALPDFObjectRW *GDALPDFObjectRW::CreateNull()
595 : {
596 12 : return new GDALPDFObjectRW(PDFObjectType_Null);
597 : }
598 :
599 : /************************************************************************/
600 : /* CreateBool() */
601 : /************************************************************************/
602 :
603 23 : GDALPDFObjectRW *GDALPDFObjectRW::CreateBool(int bVal)
604 : {
605 23 : GDALPDFObjectRW *poObj = new GDALPDFObjectRW(PDFObjectType_Bool);
606 23 : poObj->m_nVal = bVal;
607 23 : return poObj;
608 : }
609 :
610 : /************************************************************************/
611 : /* CreateInt() */
612 : /************************************************************************/
613 :
614 2477 : GDALPDFObjectRW *GDALPDFObjectRW::CreateInt(int nVal)
615 : {
616 2477 : GDALPDFObjectRW *poObj = new GDALPDFObjectRW(PDFObjectType_Int);
617 2477 : poObj->m_nVal = nVal;
618 2477 : return poObj;
619 : }
620 :
621 : /************************************************************************/
622 : /* CreateReal() */
623 : /************************************************************************/
624 :
625 1591 : GDALPDFObjectRW *GDALPDFObjectRW::CreateReal(double dfVal,
626 : int bCanRepresentRealAsString)
627 : {
628 1591 : GDALPDFObjectRW *poObj = new GDALPDFObjectRW(PDFObjectType_Real);
629 1591 : poObj->m_dfVal = dfVal;
630 1591 : poObj->m_bCanRepresentRealAsString = bCanRepresentRealAsString;
631 1591 : return poObj;
632 : }
633 :
634 : /************************************************************************/
635 : /* CreateRealWithPrecision() */
636 : /************************************************************************/
637 :
638 64 : GDALPDFObjectRW *GDALPDFObjectRW::CreateRealWithPrecision(double dfVal,
639 : int nPrecision)
640 : {
641 64 : GDALPDFObjectRW *poObj = new GDALPDFObjectRW(PDFObjectType_Real);
642 64 : poObj->m_dfVal = dfVal;
643 64 : poObj->m_nPrecision = nPrecision;
644 64 : return poObj;
645 : }
646 :
647 : /************************************************************************/
648 : /* CreateString() */
649 : /************************************************************************/
650 :
651 631 : GDALPDFObjectRW *GDALPDFObjectRW::CreateString(const char *pszStr)
652 : {
653 631 : GDALPDFObjectRW *poObj = new GDALPDFObjectRW(PDFObjectType_String);
654 631 : poObj->m_osVal = pszStr;
655 631 : return poObj;
656 : }
657 :
658 : /************************************************************************/
659 : /* CreateName() */
660 : /************************************************************************/
661 :
662 2149 : GDALPDFObjectRW *GDALPDFObjectRW::CreateName(const char *pszName)
663 : {
664 2149 : GDALPDFObjectRW *poObj = new GDALPDFObjectRW(PDFObjectType_Name);
665 2149 : poObj->m_osVal = pszName;
666 2149 : return poObj;
667 : }
668 :
669 : /************************************************************************/
670 : /* CreateDictionary() */
671 : /************************************************************************/
672 :
673 923 : GDALPDFObjectRW *GDALPDFObjectRW::CreateDictionary(GDALPDFDictionaryRW *poDict)
674 : {
675 923 : CPLAssert(poDict);
676 923 : GDALPDFObjectRW *poObj = new GDALPDFObjectRW(PDFObjectType_Dictionary);
677 923 : poObj->m_poDict = poDict;
678 923 : return poObj;
679 : }
680 :
681 : /************************************************************************/
682 : /* CreateArray() */
683 : /************************************************************************/
684 :
685 856 : GDALPDFObjectRW *GDALPDFObjectRW::CreateArray(GDALPDFArrayRW *poArray)
686 : {
687 856 : CPLAssert(poArray);
688 856 : GDALPDFObjectRW *poObj = new GDALPDFObjectRW(PDFObjectType_Array);
689 856 : poObj->m_poArray = poArray;
690 856 : return poObj;
691 : }
692 :
693 : /************************************************************************/
694 : /* GetTypeNameNative() */
695 : /************************************************************************/
696 :
697 0 : const char *GDALPDFObjectRW::GetTypeNameNative()
698 : {
699 0 : CPLError(CE_Failure, CPLE_AppDefined, "Should not go here");
700 0 : return "";
701 : }
702 :
703 : /************************************************************************/
704 : /* GetType() */
705 : /************************************************************************/
706 :
707 8664 : GDALPDFObjectType GDALPDFObjectRW::GetType()
708 : {
709 8664 : return m_eType;
710 : }
711 :
712 : /************************************************************************/
713 : /* GetBool() */
714 : /************************************************************************/
715 :
716 23 : int GDALPDFObjectRW::GetBool()
717 : {
718 23 : if (m_eType == PDFObjectType_Bool)
719 23 : return m_nVal;
720 :
721 0 : return FALSE;
722 : }
723 :
724 : /************************************************************************/
725 : /* GetInt() */
726 : /************************************************************************/
727 :
728 2445 : int GDALPDFObjectRW::GetInt()
729 : {
730 2445 : if (m_eType == PDFObjectType_Int)
731 2445 : return m_nVal;
732 :
733 0 : return 0;
734 : }
735 :
736 : /************************************************************************/
737 : /* GetReal() */
738 : /************************************************************************/
739 :
740 1653 : double GDALPDFObjectRW::GetReal()
741 : {
742 1653 : return m_dfVal;
743 : }
744 :
745 : /************************************************************************/
746 : /* GetString() */
747 : /************************************************************************/
748 :
749 631 : const CPLString &GDALPDFObjectRW::GetString()
750 : {
751 631 : return m_osVal;
752 : }
753 :
754 : /************************************************************************/
755 : /* GetName() */
756 : /************************************************************************/
757 :
758 2139 : const CPLString &GDALPDFObjectRW::GetName()
759 : {
760 2139 : return m_osVal;
761 : }
762 :
763 : /************************************************************************/
764 : /* GetDictionary() */
765 : /************************************************************************/
766 :
767 922 : GDALPDFDictionary *GDALPDFObjectRW::GetDictionary()
768 : {
769 922 : return m_poDict;
770 : }
771 :
772 : /************************************************************************/
773 : /* GetArray() */
774 : /************************************************************************/
775 :
776 839 : GDALPDFArray *GDALPDFObjectRW::GetArray()
777 : {
778 839 : return m_poArray;
779 : }
780 :
781 : /************************************************************************/
782 : /* GetStream() */
783 : /************************************************************************/
784 :
785 0 : GDALPDFStream *GDALPDFObjectRW::GetStream()
786 : {
787 0 : return nullptr;
788 : }
789 :
790 : /************************************************************************/
791 : /* GetRefNum() */
792 : /************************************************************************/
793 :
794 10754 : GDALPDFObjectNum GDALPDFObjectRW::GetRefNum()
795 : {
796 10754 : return m_nNum;
797 : }
798 :
799 : /************************************************************************/
800 : /* GetRefGen() */
801 : /************************************************************************/
802 :
803 2090 : int GDALPDFObjectRW::GetRefGen()
804 : {
805 2090 : return m_nGen;
806 : }
807 :
808 : /************************************************************************/
809 : /* ==================================================================== */
810 : /* GDALPDFDictionaryRW */
811 : /* ==================================================================== */
812 : /************************************************************************/
813 :
814 : /************************************************************************/
815 : /* GDALPDFDictionaryRW() */
816 : /************************************************************************/
817 :
818 : GDALPDFDictionaryRW::GDALPDFDictionaryRW() = default;
819 :
820 : /************************************************************************/
821 : /* ~GDALPDFDictionaryRW() */
822 : /************************************************************************/
823 :
824 3150 : GDALPDFDictionaryRW::~GDALPDFDictionaryRW()
825 : {
826 2212 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.begin();
827 2212 : std::map<CPLString, GDALPDFObject *>::iterator oEnd = m_map.end();
828 9422 : for (; oIter != oEnd; ++oIter)
829 7210 : delete oIter->second;
830 3150 : }
831 :
832 : /************************************************************************/
833 : /* Get() */
834 : /************************************************************************/
835 :
836 3 : GDALPDFObject *GDALPDFDictionaryRW::Get(const char *pszKey)
837 : {
838 3 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.find(pszKey);
839 3 : if (oIter != m_map.end())
840 2 : return oIter->second;
841 1 : return nullptr;
842 : }
843 :
844 : /************************************************************************/
845 : /* GetValues() */
846 : /************************************************************************/
847 :
848 2204 : std::map<CPLString, GDALPDFObject *> &GDALPDFDictionaryRW::GetValues()
849 : {
850 2204 : return m_map;
851 : }
852 :
853 : /************************************************************************/
854 : /* Add() */
855 : /************************************************************************/
856 :
857 7216 : GDALPDFDictionaryRW &GDALPDFDictionaryRW::Add(const char *pszKey,
858 : GDALPDFObject *poVal)
859 : {
860 7216 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.find(pszKey);
861 7216 : if (oIter != m_map.end())
862 : {
863 0 : delete oIter->second;
864 0 : oIter->second = poVal;
865 : }
866 : else
867 7216 : m_map[pszKey] = poVal;
868 :
869 7216 : return *this;
870 : }
871 :
872 : /************************************************************************/
873 : /* Remove() */
874 : /************************************************************************/
875 :
876 15 : GDALPDFDictionaryRW &GDALPDFDictionaryRW::Remove(const char *pszKey)
877 : {
878 15 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.find(pszKey);
879 15 : if (oIter != m_map.end())
880 : {
881 6 : delete oIter->second;
882 6 : m_map.erase(pszKey);
883 : }
884 :
885 15 : return *this;
886 : }
887 :
888 : /************************************************************************/
889 : /* ==================================================================== */
890 : /* GDALPDFArrayRW */
891 : /* ==================================================================== */
892 : /************************************************************************/
893 :
894 : /************************************************************************/
895 : /* GDALPDFArrayRW() */
896 : /************************************************************************/
897 :
898 931 : GDALPDFArrayRW::GDALPDFArrayRW()
899 : {
900 931 : }
901 :
902 : /************************************************************************/
903 : /* ~GDALPDFArrayRW() */
904 : /************************************************************************/
905 :
906 1787 : GDALPDFArrayRW::~GDALPDFArrayRW()
907 : {
908 4114 : for (size_t i = 0; i < m_array.size(); i++)
909 3183 : delete m_array[i];
910 1787 : }
911 :
912 : /************************************************************************/
913 : /* GetLength() */
914 : /************************************************************************/
915 :
916 4060 : int GDALPDFArrayRW::GetLength()
917 : {
918 4060 : return static_cast<int>(m_array.size());
919 : }
920 :
921 : /************************************************************************/
922 : /* Get() */
923 : /************************************************************************/
924 :
925 3145 : GDALPDFObject *GDALPDFArrayRW::Get(int nIndex)
926 : {
927 3145 : if (nIndex < 0 || nIndex >= GetLength())
928 0 : return nullptr;
929 3145 : return m_array[nIndex];
930 : }
931 :
932 : /************************************************************************/
933 : /* Add() */
934 : /************************************************************************/
935 :
936 3183 : GDALPDFArrayRW &GDALPDFArrayRW::Add(GDALPDFObject *poObj)
937 : {
938 3183 : m_array.push_back(poObj);
939 3183 : return *this;
940 : }
941 :
942 : /************************************************************************/
943 : /* Add() */
944 : /************************************************************************/
945 :
946 0 : GDALPDFArrayRW &GDALPDFArrayRW::Add(double *padfVal, int nCount,
947 : int bCanRepresentRealAsString)
948 : {
949 0 : for (int i = 0; i < nCount; i++)
950 0 : m_array.push_back(
951 0 : GDALPDFObjectRW::CreateReal(padfVal[i], bCanRepresentRealAsString));
952 0 : return *this;
953 : }
954 :
955 : #ifdef HAVE_POPPLER
956 :
957 : /************************************************************************/
958 : /* ==================================================================== */
959 : /* GDALPDFDictionaryPoppler */
960 : /* ==================================================================== */
961 : /************************************************************************/
962 :
963 : class GDALPDFDictionaryPoppler final : public GDALPDFDictionary
964 : {
965 : private:
966 : Dict *m_poDict;
967 : std::map<CPLString, GDALPDFObject *> m_map{};
968 :
969 : CPL_DISALLOW_COPY_ASSIGN(GDALPDFDictionaryPoppler)
970 :
971 : public:
972 4727 : GDALPDFDictionaryPoppler(Dict *poDict) : m_poDict(poDict)
973 : {
974 4727 : }
975 :
976 : ~GDALPDFDictionaryPoppler() override;
977 :
978 : GDALPDFObject *Get(const char *pszKey) override;
979 : std::map<CPLString, GDALPDFObject *> &GetValues() override;
980 : };
981 :
982 : /************************************************************************/
983 : /* ==================================================================== */
984 : /* GDALPDFArrayPoppler */
985 : /* ==================================================================== */
986 : /************************************************************************/
987 :
988 : class GDALPDFArrayPoppler final : public GDALPDFArray
989 : {
990 : private:
991 : const Array *m_poArray;
992 : std::vector<std::unique_ptr<GDALPDFObject>> m_v{};
993 :
994 : CPL_DISALLOW_COPY_ASSIGN(GDALPDFArrayPoppler)
995 :
996 : public:
997 1286 : GDALPDFArrayPoppler(const Array *poArray) : m_poArray(poArray)
998 : {
999 1286 : }
1000 :
1001 : int GetLength() override;
1002 : GDALPDFObject *Get(int nIndex) override;
1003 : };
1004 :
1005 : /************************************************************************/
1006 : /* ==================================================================== */
1007 : /* GDALPDFStreamPoppler */
1008 : /* ==================================================================== */
1009 : /************************************************************************/
1010 :
1011 : class GDALPDFStreamPoppler final : public GDALPDFStream
1012 : {
1013 : private:
1014 : int64_t m_nLength = -1;
1015 : Stream *m_poStream;
1016 : int64_t m_nRawLength = -1;
1017 :
1018 : CPL_DISALLOW_COPY_ASSIGN(GDALPDFStreamPoppler)
1019 :
1020 : public:
1021 457 : GDALPDFStreamPoppler(Stream *poStream) : m_poStream(poStream)
1022 : {
1023 457 : }
1024 :
1025 : int64_t GetLength(int64_t nMaxSize = 0) override;
1026 : char *GetBytes() override;
1027 :
1028 : int64_t GetRawLength() override;
1029 : char *GetRawBytes() override;
1030 : };
1031 :
1032 : /************************************************************************/
1033 : /* ==================================================================== */
1034 : /* GDALPDFObjectPoppler */
1035 : /* ==================================================================== */
1036 : /************************************************************************/
1037 :
1038 : /************************************************************************/
1039 : /* ~GDALPDFObjectPoppler() */
1040 : /************************************************************************/
1041 :
1042 30952 : GDALPDFObjectPoppler::~GDALPDFObjectPoppler()
1043 : {
1044 15585 : delete m_poToDestroy;
1045 15585 : delete m_poDict;
1046 15585 : delete m_poArray;
1047 15585 : delete m_poStream;
1048 30952 : }
1049 :
1050 : /************************************************************************/
1051 : /* GetType() */
1052 : /************************************************************************/
1053 :
1054 60939 : GDALPDFObjectType GDALPDFObjectPoppler::GetType()
1055 : {
1056 60939 : switch (m_poConst->getType())
1057 : {
1058 179 : case objNull:
1059 179 : return PDFObjectType_Null;
1060 0 : case objBool:
1061 0 : return PDFObjectType_Bool;
1062 17075 : case objInt:
1063 17075 : return PDFObjectType_Int;
1064 4844 : case objReal:
1065 4844 : return PDFObjectType_Real;
1066 2727 : case objString:
1067 2727 : return PDFObjectType_String;
1068 6653 : case objName:
1069 6653 : return PDFObjectType_Name;
1070 13261 : case objArray:
1071 13261 : return PDFObjectType_Array;
1072 11992 : case objDict:
1073 11992 : return PDFObjectType_Dictionary;
1074 4208 : case objStream:
1075 4208 : return PDFObjectType_Dictionary;
1076 0 : default:
1077 0 : return PDFObjectType_Unknown;
1078 : }
1079 : }
1080 :
1081 : /************************************************************************/
1082 : /* GetTypeNameNative() */
1083 : /************************************************************************/
1084 :
1085 0 : const char *GDALPDFObjectPoppler::GetTypeNameNative()
1086 : {
1087 0 : return m_poConst->getTypeName();
1088 : }
1089 :
1090 : /************************************************************************/
1091 : /* GetBool() */
1092 : /************************************************************************/
1093 :
1094 0 : int GDALPDFObjectPoppler::GetBool()
1095 : {
1096 0 : if (GetType() == PDFObjectType_Bool)
1097 0 : return m_poConst->getBool();
1098 : else
1099 0 : return 0;
1100 : }
1101 :
1102 : /************************************************************************/
1103 : /* GetInt() */
1104 : /************************************************************************/
1105 :
1106 5455 : int GDALPDFObjectPoppler::GetInt()
1107 : {
1108 5455 : if (GetType() == PDFObjectType_Int)
1109 5455 : return m_poConst->getInt();
1110 : else
1111 0 : return 0;
1112 : }
1113 :
1114 : /************************************************************************/
1115 : /* GetReal() */
1116 : /************************************************************************/
1117 :
1118 1206 : double GDALPDFObjectPoppler::GetReal()
1119 : {
1120 1206 : if (GetType() == PDFObjectType_Real)
1121 1206 : return m_poConst->getReal();
1122 : else
1123 0 : return 0.0;
1124 : }
1125 :
1126 : /************************************************************************/
1127 : /* GetString() */
1128 : /************************************************************************/
1129 :
1130 1389 : const std::string &GDALPDFObjectPoppler::GetString()
1131 : {
1132 1389 : if (GetType() == PDFObjectType_String)
1133 : {
1134 : const std::string &osStdStr = *(
1135 1389 : [](auto &&obj) -> const std::string *
1136 : {
1137 : if constexpr (std::is_same_v<decltype(obj),
1138 : const std::string &>)
1139 : {
1140 : // Since Poppler 26.04
1141 : return &obj;
1142 : }
1143 : else
1144 : {
1145 : static_assert(
1146 : std::is_same_v<decltype(obj), const GooString *&&>);
1147 : static_assert(std::is_same_v<decltype(obj->toStr()),
1148 : const std::string &>);
1149 1389 : return &(obj->toStr());
1150 : }
1151 1389 : }(m_poConst->getString()));
1152 : const bool bLEUnicodeMarker =
1153 1735 : osStdStr.size() >= 2 && static_cast<uint8_t>(osStdStr[0]) == 0xFE &&
1154 346 : static_cast<uint8_t>(osStdStr[1]) == 0xFF;
1155 : const bool bBEUnicodeMarker =
1156 1479 : osStdStr.size() >= 2 && static_cast<uint8_t>(osStdStr[0]) == 0xFF &&
1157 90 : static_cast<uint8_t>(osStdStr[1]) == 0xFE;
1158 1389 : if (!bLEUnicodeMarker && !bBEUnicodeMarker)
1159 : {
1160 953 : if (CPLIsUTF8(osStdStr.c_str(), -1))
1161 : {
1162 953 : return osStdStr;
1163 : }
1164 : else
1165 : {
1166 : char *pszUTF8 =
1167 0 : CPLRecode(osStdStr.data(), CPL_ENC_ISO8859_1, CPL_ENC_UTF8);
1168 0 : osStr = pszUTF8;
1169 0 : CPLFree(pszUTF8);
1170 0 : return osStr;
1171 : }
1172 : }
1173 1308 : return (osStr = GDALPDFGetUTF8StringFromBytes(
1174 436 : reinterpret_cast<const GByte *>(osStdStr.data()),
1175 436 : osStdStr.size()));
1176 : }
1177 : else
1178 0 : return (osStr = "");
1179 : }
1180 :
1181 : /************************************************************************/
1182 : /* GetName() */
1183 : /************************************************************************/
1184 :
1185 2931 : const std::string &GDALPDFObjectPoppler::GetName()
1186 : {
1187 2931 : if (GetType() == PDFObjectType_Name)
1188 2931 : return (osStr = m_poConst->getName());
1189 : else
1190 0 : return (osStr = "");
1191 : }
1192 :
1193 : /************************************************************************/
1194 : /* GetDictionary() */
1195 : /************************************************************************/
1196 :
1197 7525 : GDALPDFDictionary *GDALPDFObjectPoppler::GetDictionary()
1198 : {
1199 7525 : if (GetType() != PDFObjectType_Dictionary)
1200 0 : return nullptr;
1201 :
1202 7525 : if (m_poDict)
1203 2798 : return m_poDict;
1204 :
1205 4727 : Dict *poDict = (m_poConst->getType() == objStream)
1206 4727 : ? m_poConst->getStream()->getDict()
1207 3666 : : m_poConst->getDict();
1208 4727 : if (poDict == nullptr)
1209 0 : return nullptr;
1210 4727 : m_poDict = new GDALPDFDictionaryPoppler(poDict);
1211 4727 : return m_poDict;
1212 : }
1213 :
1214 : /************************************************************************/
1215 : /* GetArray() */
1216 : /************************************************************************/
1217 :
1218 6611 : GDALPDFArray *GDALPDFObjectPoppler::GetArray()
1219 : {
1220 6611 : if (GetType() != PDFObjectType_Array)
1221 0 : return nullptr;
1222 :
1223 6611 : if (m_poArray)
1224 5387 : return m_poArray;
1225 :
1226 1224 : Array *poArray = m_poConst->getArray();
1227 1224 : if (poArray == nullptr)
1228 0 : return nullptr;
1229 1224 : m_poArray = new GDALPDFArrayPoppler(poArray);
1230 1224 : return m_poArray;
1231 : }
1232 :
1233 : /************************************************************************/
1234 : /* GetStream() */
1235 : /************************************************************************/
1236 :
1237 604 : GDALPDFStream *GDALPDFObjectPoppler::GetStream()
1238 : {
1239 604 : if (m_poConst->getType() != objStream)
1240 14 : return nullptr;
1241 :
1242 590 : if (m_poStream)
1243 133 : return m_poStream;
1244 457 : m_poStream = new GDALPDFStreamPoppler(m_poConst->getStream());
1245 457 : return m_poStream;
1246 : }
1247 :
1248 : /************************************************************************/
1249 : /* SetRefNumAndGen() */
1250 : /************************************************************************/
1251 :
1252 15149 : void GDALPDFObjectPoppler::SetRefNumAndGen(const GDALPDFObjectNum &nNum,
1253 : int nGen)
1254 : {
1255 15149 : m_nRefNum = nNum;
1256 15149 : m_nRefGen = nGen;
1257 15149 : }
1258 :
1259 : /************************************************************************/
1260 : /* GetRefNum() */
1261 : /************************************************************************/
1262 :
1263 2631 : GDALPDFObjectNum GDALPDFObjectPoppler::GetRefNum()
1264 : {
1265 2631 : return m_nRefNum;
1266 : }
1267 :
1268 : /************************************************************************/
1269 : /* GetRefGen() */
1270 : /************************************************************************/
1271 :
1272 1655 : int GDALPDFObjectPoppler::GetRefGen()
1273 : {
1274 1655 : return m_nRefGen;
1275 : }
1276 :
1277 : /************************************************************************/
1278 : /* ==================================================================== */
1279 : /* GDALPDFDictionaryPoppler */
1280 : /* ==================================================================== */
1281 : /************************************************************************/
1282 :
1283 : /************************************************************************/
1284 : /* ~GDALPDFDictionaryPoppler() */
1285 : /************************************************************************/
1286 :
1287 9454 : GDALPDFDictionaryPoppler::~GDALPDFDictionaryPoppler()
1288 : {
1289 4727 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.begin();
1290 4727 : std::map<CPLString, GDALPDFObject *>::iterator oEnd = m_map.end();
1291 14190 : for (; oIter != oEnd; ++oIter)
1292 9463 : delete oIter->second;
1293 9454 : }
1294 :
1295 : /************************************************************************/
1296 : /* Get() */
1297 : /************************************************************************/
1298 :
1299 14285 : GDALPDFObject *GDALPDFDictionaryPoppler::Get(const char *pszKey)
1300 : {
1301 14285 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.find(pszKey);
1302 14285 : if (oIter != m_map.end())
1303 2432 : return oIter->second;
1304 :
1305 11853 : auto &&o(m_poDict->lookupNF(pszKey));
1306 11853 : if (!o.isNull())
1307 : {
1308 9463 : GDALPDFObjectNum nRefNum;
1309 9463 : int nRefGen = 0;
1310 9463 : if (o.isRef())
1311 : {
1312 3009 : nRefNum = o.getRefNum();
1313 3009 : nRefGen = o.getRefGen();
1314 3009 : Object o2(m_poDict->lookup(pszKey));
1315 3009 : if (!o2.isNull())
1316 : {
1317 : GDALPDFObjectPoppler *poObj =
1318 3009 : new GDALPDFObjectPoppler(new Object(std::move(o2)), TRUE);
1319 3009 : poObj->SetRefNumAndGen(nRefNum, nRefGen);
1320 3009 : m_map[pszKey] = poObj;
1321 3009 : return poObj;
1322 : }
1323 : }
1324 : else
1325 : {
1326 : GDALPDFObjectPoppler *poObj =
1327 6454 : new GDALPDFObjectPoppler(new Object(o.copy()), TRUE);
1328 6454 : poObj->SetRefNumAndGen(nRefNum, nRefGen);
1329 6454 : m_map[pszKey] = poObj;
1330 6454 : return poObj;
1331 : }
1332 : }
1333 2390 : return nullptr;
1334 : }
1335 :
1336 : /************************************************************************/
1337 : /* GetValues() */
1338 : /************************************************************************/
1339 :
1340 476 : std::map<CPLString, GDALPDFObject *> &GDALPDFDictionaryPoppler::GetValues()
1341 : {
1342 476 : int i = 0;
1343 476 : int nLength = m_poDict->getLength();
1344 2341 : for (i = 0; i < nLength; i++)
1345 : {
1346 : #if POPPLER_MAJOR_VERSION > 26 || \
1347 : (POPPLER_MAJOR_VERSION == 26 && POPPLER_MINOR_VERSION > 5) || \
1348 : (POPPLER_MAJOR_VERSION == 26 && POPPLER_MINOR_VERSION == 5 && \
1349 : POPPLER_MICRO_VERSION > 0)
1350 : Get(m_poDict->getKey(i).c_str());
1351 : #else
1352 1865 : const char *pszKey = m_poDict->getKey(i);
1353 1865 : Get(pszKey);
1354 : #endif
1355 : }
1356 476 : return m_map;
1357 : }
1358 :
1359 : /************************************************************************/
1360 : /* ==================================================================== */
1361 : /* GDALPDFArrayPoppler */
1362 : /* ==================================================================== */
1363 : /************************************************************************/
1364 :
1365 : /************************************************************************/
1366 : /* GDALPDFCreateArray() */
1367 : /************************************************************************/
1368 :
1369 62 : GDALPDFArray *GDALPDFCreateArray(const Array *array)
1370 : {
1371 62 : return new GDALPDFArrayPoppler(array);
1372 : }
1373 :
1374 : /************************************************************************/
1375 : /* GetLength() */
1376 : /************************************************************************/
1377 :
1378 9431 : int GDALPDFArrayPoppler::GetLength()
1379 : {
1380 9431 : return m_poArray->getLength();
1381 : }
1382 :
1383 : /************************************************************************/
1384 : /* Get() */
1385 : /************************************************************************/
1386 :
1387 6345 : GDALPDFObject *GDALPDFArrayPoppler::Get(int nIndex)
1388 : {
1389 6345 : if (nIndex < 0 || nIndex >= GetLength())
1390 0 : return nullptr;
1391 :
1392 6345 : if (m_v.empty())
1393 1251 : m_v.resize(GetLength());
1394 :
1395 6345 : if (m_v[nIndex] != nullptr)
1396 877 : return m_v[nIndex].get();
1397 :
1398 5468 : auto &&o(m_poArray->getNF(nIndex));
1399 5468 : if (!o.isNull())
1400 : {
1401 5468 : GDALPDFObjectNum nRefNum;
1402 5468 : int nRefGen = 0;
1403 5468 : if (o.isRef())
1404 : {
1405 868 : nRefNum = o.getRefNum();
1406 868 : nRefGen = o.getRefGen();
1407 868 : Object o2(m_poArray->get(nIndex));
1408 868 : if (!o2.isNull())
1409 : {
1410 : auto poObj = std::make_unique<GDALPDFObjectPoppler>(
1411 1736 : new Object(std::move(o2)), TRUE);
1412 868 : poObj->SetRefNumAndGen(nRefNum, nRefGen);
1413 868 : m_v[nIndex] = std::move(poObj);
1414 868 : return m_v[nIndex].get();
1415 : }
1416 : }
1417 : else
1418 : {
1419 : auto poObj = std::make_unique<GDALPDFObjectPoppler>(
1420 9200 : new Object(o.copy()), TRUE);
1421 4600 : poObj->SetRefNumAndGen(nRefNum, nRefGen);
1422 4600 : m_v[nIndex] = std::move(poObj);
1423 4600 : return m_v[nIndex].get();
1424 : }
1425 : }
1426 0 : return nullptr;
1427 : }
1428 :
1429 : /************************************************************************/
1430 : /* ==================================================================== */
1431 : /* GDALPDFStreamPoppler */
1432 : /* ==================================================================== */
1433 : /************************************************************************/
1434 :
1435 : /************************************************************************/
1436 : /* GetLength() */
1437 : /************************************************************************/
1438 :
1439 555 : int64_t GDALPDFStreamPoppler::GetLength(int64_t nMaxSize)
1440 : {
1441 555 : if (m_nLength >= 0)
1442 292 : return m_nLength;
1443 :
1444 : #if POPPLER_MAJOR_VERSION > 25
1445 : if (!m_poStream->rewind())
1446 : return 0;
1447 : #elif POPPLER_MAJOR_VERSION == 25 && POPPLER_MINOR_VERSION >= 2
1448 : if (!m_poStream->reset())
1449 : return 0;
1450 : #else
1451 263 : m_poStream->reset();
1452 : #endif
1453 263 : m_nLength = 0;
1454 : unsigned char readBuf[4096];
1455 : int readChars;
1456 526 : while ((readChars = m_poStream->doGetChars(4096, readBuf)) != 0)
1457 : {
1458 263 : m_nLength += readChars;
1459 263 : if (nMaxSize != 0 && m_nLength > nMaxSize)
1460 : {
1461 0 : m_nLength = -1;
1462 0 : return std::numeric_limits<int64_t>::max();
1463 : }
1464 : }
1465 263 : return m_nLength;
1466 : }
1467 :
1468 : /************************************************************************/
1469 : /* GooStringToCharStart() */
1470 : /************************************************************************/
1471 :
1472 529 : static char *GooStringToCharStart(GooString &gstr)
1473 : {
1474 : #if POPPLER_MAJOR_VERSION > 25 || \
1475 : (POPPLER_MAJOR_VERSION == 25 && POPPLER_MINOR_VERSION >= 10)
1476 : const auto nLength = gstr.size();
1477 : #else
1478 529 : const auto nLength = gstr.getLength();
1479 : #endif
1480 529 : if (nLength)
1481 : {
1482 527 : char *pszContent = static_cast<char *>(VSI_MALLOC_VERBOSE(nLength + 1));
1483 527 : if (pszContent)
1484 : {
1485 527 : const char *srcStr = gstr.c_str();
1486 527 : memcpy(pszContent, srcStr, nLength);
1487 527 : pszContent[nLength] = '\0';
1488 : }
1489 527 : return pszContent;
1490 : }
1491 2 : return nullptr;
1492 : }
1493 :
1494 : /************************************************************************/
1495 : /* GetBytes() */
1496 : /************************************************************************/
1497 :
1498 527 : char *GDALPDFStreamPoppler::GetBytes()
1499 : {
1500 1054 : GooString gstr;
1501 : try
1502 : {
1503 527 : m_poStream->fillGooString(&gstr);
1504 : }
1505 0 : catch (const std::exception &e)
1506 : {
1507 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
1508 0 : "GDALPDFStreamPoppler::GetBytes(): %s", e.what());
1509 0 : return nullptr;
1510 : }
1511 527 : m_nLength = static_cast<int64_t>(gstr.toStr().size());
1512 527 : return GooStringToCharStart(gstr);
1513 : }
1514 :
1515 : /************************************************************************/
1516 : /* GetRawLength() */
1517 : /************************************************************************/
1518 :
1519 63 : int64_t GDALPDFStreamPoppler::GetRawLength()
1520 : {
1521 63 : if (m_nRawLength >= 0)
1522 2 : return m_nRawLength;
1523 :
1524 61 : auto undecodeStream = m_poStream->getUndecodedStream();
1525 : #if POPPLER_MAJOR_VERSION > 25
1526 : if (!undecodeStream->rewind())
1527 : return 0;
1528 : #elif POPPLER_MAJOR_VERSION == 25 && POPPLER_MINOR_VERSION >= 2
1529 : if (!undecodeStream->reset())
1530 : return 0;
1531 : #else
1532 61 : undecodeStream->reset();
1533 : #endif
1534 61 : m_nRawLength = 0;
1535 25531 : while (undecodeStream->getChar() != EOF)
1536 25470 : m_nRawLength++;
1537 61 : return m_nRawLength;
1538 : }
1539 :
1540 : /************************************************************************/
1541 : /* GetRawBytes() */
1542 : /************************************************************************/
1543 :
1544 2 : char *GDALPDFStreamPoppler::GetRawBytes()
1545 : {
1546 4 : GooString gstr;
1547 2 : auto undecodeStream = m_poStream->getUndecodedStream();
1548 : try
1549 : {
1550 2 : undecodeStream->fillGooString(&gstr);
1551 : }
1552 0 : catch (const std::exception &e)
1553 : {
1554 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
1555 0 : "GDALPDFStreamPoppler::GetRawBytes(): %s", e.what());
1556 0 : return nullptr;
1557 : }
1558 : #if POPPLER_MAJOR_VERSION > 25 || \
1559 : (POPPLER_MAJOR_VERSION == 25 && POPPLER_MINOR_VERSION >= 10)
1560 : m_nRawLength = gstr.size();
1561 : #else
1562 2 : m_nRawLength = gstr.getLength();
1563 : #endif
1564 2 : return GooStringToCharStart(gstr);
1565 : }
1566 :
1567 : #endif // HAVE_POPPLER
1568 :
1569 : #ifdef HAVE_PODOFO
1570 :
1571 : /************************************************************************/
1572 : /* ==================================================================== */
1573 : /* GDALPDFDictionaryPodofo */
1574 : /* ==================================================================== */
1575 : /************************************************************************/
1576 :
1577 : class GDALPDFDictionaryPodofo final : public GDALPDFDictionary
1578 : {
1579 : private:
1580 : const PoDoFo::PdfDictionary *m_poDict;
1581 : const PoDoFo::PdfVecObjects &m_poObjects;
1582 : std::map<CPLString, GDALPDFObject *> m_map{};
1583 :
1584 : CPL_DISALLOW_COPY_ASSIGN(GDALPDFDictionaryPodofo)
1585 :
1586 : public:
1587 : GDALPDFDictionaryPodofo(const PoDoFo::PdfDictionary *poDict,
1588 : const PoDoFo::PdfVecObjects &poObjects)
1589 : : m_poDict(poDict), m_poObjects(poObjects)
1590 : {
1591 : }
1592 :
1593 : ~GDALPDFDictionaryPodofo() override;
1594 :
1595 : GDALPDFObject *Get(const char *pszKey) override;
1596 : std::map<CPLString, GDALPDFObject *> &GetValues() override;
1597 : };
1598 :
1599 : /************************************************************************/
1600 : /* ==================================================================== */
1601 : /* GDALPDFArrayPodofo */
1602 : /* ==================================================================== */
1603 : /************************************************************************/
1604 :
1605 : class GDALPDFArrayPodofo final : public GDALPDFArray
1606 : {
1607 : private:
1608 : const PoDoFo::PdfArray *m_poArray;
1609 : const PoDoFo::PdfVecObjects &m_poObjects;
1610 : std::vector<std::unique_ptr<GDALPDFObject>> m_v{};
1611 :
1612 : CPL_DISALLOW_COPY_ASSIGN(GDALPDFArrayPodofo)
1613 :
1614 : public:
1615 : GDALPDFArrayPodofo(const PoDoFo::PdfArray *poArray,
1616 : const PoDoFo::PdfVecObjects &poObjects)
1617 : : m_poArray(poArray), m_poObjects(poObjects)
1618 : {
1619 : }
1620 :
1621 : int GetLength() override;
1622 : GDALPDFObject *Get(int nIndex) override;
1623 : };
1624 :
1625 : /************************************************************************/
1626 : /* ==================================================================== */
1627 : /* GDALPDFStreamPodofo */
1628 : /* ==================================================================== */
1629 : /************************************************************************/
1630 :
1631 : class GDALPDFStreamPodofo final : public GDALPDFStream
1632 : {
1633 : private:
1634 : #if PODOFO_VERSION_MAJOR > 0 || \
1635 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1636 : const PoDoFo::PdfObjectStream *m_pStream;
1637 : #else
1638 : const PoDoFo::PdfStream *m_pStream;
1639 : #endif
1640 :
1641 : CPL_DISALLOW_COPY_ASSIGN(GDALPDFStreamPodofo)
1642 :
1643 : public:
1644 : GDALPDFStreamPodofo(
1645 : #if PODOFO_VERSION_MAJOR > 0 || \
1646 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1647 : const PoDoFo::PdfObjectStream *
1648 : #else
1649 : const PoDoFo::PdfStream *
1650 : #endif
1651 : pStream)
1652 : : m_pStream(pStream)
1653 : {
1654 : }
1655 :
1656 : int64_t GetLength(int64_t nMaxSize = 0) override;
1657 : char *GetBytes() override;
1658 :
1659 : int64_t GetRawLength() override;
1660 : char *GetRawBytes() override;
1661 : };
1662 :
1663 : /************************************************************************/
1664 : /* ==================================================================== */
1665 : /* GDALPDFObjectPodofo */
1666 : /* ==================================================================== */
1667 : /************************************************************************/
1668 :
1669 : /************************************************************************/
1670 : /* GDALPDFObjectPodofo() */
1671 : /************************************************************************/
1672 :
1673 : GDALPDFObjectPodofo::GDALPDFObjectPodofo(const PoDoFo::PdfObject *po,
1674 : const PoDoFo::PdfVecObjects &poObjects)
1675 : : m_po(po), m_poObjects(poObjects)
1676 : {
1677 : try
1678 : {
1679 : #if PODOFO_VERSION_MAJOR > 0 || \
1680 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1681 : if (m_po->GetDataType() == PoDoFo::PdfDataType::Reference)
1682 : {
1683 : PoDoFo::PdfObject *poObj =
1684 : m_poObjects.GetObject(m_po->GetReference());
1685 : if (poObj)
1686 : m_po = poObj;
1687 : }
1688 : #else
1689 : if (m_po->GetDataType() == PoDoFo::ePdfDataType_Reference)
1690 : {
1691 : PoDoFo::PdfObject *poObj =
1692 : m_poObjects.GetObject(m_po->GetReference());
1693 : if (poObj)
1694 : m_po = poObj;
1695 : }
1696 : #endif
1697 : }
1698 : catch (PoDoFo::PdfError &oError)
1699 : {
1700 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid PDF : %s",
1701 : oError.what());
1702 : }
1703 : }
1704 :
1705 : /************************************************************************/
1706 : /* ~GDALPDFObjectPodofo() */
1707 : /************************************************************************/
1708 :
1709 : GDALPDFObjectPodofo::~GDALPDFObjectPodofo()
1710 : {
1711 : delete m_poDict;
1712 : delete m_poArray;
1713 : delete m_poStream;
1714 : }
1715 :
1716 : /************************************************************************/
1717 : /* GetType() */
1718 : /************************************************************************/
1719 :
1720 : GDALPDFObjectType GDALPDFObjectPodofo::GetType()
1721 : {
1722 : try
1723 : {
1724 : switch (m_po->GetDataType())
1725 : {
1726 : #if PODOFO_VERSION_MAJOR > 0 || \
1727 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1728 : case PoDoFo::PdfDataType::Null:
1729 : return PDFObjectType_Null;
1730 : case PoDoFo::PdfDataType::Bool:
1731 : return PDFObjectType_Bool;
1732 : case PoDoFo::PdfDataType::Number:
1733 : return PDFObjectType_Int;
1734 : case PoDoFo::PdfDataType::Real:
1735 : return PDFObjectType_Real;
1736 : case PoDoFo::PdfDataType::String:
1737 : return PDFObjectType_String;
1738 : case PoDoFo::PdfDataType::Name:
1739 : return PDFObjectType_Name;
1740 : case PoDoFo::PdfDataType::Array:
1741 : return PDFObjectType_Array;
1742 : case PoDoFo::PdfDataType::Dictionary:
1743 : return PDFObjectType_Dictionary;
1744 : default:
1745 : return PDFObjectType_Unknown;
1746 : #else
1747 : case PoDoFo::ePdfDataType_Null:
1748 : return PDFObjectType_Null;
1749 : case PoDoFo::ePdfDataType_Bool:
1750 : return PDFObjectType_Bool;
1751 : case PoDoFo::ePdfDataType_Number:
1752 : return PDFObjectType_Int;
1753 : case PoDoFo::ePdfDataType_Real:
1754 : return PDFObjectType_Real;
1755 : case PoDoFo::ePdfDataType_HexString:
1756 : return PDFObjectType_String;
1757 : case PoDoFo::ePdfDataType_String:
1758 : return PDFObjectType_String;
1759 : case PoDoFo::ePdfDataType_Name:
1760 : return PDFObjectType_Name;
1761 : case PoDoFo::ePdfDataType_Array:
1762 : return PDFObjectType_Array;
1763 : case PoDoFo::ePdfDataType_Dictionary:
1764 : return PDFObjectType_Dictionary;
1765 : default:
1766 : return PDFObjectType_Unknown;
1767 : #endif
1768 : }
1769 : }
1770 : catch (PoDoFo::PdfError &oError)
1771 : {
1772 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid PDF : %s",
1773 : oError.what());
1774 : return PDFObjectType_Unknown;
1775 : }
1776 : }
1777 :
1778 : /************************************************************************/
1779 : /* GetTypeNameNative() */
1780 : /************************************************************************/
1781 :
1782 : const char *GDALPDFObjectPodofo::GetTypeNameNative()
1783 : {
1784 : try
1785 : {
1786 : osStr = m_po->GetDataTypeString();
1787 : return osStr.c_str();
1788 : }
1789 : catch (PoDoFo::PdfError &oError)
1790 : {
1791 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid PDF : %s",
1792 : oError.what());
1793 : return "unknown";
1794 : }
1795 : }
1796 :
1797 : /************************************************************************/
1798 : /* GetBool() */
1799 : /************************************************************************/
1800 :
1801 : int GDALPDFObjectPodofo::GetBool()
1802 : {
1803 : #if PODOFO_VERSION_MAJOR > 0 || \
1804 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1805 : if (m_po->GetDataType() == PoDoFo::PdfDataType::Bool)
1806 : #else
1807 : if (m_po->GetDataType() == PoDoFo::ePdfDataType_Bool)
1808 : #endif
1809 : return m_po->GetBool();
1810 : else
1811 : return 0;
1812 : }
1813 :
1814 : /************************************************************************/
1815 : /* GetInt() */
1816 : /************************************************************************/
1817 :
1818 : int GDALPDFObjectPodofo::GetInt()
1819 : {
1820 : #if PODOFO_VERSION_MAJOR > 0 || \
1821 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1822 : if (m_po->GetDataType() == PoDoFo::PdfDataType::Number)
1823 : #else
1824 : if (m_po->GetDataType() == PoDoFo::ePdfDataType_Number)
1825 : #endif
1826 : return static_cast<int>(m_po->GetNumber());
1827 : else
1828 : return 0;
1829 : }
1830 :
1831 : /************************************************************************/
1832 : /* GetReal() */
1833 : /************************************************************************/
1834 :
1835 : double GDALPDFObjectPodofo::GetReal()
1836 : {
1837 : if (GetType() == PDFObjectType_Real)
1838 : return m_po->GetReal();
1839 : else
1840 : return 0.0;
1841 : }
1842 :
1843 : /************************************************************************/
1844 : /* GetString() */
1845 : /************************************************************************/
1846 :
1847 : const std::string &GDALPDFObjectPodofo::GetString()
1848 : {
1849 : if (GetType() == PDFObjectType_String)
1850 : #if PODOFO_VERSION_MAJOR > 0 || \
1851 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1852 : return (osStr = m_po->GetString().GetString());
1853 : #else
1854 : return (osStr = m_po->GetString().GetStringUtf8());
1855 : #endif
1856 : else
1857 : return (osStr = "");
1858 : }
1859 :
1860 : /************************************************************************/
1861 : /* GetName() */
1862 : /************************************************************************/
1863 :
1864 : const std::string &GDALPDFObjectPodofo::GetName()
1865 : {
1866 : if (GetType() == PDFObjectType_Name)
1867 : #if PODOFO_VERSION_MAJOR > 0 || \
1868 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1869 : return (osStr = m_po->GetName().GetString());
1870 : #else
1871 : return (osStr = m_po->GetName().GetName());
1872 : #endif
1873 : else
1874 : return (osStr = "");
1875 : }
1876 :
1877 : /************************************************************************/
1878 : /* GetDictionary() */
1879 : /************************************************************************/
1880 :
1881 : GDALPDFDictionary *GDALPDFObjectPodofo::GetDictionary()
1882 : {
1883 : if (GetType() != PDFObjectType_Dictionary)
1884 : return nullptr;
1885 :
1886 : if (m_poDict)
1887 : return m_poDict;
1888 :
1889 : m_poDict = new GDALPDFDictionaryPodofo(&m_po->GetDictionary(), m_poObjects);
1890 : return m_poDict;
1891 : }
1892 :
1893 : /************************************************************************/
1894 : /* GetArray() */
1895 : /************************************************************************/
1896 :
1897 : GDALPDFArray *GDALPDFObjectPodofo::GetArray()
1898 : {
1899 : if (GetType() != PDFObjectType_Array)
1900 : return nullptr;
1901 :
1902 : if (m_poArray)
1903 : return m_poArray;
1904 :
1905 : m_poArray = new GDALPDFArrayPodofo(&m_po->GetArray(), m_poObjects);
1906 : return m_poArray;
1907 : }
1908 :
1909 : /************************************************************************/
1910 : /* GetStream() */
1911 : /************************************************************************/
1912 :
1913 : GDALPDFStream *GDALPDFObjectPodofo::GetStream()
1914 : {
1915 : try
1916 : {
1917 : if (!m_po->HasStream())
1918 : return nullptr;
1919 : }
1920 : catch (PoDoFo::PdfError &oError)
1921 : {
1922 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid object : %s",
1923 : oError.what());
1924 : return nullptr;
1925 : }
1926 : catch (...)
1927 : {
1928 : CPLError(CE_Failure, CPLE_AppDefined, "Invalid object");
1929 : return nullptr;
1930 : }
1931 :
1932 : if (m_poStream == nullptr)
1933 : m_poStream = new GDALPDFStreamPodofo(m_po->GetStream());
1934 : return m_poStream;
1935 : }
1936 :
1937 : /************************************************************************/
1938 : /* GetRefNum() */
1939 : /************************************************************************/
1940 :
1941 : GDALPDFObjectNum GDALPDFObjectPodofo::GetRefNum()
1942 : {
1943 : #if PODOFO_VERSION_MAJOR > 0 || \
1944 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1945 : return GDALPDFObjectNum(m_po->GetIndirectReference().ObjectNumber());
1946 : #else
1947 : return GDALPDFObjectNum(m_po->Reference().ObjectNumber());
1948 : #endif
1949 : }
1950 :
1951 : /************************************************************************/
1952 : /* GetRefGen() */
1953 : /************************************************************************/
1954 :
1955 : int GDALPDFObjectPodofo::GetRefGen()
1956 : {
1957 : #if PODOFO_VERSION_MAJOR > 0 || \
1958 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
1959 : return m_po->GetIndirectReference().GenerationNumber();
1960 : #else
1961 : return m_po->Reference().GenerationNumber();
1962 : #endif
1963 : }
1964 :
1965 : /************************************************************************/
1966 : /* ==================================================================== */
1967 : /* GDALPDFDictionaryPodofo */
1968 : /* ==================================================================== */
1969 : /************************************************************************/
1970 :
1971 : /************************************************************************/
1972 : /* ~GDALPDFDictionaryPodofo() */
1973 : /************************************************************************/
1974 :
1975 : GDALPDFDictionaryPodofo::~GDALPDFDictionaryPodofo()
1976 : {
1977 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.begin();
1978 : std::map<CPLString, GDALPDFObject *>::iterator oEnd = m_map.end();
1979 : for (; oIter != oEnd; ++oIter)
1980 : delete oIter->second;
1981 : }
1982 :
1983 : /************************************************************************/
1984 : /* Get() */
1985 : /************************************************************************/
1986 :
1987 : GDALPDFObject *GDALPDFDictionaryPodofo::Get(const char *pszKey)
1988 : {
1989 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.find(pszKey);
1990 : if (oIter != m_map.end())
1991 : return oIter->second;
1992 :
1993 : const PoDoFo::PdfObject *poVal = m_poDict->GetKey(PoDoFo::PdfName(pszKey));
1994 : if (poVal)
1995 : {
1996 : GDALPDFObjectPodofo *poObj =
1997 : new GDALPDFObjectPodofo(poVal, m_poObjects);
1998 : m_map[pszKey] = poObj;
1999 : return poObj;
2000 : }
2001 : else
2002 : {
2003 : return nullptr;
2004 : }
2005 : }
2006 :
2007 : /************************************************************************/
2008 : /* GetValues() */
2009 : /************************************************************************/
2010 :
2011 : std::map<CPLString, GDALPDFObject *> &GDALPDFDictionaryPodofo::GetValues()
2012 : {
2013 : #if PODOFO_VERSION_MAJOR > 0 || \
2014 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
2015 : for (const auto &oIter : *m_poDict)
2016 : {
2017 : const std::string osTmp(oIter.first.GetString());
2018 : Get(osTmp.c_str());
2019 : }
2020 : #else
2021 : for (const auto &oIter : m_poDict->GetKeys())
2022 : {
2023 : Get(oIter.first.GetName().c_str());
2024 : }
2025 : #endif
2026 : return m_map;
2027 : }
2028 :
2029 : /************************************************************************/
2030 : /* ==================================================================== */
2031 : /* GDALPDFArrayPodofo */
2032 : /* ==================================================================== */
2033 : /************************************************************************/
2034 :
2035 : /************************************************************************/
2036 : /* GetLength() */
2037 : /************************************************************************/
2038 :
2039 : int GDALPDFArrayPodofo::GetLength()
2040 : {
2041 : return static_cast<int>(m_poArray->GetSize());
2042 : }
2043 :
2044 : /************************************************************************/
2045 : /* Get() */
2046 : /************************************************************************/
2047 :
2048 : GDALPDFObject *GDALPDFArrayPodofo::Get(int nIndex)
2049 : {
2050 : if (nIndex < 0 || nIndex >= GetLength())
2051 : return nullptr;
2052 :
2053 : if (m_v.empty())
2054 : m_v.resize(GetLength());
2055 :
2056 : if (m_v[nIndex] != nullptr)
2057 : return m_v[nIndex].get();
2058 :
2059 : const PoDoFo::PdfObject &oVal = (*m_poArray)[nIndex];
2060 : m_v[nIndex] = std::make_unique<GDALPDFObjectPodofo>(&oVal, m_poObjects);
2061 : return m_v[nIndex].get();
2062 : }
2063 :
2064 : /************************************************************************/
2065 : /* ==================================================================== */
2066 : /* GDALPDFStreamPodofo */
2067 : /* ==================================================================== */
2068 : /************************************************************************/
2069 :
2070 : /************************************************************************/
2071 : /* GetLength() */
2072 : /************************************************************************/
2073 :
2074 : int64_t GDALPDFStreamPodofo::GetLength(int64_t /* nMaxSize */)
2075 : {
2076 : #if PODOFO_VERSION_MAJOR > 0 || \
2077 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
2078 : PoDoFo::charbuff str;
2079 : try
2080 : {
2081 : m_pStream->CopyToSafe(str);
2082 : }
2083 : catch (PoDoFo::PdfError &e)
2084 : {
2085 : CPLError(CE_Failure, CPLE_AppDefined, "CopyToSafe() failed: %s",
2086 : e.what());
2087 : return 0;
2088 : }
2089 : return static_cast<int64_t>(str.size());
2090 : #else
2091 : char *pBuffer = nullptr;
2092 : PoDoFo::pdf_long nLen = 0;
2093 : try
2094 : {
2095 : m_pStream->GetFilteredCopy(&pBuffer, &nLen);
2096 : PoDoFo::podofo_free(pBuffer);
2097 : return static_cast<int64_t>(nLen);
2098 : }
2099 : catch (PoDoFo::PdfError &e)
2100 : {
2101 : }
2102 : return 0;
2103 : #endif
2104 : }
2105 :
2106 : /************************************************************************/
2107 : /* GetBytes() */
2108 : /************************************************************************/
2109 :
2110 : char *GDALPDFStreamPodofo::GetBytes()
2111 : {
2112 : #if PODOFO_VERSION_MAJOR > 0 || \
2113 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
2114 : PoDoFo::charbuff str;
2115 : try
2116 : {
2117 : m_pStream->CopyToSafe(str);
2118 : }
2119 : catch (PoDoFo::PdfError &e)
2120 : {
2121 : CPLError(CE_Failure, CPLE_AppDefined, "CopyToSafe() failed: %s",
2122 : e.what());
2123 : return nullptr;
2124 : }
2125 : char *pszContent = static_cast<char *>(VSI_MALLOC_VERBOSE(str.size() + 1));
2126 : if (!pszContent)
2127 : {
2128 : return nullptr;
2129 : }
2130 : memcpy(pszContent, str.data(), str.size());
2131 : pszContent[str.size()] = '\0';
2132 : return pszContent;
2133 : #else
2134 : char *pBuffer = nullptr;
2135 : PoDoFo::pdf_long nLen = 0;
2136 : try
2137 : {
2138 : m_pStream->GetFilteredCopy(&pBuffer, &nLen);
2139 : }
2140 : catch (PoDoFo::PdfError &e)
2141 : {
2142 : return nullptr;
2143 : }
2144 : char *pszContent = static_cast<char *>(VSI_MALLOC_VERBOSE(nLen + 1));
2145 : if (!pszContent)
2146 : {
2147 : PoDoFo::podofo_free(pBuffer);
2148 : return nullptr;
2149 : }
2150 : memcpy(pszContent, pBuffer, nLen);
2151 : PoDoFo::podofo_free(pBuffer);
2152 : pszContent[nLen] = '\0';
2153 : return pszContent;
2154 : #endif
2155 : }
2156 :
2157 : /************************************************************************/
2158 : /* GetRawLength() */
2159 : /************************************************************************/
2160 :
2161 : int64_t GDALPDFStreamPodofo::GetRawLength()
2162 : {
2163 : try
2164 : {
2165 : auto nLen = m_pStream->GetLength();
2166 : return static_cast<int64_t>(nLen);
2167 : }
2168 : catch (PoDoFo::PdfError &e)
2169 : {
2170 : }
2171 : return 0;
2172 : }
2173 :
2174 : /************************************************************************/
2175 : /* GetRawBytes() */
2176 : /************************************************************************/
2177 :
2178 : char *GDALPDFStreamPodofo::GetRawBytes()
2179 : {
2180 : #if PODOFO_VERSION_MAJOR > 0 || \
2181 : (PODOFO_VERSION_MAJOR == 0 && PODOFO_VERSION_MINOR >= 10)
2182 : PoDoFo::charbuff str;
2183 : try
2184 : {
2185 : PoDoFo::StringStreamDevice stream(str);
2186 : #ifdef USE_HACK_BECAUSE_PdfInputStream_constructor_is_not_exported_in_podofo_0_11
2187 : auto *poNonConstStream =
2188 : const_cast<PoDoFo::PdfObjectStream *>(m_pStream);
2189 : auto inputStream = poNonConstStream->GetProvider().GetInputStream(
2190 : poNonConstStream->GetParent());
2191 : inputStream->CopyTo(stream);
2192 : #else
2193 : // Should work but fails to link because PdfInputStream destructor
2194 : // is not exported
2195 : auto inputStream = m_pStream->GetInputStream(/*raw=*/true);
2196 : inputStream.CopyTo(stream);
2197 : #endif
2198 : stream.Flush();
2199 : }
2200 : catch (PoDoFo::PdfError &e)
2201 : {
2202 : CPLError(CE_Failure, CPLE_AppDefined, "CopyToSafe() failed: %s",
2203 : e.what());
2204 : return nullptr;
2205 : }
2206 : char *pszContent = static_cast<char *>(VSI_MALLOC_VERBOSE(str.size() + 1));
2207 : if (!pszContent)
2208 : {
2209 : return nullptr;
2210 : }
2211 : memcpy(pszContent, str.data(), str.size());
2212 : pszContent[str.size()] = '\0';
2213 : return pszContent;
2214 : #else
2215 : char *pBuffer = nullptr;
2216 : PoDoFo::pdf_long nLen = 0;
2217 : try
2218 : {
2219 : m_pStream->GetCopy(&pBuffer, &nLen);
2220 : }
2221 : catch (PoDoFo::PdfError &e)
2222 : {
2223 : return nullptr;
2224 : }
2225 : char *pszContent = static_cast<char *>(VSI_MALLOC_VERBOSE(nLen + 1));
2226 : if (!pszContent)
2227 : {
2228 : PoDoFo::podofo_free(pBuffer);
2229 : return nullptr;
2230 : }
2231 : memcpy(pszContent, pBuffer, nLen);
2232 : PoDoFo::podofo_free(pBuffer);
2233 : pszContent[nLen] = '\0';
2234 : return pszContent;
2235 : #endif
2236 : }
2237 :
2238 : #endif // HAVE_PODOFO
2239 :
2240 : #ifdef HAVE_PDFIUM
2241 :
2242 : /************************************************************************/
2243 : /* ==================================================================== */
2244 : /* GDALPDFDictionaryPdfium */
2245 : /* ==================================================================== */
2246 : /************************************************************************/
2247 :
2248 : class GDALPDFDictionaryPdfium final : public GDALPDFDictionary
2249 : {
2250 : private:
2251 : RetainPtr<const CPDF_Dictionary> m_poDict;
2252 : std::map<CPLString, GDALPDFObject *> m_map{};
2253 :
2254 : public:
2255 : GDALPDFDictionaryPdfium(RetainPtr<const CPDF_Dictionary> poDict)
2256 : : m_poDict(std::move(poDict))
2257 : {
2258 : }
2259 :
2260 : ~GDALPDFDictionaryPdfium() override;
2261 :
2262 : GDALPDFObject *Get(const char *pszKey) override;
2263 : std::map<CPLString, GDALPDFObject *> &GetValues() override;
2264 : };
2265 :
2266 : /************************************************************************/
2267 : /* ==================================================================== */
2268 : /* GDALPDFArrayPdfium */
2269 : /* ==================================================================== */
2270 : /************************************************************************/
2271 :
2272 : class GDALPDFArrayPdfium final : public GDALPDFArray
2273 : {
2274 : private:
2275 : const CPDF_Array *m_poArray;
2276 : std::vector<std::unique_ptr<GDALPDFObject>> m_v{};
2277 :
2278 : CPL_DISALLOW_COPY_ASSIGN(GDALPDFArrayPdfium)
2279 :
2280 : public:
2281 : GDALPDFArrayPdfium(const CPDF_Array *poArray) : m_poArray(poArray)
2282 : {
2283 : }
2284 :
2285 : int GetLength() override;
2286 : GDALPDFObject *Get(int nIndex) override;
2287 : };
2288 :
2289 : /************************************************************************/
2290 : /* ==================================================================== */
2291 : /* GDALPDFStreamPdfium */
2292 : /* ==================================================================== */
2293 : /************************************************************************/
2294 :
2295 : class GDALPDFStreamPdfium final : public GDALPDFStream
2296 : {
2297 : private:
2298 : RetainPtr<const CPDF_Stream> m_pStream;
2299 : int64_t m_nSize = 0;
2300 : std::unique_ptr<uint8_t, VSIFreeReleaser> m_pData = nullptr;
2301 : int64_t m_nRawSize = 0;
2302 : std::unique_ptr<uint8_t, VSIFreeReleaser> m_pRawData = nullptr;
2303 :
2304 : void Decompress();
2305 : void FillRaw();
2306 :
2307 : public:
2308 : GDALPDFStreamPdfium(RetainPtr<const CPDF_Stream> pStream)
2309 : : m_pStream(std::move(pStream))
2310 : {
2311 : }
2312 :
2313 : int64_t GetLength(int64_t nMaxSize = 0) override;
2314 : char *GetBytes() override;
2315 :
2316 : int64_t GetRawLength() override;
2317 : char *GetRawBytes() override;
2318 : };
2319 :
2320 : /************************************************************************/
2321 : /* ==================================================================== */
2322 : /* GDALPDFObjectPdfium */
2323 : /* ==================================================================== */
2324 : /************************************************************************/
2325 :
2326 : /************************************************************************/
2327 : /* GDALPDFObjectPdfium() */
2328 : /************************************************************************/
2329 :
2330 : GDALPDFObjectPdfium::GDALPDFObjectPdfium(RetainPtr<const CPDF_Object> obj)
2331 : : m_obj(std::move(obj))
2332 : {
2333 : CPLAssert(m_obj != nullptr);
2334 : }
2335 :
2336 : /************************************************************************/
2337 : /* ~GDALPDFObjectPdfium() */
2338 : /************************************************************************/
2339 :
2340 : GDALPDFObjectPdfium::~GDALPDFObjectPdfium()
2341 : {
2342 : delete m_poDict;
2343 : delete m_poArray;
2344 : delete m_poStream;
2345 : }
2346 :
2347 : /************************************************************************/
2348 : /* Build() */
2349 : /************************************************************************/
2350 :
2351 : GDALPDFObjectPdfium *
2352 : GDALPDFObjectPdfium::Build(RetainPtr<const CPDF_Object> obj)
2353 : {
2354 : if (obj == nullptr)
2355 : return nullptr;
2356 : if (obj->GetType() == CPDF_Object::Type::kReference)
2357 : {
2358 : obj = obj->GetDirect();
2359 : if (obj == nullptr)
2360 : {
2361 : CPLError(CE_Failure, CPLE_AppDefined,
2362 : "Cannot resolve indirect object");
2363 : return nullptr;
2364 : }
2365 : }
2366 : return new GDALPDFObjectPdfium(std::move(obj));
2367 : }
2368 :
2369 : /************************************************************************/
2370 : /* GetType() */
2371 : /************************************************************************/
2372 :
2373 : GDALPDFObjectType GDALPDFObjectPdfium::GetType()
2374 : {
2375 : switch (m_obj->GetType())
2376 : {
2377 : case CPDF_Object::Type::kNullobj:
2378 : return PDFObjectType_Null;
2379 : case CPDF_Object::Type::kBoolean:
2380 : return PDFObjectType_Bool;
2381 : case CPDF_Object::Type::kNumber:
2382 : return (cpl::down_cast<const CPDF_Number *>(m_obj.Get()))
2383 : ->IsInteger()
2384 : ? PDFObjectType_Int
2385 : : PDFObjectType_Real;
2386 : case CPDF_Object::Type::kString:
2387 : return PDFObjectType_String;
2388 : case CPDF_Object::Type::kName:
2389 : return PDFObjectType_Name;
2390 : case CPDF_Object::Type::kArray:
2391 : return PDFObjectType_Array;
2392 : case CPDF_Object::Type::kDictionary:
2393 : return PDFObjectType_Dictionary;
2394 : case CPDF_Object::Type::kStream:
2395 : return PDFObjectType_Dictionary;
2396 : case CPDF_Object::Type::kReference:
2397 : // unresolved reference
2398 : return PDFObjectType_Unknown;
2399 : default:
2400 : CPLAssert(false);
2401 : return PDFObjectType_Unknown;
2402 : }
2403 : }
2404 :
2405 : /************************************************************************/
2406 : /* GetTypeNameNative() */
2407 : /************************************************************************/
2408 :
2409 : const char *GDALPDFObjectPdfium::GetTypeNameNative()
2410 : {
2411 : if (m_obj->GetType() == CPDF_Object::Type::kStream)
2412 : return "stream";
2413 : else
2414 : return "";
2415 : }
2416 :
2417 : /************************************************************************/
2418 : /* GetBool() */
2419 : /************************************************************************/
2420 :
2421 : int GDALPDFObjectPdfium::GetBool()
2422 : {
2423 : return m_obj->GetInteger();
2424 : }
2425 :
2426 : /************************************************************************/
2427 : /* GetInt() */
2428 : /************************************************************************/
2429 :
2430 : int GDALPDFObjectPdfium::GetInt()
2431 : {
2432 : return m_obj->GetInteger();
2433 : }
2434 :
2435 : /************************************************************************/
2436 : /* CPLRoundToMoreLikelyDouble() */
2437 : /************************************************************************/
2438 :
2439 : // We try to compensate for rounding errors when converting the number
2440 : // in the PDF expressed as a string (e.g 297.84) to float32 by pdfium :
2441 : // 297.8399963378906 Which is technically correct per the PDF spec, but in
2442 : // practice poppler or podofo use double and Geospatial PDF are often encoded
2443 : // with double precision.
2444 :
2445 : static double CPLRoundToMoreLikelyDouble(float f)
2446 : {
2447 : if (std::round(f) == f)
2448 : return f;
2449 :
2450 : char szBuffer[80];
2451 : CPLsnprintf(szBuffer, 80, "%f\n", f);
2452 : double d = f;
2453 : char *pszDot = strchr(szBuffer, '.');
2454 : if (pszDot == nullptr)
2455 : return d;
2456 : pszDot++;
2457 : if (pszDot[0] == 0 || pszDot[1] == 0)
2458 : return d;
2459 : if (STARTS_WITH(pszDot + 2, "99"))
2460 : {
2461 : pszDot[2] = 0;
2462 : double d2 = CPLAtof(szBuffer) + 0.01;
2463 : float f2 = static_cast<float>(d2);
2464 : if (f == f2 || nextafterf(f, f + 1.0f) == f2 ||
2465 : nextafterf(f, f - 1.0f) == f2)
2466 : d = d2;
2467 : }
2468 : else if (STARTS_WITH(pszDot + 2, "00"))
2469 : {
2470 : pszDot[2] = 0;
2471 : double d2 = CPLAtof(szBuffer);
2472 : float f2 = static_cast<float>(d2);
2473 : if (f == f2 || nextafterf(f, f + 1.0f) == f2 ||
2474 : nextafterf(f, f - 1.0f) == f2)
2475 : d = d2;
2476 : }
2477 : return d;
2478 : }
2479 :
2480 : /************************************************************************/
2481 : /* GetReal() */
2482 : /************************************************************************/
2483 :
2484 : double GDALPDFObjectPdfium::GetReal()
2485 : {
2486 : return CPLRoundToMoreLikelyDouble(m_obj->GetNumber());
2487 : }
2488 :
2489 : /************************************************************************/
2490 : /* GetString() */
2491 : /************************************************************************/
2492 :
2493 : const std::string &GDALPDFObjectPdfium::GetString()
2494 : {
2495 : if (GetType() == PDFObjectType_String)
2496 : {
2497 : const auto bs = m_obj->GetString();
2498 : // If empty string, code crashes
2499 : if (bs.IsEmpty())
2500 : return (osStr = "");
2501 : return (osStr = GDALPDFGetUTF8StringFromBytes(
2502 : reinterpret_cast<const GByte *>(bs.c_str()),
2503 : static_cast<int>(bs.GetLength())));
2504 : }
2505 : else
2506 : return (osStr = "");
2507 : }
2508 :
2509 : /************************************************************************/
2510 : /* GetName() */
2511 : /************************************************************************/
2512 :
2513 : const std::string &GDALPDFObjectPdfium::GetName()
2514 : {
2515 : if (GetType() == PDFObjectType_Name)
2516 : return (osStr = m_obj->GetString().c_str());
2517 : else
2518 : return (osStr = "");
2519 : }
2520 :
2521 : /************************************************************************/
2522 : /* GetDictionary() */
2523 : /************************************************************************/
2524 :
2525 : GDALPDFDictionary *GDALPDFObjectPdfium::GetDictionary()
2526 : {
2527 : if (GetType() != PDFObjectType_Dictionary)
2528 : return nullptr;
2529 :
2530 : if (m_poDict)
2531 : return m_poDict;
2532 :
2533 : m_poDict = new GDALPDFDictionaryPdfium(m_obj->GetDict());
2534 : return m_poDict;
2535 : }
2536 :
2537 : /************************************************************************/
2538 : /* GetArray() */
2539 : /************************************************************************/
2540 :
2541 : GDALPDFArray *GDALPDFObjectPdfium::GetArray()
2542 : {
2543 : if (GetType() != PDFObjectType_Array)
2544 : return nullptr;
2545 :
2546 : if (m_poArray)
2547 : return m_poArray;
2548 :
2549 : m_poArray =
2550 : new GDALPDFArrayPdfium(cpl::down_cast<const CPDF_Array *>(m_obj.Get()));
2551 : return m_poArray;
2552 : }
2553 :
2554 : /************************************************************************/
2555 : /* GetStream() */
2556 : /************************************************************************/
2557 :
2558 : GDALPDFStream *GDALPDFObjectPdfium::GetStream()
2559 : {
2560 : if (m_obj->GetType() != CPDF_Object::Type::kStream)
2561 : return nullptr;
2562 :
2563 : if (m_poStream)
2564 : return m_poStream;
2565 : auto pStream = pdfium::WrapRetain(m_obj->AsStream());
2566 : if (pStream)
2567 : {
2568 : m_poStream = new GDALPDFStreamPdfium(std::move(pStream));
2569 : return m_poStream;
2570 : }
2571 : else
2572 : return nullptr;
2573 : }
2574 :
2575 : /************************************************************************/
2576 : /* GetRefNum() */
2577 : /************************************************************************/
2578 :
2579 : GDALPDFObjectNum GDALPDFObjectPdfium::GetRefNum()
2580 : {
2581 : return GDALPDFObjectNum(m_obj->GetObjNum());
2582 : }
2583 :
2584 : /************************************************************************/
2585 : /* GetRefGen() */
2586 : /************************************************************************/
2587 :
2588 : int GDALPDFObjectPdfium::GetRefGen()
2589 : {
2590 : return m_obj->GetGenNum();
2591 : }
2592 :
2593 : /************************************************************************/
2594 : /* ==================================================================== */
2595 : /* GDALPDFDictionaryPdfium */
2596 : /* ==================================================================== */
2597 : /************************************************************************/
2598 :
2599 : /************************************************************************/
2600 : /* ~GDALPDFDictionaryPdfium() */
2601 : /************************************************************************/
2602 :
2603 : GDALPDFDictionaryPdfium::~GDALPDFDictionaryPdfium()
2604 : {
2605 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.begin();
2606 : std::map<CPLString, GDALPDFObject *>::iterator oEnd = m_map.end();
2607 : for (; oIter != oEnd; ++oIter)
2608 : delete oIter->second;
2609 : }
2610 :
2611 : /************************************************************************/
2612 : /* Get() */
2613 : /************************************************************************/
2614 :
2615 : GDALPDFObject *GDALPDFDictionaryPdfium::Get(const char *pszKey)
2616 : {
2617 : std::map<CPLString, GDALPDFObject *>::iterator oIter = m_map.find(pszKey);
2618 : if (oIter != m_map.end())
2619 : return oIter->second;
2620 :
2621 : ByteStringView pdfiumKey(pszKey);
2622 : GDALPDFObjectPdfium *poObj =
2623 : GDALPDFObjectPdfium::Build(m_poDict->GetObjectFor(pdfiumKey));
2624 : if (poObj)
2625 : {
2626 : m_map[pszKey] = poObj;
2627 : return poObj;
2628 : }
2629 : else
2630 : {
2631 : return nullptr;
2632 : }
2633 : }
2634 :
2635 : /************************************************************************/
2636 : /* GetValues() */
2637 : /************************************************************************/
2638 :
2639 : std::map<CPLString, GDALPDFObject *> &GDALPDFDictionaryPdfium::GetValues()
2640 : {
2641 : CPDF_DictionaryLocker dictIterator(m_poDict);
2642 : for (const auto &iter : dictIterator)
2643 : {
2644 : // No object for this key
2645 : if (!iter.second)
2646 : continue;
2647 :
2648 : const char *pszKey = iter.first.c_str();
2649 : // Objects exists in the map
2650 : if (m_map.find(pszKey) != m_map.end())
2651 : continue;
2652 : GDALPDFObjectPdfium *poObj = GDALPDFObjectPdfium::Build(iter.second);
2653 : if (poObj == nullptr)
2654 : continue;
2655 : m_map[pszKey] = poObj;
2656 : }
2657 :
2658 : return m_map;
2659 : }
2660 :
2661 : /************************************************************************/
2662 : /* ==================================================================== */
2663 : /* GDALPDFArrayPdfium */
2664 : /* ==================================================================== */
2665 : /************************************************************************/
2666 :
2667 : /************************************************************************/
2668 : /* GetLength() */
2669 : /************************************************************************/
2670 :
2671 : int GDALPDFArrayPdfium::GetLength()
2672 : {
2673 : return static_cast<int>(m_poArray->size());
2674 : }
2675 :
2676 : /************************************************************************/
2677 : /* Get() */
2678 : /************************************************************************/
2679 :
2680 : GDALPDFObject *GDALPDFArrayPdfium::Get(int nIndex)
2681 : {
2682 : if (nIndex < 0 || nIndex >= GetLength())
2683 : return nullptr;
2684 :
2685 : if (m_v.empty())
2686 : m_v.resize(GetLength());
2687 :
2688 : if (m_v[nIndex] != nullptr)
2689 : return m_v[nIndex].get();
2690 :
2691 : auto poObj = std::unique_ptr<GDALPDFObjectPdfium>(
2692 : GDALPDFObjectPdfium::Build(m_poArray->GetObjectAt(nIndex)));
2693 : if (poObj == nullptr)
2694 : return nullptr;
2695 : m_v[nIndex] = std::move(poObj);
2696 : return m_v[nIndex].get();
2697 : }
2698 :
2699 : /************************************************************************/
2700 : /* ==================================================================== */
2701 : /* GDALPDFStreamPdfium */
2702 : /* ==================================================================== */
2703 : /************************************************************************/
2704 :
2705 : void GDALPDFStreamPdfium::Decompress()
2706 : {
2707 : if (m_pData != nullptr)
2708 : return;
2709 : auto acc(pdfium::MakeRetain<CPDF_StreamAcc>(m_pStream));
2710 : acc->LoadAllDataFiltered();
2711 : m_nSize = static_cast<int64_t>(acc->GetSize());
2712 : m_pData.reset();
2713 : const auto nSize = static_cast<size_t>(m_nSize);
2714 : if (static_cast<int64_t>(nSize) != m_nSize)
2715 : {
2716 : m_nSize = 0;
2717 : }
2718 : if (m_nSize)
2719 : {
2720 : m_pData.reset(static_cast<uint8_t *>(VSI_MALLOC_VERBOSE(nSize)));
2721 : if (!m_pData)
2722 : m_nSize = 0;
2723 : else
2724 : memcpy(&m_pData.get()[0], acc->DetachData().data(), nSize);
2725 : }
2726 : }
2727 :
2728 : /************************************************************************/
2729 : /* GetLength() */
2730 : /************************************************************************/
2731 :
2732 : int64_t GDALPDFStreamPdfium::GetLength(int64_t /* nMaxSize */)
2733 : {
2734 : Decompress();
2735 : return m_nSize;
2736 : }
2737 :
2738 : /************************************************************************/
2739 : /* GetBytes() */
2740 : /************************************************************************/
2741 :
2742 : char *GDALPDFStreamPdfium::GetBytes()
2743 : {
2744 : size_t nLength = static_cast<size_t>(GetLength());
2745 : if (nLength == 0)
2746 : return nullptr;
2747 : char *pszContent = static_cast<char *>(VSI_MALLOC_VERBOSE(nLength + 1));
2748 : if (!pszContent)
2749 : return nullptr;
2750 : memcpy(pszContent, m_pData.get(), nLength);
2751 : pszContent[nLength] = '\0';
2752 : return pszContent;
2753 : }
2754 :
2755 : /************************************************************************/
2756 : /* FillRaw() */
2757 : /************************************************************************/
2758 :
2759 : void GDALPDFStreamPdfium::FillRaw()
2760 : {
2761 : if (m_pRawData != nullptr)
2762 : return;
2763 : auto acc(pdfium::MakeRetain<CPDF_StreamAcc>(m_pStream));
2764 : acc->LoadAllDataRaw();
2765 : m_nRawSize = static_cast<int64_t>(acc->GetSize());
2766 : m_pRawData.reset();
2767 : const auto nSize = static_cast<size_t>(m_nRawSize);
2768 : if (static_cast<int64_t>(nSize) != m_nRawSize)
2769 : {
2770 : m_nRawSize = 0;
2771 : }
2772 : if (m_nRawSize)
2773 : {
2774 : m_pRawData.reset(
2775 : static_cast<uint8_t *>(VSI_MALLOC_VERBOSE(m_nRawSize)));
2776 : if (!m_pRawData)
2777 : m_nRawSize = 0;
2778 : else
2779 : memcpy(&m_pRawData.get()[0], acc->DetachData().data(), m_nRawSize);
2780 : }
2781 : }
2782 :
2783 : /************************************************************************/
2784 : /* GetRawLength() */
2785 : /************************************************************************/
2786 :
2787 : int64_t GDALPDFStreamPdfium::GetRawLength()
2788 : {
2789 : FillRaw();
2790 : return m_nRawSize;
2791 : }
2792 :
2793 : /************************************************************************/
2794 : /* GetRawBytes() */
2795 : /************************************************************************/
2796 :
2797 : char *GDALPDFStreamPdfium::GetRawBytes()
2798 : {
2799 : size_t nLength = static_cast<size_t>(GetRawLength());
2800 : if (nLength == 0)
2801 : return nullptr;
2802 : char *pszContent =
2803 : static_cast<char *>(VSI_MALLOC_VERBOSE(sizeof(char) * (nLength + 1)));
2804 : if (!pszContent)
2805 : return nullptr;
2806 : memcpy(pszContent, m_pRawData.get(), nLength);
2807 : pszContent[nLength] = '\0';
2808 : return pszContent;
2809 : }
2810 :
2811 : #endif // HAVE_PDFIUM
|