-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathgds.cpp
More file actions
4735 lines (3989 loc) · 112 KB
/
gds.cpp
File metadata and controls
4735 lines (3989 loc) · 112 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
/*
* PROGRAM: JRD Access Method
* MODULE: gds.cpp
* DESCRIPTION: User callable routines
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (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.Inprise.com/IPL.html
*
* 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 was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* 2002.02.15 Sean Leyne - Code Cleanup, removed obsolete "IMP" port
* 2002.02.15 Sean Leyne - Code Cleanup, removed obsolete "M88K" port
*
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
* 2002.10.30 Sean Leyne - Removed support for obsolete "PC_PLATFORM" define
* 2003.05.11 Nickolay Samofatov - rework temp stuff
*
*/
// 11 Sept 2002 Nickolay Samofatov
// this defined in included dsc2.h
//#define ISC_TIME_SECONDS_PRECISION 10000L
//#define ISC_TIME_SECONDS_PRECISION_SCALE -4
#include "firebird.h"
#include "firebird/impl/msg_helper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "firebird/Interface.h"
#include "../common/gdsassert.h"
#include "../common/file_params.h"
#include "../common/msg_encode.h"
#include "../yvalve/gds_proto.h"
#include "../common/os/path_utils.h"
#include "../common/dsc.h"
#include "../common/TimeZoneUtil.h"
#include "../jrd/constants.h"
#include "../jrd/status.h"
#include "../common/os/os_utils.h"
#include "../common/os/mac_utils.h"
#include "../common/classes/BlrReader.h"
#include "../common/classes/alloc.h"
#include "../common/classes/locks.h"
#include "../common/classes/timestamp.h"
#include "../common/classes/init.h"
#include "../common/classes/TempFile.h"
#include "../common/utils_proto.h"
#include "../common/ThreadStart.h"
#include "../common/Int128.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <errno.h>
#include <stdarg.h>
#if defined(WIN_NT)
#include <io.h> // umask, close, lseek, read, open, _sopen
#include <process.h>
#include <sys/types.h>
#include <sys/timeb.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#ifndef O_RDWR
#include <fcntl.h>
#endif
#include "../common/config/config.h"
#include "iberror.h"
#include "ibase.h"
#include "firebird/impl/blr.h"
#include "../yvalve/msg.h"
#include "../common/isc_proto.h"
#include "../common/os/isc_i_proto.h"
#ifdef WIN_NT
#include <shlobj.h>
#if _MSC_VER <= 1500
#include <shfolder.h>
#endif
#define _WINSOCKAPI_
#include <share.h>
#undef leave
#endif // WIN_NT
#ifdef ANDROID
#include <android/log.h>
#endif
static char fb_prefix_val[MAXPATHLEN];
static char fb_prefix_lock_val[MAXPATHLEN];
static char fb_prefix_msg_val[MAXPATHLEN];
static char fbTempDir[MAXPATHLEN];
static char* fb_prefix = NULL;
static char* fb_prefix_lock = NULL;
static char* fb_prefix_msg = NULL;
#define FB_IMPL_MSG_NO_SYMBOL(facility, number, text)
#define FB_IMPL_MSG_SYMBOL(facility, number, symbol, text)
#define FB_IMPL_MSG(facility, number, symbol, sqlCode, sqlClass, sqlSubClass, text) \
{ENCODE_ISC_MSG(number, FB_IMPL_MSG_FACILITY_##facility), text},
static const struct {
SLONG code_number;
const SCHAR *code_text;
} messages[] = {
#include "firebird/impl/msg/all.h"
{0, nullptr}
};
#undef FB_IMPL_MSG_NO_SYMBOL
#undef FB_IMPL_MSG_SYMBOL
#undef FB_IMPL_MSG
#define FB_IMPL_MSG_NO_SYMBOL(facility, number, text)
#define FB_IMPL_MSG_SYMBOL(facility, number, symbol, text)
#define FB_IMPL_MSG(facility, number, symbol, sqlCode, sqlClass, sqlSubClass, text) \
{ENCODE_ISC_MSG(number, FB_IMPL_MSG_FACILITY_##facility), sqlClass sqlSubClass},
static const struct {
SLONG gds_code;
const char* sql_state;
} sql_states[] = {
#include "firebird/impl/msg/all.h"
{0, nullptr}
};
#undef FB_IMPL_MSG_NO_SYMBOL
#undef FB_IMPL_MSG_SYMBOL
#undef FB_IMPL_MSG
#define FB_IMPL_MSG_NO_SYMBOL(facility, number, text)
#define FB_IMPL_MSG_SYMBOL(facility, number, symbol, text)
#define FB_IMPL_MSG(facility, number, symbol, sqlCode, sqlClass, sqlSubClass, text) \
{ENCODE_ISC_MSG(number, FB_IMPL_MSG_FACILITY_##facility), sqlCode},
static const struct {
SLONG gds_code;
SSHORT sql_code;
} sql_codes[] = {
#include "firebird/impl/msg/all.h"
{0, 0}
};
#undef FB_IMPL_MSG_NO_SYMBOL
#undef FB_IMPL_MSG_SYMBOL
#undef FB_IMPL_MSG
const SLONG GENERIC_SQLCODE = -999;
#include "fb_types.h"
#include "../common/utils_proto.h"
#include "../common/classes/SafeArg.h"
#include "../common/classes/MsgPrint.h"
using Firebird::TimeStamp;
using Firebird::TimeZoneUtil;
using Firebird::BlrReader;
// This structure is used to parse the firebird.msg file.
struct gds_msg
{
ULONG msg_top_tree;
int msg_file;
USHORT msg_bucket_size;
USHORT msg_levels;
SCHAR msg_bucket[1];
};
// CVC: This structure has a totally different layout than "class ctl" from
// blob_filter.h and "struct isc_blob_ctl" from ibase.h. These two should match
// for blob filters to work. However, this one is private to gds.cpp and hence
// I renamed it gds_ctl to avoid confusion and possible name clashes.
// However, filters.cpp calls fb_print_blr(), but this struct is not shared
// between the two modules.
struct gds_ctl
{
BlrReader ctl_blr_reader;
FPTR_PRINT_CALLBACK ctl_routine; // Call back
void* ctl_user_arg; // User argument
SSHORT ctl_language;
Firebird::string ctl_string;
};
#ifdef DEV_BUILD
void GDS_breakpoint(int);
#endif
static void blr_error(gds_ctl*, const TEXT*, ...) ATTRIBUTE_FORMAT(2,3);
static void blr_format(gds_ctl*, const char*, va_list args);
static void blr_format(gds_ctl*, const char*, ...) ATTRIBUTE_FORMAT(2,3);
static void blr_indent(gds_ctl*, SSHORT);
static void blr_print_blr(gds_ctl*, UCHAR);
static SCHAR blr_print_byte(gds_ctl*);
static SCHAR blr_print_char(gds_ctl*);
static void blr_print_cond(gds_ctl*, SSHORT);
static int blr_print_dtype(gds_ctl*);
static void blr_print_join(gds_ctl*);
static SLONG blr_print_line(gds_ctl*, SSHORT);
static void blr_print_name(gds_ctl*);
static void blr_print_verb(gds_ctl*, SSHORT);
static int blr_print_word(gds_ctl*);
static void sanitize(Firebird::string& locale);
// New functions that try to be safe.
static SLONG safe_interpret(char* const s, const FB_SIZE_T bufsize,
const ISC_STATUS** const vector, bool legacy = false);
static void safe_strncpy(char* target, const char* source, size_t bs);
// Useful only in Windows. The hardcoded definition for the English-only version
// is too crude.
static bool GetProgramFilesDir(Firebird::PathName& output);
// Generic cleanup handlers
struct clean_t
{
clean_t* clean_next;
void (*clean_routine)(void*);
void* clean_arg;
};
static Firebird::GlobalPtr<Firebird::Mutex> cleanup_handlers_mutex;
static clean_t* cleanup_handlers = NULL;
static Firebird::GlobalPtr<Firebird::Mutex> global_msg_mutex;
static gds_msg* global_default_msg = NULL;
VoidPtr API_ROUTINE gds__alloc_debug(SLONG size_request, const TEXT* filename, ULONG lineno)
{
try
{
return getDefaultMemoryPool()->allocate(size_request
#ifdef DEBUG_GDS_ALLOC
, filename, lineno
#endif
);
}
catch (const Firebird::Exception&)
{
return NULL;
}
}
ULONG API_ROUTINE gds__free(void* blk)
{
getDefaultMemoryPool()->deallocate(blk);
return 0;
}
#ifdef UNIX
static SLONG gds_pid = 0;
#endif
// BLR Pretty print stuff
const int op_line = 1;
const int op_verb = 2;
const int op_byte = 3;
const int op_word = 4;
const int op_pad = 5;
const int op_dtype = 6;
const int op_message = 7;
const int op_literal = 8;
const int op_begin = 9;
const int op_map = 10;
const int op_args = 11;
const int op_union = 12;
const int op_indent = 13;
const int op_join = 14;
const int op_parameters = 15;
const int op_error_handler = 16;
const int op_set_error = 17;
const int op_literals = 18;
const int op_exec_into = 21;
const int op_cursor_stmt = 22;
const int op_byte_opt_verb = 23;
const int op_exec_stmt = 24;
const int op_derived_expr = 25;
const int op_partition_args = 26;
const int op_subproc_decl = 27;
const int op_subfunc_decl = 28;
const int op_window_win = 29;
const int op_erase = 30; // special due to optional blr_marks after blr_erase
const int op_dcl_local_table = 31;
const int op_outer_map = 32;
const int op_invoke_function = 33;
const int op_invsel_procedure = 34;
const int op_table_value_fun = 35;
const int op_for_range = 36;
static const UCHAR
// generic print formats
zero[] = { op_line, 0 },
one[] = { op_line, op_verb, 0},
two[] = { op_line, op_verb, op_verb, 0},
three[] = { op_line, op_verb, op_verb, op_verb, 0},
field[] = { op_byte, op_byte, op_literal, op_pad, op_line, 0},
byte_line[] = { op_byte, op_line, 0},
byte_args[] = { op_byte, op_line, op_args, 0},
byte_verb[] = { op_byte, op_line, op_verb, 0},
byte_literal[] = { op_byte, op_literal, op_line, 0},
byte_byte_verb[] = { op_byte, op_byte, op_line, op_verb, 0},
verb_byte_verb[] = { op_verb, op_byte, op_line, op_verb, 0},
parm[] = { op_byte, op_word, op_line, 0}, // also field id
parm2[] = { op_byte, op_word, op_word, op_line, 0},
parm3[] = { op_byte, op_word, op_word, op_word, op_line, 0},
// formats specific to a verb
begin[] = { op_line, op_begin, op_verb, 0},
literal[] = { op_dtype, op_literal, op_line, 0},
message[] = { op_byte, op_word, op_line, op_message, 0},
rse[] = { op_byte, op_line, op_begin, op_verb, 0},
relation[] = { op_byte, op_literal, op_pad, op_byte, op_line, 0},
relation2[] = { op_byte, op_literal, op_line, op_indent, op_byte,
op_literal, op_pad, op_byte, op_line, 0},
relation3[] = { op_line, op_indent, op_byte, op_literal,
op_line, op_indent, op_byte, op_literal,
op_line, op_indent, op_byte, op_literal,
op_line, op_indent, op_byte, op_line, 0},
aggregate[] = { op_byte, op_line, op_verb, op_verb, op_verb, 0},
rid[] = { op_word, op_byte, op_line, 0},
rid2[] = { op_word, op_byte, op_literal, op_pad, op_byte, op_line, 0},
union_ops[] = { op_byte, op_byte, op_line, op_union, 0},
map[] = { op_word, op_line, op_map, 0},
function[] = { op_byte, op_literal, op_byte, op_line, op_args, 0},
function2[] = { op_byte, op_literal, op_pad, op_byte, op_literal, op_pad, op_byte, op_line,
op_args, 0},
gen_id[] = { op_byte, op_literal, op_line, op_verb, 0},
gen_id2[] = { op_byte, op_literal, op_line, 0},
gen_id3[] = { op_line, op_indent, op_byte, op_literal, op_line, op_indent, op_byte, op_literal,
op_line, op_indent, op_byte_opt_verb, 0},
declare[] = { op_word, op_dtype, op_line, 0},
one_word[] = { op_word, op_line, 0},
indx[] = { op_line, op_verb, op_indent, op_byte, op_line, op_args, 0},
seek[] = { op_line, op_verb, op_verb, 0},
join[] = { op_join, op_line, 0},
exec_proc[] = { op_byte, op_literal, op_line, op_indent, op_word, op_line,
op_parameters, op_indent, op_word, op_line, op_parameters, 0},
exec_proc2[] = { op_byte, op_literal, op_pad, op_byte, op_literal, op_line, op_indent, op_word, op_line,
op_parameters, op_indent, op_word, op_line, op_parameters, 0},
procedure[] = { op_byte, op_literal, op_pad, op_byte, op_line, op_indent,
op_word, op_line, op_parameters, 0},
procedure2[] = { op_byte, op_literal, op_pad, op_byte, op_literal, op_pad, op_byte, op_line, op_indent,
op_word, op_line, op_parameters, 0},
procedure3[] = { op_byte, op_literal, op_pad, op_byte, op_literal, op_pad,
op_byte, op_line, op_indent,
op_word, op_line, op_parameters, 0},
procedure4[] = { op_byte, op_literal, op_pad, op_byte, op_literal, op_pad,
op_byte, op_literal, op_pad, op_byte, op_line, op_indent,
op_word, op_line, op_parameters, 0},
pid[] = { op_word, op_pad, op_byte, op_line, op_indent, op_word,
op_line, op_parameters, 0},
pid2[] = { op_word, op_byte, op_literal, op_pad, op_byte, op_line, op_indent, op_word,
op_line, op_parameters, 0},
error_handler[] = { op_word, op_line, op_error_handler, 0},
set_error[] = { op_set_error, op_line, 0},
cast[] = { op_dtype, op_line, op_verb, 0},
indices[] = { op_byte, op_line, op_literals, 0},
extract[] = { op_line, op_byte, op_verb, 0},
user_savepoint[] = { op_byte, op_byte, op_literal, op_line, 0},
exec_into[] = { op_word, op_line, op_indent, op_exec_into, 0},
dcl_cursor[] = { op_word, op_line, op_verb, op_indent, op_word, op_line, op_args, 0},
dcl_local_table[] = { op_dcl_local_table, 0 },
cursor_stmt[] = { op_cursor_stmt, 0 },
strlength[] = { op_byte, op_line, op_verb, 0},
trim[] = { op_byte, op_byte_opt_verb, op_verb, 0},
modify2[] = { op_byte, op_byte, op_line, op_verb, op_verb, 0},
similar[] = { op_line, op_verb, op_verb, op_indent, op_byte_opt_verb, 0},
exec_stmt[] = { op_exec_stmt, 0},
derived_expr[] = { op_derived_expr, 0},
window[] = {op_line, op_verb, op_indent, op_byte, op_line, op_args, 0},
partition_by[] = {op_byte, op_line, op_partition_args, op_verb, 0},
decode[] = { op_line, op_verb, op_indent, op_byte, op_line, op_args, op_indent, op_byte,
op_line, op_args, 0},
subproc_decl[] = { op_subproc_decl, 0},
subfunc_decl[] = { op_subfunc_decl, 0},
window_win[] = { op_byte, op_window_win, 0},
relation_field[] = { op_line, op_indent, op_byte, op_literal,
op_line, op_indent, op_byte, op_literal, op_pad, op_line, 0},
store3[] = { op_line, op_byte, op_line, op_verb, op_verb, op_verb, 0},
marks[] = { op_byte, op_literal, op_line, op_verb, 0},
erase[] = { op_erase, 0},
erase2[] = { op_erase, op_verb, 0},
local_table[] = { op_word, op_byte, op_literal, op_byte, op_line, 0},
outer_map[] = { op_outer_map, 0 },
in_list[] = { op_line, op_verb, op_indent, op_word, op_line, op_args, 0},
invoke_function[] = { op_invoke_function, 0 },
invsel_procedure[] = { op_invsel_procedure, 0 },
cast_format[] = { op_line, op_indent, op_byte, op_literal, op_line, op_indent, op_dtype, op_line, op_verb, 0 },
table_value_fun[] = { op_table_value_fun, 0 },
for_range[] = { op_for_range, 0 },
default2[] = { op_line, op_indent, op_byte, op_literal,
op_line, op_indent, op_byte, op_literal,
op_line, op_indent, op_byte, op_literal,
op_pad, op_line, 0};
#include "../jrd/blp.h"
const char* const FB_LOCK_ENV = "FIREBIRD_LOCK";
const char* const FB_MSG_ENV = "FIREBIRD_MSG";
const char* const FB_TMP_ENV = "FIREBIRD_TMP";
#ifdef WIN_NT
#define EXPAND_PATH(relative, absolute) _fullpath(absolute, relative, MAXPATHLEN)
#define COMPARE_PATH(a, b) _stricmp(a, b)
#else
#define EXPAND_PATH(relative, absolute) realpath(relative, absolute)
#define COMPARE_PATH(a, b) strcmp(a, b)
#endif
// This function is very crude, but signal-safe.
// CVC: This function converts a FB_UINT64 into a string. I renamed the param
// maxlen to minlen because it represents the minimum length the number will
// be printed in. However, this means buffer should be large enough to contain
// any ULONG (11 bytes) and thus prevent a buffer overflow. If minlen >= 11,
// then buffer should have be of size = (minlen + 1).
void gds__ulstr(char* buffer, FB_UINT64 value, const int minlen, const char filler)
{
FB_UINT64 n = value;
int c = 0;
do {
n = n / 10;
c++;
} while (n);
if (minlen > c)
c = minlen;
char* p = buffer + c;
do {
*--p = '0' + (value % 10);
value = value / 10;
} while (value);
fb_assert(p >= buffer); // did we overflow?
while (p != buffer) {
*--p = filler;
}
buffer[c] = 0;
}
ISC_STATUS API_ROUTINE gds__decode(ISC_STATUS code, USHORT* fac, USHORT* code_class)
{
/**************************************
*
* g d s _ $ d e c o d e
*
**************************************
*
* Functional description
* Translate a status codes from system dependent form to
* network form.
*
**************************************/
if (!code)
return FB_SUCCESS;
// not an ISC error message
if ((code & ISC_MASK) != ISC_MASK)
return code;
*fac = GET_FACILITY(code);
*code_class = GET_CLASS(code);
return GET_CODE(code);
}
void API_ROUTINE isc_decode_date(const ISC_QUAD* date, void* times_arg)
{
/**************************************
*
* i s c _ d e c o d e _ d a t e
*
**************************************
*
* Functional description
* Convert from internal timestamp format to UNIX time structure.
*
* Note: this API is historical - the preferred entrypoint is
* isc_decode_timestamp
*
**************************************/
isc_decode_timestamp((const GDS_TIMESTAMP*) date, times_arg);
}
void API_ROUTINE isc_decode_sql_date(const GDS_DATE* date, void* times_arg)
{
/**************************************
*
* i s c _ d e c o d e _ s q l _ d a t e
*
**************************************
*
* Functional description
* Convert from internal DATE format to UNIX time structure.
*
**************************************/
tm* const times = static_cast<struct tm*>(times_arg);
TimeStamp::decode_date(*date, times);
}
void API_ROUTINE isc_decode_sql_time(const GDS_TIME* sql_time, void* times_arg)
{
/**************************************
*
* i s c _ d e c o d e _ s q l _ t i m e
*
**************************************
*
* Functional description
* Convert from internal TIME format to UNIX time structure.
*
**************************************/
tm* const times = static_cast<struct tm*>(times_arg);
memset(times, 0, sizeof(*times));
Firebird::TimeStamp::decode_time(*sql_time, ×->tm_hour, ×->tm_min, ×->tm_sec);
}
void API_ROUTINE isc_decode_timestamp(const GDS_TIMESTAMP* date, void* times_arg)
{
/**************************************
*
* i s c _ d e c o d e _ t i m e s t a m p
*
**************************************
*
* Functional description
* Convert from internal timestamp format to UNIX time structure.
*
* Note: This routine is intended only for public API use. Engine itself and
* utilities should be using TimeStamp class directly in type-safe manner.
*
**************************************/
tm* const times = static_cast<struct tm*>(times_arg);
Firebird::TimeStamp::decode_timestamp(*date, times);
}
ISC_STATUS API_ROUTINE gds__encode(ISC_STATUS code, USHORT facility)
{
/**************************************
*
* g d s _ $ e n c o d e
*
**************************************
*
* Functional description
* Translate a status codes from network format to system
* dependent form.
*
**************************************/
return (code ? ENCODE_ISC_MSG(code, facility) : FB_SUCCESS);
}
void API_ROUTINE isc_encode_date(const void* times_arg, ISC_QUAD* date)
{
/**************************************
*
* i s c _ e n c o d e _ d a t e
*
**************************************
*
* Functional description
* Convert from UNIX time structure to internal timestamp format.
*
* Note: This API is historical -- the preferred entrypoint is
* isc_encode_timestamp
*
**************************************/
isc_encode_timestamp(times_arg, (ISC_TIMESTAMP*) date);
}
void API_ROUTINE isc_encode_sql_date(const void* times_arg, GDS_DATE* date)
{
/**************************************
*
* i s c _ e n c o d e _ s q l _ d a t e
*
**************************************
*
* Functional description
* Convert from UNIX time structure to internal date format.
*
**************************************/
const tm* const times = static_cast<const struct tm*>(times_arg);
*date = TimeStamp::encode_date(times);
}
void API_ROUTINE isc_encode_sql_time(const void* times_arg, GDS_TIME* isc_time)
{
/**************************************
*
* i s c _ e n c o d e _ s q l _ t i m e
*
**************************************
*
* Functional description
* Convert from UNIX time structure to internal TIME format.
*
**************************************/
const tm* const times = static_cast<const struct tm*>(times_arg);
*isc_time = Firebird::TimeStamp::encode_time(times->tm_hour, times->tm_min, times->tm_sec);
}
void API_ROUTINE isc_encode_timestamp(const void* times_arg, GDS_TIMESTAMP* date)
{
/**************************************
*
* i s c _ e n c o d e _ t i m e s t a m p
*
**************************************
*
* Functional description
* Convert from UNIX time structure to internal timestamp format.
*
* Note: This routine is intended only for public API use. Engine itself and
* utilities should be using TimeStamp class directly in type-safe manner.
*
**************************************/
const tm* const times = static_cast<const struct tm*>(times_arg);
*date = Firebird::TimeStamp::encode_timestamp(times);
}
#ifdef DEV_BUILD
void GDS_breakpoint(int /*parameter*/)
{
/**************************************
*
* G D S _ b r e a k p o i n t
*
**************************************
*
* Functional description
* Just a handy place to put a debugger breakpoint
* Calls to this can then be embedded for DEV_BUILD
* using the BREAKPOINT(x) macro.
*
**************************************/
// Put a breakpoint here via the debugger
}
#endif
SINT64 API_ROUTINE isc_portable_integer(const UCHAR* ptr, SSHORT length)
{
/**************************************
*
* i s c _ p o r t a b l e _ i n t e g e r
*
**************************************
*
* Functional description
* Pick up (and convert) a Little Endian (VAX) style integer
* of variable length to local system's Endian format.
*
* various parameter blocks (i.e., dpb, tpb, spb) flatten out multibyte
* values into little endian byte ordering before network transmission.
* programs need to use this function in order to get the correct value
* of the integer in the correct Endian format.
*
* **Note**
*
* This function is similar to gds__vax_integer() in functionality.
* The difference is in the return type. Changed from SLONG to SINT64
* Since gds__vax_integer() is a public API routine, it could not be
* changed. This function has been made public so gbak can use it.
*
**************************************/
if (!ptr || length <= 0 || length > 8)
return 0;
SINT64 value = 0;
int shift = 0;
while (--length > 0)
{
value += ((SINT64) *ptr++) << shift;
shift += 8;
}
value += ((SINT64)(SCHAR) *ptr) << shift;
return value;
}
void API_ROUTINE gds_alloc_flag_unfreed(void* /*blk*/)
{
/**************************************
*
* g d s _ a l l o c _ f l a g _ u n f r e e d
*
**************************************
*
* Functional description
* Flag a buffer as "known to be unfreed" so we
* don't report it in gds_alloc_report
*
**************************************/
// JMB: need to rework this for the new pools
// Skidder: Not sure we need to rework this routine.
// What we really need is to fix all memory leaks including very old.
}
void API_ROUTINE gds_alloc_report(ULONG flags, const char* filter_filename, int /*lineno*/)
{
/**************************************
*
* g d s _ a l l o c _ r e p o r t
*
**************************************
*
* Functional description
* Print buffers that might be memory leaks.
* Or that might have been clobbered.
*
**************************************/
// Skidder: Calls to this function must be replaced with MemoryPool::print_contents
Firebird::PathName report_name = fb_utils::getPrefix(Firebird::IConfigManager::DIR_LOG, "fbsrvreport.txt");
// Our new facilities don't expose flags for reporting.
const bool used_only = !(flags & ALLOC_verbose);
getDefaultMemoryPool()->print_contents(report_name.c_str(),
used_only ? MemoryPool::PRINT_USED_ONLY : 0, filter_filename);
}
/**
fb_interpret
@brief Buffer overrun-aware version of the old gds__interprete
without the double underscore and without the misspelling.
Translate a status code with arguments to a string. Return the
length of the string while updating the vector address. If the
message is null (end of messages) or invalid, return 0;
@param s the output buffer where a human readable version of the error is put
@param bufsize the size of the output buffer
@param vector the input, the address of const pointer to the status vector
that was filled by an API call that reported an error. The function
positions the pointer on the next element of the vector.
**/
SLONG API_ROUTINE fb_interpret(char* s, unsigned int bufsize, const ISC_STATUS** vector)
{
return safe_interpret(s, bufsize, vector);
}
/* CVC: This non-const signature is needed for compatibility. The reason is
that, unlike int* that can be assigned to const int* transparently to the caller,
int** CANNOT be assigned to const int**, so applications would have to be
fixed to provide such const int**; while it may be more correct from the
semantic POV, we can't estimate how much work it's for app developers.
Therefore, we go back to the old signature and provide fb_interpret
for compliance, to be used inside the engine, too. September, 2003.
November, 2004: We agree that fb_interpret is the new, safe interface.
Both gds__interprete and isc_interprete are deprecated. */
SLONG API_ROUTINE gds__interprete(char* s, ISC_STATUS** vector)
{
/**************************************
*
* g d s _ $ i n t e r p r e t e
*
**************************************
*
* Functional description
* See safe_interpret for details. Now this is a wrapper for that function.
* CVC: Since this routine doesn't get the size of the input buffer,
* it's DEPRECATED and we'll assume the buffer size was 1024 as in Borland examples.
*
**************************************/
return safe_interpret(s, 1024, const_cast<const ISC_STATUS**>(vector), true);
}
/**
safe_interpret
@brief Translate a status code with arguments to a string. Return the
length of the string while updating the vector address. If the
message is null (end of messages) or invalid, return 0;
@param s the output buffer where a human readable version of the error is put
@param bufsize the size of the output buffer
@param vector the input, the address of const pointer to the status vector
that was filled by an API call that reported an error. The function
positions the pointer on the next element of the vector.
**/
static SLONG safe_interpret(char* const s, const FB_SIZE_T bufsize,
const ISC_STATUS** const vector, bool legacy)
{
// CVC: It doesn't make sense to provide a buffer smaller than 50 bytes.
// Return error otherwise.
if (bufsize < 50)
return 0;
// Skip the SQLSTATE
if ((*vector)[0] == isc_arg_sql_state)
*vector += 2;
// If the first element of the vector doesn't signal an error, return.
if (!**vector)
return 0;
const ISC_STATUS* v;
ISC_STATUS code;
// Handle a case: "no errors, some warning(s)"
if ((*vector)[1] == 0 && (*vector)[2] == isc_arg_warning)
{
v = *vector + 4;
code = (*vector)[3];
}
else
{
v = *vector + 2;
code = (*vector)[1];
}
const TEXT* args[10];
const TEXT** arg = args;
const char* const* const argend = arg + FB_NELEM(args);
MsgFormat::SafeArg safe;
// Parse and collect any arguments that may be present
TEXT* p = 0;
const TEXT* q;
int temp_len = BUFFER_SMALL;
TEXT* temp = NULL;
for (;;)
{
// CVC: Close overflow in "args".
if (arg >= argend)
break;
int len;
const UCHAR x = (UCHAR) *v++;
switch (x)
{
case isc_arg_string:
*arg++ = (TEXT*) *v;
safe << (TEXT*) *v;
++v;
continue;
case isc_arg_number:
*arg++ = (TEXT*) *v;
safe << *v;
++v;
continue;
case isc_arg_cstring:
if (!temp)
{
// We need a temporary buffer when cstrings are involved.
// Give up if we can't get one.
p = temp = (TEXT*) gds__alloc((SLONG) temp_len);
// FREE: at procedure exit
if (!temp) // NOMEM:
return 0;
}
len = 1 + (int) *v++; // CVC: Add one for the needed terminator.
q = (const TEXT*) *v++;
// Ensure that we do not overflow the buffer allocated
len = (temp_len < len) ? temp_len : len;
if (len)
{
// CVC: On the next iteration, we don't have the full buffer
// but only the remainer of the buffer. We decrement here before
// the loop changes "len".
temp_len -= len;
*arg++ = p;
safe << p;
// We'll silently truncate the parameter to our available space.
while (--len) // CVC: Decrement first to make room for the null terminator.
*p++ = *q++;
*p++ = 0;
}
else // No space at all, pass the empty string.
{
*arg++ = "";
safe << "";
}
continue;
default:
break;
}
--v;
break;
}
// Handle primary code on a system by system basis
switch ((UCHAR) (*vector)[0])
{
case isc_arg_warning:
case isc_arg_gds:
{
while (arg < args + 5) // could be argend, but we only use up to args[4]
*arg++ = 0;
USHORT fac = 0, dummy_class = 0;
const ISC_STATUS decoded = gds__decode(code, &fac, &dummy_class);
if (fb_msg_format(0, fac, (USHORT) decoded, bufsize, s, safe) < 0)
{
bool found = false;
for (int i = 0; messages[i].code_number; ++i)
{
if (code == messages[i].code_number)
{
if (legacy && strchr(messages[i].code_text, '%'))
{
snprintf(s, bufsize, messages[i].code_text,
args[0], args[1], args[2], args[3], args[4]);
}
else
MsgFormat::MsgPrint(s, bufsize, messages[i].code_text, safe);
found = true;
break;
}
}
if (!found) {
snprintf(s, bufsize, "unknown ISC error %" SLONGFORMAT, (SLONG) code); // TXNN
}
}
}
break;
case isc_arg_interpreted:
q = (const TEXT*) (*vector)[1];
if (legacy)
safe_strncpy(s, q, bufsize);