forked from synopse/mORMot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynSQLite3Static.pas
1308 lines (1179 loc) · 48.5 KB
/
SynSQLite3Static.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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// SQLite3 3.44.2 Database engine - statically linked for Windows/Linux
// - this unit is a part of the freeware Synopse mORMot framework,
// licensed under a MPL/GPL/LGPL tri-license; version 1.18
unit SynSQLite3Static;
{
This file is part of Synopse mORMot framework.
Synopse mORMot framework. Copyright (c) Arnaud Bouchez
Synopse Informatique - https://synopse.info
*** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (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.mozilla.org/MPL
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
The Original Code is Synopse mORMot framework.
The Initial Developer of the Original Code is Arnaud Bouchez.
Portions created by the Initial Developer are Copyright (c)
the Initial Developer. All Rights Reserved.
Contributor(s):
- Alfred Glaenzer (alf)
- Maciej Izak (hnb)
Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****
Statically linked SQLite3 3.44.2 engine with optional AES encryption
**********************************************************************
To be declared in your project uses clause: will fill SynSQlite3.sqlite3
global variable with all statically linked .obj API entries.
sqlite3 := TSQLite3LibraryStatic.Create; is called at unit initialization.
Will work on Windows 32-bit or 64-bit (with Delphi or FPC, with expected
.obj / .o) or Linux 32-bit 64-bit on Intel and ARM (with FPC, with the
corresponding .o) under other platforms, this unit will just do nothing
(but compile).
To patch and compile the official SQlite3 amalgamation file, follow the
instruction from SQLite3\amalgamation\ReadMe.md
Uses TSQLite3LibraryDynamic to access external library (e.g. sqlite3.dll/.so)
}
{$I Synopse.inc} // define HASINLINE CPU32 CPU64 OWNNORMTOUPPER
interface
{$ifdef NOSQLITE3STATIC} // conditional defined -> auto-load local .dll/.so
uses
SysUtils,
SynSQLite3;
implementation
uses
SynCommons;
procedure DoInitialization;
begin
FreeAndNil(sqlite3);
try
sqlite3 := TSQLite3LibraryDynamic.Create(SQLITE_LIBRARY_DEFAULT_NAME);
sqlite3.ForceToUseSharedMemoryManager; // faster process
except
on E: Exception do
{$ifdef LINUX}
writeln(SQLITE_LIBRARY_DEFAULT_NAME+' initialization failed with ',
E.ClassName,': ',E.Message);
{$endif}
end;
end;
initialization
DoInitialization;
{$else NOSTATIC}
uses
{$ifdef MSWINDOWS}
Windows,
{$else}
{$ifdef FPC}
SynFPCLinux,
BaseUnix,
{$endif}
{$ifdef KYLIX3}
Types,
LibC,
SynKylix,
{$endif}
{$endif}
Classes,
SysUtils,
SynCommons,
SynSQLite3,
SynCrypto;
type
/// access class to the static .obj SQLite3 engine
// - the intialization section of this unit calls:
// ! sqlite3 := TSQLite3LibraryStatic.Create;
// therefore, adding SynSQLite3Static to your uses clause is enough to use
// the statically linked SQLite3 engine with SynSQLite3
TSQLite3LibraryStatic = class(TSQLite3Library)
public
/// fill the internal API reference s with the static .obj engine
constructor Create; override;
/// unload the static library
destructor Destroy; override;
end;
/// use this procedure to change the password for an existing SQLite3 database file
// - convenient and faster alternative to the sqlite3.rekey() API call
// - conversion is done in-place at file level, with no SQL nor BTree pages
// involved, therefore it can process very big files with best possible speed
// - the OldPassWord must be correct, otherwise the resulting file will be corrupted
// - any password can be '' to mark no encryption as input or output
// - the password may be a JSON-serialized TSynSignerParams object, or will use
// AES-OFB-128 after SHAKE_128 with rounds=1000 and a fixed salt on plain password text
// - please note that this encryption is compatible only with SQlite3 files made
// with SynSQLiteStatic.pas unit (not external/official/wxsqlite3 dll)
// - implementation is NOT compatible with the official SQLite Encryption Extension
// (SEE) file format, not the wxsqlite3 extension, but is (much) faster thanks
// to our SynCrypto AES-NI enabled unit
// - if the key is not correct, a ESQLite3Exception will be raised with
// 'database disk image is malformed' (SQLITE_CORRUPT) at database opening
// - see also IsSQLite3File/IsSQLite3FileEncrypted functions
// - warning: this encryption is NOT compatible with our previous (<1.18.4413)
// cyphered format, which was much less safe (simple XOR on fixed tables), and
// was not working on any database size, making unclean patches to the official
// sqlite3.c amalgamation file, so is deprecated and unsupported any longer -
// see OldSQLEncryptTablePassWordToPlain() to convert your existing databases
function ChangeSQLEncryptTablePassWord(const FileName: TFileName;
const OldPassWord, NewPassword: RawUTF8): boolean;
/// this function may be used to create a plain database file from an existing
// one encrypted with our old/deprecated/unsupported format (<1.18.4413)
// - then call ChangeSQLEncryptTablePassWord() to convert to the new safer format
procedure OldSQLEncryptTablePassWordToPlain(const FileName: TFileName;
const OldPassWord: RawUTF8);
/// could be used to detect a database in old/deprecated/unsupported format (<1.18.4413)
// - to call OldSQLEncryptTablePassWordToPlain + ChangeSQLEncryptTablePassWord
// and switch to the new format
function IsOldSQLEncryptTable(const FileName: TFileName): boolean;
var
/// global flag to use initial AES encryption scheme
// - IV derivation was hardened in revision 1.18.4607 - set TRUE to this
// global constant to use the former implementation (theoritically slightly
// less resistant to brute force attacks) and convert existing databases
ForceSQLite3LegacyAES: boolean;
implementation
{$ifdef FPC} // FPC expects .o linking, and only one version including FTS
{$ifdef MSWINDOWS}
{$ifdef CPU64}
const _PREFIX = '';
{$L .\static\x86_64-win64\sqlite3.o}
{$linklib .\static\x86_64-win64\libkernel32.a}
{$linklib .\static\x86_64-win64\libgcc.a}
{$linklib .\static\x86_64-win64\libmsvcrt.a}
{$else}
const _PREFIX = '_';
{$L .\static\i386-win32\sqlite3.o}
{$linklib .\static\i386-win32\libkernel32.a}
{$linklib .\static\i386-win32\libgcc.a}
{$linklib .\static\i386-win32\libmsvcrt.a}
{$endif CPU64}
{$endif MSWINDOWS}
{$ifdef Darwin}
const _PREFIX = '_';
{$ifdef CPU64}
{$linklib .\static\x86_64-darwin\libsqlite3.a}
{$else}
{$linklib .\static\i386-darwin\libsqlite3.a}
{$endif}
{$endif Darwin}
{$ifdef ANDROID}
const _PREFIX = '';
{$ifdef CPUAARCH64}
{$L .\static\aarch64-android\sqlite3.o}
{$linklib .\static\aarch64-android\libgcc.a}
{$endif CPUAARCH64}
{$ifdef CPUARM}
{$L .\static\arm-android\sqlite3.o}
{$linklib .\static\arm-android\libgcc.a}
{$endif CPUARM}
{$ifdef CPUX86}
{$L .\static\i386-android\sqlite3.o}
{$endif CPUX86}
{$ifdef CPUX64}
{$L .\static\x86_64-android\sqlite3.o}
// x86_64-linux-android-ld.bfd: final link failed
// (Nonrepresentable section on output)
{$endif CPUX64}
{$endif ANDROID}
{$ifdef FREEBSD}
{$ifdef CPUX86}
const _PREFIX = '';
{$L .\static\i386-freebsd\sqlite3.o}
{$ifdef FPC_CROSSCOMPILING}
{$linklib .\static\i386-freebsd\libgcc.a}
{$endif}
{$endif CPUX86}
{$ifdef CPUX64}
const _PREFIX = '';
{$L .\static\x86_64-freebsd\sqlite3.o}
{$ifdef FPC_CROSSCOMPILING}
{$linklib .\static\x86_64-freebsd\libgcc.a}
{$endif}
{$endif CPUX64}
{$endif FREEBSD}
{$ifdef OPENBSD}
{$ifdef CPUX86}
const _PREFIX = '';
{$L .\static\i386-openbsd\sqlite3.o}
{$ifdef FPC_CROSSCOMPILING}
{$linklib .\static\i386-openbsd\libgcc.a}
{$endif}
{$endif CPUX86}
{$ifdef CPUX64}
const _PREFIX = '';
{$L .\static\x86_64-openbsd\sqlite3.o}
{$ifdef FPC_CROSSCOMPILING}
{$linklib .\static\x86_64-openbsd\libgcc.a}
{$endif}
{$endif CPUX64}
{$endif OPENBSD}
{$if defined(Linux) and not defined(BSD) and not defined(Android)}
const _PREFIX = '';
{$ifdef CPUAARCH64}
{$L .\static\aarch64-linux\sqlite3.o}
{$L .\static\aarch64-linux\libgcc.a}
{$endif CPUAARCH64}
{$ifdef CPUARM}
{$L .\static\arm-linux\sqlite3.o}
{$L .\static\arm-linux\libgcc.a}
{$endif CPUARM}
{$ifdef CPUX86}
{$L .\static\i386-linux\sqlite3.o}
{$ifdef FPC_CROSSCOMPILING}
{$linklib .\static\i386-linux\libgcc.a}
{$endif}
{$endif CPUX86}
{$ifdef CPUX64}
{$L .\static\x86_64-linux\sqlite3.o}
{$ifdef FPC_CROSSCOMPILING}
{$linklib .\static\x86_64-linux\libgcc.a}
{$endif}
{$endif CPUX64}
{$ifend}
function log(x: double): double; cdecl; public name _PREFIX+'log'; export;
begin
result := ln(x);
end;
{$ifdef MSWINDOWS}
{$ifdef CPUX86} // not a compiler intrinsic on x86
function _InterlockedCompareExchange(var Dest: longint; New,Comp: longint): longint; stdcall;
public alias: '_InterlockedCompareExchange@12';
begin
result := InterlockedCompareExchange(Dest,New,Comp);
end;
{$endif CPUX86}
{$endif MSWINDOWS}
{$ifdef DARWIN}
function moddi3(num, den: int64): int64; cdecl; public alias: '___moddi3';
begin
result := num mod den;
end;
function umoddi3(num, den: uint64): uint64; cdecl; public alias: '___umoddi3';
begin
result := num mod den;
end;
function divdi3(num, den: int64): int64; cdecl; public alias: '___divdi3';
begin
result := num div den;
end;
function udivdi3(num, den: uint64): uint64; cdecl; public alias: '___udivdi3';
begin
result := num div den;
end;
{$endif DARWIN}
{$ifdef ANDROID}
{$ifdef CPUARM}
function bswapsi2(num:uint32):uint32; cdecl; public alias: '__bswapsi2';
asm
rev r0, r0 // reverse bytes in parameter and put into result register
bx lr
end;
function bswapdi2(num:uint64):uint64; cdecl; public alias: '__bswapdi2';
asm
rev r2, r0 // r2 = rev(r0)
rev r0, r1 // r0 = rev(r1)
mov r1, r2 // r1 = r2 = rev(r0)
bx lr
end;
{$endif}
{$endif ANDROID}
{$else FPC}
// Delphi has a diverse linking strategy, since $linklib doesn't exist :(
{$ifdef MSWINDOWS}
{$ifdef CPU64}
{$L sqlite3.o} // compiled with C++ Builder 10.3 Community Edition bcc64
{$else}
{$L sqlite3.obj} // compiled with free Borland C++ Compiler 5.5
{$endif}
{$else}
{$ifdef KYLIX3} // in practice, we failed to compile SQLite3 with gcc 2 :(
{$L kylix/sqlite3/sqlite3.o}
{$L kylix/sqlite3/_divdi3.o}
{$L kylix/sqlite3/_moddi3.o}
{$L kylix/sqlite3/_udivdi3.o}
{$L kylix/sqlite3/_umoddi3.o}
{$L kylix/sqlite3/_cmpdi2.o}
{$endif KYLIX3}
{$endif MSWINDOWS}
// those functions will be called only under Delphi + Win32/Win64
function malloc(size: cardinal): Pointer; cdecl; { always cdecl }
begin
GetMem(Result, size);
end;
procedure free(P: Pointer); cdecl; { always cdecl }
begin
FreeMem(P);
end;
function realloc(P: Pointer; Size: Integer): Pointer; cdecl; { always cdecl }
begin
result := P;
ReallocMem(result,Size);
end;
function rename(oldname, newname: PUTF8Char): integer; cdecl; { always cdecl }
begin
if RenameFile(UTF8DecodeToString(oldname,StrLen(oldname)),
UTF8DecodeToString(newname,StrLen(newname))) then
result := 0 else
result := -1;
end;
{$ifdef MSWINDOWS}
{$ifdef CPU32} // Delphi Win32 will link static Borland C++ sqlite3.obj
// we then implement all needed Borland C++ runtime functions in pure pascal:
function _ftol: Int64;
// Borland C++ float to integer (Int64) conversion
asm
jmp System.@Trunc // FST(0) -> EDX:EAX, as expected by BCC32 compiler
end;
function _ftoul: Int64;
// Borland C++ float to integer (Int64) conversion
asm
jmp System.@Trunc // FST(0) -> EDX:EAX, as expected by BCC32 compiler
end;
var __turbofloat: word; { not used, but must be present for linking }
// Borland C++ and Delphi share the same low level Int64 _ll*() functions:
procedure _lldiv;
asm
jmp System.@_lldiv
end;
procedure _lludiv;
asm
jmp System.@_lludiv
end;
procedure _llmod;
asm
jmp System.@_llmod
end;
procedure _llmul;
asm
jmp System.@_llmul
end;
procedure _llumod;
asm
jmp System.@_llumod
end;
procedure _llshl;
asm
jmp System.@_llshl
end;
procedure _llshr;
asm
{$ifndef ENHANCEDRTL} // need this code for Borland/CodeGear default System.pas
shrd eax, edx, cl
sar edx, cl
cmp cl, 32
jl @@Done
cmp cl, 64
jge @@RetSign
mov eax, edx
sar edx, 31
ret
@@RetSign:
sar edx, 31
mov eax, edx
@@Done:
{$else}
// our customized System.pas didn't forget to put _llshr in its interface :)
jmp System.@_llshr
{$endif}
end;
procedure _llushr;
asm
jmp System.@_llushr
end;
function log(const val: double): double; cdecl; { always cdecl }
asm
fld qword ptr val
fldln2
fxch
fyl2x
end;
function fabs(x: double): double; cdecl; // needed since 3.44.2
begin
result := abs(x);
end;
{$endif CPU32}
{$endif MSWINDOWS}
function memset(P: Pointer; B: Integer; count: Integer): pointer; cdecl; { always cdecl }
// a fast full pascal version of the standard C library function
begin
FillCharFast(P^, count, B);
result := P;
end;
function memmove(dest, source: pointer; count: Integer): pointer; cdecl; { always cdecl }
{$ifdef FPC}public name{$ifdef CPU64}'memmove'{$else}'_memmove'{$endif};{$endif}
// a fast full pascal version of the standard C library function
begin
MoveFast(source^, dest^, count); // move() is overlapping-friendly
result := dest;
end;
function memcpy(dest, source: Pointer; count: Integer): pointer; cdecl; { always cdecl }
{$ifdef FPC}public name{$ifdef CPU64}'memcpy'{$else}'_memcpy'{$endif};{$endif}
// a fast full pascal version of the standard C library function
begin
MoveFast(source^, dest^, count);
result := dest;
end;
function strlen(p: PAnsiChar): integer; cdecl; { always cdecl }
{$ifdef FPC}public name{$ifdef CPU64}'strlen'{$else}'_strlen'{$endif};{$endif}
// a fast full pascal version of the standard C library function
begin // called only by some obscure FTS3 functions (normal code use dedicated functions)
result := SynCommons.StrLen(pointer(p));
end;
function strcmp(p1,p2: PAnsiChar): integer; cdecl; { always cdecl }
{$ifdef FPC}public name{$ifdef CPU64}'strcmp'{$else}'_strcmp'{$endif};{$endif}
// a fast full pascal version of the standard C library function
begin // called only by some obscure FTS3 functions (normal code use dedicated functions)
result := SynCommons.StrComp(p1,p2);
end;
function strcspn(str,reject: PAnsiChar): integer; cdecl;
{$ifdef FPC}public name{$ifdef CPU64}'strcspn'{$else}'_strcspn'{$endif};{$endif}
begin // called e.g. during LIKE process
result := SynCommons.strcspn(str,reject); // use SSE4.2 if available
end;
function strspn(str,reject: PAnsiChar): integer; cdecl;
{$ifdef FPC}public name{$ifdef CPU64}'strcspn'{$else}'_strcspn'{$endif};{$endif}
begin // appeared with SQlite 3.44.2
result := SynCommons.strspn(str,reject);
end;
function strrchr(s: PAnsiChar; c: AnsiChar): PAnsiChar; cdecl;
{$ifdef FPC}public name{$ifdef CPU64}'strrchr'{$else}'_strrchr'{$endif};{$endif}
begin // simple full pascal version of the standard C library function
result := nil;
if s<>nil then
while s^<>#0 do begin
if s^=c then
result := s;
inc(s);
end;
end;
function memcmp(p1, p2: pByte; Size: integer): integer; cdecl; { always cdecl }
{$ifdef FPC}
public name{$ifdef CPU64}'memcmp'{$else}'_memcmp'{$endif};
begin
result := CompareByte(p1,p2,Size); // use FPC
end;
{$else}
begin // full pascal version of the standard C library function
if (p1<>p2) and (Size<>0) then
if p1<>nil then
if p2<>nil then begin
repeat
if p1^=p2^ then begin
inc(p1);
inc(p2);
dec(Size);
if Size<>0 then
continue else break;
end;
result := p1^-p2^;
exit;
until false;
result := 0;
end else
result := 1 else
result := -1 else
result := 0;
end;
{$endif}
function strncmp(p1, p2: PByte; Size: integer): integer; cdecl; { always cdecl }
{$ifdef FPC}public name{$ifdef CPU64}'strncmp'{$else}'_strncmp'{$endif};{$endif}
var i: integer;
begin // a fast full pascal version of the standard C library function
for i := 1 to Size do begin
result := p1^-p2^;
if (result<>0) or (p1^=0) then
exit;
inc(p1);
inc(p2);
end;
result := 0;
end;
type
// qsort() is used if SQLITE_ENABLE_FTS3 is defined
// this function type is defined for calling termDataCmp() in sqlite3.c
qsort_compare_func = function(P1,P2: pointer): integer; cdecl; { always cdecl }
procedure QuickSortPtr(base: PPointerArray; L, R: Integer; comparF: qsort_compare_func);
var I, J, P: Integer;
PP, C: PAnsiChar;
begin
repeat // from SQLite (FTS), With=sizeof(PAnsiChar) AFAIK
I := L;
J := R;
P := (L+R) shr 1;
repeat
PP := @base[P];
while comparF(@base[I],PP)<0 do
inc(I);
while comparF(@base[J],PP)>0 do
dec(J);
if I<=J then begin
C := base[I];
base[I] := base[J];
base[J] := C; // fast memory exchange
if P=I then P := J else if P=J then P := I;
inc(I);
dec(J);
end;
until I>J;
if L<J then
QuickSortPtr(base, L, J, comparF);
L := I;
until I>=R;
end;
procedure QuickSort(baseP: PAnsiChar; Width: integer; L, R: Integer; comparF: qsort_compare_func);
// code below is very fast and optimized
procedure Exchg(P1,P2: PAnsiChar; Size: integer);
var B: AnsiChar;
i: integer;
begin
for i := 0 to Size-1 do begin
B := P1[i];
P1[i] := P2[i];
P2[i] := B;
end;
end;
var I, J, P: Integer;
PP, C: PAnsiChar;
begin
repeat // generic sorting algorithm
I := L;
J := R;
P := (L+R) shr 1;
repeat
PP := baseP+P*Width; // compute PP at every loop, since P may change
C := baseP+I*Width;
while comparF(C,PP)<0 do begin
inc(I);
inc(C,width); // avoid slower multiplication in loop
end;
C := baseP+J*Width;
while comparF(C,PP)>0 do begin
dec(J);
dec(C,width); // avoid slower multiplication in loop
end;
if I<=J then begin
Exchg(baseP+I*Width,baseP+J*Width,Width); // fast memory exchange
if P=I then P := J else if P=J then P := I;
inc(I);
dec(J);
end;
until I>J;
if L<J then
QuickSort(baseP, Width, L, J, comparF);
L := I;
until I>=R;
end;
procedure qsort(baseP: pointer; NElem, Width: integer; comparF: pointer); cdecl; { always cdecl }
{$ifdef FPC}public name{$ifdef CPU64}'qsort'{$else}'_qsort'{$endif};{$endif}
// a fast full pascal version of the standard C library function
begin
if (cardinal(NElem)>1) and (Width>0) then
if Width=sizeof(pointer) then
QuickSortPtr(baseP, 0, NElem-1, qsort_compare_func(comparF)) else
QuickSort(baseP, Width, 0, NElem-1, qsort_compare_func(comparF));
end;
var
{ as standard C library documentation states:
Statically allocated buffer, shared by the functions gmtime() and localtime().
Each call of these functions overwrites the content of this structure.
-> since timing is not thread-dependent, it's OK to share this buffer :) }
atm: packed record
tm_sec: Integer; { Seconds. [0-60] (1 leap second) }
tm_min: Integer; { Minutes. [0-59] }
tm_hour: Integer; { Hours. [0-23] }
tm_mday: Integer; { Day. [1-31] }
tm_mon: Integer; { Month. [0-11] }
tm_year: Integer; { Year - 1900. }
tm_wday: Integer; { Day of week. [0-6] }
tm_yday: Integer; { Days in year. [0-365] }
tm_isdst: Integer; { DST. [-1/0/1]}
__tm_gmtoff: Integer; { Seconds east of UTC. }
__tm_zone: ^Char; { Timezone abbreviation.}
end;
function localtime64(const t: Int64): pointer; cdecl; { always cdecl }
{$ifdef FPC}public name '__imp__localtime64';{$endif}
// a fast full pascal version of the standard C library function
var {$ifdef MSWINDOWS}
uTm: TFileTime;
lTm: TFileTime;
{$endif}
S: TSystemTime;
begin
{$ifdef MSWINDOWS}
Int64(uTm) := (t+11644473600)*10000000; // unix time to dos file time
FileTimeToLocalFileTime(uTM,lTM);
FileTimeToSystemTime(lTM,S);
atm.tm_sec := S.wSecond;
atm.tm_min := S.wMinute;
atm.tm_hour := S.wHour;
atm.tm_mday := S.wDay;
atm.tm_mon := S.wMonth-1;
atm.tm_year := S.wYear-1900;
atm.tm_wday := S.wDayOfWeek;
{$else}
GetNowUTCSystem(S);
atm.tm_sec := S.Second;
atm.tm_min := S.Minute;
atm.tm_hour := S.Hour;
atm.tm_mday := S.Day;
atm.tm_mon := S.Month-1;
atm.tm_year := S.Year-1900;
atm.tm_wday := S.Day;
{$endif}
result := @atm;
end;
function localtime(t: PCardinal): pointer; cdecl; { always cdecl }
{$ifdef FPC}public name{$ifdef CPU64}'localtime32'{$else}'__localtime32'{$endif};{$endif}
begin
result := localtime64(t^);
end;
{$ifdef MSWINDOWS}
const
msvcrt = 'msvcrt.dll';
kernel = 'kernel32.dll';
function _beginthreadex(security: pointer; stksize: dword;
start,arg: pointer; flags: dword; var threadid: dword): THandle; cdecl; external msvcrt;
procedure _endthreadex(exitcode: dword); cdecl; external msvcrt;
{$ifdef CPU64}
// Delphi Win64 will link its own static sqlite3.o (diverse from FPC's)
function _log(x: double): double; export; // to link LLVM bcc64 compiler
begin
result := ln(x);
end;
function log(x: double): double; export; // to link old non-LLVM bcc64 compiler
begin
result := ln(x);
end;
procedure __chkstk;
begin
end;
procedure __faststorefence;
asm
.noframe
mfence;
end;
var
_finf: double = 1.0 / 0.0; // compiles to some double infinity constant
_fltused: Int64 = 0; // to link old non-LLVM bcc64 compiler
{$endif CPU64}
{$else MSWINDOWS}
{$ifdef KYLIX3}
function close(Handle: Integer): Integer; cdecl;
external libcmodulename;
function read(Handle: Integer; var Buffer; Count: size_t): ssize_t; cdecl;
external libcmodulename;
function write(Handle: Integer; const Buffer; Count: size_t): ssize_t; cdecl;
external libcmodulename;
function __fixunsdfdi(a: double): Int64; cdecl;
begin
if a<0 then
result := 0 else
result := round(a);
end;
{$endif KYLIX3}
{$endif MSWINDOWS}
{$endif FPC}
// some external functions as expected by codecext.c and our sqlite3mc.c wrapper
procedure CodecGenerateKey(var aes: TAES; userPassword: pointer; passwordLength: integer);
var s: TSynSigner;
k: THash512Rec;
begin
s.PBKDF2(userPassword,passwordLength,k,'J6CuDftfPr22FnYn');
s.AssignTo(k,aes,{encrypt=}true);
end;
function CodecGetReadKey(codec: pointer): PAES; cdecl; external;
function CodecGetWriteKey(codec: pointer): PAES; cdecl; external;
procedure CodecGenerateReadKey(codec: pointer;
userPassword: PAnsiChar; passwordLength: integer); cdecl;
{$ifdef FPC}public name _PREFIX+'CodecGenerateReadKey';{$endif} export;
begin
CodecGenerateKey(CodecGetReadKey(codec)^,userPassword,passwordLength);
end;
procedure CodecGenerateWriteKey(codec: pointer;
userPassword: PAnsiChar; passwordLength: integer); cdecl;
{$ifdef FPC}public name _PREFIX+'CodecGenerateWriteKey';{$endif} export;
begin
CodecGenerateKey(CodecGetWriteKey(codec)^,userPassword,passwordLength);
end;
procedure CodecAESProcess(page: cardinal; data: PAnsiChar; len: integer;
aes: PAES; encrypt: boolean);
var plain: Int64; // bytes 16..23 should always be unencrypted
iv: THash128Rec; // is genuine and AES-protected (since not random)
begin
if (len and AESBlockMod<>0) or (len<=0) or (integer(page)<=0) then
raise ESQLite3Exception.CreateUTF8('CodecAESProcess(page=%,len=%)', [page,len]);
iv.c0 := page xor 668265263; // prime-based initialization
iv.c1 := page*2654435761;
iv.c2 := page*2246822519;
iv.c3 := page*3266489917;
if not ForceSQLite3LegacyAES then
aes^.Encrypt(iv.b); // avoid potential brute force attack
len := len shr AESBlockShift;
if page=1 then // ensure header bytes 16..23 are stored unencrypted
if (PInt64(data)^=SQLITE_FILE_HEADER128.lo) and
(data[21]=#64) and (data[22]=#32) and (data[23]=#32) then
if encrypt then begin
plain := PInt64(data+16)^;
aes^.DoBlocksOFB(iv.b,data+16,data+16,len-1);
PInt64(data+8)^ := PInt64(data+16)^; // 8..15 are encrypted bytes 16..23
PInt64(data+16)^ := plain;
end else begin
PInt64(data+16)^ := PInt64(data+8)^;
aes^.DoBlocksOFB(iv.b,data+16,data+16,len-1);
if (data[21]=#64) and (data[22]=#32) and (data[23]=#32) then
PHash128(data)^ := SQLITE_FILE_HEADER128.b else
FillZero(PHash128(data)^); // report incorrect password
end else
FillZero(PHash128(data)^) else
aes^.DoBlocksOFB(iv.b,data,data,len);
end;
function CodecEncrypt(codec: pointer; page: integer; data: PAnsiChar;
len, useWriteKey: integer): integer; cdecl;
{$ifdef FPC}public name _PREFIX+'CodecEncrypt';{$endif} export;
begin
if useWriteKey=1 then
CodecAESProcess(page,data,len,CodecGetWriteKey(codec),true) else
CodecAESProcess(page,data,len,CodecGetReadKey(codec),true);
result := SQLITE_OK;
end;
function CodecDecrypt(codec: pointer; page: integer;
data: PAnsiChar; len: integer): integer; cdecl;
{$ifdef FPC}public name _PREFIX+'CodecDecrypt';{$endif} export;
begin
CodecAESProcess(page,data,len,CodecGetReadKey(codec),false);
result := SQLITE_OK;
end;
function CodecTerm(codec: pointer): integer; cdecl;
{$ifdef FPC}public name _PREFIX+'CodecTerm';{$endif} export;
begin
CodecGetReadKey(codec)^.Done;
CodecGetWriteKey(codec)^.Done;
result := SQLITE_OK;
end;
function ChangeSQLEncryptTablePassWord(const FileName: TFileName;
const OldPassWord, NewPassword: RawUTF8): boolean;
var F: THandle;
bufsize,page,pagesize,pagecount,n,p,read: cardinal;
head: THash256Rec;
buf: PAnsiChar;
temp: RawByteString;
size: TQWordRec;
posi: Int64;
old, new: TAES;
begin
result := false;
if OldPassword=NewPassword then
exit;
F := FileOpen(FileName,fmOpenReadWrite);
if F<>INVALID_HANDLE_VALUE then
try
if OldPassword<>'' then
CodecGenerateKey(old,pointer(OldPassword),length(OldPassWord));
if NewPassword<>'' then
CodecGenerateKey(new,pointer(NewPassword),length(NewPassWord));
size.L := GetFileSize(F,@size.H);
read := FileRead(F,head,SizeOf(head));
if read<>SizeOf(head) then
exit;
if size.V>4 shl 20 then // use up to 4MB of R/W buffer
bufsize := 4 shl 20 else
bufsize := size.L;
pagesize := cardinal(head.b[16]) shl 8+head.b[17];
pagecount := size.V div pagesize;
if (pagesize<1024) or (pagesize and AESBlockMod<>0) or (pagesize>bufsize) or
(QWord(pagecount)*pagesize<>size.V) or (head.d0<>SQLITE_FILE_HEADER128.Lo) or
((head.d1=SQLITE_FILE_HEADER128.Hi)<>(OldPassWord='')) then
exit;
FileSeek64(F,0,soFromBeginning);
SetLength(temp,bufsize);
posi := 0;
page := 1;
while page<=pagecount do begin
n := bufsize div pagesize;
read := pagecount-page+1;
if read < n then
n := read;
buf := pointer(temp);
read := FileRead(F,buf^,pagesize*n);
if read<>pagesize*n then
exit; // stop on any read error
for p := 0 to n-1 do begin
if OldPassword<>'' then begin
CodecAESProcess(page+p,buf,pagesize,@old,false);
if (p=0) and (page=1) and (PInteger(buf)^=0) then
exit; // OldPassword is obviously incorrect
end;
if NewPassword<>'' then
CodecAESProcess(page+p,buf,pagesize,@new,true);
inc(buf,pagesize);
end;
FileSeek64(F,posi,soFromBeginning);
FileWrite(F,pointer(temp)^,pagesize*n); // update in-place
inc(posi,pagesize*n);
inc(page,n);
end;
result := true;
finally
FileClose(F);
if OldPassword<>'' then
old.Done;
if NewPassword<>'' then
new.Done;
end;
end;
function IsOldSQLEncryptTable(const FileName: TFileName): boolean;
var F: THandle;
Header: array[0..2047] of byte;
begin
result := false;
F := FileOpen(FileName,fmOpenRead or fmShareDenyNone);
if F=INVALID_HANDLE_VALUE then
exit;
if (FileRead(F,Header,SizeOf(Header))=SizeOf(Header)) and
// see https://www.sqlite.org/fileformat.html (4 in big endian = 1024 bytes)
(PWord(@Header[16])^=4) and
IsEqual(PHash128(@Header)^,SQLITE_FILE_HEADER128.b) then
if not(Header[1024] in [5,10,13]) then
// B-tree leaf Type to be either 5 (interior) 10 (index) or 13 (table)
result := true;
FileClose(F);
end;
procedure OldSQLEncryptTablePassWordToPlain(const FileName: TFileName;
const OldPassWord: RawUTF8);
const
SQLEncryptTableSize = $4000;
procedure CreateSQLEncryptTableBytes(const PassWord: RawUTF8; Table: PByteArray);
// very fast table (private key) computation from a given password
// - use a simple prime-based random generator, strong enough for common use
// - execution speed and code size was the goal here: can be easily broken
// - SynCrypto proposes SHA-256 and AES-256 for more secure encryption
var i, j, k, L: integer;
begin
L := length(Password)-1;
j := 0;
k := integer(L*ord(Password[1]))+134775813; // initial value, prime number derivated
for i := 0 to SQLEncryptTableSize-1 do begin
Table^[i] := (ord(PassWord[j+1])) xor byte(k);
k := Integer(k*3+i); // fast prime-based pseudo random generator
if j=L then
j := 0 else
inc(j);
end;
end;
procedure XorOffset(P: PByte; Index, Count: cardinal; SQLEncryptTable: PByteArray);
var Len: cardinal;
begin // fast and simple Cypher using Index (= offset in file)
if Count>0 then
repeat
Index := Index and (SQLEncryptTableSize-1);
Len := SQLEncryptTableSize-Index;
if Len>Count then