1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#if defined _MSC_VER
#pragma warning(push, 1)
#endif
#include "pdfparse.hxx"
// workaround windows compiler: do not include multi_pass.hpp
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_utility.hpp>
#include <boost/spirit/include/classic_error_handling.hpp>
#include <boost/spirit/include/classic_file_iterator.hpp>
#include <boost/bind.hpp>
#include <string.h>
#include <rtl/strbuf.hxx>
#include <rtl/alloc.h>
// disable warnings again because someone along the line has enabled them
#if defined _MSC_VER
#pragma warning(push, 1)
#endif
using namespace boost::spirit;
using namespace pdfparse;
class StringEmitContext : public EmitContext
{
OStringBuffer m_aBuf;
public:
StringEmitContext() : EmitContext(), m_aBuf(256) {}
virtual ~StringEmitContext() {}
virtual bool write( const void* pBuf, unsigned int nLen ) throw() SAL_OVERRIDE
{
m_aBuf.append( static_cast<const sal_Char*>(pBuf), nLen );
return true;
}
virtual unsigned int getCurPos() throw() SAL_OVERRIDE { return m_aBuf.getLength(); }
virtual bool copyOrigBytes( unsigned int nOrigOffset, unsigned int nLen ) throw() SAL_OVERRIDE
{ return (nOrigOffset+nLen < static_cast<unsigned int>(m_aBuf.getLength()) ) &&
write( m_aBuf.getStr() + nOrigOffset, nLen ); }
virtual unsigned int readOrigBytes( unsigned int nOrigOffset, unsigned int nLen, void* pBuf ) throw() SAL_OVERRIDE
{
if( nOrigOffset+nLen < static_cast<unsigned int>(m_aBuf.getLength()) )
{
memcpy( pBuf, m_aBuf.getStr()+nOrigOffset, nLen );
return nLen;
}
return 0;
}
OString getString() { return m_aBuf.makeStringAndClear(); }
};
template< class iteratorT >
class PDFGrammar : public grammar< PDFGrammar<iteratorT> >
{
public:
PDFGrammar( const iteratorT& first )
: m_fDouble( 0.0 ), m_aGlobalBegin( first ) {}
~PDFGrammar()
{
if( !m_aObjectStack.empty() )
delete m_aObjectStack.front();
}
double m_fDouble;
std::vector< unsigned int > m_aUIntStack;
std::vector< PDFEntry* > m_aObjectStack;
OString m_aErrorString;
iteratorT m_aGlobalBegin;
public:
struct pdf_string_parser
{
typedef nil_t result_t;
template <typename ScannerT>
std::ptrdiff_t
operator()(ScannerT const& scan, result_t&) const
{
std::ptrdiff_t len = 0;
int nBraceLevel = 0;
while( ! scan.at_end() )
{
char c = *scan;
if( c == ')' )
{
nBraceLevel--;
if( nBraceLevel < 0 )
break;
}
else if( c == '(' )
nBraceLevel++;
else if( c == '\\' ) // ignore escaped braces
{
++len;
++scan.first; // tdf#63054: avoid skipping spaces
if( scan.first == scan.last ) // tdf#63054: avoid skipping spaces
break;
}
++len;
++scan;
}
return scan.at_end() ? -1 : len;
}
};
template< typename ScannerT >
struct definition
{
definition( const PDFGrammar<iteratorT>& rSelf )
{
PDFGrammar<iteratorT>* pSelf = const_cast< PDFGrammar<iteratorT>* >( &rSelf );
// workaround workshop compiler: comment_p doesn't work
// comment = comment_p("%")[boost::bind(&PDFGrammar::pushComment, pSelf, _1, _2 )];
comment = lexeme_d[ (ch_p('%') >> *(~ch_p('\r') & ~ch_p('\n')) >> eol_p)[boost::bind(&PDFGrammar::pushComment, pSelf, _1, _2 )] ];
boolean = (str_p("true") | str_p("false"))[boost::bind(&PDFGrammar::pushBool, pSelf, _1, _2)];
// workaround workshop compiler: confix_p doesn't work
//stream = confix_p( "stream", *anychar_p, "endstream" )[boost::bind(&PDFGrammar::emitStream, pSelf, _1, _2 )];
stream = (str_p("stream") >> *(anychar_p - str_p("endstream")) >> str_p("endstream"))[boost::bind(&PDFGrammar::emitStream, pSelf, _1, _2 )];
name = lexeme_d[
ch_p('/')
>> (*(anychar_p-chset_p("\t\n\f\r ()<>[]{}/%")-ch_p('\0')))
[boost::bind(&PDFGrammar::pushName, pSelf, _1, _2)] ];
// workaround workshop compiler: confix_p doesn't work
//stringtype = ( confix_p("(",*anychar_p, ")") |
// confix_p("<",*xdigit_p, ">") )
// [boost::bind(&PDFGrammar::pushString,pSelf, _1, _2)];
stringtype = ( ( ch_p('(') >> functor_parser<pdf_string_parser>() >> ch_p(')') ) |
( ch_p('<') >> *xdigit_p >> ch_p('>') ) )
[boost::bind(&PDFGrammar::pushString,pSelf, _1, _2)];
null_object = str_p( "null" )[boost::bind(&PDFGrammar::pushNull, pSelf, _1, _2)];
#ifdef USE_ASSIGN_ACTOR
objectref = ( uint_p[push_back_a(pSelf->m_aUIntStack)]
>> uint_p[push_back_a(pSelf->m_aUIntStack)]
>> ch_p('R')
>> eps_p
)[boost::bind(&PDFGrammar::pushObjectRef, pSelf, _1, _2)];
#else
objectref = ( uint_p[boost::bind(&PDFGrammar::push_back_action_uint, pSelf, _1)]
>> uint_p[boost::bind(&PDFGrammar::push_back_action_uint, pSelf, _1)]
>> ch_p('R')
>> eps_p
)[boost::bind(&PDFGrammar::pushObjectRef, pSelf, _1, _2)];
#endif
#ifdef USE_ASSIGN_ACTOR
simple_type = objectref | name |
( real_p[assign_a(pSelf->m_fDouble)] >> eps_p )
[boost::bind(&PDFGrammar::pushDouble, pSelf, _1, _2)]
| stringtype | boolean | null_object;
#else
simple_type = objectref | name |
( real_p[boost::bind(&PDFGrammar::assign_action_double, pSelf, _1)] >> eps_p )
[boost::bind(&PDFGrammar::pushDouble, pSelf, _1, _2)]
| stringtype | boolean | null_object;
#endif
dict_begin = str_p( "<<" )[boost::bind(&PDFGrammar::beginDict, pSelf, _1, _2)];
dict_end = str_p( ">>" )[boost::bind(&PDFGrammar::endDict, pSelf, _1, _2)];
array_begin = str_p("[")[boost::bind(&PDFGrammar::beginArray,pSelf, _1, _2)];
array_end = str_p("]")[boost::bind(&PDFGrammar::endArray,pSelf, _1, _2)];
#ifdef USE_ASSIGN_ACTOR
object_begin= uint_p[push_back_a(pSelf->m_aUIntStack)]
>> uint_p[push_back_a(pSelf->m_aUIntStack)]
>> str_p("obj" )[boost::bind(&PDFGrammar::beginObject, pSelf, _1, _2)];
#else
object_begin= uint_p[boost::bind(&PDFGrammar::push_back_action_uint, pSelf, _1)]
>> uint_p[boost::bind(&PDFGrammar::push_back_action_uint, pSelf, _1)]
>> str_p("obj" )[boost::bind(&PDFGrammar::beginObject, pSelf, _1, _2)];
#endif
object_end = str_p( "endobj" )[boost::bind(&PDFGrammar::endObject, pSelf, _1, _2)];
xref = str_p( "xref" ) >> uint_p >> uint_p
>> lexeme_d[
+( repeat_p(10)[digit_p]
>> blank_p
>> repeat_p(5)[digit_p]
>> blank_p
>> ( ch_p('n') | ch_p('f') )
>> repeat_p(2)[space_p]
) ];
dict_element= dict_begin | comment | simple_type
| array_begin | array_end | dict_end;
object = object_begin
>> *dict_element
>> !stream
>> object_end;
trailer = str_p( "trailer" )[boost::bind(&PDFGrammar::beginTrailer,pSelf,_1,_2)]
>> *dict_element
>> str_p("startxref")
>> uint_p
>> str_p("%%EOF")[boost::bind(&PDFGrammar::endTrailer,pSelf,_1,_2)];
#ifdef USE_ASSIGN_ACTOR
pdfrule = ! (lexeme_d[
str_p( "%PDF-" )
>> uint_p[push_back_a(pSelf->m_aUIntStack)]
>> ch_p('.')
>> uint_p[push_back_a(pSelf->m_aUIntStack)]
>> *((~ch_p('\r') & ~ch_p('\n')))
>> eol_p
])[boost::bind(&PDFGrammar::haveFile,pSelf, _1, _2)]
>> *( comment | object | ( xref >> trailer ) );
#else
pdfrule = ! (lexeme_d[
str_p( "%PDF-" )
>> uint_p[boost::bind(&PDFGrammar::push_back_action_uint, pSelf, _1)]
>> ch_p('.')
>> uint_p[boost::bind(&PDFGrammar::push_back_action_uint, pSelf, _1)]
>> *((~ch_p('\r') & ~ch_p('\n')))
>> eol_p
])[boost::bind(&PDFGrammar::haveFile,pSelf, _1, _2)]
>> *( comment | object | ( xref >> trailer ) );
#endif
}
rule< ScannerT > comment, stream, boolean, name, stringtype, null_object, simple_type,
objectref, array, value, dict_element, dict_begin, dict_end,
array_begin, array_end, object, object_begin, object_end,
xref, trailer, pdfrule;
const rule< ScannerT >& start() const { return pdfrule; }
};
#ifndef USE_ASSIGN_ACTOR
void push_back_action_uint( unsigned int i )
{
m_aUIntStack.push_back( i );
}
void assign_action_double( double d )
{
m_fDouble = d;
}
#endif
static void parseError( const char* pMessage, iteratorT pLocation )
{
throw_( pLocation, pMessage );
}
OString iteratorToString( iteratorT first, iteratorT last ) const
{
OStringBuffer aStr( 32 );
while( first != last )
{
aStr.append( *first );
++first;
}
return aStr.makeStringAndClear();
}
void haveFile( iteratorT pBegin, SAL_UNUSED_PARAMETER iteratorT /*pEnd*/ )
{
if( m_aObjectStack.empty() )
{
PDFFile* pFile = new PDFFile();
pFile->m_nMinor = m_aUIntStack.back();
m_aUIntStack.pop_back();
pFile->m_nMajor = m_aUIntStack.back();
m_aUIntStack.pop_back();
m_aObjectStack.push_back( pFile );
}
else
parseError( "found file header in unusual place", pBegin );
}
void pushComment( iteratorT first, iteratorT last )
{
// add a comment to the current stack element
PDFComment* pComment =
new PDFComment(iteratorToString(first,last));
if( m_aObjectStack.empty() )
m_aObjectStack.push_back( new PDFPart() );
PDFContainer* pContainer = dynamic_cast<PDFContainer*>(m_aObjectStack.back());
if( pContainer == NULL )
parseError( "comment without container", first );
pContainer->m_aSubElements.push_back( pComment );
}
void insertNewValue( PDFEntry* pNewValue, iteratorT pPos )
{
PDFContainer* pContainer = NULL;
const char* pMsg = NULL;
if( ! m_aObjectStack.empty() &&
(pContainer = dynamic_cast<PDFContainer*>(m_aObjectStack.back())) != NULL )
{
if( dynamic_cast<PDFDict*>(pContainer) == NULL &&
dynamic_cast<PDFArray*>(pContainer) == NULL )
{
PDFObject* pObj = dynamic_cast<PDFObject*>(pContainer);
if( pObj )
{
if( pObj->m_pObject == NULL )
pObj->m_pObject = pNewValue;
else
{
pMsg = "second value for object";
pContainer = NULL;
}
}
else if( dynamic_cast<PDFDict*>(pNewValue) )
{
PDFTrailer* pTrailer = dynamic_cast<PDFTrailer*>(pContainer);
if( pTrailer )
{
if( pTrailer->m_pDict == NULL )
pTrailer->m_pDict = dynamic_cast<PDFDict*>(pNewValue);
else
pContainer = NULL;
}
else
pContainer = NULL;
}
else
pContainer = NULL;
}
}
if( pContainer )
pContainer->m_aSubElements.push_back( pNewValue );
else
{
if( ! pMsg )
{
if( dynamic_cast<PDFContainer*>(pNewValue) )
pMsg = "array without container";
else
pMsg = "value without container";
}
delete pNewValue;
parseError( pMsg, pPos );
}
}
void pushName( iteratorT first, iteratorT last )
{
insertNewValue( new PDFName(iteratorToString(first,last)), first );
}
void pushDouble( iteratorT first, SAL_UNUSED_PARAMETER iteratorT /*last*/ )
{
insertNewValue( new PDFNumber(m_fDouble), first );
}
void pushString( iteratorT first, iteratorT last )
{
insertNewValue( new PDFString(iteratorToString(first,last)), first );
}
void pushBool( iteratorT first, iteratorT last )
{
insertNewValue( new PDFBool( (last-first == 4) ), first );
}
void pushNull( iteratorT first, SAL_UNUSED_PARAMETER iteratorT )
{
insertNewValue( new PDFNull(), first );
}
void beginObject( iteratorT first, SAL_UNUSED_PARAMETER iteratorT /*last*/ )
{
if( m_aObjectStack.empty() )
m_aObjectStack.push_back( new PDFPart() );
unsigned int nGeneration = m_aUIntStack.back();
m_aUIntStack.pop_back();
unsigned int nObject = m_aUIntStack.back();
m_aUIntStack.pop_back();
PDFObject* pObj = new PDFObject( nObject, nGeneration );
pObj->m_nOffset = first - m_aGlobalBegin;
PDFContainer* pContainer = dynamic_cast<PDFContainer*>(m_aObjectStack.back());
if( pContainer &&
( dynamic_cast<PDFFile*>(pContainer) ||
dynamic_cast<PDFPart*>(pContainer) ) )
{
pContainer->m_aSubElements.push_back( pObj );
m_aObjectStack.push_back( pObj );
}
else
parseError( "object in wrong place", first );
}
void endObject( iteratorT first, SAL_UNUSED_PARAMETER iteratorT )
{
if( m_aObjectStack.empty() )
parseError( "endobj without obj", first );
else if( dynamic_cast<PDFObject*>(m_aObjectStack.back()) == NULL )
parseError( "spurious endobj", first );
else
m_aObjectStack.pop_back();
}
void pushObjectRef( iteratorT first, SAL_UNUSED_PARAMETER iteratorT )
{
unsigned int nGeneration = m_aUIntStack.back();
m_aUIntStack.pop_back();
unsigned int nObject = m_aUIntStack.back();
m_aUIntStack.pop_back();
insertNewValue( new PDFObjectRef(nObject,nGeneration), first );
}
void beginDict( iteratorT first, SAL_UNUSED_PARAMETER iteratorT )
{
PDFDict* pDict = new PDFDict();
pDict->m_nOffset = first - m_aGlobalBegin;
insertNewValue( pDict, first );
// will not come here if insertion fails (exception)
m_aObjectStack.push_back( pDict );
}
void endDict( iteratorT first, SAL_UNUSED_PARAMETER iteratorT )
{
PDFDict* pDict = NULL;
if( m_aObjectStack.empty() )
parseError( "dictionary end without begin", first );
else if( (pDict = dynamic_cast<PDFDict*>(m_aObjectStack.back())) == NULL )
parseError( "spurious dictionary end", first );
else
m_aObjectStack.pop_back();
PDFEntry* pOffender = pDict->buildMap();
if( pOffender )
{
StringEmitContext aCtx;
aCtx.write( "offending dictionary element: ", 30 );
pOffender->emit( aCtx );
m_aErrorString = aCtx.getString();
parseError( m_aErrorString.getStr(), first );
}
}
void beginArray( iteratorT first, SAL_UNUSED_PARAMETER iteratorT )
{
PDFArray* pArray = new PDFArray();
pArray->m_nOffset = first - m_aGlobalBegin;
insertNewValue( pArray, first );
// will not come here if insertion fails (exception)
m_aObjectStack.push_back( pArray );
}
void endArray( iteratorT first, SAL_UNUSED_PARAMETER iteratorT )
{
if( m_aObjectStack.empty() )
parseError( "array end without begin", first );
else if( dynamic_cast<PDFArray*>(m_aObjectStack.back()) == NULL )
parseError( "spurious array end", first );
else
m_aObjectStack.pop_back();
}
void emitStream( iteratorT first, iteratorT last )
{
if( m_aObjectStack.empty() )
parseError( "stream without object", first );
PDFObject* pObj = dynamic_cast<PDFObject*>(m_aObjectStack.back());
if( pObj && pObj->m_pObject )
{
if( pObj->m_pStream )
parseError( "multiple streams in object", first );
PDFDict* pDict = dynamic_cast<PDFDict*>(pObj->m_pObject);
if( pDict )
{
PDFStream* pStream = new PDFStream( first - m_aGlobalBegin, last - m_aGlobalBegin, pDict );
pObj->m_pStream = pStream;
pObj->m_aSubElements.push_back( pStream );
}
}
else
parseError( "stream without object", first );
}
void beginTrailer( iteratorT first, SAL_UNUSED_PARAMETER iteratorT )
{
if( m_aObjectStack.empty() )
m_aObjectStack.push_back( new PDFPart() );
PDFTrailer* pTrailer = new PDFTrailer();
pTrailer->m_nOffset = first - m_aGlobalBegin;
PDFContainer* pContainer = dynamic_cast<PDFContainer*>(m_aObjectStack.back());
if( pContainer &&
( dynamic_cast<PDFFile*>(pContainer) ||
dynamic_cast<PDFPart*>(pContainer) ) )
{
pContainer->m_aSubElements.push_back( pTrailer );
m_aObjectStack.push_back( pTrailer );
}
else
parseError( "trailer in wrong place", first );
}
void endTrailer( iteratorT first, SAL_UNUSED_PARAMETER iteratorT )
{
if( m_aObjectStack.empty() )
parseError( "%%EOF without trailer", first );
else if( dynamic_cast<PDFTrailer*>(m_aObjectStack.back()) == NULL )
parseError( "spurious %%EOF", first );
else
m_aObjectStack.pop_back();
}
};
#ifdef WIN32
PDFEntry* PDFReader::read( const char* pBuffer, unsigned int nLen )
{
PDFGrammar<const char*> aGrammar( pBuffer );
try
{
#if OSL_DEBUG_LEVEL > 1
boost::spirit::parse_info<const char*> aInfo =
#endif
boost::spirit::parse( pBuffer,
pBuffer+nLen,
aGrammar,
boost::spirit::space_p );
#if OSL_DEBUG_LEVEL > 1
SAL_INFO("sdext.pdfimport.pdfparse", "parseinfo: stop = " << aInfo.stop << " (buff=" << pBuffer << ", offset = " << aInfo.stop - pBuffer << "), hit = " << (aInfo.hit ? OUString("true") : OUString("false")) << ", full = " << (aInfo.full ? OUString("true") : OUString("false")) << ", length = " << (int)aInfo.length );
#endif
}
catch( const parser_error<const char*, const char*>& rError )
{
#if OSL_DEBUG_LEVEL > 1
OString aTmp;
unsigned int nElem = aGrammar.m_aObjectStack.size();
for( unsigned int i = 0; i < nElem; i++ )
aTmp += " " + OString(typeid( *(aGrammar.m_aObjectStack[i]) ).name());
SAL_WARN("sdext.pdfimport.pdfparse", "parse error: " << rError.descriptor << " at buffer pos " << rError.where - pBuffer << ", object stack: " << aTmp);
#endif
}
PDFEntry* pRet = NULL;
unsigned int nEntries = aGrammar.m_aObjectStack.size();
if( nEntries == 1 )
{
pRet = aGrammar.m_aObjectStack.back();
aGrammar.m_aObjectStack.pop_back();
}
#if OSL_DEBUG_LEVEL > 1
else if( nEntries > 1 )
SAL_WARN("sdext.pdfimport.pdfparse", "error got " << nEntries << " stack objects in parse" );
#endif
return pRet;
}
#endif
PDFEntry* PDFReader::read( const char* pFileName )
{
#ifdef WIN32
/* #i106583#
since converting to boost 1.39 file_iterator does not work anymore on all Windows systems
C++ stdlib istream_iterator does not allow "-" apparently
using spirit 2.0 doesn't work in our environment with the MSC
So for the time being bite the bullet and read the whole file.
FIXME: give Spirit 2.x another try when we upgrade boost again.
*/
PDFEntry* pRet = NULL;
FILE* fp = fopen( pFileName, "rb" );
if( fp )
{
fseek( fp, 0, SEEK_END );
unsigned int nLen = (unsigned int)ftell( fp );
fseek( fp, 0, SEEK_SET );
char* pBuf = (char*)rtl_allocateMemory( nLen );
if( pBuf )
{
fread( pBuf, 1, nLen, fp );
pRet = read( pBuf, nLen );
rtl_freeMemory( pBuf );
}
fclose( fp );
}
return pRet;
#else
file_iterator<> file_start( pFileName );
if( ! file_start )
return NULL;
file_iterator<> file_end = file_start.make_end();
PDFGrammar< file_iterator<> > aGrammar( file_start );
try
{
#if OSL_DEBUG_LEVEL > 1
boost::spirit::parse_info< file_iterator<> > aInfo =
#endif
boost::spirit::parse( file_start,
file_end,
aGrammar,
boost::spirit::space_p );
#if OSL_DEBUG_LEVEL > 1
SAL_INFO("sdext.pdfimport.pdfparse", "parseinfo: stop at offset = " << aInfo.stop - file_start << ", hit = " << (aInfo.hit ? "true" : "false") << ", full = " << (aInfo.full ? "true" : "false") << ", length = " << aInfo.length);
#endif
}
catch( const parser_error< const char*, file_iterator<> >& rError )
{
SAL_WARN("sdext.pdfimport.pdfparse", "parse error: " << rError.descriptor << " at buffer pos " << rError.where - file_start);
#if OSL_DEBUG_LEVEL > 1
OUString aTmp;
unsigned int nElem = aGrammar.m_aObjectStack.size();
for( unsigned int i = 0; i < nElem; i++ )
{
aTmp += " ";
aTmp += OUString(typeid( *(aGrammar.m_aObjectStack[i]) ).name(),
strlen(typeid( *(aGrammar.m_aObjectStack[i]) ).name()),
RTL_TEXTENCODING_ASCII_US);
}
SAL_WARN("sdext.pdfimport.pdfparse", "parse error object stack: " << aTmp);
#endif
}
PDFEntry* pRet = NULL;
unsigned int nEntries = aGrammar.m_aObjectStack.size();
if( nEntries == 1 )
{
pRet = aGrammar.m_aObjectStack.back();
aGrammar.m_aObjectStack.pop_back();
}
#if OSL_DEBUG_LEVEL > 1
else if( nEntries > 1 )
{
SAL_WARN("sdext.pdfimport.pdfparse", "error got " << nEntries << " stack objects in parse");
for( unsigned int i = 0; i < nEntries; i++ )
{
SAL_WARN("sdext.pdfimport.pdfparse", typeid(*aGrammar.m_aObjectStack[i]).name());
PDFObject* pObj = dynamic_cast<PDFObject*>(aGrammar.m_aObjectStack[i]);
if( pObj )
SAL_WARN("sdext.pdfimport.pdfparse", " -> object " << pObj->m_nNumber << " generation " << pObj->m_nGeneration);
else
SAL_WARN("sdext.pdfimport.pdfparse", "(type " << typeid(*aGrammar.m_aObjectStack[i]).name() << ")");
}
}
#endif
return pRet;
#endif // WIN32
}
#if defined _MSC_VER
#pragma warning(pop)
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|