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