/* ** EXPERIMENTAL - This is not an official function. The interface may ** change. This function may disappear. Do not write code that depends ** on this function. ** ** Implementation of the QUOTE() function. This function takes a single ** argument. If the argument is numeric, the return value is the same as ** the argument. If the argument is NULL, the return value is the string ** "NULL". Otherwise, the argument is enclosed in single quotes with ** single-quote escapes. */ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ assert( argc==1 ); UNUSED_PARAMETER(argc); switch( sqlite3_value_type(argv[0]) ){ case SQLITE_INTEGER: case SQLITE_FLOAT: { sqlite3_result_value(context, argv[0]); break; } case SQLITE_BLOB: { char *zText = 0; char const *zBlob = sqlite3_value_blob(argv[0]); int nBlob = sqlite3_value_bytes(argv[0]); assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); if( zText ){ int i; for(i=0; i<nBlob; i++){ zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; } zText[(nBlob*2)+2] = '\''; zText[(nBlob*2)+3] = '\0'; zText[0] = 'X'; zText[1] = '\''; sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); sqlite3_free(zText); } break; } case SQLITE_TEXT: { int i,j; u64 n; const unsigned char *zArg = sqlite3_value_text(argv[0]); char *z; if( zArg==0 ) return; for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } z = contextMalloc(context, ((i64)i)+((i64)n)+3); if( z ){ z[0] = '\''; for(i=0, j=1; zArg[i]; i++){ z[j++] = zArg[i]; if( zArg[i]=='\'' ){ z[j++] = '\''; } } z[j++] = '\''; z[j] = 0; sqlite3_result_text(context, z, j, sqlite3_free); } break; } default: { assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); break; } } }
/* ** The hex() function. Interpret the argument as a blob. Return ** a hexadecimal rendering as text. */ static void hexFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ int i, n; const unsigned char *pBlob; char *zHex, *z; assert( argc==1 ); pBlob = sqlite3_value_blob(argv[0]); n = sqlite3_value_bytes(argv[0]); if( n*2+1>SQLITE_MAX_LENGTH ){ sqlite3_result_error_toobig(context); return; } assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ z = zHex = sqlite3_malloc(n*2 + 1); if( zHex==0 ) return; for(i=0; i<n; i++, pBlob++){ unsigned char c = *pBlob; *(z++) = hexdigits[(c>>4)&0xf]; *(z++) = hexdigits[c&0xf]; } *z = 0; sqlite3_result_text(context, zHex, n*2, sqlite3_free); }
static void _relation_compute(sqlite3_context *context,int argc,sqlite3_value **argv,RelationCompute Func) { if(argc == 2 && sqlite3_value_type(argv[0]) == SQLITE_BLOB && sqlite3_value_type(argv[1]) == SQLITE_BLOB) { GEOSGeometry* geometry1; GEOSGeometry* geometry2; GEOSGeometry* geo_result; unsigned char* wkb; size_t size; const void* data1 = sqlite3_value_blob(argv[0]); size_t data_size1 = sqlite3_value_bytes(argv[0]); const void* data2 = sqlite3_value_blob(argv[1]); size_t data_size2 = sqlite3_value_bytes(argv[1]); _init_geos(); geometry1 = _geo_from_wkb((const unsigned char*)data1,data_size1); geometry2 = _geo_from_wkb((const unsigned char*)data2,data_size2); if(geometry1 != 0 && geometry2 != 0) { geo_result = Func(geometry1,geometry2); if(geo_result != 0) { wkb = GEOSGeomToWKB_buf(geo_result,&size); sqlite3_result_blob(context,wkb,size,SQLITE_TRANSIENT); GEOSGeom_destroy(geo_result); GEOSFree(wkb); } } if(geometry1!=0)GEOSGeom_destroy(geometry1); if(geometry2!=0)GEOSGeom_destroy(geometry2); finishGEOS(); } }
static void function_rank (sqlite3_context *context, int argc, sqlite3_value *argv[]) { guint *matchinfo, *weights; gdouble rank = 0; gint i, n_columns; if (argc != 2) { sqlite3_result_error(context, "wrong number of arguments to function rank()", -1); return; } matchinfo = (unsigned int *) sqlite3_value_blob (argv[0]); weights = (unsigned int *) sqlite3_value_blob (argv[1]); n_columns = matchinfo[0]; for (i = 0; i < n_columns; i++) { if (matchinfo[i + 1] != 0) { rank += (gdouble) weights[i]; } } sqlite3_result_double(context, rank); }
static void _relation_judge(sqlite3_context *context,int argc,sqlite3_value **argv,RelationJudge Func) { if(argc == 2 && sqlite3_value_type(argv[0]) == SQLITE_BLOB && sqlite3_value_type(argv[1]) == SQLITE_BLOB) { GEOSGeometry* geometry1; GEOSGeometry* geometry2; char result; const void* data1 = sqlite3_value_blob(argv[0]); size_t data_size1 = sqlite3_value_bytes(argv[0]); const void* data2 = sqlite3_value_blob(argv[1]); size_t data_size2 = sqlite3_value_bytes(argv[1]); _init_geos(); geometry1 = _geo_from_wkb((const unsigned char*)data1,data_size1); geometry2 = _geo_from_wkb((const unsigned char*)data2,data_size2); if(geometry1 != 0 && geometry2 != 0) { result = Func(geometry1,geometry2); sqlite3_result_int(context,result); } if(geometry1!=0)GEOSGeom_destroy(geometry1); if(geometry2!=0)GEOSGeom_destroy(geometry2); finishGEOS(); } }
// sql function. takes ESSID and PASSWD, gives PMK void sql_calcpmk(sqlite3_context* context, int argc, sqlite3_value** values) { unsigned char pmk[40]; char* passwd = (char*)sqlite3_value_blob(values[1]); char* essid = (char*)sqlite3_value_blob(values[0]); if (argc < 2 || passwd == 0 || essid == 0) { sqlite3_result_error(context, "SQL function PMK() called with invalid arguments.\n", -1); return; } calc_pmk(passwd,essid,pmk); sqlite3_result_blob(context,pmk,32,SQLITE_TRANSIENT); }
void ActionLog::apply_action_xFun(sqlite3_context* context, int argc, sqlite3_value** argv) { ActionLog* the = reinterpret_cast<ActionLog*>(sqlite3_user_data(context)); if (argc != 11) { sqlite3_result_error(context, "``apply_action'' expects 10 arguments", -1); return; } Buffer device_name(sqlite3_value_blob(argv[0]), sqlite3_value_bytes(argv[0])); sqlite3_int64 seq_no = sqlite3_value_int64(argv[1]); int action = sqlite3_value_int(argv[2]); std::string filename = reinterpret_cast<const char*>(sqlite3_value_text(argv[3])); sqlite3_int64 version = sqlite3_value_int64(argv[4]); _LOG_TRACE("apply_function called with " << argc); _LOG_TRACE("device_name: " << Name(Block(reinterpret_cast<const char*>(device_name.buf()), device_name.size())) << ", action: " << action << ", file: " << filename); if (action == 0) // update { Buffer hash(sqlite3_value_blob(argv[5]), sqlite3_value_bytes(argv[5])); time_t atime = static_cast<time_t>(sqlite3_value_int64(argv[6])); time_t mtime = static_cast<time_t>(sqlite3_value_int64(argv[7])); time_t ctime = static_cast<time_t>(sqlite3_value_int64(argv[8])); int mode = sqlite3_value_int(argv[9]); int seg_num = sqlite3_value_int(argv[10]); _LOG_DEBUG("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << toHex(hash)); the->m_fileState->UpdateFile(filename, version, hash, device_name, seq_no, atime, mtime, ctime, mode, seg_num); // no callback here } else if (action == 1) // delete { the->m_fileState->DeleteFile(filename); the->m_onFileRemoved(filename); } sqlite3_result_null(context); }
static void sqlite_callback_args(int argc, sqlite3_value **argv) { int i; for (i = 0; i < argc; i++) { sqlite3_value *sv = argv[i]; Value *vp = fg->stk_top; switch (sqlite3_value_type(sv)) { case SQLITE_INTEGER: *vp = fs->int64_Value(sqlite3_value_int64(sv)); break; case SQLITE_FLOAT: *vp = fs->float_Value(fs->cls_float, sqlite3_value_double(sv)); break; case SQLITE_TEXT: { const char *p = (const char*)sqlite3_value_text(sv); int len = sqlite3_value_bytes(sv); *vp = fs->cstr_Value(NULL, p, len); break; } case SQLITE_BLOB: { const char *p = (const char*)sqlite3_value_blob(sv); int len = sqlite3_value_bytes(sv); *vp = fs->cstr_Value(fs->cls_bytes, p, len); break; } default: *vp = VALUE_NULL; break; } fg->stk_top++; } }
SQLITE_EXTENSION_INIT1 /* ** returns */ static void rank(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal) { int *aMatchinfo; int nCol; int nPhrase; int iPhrase; double score = 0.0; aMatchinfo = (int *)sqlite3_value_blob(apVal[0]); nPhrase = aMatchinfo[0]; nCol = aMatchinfo[1]; // do a simple count on string match for(iPhrase=0; iPhrase<nPhrase; iPhrase++) { int iCol; int *aPhraseinfo = &aMatchinfo[2 + iPhrase*nCol*3]; for(iCol=0; iCol<nCol; iCol++){ int nHitCount = aPhraseinfo[3*iCol]; int nGlobalHitCount = aPhraseinfo[3*iCol+1]; double weight = sqlite3_value_double(apVal[iCol+1]); if( nHitCount>0 ){ score += ((double)nHitCount / (double)nGlobalHitCount) * weight; } } } sqlite3_result_double(pCtx, score); return; }
/* ** An SQL function invoked as follows: ** ** sqlite_readint32(BLOB) -- Decode 32-bit integer from start of blob */ static void readint_function( sqlite3_context *pCtx, int nArg, sqlite3_value **apArg ){ const u8 *zBlob; int nBlob; int iOff = 0; u32 iRet = 0; if( nArg!=1 && nArg!=2 ){ sqlite3_result_error( pCtx, "wrong number of arguments to function sqlite_readint32()", -1 ); return; } if( nArg==2 ){ iOff = sqlite3_value_int(apArg[1]); } zBlob = sqlite3_value_blob(apArg[0]); nBlob = sqlite3_value_bytes(apArg[0]); if( nBlob>=(iOff+4) ){ iRet = get4byte(&zBlob[iOff]); } sqlite3_result_int64(pCtx, (sqlite3_int64)iRet); }
static void OGR2SQLITE_ogr_inflate(sqlite3_context* pContext, int argc, sqlite3_value** argv) { if( argc != 1 || sqlite3_value_type (argv[0]) != SQLITE_BLOB ) { sqlite3_result_null (pContext); return; } size_t nOutBytes = 0; void* pOut; const void* pSrc = sqlite3_value_blob (argv[0]); int nLen = sqlite3_value_bytes (argv[0]); pOut = CPLZLibInflate( pSrc, nLen, NULL, 0, &nOutBytes); if( pOut != NULL ) { sqlite3_result_blob (pContext, pOut, nOutBytes, VSIFree); } else { sqlite3_result_null (pContext); } return; }
static void OGR2SQLITE_ST_GeomFromWKB(sqlite3_context* pContext, int argc, sqlite3_value** argv) { if( sqlite3_value_type (argv[0]) != SQLITE_BLOB ) { sqlite3_result_null (pContext); return; } int nSRID = -1; if( argc == 2 && sqlite3_value_type (argv[1]) == SQLITE_INTEGER ) nSRID = sqlite3_value_int( argv[1] ); GByte* pabySLBLOB = (GByte *) sqlite3_value_blob (argv[0]); int nBLOBLen = sqlite3_value_bytes (argv[0]); OGRGeometry* poGeom = NULL; if( OGRGeometryFactory::createFromWkb(pabySLBLOB, NULL, &poGeom, nBLOBLen) == OGRERR_NONE ) { OGR2SQLITE_SetGeom_AndDestroy(pContext, poGeom, nSRID); } else sqlite3_result_null (pContext); }
/**************************** sqlite3_column_ ******************************* ** The following routines are used to access elements of the current row ** in the result set. */ const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ const void *val; sqlite3MallocDisallow(); val = sqlite3_value_blob( columnMem(pStmt,i) ); sqlite3MallocAllow(); return val; }
void geo_simplify(sqlite3_context *context,int argc,sqlite3_value **argv) { if(argc == 2 && sqlite3_value_type(argv[0]) == SQLITE_BLOB) { GEOSGeometry* geometry; GEOSGeometry* simplify_geo; unsigned char* wkb; size_t size; const void* data = sqlite3_value_blob(argv[0]); size_t data_size = sqlite3_value_bytes(argv[0]); double tolerance = sqlite3_value_double(argv[1]); _init_geos(); geometry = _geo_from_wkb((const unsigned char*)data,data_size); if(geometry != 0) { simplify_geo = GEOSSimplify(geometry,tolerance); if(simplify_geo != 0) { wkb = GEOSGeomToWKB_buf(simplify_geo,&size); sqlite3_result_blob(context,wkb,size,SQLITE_TRANSIENT); GEOSGeom_destroy(simplify_geo); GEOSFree(wkb); } } GEOSGeom_destroy(geometry); finishGEOS(); } }
void geo_polyline_encode(sqlite3_context *context,int argc,sqlite3_value **argv) { if(argc >= 1 && sqlite3_value_type(argv[0]) == SQLITE_BLOB) { GEOSGeometry* geometry; char* encodestr; const void* data = sqlite3_value_blob(argv[0]); size_t data_size = sqlite3_value_bytes(argv[0]); int point = 1; if(argc > 1) { point = sqlite3_value_int(argv[1]); } _init_geos(); geometry = _geo_from_wkb((const unsigned char*)data,data_size); if(geometry != 0) { encodestr = polyline_encode(geometry,point); if(encodestr != 0) { sqlite3_result_text(context,encodestr,-1,SQLITE_TRANSIENT); free(encodestr); } } GEOSGeom_destroy(geometry); finishGEOS(); } }
/* ** Implementation of the sha3(X,SIZE) function. ** ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default ** size is 256. If X is a BLOB, it is hashed as is. ** For all other non-NULL types of input, X is converted into a UTF-8 string ** and the string is hashed without the trailing 0x00 terminator. The hash ** of a NULL value is NULL. */ static void sha3Func( sqlite3_context *context, int argc, sqlite3_value **argv ){ SHA3Context cx; int eType = sqlite3_value_type(argv[0]); int nByte = sqlite3_value_bytes(argv[0]); int iSize; if( argc==1 ){ iSize = 256; }else{ iSize = sqlite3_value_int(argv[1]); if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " "384 512", -1); return; } } if( eType==SQLITE_NULL ) return; SHA3Init(&cx, iSize); if( eType==SQLITE_BLOB ){ SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte); }else{ SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte); } sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); }
/* ** EXPERIMENTAL - This is not an official function. The interface may ** change. This function may disappear. Do not write code that depends ** on this function. ** ** Implementation of the QUOTE() function. This function takes a single ** argument. If the argument is numeric, the return value is the same as ** the argument. If the argument is NULL, the return value is the string ** "NULL". Otherwise, the argument is enclosed in single quotes with ** single-quote escapes. */ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv) { if( argc<1 ) return; switch( sqlite3_value_type(argv[0]) ) { case SQLITE_NULL: { sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); break; } case SQLITE_INTEGER: case SQLITE_FLOAT: { sqlite3_result_value(context, argv[0]); break; } case SQLITE_BLOB: { char *zText = 0; int nBlob = sqlite3_value_bytes(argv[0]); char const *zBlob = sqlite3_value_blob(argv[0]); zText = (char *)sqliteMalloc((2*nBlob)+4); if( !zText ) { sqlite3_result_error(context, "out of memory", -1); } else { int i; for(i=0; i<nBlob; i++) { zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; } zText[(nBlob*2)+2] = '\''; zText[(nBlob*2)+3] = '\0'; zText[0] = 'X'; zText[1] = '\''; sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); sqliteFree(zText); } break; } case SQLITE_TEXT: { int i,j,n; const unsigned char *zArg = sqlite3_value_text(argv[0]); char *z; for(i=n=0; zArg[i]; i++) { if( zArg[i]=='\'' ) n++; } z = sqliteMalloc( i+n+3 ); if( z==0 ) return; z[0] = '\''; for(i=0, j=1; zArg[i]; i++) { z[j++] = zArg[i]; if( zArg[i]=='\'' ) { z[j++] = '\''; } } z[j++] = '\''; z[j] = 0; sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); sqliteFree(z); } } }
static void OGR2SQLITE_Transform(sqlite3_context* pContext, int argc, sqlite3_value** argv) { if( argc != 3 ) { sqlite3_result_null (pContext); return; } if( sqlite3_value_type (argv[0]) != SQLITE_BLOB ) { sqlite3_result_null (pContext); return; } if( sqlite3_value_type (argv[1]) != SQLITE_INTEGER ) { sqlite3_result_null (pContext); return; } if( sqlite3_value_type (argv[2]) != SQLITE_INTEGER ) { sqlite3_result_null (pContext); return; } int nSrcSRSId = sqlite3_value_int(argv[1]); int nDstSRSId = sqlite3_value_int(argv[2]); OGRSQLiteExtensionData* poModule = (OGRSQLiteExtensionData*) sqlite3_user_data(pContext); OGRCoordinateTransformation* poCT = poModule->GetTransform(nSrcSRSId, nDstSRSId); if( poCT == NULL ) { sqlite3_result_null (pContext); return; } GByte* pabySLBLOB = (GByte *) sqlite3_value_blob (argv[0]); int nBLOBLen = sqlite3_value_bytes (argv[0]); OGRGeometry* poGeom = NULL; if( OGRSQLiteLayer::ImportSpatiaLiteGeometry( pabySLBLOB, nBLOBLen, &poGeom ) == CE_None && poGeom->transform(poCT) == OGRERR_NONE && OGRSQLiteLayer::ExportSpatiaLiteGeometry( poGeom, nDstSRSId, wkbNDR, FALSE, FALSE, FALSE, &pabySLBLOB, &nBLOBLen ) == CE_None ) { sqlite3_result_blob(pContext, pabySLBLOB, nBLOBLen, CPLFree); } else { sqlite3_result_null (pContext); } delete poGeom; }
/* ** Implementation of the substr() function. ** ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. ** p1 is 1-indexed. So substr(x,1,1) returns the first character ** of x. If x is text, then we actually count UTF-8 characters. ** If x is a blob, then we count bytes. ** ** If p1 is negative, then we begin abs(p1) from the end of x[]. */ static void substrFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ const unsigned char *z; const unsigned char *z2; int len; int p0type; i64 p1, p2; assert( argc==3 || argc==2 ); p0type = sqlite3_value_type(argv[0]); if( p0type==SQLITE_BLOB ){ len = sqlite3_value_bytes(argv[0]); z = sqlite3_value_blob(argv[0]); if( z==0 ) return; assert( len==sqlite3_value_bytes(argv[0]) ); }else{ z = sqlite3_value_text(argv[0]); if( z==0 ) return; len = 0; for(z2=z; *z2; len++){ SQLITE_SKIP_UTF8(z2); } } p1 = sqlite3_value_int(argv[1]); if( argc==3 ){ p2 = sqlite3_value_int(argv[2]); }else{ p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; } if( p1<0 ){ p1 += len; if( p1<0 ){ p2 += p1; p1 = 0; } }else if( p1>0 ){ p1--; } if( p1+p2>len ){ p2 = len-p1; } if( p0type!=SQLITE_BLOB ){ while( *z && p1 ){ SQLITE_SKIP_UTF8(z); p1--; } for(z2=z; *z2 && p2; p2--){ SQLITE_SKIP_UTF8(z2); } sqlite3_result_text(context, (char*)z, z2-z, SQLITE_TRANSIENT); }else{ if( p2<0 ) p2 = 0; sqlite3_result_blob(context, (char*)&z[p1], p2, SQLITE_TRANSIENT); } }
/* * rank_func -- * Sqlite user defined function for ranking the documents. * For each phrase of the query, it computes the tf and idf and adds them over. * It computes the final rank, by multiplying tf and idf together. * Weight of term t for document d = (term frequency of t in d * * inverse document frequency of t) * * Term Frequency of term t in document d = Number of times t occurs in d / * Number of times t appears in all * documents * * Inverse document frequency of t = log(Total number of documents / * Number of documents in which t occurs) */ static void rank_func(sqlite3_context *pctx, int nval, sqlite3_value **apval) { inverse_document_frequency *idf = sqlite3_user_data(pctx); double tf = 0.0; const unsigned int *matchinfo; int ncol; int nphrase; int iphrase; int ndoc; int doclen = 0; const double k = 3.75; /* Check that the number of arguments passed to this function is correct. */ assert(nval == 1); matchinfo = (const unsigned int *) sqlite3_value_blob(apval[0]); nphrase = matchinfo[0]; ncol = matchinfo[1]; ndoc = matchinfo[2 + 3 * ncol * nphrase + ncol]; for (iphrase = 0; iphrase < nphrase; iphrase++) { int icol; const unsigned int *phraseinfo = &matchinfo[2 + ncol+ iphrase * ncol * 3]; for(icol = 1; icol < ncol; icol++) { /* nhitcount: number of times the current phrase occurs in the current * column in the current document. * nglobalhitcount: number of times current phrase occurs in the current * column in all documents. * ndocshitcount: number of documents in which the current phrase * occurs in the current column at least once. */ int nhitcount = phraseinfo[3 * icol]; int nglobalhitcount = phraseinfo[3 * icol + 1]; int ndocshitcount = phraseinfo[3 * icol + 2]; doclen = matchinfo[2 + icol ]; double weight = col_weights[icol - 1]; if (idf->status == 0 && ndocshitcount) idf->value += log(((double)ndoc / ndocshitcount))* weight; /* Dividing the tf by document length to normalize the effect of * longer documents. */ if (nglobalhitcount > 0 && nhitcount) tf += (((double)nhitcount * weight) / (nglobalhitcount * doclen)); } } idf->status = 1; /* Final score = (tf * idf)/ ( k + tf) * Dividing by k+ tf further normalizes the weight leading to better * results. * The value of k is experimental */ double score = (tf * idf->value/ ( k + tf)) ; sqlite3_result_double(pctx, score); return; }
/* ** Output an sqlite3_value object's value as an SQL literal. */ static void vtablogQuote(sqlite3_value *p){ char z[50]; switch( sqlite3_value_type(p) ){ case SQLITE_NULL: { printf("NULL"); break; } case SQLITE_INTEGER: { sqlite3_snprintf(50,z,"%lld", sqlite3_value_int64(p)); printf("%s", z); break; } case SQLITE_FLOAT: { sqlite3_snprintf(50,z,"%!.20g", sqlite3_value_double(p)); printf("%s", z); break; } case SQLITE_BLOB: { int n = sqlite3_value_bytes(p); const unsigned char *z = (const unsigned char*)sqlite3_value_blob(p); int i; printf("x'"); for(i=0; i<n; i++) printf("%02x", z[i]); printf("'"); break; } case SQLITE_TEXT: { const char *z = (const char*)sqlite3_value_text(p); int i; char c; for(i=0; (c = z[i])!=0 && c!='\''; i++){} if( c==0 ){ printf("'%s'",z); }else{ printf("'"); while( *z ){ for(i=0; (c = z[i])!=0 && c!='\''; i++){} if( c=='\'' ) i++; if( i ){ printf("%.*s", i, z); z += i; } if( c=='\'' ){ printf("'"); continue; } if( c==0 ){ break; } z++; } printf("'"); } break; } } }
void geo_bound(sqlite3_context *context,int argc,sqlite3_value **argv) { if(argc >= 1) { const unsigned char* ogc; unsigned char* ret_geo_buf; size_t size; double x1,x2,y1,y2; GEOSCoordSequence* seq = 0; GEOSGeometry* geometry = 0; GEOSGeometry* middle_geo = 0; _init_geos(); if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_BLOB) { size = sqlite3_value_bytes(argv[0]); ogc = (const unsigned char*)sqlite3_value_blob(argv[0]); middle_geo = _geo_from_wkb(ogc,size); } else if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_TEXT) { ogc = sqlite3_value_text(argv[0]); middle_geo = _geo_from_wkt(ogc); } else if(argc == 4) { x1 = sqlite3_value_double(argv[0]); y1 = sqlite3_value_double(argv[1]); x2 = sqlite3_value_double(argv[2]); y2 = sqlite3_value_double(argv[3]); seq = GEOSCoordSeq_create(2,2); GEOSCoordSeq_setX(seq,0,x1); GEOSCoordSeq_setY(seq,0,y1); GEOSCoordSeq_setX(seq,1,x2); GEOSCoordSeq_setY(seq,1,y2); middle_geo = GEOSGeom_createLineString(seq); } if(middle_geo != 0) { geometry = GEOSEnvelope(middle_geo); if(geometry != 0) { ret_geo_buf = GEOSGeomToWKB_buf(geometry,&size); sqlite3_result_blob(context,ret_geo_buf,size,SQLITE_TRANSIENT); GEOSGeom_destroy(geometry); GEOSFree(ret_geo_buf); } GEOSGeom_destroy(middle_geo); } finishGEOS(); } }
int fetch_bfp_arg(sqlite3_value* arg, Bfp **ppBfp) { int rc = SQLITE_MISMATCH; /* Check that value is a blob */ if (sqlite3_value_type(arg) == SQLITE_BLOB) { int sz = sqlite3_value_bytes(arg); rc = blob_to_bfp(sqlite3_value_blob(arg), sz, ppBfp); } return rc; }
/* * Implement regular expression match * as an application-defined SQL function. */ static void sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv) { assert(2 == argc); sqlite3_result_int(context, !regexec( (regex_t *)sqlite3_value_blob(argv[0]), (const char *)sqlite3_value_text(argv[1]), 0, NULL, 0)); }
/**************************** sqlite3_column_ ******************************* ** The following routines are used to access elements of the current row ** in the result set. */ const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ const void *val; val = sqlite3_value_blob( columnMem(pStmt,i) ); /* Even though there is no encoding conversion, value_blob() might ** need to call malloc() to expand the result of a zeroblob() ** expression. */ columnMallocFailure(pStmt); return val; }
void PibDb::keyDeletedFun(sqlite3_context* context, int argc, sqlite3_value** argv) { BOOST_ASSERT(argc == 1); PibDb* pibDb = reinterpret_cast<PibDb*>(sqlite3_user_data(context)); Name keyName(Block(sqlite3_value_blob(argv[0]), sqlite3_value_bytes(argv[0]))); pibDb->keyDeleted(keyName); }
void PibDb::certInsertedFun(sqlite3_context* context, int argc, sqlite3_value** argv) { BOOST_ASSERT(argc == 1); PibDb* pibDb = reinterpret_cast<PibDb*>(sqlite3_user_data(context)); Name certName(Block(sqlite3_value_blob(argv[0]), sqlite3_value_bytes(argv[0]))); pibDb->certificateInserted(certName); }
/* * unsigned int v; // count the number of bits set in v * unsigned int c; // c accumulates the total bits set in v * for (c = 0; v; c++) * { * v &= v - 1; // clear the least significant bit set * } */ extern void my_bit_count(sqlite3_context * context, int argc, sqlite3_value ** argv) { int i; char *binstring; unsigned char *val; int sz; unsigned int v; unsigned int cnt = 0; unsigned int c; int ok = 1; _ksu_null_if_null_param(argc, argv); binstring = (char *)sqlite3_value_text(argv[0]); if (*binstring == 'b') { binstring++; while (ok && *binstring) { switch(*binstring) { case '0': break; case '1': cnt++; break; default: // Not a binary string representation cnt = 0; ok = 0; break; } binstring++; } if (ok) { sqlite3_result_int(context, cnt); return; } } if (sqlite3_value_type(argv[0]) == SQLITE_INTEGER) { v = (unsigned int)sqlite3_value_int(argv[0]); // Brian Kernighan's method for (cnt = 0; v; cnt++) { v &= v - 1; // clear the least significant bit set } } else { sz = sqlite3_value_bytes(argv[0]); val = (unsigned char *)sqlite3_value_blob(argv[0]); for (i = 0; i < sz; i++) { v = (unsigned int)val[i]; for (c = 0; v; c++) { v &= v - 1; } cnt += c; } } sqlite3_result_int(context, cnt); }
static void db_pwhash(sqlite3_context *context, int argc, sqlite3_value **argv){ int res; assert( argc==2 ); assert( sqlite3_value_type(argv[0]) == SQLITE_BLOB ); assert( sqlite3_value_type(argv[1]) == SQLITE3_TEXT ); const int saltsize = sqlite3_value_bytes(argv[0]); const unsigned char *salt = sqlite3_value_blob(argv[0]); const int pwsize = sqlite3_value_bytes(argv[1]); const unsigned char *pw = sqlite3_value_blob(argv[1]); unsigned char *out = calloc(sizeof (unsigned char), DB_PWHASH_OUTLEN); res = PKCS5_PBKDF2_HMAC_SHA1((const char *)pw, pwsize, salt, saltsize, DB_PWHASH_ITERS, DB_PWHASH_OUTLEN, out); assert(res != 0); sqlite3_result_blob(context, out, DB_PWHASH_OUTLEN, free); }
/* ** Implementation of the SQL scalar function for accessing the underlying ** hash table. This function may be called as follows: ** ** SELECT <function-name>(<key-name>); ** SELECT <function-name>(<key-name>, <pointer>); ** ** where <function-name> is the name passed as the second argument ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer'). ** ** If the <pointer> argument is specified, it must be a blob value ** containing a pointer to be stored as the hash data corresponding ** to the string <key-name>. If <pointer> is not specified, then ** the string <key-name> must already exist in the has table. Otherwise, ** an error is returned. ** ** Whether or not the <pointer> argument is specified, the value returned ** is a blob containing the pointer stored as the hash data corresponding ** to string <key-name> (after the hash-table is updated, if applicable). */ static void scalarFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ Fts3Hash *pHash; void *pPtr = 0; const unsigned char *zName; int nName; assert( argc==1 || argc==2 ); pHash = (Fts3Hash *)sqlite3_user_data(context); zName = sqlite3_value_text(argv[0]); nName = sqlite3_value_bytes(argv[0])+1; if( argc==2 ){ #ifdef SQLITE_ENABLE_FTS3_TOKENIZER void *pOld; int n = sqlite3_value_bytes(argv[1]); if( zName==0 || n!=sizeof(pPtr) ){ sqlite3_result_error(context, "argument type mismatch", -1); return; } pPtr = *(void **)sqlite3_value_blob(argv[1]); pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); if( pOld==pPtr ){ sqlite3_result_error(context, "out of memory", -1); return; } #else sqlite3_result_error(context, "fts3tokenize: " "disabled - rebuild with -DSQLITE_ENABLE_FTS3_TOKENIZER", -1 ); return; #endif /* SQLITE_ENABLE_FTS3_TOKENIZER */ }else { if( zName ){ pPtr = sqlite3Fts3HashFind(pHash, zName, nName); } if( !pPtr ){ char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); sqlite3_result_error(context, zErr, -1); sqlite3_free(zErr); return; } } sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); }