Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/test_hexio.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / test_hexio.c` 475 lines ยท 12607 bytes Raw Blame History 1 /* 2 ** 2007 April 6 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 ** Code for testing all sorts of SQLite interfaces. This code 13 ** implements TCL commands for reading and writing the binary 14 ** database files and displaying the content of those files as 15 ** hexadecimal. We could, in theory, use the built-in "binary" 16 ** command of TCL to do a lot of this, but there are some issues 17 ** with historical versions of the "binary" command. So it seems 18 ** easier and safer to build our own mechanism. 19 */ 20 #include "sqliteInt.h" 21 #include "tclsqlite.h" 22 #include 23 #include 24 #include 25 26 27 /* 28 ** Convert binary to hex. The input zBuf[] contains N bytes of 29 ** binary data. zBuf[] is 2*n+1 bytes long. Overwrite zBuf[] 30 ** with a hexadecimal representation of its original binary input. 31 */ 32 void sqlite3TestBinToHex(unsigned char *zBuf, int N){ 33 const unsigned char zHex[] = "0123456789ABCDEF"; 34 int i, j; 35 unsigned char c; 36 i = N*2; 37 zBuf[i--] = 0; 38 for(j=N-1; j>=0; j--){ 39 c = zBuf[j]; 40 zBuf[i--] = zHex[c&0xf]; 41 zBuf[i--] = zHex[c>>4]; 42 } 43 assert( i==-1 ); 44 } 45 46 /* 47 ** Convert hex to binary. The input zIn[] contains N bytes of 48 ** hexadecimal. Convert this into binary and write aOut[] with 49 ** the binary data. Spaces in the original input are ignored. 50 ** Return the number of bytes of binary rendered. 51 */ 52 int sqlite3TestHexToBin(const unsigned char *zIn, int N, unsigned char *aOut){ 53 const unsigned char aMap[] = { 54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 57 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 0, 0, 0, 0, 0, 0, 58 0,11,12,13,14,15,16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60 0,11,12,13,14,15,16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70 }; 71 int i, j; 72 int hi=1; 73 unsigned char c; 74 75 for(i=j=0; i=2 && n<=13 && memcmp(z, "-littleendian", n)==0 ){ 214 bLittle = 1; 215 } 216 } 217 if( (objc-bLittle)!=2 ){ 218 Tcl_WrongNumArgs(interp, 1, objv, "[-littleendian] HEXDATA"); 219 return TCL_ERROR; 220 } 221 zIn = (const unsigned char *)Tcl_GetStringFromObj(objv[1+bLittle], &nIn); 222 aOut = sqlite3_malloc64( 1 + nIn/2 ); 223 if( aOut==0 ){ 224 return TCL_ERROR; 225 } 226 nOut = sqlite3TestHexToBin(zIn, (int)nIn, aOut); 227 if( nOut>=4 ){ 228 memcpy(aNum, aOut, 4); 229 }else{ 230 memset(aNum, 0, sizeof(aNum)); 231 memcpy(&aNum[4-nOut], aOut, nOut); 232 } 233 sqlite3_free(aOut); 234 if( bLittle ){ 235 val = (int)((u32)aNum[3]<<24) | (aNum[2]<<16) | (aNum[1]<<8) | aNum[0]; 236 }else{ 237 val = (int)((u32)aNum[0]<<24) | (aNum[1]<<16) | (aNum[2]<<8) | aNum[3]; 238 } 239 Tcl_SetObjResult(interp, Tcl_NewIntObj(val)); 240 return TCL_OK; 241 } 242 243 244 /* 245 ** USAGE: hexio_render_int16 INTEGER 246 ** 247 ** Render INTEGER has a 16-bit big-endian integer in hexadecimal. 248 */ 249 static int SQLITE_TCLAPI hexio_render_int16( 250 void * clientData, 251 Tcl_Interp *interp, 252 int objc, 253 Tcl_Obj *CONST objv[] 254 ){ 255 int val; 256 unsigned char aNum[10]; 257 258 if( objc!=2 ){ 259 Tcl_WrongNumArgs(interp, 1, objv, "INTEGER"); 260 return TCL_ERROR; 261 } 262 if( Tcl_GetIntFromObj(interp, objv[1], &val) ) return TCL_ERROR; 263 aNum[0] = val>>8; 264 aNum[1] = val; 265 sqlite3TestBinToHex(aNum, 2); 266 Tcl_SetObjResult(interp, Tcl_NewStringObj((char*)aNum, 4)); 267 return TCL_OK; 268 } 269 270 271 /* 272 ** USAGE: hexio_render_int32 INTEGER 273 ** 274 ** Render INTEGER has a 32-bit big-endian integer in hexadecimal. 275 */ 276 static int SQLITE_TCLAPI hexio_render_int32( 277 void * clientData, 278 Tcl_Interp *interp, 279 int objc, 280 Tcl_Obj *CONST objv[] 281 ){ 282 int val; 283 unsigned char aNum[10]; 284 285 if( objc!=2 ){ 286 Tcl_WrongNumArgs(interp, 1, objv, "INTEGER"); 287 return TCL_ERROR; 288 } 289 if( Tcl_GetIntFromObj(interp, objv[1], &val) ) return TCL_ERROR; 290 aNum[0] = val>>24; 291 aNum[1] = val>>16; 292 aNum[2] = val>>8; 293 aNum[3] = val; 294 sqlite3TestBinToHex(aNum, 4); 295 Tcl_SetObjResult(interp, Tcl_NewStringObj((char*)aNum, 8)); 296 return TCL_OK; 297 } 298 299 /* 300 ** USAGE: utf8_to_utf8 HEX 301 ** 302 ** The argument is a UTF8 string represented in hexadecimal. 303 ** The UTF8 might not be well-formed. Run this string through 304 ** sqlite3Utf8to8() convert it back to hex and return the result. 305 */ 306 static int SQLITE_TCLAPI utf8_to_utf8( 307 void * clientData, 308 Tcl_Interp *interp, 309 int objc, 310 Tcl_Obj *CONST objv[] 311 ){ 312 #ifdef SQLITE_DEBUG 313 Tcl_Size n; 314 int nOut; 315 const unsigned char *zOrig; 316 unsigned char *z; 317 if( objc!=2 ){ 318 Tcl_WrongNumArgs(interp, 1, objv, "HEX"); 319 return TCL_ERROR; 320 } 321 zOrig = (unsigned char *)Tcl_GetStringFromObj(objv[1], &n); 322 z = sqlite3_malloc64( n+4 ); 323 n = sqlite3TestHexToBin(zOrig, (int)n, z); 324 z[n] = 0; 325 nOut = sqlite3Utf8To8(z); 326 sqlite3TestBinToHex(z,nOut); 327 Tcl_AppendResult(interp, (char*)z, NULL); 328 sqlite3_free(z); 329 return TCL_OK; 330 #else 331 Tcl_AppendResult(interp, 332 "[utf8_to_utf8] unavailable - SQLITE_DEBUG not defined", NULL 333 ); 334 return TCL_ERROR; 335 #endif 336 } 337 338 static int getFts3Varint(const char *p, sqlite_int64 *v){ 339 const unsigned char *q = (const unsigned char *) p; 340 sqlite_uint64 x = 0, y = 1; 341 while( (*q & 0x80) == 0x80 ){ 342 x += y * (*q++ & 0x7f); 343 y <<= 7; 344 } 345 x += y * (*q++); 346 *v = (sqlite_int64) x; 347 return (int) (q - (unsigned char *)p); 348 } 349 350 static int putFts3Varint(char *p, sqlite_int64 v){ 351 unsigned char *q = (unsigned char *) p; 352 sqlite_uint64 vu = v; 353 do{ 354 *q++ = (unsigned char) ((vu & 0x7f) | 0x80); 355 vu >>= 7; 356 }while( vu!=0 ); 357 q[-1] &= 0x7f; /* turn off high bit in final byte */ 358 assert( q - (unsigned char *)p <= 10 ); 359 return (int) (q - (unsigned char *)p); 360 } 361 362 /* 363 ** USAGE: read_fts3varint BLOB VARNAME 364 ** 365 ** Read a varint from the start of BLOB. Set variable VARNAME to contain 366 ** the interpreted value. Return the number of bytes of BLOB consumed. 367 */ 368 static int SQLITE_TCLAPI read_fts3varint( 369 void * clientData, 370 Tcl_Interp *interp, 371 int objc, 372 Tcl_Obj *CONST objv[] 373 ){ 374 Tcl_Size nBlob; 375 unsigned char *zBlob; 376 sqlite3_int64 iVal; 377 int nVal; 378 379 if( objc!=3 ){ 380 Tcl_WrongNumArgs(interp, 1, objv, "BLOB VARNAME"); 381 return TCL_ERROR; 382 } 383 zBlob = Tcl_GetByteArrayFromObj(objv[1], &nBlob); 384 385 nVal = getFts3Varint((char*)zBlob, (sqlite3_int64 *)(&iVal)); 386 Tcl_ObjSetVar2(interp, objv[2], 0, Tcl_NewWideIntObj(iVal), 0); 387 Tcl_SetObjResult(interp, Tcl_NewIntObj(nVal)); 388 return TCL_OK; 389 } 390 391 /* 392 ** USAGE: make_fts3record ARGLIST 393 */ 394 static int SQLITE_TCLAPI make_fts3record( 395 void * clientData, 396 Tcl_Interp *interp, 397 int objc, 398 Tcl_Obj *CONST objv[] 399 ){ 400 Tcl_Obj **aArg = 0; 401 Tcl_Size nArg = 0; 402 unsigned char *aOut = 0; 403 sqlite3_int64 nOut = 0; 404 sqlite3_int64 nAlloc = 0; 405 int i; 406 407 if( objc!=2 ){ 408 Tcl_WrongNumArgs(interp, 1, objv, "LIST"); 409 return TCL_ERROR; 410 } 411 if( Tcl_ListObjGetElements(interp, objv[1], &nArg, &aArg) ){ 412 return TCL_ERROR; 413 } 414 415 for(i=0; i<(int)nArg; i++){ 416 Tcl_WideInt iVal; 417 if( TCL_OK==Tcl_GetWideIntFromObj(0, aArg[i], &iVal) ){ 418 if( nOut+10>nAlloc ){ 419 int nNew = nAlloc?nAlloc*2:128; 420 unsigned char *aNew = sqlite3_realloc(aOut, nNew); 421 if( aNew==0 ){ 422 sqlite3_free(aOut); 423 return TCL_ERROR; 424 } 425 aOut = aNew; 426 nAlloc = nNew; 427 } 428 nOut += putFts3Varint((char*)&aOut[nOut], iVal); 429 }else{ 430 Tcl_Size nVal = 0; 431 char *zVal = Tcl_GetStringFromObj(aArg[i], &nVal); 432 while( (nOut + nVal)>nAlloc ){ 433 sqlite3_int64 nNew = nAlloc?nAlloc*2:128; 434 unsigned char *aNew = sqlite3_realloc64(aOut, nNew); 435 if( aNew==0 ){ 436 sqlite3_free(aOut); 437 return TCL_ERROR; 438 } 439 aOut = aNew; 440 nAlloc = nNew; 441 } 442 memcpy(&aOut[nOut], zVal, nVal); 443 nOut += nVal; 444 } 445 } 446 447 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(aOut, nOut)); 448 sqlite3_free(aOut); 449 return TCL_OK; 450 } 451 452 453 /* 454 ** Register commands with the TCL interpreter. 455 */ 456 int Sqlitetest_hexio_Init(Tcl_Interp *interp){ 457 static struct { 458 char *zName; 459 Tcl_ObjCmdProc *xProc; 460 } aObjCmd[] = { 461 { "hexio_read", hexio_read }, 462 { "hexio_write", hexio_write }, 463 { "hexio_get_int", hexio_get_int }, 464 { "hexio_render_int16", hexio_render_int16 }, 465 { "hexio_render_int32", hexio_render_int32 }, 466 { "utf8_to_utf8", utf8_to_utf8 }, 467 { "read_fts3varint", read_fts3varint }, 468 { "make_fts3record", make_fts3record }, 469 }; 470 int i; 471 for(i=0; i