-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oodle.pas
743 lines (627 loc) · 21.4 KB
/
Oodle.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
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
unit Oodle;
{
Oodle implementation
TetzkatLipHoka - 2023
}
interface
{$DEFINE AUTO_INIT} // AutoInit/DeInit Oodle(s) during initialization/finalization
{.$DEFINE VERCHECK} // Perform Version-Check and inform on missmatch
{$DEFINE OnlyWarnOnLowerVersions}
{.$DEFINE SILENT} // Don't show any warnings (missing Oodle, Version-Missmatch) but Linking Errors
{.$DEFINE CLOSE_APP_ON_FAIL}
{$DEFINE WARN_DLLs_IN_FOLDER} // Warning if DLLs are present in Application-Folder
{.$DEFINE IGNORE_LINKING_ERRORS} // Ignore linking errors (DEBUG)
{.$DEFINE LIST_MISSING_MODULES} // uses MemoryModule
// ResourceFile
{$DEFINE ResourceMode} // Load DLL from Resourcefile
{$IFDEF ResourceMode}
// {$R Oodle.res} // DLL-ResourceFile (each DLLName without extension as RCDATA)
{$IFDEF Win64}
{$R oo2core_7_win64.res} // DLL-ResourceFile (each DLLName without extension as RCDATA)
{$ELSE}
{$R oo2core_5_win32.res} // DLL-ResourceFile (each DLLName without extension as RCDATA)
{$ENDIF}
{$ENDIF}
{$DEFINE ResourceCompression} // DLLs in ResourceFile are 7zip compressed (JEDI-Unit)
{$DEFINE PREFER_DLL_IN_FOLDER} // If DLL is present in ApplicationFolder use it
{$DEFINE MemoryModule} // use MemoryModule (Load without HDD-caching)
{$DEFINE GetModuleHandle} // Try GetModuleHandle before LoadLibrary
{.$DEFINE ONLY_LOADLIBRARY_ERRORS} // Ignore 'GetLastError <> ERROR_SUCCESS' unless LoadLibrary actually failed
{.$DEFINE NOAUTO_DEINIT}
{.$DEFINE TESTCASE}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$DEFINE section_INTERFACE_USES}
{$I DynamicDLL.inc}
{$UNDEF section_INTERFACE_USES}
,types
;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$DEFINE section_INTERFACE}
{$I DynamicDLL.inc}
{$UNDEF section_INTERFACE}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Constantes~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const
DLLCount_ = 1;
var
DLLPath_ : Array [0..DLLCount_-1] of String = ( '' );
const
DLLLoadDLL_ : Array [0..DLLCount_-1] of boolean = ( True );
{$IFDEF Win64}
DLLName_ : Array [0..DLLCount_-1] of String = ( 'oo2core_7_win64.dll' );
{$ELSE}
DLLName_ : Array [0..DLLCount_-1] of String = ( 'oo2core_5_win32.dll' );
{$ENDIF}
DLLVersion_ : Array [0..DLLCount_-1] of String = ( '' );
{$IFDEF ResourceMode}
DLLPass_ : Array [0..DLLCount_-1] of String = ( '' );
{$ENDIF}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Oodle Declarations~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$WARNINGS OFF}
{$DEFINE section_Declaration} // Include
{.$I Oodle.inc}
{$UNDEF section_Declaration}
type
{$IF CompilerVersion < 23}
NativeUInt = Cardinal;
PNativeUInt = ^NativeUInt;
{$IFEND}
Toodle_NoAssert = function( const a : PAnsiChar; const b : Integer; const c : PAnsiChar; const d : PAnsiChar ) : Integer; stdcall;
TOodleAlgo = (
oaLZH = 0,
oaLZHLW = 1,
oaLZNIB = 2,
oaNone = 3,
oaLZB16 = 4,
oaLZBLW = 5,
oaLZA = 6,
oaLZNA = 7,
oaKraken = 8,
oaMermaid = 9,
oaBitKnit = 10,
oaSelkie = 11,
oaAkkorokamui = 12,
oaLZQ1 = 8, // old name of Kraken
oaLZNIB2 = 9 // old name of Mermaid
);
TOodleCompressionLevel = (
olNone = 0,
olSuperFast = 1,
olVeryFast = 2,
olFast = 3,
olNormal = 4,
olOptimal1 = 5,
olOptimal2 = 6,
olOptimal3 = 7,
olOptimal4 = 8,
olOptimal5 = 9
);
const
OodleAlgoName : Array [ oaLZH..oaAkkorokamui ] of AnsiString = (
'LZH',
'LZHLW',
'LZNIB',
'None', // 0x8c->0xcc
'LZB16',
'LZBLW',
'LZA',
'LZNA',
'Kraken',
'Mermaid',
'BitKnit',
'Selkie',
'Akkorokamui'
// 'LZQ1', // old name of Kraken
// 'LZNIB2' // old name of Mermaid
);
OodleAlgoRAW : Array [ oaLZH..oaAkkorokamui ] of Byte = (
7,
0,
1,
7,
2,
3,
4,
5,
6,
10,
11,
10,
6
);
var
OodleLZ_Compress : function( Algo : Cardinal; Source : PByte; Size : NativeUInt; Destination : PByte; CompressionLevel : Cardinal; A : Pointer = nil; B : Pointer = nil; C : Pointer = nil ) : Integer; stdcall = NIL;
OodleLZ_Decompress : function( Source : PByte; Size : NativeUInt; Destination : PByte; DestSize : NativeUInt; A : Integer; B : Integer; C : Integer; D : Pointer = nil; E : Pointer = nil; F : Pointer = nil; G : Pointer = nil; H : Pointer = nil; I : Pointer = nil; ThreadModule : Integer = 3 ) : Integer; stdcall = NIL; // Oodle 2.3.0
OodlePlugins_SetAssertion : procedure( func : Toodle_NoAssert ); stdcall = NIL;
function OodlePlugins_NoAssert : Integer; stdcall;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CompressOodle( Source : PByte; Len : Cardinal; var Compressed : TByteDynArray; Algo : TOodleAlgo = oaLZNA; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False ) : Int64; overload;
{$IFNDEF Win64}
function CompressOodle( Source : PByte; Len : Cardinal; var Compressed : Pointer; Algo : TOodleAlgo = oaLZNA; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False ) : Int64; overload;
{$ENDIF}
function ExtractOodle( Source : PByte; Len : Cardinal; var Decompressed : TByteDynArray ) : Int64; overload;
{$IFNDEF Win64}
function ExtractOodle( Source : PByte; Len : Cardinal; var Decompressed : Pointer ) : Int64; overload;
{$ENDIF}
function CompressStreamOodle( Source : TStream; Compressed : TStream; Algo : TOodleAlgo = oaLZNA; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False ) : Int64; overload;
function CompressStreamOodle( var Source : TStream; Algo : TOodleAlgo = oaLZNA; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False ) : Int64; overload;
function ExtractStreamOodle( Source : TStream; DeCompressed : TStream ) : Int64; overload;
function ExtractStreamOodle( var Source : TStream ) : Int64; overload;
{$IFDEF TESTCASE}
function TestOodle( FileName : string; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False; SaveDebugFiles : Boolean = False ) : Int64;
{$ENDIF TESTCASE}
{$WARNINGS ON}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
implementation
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$DEFINE section_IMPLEMENTATION_USES}
{$I DynamicDLL.inc}
{$UNDEF section_IMPLEMENTATION_USES}
{$IFDEF TESTCASE}
,Dialogs
{$ENDIF TESTCASE}
;
{$DEFINE section_IMPLEMENTATION_INITVAR}
{$I DynamicDLL.inc}
{$UNDEF section_IMPLEMENTATION_INITVAR}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure PreInitialization;
begin
// Code needed before Init, likely change Oodle OS-Dependent
end;
procedure InitDLL( ID : Byte; StrL : TStringList );
begin
{$DEFINE section_InitVar}
{$WARNINGS OFF}
case ID of
0 : begin
{.$I Oodle.inc} // Include
{$IFDEF Win64}
InitVar( @OodleLZ_Compress, 'OodleLZ_Compress', ID, StrL );
InitVar( @OodleLZ_Decompress, 'OodleLZ_Decompress', ID, StrL );
InitVar( @OodlePlugins_SetAssertion, 'OodleCore_Plugins_SetAssertion', ID, StrL );
// InitVar( @OodlePlugins_SetAssertion, 'OodlePlugins_SetAssertion', ID, StrL );
{$ELSE}
InitVar( @OodleLZ_Compress, '_OodleLZ_Compress@32', ID, StrL );
InitVar( @OodleLZ_Decompress, '_OodleLZ_Decompress@56', ID, StrL );
InitVar( @OodlePlugins_SetAssertion, '_OodlePlugins_SetAssertion@4', ID, StrL );
{$ENDIF}
end;
end;
{$WARNINGS ON}
{$UNDEF section_InitVar}
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Redirects~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$DEFINE section_Redirects}
{.$I Oodle.inc}
{$UNDEF section_Redirects}
function OodlePlugins_NoAssert : Integer; stdcall;
begin
result := 0;
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$WARN UNSAFE_CODE OFF}
{$WARN UNSAFE_TYPE OFF}
const
HEADER_ = AnsiString( 'oodle' );
HEADER_LEN_ = Length( HEADER_ );
type
tHeader = Array [ 0..HEADER_LEN_-1 ] of AnsiChar;
pHeader = ^tHeader;
function CompressOodle( Source : PByte; Len : Cardinal; var Compressed : TByteDynArray; Algo : TOodleAlgo = oaLZNA; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False ) : Int64;
var
Offset : Cardinal;
tLen : Cardinal;
begin
result := -102;
if NOT Oodle.IsInitDLL then
Exit;
result := -101;
if NOT Assigned( Source ) OR ( Len = 0 ) then
Exit;
SetLength( Compressed, 0 );
result := -100;
if ( Algo = oaNone ) OR ( ( Algo = oaKraken ) AND ( Len < 500*1024 ) ) then
Exit;
if Header then
Offset := HEADER_LEN_+SizeOf( Len )
else
Offset := SizeOf( Len );
// GetCompressionBound
tLen := Trunc( Len + 274 * ( ( Len + $3FFFF ) / $40000 ) );
SetLength( Compressed, Offset+tLen );
if Header then
begin
Move( HEADER_[ 1 ], Compressed[ 0 ], HEADER_LEN_ );
Move( Len, Compressed[ HEADER_LEN_ ], SizeOf( Len ) );
end
else
Move( Len, Compressed[ 0 ], SizeOf( Len ) );
result := OodleLZ_Compress( Cardinal( Algo ), Source, Len, @Compressed[ Offset ], Cardinal( CompressionLevel ), nil, nil, nil );
if ( result > 0 ) then
begin
Result := Offset+result;
SetLength( Compressed, result );
end
else
SetLength( Compressed, 0 );
end;
{$IFNDEF Win64}
function CompressOodle( Source : PByte; Len : Cardinal; var Compressed : Pointer; Algo : TOodleAlgo = oaLZNA; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False ) : Int64;
var
bCompressed : PByte;
Offset : Cardinal;
tLen : Cardinal;
begin
result := -102;
if NOT Oodle.IsInitDLL then
Exit;
result := -101;
if NOT Assigned( Source ) OR ( Len = 0 ) then
Exit;
if Assigned( Compressed ) then
ReallocMem( Compressed, 0 );
result := -100;
if ( Algo = oaNone ) OR ( ( Algo = oaKraken ) AND ( Len < 500*1024 ) ) then
Exit;
if Header then
Offset := HEADER_LEN_+SizeOf( Len )
else
Offset := SizeOf( Len );
// GetCompressionBound
tLen := Trunc( Len + 274 * ( ( Len + $3FFFF ) / $40000 ) );
ReallocMem( Compressed, Offset+tLen );
bCompressed := Compressed;
if Header then
begin
Move( HEADER_[ 1 ], bCompressed^, HEADER_LEN_ );
Inc( bCompressed, HEADER_LEN_ );
end;
Move( Len, bCompressed^, SizeOf( Len ) );
Inc( bCompressed, SizeOf( Len ) );
result := OodleLZ_Compress( Cardinal( Algo ), Source, Len, bCompressed, Cardinal( CompressionLevel ), nil, nil, nil );
if ( result > 0 ) then
begin
Result := Offset+result;
ReallocMem( Compressed, result );
end
else
ReallocMem( Compressed, 0 );
end;
{$ENDIF}
function CompressStreamOodle( Source : TStream; Compressed : TStream; Algo : TOodleAlgo = oaLZNA; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False ) : Int64;
var
S : TMemoryStream;
tmp : TByteDynArray;
begin
if ( Source = Compressed ) then
begin
result := CompressStreamOodle( Source, Algo, CompressionLevel, Header );
Exit;
end;
result := -4;
if NOT Assigned( Source ) then
Exit;
result := -3;
if ( Source.Size <= 0 ) then
Exit;
result := -2;
if NOT Assigned( Compressed ) then
Exit;
S := TMemoryStream.Create;
S.CopyFrom( Source, Source.Size-Source.Position );
S.Position := 0;
SetLength( tmp, S.Size );
Result := CompressOodle( S.Memory, S.Size, tmp, Algo, CompressionLevel, Header );
if ( result > 0 ) then
begin
// Compressed.Position := 0;
// Compressed.Size := 0;
Compressed.Write( tmp[ 0 ], result );
end
else
result := -1;
SetLength( tmp, 0 );
S.Free;
end;
function CompressStreamOodle( var Source : TStream; Algo : TOodleAlgo = oaLZNA; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False ) : Int64;
var
S : TMemoryStream;
tmp : TByteDynArray;
begin
result := -3;
if NOT Assigned( Source ) then
Exit;
result := -2;
if ( Source.Size <= 0 ) then
Exit;
S := TMemoryStream.Create;
S.CopyFrom( Source, Source.Size-Source.Position );
S.Position := 0;
SetLength( tmp, S.Size );
Result := CompressOodle( S.Memory, S.Size, tmp, Algo, CompressionLevel, Header );
if ( result > 0 ) then
begin
Source.Position := 0;
Source.Size := 0;
Source.Write( tmp[ 0 ], result );
end
else
result := -1;
SetLength( tmp, 0 );
S.Free;
end;
function ExtractStreamOodle( Source : TStream; DeCompressed : TStream ) : Int64;
var
S : TMemoryStream;
tmp : TByteDynArray;
begin
if ( Source = DeCompressed ) then
begin
result := ExtractStreamOodle( Source );
Exit;
end;
result := -4;
if NOT Assigned( Source ) then
Exit;
result := -3;
if ( Source.Size <= 0 ) then
Exit;
result := -2;
if NOT Assigned( DeCompressed ) then
Exit;
S := TMemoryStream.Create;
S.CopyFrom( Source, Source.Size-Source.Position );
S.Position := 0;
SetLength( tmp, S.Size );
Result := ExtractOodle( S.Memory, S.Size, tmp );
if ( result > 0 ) then
begin
// Compressed.Position := 0;
// Compressed.Size := 0;
DeCompressed.Write( tmp[ 0 ], result );
end
else
result := -1;
SetLength( tmp, 0 );
S.Free;
end;
function ExtractStreamOodle( var Source : TStream ) : Int64;
var
S : TMemoryStream;
tmp : TByteDynArray;
begin
result := -3;
if NOT Assigned( Source ) then
Exit;
result := -2;
if ( Source.Size <= 0 ) then
Exit;
S := TMemoryStream.Create;
S.CopyFrom( Source, Source.Size-Source.Position );
S.Position := 0;
SetLength( tmp, S.Size );
Result := ExtractOodle( S.Memory, S.Size, tmp );
if ( result > 0 ) then
begin
Source.Position := 0;
Source.Size := 0;
Source.Write( tmp[ 0 ], result );
end
else
result := -1;
SetLength( tmp, 0 );
S.Free;
end;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ExtractOodle( Source : PByte; Len : Cardinal; var Decompressed : TByteDynArray ) : Int64;
var
OutSize : Cardinal;
begin
result := -101;
if NOT Oodle.IsInitDLL then
Exit;
result := -100;
if NOT Assigned( Source ) OR ( Len = 0 ) then
Exit;
SetLength( Decompressed, 0 );
if ( pHeader( Source )^ = HEADER_ ) then
begin
Inc( Source, HEADER_LEN_ );
Dec( Len, HEADER_LEN_ );
end;
OutSize := PCardinal( Source )^;
Inc( Source, SizeOf( OutSize ) );
Dec( Len, SizeOf( OutSize ) );
// OutSize := Len*4;
// GetDecompressionBound
// OutSize := Trunc( bufferSize + 16731 + 2 * ( Len + $3FFFF ) / $40000 );
// if ( OutSize < Len + 272 ) then
// OutSize := Len + 272;
SetLength( Decompressed, OutSize );
result := OodleLZ_Decompress( Source, Len, @Decompressed[ 0 ], OutSize, 0, 0, 0, nil, nil, nil, nil, nil, nil, 3 );
if ( result = OutSize ) then
begin
// result := OutSize;
SetLength( Decompressed, result );
end
else
SetLength( Decompressed, 0 );
end;
{$IFNDEF Win64}
function ExtractOodle( Source : PByte; Len : Cardinal; var Decompressed : Pointer ) : Int64;
var
OutSize : Cardinal;
begin
result := -101;
if NOT Oodle.IsInitDLL then
Exit;
result := -100;
if NOT Assigned( Source ) OR ( Len = 0 ) then
Exit;
if Assigned( Decompressed ) then
ReallocMem( Decompressed, 0 );
if ( pHeader( Source )^ = HEADER_ ) then
begin
Inc( Source, HEADER_LEN_ );
Dec( Len, HEADER_LEN_ );
end;
OutSize := PCardinal( Source )^;
Inc( Source, SizeOf( OutSize ) );
Dec( Len, SizeOf( OutSize ) );
// OutSize := Len*4;
// GetDecompressionBound
// OutSize := Trunc( bufferSize + 16731 + 2 * ( Len + $3FFFF ) / $40000 );
// if ( OutSize < Len + 272 ) then
// OutSize := Len + 272;
GetMem( Decompressed, OutSize );
result := OodleLZ_Decompress( Source, Len, Decompressed, OutSize, 0, 0, 0, nil, nil, nil, nil, nil, nil, 3 );
if ( result = OutSize ) then
begin
result := OutSize;
ReallocMem( Decompressed, result )
end
else
ReallocMem( Decompressed, 0 );
end;
{$ENDIF Win64}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$IFDEF TESTCASE}
function TestOodle( FileName : string; CompressionLevel : TOodleCompressionLevel = olOptimal3; Header : boolean = False; SaveDebugFiles : Boolean = False ) : Int64;
var
sIn : TMemoryStream;
function PointerToArray( Algo : TOodleAlgo ) : Int64;
var
sCompress, sOut : TMemoryStream;
aDecompressed : TByteDynArray;
begin
SetLength( aDecompressed, 0 );
sCompress := TMemoryStream.Create;
result := CompressOodle( sIn.Memory, sIn.Size, aDecompressed, Algo, CompressionLevel, Header );
if ( result <= 0 ) then
begin
sCompress.free;
result := -3;
Exit;
end;
sCompress.Write( aDecompressed[ 0 ], Length( aDecompressed ) );
SetLength( aDecompressed, 0 );
if SaveDebugFiles then
sCompress.SaveToFile( ChangeFileExt( FileName, '.' + OodleAlgoName[ Algo ] ) );
sOut := TMemoryStream.Create;
sCompress.Position := 0;
SetLength( aDecompressed, 0 );
result := ExtractOodle( sCompress.Memory, sCompress.Size, aDecompressed );
if ( result <= 0 ) then
begin
sOut.free;
sCompress.free;
result := -2;
Exit;
end;
sOut.Write( aDecompressed[ 0 ], Length( aDecompressed ) );
SetLength( aDecompressed, 0 );
if ( sIn.Size <> sOut.Size ) OR NOT CompareMem( sIn.Memory, sOut.Memory, sIn.Size ) then
result := -1;
if SaveDebugFiles then
sOut.SaveToFile( ChangeFileExt( FileName, '.extract' + OodleAlgoName[ Algo ] ) );
sOut.free;
sCompress.free;
end;
{$IFNDEF Win64}
function PointerToPointer( Algo : TOodleAlgo ) : Int64;
var
sCompress, sOut : TMemoryStream;
pDecompressed : Pointer;
begin
pDecompressed := nil;
sCompress := TMemoryStream.Create;
result := CompressOodle( sIn.Memory, sIn.Size, pDecompressed, Algo, CompressionLevel, Header );
if ( result <= 0 ) then
begin
sCompress.free;
result := -3;
Exit;
end;
sCompress.Write( pDecompressed^, result );
ReallocMem( pDecompressed, 0 );
if SaveDebugFiles then
sCompress.SaveToFile( ChangeFileExt( FileName, '.' + OodleAlgoName[ Algo ] ) );
sOut := TMemoryStream.Create;
sCompress.Position := 0;
pDecompressed := nil;
result := ExtractOodle( sCompress.Memory, sCompress.Size, pDecompressed );
if ( result <= 0 ) then
begin
sOut.free;
sCompress.free;
result := -2;
Exit;
end;
sOut.Write( PByte( pDecompressed )^, result );
ReallocMem( pDecompressed, 0 );
if ( sIn.Size <> sOut.Size ) OR NOT CompareMem( sIn.Memory, sOut.Memory, sIn.Size ) then
result := -1;
if SaveDebugFiles then
sOut.SaveToFile( ChangeFileExt( FileName, '.extract' + OodleAlgoName[ Algo ] ) );
sOut.free;
sCompress.free;
end;
{$ENDIF Win64}
var
i : TOodleAlgo;
begin
result := -999;
if NOT FileExists( FileName ) then
Exit;
sIn := TMemoryStream.Create;
sIn.LoadFromFile( FileName );
sIn.Position := 0;
for i := Low( TOodleAlgo ) to High( TOodleAlgo ) do
begin
if ( i = oaNone ) then
Continue;
result := PointerToArray( i );
case result of
-999 : ShowMessage( 'PointerToArray: Invalid File (' + OodleAlgoName[ i ] + ')' );
-3 : ShowMessage( 'PointerToArray: Failed compress (' + OodleAlgoName[ i ] + ')' );
-2 : ShowMessage( 'PointerToArray: Failed decompress (' + OodleAlgoName[ i ] + ')' );
-1 : ShowMessage( 'PointerToArray: Size invalid or CompareMem failed (' + OodleAlgoName[ i ] + ')' );
0 : ShowMessage( 'PointerToArray: Failed to Extract (' + OodleAlgoName[ i ] + ')' );
// else
// ShowMessage( 'PointerToArray: OK' );
end;
// if ( result < 0 ) then
// begin
// sIn.Free;
// Exit;
// end;
{$IFNDEF Win64}
result := PointerToPointer( i );
case result of
-999 : ShowMessage( 'PointerToPointer: Invalid File (' + OodleAlgoName[ i ] + ')' );
-3 : ShowMessage( 'PointerToPointer: Failed compress (' + OodleAlgoName[ i ] + ')' );
-2 : ShowMessage( 'PointerToPointer: Failed decompress (' + OodleAlgoName[ i ] + ')' );
-1 : ShowMessage( 'PointerToPointer: Size invalid or CompareMem failed (' + OodleAlgoName[ i ] + ')' );
0 : ShowMessage( 'PointerToPointer: Failed to Extract (' + OodleAlgoName[ i ] + ')' );
// else
// ShowMessage( 'PointerToPointer: OK' );
end;
// if ( result < 0 ) then
// begin
// sIn.Free;
// Exit;
// end;
{$ENDIF Win64}
end;
sIn.free;
end;
{$ENDIF TESTCASE}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{$DEFINE section_IMPLEMENTATION}
{$I DynamicDLL.inc}
{$UNDEF section_IMPLEMENTATION}
end.