Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Changes In Branch zonefile Excluding Merge-Ins
This is equivalent to a diff from 15c587cf to 84e9351b
2018-02-27
| ||
20:09 | Update zonefile README.md file to mention the frame cache. (Leaf check-in: 84e9351b user: dan tags: zonefile) | |
19:50 | Rationalize some code in zonefile.c. Fix other minor issues in the same. (check-in: f11beb16 user: dan tags: zonefile) | |
2018-02-26
| ||
15:27 | Always interpret non-zero floating-point values as true even if their integer part is zero. Fix for ticket [36fae083b450e3af857a459e20]. (check-in: a983fa85 user: drh tags: trunk) | |
03:20 | Experimental implementation of IS TRUE and IS FALSE operators. All TRUE and FALSE to act like constants if the names do not resolve to a column name. (check-in: 40314bc9 user: drh tags: is-true-operator) | |
2018-02-23
| ||
13:45 | Merge updates from trunk. (check-in: 53f2100a user: mistachkin tags: zonefile) | |
13:38 | Fix harmless compiler warnings in the zipfile extension seen with MSVC. (check-in: 15c587cf user: mistachkin tags: trunk) | |
13:38 | Enable the JSON1 extension when compiling the test fixture with MSVC. (check-in: e9e9f27b user: mistachkin tags: trunk) | |
Changes to Makefile.msc.
︙ | ︙ | |||
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 | $(TOP)\ext\misc\amatch.c \ $(TOP)\ext\misc\carray.c \ $(TOP)\ext\misc\closure.c \ $(TOP)\ext\misc\csv.c \ $(TOP)\ext\misc\eval.c \ $(TOP)\ext\misc\fileio.c \ $(TOP)\ext\misc\fuzzer.c \ $(TOP)\ext\fts5\fts5_tcl.c \ $(TOP)\ext\fts5\fts5_test_mi.c \ $(TOP)\ext\fts5\fts5_test_tok.c \ $(TOP)\ext\misc\ieee754.c \ $(TOP)\ext\misc\mmapwarm.c \ $(TOP)\ext\misc\nextchar.c \ $(TOP)\ext\misc\normalize.c \ | > | 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 | $(TOP)\ext\misc\amatch.c \ $(TOP)\ext\misc\carray.c \ $(TOP)\ext\misc\closure.c \ $(TOP)\ext\misc\csv.c \ $(TOP)\ext\misc\eval.c \ $(TOP)\ext\misc\fileio.c \ $(TOP)\ext\misc\fuzzer.c \ $(TOP)\ext\zonefile\zonefile.c \ $(TOP)\ext\fts5\fts5_tcl.c \ $(TOP)\ext\fts5\fts5_test_mi.c \ $(TOP)\ext\fts5\fts5_test_tok.c \ $(TOP)\ext\misc\ieee754.c \ $(TOP)\ext\misc\mmapwarm.c \ $(TOP)\ext\misc\nextchar.c \ $(TOP)\ext\misc\normalize.c \ |
︙ | ︙ |
Added ext/zonefile/README.md.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | # The Zonefile Extension ## Functionality ### Creating Zonefile Files To create a new zonefile, first create a database table with the following schema: > CREATE TABLE data( > k INTEGER PRIMARY KEY, > frame INTEGER DEFAULT -1, -- frame number. Automatic if -1 > idx INTEGER DEFAULT -1, -- index of entry within frame. Auto if -1 > v BLOB > ); The table may be created in a persistent or temporary database and may take any name, but must contain the columns above. The table must be populated with a row for each key intended to appear in the new zonefile file. Once the table is populated, a zonefile is created using the following SQL: > SELECT zonefile_write(<file>, <table> [, <parameters>]); where <file> is the name of the file to create on disk, <table> is the name of the database table to read and optional argument <parameters> is a JSON object containing various attributes that influence creation of the zonefile file. Currently, the following <parameters> attributes are supported: <table border=1> <tr align=left><th>Attribute<th>Default<th>Interpretation <tr valign=top><td>maxAutoFrameSize<td>65536 <td>The maximum uncompressed frame size in bytes for automatically generated zonefile frames. <tr valign=top><td>compressionTypeContent<td>"none" <td>The compression type used to compress each frame in the zonefile. Valid values are "none" (no compression), "zstd", "zstd_global_dict", "zlib", "brotli", "lz4" and "lz4hc". Not all compression methods are supported by all builds. The compression method supported by a build depends on the combination of SQLITE_HAVE_ZSTD, SQLITE_HAVE_ZLIB, SQLITE_HAVE_BROTLI and SQLITE_HAVE_LZ4 pre-processor symbols defined at build time. <tr valign=top><td>compressionTypeIndexData<td>"none" <td>The compression type used to compress the zonefile index structure. All values that are valid for the <i>compressionTypeContent</i> parameter, except for "zstd_global_dict", are also valid for this option. <tr valign=top><td>encryptionType<td>"none" <td>The encryption type to use. At present the only valid values are "none" (no encryption) and "xor" (an insecure mock encryption method useful for testing only). Enhanced implementations may support any or all of the following encryption schemes: <ul> <li> "AES_128_CTR" <li> "AES_128_CBC" <li> "AES_256_CTR" <li> "AES_256_CBC" </ul> <tr valign=top><td>encryptionKey<td>"" <td>The encryption key to use. The encryption key must be specified as an even number of hexadecimal that will be converted to a binary key before use. It is the responsibility of the caller to specify a key of the optimal length for each encryption algorithm (e.g. 16 bytes (32 hex digits) for a 128-bit encryption, or 32 bytes (64 digits) for a 256-bit method). This option is ignored if <i>encryptionType</i> is set to "none". </table> For example, to create a zonefile named "test.zonefile" based on the contents of database table "test_input", with a maximum automatic frame size of 4096 bytes and using "xor" encryption with a 128-bit key: > SELECT zonefile_write('test.zonefile', 'test_input', > '{"maxAutoFrameSize":4096, > "encryptionType":"xor", > "encryptionKey":"e6e600bc063aad12f6387beab650c48a" > }' > ); ### Using (Reading) Zonefile Files To create a new zonefile table, one of the following: > CREATE VIRTUAL TABLE z1 USING zonefile; > CREATE VIRTUAL TABLE z1 USING zonefile(cachesize=N); where <i>N</i> is any non-zero positive integer. If the zonefile is used to access any files containing compressed or encrypted data, it maintains an LRU cache of uncompressed frame data <i>N</i> frames in size. The default value of <i>N</i> is 1. Creating a "zonefile" virtual table actually creates two virtual tables in the database schema. One read-only table named "z1", with a schema equivalent to: > CREATE TABLE z1( -- this whole table is read-only > k INTEGER PRIMARY KEY, -- key value > v BLOB, -- associated blob of data > fileid INTEGER, -- file id (rowid value for z1_files) > sz INTEGER -- size of blob of data in bytes > ); And a read-write table named "z1_files" with a schema like: > CREATE TABLE z1_files( > filename TEXT PRIMARY KEY, > ekey BLOB, -- encryption key > header JSON HIDDEN -- read-only > ); Both tables are initially empty. To add a zonefile to the index, insert a row into the "z1_files" table: > INSERT INTO z1_files(filename) VALUES(<filename>); If the file is an encrypted file, then the encryption key (a blob) must be inserted into the "ekey" column. Encryption keys are not stored in the database, they are held in main-memory only. This means that each new connection must configure encryption key using UPDATE statements before accessing any encrypted files. For example: > -- Add new encrypted file to database: > INSERT INTO z1_files(filename, ekey) VALUES(<filename>, <ekey>); > > -- Configure encryption key for existing file after opening database: > UPDATE z1_files SET ekey = <ekey> WHERE filename = <filename>; Currently, values provided for any columns other than "filename" and "ekey" are ignored. Files are removed from the index by deleting rows from the z1_files table: > DELETE FROM z1_files WHERE filename = <filename>; Once zonefile files have been added to the index, their contents are visible in table "z1". To retrieve the value associated with a single key from one of the zonefile files in the index: > SELECT v FROM z1 WHERE k = <key>; ## Notes * Contrary to the spec, the implementation uses 32-bit (not 16-bit) frame numbers. So the KeyOffsetPair structure becomes: KeyOffsetPair { uint64 key; uint32 frameNo; uint32 frameByteOffset; }; Also, the ZonefileHeader.numFrames field is now 32-bit. Which makes the ZonefileHeader structure 26 bytes in size. The implementation pads this out to 32 bytes so that the ZoneFileIndex is 8-byte aligned. * Multi-byte integer values are big-endian. * The offsets in the ZoneFileIndex.byteOffsetZoneFrame[] array are relative to the offset in ZoneFileHeader.byteOffsetFrames. This is necessary as we may not know the offset of the start of the frame data until after the ZoneFileIndex structure is compressed. * The offsets in the ZoneFileIndex.byteOffsetZoneFrame[] array are the offsets for the first byte past the end of the corresponding frame. For example, byteOffsetZoneFrame[] identifies the first byte of the second frame, and byteOffsetZoneFrame[numFrames-1] is one byte past the end of the last frame in the file. This is better as if we store the starting offset of each frame, there is no way to determine the size of the last frame in the file without trusting the filesize itself. * Currently there is no support at all for encryption. * Zonefile currently uses json1 to parse the json argument to zonefile\_write(). And so must be used with an SQLITE\_ENABLE\_JSON1 or otherwise json1-enabled SQLite. |
Added ext/zonefile/zonefile.c.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 | /* ** 2018-02-10 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ****************************************************************************** */ #include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 #ifndef SQLITE_OMIT_VIRTUALTABLE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> /* ** Default values for various zonefile_write() parameters. */ #define ZONEFILE_DEFAULT_MAXAUTOFRAMESIZE (64*1024) #define ZONEFILE_DEFAULT_ENCRYPTION 1 #define ZONEFILE_DEFAULT_COMPRESSION 0 #define ZONEFILE_DEFAULT_DICTSIZE (64*1024) /* ** Value to use for the first 4 bytes of a zonefile file header. */ #define ZONEFILE_MAGIC_NUMBER 0x464B3138 /* ** Size of a zonefile header. And of each entry in the ** ZonefileIndex.keyOffsets array. */ #define ZONEFILE_SZ_HEADER 32 #define ZONEFILE_SZ_KEYOFFSETS_ENTRY 20 /* ** Constants for supported compression types. These are copied from the ** published file format spec. */ #define ZONEFILE_COMPRESSION_NONE 0 #define ZONEFILE_COMPRESSION_ZSTD 1 #define ZONEFILE_COMPRESSION_ZSTD_GLOBAL_DICT 2 #define ZONEFILE_COMPRESSION_ZLIB 3 #define ZONEFILE_COMPRESSION_BROTLI 4 #define ZONEFILE_COMPRESSION_LZ4 5 #define ZONEFILE_COMPRESSION_LZ4HC 6 /* ** Schema for "zonefile" virtual table. */ #define ZONEFILE_SCHEMA \ "CREATE TABLE z1(" \ " k INTEGER PRIMARY KEY," \ " v BLOB," \ " fileid INTEGER," \ " sz INTEGER" \ ")" /* ** Schema for "zonefile_files" virtual table. */ #define ZONEFILE_FILES_SCHEMA \ "CREATE TABLE z2(" \ " filename TEXT," \ " ekey BLOB," \ " header JSON HIDDEN" \ ")" #ifndef SQLITE_AMALGAMATION typedef sqlite3_int64 i64; typedef sqlite3_uint64 u64; typedef unsigned char u8; typedef unsigned short u16; typedef unsigned long u32; #define MIN(a,b) ((a)<(b) ? (a) : (b)) #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) # define ALWAYS(X) (1) # define NEVER(X) (0) #elif !defined(NDEBUG) # define ALWAYS(X) ((X)?1:(assert(0),0)) # define NEVER(X) ((X)?(assert(0),1):0) #else # define ALWAYS(X) (X) # define NEVER(X) (X) #endif #endif /* SQLITE_AMALGAMATION */ /* ** Forward declarations for encryption/decryption functions. ** ** If this module is not compiled with SQLITE_HAVE_ZONEFILE_CODEC, then ** implementations of the following type and functions that support the ** mock encryption method "xor" only are provided. Alternatively, the ** application may append a more functional implementation of the following ** type and functions to this file before compiling it with ** SQLITE_HAVE_ZONEFILE_CODEC defined. */ typedef struct ZonefileCodec ZonefileCodec; static int zonefileCodecCreate( int,int,unsigned char*,int,ZonefileCodec**,char**); static int zonefileCodecNonceSize(ZonefileCodec*); static void zonefileCodecEncode(ZonefileCodec*, unsigned char*, int); static void zonefileCodecDecode(ZonefileCodec*, unsigned char*, int); static void zonefileCodecDestroy(ZonefileCodec*); #ifndef SQLITE_HAVE_ZONEFILE_CODEC typedef struct ZonefileCodec ZonefileCodec; struct ZonefileCodec { u8 aKey[16]; int bEncrypt; /* Second parameter passed to Create() */ }; /* Create a new encryption module instance using algorithm iAlg. ** ** iAlg==1 AES128 CTR ** iAlg==2 AES128 CBC ** iAlg==3 AES256 CTR ** iAlg==4 AES256 CBC ** iAlg==5 XOR Testing use only ** ** If the requested algorithm is not available, the routine returns ** a NULL pointer. NULL is also returned on a OOM error. ** ** Use zonefileCodecDestroy() to reclaim memory. */ static int zonefileCodecCreate( int iAlg, int bEncrypt, /* True for encryption, zero for decryption */ unsigned char *pKey, int nKey, ZonefileCodec **pp, char **pzErr ){ ZonefileCodec *pRet; int rc = SQLITE_OK; if( iAlg!=5 ){ *pzErr = sqlite3_mprintf("unsupported encryption method: %d", iAlg); rc = SQLITE_ERROR; }else{ *pp = pRet = (ZonefileCodec*)sqlite3_malloc(sizeof(ZonefileCodec)); if( pRet==0 ){ rc = SQLITE_NOMEM; }else{ int i; for(i=0; i<sizeof(pRet->aKey); i++){ pRet->aKey[i] = pKey[i % nKey]; } pRet->bEncrypt = bEncrypt; } } return rc; } /* Return the size of the nonce used for the given encryption module */ static int zonefileCodecNonceSize(ZonefileCodec *pCodec){ return 16; } /* Encrypt in-place. ** ** The size of the content will grow by the nonce size. Hence, the ** buffer must have at least nonce bytes of extra space available at ** the end to accommodate that growth. When persisting results, be ** sure to include the extra bytes. */ static void zonefileCodecEncode( ZonefileCodec *pCodec, unsigned char *pIn, int nIn ){ int i; u8 *aNonce = &pIn[nIn]; assert( pCodec->bEncrypt ); sqlite3_randomness(16, aNonce); for(i=0; i<nIn; i++){ pIn[i] = pIn[i] ^ aNonce[i%16] ^ pCodec->aKey[i%16]; } } /* Decrypt in-place. ** ** The size of the decrypted text will be less than the input buffer ** by nonce-size bytes. */ static void zonefileCodecDecode( ZonefileCodec *pCodec, unsigned char *pIn, int nIn ){ int i; u8 *aNonce = &pIn[nIn-16]; assert( pCodec->bEncrypt==0 ); for(i=0; i<nIn-16; i++){ pIn[i] = pIn[i] ^ aNonce[i%16] ^ pCodec->aKey[i%16]; } } /* Destroy an encryption module. ** It is harmless to pass in a NULL pointer. */ static void zonefileCodecDestroy(ZonefileCodec *pCodec){ sqlite3_free(pCodec); } #endif /* SQLITE_HAVE_ZONEFILE_CODEC */ /* ** All zonefile and zonefile_files virtual table instances that belong ** to the same database handle (sqlite3*) share a single instance of the ** ZonefileGlobal object. This global object contains a table of ** configured encryption keys for the various zonefiles in the system. */ typedef struct ZonefileGlobal ZonefileGlobal; typedef struct ZonefileKey ZonefileKey; struct ZonefileGlobal { int nEntry; /* Number of entries in the hash table */ int nHash; /* Size of aHash[] array */ ZonefileKey **aHash; /* Hash buckets */ }; struct ZonefileKey { const char *zName; /* Zonefile table name */ const char *zDb; /* Database name ("main", "temp" etc.) */ i64 iFileid; /* File id */ const u8 *aKey; /* Key buffer */ int nKey; /* Size of zKey in bytes */ u32 iHash; /* zonefileKeyHash() value */ ZonefileKey *pHashNext; /* Next colliding key in hash table */ }; /* ** Return a 32-bit hash value for the three arguments. */ static u32 zonefileKeyHash( const char *zDb, const char *zTab, i64 iFileid ){ u32 iHash = 0; int i; for(i=0; zDb[i]; i++) iHash += (iHash<<3) + (u8)zDb[i]; for(i=0; zTab[i]; i++) iHash += (iHash<<3) + (u8)zTab[i]; return (iHash ^ (iFileid & 0xFFFFFFFF)); } /* ** Store encryption key aKey in the key-store passed as the first argument. ** Return SQLITE_OK if successful, or an SQLite error code (SQLITE_NOMEM) ** otherwise. */ static int zonefileKeyStore( ZonefileGlobal *pGlobal, const char *zDb, /* Database containing zonefile table */ const char *zTab, /* Name of zonefile table */ i64 iFileid, /* File-id to configure key for */ const u8 *aKey, /* Key to store */ int nKey /* Size of aKey[] in bytes */ ){ ZonefileKey **pp; u32 iHash = zonefileKeyHash(zDb, zTab, iFileid); /* Remove any old entry */ if( pGlobal->nHash ){ for(pp=&pGlobal->aHash[iHash%pGlobal->nHash]; *pp; pp=&((*pp)->pHashNext)){ ZonefileKey *pThis = *pp; if( pThis->iFileid==iFileid && 0==sqlite3_stricmp(zTab, pThis->zName) && 0==sqlite3_stricmp(zDb, pThis->zDb) ){ pGlobal->nEntry--; *pp = pThis->pHashNext; sqlite3_free(pThis); break; } } } if( aKey ){ int nDb = strlen(zDb); int nTab = strlen(zTab); ZonefileKey *pNew; /* Resize the hash-table, if necessary */ if( pGlobal->nEntry>=pGlobal->nHash ){ int i; int n = pGlobal->nHash ? pGlobal->nHash*2 : 16; ZonefileKey **a = (ZonefileKey**)sqlite3_malloc(n*sizeof(ZonefileKey*)); if( a==0 ) return SQLITE_NOMEM; memset(a, 0, n*sizeof(ZonefileKey*)); for(i=0; i<pGlobal->nHash; i++){ ZonefileKey *p; ZonefileKey *pNext; for(p=pGlobal->aHash[i]; p; p=pNext){ pNext = p->pHashNext; p->pHashNext = a[p->iHash % n]; a[p->iHash % n] = p; } } sqlite3_free(pGlobal->aHash); pGlobal->aHash = a; pGlobal->nHash = n; } pNew = (ZonefileKey*)sqlite3_malloc( sizeof(ZonefileKey) + nKey+1 + nDb+1 + nTab+1 ); if( pNew==0 ) return SQLITE_NOMEM; memset(pNew, 0, sizeof(ZonefileKey)); pNew->iFileid = iFileid; pNew->iHash = iHash; pNew->aKey = (const u8*)&pNew[1]; pNew->nKey = nKey; pNew->zDb = (const char*)&pNew->aKey[nKey+1]; pNew->zName = &pNew->zDb[nDb+1]; memcpy((u8*)pNew->aKey, aKey, nKey+1); memcpy((char*)pNew->zDb, zDb, nDb+1); memcpy((char*)pNew->zName, zTab, nTab+1); pNew->pHashNext = pGlobal->aHash[iHash % pGlobal->nHash]; pGlobal->aHash[iHash % pGlobal->nHash] = pNew; pGlobal->nEntry++; } return SQLITE_OK; } /* ** Search the key-store passed as the first argument for an encryption ** key to use with the file with file-id iFileid in zonefile table zTab ** in database zDb. If successful, set (*paKey) to point to the key ** buffer and return the size of the key in bytes. ** ** If no key is found, return 0. The final value of (*paKey) is undefined ** in this case. */ static int zonefileKeyFind( ZonefileGlobal *pGlobal, const char *zDb, /* Database containing zonefile table */ const char *zTab, /* Name of zonefile table */ i64 iFileid, /* File-id to configure key for */ const u8 **paKey /* OUT: Pointer to key buffer */ ){ if( pGlobal->nHash ){ ZonefileKey *pKey; u32 iHash = zonefileKeyHash(zDb, zTab, iFileid); for(pKey=pGlobal->aHash[iHash%pGlobal->nHash]; pKey; pKey=pKey->pHashNext){ if( pKey->iFileid==iFileid && 0==sqlite3_stricmp(zTab, pKey->zName) && 0==sqlite3_stricmp(zDb, pKey->zDb) ){ *paKey = pKey->aKey; return pKey->nKey; } } } return 0; } /* ** The pointer passed as the only argument must actually point to a ** ZonefileGlobal structure. This function frees the structure and all ** of its components. */ static void zonefileKeyDestroy(void *p){ ZonefileGlobal *pGlobal = (ZonefileGlobal*)p; int i; for(i=0; i<pGlobal->nHash; i++){ ZonefileKey *pKey; ZonefileKey *pNext; for(pKey=pGlobal->aHash[i]; pKey; pKey=pNext){ pNext = pKey->pHashNext; sqlite3_free(pKey); } } sqlite3_free(pGlobal->aHash); sqlite3_free(pGlobal); } /* ** Write value v to buffer aBuf as an unsigned 32-bit big-endian integer. */ static void zonefilePut32(u8 *aBuf, u32 v){ aBuf[0] = (v >> 24) & 0xFF; aBuf[1] = (v >> 16) & 0xFF; aBuf[2] = (v >> 8) & 0xFF; aBuf[3] = v & 0xFF; } /* ** Read and return an unsigned 32-bit big-endian integer from buffer aBuf. */ static u32 zonefileGet32(const u8 *aBuf){ return (((u32)aBuf[0]) << 24) + (((u32)aBuf[1]) << 16) + (((u32)aBuf[2]) << 8) + (((u32)aBuf[3]) << 0); } /* ** Generic xOpen, xClose and xUncompressSize methods used by a few ** different compression method bindings. */ static int zfGenericOpen(void **pp, u8 *aDict, int nDict){ *pp = 0; return SQLITE_OK; } static void zfGenericClose(void *p){ } static int zfGenericUncompressSize( void *p, const u8 *aSrc, int nSrc ){ return (int)zonefileGet32(aSrc); } #ifdef SQLITE_HAVE_ZLIB #include <zlib.h> static int zfZlibCompressBound(void *p, int nSrc){ return (int)compressBound((uLong)nSrc) + 4; } static int zfZlibCompress( void *p, u8 *aDest, int *pnDest, const u8 *aSrc, int nSrc ){ uLongf destLen = (uLongf)(*pnDest)-4; int rc = compress(&aDest[4], &destLen, aSrc, (uLong)nSrc); *pnDest = (int)(destLen+4); zonefilePut32(aDest, nSrc); return rc==Z_OK ? SQLITE_OK : SQLITE_ERROR; } static int zfZlibUncompress( void *p, u8 *aDest, int nDest, const u8 *aSrc, int nSrc ){ uLongf destLen = (uLongf)nDest; int rc = uncompress(aDest, &destLen, &aSrc[4], (uLong)nSrc-4); return rc==Z_OK ? SQLITE_OK : SQLITE_ERROR; } #endif #ifdef SQLITE_HAVE_ZSTD #include <zstd.h> static int zfZstdCompressBound(void *p, int nSrc){ return (int)ZSTD_compressBound((size_t)nSrc); } static int zfZstdCompress( void *p, u8 *aDest, int *pnDest, const u8 *aSrc, int nSrc ){ size_t szDest = (size_t)(*pnDest); size_t rc = ZSTD_compress(aDest, szDest, aSrc, (size_t)nSrc, 1); if( ZSTD_isError(rc) ) return SQLITE_ERROR; *pnDest = (int)rc; return SQLITE_OK; } static int zfZstdUncompressSize(void *p, const u8 *aSrc, int nSrc){ return (int)ZSTD_getFrameContentSize(aSrc, (size_t)nSrc); } static int zfZstdUncompress( void *p, u8 *aDest, int nDest, const u8 *aSrc, int nSrc ){ size_t rc = ZSTD_decompress(aDest, (size_t)nDest, aSrc, (size_t)nSrc); if( rc!=(size_t)nDest ) return SQLITE_ERROR; return SQLITE_OK; } #include <zdict.h> typedef struct ZfZstddict ZfZstddict; struct ZfZstddict { ZSTD_CDict *pCDict; ZSTD_CCtx *pCCtx; ZSTD_DDict *pDDict; ZSTD_DCtx *pDCtx; }; static void zfZstddictClose(void *p){ if( p ){ ZfZstddict *pCmp = (ZfZstddict*)p; if( pCmp->pCDict ) ZSTD_freeCDict(pCmp->pCDict); if( pCmp->pCCtx ) ZSTD_freeCCtx(pCmp->pCCtx); if( pCmp->pDCtx ) ZSTD_freeDCtx(pCmp->pDCtx); sqlite3_free(pCmp); } } static int zfZstddictOpen(void **pp, u8 *aDict, int nDict){ int rc = SQLITE_OK; ZfZstddict *pDict = (ZfZstddict*)sqlite3_malloc(sizeof(ZfZstddict)); if( pDict==0 ){ rc = SQLITE_NOMEM; }else{ memset(pDict, 0, sizeof(ZfZstddict)); if( aDict ){ pDict->pDDict = ZSTD_createDDict(aDict, nDict); pDict->pDCtx = ZSTD_createDCtx(); if( pDict->pDDict==0 || pDict->pDCtx==0 ){ zfZstddictClose((void*)pDict); pDict = 0; rc = SQLITE_ERROR; } } } *pp = (void*)pDict; return rc; } static int zfZstddictTrain( void *p, /* Compressor handle */ u8 *aDict, int *pnDict, /* OUT: Dictionary buffer */ u8 *aSamp, size_t *aSz, int nSamp /* IN: Training samples */ ){ ZfZstddict *pCmp = (ZfZstddict*)p; size_t sz = ZDICT_trainFromBuffer(aDict, (size_t)*pnDict, aSamp, aSz, nSamp); if( ZDICT_isError(sz) ) return SQLITE_ERROR; pCmp->pCDict = ZSTD_createCDict(aDict, sz, 1); pCmp->pCCtx = ZSTD_createCCtx(); if( pCmp->pCDict==0 || pCmp->pCCtx==0 ) return SQLITE_ERROR; *pnDict = (int)sz; return SQLITE_OK; } static int zfZstddictCompress( void *p, u8 *aDest, int *pnDest, const u8 *aSrc, int nSrc ){ ZfZstddict *pCmp = (ZfZstddict*)p; size_t szDest = (size_t)(*pnDest); size_t rc; assert( pCmp && pCmp->pCDict && pCmp->pCCtx ); rc = ZSTD_compress_usingCDict( pCmp->pCCtx, aDest, szDest, aSrc, (size_t)nSrc, pCmp->pCDict ); if( ZSTD_isError(rc) ) return SQLITE_ERROR; *pnDest = (int)rc; return SQLITE_OK; } static int zfZstddictUncompress( void *p, u8 *aDest, int nDest, const u8 *aSrc, int nSrc ){ ZfZstddict *pCmp = (ZfZstddict*)p; size_t rc = ZSTD_decompress_usingDDict( pCmp->pDCtx, aDest, (size_t)nDest, aSrc, (size_t)nSrc, pCmp->pDDict ); if( rc!=(size_t)nDest ) return SQLITE_ERROR; return SQLITE_OK; } #endif #ifdef SQLITE_HAVE_LZ4 #include <lz4.h> #include <lz4hc.h> static int zfLz4CompressBound(void *p, int nSrc){ return (int)LZ4_compressBound(nSrc) + 4; } static int zfLz4Uncompress( void *p, u8 *aDest, int nDest, const u8 *aSrc, int nSrc ){ int rc = LZ4_decompress_safe( (const char*)&aSrc[4], (char*)aDest, nSrc-4, nDest ); return rc==nDest ? SQLITE_OK : SQLITE_ERROR; } static int zfLz4Compress( void *p, u8 *aDest, int *pnDest, const u8 *aSrc, int nSrc ){ int rc = LZ4_compress_default( (const char*)aSrc, (char*)&aDest[4], nSrc, (*pnDest - 4) ); *pnDest = rc+4; zonefilePut32(aDest, nSrc); return rc==0 ? SQLITE_ERROR : SQLITE_OK; } static int zfLz4hcCompress( void *p, u8 *aDest, int *pnDest, const u8 *aSrc, int nSrc ){ int rc = LZ4_compress_HC( (const char*)aSrc, (char*)&aDest[4], nSrc, *pnDest, 0 ); *pnDest = rc+4; zonefilePut32(aDest, nSrc); return rc==0 ? SQLITE_ERROR : SQLITE_OK; } #endif #ifdef SQLITE_HAVE_BROTLI #include <brotli/encode.h> #include <brotli/decode.h> static int zfBrotliCompressBound(void *p, int nSrc){ return (int)BrotliEncoderMaxCompressedSize((size_t)nSrc) + 4; } static int zfBrotliCompress( void *p, u8 *aDest, int *pnDest, const u8 *aSrc, int nSrc ){ size_t nDest = (size_t)*pnDest - 4; BROTLI_BOOL rc = BrotliEncoderCompress( BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW, BROTLI_DEFAULT_MODE, (size_t)nSrc, aSrc, &nDest, &aDest[4] ); *pnDest = (int)nDest + 4; zonefilePut32(aDest, nSrc); return rc==0 ? SQLITE_ERROR : SQLITE_OK; } static int zfBrotliUncompress( void *p, u8 *aDest, int nDest, const u8 *aSrc, int nSrc ){ size_t n = nDest; BrotliDecoderResult rc = BrotliDecoderDecompress(nSrc-4, &aSrc[4], &n, aDest); return rc==BROTLI_DECODER_RESULT_SUCCESS ? SQLITE_OK : SQLITE_ERROR; } #endif typedef struct ZonefileCompress ZonefileCompress; static struct ZonefileCompress { int eType; const char *zName; int (*xOpen)(void**, u8 *aDict, int nDict); void (*xClose)(void*); int (*xTrain)(void*, u8 *aDict, int *pnDict, u8 *a, size_t *aSz, int n); int (*xCompressBound)(void*, int nSrc); int (*xCompress)(void*, u8 *aDest, int *pnDest, const u8 *aSrc, int nSrc); int (*xUncompressSize)(void*, const u8 *aSrc, int nSrc); int (*xUncompress)(void*, u8 *aDest, int nDest, const u8 *aSrc, int nSrc); } aZonefileCompress[] = { { ZONEFILE_COMPRESSION_NONE, "none", 0, 0, 0, 0, 0, 0, 0 }, #ifdef SQLITE_HAVE_ZSTD { ZONEFILE_COMPRESSION_ZSTD, "zstd", zfGenericOpen, zfGenericClose, 0, zfZstdCompressBound, zfZstdCompress, zfZstdUncompressSize, zfZstdUncompress }, { ZONEFILE_COMPRESSION_ZSTD_GLOBAL_DICT, "zstd_global_dict", zfZstddictOpen, zfZstddictClose, zfZstddictTrain, zfZstdCompressBound, zfZstddictCompress, zfZstdUncompressSize, zfZstddictUncompress }, #endif /* SQLITE_HAVE_ZSTD */ #ifdef SQLITE_HAVE_ZLIB { ZONEFILE_COMPRESSION_ZLIB, "zlib", zfGenericOpen, zfGenericClose, 0, zfZlibCompressBound, zfZlibCompress, zfGenericUncompressSize, zfZlibUncompress }, #endif /* SQLITE_HAVE_ZLIB */ #ifdef SQLITE_HAVE_BROTLI { ZONEFILE_COMPRESSION_BROTLI, "brotli", zfGenericOpen, zfGenericClose, 0, zfBrotliCompressBound, zfBrotliCompress, zfGenericUncompressSize, zfBrotliUncompress }, #endif /* SQLITE_HAVE_BROTLI */ #ifdef SQLITE_HAVE_LZ4 { ZONEFILE_COMPRESSION_LZ4, "lz4", zfGenericOpen, zfGenericClose, 0, zfLz4CompressBound, zfLz4Compress, zfGenericUncompressSize, zfLz4Uncompress }, { ZONEFILE_COMPRESSION_LZ4HC, "lz4hc", zfGenericOpen, zfGenericClose, 0, zfLz4CompressBound, zfLz4hcCompress, zfGenericUncompressSize, zfLz4Uncompress }, #endif /* SQLITE_HAVE_LZ4 */ }; /* ** Find the ZonefileCompress object for the compression scheme named ** by zName. If successful, set output variable (*pp) to point to the ** object and return SQLITE_OK. Otherwise, return SQLITE_ERROR and ** leave output variable (*pzErr) pointing to an English language error ** message. It is the reponsibility of the caller to eventually free ** the error message buffer using sqlite3_free(). */ static int zonefileCompress( const char *zName, /* Name of requested compression method */ ZonefileCompress **pp, /* OUT: Pointer to compression object */ char **pzErr /* OUT: Error message */ ){ int i; assert( *pzErr==0 ); for(i=0; i<sizeof(aZonefileCompress)/sizeof(aZonefileCompress[0]); i++){ if( sqlite3_stricmp(aZonefileCompress[i].zName, zName)==0 ){ *pp = &aZonefileCompress[i]; return SQLITE_OK; } } *pzErr = sqlite3_mprintf("unknown compression scheme: \"%s\"", zName); return SQLITE_ERROR; } /* ** Find a compression routine based on its associated integer constant ** (defined as part of the zonefile file format). If successful, return ** a pointer to the associated ZonefileCompression object. Or, if the ** nominated compression scheme is not supported in this build, return ** NULL. */ static ZonefileCompress *zonefileCompressByValue(int eType){ int i; for(i=0; i<sizeof(aZonefileCompress)/sizeof(aZonefileCompress[0]); i++){ if( aZonefileCompress[i].eType==eType ){ return &aZonefileCompress[i]; } } return 0; } /* End of code for compression routines **************************************************************************/ /* ** A structure to store the parameters for a single zonefile_write() ** invocation. An instance of this structure is populated based on ** the json parameters passed to zonefile_write() by function ** zonefileGetParams(); */ typedef struct ZonefileParam ZonefileParam; struct ZonefileParam { ZonefileCompress *pCmpIdx; /* For compressing the index */ ZonefileCompress *pCmpData; /* For compressing each frame */ int encryptionType; int maxAutoFrameSize; int debugExtendedHeaderSize; /* Size of extended header */ int debugEncryptionKeyText; /* True to allow text keys */ char *encryptionKey; /* Encryption key */ }; /* ** A structure to store a deserialized zonefile header in. */ typedef struct ZonefileHeader ZonefileHeader; struct ZonefileHeader { u32 magicNumber; u8 compressionTypeIndexData; u8 compressionTypeContent; u32 byteOffsetDictionary; u32 byteOffsetFrames; u32 numFrames; u32 numKeys; u8 encryptionType; u8 encryptionKeyIdx; u8 extendedHeaderVersion; u8 extendedHeaderSize; }; /* ** Resizable buffer structure used by the zonefile_write() implementation. */ typedef struct ZonefileBuffer ZonefileBuffer; struct ZonefileBuffer { u8 *a; int n; int nAlloc; }; /* ** Set the error message contained in context ctx to the results of ** vprintf(zFmt, ...). */ static void zonefileCtxError(sqlite3_context *ctx, const char *zFmt, ...){ char *zMsg = 0; va_list ap; va_start(ap, zFmt); zMsg = sqlite3_vmprintf(zFmt, ap); sqlite3_result_error(ctx, zMsg, -1); sqlite3_free(zMsg); va_end(ap); } /* ** Retrieve the error message from the database handle associated with ** context object pCtx and copy it into pCtx itself. */ static void zonefileTransferError(sqlite3_context *pCtx){ sqlite3 *db = sqlite3_context_db_handle(pCtx); const char *zErr = sqlite3_errmsg(db); sqlite3_result_error(pCtx, zErr, -1); } /* ** Create an SQL statement from the printf() style format string passed ** as the 4th argument and any trailing arguments. Attempt to prepare ** it against database handle db. If successful, set output variable ** (*ppStmt) to point to the new statement handle and return SQLITE_OK. ** In this case it is the responsibility of the caller to eventually ** release the statement handle using sqlite3_finalize(). ** ** Or, if an error occurs, return an SQLite error code and optionally ** set output parameter (*pzErr) to point to a buffer containing an ** English language error message. The caller must eventually free any ** such buffer using sqlite3_free(). */ static int zonefilePrepare( sqlite3 *db, /* Database handle */ sqlite3_stmt **ppStmt, /* OUT: New statement handle */ char **pzErr, /* OUT: Error message */ const char *zFmt, /* printf() style formatting string */ ... ){ int rc; va_list ap; char *zSql; va_start(ap, zFmt); zSql = sqlite3_vmprintf(zFmt, ap); *ppStmt = 0; if( zSql ){ rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); if( rc!=SQLITE_OK ){ *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); } sqlite3_free(zSql); }else{ rc = SQLITE_NOMEM; } return rc; } /* ** Return zero if the two SQL values passed as arguments are equal, or ** non-zero otherwise. Values with different types are considered unequal, ** even if they both contain the same numeric value (e.g. 2 and 2.0). */ static int zonefileCompareValue(sqlite3_value *p1, sqlite3_value *p2){ int eType; assert( p1 ); eType = sqlite3_value_type(p1); if( sqlite3_value_type(p2)!=eType ) return 1; switch( eType ){ case SQLITE_INTEGER: return sqlite3_value_int64(p1)!=sqlite3_value_int64(p2); case SQLITE_FLOAT: return sqlite3_value_double(p1)!=sqlite3_value_double(p2); case SQLITE_TEXT: case SQLITE_BLOB: { int n1 = sqlite3_value_bytes(p1); int n2 = sqlite3_value_bytes(p2); if( n1!=n2 ) return 1; return memcmp(sqlite3_value_blob(p1), sqlite3_value_blob(p2), n1); } default: assert( eType==SQLITE_NULL); } return 0; } /* ** Return true if the value object passed as the only argument contains ** integer value -1. False otherwise. */ int zonefileIsAutoFrame(sqlite3_value *pFrame){ return ( sqlite3_value_type(pFrame)==SQLITE_INTEGER && sqlite3_value_int64(pFrame)==-1 ); } #define SQLITE_ZONEFILE_AES_128_CTR 1 #define SQLITE_ZONEFILE_AES_128_CBC 2 #define SQLITE_ZONEFILE_AES_256_CTR 3 #define SQLITE_ZONEFILE_AES_256_CBC 4 /* ** Argument zName is the name of an encryption scheme. If it is recognized ** output variable (*peType) is set to the corresponding integer constant ** (defined as part of the zonefile file format) and SQLITE_OK is returned. ** Or, if the named encryption method is not recognized, SQLITE_ERROR is ** returned and (*pzErr) set to point to a buffer containing an English ** language error message. In this case it is the responsibility of ** the caller to eventually free the error message buffer using a call ** to sqlite3_free(). */ static int zonefileEncryption(const char *zName, int *peType, char **pzErr){ struct Encryption { const char *zName; int eType; } a[] = { {"NONE", 0}, {"AES_128_CTR", SQLITE_ZONEFILE_AES_128_CTR}, {"AES_128_CBC", SQLITE_ZONEFILE_AES_128_CBC}, {"AES_256_CTR", SQLITE_ZONEFILE_AES_256_CTR}, {"AES_256_CBC", SQLITE_ZONEFILE_AES_256_CBC}, {"XOR", 5}, }; int i; for(i=0; i<sizeof(a)/sizeof(a[0]); i++){ if( 0==sqlite3_stricmp(zName, a[i].zName) ){ *peType = a[i].eType; return SQLITE_OK; } } *pzErr = sqlite3_mprintf("unknown encryption method: %s", zName); return SQLITE_ERROR; } /* ** Interpret the "json paramaters" argument passed to an invocation of ** the zonefile_write() SQL function. Populate the ZonefileParam indicated ** by the 3rd argument to this function with the results. ** ** If successful, return SQLITE_OK. Otherwise, return an SQLite error ** code and leave an error message in context object pCtx. */ static int zonefileGetParams( sqlite3_context *pCtx, /* Leave any error message here */ const char *zJson, /* JSON configuration parameter (or NULL) */ ZonefileParam *p /* Populate this object before returning */ ){ sqlite3 *db = sqlite3_context_db_handle(pCtx); sqlite3_stmt *pStmt = 0; char *zErr = 0; int rc = SQLITE_OK; int rc2; memset(p, 0, sizeof(ZonefileParam)); p->maxAutoFrameSize = ZONEFILE_DEFAULT_MAXAUTOFRAMESIZE; p->encryptionType = ZONEFILE_DEFAULT_ENCRYPTION; p->pCmpData = p->pCmpIdx = zonefileCompressByValue(ZONEFILE_COMPRESSION_NONE); rc = zonefilePrepare(db, &pStmt, &zErr,"SELECT key, value FROM json_each(?)"); if( rc==SQLITE_OK ){ sqlite3_bind_text(pStmt, 1, zJson, -1, SQLITE_STATIC); } while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ const char *zKey = (const char*)sqlite3_column_text(pStmt, 0); int iVal = sqlite3_column_int(pStmt, 1); if( sqlite3_stricmp("debugExtendedHeaderSize", zKey)==0 ){ if( iVal<0 || iVal>255 ){ zErr = sqlite3_mprintf( "debugExtendedHeaderSize value out of range: %d", iVal ); rc = SQLITE_ERROR; } p->debugExtendedHeaderSize = iVal; }else if( sqlite3_stricmp("debugEncryptionKeyText", zKey)==0 ){ p->debugEncryptionKeyText = iVal; }else if( sqlite3_stricmp("maxAutoFrameSize", zKey)==0 ){ p->maxAutoFrameSize = iVal; }else if( sqlite3_stricmp("compressionTypeIndexData", zKey)==0 ){ const char *zName = (const char*)sqlite3_column_text(pStmt, 1); rc = zonefileCompress(zName, &p->pCmpIdx, &zErr); }else if( sqlite3_stricmp("compressionTypeContent", zKey)==0 ){ const char *zName = (const char*)sqlite3_column_text(pStmt, 1); rc = zonefileCompress(zName, &p->pCmpData, &zErr); }else if( sqlite3_stricmp("encryptionKey", zKey)==0 ){ const char *zKey = (const char*)sqlite3_column_text(pStmt, 1); p->encryptionKey = sqlite3_mprintf("%s", zKey); if( p->encryptionKey==0 ) rc = SQLITE_NOMEM; }else if( sqlite3_stricmp("encryptionType", zKey)==0 ){ const char *zName = (const char*)sqlite3_column_text(pStmt, 1); rc = zonefileEncryption(zName, &p->encryptionType, &zErr); }else{ rc = SQLITE_ERROR; zErr = sqlite3_mprintf("unknown parameter name: \"%s\"", zKey); } } rc2 = sqlite3_finalize(pStmt); if( rc==SQLITE_OK ) rc = rc2; if( zErr ){ sqlite3_result_error(pCtx, zErr, -1); sqlite3_free(zErr); }else if( rc ){ sqlite3_result_error_code(pCtx, rc); }else{ if( p->encryptionKey==0 ){ p->encryptionType = 0; } } return rc; } /* ** Check that there is room in buffer pBuf for at least nByte bytes more ** data. If not, attempt to allocate more space. If the allocation attempt ** fails, leave an error message in context pCtx and return SQLITE_ERROR. ** ** If no error occurs, SQLITE_OK is returned. */ static int zonefileBufferGrow( sqlite3_context *pCtx, ZonefileBuffer *pBuf, int nByte ){ int nReq = pBuf->n + nByte; if( nReq>pBuf->nAlloc ){ u8 *aNew; int nNew = pBuf->nAlloc ? pBuf->nAlloc*2 : 128; while( nNew<nReq ) nNew = nNew*2; aNew = sqlite3_realloc(pBuf->a, nNew); if( aNew==0 ){ sqlite3_result_error_nomem(pCtx); return SQLITE_ERROR; } pBuf->a = aNew; pBuf->nAlloc = nNew; } return SQLITE_OK; } /* ** Free the memory allocation associated with buffer pBuf. */ static void zonefileBufferFree(ZonefileBuffer *pBuf){ sqlite3_free(pBuf->a); memset(pBuf, 0, sizeof(ZonefileBuffer)); } /* ** Read and return a 64-bit unsigned integer in big-endian format from ** buffer aBuf. */ static u64 zonefileGet64(u8 *aBuf){ return (((u64)aBuf[0]) << 56) + (((u64)aBuf[1]) << 48) + (((u64)aBuf[2]) << 40) + (((u64)aBuf[3]) << 32) + (((u64)aBuf[4]) << 24) + (((u64)aBuf[5]) << 16) + (((u64)aBuf[6]) << 8) + (((u64)aBuf[7]) << 0); } /* ** Append a 32-bit big-endian integer with value v to buffer pBuf. Space ** must have already been reserved in the buffer using zonefileBufferGrow(). */ static void zonefileAppend32(ZonefileBuffer *pBuf, u32 v){ zonefilePut32(&pBuf->a[pBuf->n], v); pBuf->n += 4; } /* ** Append a 64-bit big-endian integer with value v to buffer pBuf. Space ** must have already been reserved in the buffer using zonefileBufferGrow(). */ static void zonefileAppend64(ZonefileBuffer *pBuf, u64 v){ zonefileAppend32(pBuf, v>>32); zonefileAppend32(pBuf, v & 0xFFFFFFFF); } /* ** Append the n bytse of data in buffer p to buffer pBuf. Space must have ** already been reserved in the buffer using zonefileBufferGrow(). */ static void zonefileAppendBlob(ZonefileBuffer *pBuf, const u8 *p, int n){ memcpy(&pBuf->a[pBuf->n], p, n); pBuf->n += n; } /* ** Write nBuf bytes of data from buffer aBuf to the file opened by ** file-handle pFd. Return SQLITE_OK if successful, or SQLITE_ERROR ** otherwise. */ static int zonefileFileWrite(FILE *pFd, const u8 *aBuf, int nBuf){ size_t res = fwrite(aBuf, 1, nBuf, pFd); return res!=(size_t)nBuf ? SQLITE_ERROR : SQLITE_OK; } /* ** Read nBuf bytes of data from offset iOff of the file opened by ** file-handle pFd into buffer aBuf. Return SQLITE_OK if successful, or ** SQLITE_ERROR otherwise. */ static int zonefileFileRead(FILE *pFd, u8 *aBuf, int nBuf, i64 iOff){ int rc = fseek(pFd, (long)iOff, SEEK_SET); if( rc==0 ){ rc = fread(aBuf, 1, nBuf, pFd); rc = (rc==nBuf) ? SQLITE_OK : SQLITE_ERROR; }else{ rc = SQLITE_ERROR; } return rc; } /* ** Open the file identified by path zFile for writing (if bWrite==1) or ** reading (if bWrite==0). If successful, return a pointer to the new ** file-handle object. Otherwise, return NULL and set output variable ** (*pzErr) to point to a buffer containing an English language error ** message. It is the responsibility of the caller to eventually free ** any error message buffer using sqlite3_free(). */ static FILE *zonefileFileOpen(const char *zFile, int bWrite, char **pzErr){ FILE *pFd = fopen(zFile, bWrite ? "wb" : "rb"); if( pFd==0 ){ *pzErr = sqlite3_mprintf("failed to open file \"%s\" for %s", zFile, bWrite ? "writing" : "reading" ); } return pFd; } /* ** Close the file handle passed as the only argument. */ static void zonefileFileClose(FILE *pFd){ if( pFd ) fclose(pFd); } /* ** Append the contents of buffer pFrom to buffer pTo. If successful, return ** SQLITE_OK. Otherwise, return an SQLite error code and leave an error ** message in context object pCtx. ** ** If argument pMethod is not NULL, then it is used along with pCmp to ** compress the data before appending it to pFrom. Similarly, if argument ** pCodec is not NULL, then it is used to encrypt the data before it is ** appended. */ static int zonefileAppendData( sqlite3_context *pCtx, /* Leave any error message here */ ZonefileCompress *pMethod, /* Compression method object */ void *pCmp, /* Compression handle */ ZonefileCodec *pCodec, /* Compression method, if any */ ZonefileBuffer *pTo, /* Append new data here */ ZonefileBuffer *pFrom /* Input buffer */ ){ int rc = SQLITE_OK; int nNonce = pCodec ? zonefileCodecNonceSize(pCodec) : 0; int nOrig = pTo->n; if( pMethod->eType==ZONEFILE_COMPRESSION_NONE ){ if( zonefileBufferGrow(pCtx, pTo, pFrom->n + nNonce) ){ rc = SQLITE_ERROR; }else{ zonefileAppendBlob(pTo, pFrom->a, pFrom->n); } }else{ int nReq = pMethod->xCompressBound(pCmp, pFrom->n); if( zonefileBufferGrow(pCtx, pTo, nReq + nNonce) ) return SQLITE_ERROR; rc = pMethod->xCompress(pCmp, &pTo->a[pTo->n], &nReq, pFrom->a, pFrom->n); pTo->n += nReq; } /* Encrypt the data just appended to buffer pTo. */ if( rc==SQLITE_OK && pCodec ){ zonefileCodecEncode(pCodec, &pTo->a[nOrig], pTo->n - nOrig); pTo->n += nNonce; } return rc; } /* ** Append nByte bytes of garbage data to the file opened with pFd. Return ** SQLITE_OK if successful, or SQLITE_ERROR if an error occurs. ** ** Parameter nByte is only ever non-zero when running tests. So it doesn't ** matter if this function is inefficient in those cases. */ static int zonefilePad(FILE *pFd, int nByte){ assert( nByte>=0 && nByte<256 ); if( nByte ){ int nRem = nByte; u8 buf[17] = "0123456789ABCDEF"; while( nRem>0 ){ int n = MIN(nRem, sizeof(buf)); if( zonefileFileWrite(pFd, buf, n) ) return SQLITE_ERROR; nRem -= n; } } return SQLITE_OK; } /* ** If character c is not a hexadecimal digit, return -1. Otherwise, return ** the value of the hex digit (a value between 0 and 15). */ static int zonefileHexChar(char c){ if( c>='0' && c<='9' ) return c-'0'; c = c & ~0x20; if( c>='A' && c<='F' ) return c-('A'-10); return -1; } /* ** String ZonefileParam.encryptionKey currently contains a string specified ** for the encryptionKey attribute of a JSON object passed to SQL function ** zonefile_write(). The string is (*pn) bytes in size. ** ** If the ZonefileParam.debugEncryptionKeyText flag is true this function ** is a no-op. Otherwise, an attempt is made to overwrite the hex string in ** ZonefileParam.encryptionKey with the corresponding binary data. If ** successful, SQLITE_OK is returned and (*pn) is set to the number of ** bytes in the binary key. Otherwise, if an error occurs, an SQLite error ** code is returned and (*pzErr) set to point to an English language error ** message. It is the responsibility of the caller to eventually free any ** error message buffer using sqlite3_free(). */ static int zonefileDecodeEncryptionKey(ZonefileParam *p, int *pn, char **pzErr){ if( p->debugEncryptionKeyText==0 ){ u8 *z = (u8*)p->encryptionKey; int n = *pn; int i; if( n&0x01 ) goto bad_format; for(i=0; i<n; i+=2){ int a = zonefileHexChar(z[i]); int b = zonefileHexChar(z[i+1]); if( a<0 || b<0 ) goto bad_format; z[i/2] = (u8)(a<<4) + (u8)b; } *pn = n/2; } return SQLITE_OK; bad_format: *pzErr = sqlite3_mprintf("badly formatted hex string"); return SQLITE_ERROR; } /* ** Function: zonefile_write(F,T[,J]) */ static void zonefileWriteFunc( sqlite3_context *pCtx, /* Context object */ int objc, /* Number of SQL arguments */ sqlite3_value **objv /* Array of SQL arguments */ ){ const char *zFile = 0; /* File to write to */ const char *zTbl = 0; /* Database object to read from */ const char *zJson = 0; /* JSON configuration parameters */ ZonefileParam sParam; /* Decoded JSON parameters */ int nKey = 0; /* Number of keys in new zonefile */ int nFrame = 0; /* Number of frames in new zonefile */ sqlite3_stmt *pStmt = 0; /* SQL used to read data from source table */ FILE *pFd = 0; int rc = SQLITE_OK; sqlite3_value *pPrev = 0; char *zErr = 0; void *pCmp = 0; /* Data compressor handle */ u32 iOff; ZonefileCodec *pCodec = 0; ZonefileBuffer sFrameIdx = {0, 0, 0}; /* Array of frame offsets */ ZonefileBuffer sKeyIdx = {0, 0, 0}; /* Array of key locations */ ZonefileBuffer sData = {0, 0, 0}; /* All completed frames so far */ ZonefileBuffer sFrame = {0, 0, 0}; /* Current frame (uncompressed) */ ZonefileBuffer sDict = {0, 0, 0}; /* Compression model (if any) */ ZonefileBuffer sSample = {0,0,0}; /* Sample data for compressor */ size_t *aSample = 0; /* Array of sample sizes */ u8 aHdr[ZONEFILE_SZ_HEADER]; /* Space to assemble zonefile header */ assert( objc==2 || objc==3 ); zFile = (const char*)sqlite3_value_text(objv[0]); zTbl = (const char*)sqlite3_value_text(objv[1]); if( objc==3 ){ zJson = (const char*)sqlite3_value_text(objv[2]); } if( zonefileGetParams(pCtx, zJson, &sParam) ) return; if( sParam.encryptionType!=0 ){ int n = strlen(sParam.encryptionKey); rc = zonefileDecodeEncryptionKey(&sParam, &n, &zErr); if( rc==SQLITE_OK ){ rc = zonefileCodecCreate(sParam.encryptionType, 1, (u8*)sParam.encryptionKey, n, &pCodec, &zErr ); } if( rc!=SQLITE_OK ){ if( zErr ){ sqlite3_result_error(pCtx, zErr, -1); }else{ sqlite3_result_error_code(pCtx, rc); } sqlite3_free(zErr); goto zone_write_out; } } /* Check that the index-data compressor is not one that uses an external ** dictionary. This is not permitted as there is no sense in using a ** dictionary to compress a single blob of data, and the index-data ** compressor only ever compresses a single frame. Also, there is nowhere ** in the file-format to store such a dictionary for the index-data. */ if( sParam.pCmpIdx->xTrain ){ zonefileCtxError(pCtx, "compressor \"%s\" may not be used to compress the zonefile index", sParam.pCmpIdx->zName ); goto zone_write_out; } if( sParam.pCmpData->xOpen ){ rc = sParam.pCmpData->xOpen(&pCmp, 0, 0); if( rc!=SQLITE_OK ){ zonefileCtxError(pCtx, "error in compressor construction"); goto zone_write_out; } } /* Prepare the SQL statement used to read data from the source table. This ** also serves to verify the suitability of the source table schema. */ rc = zonefilePrepare(sqlite3_context_db_handle(pCtx), &pStmt, &zErr, "SELECT k, frame, v FROM %Q ORDER BY frame, idx, k", zTbl ); /* Open the file-handle used to write out the zonefile */ if( rc==SQLITE_OK ){ pFd = zonefileFileOpen(zFile, 1, &zErr); } if( pFd==0 ){ sqlite3_result_error(pCtx, zErr, -1); sqlite3_free(zErr); goto zone_write_out; } /* If the data compressor uses a global dictionary, create the dictionary ** and store it in buffer sDict. */ if( sParam.pCmpData->xTrain ){ int nSample = 0; while( SQLITE_ROW==sqlite3_step(pStmt) ){ int nByte = sqlite3_column_bytes(pStmt, 2); const u8 *aByte = (const u8*)sqlite3_column_blob(pStmt, 2); if( zonefileBufferGrow(pCtx, &sSample, nByte) ){ goto zone_write_out; } if( (nSample & (nSample-1))==0 ){ int nNew = nSample ? nSample*2 : 8; size_t *aNew = (size_t*)sqlite3_realloc(aSample, sizeof(size_t) * nNew); if( aNew==0 ){ sqlite3_result_error_nomem(pCtx); goto zone_write_out; } aSample = aNew; } aSample[nSample] = nByte; zonefileAppendBlob(&sSample, aByte, nByte); nSample++; } rc = sqlite3_reset(pStmt); if( rc!=SQLITE_OK ){ zonefileTransferError(pCtx); goto zone_write_out; } if( zonefileBufferGrow(pCtx, &sDict, ZONEFILE_DEFAULT_DICTSIZE) ){ goto zone_write_out; } sDict.n = sDict.nAlloc; rc = sParam.pCmpData->xTrain( pCmp, sDict.a, &sDict.n, sSample.a, aSample, nSample ); if( rc!=SQLITE_OK ){ zonefileCtxError(pCtx, "error generating dictionary"); goto zone_write_out; } } while( SQLITE_ROW==sqlite3_step(pStmt) ){ sqlite3_int64 k = sqlite3_column_int64(pStmt, 0); sqlite3_value *pFrame = sqlite3_column_value(pStmt, 1); int nBlob = sqlite3_column_bytes(pStmt, 2); const u8 *pBlob = (const u8*)sqlite3_column_blob(pStmt, 2); int bAuto = zonefileIsAutoFrame(pFrame); if( sFrame.n>0 ){ if( zonefileCompareValue(pFrame, pPrev) || (bAuto && (sFrame.n+nBlob)>sParam.maxAutoFrameSize) ){ /* Add new entry to sFrame */ if( zonefileBufferGrow(pCtx, &sFrameIdx, 4) || zonefileAppendData(pCtx,sParam.pCmpData,pCmp,pCodec,&sData,&sFrame) ){ goto zone_write_out; } sFrame.n = 0; zonefileAppend32(&sFrameIdx, sData.n); sqlite3_value_free(pPrev); pPrev = 0; nFrame++; } } if( pPrev==0 ){ pPrev = sqlite3_value_dup(pFrame); if( pPrev==0 ){ sqlite3_result_error_nomem(pCtx); goto zone_write_out; } } /* Add new entry to sKeyIdx */ if( zonefileBufferGrow(pCtx, &sKeyIdx, ZONEFILE_SZ_KEYOFFSETS_ENTRY) ){ goto zone_write_out; } zonefileAppend64(&sKeyIdx, k); zonefileAppend32(&sKeyIdx, nFrame); zonefileAppend32(&sKeyIdx, sFrame.n); zonefileAppend32(&sKeyIdx, nBlob); /* Add uncompressed data for new entry to sFrame */ if( zonefileBufferGrow(pCtx, &sFrame, nBlob) ) goto zone_write_out; zonefileAppendBlob(&sFrame, pBlob, nBlob); nKey++; } if( sFrame.n>0 ){ if( zonefileBufferGrow(pCtx, &sFrameIdx, 4) || zonefileAppendData(pCtx, sParam.pCmpData, pCmp, pCodec, &sData, &sFrame) ){ goto zone_write_out; } zonefileAppend32(&sFrameIdx, sData.n); nFrame++; } /* If a compression method was specified, compress the key-index here */ if( sParam.pCmpIdx->eType!=ZONEFILE_COMPRESSION_NONE ){ if( zonefileBufferGrow(pCtx, &sFrameIdx, sKeyIdx.n) ) goto zone_write_out; zonefileAppendBlob(&sFrameIdx, sKeyIdx.a, sKeyIdx.n); zonefileBufferFree(&sKeyIdx); rc = zonefileAppendData(pCtx, sParam.pCmpIdx, 0, 0, &sKeyIdx, &sFrameIdx); sFrameIdx.n = 0; if( rc ) goto zone_write_out; } /* Create the zonefile header in the in-memory buffer */ memset(aHdr, 0, ZONEFILE_SZ_HEADER); zonefilePut32(&aHdr[0], ZONEFILE_MAGIC_NUMBER); aHdr[4] = (u8)sParam.pCmpIdx->eType; aHdr[5] = (u8)sParam.pCmpData->eType; iOff = ZONEFILE_SZ_HEADER + sFrameIdx.n + sKeyIdx.n; zonefilePut32(&aHdr[6], sDict.n ? iOff+sParam.debugExtendedHeaderSize : 0); zonefilePut32(&aHdr[10], iOff + sParam.debugExtendedHeaderSize + sDict.n); zonefilePut32(&aHdr[14], nFrame); zonefilePut32(&aHdr[18], nKey); aHdr[22] = (u8)sParam.encryptionType; aHdr[23] = 0; /* Encryption key index */ aHdr[24] = 0; /* extended header version */ aHdr[25] = (u8)sParam.debugExtendedHeaderSize; assert( ZONEFILE_SZ_HEADER>=26 ); rc = zonefileFileWrite(pFd, aHdr, ZONEFILE_SZ_HEADER); if( rc==SQLITE_OK ) rc = zonefilePad(pFd, sParam.debugExtendedHeaderSize); if( rc==SQLITE_OK ) rc = zonefileFileWrite(pFd, sFrameIdx.a, sFrameIdx.n); if( rc==SQLITE_OK ) rc = zonefileFileWrite(pFd, sKeyIdx.a, sKeyIdx.n); if( rc==SQLITE_OK ) rc = zonefileFileWrite(pFd, sDict.a, sDict.n); if( rc==SQLITE_OK ) rc = zonefileFileWrite(pFd, sData.a, sData.n); if( rc ){ zonefileCtxError(pCtx, "error writing file \"%s\" (fwrite())", zFile); goto zone_write_out; } if( fclose(pFd) ){ zonefileCtxError(pCtx, "error writing file \"%s\" (fclose())", zFile); } pFd = 0; zone_write_out: if( pCmp ) sParam.pCmpData->xClose(pCmp); if( pFd ) fclose(pFd); sqlite3_value_free(pPrev); sqlite3_finalize(pStmt); zonefileCodecDestroy(pCodec); zonefileBufferFree(&sFrameIdx); zonefileBufferFree(&sKeyIdx); zonefileBufferFree(&sFrame); zonefileBufferFree(&sDict); zonefileBufferFree(&sData); zonefileBufferFree(&sSample); sqlite3_free(aSample); sqlite3_free(sParam.encryptionKey); if( rc==SQLITE_NOMEM ){ sqlite3_result_error_nomem(pCtx); } } /* ** Virtual table type for zonefile_files virtual tables. */ typedef struct ZonefileFilesTab ZonefileFilesTab; struct ZonefileFilesTab { sqlite3_vtab base; /* Base class - must be first */ sqlite3 *db; char *zBase; /* Name of this table */ char *zDb; /* Database containing this table */ ZonefileGlobal *pGlobal; /* Global object */ sqlite3_stmt *pInsert; /* Insert into the %_shadow_file table */ sqlite3_stmt *pInsertIdx; /* Insert into the %_shadow_idx table */ sqlite3_stmt *pDeleteIdx; /* Delete by fileid from %_shadow_idx table */ sqlite3_stmt *pDelete; /* Delete by rowid from %_shadow_file table */ }; /* ** Virtual table cursor type for zonefile_files virtual tables. */ typedef struct ZonefileFilesCsr ZonefileFilesCsr; struct ZonefileFilesCsr { sqlite3_vtab_cursor base; /* Base class - must be first */ sqlite3_stmt *pSelect; }; /* ** This function does the work of xCreate (if bCreate!=0) or xConnect ** (if bCreate==0) for the zonefile_files module. */ static int zffCreateConnect( int bCreate, void *pAux, sqlite3 *db, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ ZonefileFilesTab *p; const char *zName = argv[2]; const char *zDb = argv[1]; int nName = strlen(zName); int nDb = strlen(zDb); int rc = SQLITE_OK; if( nName<6 || memcmp(&zName[nName-6], "_files", 6)!=0 ){ *pzErr = sqlite3_mprintf("do not create zonefile_files tables directly!"); *ppVtab = 0; return SQLITE_ERROR; } p = (ZonefileFilesTab*)sqlite3_malloc(sizeof(ZonefileFilesTab)+nName+1+nDb+1); if( !p ){ rc = SQLITE_NOMEM; }else{ memset(p, 0, sizeof(ZonefileFilesTab)); p->zBase = (char*)&p[1]; memcpy(p->zBase, zName, nName-6); p->zBase[nName-6] = '\0'; p->zDb = &p->zBase[nName+1]; memcpy(p->zDb, zDb, nDb+1); p->db = db; p->pGlobal = (ZonefileGlobal*)pAux; rc = sqlite3_declare_vtab(db, ZONEFILE_FILES_SCHEMA); } if( rc!=SQLITE_OK ){ sqlite3_free(p); p = 0; } *ppVtab = (sqlite3_vtab*)p; return rc; } /* ** zonefile_files virtual table module xCreate method. */ static int zffCreate( sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ return zffCreateConnect(1, pAux, db, argc, argv, ppVtab, pzErr); } /* ** zonefile_files virtual table module xConnect method. */ static int zffConnect( sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ return zffCreateConnect(0, pAux, db, argc, argv, ppVtab, pzErr); } /* ** zonefile_files virtual table module xDisconnect method. */ static int zffDisconnect(sqlite3_vtab *pVtab){ ZonefileFilesTab *pTab = (ZonefileFilesTab*)pVtab; sqlite3_finalize(pTab->pInsert); sqlite3_finalize(pTab->pDelete); sqlite3_finalize(pTab->pInsertIdx); sqlite3_finalize(pTab->pDeleteIdx); sqlite3_free(pTab); return SQLITE_OK; } /* ** zonefile_files virtual table module xBestIndex method. */ static int zffBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pInfo){ return SQLITE_OK; } /* ** zonefile_files virtual table module xOpen method. */ static int zffOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){ ZonefileFilesCsr *pCsr; pCsr = (ZonefileFilesCsr*)sqlite3_malloc(sizeof(ZonefileFilesCsr)); if( pCsr==0 ){ return SQLITE_NOMEM; } memset(pCsr, 0, sizeof(ZonefileFilesCsr)); *ppCursor = (sqlite3_vtab_cursor*)pCsr; return SQLITE_OK; } /* ** Reset a ZonefileFilesCsr object to the state it is in immediately after ** it is allocated by zffOpen(). */ static void zffCursorReset(ZonefileFilesCsr *pCsr){ sqlite3_finalize(pCsr->pSelect); pCsr->pSelect = 0; } /* ** zonefile_files virtual table module xClose method. */ static int zffClose(sqlite3_vtab_cursor *cur){ ZonefileFilesCsr *pCsr = (ZonefileFilesCsr*)cur; zffCursorReset(pCsr); sqlite3_free(pCsr); return SQLITE_OK; } /* ** zonefile_files virtual table module xNext method. */ static int zffNext(sqlite3_vtab_cursor *cur){ ZonefileFilesCsr *pCsr = (ZonefileFilesCsr*)cur; int rc; if( SQLITE_ROW==sqlite3_step(pCsr->pSelect) ){ rc = SQLITE_OK; }else{ rc = sqlite3_finalize(pCsr->pSelect); pCsr->pSelect = 0; } return rc; } /* ** zonefile_files virtual table module xFilter method. */ static int zffFilter( sqlite3_vtab_cursor *cur, int idxNum, const char *idxStr, int argc, sqlite3_value **argv ){ ZonefileFilesCsr *pCsr = (ZonefileFilesCsr*)cur; ZonefileFilesTab *pTab = (ZonefileFilesTab*)(pCsr->base.pVtab); int rc; zffCursorReset(pCsr); rc = zonefilePrepare(pTab->db, &pCsr->pSelect, &pTab->base.zErrMsg, "SELECT filename, fileid FROM %Q.'%q_shadow_file'", pTab->zDb, pTab->zBase ); if( rc==SQLITE_OK ){ rc = zffNext(cur); } return rc; } /* ** zonefile_files virtual table module xEof method. */ static int zffEof(sqlite3_vtab_cursor *cur){ ZonefileFilesCsr *pCsr = (ZonefileFilesCsr*)cur; return pCsr->pSelect==0; } /* ** Deserialize the ZONEFILE_SZ_HEADER byte zonefile header in the ** buffer. Populate (*pHdr) with the results. */ static void zonefileHeaderDeserialize(u8 *aBuf, ZonefileHeader *pHdr){ pHdr->magicNumber = zonefileGet32(&aBuf[0]); pHdr->compressionTypeIndexData = aBuf[4]; pHdr->compressionTypeContent = aBuf[5]; pHdr->byteOffsetDictionary = zonefileGet32(&aBuf[6]); pHdr->byteOffsetFrames = zonefileGet32(&aBuf[10]); pHdr->numFrames = zonefileGet32(&aBuf[14]); pHdr->numKeys = zonefileGet32(&aBuf[18]); pHdr->encryptionType = aBuf[22]; pHdr->encryptionKeyIdx = aBuf[23]; pHdr->extendedHeaderVersion = aBuf[24]; pHdr->extendedHeaderSize = aBuf[25]; } /* ** Read and decode a Zonefile header from the start of the file opened ** by file-handle pFd. If successful, populate object (*pHdr) before ** returning SQLITE_OK. Otherwise, if an error occurs, set output ** parameter (*pzErr) to point to an English language error message and ** return an SQLite error code. ** ** It is the responsibility of the caller to eventually free any error ** message returned via (*pzErr) using sqlite3_free(). */ static int zonefileReadHeader( FILE *pFd, /* File to read from */ const char *zFile, /* Name of file opened by pFd */ ZonefileHeader *pHdr, /* Populate this object before returning */ char **pzErr /* OUT: Error message */ ){ u8 aBuf[ZONEFILE_SZ_HEADER]; int rc = zonefileFileRead(pFd, aBuf, ZONEFILE_SZ_HEADER, 0); if( rc==SQLITE_OK ){ zonefileHeaderDeserialize(aBuf, pHdr); if( pHdr->magicNumber!=ZONEFILE_MAGIC_NUMBER ){ rc = SQLITE_ERROR; } } if( rc!=SQLITE_OK ){ *pzErr = sqlite3_mprintf( "failed to read zonefile header from file \"%s\"", zFile ); } return rc; } /* ** Read the zonefile header from file zFile and set the result of pCtx ** to a JSON object that represents the contents. Or, if an error occurs, ** leave an error message in pCtx. This function is called whenever the ** "header" column of a zonefile_files virtual table is requested. */ static void zonefileJsonHeader(sqlite3_context *pCtx, const char *zFile){ char *zErr = 0; int rc = SQLITE_OK; ZonefileHeader hdr = {0}; FILE *pFd = zonefileFileOpen(zFile, 0, &zErr); if( pFd ){ rc = zonefileReadHeader(pFd, zFile, &hdr, &zErr); }else{ rc = SQLITE_ERROR; } if( rc==SQLITE_OK ){ char *zJson = sqlite3_mprintf("{" "\"magicNumber\":%u," "\"compressionTypeIndexData\":%u," "\"compressionTypeContent\":%u," "\"byteOffsetDictionary\":%u," "\"byteOffsetFrames\":%u," "\"numFrames\":%u," "\"numKeys\":%u," "\"encryptionType\":%u," "\"encryptionKeyIdx\":%u," "\"extendedHeaderVersion\":%u," "\"extendedHeaderSize\":%u}", (u32)hdr.magicNumber, (u32)hdr.compressionTypeIndexData, (u32)hdr.compressionTypeContent, (u32)hdr.byteOffsetDictionary, (u32)hdr.byteOffsetFrames, (u32)hdr.numFrames, (u32)hdr.numKeys, (u32)hdr.encryptionType, (u32)hdr.encryptionKeyIdx, (u32)hdr.extendedHeaderVersion, (u32)hdr.extendedHeaderSize ); if( zJson ){ sqlite3_result_text(pCtx, zJson, -1, SQLITE_TRANSIENT); sqlite3_free(zJson); }else{ sqlite3_result_error_nomem(pCtx); } }else{ sqlite3_result_error(pCtx, zErr, -1); sqlite3_free(zErr); } zonefileFileClose(pFd); } /* ** zonefile_files virtual table module xColumn method. */ static int zffColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ ZonefileFilesCsr *pCsr = (ZonefileFilesCsr*)cur; if( sqlite3_vtab_nochange(ctx) ){ return SQLITE_OK; } switch( i ){ case 0: /* filename */ sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pSelect, 0)); break; case 1: /* ekey */ break; default: { const char *zFile = (const char*)sqlite3_column_text(pCsr->pSelect, 0); zonefileJsonHeader(ctx, zFile); assert( i==2 ); break; } } return SQLITE_OK; } /* ** zonefile_files virtual table module xRowid method. */ static int zffRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ ZonefileFilesCsr *pCsr = (ZonefileFilesCsr*)cur; *pRowid = sqlite3_column_int64(pCsr->pSelect, 1); return SQLITE_OK; } /* ** Uncompress buffer aIn/nIn using the compression routines pMethod ** and the compressor instance pCmp into space obtained from ** sqlite3_malloc(). If successful, return SQLITE_OK and set output ** parameters (*paOut) and (*pnOut) to point to the allocated buffer ** and its size in bytes respectively. In this case it is the ** responsibility of the caller to eventually free buffer (*paOut) ** using sqlite3_free(). ** ** Or, if an error occurs, set both output parameters to zero and ** return an SQLite error code. */ static int zonefileUncompress( ZonefileCompress *pMethod, /* Compression routines */ void *pCmp, /* Compressor instance */ u8 *aIn, int nIn, /* Input buffer */ u8 **paOut, int *pnOut /* Output buffer */ ){ int rc; int nOut = pMethod->xUncompressSize(pCmp, aIn, nIn); u8 *aOut = sqlite3_malloc(nOut); assert( pMethod->eType!=ZONEFILE_COMPRESSION_NONE ); if( aOut==0 ){ rc = SQLITE_NOMEM; }else{ rc = pMethod->xUncompress(pCmp, aOut, nOut, aIn, nIn); } if( rc ){ sqlite3_free(aOut); aOut = 0; } *paOut = aOut; *pnOut = nOut; return rc; } /* ** Attempt to find the compression methods object for the compression method ** identified by integer constant eType (defined as part of the zonefile file ** format). If successful, set output parameter (*pp) to point to the object ** and return SQLITE_OK. Otherwise, return an SQLite error code and set ** (*pzErr) to point to a buffer containing an English language error ** message. It is the responsibility of the caller to eventually free any ** error message buffer using sqlite3_free(). */ static int zonefileFindCompress(int eType, ZonefileCompress **pp, char **pzErr){ int rc = SQLITE_OK; ZonefileCompress *pCmp; pCmp = zonefileCompressByValue(eType); if( pCmp==0 ){ *pzErr = sqlite3_mprintf("unsupported compression method: %d", eType); rc = SQLITE_ERROR; }else if( pCmp->eType==ZONEFILE_COMPRESSION_NONE ){ pCmp = 0; } *pp = pCmp; return rc; } /* ** Argument pHdr points to a deserialized zonefile header read from the ** zonefile opened by file handle pFd. This function attempts to read ** the entire zonefile-index structure into space obtained from ** sqlite3_malloc(). If successful, it sets output parameters (*paIdx) ** and (*pnIdx) to point to the buffer and its size in bytes respectively ** before returning SQLITE_OK. ** ** Otherwise, if an error occurs, an SQLite error code is returned and ** output parameter (*pzErr) may be set to point to a buffer containing an ** English language error message. It is the responsibility of the caller to ** eventually free any error message buffer using sqlite3_free(). */ static int zonefileLoadIndex( ZonefileHeader *pHdr, /* Deserialized header read from file */ FILE *pFd, /* File to read from */ u8 **paIdx, int *pnIdx, /* OUT: Buffer containing zonefile index */ char **pzErr /* OUT: Error message */ ){ ZonefileCompress *pCmp = 0; int rc; u8 *aIdx = 0; int nIdx = 0; rc = zonefileFindCompress(pHdr->compressionTypeIndexData, &pCmp, pzErr); if( rc==SQLITE_OK ){ if( pHdr->byteOffsetDictionary ){ nIdx = pHdr->byteOffsetDictionary - ZONEFILE_SZ_HEADER; }else{ nIdx = pHdr->byteOffsetFrames - ZONEFILE_SZ_HEADER; } nIdx -= pHdr->extendedHeaderSize; aIdx = (u8*)sqlite3_malloc(nIdx); if( aIdx==0 ){ rc = SQLITE_NOMEM; }else{ i64 iOff = ZONEFILE_SZ_HEADER + pHdr->extendedHeaderSize; rc = zonefileFileRead(pFd, aIdx, nIdx, iOff); } } if( rc==SQLITE_OK && pCmp ){ u8 *aUn = 0; int nUn = 0; rc = zonefileUncompress(pCmp, 0, aIdx, nIdx, &aUn, &nUn); if( rc==SQLITE_ERROR ){ *pzErr = sqlite3_mprintf("failed to uncompress index"); } sqlite3_free(aIdx); aIdx = aUn; nIdx = nUn; } if( rc!=SQLITE_OK ){ sqlite3_free(aIdx); aIdx = 0; nIdx = 0; } *paIdx = aIdx; *pnIdx = nIdx; return rc; } static int zonefilePopulateIndex( ZonefileFilesTab *pTab, const char *zFile, i64 iFileid ){ ZonefileHeader hdr = { 0 }; int rc; FILE *pFd = zonefileFileOpen(zFile, 0, &pTab->base.zErrMsg); if( pFd==0 ){ rc = SQLITE_ERROR; }else{ rc = zonefileReadHeader(pFd, zFile, &hdr, &pTab->base.zErrMsg); } if( rc==SQLITE_OK && hdr.numKeys>0 ){ u8 *aKey; /* Entire KeyOffsets array */ int nKey; /* Size of buffer aKey[] in bytes */ int i; rc = zonefileLoadIndex(&hdr, pFd, &aKey, &nKey, &pTab->base.zErrMsg); if( rc==SQLITE_OK && pTab->pInsertIdx==0 ){ rc = zonefilePrepare(pTab->db, &pTab->pInsertIdx, &pTab->base.zErrMsg, "INSERT INTO %Q.'%q_shadow_idx'(k, fileid, fofst, fsz, ofst, sz)" "VALUES(?,?,?,?,?,?)", pTab->zDb, pTab->zBase ); } for(i=0; (u32)i<hdr.numKeys && rc==SQLITE_OK; i++){ u8 *aEntry = &aKey[4*hdr.numFrames + ZONEFILE_SZ_KEYOFFSETS_ENTRY * i]; int iFrame = zonefileGet32(&aEntry[8]); i64 iFrameOff = 0; /* Offset of frame */ int szFrame; /* Compressed size of frame */ i64 iOff; /* Offset of blob within uncompressed frame */ int sz; /* Size of blob within uncompressed frame */ szFrame = zonefileGet32(&aKey[iFrame*4]); if( iFrame>0 ){ iFrameOff = zonefileGet32(&aKey[(iFrame-1)*4]); szFrame -= (int)iFrameOff; } iFrameOff += hdr.byteOffsetFrames; iOff = (i64)zonefileGet32(&aEntry[12]); sz = (int)zonefileGet32(&aEntry[16]); sqlite3_bind_int64(pTab->pInsertIdx, 1, (i64)zonefileGet64(&aEntry[0])); sqlite3_bind_int64(pTab->pInsertIdx, 2, iFileid); if( hdr.encryptionType || hdr.compressionTypeContent ){ sqlite3_bind_int64(pTab->pInsertIdx, 5, iOff); sqlite3_bind_int(pTab->pInsertIdx, 6, sz); }else{ iFrameOff += iOff; szFrame = sz; } sqlite3_bind_int64(pTab->pInsertIdx, 3, iFrameOff); sqlite3_bind_int(pTab->pInsertIdx, 4, szFrame); sqlite3_step(pTab->pInsertIdx); rc = sqlite3_reset(pTab->pInsertIdx); if( rc!=SQLITE_OK ){ pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); } } sqlite3_free(aKey); } zonefileFileClose(pFd); return rc; } /* ** zonefile_files virtual table module xUpdate method. ** ** A delete specifies a single argument - the rowid of the row to remove. ** ** Update and insert operations pass: ** ** 1. The "old" rowid, or NULL. ** 2. The "new" rowid. ** 3. Values for each of the 3 columns: (filename,ekey,header) */ static int zffUpdate( sqlite3_vtab *pVtab, int nVal, sqlite3_value **apVal, sqlite_int64 *pRowid ){ int rc = SQLITE_OK; int bUpdateKey = 0; ZonefileFilesTab *pTab = (ZonefileFilesTab*)pVtab; if( sqlite3_value_type(apVal[0])==SQLITE_INTEGER ){ if( nVal>1 && sqlite3_value_nochange(apVal[2]) ){ bUpdateKey = 1; }else{ if( pTab->pDelete==0 ){ rc = zonefilePrepare(pTab->db, &pTab->pDelete, &pVtab->zErrMsg, "DELETE FROM %Q.'%q_shadow_file' WHERE fileid=?", pTab->zDb, pTab->zBase ); } if( rc==SQLITE_OK && pTab->pDeleteIdx==0 ){ rc = zonefilePrepare(pTab->db, &pTab->pDeleteIdx, &pVtab->zErrMsg, "DELETE FROM %Q.'%q_shadow_idx' WHERE fileid=?", pTab->zDb, pTab->zBase ); } if( rc==SQLITE_OK ){ sqlite3_bind_value(pTab->pDelete, 1, apVal[0]); sqlite3_step(pTab->pDelete); rc = sqlite3_reset(pTab->pDelete); } if( rc==SQLITE_OK ){ sqlite3_bind_value(pTab->pDeleteIdx, 1, apVal[0]); sqlite3_step(pTab->pDeleteIdx); rc = sqlite3_reset(pTab->pDeleteIdx); } } } if( nVal>1 ){ i64 iFileid = 0; if( bUpdateKey ){ iFileid = sqlite3_value_int64(apVal[0]); }else{ const char *zFile = (const char*)sqlite3_value_text(apVal[2]); if( pTab->pInsert==0 ){ rc = zonefilePrepare(pTab->db, &pTab->pInsert, &pVtab->zErrMsg, "INSERT INTO %Q.'%q_shadow_file'(filename) VALUES(?)", pTab->zDb, pTab->zBase ); } /* Add the new entry to the %_shadow_file table. */ if( rc==SQLITE_OK ){ sqlite3_bind_text(pTab->pInsert, 1, zFile, -1, SQLITE_TRANSIENT); sqlite3_step(pTab->pInsert); rc = sqlite3_reset(pTab->pInsert); } /* Populate the %_shadow_idx table with entries for all keys in ** the zonefile just added to %_shadow_file. */ if( rc==SQLITE_OK ){ iFileid = sqlite3_last_insert_rowid(pTab->db); rc = zonefilePopulateIndex(pTab, zFile, iFileid); } } if( rc==SQLITE_OK ){ int nKey = sqlite3_value_bytes(apVal[3]); const u8 *aKey = (const u8*)sqlite3_value_blob(apVal[3]); rc = zonefileKeyStore( pTab->pGlobal, pTab->zDb, pTab->zBase, iFileid, aKey, nKey ); } } return rc; } /* Each entry in the frame-cache is represented by an instance of the ** following structure. */ typedef struct ZonefileFrame ZonefileFrame; struct ZonefileFrame { i64 iFileid; /* Fileid for this frame */ i64 iFrameOff; /* Offset of frame in file */ int nBuf; /* Size of aBuf[] in bytes */ u8 *aBuf; /* Buffer containing uncompressed frame data */ ZonefileFrame *pLruNext; /* Next element in LRU list */ ZonefileFrame *pLruPrev; /* Previous element in LRU list */ ZonefileFrame *pHashNext; /* Next element in same hash bucket */ }; typedef struct ZonefileTab ZonefileTab; struct ZonefileTab { sqlite3_vtab base; /* Base class - must be first */ sqlite3 *db; sqlite3_stmt *pIdToName; /* Translate fileid to filename */ ZonefileGlobal *pGlobal; char *zName; /* Name of this table */ char *zDb; /* Name of db containing this table */ /* The following variables are used to implement the frame-cache */ int nCacheSize; /* Configured cache size */ int nCacheEntry; /* Current number of entries in cache */ int nCacheBucket; /* Number of buckets in frame cache hash table */ ZonefileFrame **aCache; /* Array of hash buckets */ ZonefileFrame *pCacheFirst;/* Most recently used frame */ ZonefileFrame *pCacheLast; /* Least recently used frame (first to discard) */ }; typedef struct ZonefileCsr ZonefileCsr; struct ZonefileCsr { sqlite3_vtab_cursor base; /* Base class - must be first */ sqlite3_stmt *pSelect; /* SELECT on %_shadow_idx table */ }; /* ** Attempt to interpret the contents of string z as an integer. If ** successful, set (*piVal) to the integer value and return SQLITE_OK. ** Otherwise, return SQLITE_ERROR. */ static int zonefileParseInteger(const char *z, int *piVal){ *piVal = atoi(z); return SQLITE_OK; } /* ** Return true character i is considered to be whitespace. */ static int zonefile_isspace(char i){ return (i==' '); } /* ** This function is called as part of constructing zonefile virtual table ** pTab. Argument zOption is the full text of a parameter (column name) ** specified as part of the CREATE VIRTUAL TABLE statement. This function ** attempts to interpret the parameter and update structure pTab ** accordingly. If successful, SQLITE_OK is returned. Otherwise, an ** SQLite error code is returned and (*pzErr) is left pointing to ** a buffer containing an English language error message. It is the ** responsibility of the caller to eventually free this buffer using ** sqlite3_free(). */ static int zonefileParseOption( ZonefileTab *pTab, /* Zonefile vtab under construction */ const char *zOption, /* Text of option (column name) */ char **pzErr /* OUT: Error message */ ){ const char *z = zOption; const char *zOpt = z; int nOpt; const char *zVal; int rc = SQLITE_OK; /* Skip until EOF, whitespace or "=" */ assert( 0==zonefile_isspace(*z) ); while( *z && !zonefile_isspace(*z) && *z!='=' ) z++; nOpt = z-zOpt; /* Skip whitespace. Then check there is an "=". */ while( zonefile_isspace(*z) ) z++; if( *z!='=' ) goto parse_error; z++; while( zonefile_isspace(*z) ) z++; zVal = z; if( nOpt==9 && sqlite3_strnicmp(zOpt, "cachesize", 9)==0 ){ rc = zonefileParseInteger(zVal, &pTab->nCacheSize); }else{ goto parse_error; } return rc; parse_error: *pzErr = sqlite3_mprintf("parse error in option: %s", zOption); return SQLITE_ERROR; } /* ** This function does the work of xCreate (if bCreate!=0) or xConnect ** (if bCreate==0) for the zonefile module. ** ** argv[0] -> module name ("zonefile") ** argv[1] -> database name ** argv[2] -> table name */ static int zonefileCreateConnect( int bCreate, void *pAux, sqlite3 *db, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ ZonefileTab *p; const char *zName = argv[2]; const char *zDb = argv[1]; int nName = strlen(zName); int nDb = strlen(zDb); int rc = SQLITE_OK; p = (ZonefileTab*)sqlite3_malloc(sizeof(ZonefileTab) + nName+1 + nDb+1); if( !p ){ rc = SQLITE_NOMEM; }else{ int i; memset(p, 0, sizeof(ZonefileTab)); p->zName = (char*)&p[1]; memcpy(p->zName, zName, nName+1); p->zDb = &p->zName[nName+1]; memcpy(p->zDb, zDb, nDb+1); p->db = db; p->pGlobal = (ZonefileGlobal*)pAux; if( bCreate ){ char *zSql = sqlite3_mprintf( "CREATE TABLE %Q.'%q_shadow_idx'(" " k INTEGER PRIMARY KEY," " fileid INTEGER," " fofst INTEGER," " fsz INTEGER," " ofst INTEGER," " sz INTEGER" ");" "CREATE TABLE %Q.'%q_shadow_file'(" " filename TEXT," " fileid INTEGER PRIMARY KEY" ");" "CREATE VIRTUAL TABLE %Q.'%q_files' USING zonefile_files;", zDb, zName, zDb, zName, zDb, zName ); if( zSql==0 ){ rc = SQLITE_NOMEM; }else{ rc = sqlite3_exec(db, zSql, 0, 0, pzErr); sqlite3_free(zSql); } } if( rc==SQLITE_OK ){ rc = sqlite3_declare_vtab(db, ZONEFILE_SCHEMA); } for(i=3; i<argc && rc==SQLITE_OK; i++){ rc = zonefileParseOption(p, argv[i], pzErr); } if( rc==SQLITE_OK && p->nCacheSize<1 ) p->nCacheSize = 1; } if( rc!=SQLITE_OK ){ sqlite3_free(p); p = 0; } *ppVtab = (sqlite3_vtab*)p; return rc; } /* ** zonefile virtual table module xCreate method. */ static int zonefileCreate( sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ return zonefileCreateConnect(1, pAux, db, argc, argv, ppVtab, pzErr); } /* ** zonefile virtual table module xConnect method. */ static int zonefileConnect( sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVtab, char **pzErr ){ return zonefileCreateConnect(0, pAux, db, argc, argv, ppVtab, pzErr); } /* ** Zonefile virtual table module xBestIndex method. ** ** Equality and range constraints on either the rowid or column "k" (which ** are the same thing) are processed. Bits in the idxNum parameter are ** set to indicate the constraints present: ** ** 0x01: k == ? ** 0x02: k < ? ** 0x04: k <= ? ** 0x08: k > ? ** 0x10: k >= ? ** ** Only some combinations are valid: ** ** * If an == constraint is found, no other bits are set. ** * If a < constraint is present, any <= is ignored. ** * If a > constraint is present, any >= is ignored. */ static int zonefileBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pInfo){ int iEq = -1; int iLt = -1; int iLe = -1; int iGt = -1; int iGe = -1; int i; int idxNum = 0; double cost = 1000000000.0; for(i=0; i<pInfo->nConstraint; i++){ struct sqlite3_index_constraint *p = &pInfo->aConstraint[i]; if( p->usable && p->iColumn<=0 ){ switch( p->op ){ case SQLITE_INDEX_CONSTRAINT_EQ: iEq = i; break; case SQLITE_INDEX_CONSTRAINT_LT: iLt = i; break; case SQLITE_INDEX_CONSTRAINT_LE: iLe = i; break; case SQLITE_INDEX_CONSTRAINT_GT: iGt = i; break; case SQLITE_INDEX_CONSTRAINT_GE: iGe = i; break; } } } if( iEq>=0 ){ cost = 10.0; idxNum = 0x01; pInfo->aConstraintUsage[iEq].argvIndex = 1; }else{ int iIdx = 1; if( iLt>=0 ){ pInfo->aConstraintUsage[iLt].argvIndex = iIdx++; idxNum |= 0x02; }else if( iLe>=0 ){ pInfo->aConstraintUsage[iLe].argvIndex = iIdx++; idxNum |= 0x04; } if( iGt>=0 ){ pInfo->aConstraintUsage[iGt].argvIndex = iIdx++; idxNum |= 0x08; }else if( iGe>=0 ){ pInfo->aConstraintUsage[iGe].argvIndex = iIdx++; idxNum |= 0x10; } if( iIdx==2 ) cost = 10000.0; if( iIdx==3 ) cost = 100.0; } pInfo->idxNum = idxNum; pInfo->estimatedCost = cost; return SQLITE_OK; } /* ** zonefile virtual table module xOpen method. */ static int zonefileOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ ZonefileCsr *pCsr; pCsr = (ZonefileCsr*)sqlite3_malloc(sizeof(ZonefileCsr)); if( pCsr==0 ){ return SQLITE_NOMEM; } memset(pCsr, 0, sizeof(ZonefileCsr)); *ppCursor = (sqlite3_vtab_cursor*)pCsr; return SQLITE_OK; } /* ** Reset a ZonefileCsr object to the state it is in immediately after ** it is allocated by zffOpen(). */ static void zonefileCursorReset(ZonefileCsr *pCsr){ sqlite3_finalize(pCsr->pSelect); pCsr->pSelect = 0; } /* ** zonefile virtual table module xClose method. */ static int zonefileClose(sqlite3_vtab_cursor *cur){ ZonefileCsr *pCsr = (ZonefileCsr*)cur; zonefileCursorReset(pCsr); sqlite3_free(pCsr); return SQLITE_OK; } /* ** zonefile virtual table module xNext method. */ static int zonefileNext(sqlite3_vtab_cursor *cur){ ZonefileCsr *pCsr = (ZonefileCsr*)cur; int rc = SQLITE_OK; if( SQLITE_ROW!=sqlite3_step(pCsr->pSelect) ){ rc = sqlite3_finalize(pCsr->pSelect); if( rc!=SQLITE_OK ){ ZonefileTab *pTab = (ZonefileTab*)pCsr->base.pVtab; pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); } pCsr->pSelect = 0; } return rc; } /* ** zonefile virtual table module xFilter method. */ static int zonefileFilter( sqlite3_vtab_cursor *cur, int idxNum, const char *idxStr, int argc, sqlite3_value **argv ){ ZonefileCsr *pCsr = (ZonefileCsr*)cur; ZonefileTab *pTab = (ZonefileTab*)pCsr->base.pVtab; int rc; const char *z1 = 0; const char *z2 = 0; if( idxNum & 0x01 ){ z1 = "k = ?"; }else{ if( idxNum & 0x02 ) { z1 = "k < ?"; } if( idxNum & 0x04 ) { z1 = "k <= ?"; } if( idxNum & 0x08 ) { if( z1 ) z2 = "k > ?"; else z1 = "k > ?"; } if( idxNum & 0x10 ) { if( z1 ) z2 = "k >= ?"; else z1 = "k >= ?"; } } rc = zonefilePrepare(pTab->db, &pCsr->pSelect, &pTab->base.zErrMsg, "SELECT k, fileid, fofst, fsz, ofst, sz FROM %Q.'%q_shadow_idx'%s%s%s%s", pTab->zDb, pTab->zName, z1 ? " WHERE " : "", z1, z2 ? " AND " : "", z2 ); if( z1 ) sqlite3_bind_value(pCsr->pSelect, 1, argv[0]); if( z2 ) sqlite3_bind_value(pCsr->pSelect, 2, argv[1]); if( rc==SQLITE_OK ){ rc = zonefileNext(cur); } return rc; } /* ** zonefile virtual table module xEof method. */ static int zonefileEof(sqlite3_vtab_cursor *cur){ ZonefileCsr *pCsr = (ZonefileCsr*)cur; return (pCsr->pSelect==0); } static void zonefileFree(void *p){ sqlite3_free(p); } static int zonefileGetFile( sqlite3_context *pCtx, /* Leave error message here */ ZonefileCsr *pCsr, /* Cursor object */ const char **pzFile /* OUT: Pointer to current file */ ){ ZonefileTab *pTab = (ZonefileTab*)pCsr->base.pVtab; int rc = SQLITE_OK; i64 iFileid; if( pTab->pIdToName==0 ){ rc = zonefilePrepare(pTab->db, &pTab->pIdToName, &pTab->base.zErrMsg, "SELECT filename FROM %Q.'%q_shadow_file' WHERE fileid=?", pTab->zDb, pTab->zName ); if( rc==SQLITE_ERROR ) zonefileTransferError(pCtx); } if( rc==SQLITE_OK ){ iFileid = sqlite3_column_int64(pCsr->pSelect,1); sqlite3_bind_int64(pTab->pIdToName, 1, iFileid); if( SQLITE_ROW==sqlite3_step(pTab->pIdToName) ){ *pzFile = (const char*)sqlite3_column_text(pTab->pIdToName, 0); }else{ rc = sqlite3_reset(pTab->pIdToName); if( rc==SQLITE_OK ){ rc = SQLITE_CORRUPT_VTAB; }else{ zonefileTransferError(pCtx); } } } return rc; } static void zonefileReleaseFile(ZonefileCsr *pCsr){ ZonefileTab *pTab = (ZonefileTab*)pCsr->base.pVtab; sqlite3_reset(pTab->pIdToName); } static int zonefileValueReadDirect(sqlite3_context *pCtx, ZonefileCsr *pCsr){ i64 iOff = sqlite3_column_int64(pCsr->pSelect, 2); int sz = sqlite3_column_int(pCsr->pSelect, 3); FILE *pFd = 0; /* File handle open on zonefile */ u8 *aBuf; /* Buffer to read blob into */ int rc; /* Return code */ aBuf = sqlite3_malloc(sz); if( aBuf==0 ){ rc = SQLITE_NOMEM; }else{ const char *zFile = 0; /* Open the file to read the blob from */ rc = zonefileGetFile(pCtx, pCsr, &zFile); if( rc==SQLITE_OK ){ char *zErr = 0; pFd = zonefileFileOpen(zFile, 0, &zErr); if( pFd==0 ){ sqlite3_result_error(pCtx, zErr, -1); sqlite3_free(zErr); rc = SQLITE_ERROR; } zonefileReleaseFile(pCsr); } } if( rc==SQLITE_OK ){ rc = zonefileFileRead(pFd, aBuf, sz, iOff); if( rc==SQLITE_OK ){ sqlite3_result_blob(pCtx, aBuf, sz, zonefileFree); aBuf = 0; } } zonefileFileClose(pFd); sqlite3_free(aBuf); return rc; } #ifndef NDEBUG static void zonefileCacheCheck(ZonefileTab *pTab){ ZonefileFrame *p; int n = 0; for(p=pTab->pCacheFirst; p; p=p->pLruNext){ assert( p!=pTab->pCacheFirst || p->pLruPrev==0 ); assert( p!=pTab->pCacheLast || p->pLruNext==0 ); assert( p==pTab->pCacheFirst || p->pLruPrev->pLruNext==p ); assert( p==pTab->pCacheLast || p->pLruNext->pLruPrev==p ); n++; } assert( n==pTab->nCacheEntry ); } #else # define zonefileCacheCheck(x) #endif /* ** Search the frame-cache belonging to virtual table pTab for an entry ** corresponding to the frame at byte offset iFrameOff of the file with ** file id iFile. If such an entry is found, return a pointer to it. ** Otherwise, if no such entry exists, return NULL. */ static ZonefileFrame *zonefileCacheFind( ZonefileTab *pTab, i64 iFile, i64 iFrameOff ){ ZonefileFrame *pFrame; for(pFrame=pTab->pCacheFirst; pFrame; pFrame=pFrame->pLruNext){ if( pFrame->iFileid==iFile && pFrame->iFrameOff==iFrameOff ){ /* Found a match. Move it to the front of the LRU list and return ** a pointer to it. */ assert( (pFrame->pLruPrev==0)==(pFrame==pTab->pCacheFirst) ); assert( (pFrame->pLruNext==0)==(pFrame==pTab->pCacheLast) ); if( pFrame->pLruPrev ){ assert( pFrame==pFrame->pLruPrev->pLruNext ); pFrame->pLruPrev->pLruNext = pFrame->pLruNext; if( pFrame->pLruNext ){ assert( pFrame==pFrame->pLruNext->pLruPrev ); pFrame->pLruNext->pLruPrev = pFrame->pLruPrev; }else{ assert( pFrame==pTab->pCacheLast ); pTab->pCacheLast = pFrame->pLruPrev; pTab->pCacheLast->pLruNext = 0; } pFrame->pLruPrev = 0; pFrame->pLruNext = pTab->pCacheFirst; pTab->pCacheFirst->pLruPrev = pFrame; pTab->pCacheFirst = pFrame; } zonefileCacheCheck(pTab); return pFrame; } } return 0; } /* ** Return the index of the hash bucket that iFileid/iFrameOff belongs to. */ int zonefileCacheHash(ZonefileTab *pTab, i64 iFileid, i64 iFrameOff){ u32 h; h = (iFileid & 0xFFFFFFFF) ^ (iFrameOff & 0xFFFFFFFF); return (h % pTab->nCacheBucket); } /* ** Store the frame object passed as the second argument in the frame ** cache belonging to table pTab. */ static int zonefileCacheStore(ZonefileTab *pTab, ZonefileFrame *pFrame){ int h; /* Allocate the hash table if it has not already been allocated. */ if( pTab->aCache==0 ){ int nByte = pTab->nCacheSize * 2 * sizeof(ZonefileFrame*); pTab->aCache = (ZonefileFrame**)sqlite3_malloc(nByte); if( pTab->aCache==0 ){ sqlite3_free(pFrame); return SQLITE_NOMEM; } memset(pTab->aCache, 0, nByte); pTab->nCacheBucket = pTab->nCacheSize * 2; } /* Add the new entry to the hash table */ h = zonefileCacheHash(pTab, pFrame->iFileid, pFrame->iFrameOff); assert( h>=0 && h<pTab->nCacheBucket ); pFrame->pHashNext = pTab->aCache[h]; pTab->aCache[h] = pFrame; /* Add the new entry to the LRU list */ pFrame->pLruPrev = 0; pFrame->pLruNext = pTab->pCacheFirst; pTab->pCacheFirst = pFrame; if( pFrame->pLruNext==0 ){ pTab->pCacheLast = pFrame; }else{ pFrame->pLruNext->pLruPrev = pFrame; } pTab->nCacheEntry++; if( pTab->nCacheEntry>pTab->nCacheSize ){ ZonefileFrame **pp; /* Remove the oldest entry from the LRU list. */ ZonefileFrame *pLast = pTab->pCacheLast; assert( pTab->pCacheLast ); assert( pTab->nCacheEntry>1 ); pTab->pCacheLast = pLast->pLruPrev; pTab->pCacheLast->pLruNext = 0; pTab->nCacheEntry--; /* Remove the same entry from the hash table. */ h = zonefileCacheHash(pTab, pLast->iFileid, pLast->iFrameOff); assert( h>=0 && h<pTab->nCacheBucket ); for(pp=&pTab->aCache[h]; *pp!=pLast; pp=&((*pp)->pHashNext)); *pp = pLast->pHashNext; sqlite3_free(pLast); } zonefileCacheCheck(pTab); return SQLITE_OK; } /* ** Delete all resources associated with the frame-cache for table pTab. */ static void zonefileCacheDelete(ZonefileTab *pTab){ ZonefileFrame *p; ZonefileFrame *pNext; for(p=pTab->pCacheFirst; p; p=pNext){ pNext = p->pLruNext; sqlite3_free(p); } sqlite3_free(pTab->aCache); } static int zonefileValueReadCache(sqlite3_context *pCtx, ZonefileCsr *pCsr){ int rc = SQLITE_OK; ZonefileTab *pTab = (ZonefileTab*)pCsr->base.pVtab; ZonefileFrame *pFrame = 0; i64 iFile = sqlite3_column_int64(pCsr->pSelect, 1); i64 iFrameOff = sqlite3_column_int64(pCsr->pSelect, 2); i64 iKeyOff = sqlite3_column_int64(pCsr->pSelect, 4); int nKeySz = sqlite3_column_int(pCsr->pSelect, 5); /* Check if this frame is already in the cache. If not, read it from ** the file. */ pFrame = zonefileCacheFind(pTab, iFile, iFrameOff); if( pFrame==0 ){ const char *zFile = 0; char *zErr = 0; FILE *pFd = 0; ZonefileHeader hdr = { 0 }; ZonefileCompress *pCmpMethod = 0; ZonefileCodec *pCodec = 0; void *pCmp = 0; /* Open the file to read the blob from */ rc = zonefileGetFile(pCtx, pCsr, &zFile); if( rc==SQLITE_OK ){ pFd = zonefileFileOpen(zFile, 0, &zErr); if( pFd==0 ) rc = SQLITE_ERROR; } /* Read the zonefile header */ if( rc==SQLITE_OK ){ rc = zonefileReadHeader(pFd, zFile, &hdr, &zErr); } /* Find the compression method and open the compressor handle. */ if( rc==SQLITE_OK ){ rc = zonefileFindCompress(hdr.compressionTypeContent, &pCmpMethod, &zErr); } if( pCmpMethod ){ int nDict = 0; u8 *aDict = 0; assert( rc==SQLITE_OK ); if( hdr.byteOffsetDictionary ){ nDict = hdr.byteOffsetFrames - hdr.byteOffsetDictionary; aDict = sqlite3_malloc(nDict); if( aDict==0 ){ rc = SQLITE_NOMEM; }else{ rc = zonefileFileRead(pFd, aDict, nDict, hdr.byteOffsetDictionary); } } if( rc==SQLITE_OK ){ rc = pCmpMethod->xOpen(&pCmp, aDict, nDict); } sqlite3_free(aDict); } /* Find the encryption method and key. */ if( rc==SQLITE_OK && hdr.encryptionType ){ const u8 *a = 0; int n = zonefileKeyFind(pTab->pGlobal, pTab->zDb, pTab->zName, iFile, &a); if( n==0 ){ zErr = sqlite3_mprintf("missing encryption key for file \"%s\"", zFile); rc = SQLITE_ERROR; }else{ rc = zonefileCodecCreate(hdr.encryptionType, 0, (u8*)a,n,&pCodec,&zErr); } } /* Read some data into memory. */ if( rc==SQLITE_OK ){ int szFrame = sqlite3_column_int(pCsr->pSelect, 3); pFrame = (ZonefileFrame*)sqlite3_malloc(szFrame + sizeof(ZonefileFrame)); if( pFrame==0 ){ rc = SQLITE_NOMEM; }else{ memset(pFrame, 0, sizeof(ZonefileFrame)); pFrame->aBuf = (u8*)&pFrame[1]; pFrame->nBuf = szFrame; pFrame->iFrameOff = iFrameOff; pFrame->iFileid = iFile; rc = zonefileFileRead(pFd, pFrame->aBuf, szFrame, iFrameOff); } } /* Decrypt data if necessary */ if( rc==SQLITE_OK && pCodec ){ zonefileCodecDecode(pCodec, pFrame->aBuf, pFrame->nBuf); pFrame->nBuf -= zonefileCodecNonceSize(pCodec); } /* Uncompress data if required */ if( rc==SQLITE_OK && pCmpMethod ){ ZonefileFrame *p = 0; int nOut = pCmpMethod->xUncompressSize(pCmp, pFrame->aBuf, pFrame->nBuf); p = (ZonefileFrame*)sqlite3_malloc(nOut + sizeof(ZonefileFrame)); if( p==0 ){ rc = SQLITE_NOMEM; }else{ memset(p, 0, sizeof(ZonefileFrame)); p->aBuf = (u8*)&p[1]; p->nBuf = nOut; p->iFrameOff = iFrameOff; p->iFileid = iFile; rc = pCmpMethod->xUncompress( pCmp, p->aBuf, p->nBuf, pFrame->aBuf, pFrame->nBuf ); sqlite3_free(pFrame); pFrame = p; } } if( rc!=SQLITE_OK ){ sqlite3_free(pFrame); pFrame = 0; }else{ rc = zonefileCacheStore(pTab, pFrame); if( rc!=SQLITE_OK ) pFrame = 0; } zonefileReleaseFile(pCsr); zonefileFileClose(pFd); zonefileCodecDestroy(pCodec); if( pCmpMethod ) pCmpMethod->xClose(pCmp); if( zErr ){ assert( rc!=SQLITE_OK ); sqlite3_result_error(pCtx, zErr, -1); sqlite3_free(zErr); } } if( pFrame ){ assert( rc==SQLITE_OK ); sqlite3_result_blob(pCtx, &pFrame->aBuf[iKeyOff], nKeySz, SQLITE_TRANSIENT); } return rc; } /* ** zonefile virtual table module xColumn method. */ static int zonefileColumn( sqlite3_vtab_cursor *cur, sqlite3_context *pCtx, int i ){ ZonefileCsr *pCsr = (ZonefileCsr*)cur; int rc = SQLITE_OK; switch( i ){ case 0: /* k */ sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pSelect, 0)); break; case 1: /* v */ if( sqlite3_column_type(pCsr->pSelect, 5)==SQLITE_NULL ){ rc = zonefileValueReadDirect(pCtx, pCsr); }else{ rc = zonefileValueReadCache(pCtx, pCsr); } break; case 2: /* fileid */ sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pSelect, 1)); break; default: { /* sz */ int iCol; if( sqlite3_column_type(pCsr->pSelect, 5)==SQLITE_NULL ){ iCol = 3; }else{ iCol = 5; } sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pSelect, iCol)); break; } } return rc; } /* ** zonefile virtual table module xDisconnect method. */ static int zonefileDisconnect(sqlite3_vtab *pVtab){ ZonefileTab *pTab = (ZonefileTab*)pVtab; zonefileCacheDelete(pTab); sqlite3_finalize(pTab->pIdToName); sqlite3_free(pTab); return SQLITE_OK; } /* ** zonefile virtual table module xDestroy method. */ static int zonefileDestroy(sqlite3_vtab *pVtab){ ZonefileTab *pTab = (ZonefileTab*)pVtab; int rc = SQLITE_OK; char *zSql = sqlite3_mprintf( "DROP TABLE IF EXISTS %Q.'%q_shadow_idx';" "DROP TABLE IF EXISTS %Q.'%q_shadow_file';" "DROP TABLE IF EXISTS %Q.'%q_shadow_frame';" "DROP TABLE IF EXISTS %Q.'%q_files';", pTab->zDb, pTab->zName, pTab->zDb, pTab->zName, pTab->zDb, pTab->zName, pTab->zDb, pTab->zName ); if( zSql==0 ){ rc = SQLITE_NOMEM; }else{ rc = sqlite3_exec(pTab->db, zSql, 0, 0, 0); sqlite3_free(zSql); } if( rc==SQLITE_OK ){ zonefileDisconnect(pVtab); } return rc; } /* ** zonefile virtual table module xRowid method. */ static int zonefileRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ ZonefileCsr *pCsr = (ZonefileCsr*)cur; *pRowid = sqlite3_column_int64(pCsr->pSelect, 0); return SQLITE_OK; } /* ** Register the "zonefile" extensions. */ static int zonefileRegister(sqlite3 *db){ static sqlite3_module filesModule = { 0, /* iVersion */ zffCreate, /* xCreate - create a table */ zffConnect, /* xConnect - connect to an existing table */ zffBestIndex, /* xBestIndex - Determine search strategy */ zffDisconnect, /* xDisconnect - Disconnect from a table */ zffDisconnect, /* xDestroy - Drop a table */ zffOpen, /* xOpen - open a cursor */ zffClose, /* xClose - close a cursor */ zffFilter, /* xFilter - configure scan constraints */ zffNext, /* xNext - advance a cursor */ zffEof, /* xEof */ zffColumn, /* xColumn - read data */ zffRowid, /* xRowid - read data */ zffUpdate, /* xUpdate - write data */ 0, /* xBegin - begin transaction */ 0, /* xSync - sync transaction */ 0, /* xCommit - commit transaction */ 0, /* xRollback - rollback transaction */ 0, /* xFindFunction - function overloading */ 0, /* xRename - rename the table */ 0, /* xSavepoint */ 0, /* xRelease */ 0 /* xRollbackTo */ }; static sqlite3_module zonefileModule = { 0, /* iVersion */ zonefileCreate, /* xCreate - create a table */ zonefileConnect, /* xConnect - connect to an existing table */ zonefileBestIndex, /* xBestIndex - Determine search strategy */ zonefileDisconnect, /* xDisconnect - Disconnect from a table */ zonefileDestroy, /* xDestroy - Drop a table */ zonefileOpen, /* xOpen - open a cursor */ zonefileClose, /* xClose - close a cursor */ zonefileFilter, /* xFilter - configure scan constraints */ zonefileNext, /* xNext - advance a cursor */ zonefileEof, /* xEof */ zonefileColumn, /* xColumn - read data */ zonefileRowid, /* xRowid - read data */ 0, /* xUpdate - write data */ 0, /* xBegin - begin transaction */ 0, /* xSync - sync transaction */ 0, /* xCommit - commit transaction */ 0, /* xRollback - rollback transaction */ 0, /* xFindFunction - function overloading */ 0, /* xRename - rename the table */ 0, /* xSavepoint */ 0, /* xRelease */ 0 /* xRollbackTo */ }; struct Func { const char *z; int n; void (*x)(sqlite3_context*,int,sqlite3_value**); } aFunc[] = { { "zonefile_write", 2, zonefileWriteFunc }, { "zonefile_write", 3, zonefileWriteFunc }, }; ZonefileGlobal *pGlobal = 0; int rc = SQLITE_OK; int i; for(i=0; rc==SQLITE_OK && i<sizeof(aFunc)/sizeof(aFunc[0]); i++){ struct Func *p = &aFunc[i]; rc = sqlite3_create_function(db, p->z, p->n, SQLITE_ANY, 0, p->x, 0, 0); } if( rc==SQLITE_OK ){ pGlobal = (ZonefileGlobal*)sqlite3_malloc(sizeof(ZonefileGlobal)); if( pGlobal==0 ){ rc = SQLITE_NOMEM; }else{ memset(pGlobal, 0, sizeof(ZonefileGlobal)); } } if( rc==SQLITE_OK ){ rc = sqlite3_create_module( db, "zonefile_files", &filesModule, (void*)pGlobal ); } if( rc==SQLITE_OK ){ rc = sqlite3_create_module_v2(db, "zonefile", &zonefileModule, (void*)pGlobal, zonefileKeyDestroy ); pGlobal = 0; } sqlite3_free(pGlobal); return rc; } #else /* SQLITE_OMIT_VIRTUALTABLE */ # define zonefileRegister(x) SQLITE_OK #endif #ifdef _WIN32 __declspec(dllexport) #endif int sqlite3_zonefile_init( sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ){ SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ return zonefileRegister(db); } |
Added ext/zonefile/zonefile1.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | # 2018 Feb 11 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # # The focus of this file is testing the zonefile extension. # if {![info exists testdir]} { set testdir [file join [file dirname [info script]] .. .. test] } source [file join $testdir tester.tcl] set testprefix zonefile1 load_static_extension db zonefile do_execsql_test 1.0 { CREATE TABLE zz(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); INSERT INTO zz VALUES(1, -1, -1, randomblob(100)); INSERT INTO zz VALUES(2, -1, -1, randomblob(100)); INSERT INTO zz VALUES(3, -1, -1, randomblob(100)); } do_execsql_test 1.1 { SELECT zonefile_write('test.zonefile', 'zz'); } {{}} do_execsql_test 1.2 { CREATE VIRTUAL TABLE z1 USING zonefile; SELECT name FROM sqlite_master WHERE name LIKE 'z1%' ORDER BY 1; } {z1 z1_files z1_shadow_file z1_shadow_idx} do_execsql_test 1.3 { INSERT INTO z1_files(filename) VALUES('test.zonefile'); SELECT filename, json_extract(header, '$.magicNumber'), json_extract(header, '$.numFrames'), json_extract(header, '$.numKeys') FROM z1_files; } {test.zonefile 1179332920 1 3} do_execsql_test 1.4 { SELECT count(*) FROM z1_shadow_idx } 3 do_execsql_test 1.5.1 { SELECT k FROM z1 } {1 2 3} do_execsql_test 1.5.2 { SELECT fileid FROM z1 } {1 1 1} do_execsql_test 1.5.4 { SELECT sz FROM z1 } {100 100 100} do_execsql_test 1.5.5 { SELECT zz.v==z1.v FROM zz, z1 WHERE zz.k=z1.k } {1 1 1} do_execsql_test 1.5 { DELETE FROM z1_files; SELECT * FROM z1_files; } {} do_execsql_test 1.6 { SELECT count(*) FROM z1_shadow_idx } 0 do_execsql_test 1.7 { DROP TABLE z1 } do_execsql_test 1.8 { SELECT * FROM sqlite_master WHERE name LIKE 'z1%'; } #------------------------------------------------------------------------- # Figure out which compression algorithms, if any, are supported by # this build. Populate the global list $COMPRESSION_METHODS with the # result. reset_db load_static_extension db zonefile do_execsql_test 2.0 { CREATE TABLE bb( k INTEGER PRIMARY KEY, frame INTEGER DEFAULT -1, idx INTEGER DEFAULT -1, v BLOB ); INSERT INTO bb(k, v) VALUES(1, randomblob(100)); } set COMPRESSION_METHODS [list] foreach cmp { none zlib zstd zstd_global_dict lz4 lz4hc brotli nosuchcmp } { set res [catchsql { WITH p(n,v) AS ( VALUES('compressionTypeContent', $cmp) ) SELECT zonefile_write('test.zonefile', 'bb', json_group_object(n,v)) FROM p; }] if {[lindex $res 0]==0} { lappend COMPRESSION_METHODS $cmp } } # Check that it is not possible to use zstd_global_dict to compress # the zonefile index. # if {[lsearch $COMPRESSION_METHODS zstd_global_dict]>=0} { do_catchsql_test 2.1 { WITH p(n,v) AS ( VALUES('compressionTypeIndexData', 'zstd_global_dict') ) SELECT zonefile_write('test.zonefile', 'bb', json_group_object(n,v)) FROM p; } {1 {compressor "zstd_global_dict" may not be used to compress the zonefile index}} } set extra_header 0 set cachesize 0 foreach cmp $COMPRESSION_METHODS { foreach cmpidx $COMPRESSION_METHODS { if {$cmpidx == "zstd_global_dict"} continue reset_db load_static_extension db zonefile set tn "$cmp/$cmpidx" set extra_header [expr {$extra_header ? 0 : 100}] set cachesize [expr {$cachesize ? 0 : 10}] do_execsql_test 2.$tn.0.1 { CREATE TABLE zz( k INTEGER PRIMARY KEY, frame INTEGER DEFAULT -1, idx INTEGER DEFAULT -1, v BLOB ); CREATE TABLE rt(k INTEGER PRIMARY KEY, v BLOB); } do_execsql_test 2.$tn.0.2 " CREATE VIRTUAL TABLE zone USING zonefile(cachesize = $cachesize) " {} set nMinByte 0 set nMaxByte 444 foreach {zonefile lKey} { test1.zonefile {195 1238 298 405 297} test2.zonefile {124 1624 82 1929} test3.zonefile {932 683 1751 410 41} test4.zonefile {427 1491} test5.zonefile {1004 473 801 394 1672 816 1577} test6.zonefile {1374 1454 1005} test7.zonefile {450 241 319 133} test8.zonefile {1414 900 1406 1917 127 673} test9.zonefile {1192 226 988 1292 718 1345 1675} test10.zonefile {314} test11.zonefile {1177 1597 60 532 291 1164 812} test12.zonefile {1168 1290 1585 939 1916} test13.zonefile {644 1784 1476 1283 433 506} test14.zonefile {1141 1547 1506 364} test15.zonefile {1756 1885 844 1880 1896 354} test16.zonefile {1383 1928 1371} test17.zonefile {93} test18.zonefile {1067} test19.zonefile {642} test20.zonefile {1380 1857} test21.zonefile {288 293 1968 1207 1739 231 300} test22.zonefile {651 1007 607 830 299 1431} test23.zonefile {81 1651 543 1949 256 119 1088} test24.zonefile {1278 2024 682 1115 194 636 1804} test25.zonefile {514 1155 171 2015 791} test26.zonefile {1615 1228 147 1464} test27.zonefile {55 1130 781 678 78} test28.zonefile {1981 1401 1178} test29.zonefile {1754 864 183 1953 1901} test30.zonefile {1461 817} test31.zonefile {1720 1722 686 1833} } { forcedelete $zonefile execsql { DELETE FROM zz; } foreach k $lKey { execsql { INSERT INTO zz(k, v) VALUES($k, randomblob($k)) } } execsql { INSERT INTO rt SELECT k, v FROM zz } execsql { WITH p(n,v) AS ( VALUES('maxAutoFrameSize', 2000) UNION ALL VALUES('compressionTypeIndexData', $cmpidx) UNION ALL VALUES('compressionTypeContent', $cmp) UNION ALL VALUES('debugExtendedHeaderSize', $extra_header) ) SELECT zonefile_write($zonefile, 'zz', json_group_object(n, v)) FROM p; INSERT INTO zone_files(filename) VALUES($zonefile); } } do_execsql_test 2.$tn.1 { SELECT k FROM zone JOIN rt USING (k) WHERE zone.v!=rt.v } do_execsql_test 2.$tn.2 { SELECT count(*) FROM zone JOIN rt USING (k); } {135} do_execsql_test 2.$tn.3 { SELECT filename, json_extract(header, '$.numKeys'), json_extract(header, '$.numFrames') FROM zone_files WHERE filename IN ('test19.zonefile', 'test20.zonefile', 'test21.zonefile') ORDER BY 1 } { test19.zonefile 1 1 test20.zonefile 2 2 test21.zonefile 7 4 } }} #-------------------------------------------------------------------------- # reset_db load_static_extension db zonefile do_execsql_test 3.0 { CREATE TABLE dd(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); INSERT INTO dd VALUES(1000, 1, -1, randomblob(44)); INSERT INTO dd VALUES(1001, 1, -1, randomblob(55)); INSERT INTO dd VALUES(1002, 2, -1, randomblob(66)); WITH p(n,v) AS ( VALUES('maxAutoFrameSize', 2000) UNION ALL VALUES('encryptionType', 'xor') UNION ALL VALUES('debugEncryptionKeyText', 1) UNION ALL VALUES('encryptionKey', '0123456789') ) SELECT zonefile_write('test.zonefile', 'dd', json_group_object(n, v)) FROM p; } {{}} do_execsql_test 3.1 { CREATE VIRTUAL TABLE cc USING zonefile; INSERT INTO cc_files(filename,ekey) VALUES('test.zonefile','0123456789'); SELECT quote(dd.v)==quote(cc.v) FROM cc JOIN dd USING (k) } {1 1 1} do_execsql_test 3.2.1 { DELETE FROM cc_files; INSERT INTO cc_files(filename,ekey) VALUES('test.zonefile','abcdefghij'); SELECT quote(dd.v)==quote(cc.v) FROM cc JOIN dd USING (k) } {0 0 0} do_execsql_test 3.2.2 { SELECT rowid,sz FROM cc; } {1000 44 1001 55 1002 66} do_execsql_test 3.3 { UPDATE cc_files SET ekey = '0123456789'; SELECT quote(dd.v)==quote(cc.v) FROM cc JOIN dd USING (k) } {1 1 1} close [open test.zonefile w+] do_catchsql_test 3.4 { SELECT header FROM cc_files } {1 {failed to read zonefile header from file "test.zonefile"}} forcedelete test.zonefile do_catchsql_test 3.5 { SELECT header FROM cc_files } {1 {failed to open file "test.zonefile" for reading}} do_execsql_test 3.6 { SELECT ekey FROM cc_files } {{}} forcedelete test.zonefile do_catchsql_test 3.7 { SELECT * FROM cc; } {1 {failed to open file "test.zonefile" for reading}} #------------------------------------------------------------------------- # Check that a file that uses an unknown compression method is handled # correctly (an error is returned). # reset_db load_static_extension db zonefile do_execsql_test 4.0 { CREATE TABLE dd(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); INSERT INTO dd VALUES(1000, 1, -1, randomblob(44)); INSERT INTO dd VALUES(1001, 1, -1, randomblob(55)); INSERT INTO dd VALUES(1002, 2, -1, randomblob(66)); SELECT zonefile_write('test.zonefile', 'dd'); CREATE VIRTUAL TABLE x1 USING zonefile; } {{}} do_test 4.1 { hexio_write test.zonefile 5 77 } {1} do_execsql_test 4.2 { INSERT INTO x1_files(filename) VALUES('test.zonefile'); } {} do_catchsql_test 4.3 { SELECT * FROM x1 } {1 {unsupported compression method: 119}} do_test 4.4 { hexio_write test.zonefile 4 77 } {1} do_catchsql_test 4.5 { DELETE FROM x1_files; INSERT INTO x1_files(filename) VALUES('test.zonefile'); } {1 {unsupported compression method: 119}} do_test 4.6 { hexio_write test.zonefile 0 00 } {1} do_catchsql_test 4.7 { INSERT INTO x1_files(filename) VALUES('test.zonefile'); } {1 {failed to read zonefile header from file "test.zonefile"}} #------------------------------------------------------------------------- # Test using various types in the "frame" field of an input table. # reset_db load_static_extension db zonefile if {[lsearch $COMPRESSION_METHODS zlib]>=0} { do_execsql_test 5.0 { CREATE TABLE "a b"(k INTEGER PRIMARY KEY,frame INTEGER,idx INTEGER,v BLOB); INSERT INTO "a b" VALUES(1, 0.5, -1, randomblob(44)); INSERT INTO "a b" VALUES(2, 0.5, -1, randomblob(55)); INSERT INTO "a b" VALUES(3, 1.5, -1, randomblob(55)); INSERT INTO "a b" VALUES(4, 1.5, -1, randomblob(55)); INSERT INTO "a b" VALUES(5, 2, -1, randomblob(55)); INSERT INTO "a b" VALUES(6, 2, -1, randomblob(55)); INSERT INTO "a b" VALUES(7, 200, -1, randomblob(55)); INSERT INTO "a b" VALUES(8, 200, -1, randomblob(55)); INSERT INTO "a b" VALUES(9, 300, -1, randomblob(55)); INSERT INTO "a b" VALUES(10, 300, -1, randomblob(55)); INSERT INTO "a b" VALUES(11, NULL, -1, randomblob(55)); INSERT INTO "a b" VALUES(12, NULL, -1, randomblob(55)); INSERT INTO "a b" VALUES(13, 'f1', -1, randomblob(55)); INSERT INTO "a b" VALUES(14, 'f1', -1, randomblob(55)); INSERT INTO "a b" VALUES(15, 'frame2', -1, randomblob(55)); INSERT INTO "a b" VALUES(16, 'frame2', -1, randomblob(55)); INSERT INTO "a b" VALUES(17, x'1234', -1, randomblob(55)); INSERT INTO "a b" VALUES(18, x'1234', -1, randomblob(55)); INSERT INTO "a b" VALUES(19, x'abcd', -1, randomblob(55)); INSERT INTO "a b" VALUES(20, x'abcd', -1, randomblob(55)); SELECT zonefile_write('test.zonefile', 'a b', '{"compressionTypeContent":"zlib"}' ); } {{}} do_execsql_test 5.1 { CREATE VIRTUAL TABLE abc USING zonefile; INSERT INTO abc_files(filename) VALUES('test.zonefile'); SELECT group_concat(k) FROM abc_shadow_idx GROUP BY fofst } { 11,12 1,2 3,4 5,6 7,8 9,10 13,14 15,16 17,18 19,20 } } do_execsql_test 6.0 { CREATE TABLE "ab"(k INTEGER PRIMARY KEY,frame INTEGER,idx INTEGER,v BLOB); INSERT INTO "ab" VALUES(1, 0.5, -1, randomblob(44)); INSERT INTO "ab" VALUES(2, 0.5, -1, randomblob(55)); INSERT INTO "ab" VALUES(3, 1.5, -1, randomblob(55)); INSERT INTO "ab" VALUES(4, 1.5, -1, randomblob(55)); } do_catchsql_test 6.1.1 { SELECT zonefile_write('test.zonefile', 'ab', '{"debugExtendedHeaderSize":-1}' ); } {1 {debugExtendedHeaderSize value out of range: -1}} do_catchsql_test 6.1.2 { SELECT zonefile_write('test.zonefile', 'ab', '{"debugExtendedHeaderSize":256}' ); } {1 {debugExtendedHeaderSize value out of range: 256}} do_catchsql_test 6.2 { SELECT zonefile_write('test.zonefile', 'ab', '{"unknown":256}' ); } {1 {unknown parameter name: "unknown"}} forcedelete test.dir file mkdir test.dir do_catchsql_test 6.3 { SELECT zonefile_write('test.dir', 'ab'); } {1 {failed to open file "test.dir" for writing}} do_catchsql_test 6.4 { CREATE VIRTUAL TABLE zzz USING zonefile; INSERT INTO zzz_files(filename) VALUES('nosuchfile.zonefile'); } {1 {failed to open file "nosuchfile.zonefile" for reading}} if {$tcl_platform(platform)=="windows"} { do_catchsql_test 6.5 { INSERT INTO zzz_files(filename) VALUES('test.dir'); } {1 {failed to open file "test.dir" for reading}} } else { do_catchsql_test 6.5 { INSERT INTO zzz_files(filename) VALUES('test.dir'); } {1 {failed to read zonefile header from file "test.dir"}} } #------------------------------------------------------------------------- # Check that errors generated when building a dictionary are handled. # The zstd library routines for building a dictionary throw an error # if they are provided with too little data. # # Also test that zstd_global_dict cannot be used to compress the zonefile # index (as there is nowhere in the file format to store the dictionary # for this compression). # reset_db load_static_extension db zonefile if {[lsearch $COMPRESSION_METHODS zstd_global_dict]>=0} { do_execsql_test 7.0 { CREATE TABLE "ab"(k INTEGER PRIMARY KEY,frame INTEGER,idx INTEGER,v BLOB); INSERT INTO "ab" VALUES(1, -1, -1, 'abc'); } do_catchsql_test 7.1 { SELECT zonefile_write('test.zonefile', 'ab', '{"compressionTypeContent":"zstd_global_dict"}' ); } {1 {error generating dictionary}} do_catchsql_test 7.2 { SELECT zonefile_write('test.zonefile', 'ab', '{"compressionTypeIndexData":"zstd_global_dict"}' ); } {1 {compressor "zstd_global_dict" may not be used to compress the zonefile index}} } #------------------------------------------------------------------------- # reset_db load_static_extension db zonefile do_catchsql_test 8.1 { CREATE VIRTUAL TABLE one USING zonefile_files; } {1 {do not create zonefile_files tables directly!}} do_catchsql_test 8.2 { CREATE VIRTUAL TABLE onetwothree USING zonefile_files; } {1 {do not create zonefile_files tables directly!}} #------------------------------------------------------------------------- # A zone file containing zero keys. # reset_db load_static_extension db zonefile do_execsql_test 9.0 { CREATE TABLE s(k INTEGER PRIMARY KEY,frame INTEGER,idx INTEGER,v BLOB); SELECT zonefile_write('test.zonefile', 's'); } {{}} do_execsql_test 9.1 { CREATE VIRTUAL TABLE ss USING zonefile; INSERT INTO ss_files(filename) VALUES('test.zonefile'); SELECT * FROM ss; } {} #------------------------------------------------------------------------- # Test UPDATE and DELETE statements on the %_files virtual table. # reset_db load_static_extension db zonefile do_execsql_test 10.0 { CREATE TABLE data(k INTEGER PRIMARY KEY, v BLOB); INSERT INTO data(k, v) VALUES(1, randomblob(100)); INSERT INTO data(k, v) VALUES(2, randomblob(100)); INSERT INTO data(k, v) VALUES(3, randomblob(100)); INSERT INTO data(k, v) VALUES(4, randomblob(100)); INSERT INTO data(k, v) VALUES(5, randomblob(100)); INSERT INTO data(k, v) VALUES(6, randomblob(100)); INSERT INTO data(k, v) VALUES(7, randomblob(100)); INSERT INTO data(k, v) VALUES(8, randomblob(100)); INSERT INTO data(k, v) VALUES(9, randomblob(100)); CREATE VIEW v1 AS SELECT k, -1 AS frame, -1 AS idx, v FROM data WHERE k IN (1,2,3); SELECT zonefile_write('test1.zonefile', 'v1'); CREATE VIEW v2 AS SELECT k, -1 AS frame, -1 AS idx, v FROM data WHERE k IN (4,5,6); SELECT zonefile_write('test2.zonefile', 'v2'); CREATE VIEW v3 AS SELECT k, -1 AS frame, -1 AS idx, v FROM data WHERE k IN (7,8,9); SELECT zonefile_write('test3.zonefile', 'v3'); } {{} {} {}} do_execsql_test 10.1 { CREATE VIRTUAL TABLE "a b" USING zonefile; INSERT INTO "a b_files"(filename) VALUES('test1.zonefile'); SELECT k FROM "a b" } {1 2 3} do_execsql_test 10.2 { UPDATE "a b_files" SET filename = 'test2.zonefile'; SELECT k FROM "a b" } {4 5 6} do_execsql_test 10.3 { INSERT INTO "a b_files"(filename) VALUES('test1.zonefile'); INSERT INTO "a b_files"(filename) VALUES('test3.zonefile'); SELECT k FROM "a b" } {1 2 3 4 5 6 7 8 9} do_execsql_test 10.4 { DELETE FROM "a b_files" WHERE filename = 'test2.zonefile'; SELECT k FROM "a b" } {1 2 3 7 8 9} do_execsql_test 10.5 { DELETE FROM "a b_files" WHERE filename = 'test1.zonefile'; SELECT k FROM "a b" } {7 8 9} #------------------------------------------------------------------------- # reset_db load_static_extension db zonefile do_execsql_test 11.0 { CREATE TABLE data(k INTEGER PRIMARY KEY, v BLOB); INSERT INTO data(k, v) VALUES(-1, randomblob(100)); INSERT INTO data(k, v) VALUES(1000, randomblob(100)); INSERT INTO data(k, v) VALUES(-1000, randomblob(100)); INSERT INTO data(k, v) VALUES(9223372036854775807, randomblob(100)); INSERT INTO data(k, v) VALUES(-9223372036854775807, randomblob(100)); INSERT INTO data(k, v) VALUES(-9223372036854775808, randomblob(100)); CREATE VIEW v1 AS SELECT k, -1 AS frame, -1 AS idx, v FROM data; SELECT zonefile_write('test1.zonefile', 'v1'); } {{}} do_execsql_test 11.1 { CREATE VIRTUAL TABLE one USING zonefile; INSERT INTO "one_files"(filename) VALUES('test1.zonefile'); SELECT k FROM one } {-9223372036854775808 -9223372036854775807 -1000 -1 1000 9223372036854775807} do_execsql_test 11.2 { SELECT count(*) FROM data JOIN one USING (k) WHERE one.v==data.v } 6 #------------------------------------------------------------------------- # do_catchsql_test 12.1 { CREATE VIRTUAL TABLE nm USING zonefile(abcd) } {1 {parse error in option: abcd}} do_catchsql_test 12.2 { CREATE VIRTUAL TABLE nm USING zonefile(a=b) } {1 {parse error in option: a=b}} do_catchsql_test 12.3 { CREATE VIRTUAL TABLE nm USING zonefile(cachesiza=b) } {1 {parse error in option: cachesiza=b}} #------------------------------------------------------------------------- # reset_db load_static_extension db zonefile do_execsql_test 13.0 { CREATE TABLE data(k INTEGER PRIMARY KEY, v BLOB); WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<250 ) INSERT INTO data SELECT i, randomblob(100) FROM s; CREATE VIEW v1 AS SELECT k, -1 AS frame, -1 AS idx, v FROM data; SELECT zonefile_write('test1.zonefile', 'v1'); CREATE VIRTUAL TABLE nm USING zonefile; INSERT INTO nm_files(filename) VALUES('test1.zonefile'); } {{}} foreach {tn cond} { 1 "k > 30" 2 "k >= 100" 3 "k <= 100" 4 "k < 55" 5 "k LIKE '1%'" 6 "k BETWEEN 10 AND 20" 7 "k > 100 AND k < 200" } { do_execsql_test 13.1.$tn.1 [subst { SELECT count(*) FROM nm WHERE $cond }] [db one "SELECT count(*) FROM data WHERE $cond"] do_execsql_test 13.1.$tn.2 [subst { SELECT count(*) FROM nm WHERE $cond AND v!=(SELECT v FROM data WHERE k=nm.k); }] 0 } close [open test1.zonefile w+] do_catchsql_test 13.2.1 { SELECT * FROM nm WHERE k=24; } {1 {SQL logic error}} forcedelete test1.zonefile do_catchsql_test 13.2.2 { SELECT * FROM nm WHERE k=24; } {1 {failed to open file "test1.zonefile" for reading}} do_catchsql_test 13.3.1 { DELETE FROM nm_shadow_file; SELECT * FROM nm WHERE k=24; } {1 {database disk image is malformed}} do_catchsql_test 13.3.2 { DROP TABLE nm_shadow_file; SELECT * FROM nm WHERE k=24; } {1 {no such table: main.nm_shadow_file}} db close sqlite3 db test.db load_static_extension db zonefile do_catchsql_test 13.3.3 { SELECT * FROM nm WHERE k=24; } {1 {no such table: main.nm_shadow_file}} #------------------------------------------------------------------------- # reset_db load_static_extension db zonefile do_execsql_test 14.0 { CREATE TABLE data(k INTEGER PRIMARY KEY, frame, idx, v BLOB); INSERT INTO data VALUES(1, 1, -1, randomblob(200)); INSERT INTO data VALUES(2, 2, -1, randomblob(200)); INSERT INTO data VALUES(3, 3, -1, randomblob(200)); SELECT zonefile_write('test.zonefile', 'data', '{"encryptionType":"xor","encryptionKey":"pass","debugEncryptionKeyText":1}' ); CREATE VIRTUAL TABLE nm USING zonefile(cachesize=2); INSERT INTO nm_files(filename,ekey) VALUES('test.zonefile','pass'); } {{}} set i 0 foreach id {1 2 3 2 3 1} { do_execsql_test 14.1.$i { SELECT data.v=nm.v FROM data,nm WHERE data.k=$id AND nm.k=$id } 1 incr i } if {[file exists /dev/null]} { do_catchsql_test 14.2 { INSERT INTO nm_files(filename) VALUES('/dev/null'); } {1 {failed to read zonefile header from file "/dev/null"}} } finish_test |
Added ext/zonefile/zonefileenc.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | # 2018 Feb 11 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # # The focus of this file is testing the zonefile extension. # package require Tcl 8.6 if {![info exists testdir]} { set testdir [file join [file dirname [info script]] .. .. test] } source [file join $testdir tester.tcl] set testprefix zonefileenc foreach {tn code} { 1 { set K { braking bramble brambles brambly bran branch branched branches branching branchings brand branded } set textkey 1 } 2 { set K { 5e008542742ce0442e37cbf2512e9492 c91c26e0573ca3464e037568c51126da e90e17489c1aef80ac620c9059271a5a 163338707cbe4c72b18d1058a42c5c78 5c6b1e7c7c9e8e4a8d8fdc30dfc11bea ff1012687828ecaac6c9ca86ea0f895e a203f25eb11d4c6afa841dfcf7cd0be0 b6c71e38ca914c460926ef90db39dba0 b38255d031d026c258a0a41a9a75d46a adccca5e5ffa3a7625144a345713aef0 cd423b38b73e42ce5894405e6d0e08c0 b460ad2e370a0386726d6ea46e7b0bac 503b81de72cb3ef87d9346a850040000 369c290a464a6b88bfd9d1c4755afd42 a8a9343efca528f2bf23a972be49dd66 e366b5226bfe3fd0010fa814aae3b996 4cad7e80124c2cd447131bae377e60f6 4a0fd2f054e1b08cad0de2dc6aa93246 8a23c85e3337da2c97d498f806870fa8 8d14e1f055fd9bec7d07cf0e8baae042 7f6954b0dc373028ab3b030aaf44dd58 d220164c3898435a946de6bcbb478cc4 566af7ea88ba4ff87fd868e858cf98ea a5405832235e8f601516f9c49767bdac 1bd5b4dc6b54e5ca92ba67d20bf65740 59da30e203bf73840e38e108b83ddb82 e516924c2cdf3114f10f2f0e1bdabbc6 b55dd27222a39764222838007e749984 190ae9f81b86a5a024e3b97ee2a7121c 469660843a9a9e507d0fb43e92029296 e6e600bc063aad12f6387beef650c48a 3097be5c3a52a2f00747587add01b550 } set textkey 0 } } { reset_db load_static_extension db zonefile set nFile 100 eval $code do_execsql_test 1.$tn.0 { CREATE TABLE zz(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); CREATE TABLE rr(k INTEGER PRIMARY KEY, v); } do_test 1.$tn.1 { for {set i 0} {$i < $nFile} {incr i} { set k [lindex $K [expr $i % [llength $K]]] execsql { DELETE FROM zz; INSERT INTO zz VALUES($i*10+1, 1, -1, randomblob(100)); INSERT INTO zz VALUES($i*10+2, 2, -1, randomblob(100)); INSERT INTO zz VALUES($i*10+3, 1, -1, randomblob(100)); INSERT INTO rr SELECT k,v FROM zz; WITH p(n,v) AS ( VALUES('encryptionType', 'xor') UNION ALL VALUES('debugEncryptionKeyText', $textkey) UNION ALL VALUES('encryptionKey', $k) ) SELECT zonefile_write('test' || $i || '.zonefile', 'zz', json_group_object(n, v) ) FROM p; } } } {} proc k {i} { set val [lindex $::K [expr $i % [llength $::K]]] if {$::textkey==0} { return [binary decode hex $val] } return $val } db func k k do_execsql_test 1.$tn.2 { CREATE VIRTUAL TABLE gg USING zonefile; } for {set i 0} {$i < $nFile} {incr i} { do_execsql_test 1.$tn.2.$i { INSERT INTO gg_files(filename, ekey) VALUES('test' || $i || '.zonefile', k($i)); SELECT count(*) FROM rr JOIN gg USING(k) WHERE rr.v!=gg.v; } 0 } db close sqlite3 db test.db load_static_extension db zonefile db func k k do_catchsql_test 1.$tn.3 { SELECT count(*) FROM rr JOIN gg USING(k) WHERE rr.v!=gg.v; } {1 {missing encryption key for file "test0.zonefile"}} do_execsql_test 1.$tn.4 { UPDATE gg_files SET ekey = k(0) WHERE filename='test0.zonefile'; } do_execsql_test 1.$tn.4.2 { SELECT count(*) FROM rr JOIN gg USING(k) WHERE rr.v==gg.v AND k IN (1,2,3); } {3} do_catchsql_test 1.5 { SELECT count(*) FROM rr JOIN gg USING(k) WHERE rr.v!=gg.v; } {1 {missing encryption key for file "test1.zonefile"}} do_execsql_test 1.$tn.6 { UPDATE gg_files SET ekey = k(rowid-1); } do_execsql_test 1.$tn.7 { SELECT count(*) FROM rr JOIN gg USING(k) WHERE rr.v!=gg.v; } {0} do_execsql_test 1.$tn.8 { SELECT count(*) FROM rr JOIN gg USING(k) WHERE rr.v==gg.v; } {300} forcedelete test.db2 do_execsql_test 1.$tn.9.1 { ATTACH 'test.db2' AS maing; CREATE VIRTUAL TABLE maing.g USING zonefile; INSERT INTO g_files(filename) SELECT filename FROM gg_files; } do_catchsql_test 1.$tn.9.2 { SELECT count(*) FROM rr JOIN g USING(k) WHERE rr.v!=g.v; } {1 {missing encryption key for file "test0.zonefile"}} do_execsql_test 1.$tn.9.3 { UPDATE g_files SET ekey = k(rowid-1); SELECT count(*) FROM rr JOIN g USING(k) WHERE rr.v==g.v; } {300} do_execsql_test 1.$tn.10 { SELECT count(*) FROM rr JOIN gg USING(k) WHERE rr.v==gg.v; } {300} } #------------------------------------------------------------------------- reset_db load_static_extension db zonefile do_execsql_test 2.0 { CREATE TABLE zz(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); } foreach {tn alg id} { 1 aes_128_ctr 1 2 aes_128_cbc 2 3 AES_256_CTR 3 4 Aes_256_CBC 4 } { do_catchsql_test 2.1.$tn { WITH p(n,v) AS ( VALUES('encryptionType', $alg) UNION ALL VALUES('debugEncryptionKeyText', 1) UNION ALL VALUES('encryptionKey', 'secret') ) SELECT zonefile_write('test' || $i || '.zonefile', 'zz', json_group_object(n, v) ) FROM p; } "1 {unsupported encryption method: $id}" } foreach {tn alg} { 1 nosuchmethod! } { do_catchsql_test 2.2.$tn { WITH p(n,v) AS ( VALUES('encryptionType', $alg) UNION ALL VALUES('debugEncryptionKeyText', 1) UNION ALL VALUES('encryptionKey', 'secret') ) SELECT zonefile_write('test' || $i || '.zonefile', 'zz', json_group_object(n, v) ) FROM p; } "1 {unknown encryption method: $alg}" } #------------------------------------------------------------------------- # Test some hash collisions in the encryption key table. # # This is the same hash function used internally to store keys. # proc hash {zDb zTab iFile} { binary scan $zDb c* A binary scan $zTab c* B set h 0 foreach i $A { set h [expr ($h + ($h << 3) + $i) & 0xFFFFFFFF] } foreach i $B { set h [expr ($h + ($h << 3) + $i) & 0xFFFFFFFF] } return [expr $h ^ $iFile] } do_test 3.0 { set h1 [expr [hash main zone 1] % 512] for {set i 0} {1} {incr i} { set h2 [expr [hash "aux$i" zone 1] % 512] if {$h1==$h2} break } set i } 52 reset_db load_static_extension db zonefile forcedelete test.db2 do_execsql_test 3.1 { CREATE TABLE zz(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); INSERT INTO zz VALUES(222, -1, -1, randomblob(60)); WITH p(n,v) AS ( VALUES('encryptionType', 'xor') UNION ALL VALUES('debugEncryptionKeyText', 1) UNION ALL VALUES('encryptionKey', 'pass') ) SELECT zonefile_write('test1.zonefile', 'zz', json_group_object(n, v) ) FROM p; DELETE FROM zz; INSERT INTO zz VALUES(333, -1, -1, randomblob(80)); WITH p(n,v) AS ( VALUES('encryptionType', 'xor') UNION ALL VALUES('debugEncryptionKeyText', 1) UNION ALL VALUES('encryptionKey', 'pass') ) SELECT zonefile_write('test2.zonefile', 'zz', json_group_object(n, v) ) FROM p; } {{} {}} do_execsql_test 3.2 { ATTACH 'test.db2' AS aux52; CREATE VIRTUAL TABLE main.zone USING zonefile; CREATE VIRTUAL TABLE aux52.zone USING zonefile; INSERT INTO main.zone_files(filename, ekey) VALUES('test1.zonefile', 'pass'); INSERT INTO aux52.zone_files(filename, ekey) VALUES('test2.zonefile', 'pass'); } do_execsql_test 3.3 { SELECT v IS NULL FROM main.zone; SELECT v IS NULL FROM aux52.zone; } {0 0} do_test 3.4 { set h1 [expr [hash main zone 1] % 512] for {set i 0} {1} {incr i} { set h2 [expr [hash "aux$i" zone 2] % 512] if {$h1==$h2} break } set i } 682 forcedelete test.db3 do_execsql_test 3.5 { ATTACH 'test.db3' AS aux682; CREATE VIRTUAL TABLE aux682.zone USING zonefile; INSERT INTO aux682.zone_files(filename, ekey) VALUES('test1.zonefile','pass'); INSERT INTO aux682.zone_files(filename, ekey) VALUES('test2.zonefile','pass'); INSERT INTO main.zone_files(filename, ekey) VALUES('test2.zonefile','pass'); } do_execsql_test 3.6 { SELECT v IS NULL FROM main.zone; SELECT v IS NULL FROM aux682.zone; SELECT v IS NULL FROM main.zone; } {0 0 0 0 0 0} finish_test |
Added ext/zonefile/zonefilefault.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | # 2018 Feb 11 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # # The focus of this file is testing the zonefile extension. # if {![info exists testdir]} { set testdir [file join [file dirname [info script]] .. .. test] } source [file join $testdir tester.tcl] source [file join $testdir malloc_common.tcl] set testprefix zonefilefault load_static_extension db zonefile do_execsql_test 1.0 { CREATE TABLE tt(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); INSERT INTO tt VALUES(1, -1, -1, randomblob(100)); INSERT INTO tt VALUES(2, -1, -1, randomblob(200)); INSERT INTO tt VALUES(3, 1, -1, randomblob(300)); INSERT INTO tt VALUES(4, 2, -1, randomblob(400)); } do_faultsim_test 1.1 -faults oom* -prep { sqlite3 db test.db load_static_extension db zonefile } -body { execsql { SELECT zonefile_write('test.zonefile', 'tt', '{"encryptionType":"xor", "encryptionKey":"secret", "debugEncryptionKeyText":1 }' ); } } -test { faultsim_test_result {0 {{}}} } set sql { SELECT zonefile_write('test.zonefile', 'tt', '{"compressionTypeContent":"zstd_global_dict"}' ); } set HAVE_ZSTD 0 if {[catch { db eval $sql }]==0} { set HAVE_ZSTD 1 } if {$HAVE_ZSTD} { do_faultsim_test 1.2 -faults oom* -prep { sqlite3 db test.db load_static_extension db zonefile } -body { execsql { SELECT zonefile_write('test.zonefile', 'tt', '{"compressionTypeContent":"zstd_global_dict"}' ); } } -test { faultsim_test_result {0 {{}}} } } do_execsql_test 1.3.0 { UPDATE tt SET frame = NULL; } do_faultsim_test 1.3 -faults oom* -prep { sqlite3 db test.db load_static_extension db zonefile } -body { execsql { SELECT zonefile_write('test.zonefile', 'tt'); } } -test { faultsim_test_result {0 {{}}} } do_execsql_test 1.4.0 { INSERT INTO tt VALUES(5, -1, -1, randomblob(100)); INSERT INTO tt VALUES(6, -1, -1, randomblob(100)); INSERT INTO tt VALUES(7, -1, -1, randomblob(100)); INSERT INTO tt VALUES(8, -1, -1, randomblob(100)); INSERT INTO tt VALUES(9, -1, -1, randomblob(100)); CREATE VIRTUAL TABLE ttz USING zonefile; } if {$HAVE_ZSTD} { faultsim_save_and_close do_faultsim_test 1.4 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { execsql { SELECT zonefile_write('test.zonefile', 'tt', '{"compressionTypeIndexData":"zstd"}' ); } } -test { faultsim_test_result {0 {{}}} } faultsim_save_and_close do_faultsim_test 1.5 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { execsql { INSERT INTO ttz_files(filename) VALUES('test.zonefile'); } } -test { faultsim_test_result {0 {}} } } #------------------------------------------------------------------------- # do_execsql_test 2.0 { SELECT zonefile_write('test.zonefile', 'tt', '{"encryptionType":"xor", "encryptionKey":"secret", "debugEncryptionKeyText":1 }' ); CREATE VIRTUAL TABLE zz USING zonefile; } {{}} faultsim_save_and_close do_faultsim_test 2.1 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { execsql { INSERT INTO zz_files(filename, ekey) VALUES('test.zonefile', 'secret') } } -test { faultsim_test_result {0 {}} } faultsim_save_and_close do_faultsim_test 2.2 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { execsql { SELECT json_extract(header, '$.magicNumber') FROM zz_files } } -test { faultsim_test_result {0 1179332920} } do_faultsim_test 2.3 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { execsql { DELETE FROM zz_files } } -test { faultsim_test_result {0 {}} } #------------------------------------------------------------------------- # reset_db faultsim_save_and_close do_faultsim_test 3.1 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { execsql { CREATE VIRTUAL TABLE t1 USING zonefile(cachesize=5) } } -test { faultsim_test_result {0 {}} } faultsim_save_and_close do_faultsim_test 3.2 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { execsql { DROP TABLE t1 } } -test { faultsim_test_result {0 {}} } #------------------------------------------------------------------------- # reset_db load_static_extension db zonefile do_execsql_test 4.0 { CREATE TABLE zz(k INTEGER PRIMARY KEY, frame INTEGER, idx INTEGER, v BLOB); INSERT INTO zz VALUES(1, -1, -1, randomblob(100)); INSERT INTO zz VALUES(2, -1, -1, randomblob(100)); INSERT INTO zz VALUES(3, -1, -1, randomblob(100)); SELECT zonefile_write('test.zonefile', 'zz'); CREATE VIRTUAL TABLE zone USING zonefile; INSERT INTO zone_files(filename) VALUES('test.zonefile'); } {{}} faultsim_save_and_close do_faultsim_test 4.1 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile } -body { execsql { SELECT v IS NULL FROM zone WHERE k = 2 } } -test { faultsim_test_result {0 0} } if {$HAVE_ZSTD} { set params { {"encryptionType":"xor","encryptionKey":"pass", "compressionTypeContent":"zstd_global_dict", "debugEncryptionKeyText":1 } } } else { set params { {"encryptionType":"xor","encryptionKey":"pass", "debugEncryptionKeyText":1 } } } do_execsql_test 4.2 { SELECT zonefile_write('test.zonefile', 'zz', $params); CREATE VIRTUAL TABLE zone2 USING zonefile; INSERT INTO zone2_files(filename,ekey) VALUES('test.zonefile','pass'); } {{}} faultsim_save_and_close do_faultsim_test 4.3 -faults oom* -prep { faultsim_restore_and_reopen load_static_extension db zonefile execsql { UPDATE zone2_files SET ekey='pass' } } -body { execsql { SELECT v IS NULL FROM zone2 WHERE k = 2 } } -test { faultsim_test_result {0 0} } #------------------------------------------------------------------------- reset_db do_faultsim_test 4.4 -faults oom* -prep { faultsim_restore_and_reopen } -body { load_static_extension db zonefile } -test { faultsim_test_result {0 {}} {1 {initialization of zonefile failed: }} } finish_test |
Changes to main.mk.
︙ | ︙ | |||
368 369 370 371 372 373 374 375 376 377 378 379 380 381 | $(TOP)/ext/misc/series.c \ $(TOP)/ext/misc/spellfix.c \ $(TOP)/ext/misc/totype.c \ $(TOP)/ext/misc/unionvtab.c \ $(TOP)/ext/misc/wholenumber.c \ $(TOP)/ext/misc/vfslog.c \ $(TOP)/ext/misc/zipfile.c \ $(TOP)/ext/fts5/fts5_tcl.c \ $(TOP)/ext/fts5/fts5_test_mi.c \ $(TOP)/ext/fts5/fts5_test_tok.c #TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c #TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c | > | 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | $(TOP)/ext/misc/series.c \ $(TOP)/ext/misc/spellfix.c \ $(TOP)/ext/misc/totype.c \ $(TOP)/ext/misc/unionvtab.c \ $(TOP)/ext/misc/wholenumber.c \ $(TOP)/ext/misc/vfslog.c \ $(TOP)/ext/misc/zipfile.c \ $(TOP)/ext/zonefile/zonefile.c \ $(TOP)/ext/fts5/fts5_tcl.c \ $(TOP)/ext/fts5/fts5_test_mi.c \ $(TOP)/ext/fts5/fts5_test_tok.c #TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c #TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c |
︙ | ︙ |
Changes to src/test1.c.
︙ | ︙ | |||
6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 | extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*); #ifdef SQLITE_HAVE_ZLIB extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*); #endif static const struct { const char *zExtName; int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); } aExtension[] = { { "amatch", sqlite3_amatch_init }, { "carray", sqlite3_carray_init }, { "closure", sqlite3_closure_init }, | > | 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 | extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*); extern int sqlite3_unionvtab_init(sqlite3*,char**,const sqlite3_api_routines*); #ifdef SQLITE_HAVE_ZLIB extern int sqlite3_zipfile_init(sqlite3*,char**,const sqlite3_api_routines*); #endif extern int sqlite3_zonefile_init(sqlite3*,char**,const sqlite3_api_routines*); static const struct { const char *zExtName; int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*); } aExtension[] = { { "amatch", sqlite3_amatch_init }, { "carray", sqlite3_carray_init }, { "closure", sqlite3_closure_init }, |
︙ | ︙ | |||
7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 | { "spellfix", sqlite3_spellfix_init }, { "totype", sqlite3_totype_init }, { "unionvtab", sqlite3_unionvtab_init }, { "wholenumber", sqlite3_wholenumber_init }, #ifdef SQLITE_HAVE_ZLIB { "zipfile", sqlite3_zipfile_init }, #endif }; sqlite3 *db; const char *zName; int i, j, rc; char *zErrMsg = 0; if( objc<3 ){ Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); | > | 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 | { "spellfix", sqlite3_spellfix_init }, { "totype", sqlite3_totype_init }, { "unionvtab", sqlite3_unionvtab_init }, { "wholenumber", sqlite3_wholenumber_init }, #ifdef SQLITE_HAVE_ZLIB { "zipfile", sqlite3_zipfile_init }, #endif { "zonefile", sqlite3_zonefile_init }, }; sqlite3 *db; const char *zName; int i, j, rc; char *zErrMsg = 0; if( objc<3 ){ Tcl_WrongNumArgs(interp, 1, objv, "DB NAME ..."); |
︙ | ︙ |