-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPdfParse.pas
548 lines (443 loc) · 11.7 KB
/
PdfParse.pas
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
unit PdfParse;
{$mode objfpc}{$H+}
interface
uses PdfObjs,pdfNotifiers;
const
pdf_White_space_chars = [#0, #8, #9, #10, #12, #13, #32];
pdf_Delimiter_chars = ['[', ']', '<', '>', '(', ')', '/', '%'];
all_Delimiter_chars = [#0, #8, #9, #10, #12, #13, #32, '[', ']', '<', '>', '(', ')', '/', '%'];
pdf_Digits = ['.', '0'..'9'];
pdf_Numeric = ['+', '-', '.', '0'..'9'];
LF = #10;
CR = #13;
CRLF = #13#10;
type
char_set= set of char;
pdfParser = class
private
FBuffer: PChar;
FPos: PtrUInt;
FBufSize: PtrUInt;
FProgressor: TProgress;
public
function EOF: Boolean;
function BOF: Boolean;
function Next : Boolean;
function Prev : Boolean;
function Current: Char;
function PeekNext: Char;
function PeekPrev: Char;
procedure Jump(NewPos: PtrUInt);
procedure Skip(Num: integer);
function SkipBlank:Char;
procedure SkipTo(C: char);
procedure SkipTo(const Chars: char_set);
procedure SkipTo(const Str: string);
function GetToken:String;
function GetInteger(out num: integer): boolean;
function GetUInt(out num: PtrUInt): boolean;
function GetLine: string;
function Compare(const Txt: string): boolean;
function ParseObj : PPdfObj;
// function ParseDictionary(const TheContainer: TPdfVar):pdfDataType;
function ParseArray : PPdfObj;
{ function ParseHexString(const TheContainer: TPdfVar):pdfDataType;
function ParseString(const TheContainer: TPdfVar):pdfDataType;
function ParseName(const TheContainer: TPdfVar):pdfDataType;
function ParseRef(const TheContainer: TPdfVar):pdfDataType;
function ParseNumeric(const TheContainer: TPdfVar):pdfDataType;
function ParseBoolean(const TheContainer: TPdfVar):pdfDataType; }
constructor Create(ABuffer: Pointer; ABufSz: PtrUInt;AProgressor: TProgress = nil);
destructor Destroy; override;
// function ReadObject(Offset: Integer=0): pdfObject;
function FindStartXRef: PtrUInt;
property Position: PtrUInt read FPos;
property BufferSize: PtrUInt read FBufSize;
end;
implementation
uses Classes, SysUtils;
// Parser Class
constructor PdfParser.Create(ABuffer: Pointer; ABufSz: PtrUInt;AProgressor: TProgress = nil);
begin
inherited Create;
FBuffer := ABuffer;
FPos := 0;
FBufSize := ABufSz;
FProgressor := AProgressor;
end;
destructor PdfParser.Destroy;
begin
FProgressor.Free;
inherited Destroy;
end;
function pdfParser.EOF:boolean;inline;
begin
Result:=not Position<FBufSize-1;
end;
function pdfParser.BOF:boolean;inline;
begin
Result:=Position=0;
end;
function pdfParser.Current:Char;inline;
begin
Result:=FBuffer[Position];
end;
function pdfParser.PeekNext: Char;inline;
begin
if not EOF then result:=FBuffer[Position+1]
else result:=#0
end;
function pdfParser.PeekPrev:Char;inline;
begin
if not BOF then result:=FBuffer[Position-1]
else result:=#0
end;
function pdfParser.Next : Boolean;inline;
begin
Result:=Not EOF;
if Result then Inc(FPos);
end;
function pdfParser.Prev : Boolean;inline;
begin
Result:=not BOF;
if Result then Dec(FPos);
end;
procedure pdfParser.Jump(NewPos: PtrUInt); inline;
begin
FPos := NewPos;
end;
procedure pdfParser.Skip(Num: Integer); inline;
begin
Inc(FPos, Num);
end;
function pdfParser.SkipBlank:Char;inline;
begin
while (Current in pdf_white_space_chars) and Next do;
Result := Current
end;
procedure pdfParser.SkipTo(C: Char);inline;
begin
while (Current <> C) and Next do;
end;
procedure pdfParser.SkipTo(const Chars:char_set );inline;
begin
while (not (Current in chars)) and Next do;
end;
procedure pdfParser.SkipTo(const Str: string);
begin
while True do
begin
SkipTo(Str[1]);
if Compare(Str) then Break
else Next
end
end;
function pdfParser.GetToken:String;
var TokenStart: PtrUInt;
begin
TokenStart:=Position;
while (not (Current in all_delimiter_chars)) and Next do;
SetString(Result,@FBuffer[TokenStart],Position-TokenStart);
end;
function pdfParser.GetLine:String;
var
LineStart: PtrUInt;
begin
LineStart := Position;
SkipTo(CR);
SetString(Result, @FBuffer[LineStart], Position - LineStart);
if PeekNext = LF then Next;
end;
function pdfParser.Compare(const Txt: string): boolean;
var
i, sz: integer;
begin
i := 0;
sz := Length(txt);
while (Current = Txt[i + 1]) and next do
if i < sz then inc(i) else break;
Result := i = sz;
end;
{function pdfParser.ReadObject(Offset: Integer=0): pdfObject;
var ObjNum,ObjGen: Integer;
begin
// PdfNotify(ntInfo,'begin read object ->>',nil);
result:=nil;
if Offset<>0 then Jump(Offset);
if (not GetInteger(ObjNum)) then exit;
if not(GetInteger(ObjGen)) then exit;
https://dl.dropbox.com/u/65215004/Badem%20Book.zip
SkipBlank;
if not Compare('obj') then exit;
SkipBlank;
Result:=pdfObject.Create(ObjNum,ObjGen,Offset,ReadVar);
// PdfNotify(ntInfo,'<--end read.',nil);
end;
}
const ParseErrorMsg='Parsing of object failed!!! Expecting Valid TOKEN but "%s" found near "%s" at offset %d';
function pdfParser.ParseObj: PPdfObj;
begin
Result := nil;
case SkipBlank of
// '<': if PeekNext = '<' then Result := ParseDictionary;
// else Result := ParseHexString(TheContainer);
'[': Result := ParseArray;
{ '(': Result := ParseString(TheContainer);
'/': Result := ParseName(TheContainer);
'.',
'-',
'+': Result := ParseNumeric(TheContainer);
'0'..'9':
begin
Result := ParseRef(TheContainer);
if Result = pdfInvalid then Result := ParseNumeric(TheContainer);
end;
'n': if Compare('null') then Result:=TheContainer.InitNull;
'F', 'T', 'f', 't': Result := ParseBoolean(TheContainer);
}
end;
if Result = nil then PdfNotify(ntWarnning, format(ParseErrorMsg,
[@FBuffer[Position], Copy(FBuffer[Position- 8], 1, 64), Position]),nil);
if FProgressor <> nil then FProgressor.DoSubProgress(1);
end;
function pdfParser.ParseArray: PPdfObj;
var NewItem: PPdfObj;
begin
if Current <> '[' then exit(nil);
Result:=TPdfObj.NewArray;
Next;
while (SkipBlank <> ']') do
begin
NewItem:=ParseObj;
if NewItem=nil then
begin
TPdfObj.Release(Result);
exit;
end
else Result^.AddItem(NewItem)
end;
Next
end;
function pdfParser.ParseDictionary(const TheContainer: TPdfVar): pdfDataType;
var
M: TMemoryStream;
NewItem: PPDFVar;
begin
if not((Current= '<') and (PeekNext = '<')) then exit(pdfInvalid);
Skip(2);
Result:=TheContainer.InitDict;
while (SkipBlank<> '>') do
begin
if Current = '/' then
begin
Next;
NewItem:=TPdfVar.New;
NewItem^.Key:=pdfNamesManager.Add(GetToken);
Result:=ParseVar(NewItem^);
if Result=pdfInvalid then
begin
NewItem^.Dispose;
exit;
end
else TheContainer.AddItem(NewItem);
end
else PdfNotify(ntwarnning, 'Cannot Read Item Name!', nil)
end;
// okay we have the first > of the end of the dictionary let's check for the next >
Next;
if Current= '>' then next
else PdfNotify(ntwarnning, format('Wrong Char - Expecting ">" but %s found', [FBuffer[FCursor]]),nil);
SkipBlank;
Result:=pdfDict;
{ if Compare('stream') then
begin
if (FCursor^ = #13) then
Inc(FCursor); // skip the cr
if (FCursor^ = #10) then
Inc(FCursor) // skip the lf
else
PdfNotify(ntwarnning, 'Missing LF before Stream Content!!!', nil);
L := Result['/Length'].AsInteger;
M := TMemoryStream.Create;
M.SetSize(L);
Move(FCursor^, M.Memory^, L);
Result.SetToStream(M);
Skip(L);
end;}
end;
function pdfParser.ParseString(const TheContainer: TPdfVar):pdfDataType;
var
StrStart: PtrUInt;
begin
if Current <> '(' then Exit(pdfInvalid);
Next;
StrStart := FCursor;
while True do
begin
SkipTo(')');
if PeekPrev <> '\' then Break; // check if this is an escape \)
Inc(FCursor);
end;
Result:=TheContainer.Init(pdfStr,Copy(FBuffer[StrStart],0,FCursor - StrStart));
Next;
end;
function pdfParser.ParseHexString(const TheContainer: TPdfVar):pdfDataType;
var
StrStart: PtrUInt;
begin
if Current <> '<' then Exit(pdfInvalid);
Next;
StrStart := FCursor;
SkipTo('>');
Result:=TheContainer.Init(pdfHexStr,Copy(FBuffer[StrStart],0,FCursor - StrStart));
Next;
end;
function pdfParser.ParseName(const TheContainer: TPdfVar):pdfDataType;
begin
if Current <> '/' then Exit(pdfInvalid);
Next;
Result:=PdfName;
Result:=TheContainer.Init(Result,pdfNamesManager.Add(GetToken));
end;
function pdfParser.ParseBoolean(const TheContainer: TPdfVar):pdfDataType;
var
b:boolean;
s: String;
begin
if (Current in ['F', 'T', 'f', 't']) then
begin
S:=UpperCase(GetToken);
if (S <> 'TRUE') and (S <> 'FALSE') then exit(pdfInvalid);
b:=S = 'TRUE'; LConvEncoding
Result:=TheContainer.Init(pdfBool,b);
end
else Result:=pdfInvalid
end;
function Char2Num(C: char): byte; inline;
begin
Result := Ord(c) - Ord('0');
end;
function pdfParser.ParseNumeric(const TheContainer: TPdfVar):pdfDataType;
var
NumStart: PtrUInt;
isNeg: boolean;
Scale: extended;
xi: Single;
i: Integer;
begin
if Current in pdf_numeric then Result:=pdfInt
else exit(pdfInvalid);
isNeg := Current = '-';
if isNeg then Next;
if Current = '+' then Next;
Scale := 10;
NumStart := FCursor;
xi := 0;
while Current in Pdf_Digits do
begin
case Current of
'.':
begin
if Result=pdfFloat then exit(pdfInvalid)
else Result:=pdfFloat;
Scale := 0.1;
Inc(FCursor);
end;
'0'..'9':
begin
xi := xi * scale + Char2Num(Current);
Inc(FCursor);
end;
end;
end;
if FCursor = NumStart then exit(pdfInvalid);
if isNeg then xi := -xi;
if Result = pdfInt then
begin
i:=Trunc(xi);
TheContainer.Init(Result,i)
end
else
TheContainer.Init(Result,xi)
end;
function pdfParser.GetInteger(out num: integer): boolean;
var
xi: integer;
isNeg: boolean;
NumStart: PtrUInt;
digit: char;
begin
Result := False;
IsNeg := False;
SkipTo(pdf_numeric);
if Current = '-' then //check for a sign in the begining
begin
IsNeg := True;
Next;
end
else if Current = '+' then next;
xi := 0;
NumStart := FCursor;
repeat
digit := Current;
if not (digit in ['0'..'9']) then break;
xi := (xi * 10);
Dec(digit, Ord('0'));
Inc(xi, Ord(digit));
Inc(FCursor);
until (False);
if (NumStart = FCursor) then exit;
if IsNeg then xi := -xi;
Num := xi;
Result := True;
end;
function pdfParser.GetUInt(out num: PtrUInt): boolean;
var
xi: PtrUInt;
NumStart: PtrUInt;
digit: char;
begin
Result := False;
SkipTo(['0'..'9']);
xi := 0;
NumStart := FCursor;
repeat
digit := Current;
if not (digit in ['0'..'9']) then break;
xi := (xi * 10);
Dec(digit, Ord('0'));
Inc(xi, Ord(digit));
Inc(FCursor);
until (False);
if (NumStart = FCursor) then exit;
Num := xi;
Result := True;
end;
// if the format is not NNNN NN R then the cursor is not incremented to allow read of an numeric value on failure
function pdfParser.ParseRef(const TheContainer: TPdfVar):pdfDataType;
var
Start: PtrUInt;
Ref: TPdfRef;
begin
Start := FCursor;
if GetUInt(Ref.FNum) and GetUInt(Ref.FGen) and Compare(' R') then Result:=TheContainer.Init(pdfRef,Ref)
else
begin
Result:=pdfInvalid;
Jump(Start)
end;
end;
function pdfParser.FindStartXRef: PtrUInt;
var i: Integer;
begin
Result := -1;
jump(BufferSize - 1);
repeat
while (Current <> 'f') do Prev;
if StrLComp(@FBuffer[FCursor - 8], 'startxref', 9) = 0 then break;
Prev;
until False;
if GetInteger(i) then Result :=i
else Result :=0;
end;
end.