| | @@ -120,13 +120,10 @@ |
| 120 | 120 | #include <math.h> |
| 121 | 121 | #include "sqlite3.h" |
| 122 | 122 | typedef sqlite3_int64 i64; |
| 123 | 123 | typedef sqlite3_uint64 u64; |
| 124 | 124 | typedef unsigned char u8; |
| 125 | | -#if SQLITE_USER_AUTHENTICATION |
| 126 | | -# include "sqlite3userauth.h" |
| 127 | | -#endif |
| 128 | 125 | #include <ctype.h> |
| 129 | 126 | #include <stdarg.h> |
| 130 | 127 | |
| 131 | 128 | #if !defined(_WIN32) && !defined(WIN32) |
| 132 | 129 | # include <signal.h> |
| | @@ -208,12 +205,10 @@ |
| 208 | 205 | # define unlink _unlink |
| 209 | 206 | # endif |
| 210 | 207 | # ifndef strdup |
| 211 | 208 | # define strdup _strdup |
| 212 | 209 | # endif |
| 213 | | -# undef popen |
| 214 | | -# define popen _popen |
| 215 | 210 | # undef pclose |
| 216 | 211 | # define pclose _pclose |
| 217 | 212 | # endif |
| 218 | 213 | #else |
| 219 | 214 | /* Make sure isatty() has a prototype. */ |
| | @@ -253,10 +248,370 @@ |
| 253 | 248 | /* string conversion routines only needed on Win32 */ |
| 254 | 249 | extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| 255 | 250 | extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); |
| 256 | 251 | #endif |
| 257 | 252 | |
| 253 | +/************************* Begin ../ext/misc/sqlite3_stdio.h ******************/ |
| 254 | +/* |
| 255 | +** 2024-09-24 |
| 256 | +** |
| 257 | +** The author disclaims copyright to this source code. In place of |
| 258 | +** a legal notice, here is a blessing: |
| 259 | +** |
| 260 | +** May you do good and not evil. |
| 261 | +** May you find forgiveness for yourself and forgive others. |
| 262 | +** May you share freely, never taking more than you give. |
| 263 | +** |
| 264 | +************************************************************************* |
| 265 | +** |
| 266 | +** This header file contains definitions of interfaces that provide |
| 267 | +** cross-platform I/O for UTF-8 content. |
| 268 | +** |
| 269 | +** On most platforms, the interfaces definitions in this file are |
| 270 | +** just #defines. For example sqlite3_fopen() is a macro that resolves |
| 271 | +** to the standard fopen() in the C-library. |
| 272 | +** |
| 273 | +** But Windows does not have a standard C-library, at least not one that |
| 274 | +** can handle UTF-8. So for windows build, the interfaces resolve to new |
| 275 | +** C-language routines contained in the separate sqlite3_stdio.c source file. |
| 276 | +** |
| 277 | +** So on all non-Windows platforms, simply #include this header file and |
| 278 | +** use the interfaces defined herein. Then to run your application on Windows, |
| 279 | +** also link in the accompanying sqlite3_stdio.c source file when compiling |
| 280 | +** to get compatible interfaces. |
| 281 | +*/ |
| 282 | +#ifndef _SQLITE3_STDIO_H_ |
| 283 | +#define _SQLITE3_STDIO_H_ 1 |
| 284 | +#ifdef _WIN32 |
| 285 | +/**** Definitions For Windows ****/ |
| 286 | +#include <stdio.h> |
| 287 | +#include <windows.h> |
| 288 | + |
| 289 | +FILE *sqlite3_fopen(const char *zFilename, const char *zMode); |
| 290 | +FILE *sqlite3_popen(const char *zCommand, const char *type); |
| 291 | +char *sqlite3_fgets(char *s, int size, FILE *stream); |
| 292 | +int sqlite3_fputs(const char *s, FILE *stream); |
| 293 | +int sqlite3_fprintf(FILE *stream, const char *format, ...); |
| 294 | +void sqlite3_fsetmode(FILE *stream, int mode); |
| 295 | + |
| 296 | + |
| 297 | +#else |
| 298 | +/**** Definitions For All Other Platforms ****/ |
| 299 | +#include <stdio.h> |
| 300 | +#define sqlite3_fopen fopen |
| 301 | +#define sqlite3_popen popen |
| 302 | +#define sqlite3_fgets fgets |
| 303 | +#define sqlite3_fputs fputs |
| 304 | +#define sqlite3_fprintf fprintf |
| 305 | +#define sqlite3_fsetmode(F,X) /*no-op*/ |
| 306 | + |
| 307 | +#endif |
| 308 | +#endif /* _SQLITE3_STDIO_H_ */ |
| 309 | + |
| 310 | +/************************* End ../ext/misc/sqlite3_stdio.h ********************/ |
| 311 | +/************************* Begin ../ext/misc/sqlite3_stdio.c ******************/ |
| 312 | +/* |
| 313 | +** 2024-09-24 |
| 314 | +** |
| 315 | +** The author disclaims copyright to this source code. In place of |
| 316 | +** a legal notice, here is a blessing: |
| 317 | +** |
| 318 | +** May you do good and not evil. |
| 319 | +** May you find forgiveness for yourself and forgive others. |
| 320 | +** May you share freely, never taking more than you give. |
| 321 | +** |
| 322 | +************************************************************************* |
| 323 | +** |
| 324 | +** Implementation of standard I/O interfaces for UTF-8 that are missing |
| 325 | +** on Windows. |
| 326 | +*/ |
| 327 | +#ifdef _WIN32 /* This file is a no-op on all platforms except Windows */ |
| 328 | +#ifndef _SQLITE3_STDIO_H_ |
| 329 | +/* #include "sqlite3_stdio.h" */ |
| 330 | +#endif |
| 331 | +#undef WIN32_LEAN_AND_MEAN |
| 332 | +#define WIN32_LEAN_AND_MEAN |
| 333 | +#include <windows.h> |
| 334 | +#include <stdlib.h> |
| 335 | +#include <string.h> |
| 336 | +#include <stdio.h> |
| 337 | +#include <assert.h> |
| 338 | +/* #include "sqlite3.h" */ |
| 339 | +#include <ctype.h> |
| 340 | +#include <stdarg.h> |
| 341 | +#include <io.h> |
| 342 | +#include <fcntl.h> |
| 343 | + |
| 344 | +/* |
| 345 | +** If the SQLITE_U8TEXT_ONLY option is defined, then use O_U8TEXT |
| 346 | +** when appropriate on all output. (Sometimes use O_BINARY when |
| 347 | +** rendering ASCII text in cases where NL-to-CRLF expansion would |
| 348 | +** not be correct.) |
| 349 | +** |
| 350 | +** If the SQLITE_U8TEXT_STDIO option is defined, then use O_U8TEXT |
| 351 | +** when appropriate when writing to stdout or stderr. Use O_BINARY |
| 352 | +** or O_TEXT (depending on things like the .mode and the .crlf setting |
| 353 | +** in the CLI, or other context clues in other applications) for all |
| 354 | +** other output channels. |
| 355 | +** |
| 356 | +** The default behavior, if neither of the above is defined is to |
| 357 | +** use O_U8TEXT when writing to the Windows console (or anything |
| 358 | +** else for which _isatty() returns true) and to use O_BINARY or O_TEXT |
| 359 | +** for all other output channels. |
| 360 | +*/ |
| 361 | +#if defined(SQLITE_U8TEXT_ONLY) |
| 362 | +# define UseWtextForOutput(fd) 1 |
| 363 | +# define UseWtextForInput(fd) 1 |
| 364 | +# define IsConsole(fd) _isatty(_fileno(fd)) |
| 365 | +#elif defined(SQLITE_U8TEXT_STDIO) |
| 366 | +# define UseWtextForOutput(fd) ((fd)==stdout || (fd)==stderr) |
| 367 | +# define UseWtextForInput(fd) ((fd)==stdin) |
| 368 | +# define IsConsole(fd) _isatty(_fileno(fd)) |
| 369 | +#else |
| 370 | +# define UseWtextForOutput(fd) _isatty(_fileno(fd)) |
| 371 | +# define UseWtextForInput(fd) _isatty(_fileno(fd)) |
| 372 | +# define IsConsole(fd) 1 |
| 373 | +#endif |
| 374 | + |
| 375 | +/* |
| 376 | +** Global variables determine if simulated O_BINARY mode is to be |
| 377 | +** used for stdout or other, respectively. Simulated O_BINARY mode |
| 378 | +** means the mode is usually O_BINARY, but switches to O_U8TEXT for |
| 379 | +** unicode characters U+0080 or greater (any character that has a |
| 380 | +** multi-byte representation in UTF-8). This is the only way we |
| 381 | +** have found to render Unicode characters on a Windows console while |
| 382 | +** at the same time avoiding undesirable \n to \r\n translation. |
| 383 | +*/ |
| 384 | +static int simBinaryStdout = 0; |
| 385 | +static int simBinaryOther = 0; |
| 386 | + |
| 387 | + |
| 388 | +/* |
| 389 | +** Determine if simulated binary mode should be used for output to fd |
| 390 | +*/ |
| 391 | +static int UseBinaryWText(FILE *fd){ |
| 392 | + if( fd==stdout || fd==stderr ){ |
| 393 | + return simBinaryStdout; |
| 394 | + }else{ |
| 395 | + return simBinaryOther; |
| 396 | + } |
| 397 | +} |
| 398 | + |
| 399 | + |
| 400 | +/* |
| 401 | +** Work-alike for the fopen() routine from the standard C library. |
| 402 | +*/ |
| 403 | +FILE *sqlite3_fopen(const char *zFilename, const char *zMode){ |
| 404 | + FILE *fp = 0; |
| 405 | + wchar_t *b1, *b2; |
| 406 | + int sz1, sz2; |
| 407 | + |
| 408 | + sz1 = (int)strlen(zFilename); |
| 409 | + sz2 = (int)strlen(zMode); |
| 410 | + b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) ); |
| 411 | + b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) ); |
| 412 | + if( b1 && b2 ){ |
| 413 | + sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1); |
| 414 | + b1[sz1] = 0; |
| 415 | + sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); |
| 416 | + b2[sz2] = 0; |
| 417 | + fp = _wfopen(b1, b2); |
| 418 | + } |
| 419 | + sqlite3_free(b1); |
| 420 | + sqlite3_free(b2); |
| 421 | + simBinaryOther = 0; |
| 422 | + return fp; |
| 423 | +} |
| 424 | + |
| 425 | + |
| 426 | +/* |
| 427 | +** Work-alike for the popen() routine from the standard C library. |
| 428 | +*/ |
| 429 | +FILE *sqlite3_popen(const char *zCommand, const char *zMode){ |
| 430 | + FILE *fp = 0; |
| 431 | + wchar_t *b1, *b2; |
| 432 | + int sz1, sz2; |
| 433 | + |
| 434 | + sz1 = (int)strlen(zCommand); |
| 435 | + sz2 = (int)strlen(zMode); |
| 436 | + b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) ); |
| 437 | + b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) ); |
| 438 | + if( b1 && b2 ){ |
| 439 | + sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1); |
| 440 | + b1[sz1] = 0; |
| 441 | + sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); |
| 442 | + b2[sz2] = 0; |
| 443 | + fp = _wpopen(b1, b2); |
| 444 | + } |
| 445 | + sqlite3_free(b1); |
| 446 | + sqlite3_free(b2); |
| 447 | + return fp; |
| 448 | +} |
| 449 | + |
| 450 | +/* |
| 451 | +** Work-alike for fgets() from the standard C library. |
| 452 | +*/ |
| 453 | +char *sqlite3_fgets(char *buf, int sz, FILE *in){ |
| 454 | + if( UseWtextForInput(in) ){ |
| 455 | + /* When reading from the command-prompt in Windows, it is necessary |
| 456 | + ** to use _O_WTEXT input mode to read UTF-16 characters, then translate |
| 457 | + ** that into UTF-8. Otherwise, non-ASCII characters all get translated |
| 458 | + ** into '?'. |
| 459 | + */ |
| 460 | + wchar_t *b1 = sqlite3_malloc( sz*sizeof(wchar_t) ); |
| 461 | + if( b1==0 ) return 0; |
| 462 | +#ifndef SQLITE_USE_STDIO_FOR_CONSOLE |
| 463 | + DWORD nRead = 0; |
| 464 | + if( IsConsole(in) |
| 465 | + && ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), b1, sz, &nRead, 0) |
| 466 | + ){ |
| 467 | + b1[nRead] = 0; |
| 468 | + }else |
| 469 | +#endif |
| 470 | + { |
| 471 | + _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT); |
| 472 | + if( fgetws(b1, sz/4, in)==0 ){ |
| 473 | + sqlite3_free(b1); |
| 474 | + return 0; |
| 475 | + } |
| 476 | + } |
| 477 | + WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0); |
| 478 | + sqlite3_free(b1); |
| 479 | + return buf; |
| 480 | + }else{ |
| 481 | + /* Reading from a file or other input source, just read bytes without |
| 482 | + ** any translation. */ |
| 483 | + return fgets(buf, sz, in); |
| 484 | + } |
| 485 | +} |
| 486 | + |
| 487 | +/* |
| 488 | +** Send ASCII text as O_BINARY. But for Unicode characters U+0080 and |
| 489 | +** greater, switch to O_U8TEXT. |
| 490 | +*/ |
| 491 | +static void piecemealOutput(wchar_t *b1, int sz, FILE *out){ |
| 492 | + int i; |
| 493 | + wchar_t c; |
| 494 | + while( sz>0 ){ |
| 495 | + for(i=0; i<sz && b1[i]>=0x80; i++){} |
| 496 | + if( i>0 ){ |
| 497 | + c = b1[i]; |
| 498 | + b1[i] = 0; |
| 499 | + fflush(out); |
| 500 | + _setmode(_fileno(out), _O_U8TEXT); |
| 501 | + fputws(b1, out); |
| 502 | + fflush(out); |
| 503 | + b1 += i; |
| 504 | + b1[0] = c; |
| 505 | + sz -= i; |
| 506 | + }else{ |
| 507 | + fflush(out); |
| 508 | + _setmode(_fileno(out), _O_TEXT); |
| 509 | + _setmode(_fileno(out), _O_BINARY); |
| 510 | + fwrite(&b1[0], 1, 1, out); |
| 511 | + for(i=1; i<sz && b1[i]<0x80; i++){ |
| 512 | + fwrite(&b1[i], 1, 1, out); |
| 513 | + } |
| 514 | + fflush(out); |
| 515 | + _setmode(_fileno(out), _O_U8TEXT); |
| 516 | + b1 += i; |
| 517 | + sz -= i; |
| 518 | + } |
| 519 | + } |
| 520 | +} |
| 521 | + |
| 522 | +/* |
| 523 | +** Work-alike for fputs() from the standard C library. |
| 524 | +*/ |
| 525 | +int sqlite3_fputs(const char *z, FILE *out){ |
| 526 | + if( !UseWtextForOutput(out) ){ |
| 527 | + /* Writing to a file or other destination, just write bytes without |
| 528 | + ** any translation. */ |
| 529 | + return fputs(z, out); |
| 530 | + }else{ |
| 531 | + /* One must use UTF16 in order to get unicode support when writing |
| 532 | + ** to the console on Windows. |
| 533 | + */ |
| 534 | + int sz = (int)strlen(z); |
| 535 | + wchar_t *b1 = sqlite3_malloc( (sz+1)*sizeof(wchar_t) ); |
| 536 | + if( b1==0 ) return 0; |
| 537 | + sz = MultiByteToWideChar(CP_UTF8, 0, z, sz, b1, sz); |
| 538 | + b1[sz] = 0; |
| 539 | + |
| 540 | +#ifndef SQLITE_STDIO_FOR_CONSOLE |
| 541 | + DWORD nWr = 0; |
| 542 | + if( IsConsole(out) |
| 543 | + && WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),b1,sz,&nWr,0) |
| 544 | + ){ |
| 545 | + /* If writing to the console, then the WriteConsoleW() is all we |
| 546 | + ** need to do. */ |
| 547 | + }else |
| 548 | +#endif |
| 549 | + { |
| 550 | + /* For non-console I/O, or if SQLITE_USE_STDIO_FOR_CONSOLE is defined |
| 551 | + ** then write using the standard library. */ |
| 552 | + _setmode(_fileno(out), _O_U8TEXT); |
| 553 | + if( UseBinaryWText(out) ){ |
| 554 | + piecemealOutput(b1, sz, out); |
| 555 | + }else{ |
| 556 | + fputws(b1, out); |
| 557 | + } |
| 558 | + } |
| 559 | + sqlite3_free(b1); |
| 560 | + return 0; |
| 561 | + } |
| 562 | +} |
| 563 | + |
| 564 | + |
| 565 | +/* |
| 566 | +** Work-alike for fprintf() from the standard C library. |
| 567 | +*/ |
| 568 | +int sqlite3_fprintf(FILE *out, const char *zFormat, ...){ |
| 569 | + int rc; |
| 570 | + if( UseWtextForOutput(out) ){ |
| 571 | + /* When writing to the command-prompt in Windows, it is necessary |
| 572 | + ** to use _O_WTEXT input mode and write UTF-16 characters. |
| 573 | + */ |
| 574 | + char *z; |
| 575 | + va_list ap; |
| 576 | + |
| 577 | + va_start(ap, zFormat); |
| 578 | + z = sqlite3_vmprintf(zFormat, ap); |
| 579 | + va_end(ap); |
| 580 | + sqlite3_fputs(z, out); |
| 581 | + rc = (int)strlen(z); |
| 582 | + sqlite3_free(z); |
| 583 | + }else{ |
| 584 | + /* Writing to a file or other destination, just write bytes without |
| 585 | + ** any translation. */ |
| 586 | + va_list ap; |
| 587 | + va_start(ap, zFormat); |
| 588 | + rc = vfprintf(out, zFormat, ap); |
| 589 | + va_end(ap); |
| 590 | + } |
| 591 | + return rc; |
| 592 | +} |
| 593 | + |
| 594 | +/* |
| 595 | +** Set the mode for an output stream. mode argument is typically _O_BINARY or |
| 596 | +** _O_TEXT. |
| 597 | +*/ |
| 598 | +void sqlite3_fsetmode(FILE *fp, int mode){ |
| 599 | + if( !UseWtextForOutput(fp) ){ |
| 600 | + fflush(fp); |
| 601 | + _setmode(_fileno(fp), mode); |
| 602 | + }else if( fp==stdout || fp==stderr ){ |
| 603 | + simBinaryStdout = (mode==_O_BINARY); |
| 604 | + }else{ |
| 605 | + simBinaryOther = (mode==_O_BINARY); |
| 606 | + } |
| 607 | +} |
| 608 | + |
| 609 | +#endif /* defined(_WIN32) */ |
| 610 | + |
| 611 | +/************************* End ../ext/misc/sqlite3_stdio.c ********************/ |
| 612 | + |
| 258 | 613 | /* Use console I/O package as a direct INCLUDE. */ |
| 259 | 614 | #define SQLITE_INTERNAL_LINKAGE static |
| 260 | 615 | |
| 261 | 616 | #ifdef SQLITE_SHELL_FIDDLE |
| 262 | 617 | /* Deselect most features from the console I/O package for Fiddle. */ |
| | @@ -264,1118 +619,13 @@ |
| 264 | 619 | # define SQLITE_CIO_NO_CLASSIFY |
| 265 | 620 | # define SQLITE_CIO_NO_TRANSLATE |
| 266 | 621 | # define SQLITE_CIO_NO_SETMODE |
| 267 | 622 | # define SQLITE_CIO_NO_FLUSH |
| 268 | 623 | #endif |
| 269 | | -/************************* Begin ../ext/consio/console_io.h ******************/ |
| 270 | | -/* |
| 271 | | -** 2023 November 1 |
| 272 | | -** |
| 273 | | -** The author disclaims copyright to this source code. In place of |
| 274 | | -** a legal notice, here is a blessing: |
| 275 | | -** |
| 276 | | -** May you do good and not evil. |
| 277 | | -** May you find forgiveness for yourself and forgive others. |
| 278 | | -** May you share freely, never taking more than you give. |
| 279 | | -** |
| 280 | | -******************************************************************************** |
| 281 | | -** This file exposes various interfaces used for console and other I/O |
| 282 | | -** by the SQLite project command-line tools. These interfaces are used |
| 283 | | -** at either source conglomeration time, compilation time, or run time. |
| 284 | | -** This source provides for either inclusion into conglomerated, |
| 285 | | -** "single-source" forms or separate compilation then linking. |
| 286 | | -** |
| 287 | | -** Platform dependencies are "hidden" here by various stratagems so |
| 288 | | -** that, provided certain conditions are met, the programs using this |
| 289 | | -** source or object code compiled from it need no explicit conditional |
| 290 | | -** compilation in their source for their console and stream I/O. |
| 291 | | -** |
| 292 | | -** The symbols and functionality exposed here are not a public API. |
| 293 | | -** This code may change in tandem with other project code as needed. |
| 294 | | -** |
| 295 | | -** When this .h file and its companion .c are directly incorporated into |
| 296 | | -** a source conglomeration (such as shell.c), the preprocessor symbol |
| 297 | | -** CIO_WIN_WC_XLATE is defined as 0 or 1, reflecting whether console I/O |
| 298 | | -** translation for Windows is effected for the build. |
| 299 | | -*/ |
| 300 | | -#define HAVE_CONSOLE_IO_H 1 |
| 301 | | -#ifndef SQLITE_INTERNAL_LINKAGE |
| 302 | | -# define SQLITE_INTERNAL_LINKAGE extern /* external to translation unit */ |
| 303 | | -# include <stdio.h> |
| 304 | | -#else |
| 305 | | -# define SHELL_NO_SYSINC /* Better yet, modify mkshellc.tcl for this. */ |
| 306 | | -#endif |
| 307 | | - |
| 308 | | -#ifndef SQLITE3_H |
| 309 | | -/* # include "sqlite3.h" */ |
| 310 | | -#endif |
| 311 | | - |
| 312 | | -#ifndef SQLITE_CIO_NO_CLASSIFY |
| 313 | | - |
| 314 | | -/* Define enum for use with following function. */ |
| 315 | | -typedef enum StreamsAreConsole { |
| 316 | | - SAC_NoConsole = 0, |
| 317 | | - SAC_InConsole = 1, SAC_OutConsole = 2, SAC_ErrConsole = 4, |
| 318 | | - SAC_AnyConsole = 0x7 |
| 319 | | -} StreamsAreConsole; |
| 320 | | - |
| 321 | | -/* |
| 322 | | -** Classify the three standard I/O streams according to whether |
| 323 | | -** they are connected to a console attached to the process. |
| 324 | | -** |
| 325 | | -** Returns the bit-wise OR of SAC_{In,Out,Err}Console values, |
| 326 | | -** or SAC_NoConsole if none of the streams reaches a console. |
| 327 | | -** |
| 328 | | -** This function should be called before any I/O is done with |
| 329 | | -** the given streams. As a side-effect, the given inputs are |
| 330 | | -** recorded so that later I/O operations on them may be done |
| 331 | | -** differently than the C library FILE* I/O would be done, |
| 332 | | -** iff the stream is used for the I/O functions that follow, |
| 333 | | -** and to support the ones that use an implicit stream. |
| 334 | | -** |
| 335 | | -** On some platforms, stream or console mode alteration (aka |
| 336 | | -** "Setup") may be made which is undone by consoleRestore(). |
| 337 | | -*/ |
| 338 | | -SQLITE_INTERNAL_LINKAGE StreamsAreConsole |
| 339 | | -consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ); |
| 340 | | -/* A usual call for convenience: */ |
| 341 | | -#define SQLITE_STD_CONSOLE_INIT() consoleClassifySetup(stdin,stdout,stderr) |
| 342 | | - |
| 343 | | -/* |
| 344 | | -** After an initial call to consoleClassifySetup(...), renew |
| 345 | | -** the same setup it effected. (A call not after is an error.) |
| 346 | | -** This will restore state altered by consoleRestore(); |
| 347 | | -** |
| 348 | | -** Applications which run an inferior (child) process which |
| 349 | | -** inherits the same I/O streams may call this function after |
| 350 | | -** such a process exits to guard against console mode changes. |
| 351 | | -*/ |
| 352 | | -SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void); |
| 353 | | - |
| 354 | | -/* |
| 355 | | -** Undo any side-effects left by consoleClassifySetup(...). |
| 356 | | -** |
| 357 | | -** This should be called after consoleClassifySetup() and |
| 358 | | -** before the process terminates normally. It is suitable |
| 359 | | -** for use with the atexit() C library procedure. After |
| 360 | | -** this call, no console I/O should be done until one of |
| 361 | | -** console{Classify or Renew}Setup(...) is called again. |
| 362 | | -** |
| 363 | | -** Applications which run an inferior (child) process that |
| 364 | | -** inherits the same I/O streams might call this procedure |
| 365 | | -** before so that said process will have a console setup |
| 366 | | -** however users have configured it or come to expect. |
| 367 | | -*/ |
| 368 | | -SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ); |
| 369 | | - |
| 370 | | -#else /* defined(SQLITE_CIO_NO_CLASSIFY) */ |
| 371 | | -# define consoleClassifySetup(i,o,e) |
| 372 | | -# define consoleRenewSetup() |
| 373 | | -# define consoleRestore() |
| 374 | | -#endif /* defined(SQLITE_CIO_NO_CLASSIFY) */ |
| 375 | | - |
| 376 | | -#ifndef SQLITE_CIO_NO_REDIRECT |
| 377 | | -/* |
| 378 | | -** Set stream to be used for the functions below which write |
| 379 | | -** to "the designated X stream", where X is Output or Error. |
| 380 | | -** Returns the previous value. |
| 381 | | -** |
| 382 | | -** Alternatively, pass the special value, invalidFileStream, |
| 383 | | -** to get the designated stream value without setting it. |
| 384 | | -** |
| 385 | | -** Before the designated streams are set, they default to |
| 386 | | -** those passed to consoleClassifySetup(...), and before |
| 387 | | -** that is called they default to stdout and stderr. |
| 388 | | -** |
| 389 | | -** It is error to close a stream so designated, then, without |
| 390 | | -** designating another, use the corresponding {o,e}Emit(...). |
| 391 | | -*/ |
| 392 | | -SQLITE_INTERNAL_LINKAGE FILE *invalidFileStream; |
| 393 | | -SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf); |
| 394 | | -# ifdef CONSIO_SET_ERROR_STREAM |
| 395 | | -SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf); |
| 396 | | -# endif |
| 397 | | -#else |
| 398 | | -# define setOutputStream(pf) |
| 399 | | -# define setErrorStream(pf) |
| 400 | | -#endif /* !defined(SQLITE_CIO_NO_REDIRECT) */ |
| 401 | | - |
| 402 | | -#ifndef SQLITE_CIO_NO_TRANSLATE |
| 403 | | -/* |
| 404 | | -** Emit output like fprintf(). If the output is going to the |
| 405 | | -** console and translation from UTF-8 is necessary, perform |
| 406 | | -** the needed translation. Otherwise, write formatted output |
| 407 | | -** to the provided stream almost as-is, possibly with newline |
| 408 | | -** translation as specified by set{Binary,Text}Mode(). |
| 409 | | -*/ |
| 410 | | -SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...); |
| 411 | | -/* Like fPrintfUtf8 except stream is always the designated output. */ |
| 412 | | -SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...); |
| 413 | | -/* Like fPrintfUtf8 except stream is always the designated error. */ |
| 414 | | -SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...); |
| 415 | | - |
| 416 | | -/* |
| 417 | | -** Emit output like fputs(). If the output is going to the |
| 418 | | -** console and translation from UTF-8 is necessary, perform |
| 419 | | -** the needed translation. Otherwise, write given text to the |
| 420 | | -** provided stream almost as-is, possibly with newline |
| 421 | | -** translation as specified by set{Binary,Text}Mode(). |
| 422 | | -*/ |
| 423 | | -SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO); |
| 424 | | -/* Like fPutsUtf8 except stream is always the designated output. */ |
| 425 | | -SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z); |
| 426 | | -/* Like fPutsUtf8 except stream is always the designated error. */ |
| 427 | | -SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z); |
| 428 | | - |
| 429 | | -/* |
| 430 | | -** Emit output like fPutsUtf8(), except that the length of the |
| 431 | | -** accepted char or character sequence is limited by nAccept. |
| 432 | | -** |
| 433 | | -** Returns the number of accepted char values. |
| 434 | | -*/ |
| 435 | | -#ifdef CONSIO_SPUTB |
| 436 | | -SQLITE_INTERNAL_LINKAGE int |
| 437 | | -fPutbUtf8(FILE *pfOut, const char *cBuf, int nAccept); |
| 438 | | -/* Like fPutbUtf8 except stream is always the designated output. */ |
| 439 | | -#endif |
| 440 | | -SQLITE_INTERNAL_LINKAGE int |
| 441 | | -oPutbUtf8(const char *cBuf, int nAccept); |
| 442 | | -/* Like fPutbUtf8 except stream is always the designated error. */ |
| 443 | | -#ifdef CONSIO_EPUTB |
| 444 | | -SQLITE_INTERNAL_LINKAGE int |
| 445 | | -ePutbUtf8(const char *cBuf, int nAccept); |
| 446 | | -#endif |
| 447 | | - |
| 448 | | -/* |
| 449 | | -** Flush the given output stream. Return non-zero for success, else 0. |
| 450 | | -*/ |
| 451 | | -#if !defined(SQLITE_CIO_NO_FLUSH) && !defined(SQLITE_CIO_NO_SETMODE) |
| 452 | | -SQLITE_INTERNAL_LINKAGE int |
| 453 | | -fFlushBuffer(FILE *pfOut); |
| 454 | | -#endif |
| 455 | | - |
| 456 | | -/* |
| 457 | | -** Collect input like fgets(...) with special provisions for input |
| 458 | | -** from the console on such platforms as require same. Newline |
| 459 | | -** translation may be done as set by set{Binary,Text}Mode(). |
| 460 | | -** As a convenience, pfIn==NULL is treated as stdin. |
| 461 | | -*/ |
| 462 | | -SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn); |
| 463 | | -/* Like fGetsUtf8 except stream is always the designated input. */ |
| 464 | | -/* SQLITE_INTERNAL_LINKAGE char* iGetsUtf8(char *cBuf, int ncMax); */ |
| 465 | | - |
| 466 | | -#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */ |
| 467 | | - |
| 468 | | -#ifndef SQLITE_CIO_NO_SETMODE |
| 469 | | -/* |
| 470 | | -** Set given stream for binary mode, where newline translation is |
| 471 | | -** not done, or for text mode where, for some platforms, newlines |
| 472 | | -** are translated to the platform's conventional char sequence. |
| 473 | | -** If bFlush true, flush the stream. |
| 474 | | -** |
| 475 | | -** An additional side-effect is that if the stream is one passed |
| 476 | | -** to consoleClassifySetup() as an output, it is flushed first. |
| 477 | | -** |
| 478 | | -** Note that binary/text mode has no effect on console I/O |
| 479 | | -** translation. On all platforms, newline to the console starts |
| 480 | | -** a new line and CR,LF chars from the console become a newline. |
| 481 | | -*/ |
| 482 | | -SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *, short bFlush); |
| 483 | | -SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *, short bFlush); |
| 484 | | -#endif |
| 485 | | - |
| 486 | | -#ifdef SQLITE_CIO_PROMPTED_IN |
| 487 | | -typedef struct Prompts { |
| 488 | | - int numPrompts; |
| 489 | | - const char **azPrompts; |
| 490 | | -} Prompts; |
| 491 | | - |
| 492 | | -/* |
| 493 | | -** Macros for use of a line editor. |
| 494 | | -** |
| 495 | | -** The following macros define operations involving use of a |
| 496 | | -** line-editing library or simple console interaction. |
| 497 | | -** A "T" argument is a text (char *) buffer or filename. |
| 498 | | -** A "N" argument is an integer. |
| 499 | | -** |
| 500 | | -** SHELL_ADD_HISTORY(T) // Record text as line(s) of history. |
| 501 | | -** SHELL_READ_HISTORY(T) // Read history from file named by T. |
| 502 | | -** SHELL_WRITE_HISTORY(T) // Write history to file named by T. |
| 503 | | -** SHELL_STIFLE_HISTORY(N) // Limit history to N entries. |
| 504 | | -** |
| 505 | | -** A console program which does interactive console input is |
| 506 | | -** expected to call: |
| 507 | | -** SHELL_READ_HISTORY(T) before collecting such input; |
| 508 | | -** SHELL_ADD_HISTORY(T) as record-worthy input is taken; |
| 509 | | -** SHELL_STIFLE_HISTORY(N) after console input ceases; then |
| 510 | | -** SHELL_WRITE_HISTORY(T) before the program exits. |
| 511 | | -*/ |
| 512 | | - |
| 513 | | -/* |
| 514 | | -** Retrieve a single line of input text from an input stream. |
| 515 | | -** |
| 516 | | -** If pfIn is the input stream passed to consoleClassifySetup(), |
| 517 | | -** and azPrompt is not NULL, then a prompt is issued before the |
| 518 | | -** line is collected, as selected by the isContinuation flag. |
| 519 | | -** Array azPrompt[{0,1}] holds the {main,continuation} prompt. |
| 520 | | -** |
| 521 | | -** If zBufPrior is not NULL then it is a buffer from a prior |
| 522 | | -** call to this routine that can be reused, or will be freed. |
| 523 | | -** |
| 524 | | -** The result is stored in space obtained from malloc() and |
| 525 | | -** must either be freed by the caller or else passed back to |
| 526 | | -** this function as zBufPrior for reuse. |
| 527 | | -** |
| 528 | | -** This function may call upon services of a line-editing |
| 529 | | -** library to interactively collect line edited input. |
| 530 | | -*/ |
| 531 | | -SQLITE_INTERNAL_LINKAGE char * |
| 532 | | -shellGetLine(FILE *pfIn, char *zBufPrior, int nLen, |
| 533 | | - short isContinuation, Prompts azPrompt); |
| 534 | | -#endif /* defined(SQLITE_CIO_PROMPTED_IN) */ |
| 535 | | -/* |
| 536 | | -** TBD: Define an interface for application(s) to generate |
| 537 | | -** completion candidates for use by the line-editor. |
| 538 | | -** |
| 539 | | -** This may be premature; the CLI is the only application |
| 540 | | -** that does this. Yet, getting line-editing melded into |
| 541 | | -** console I/O is desirable because a line-editing library |
| 542 | | -** may have to establish console operating mode, possibly |
| 543 | | -** in a way that interferes with the above functionality. |
| 544 | | -*/ |
| 545 | | - |
| 546 | | -#if !(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE)) |
| 547 | | -/* Skip over as much z[] input char sequence as is valid UTF-8, |
| 548 | | -** limited per nAccept char's or whole characters and containing |
| 549 | | -** no char cn such that ((1<<cn) & ccm)!=0. On return, the |
| 550 | | -** sequence z:return (inclusive:exclusive) is validated UTF-8. |
| 551 | | -** Limit: nAccept>=0 => char count, nAccept<0 => character |
| 552 | | - */ |
| 553 | | -SQLITE_INTERNAL_LINKAGE const char* |
| 554 | | -zSkipValidUtf8(const char *z, int nAccept, long ccm); |
| 555 | | - |
| 556 | | -#endif |
| 557 | | - |
| 558 | | -/************************* End ../ext/consio/console_io.h ********************/ |
| 559 | | -/************************* Begin ../ext/consio/console_io.c ******************/ |
| 560 | | -/* |
| 561 | | -** 2023 November 4 |
| 562 | | -** |
| 563 | | -** The author disclaims copyright to this source code. In place of |
| 564 | | -** a legal notice, here is a blessing: |
| 565 | | -** |
| 566 | | -** May you do good and not evil. |
| 567 | | -** May you find forgiveness for yourself and forgive others. |
| 568 | | -** May you share freely, never taking more than you give. |
| 569 | | -** |
| 570 | | -******************************************************************************** |
| 571 | | -** This file implements various interfaces used for console and stream I/O |
| 572 | | -** by the SQLite project command-line tools, as explained in console_io.h . |
| 573 | | -** Functions prefixed by "SQLITE_INTERNAL_LINKAGE" behave as described there. |
| 574 | | -*/ |
| 575 | | - |
| 576 | | -#ifndef SQLITE_CDECL |
| 577 | | -# define SQLITE_CDECL |
| 578 | | -#endif |
| 579 | | - |
| 580 | | -#ifndef SHELL_NO_SYSINC |
| 581 | | -# include <stdarg.h> |
| 582 | | -# include <string.h> |
| 583 | | -# include <stdlib.h> |
| 584 | | -# include <limits.h> |
| 585 | | -# include <assert.h> |
| 586 | | -/* # include "sqlite3.h" */ |
| 587 | | -#endif |
| 588 | | -#ifndef HAVE_CONSOLE_IO_H |
| 589 | | -# include "console_io.h" |
| 590 | | -#endif |
| 591 | | -#if defined(_MSC_VER) |
| 592 | | -# pragma warning(disable : 4204) |
| 593 | | -#endif |
| 594 | | - |
| 595 | | -#ifndef SQLITE_CIO_NO_TRANSLATE |
| 596 | | -# if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT |
| 597 | | -# ifndef SHELL_NO_SYSINC |
| 598 | | -# include <io.h> |
| 599 | | -# include <fcntl.h> |
| 600 | | -# undef WIN32_LEAN_AND_MEAN |
| 601 | | -# define WIN32_LEAN_AND_MEAN |
| 602 | | -# include <windows.h> |
| 603 | | -# endif |
| 604 | | -# define CIO_WIN_WC_XLATE 1 /* Use WCHAR Windows APIs for console I/O */ |
| 605 | | -# else |
| 606 | | -# ifndef SHELL_NO_SYSINC |
| 607 | | -# include <unistd.h> |
| 608 | | -# endif |
| 609 | | -# define CIO_WIN_WC_XLATE 0 /* Use plain C library stream I/O at console */ |
| 610 | | -# endif |
| 611 | | -#else |
| 612 | | -# define CIO_WIN_WC_XLATE 0 /* Not exposing translation routines at all */ |
| 613 | | -#endif |
| 614 | | - |
| 615 | | -#if CIO_WIN_WC_XLATE |
| 616 | | -static HANDLE handleOfFile(FILE *pf){ |
| 617 | | - int fileDesc = _fileno(pf); |
| 618 | | - union { intptr_t osfh; HANDLE fh; } fid = { |
| 619 | | - (fileDesc>=0)? _get_osfhandle(fileDesc) : (intptr_t)INVALID_HANDLE_VALUE |
| 620 | | - }; |
| 621 | | - return fid.fh; |
| 622 | | -} |
| 623 | | -#endif |
| 624 | | - |
| 625 | | -#ifndef SQLITE_CIO_NO_TRANSLATE |
| 626 | | -typedef struct PerStreamTags { |
| 627 | | -# if CIO_WIN_WC_XLATE |
| 628 | | - HANDLE hx; |
| 629 | | - DWORD consMode; |
| 630 | | - char acIncomplete[4]; |
| 631 | | -# else |
| 632 | | - short reachesConsole; |
| 633 | | -# endif |
| 634 | | - FILE *pf; |
| 635 | | -} PerStreamTags; |
| 636 | | - |
| 637 | | -/* Define NULL-like value for things which can validly be 0. */ |
| 638 | | -# define SHELL_INVALID_FILE_PTR ((FILE *)~0) |
| 639 | | -# if CIO_WIN_WC_XLATE |
| 640 | | -# define SHELL_INVALID_CONS_MODE 0xFFFF0000 |
| 641 | | -# endif |
| 642 | | - |
| 643 | | -# if CIO_WIN_WC_XLATE |
| 644 | | -# define PST_INITIALIZER { INVALID_HANDLE_VALUE, SHELL_INVALID_CONS_MODE, \ |
| 645 | | - {0,0,0,0}, SHELL_INVALID_FILE_PTR } |
| 646 | | -# else |
| 647 | | -# define PST_INITIALIZER { 0, SHELL_INVALID_FILE_PTR } |
| 648 | | -# endif |
| 649 | | - |
| 650 | | -/* Quickly say whether a known output is going to the console. */ |
| 651 | | -# if CIO_WIN_WC_XLATE |
| 652 | | -static short pstReachesConsole(PerStreamTags *ppst){ |
| 653 | | - return (ppst->hx != INVALID_HANDLE_VALUE); |
| 654 | | -} |
| 655 | | -# else |
| 656 | | -# define pstReachesConsole(ppst) 0 |
| 657 | | -# endif |
| 658 | | - |
| 659 | | -# if CIO_WIN_WC_XLATE |
| 660 | | -static void restoreConsoleArb(PerStreamTags *ppst){ |
| 661 | | - if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode); |
| 662 | | -} |
| 663 | | -# else |
| 664 | | -# define restoreConsoleArb(ppst) |
| 665 | | -# endif |
| 666 | | - |
| 667 | | -/* Say whether FILE* appears to be a console, collect associated info. */ |
| 668 | | -static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){ |
| 669 | | -# if CIO_WIN_WC_XLATE |
| 670 | | - short rv = 0; |
| 671 | | - DWORD dwCM = SHELL_INVALID_CONS_MODE; |
| 672 | | - HANDLE fh = handleOfFile(pf); |
| 673 | | - ppst->pf = pf; |
| 674 | | - if( INVALID_HANDLE_VALUE != fh ){ |
| 675 | | - rv = (GetFileType(fh) == FILE_TYPE_CHAR && GetConsoleMode(fh,&dwCM)); |
| 676 | | - } |
| 677 | | - ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE; |
| 678 | | - ppst->consMode = dwCM; |
| 679 | | - return rv; |
| 680 | | -# else |
| 681 | | - ppst->pf = pf; |
| 682 | | - ppst->reachesConsole = ( (short)isatty(fileno(pf)) ); |
| 683 | | - return ppst->reachesConsole; |
| 684 | | -# endif |
| 685 | | -} |
| 686 | | - |
| 687 | | -# ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING |
| 688 | | -# define ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4) |
| 689 | | -# endif |
| 690 | | - |
| 691 | | -# if CIO_WIN_WC_XLATE |
| 692 | | -/* Define console modes for use with the Windows Console API. */ |
| 693 | | -# define SHELL_CONI_MODE \ |
| 694 | | - (ENABLE_ECHO_INPUT | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | 0x80 \ |
| 695 | | - | ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT) |
| 696 | | -# define SHELL_CONO_MODE (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT \ |
| 697 | | - | ENABLE_VIRTUAL_TERMINAL_PROCESSING) |
| 698 | | -# endif |
| 699 | | - |
| 700 | | -typedef struct ConsoleInfo { |
| 701 | | - PerStreamTags pstSetup[3]; |
| 702 | | - PerStreamTags pstDesignated[3]; |
| 703 | | - StreamsAreConsole sacSetup; |
| 704 | | -} ConsoleInfo; |
| 705 | | - |
| 706 | | -static short isValidStreamInfo(PerStreamTags *ppst){ |
| 707 | | - return (ppst->pf != SHELL_INVALID_FILE_PTR); |
| 708 | | -} |
| 709 | | - |
| 710 | | -static ConsoleInfo consoleInfo = { |
| 711 | | - { /* pstSetup */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER }, |
| 712 | | - { /* pstDesignated[] */ PST_INITIALIZER, PST_INITIALIZER, PST_INITIALIZER }, |
| 713 | | - SAC_NoConsole /* sacSetup */ |
| 714 | | -}; |
| 715 | | - |
| 716 | | -SQLITE_INTERNAL_LINKAGE FILE* invalidFileStream = (FILE *)~0; |
| 717 | | - |
| 718 | | -# if CIO_WIN_WC_XLATE |
| 719 | | -static void maybeSetupAsConsole(PerStreamTags *ppst, short odir){ |
| 720 | | - if( pstReachesConsole(ppst) ){ |
| 721 | | - DWORD cm = odir? SHELL_CONO_MODE : SHELL_CONI_MODE; |
| 722 | | - SetConsoleMode(ppst->hx, cm); |
| 723 | | - } |
| 724 | | -} |
| 725 | | -# else |
| 726 | | -# define maybeSetupAsConsole(ppst,odir) |
| 727 | | -# endif |
| 728 | | - |
| 729 | | -SQLITE_INTERNAL_LINKAGE void consoleRenewSetup(void){ |
| 730 | | -# if CIO_WIN_WC_XLATE |
| 731 | | - int ix = 0; |
| 732 | | - while( ix < 6 ){ |
| 733 | | - PerStreamTags *ppst = (ix<3)? |
| 734 | | - &consoleInfo.pstSetup[ix] : &consoleInfo.pstDesignated[ix-3]; |
| 735 | | - maybeSetupAsConsole(ppst, (ix % 3)>0); |
| 736 | | - ++ix; |
| 737 | | - } |
| 738 | | -# endif |
| 739 | | -} |
| 740 | | - |
| 741 | | -SQLITE_INTERNAL_LINKAGE StreamsAreConsole |
| 742 | | -consoleClassifySetup( FILE *pfIn, FILE *pfOut, FILE *pfErr ){ |
| 743 | | - StreamsAreConsole rv = SAC_NoConsole; |
| 744 | | - FILE* apf[3] = { pfIn, pfOut, pfErr }; |
| 745 | | - int ix; |
| 746 | | - for( ix = 2; ix >= 0; --ix ){ |
| 747 | | - PerStreamTags *ppst = &consoleInfo.pstSetup[ix]; |
| 748 | | - if( streamOfConsole(apf[ix], ppst) ){ |
| 749 | | - rv |= (SAC_InConsole<<ix); |
| 750 | | - } |
| 751 | | - consoleInfo.pstDesignated[ix] = *ppst; |
| 752 | | - if( ix > 0 ) fflush(apf[ix]); |
| 753 | | - } |
| 754 | | - consoleInfo.sacSetup = rv; |
| 755 | | - consoleRenewSetup(); |
| 756 | | - return rv; |
| 757 | | -} |
| 758 | | - |
| 759 | | -SQLITE_INTERNAL_LINKAGE void SQLITE_CDECL consoleRestore( void ){ |
| 760 | | -# if CIO_WIN_WC_XLATE |
| 761 | | - static ConsoleInfo *pci = &consoleInfo; |
| 762 | | - if( pci->sacSetup ){ |
| 763 | | - int ix; |
| 764 | | - for( ix=0; ix<3; ++ix ){ |
| 765 | | - if( pci->sacSetup & (SAC_InConsole<<ix) ){ |
| 766 | | - PerStreamTags *ppst = &pci->pstSetup[ix]; |
| 767 | | - SetConsoleMode(ppst->hx, ppst->consMode); |
| 768 | | - } |
| 769 | | - } |
| 770 | | - } |
| 771 | | -# endif |
| 772 | | -} |
| 773 | | -#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */ |
| 774 | | - |
| 775 | | -#ifdef SQLITE_CIO_INPUT_REDIR |
| 776 | | -/* Say whether given FILE* is among those known, via either |
| 777 | | -** consoleClassifySetup() or set{Output,Error}Stream, as |
| 778 | | -** readable, and return an associated PerStreamTags pointer |
| 779 | | -** if so. Otherwise, return 0. |
| 780 | | -*/ |
| 781 | | -static PerStreamTags * isKnownReadable(FILE *pf){ |
| 782 | | - static PerStreamTags *apst[] = { |
| 783 | | - &consoleInfo.pstDesignated[0], &consoleInfo.pstSetup[0], 0 |
| 784 | | - }; |
| 785 | | - int ix = 0; |
| 786 | | - do { |
| 787 | | - if( apst[ix]->pf == pf ) break; |
| 788 | | - } while( apst[++ix] != 0 ); |
| 789 | | - return apst[ix]; |
| 790 | | -} |
| 791 | | -#endif |
| 792 | | - |
| 793 | | -#ifndef SQLITE_CIO_NO_TRANSLATE |
| 794 | | -/* Say whether given FILE* is among those known, via either |
| 795 | | -** consoleClassifySetup() or set{Output,Error}Stream, as |
| 796 | | -** writable, and return an associated PerStreamTags pointer |
| 797 | | -** if so. Otherwise, return 0. |
| 798 | | -*/ |
| 799 | | -static PerStreamTags * isKnownWritable(FILE *pf){ |
| 800 | | - static PerStreamTags *apst[] = { |
| 801 | | - &consoleInfo.pstDesignated[1], &consoleInfo.pstDesignated[2], |
| 802 | | - &consoleInfo.pstSetup[1], &consoleInfo.pstSetup[2], 0 |
| 803 | | - }; |
| 804 | | - int ix = 0; |
| 805 | | - do { |
| 806 | | - if( apst[ix]->pf == pf ) break; |
| 807 | | - } while( apst[++ix] != 0 ); |
| 808 | | - return apst[ix]; |
| 809 | | -} |
| 810 | | - |
| 811 | | -static FILE *designateEmitStream(FILE *pf, unsigned chix){ |
| 812 | | - FILE *rv = consoleInfo.pstDesignated[chix].pf; |
| 813 | | - if( pf == invalidFileStream ) return rv; |
| 814 | | - else{ |
| 815 | | - /* Setting a possibly new output stream. */ |
| 816 | | - PerStreamTags *ppst = isKnownWritable(pf); |
| 817 | | - if( ppst != 0 ){ |
| 818 | | - PerStreamTags pst = *ppst; |
| 819 | | - consoleInfo.pstDesignated[chix] = pst; |
| 820 | | - }else streamOfConsole(pf, &consoleInfo.pstDesignated[chix]); |
| 821 | | - } |
| 822 | | - return rv; |
| 823 | | -} |
| 824 | | - |
| 825 | | -SQLITE_INTERNAL_LINKAGE FILE *setOutputStream(FILE *pf){ |
| 826 | | - return designateEmitStream(pf, 1); |
| 827 | | -} |
| 828 | | -# ifdef CONSIO_SET_ERROR_STREAM |
| 829 | | -SQLITE_INTERNAL_LINKAGE FILE *setErrorStream(FILE *pf){ |
| 830 | | - return designateEmitStream(pf, 2); |
| 831 | | -} |
| 832 | | -# endif |
| 833 | | -#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */ |
| 834 | | - |
| 835 | | -#ifndef SQLITE_CIO_NO_SETMODE |
| 836 | | -# if CIO_WIN_WC_XLATE |
| 837 | | -static void setModeFlushQ(FILE *pf, short bFlush, int mode){ |
| 838 | | - if( bFlush ) fflush(pf); |
| 839 | | - _setmode(_fileno(pf), mode); |
| 840 | | -} |
| 841 | | -# else |
| 842 | | -# define setModeFlushQ(f, b, m) if(b) fflush(f) |
| 843 | | -# endif |
| 844 | | - |
| 845 | | -SQLITE_INTERNAL_LINKAGE void setBinaryMode(FILE *pf, short bFlush){ |
| 846 | | - setModeFlushQ(pf, bFlush, _O_BINARY); |
| 847 | | -} |
| 848 | | -SQLITE_INTERNAL_LINKAGE void setTextMode(FILE *pf, short bFlush){ |
| 849 | | - setModeFlushQ(pf, bFlush, _O_TEXT); |
| 850 | | -} |
| 851 | | -# undef setModeFlushQ |
| 852 | | - |
| 853 | | -#else /* defined(SQLITE_CIO_NO_SETMODE) */ |
| 854 | | -# define setBinaryMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0) |
| 855 | | -# define setTextMode(f, bFlush) do{ if((bFlush)) fflush(f); }while(0) |
| 856 | | -#endif /* defined(SQLITE_CIO_NO_SETMODE) */ |
| 857 | | - |
| 858 | | -#ifndef SQLITE_CIO_NO_TRANSLATE |
| 859 | | -# if CIO_WIN_WC_XLATE |
| 860 | | -/* Write buffer cBuf as output to stream known to reach console, |
| 861 | | -** limited to ncTake char's. Return ncTake on success, else 0. */ |
| 862 | | -static int conZstrEmit(PerStreamTags *ppst, const char *z, int ncTake){ |
| 863 | | - int rv = 0; |
| 864 | | - if( z!=NULL ){ |
| 865 | | - int nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, 0,0); |
| 866 | | - if( nwc > 0 ){ |
| 867 | | - WCHAR *zw = sqlite3_malloc64(nwc*sizeof(WCHAR)); |
| 868 | | - if( zw!=NULL ){ |
| 869 | | - nwc = MultiByteToWideChar(CP_UTF8,0, z,ncTake, zw,nwc); |
| 870 | | - if( nwc > 0 ){ |
| 871 | | - /* Translation from UTF-8 to UTF-16, then WCHARs out. */ |
| 872 | | - if( WriteConsoleW(ppst->hx, zw,nwc, 0, NULL) ){ |
| 873 | | - rv = ncTake; |
| 874 | | - } |
| 875 | | - } |
| 876 | | - sqlite3_free(zw); |
| 877 | | - } |
| 878 | | - } |
| 879 | | - } |
| 880 | | - return rv; |
| 881 | | -} |
| 882 | | - |
| 883 | | -/* For {f,o,e}PrintfUtf8() when stream is known to reach console. */ |
| 884 | | -static int conioVmPrintf(PerStreamTags *ppst, const char *zFormat, va_list ap){ |
| 885 | | - char *z = sqlite3_vmprintf(zFormat, ap); |
| 886 | | - if( z ){ |
| 887 | | - int rv = conZstrEmit(ppst, z, (int)strlen(z)); |
| 888 | | - sqlite3_free(z); |
| 889 | | - return rv; |
| 890 | | - }else return 0; |
| 891 | | -} |
| 892 | | -# endif /* CIO_WIN_WC_XLATE */ |
| 893 | | - |
| 894 | | -# ifdef CONSIO_GET_EMIT_STREAM |
| 895 | | -static PerStreamTags * getDesignatedEmitStream(FILE *pf, unsigned chix, |
| 896 | | - PerStreamTags *ppst){ |
| 897 | | - PerStreamTags *rv = isKnownWritable(pf); |
| 898 | | - short isValid = (rv!=0)? isValidStreamInfo(rv) : 0; |
| 899 | | - if( rv != 0 && isValid ) return rv; |
| 900 | | - streamOfConsole(pf, ppst); |
| 901 | | - return ppst; |
| 902 | | -} |
| 903 | | -# endif |
| 904 | | - |
| 905 | | -/* Get stream info, either for designated output or error stream when |
| 906 | | -** chix equals 1 or 2, or for an arbitrary stream when chix == 0. |
| 907 | | -** In either case, ppst references a caller-owned PerStreamTags |
| 908 | | -** struct which may be filled in if none of the known writable |
| 909 | | -** streams is being held by consoleInfo. The ppf parameter is a |
| 910 | | -** byref output when chix!=0 and a byref input when chix==0. |
| 911 | | - */ |
| 912 | | -static PerStreamTags * |
| 913 | | -getEmitStreamInfo(unsigned chix, PerStreamTags *ppst, |
| 914 | | - /* in/out */ FILE **ppf){ |
| 915 | | - PerStreamTags *ppstTry; |
| 916 | | - FILE *pfEmit; |
| 917 | | - if( chix > 0 ){ |
| 918 | | - ppstTry = &consoleInfo.pstDesignated[chix]; |
| 919 | | - if( !isValidStreamInfo(ppstTry) ){ |
| 920 | | - ppstTry = &consoleInfo.pstSetup[chix]; |
| 921 | | - pfEmit = ppst->pf; |
| 922 | | - }else pfEmit = ppstTry->pf; |
| 923 | | - if( !isValidStreamInfo(ppstTry) ){ |
| 924 | | - pfEmit = (chix > 1)? stderr : stdout; |
| 925 | | - ppstTry = ppst; |
| 926 | | - streamOfConsole(pfEmit, ppstTry); |
| 927 | | - } |
| 928 | | - *ppf = pfEmit; |
| 929 | | - }else{ |
| 930 | | - ppstTry = isKnownWritable(*ppf); |
| 931 | | - if( ppstTry != 0 ) return ppstTry; |
| 932 | | - streamOfConsole(*ppf, ppst); |
| 933 | | - return ppst; |
| 934 | | - } |
| 935 | | - return ppstTry; |
| 936 | | -} |
| 937 | | - |
| 938 | | -SQLITE_INTERNAL_LINKAGE int oPrintfUtf8(const char *zFormat, ...){ |
| 939 | | - va_list ap; |
| 940 | | - int rv; |
| 941 | | - FILE *pfOut; |
| 942 | | - PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ |
| 943 | | -# if CIO_WIN_WC_XLATE |
| 944 | | - PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut); |
| 945 | | -# else |
| 946 | | - getEmitStreamInfo(1, &pst, &pfOut); |
| 947 | | -# endif |
| 948 | | - assert(zFormat!=0); |
| 949 | | - va_start(ap, zFormat); |
| 950 | | -# if CIO_WIN_WC_XLATE |
| 951 | | - if( pstReachesConsole(ppst) ){ |
| 952 | | - rv = conioVmPrintf(ppst, zFormat, ap); |
| 953 | | - }else{ |
| 954 | | -# endif |
| 955 | | - rv = vfprintf(pfOut, zFormat, ap); |
| 956 | | -# if CIO_WIN_WC_XLATE |
| 957 | | - } |
| 958 | | -# endif |
| 959 | | - va_end(ap); |
| 960 | | - return rv; |
| 961 | | -} |
| 962 | | - |
| 963 | | -SQLITE_INTERNAL_LINKAGE int ePrintfUtf8(const char *zFormat, ...){ |
| 964 | | - va_list ap; |
| 965 | | - int rv; |
| 966 | | - FILE *pfErr; |
| 967 | | - PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ |
| 968 | | -# if CIO_WIN_WC_XLATE |
| 969 | | - PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr); |
| 970 | | -# else |
| 971 | | - getEmitStreamInfo(2, &pst, &pfErr); |
| 972 | | -# endif |
| 973 | | - assert(zFormat!=0); |
| 974 | | - va_start(ap, zFormat); |
| 975 | | -# if CIO_WIN_WC_XLATE |
| 976 | | - if( pstReachesConsole(ppst) ){ |
| 977 | | - rv = conioVmPrintf(ppst, zFormat, ap); |
| 978 | | - }else{ |
| 979 | | -# endif |
| 980 | | - rv = vfprintf(pfErr, zFormat, ap); |
| 981 | | -# if CIO_WIN_WC_XLATE |
| 982 | | - } |
| 983 | | -# endif |
| 984 | | - va_end(ap); |
| 985 | | - return rv; |
| 986 | | -} |
| 987 | | - |
| 988 | | -SQLITE_INTERNAL_LINKAGE int fPrintfUtf8(FILE *pfO, const char *zFormat, ...){ |
| 989 | | - va_list ap; |
| 990 | | - int rv; |
| 991 | | - PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ |
| 992 | | -# if CIO_WIN_WC_XLATE |
| 993 | | - PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO); |
| 994 | | -# else |
| 995 | | - getEmitStreamInfo(0, &pst, &pfO); |
| 996 | | -# endif |
| 997 | | - assert(zFormat!=0); |
| 998 | | - va_start(ap, zFormat); |
| 999 | | -# if CIO_WIN_WC_XLATE |
| 1000 | | - if( pstReachesConsole(ppst) ){ |
| 1001 | | - maybeSetupAsConsole(ppst, 1); |
| 1002 | | - rv = conioVmPrintf(ppst, zFormat, ap); |
| 1003 | | - if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst); |
| 1004 | | - }else{ |
| 1005 | | -# endif |
| 1006 | | - rv = vfprintf(pfO, zFormat, ap); |
| 1007 | | -# if CIO_WIN_WC_XLATE |
| 1008 | | - } |
| 1009 | | -# endif |
| 1010 | | - va_end(ap); |
| 1011 | | - return rv; |
| 1012 | | -} |
| 1013 | | - |
| 1014 | | -SQLITE_INTERNAL_LINKAGE int fPutsUtf8(const char *z, FILE *pfO){ |
| 1015 | | - PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ |
| 1016 | | -# if CIO_WIN_WC_XLATE |
| 1017 | | - PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO); |
| 1018 | | -# else |
| 1019 | | - getEmitStreamInfo(0, &pst, &pfO); |
| 1020 | | -# endif |
| 1021 | | - assert(z!=0); |
| 1022 | | -# if CIO_WIN_WC_XLATE |
| 1023 | | - if( pstReachesConsole(ppst) ){ |
| 1024 | | - int rv; |
| 1025 | | - maybeSetupAsConsole(ppst, 1); |
| 1026 | | - rv = conZstrEmit(ppst, z, (int)strlen(z)); |
| 1027 | | - if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst); |
| 1028 | | - return rv; |
| 1029 | | - }else { |
| 1030 | | -# endif |
| 1031 | | - return (fputs(z, pfO)<0)? 0 : (int)strlen(z); |
| 1032 | | -# if CIO_WIN_WC_XLATE |
| 1033 | | - } |
| 1034 | | -# endif |
| 1035 | | -} |
| 1036 | | - |
| 1037 | | -SQLITE_INTERNAL_LINKAGE int ePutsUtf8(const char *z){ |
| 1038 | | - FILE *pfErr; |
| 1039 | | - PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ |
| 1040 | | -# if CIO_WIN_WC_XLATE |
| 1041 | | - PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr); |
| 1042 | | -# else |
| 1043 | | - getEmitStreamInfo(2, &pst, &pfErr); |
| 1044 | | -# endif |
| 1045 | | - assert(z!=0); |
| 1046 | | -# if CIO_WIN_WC_XLATE |
| 1047 | | - if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z)); |
| 1048 | | - else { |
| 1049 | | -# endif |
| 1050 | | - return (fputs(z, pfErr)<0)? 0 : (int)strlen(z); |
| 1051 | | -# if CIO_WIN_WC_XLATE |
| 1052 | | - } |
| 1053 | | -# endif |
| 1054 | | -} |
| 1055 | | - |
| 1056 | | -SQLITE_INTERNAL_LINKAGE int oPutsUtf8(const char *z){ |
| 1057 | | - FILE *pfOut; |
| 1058 | | - PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ |
| 1059 | | -# if CIO_WIN_WC_XLATE |
| 1060 | | - PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut); |
| 1061 | | -# else |
| 1062 | | - getEmitStreamInfo(1, &pst, &pfOut); |
| 1063 | | -# endif |
| 1064 | | - assert(z!=0); |
| 1065 | | -# if CIO_WIN_WC_XLATE |
| 1066 | | - if( pstReachesConsole(ppst) ) return conZstrEmit(ppst, z, (int)strlen(z)); |
| 1067 | | - else { |
| 1068 | | -# endif |
| 1069 | | - return (fputs(z, pfOut)<0)? 0 : (int)strlen(z); |
| 1070 | | -# if CIO_WIN_WC_XLATE |
| 1071 | | - } |
| 1072 | | -# endif |
| 1073 | | -} |
| 1074 | | - |
| 1075 | | -#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */ |
| 1076 | | - |
| 1077 | | -#if !(defined(SQLITE_CIO_NO_UTF8SCAN) && defined(SQLITE_CIO_NO_TRANSLATE)) |
| 1078 | | -/* Skip over as much z[] input char sequence as is valid UTF-8, |
| 1079 | | -** limited per nAccept char's or whole characters and containing |
| 1080 | | -** no char cn such that ((1<<cn) & ccm)!=0. On return, the |
| 1081 | | -** sequence z:return (inclusive:exclusive) is validated UTF-8. |
| 1082 | | -** Limit: nAccept>=0 => char count, nAccept<0 => character |
| 1083 | | - */ |
| 1084 | | -SQLITE_INTERNAL_LINKAGE const char* |
| 1085 | | -zSkipValidUtf8(const char *z, int nAccept, long ccm){ |
| 1086 | | - int ng = (nAccept<0)? -nAccept : 0; |
| 1087 | | - const char *pcLimit = (nAccept>=0)? z+nAccept : 0; |
| 1088 | | - assert(z!=0); |
| 1089 | | - while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){ |
| 1090 | | - char c = *z; |
| 1091 | | - if( (c & 0x80) == 0 ){ |
| 1092 | | - if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z; |
| 1093 | | - ++z; /* ASCII */ |
| 1094 | | - }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */ |
| 1095 | | - else{ |
| 1096 | | - const char *zt = z+1; /* Got lead byte, look at trail bytes.*/ |
| 1097 | | - do{ |
| 1098 | | - if( pcLimit && zt >= pcLimit ) return z; |
| 1099 | | - else{ |
| 1100 | | - char ct = *zt++; |
| 1101 | | - if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){ |
| 1102 | | - /* Trailing bytes are too few, too many, or invalid. */ |
| 1103 | | - return z; |
| 1104 | | - } |
| 1105 | | - } |
| 1106 | | - } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */ |
| 1107 | | - z = zt; |
| 1108 | | - } |
| 1109 | | - } |
| 1110 | | - return z; |
| 1111 | | -} |
| 1112 | | -#endif /*!(defined(SQLITE_CIO_NO_UTF8SCAN)&&defined(SQLITE_CIO_NO_TRANSLATE))*/ |
| 1113 | | - |
| 1114 | | -#ifndef SQLITE_CIO_NO_TRANSLATE |
| 1115 | | -# ifdef CONSIO_SPUTB |
| 1116 | | -SQLITE_INTERNAL_LINKAGE int |
| 1117 | | -fPutbUtf8(FILE *pfO, const char *cBuf, int nAccept){ |
| 1118 | | - assert(pfO!=0); |
| 1119 | | -# if CIO_WIN_WC_XLATE |
| 1120 | | - PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ |
| 1121 | | - PerStreamTags *ppst = getEmitStreamInfo(0, &pst, &pfO); |
| 1122 | | - if( pstReachesConsole(ppst) ){ |
| 1123 | | - int rv; |
| 1124 | | - maybeSetupAsConsole(ppst, 1); |
| 1125 | | - rv = conZstrEmit(ppst, cBuf, nAccept); |
| 1126 | | - if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst); |
| 1127 | | - return rv; |
| 1128 | | - }else { |
| 1129 | | -# endif |
| 1130 | | - return (int)fwrite(cBuf, 1, nAccept, pfO); |
| 1131 | | -# if CIO_WIN_WC_XLATE |
| 1132 | | - } |
| 1133 | | -# endif |
| 1134 | | -} |
| 1135 | | -# endif |
| 1136 | | - |
| 1137 | | -SQLITE_INTERNAL_LINKAGE int |
| 1138 | | -oPutbUtf8(const char *cBuf, int nAccept){ |
| 1139 | | - FILE *pfOut; |
| 1140 | | - PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ |
| 1141 | | -# if CIO_WIN_WC_XLATE |
| 1142 | | - PerStreamTags *ppst = getEmitStreamInfo(1, &pst, &pfOut); |
| 1143 | | -# else |
| 1144 | | - getEmitStreamInfo(1, &pst, &pfOut); |
| 1145 | | -# endif |
| 1146 | | -# if CIO_WIN_WC_XLATE |
| 1147 | | - if( pstReachesConsole(ppst) ){ |
| 1148 | | - return conZstrEmit(ppst, cBuf, nAccept); |
| 1149 | | - }else { |
| 1150 | | -# endif |
| 1151 | | - return (int)fwrite(cBuf, 1, nAccept, pfOut); |
| 1152 | | -# if CIO_WIN_WC_XLATE |
| 1153 | | - } |
| 1154 | | -# endif |
| 1155 | | -} |
| 1156 | | - |
| 1157 | | -/* |
| 1158 | | -** Flush the given output stream. Return non-zero for success, else 0. |
| 1159 | | -*/ |
| 1160 | | -#if !defined(SQLITE_CIO_NO_FLUSH) && !defined(SQLITE_CIO_NO_SETMODE) |
| 1161 | | -SQLITE_INTERNAL_LINKAGE int |
| 1162 | | -fFlushBuffer(FILE *pfOut){ |
| 1163 | | -# if CIO_WIN_WC_XLATE && !defined(SHELL_OMIT_FIO_DUPE) |
| 1164 | | - return FlushFileBuffers(handleOfFile(pfOut))? 1 : 0; |
| 1165 | | -# else |
| 1166 | | - return fflush(pfOut); |
| 1167 | | -# endif |
| 1168 | | -} |
| 1169 | | -#endif |
| 1170 | | - |
| 1171 | | -#if CIO_WIN_WC_XLATE \ |
| 1172 | | - && !defined(SHELL_OMIT_FIO_DUPE) \ |
| 1173 | | - && defined(SQLITE_USE_ONLY_WIN32) |
| 1174 | | -static struct FileAltIds { |
| 1175 | | - int fd; |
| 1176 | | - HANDLE fh; |
| 1177 | | -} altIdsOfFile(FILE *pf){ |
| 1178 | | - struct FileAltIds rv = { _fileno(pf) }; |
| 1179 | | - union { intptr_t osfh; HANDLE fh; } fid = { |
| 1180 | | - (rv.fd>=0)? _get_osfhandle(rv.fd) : (intptr_t)INVALID_HANDLE_VALUE |
| 1181 | | - }; |
| 1182 | | - rv.fh = fid.fh; |
| 1183 | | - return rv; |
| 1184 | | -} |
| 1185 | | - |
| 1186 | | -SQLITE_INTERNAL_LINKAGE size_t |
| 1187 | | -cfWrite(const void *buf, size_t osz, size_t ocnt, FILE *pf){ |
| 1188 | | - size_t rv = 0; |
| 1189 | | - struct FileAltIds fai = altIdsOfFile(pf); |
| 1190 | | - int fmode = _setmode(fai.fd, _O_BINARY); |
| 1191 | | - _setmode(fai.fd, fmode); |
| 1192 | | - while( rv < ocnt ){ |
| 1193 | | - size_t nbo = osz; |
| 1194 | | - while( nbo > 0 ){ |
| 1195 | | - DWORD dwno = (nbo>(1L<<24))? 1L<<24 : (DWORD)nbo; |
| 1196 | | - BOOL wrc = TRUE; |
| 1197 | | - BOOL genCR = (fmode & _O_TEXT)!=0; |
| 1198 | | - if( genCR ){ |
| 1199 | | - const char *pnl = (const char*)memchr(buf, '\n', nbo); |
| 1200 | | - if( pnl ) nbo = pnl - (const char*)buf; |
| 1201 | | - else genCR = 0; |
| 1202 | | - } |
| 1203 | | - if( dwno>0 ) wrc = WriteFile(fai.fh, buf, dwno, 0,0); |
| 1204 | | - if( genCR && wrc ){ |
| 1205 | | - wrc = WriteFile(fai.fh, "\r\n", 2, 0,0); |
| 1206 | | - ++dwno; /* Skip over the LF */ |
| 1207 | | - } |
| 1208 | | - if( !wrc ) return rv; |
| 1209 | | - buf = (const char*)buf + dwno; |
| 1210 | | - nbo += dwno; |
| 1211 | | - } |
| 1212 | | - ++rv; |
| 1213 | | - } |
| 1214 | | - return rv; |
| 1215 | | -} |
| 1216 | | - |
| 1217 | | -SQLITE_INTERNAL_LINKAGE char * |
| 1218 | | -cfGets(char *cBuf, int n, FILE *pf){ |
| 1219 | | - int nci = 0; |
| 1220 | | - struct FileAltIds fai = altIdsOfFile(pf); |
| 1221 | | - int fmode = _setmode(fai.fd, _O_BINARY); |
| 1222 | | - BOOL eatCR = (fmode & _O_TEXT)!=0; |
| 1223 | | - _setmode(fai.fd, fmode); |
| 1224 | | - while( nci < n-1 ){ |
| 1225 | | - DWORD nr; |
| 1226 | | - if( !ReadFile(fai.fh, cBuf+nci, 1, &nr, 0) || nr==0 ) break; |
| 1227 | | - if( nr>0 && (!eatCR || cBuf[nci]!='\r') ) nci += nr; |
| 1228 | | - } |
| 1229 | | - if( nci < n ) cBuf[nci] = 0; |
| 1230 | | - return (nci>0)? cBuf : 0; |
| 1231 | | -} |
| 1232 | | -# else |
| 1233 | | -# define cfWrite(b,os,no,f) fwrite(b,os,no,f) |
| 1234 | | -# define cfGets(b,n,f) fgets(b,n,f) |
| 1235 | | -# endif |
| 1236 | | - |
| 1237 | | -# ifdef CONSIO_EPUTB |
| 1238 | | -SQLITE_INTERNAL_LINKAGE int |
| 1239 | | -ePutbUtf8(const char *cBuf, int nAccept){ |
| 1240 | | - FILE *pfErr; |
| 1241 | | - PerStreamTags pst = PST_INITIALIZER; /* for unknown streams */ |
| 1242 | | - PerStreamTags *ppst = getEmitStreamInfo(2, &pst, &pfErr); |
| 1243 | | -# if CIO_WIN_WC_XLATE |
| 1244 | | - if( pstReachesConsole(ppst) ){ |
| 1245 | | - return conZstrEmit(ppst, cBuf, nAccept); |
| 1246 | | - }else { |
| 1247 | | -# endif |
| 1248 | | - return (int)cfWrite(cBuf, 1, nAccept, pfErr); |
| 1249 | | -# if CIO_WIN_WC_XLATE |
| 1250 | | - } |
| 1251 | | -# endif |
| 1252 | | -} |
| 1253 | | -# endif /* defined(CONSIO_EPUTB) */ |
| 1254 | | - |
| 1255 | | -SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){ |
| 1256 | | - if( pfIn==0 ) pfIn = stdin; |
| 1257 | | -# if CIO_WIN_WC_XLATE |
| 1258 | | - if( pfIn == consoleInfo.pstSetup[0].pf |
| 1259 | | - && (consoleInfo.sacSetup & SAC_InConsole)!=0 ){ |
| 1260 | | -# if CIO_WIN_WC_XLATE==1 |
| 1261 | | -# define SHELL_GULP 150 /* Count of WCHARS to be gulped at a time */ |
| 1262 | | - WCHAR wcBuf[SHELL_GULP+1]; |
| 1263 | | - int lend = 0, noc = 0; |
| 1264 | | - if( ncMax > 0 ) cBuf[0] = 0; |
| 1265 | | - while( noc < ncMax-8-1 && !lend ){ |
| 1266 | | - /* There is room for at least 2 more characters and a 0-terminator. */ |
| 1267 | | - int na = (ncMax > SHELL_GULP*4+1 + noc)? SHELL_GULP : (ncMax-1 - noc)/4; |
| 1268 | | -# undef SHELL_GULP |
| 1269 | | - DWORD nbr = 0; |
| 1270 | | - BOOL bRC = ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf, na, &nbr, 0); |
| 1271 | | - if( bRC && nbr>0 && (wcBuf[nbr-1]&0xF800)==0xD800 ){ |
| 1272 | | - /* Last WHAR read is first of a UTF-16 surrogate pair. Grab its mate. */ |
| 1273 | | - DWORD nbrx; |
| 1274 | | - bRC &= ReadConsoleW(consoleInfo.pstSetup[0].hx, wcBuf+nbr, 1, &nbrx, 0); |
| 1275 | | - if( bRC ) nbr += nbrx; |
| 1276 | | - } |
| 1277 | | - if( !bRC || (noc==0 && nbr==0) ) return 0; |
| 1278 | | - if( nbr > 0 ){ |
| 1279 | | - int nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,0,0,0,0); |
| 1280 | | - if( nmb != 0 && noc+nmb <= ncMax ){ |
| 1281 | | - int iseg = noc; |
| 1282 | | - nmb = WideCharToMultiByte(CP_UTF8, 0, wcBuf,nbr,cBuf+noc,nmb,0,0); |
| 1283 | | - noc += nmb; |
| 1284 | | - /* Fixup line-ends as coded by Windows for CR (or "Enter".) |
| 1285 | | - ** This is done without regard for any setMode{Text,Binary}() |
| 1286 | | - ** call that might have been done on the interactive input. |
| 1287 | | - */ |
| 1288 | | - if( noc > 0 ){ |
| 1289 | | - if( cBuf[noc-1]=='\n' ){ |
| 1290 | | - lend = 1; |
| 1291 | | - if( noc > 1 && cBuf[noc-2]=='\r' ) cBuf[--noc-1] = '\n'; |
| 1292 | | - } |
| 1293 | | - } |
| 1294 | | - /* Check for ^Z (anywhere in line) too, to act as EOF. */ |
| 1295 | | - while( iseg < noc ){ |
| 1296 | | - if( cBuf[iseg]=='\x1a' ){ |
| 1297 | | - noc = iseg; /* Chop ^Z and anything following. */ |
| 1298 | | - lend = 1; /* Counts as end of line too. */ |
| 1299 | | - break; |
| 1300 | | - } |
| 1301 | | - ++iseg; |
| 1302 | | - } |
| 1303 | | - }else break; /* Drop apparent garbage in. (Could assert.) */ |
| 1304 | | - }else break; |
| 1305 | | - } |
| 1306 | | - /* If got nothing, (after ^Z chop), must be at end-of-file. */ |
| 1307 | | - if( noc > 0 ){ |
| 1308 | | - cBuf[noc] = 0; |
| 1309 | | - return cBuf; |
| 1310 | | - }else return 0; |
| 1311 | | -# endif |
| 1312 | | - }else{ |
| 1313 | | -# endif |
| 1314 | | - return cfGets(cBuf, ncMax, pfIn); |
| 1315 | | -# if CIO_WIN_WC_XLATE |
| 1316 | | - } |
| 1317 | | -# endif |
| 1318 | | -} |
| 1319 | | -#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */ |
| 1320 | | - |
| 1321 | | -#if defined(_MSC_VER) |
| 1322 | | -# pragma warning(default : 4204) |
| 1323 | | -#endif |
| 1324 | | - |
| 1325 | | -#undef SHELL_INVALID_FILE_PTR |
| 1326 | | - |
| 1327 | | -/************************* End ../ext/consio/console_io.c ********************/ |
| 1328 | | - |
| 1329 | | -#ifndef SQLITE_SHELL_FIDDLE |
| 1330 | | - |
| 1331 | | -/* From here onward, fgets() is redirected to the console_io library. */ |
| 1332 | | -# define fgets(b,n,f) fGetsUtf8(b,n,f) |
| 1333 | | -/* |
| 1334 | | - * Define macros for emitting output text in various ways: |
| 1335 | | - * sputz(s, z) => emit 0-terminated string z to given stream s |
| 1336 | | - * sputf(s, f, ...) => emit varargs per format f to given stream s |
| 1337 | | - * oputz(z) => emit 0-terminated string z to default stream |
| 1338 | | - * oputf(f, ...) => emit varargs per format f to default stream |
| 1339 | | - * eputz(z) => emit 0-terminated string z to error stream |
| 1340 | | - * eputf(f, ...) => emit varargs per format f to error stream |
| 1341 | | - * oputb(b, n) => emit char buffer b[0..n-1] to default stream |
| 1342 | | - * |
| 1343 | | - * Note that the default stream is whatever has been last set via: |
| 1344 | | - * setOutputStream(FILE *pf) |
| 1345 | | - * This is normally the stream that CLI normal output goes to. |
| 1346 | | - * For the stand-alone CLI, it is stdout with no .output redirect. |
| 1347 | | - * |
| 1348 | | - * The ?putz(z) forms are required for the Fiddle builds for string literal |
| 1349 | | - * output, in aid of enforcing format string to argument correspondence. |
| 1350 | | - */ |
| 1351 | | -# define sputz(s,z) fPutsUtf8(z,s) |
| 1352 | | -# define sputf fPrintfUtf8 |
| 1353 | | -# define oputz(z) oPutsUtf8(z) |
| 1354 | | -# define oputf oPrintfUtf8 |
| 1355 | | -# define eputz(z) ePutsUtf8(z) |
| 1356 | | -# define eputf ePrintfUtf8 |
| 1357 | | -# define oputb(buf,na) oPutbUtf8(buf,na) |
| 1358 | | -# define fflush(s) fFlushBuffer(s); |
| 1359 | | - |
| 1360 | | -#else |
| 1361 | | -/* For Fiddle, all console handling and emit redirection is omitted. */ |
| 1362 | | -/* These next 3 macros are for emitting formatted output. When complaints |
| 1363 | | - * from the WASM build are issued for non-formatted output, when a mere |
| 1364 | | - * string literal is to be emitted, the ?putz(z) forms should be used. |
| 1365 | | - * (This permits compile-time checking of format string / argument mismatch.) |
| 1366 | | - */ |
| 1367 | | -# define oputf(fmt, ...) printf(fmt,__VA_ARGS__) |
| 1368 | | -# define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__) |
| 1369 | | -# define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__) |
| 1370 | | -/* These next 3 macros are for emitting simple string literals. */ |
| 1371 | | -# define oputz(z) fputs(z,stdout) |
| 1372 | | -# define eputz(z) fputs(z,stderr) |
| 1373 | | -# define sputz(fp,z) fputs(z,fp) |
| 1374 | | -# define oputb(buf,na) fwrite(buf,1,na,stdout) |
| 1375 | | -# undef fflush |
| 1376 | | -#endif |
| 624 | + |
| 625 | +#define eputz(z) sqlite3_fputs(z,stderr) |
| 626 | +#define sputz(fp,z) sqlite3_fputs(z,fp) |
| 1377 | 627 | |
| 1378 | 628 | /* True if the timer is enabled */ |
| 1379 | 629 | static int enableTimer = 0; |
| 1380 | 630 | |
| 1381 | 631 | /* A version of strcmp() that works with NULL values */ |
| | @@ -1416,10 +666,11 @@ |
| 1416 | 666 | struct timeval ru_utime; /* user CPU time used */ |
| 1417 | 667 | struct timeval ru_stime; /* system CPU time used */ |
| 1418 | 668 | }; |
| 1419 | 669 | #define getrusage(A,B) memset(B,0,sizeof(*B)) |
| 1420 | 670 | #endif |
| 671 | + |
| 1421 | 672 | |
| 1422 | 673 | /* Saved resource information for the beginning of an operation */ |
| 1423 | 674 | static struct rusage sBegin; /* CPU time at start */ |
| 1424 | 675 | static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
| 1425 | 676 | |
| | @@ -1440,24 +691,24 @@ |
| 1440 | 691 | } |
| 1441 | 692 | |
| 1442 | 693 | /* |
| 1443 | 694 | ** Print the timing results. |
| 1444 | 695 | */ |
| 1445 | | -static void endTimer(void){ |
| 696 | +static void endTimer(FILE *out){ |
| 1446 | 697 | if( enableTimer ){ |
| 1447 | 698 | sqlite3_int64 iEnd = timeOfDay(); |
| 1448 | 699 | struct rusage sEnd; |
| 1449 | 700 | getrusage(RUSAGE_SELF, &sEnd); |
| 1450 | | - sputf(stdout, "Run Time: real %.3f user %f sys %f\n", |
| 701 | + sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n", |
| 1451 | 702 | (iEnd - iBegin)*0.001, |
| 1452 | 703 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 1453 | 704 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 1454 | 705 | } |
| 1455 | 706 | } |
| 1456 | 707 | |
| 1457 | 708 | #define BEGIN_TIMER beginTimer() |
| 1458 | | -#define END_TIMER endTimer() |
| 709 | +#define END_TIMER(X) endTimer(X) |
| 1459 | 710 | #define HAS_TIMER 1 |
| 1460 | 711 | |
| 1461 | 712 | #elif (defined(_WIN32) || defined(WIN32)) |
| 1462 | 713 | |
| 1463 | 714 | /* Saved resource information for the beginning of an operation */ |
| | @@ -1519,29 +770,29 @@ |
| 1519 | 770 | } |
| 1520 | 771 | |
| 1521 | 772 | /* |
| 1522 | 773 | ** Print the timing results. |
| 1523 | 774 | */ |
| 1524 | | -static void endTimer(void){ |
| 775 | +static void endTimer(FILE *out){ |
| 1525 | 776 | if( enableTimer && getProcessTimesAddr){ |
| 1526 | 777 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
| 1527 | 778 | sqlite3_int64 ftWallEnd = timeOfDay(); |
| 1528 | 779 | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
| 1529 | | - sputf(stdout, "Run Time: real %.3f user %f sys %f\n", |
| 780 | + sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n", |
| 1530 | 781 | (ftWallEnd - ftWallBegin)*0.001, |
| 1531 | 782 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 1532 | 783 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 1533 | 784 | } |
| 1534 | 785 | } |
| 1535 | 786 | |
| 1536 | 787 | #define BEGIN_TIMER beginTimer() |
| 1537 | | -#define END_TIMER endTimer() |
| 788 | +#define END_TIMER(X) endTimer(X) |
| 1538 | 789 | #define HAS_TIMER hasTimer() |
| 1539 | 790 | |
| 1540 | 791 | #else |
| 1541 | 792 | #define BEGIN_TIMER |
| 1542 | | -#define END_TIMER |
| 793 | +#define END_TIMER(X) /*no-op*/ |
| 1543 | 794 | #define HAS_TIMER 0 |
| 1544 | 795 | #endif |
| 1545 | 796 | |
| 1546 | 797 | /* |
| 1547 | 798 | ** Used to prevent warnings about unused parameters |
| | @@ -1738,41 +989,216 @@ |
| 1738 | 989 | char *z; |
| 1739 | 990 | if( iotrace==0 ) return; |
| 1740 | 991 | va_start(ap, zFormat); |
| 1741 | 992 | z = sqlite3_vmprintf(zFormat, ap); |
| 1742 | 993 | va_end(ap); |
| 1743 | | - sputf(iotrace, "%s", z); |
| 994 | + sqlite3_fprintf(iotrace, "%s", z); |
| 1744 | 995 | sqlite3_free(z); |
| 1745 | 996 | } |
| 1746 | 997 | #endif |
| 1747 | 998 | |
| 999 | +/* Lookup table to estimate the number of columns consumed by a Unicode |
| 1000 | +** character. |
| 1001 | +*/ |
| 1002 | +static const struct { |
| 1003 | + unsigned char w; /* Width of the character in columns */ |
| 1004 | + int iFirst; /* First character in a span having this width */ |
| 1005 | +} aUWidth[] = { |
| 1006 | + /* {1, 0x00000}, */ |
| 1007 | + {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488}, |
| 1008 | + {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0}, |
| 1009 | + {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7}, |
| 1010 | + {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616}, |
| 1011 | + {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6}, |
| 1012 | + {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee}, |
| 1013 | + {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730}, |
| 1014 | + {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4}, |
| 1015 | + {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941}, |
| 1016 | + {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955}, |
| 1017 | + {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc}, |
| 1018 | + {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce}, |
| 1019 | + {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c}, |
| 1020 | + {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49}, |
| 1021 | + {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81}, |
| 1022 | + {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6}, |
| 1023 | + {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2}, |
| 1024 | + {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d}, |
| 1025 | + {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d}, |
| 1026 | + {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83}, |
| 1027 | + {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e}, |
| 1028 | + {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e}, |
| 1029 | + {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf}, |
| 1030 | + {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce}, |
| 1031 | + {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d}, |
| 1032 | + {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5}, |
| 1033 | + {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34}, |
| 1034 | + {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2}, |
| 1035 | + {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8}, |
| 1036 | + {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36}, |
| 1037 | + {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71}, |
| 1038 | + {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88}, |
| 1039 | + {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6}, |
| 1040 | + {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033}, |
| 1041 | + {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058}, |
| 1042 | + {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f}, |
| 1043 | + {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735}, |
| 1044 | + {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4}, |
| 1045 | + {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7}, |
| 1046 | + {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b}, |
| 1047 | + {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923}, |
| 1048 | + {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939}, |
| 1049 | + {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04}, |
| 1050 | + {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c}, |
| 1051 | + {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74}, |
| 1052 | + {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b}, |
| 1053 | + {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064}, |
| 1054 | + {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329}, |
| 1055 | + {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f}, |
| 1056 | + {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806}, |
| 1057 | + {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827}, |
| 1058 | + {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e}, |
| 1059 | + {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20}, |
| 1060 | + {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00}, |
| 1061 | + {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc}, |
| 1062 | + {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c}, |
| 1063 | + {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40}, |
| 1064 | + {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185}, |
| 1065 | + {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245}, |
| 1066 | + {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001}, |
| 1067 | + {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0} |
| 1068 | +}; |
| 1069 | + |
| 1748 | 1070 | /* |
| 1749 | | -** Output string zUtf to Out stream as w characters. If w is negative, |
| 1071 | +** Return an estimate of the width, in columns, for the single Unicode |
| 1072 | +** character c. For normal characters, the answer is always 1. But the |
| 1073 | +** estimate might be 0 or 2 for zero-width and double-width characters. |
| 1074 | +** |
| 1075 | +** Different display devices display unicode using different widths. So |
| 1076 | +** it is impossible to know that true display width with 100% accuracy. |
| 1077 | +** Inaccuracies in the width estimates might cause columns to be misaligned. |
| 1078 | +** Unfortunately, there is nothing we can do about that. |
| 1079 | +*/ |
| 1080 | +int cli_wcwidth(int c){ |
| 1081 | + int iFirst, iLast; |
| 1082 | + |
| 1083 | + /* Fast path for common characters */ |
| 1084 | + if( c<=0x300 ) return 1; |
| 1085 | + |
| 1086 | + /* The general case */ |
| 1087 | + iFirst = 0; |
| 1088 | + iLast = sizeof(aUWidth)/sizeof(aUWidth[0]) - 1; |
| 1089 | + while( iFirst<iLast-1 ){ |
| 1090 | + int iMid = (iFirst+iLast)/2; |
| 1091 | + int cMid = aUWidth[iMid].iFirst; |
| 1092 | + if( cMid < c ){ |
| 1093 | + iFirst = iMid; |
| 1094 | + }else if( cMid > c ){ |
| 1095 | + iLast = iMid - 1; |
| 1096 | + }else{ |
| 1097 | + return aUWidth[iMid].w; |
| 1098 | + } |
| 1099 | + } |
| 1100 | + if( aUWidth[iLast].iFirst > c ) return aUWidth[iFirst].w; |
| 1101 | + return aUWidth[iLast].w; |
| 1102 | +} |
| 1103 | + |
| 1104 | +/* |
| 1105 | +** Compute the value and length of a multi-byte UTF-8 character that |
| 1106 | +** begins at z[0]. Return the length. Write the Unicode value into *pU. |
| 1107 | +** |
| 1108 | +** This routine only works for *multi-byte* UTF-8 characters. |
| 1109 | +*/ |
| 1110 | +static int decodeUtf8(const unsigned char *z, int *pU){ |
| 1111 | + if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){ |
| 1112 | + *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f); |
| 1113 | + return 2; |
| 1114 | + } |
| 1115 | + if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){ |
| 1116 | + *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f); |
| 1117 | + return 3; |
| 1118 | + } |
| 1119 | + if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 |
| 1120 | + && (z[3] & 0xc0)==0x80 |
| 1121 | + ){ |
| 1122 | + *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6 |
| 1123 | + | (z[4] & 0x3f); |
| 1124 | + return 4; |
| 1125 | + } |
| 1126 | + *pU = 0; |
| 1127 | + return 1; |
| 1128 | +} |
| 1129 | + |
| 1130 | + |
| 1131 | +#if 0 /* NOT USED */ |
| 1132 | +/* |
| 1133 | +** Return the width, in display columns, of a UTF-8 string. |
| 1134 | +** |
| 1135 | +** Each normal character counts as 1. Zero-width characters count |
| 1136 | +** as zero, and double-width characters count as 2. |
| 1137 | +*/ |
| 1138 | +int cli_wcswidth(const char *z){ |
| 1139 | + const unsigned char *a = (const unsigned char*)z; |
| 1140 | + int n = 0; |
| 1141 | + int i = 0; |
| 1142 | + unsigned char c; |
| 1143 | + while( (c = a[i])!=0 ){ |
| 1144 | + if( c>=0xc0 ){ |
| 1145 | + int u; |
| 1146 | + int len = decodeUtf8(&a[i], &u); |
| 1147 | + i += len; |
| 1148 | + n += cli_wcwidth(u); |
| 1149 | + }else if( c>=' ' ){ |
| 1150 | + n++; |
| 1151 | + i++; |
| 1152 | + }else{ |
| 1153 | + i++; |
| 1154 | + } |
| 1155 | + } |
| 1156 | + return n; |
| 1157 | +} |
| 1158 | +#endif |
| 1159 | + |
| 1160 | +/* |
| 1161 | +** Output string zUtf to stdout as w characters. If w is negative, |
| 1750 | 1162 | ** then right-justify the text. W is the width in UTF-8 characters, not |
| 1751 | 1163 | ** in bytes. This is different from the %*.*s specification in printf |
| 1752 | 1164 | ** since with %*.*s the width is measured in bytes, not characters. |
| 1165 | +** |
| 1166 | +** Take into account zero-width and double-width Unicode characters. |
| 1167 | +** In other words, a zero-width character does not count toward the |
| 1168 | +** the w limit. A double-width character counts as two. |
| 1753 | 1169 | */ |
| 1754 | | -static void utf8_width_print(int w, const char *zUtf){ |
| 1755 | | - int i; |
| 1756 | | - int n; |
| 1170 | +static void utf8_width_print(FILE *out, int w, const char *zUtf){ |
| 1171 | + const unsigned char *a = (const unsigned char*)zUtf; |
| 1172 | + unsigned char c; |
| 1173 | + int i = 0; |
| 1174 | + int n = 0; |
| 1757 | 1175 | int aw = w<0 ? -w : w; |
| 1758 | 1176 | if( zUtf==0 ) zUtf = ""; |
| 1759 | | - for(i=n=0; zUtf[i]; i++){ |
| 1760 | | - if( (zUtf[i]&0xc0)!=0x80 ){ |
| 1177 | + while( (c = a[i])!=0 ){ |
| 1178 | + if( (c&0xc0)==0xc0 ){ |
| 1179 | + int u; |
| 1180 | + int len = decodeUtf8(a+i, &u); |
| 1181 | + int x = cli_wcwidth(u); |
| 1182 | + if( x+n>aw ){ |
| 1183 | + break; |
| 1184 | + } |
| 1185 | + i += len; |
| 1186 | + n += x; |
| 1187 | + }else if( n>=aw ){ |
| 1188 | + break; |
| 1189 | + }else{ |
| 1761 | 1190 | n++; |
| 1762 | | - if( n==aw ){ |
| 1763 | | - do{ i++; }while( (zUtf[i]&0xc0)==0x80 ); |
| 1764 | | - break; |
| 1765 | | - } |
| 1191 | + i++; |
| 1766 | 1192 | } |
| 1767 | 1193 | } |
| 1768 | 1194 | if( n>=aw ){ |
| 1769 | | - oputf("%.*s", i, zUtf); |
| 1195 | + sqlite3_fprintf(out, "%.*s", i, zUtf); |
| 1770 | 1196 | }else if( w<0 ){ |
| 1771 | | - oputf("%*s%s", aw-n, "", zUtf); |
| 1197 | + sqlite3_fprintf(out, "%*s%s", aw-n, "", zUtf); |
| 1772 | 1198 | }else{ |
| 1773 | | - oputf("%s%*s", zUtf, aw-n, ""); |
| 1199 | + sqlite3_fprintf(out, "%s%*s", zUtf, aw-n, ""); |
| 1774 | 1200 | } |
| 1775 | 1201 | } |
| 1776 | 1202 | |
| 1777 | 1203 | |
| 1778 | 1204 | /* |
| | @@ -1834,11 +1260,11 @@ |
| 1834 | 1260 | struct __stat64 x = {0}; |
| 1835 | 1261 | # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0) |
| 1836 | 1262 | /* On Windows, open first, then check the stream nature. This order |
| 1837 | 1263 | ** is necessary because _stat() and sibs, when checking a named pipe, |
| 1838 | 1264 | ** effectively break the pipe as its supplier sees it. */ |
| 1839 | | - FILE *rv = fopen(zFile, "rb"); |
| 1265 | + FILE *rv = sqlite3_fopen(zFile, "rb"); |
| 1840 | 1266 | if( rv==0 ) return 0; |
| 1841 | 1267 | if( _fstat64(_fileno(rv), &x) != 0 |
| 1842 | 1268 | || !STAT_CHR_SRC(x.st_mode)){ |
| 1843 | 1269 | fclose(rv); |
| 1844 | 1270 | rv = 0; |
| | @@ -1848,11 +1274,11 @@ |
| 1848 | 1274 | struct stat x = {0}; |
| 1849 | 1275 | int rc = stat(zFile, &x); |
| 1850 | 1276 | # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode)) |
| 1851 | 1277 | if( rc!=0 ) return 0; |
| 1852 | 1278 | if( STAT_CHR_SRC(x.st_mode) ){ |
| 1853 | | - return fopen(zFile, "rb"); |
| 1279 | + return sqlite3_fopen(zFile, "rb"); |
| 1854 | 1280 | }else{ |
| 1855 | 1281 | return 0; |
| 1856 | 1282 | } |
| 1857 | 1283 | #endif |
| 1858 | 1284 | #undef STAT_CHR_SRC |
| | @@ -1875,11 +1301,11 @@ |
| 1875 | 1301 | if( n+100>nLine ){ |
| 1876 | 1302 | nLine = nLine*2 + 100; |
| 1877 | 1303 | zLine = realloc(zLine, nLine); |
| 1878 | 1304 | shell_check_oom(zLine); |
| 1879 | 1305 | } |
| 1880 | | - if( fgets(&zLine[n], nLine - n, in)==0 ){ |
| 1306 | + if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){ |
| 1881 | 1307 | if( n==0 ){ |
| 1882 | 1308 | free(zLine); |
| 1883 | 1309 | return 0; |
| 1884 | 1310 | } |
| 1885 | 1311 | zLine[n] = 0; |
| | @@ -2941,11 +2367,11 @@ |
| 2941 | 2367 | ** |
| 2942 | 2368 | ****************************************************************************** |
| 2943 | 2369 | ** |
| 2944 | 2370 | ** This SQLite extension implements functions that compute SHA3 hashes |
| 2945 | 2371 | ** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard. |
| 2946 | | -** Two SQL functions are implemented: |
| 2372 | +** Three SQL functions are implemented: |
| 2947 | 2373 | ** |
| 2948 | 2374 | ** sha3(X,SIZE) |
| 2949 | 2375 | ** sha3_agg(Y,SIZE) |
| 2950 | 2376 | ** sha3_query(Z,SIZE) |
| 2951 | 2377 | ** |
| | @@ -3783,10 +3209,422 @@ |
| 3783 | 3209 | } |
| 3784 | 3210 | return rc; |
| 3785 | 3211 | } |
| 3786 | 3212 | |
| 3787 | 3213 | /************************* End ../ext/misc/shathree.c ********************/ |
| 3214 | +/************************* Begin ../ext/misc/sha1.c ******************/ |
| 3215 | +/* |
| 3216 | +** 2017-01-27 |
| 3217 | +** |
| 3218 | +** The author disclaims copyright to this source code. In place of |
| 3219 | +** a legal notice, here is a blessing: |
| 3220 | +** |
| 3221 | +** May you do good and not evil. |
| 3222 | +** May you find forgiveness for yourself and forgive others. |
| 3223 | +** May you share freely, never taking more than you give. |
| 3224 | +** |
| 3225 | +****************************************************************************** |
| 3226 | +** |
| 3227 | +** This SQLite extension implements functions that compute SHA1 hashes. |
| 3228 | +** Two SQL functions are implemented: |
| 3229 | +** |
| 3230 | +** sha1(X) |
| 3231 | +** sha1_query(Y) |
| 3232 | +** |
| 3233 | +** The sha1(X) function computes the SHA1 hash of the input X, or NULL if |
| 3234 | +** X is NULL. |
| 3235 | +** |
| 3236 | +** The sha1_query(Y) function evalutes all queries in the SQL statements of Y |
| 3237 | +** and returns a hash of their results. |
| 3238 | +*/ |
| 3239 | +/* #include "sqlite3ext.h" */ |
| 3240 | +SQLITE_EXTENSION_INIT1 |
| 3241 | +#include <assert.h> |
| 3242 | +#include <string.h> |
| 3243 | +#include <stdarg.h> |
| 3244 | + |
| 3245 | +/****************************************************************************** |
| 3246 | +** The Hash Engine |
| 3247 | +*/ |
| 3248 | +/* Context for the SHA1 hash */ |
| 3249 | +typedef struct SHA1Context SHA1Context; |
| 3250 | +struct SHA1Context { |
| 3251 | + unsigned int state[5]; |
| 3252 | + unsigned int count[2]; |
| 3253 | + unsigned char buffer[64]; |
| 3254 | +}; |
| 3255 | + |
| 3256 | +#define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r)) |
| 3257 | +#define rol(x,k) SHA_ROT(x,k,32-(k)) |
| 3258 | +#define ror(x,k) SHA_ROT(x,32-(k),k) |
| 3259 | + |
| 3260 | +#define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \ |
| 3261 | + |(rol(block[i],8)&0x00FF00FF)) |
| 3262 | +#define blk0be(i) block[i] |
| 3263 | +#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ |
| 3264 | + ^block[(i+2)&15]^block[i&15],1)) |
| 3265 | + |
| 3266 | +/* |
| 3267 | + * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 |
| 3268 | + * |
| 3269 | + * Rl0() for little-endian and Rb0() for big-endian. Endianness is |
| 3270 | + * determined at run-time. |
| 3271 | + */ |
| 3272 | +#define Rl0(v,w,x,y,z,i) \ |
| 3273 | + z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2); |
| 3274 | +#define Rb0(v,w,x,y,z,i) \ |
| 3275 | + z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2); |
| 3276 | +#define R1(v,w,x,y,z,i) \ |
| 3277 | + z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2); |
| 3278 | +#define R2(v,w,x,y,z,i) \ |
| 3279 | + z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2); |
| 3280 | +#define R3(v,w,x,y,z,i) \ |
| 3281 | + z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2); |
| 3282 | +#define R4(v,w,x,y,z,i) \ |
| 3283 | + z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2); |
| 3284 | + |
| 3285 | +/* |
| 3286 | + * Hash a single 512-bit block. This is the core of the algorithm. |
| 3287 | + */ |
| 3288 | +static void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]){ |
| 3289 | + unsigned int qq[5]; /* a, b, c, d, e; */ |
| 3290 | + static int one = 1; |
| 3291 | + unsigned int block[16]; |
| 3292 | + memcpy(block, buffer, 64); |
| 3293 | + memcpy(qq,state,5*sizeof(unsigned int)); |
| 3294 | + |
| 3295 | +#define a qq[0] |
| 3296 | +#define b qq[1] |
| 3297 | +#define c qq[2] |
| 3298 | +#define d qq[3] |
| 3299 | +#define e qq[4] |
| 3300 | + |
| 3301 | + /* Copy p->state[] to working vars */ |
| 3302 | + /* |
| 3303 | + a = state[0]; |
| 3304 | + b = state[1]; |
| 3305 | + c = state[2]; |
| 3306 | + d = state[3]; |
| 3307 | + e = state[4]; |
| 3308 | + */ |
| 3309 | + |
| 3310 | + /* 4 rounds of 20 operations each. Loop unrolled. */ |
| 3311 | + if( 1 == *(unsigned char*)&one ){ |
| 3312 | + 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); |
| 3313 | + 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); |
| 3314 | + 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); |
| 3315 | + 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); |
| 3316 | + }else{ |
| 3317 | + 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); |
| 3318 | + 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); |
| 3319 | + 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); |
| 3320 | + 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); |
| 3321 | + } |
| 3322 | + 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); |
| 3323 | + 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); |
| 3324 | + 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); |
| 3325 | + 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); |
| 3326 | + 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); |
| 3327 | + 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); |
| 3328 | + 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); |
| 3329 | + 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); |
| 3330 | + 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); |
| 3331 | + 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); |
| 3332 | + 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); |
| 3333 | + 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); |
| 3334 | + 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); |
| 3335 | + 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); |
| 3336 | + 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); |
| 3337 | + 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); |
| 3338 | + |
| 3339 | + /* Add the working vars back into context.state[] */ |
| 3340 | + state[0] += a; |
| 3341 | + state[1] += b; |
| 3342 | + state[2] += c; |
| 3343 | + state[3] += d; |
| 3344 | + state[4] += e; |
| 3345 | + |
| 3346 | +#undef a |
| 3347 | +#undef b |
| 3348 | +#undef c |
| 3349 | +#undef d |
| 3350 | +#undef e |
| 3351 | +} |
| 3352 | + |
| 3353 | + |
| 3354 | +/* Initialize a SHA1 context */ |
| 3355 | +static void hash_init(SHA1Context *p){ |
| 3356 | + /* SHA1 initialization constants */ |
| 3357 | + p->state[0] = 0x67452301; |
| 3358 | + p->state[1] = 0xEFCDAB89; |
| 3359 | + p->state[2] = 0x98BADCFE; |
| 3360 | + p->state[3] = 0x10325476; |
| 3361 | + p->state[4] = 0xC3D2E1F0; |
| 3362 | + p->count[0] = p->count[1] = 0; |
| 3363 | +} |
| 3364 | + |
| 3365 | +/* Add new content to the SHA1 hash */ |
| 3366 | +static void hash_step( |
| 3367 | + SHA1Context *p, /* Add content to this context */ |
| 3368 | + const unsigned char *data, /* Data to be added */ |
| 3369 | + unsigned int len /* Number of bytes in data */ |
| 3370 | +){ |
| 3371 | + unsigned int i, j; |
| 3372 | + |
| 3373 | + j = p->count[0]; |
| 3374 | + if( (p->count[0] += len << 3) < j ){ |
| 3375 | + p->count[1] += (len>>29)+1; |
| 3376 | + } |
| 3377 | + j = (j >> 3) & 63; |
| 3378 | + if( (j + len) > 63 ){ |
| 3379 | + (void)memcpy(&p->buffer[j], data, (i = 64-j)); |
| 3380 | + SHA1Transform(p->state, p->buffer); |
| 3381 | + for(; i + 63 < len; i += 64){ |
| 3382 | + SHA1Transform(p->state, &data[i]); |
| 3383 | + } |
| 3384 | + j = 0; |
| 3385 | + }else{ |
| 3386 | + i = 0; |
| 3387 | + } |
| 3388 | + (void)memcpy(&p->buffer[j], &data[i], len - i); |
| 3389 | +} |
| 3390 | + |
| 3391 | +/* Compute a string using sqlite3_vsnprintf() and hash it */ |
| 3392 | +static void hash_step_vformat( |
| 3393 | + SHA1Context *p, /* Add content to this context */ |
| 3394 | + const char *zFormat, |
| 3395 | + ... |
| 3396 | +){ |
| 3397 | + va_list ap; |
| 3398 | + int n; |
| 3399 | + char zBuf[50]; |
| 3400 | + va_start(ap, zFormat); |
| 3401 | + sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); |
| 3402 | + va_end(ap); |
| 3403 | + n = (int)strlen(zBuf); |
| 3404 | + hash_step(p, (unsigned char*)zBuf, n); |
| 3405 | +} |
| 3406 | + |
| 3407 | + |
| 3408 | +/* Add padding and compute the message digest. Render the |
| 3409 | +** message digest as lower-case hexadecimal and put it into |
| 3410 | +** zOut[]. zOut[] must be at least 41 bytes long. */ |
| 3411 | +static void hash_finish( |
| 3412 | + SHA1Context *p, /* The SHA1 context to finish and render */ |
| 3413 | + char *zOut, /* Store hex or binary hash here */ |
| 3414 | + int bAsBinary /* 1 for binary hash, 0 for hex hash */ |
| 3415 | +){ |
| 3416 | + unsigned int i; |
| 3417 | + unsigned char finalcount[8]; |
| 3418 | + unsigned char digest[20]; |
| 3419 | + static const char zEncode[] = "0123456789abcdef"; |
| 3420 | + |
| 3421 | + for (i = 0; i < 8; i++){ |
| 3422 | + finalcount[i] = (unsigned char)((p->count[(i >= 4 ? 0 : 1)] |
| 3423 | + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ |
| 3424 | + } |
| 3425 | + hash_step(p, (const unsigned char *)"\200", 1); |
| 3426 | + while ((p->count[0] & 504) != 448){ |
| 3427 | + hash_step(p, (const unsigned char *)"\0", 1); |
| 3428 | + } |
| 3429 | + hash_step(p, finalcount, 8); /* Should cause a SHA1Transform() */ |
| 3430 | + for (i = 0; i < 20; i++){ |
| 3431 | + digest[i] = (unsigned char)((p->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
| 3432 | + } |
| 3433 | + if( bAsBinary ){ |
| 3434 | + memcpy(zOut, digest, 20); |
| 3435 | + }else{ |
| 3436 | + for(i=0; i<20; i++){ |
| 3437 | + zOut[i*2] = zEncode[(digest[i]>>4)&0xf]; |
| 3438 | + zOut[i*2+1] = zEncode[digest[i] & 0xf]; |
| 3439 | + } |
| 3440 | + zOut[i*2]= 0; |
| 3441 | + } |
| 3442 | +} |
| 3443 | +/* End of the hashing logic |
| 3444 | +*****************************************************************************/ |
| 3445 | + |
| 3446 | +/* |
| 3447 | +** Implementation of the sha1(X) function. |
| 3448 | +** |
| 3449 | +** Return a lower-case hexadecimal rendering of the SHA1 hash of the |
| 3450 | +** argument X. If X is a BLOB, it is hashed as is. For all other |
| 3451 | +** types of input, X is converted into a UTF-8 string and the string |
| 3452 | +** is hash without the trailing 0x00 terminator. The hash of a NULL |
| 3453 | +** value is NULL. |
| 3454 | +*/ |
| 3455 | +static void sha1Func( |
| 3456 | + sqlite3_context *context, |
| 3457 | + int argc, |
| 3458 | + sqlite3_value **argv |
| 3459 | +){ |
| 3460 | + SHA1Context cx; |
| 3461 | + int eType = sqlite3_value_type(argv[0]); |
| 3462 | + int nByte = sqlite3_value_bytes(argv[0]); |
| 3463 | + char zOut[44]; |
| 3464 | + |
| 3465 | + assert( argc==1 ); |
| 3466 | + if( eType==SQLITE_NULL ) return; |
| 3467 | + hash_init(&cx); |
| 3468 | + if( eType==SQLITE_BLOB ){ |
| 3469 | + hash_step(&cx, sqlite3_value_blob(argv[0]), nByte); |
| 3470 | + }else{ |
| 3471 | + hash_step(&cx, sqlite3_value_text(argv[0]), nByte); |
| 3472 | + } |
| 3473 | + if( sqlite3_user_data(context)!=0 ){ |
| 3474 | + hash_finish(&cx, zOut, 1); |
| 3475 | + sqlite3_result_blob(context, zOut, 20, SQLITE_TRANSIENT); |
| 3476 | + }else{ |
| 3477 | + hash_finish(&cx, zOut, 0); |
| 3478 | + sqlite3_result_blob(context, zOut, 40, SQLITE_TRANSIENT); |
| 3479 | + } |
| 3480 | +} |
| 3481 | + |
| 3482 | +/* |
| 3483 | +** Implementation of the sha1_query(SQL) function. |
| 3484 | +** |
| 3485 | +** This function compiles and runs the SQL statement(s) given in the |
| 3486 | +** argument. The results are hashed using SHA1 and that hash is returned. |
| 3487 | +** |
| 3488 | +** The original SQL text is included as part of the hash. |
| 3489 | +** |
| 3490 | +** The hash is not just a concatenation of the outputs. Each query |
| 3491 | +** is delimited and each row and value within the query is delimited, |
| 3492 | +** with all values being marked with their datatypes. |
| 3493 | +*/ |
| 3494 | +static void sha1QueryFunc( |
| 3495 | + sqlite3_context *context, |
| 3496 | + int argc, |
| 3497 | + sqlite3_value **argv |
| 3498 | +){ |
| 3499 | + sqlite3 *db = sqlite3_context_db_handle(context); |
| 3500 | + const char *zSql = (const char*)sqlite3_value_text(argv[0]); |
| 3501 | + sqlite3_stmt *pStmt = 0; |
| 3502 | + int nCol; /* Number of columns in the result set */ |
| 3503 | + int i; /* Loop counter */ |
| 3504 | + int rc; |
| 3505 | + int n; |
| 3506 | + const char *z; |
| 3507 | + SHA1Context cx; |
| 3508 | + char zOut[44]; |
| 3509 | + |
| 3510 | + assert( argc==1 ); |
| 3511 | + if( zSql==0 ) return; |
| 3512 | + hash_init(&cx); |
| 3513 | + while( zSql[0] ){ |
| 3514 | + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); |
| 3515 | + if( rc ){ |
| 3516 | + char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", |
| 3517 | + zSql, sqlite3_errmsg(db)); |
| 3518 | + sqlite3_finalize(pStmt); |
| 3519 | + sqlite3_result_error(context, zMsg, -1); |
| 3520 | + sqlite3_free(zMsg); |
| 3521 | + return; |
| 3522 | + } |
| 3523 | + if( !sqlite3_stmt_readonly(pStmt) ){ |
| 3524 | + char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); |
| 3525 | + sqlite3_finalize(pStmt); |
| 3526 | + sqlite3_result_error(context, zMsg, -1); |
| 3527 | + sqlite3_free(zMsg); |
| 3528 | + return; |
| 3529 | + } |
| 3530 | + nCol = sqlite3_column_count(pStmt); |
| 3531 | + z = sqlite3_sql(pStmt); |
| 3532 | + n = (int)strlen(z); |
| 3533 | + hash_step_vformat(&cx,"S%d:",n); |
| 3534 | + hash_step(&cx,(unsigned char*)z,n); |
| 3535 | + |
| 3536 | + /* Compute a hash over the result of the query */ |
| 3537 | + while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 3538 | + hash_step(&cx,(const unsigned char*)"R",1); |
| 3539 | + for(i=0; i<nCol; i++){ |
| 3540 | + switch( sqlite3_column_type(pStmt,i) ){ |
| 3541 | + case SQLITE_NULL: { |
| 3542 | + hash_step(&cx, (const unsigned char*)"N",1); |
| 3543 | + break; |
| 3544 | + } |
| 3545 | + case SQLITE_INTEGER: { |
| 3546 | + sqlite3_uint64 u; |
| 3547 | + int j; |
| 3548 | + unsigned char x[9]; |
| 3549 | + sqlite3_int64 v = sqlite3_column_int64(pStmt,i); |
| 3550 | + memcpy(&u, &v, 8); |
| 3551 | + for(j=8; j>=1; j--){ |
| 3552 | + x[j] = u & 0xff; |
| 3553 | + u >>= 8; |
| 3554 | + } |
| 3555 | + x[0] = 'I'; |
| 3556 | + hash_step(&cx, x, 9); |
| 3557 | + break; |
| 3558 | + } |
| 3559 | + case SQLITE_FLOAT: { |
| 3560 | + sqlite3_uint64 u; |
| 3561 | + int j; |
| 3562 | + unsigned char x[9]; |
| 3563 | + double r = sqlite3_column_double(pStmt,i); |
| 3564 | + memcpy(&u, &r, 8); |
| 3565 | + for(j=8; j>=1; j--){ |
| 3566 | + x[j] = u & 0xff; |
| 3567 | + u >>= 8; |
| 3568 | + } |
| 3569 | + x[0] = 'F'; |
| 3570 | + hash_step(&cx,x,9); |
| 3571 | + break; |
| 3572 | + } |
| 3573 | + case SQLITE_TEXT: { |
| 3574 | + int n2 = sqlite3_column_bytes(pStmt, i); |
| 3575 | + const unsigned char *z2 = sqlite3_column_text(pStmt, i); |
| 3576 | + hash_step_vformat(&cx,"T%d:",n2); |
| 3577 | + hash_step(&cx, z2, n2); |
| 3578 | + break; |
| 3579 | + } |
| 3580 | + case SQLITE_BLOB: { |
| 3581 | + int n2 = sqlite3_column_bytes(pStmt, i); |
| 3582 | + const unsigned char *z2 = sqlite3_column_blob(pStmt, i); |
| 3583 | + hash_step_vformat(&cx,"B%d:",n2); |
| 3584 | + hash_step(&cx, z2, n2); |
| 3585 | + break; |
| 3586 | + } |
| 3587 | + } |
| 3588 | + } |
| 3589 | + } |
| 3590 | + sqlite3_finalize(pStmt); |
| 3591 | + } |
| 3592 | + hash_finish(&cx, zOut, 0); |
| 3593 | + sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT); |
| 3594 | +} |
| 3595 | + |
| 3596 | + |
| 3597 | +#ifdef _WIN32 |
| 3598 | + |
| 3599 | +#endif |
| 3600 | +int sqlite3_sha_init( |
| 3601 | + sqlite3 *db, |
| 3602 | + char **pzErrMsg, |
| 3603 | + const sqlite3_api_routines *pApi |
| 3604 | +){ |
| 3605 | + int rc = SQLITE_OK; |
| 3606 | + static int one = 1; |
| 3607 | + SQLITE_EXTENSION_INIT2(pApi); |
| 3608 | + (void)pzErrMsg; /* Unused parameter */ |
| 3609 | + rc = sqlite3_create_function(db, "sha1", 1, |
| 3610 | + SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, |
| 3611 | + 0, sha1Func, 0, 0); |
| 3612 | + if( rc==SQLITE_OK ){ |
| 3613 | + rc = sqlite3_create_function(db, "sha1b", 1, |
| 3614 | + SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, |
| 3615 | + (void*)&one, sha1Func, 0, 0); |
| 3616 | + } |
| 3617 | + if( rc==SQLITE_OK ){ |
| 3618 | + rc = sqlite3_create_function(db, "sha1_query", 1, |
| 3619 | + SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
| 3620 | + sha1QueryFunc, 0, 0); |
| 3621 | + } |
| 3622 | + return rc; |
| 3623 | +} |
| 3624 | + |
| 3625 | +/************************* End ../ext/misc/sha1.c ********************/ |
| 3788 | 3626 | /************************* Begin ../ext/misc/uint.c ******************/ |
| 3789 | 3627 | /* |
| 3790 | 3628 | ** 2020-04-14 |
| 3791 | 3629 | ** |
| 3792 | 3630 | ** The author disclaims copyright to this source code. In place of |
| | @@ -5075,11 +4913,11 @@ |
| 5075 | 4913 | }else if( !p->bSorted || y>=p->a[p->nUsed-1] ){ |
| 5076 | 4914 | p->a[p->nUsed++] = y; |
| 5077 | 4915 | }else if( p->bKeepSorted ){ |
| 5078 | 4916 | int i; |
| 5079 | 4917 | i = percentBinarySearch(p, y, 0); |
| 5080 | | - if( i<p->nUsed ){ |
| 4918 | + if( i<(int)p->nUsed ){ |
| 5081 | 4919 | memmove(&p->a[i+1], &p->a[i], (p->nUsed-i)*sizeof(p->a[0])); |
| 5082 | 4920 | } |
| 5083 | 4921 | p->a[i] = y; |
| 5084 | 4922 | p->nUsed++; |
| 5085 | 4923 | }else{ |
| | @@ -5191,11 +5029,11 @@ |
| 5191 | 5029 | |
| 5192 | 5030 | /* Find and remove the row */ |
| 5193 | 5031 | i = percentBinarySearch(p, y, 1); |
| 5194 | 5032 | if( i>=0 ){ |
| 5195 | 5033 | p->nUsed--; |
| 5196 | | - if( i<p->nUsed ){ |
| 5034 | + if( i<(int)p->nUsed ){ |
| 5197 | 5035 | memmove(&p->a[i], &p->a[i+1], (p->nUsed - i)*sizeof(p->a[0])); |
| 5198 | 5036 | } |
| 5199 | 5037 | } |
| 5200 | 5038 | } |
| 5201 | 5039 | |
| | @@ -5251,15 +5089,15 @@ |
| 5251 | 5089 | sqlite3 *db, |
| 5252 | 5090 | char **pzErrMsg, |
| 5253 | 5091 | const sqlite3_api_routines *pApi |
| 5254 | 5092 | ){ |
| 5255 | 5093 | int rc = SQLITE_OK; |
| 5256 | | - int i; |
| 5257 | | -#if defined(SQLITE3_H) || defined(SQLITE_STATIC_PERCENTILE) |
| 5258 | | - (void)pApi; /* Unused parameter */ |
| 5259 | | -#else |
| 5094 | + unsigned int i; |
| 5095 | +#ifdef SQLITE3EXT_H |
| 5260 | 5096 | SQLITE_EXTENSION_INIT2(pApi); |
| 5097 | +#else |
| 5098 | + (void)pApi; /* Unused parameter */ |
| 5261 | 5099 | #endif |
| 5262 | 5100 | (void)pzErrMsg; /* Unused parameter */ |
| 5263 | 5101 | for(i=0; i<sizeof(aPercentFunc)/sizeof(aPercentFunc[0]); i++){ |
| 5264 | 5102 | rc = sqlite3_create_window_function(db, |
| 5265 | 5103 | aPercentFunc[i].zName, |
| | @@ -7012,50 +6850,58 @@ |
| 7012 | 6850 | aIdx[4] = i; |
| 7013 | 6851 | idxNum |= 0x40; |
| 7014 | 6852 | } |
| 7015 | 6853 | continue; |
| 7016 | 6854 | } |
| 7017 | | - if( pConstraint->iColumn==SERIES_COLUMN_VALUE ){ |
| 7018 | | - switch( op ){ |
| 7019 | | - case SQLITE_INDEX_CONSTRAINT_EQ: |
| 7020 | | - case SQLITE_INDEX_CONSTRAINT_IS: { |
| 7021 | | - idxNum |= 0x0080; |
| 7022 | | - idxNum &= ~0x3300; |
| 7023 | | - aIdx[5] = i; |
| 7024 | | - aIdx[6] = -1; |
| 7025 | | - bStartSeen = 1; |
| 7026 | | - break; |
| 7027 | | - } |
| 7028 | | - case SQLITE_INDEX_CONSTRAINT_GE: { |
| 7029 | | - if( idxNum & 0x0080 ) break; |
| 7030 | | - idxNum |= 0x0100; |
| 7031 | | - idxNum &= ~0x0200; |
| 7032 | | - aIdx[5] = i; |
| 7033 | | - bStartSeen = 1; |
| 7034 | | - break; |
| 7035 | | - } |
| 7036 | | - case SQLITE_INDEX_CONSTRAINT_GT: { |
| 7037 | | - if( idxNum & 0x0080 ) break; |
| 7038 | | - idxNum |= 0x0200; |
| 7039 | | - idxNum &= ~0x0100; |
| 7040 | | - aIdx[5] = i; |
| 7041 | | - bStartSeen = 1; |
| 7042 | | - break; |
| 7043 | | - } |
| 7044 | | - case SQLITE_INDEX_CONSTRAINT_LE: { |
| 7045 | | - if( idxNum & 0x0080 ) break; |
| 7046 | | - idxNum |= 0x1000; |
| 7047 | | - idxNum &= ~0x2000; |
| 7048 | | - aIdx[6] = i; |
| 7049 | | - break; |
| 7050 | | - } |
| 7051 | | - case SQLITE_INDEX_CONSTRAINT_LT: { |
| 7052 | | - if( idxNum & 0x0080 ) break; |
| 7053 | | - idxNum |= 0x2000; |
| 7054 | | - idxNum &= ~0x1000; |
| 7055 | | - aIdx[6] = i; |
| 7056 | | - break; |
| 6855 | + if( pConstraint->iColumn<SERIES_COLUMN_START ){ |
| 6856 | + if( pConstraint->iColumn==SERIES_COLUMN_VALUE && pConstraint->usable ){ |
| 6857 | + switch( op ){ |
| 6858 | + case SQLITE_INDEX_CONSTRAINT_EQ: |
| 6859 | + case SQLITE_INDEX_CONSTRAINT_IS: { |
| 6860 | + idxNum |= 0x0080; |
| 6861 | + idxNum &= ~0x3300; |
| 6862 | + aIdx[5] = i; |
| 6863 | + aIdx[6] = -1; |
| 6864 | +#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
| 6865 | + bStartSeen = 1; |
| 6866 | +#endif |
| 6867 | + break; |
| 6868 | + } |
| 6869 | + case SQLITE_INDEX_CONSTRAINT_GE: { |
| 6870 | + if( idxNum & 0x0080 ) break; |
| 6871 | + idxNum |= 0x0100; |
| 6872 | + idxNum &= ~0x0200; |
| 6873 | + aIdx[5] = i; |
| 6874 | +#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
| 6875 | + bStartSeen = 1; |
| 6876 | +#endif |
| 6877 | + break; |
| 6878 | + } |
| 6879 | + case SQLITE_INDEX_CONSTRAINT_GT: { |
| 6880 | + if( idxNum & 0x0080 ) break; |
| 6881 | + idxNum |= 0x0200; |
| 6882 | + idxNum &= ~0x0100; |
| 6883 | + aIdx[5] = i; |
| 6884 | +#ifndef ZERO_ARGUMENT_GENERATE_SERIES |
| 6885 | + bStartSeen = 1; |
| 6886 | +#endif |
| 6887 | + break; |
| 6888 | + } |
| 6889 | + case SQLITE_INDEX_CONSTRAINT_LE: { |
| 6890 | + if( idxNum & 0x0080 ) break; |
| 6891 | + idxNum |= 0x1000; |
| 6892 | + idxNum &= ~0x2000; |
| 6893 | + aIdx[6] = i; |
| 6894 | + break; |
| 6895 | + } |
| 6896 | + case SQLITE_INDEX_CONSTRAINT_LT: { |
| 6897 | + if( idxNum & 0x0080 ) break; |
| 6898 | + idxNum |= 0x2000; |
| 6899 | + idxNum &= ~0x1000; |
| 6900 | + aIdx[6] = i; |
| 6901 | + break; |
| 6902 | + } |
| 7057 | 6903 | } |
| 7058 | 6904 | } |
| 7059 | 6905 | continue; |
| 7060 | 6906 | } |
| 7061 | 6907 | iCol = pConstraint->iColumn - SERIES_COLUMN_START; |
| | @@ -8115,11 +7961,11 @@ |
| 8115 | 7961 | ** If the optional MTIME argument is present, then it is interpreted |
| 8116 | 7962 | ** as an integer - the number of seconds since the unix epoch. The |
| 8117 | 7963 | ** modification-time of the target file is set to this value before |
| 8118 | 7964 | ** returning. |
| 8119 | 7965 | ** |
| 8120 | | -** If three or more arguments are passed to this function and an |
| 7966 | +** If five or more arguments are passed to this function and an |
| 8121 | 7967 | ** error is encountered, an exception is raised. |
| 8122 | 7968 | ** |
| 8123 | 7969 | ** READFILE(FILE): |
| 8124 | 7970 | ** |
| 8125 | 7971 | ** Read and return the contents of file FILE (type blob) from disk. |
| | @@ -8185,10 +8031,17 @@ |
| 8185 | 8031 | # define lstat(path,buf) stat(path,buf) |
| 8186 | 8032 | #endif |
| 8187 | 8033 | #include <time.h> |
| 8188 | 8034 | #include <errno.h> |
| 8189 | 8035 | |
| 8036 | +/* When used as part of the CLI, the sqlite3_stdio.h module will have |
| 8037 | +** been included before this one. In that case use the sqlite3_stdio.h |
| 8038 | +** #defines. If not, create our own for fopen(). |
| 8039 | +*/ |
| 8040 | +#ifndef _SQLITE3_STDIO_H_ |
| 8041 | +# define sqlite3_fopen fopen |
| 8042 | +#endif |
| 8190 | 8043 | |
| 8191 | 8044 | /* |
| 8192 | 8045 | ** Structure of the fsdir() table-valued function |
| 8193 | 8046 | */ |
| 8194 | 8047 | /* 0 1 2 3 4 5 */ |
| | @@ -8217,11 +8070,11 @@ |
| 8217 | 8070 | sqlite3_int64 nIn; |
| 8218 | 8071 | void *pBuf; |
| 8219 | 8072 | sqlite3 *db; |
| 8220 | 8073 | int mxBlob; |
| 8221 | 8074 | |
| 8222 | | - in = fopen(zName, "rb"); |
| 8075 | + in = sqlite3_fopen(zName, "rb"); |
| 8223 | 8076 | if( in==0 ){ |
| 8224 | 8077 | /* File does not exist or is unreadable. Leave the result set to NULL. */ |
| 8225 | 8078 | return; |
| 8226 | 8079 | } |
| 8227 | 8080 | fseek(in, 0, SEEK_END); |
| | @@ -8472,11 +8325,11 @@ |
| 8472 | 8325 | } |
| 8473 | 8326 | }else{ |
| 8474 | 8327 | sqlite3_int64 nWrite = 0; |
| 8475 | 8328 | const char *z; |
| 8476 | 8329 | int rc = 0; |
| 8477 | | - FILE *out = fopen(zFile, "wb"); |
| 8330 | + FILE *out = sqlite3_fopen(zFile, "wb"); |
| 8478 | 8331 | if( out==0 ) return 1; |
| 8479 | 8332 | z = (const char*)sqlite3_value_blob(pData); |
| 8480 | 8333 | if( z ){ |
| 8481 | 8334 | sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out); |
| 8482 | 8335 | nWrite = sqlite3_value_bytes(pData); |
| | @@ -10333,10 +10186,18 @@ |
| 10333 | 10186 | #ifndef SQLITE_NO_STDINT |
| 10334 | 10187 | # include <stdint.h> |
| 10335 | 10188 | #endif |
| 10336 | 10189 | |
| 10337 | 10190 | #include <zlib.h> |
| 10191 | + |
| 10192 | +/* When used as part of the CLI, the sqlite3_stdio.h module will have |
| 10193 | +** been included before this one. In that case use the sqlite3_stdio.h |
| 10194 | +** #defines. If not, create our own for fopen(). |
| 10195 | +*/ |
| 10196 | +#ifndef _SQLITE3_STDIO_H_ |
| 10197 | +# define sqlite3_fopen fopen |
| 10198 | +#endif |
| 10338 | 10199 | |
| 10339 | 10200 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 10340 | 10201 | |
| 10341 | 10202 | #ifndef SQLITE_AMALGAMATION |
| 10342 | 10203 | |
| | @@ -11590,11 +11451,11 @@ |
| 11590 | 11451 | }else{ |
| 11591 | 11452 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 11592 | 11453 | } |
| 11593 | 11454 | |
| 11594 | 11455 | if( 0==pTab->pWriteFd && 0==bInMemory ){ |
| 11595 | | - pCsr->pFile = zFile ? fopen(zFile, "rb") : 0; |
| 11456 | + pCsr->pFile = zFile ? sqlite3_fopen(zFile, "rb") : 0; |
| 11596 | 11457 | if( pCsr->pFile==0 ){ |
| 11597 | 11458 | zipfileCursorErr(pCsr, "cannot open file: %s", zFile); |
| 11598 | 11459 | rc = SQLITE_ERROR; |
| 11599 | 11460 | }else{ |
| 11600 | 11461 | rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd); |
| | @@ -11780,11 +11641,11 @@ |
| 11780 | 11641 | |
| 11781 | 11642 | /* Open a write fd on the file. Also load the entire central directory |
| 11782 | 11643 | ** structure into memory. During the transaction any new file data is |
| 11783 | 11644 | ** appended to the archive file, but the central directory is accumulated |
| 11784 | 11645 | ** in main-memory until the transaction is committed. */ |
| 11785 | | - pTab->pWriteFd = fopen(pTab->zFile, "ab+"); |
| 11646 | + pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+"); |
| 11786 | 11647 | if( pTab->pWriteFd==0 ){ |
| 11787 | 11648 | pTab->base.zErrMsg = sqlite3_mprintf( |
| 11788 | 11649 | "zipfile: failed to open file %s for writing", pTab->zFile |
| 11789 | 11650 | ); |
| 11790 | 11651 | rc = SQLITE_ERROR; |
| | @@ -14233,10 +14094,70 @@ |
| 14233 | 14094 | } |
| 14234 | 14095 | |
| 14235 | 14096 | return rc; |
| 14236 | 14097 | } |
| 14237 | 14098 | |
| 14099 | +/* |
| 14100 | +** This function tests if the schema of the main database of database handle |
| 14101 | +** db contains an object named zTab. Assuming no error occurs, output parameter |
| 14102 | +** (*pbContains) is set to true if zTab exists, or false if it does not. |
| 14103 | +** |
| 14104 | +** Or, if an error occurs, an SQLite error code is returned. The final value |
| 14105 | +** of (*pbContains) is undefined in this case. |
| 14106 | +*/ |
| 14107 | +static int expertDbContainsObject( |
| 14108 | + sqlite3 *db, |
| 14109 | + const char *zTab, |
| 14110 | + int *pbContains /* OUT: True if object exists */ |
| 14111 | +){ |
| 14112 | + const char *zSql = "SELECT 1 FROM sqlite_schema WHERE name = ?"; |
| 14113 | + sqlite3_stmt *pSql = 0; |
| 14114 | + int rc = SQLITE_OK; |
| 14115 | + int ret = 0; |
| 14116 | + |
| 14117 | + rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
| 14118 | + if( rc==SQLITE_OK ){ |
| 14119 | + sqlite3_bind_text(pSql, 1, zTab, -1, SQLITE_STATIC); |
| 14120 | + if( SQLITE_ROW==sqlite3_step(pSql) ){ |
| 14121 | + ret = 1; |
| 14122 | + } |
| 14123 | + rc = sqlite3_finalize(pSql); |
| 14124 | + } |
| 14125 | + |
| 14126 | + *pbContains = ret; |
| 14127 | + return rc; |
| 14128 | +} |
| 14129 | + |
| 14130 | +/* |
| 14131 | +** Execute SQL command zSql using database handle db. If no error occurs, |
| 14132 | +** set (*pzErr) to NULL and return SQLITE_OK. |
| 14133 | +** |
| 14134 | +** If an error does occur, return an SQLite error code and set (*pzErr) to |
| 14135 | +** point to a buffer containing an English language error message. Except, |
| 14136 | +** if the error message begins with "no such module:", then ignore the |
| 14137 | +** error and return as if the SQL statement had succeeded. |
| 14138 | +** |
| 14139 | +** This is used to copy as much of the database schema as possible while |
| 14140 | +** ignoring any errors related to missing virtual table modules. |
| 14141 | +*/ |
| 14142 | +static int expertSchemaSql(sqlite3 *db, const char *zSql, char **pzErr){ |
| 14143 | + int rc = SQLITE_OK; |
| 14144 | + char *zErr = 0; |
| 14145 | + |
| 14146 | + rc = sqlite3_exec(db, zSql, 0, 0, &zErr); |
| 14147 | + if( rc!=SQLITE_OK && zErr ){ |
| 14148 | + int nErr = STRLEN(zErr); |
| 14149 | + if( nErr>=15 && memcmp(zErr, "no such module:", 15)==0 ){ |
| 14150 | + sqlite3_free(zErr); |
| 14151 | + rc = SQLITE_OK; |
| 14152 | + zErr = 0; |
| 14153 | + } |
| 14154 | + } |
| 14155 | + |
| 14156 | + *pzErr = zErr; |
| 14157 | + return rc; |
| 14158 | +} |
| 14238 | 14159 | |
| 14239 | 14160 | static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){ |
| 14240 | 14161 | int rc = idxRegisterVtab(p); |
| 14241 | 14162 | sqlite3_stmt *pSchema = 0; |
| 14242 | 14163 | |
| | @@ -14244,30 +14165,39 @@ |
| 14244 | 14165 | ** |
| 14245 | 14166 | ** 1) Add an entry to the p->pTable list, and |
| 14246 | 14167 | ** 2) Create the equivalent virtual table in dbv. |
| 14247 | 14168 | */ |
| 14248 | 14169 | rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg, |
| 14249 | | - "SELECT type, name, sql, 1 FROM sqlite_schema " |
| 14250 | | - "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' " |
| 14170 | + "SELECT type, name, sql, 1, " |
| 14171 | + " substr(sql,1,14)=='create virtual' COLLATE nocase " |
| 14172 | + "FROM sqlite_schema " |
| 14173 | + "WHERE type IN ('table','view') AND " |
| 14174 | + " substr(name,1,7)!='sqlite_' COLLATE nocase " |
| 14251 | 14175 | " UNION ALL " |
| 14252 | | - "SELECT type, name, sql, 2 FROM sqlite_schema " |
| 14176 | + "SELECT type, name, sql, 2, 0 FROM sqlite_schema " |
| 14253 | 14177 | "WHERE type = 'trigger'" |
| 14254 | 14178 | " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') " |
| 14255 | | - "ORDER BY 4, 1" |
| 14179 | + "ORDER BY 4, 5 DESC, 1" |
| 14256 | 14180 | ); |
| 14257 | 14181 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){ |
| 14258 | 14182 | const char *zType = (const char*)sqlite3_column_text(pSchema, 0); |
| 14259 | 14183 | const char *zName = (const char*)sqlite3_column_text(pSchema, 1); |
| 14260 | 14184 | const char *zSql = (const char*)sqlite3_column_text(pSchema, 2); |
| 14185 | + int bVirtual = sqlite3_column_int(pSchema, 4); |
| 14186 | + int bExists = 0; |
| 14261 | 14187 | |
| 14262 | 14188 | if( zType==0 || zName==0 ) continue; |
| 14263 | | - if( zType[0]=='v' || zType[1]=='r' ){ |
| 14264 | | - if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg); |
| 14189 | + rc = expertDbContainsObject(p->dbv, zName, &bExists); |
| 14190 | + if( rc || bExists ) continue; |
| 14191 | + |
| 14192 | + if( zType[0]=='v' || zType[1]=='r' || bVirtual ){ |
| 14193 | + /* A view. Or a trigger on a view. */ |
| 14194 | + if( zSql ) rc = expertSchemaSql(p->dbv, zSql, pzErrmsg); |
| 14265 | 14195 | }else{ |
| 14266 | 14196 | IdxTable *pTab; |
| 14267 | 14197 | rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg); |
| 14268 | | - if( rc==SQLITE_OK ){ |
| 14198 | + if( rc==SQLITE_OK && ALWAYS(pTab!=0) ){ |
| 14269 | 14199 | int i; |
| 14270 | 14200 | char *zInner = 0; |
| 14271 | 14201 | char *zOuter = 0; |
| 14272 | 14202 | pTab->pNext = p->pTable; |
| 14273 | 14203 | p->pTable = pTab; |
| | @@ -14464,10 +14394,16 @@ |
| 14464 | 14394 | sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC); |
| 14465 | 14395 | while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){ |
| 14466 | 14396 | const char *zComma = zCols==0 ? "" : ", "; |
| 14467 | 14397 | const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0); |
| 14468 | 14398 | const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1); |
| 14399 | + if( zName==0 ){ |
| 14400 | + /* This index contains an expression. Ignore it. */ |
| 14401 | + sqlite3_free(zCols); |
| 14402 | + sqlite3_free(zOrder); |
| 14403 | + return sqlite3_reset(pIndexXInfo); |
| 14404 | + } |
| 14469 | 14405 | zCols = idxAppendText(&rc, zCols, |
| 14470 | 14406 | "%sx.%Q IS sqlite_expert_rem(%d, x.%Q) COLLATE %s", |
| 14471 | 14407 | zComma, zName, nCol, zName, zColl |
| 14472 | 14408 | ); |
| 14473 | 14409 | zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol); |
| | @@ -14792,16 +14728,22 @@ |
| 14792 | 14728 | |
| 14793 | 14729 | /* Copy the entire schema of database [db] into [dbm]. */ |
| 14794 | 14730 | if( rc==SQLITE_OK ){ |
| 14795 | 14731 | sqlite3_stmt *pSql = 0; |
| 14796 | 14732 | rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, |
| 14797 | | - "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'" |
| 14798 | | - " AND sql NOT LIKE 'CREATE VIRTUAL %%' ORDER BY rowid" |
| 14733 | + "SELECT sql, name, substr(sql,1,14)=='create virtual' COLLATE nocase" |
| 14734 | + " FROM sqlite_schema WHERE substr(name,1,7)!='sqlite_' COLLATE nocase" |
| 14735 | + " ORDER BY 3 DESC, rowid" |
| 14799 | 14736 | ); |
| 14800 | 14737 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
| 14801 | 14738 | const char *zSql = (const char*)sqlite3_column_text(pSql, 0); |
| 14802 | | - if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg); |
| 14739 | + const char *zName = (const char*)sqlite3_column_text(pSql, 1); |
| 14740 | + int bExists = 0; |
| 14741 | + rc = expertDbContainsObject(pNew->dbm, zName, &bExists); |
| 14742 | + if( rc==SQLITE_OK && zSql && bExists==0 ){ |
| 14743 | + rc = expertSchemaSql(pNew->dbm, zSql, pzErrmsg); |
| 14744 | + } |
| 14803 | 14745 | } |
| 14804 | 14746 | idxFinalize(&rc, pSql); |
| 14805 | 14747 | } |
| 14806 | 14748 | |
| 14807 | 14749 | /* Create the vtab schema */ |
| | @@ -16211,10 +16153,1197 @@ |
| 16211 | 16153 | } |
| 16212 | 16154 | return rc; |
| 16213 | 16155 | } |
| 16214 | 16156 | |
| 16215 | 16157 | /************************* End ../ext/misc/stmtrand.c ********************/ |
| 16158 | +/************************* Begin ../ext/misc/vfstrace.c ******************/ |
| 16159 | +/* |
| 16160 | +** 2011 March 16 |
| 16161 | +** |
| 16162 | +** The author disclaims copyright to this source code. In place of |
| 16163 | +** a legal notice, here is a blessing: |
| 16164 | +** |
| 16165 | +** May you do good and not evil. |
| 16166 | +** May you find forgiveness for yourself and forgive others. |
| 16167 | +** May you share freely, never taking more than you give. |
| 16168 | +** |
| 16169 | +****************************************************************************** |
| 16170 | +** |
| 16171 | +** This file contains code implements a VFS shim that writes diagnostic |
| 16172 | +** output for each VFS call, similar to "strace". |
| 16173 | +** |
| 16174 | +** USAGE: |
| 16175 | +** |
| 16176 | +** This source file exports a single symbol which is the name of a |
| 16177 | +** function: |
| 16178 | +** |
| 16179 | +** int vfstrace_register( |
| 16180 | +** const char *zTraceName, // Name of the newly constructed VFS |
| 16181 | +** const char *zOldVfsName, // Name of the underlying VFS |
| 16182 | +** int (*xOut)(const char*,void*), // Output routine. ex: fputs |
| 16183 | +** void *pOutArg, // 2nd argument to xOut. ex: stderr |
| 16184 | +** int makeDefault // Make the new VFS the default |
| 16185 | +** ); |
| 16186 | +** |
| 16187 | +** Applications that want to trace their VFS usage must provide a callback |
| 16188 | +** function with this prototype: |
| 16189 | +** |
| 16190 | +** int traceOutput(const char *zMessage, void *pAppData); |
| 16191 | +** |
| 16192 | +** This function will "output" the trace messages, where "output" can |
| 16193 | +** mean different things to different applications. The traceOutput function |
| 16194 | +** for the command-line shell (see shell.c) is "fputs" from the standard |
| 16195 | +** library, which means that all trace output is written on the stream |
| 16196 | +** specified by the second argument. In the case of the command-line shell |
| 16197 | +** the second argument is stderr. Other applications might choose to output |
| 16198 | +** trace information to a file, over a socket, or write it into a buffer. |
| 16199 | +** |
| 16200 | +** The vfstrace_register() function creates a new "shim" VFS named by |
| 16201 | +** the zTraceName parameter. A "shim" VFS is an SQLite backend that does |
| 16202 | +** not really perform the duties of a true backend, but simply filters or |
| 16203 | +** interprets VFS calls before passing them off to another VFS which does |
| 16204 | +** the actual work. In this case the other VFS - the one that does the |
| 16205 | +** real work - is identified by the second parameter, zOldVfsName. If |
| 16206 | +** the 2nd parameter is NULL then the default VFS is used. The common |
| 16207 | +** case is for the 2nd parameter to be NULL. |
| 16208 | +** |
| 16209 | +** The third and fourth parameters are the pointer to the output function |
| 16210 | +** and the second argument to the output function. For the SQLite |
| 16211 | +** command-line shell, when the -vfstrace option is used, these parameters |
| 16212 | +** are fputs and stderr, respectively. |
| 16213 | +** |
| 16214 | +** The fifth argument is true (non-zero) to cause the newly created VFS |
| 16215 | +** to become the default VFS. The common case is for the fifth parameter |
| 16216 | +** to be true. |
| 16217 | +** |
| 16218 | +** The call to vfstrace_register() simply creates the shim VFS that does |
| 16219 | +** tracing. The application must also arrange to use the new VFS for |
| 16220 | +** all database connections that are created and for which tracing is |
| 16221 | +** desired. This can be done by specifying the trace VFS using URI filename |
| 16222 | +** notation, or by specifying the trace VFS as the 4th parameter to |
| 16223 | +** sqlite3_open_v2() or by making the trace VFS be the default (by setting |
| 16224 | +** the 5th parameter of vfstrace_register() to 1). |
| 16225 | +** |
| 16226 | +** |
| 16227 | +** ENABLING VFSTRACE IN A COMMAND-LINE SHELL |
| 16228 | +** |
| 16229 | +** The SQLite command line shell implemented by the shell.c source file |
| 16230 | +** can be used with this module. To compile in -vfstrace support, first |
| 16231 | +** gather this file (test_vfstrace.c), the shell source file (shell.c), |
| 16232 | +** and the SQLite amalgamation source files (sqlite3.c, sqlite3.h) into |
| 16233 | +** the working directory. Then compile using a command like the following: |
| 16234 | +** |
| 16235 | +** gcc -o sqlite3 -Os -I. -DSQLITE_ENABLE_VFSTRACE \ |
| 16236 | +** -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE \ |
| 16237 | +** -DHAVE_READLINE -DHAVE_USLEEP=1 \ |
| 16238 | +** shell.c test_vfstrace.c sqlite3.c -ldl -lreadline -lncurses |
| 16239 | +** |
| 16240 | +** The gcc command above works on Linux and provides (in addition to the |
| 16241 | +** -vfstrace option) support for FTS3 and FTS4, RTREE, and command-line |
| 16242 | +** editing using the readline library. The command-line shell does not |
| 16243 | +** use threads so we added -DSQLITE_THREADSAFE=0 just to make the code |
| 16244 | +** run a little faster. For compiling on a Mac, you'll probably need |
| 16245 | +** to omit the -DHAVE_READLINE, the -lreadline, and the -lncurses options. |
| 16246 | +** The compilation could be simplified to just this: |
| 16247 | +** |
| 16248 | +** gcc -DSQLITE_ENABLE_VFSTRACE \ |
| 16249 | +** shell.c test_vfstrace.c sqlite3.c -ldl -lpthread |
| 16250 | +** |
| 16251 | +** In this second example, all unnecessary options have been removed |
| 16252 | +** Note that since the code is now threadsafe, we had to add the -lpthread |
| 16253 | +** option to pull in the pthreads library. |
| 16254 | +** |
| 16255 | +** To cross-compile for windows using MinGW, a command like this might |
| 16256 | +** work: |
| 16257 | +** |
| 16258 | +** /opt/mingw/bin/i386-mingw32msvc-gcc -o sqlite3.exe -Os -I \ |
| 16259 | +** -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_VFSTRACE \ |
| 16260 | +** shell.c test_vfstrace.c sqlite3.c |
| 16261 | +** |
| 16262 | +** Similar compiler commands will work on different systems. The key |
| 16263 | +** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that |
| 16264 | +** the shell.c source file will know to include the -vfstrace command-line |
| 16265 | +** option and (2) you must compile and link the three source files |
| 16266 | +** shell,c, test_vfstrace.c, and sqlite3.c. |
| 16267 | +** |
| 16268 | +** RUNTIME CONTROL OF VFSTRACE OUTPUT |
| 16269 | +** |
| 16270 | +** The application can use the "vfstrace" pragma to control which VFS |
| 16271 | +** APIs are traced. To disable all output: |
| 16272 | +** |
| 16273 | +** PRAGMA vfstrace('-all'); |
| 16274 | +** |
| 16275 | +** To enable all output (which is the default setting): |
| 16276 | +** |
| 16277 | +** PRAGMA vfstrace('+all'); |
| 16278 | +** |
| 16279 | +** Individual APIs can be enabled or disabled by name, with or without |
| 16280 | +** the initial "x" character. For example, to set up for tracing lock |
| 16281 | +** primatives only: |
| 16282 | +** |
| 16283 | +** PRAGMA vfstrace('-all, +Lock,Unlock,ShmLock'); |
| 16284 | +** |
| 16285 | +** The argument to the vfstrace pragma ignores capitalization and any |
| 16286 | +** characters other than alphabetics, '+', and '-'. |
| 16287 | +*/ |
| 16288 | +#include <stdlib.h> |
| 16289 | +#include <string.h> |
| 16290 | +/* #include "sqlite3.h" */ |
| 16291 | + |
| 16292 | +/* |
| 16293 | +** An instance of this structure is attached to the each trace VFS to |
| 16294 | +** provide auxiliary information. |
| 16295 | +*/ |
| 16296 | +typedef struct vfstrace_info vfstrace_info; |
| 16297 | +struct vfstrace_info { |
| 16298 | + sqlite3_vfs *pRootVfs; /* The underlying real VFS */ |
| 16299 | + int (*xOut)(const char*, void*); /* Send output here */ |
| 16300 | + unsigned int mTrace; /* Mask of interfaces to trace */ |
| 16301 | + u8 bOn; /* Tracing on/off */ |
| 16302 | + void *pOutArg; /* First argument to xOut */ |
| 16303 | + const char *zVfsName; /* Name of this trace-VFS */ |
| 16304 | + sqlite3_vfs *pTraceVfs; /* Pointer back to the trace VFS */ |
| 16305 | +}; |
| 16306 | + |
| 16307 | +/* |
| 16308 | +** The sqlite3_file object for the trace VFS |
| 16309 | +*/ |
| 16310 | +typedef struct vfstrace_file vfstrace_file; |
| 16311 | +struct vfstrace_file { |
| 16312 | + sqlite3_file base; /* Base class. Must be first */ |
| 16313 | + vfstrace_info *pInfo; /* The trace-VFS to which this file belongs */ |
| 16314 | + const char *zFName; /* Base name of the file */ |
| 16315 | + sqlite3_file *pReal; /* The real underlying file */ |
| 16316 | +}; |
| 16317 | + |
| 16318 | +/* |
| 16319 | +** Bit values for vfstrace_info.mTrace. |
| 16320 | +*/ |
| 16321 | +#define VTR_CLOSE 0x00000001 |
| 16322 | +#define VTR_READ 0x00000002 |
| 16323 | +#define VTR_WRITE 0x00000004 |
| 16324 | +#define VTR_TRUNC 0x00000008 |
| 16325 | +#define VTR_SYNC 0x00000010 |
| 16326 | +#define VTR_FSIZE 0x00000020 |
| 16327 | +#define VTR_LOCK 0x00000040 |
| 16328 | +#define VTR_UNLOCK 0x00000080 |
| 16329 | +#define VTR_CRL 0x00000100 |
| 16330 | +#define VTR_FCTRL 0x00000200 |
| 16331 | +#define VTR_SECSZ 0x00000400 |
| 16332 | +#define VTR_DEVCHAR 0x00000800 |
| 16333 | +#define VTR_SHMLOCK 0x00001000 |
| 16334 | +#define VTR_SHMMAP 0x00002000 |
| 16335 | +#define VTR_SHMBAR 0x00004000 |
| 16336 | +#define VTR_SHMUNMAP 0x00008000 |
| 16337 | +#define VTR_OPEN 0x00010000 |
| 16338 | +#define VTR_DELETE 0x00020000 |
| 16339 | +#define VTR_ACCESS 0x00040000 |
| 16340 | +#define VTR_FULLPATH 0x00080000 |
| 16341 | +#define VTR_DLOPEN 0x00100000 |
| 16342 | +#define VTR_DLERR 0x00200000 |
| 16343 | +#define VTR_DLSYM 0x00400000 |
| 16344 | +#define VTR_DLCLOSE 0x00800000 |
| 16345 | +#define VTR_RAND 0x01000000 |
| 16346 | +#define VTR_SLEEP 0x02000000 |
| 16347 | +#define VTR_CURTIME 0x04000000 |
| 16348 | +#define VTR_LASTERR 0x08000000 |
| 16349 | + |
| 16350 | +/* |
| 16351 | +** Method declarations for vfstrace_file. |
| 16352 | +*/ |
| 16353 | +static int vfstraceClose(sqlite3_file*); |
| 16354 | +static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
| 16355 | +static int vfstraceWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64); |
| 16356 | +static int vfstraceTruncate(sqlite3_file*, sqlite3_int64 size); |
| 16357 | +static int vfstraceSync(sqlite3_file*, int flags); |
| 16358 | +static int vfstraceFileSize(sqlite3_file*, sqlite3_int64 *pSize); |
| 16359 | +static int vfstraceLock(sqlite3_file*, int); |
| 16360 | +static int vfstraceUnlock(sqlite3_file*, int); |
| 16361 | +static int vfstraceCheckReservedLock(sqlite3_file*, int *); |
| 16362 | +static int vfstraceFileControl(sqlite3_file*, int op, void *pArg); |
| 16363 | +static int vfstraceSectorSize(sqlite3_file*); |
| 16364 | +static int vfstraceDeviceCharacteristics(sqlite3_file*); |
| 16365 | +static int vfstraceShmLock(sqlite3_file*,int,int,int); |
| 16366 | +static int vfstraceShmMap(sqlite3_file*,int,int,int, void volatile **); |
| 16367 | +static void vfstraceShmBarrier(sqlite3_file*); |
| 16368 | +static int vfstraceShmUnmap(sqlite3_file*,int); |
| 16369 | + |
| 16370 | +/* |
| 16371 | +** Method declarations for vfstrace_vfs. |
| 16372 | +*/ |
| 16373 | +static int vfstraceOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
| 16374 | +static int vfstraceDelete(sqlite3_vfs*, const char *zName, int syncDir); |
| 16375 | +static int vfstraceAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
| 16376 | +static int vfstraceFullPathname(sqlite3_vfs*, const char *zName, int, char *); |
| 16377 | +static void *vfstraceDlOpen(sqlite3_vfs*, const char *zFilename); |
| 16378 | +static void vfstraceDlError(sqlite3_vfs*, int nByte, char *zErrMsg); |
| 16379 | +static void (*vfstraceDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void); |
| 16380 | +static void vfstraceDlClose(sqlite3_vfs*, void*); |
| 16381 | +static int vfstraceRandomness(sqlite3_vfs*, int nByte, char *zOut); |
| 16382 | +static int vfstraceSleep(sqlite3_vfs*, int microseconds); |
| 16383 | +static int vfstraceCurrentTime(sqlite3_vfs*, double*); |
| 16384 | +static int vfstraceGetLastError(sqlite3_vfs*, int, char*); |
| 16385 | +static int vfstraceCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); |
| 16386 | +static int vfstraceSetSystemCall(sqlite3_vfs*,const char*, sqlite3_syscall_ptr); |
| 16387 | +static sqlite3_syscall_ptr vfstraceGetSystemCall(sqlite3_vfs*, const char *); |
| 16388 | +static const char *vfstraceNextSystemCall(sqlite3_vfs*, const char *zName); |
| 16389 | + |
| 16390 | +/* |
| 16391 | +** Return a pointer to the tail of the pathname. Examples: |
| 16392 | +** |
| 16393 | +** /home/drh/xyzzy.txt -> xyzzy.txt |
| 16394 | +** xyzzy.txt -> xyzzy.txt |
| 16395 | +*/ |
| 16396 | +static const char *fileTail(const char *z){ |
| 16397 | + size_t i; |
| 16398 | + if( z==0 ) return 0; |
| 16399 | + i = strlen(z)-1; |
| 16400 | + while( i>0 && z[i-1]!='/' ){ i--; } |
| 16401 | + return &z[i]; |
| 16402 | +} |
| 16403 | + |
| 16404 | +/* |
| 16405 | +** Send trace output defined by zFormat and subsequent arguments. |
| 16406 | +*/ |
| 16407 | +static void vfstrace_printf( |
| 16408 | + vfstrace_info *pInfo, |
| 16409 | + const char *zFormat, |
| 16410 | + ... |
| 16411 | +){ |
| 16412 | + va_list ap; |
| 16413 | + char *zMsg; |
| 16414 | + if( pInfo->bOn ){ |
| 16415 | + va_start(ap, zFormat); |
| 16416 | + zMsg = sqlite3_vmprintf(zFormat, ap); |
| 16417 | + va_end(ap); |
| 16418 | + pInfo->xOut(zMsg, pInfo->pOutArg); |
| 16419 | + sqlite3_free(zMsg); |
| 16420 | + } |
| 16421 | +} |
| 16422 | + |
| 16423 | +/* |
| 16424 | +** Try to convert an error code into a symbolic name for that error code. |
| 16425 | +*/ |
| 16426 | +static const char *vfstrace_errcode_name(int rc ){ |
| 16427 | + const char *zVal = 0; |
| 16428 | + switch( rc ){ |
| 16429 | + case SQLITE_OK: zVal = "SQLITE_OK"; break; |
| 16430 | + case SQLITE_INTERNAL: zVal = "SQLITE_INTERNAL"; break; |
| 16431 | + case SQLITE_ERROR: zVal = "SQLITE_ERROR"; break; |
| 16432 | + case SQLITE_PERM: zVal = "SQLITE_PERM"; break; |
| 16433 | + case SQLITE_ABORT: zVal = "SQLITE_ABORT"; break; |
| 16434 | + case SQLITE_BUSY: zVal = "SQLITE_BUSY"; break; |
| 16435 | + case SQLITE_LOCKED: zVal = "SQLITE_LOCKED"; break; |
| 16436 | + case SQLITE_NOMEM: zVal = "SQLITE_NOMEM"; break; |
| 16437 | + case SQLITE_READONLY: zVal = "SQLITE_READONLY"; break; |
| 16438 | + case SQLITE_INTERRUPT: zVal = "SQLITE_INTERRUPT"; break; |
| 16439 | + case SQLITE_IOERR: zVal = "SQLITE_IOERR"; break; |
| 16440 | + case SQLITE_CORRUPT: zVal = "SQLITE_CORRUPT"; break; |
| 16441 | + case SQLITE_NOTFOUND: zVal = "SQLITE_NOTFOUND"; break; |
| 16442 | + case SQLITE_FULL: zVal = "SQLITE_FULL"; break; |
| 16443 | + case SQLITE_CANTOPEN: zVal = "SQLITE_CANTOPEN"; break; |
| 16444 | + case SQLITE_PROTOCOL: zVal = "SQLITE_PROTOCOL"; break; |
| 16445 | + case SQLITE_EMPTY: zVal = "SQLITE_EMPTY"; break; |
| 16446 | + case SQLITE_SCHEMA: zVal = "SQLITE_SCHEMA"; break; |
| 16447 | + case SQLITE_TOOBIG: zVal = "SQLITE_TOOBIG"; break; |
| 16448 | + case SQLITE_CONSTRAINT: zVal = "SQLITE_CONSTRAINT"; break; |
| 16449 | + case SQLITE_MISMATCH: zVal = "SQLITE_MISMATCH"; break; |
| 16450 | + case SQLITE_MISUSE: zVal = "SQLITE_MISUSE"; break; |
| 16451 | + case SQLITE_NOLFS: zVal = "SQLITE_NOLFS"; break; |
| 16452 | + case SQLITE_IOERR_READ: zVal = "SQLITE_IOERR_READ"; break; |
| 16453 | + case SQLITE_IOERR_SHORT_READ: zVal = "SQLITE_IOERR_SHORT_READ"; break; |
| 16454 | + case SQLITE_IOERR_WRITE: zVal = "SQLITE_IOERR_WRITE"; break; |
| 16455 | + case SQLITE_IOERR_FSYNC: zVal = "SQLITE_IOERR_FSYNC"; break; |
| 16456 | + case SQLITE_IOERR_DIR_FSYNC: zVal = "SQLITE_IOERR_DIR_FSYNC"; break; |
| 16457 | + case SQLITE_IOERR_TRUNCATE: zVal = "SQLITE_IOERR_TRUNCATE"; break; |
| 16458 | + case SQLITE_IOERR_FSTAT: zVal = "SQLITE_IOERR_FSTAT"; break; |
| 16459 | + case SQLITE_IOERR_UNLOCK: zVal = "SQLITE_IOERR_UNLOCK"; break; |
| 16460 | + case SQLITE_IOERR_RDLOCK: zVal = "SQLITE_IOERR_RDLOCK"; break; |
| 16461 | + case SQLITE_IOERR_DELETE: zVal = "SQLITE_IOERR_DELETE"; break; |
| 16462 | + case SQLITE_IOERR_BLOCKED: zVal = "SQLITE_IOERR_BLOCKED"; break; |
| 16463 | + case SQLITE_IOERR_NOMEM: zVal = "SQLITE_IOERR_NOMEM"; break; |
| 16464 | + case SQLITE_IOERR_ACCESS: zVal = "SQLITE_IOERR_ACCESS"; break; |
| 16465 | + case SQLITE_IOERR_CHECKRESERVEDLOCK: |
| 16466 | + zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break; |
| 16467 | + case SQLITE_IOERR_LOCK: zVal = "SQLITE_IOERR_LOCK"; break; |
| 16468 | + case SQLITE_IOERR_CLOSE: zVal = "SQLITE_IOERR_CLOSE"; break; |
| 16469 | + case SQLITE_IOERR_DIR_CLOSE: zVal = "SQLITE_IOERR_DIR_CLOSE"; break; |
| 16470 | + case SQLITE_IOERR_SHMOPEN: zVal = "SQLITE_IOERR_SHMOPEN"; break; |
| 16471 | + case SQLITE_IOERR_SHMSIZE: zVal = "SQLITE_IOERR_SHMSIZE"; break; |
| 16472 | + case SQLITE_IOERR_SHMLOCK: zVal = "SQLITE_IOERR_SHMLOCK"; break; |
| 16473 | + case SQLITE_IOERR_SHMMAP: zVal = "SQLITE_IOERR_SHMMAP"; break; |
| 16474 | + case SQLITE_IOERR_SEEK: zVal = "SQLITE_IOERR_SEEK"; break; |
| 16475 | + case SQLITE_IOERR_GETTEMPPATH: zVal = "SQLITE_IOERR_GETTEMPPATH"; break; |
| 16476 | + case SQLITE_IOERR_CONVPATH: zVal = "SQLITE_IOERR_CONVPATH"; break; |
| 16477 | + case SQLITE_READONLY_DBMOVED: zVal = "SQLITE_READONLY_DBMOVED"; break; |
| 16478 | + case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break; |
| 16479 | + case SQLITE_BUSY_RECOVERY: zVal = "SQLITE_BUSY_RECOVERY"; break; |
| 16480 | + case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break; |
| 16481 | + } |
| 16482 | + return zVal; |
| 16483 | +} |
| 16484 | + |
| 16485 | +/* |
| 16486 | +** Convert value rc into a string and print it using zFormat. zFormat |
| 16487 | +** should have exactly one %s |
| 16488 | +*/ |
| 16489 | +static void vfstrace_print_errcode( |
| 16490 | + vfstrace_info *pInfo, |
| 16491 | + const char *zFormat, |
| 16492 | + int rc |
| 16493 | +){ |
| 16494 | + const char *zVal; |
| 16495 | + char zBuf[50]; |
| 16496 | + zVal = vfstrace_errcode_name(rc); |
| 16497 | + if( zVal==0 ){ |
| 16498 | + zVal = vfstrace_errcode_name(rc&0xff); |
| 16499 | + if( zVal ){ |
| 16500 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "%s | 0x%x", zVal, rc&0xffff00); |
| 16501 | + }else{ |
| 16502 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "%d (0x%x)", rc, rc); |
| 16503 | + } |
| 16504 | + zVal = zBuf; |
| 16505 | + } |
| 16506 | + vfstrace_printf(pInfo, zFormat, zVal); |
| 16507 | +} |
| 16508 | + |
| 16509 | +/* |
| 16510 | +** Append to a buffer. |
| 16511 | +*/ |
| 16512 | +static void strappend(char *z, int *pI, const char *zAppend){ |
| 16513 | + int i = *pI; |
| 16514 | + while( zAppend[0] ){ z[i++] = *(zAppend++); } |
| 16515 | + z[i] = 0; |
| 16516 | + *pI = i; |
| 16517 | +} |
| 16518 | + |
| 16519 | +/* |
| 16520 | +** Turn tracing output on or off according to mMask. |
| 16521 | +*/ |
| 16522 | +static void vfstraceOnOff(vfstrace_info *pInfo, unsigned int mMask){ |
| 16523 | + pInfo->bOn = (pInfo->mTrace & mMask)!=0; |
| 16524 | +} |
| 16525 | + |
| 16526 | +/* |
| 16527 | +** Close an vfstrace-file. |
| 16528 | +*/ |
| 16529 | +static int vfstraceClose(sqlite3_file *pFile){ |
| 16530 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16531 | + vfstrace_info *pInfo = p->pInfo; |
| 16532 | + int rc; |
| 16533 | + vfstraceOnOff(pInfo, VTR_CLOSE); |
| 16534 | + vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName); |
| 16535 | + rc = p->pReal->pMethods->xClose(p->pReal); |
| 16536 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16537 | + if( rc==SQLITE_OK ){ |
| 16538 | + sqlite3_free((void*)p->base.pMethods); |
| 16539 | + p->base.pMethods = 0; |
| 16540 | + } |
| 16541 | + return rc; |
| 16542 | +} |
| 16543 | + |
| 16544 | +/* |
| 16545 | +** Read data from an vfstrace-file. |
| 16546 | +*/ |
| 16547 | +static int vfstraceRead( |
| 16548 | + sqlite3_file *pFile, |
| 16549 | + void *zBuf, |
| 16550 | + int iAmt, |
| 16551 | + sqlite_int64 iOfst |
| 16552 | +){ |
| 16553 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16554 | + vfstrace_info *pInfo = p->pInfo; |
| 16555 | + int rc; |
| 16556 | + vfstraceOnOff(pInfo, VTR_READ); |
| 16557 | + vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)", |
| 16558 | + pInfo->zVfsName, p->zFName, iAmt, iOfst); |
| 16559 | + rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst); |
| 16560 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16561 | + return rc; |
| 16562 | +} |
| 16563 | + |
| 16564 | +/* |
| 16565 | +** Write data to an vfstrace-file. |
| 16566 | +*/ |
| 16567 | +static int vfstraceWrite( |
| 16568 | + sqlite3_file *pFile, |
| 16569 | + const void *zBuf, |
| 16570 | + int iAmt, |
| 16571 | + sqlite_int64 iOfst |
| 16572 | +){ |
| 16573 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16574 | + vfstrace_info *pInfo = p->pInfo; |
| 16575 | + int rc; |
| 16576 | + vfstraceOnOff(pInfo, VTR_WRITE); |
| 16577 | + vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)", |
| 16578 | + pInfo->zVfsName, p->zFName, iAmt, iOfst); |
| 16579 | + rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst); |
| 16580 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16581 | + return rc; |
| 16582 | +} |
| 16583 | + |
| 16584 | +/* |
| 16585 | +** Truncate an vfstrace-file. |
| 16586 | +*/ |
| 16587 | +static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
| 16588 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16589 | + vfstrace_info *pInfo = p->pInfo; |
| 16590 | + int rc; |
| 16591 | + vfstraceOnOff(pInfo, VTR_TRUNC); |
| 16592 | + vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName, |
| 16593 | + size); |
| 16594 | + rc = p->pReal->pMethods->xTruncate(p->pReal, size); |
| 16595 | + vfstrace_printf(pInfo, " -> %d\n", rc); |
| 16596 | + return rc; |
| 16597 | +} |
| 16598 | + |
| 16599 | +/* |
| 16600 | +** Sync an vfstrace-file. |
| 16601 | +*/ |
| 16602 | +static int vfstraceSync(sqlite3_file *pFile, int flags){ |
| 16603 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16604 | + vfstrace_info *pInfo = p->pInfo; |
| 16605 | + int rc; |
| 16606 | + int i; |
| 16607 | + char zBuf[100]; |
| 16608 | + memcpy(zBuf, "|0", 3); |
| 16609 | + i = 0; |
| 16610 | + if( flags & SQLITE_SYNC_FULL ) strappend(zBuf, &i, "|FULL"); |
| 16611 | + else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL"); |
| 16612 | + if( flags & SQLITE_SYNC_DATAONLY ) strappend(zBuf, &i, "|DATAONLY"); |
| 16613 | + if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){ |
| 16614 | + sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags); |
| 16615 | + } |
| 16616 | + vfstraceOnOff(pInfo, VTR_SYNC); |
| 16617 | + vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName, |
| 16618 | + &zBuf[1]); |
| 16619 | + rc = p->pReal->pMethods->xSync(p->pReal, flags); |
| 16620 | + vfstrace_printf(pInfo, " -> %d\n", rc); |
| 16621 | + return rc; |
| 16622 | +} |
| 16623 | + |
| 16624 | +/* |
| 16625 | +** Return the current file-size of an vfstrace-file. |
| 16626 | +*/ |
| 16627 | +static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
| 16628 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16629 | + vfstrace_info *pInfo = p->pInfo; |
| 16630 | + int rc; |
| 16631 | + vfstraceOnOff(pInfo, VTR_FSIZE); |
| 16632 | + vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName); |
| 16633 | + rc = p->pReal->pMethods->xFileSize(p->pReal, pSize); |
| 16634 | + vfstrace_print_errcode(pInfo, " -> %s,", rc); |
| 16635 | + vfstrace_printf(pInfo, " size=%lld\n", *pSize); |
| 16636 | + return rc; |
| 16637 | +} |
| 16638 | + |
| 16639 | +/* |
| 16640 | +** Return the name of a lock. |
| 16641 | +*/ |
| 16642 | +static const char *lockName(int eLock){ |
| 16643 | + const char *azLockNames[] = { |
| 16644 | + "NONE", "SHARED", "RESERVED", "PENDING", "EXCLUSIVE" |
| 16645 | + }; |
| 16646 | + if( eLock<0 || eLock>=(int)(sizeof(azLockNames)/sizeof(azLockNames[0])) ){ |
| 16647 | + return "???"; |
| 16648 | + }else{ |
| 16649 | + return azLockNames[eLock]; |
| 16650 | + } |
| 16651 | +} |
| 16652 | + |
| 16653 | +/* |
| 16654 | +** Lock an vfstrace-file. |
| 16655 | +*/ |
| 16656 | +static int vfstraceLock(sqlite3_file *pFile, int eLock){ |
| 16657 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16658 | + vfstrace_info *pInfo = p->pInfo; |
| 16659 | + int rc; |
| 16660 | + vfstraceOnOff(pInfo, VTR_LOCK); |
| 16661 | + vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName, |
| 16662 | + lockName(eLock)); |
| 16663 | + rc = p->pReal->pMethods->xLock(p->pReal, eLock); |
| 16664 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16665 | + return rc; |
| 16666 | +} |
| 16667 | + |
| 16668 | +/* |
| 16669 | +** Unlock an vfstrace-file. |
| 16670 | +*/ |
| 16671 | +static int vfstraceUnlock(sqlite3_file *pFile, int eLock){ |
| 16672 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16673 | + vfstrace_info *pInfo = p->pInfo; |
| 16674 | + int rc; |
| 16675 | + vfstraceOnOff(pInfo, VTR_UNLOCK); |
| 16676 | + vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName, |
| 16677 | + lockName(eLock)); |
| 16678 | + rc = p->pReal->pMethods->xUnlock(p->pReal, eLock); |
| 16679 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16680 | + return rc; |
| 16681 | +} |
| 16682 | + |
| 16683 | +/* |
| 16684 | +** Check if another file-handle holds a RESERVED lock on an vfstrace-file. |
| 16685 | +*/ |
| 16686 | +static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 16687 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16688 | + vfstrace_info *pInfo = p->pInfo; |
| 16689 | + int rc; |
| 16690 | + vfstraceOnOff(pInfo, VTR_CRL); |
| 16691 | + vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)", |
| 16692 | + pInfo->zVfsName, p->zFName); |
| 16693 | + rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut); |
| 16694 | + vfstrace_print_errcode(pInfo, " -> %s", rc); |
| 16695 | + vfstrace_printf(pInfo, ", out=%d\n", *pResOut); |
| 16696 | + return rc; |
| 16697 | +} |
| 16698 | + |
| 16699 | +/* |
| 16700 | +** File control method. For custom operations on an vfstrace-file. |
| 16701 | +*/ |
| 16702 | +static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 16703 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16704 | + vfstrace_info *pInfo = p->pInfo; |
| 16705 | + int rc; |
| 16706 | + char zBuf[100]; |
| 16707 | + char zBuf2[100]; |
| 16708 | + char *zOp; |
| 16709 | + char *zRVal = 0; |
| 16710 | + vfstraceOnOff(pInfo, VTR_FCTRL); |
| 16711 | + switch( op ){ |
| 16712 | + case SQLITE_FCNTL_LOCKSTATE: zOp = "LOCKSTATE"; break; |
| 16713 | + case SQLITE_GET_LOCKPROXYFILE: zOp = "GET_LOCKPROXYFILE"; break; |
| 16714 | + case SQLITE_SET_LOCKPROXYFILE: zOp = "SET_LOCKPROXYFILE"; break; |
| 16715 | + case SQLITE_LAST_ERRNO: zOp = "LAST_ERRNO"; break; |
| 16716 | + case SQLITE_FCNTL_SIZE_HINT: { |
| 16717 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "SIZE_HINT,%lld", |
| 16718 | + *(sqlite3_int64*)pArg); |
| 16719 | + zOp = zBuf; |
| 16720 | + break; |
| 16721 | + } |
| 16722 | + case SQLITE_FCNTL_CHUNK_SIZE: { |
| 16723 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg); |
| 16724 | + zOp = zBuf; |
| 16725 | + break; |
| 16726 | + } |
| 16727 | + case SQLITE_FCNTL_FILE_POINTER: zOp = "FILE_POINTER"; break; |
| 16728 | + case SQLITE_FCNTL_WIN32_AV_RETRY: zOp = "WIN32_AV_RETRY"; break; |
| 16729 | + case SQLITE_FCNTL_PERSIST_WAL: { |
| 16730 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "PERSIST_WAL,%d", *(int*)pArg); |
| 16731 | + zOp = zBuf; |
| 16732 | + break; |
| 16733 | + } |
| 16734 | + case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break; |
| 16735 | + case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break; |
| 16736 | + case SQLITE_FCNTL_POWERSAFE_OVERWRITE: zOp = "POWERSAFE_OVERWRITE"; break; |
| 16737 | + case SQLITE_FCNTL_PRAGMA: { |
| 16738 | + const char *const* a = (const char*const*)pArg; |
| 16739 | + if( a[1] && strcmp(a[1],"vfstrace")==0 && a[2] ){ |
| 16740 | + const u8 *zArg = (const u8*)a[2]; |
| 16741 | + if( zArg[0]>='0' && zArg[0]<=9 ){ |
| 16742 | + pInfo->mTrace = (sqlite3_uint64)strtoll(a[2], 0, 0); |
| 16743 | + }else{ |
| 16744 | + static const struct { |
| 16745 | + const char *z; |
| 16746 | + unsigned int m; |
| 16747 | + } aKw[] = { |
| 16748 | + { "all", 0xffffffff }, |
| 16749 | + { "close", VTR_CLOSE }, |
| 16750 | + { "read", VTR_READ }, |
| 16751 | + { "write", VTR_WRITE }, |
| 16752 | + { "truncate", VTR_TRUNC }, |
| 16753 | + { "sync", VTR_SYNC }, |
| 16754 | + { "filesize", VTR_FSIZE }, |
| 16755 | + { "lock", VTR_LOCK }, |
| 16756 | + { "unlock", VTR_UNLOCK }, |
| 16757 | + { "checkreservedlock", VTR_CRL }, |
| 16758 | + { "filecontrol", VTR_FCTRL }, |
| 16759 | + { "sectorsize", VTR_SECSZ }, |
| 16760 | + { "devicecharacteristics", VTR_DEVCHAR }, |
| 16761 | + { "shmlock", VTR_SHMLOCK }, |
| 16762 | + { "shmmap", VTR_SHMMAP }, |
| 16763 | + { "shmummap", VTR_SHMUNMAP }, |
| 16764 | + { "shmbarrier", VTR_SHMBAR }, |
| 16765 | + { "open", VTR_OPEN }, |
| 16766 | + { "delete", VTR_DELETE }, |
| 16767 | + { "access", VTR_ACCESS }, |
| 16768 | + { "fullpathname", VTR_FULLPATH }, |
| 16769 | + { "dlopen", VTR_DLOPEN }, |
| 16770 | + { "dlerror", VTR_DLERR }, |
| 16771 | + { "dlsym", VTR_DLSYM }, |
| 16772 | + { "dlclose", VTR_DLCLOSE }, |
| 16773 | + { "randomness", VTR_RAND }, |
| 16774 | + { "sleep", VTR_SLEEP }, |
| 16775 | + { "currenttime", VTR_CURTIME }, |
| 16776 | + { "currenttimeint64", VTR_CURTIME }, |
| 16777 | + { "getlasterror", VTR_LASTERR }, |
| 16778 | + }; |
| 16779 | + int onOff = 1; |
| 16780 | + while( zArg[0] ){ |
| 16781 | + int jj, n; |
| 16782 | + while( zArg[0]!=0 && zArg[0]!='-' && zArg[0]!='+' |
| 16783 | + && !isalpha(zArg[0]) ) zArg++; |
| 16784 | + if( zArg[0]==0 ) break; |
| 16785 | + if( zArg[0]=='-' ){ |
| 16786 | + onOff = 0; |
| 16787 | + zArg++; |
| 16788 | + }else if( zArg[0]=='+' ){ |
| 16789 | + onOff = 1; |
| 16790 | + zArg++; |
| 16791 | + } |
| 16792 | + while( !isalpha(zArg[0]) ){ |
| 16793 | + if( zArg[0]==0 ) break; |
| 16794 | + zArg++; |
| 16795 | + } |
| 16796 | + if( zArg[0]=='x' && isalpha(zArg[1]) ) zArg++; |
| 16797 | + for(n=0; isalpha(zArg[n]); n++){} |
| 16798 | + for(jj=0; jj<(int)(sizeof(aKw)/sizeof(aKw[0])); jj++){ |
| 16799 | + if( sqlite3_strnicmp(aKw[jj].z,(const char*)zArg,n)==0 ){ |
| 16800 | + if( onOff ){ |
| 16801 | + pInfo->mTrace |= aKw[jj].m; |
| 16802 | + }else{ |
| 16803 | + pInfo->mTrace &= ~aKw[jj].m; |
| 16804 | + } |
| 16805 | + break; |
| 16806 | + } |
| 16807 | + } |
| 16808 | + zArg += n; |
| 16809 | + } |
| 16810 | + } |
| 16811 | + } |
| 16812 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]); |
| 16813 | + zOp = zBuf; |
| 16814 | + break; |
| 16815 | + } |
| 16816 | + case SQLITE_FCNTL_BUSYHANDLER: zOp = "BUSYHANDLER"; break; |
| 16817 | + case SQLITE_FCNTL_TEMPFILENAME: zOp = "TEMPFILENAME"; break; |
| 16818 | + case SQLITE_FCNTL_MMAP_SIZE: { |
| 16819 | + sqlite3_int64 iMMap = *(sqlite3_int64*)pArg; |
| 16820 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "MMAP_SIZE,%lld",iMMap); |
| 16821 | + zOp = zBuf; |
| 16822 | + break; |
| 16823 | + } |
| 16824 | + case SQLITE_FCNTL_TRACE: zOp = "TRACE"; break; |
| 16825 | + case SQLITE_FCNTL_HAS_MOVED: zOp = "HAS_MOVED"; break; |
| 16826 | + case SQLITE_FCNTL_SYNC: zOp = "SYNC"; break; |
| 16827 | + case SQLITE_FCNTL_COMMIT_PHASETWO: zOp = "COMMIT_PHASETWO"; break; |
| 16828 | + case SQLITE_FCNTL_WIN32_SET_HANDLE: zOp = "WIN32_SET_HANDLE"; break; |
| 16829 | + case SQLITE_FCNTL_WAL_BLOCK: zOp = "WAL_BLOCK"; break; |
| 16830 | + case SQLITE_FCNTL_ZIPVFS: zOp = "ZIPVFS"; break; |
| 16831 | + case SQLITE_FCNTL_RBU: zOp = "RBU"; break; |
| 16832 | + case SQLITE_FCNTL_VFS_POINTER: zOp = "VFS_POINTER"; break; |
| 16833 | + case SQLITE_FCNTL_JOURNAL_POINTER: zOp = "JOURNAL_POINTER"; break; |
| 16834 | + case SQLITE_FCNTL_WIN32_GET_HANDLE: zOp = "WIN32_GET_HANDLE"; break; |
| 16835 | + case SQLITE_FCNTL_PDB: zOp = "PDB"; break; |
| 16836 | + case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: zOp = "BEGIN_ATOMIC_WRITE"; break; |
| 16837 | + case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: zOp = "COMMIT_ATOMIC_WRITE"; break; |
| 16838 | + case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { |
| 16839 | + zOp = "ROLLBACK_ATOMIC_WRITE"; |
| 16840 | + break; |
| 16841 | + } |
| 16842 | + case SQLITE_FCNTL_LOCK_TIMEOUT: { |
| 16843 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "LOCK_TIMEOUT,%d", *(int*)pArg); |
| 16844 | + zOp = zBuf; |
| 16845 | + break; |
| 16846 | + } |
| 16847 | + case SQLITE_FCNTL_DATA_VERSION: zOp = "DATA_VERSION"; break; |
| 16848 | + case SQLITE_FCNTL_SIZE_LIMIT: zOp = "SIZE_LIMIT"; break; |
| 16849 | + case SQLITE_FCNTL_CKPT_DONE: zOp = "CKPT_DONE"; break; |
| 16850 | + case SQLITE_FCNTL_RESERVE_BYTES: zOp = "RESERVED_BYTES"; break; |
| 16851 | + case SQLITE_FCNTL_CKPT_START: zOp = "CKPT_START"; break; |
| 16852 | + case SQLITE_FCNTL_EXTERNAL_READER: zOp = "EXTERNAL_READER"; break; |
| 16853 | + case SQLITE_FCNTL_CKSM_FILE: zOp = "CKSM_FILE"; break; |
| 16854 | + case SQLITE_FCNTL_RESET_CACHE: zOp = "RESET_CACHE"; break; |
| 16855 | + case 0xca093fa0: zOp = "DB_UNCHANGED"; break; |
| 16856 | + default: { |
| 16857 | + sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op); |
| 16858 | + zOp = zBuf; |
| 16859 | + break; |
| 16860 | + } |
| 16861 | + } |
| 16862 | + vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)", |
| 16863 | + pInfo->zVfsName, p->zFName, zOp); |
| 16864 | + rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg); |
| 16865 | + if( rc==SQLITE_OK ){ |
| 16866 | + switch( op ){ |
| 16867 | + case SQLITE_FCNTL_VFSNAME: { |
| 16868 | + *(char**)pArg = sqlite3_mprintf("vfstrace.%s/%z", |
| 16869 | + pInfo->zVfsName, *(char**)pArg); |
| 16870 | + zRVal = *(char**)pArg; |
| 16871 | + break; |
| 16872 | + } |
| 16873 | + case SQLITE_FCNTL_MMAP_SIZE: { |
| 16874 | + sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%lld", *(sqlite3_int64*)pArg); |
| 16875 | + zRVal = zBuf2; |
| 16876 | + break; |
| 16877 | + } |
| 16878 | + case SQLITE_FCNTL_HAS_MOVED: |
| 16879 | + case SQLITE_FCNTL_PERSIST_WAL: { |
| 16880 | + sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%d", *(int*)pArg); |
| 16881 | + zRVal = zBuf2; |
| 16882 | + break; |
| 16883 | + } |
| 16884 | + case SQLITE_FCNTL_PRAGMA: |
| 16885 | + case SQLITE_FCNTL_TEMPFILENAME: { |
| 16886 | + zRVal = *(char**)pArg; |
| 16887 | + break; |
| 16888 | + } |
| 16889 | + } |
| 16890 | + } |
| 16891 | + if( zRVal ){ |
| 16892 | + vfstrace_print_errcode(pInfo, " -> %s", rc); |
| 16893 | + vfstrace_printf(pInfo, ", %s\n", zRVal); |
| 16894 | + }else{ |
| 16895 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16896 | + } |
| 16897 | + return rc; |
| 16898 | +} |
| 16899 | + |
| 16900 | +/* |
| 16901 | +** Return the sector-size in bytes for an vfstrace-file. |
| 16902 | +*/ |
| 16903 | +static int vfstraceSectorSize(sqlite3_file *pFile){ |
| 16904 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16905 | + vfstrace_info *pInfo = p->pInfo; |
| 16906 | + int rc; |
| 16907 | + vfstraceOnOff(pInfo, VTR_SECSZ); |
| 16908 | + vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName); |
| 16909 | + rc = p->pReal->pMethods->xSectorSize(p->pReal); |
| 16910 | + vfstrace_printf(pInfo, " -> %d\n", rc); |
| 16911 | + return rc; |
| 16912 | +} |
| 16913 | + |
| 16914 | +/* |
| 16915 | +** Return the device characteristic flags supported by an vfstrace-file. |
| 16916 | +*/ |
| 16917 | +static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){ |
| 16918 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16919 | + vfstrace_info *pInfo = p->pInfo; |
| 16920 | + int rc; |
| 16921 | + vfstraceOnOff(pInfo, VTR_DEVCHAR); |
| 16922 | + vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)", |
| 16923 | + pInfo->zVfsName, p->zFName); |
| 16924 | + rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal); |
| 16925 | + vfstrace_printf(pInfo, " -> 0x%08x\n", rc); |
| 16926 | + return rc; |
| 16927 | +} |
| 16928 | + |
| 16929 | +/* |
| 16930 | +** Shared-memory operations. |
| 16931 | +*/ |
| 16932 | +static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){ |
| 16933 | + static const char *azLockName[] = { |
| 16934 | + "WRITE", |
| 16935 | + "CKPT", |
| 16936 | + "RECOVER", |
| 16937 | + "READ0", |
| 16938 | + "READ1", |
| 16939 | + "READ2", |
| 16940 | + "READ3", |
| 16941 | + "READ4", |
| 16942 | + }; |
| 16943 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16944 | + vfstrace_info *pInfo = p->pInfo; |
| 16945 | + int rc; |
| 16946 | + char zLck[100]; |
| 16947 | + int i = 0; |
| 16948 | + vfstraceOnOff(pInfo, VTR_SHMLOCK); |
| 16949 | + memcpy(zLck, "|0", 3); |
| 16950 | + if( flags & SQLITE_SHM_UNLOCK ) strappend(zLck, &i, "|UNLOCK"); |
| 16951 | + if( flags & SQLITE_SHM_LOCK ) strappend(zLck, &i, "|LOCK"); |
| 16952 | + if( flags & SQLITE_SHM_SHARED ) strappend(zLck, &i, "|SHARED"); |
| 16953 | + if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE"); |
| 16954 | + if( flags & ~(0xf) ){ |
| 16955 | + sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags); |
| 16956 | + } |
| 16957 | + if( ofst>=0 && ofst<(int)(sizeof(azLockName)/sizeof(azLockName[0])) ){ |
| 16958 | + vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d(%s),n=%d,%s)", |
| 16959 | + pInfo->zVfsName, p->zFName, ofst, azLockName[ofst], |
| 16960 | + n, &zLck[1]); |
| 16961 | + }else{ |
| 16962 | + vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=5d,n=%d,%s)", |
| 16963 | + pInfo->zVfsName, p->zFName, ofst, |
| 16964 | + n, &zLck[1]); |
| 16965 | + } |
| 16966 | + rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags); |
| 16967 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16968 | + return rc; |
| 16969 | +} |
| 16970 | +static int vfstraceShmMap( |
| 16971 | + sqlite3_file *pFile, |
| 16972 | + int iRegion, |
| 16973 | + int szRegion, |
| 16974 | + int isWrite, |
| 16975 | + void volatile **pp |
| 16976 | +){ |
| 16977 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16978 | + vfstrace_info *pInfo = p->pInfo; |
| 16979 | + int rc; |
| 16980 | + vfstraceOnOff(pInfo, VTR_SHMMAP); |
| 16981 | + vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)", |
| 16982 | + pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite); |
| 16983 | + rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp); |
| 16984 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 16985 | + return rc; |
| 16986 | +} |
| 16987 | +static void vfstraceShmBarrier(sqlite3_file *pFile){ |
| 16988 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16989 | + vfstrace_info *pInfo = p->pInfo; |
| 16990 | + vfstraceOnOff(pInfo, VTR_SHMBAR); |
| 16991 | + vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName); |
| 16992 | + p->pReal->pMethods->xShmBarrier(p->pReal); |
| 16993 | +} |
| 16994 | +static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){ |
| 16995 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 16996 | + vfstrace_info *pInfo = p->pInfo; |
| 16997 | + int rc; |
| 16998 | + vfstraceOnOff(pInfo, VTR_SHMUNMAP); |
| 16999 | + vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)", |
| 17000 | + pInfo->zVfsName, p->zFName, delFlag); |
| 17001 | + rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag); |
| 17002 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 17003 | + return rc; |
| 17004 | +} |
| 17005 | + |
| 17006 | + |
| 17007 | + |
| 17008 | +/* |
| 17009 | +** Open an vfstrace file handle. |
| 17010 | +*/ |
| 17011 | +static int vfstraceOpen( |
| 17012 | + sqlite3_vfs *pVfs, |
| 17013 | + const char *zName, |
| 17014 | + sqlite3_file *pFile, |
| 17015 | + int flags, |
| 17016 | + int *pOutFlags |
| 17017 | +){ |
| 17018 | + int rc; |
| 17019 | + vfstrace_file *p = (vfstrace_file *)pFile; |
| 17020 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17021 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17022 | + p->pInfo = pInfo; |
| 17023 | + p->zFName = zName ? fileTail(zName) : "<temp>"; |
| 17024 | + p->pReal = (sqlite3_file *)&p[1]; |
| 17025 | + rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags); |
| 17026 | + vfstraceOnOff(pInfo, VTR_OPEN); |
| 17027 | + vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)", |
| 17028 | + pInfo->zVfsName, p->zFName, flags); |
| 17029 | + if( p->pReal->pMethods ){ |
| 17030 | + sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) ); |
| 17031 | + const sqlite3_io_methods *pSub = p->pReal->pMethods; |
| 17032 | + memset(pNew, 0, sizeof(*pNew)); |
| 17033 | + pNew->iVersion = pSub->iVersion; |
| 17034 | + pNew->xClose = vfstraceClose; |
| 17035 | + pNew->xRead = vfstraceRead; |
| 17036 | + pNew->xWrite = vfstraceWrite; |
| 17037 | + pNew->xTruncate = vfstraceTruncate; |
| 17038 | + pNew->xSync = vfstraceSync; |
| 17039 | + pNew->xFileSize = vfstraceFileSize; |
| 17040 | + pNew->xLock = vfstraceLock; |
| 17041 | + pNew->xUnlock = vfstraceUnlock; |
| 17042 | + pNew->xCheckReservedLock = vfstraceCheckReservedLock; |
| 17043 | + pNew->xFileControl = vfstraceFileControl; |
| 17044 | + pNew->xSectorSize = vfstraceSectorSize; |
| 17045 | + pNew->xDeviceCharacteristics = vfstraceDeviceCharacteristics; |
| 17046 | + if( pNew->iVersion>=2 ){ |
| 17047 | + pNew->xShmMap = pSub->xShmMap ? vfstraceShmMap : 0; |
| 17048 | + pNew->xShmLock = pSub->xShmLock ? vfstraceShmLock : 0; |
| 17049 | + pNew->xShmBarrier = pSub->xShmBarrier ? vfstraceShmBarrier : 0; |
| 17050 | + pNew->xShmUnmap = pSub->xShmUnmap ? vfstraceShmUnmap : 0; |
| 17051 | + } |
| 17052 | + pFile->pMethods = pNew; |
| 17053 | + } |
| 17054 | + vfstrace_print_errcode(pInfo, " -> %s", rc); |
| 17055 | + if( pOutFlags ){ |
| 17056 | + vfstrace_printf(pInfo, ", outFlags=0x%x\n", *pOutFlags); |
| 17057 | + }else{ |
| 17058 | + vfstrace_printf(pInfo, "\n"); |
| 17059 | + } |
| 17060 | + return rc; |
| 17061 | +} |
| 17062 | + |
| 17063 | +/* |
| 17064 | +** Delete the file located at zPath. If the dirSync argument is true, |
| 17065 | +** ensure the file-system modifications are synced to disk before |
| 17066 | +** returning. |
| 17067 | +*/ |
| 17068 | +static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 17069 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17070 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17071 | + int rc; |
| 17072 | + vfstraceOnOff(pInfo, VTR_DELETE); |
| 17073 | + vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)", |
| 17074 | + pInfo->zVfsName, zPath, dirSync); |
| 17075 | + rc = pRoot->xDelete(pRoot, zPath, dirSync); |
| 17076 | + vfstrace_print_errcode(pInfo, " -> %s\n", rc); |
| 17077 | + return rc; |
| 17078 | +} |
| 17079 | + |
| 17080 | +/* |
| 17081 | +** Test for access permissions. Return true if the requested permission |
| 17082 | +** is available, or false otherwise. |
| 17083 | +*/ |
| 17084 | +static int vfstraceAccess( |
| 17085 | + sqlite3_vfs *pVfs, |
| 17086 | + const char *zPath, |
| 17087 | + int flags, |
| 17088 | + int *pResOut |
| 17089 | +){ |
| 17090 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17091 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17092 | + int rc; |
| 17093 | + vfstraceOnOff(pInfo, VTR_ACCESS); |
| 17094 | + vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)", |
| 17095 | + pInfo->zVfsName, zPath, flags); |
| 17096 | + rc = pRoot->xAccess(pRoot, zPath, flags, pResOut); |
| 17097 | + vfstrace_print_errcode(pInfo, " -> %s", rc); |
| 17098 | + vfstrace_printf(pInfo, ", out=%d\n", *pResOut); |
| 17099 | + return rc; |
| 17100 | +} |
| 17101 | + |
| 17102 | +/* |
| 17103 | +** Populate buffer zOut with the full canonical pathname corresponding |
| 17104 | +** to the pathname in zPath. zOut is guaranteed to point to a buffer |
| 17105 | +** of at least (DEVSYM_MAX_PATHNAME+1) bytes. |
| 17106 | +*/ |
| 17107 | +static int vfstraceFullPathname( |
| 17108 | + sqlite3_vfs *pVfs, |
| 17109 | + const char *zPath, |
| 17110 | + int nOut, |
| 17111 | + char *zOut |
| 17112 | +){ |
| 17113 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17114 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17115 | + int rc; |
| 17116 | + vfstraceOnOff(pInfo, VTR_FULLPATH); |
| 17117 | + vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")", |
| 17118 | + pInfo->zVfsName, zPath); |
| 17119 | + rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut); |
| 17120 | + vfstrace_print_errcode(pInfo, " -> %s", rc); |
| 17121 | + vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut); |
| 17122 | + return rc; |
| 17123 | +} |
| 17124 | + |
| 17125 | +/* |
| 17126 | +** Open the dynamic library located at zPath and return a handle. |
| 17127 | +*/ |
| 17128 | +static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 17129 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17130 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17131 | + vfstraceOnOff(pInfo, VTR_DLOPEN); |
| 17132 | + vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath); |
| 17133 | + return pRoot->xDlOpen(pRoot, zPath); |
| 17134 | +} |
| 17135 | + |
| 17136 | +/* |
| 17137 | +** Populate the buffer zErrMsg (size nByte bytes) with a human readable |
| 17138 | +** utf-8 string describing the most recent error encountered associated |
| 17139 | +** with dynamic libraries. |
| 17140 | +*/ |
| 17141 | +static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ |
| 17142 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17143 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17144 | + vfstraceOnOff(pInfo, VTR_DLERR); |
| 17145 | + vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte); |
| 17146 | + pRoot->xDlError(pRoot, nByte, zErrMsg); |
| 17147 | + vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg); |
| 17148 | +} |
| 17149 | + |
| 17150 | +/* |
| 17151 | +** Return a pointer to the symbol zSymbol in the dynamic library pHandle. |
| 17152 | +*/ |
| 17153 | +static void (*vfstraceDlSym(sqlite3_vfs *pVfs,void *p,const char *zSym))(void){ |
| 17154 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17155 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17156 | + vfstrace_printf(pInfo, "%s.xDlSym(\"%s\")\n", pInfo->zVfsName, zSym); |
| 17157 | + return pRoot->xDlSym(pRoot, p, zSym); |
| 17158 | +} |
| 17159 | + |
| 17160 | +/* |
| 17161 | +** Close the dynamic library handle pHandle. |
| 17162 | +*/ |
| 17163 | +static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 17164 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17165 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17166 | + vfstraceOnOff(pInfo, VTR_DLCLOSE); |
| 17167 | + vfstrace_printf(pInfo, "%s.xDlOpen()\n", pInfo->zVfsName); |
| 17168 | + pRoot->xDlClose(pRoot, pHandle); |
| 17169 | +} |
| 17170 | + |
| 17171 | +/* |
| 17172 | +** Populate the buffer pointed to by zBufOut with nByte bytes of |
| 17173 | +** random data. |
| 17174 | +*/ |
| 17175 | +static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 17176 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17177 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17178 | + vfstraceOnOff(pInfo, VTR_RAND); |
| 17179 | + vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte); |
| 17180 | + return pRoot->xRandomness(pRoot, nByte, zBufOut); |
| 17181 | +} |
| 17182 | + |
| 17183 | +/* |
| 17184 | +** Sleep for nMicro microseconds. Return the number of microseconds |
| 17185 | +** actually slept. |
| 17186 | +*/ |
| 17187 | +static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){ |
| 17188 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17189 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17190 | + vfstraceOnOff(pInfo, VTR_SLEEP); |
| 17191 | + vfstrace_printf(pInfo, "%s.xSleep(%d)\n", pInfo->zVfsName, nMicro); |
| 17192 | + return pRoot->xSleep(pRoot, nMicro); |
| 17193 | +} |
| 17194 | + |
| 17195 | +/* |
| 17196 | +** Return the current time as a Julian Day number in *pTimeOut. |
| 17197 | +*/ |
| 17198 | +static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
| 17199 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17200 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17201 | + int rc; |
| 17202 | + vfstraceOnOff(pInfo, VTR_CURTIME); |
| 17203 | + vfstrace_printf(pInfo, "%s.xCurrentTime()", pInfo->zVfsName); |
| 17204 | + rc = pRoot->xCurrentTime(pRoot, pTimeOut); |
| 17205 | + vfstrace_printf(pInfo, " -> %.17g\n", *pTimeOut); |
| 17206 | + return rc; |
| 17207 | +} |
| 17208 | +static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ |
| 17209 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17210 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17211 | + int rc; |
| 17212 | + vfstraceOnOff(pInfo, VTR_CURTIME); |
| 17213 | + vfstrace_printf(pInfo, "%s.xCurrentTimeInt64()", pInfo->zVfsName); |
| 17214 | + rc = pRoot->xCurrentTimeInt64(pRoot, pTimeOut); |
| 17215 | + vfstrace_printf(pInfo, " -> %lld\n", *pTimeOut); |
| 17216 | + return rc; |
| 17217 | +} |
| 17218 | + |
| 17219 | +/* |
| 17220 | +** Return the most recent error code and message |
| 17221 | +*/ |
| 17222 | +static int vfstraceGetLastError(sqlite3_vfs *pVfs, int nErr, char *zErr){ |
| 17223 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17224 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17225 | + int rc; |
| 17226 | + vfstraceOnOff(pInfo, VTR_LASTERR); |
| 17227 | + vfstrace_printf(pInfo, "%s.xGetLastError(%d,zBuf)", pInfo->zVfsName, nErr); |
| 17228 | + if( nErr ) zErr[0] = 0; |
| 17229 | + rc = pRoot->xGetLastError(pRoot, nErr, zErr); |
| 17230 | + vfstrace_printf(pInfo, " -> zBuf[] = \"%s\", rc = %d\n", nErr?zErr:"", rc); |
| 17231 | + return rc; |
| 17232 | +} |
| 17233 | + |
| 17234 | +/* |
| 17235 | +** Override system calls. |
| 17236 | +*/ |
| 17237 | +static int vfstraceSetSystemCall( |
| 17238 | + sqlite3_vfs *pVfs, |
| 17239 | + const char *zName, |
| 17240 | + sqlite3_syscall_ptr pFunc |
| 17241 | +){ |
| 17242 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17243 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17244 | + return pRoot->xSetSystemCall(pRoot, zName, pFunc); |
| 17245 | +} |
| 17246 | +static sqlite3_syscall_ptr vfstraceGetSystemCall( |
| 17247 | + sqlite3_vfs *pVfs, |
| 17248 | + const char *zName |
| 17249 | +){ |
| 17250 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17251 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17252 | + return pRoot->xGetSystemCall(pRoot, zName); |
| 17253 | +} |
| 17254 | +static const char *vfstraceNextSystemCall(sqlite3_vfs *pVfs, const char *zName){ |
| 17255 | + vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; |
| 17256 | + sqlite3_vfs *pRoot = pInfo->pRootVfs; |
| 17257 | + return pRoot->xNextSystemCall(pRoot, zName); |
| 17258 | +} |
| 17259 | + |
| 17260 | + |
| 17261 | +/* |
| 17262 | +** Clients invoke this routine to construct a new trace-vfs shim. |
| 17263 | +** |
| 17264 | +** Return SQLITE_OK on success. |
| 17265 | +** |
| 17266 | +** SQLITE_NOMEM is returned in the case of a memory allocation error. |
| 17267 | +** SQLITE_NOTFOUND is returned if zOldVfsName does not exist. |
| 17268 | +*/ |
| 17269 | +int vfstrace_register( |
| 17270 | + const char *zTraceName, /* Name of the newly constructed VFS */ |
| 17271 | + const char *zOldVfsName, /* Name of the underlying VFS */ |
| 17272 | + int (*xOut)(const char*,void*), /* Output routine. ex: fputs */ |
| 17273 | + void *pOutArg, /* 2nd argument to xOut. ex: stderr */ |
| 17274 | + int makeDefault /* True to make the new VFS the default */ |
| 17275 | +){ |
| 17276 | + sqlite3_vfs *pNew; |
| 17277 | + sqlite3_vfs *pRoot; |
| 17278 | + vfstrace_info *pInfo; |
| 17279 | + size_t nName; |
| 17280 | + size_t nByte; |
| 17281 | + |
| 17282 | + pRoot = sqlite3_vfs_find(zOldVfsName); |
| 17283 | + if( pRoot==0 ) return SQLITE_NOTFOUND; |
| 17284 | + nName = strlen(zTraceName); |
| 17285 | + nByte = sizeof(*pNew) + sizeof(*pInfo) + nName + 1; |
| 17286 | + pNew = sqlite3_malloc64( nByte ); |
| 17287 | + if( pNew==0 ) return SQLITE_NOMEM; |
| 17288 | + memset(pNew, 0, nByte); |
| 17289 | + pInfo = (vfstrace_info*)&pNew[1]; |
| 17290 | + pNew->iVersion = pRoot->iVersion; |
| 17291 | + pNew->szOsFile = pRoot->szOsFile + sizeof(vfstrace_file); |
| 17292 | + pNew->mxPathname = pRoot->mxPathname; |
| 17293 | + pNew->zName = (char*)&pInfo[1]; |
| 17294 | + memcpy((char*)&pInfo[1], zTraceName, nName+1); |
| 17295 | + pNew->pAppData = pInfo; |
| 17296 | + pNew->xOpen = vfstraceOpen; |
| 17297 | + pNew->xDelete = vfstraceDelete; |
| 17298 | + pNew->xAccess = vfstraceAccess; |
| 17299 | + pNew->xFullPathname = vfstraceFullPathname; |
| 17300 | + pNew->xDlOpen = pRoot->xDlOpen==0 ? 0 : vfstraceDlOpen; |
| 17301 | + pNew->xDlError = pRoot->xDlError==0 ? 0 : vfstraceDlError; |
| 17302 | + pNew->xDlSym = pRoot->xDlSym==0 ? 0 : vfstraceDlSym; |
| 17303 | + pNew->xDlClose = pRoot->xDlClose==0 ? 0 : vfstraceDlClose; |
| 17304 | + pNew->xRandomness = vfstraceRandomness; |
| 17305 | + pNew->xSleep = vfstraceSleep; |
| 17306 | + pNew->xCurrentTime = vfstraceCurrentTime; |
| 17307 | + pNew->xGetLastError = pRoot->xGetLastError==0 ? 0 : vfstraceGetLastError; |
| 17308 | + if( pNew->iVersion>=2 ){ |
| 17309 | + pNew->xCurrentTimeInt64 = pRoot->xCurrentTimeInt64==0 ? 0 : |
| 17310 | + vfstraceCurrentTimeInt64; |
| 17311 | + if( pNew->iVersion>=3 ){ |
| 17312 | + pNew->xSetSystemCall = pRoot->xSetSystemCall==0 ? 0 : |
| 17313 | + vfstraceSetSystemCall; |
| 17314 | + pNew->xGetSystemCall = pRoot->xGetSystemCall==0 ? 0 : |
| 17315 | + vfstraceGetSystemCall; |
| 17316 | + pNew->xNextSystemCall = pRoot->xNextSystemCall==0 ? 0 : |
| 17317 | + vfstraceNextSystemCall; |
| 17318 | + } |
| 17319 | + } |
| 17320 | + pInfo->pRootVfs = pRoot; |
| 17321 | + pInfo->xOut = xOut; |
| 17322 | + pInfo->pOutArg = pOutArg; |
| 17323 | + pInfo->zVfsName = pNew->zName; |
| 17324 | + pInfo->pTraceVfs = pNew; |
| 17325 | + pInfo->mTrace = 0xffffffff; |
| 17326 | + pInfo->bOn = 1; |
| 17327 | + vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n", |
| 17328 | + pInfo->zVfsName, pRoot->zName); |
| 17329 | + return sqlite3_vfs_register(pNew, makeDefault); |
| 17330 | +} |
| 17331 | + |
| 17332 | +/* |
| 17333 | +** Look for the named VFS. If it is a TRACEVFS, then unregister it |
| 17334 | +** and delete it. |
| 17335 | +*/ |
| 17336 | +void vfstrace_unregister(const char *zTraceName){ |
| 17337 | + sqlite3_vfs *pVfs = sqlite3_vfs_find(zTraceName); |
| 17338 | + if( pVfs==0 ) return; |
| 17339 | + if( pVfs->xOpen!=vfstraceOpen ) return; |
| 17340 | + sqlite3_vfs_unregister(pVfs); |
| 17341 | + sqlite3_free(pVfs); |
| 17342 | +} |
| 17343 | + |
| 17344 | +/************************* End ../ext/misc/vfstrace.c ********************/ |
| 16216 | 17345 | |
| 16217 | 17346 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
| 16218 | 17347 | #define SQLITE_SHELL_HAVE_RECOVER 1 |
| 16219 | 17348 | #else |
| 16220 | 17349 | #define SQLITE_SHELL_HAVE_RECOVER 0 |
| | @@ -18241,11 +19370,11 @@ |
| 18241 | 19370 | }while( strstr(z,zBuf)!=0 ); |
| 18242 | 19371 | return zBuf; |
| 18243 | 19372 | } |
| 18244 | 19373 | |
| 18245 | 19374 | /* |
| 18246 | | -** Implementation of scalar SQL function "escape_crnl". The argument passed to |
| 19375 | +** Implementation of scalar SQL function "escape_crlf". The argument passed to |
| 18247 | 19376 | ** this function is the output of built-in function quote(). If the first |
| 18248 | 19377 | ** character of the input is "'", indicating that the value passed to quote() |
| 18249 | 19378 | ** was a text value, then this function searches the input for "\n" and "\r" |
| 18250 | 19379 | ** characters and adds a wrapper similar to the following: |
| 18251 | 19380 | ** |
| | @@ -18252,11 +19381,11 @@ |
| 18252 | 19381 | ** replace(replace(<input>, '\n' |