** Implementation of the sha3_query(SQL,SIZE) function. ** ** This function compiles and runs the SQL statement(s) given in the ** argument. The results are hashed using a SIZE-bit SHA3. The default ** size is 256. ** ** The format of the byte stream that is hashed is summarized as follows: ** ** S : ** R ** N ** I ** F ** B :<bytes
| 1971 | ** with no delimiters of any kind. |
| 1972 | */ |
| 1973 | static void sha3QueryFunc( |
| 1974 | sqlite3_context *context, |
| 1975 | int argc, |
| 1976 | sqlite3_value **argv |
| 1977 | ){ |
| 1978 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 1979 | const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 1980 | sqlite3_stmt *pStmt = 0; |
| 1981 | int nCol; /* Number of columns in the result set */ |
| 1982 | int i; /* Loop counter */ |
| 1983 | int rc; |
| 1984 | int n; |
| 1985 | const char *z; |
| 1986 | SHA3Context cx; |
| 1987 | int iSize; |
| 1988 | |
| 1989 | if( argc==1 ){ |
| 1990 | iSize = 256; |
| 1991 | }else{ |
| 1992 | iSize = sqlite3_value_int(argv[1]); |
| 1993 | if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ |
| 1994 | sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " |
| 1995 | "384 512", -1); |
| 1996 | return; |
| 1997 | } |
| 1998 | } |
| 1999 | if( zSql==0 ) return; |
| 2000 | SHA3Init(&cx, iSize); |
| 2001 | while( zSql[0] ){ |
| 2002 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 2003 | if( rc ){ |
| 2004 | char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", |
| 2005 | zSql, sqlite3_errmsg(db)); |
| 2006 | sqlite3_finalize(pStmt); |
| 2007 | sqlite3_result_error(context, zMsg, -1); |
| 2008 | sqlite3_free(zMsg); |
| 2009 | return; |
| 2010 | } |
| 2011 | if( !sqlite3_stmt_readonly(pStmt) ){ |
| 2012 | char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); |
| 2013 | sqlite3_finalize(pStmt); |
| 2014 | sqlite3_result_error(context, zMsg, -1); |
| 2015 | sqlite3_free(zMsg); |
| 2016 | return; |
| 2017 | } |
| 2018 | nCol = sqlite3_column_count(pStmt); |
| 2019 | z = sqlite3_sql(pStmt); |
| 2020 | if( z ){ |
| 2021 | n = (int)strlen(z); |
| 2022 | hash_step_vformat(&cx,"S%d:",n); |
| 2023 | SHA3Update(&cx,(unsigned char*)z,n); |
| 2024 | } |
| 2025 | |
| 2026 | /* Compute a hash over the result of the query */ |
| 2027 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 2028 | SHA3Update(&cx,(const unsigned char*)"R",1); |
| 2029 | for(i=0; i<nCol; i++){ |
| 2030 | switch( sqlite3_column_type(pStmt,i) ){ |
nothing calls this directly
no test coverage detected