コード例 #1
0
ファイル: func.c プロジェクト: blingstorm/sqlcipher
/*
** Compute the soundex encoding of a word.
**
** IMP: R-59782-00072 The soundex(X) function returns a string that is the
** soundex encoding of the string X. 
*/
static void soundexFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  char zResult[8];
  const u8 *zIn;
  int i, j;
  static const unsigned char iCode[] = {
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
    0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
    1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
  };
  assert( argc==1 );
  zIn = (u8*)sqlite3_value_text(argv[0]);
  if( zIn==0 ) zIn = (u8*)"";
  for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
  if( zIn[i] ){
    u8 prevcode = iCode[zIn[i]&0x7f];
    zResult[0] = sqlite3Toupper(zIn[i]);
    for(j=1; j<4 && zIn[i]; i++){
      int code = iCode[zIn[i]&0x7f];
      if( code>0 ){
        if( code!=prevcode ){
          prevcode = code;
          zResult[j++] = code + '0';
        }
      }else{
        prevcode = 0;
      }
    }
    while( j<4 ){
      zResult[j++] = '0';
    }
    zResult[j] = 0;
    sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
  }else{
    /* IMP: R-64894-50321 The string "?000" is returned if the argument
    ** is NULL or contains no ASCII alphabetic characters. */
    sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
  }
}
コード例 #2
0
/*
 ** 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;
    int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
    char *zErrmsg = 0;
    const char *zEntry;
    char *zAltEntry = 0;
    void **aHandle;
    int nMsg = 300 + sqlite3Strlen30(zFile);
    int ii;
    
    /* 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 sqlite3_enable_load_extension() to turn on extension
     ** loading.  Otherwise you get the following error.
     */
    if( (db->flags & SQLITE_LoadExtension)==0 ){
        if( pzErrMsg ){
            *pzErrMsg = sqlite3_mprintf("not authorized");
        }
        return SQLITE_ERROR;
    }
    
    zEntry = zProc ? zProc : "sqlite3_extension_init";
    
    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;
        handle = sqlite3OsDlOpen(pVfs, zAltFile);
        sqlite3_free(zAltFile);
    }
#endif
    if( handle==0 ){
        if( pzErrMsg ){
            *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
            if( zErrmsg ){
                sqlite3_snprintf(nMsg, zErrmsg,
                                 "unable to open shared library [%s]", zFile);
                sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
            }
        }
        return SQLITE_ERROR;
    }
    xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
    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 "/" upto the first ".",
     ** and eliding 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( xInit==0 && zProc==0 ){
        int iFile, iEntry, c;
        int ncFile = sqlite3Strlen30(zFile);
        zAltEntry = sqlite3_malloc(ncFile+30);
        if( zAltEntry==0 ){
            sqlite3OsDlClose(pVfs, handle);
            return SQLITE_NOMEM;
        }
        memcpy(zAltEntry, "sqlite3_", 8);
        for(iFile=ncFile-1; iFile>=0 && 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) ){
                zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
            }
        }
        memcpy(zAltEntry+iEntry, "_init", 6);
        zEntry = zAltEntry;
        xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
        sqlite3OsDlSym(pVfs, handle, zEntry);
    }
    if( xInit==0 ){
        if( pzErrMsg ){
            nMsg += sqlite3Strlen30(zEntry);
            *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
            if( zErrmsg ){
                sqlite3_snprintf(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);
    if( xInit(db, &zErrmsg, &sqlite3Apis) ){
        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;
    }
    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;
}