forked from xinpengdr/MySQL-Front
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLClient.pas
More file actions
3066 lines (2701 loc) · 96.9 KB
/
MySQLClient.pas
File metadata and controls
3066 lines (2701 loc) · 96.9 KB
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
unit MySQLClient;
interface {********************************************************************}
uses
SyncObjs, WinSock,
SysUtils,
MySQLConsts;
type
MYSQL_RES = class;
MYSQL = class;
TMySQL_IO = class
type
TType = (itNone, itNamedPipe, itTCPIP);
TDirection = (idRead, idWrite);
private
FErrNo: my_uint;
FError: RawByteString;
FDirection: TDirection;
Pipe: THandle;
Socket: TSocket;
protected
IOType: TType;
function GetCodePage(): Cardinal; virtual;
procedure SetDirection(ADirection: TDirection); virtual;
procedure Close(); virtual;
function DecodeString(const Str: RawByteString): string; virtual;
function EncodeString(const Str: string): RawByteString; virtual;
function Open(const AIOType: TType; const Host, PipeName: RawByteString;
const Port, Timeout: my_uint): Boolean; virtual;
function Receive(var Buffer; const BytesToRead: my_uint): Boolean; virtual;
function Send(const Buffer; const BytesToWrite: my_uint): Boolean; virtual;
function Seterror(const AErrNo: my_uint; const AError: RawByteString = ''): my_uint; virtual;
property CodePage: Cardinal read GetCodePage;
property Direction: TDirection read FDirection write SetDirection;
public
constructor Create(); virtual;
destructor Destroy(); override;
function errno(): my_uint; inline;
function error(): RawByteString; inline;
end;
TMySQL_Packet = class (TMySQL_IO)
type
TBuffer = record
Mem: my_char;
MemSize: my_uint;
Offset: my_uint;
Size: my_uint;
end;
TClientStatus = (MYSQL_STATUS_READY, MYSQL_STATUS_GET_RESULT, MYSQL_STATUS_USE_RESULT);
private
CompPacketNr: Byte;
CompressedBuffer: TBuffer;
DecompressedBuffer: TBuffer;
PacketBuffer: TBuffer;
PacketNr: Byte;
UseCompression: Boolean;
function ReceivePacket(): Boolean;
protected
function CreatePacket(const AIOType: TMySQL_IO.TType;
const Host, UnixSocket: RawByteString; const Port, Timeout: my_uint): Boolean; virtual;
procedure ClosePacket(); virtual;
function FlushPacketBuffers(): Boolean; virtual;
procedure FreeBuffer(var Buffer: TBuffer); virtual;
function GetPacketSize(): my_int; virtual;
procedure next_command(); virtual;
function next_result(): my_int; virtual;
function ReadPacket(const Buffer: my_char; const Size: my_uint): Boolean; overload; virtual;
function ReadPacket(out Value: my_int; const Size: Byte = 0): Boolean; overload; virtual;
function ReadPacket(out Value: my_uint; const Size: Byte = 0): Boolean; overload; virtual;
function ReadPacket(out Value: my_ulonglong; const Size: Byte = 0): Boolean; overload; virtual;
function ReadPacket(out Value: RawByteString; const NTS: Boolean = True; const Size: Byte = 0): Boolean; overload; virtual;
function ReallocBuffer(var Buffer: TBuffer; const NeededSize: my_uint; const ReduceSize: Boolean = False): Boolean;
function SetPacketPointer(const DistanceToMove: my_int; const MoveMethod: my_int): my_int; virtual;
procedure SetDirection(ADirection: TMySQL_IO.TDirection); override;
function WritePacket(const Buffer: my_char; const Size: my_uint): Boolean; overload; virtual;
function WritePacket(const Value: my_ulonglong; const Size: my_uint): Boolean; overload; virtual;
function WritePacket(const Value: RawByteString; const NTS: Boolean = True): Boolean; overload; virtual;
public
constructor Create(); override;
end;
MYSQL_RES = class
type
PRow = ^TRow;
TRow = record
MemSize: my_uint;
Lengths: MYSQL_LENGTHS;
Row: MYSQL_ROW;
Next: PRow;
end;
private
CurrentRow: PRow;
FieldCount: my_uint;
FieldIndex: my_int;
FirstRow: PRow;
mysql: MYSQL;
ResultType: (rtUsed, rtStored);
RowCount: my_ulonglong;
RowIndex: my_ulonglong;
protected
Fields: MYSQL_FIELDS;
Lengths: MYSQL_LENGTHS;
property MysqlClient: MYSQL read mysql;
public
constructor Create(const Amysql: MYSQL; const AFieldCount: my_uint); virtual;
procedure data_seek(offset: my_ulonglong); virtual;
destructor Destroy(); override;
function fetch_field(): MYSQL_FIELD; virtual;
function fetch_field_direct(fieldnr: my_uint): MYSQL_FIELD; virtual;
function fetch_fields(): MYSQL_FIELDS; virtual;
function fetch_lengths(): MYSQL_LENGTHS; virtual;
function fetch_row(): MYSQL_ROW; virtual;
function num_fields(): my_uint; virtual;
function num_rows(): my_ulonglong; virtual;
end;
MYSQL = class (TMySQL_Packet)
private const
SQLSTATE_LENGTH = 5;
private
CriticalSection: TCriticalSection;
FieldCount: my_uint;
FSQLState: array [0 .. SQLSTATE_LENGTH - 1] of AnsiChar;
UseNamedPipe: Boolean;
function Reconnect(): Boolean;
function SendFile(const Filename: RawByteString): Boolean;
protected
faffected_rows: my_ulonglong;
fca: RawByteString;
fca_path: RawByteString;
fcert: RawByteString;
fcharacter_set_name: RawByteString;
fcipher: RawByteString;
fclient_capabilities: my_uint;
fclient_status: TMySQL_Packet.TClientStatus;
fcompress: Boolean;
fdb: RawByteString;
fhost: RawByteString;
fhost_info: RawByteString;
finfo: my_char;
finsert_id: my_ulonglong;
fkey: RawByteString;
flocal_infile_end: Tlocal_infile_end;
flocal_infile_error: Tlocal_infile_error;
flocal_infile_init: Tlocal_infile_init;
flocal_infile_read: Tlocal_infile_read;
flocal_infile_userdata: Pointer;
fpasswd: RawByteString;
fpipe_name: RawByteString;
fport: Cardinal;
freconnect: Boolean;
fres: MYSQL_RES;
fserver_capabilities: my_uint;
fserver_info: my_char;
fserver_status: my_int;
fserver_version: my_uint;
fstat: RawByteString;
fthread_id: my_uint;
ftimeout: my_uint;
fuser: RawByteString;
fwarning_count: my_uint;
procedure ClosePacket(); override;
function ExecuteCommand(const Command: enum_server_command; const Bin: my_char; const Size: my_int; const Retry: Boolean): my_int; virtual;
function GetCodePage(): Cardinal; override;
function ReadRow(var Row: MYSQL_RES.PRow): my_int; virtual;
procedure ReadRows(const Ares: MYSQL_RES); virtual;
function ServerError(): Boolean; virtual;
function Seterror(const AErrNo: my_uint; const AError: RawByteString = ''): my_uint; override;
property ClientStatus: TMySQL_Packet.TClientStatus read fclient_status;
property CodePage: Cardinal read GetCodePage;
public
constructor Create(); override;
destructor Destroy(); override;
function affected_rows(): my_ulonglong; virtual;
function character_set_name(): my_char; virtual;
function dump_debug_info(): my_int; virtual;
function eof(): my_bool; virtual;
function get_client_info(): my_char; virtual;
function get_client_version(): my_uint; virtual;
function get_host_info(): my_char; virtual;
function get_server_info(): my_char; virtual;
function get_server_status(): my_uint; virtual;
function get_server_version(): my_int; virtual;
function info(): my_char; virtual;
function insert_id(): my_ulonglong; virtual;
function kill(pid: my_uint): my_int; virtual;
function more_results(): my_bool; virtual;
function next_result(): my_int; override;
function options(option: enum_mysql_option; const arg: my_char): my_int; virtual;
function ping(): my_int; virtual;
function query(const q: my_char): my_int;
function real_connect(host, user, passwd, db: my_char; port: my_uint; unix_socket: my_char; client_flag: my_uint): MYSQL; virtual;
function real_escape_string(_to: my_char; const from: my_char; length: my_uint): my_uint; virtual;
function real_query(query: my_char; length: my_int): my_int; virtual;
function refresh(options: my_int): my_int; virtual;
function select_db(db: my_char): my_int; virtual;
function set_character_set(const csname: my_char): my_int; virtual;
procedure set_local_infile_default(); virtual;
procedure set_local_infile_handler(local_infile_init: Tlocal_infile_init; local_infile_read: Tlocal_infile_read; local_infile_end: Tlocal_infile_end; local_infile_error: Tlocal_infile_error; userdata: Pointer); virtual;
function set_server_option(option: enum_mysql_set_option): my_int; virtual;
function shutdown(shutdown_level: mysql_enum_shutdown_level): my_int; virtual;
function sqlstate(): my_char; virtual;
function store_result(): MYSQL_RES; virtual;
function thread_id(): my_uint; virtual;
function use_result(): MYSQL_RES; virtual;
function warning_count(): my_uint; virtual;
property client_status: TMySQL_Packet.TClientStatus read fclient_status;
property res: MYSQL_RES read fres;
end;
function mysql_affected_rows(mysql: MYSQL): my_ulonglong; stdcall;
// function mysql_change_user(mysql: MYSQL; const user, passwd, db: my_char): my_bool; stdcall;
function mysql_character_set_name(mysql: MYSQL): my_char; stdcall;
procedure mysql_close(mysql: MYSQL); stdcall;
// function mysql_connect(mysql: MYSQL; const host, user, passwd: my_char): MYSQL; stdcall;
// function mysql_create_db(mysql: MYSQL; const DB: my_char): my_int; stdcall;
procedure mysql_data_seek(res: MYSQL_RES; offset: my_ulonglong); stdcall;
// procedure mysql_debug(const debug: my_char); stdcall;
// function mysql_drop_db(mysql: MYSQL; const DB: my_char): my_int; stdcall;
// function mysql_dump_debug_info(mysql: MYSQL): my_int; stdcall;
function mysql_eof(mysql: MYSQL): my_bool; stdcall;
function mysql_errno(mysql: MYSQL): my_uint; stdcall;
function mysql_error(mysql: MYSQL): my_char; stdcall;
// function mysql_escape_string(_to: my_char; const from: my_char; from_length: my_uint): my_uint; stdcall;
function mysql_fetch_field(res: MYSQL_RES): MYSQL_FIELD; stdcall;
function mysql_fetch_fields(res: MYSQL_RES): MYSQL_FIELDS; stdcall;
function mysql_fetch_field_direct(res: MYSQL_RES; fieldnr: my_uint): MYSQL_FIELD; stdcall;
function mysql_fetch_lengths(res: MYSQL_RES): MYSQL_LENGTHS; stdcall;
function mysql_fetch_row(res: MYSQL_RES): MYSQL_ROW; stdcall;
function mysql_field_count(mysql: MYSQL): my_uint; stdcall;
procedure mysql_free_result(res: MYSQL_RES); stdcall;
function mysql_get_client_info: my_char; stdcall;
function mysql_get_client_version: my_uint; stdcall;
function mysql_get_host_info(mysql: MYSQL): my_char; stdcall;
function mysql_get_proto_info(mysql: MYSQL): my_uint; stdcall;
function mysql_get_server_info(mysql: MYSQL): my_char; stdcall;
function mysql_get_server_status(mysql: MYSQL): my_uint; stdcall;
function mysql_get_server_version(mysql: MYSQL): my_uint; stdcall;
function mysql_info(mysql: MYSQL): my_char; stdcall;
function mysql_init(mysql: MYSQL): MYSQL; stdcall;
function mysql_insert_id(mysql: MYSQL): my_ulonglong; stdcall;
function mysql_kill(mysql: MYSQL; pid: my_uint): my_int; stdcall;
// function mysql_list_dbs(mysql: MYSQL; const wild: my_char): MYSQL_RES; stdcall;
// function mysql_list_fields(mysql: MYSQL; const table, wild: my_char): MYSQL_RES; stdcall;
// function mysql_list_processes(mysql: MYSQL): MYSQL_RES; stdcall;
// function mysql_list_tables(mysql: MYSQL; const wild: my_char): MYSQL_RES; stdcall;
function mysql_num_fields(res: MYSQL_RES): my_uint; stdcall;
function mysql_num_rows(res: MYSQL_RES): my_ulonglong; stdcall;
function mysql_options(mysql: MYSQL; option: enum_mysql_option; const arg: my_char): my_int; stdcall;
function mysql_ping(mysql: MYSQL): my_int; stdcall;
function mysql_query(mysql: MYSQL; const q: my_char): my_int; stdcall;
function mysql_real_connect(mysql: MYSQL; host, user, passwd, db: my_char; port: my_uint; unix_socket: my_char; client_flag: my_uint): MYSQL; stdcall;
function mysql_real_escape_string(mysql: MYSQL; _to: my_char; const from: my_char; length: my_uint): my_uint; stdcall;
function mysql_real_query(mysql: MYSQL; query: my_char; length: my_int): my_int; stdcall;
// function mysql_reload(mysql: MYSQL): my_int; stdcall;
// function mysql_row_seek(res: MYSQL_RES; offset: MYSQL_ROW_OFFSET): MYSQL_ROW_OFFSET; stdcall;
// function mysql_row_tell(res: MYSQL_RES): MYSQL_ROW_OFFSET; stdcall;
function mysql_select_db(mysql: MYSQL; const db: my_char): my_int; stdcall;
function mysql_set_character_set(mysql: MYSQL; const csname: my_char): my_int; stdcall;
function mysql_set_server_option(mysql: MYSQL; option: enum_mysql_set_option): my_int; stdcall;
function mysql_shutdown(mysql: MYSQL; shutdown_level: mysql_enum_shutdown_level): my_int; stdcall;
function mysql_sqlstate(mysql: MYSQL): my_char; stdcall;
// function mysql_ssl_set(mysql: MYSQL; key, cert, ca, capath, cipher: my_char): my_int; stdcall;
// function mysql_stat(mysql: MYSQL): my_char; stdcall;
function mysql_store_result(mysql: MYSQL): MYSQL_RES; stdcall;
function mysql_thread_id(mysql: MYSQL): my_uint; stdcall;
function mysql_use_result(mysql: MYSQL): MYSQL_RES; stdcall;
function mysql_warning_count(mysql: MYSQL): my_uint; stdcall;
// function mysql_commit(mysql: MYSQL): my_bool; stdcall;
// function mysql_rollback(mysql: MYSQL): my_bool; stdcall;
// function mysql_autocommit(mysql: MYSQL; mode: my_bool): my_bool; stdcall;
function mysql_more_results(mysql: MYSQL): my_bool; stdcall;
function mysql_next_result(mysql: MYSQL): my_int; stdcall;
procedure mysql_set_local_infile_default(mysql: MYSQL); cdecl;
procedure mysql_set_local_infile_handler(mysql: MYSQL; local_infile_init: Tlocal_infile_init; local_infile_read: Tlocal_infile_read; local_infile_end: Tlocal_infile_end; local_infile_error: Tlocal_infile_error; userdata: Pointer); cdecl;
const
PACKET_CURRENT = 4;
implementation {***************************************************************}
uses
Windows, Classes,
ZLib, StrUtils;
const
AF_INET6 = 23;
COMP_HEADER_SIZE = 3;
MIN_COMPRESS_LENGTH = 50;
MYSQL_CLIENT_INFO = '4.1.1';
MYSQL_CLIENT_VERSION = 40101;
NET_HEADER_SIZE = 4;
PROTOCOL_VERSION = 10;
SCRAMBLE_LENGTH = 20;
SCRAMBLE_LENGTH_323 = 8;
CLIENT_CAPABILITIES = CLIENT_LONG_PASSWORD or
CLIENT_LONG_FLAG or
CLIENT_LOCAL_FILES or
CLIENT_PROTOCOL_41 or
CLIENT_TRANSACTIONS or
CLIENT_SECURE_CONNECTION;
type
TWSAConnectByNameA = function(
s: TSocket;
nodename: LPSTR;
servicename: LPSTR;
LocalAddressLength: LPDWORD;
LocalAddress: PSockAddrIn;
RemoteAddressLength: LPDWORD;
RemoteAddress: PSockAddrIn;
timeout: PTimeVal;
Reserved: POverlapped): Boolean; stdcall;
TSHA1Context = record
FLength: int64;
FInterimHash: array[0..4] of Longint;
FComputed: boolean;
FCorrupted: boolean;
FMsgBlockIndex: byte;
FMsgBlock: array[0..63] of Byte
end;
var
WSAData: WinSock.WSADATA;
WS2_32: THandle;
WSAConnectByNameA: TWSAConnectByNameA;
{$Q-}
procedure sha1_ProcessMessageBlock(var Context: TSHA1Context);
const
ctKeys: array[0..3] of Longint =
(Longint($5A827999), Longint($6ED9EBA1), Longint($8F1BBCDC), Longint($CA62C1D6));
var
A: Longint;
B: Longint;
C: Longint;
D: Longint;
E: Longint;
I: Integer;
J: Integer;
Temp: Longint;
W: array [0..79] of Longint;
begin
for I := 0 to 15 do
begin
J := I * 4;
W[I] := Context.FMsgBlock[J + 0] shl 24;
W[I] := W[I] or Context.FMsgBlock[J + 1] shl 16;
W[I] := W[I] or Context.FMsgBlock[J + 2] shl 8;
W[I] := W[I] or Context.FMsgBlock[J + 3];
end;
for I := 16 to 79 do
begin
W[I] := W[I-3] xor W[I-8] xor W[I-14] xor W[I-16];
W[I] := (W[I] shl 1) or (W[I] shr 31);
end;
A := Context.FInterimHash[0];
B := Context.FInterimHash[1];
C := Context.FInterimHash[2];
D := Context.FInterimHash[3];
E := Context.FInterimHash[4];
for I := 0 to 19 do
begin
Temp := ((A shl 5) or (A shr 27)) + ((B and C) or ((not B) and D)) + E + W[I] + ctKeys[0];
E := D;
D := C;
C := (B shl 30) or (B shr 2);
B := A;
A := Temp;
end;
for I := 20 to 39 do
begin
Temp := ((A shl 5) or (A shr 27)) + (B xor C xor D) + E + W[I] + ctKeys[1];
E := D;
D := C;
C := (B shl 30) or (B shr 2);
B := A;
A := Temp;
end;
for I := 40 to 59 do
begin
Temp := ((A shl 5) or (A shr 27)) + ((B and C) or (B and D) or (C and D)) + E + W[I] + ctKeys[2];
E := D;
D := C;
C := (B shl 30) or (B shr 2);
B := A;
A := Temp;
end;
for I := 60 to 79 do
begin
Temp := ((A shl 5) or (A shr 27)) + (B xor C xor D) + E + W[I] + ctKeys[3];
E := D;
D := C;
C := (B shl 30) or (B shr 2);
B := A;
A := Temp;
end;
Context.FInterimHash[0] := Context.FInterimHash[0] + A;
Context.FInterimHash[1] := Context.FInterimHash[1] + B;
Context.FInterimHash[2] := Context.FInterimHash[2] + C;
Context.FInterimHash[3] := Context.FInterimHash[3] + D;
Context.FInterimHash[4] := Context.FInterimHash[4] + E;
Context.FMsgBlockIndex := 0;
end;
procedure sha1_reset(var context: TSHA1Context);
const
ctSHAKeys: array[0..4] of Longint =
(Longint($67452301), Longint($EFCDAB89), Longint($98BADCFE), Longint($10325476), Longint($C3D2E1F0));
begin
context.FLength := 0;
context.FMsgBlockIndex := 0;
context.FInterimHash[0] := ctSHAKeys[0];
context.FInterimHash[1] := ctSHAKeys[1];
context.FInterimHash[2] := ctSHAKeys[2];
context.FInterimHash[3] := ctSHAKeys[3];
context.FInterimHash[4] := ctSHAKeys[4];
context.FComputed := false;
context.FCorrupted := false;
FillChar(context.FMsgBlock[0], 64, #0);
end;
procedure sha1_input(var context: TSHA1Context; msgArray :PAnsiChar; msgLen:cardinal);
begin
Assert(Assigned(msgArray), 'Empty array paased to sha1Input');
if (context.FComputed) then
context.FCorrupted := True;
if (not context.FCorrupted) then
while (msgLen > 0) do
begin
context.FMsgBlock[context.FMsgBlockIndex] := byte(msgArray[0]);
Inc(context.FMsgBlockIndex);
context.FLength := context.FLength+8;
if (context.FMsgBlockIndex = 64) then
sha1_ProcessMessageBlock(context);
Dec(msgLen);
Inc(msgArray);
end;
end;
procedure sha1_result(var context: TSHA1Context; msgDigest: PAnsiChar);
var
I: Integer;
begin
Assert(Assigned(msgDigest), 'Empty array passed to sha1Result');
if (not context.FCorrupted) then
begin
if (not context.FComputed) then
begin
I := context.FMsgBlockIndex;
if (I <= 55) then
begin
context.FMsgBlock[I] := $80;
Inc(I);
FillChar(context.FMsgBlock[I], (56-I), #0);
context.FMsgBlockIndex := 56;
end
else
begin
context.FMsgBlock[I] := $80;
Inc(I);
FillChar(context.FMsgBlock[I], (64-I), #0);
context.FMsgBlockIndex := 64;
sha1_ProcessMessageBlock(context);
FillChar(context.FMsgBlock[0], 56, #0);
context.FMsgBlockIndex := 56;
end;
context.FMsgBlock[56] := (context.FLength shr 56) and $FF;
context.FMsgBlock[57] := (context.FLength shr 48) and $FF;
context.FMsgBlock[58] := (context.FLength shr 40) and $FF;
context.FMsgBlock[59] := (context.FLength shr 32) and $FF;
context.FMsgBlock[60] := (context.FLength shr 24) and $FF;
context.FMsgBlock[61] := (context.FLength shr 16) and $FF;
context.FMsgBlock[62] := (context.FLength shr 8) and $FF;
context.FMsgBlock[63] := (context.FLength ) and $FF;
sha1_ProcessMessageBlock(context);
FillChar(context.FMsgBlock, SizeOf(context.FMsgBlock), #0);
context.FLength := 0;
context.FComputed := True;
end;
for I := 0 to SCRAMBLE_LENGTH -1 do
msgDigest[I] := AnsiChar(context.FInterimHash[I shr 2] shr (8 * (3 - (I and 3))) and $FF);
end;
end;
function Scramble(const Password: my_char; const Salt: my_char): RawByteString;
procedure hashPassword(const pass: my_char; var res0, res1: my_int);
var
nr, add, nr2, tmp: my_ulonglong;
I: my_int;
e1: my_ulonglong;
len: my_int;
begin
nr := 1345345333;
add := 7;
nr2 := $12345671;
len := Length(pass)-1;
for I := 0 to len do
begin
if (Pass[I] = #20) or (Pass[I] = #9)then
continue;
tmp := $ff and Byte(Pass[I]);
e1 := (((nr and 63) +add)*tmp)+(nr shl 8);
nr := nr xor e1;
nr2 := nr2+((nr2 shl 8) xor nr);
add := add+tmp;
end;
res0 := nr and $7fffffff;
res1 := nr2 and $7fffffff;
end;
function Floor(X: Extended): my_int;
begin
Result := Trunc(X);
if ((X < 0) and (Result <> X)) then
Dec(Result);
end;
var
dRes: Double;
e: Byte;
hm0: my_int;
hm1: my_int;
hp0: my_int;
hp1: my_int;
I: my_int;
maxValue: my_ulonglong;
Scramled: array [0..7] of AnsiChar;
Seed: my_ulonglong;
Seed2: my_ulonglong;
begin
hashPassword(Password, hp0, hp1);
hashPassword(Salt, hm0, hm1);
MaxValue := $3FFFFFFF;
Seed := (hp0 xor hm0) mod maxValue ;
Seed2 := (hp1 xor hm1) mod maxValue ;
for I := 0 to StrLen(Salt) - 1 do
begin
Seed := (Seed * 3 + Seed2) mod MaxValue;
Seed2 := (Seed + Seed2 + 33) mod MaxValue;
dRes := Seed / maxValue;
Scramled[I] := AnsiChar(Floor(dRes * 31) + 64);
end;
dRes := (Seed * 3 + Seed2) mod MaxValue / MaxValue;
e := Floor(dRes * 31);
for I := 0 to StrLen(Salt) - 1 do
Scramled[I] := AnsiChar(Byte(Scramled[I]) xor e);
SetString(Result, PAnsiChar(@Scramled), StrLen(Salt));
end;
function SecureScramble(const Password: my_char; const Salt: my_char): RawByteString;
var
hash_stage1: array [0 .. SCRAMBLE_LENGTH - 1] of AnsiChar;
hash_stage2: array [0 .. SCRAMBLE_LENGTH - 1] of AnsiChar;
I: my_int;
Scramled: array [0 .. SCRAMBLE_LENGTH - 1] of AnsiChar;
sha1_context: TSHA1Context;
begin
sha1_reset(sha1_context);
//* stage 1: hash Password */
sha1_input(sha1_context, Password, StrLen(Password));
sha1_result(sha1_context, @hash_stage1[0]);
//* stage 2: hash stage 1; note that hash_stage2 is stored in the database */
sha1_reset(sha1_context);
sha1_input(sha1_context, @hash_stage1[0], SCRAMBLE_LENGTH);
sha1_result(sha1_context, @hash_stage2[0]);
//* create crypt AnsiString as sha1(message, hash_stage2) */;
sha1_reset(sha1_context);
sha1_input(sha1_context, PAnsiChar(Salt), SCRAMBLE_LENGTH);
sha1_input(sha1_context, @hash_stage2[0], SCRAMBLE_LENGTH);
//* xor allows 'from' and 'to' overlap: lets take advantage of it */
sha1_result(sha1_context, @scramled);
for I := 0 to SCRAMBLE_LENGTH - 1 do
Scramled[I] := AnsiChar(Byte(Scramled[I]) xor Byte(hash_stage1[I]));
SetString(Result, PAnsiChar(@Scramled), SCRAMBLE_LENGTH);
end;
{$IFDEF Debug}
{$Q+}
{$ENDIF}
{ C API functions *************************************************************}
function mysql_affected_rows(mysql: MYSQL): my_ulonglong; stdcall;
begin
Result := mysql.affected_rows();
end;
// function mysql_change_user(mysql: MYSQL; const user, passwd, db: my_char): my_bool; stdcall;
function mysql_character_set_name(mysql: MYSQL): my_char; stdcall;
begin
Result := mysql.character_set_name();
end;
procedure mysql_close(mysql: MYSQL); stdcall;
begin
mysql.Free();
end;
// function mysql_connect(mysql: MYSQL; const host, user, passwd: my_char): MYSQL; stdcall;
// function mysql_create_db(mysql: MYSQL; const DB: my_char): my_int; stdcall;
procedure mysql_data_seek(res: MYSQL_RES; offset: my_ulonglong); stdcall;
begin
res.data_seek(offset);
end;
// procedure mysql_debug(const debug: my_char); stdcall;
// function mysql_drop_db(mysql: MYSQL; const DB: my_char): my_int; stdcall;
// function mysql_dump_debug_info(mysql: MYSQL): my_int; stdcall;
function mysql_eof(mysql: MYSQL): my_bool; stdcall;
begin
Result := mysql.eof();
end;
function mysql_errno(mysql: MYSQL): my_uint; stdcall;
begin
Result := mysql.errno();
end;
function mysql_error(mysql: MYSQL): my_char; stdcall;
begin
Result := my_char(mysql.error());
end;
// function mysql_escape_string(_to: my_char; const from: my_char; from_length: my_uint): my_uint; stdcall;
function mysql_fetch_field(res: MYSQL_RES): MYSQL_FIELD; stdcall;
begin
Result := res.fetch_field();
end;
function mysql_fetch_fields(res: MYSQL_RES): MYSQL_FIELDS; stdcall;
begin
Result := res.fetch_fields();
end;
function mysql_fetch_field_direct(res: MYSQL_RES; fieldnr: my_uint): MYSQL_FIELD; stdcall;
begin
Result := res.fetch_field_direct(fieldnr);
end;
function mysql_fetch_lengths(res: MYSQL_RES): MYSQL_LENGTHS; stdcall;
begin
Result := res.fetch_lengths();
end;
function mysql_fetch_row(res: MYSQL_RES): MYSQL_ROW; stdcall;
begin
Result := res.fetch_row();
end;
function mysql_field_count(mysql: MYSQL): my_uint; stdcall;
begin
Result := mysql.res.FieldCount;
end;
procedure mysql_free_result(res: MYSQL_RES); stdcall;
begin
res.mysql.Seterror(0);
res.Free();
end;
function mysql_get_client_info(): my_char; stdcall;
begin
Result := MYSQL_CLIENT_INFO;
end;
function mysql_get_client_version: my_uint;
begin
Result := MYSQL_CLIENT_VERSION;
end;
function mysql_get_host_info(mysql: MYSQL): my_char; stdcall;
begin
Result := mysql.get_host_info();
end;
function mysql_get_proto_info(mysql: MYSQL): my_uint; stdcall;
begin
Result := PROTOCOL_VERSION;
end;
function mysql_get_server_info(mysql: MYSQL): my_char; stdcall;
begin
Result := mysql.get_server_info();
end;
function mysql_get_server_status(mysql: MYSQL): my_uint; stdcall;
begin
Result := mysql.get_server_status();
end;
function mysql_get_server_version(mysql: MYSQL): my_uint; stdcall;
begin
Result := mysql.get_server_version();
end;
function mysql_info(mysql: MYSQL): my_char; stdcall;
begin
Result := mysql.info();
end;
function mysql_init(mysql: MYSQL): MYSQL; stdcall;
begin
if (Assigned(mysql)) then
Result := mysql
else
Result := MySQLClient.MYSQL.Create();
end;
function mysql_insert_id(mysql: MYSQL): my_ulonglong; stdcall;
begin
Result := mysql.insert_id();
end;
function mysql_kill(mysql: MYSQL; pid: my_uint): my_int; stdcall;
begin
Result := mysql.kill(pid);
end;
// function mysql_list_dbs(mysql: MYSQL; const wild: my_char): MYSQL_RES; stdcall;
// function mysql_list_fields(mysql: MYSQL; const table, wild: my_char): MYSQL_RES; stdcall;
// function mysql_list_processes(mysql: MYSQL): MYSQL_RES; stdcall;
// function mysql_list_tables(mysql: MYSQL; const wild: my_char): MYSQL_RES; stdcall;
function mysql_num_fields(res: MYSQL_RES): my_uint; stdcall;
begin
Result := res.num_fields();
end;
function mysql_num_rows(res: MYSQL_RES): my_ulonglong; stdcall;
begin
Result := res.num_rows();
end;
function mysql_options(mysql: MYSQL; option: enum_mysql_option; const arg: my_char): my_int; stdcall;
begin
Result := mysql.options(option, arg);
end;
function mysql_ping(mysql: MYSQL): my_int; stdcall;
begin
Result := mysql.ping();
end;
function mysql_query(mysql: MYSQL; const q: my_char): my_int; stdcall;
begin
Result := mysql.query(q);
end;
function mysql_real_connect(mysql: MYSQL; host, user, passwd, db: my_char; port: my_uint; unix_socket: my_char; client_flag: my_uint): MYSQL; stdcall;
begin
Result := mysql.real_connect(host, user, passwd, db, port, unix_socket, client_flag);
end;
function mysql_real_escape_string(mysql: MYSQL; _to: my_char; const from: my_char; length: my_uint): my_uint; stdcall;
begin
Result := mysql.real_escape_string(_to, from, length);
end;
function mysql_real_query(mysql: MYSQL; query: my_char; length: my_int): my_int; stdcall;
begin
Result := mysql.real_query(query, length);
end;
// function mysql_reload(mysql: MYSQL): my_int; stdcall;
// function mysql_row_seek(res: MYSQL_RES; offset: MYSQL_ROW_OFFSET): MYSQL_ROW_OFFSET; stdcall;
// function mysql_row_tell(res: MYSQL_RES): MYSQL_ROW_OFFSET; stdcall;
function mysql_select_db(mysql: MYSQL; const db: my_char): my_int; stdcall;
begin
Result := mysql.select_db(db);
end;
function mysql_set_character_set(mysql: MYSQL; const csname: my_char): my_int; stdcall;
begin
Result := mysql.set_character_set(csname);
end;
function mysql_set_server_option(mysql: MYSQL; option: enum_mysql_set_option): my_int; stdcall;
begin
Result := mysql.set_server_option(MySQLConsts.enum_mysql_set_option(option));
end;
function mysql_shutdown(mysql: MYSQL; shutdown_level: mysql_enum_shutdown_level): my_int; stdcall;
begin
Result := mysql.shutdown(shutdown_level);
end;
function mysql_sqlstate(mysql: MYSQL): my_char; stdcall;
begin
Result := mysql.sqlstate();
end;
// function mysql_ssl_set(mysql: MYSQL; key, cert, ca, capath, cipher: my_char): my_int; stdcall;
// function mysql_stat(mysql: MYSQL): my_char; stdcall;
function mysql_store_result(mysql: MYSQL): MYSQL_RES; stdcall;
begin
Result := mysql.store_result();
end;
function mysql_thread_id(mysql: MYSQL): my_uint; stdcall;
begin
Result := mysql.thread_id();
end;
function mysql_use_result(mysql: MYSQL): MYSQL_RES; stdcall;
begin
Result := mysql.use_result();
end;
function mysql_warning_count(mysql: MYSQL): my_uint; stdcall;
begin
Result := mysql.warning_count();
end;
// function mysql_commit(mysql: MYSQL): my_bool; stdcall;
// function mysql_rollback(mysql: MYSQL): my_bool; stdcall;
// function mysql_autocommit(mysql: MYSQL; mode: my_bool): my_bool; stdcall;
function mysql_more_results(mysql: MYSQL): my_bool; stdcall;
begin
Result := mysql.more_results();
end;
function mysql_next_result(mysql: MYSQL): my_int; stdcall;
begin
Result := mysql.next_result();
end;
procedure mysql_set_local_infile_default(mysql: MYSQL); cdecl;
begin
mysql.set_local_infile_default();
end;
procedure mysql_set_local_infile_handler(mysql: MYSQL; local_infile_init: Tlocal_infile_init; local_infile_read: Tlocal_infile_read; local_infile_end: Tlocal_infile_end; local_infile_error: Tlocal_infile_error; userdata: Pointer); cdecl;
begin
mysql.set_local_infile_handler(local_infile_init, local_infile_read, local_infile_end, local_infile_error, userdata);
end;
{ TMySQL_IO *******************************************************************}
procedure TMySQL_IO.Close();
begin
case (IOType) of
itNamedPipe:
begin
CloseHandle(Pipe); Pipe := INVALID_HANDLE_VALUE;
end;
itTCPIP:
begin
shutdown(Socket, SD_BOTH);
closesocket(Socket); Socket := INVALID_SOCKET;
end;
end;
IOType := itNone;
end;
constructor TMySQL_IO.Create();
begin
FError := '';
FErrno := 0;
FDirection := idRead;
Pipe := INVALID_HANDLE_VALUE;
Socket := INVALID_SOCKET;
IOType := itNone;
end;
destructor TMySQL_IO.Destroy();
begin
if (IOType <> itNone) then
Close();
inherited;
end;
function TMySQL_IO.errno(): my_uint;
begin
Result := FErrNo;
end;
function TMySQL_IO.error(): RawByteString;
begin
Result := FError;
end;
function TMySQL_IO.GetCodePage(): Cardinal;
begin
Result := 1252; // Base on the default character set "latin1"
end;
function TMySQL_IO.DecodeString(const Str: RawByteString): string;
var
Len: Integer;
begin
if (Str = '') then
Result := ''
else
begin
Len := MultiByteToWideChar(CodePage, 0, PAnsiChar(Str), Length(Str), nil, 0);
SetLength(Result, Len);
MultiByteToWideChar(CodePage, 0, PAnsiChar(Str), Length(Str), PChar(@Result[1]), Len);
end;
end;
function TMySQL_IO.EncodeString(const Str: string): RawByteString;
var
Len: Integer;
begin
if (Str = '') then
Result := ''
else
begin
Len := WideCharToMultiByte(CodePage, 0, PChar(Str), Length(Str), nil, 0, nil, nil);
SetLength(Result, Len);
WideCharToMultiByte(CodePage, 0, PChar(Str), Length(Str), PAnsiChar(@Result[1]), Len, nil, nil);
end;
end;
function TMySQL_IO.Open(const AIOType: TMYSQL_IO.TType;
const Host, PipeName: RawByteString; const Port, Timeout: my_uint): Boolean;
var
Filename: string;
HostEnt: PHostEnt;
ip_addr: u_long;
KeepAlive: BOOL;
Mode: ULONG;
RcvBuf: u_int;
ReadFDS: TFDSet;
sock_addr: sockaddr_in;
Time: timeval;
begin
Seterror(0);
case (AIOType) of
itNamedPipe:
begin
if (Host = LOCAL_HOST) then
Filename := DecodeString('\\' + LOCAL_HOST_NAMEDPIPE + '\pipe\' + PipeName)
else
Filename := DecodeString('\\' + Host + '\pipe\' + PipeName);
if (not WaitNamedPipe(PChar(Filename), Timeout * 1000)) then
if (GetLastError() = 2) then
Seterror(CR_NAMEDPIPEOPEN_ERROR, EncodeString(Format(CLIENT_ERRORS[CR_NAMEDPIPEOPEN_ERROR - CR_MIN_ERROR], [LOCAL_HOST, PipeName, GetLastError()])))
else
Seterror(CR_NAMEDPIPEWAIT_ERROR, EncodeString(Format(CLIENT_ERRORS[CR_NAMEDPIPEWAIT_ERROR - CR_MIN_ERROR], [LOCAL_HOST, PipeName, GetLastError()])))
else
begin
Pipe := CreateFile(PChar(Filename), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0);
if (Pipe = INVALID_HANDLE_VALUE) then
if (GetLastError() = ERROR_PIPE_BUSY) then
Seterror(CR_NAMEDPIPEWAIT_ERROR, EncodeString(Format(CLIENT_ERRORS[CR_NAMEDPIPEWAIT_ERROR - CR_MIN_ERROR], [LOCAL_HOST, PipeName, GetLastError()])))
else
Seterror(CR_NAMEDPIPEOPEN_ERROR, EncodeString(Format(CLIENT_ERRORS[CR_NAMEDPIPEOPEN_ERROR - CR_MIN_ERROR], [LOCAL_HOST, PipeName, GetLastError()])))
else
begin
Mode := PIPE_READMODE_BYTE or PIPE_WAIT;
if (not SetNamedPipeHandleState(Pipe, Mode, nil, nil)) then
begin
CloseHandle(Pipe); Pipe := INVALID_HANDLE_VALUE;
Seterror(CR_NAMEDPIPESETSTATE_ERROR, EncodeString(Format(CLIENT_ERRORS[CR_NAMEDPIPESETSTATE_ERROR - CR_MIN_ERROR], [Host, PipeName, GetLastError()])));
end
else
IOType := itNamedPipe;
end;
end;
end;
itTCPIP:
if ((WSAData.wVersion = 0) and (WSAStartup($0101, WSAData) <> 0)) then
Seterror(CR_UNKNOWN_ERROR)
else
begin
ip_addr := inet_addr(PAnsiChar(Host));
if (ip_addr = u_long(INADDR_NONE)) then
begin
HostEnt := gethostbyname(PAnsiChar(Host));
if (not Assigned(HostEnt)) then