-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpdfobjs.pas
566 lines (459 loc) · 13.2 KB
/
pdfobjs.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
unit pdfObjs;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
interface
type
pdfDataType = (
pdfNull=0,
pdfBool,
pdfInt,
pdfFloat,
pdfRef,
pdfStr,
pdfHexStr,
pdfName,
pdfArray,
pdfDict
);
PPdfObj = ^TPdfObj;
TPdfObj = packed record
private
FObjType: pdfDataType;
FRef: integer;
procedure Finalize;
public
class function NewNull: PPdfObj;static;
class function NewBool(AValue:Boolean) : PPdfObj;static;
class function NewInt(AValue: Integer): PPdfObj;static;
class function NewFloat(AValue: Double): PPdfObj;static;
class function NewRef(ANum,AGen: Cardinal): PPdfObj;static;
class function NewName(const S: String): PPdfObj;static;
class function NewName(S: PChar; ALen: Cardinal): PPdfObj;static;
class function NewStr(const S: String): PPdfObj;static;
class function NewStr(S: PChar; ALen: Cardinal): PPdfObj;static;
class function NewHexStr(const S: String): PPdfObj;static;
class function NewHexStr(S: PChar; ALen: Cardinal): PPdfObj;static;
class function NewArray(ACapacity: Cardinal=0): PPdfObj;static;
class function NewDictionary(ACapacity: Cardinal=0): PPdfObj;static;
class procedure Release(var Obj: PPdfObj);static;
procedure Release;
function Use: PPdfObj;
property ObjType: pdfDataType read FObjType;
function AddItem( AnItem: PPdfObj; Key: PPdfObj):boolean;
function AddItem( AnItem: PPdfObj):boolean;
function CompareTo( I : Integer ) : Integer; overload;
function CompareTo(const S: String ): Integer;overload;
function CompareTo(S: PChar; ALen: Integer): Integer;overload;
end;
implementation
type
// Records for type casting
pdfNullObj = TPdfObj;
pdfBoolObj = packed record
hdr : TPdfObj;
b : boolean;
end;
PPdfIntObj= ^pdfIntObj;
pdfIntObj = packed record
hdr : TPdfObj;
i : Integer;
class function SafeCast( Obj: TPdfObj): PPdfIntObj;static;
end;
pdfFloatObj = packed record
hdr : TPdfObj;
f : Double;
end;
pdfRefObj = packed record
hdr : TPdfObj;
num,gen : Cardinal;
end;
PPdfStrObj = ^pdfStrObj;
pdfStrObj = packed record
hdr : TPdfObj;
len : Cardinal;
chars : Array[00..00] of char;
class function SafeCast( Obj: TPdfObj): PPdfStrObj;static;
end;
PPdfArrayObj=^pdfArrayObj;
pdfArrayObj = packed record
hdr: TPdfObj;
len,
capacity: cardinal;
items: ^PPdfObj;
class function SafeCast( Obj: TPdfObj): PPdfArrayObj;static;
end;
pdfDictItem = packed record
key,value: PPdfObj;
end;
PPdfDictObj= ^pdfDictObj;
pdfDictObj = packed record
hdr: TPdfObj;
len,
capacity: cardinal;
items: ^pdfDictItem;
class function SafeCast( Obj: TPdfObj): PPdfDictObj;static;
end;
{Global Hash table to store pdf Name Objects}
// Record to keep collisions in hash table
PSlotRec = ^TSlotRec;
TSlotRec = record
FCount,
FCapacity: PtrUInt;
FNames: ^PPdfObj;
Procedure Init;
procedure Done;
procedure Add(Obj: PPdfObj);
end;
TPdfNames= class
FSlots: PSlotRec;
FSize,
FCount,
FCapacity: PtrUInt;
constructor Create(AHashSize: PtrUInt=0);
Destructor Destroy;
function Add(S: PChar; ALen: Integer): PPdfObj;
end;
function Min(a, b: Integer): Integer;inline;
begin
if a < b then Result := a
else Result := b;
end;
function Max(a, b: Integer): Integer;inline;
begin
if a > b then Result := a
else Result := b;
end;
{inline SMP check and normal lock.
the locked one is so slow, inlining doesn't matter}
function declocked(var l : longint) : boolean; inline;
begin
if ismultithread then l:=InterLockedDecrement(l)
else dec(l);
declocked:=l=0;
end;
procedure inclocked(var l : longint); inline;
begin
if ismultithread then InterLockedIncrement(l)
else inc(l);
end;
{ PDF Objects }
var
// Singletons
TheNullObj: pdfNullObj = (FObjType:pdfNull; FRef:-1);
TheTrueObj: pdfBoolObj = (hdr:(FObjType:pdfBool; FRef:-1); b: true);
TheFalseObj: pdfBoolObj = (hdr:( FObjType:pdfBool;FRef:-1); b: false);
TheNamesManager: TPdfNames = nil;
const
DefaultDelta = 8;
class function pdfArrayObj.SafeCast( Obj: TPdfObj): PPdfArrayObj;inline;
begin
if (@Obj=nil) or (obj.ObjType<>pdfArray) then result:=nil
else result:=PPdfArrayObj(pointer(@Obj));
end;
class function pdfDictObj.SafeCast( Obj: TPdfObj): PPdfDictObj;inline;
begin
if (@Obj=nil) or (obj.ObjType<>pdfDict) then result:=nil
else result:=PPdfDictObj(pointer(@Obj));
end;
class function pdfStrObj.SafeCast( Obj: TPdfObj): PPdfStrObj;inline;
begin
if (@Obj=nil) or
not (obj.ObjType in [pdfStr,pdfHexStr,pdfName])
then result:=nil
else result:=PPdfStrObj(pointer(@Obj));
end;
class function pdfIntObj.SafeCast( Obj: TPdfObj): PPdfIntObj;inline;
begin
if (@Obj=nil) or (Obj.ObjType<>pdfInt) then Result:=nil
else result:=PPdfIntObj(pointer(@Obj));
end;
class function TPdfObj.NewNull: PPdfObj;inline;
begin
Result:= @TheNullObj;
end;
class function TPdfObj.NewBool(AValue: Boolean):PPdfObj;inline;
begin
if AValue then Result:=@TheTrueObj
else Result:=@TheFalseObj;
end;
class function TPdfObj.NewInt(AValue: Integer):PPdfObj;
begin
Result := GetMem(SizeOf(pdfIntObj));
with pdfIntObj(Pointer(Result)^) do
begin
hdr.FRef:=1;
hdr.FObjType:=pdfInt;
i:=AValue;
end;
end;
class function TPdfObj.NewFloat(AValue: Double): PPdfObj;
begin
Result := GetMem(SizeOf(pdfFloatObj));
with pdfFloatObj(Pointer(Result)^) do
begin
hdr.FRef:=1;
hdr.FObjType:=pdfFloat;
f:=AValue;
end;
end;
class function TPdfObj.NewRef(ANum,AGen: Cardinal): PPdfObj;static;
begin
Result := GetMem(SizeOf(pdfRefObj));
with pdfRefObj(Pointer(Result)^) do
begin
hdr.FRef:=1;
hdr.FObjType:=pdfRef;
num:=ANum;
gen:=AGen;
end;
end;
function NewStringObject(ObjType: pdfDataType; S: PChar; ALen: Cardinal): PPdfObj;
begin
Result := GetMem(SizeOf(pdfStrObj)+ALen);
with pdfStrObj(Pointer(Result)^) do
begin
hdr.FRef:=1;
hdr.FObjType:=ObjType;
len:=ALen;
Move(S^,chars[0],len);
Chars[len]:=#0;
end;
end;
class function TPdfObj.NewName(S: PChar; ALen: Cardinal): PPdfObj;inline;
begin
if TheNamesManager=nil then Result:=NewStringObject(pdfName,S,ALen)
else Result:=TheNamesManager.Add(S,ALen)^.Use;
end;
class function TPdfObj.NewName(const S: String): PPdfObj;inline;
begin
Result:=NewName(@S[1],system.Length(S));
end;
class function TPdfObj.NewStr(S: PChar; ALen: Cardinal): PPdfObj;inline;
begin
Result:=NewStringObject(pdfStr,S,ALen);
end;
class function TPdfObj.NewStr(const S: String): PPdfObj;inline;
begin
Result:=NewStringObject(pdfStr,@S[1],system.Length(S));
end;
class function TPdfObj.NewHexStr(S: PChar; ALen: Cardinal): PPdfObj;inline;
begin
Result:=NewStringObject(pdfHexStr,S,ALen);
end;
class function TPdfObj.NewHexStr(const S: String): PPdfObj;inline;
begin
Result:=NewStringObject(pdfHexStr,@S[1],system.Length(S));
end;
class function TPdfObj.NewArray(ACapacity: Cardinal=0): PPdfObj;
begin
Result := GetMem(SizeOf(pdfArrayObj));
with pdfArrayObj(Pointer(Result)^) do
begin
hdr.FRef := 1;
hdr.FObjType := pdfArray;
len:=0;
if ACapacity=0 then ACapacity:=DefaultDelta;
capacity := ACapacity;
if capacity<>0 then items := Getmem(capacity*sizeOf(PPdfObj))
else items := nil;
end;
end;
class function TPdfObj.NewDictionary(ACapacity: Cardinal=0): PPdfObj;
begin
Result := GetMem(SizeOf(pdfDictObj));
with pdfDictObj(Pointer(Result)^) do
begin
hdr.FRef := 1;
hdr.FObjType := pdfDict;
len:=0;
if ACapacity=0 then ACapacity:=DefaultDelta;
capacity := ACapacity;
if capacity<>0 then items := Getmem(capacity*sizeOf(pdfDictItem))
else items := nil;
end;
end;
procedure TPdfObj.Finalize;
var i: integer;
begin
Case objType of
pdfArray: with pdfArrayObj(Pointer(@self)^) do
if Assigned(items) then
begin
for i:=00 to len-1 do items[i]^.Release;
Freemem(items);
end;
pdfDict: with pdfDictObj(Pointer(@self)^) do
if assigned(items) then
begin
for i:=00 to len-1 do
begin
items[i].key^.Release;
items[i].value^.Release;
end;
Freemem(items);
end;
end;
Freemem(@Self);
end;
procedure TPdfObj.Release;
begin
if not Assigned(@Self) then exit;
if FRef<0 then exit;
if declocked(FRef) then Finalize;
end;
class procedure TPdfObj.Release(var Obj: PPdfObj);
begin
Obj^.Release;
Obj:=nil;
end;
function TPdfObj.Use: PPdfObj ;inline;
begin
if FRef>0 then inclocked(FRef);
Result:=@Self;
end;
function TPdfObj.AddItem( AnItem: PPdfObj):boolean;
var A: PPdfArrayObj;
begin
A:= pdfArrayObj.SafeCast(Self);
if A<>nil then with A^ do
begin
inc(len);
if len>=capacity then
begin
inc(capacity,DefaultDelta);
items:=ReAllocMem(items,capacity*sizeOf(PPdfObj));
end;
items[len]:=AnItem;
Result:=true;
end
else result := false
end;
function TPdfObj.AddItem( AnItem: PPdfObj; Key: PPdfObj):boolean;
var D: PPdfDictObj;
begin
D:=pdfDictObj.SafeCast(Self);
if (D=nil) or
(AnItem=nil) or
(Key=nil) or
(Key^.ObjType<>pdfName) then Result := false
else with D^ do
begin
inc(len);
if len>=capacity then
begin
inc(capacity,DefaultDelta);
items:=ReAllocMem(items,capacity*sizeOf(pdfDictItem));
end;
items[len].value:=AnItem;
items[len].key:=Key;
Result:=true
end
end;
function TPdfObj.CompareTo( I : Integer): Integer;
var IObj: PPdfIntObj;
begin
IObj:=pdfIntObj.SafeCast(Self);
if IObj = nil then exit (2); // not a string object
if IObj^.i=i then exit (0);
if IObj^.i<i then exit(-1)
else Result:=1;
end;
function TPdfObj.CompareTo(S: PChar; ALen: Integer): Integer;
var Str: PPdfStrObj;
begin
Str:=pdfStrObj.SafeCast(Self);
if Str = nil then exit (2); // not a string object
Result:=CompareChar(S[0],Str^.chars[0],min(ALen,Str^.len))
end;
function TPdfObj.CompareTo(const S: String): Integer;inline;
begin
CompareTo(@S[1],system.Length(S));
end;
// TPdfNames
const GrowDelta=16;
DefHashSizeIndex=4;
PrimesArray:array[00..25] of DWord=(53,97,193,389,769,1543,3079,6151,12289,
24593,49157,98317,196613,393241,786433,
1572869,3145739,6291469,12582917,25165843,
50331653,100663319,201326611,402653189,
805306457,1610612741);
// Hash Slots
procedure TSlotRec.Init;
begin
FCount:=0;
FCapacity:=0;
FNames:=nil;
end;
procedure TSlotRec.Done;
var i : integer;
begin
if FNames<> nil then
begin
for i:=0 to FCount-1 do TPdfObj.Release(FNames[i]);
FreeMem(FNames);
FNames:=nil;
end;
FCount:=0;
FCapacity:=0;
end;
procedure TSlotRec.Add(Obj: PPdfObj);
begin
if FCapacity<=FCount then
begin
Inc(FCapacity,GrowDelta);
FNames:=ReAllocMem(FNames,FCapacity);
end;
FNames[FCount]:=Obj;
Inc(FCount);
end;
constructor TPdfNames.Create(AHashSize: PtrUInt=0);
var i : Integer;
begin
Inherited Create;
if AHashSize=0 then AHashSize:=DefHashSizeIndex;
FSize:=PrimesArray[AHashSize];
FSlots:=GetMem(FSize*SizeOf(PSlotRec));
for i:=0 to FSize-1 do FSlots[i].Init;
FCapacity:=0;
FCount:=0;
end;
destructor TPdfNames.Destroy;
var i : Integer;
begin
for i:=0 to FSize-1 do FSlots[i].Done;
inherited Destroy;
end;
function TPdfNames.Add(S: PChar; ALen: Integer): PPdfObj;
var H: DWord;
Slot: PtrUInt;
i: Integer;
N: PPdfObj;
function Hash:DWord; // FNV1a hash function
var j: integer;
const
FNV_offset_basis = 2166136261;
FNV_prime = 16777619;
begin
Result := FNV_offset_basis;
for j := 0 to ALen-1 do
Result := (Result xor Byte(s[j])) * FNV_prime;
end;
begin
H:=Hash;
Slot:=H mod FSize;
Result:=nil;
for i:=0 to FSlots[Slot].FCount-1 do
begin
N:=FSlots[Slot].FNames[i];
if N^.CompareTo(S)=0 then exit(N); //found in hash
end;
Result:=NewStringObject(pdfName,S,ALen);
FSlots[Slot].Add(Result);
Inc(FCount);;
end;
initialization
TheNamesManager:= TPdfNames.Create;
finalization
TheNamesManager.Free;
THeNamesManager:=nil;
end.