webengine/webkitutils/SqliteSymbian/loadext.c
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 ** 2006 June 7
       
     3 **
       
     4 ** The author disclaims copyright to this source code.  In place of
       
     5 ** a legal notice, here is a blessing:
       
     6 **
       
     7 **    May you do good and not evil.
       
     8 **    May you find forgiveness for yourself and forgive others.
       
     9 **    May you share freely, never taking more than you give.
       
    10 **
       
    11 *************************************************************************
       
    12 ** This file contains code used to dynamically load extensions into
       
    13 ** the SQLite library.
       
    14 */
       
    15 #ifndef SQLITE_OMIT_LOAD_EXTENSION
       
    16 
       
    17 #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
       
    18 #include "sqlite3ext.h"
       
    19 #include "sqliteInt.h"
       
    20 #include "os.h"
       
    21 #include <string.h>
       
    22 #include <ctype.h>
       
    23 
       
    24 /*
       
    25 ** Some API routines are omitted when various features are
       
    26 ** excluded from a build of SQLite.  Substitute a NULL pointer
       
    27 ** for any missing APIs.
       
    28 */
       
    29 #ifndef SQLITE_ENABLE_COLUMN_METADATA
       
    30 # define sqlite3_column_database_name   0
       
    31 # define sqlite3_column_database_name16 0
       
    32 # define sqlite3_column_table_name      0
       
    33 # define sqlite3_column_table_name16    0
       
    34 # define sqlite3_column_origin_name     0
       
    35 # define sqlite3_column_origin_name16   0
       
    36 # define sqlite3_table_column_metadata  0
       
    37 #endif
       
    38 
       
    39 #ifdef SQLITE_OMIT_AUTHORIZATION
       
    40 # define sqlite3_set_authorizer     0
       
    41 #endif
       
    42 
       
    43 #ifdef SQLITE_OMIT_UTF16
       
    44 # define sqlite3_bind_text16        0
       
    45 # define sqlite3_collation_needed16 0
       
    46 # define sqlite3_column_decltype16  0
       
    47 # define sqlite3_column_name16      0
       
    48 # define sqlite3_column_text16      0
       
    49 # define sqlite3_complete16         0
       
    50 # define sqlite3_create_collation16 0
       
    51 # define sqlite3_create_function16  0
       
    52 # define sqlite3_errmsg16           0
       
    53 # define sqlite3_open16             0
       
    54 # define sqlite3_prepare16          0
       
    55 # define sqlite3_result_error16     0
       
    56 # define sqlite3_result_text16      0
       
    57 # define sqlite3_result_text16be    0
       
    58 # define sqlite3_result_text16le    0
       
    59 # define sqlite3_value_text16       0
       
    60 # define sqlite3_value_text16be     0
       
    61 # define sqlite3_value_text16le     0
       
    62 #endif
       
    63 
       
    64 #ifdef SQLITE_OMIT_COMPLETE
       
    65 # define sqlite3_complete 0
       
    66 # define sqlite3_complete16 0
       
    67 #endif
       
    68 
       
    69 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
       
    70 # define sqlite3_progress_handler 0
       
    71 #endif
       
    72 
       
    73 #ifdef SQLITE_OMIT_VIRTUALTABLE
       
    74 # define sqlite3_create_module 0
       
    75 # define sqlite3_declare_vtab 0
       
    76 #endif
       
    77 
       
    78 /*
       
    79 ** The following structure contains pointers to all SQLite API routines.
       
    80 ** A pointer to this structure is passed into extensions when they are
       
    81 ** loaded so that the extension can make calls back into the SQLite
       
    82 ** library.
       
    83 **
       
    84 ** When adding new APIs, add them to the bottom of this structure
       
    85 ** in order to preserve backwards compatibility.
       
    86 **
       
    87 ** Extensions that use newer APIs should first call the
       
    88 ** sqlite3_libversion_number() to make sure that the API they
       
    89 ** intend to use is supported by the library.  Extensions should
       
    90 ** also check to make sure that the pointer to the function is
       
    91 ** not NULL before calling it.
       
    92 */
       
    93 const sqlite3_api_routines sqlite3_apis = {
       
    94   sqlite3_aggregate_context,
       
    95   sqlite3_aggregate_count,
       
    96   sqlite3_bind_blob,
       
    97   sqlite3_bind_double,
       
    98   sqlite3_bind_int,
       
    99   sqlite3_bind_int64,
       
   100   sqlite3_bind_null,
       
   101   sqlite3_bind_parameter_count,
       
   102   sqlite3_bind_parameter_index,
       
   103   sqlite3_bind_parameter_name,
       
   104   sqlite3_bind_text,
       
   105   sqlite3_bind_text16,
       
   106   sqlite3_bind_value,
       
   107   sqlite3_busy_handler,
       
   108   sqlite3_busy_timeout,
       
   109   sqlite3_changes,
       
   110   sqlite3_close,
       
   111   sqlite3_collation_needed,
       
   112   sqlite3_collation_needed16,
       
   113   sqlite3_column_blob,
       
   114   sqlite3_column_bytes,
       
   115   sqlite3_column_bytes16,
       
   116   sqlite3_column_count,
       
   117   sqlite3_column_database_name,
       
   118   sqlite3_column_database_name16,
       
   119   sqlite3_column_decltype,
       
   120   sqlite3_column_decltype16,
       
   121   sqlite3_column_double,
       
   122   sqlite3_column_int,
       
   123   sqlite3_column_int64,
       
   124   sqlite3_column_name,
       
   125   sqlite3_column_name16,
       
   126   sqlite3_column_origin_name,
       
   127   sqlite3_column_origin_name16,
       
   128   sqlite3_column_table_name,
       
   129   sqlite3_column_table_name16,
       
   130   sqlite3_column_text,
       
   131   sqlite3_column_text16,
       
   132   sqlite3_column_type,
       
   133   sqlite3_column_value,
       
   134   sqlite3_commit_hook,
       
   135   sqlite3_complete,
       
   136   sqlite3_complete16,
       
   137   sqlite3_create_collation,
       
   138   sqlite3_create_collation16,
       
   139   sqlite3_create_function,
       
   140   sqlite3_create_function16,
       
   141   sqlite3_create_module,
       
   142   sqlite3_data_count,
       
   143   sqlite3_db_handle,
       
   144   sqlite3_declare_vtab,
       
   145   sqlite3_enable_shared_cache,
       
   146   sqlite3_errcode,
       
   147   sqlite3_errmsg,
       
   148   sqlite3_errmsg16,
       
   149   sqlite3_exec,
       
   150   sqlite3_expired,
       
   151   sqlite3_finalize,
       
   152   sqlite3_free,
       
   153   sqlite3_free_table,
       
   154   sqlite3_get_autocommit,
       
   155   sqlite3_get_auxdata,
       
   156   sqlite3_get_table,
       
   157   sqlite3_global_recover,
       
   158   sqlite3_interrupt,
       
   159   sqlite3_last_insert_rowid,
       
   160   sqlite3_libversion,
       
   161   sqlite3_libversion_number,
       
   162   sqlite3_malloc,
       
   163   sqlite3_mprintf,
       
   164   sqlite3_open,
       
   165   sqlite3_open16,
       
   166   sqlite3_prepare,
       
   167   sqlite3_prepare16,
       
   168   sqlite3_profile,
       
   169   sqlite3_progress_handler,
       
   170   sqlite3_realloc,
       
   171   sqlite3_reset,
       
   172   sqlite3_result_blob,
       
   173   sqlite3_result_double,
       
   174   sqlite3_result_error,
       
   175   sqlite3_result_error16,
       
   176   sqlite3_result_int,
       
   177   sqlite3_result_int64,
       
   178   sqlite3_result_null,
       
   179   sqlite3_result_text,
       
   180   sqlite3_result_text16,
       
   181   sqlite3_result_text16be,
       
   182   sqlite3_result_text16le,
       
   183   sqlite3_result_value,
       
   184   sqlite3_rollback_hook,
       
   185   sqlite3_set_authorizer,
       
   186   sqlite3_set_auxdata,
       
   187   sqlite3_snprintf,
       
   188   sqlite3_step,
       
   189   sqlite3_table_column_metadata,
       
   190   sqlite3_thread_cleanup,
       
   191   sqlite3_total_changes,
       
   192   sqlite3_trace,
       
   193   sqlite3_transfer_bindings,
       
   194   sqlite3_update_hook,
       
   195   sqlite3_user_data,
       
   196   sqlite3_value_blob,
       
   197   sqlite3_value_bytes,
       
   198   sqlite3_value_bytes16,
       
   199   sqlite3_value_double,
       
   200   sqlite3_value_int,
       
   201   sqlite3_value_int64,
       
   202   sqlite3_value_numeric_type,
       
   203   sqlite3_value_text,
       
   204   sqlite3_value_text16,
       
   205   sqlite3_value_text16be,
       
   206   sqlite3_value_text16le,
       
   207   sqlite3_value_type,
       
   208   sqlite3_vmprintf,
       
   209   /*
       
   210   ** The original API set ends here.  All extensions can call any
       
   211   ** of the APIs above provided that the pointer is not NULL.  But
       
   212   ** before calling APIs that follow, extension should check the
       
   213   ** sqlite3_libversion_number() to make sure they are dealing with
       
   214   ** a library that is new enough to support that API.
       
   215   *************************************************************************
       
   216   */
       
   217 };
       
   218 
       
   219 /*
       
   220 ** The windows implementation of shared-library loaders
       
   221 */
       
   222 #if defined(_WIN32) || defined(WIN32) || defined(__MINGW32__) || defined(__BORLANDC__)
       
   223 # include <windows.h>
       
   224 # define SQLITE_LIBRARY_TYPE     HANDLE
       
   225 # define SQLITE_OPEN_LIBRARY(A)  LoadLibrary(A)
       
   226 # define SQLITE_FIND_SYMBOL(A,B) GetProcAddress(A,B)
       
   227 # define SQLITE_CLOSE_LIBRARY(A) FreeLibrary(A)
       
   228 #endif /* windows */
       
   229 
       
   230 /*
       
   231 ** The unix implementation of shared-library loaders
       
   232 */
       
   233 #if defined(HAVE_DLOPEN) && !defined(SQLITE_LIBRARY_TYPE)
       
   234 # include <dlfcn.h>
       
   235 # define SQLITE_LIBRARY_TYPE     void*
       
   236 # define SQLITE_OPEN_LIBRARY(A)  dlopen(A, RTLD_NOW | RTLD_GLOBAL)
       
   237 # define SQLITE_FIND_SYMBOL(A,B) dlsym(A,B)
       
   238 # define SQLITE_CLOSE_LIBRARY(A) dlclose(A)
       
   239 #endif
       
   240 
       
   241 /*
       
   242 ** Attempt to load an SQLite extension library contained in the file
       
   243 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
       
   244 ** default entry point name (sqlite3_extension_init) is used.  Use
       
   245 ** of the default name is recommended.
       
   246 **
       
   247 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
       
   248 **
       
   249 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
       
   250 ** error message text.  The calling function should free this memory
       
   251 ** by calling sqlite3_free().
       
   252 */
       
   253 int sqlite3_load_extension(
       
   254   sqlite3 *db,          /* Load the extension into this database connection */
       
   255   const char *zFile,    /* Name of the shared library containing extension */
       
   256   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
       
   257   char **pzErrMsg       /* Put error message here if not 0 */
       
   258 ){
       
   259 #ifdef SQLITE_LIBRARY_TYPE
       
   260   SQLITE_LIBRARY_TYPE handle;
       
   261   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
       
   262   char *zErrmsg = 0;
       
   263   SQLITE_LIBRARY_TYPE *aHandle;
       
   264 
       
   265   /* Ticket #1863.  To avoid a creating security problems for older
       
   266   ** applications that relink against newer versions of SQLite, the
       
   267   ** ability to run load_extension is turned off by default.  One
       
   268   ** must call sqlite3_enable_load_extension() to turn on extension
       
   269   ** loading.  Otherwise you get the following error.
       
   270   */
       
   271   if( (db->flags & SQLITE_LoadExtension)==0 ){
       
   272     if( pzErrMsg ){
       
   273       *pzErrMsg = sqlite3_mprintf("not authorized");
       
   274     }
       
   275     return SQLITE_ERROR;
       
   276   }
       
   277 
       
   278   if( zProc==0 ){
       
   279     zProc = "sqlite3_extension_init";
       
   280   }
       
   281 
       
   282   handle = SQLITE_OPEN_LIBRARY(zFile);
       
   283   if( handle==0 ){
       
   284     if( pzErrMsg ){
       
   285       *pzErrMsg = sqlite3_mprintf("unable to open shared library [%s]", zFile);
       
   286     }
       
   287     return SQLITE_ERROR;
       
   288   }
       
   289   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
       
   290                    SQLITE_FIND_SYMBOL(handle, zProc);
       
   291   if( xInit==0 ){
       
   292     if( pzErrMsg ){
       
   293        *pzErrMsg = sqlite3_mprintf("no entry point [%s] in shared library [%s]",
       
   294                                    zProc, zFile);
       
   295     }
       
   296     SQLITE_CLOSE_LIBRARY(handle);
       
   297     return SQLITE_ERROR;
       
   298   }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){
       
   299     if( pzErrMsg ){
       
   300       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
       
   301     }
       
   302     sqlite3_free(zErrmsg);
       
   303     SQLITE_CLOSE_LIBRARY(handle);
       
   304     return SQLITE_ERROR;
       
   305   }
       
   306 
       
   307   /* Append the new shared library handle to the db->aExtension array. */
       
   308   db->nExtension++;
       
   309   aHandle = sqliteMalloc(sizeof(handle)*db->nExtension);
       
   310   if( aHandle==0 ){
       
   311     return SQLITE_NOMEM;
       
   312   }
       
   313   if( db->nExtension>0 ){
       
   314     memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
       
   315   }
       
   316   sqliteFree(db->aExtension);
       
   317   db->aExtension = aHandle;
       
   318 
       
   319   ((SQLITE_LIBRARY_TYPE*)db->aExtension)[db->nExtension-1] = handle;
       
   320   return SQLITE_OK;
       
   321 #else
       
   322   if( pzErrMsg ){
       
   323     *pzErrMsg = sqlite3_mprintf("extension loading is disabled");
       
   324   }
       
   325   return SQLITE_ERROR;
       
   326 #endif
       
   327 }
       
   328 
       
   329 /*
       
   330 ** Call this routine when the database connection is closing in order
       
   331 ** to clean up loaded extensions
       
   332 */
       
   333 void sqlite3CloseExtensions(sqlite3 *db){
       
   334 #ifdef SQLITE_LIBRARY_TYPE
       
   335   int i;
       
   336   for(i=0; i<db->nExtension; i++){
       
   337     SQLITE_CLOSE_LIBRARY(((SQLITE_LIBRARY_TYPE*)db->aExtension)[i]);
       
   338   }
       
   339   sqliteFree(db->aExtension);
       
   340 #endif
       
   341 }
       
   342 
       
   343 /*
       
   344 ** Enable or disable extension loading.  Extension loading is disabled by
       
   345 ** default so as not to open security holes in older applications.
       
   346 */
       
   347 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
       
   348   if( onoff ){
       
   349     db->flags |= SQLITE_LoadExtension;
       
   350   }else{
       
   351     db->flags &= ~SQLITE_LoadExtension;
       
   352   }
       
   353   return SQLITE_OK;
       
   354 }
       
   355 
       
   356 /*
       
   357 ** A list of automatically loaded extensions.
       
   358 **
       
   359 ** This list is shared across threads, so be sure to hold the
       
   360 ** mutex while accessing or changing it.
       
   361 */
       
   362 static int nAutoExtension = 0;
       
   363 static void **aAutoExtension = 0;
       
   364 
       
   365 
       
   366 /*
       
   367 ** Register a statically linked extension that is automatically
       
   368 ** loaded by every new database connection.
       
   369 */
       
   370 int sqlite3_auto_extension(void *xInit){
       
   371   int i;
       
   372   int rc = SQLITE_OK;
       
   373   sqlite3OsEnterMutex();
       
   374   for(i=0; i<nAutoExtension; i++){
       
   375     if( aAutoExtension[i]==xInit ) break;
       
   376   }
       
   377   if( i==nAutoExtension ){
       
   378     nAutoExtension++;
       
   379     aAutoExtension = sqlite3Realloc( aAutoExtension,
       
   380                                      nAutoExtension*sizeof(aAutoExtension[0]) );
       
   381     if( aAutoExtension==0 ){
       
   382       nAutoExtension = 0;
       
   383       rc = SQLITE_NOMEM;
       
   384     }else{
       
   385       aAutoExtension[nAutoExtension-1] = xInit;
       
   386     }
       
   387   }
       
   388   sqlite3OsLeaveMutex();  
       
   389   return rc;
       
   390 }
       
   391 
       
   392 /*
       
   393 ** Reset the automatic extension loading mechanism.
       
   394 */
       
   395 void sqlite3_reset_auto_extension(void){
       
   396   sqlite3OsEnterMutex();
       
   397   sqliteFree(aAutoExtension);
       
   398   aAutoExtension = 0;
       
   399   nAutoExtension = 0;
       
   400   sqlite3OsLeaveMutex();
       
   401 }
       
   402 
       
   403 /*
       
   404 ** Load all automatic extensions.
       
   405 */
       
   406 int sqlite3AutoLoadExtensions(sqlite3 *db){
       
   407   int i;
       
   408   int go = 1;
       
   409   int rc = SQLITE_OK;
       
   410   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
       
   411 
       
   412   if( nAutoExtension==0 ){
       
   413     /* Common case: early out without every having to acquire a mutex */
       
   414     return SQLITE_OK;
       
   415   }
       
   416   for(i=0; go; i++){
       
   417     char *zErrmsg = 0;
       
   418     sqlite3OsEnterMutex();
       
   419     if( i>=nAutoExtension ){
       
   420       xInit = 0;
       
   421       go = 0;
       
   422     }else{
       
   423       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
       
   424               aAutoExtension[i];
       
   425     }
       
   426     sqlite3OsLeaveMutex();
       
   427     if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){
       
   428       sqlite3Error(db, SQLITE_ERROR,
       
   429             "automatic extension loading failed: %s", zErrmsg);
       
   430       go = 0;
       
   431       rc = SQLITE_ERROR;
       
   432     }
       
   433   }
       
   434   return rc;
       
   435 }
       
   436 
       
   437 #endif /* SQLITE_OMIT_LOAD_EXTENSION */