1
/******************************************************************************
5
* Purpose: GDALDataset driver for PDF dataset.
6
* Author: Even Rouault, <even dot rouault at mines dash paris dot org>
8
******************************************************************************
9
* Copyright (c) 2010, Even Rouault
11
* Permission is hereby granted, free of charge, to any person obtaining a
12
* copy of this software and associated documentation files (the "Software"),
13
* to deal in the Software without restriction, including without limitation
14
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
* and/or sell copies of the Software, and to permit persons to whom the
16
* Software is furnished to do so, subject to the following conditions:
18
* The above copyright notice and this permission notice shall be included
19
* in all copies or substantial portions of the Software.
21
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
* DEALINGS IN THE SOFTWARE.
28
****************************************************************************/
32
/* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
33
/* in include/poppler/goo/gtypes.h with the one defined in cpl_port.h */
34
#define CPL_GBOOL_DEFINED
40
CPL_CVSID("$Id: pdfio.cpp 22491 2011-06-03 16:04:53Z rouault $");
42
/************************************************************************/
43
/* VSIPDFFileStream() */
44
/************************************************************************/
46
VSIPDFFileStream::VSIPDFFileStream(VSILFILE* f, const char* pszFilename,
47
Guint startA, GBool limitedA,
48
Guint lengthA, Object *dictA):
49
#ifdef POPPLER_BASE_STREAM_HAS_TWO_ARGS
50
BaseStream(dictA, lengthA)
56
poFilename = new GooString(pszFilename);
63
nPosInBuffer = nBufferLength = -1;
66
/************************************************************************/
67
/* VSIPDFFileStream() */
68
/************************************************************************/
70
VSIPDFFileStream::VSIPDFFileStream(VSIPDFFileStream* poParent,
71
Guint startA, GBool limitedA,
72
Guint lengthA, Object *dictA):
73
#ifdef POPPLER_BASE_STREAM_HAS_TWO_ARGS
74
BaseStream(dictA, lengthA)
79
this->poParent = poParent;
80
poFilename = poParent->poFilename;
87
nPosInBuffer = nBufferLength = -1;
90
/************************************************************************/
91
/* ~VSIPDFFileStream() */
92
/************************************************************************/
94
VSIPDFFileStream::~VSIPDFFileStream()
105
/************************************************************************/
106
/* makeSubStream() */
107
/************************************************************************/
109
Stream *VSIPDFFileStream::makeSubStream(Guint startA, GBool limitedA,
110
Guint lengthA, Object *dictA)
112
return new VSIPDFFileStream(this,
117
/************************************************************************/
119
/************************************************************************/
121
int VSIPDFFileStream::getPos()
126
/************************************************************************/
128
/************************************************************************/
130
Guint VSIPDFFileStream::getStart()
135
/************************************************************************/
137
/************************************************************************/
139
StreamKind VSIPDFFileStream::getKind()
144
/************************************************************************/
146
/************************************************************************/
148
GooString *VSIPDFFileStream::getFileName()
153
/************************************************************************/
155
/************************************************************************/
157
int VSIPDFFileStream::FillBuffer()
159
if (nBufferLength == 0)
161
if (nBufferLength != -1 && nBufferLength < BUFFER_SIZE)
167
nToRead = BUFFER_SIZE;
168
else if (nCurrentPos + BUFFER_SIZE > nStart + nLength)
169
nToRead = nStart + nLength - nCurrentPos;
171
nToRead = BUFFER_SIZE;
172
nBufferLength = VSIFReadL(abyBuffer, 1, nToRead, f);
173
if (nBufferLength == 0)
179
/************************************************************************/
181
/************************************************************************/
183
/* The unoptimized version performs a bit well since we must go through */
184
/* the whole virtual I/O chain for each character reading. We save a few */
185
/* percent with this extra internal caching */
187
int VSIPDFFileStream::getChar()
189
#ifdef unoptimized_version
191
if (bLimited && nCurrentPos >= nStart + nLength)
193
if (VSIFReadL(&chRead, 1, 1, f) == 0)
196
if (nPosInBuffer == nBufferLength)
202
GByte chRead = abyBuffer[nPosInBuffer];
209
/************************************************************************/
210
/* getUnfilteredChar() */
211
/************************************************************************/
213
int VSIPDFFileStream::getUnfilteredChar ()
218
/************************************************************************/
220
/************************************************************************/
222
int VSIPDFFileStream::lookChar()
224
#ifdef unoptimized_version
225
int nPosBefore = nCurrentPos;
226
int chRead = getChar();
229
VSIFSeekL(f, nCurrentPos = nPosBefore, SEEK_SET);
232
int chRead = getChar();
241
/************************************************************************/
243
/************************************************************************/
245
void VSIPDFFileStream::reset()
247
nSavedPos = (int)VSIFTellL(f);
248
VSIFSeekL(f, nCurrentPos = nStart, SEEK_SET);
249
nPosInBuffer = nBufferLength = -1;
252
/************************************************************************/
253
/* unfilteredReset() */
254
/************************************************************************/
256
void VSIPDFFileStream::unfilteredReset ()
261
/************************************************************************/
263
/************************************************************************/
265
void VSIPDFFileStream::close()
268
VSIFSeekL(f, nCurrentPos = nSavedPos, SEEK_SET);
272
/************************************************************************/
274
/************************************************************************/
276
void VSIPDFFileStream::setPos(Guint pos, int dir)
280
VSIFSeekL(f, nCurrentPos = pos, SEEK_SET);
284
if (bLimited == gFalse)
286
VSIFSeekL(f, 0, SEEK_END);
290
VSIFSeekL(f, nStart + nLength, SEEK_SET);
292
Guint size = (Guint)VSIFTellL(f);
295
VSIFSeekL(f, nCurrentPos = size - pos, SEEK_SET);
297
nPosInBuffer = nBufferLength = -1;
300
/************************************************************************/
302
/************************************************************************/
304
void VSIPDFFileStream::moveStart(int delta)
307
VSIFSeekL(f, nCurrentPos = nStart, SEEK_SET);
308
nPosInBuffer = nBufferLength = -1;