sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** 2017-01-27 |
| 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 | ** |
| 13 | ** This SQLite extension implements functions that compute SHA1 hashes. |
| 14 | ** Two SQL functions are implemented: |
| 15 | ** |
| 16 | ** sha1(X) |
| 17 | ** sha1_query(Y) |
| 18 | ** |
| 19 | ** The sha1(X) function computes the SHA1 hash of the input X, or NULL if |
| 20 | ** X is NULL. |
| 21 | ** |
| 22 | ** The sha1_query(Y) function evalutes all queries in the SQL statements of Y |
| 23 | ** and returns a hash of their results. |
| 24 | */ |
| 25 | #include "sqlite3ext.h" |
| 26 | SQLITE_EXTENSION_INIT1 |
| 27 | #include <assert.h> |
| 28 | #include <string.h> |
| 29 | #include <stdarg.h> |
| 30 | |
| 31 | /****************************************************************************** |
| 32 | ** The Hash Engine |
| 33 | */ |
| 34 | #if defined(__GNUC__) && __GNUC__>=11 |
| 35 | # pragma GCC diagnostic push |
| 36 | # pragma GCC diagnostic ignored "-Wstringop-overread" |
| 37 | #endif |
| 38 | /* Context for the SHA1 hash */ |
| 39 | typedef struct SHA1Context SHA1Context; |
| 40 | struct SHA1Context { |
| 41 | unsigned int state[5]; |
| 42 | unsigned int count[2]; |
| 43 | unsigned char buffer[64]; |
| 44 | }; |
| 45 | |
| 46 | #define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r)) |
| 47 | #define rol(x,k) SHA_ROT(x,k,32-(k)) |
| 48 | #define ror(x,k) SHA_ROT(x,32-(k),k) |
| 49 | |
| 50 | #define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \ |
| 51 | |(rol(block[i],8)&0x00FF00FF)) |
| 52 | #define blk0be(i) block[i] |
| 53 | #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ |
| 54 | ^block[(i+2)&15]^block[i&15],1)) |
| 55 | |
| 56 | /* |
| 57 | * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 |
| 58 | * |
| 59 | * Rl0() for little-endian and Rb0() for big-endian. Endianness is |
| 60 | * determined at run-time. |
| 61 | */ |
| 62 | #define Rl0(v,w,x,y,z,i) \ |
| 63 | z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2); |
| 64 | #define Rb0(v,w,x,y,z,i) \ |
| 65 | z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2); |
| 66 | #define R1(v,w,x,y,z,i) \ |
| 67 | z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2); |
| 68 | #define R2(v,w,x,y,z,i) \ |
| 69 | z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2); |
| 70 | #define R3(v,w,x,y,z,i) \ |
| 71 | z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2); |
| 72 | #define R4(v,w,x,y,z,i) \ |
| 73 | z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2); |
| 74 | |
| 75 | /* |
| 76 | * Hash a single 512-bit block. This is the core of the algorithm. |
| 77 | */ |
| 78 | static void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]){ |
| 79 | unsigned int qq[5]; /* a, b, c, d, e; */ |
| 80 | static int one = 1; |
| 81 | unsigned int block[16]; |
| 82 | memcpy(block, buffer, 64); |
| 83 | memcpy(qq,state,5*sizeof(unsigned int)); |
| 84 | |
| 85 | #define a qq[0] |
| 86 | #define b qq[1] |
| 87 | #define c qq[2] |
| 88 | #define d qq[3] |
| 89 | #define e qq[4] |
| 90 | |
| 91 | /* Copy p->state[] to working vars */ |
| 92 | /* |
| 93 | a = state[0]; |
| 94 | b = state[1]; |
| 95 | c = state[2]; |
| 96 | d = state[3]; |
| 97 | e = state[4]; |
| 98 | */ |
| 99 | |
| 100 | /* 4 rounds of 20 operations each. Loop unrolled. */ |
| 101 | if( 1 == *(unsigned char*)&one ){ |
| 102 | Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3); |
| 103 | Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7); |
| 104 | Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11); |
| 105 | Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15); |
| 106 | }else{ |
| 107 | Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3); |
| 108 | Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7); |
| 109 | Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11); |
| 110 | Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15); |
| 111 | } |
| 112 | R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); |
| 113 | R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); |
| 114 | R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); |
| 115 | R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); |
| 116 | R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); |
| 117 | R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); |
| 118 | R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); |
| 119 | R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); |
| 120 | R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); |
| 121 | R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); |
| 122 | R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); |
| 123 | R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); |
| 124 | R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); |
| 125 | R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); |
| 126 | R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); |
| 127 | R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); |
| 128 | |
| 129 | /* Add the working vars back into context.state[] */ |
| 130 | state[0] += a; |
| 131 | state[1] += b; |
| 132 | state[2] += c; |
| 133 | state[3] += d; |
| 134 | state[4] += e; |
| 135 | |
| 136 | #undef a |
| 137 | #undef b |
| 138 | #undef c |
| 139 | #undef d |
| 140 | #undef e |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /* Initialize a SHA1 context */ |
| 145 | static void hash_init(SHA1Context *p){ |
| 146 | /* SHA1 initialization constants */ |
| 147 | p->state[0] = 0x67452301; |
| 148 | p->state[1] = 0xEFCDAB89; |
| 149 | p->state[2] = 0x98BADCFE; |
| 150 | p->state[3] = 0x10325476; |
| 151 | p->state[4] = 0xC3D2E1F0; |
| 152 | p->count[0] = p->count[1] = 0; |
| 153 | } |
| 154 | |
| 155 | /* Add new content to the SHA1 hash */ |
| 156 | static void hash_step( |
| 157 | SHA1Context *p, /* Add content to this context */ |
| 158 | const unsigned char *data, /* Data to be added */ |
| 159 | unsigned int len /* Number of bytes in data */ |
| 160 | ){ |
| 161 | unsigned int i, j; |
| 162 | |
| 163 | j = p->count[0]; |
| 164 | if( (p->count[0] += len << 3) < j ){ |
| 165 | p->count[1] += (len>>29)+1; |
| 166 | } |
| 167 | j = (j >> 3) & 63; |
| 168 | if( (j + len) > 63 ){ |
| 169 | (void)memcpy(&p->buffer[j], data, (i = 64-j)); |
| 170 | SHA1Transform(p->state, p->buffer); |
| 171 | for(; i + 63 < len; i += 64){ |
| 172 | SHA1Transform(p->state, &data[i]); |
| 173 | } |
| 174 | j = 0; |
| 175 | }else{ |
| 176 | i = 0; |
| 177 | } |
| 178 | if( len-i>0 ) (void)memcpy(&p->buffer[j], &data[i], len - i); |
| 179 | } |
| 180 | |
| 181 | /* Compute a string using sqlite3_vsnprintf() and hash it */ |
| 182 | static void hash_step_vformat( |
| 183 | SHA1Context *p, /* Add content to this context */ |
| 184 | const char *zFormat, |
| 185 | ... |
| 186 | ){ |
| 187 | va_list ap; |
| 188 | int n; |
| 189 | char zBuf[50]; |
| 190 | va_start(ap, zFormat); |
| 191 | sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); |
| 192 | va_end(ap); |
| 193 | n = (int)strlen(zBuf); |
| 194 | hash_step(p, (unsigned char*)zBuf, n); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /* Add padding and compute the message digest. Render the |
| 199 | ** message digest as lower-case hexadecimal and put it into |
| 200 | ** zOut[]. zOut[] must be at least 41 bytes long. */ |
| 201 | static void hash_finish( |
| 202 | SHA1Context *p, /* The SHA1 context to finish and render */ |
| 203 | char *zOut, /* Store hex or binary hash here */ |
| 204 | int bAsBinary /* 1 for binary hash, 0 for hex hash */ |
| 205 | ){ |
| 206 | unsigned int i; |
| 207 | unsigned char finalcount[8]; |
| 208 | unsigned char digest[20]; |
| 209 | static const char zEncode[] = "0123456789abcdef"; |
| 210 | |
| 211 | for (i = 0; i < 8; i++){ |
| 212 | finalcount[i] = (unsigned char)((p->count[(i >= 4 ? 0 : 1)] |
| 213 | >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ |
| 214 | } |
| 215 | hash_step(p, (const unsigned char *)"\200", 1); |
| 216 | while ((p->count[0] & 504) != 448){ |
| 217 | hash_step(p, (const unsigned char *)"\0", 1); |
| 218 | } |
| 219 | hash_step(p, finalcount, 8); /* Should cause a SHA1Transform() */ |
| 220 | for (i = 0; i < 20; i++){ |
| 221 | digest[i] = (unsigned char)((p->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
| 222 | } |
| 223 | if( bAsBinary ){ |
| 224 | memcpy(zOut, digest, 20); |
| 225 | }else{ |
| 226 | for(i=0; i<20; i++){ |
| 227 | zOut[i*2] = zEncode[(digest[i]>>4)&0xf]; |
| 228 | zOut[i*2+1] = zEncode[digest[i] & 0xf]; |
| 229 | } |
| 230 | zOut[i*2]= 0; |
| 231 | } |
| 232 | } |
| 233 | #if defined(__GNUC__) && __GNUC__>=11 |
| 234 | # pragma GCC diagnostic pop |
| 235 | #endif |
| 236 | /* End of the hashing logic |
| 237 | *****************************************************************************/ |
| 238 | |
| 239 | /* |
| 240 | ** Two SQL functions: sha1(X) and sha1b(X). |
| 241 | ** |
| 242 | ** sha1(X) returns a lower-case hexadecimal rendering of the SHA1 hash |
| 243 | ** of the argument X. If X is a BLOB, it is hashed as is. For all other |
| 244 | ** types of input, X is converted into a UTF-8 string and the string |
| 245 | ** is hashed without the trailing 0x00 terminator. The hash of a NULL |
| 246 | ** value is NULL. |
| 247 | ** |
| 248 | ** sha1b(X) is the same except that it returns a 20-byte BLOB containing |
| 249 | ** the binary hash instead of a hexadecimal string. |
| 250 | */ |
| 251 | static void sha1Func( |
| 252 | sqlite3_context *context, |
| 253 | int argc, |
| 254 | sqlite3_value **argv |
| 255 | ){ |
| 256 | SHA1Context cx; |
| 257 | int eType = sqlite3_value_type(argv[0]); |
| 258 | int nByte = sqlite3_value_bytes(argv[0]); |
| 259 | const unsigned char *pData; |
| 260 | char zOut[44]; |
| 261 | |
| 262 | assert( argc==1 ); |
| 263 | if( eType==SQLITE_NULL ) return; |
| 264 | hash_init(&cx); |
| 265 | if( eType==SQLITE_BLOB ){ |
| 266 | pData = (const unsigned char*)sqlite3_value_blob(argv[0]); |
| 267 | }else{ |
| 268 | pData = (const unsigned char*)sqlite3_value_text(argv[0]); |
| 269 | } |
| 270 | if( pData==0 ){ |
| 271 | if( nByte ) return; |
| 272 | pData = (const unsigned char*)""; |
| 273 | } |
| 274 | hash_step(&cx, pData, nByte); |
| 275 | if( sqlite3_user_data(context)!=0 ){ |
| 276 | /* sha1b() - binary result */ |
| 277 | hash_finish(&cx, zOut, 1); |
| 278 | sqlite3_result_blob(context, zOut, 20, SQLITE_TRANSIENT); |
| 279 | }else{ |
| 280 | /* sha1() - hexadecimal text result */ |
| 281 | hash_finish(&cx, zOut, 0); |
| 282 | sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | ** Implementation of the sha1_query(SQL) function. |
| 288 | ** |
| 289 | ** This function compiles and runs the SQL statement(s) given in the |
| 290 | ** argument. The results are hashed using SHA1 and that hash is returned. |
| 291 | ** |
| 292 | ** The original SQL text is included as part of the hash. |
| 293 | ** |
| 294 | ** The hash is not just a concatenation of the outputs. Each query |
| 295 | ** is delimited and each row and value within the query is delimited, |
| 296 | ** with all values being marked with their datatypes. |
| 297 | */ |
| 298 | static void sha1QueryFunc( |
| 299 | sqlite3_context *context, |
| 300 | int argc, |
| 301 | sqlite3_value **argv |
| 302 | ){ |
| 303 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 304 | const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 305 | sqlite3_stmt *pStmt = 0; |
| 306 | int nCol; /* Number of columns in the result set */ |
| 307 | int i; /* Loop counter */ |
| 308 | int rc; |
| 309 | int n; |
| 310 | const char *z; |
| 311 | SHA1Context cx; |
| 312 | char zOut[44]; |
| 313 | int isRecursive = 0; |
| 314 | int *pIsRecursive; |
| 315 | |
| 316 | assert( argc==1 ); |
| 317 | if( zSql==0 ) return; |
| 318 | pIsRecursive = sqlite3_get_clientdata(db, "sha1_query()"); |
| 319 | if( pIsRecursive ){ |
| 320 | *pIsRecursive = 1; |
| 321 | return; |
| 322 | } |
| 323 | hash_init(&cx); |
| 324 | while( zSql[0] ){ |
| 325 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 326 | if( rc ){ |
| 327 | char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", |
| 328 | zSql, sqlite3_errmsg(db)); |
| 329 | sqlite3_finalize(pStmt); |
| 330 | sqlite3_result_error(context, zMsg, -1); |
| 331 | sqlite3_free(zMsg); |
| 332 | return; |
| 333 | } |
| 334 | if( !sqlite3_stmt_readonly(pStmt) ){ |
| 335 | char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); |
| 336 | sqlite3_finalize(pStmt); |
| 337 | sqlite3_result_error(context, zMsg, -1); |
| 338 | sqlite3_free(zMsg); |
| 339 | return; |
| 340 | } |
| 341 | nCol = sqlite3_column_count(pStmt); |
| 342 | z = sqlite3_sql(pStmt); |
| 343 | if( z==0 ) z = ""; |
| 344 | n = (int)strlen(z); |
| 345 | hash_step_vformat(&cx,"S%d:",n); |
| 346 | hash_step(&cx,(unsigned char*)z,n); |
| 347 | |
| 348 | /* Compute a hash over the result of the query */ |
| 349 | sqlite3_set_clientdata(db, "sha1_query()", &isRecursive, 0); |
| 350 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 351 | hash_step(&cx,(const unsigned char*)"R",1); |
| 352 | for(i=0; i<nCol; i++){ |
| 353 | switch( sqlite3_column_type(pStmt,i) ){ |
| 354 | case SQLITE_NULL: { |
| 355 | hash_step(&cx, (const unsigned char*)"N",1); |
| 356 | break; |
| 357 | } |
| 358 | case SQLITE_INTEGER: { |
| 359 | sqlite3_uint64 u; |
| 360 | int j; |
| 361 | unsigned char x[9]; |
| 362 | sqlite3_int64 v = sqlite3_column_int64(pStmt,i); |
| 363 | memcpy(&u, &v, 8); |
| 364 | for(j=8; j>=1; j--){ |
| 365 | x[j] = u & 0xff; |
| 366 | u >>= 8; |
| 367 | } |
| 368 | x[0] = 'I'; |
| 369 | hash_step(&cx, x, 9); |
| 370 | break; |
| 371 | } |
| 372 | case SQLITE_FLOAT: { |
| 373 | sqlite3_uint64 u; |
| 374 | int j; |
| 375 | unsigned char x[9]; |
| 376 | double r = sqlite3_column_double(pStmt,i); |
| 377 | memcpy(&u, &r, 8); |
| 378 | for(j=8; j>=1; j--){ |
| 379 | x[j] = u & 0xff; |
| 380 | u >>= 8; |
| 381 | } |
| 382 | x[0] = 'F'; |
| 383 | hash_step(&cx,x,9); |
| 384 | break; |
| 385 | } |
| 386 | case SQLITE_TEXT: { |
| 387 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 388 | const unsigned char *z2 = sqlite3_column_text(pStmt, i); |
| 389 | hash_step_vformat(&cx,"T%d:",n2); |
| 390 | hash_step(&cx, z2, n2); |
| 391 | break; |
| 392 | } |
| 393 | case SQLITE_BLOB: { |
| 394 | int n2 = sqlite3_column_bytes(pStmt, i); |
| 395 | const unsigned char *z2 = sqlite3_column_blob(pStmt, i); |
| 396 | hash_step_vformat(&cx,"B%d:",n2); |
| 397 | hash_step(&cx, z2, n2); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | sqlite3_finalize(pStmt); |
| 404 | sqlite3_set_clientdata(db, "sha1_query()", 0, 0); |
| 405 | } |
| 406 | hash_finish(&cx, zOut, 0); |
| 407 | if( isRecursive ){ |
| 408 | sqlite3_result_error(context, "recursive use of sha1_query()", -1); |
| 409 | }else{ |
| 410 | sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | |
| 415 | #ifdef _WIN32 |
| 416 | __declspec(dllexport) |
| 417 | #endif |
| 418 | int sqlite3_sha_init( |
| 419 | sqlite3 *db, |
| 420 | char **pzErrMsg, |
| 421 | const sqlite3_api_routines *pApi |
| 422 | ){ |
| 423 | int rc = SQLITE_OK; |
| 424 | static int one = 1; |
| 425 | SQLITE_EXTENSION_INIT2(pApi); |
| 426 | (void)pzErrMsg; /* Unused parameter */ |
| 427 | rc = sqlite3_create_function(db, "sha1", 1, |
| 428 | SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, |
| 429 | 0, sha1Func, 0, 0); |
| 430 | if( rc==SQLITE_OK ){ |
| 431 | rc = sqlite3_create_function(db, "sha1b", 1, |
| 432 | SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, |
| 433 | (void*)&one, sha1Func, 0, 0); |
| 434 | } |
| 435 | if( rc==SQLITE_OK ){ |
| 436 | rc = sqlite3_create_function(db, "sha1_query", 1, |
| 437 | SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
| 438 | sha1QueryFunc, 0, 0); |
| 439 | } |
| 440 | return rc; |
| 441 | } |