FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlcipher/src/loadext.c at sqlite-release · sqlcipher/sqlcipher · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
sqlcipher
/
sqlcipher
Public
Notifications
You must be signed in to change notification settings
Fork
1.4k
Star
7.3k
Code
Issues
17
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlcipher
/
src
/
loadext.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
945 lines (900 loc) · 24.9 KB
Breadcrumbs
sqlcipher
/
src
/
loadext.c
Copy path
File metadata and controls
945 lines (900 loc) · 24.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
** 2006 June 7
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to dynamically load extensions into
** the SQLite library.
*/
#ifndef
SQLITE_CORE
#define
SQLITE_CORE
1
/* Disable the API redefinition in sqlite3ext.h */
#endif
#include
"sqlite3ext.h"
#include
"sqliteInt.h"
#ifndef
SQLITE_OMIT_LOAD_EXTENSION
/*
** Some API routines are omitted when various features are
** excluded from a build of SQLite. Substitute a NULL pointer
** for any missing APIs.
*/
#ifndef
SQLITE_ENABLE_COLUMN_METADATA
# define
sqlite3_column_database_name
0
# define
sqlite3_column_database_name16
0
# define
sqlite3_column_table_name
0
# define
sqlite3_column_table_name16
0
# define
sqlite3_column_origin_name
0
# define
sqlite3_column_origin_name16
0
#endif
#ifdef
SQLITE_OMIT_AUTHORIZATION
# define
sqlite3_set_authorizer
0
#endif
#ifdef
SQLITE_OMIT_UTF16
# define
sqlite3_bind_text16
0
# define
sqlite3_collation_needed16
0
# define
sqlite3_column_decltype16
0
# define
sqlite3_column_name16
0
# define
sqlite3_column_text16
0
# define
sqlite3_complete16
0
# define
sqlite3_create_collation16
0
# define
sqlite3_create_function16
0
# define
sqlite3_errmsg16
0
# define
sqlite3_open16
0
# define
sqlite3_prepare16
0
# define
sqlite3_prepare16_v2
0
# define
sqlite3_prepare16_v3
0
# define
sqlite3_result_error16
0
# define
sqlite3_result_text16
0
# define
sqlite3_result_text16be
0
# define
sqlite3_result_text16le
0
# define
sqlite3_value_text16
0
# define
sqlite3_value_text16be
0
# define
sqlite3_value_text16le
0
# define
sqlite3_column_database_name16
0
# define
sqlite3_column_table_name16
0
# define
sqlite3_column_origin_name16
0
#endif
#ifdef
SQLITE_OMIT_COMPLETE
# define
sqlite3_complete
0
# define
sqlite3_complete16
0
#endif
#ifdef
SQLITE_OMIT_DECLTYPE
# define
sqlite3_column_decltype16
0
# define
sqlite3_column_decltype
0
#endif
#ifdef
SQLITE_OMIT_PROGRESS_CALLBACK
# define
sqlite3_progress_handler
0
#endif
#ifdef
SQLITE_OMIT_VIRTUALTABLE
# define
sqlite3_create_module
0
# define
sqlite3_create_module_v2
0
# define
sqlite3_declare_vtab
0
# define
sqlite3_vtab_config
0
# define
sqlite3_vtab_on_conflict
0
# define
sqlite3_vtab_collation
0
#endif
#ifdef
SQLITE_OMIT_SHARED_CACHE
# define
sqlite3_enable_shared_cache
0
#endif
#if
defined(
SQLITE_OMIT_TRACE
)
||
defined(
SQLITE_OMIT_DEPRECATED
)
# define
sqlite3_profile
0
# define
sqlite3_trace
0
#endif
#ifdef
SQLITE_OMIT_GET_TABLE
# define
sqlite3_free_table
0
# define
sqlite3_get_table
0
#endif
#ifdef
SQLITE_OMIT_INCRBLOB
#define
sqlite3_bind_zeroblob
0
#define
sqlite3_blob_bytes
0
#define
sqlite3_blob_close
0
#define
sqlite3_blob_open
0
#define
sqlite3_blob_read
0
#define
sqlite3_blob_write
0
#define
sqlite3_blob_reopen
0
#endif
#if
defined(
SQLITE_OMIT_TRACE
)
# define
sqlite3_trace_v2
0
#endif
/*
** The following structure contains pointers to all SQLite API routines.
** A pointer to this structure is passed into extensions when they are
** loaded so that the extension can make calls back into the SQLite
** library.
**
** When adding new APIs, add them to the bottom of this structure
** in order to preserve backwards compatibility.
**
** Extensions that use newer APIs should first call the
** sqlite3_libversion_number() to make sure that the API they
** intend to use is supported by the library. Extensions should
** also check to make sure that the pointer to the function is
** not NULL before calling it.
*/
static
const
sqlite3_api_routines
sqlite3Apis
=
{
sqlite3_aggregate_context
,
#ifndef
SQLITE_OMIT_DEPRECATED
sqlite3_aggregate_count
,
#else
0
,
#endif
sqlite3_bind_blob
,
sqlite3_bind_double
,
sqlite3_bind_int
,
sqlite3_bind_int64
,
sqlite3_bind_null
,
sqlite3_bind_parameter_count
,
sqlite3_bind_parameter_index
,
sqlite3_bind_parameter_name
,
sqlite3_bind_text
,
sqlite3_bind_text16
,
sqlite3_bind_value
,
sqlite3_busy_handler
,
sqlite3_busy_timeout
,
sqlite3_changes
,
sqlite3_close
,
sqlite3_collation_needed
,
sqlite3_collation_needed16
,
sqlite3_column_blob
,
sqlite3_column_bytes
,
sqlite3_column_bytes16
,
sqlite3_column_count
,
sqlite3_column_database_name
,
sqlite3_column_database_name16
,
sqlite3_column_decltype
,
sqlite3_column_decltype16
,
sqlite3_column_double
,
sqlite3_column_int
,
sqlite3_column_int64
,
sqlite3_column_name
,
sqlite3_column_name16
,
sqlite3_column_origin_name
,
sqlite3_column_origin_name16
,
sqlite3_column_table_name
,
sqlite3_column_table_name16
,
sqlite3_column_text
,
sqlite3_column_text16
,
sqlite3_column_type
,
sqlite3_column_value
,
sqlite3_commit_hook
,
sqlite3_complete
,
sqlite3_complete16
,
sqlite3_create_collation
,
sqlite3_create_collation16
,
sqlite3_create_function
,
sqlite3_create_function16
,
sqlite3_create_module
,
sqlite3_data_count
,
sqlite3_db_handle
,
sqlite3_declare_vtab
,
sqlite3_enable_shared_cache
,
sqlite3_errcode
,
sqlite3_errmsg
,
sqlite3_errmsg16
,
sqlite3_exec
,
#ifndef
SQLITE_OMIT_DEPRECATED
sqlite3_expired
,
#else
0
,
#endif
sqlite3_finalize
,
sqlite3_free
,
sqlite3_free_table
,
sqlite3_get_autocommit
,
sqlite3_get_auxdata
,
sqlite3_get_table
,
0
,
/* Was sqlite3_global_recover(), but that function is deprecated */
sqlite3_interrupt
,
sqlite3_last_insert_rowid
,
sqlite3_libversion
,
sqlite3_libversion_number
,
sqlite3_malloc
,
sqlite3_mprintf
,
sqlite3_open
,
sqlite3_open16
,
sqlite3_prepare
,
sqlite3_prepare16
,
sqlite3_profile
,
sqlite3_progress_handler
,
sqlite3_realloc
,
sqlite3_reset
,
sqlite3_result_blob
,
sqlite3_result_double
,
sqlite3_result_error
,
sqlite3_result_error16
,
sqlite3_result_int
,
sqlite3_result_int64
,
sqlite3_result_null
,
sqlite3_result_text
,
sqlite3_result_text16
,
sqlite3_result_text16be
,
sqlite3_result_text16le
,
sqlite3_result_value
,
sqlite3_rollback_hook
,
sqlite3_set_authorizer
,
sqlite3_set_auxdata
,
sqlite3_snprintf
,
sqlite3_step
,
sqlite3_table_column_metadata
,
#ifndef
SQLITE_OMIT_DEPRECATED
sqlite3_thread_cleanup
,
#else
0
,
#endif
sqlite3_total_changes
,
sqlite3_trace
,
#ifndef
SQLITE_OMIT_DEPRECATED
sqlite3_transfer_bindings
,
#else
0
,
#endif
sqlite3_update_hook
,
sqlite3_user_data
,
sqlite3_value_blob
,
sqlite3_value_bytes
,
sqlite3_value_bytes16
,
sqlite3_value_double
,
sqlite3_value_int
,
sqlite3_value_int64
,
sqlite3_value_numeric_type
,
sqlite3_value_text
,
sqlite3_value_text16
,
sqlite3_value_text16be
,
sqlite3_value_text16le
,
sqlite3_value_type
,
sqlite3_vmprintf
,
/*
** The original API set ends here. All extensions can call any
** of the APIs above provided that the pointer is not NULL. But
** before calling APIs that follow, extension should check the
** sqlite3_libversion_number() to make sure they are dealing with
** a library that is new enough to support that API.
*************************************************************************
*/
sqlite3_overload_function
,
/*
** Added after 3.3.13
*/
sqlite3_prepare_v2
,
sqlite3_prepare16_v2
,
sqlite3_clear_bindings
,
/*
** Added for 3.4.1
*/
sqlite3_create_module_v2
,
/*
** Added for 3.5.0
*/
sqlite3_bind_zeroblob
,
sqlite3_blob_bytes
,
sqlite3_blob_close
,
sqlite3_blob_open
,
sqlite3_blob_read
,
sqlite3_blob_write
,
sqlite3_create_collation_v2
,
sqlite3_file_control
,
sqlite3_memory_highwater
,
sqlite3_memory_used
,
#ifdef
SQLITE_MUTEX_OMIT
0
,
0
,
0
,
0
,
0
,
#else
sqlite3_mutex_alloc
,
sqlite3_mutex_enter
,
sqlite3_mutex_free
,
sqlite3_mutex_leave
,
sqlite3_mutex_try
,
#endif
sqlite3_open_v2
,
sqlite3_release_memory
,
sqlite3_result_error_nomem
,
sqlite3_result_error_toobig
,
sqlite3_sleep
,
sqlite3_soft_heap_limit
,
sqlite3_vfs_find
,
sqlite3_vfs_register
,
sqlite3_vfs_unregister
,
/*
** Added for 3.5.8
*/
sqlite3_threadsafe
,
sqlite3_result_zeroblob
,
sqlite3_result_error_code
,
sqlite3_test_control
,
sqlite3_randomness
,
sqlite3_context_db_handle
,
/*
** Added for 3.6.0
*/
sqlite3_extended_result_codes
,
sqlite3_limit
,
sqlite3_next_stmt
,
sqlite3_sql
,
sqlite3_status
,
/*
** Added for 3.7.4
*/
sqlite3_backup_finish
,
sqlite3_backup_init
,
sqlite3_backup_pagecount
,
sqlite3_backup_remaining
,
sqlite3_backup_step
,
#ifndef
SQLITE_OMIT_COMPILEOPTION_DIAGS
sqlite3_compileoption_get
,
sqlite3_compileoption_used
,
#else
0
,
0
,
#endif
sqlite3_create_function_v2
,
sqlite3_db_config
,
sqlite3_db_mutex
,
sqlite3_db_status
,
sqlite3_extended_errcode
,
sqlite3_log
,
sqlite3_soft_heap_limit64
,
sqlite3_sourceid
,
sqlite3_stmt_status
,
sqlite3_strnicmp
,
#ifdef
SQLITE_ENABLE_UNLOCK_NOTIFY
sqlite3_unlock_notify
,
#else
0
,
#endif
#ifndef
SQLITE_OMIT_WAL
sqlite3_wal_autocheckpoint
,
sqlite3_wal_checkpoint
,
sqlite3_wal_hook
,
#else
0
,
0
,
0
,
#endif
sqlite3_blob_reopen
,
sqlite3_vtab_config
,
sqlite3_vtab_on_conflict
,
sqlite3_close_v2
,
sqlite3_db_filename
,
sqlite3_db_readonly
,
sqlite3_db_release_memory
,
sqlite3_errstr
,
sqlite3_stmt_busy
,
sqlite3_stmt_readonly
,
sqlite3_stricmp
,
sqlite3_uri_boolean
,
sqlite3_uri_int64
,
sqlite3_uri_parameter
,
sqlite3_vsnprintf
,
sqlite3_wal_checkpoint_v2
,
/* Version 3.8.7 and later */
sqlite3_auto_extension
,
sqlite3_bind_blob64
,
sqlite3_bind_text64
,
sqlite3_cancel_auto_extension
,
sqlite3_load_extension
,
sqlite3_malloc64
,
sqlite3_msize
,
sqlite3_realloc64
,
sqlite3_reset_auto_extension
,
sqlite3_result_blob64
,
sqlite3_result_text64
,
sqlite3_strglob
,
/* Version 3.8.11 and later */
(
sqlite3_value
*
(
*
)(
const
sqlite3_value
*
))
sqlite3_value_dup
,
sqlite3_value_free
,
sqlite3_result_zeroblob64
,
sqlite3_bind_zeroblob64
,
/* Version 3.9.0 and later */
sqlite3_value_subtype
,
sqlite3_result_subtype
,
/* Version 3.10.0 and later */
sqlite3_status64
,
sqlite3_strlike
,
sqlite3_db_cacheflush
,
/* Version 3.12.0 and later */
sqlite3_system_errno
,
/* Version 3.14.0 and later */
sqlite3_trace_v2
,
sqlite3_expanded_sql
,
/* Version 3.18.0 and later */
sqlite3_set_last_insert_rowid
,
/* Version 3.20.0 and later */
sqlite3_prepare_v3
,
sqlite3_prepare16_v3
,
sqlite3_bind_pointer
,
sqlite3_result_pointer
,
sqlite3_value_pointer
,
/* Version 3.22.0 and later */
sqlite3_vtab_nochange
,
sqlite3_value_nochange
,
sqlite3_vtab_collation
,
/* Version 3.24.0 and later */
sqlite3_keyword_count
,
sqlite3_keyword_name
,
sqlite3_keyword_check
,
sqlite3_str_new
,
sqlite3_str_finish
,
sqlite3_str_appendf
,
sqlite3_str_vappendf
,
sqlite3_str_append
,
sqlite3_str_appendall
,
sqlite3_str_appendchar
,
sqlite3_str_reset
,
sqlite3_str_errcode
,
sqlite3_str_length
,
sqlite3_str_value
,
/* Version 3.25.0 and later */
sqlite3_create_window_function
,
/* Version 3.26.0 and later */
#ifdef
SQLITE_ENABLE_NORMALIZE
sqlite3_normalized_sql
,
#else
0
,
#endif
/* Version 3.28.0 and later */
sqlite3_stmt_isexplain
,
sqlite3_value_frombind
,
/* Version 3.30.0 and later */
#ifndef
SQLITE_OMIT_VIRTUALTABLE
sqlite3_drop_modules
,
#else
0
,
#endif
/* Version 3.31.0 and later */
sqlite3_hard_heap_limit64
,
sqlite3_uri_key
,
sqlite3_filename_database
,
sqlite3_filename_journal
,
sqlite3_filename_wal
,
/* Version 3.32.0 and later */
sqlite3_create_filename
,
sqlite3_free_filename
,
sqlite3_database_file_object
,
/* Version 3.34.0 and later */
sqlite3_txn_state
,
/* Version 3.36.1 and later */
sqlite3_changes64
,
sqlite3_total_changes64
,
/* Version 3.37.0 and later */
sqlite3_autovacuum_pages
,
/* Version 3.38.0 and later */
sqlite3_error_offset
,
#ifndef
SQLITE_OMIT_VIRTUALTABLE
sqlite3_vtab_rhs_value
,
sqlite3_vtab_distinct
,
sqlite3_vtab_in
,
sqlite3_vtab_in_first
,
sqlite3_vtab_in_next
,
#else
0
,
0
,
0
,
0
,
0
,
#endif
/* Version 3.39.0 and later */
#ifndef
SQLITE_OMIT_DESERIALIZE
sqlite3_deserialize
,
sqlite3_serialize
,
#else
0
,
0
,
#endif
sqlite3_db_name
,
/* Version 3.40.0 and later */
sqlite3_value_encoding
,
/* Version 3.41.0 and later */
sqlite3_is_interrupted
,
/* Version 3.43.0 and later */
sqlite3_stmt_explain
,
/* Version 3.44.0 and later */
sqlite3_get_clientdata
,
sqlite3_set_clientdata
,
/* Version 3.50.0 and later */
sqlite3_setlk_timeout
,
/* Version 3.51.0 and later */
sqlite3_set_errmsg
,
sqlite3_db_status64
,
/* Version 3.52.0 and later */
sqlite3_str_truncate
,
sqlite3_str_free
,
#ifdef
SQLITE_ENABLE_CARRAY
sqlite3_carray_bind
,
sqlite3_carray_bind_v2
#else
0
,
0
#endif
};
/* True if x is the directory separator character
*/
#if
SQLITE_OS_WIN
# define
DirSep
(
X
) ((X)=='/'||(X)=='\\')
#else
# define
DirSep
(
X
) ((X)=='/')
#endif
/*
** Attempt to load an SQLite extension library contained in the file
** zFile. The entry point is zProc. zProc may be 0 in which case a
** default entry point name (sqlite3_extension_init) is used. Use
** of the default name is recommended.
**
** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
**
** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
** error message text. The calling function should free this memory
** by calling sqlite3DbFree(db, ).
*/
static
int
sqlite3LoadExtension
(
sqlite3
*
db
,
/* Load the extension into this database connection */
const
char
*
zFile
,
/* Name of the shared library containing extension */
const
char
*
zProc
,
/* Entry point. Use "sqlite3_extension_init" if 0 */
char
*
*
pzErrMsg
/* Put error message here if not 0 */
){
sqlite3_vfs
*
pVfs
=
db
->
pVfs
;
void
*
handle
;
sqlite3_loadext_entry
xInit
;
char
*
zErrmsg
=
0
;
const
char
*
zEntry
;
char
*
zAltEntry
=
0
;
void
*
*
aHandle
;
u64
nMsg
=
strlen
(
zFile
);
int
ii
;
int
rc
;
/* Shared library endings to try if zFile cannot be loaded as written */
static
const
char
*
azEndings
[]
=
{
#if
SQLITE_OS_WIN
"dll"
#elif
defined(
__APPLE__
)
"dylib"
#else
"so"
#endif
};
if
(
pzErrMsg
)
*
pzErrMsg
=
0
;
/* Ticket #1863. To avoid a creating security problems for older
** applications that relink against newer versions of SQLite, the
** ability to run load_extension is turned off by default. One
** must call either sqlite3_enable_load_extension(db) or
** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
** to turn on extension loading.
*/
if
( (
db
->
flags
&
SQLITE_LoadExtension
)
==
0
){
if
(
pzErrMsg
){
*
pzErrMsg
=
sqlite3_mprintf
(
"not authorized"
);
}
return
SQLITE_ERROR
;
}
zEntry
=
zProc
?
zProc
:
"sqlite3_extension_init"
;
/* tag-20210611-1. Some dlopen() implementations will segfault if given
** an oversize filename. Most filesystems have a pathname limit of 4K,
** so limit the extension filename length to about twice that.
** https://sqlite.org/forum/forumpost/08a0d6d9bf
**
** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
** See https://sqlite.org/forum/forumpost/24083b579d.
*/
if
(
nMsg
>
SQLITE_MAX_PATHLEN
) goto
extension_not_found
;
/* Do not allow sqlite3_load_extension() to link to a copy of the
** running application, by passing in an empty filename. */
if
(
nMsg
==
0
) goto
extension_not_found
;
handle
=
sqlite3OsDlOpen
(
pVfs
,
zFile
);
#if
SQLITE_OS_UNIX
||
SQLITE_OS_WIN
for
(
ii
=
0
;
ii
<
ArraySize
(
azEndings
)
&&
handle
==
0
;
ii
++
){
char
*
zAltFile
=
sqlite3_mprintf
(
"%s.%s"
,
zFile
,
azEndings
[
ii
]);
if
(
zAltFile
==
0
)
return
SQLITE_NOMEM_BKPT
;
if
(
nMsg
+
strlen
(
azEndings
[
ii
])
+
1
<=
SQLITE_MAX_PATHLEN
){
handle
=
sqlite3OsDlOpen
(
pVfs
,
zAltFile
);
}
sqlite3_free
(
zAltFile
);
}
#endif
if
(
handle
==
0
) goto
extension_not_found
;
xInit
=
(
sqlite3_loadext_entry
)
sqlite3OsDlSym
(
pVfs
,
handle
,
zEntry
);
/* If no entry point was specified and the default legacy
** entry point name "sqlite3_extension_init" was not found, then
** construct an entry point name "sqlite3_X_init" where the X is
** replaced by the lowercase value of every ASCII alphabetic
** character in the filename after the last "/" up to the first ".",
** and skipping the first three characters if they are "lib".
** Examples:
**
** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
**
** If that still finds no entry point, repeat a second time but this
** time include both alphabetic and numeric characters up to the first
** ".". Example:
**
** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example5_init
*/
if
(
xInit
==
0
&&
zProc
==
0
){
int
iFile
,
iEntry
,
c
;
int
ncFile
=
sqlite3Strlen30
(
zFile
);
int
cnt
=
0
;
zAltEntry
=
sqlite3_malloc64
(
ncFile
+
30
);
if
(
zAltEntry
==
0
){
sqlite3OsDlClose
(
pVfs
,
handle
);
return
SQLITE_NOMEM_BKPT
;
}
do
{
memcpy
(
zAltEntry
,
"sqlite3_"
,
8
);
for
(
iFile
=
ncFile
-
1
;
iFile
>=
0
&&
!
DirSep
(
zFile
[
iFile
]);
iFile
--
){}
iFile
++
;
if
(
sqlite3_strnicmp
(
zFile
+
iFile
,
"lib"
,
3
)
==
0
)
iFile
+=
3
;
for
(
iEntry
=
8
; (
c
=
zFile
[
iFile
])
!=
0
&&
c
!=
'.'
;
iFile
++
){
if
(
sqlite3Isalpha
(
c
)
||
(
cnt
&&
sqlite3Isdigit
(
c
)) ){
zAltEntry
[
iEntry
++
]
=
(
char
)
sqlite3UpperToLower
[(
unsigned
)
c
];
}
}
memcpy
(
zAltEntry
+
iEntry
,
"_init"
,
6
);
zEntry
=
zAltEntry
;
xInit
=
(
sqlite3_loadext_entry
)
sqlite3OsDlSym
(
pVfs
,
handle
,
zEntry
);
}
while
(
xInit
==
0
&&
(
++
cnt
)
<
2
);
}
if
(
xInit
==
0
){
if
(
pzErrMsg
){
nMsg
+=
strlen
(
zEntry
)
+
300
;
*
pzErrMsg
=
zErrmsg
=
sqlite3_malloc64
(
nMsg
);
if
(
zErrmsg
){
assert
(
nMsg
<
0x7fffffff
);
/* zErrmsg would be NULL if not so */
sqlite3_snprintf
((
int
)
nMsg
,
zErrmsg
,
"no entry point [%s] in shared library [%s]"
,
zEntry
,
zFile
);
sqlite3OsDlError
(
pVfs
,
nMsg
-
1
,
zErrmsg
);
}
}
sqlite3OsDlClose
(
pVfs
,
handle
);
sqlite3_free
(
zAltEntry
);
return
SQLITE_ERROR
;
}
sqlite3_free
(
zAltEntry
);
rc
=
xInit
(
db
,
&
zErrmsg
,
&
sqlite3Apis
);
if
(
rc
){
if
(
rc
==
SQLITE_OK_LOAD_PERMANENTLY
)
return
SQLITE_OK
;
if
(
pzErrMsg
){
*
pzErrMsg
=
sqlite3_mprintf
(
"error during initialization: %s"
,
zErrmsg
);
}
sqlite3_free
(
zErrmsg
);
sqlite3OsDlClose
(
pVfs
,
handle
);
return
SQLITE_ERROR
;
}
/* Append the new shared library handle to the db->aExtension array. */
aHandle
=
sqlite3DbMallocZero
(
db
,
sizeof
(
handle
)
*
(
db
->
nExtension
+
1
));
if
(
aHandle
==
0
){
return
SQLITE_NOMEM_BKPT
;
}
if
(
db
->
nExtension
>
0
){
memcpy
(
aHandle
,
db
->
aExtension
,
sizeof
(
handle
)
*
db
->
nExtension
);
}
sqlite3DbFree
(
db
,
db
->
aExtension
);
db
->
aExtension
=
aHandle
;
db
->
aExtension
[
db
->
nExtension
++
]
=
handle
;
return
SQLITE_OK
;
extension_not_found
:
if
(
pzErrMsg
){
nMsg
+=
300
;
*
pzErrMsg
=
zErrmsg
=
sqlite3_malloc64
(
nMsg
);
if
(
zErrmsg
){
assert
(
nMsg
<
0x7fffffff
);
/* zErrmsg would be NULL if not so */
sqlite3_snprintf
((
int
)
nMsg
,
zErrmsg
,
"unable to open shared library [%.*s]"
,
SQLITE_MAX_PATHLEN
,
zFile
);
sqlite3OsDlError
(
pVfs
,
nMsg
-
1
,
zErrmsg
);
}
}
return
SQLITE_ERROR
;
}
int
sqlite3_load_extension
(
sqlite3
*
db
,
/* Load the extension into this database connection */
const
char
*
zFile
,
/* Name of the shared library containing extension */
const
char
*
zProc
,
/* Entry point. Use "sqlite3_extension_init" if 0 */
char
*
*
pzErrMsg
/* Put error message here if not 0 */
){
int
rc
;
sqlite3_mutex_enter
(
db
->
mutex
);
rc
=
sqlite3LoadExtension
(
db
,
zFile
,
zProc
,
pzErrMsg
);
rc
=
sqlite3ApiExit
(
db
,
rc
);
sqlite3_mutex_leave
(
db
->
mutex
);
return
rc
;
}
/*
** Call this routine when the database connection is closing in order
** to clean up loaded extensions
*/
void
sqlite3CloseExtensions
(
sqlite3
*
db
){
int
i
;
assert
(
sqlite3_mutex_held
(
db
->
mutex
) );
for
(
i
=
0
;
i
<
db
->
nExtension
;
i
++
){
sqlite3OsDlClose
(
db
->
pVfs
,
db
->
aExtension
[
i
]);
}
sqlite3DbFree
(
db
,
db
->
aExtension
);
}
/*
** Enable or disable extension loading. Extension loading is disabled by
** default so as not to open security holes in older applications.
*/
int
sqlite3_enable_load_extension
(
sqlite3
*
db
,
int
onoff
){
#ifdef
SQLITE_ENABLE_API_ARMOR
if
( !
sqlite3SafetyCheckOk
(
db
) )
return
SQLITE_MISUSE_BKPT
;
#endif
sqlite3_mutex_enter
(
db
->
mutex
);
if
(
onoff
){
db
->
flags
|=
SQLITE_LoadExtension
|
SQLITE_LoadExtFunc
;
}
else
{
db
->
flags
&= ~(
u64
)(
SQLITE_LoadExtension
|
SQLITE_LoadExtFunc
);
}
sqlite3_mutex_leave
(
db
->
mutex
);
return
SQLITE_OK
;
}
#endif
/* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
/*
** The following object holds the list of automatically loaded
** extensions.
**
** This list is shared across threads. The SQLITE_MUTEX_STATIC_MAIN
** mutex must be held while accessing this list.
*/
typedef
struct
sqlite3AutoExtList
sqlite3AutoExtList
;
static
SQLITE_WSD
struct
sqlite3AutoExtList
{
u32
nExt
;
/* Number of entries in aExt[] */
void
(
*
*
aExt
)(
void
);
/* Pointers to the extension init functions */
}
sqlite3Autoext
=
{
0
,
0
};
/* The "wsdAutoext" macro will resolve to the autoextension
** state vector. If writable static data is unsupported on the target,
** we have to locate the state vector at run-time. In the more common
** case where writable static data is supported, wsdStat can refer directly
** to the "sqlite3Autoext" state vector declared above.
*/
#ifdef
SQLITE_OMIT_WSD
# define
wsdAutoextInit
\
sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
# define
wsdAutoext
x[0]
#else
# define
wsdAutoextInit
# define
wsdAutoext
sqlite3Autoext
#endif
/*
** Register a statically linked extension that is automatically
** loaded by every new database connection.
*/
int
sqlite3_auto_extension
(
void
(
*
xInit
)(
void
)
){
int
rc
=
SQLITE_OK
;
#ifdef
SQLITE_ENABLE_API_ARMOR
if
(
xInit
==
0
)
return
SQLITE_MISUSE_BKPT
;
#endif
#ifndef
SQLITE_OMIT_AUTOINIT
rc
=
sqlite3_initialize
();
if
(
rc
){
return
rc
;
}
else
#endif
{
u32
i
;
#if
SQLITE_THREADSAFE
sqlite3_mutex
*
mutex
=
sqlite3MutexAlloc
(
SQLITE_MUTEX_STATIC_MAIN
);
#endif
wsdAutoextInit
;
sqlite3_mutex_enter
(
mutex
);
for
(
i
=
0
;
i
<
wsdAutoext
.
nExt
;
i
++
){
if
(
wsdAutoext
.
aExt
[
i
]
==
xInit
)
break
;
}
if
(
i
==
wsdAutoext
.
nExt
){
u64
nByte
=
(
wsdAutoext
.
nExt
+
1
)
*
sizeof
(
wsdAutoext
.
aExt
[
0
]);
void
(
*
*
aNew
)(
void
);
aNew
=
sqlite3_realloc64
(
wsdAutoext
.
aExt
,
nByte
);
if
(
aNew
==
0
){
rc
=
SQLITE_NOMEM_BKPT
;
}
else
{
wsdAutoext
.
aExt
=
aNew
;
wsdAutoext
.
aExt
[
wsdAutoext
.
nExt
]
=
xInit
;
wsdAutoext
.
nExt
++
;
}
}
sqlite3_mutex_leave
(
mutex
);
assert
( (
rc
&
0xff
)
==
rc
);
return
rc
;
}
}
/*
** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
** set of routines that is invoked for each new database connection, if it
** is currently on the list. If xInit is not on the list, then this
** routine is a no-op.
**
** Return 1 if xInit was found on the list and removed. Return 0 if xInit
** was not on the list.
*/
int
sqlite3_cancel_auto_extension
(
void
(
*
xInit
)(
void
)
){
#if
SQLITE_THREADSAFE
sqlite3_mutex
*
mutex
=
sqlite3MutexAlloc
(
SQLITE_MUTEX_STATIC_MAIN
);
#endif
int
i
;
int
n
=
0
;
wsdAutoextInit
;
#ifdef
SQLITE_ENABLE_API_ARMOR
if
(
xInit
==
0
)
return
0
;
#endif
sqlite3_mutex_enter
(
mutex
);
for
(
i
=
(
int
)
wsdAutoext
.
nExt
-
1
;
i
>=
0
;
i
--
){
if
(
wsdAutoext
.
aExt
[
i
]
==
xInit
){
wsdAutoext
.
nExt
--
;
wsdAutoext
.
aExt
[
i
]
=
wsdAutoext
.
aExt
[
wsdAutoext
.
nExt
];
n
++
;
break
;
}
}
sqlite3_mutex_leave
(
mutex
);
return
n
;
}
/*
** Reset the automatic extension loading mechanism.
*/
void
sqlite3_reset_auto_extension
(
void
){
#ifndef
SQLITE_OMIT_AUTOINIT
if
(
sqlite3_initialize
()
==
SQLITE_OK
)
#endif
{
#if
SQLITE_THREADSAFE
sqlite3_mutex
*
mutex
=
sqlite3MutexAlloc
(
SQLITE_MUTEX_STATIC_MAIN
);
#endif
wsdAutoextInit
;
sqlite3_mutex_enter
(
mutex
);
sqlite3_free
(
wsdAutoext
.
aExt
);
wsdAutoext
.
aExt
=
0
;
wsdAutoext
.
nExt
=
0
;
sqlite3_mutex_leave
(
mutex
);
}
}
/*
** Load all automatic extensions.
**
** If anything goes wrong, set an error in the database connection.
*/
void
sqlite3AutoLoadExtensions
(
sqlite3
*
db
){
u32
i
;
int
go
=
1
;
int
rc
;
sqlite3_loadext_entry
xInit
;
wsdAutoextInit
;
if
(
wsdAutoext
.
nExt
==
0
){
/* Common case: early out without every having to acquire a mutex */
return
;
}
for
(
i
=
0
;
go
;
i
++
){
char
*
zErrmsg
;
#if
SQLITE_THREADSAFE
sqlite3_mutex
*
mutex
=
sqlite3MutexAlloc
(
SQLITE_MUTEX_STATIC_MAIN
);
#endif
#ifdef
SQLITE_OMIT_LOAD_EXTENSION
const
sqlite3_api_routines
*
pThunk
=
0
;
#else
const
sqlite3_api_routines
*
pThunk
=
&
sqlite3Apis
;
#endif
sqlite3_mutex_enter
(
mutex
);
if
(
i
>=
wsdAutoext
.
nExt
){
xInit
=
0
;
go
=
0
;
}
else
{
xInit
=
(
sqlite3_loadext_entry
)
wsdAutoext
.
aExt
[
i
];
}
sqlite3_mutex_leave
(
mutex
);
zErrmsg
=
0
;
if
(
xInit
&&
(
rc
=
xInit
(
db
,
&
zErrmsg
,
pThunk
))
!=
0
){
sqlite3ErrorWithMsg
(
db
,
rc
,
"automatic extension loading failed: %s"
,
zErrmsg
);
go
=
0
;
}
sqlite3_free
(
zErrmsg
);
}
}
Back
|
FazBrowse Home
|
New Git URL