sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / misc / compress.c
| 1 | /* |
| 2 | ** 2014-06-13 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** This SQLite extension implements SQL compression functions |
| 14 | ** compress() and uncompress() using ZLIB. |
| 15 | */ |
| 16 | #include "sqlite3ext.h" |
| 17 | SQLITE_EXTENSION_INIT1 |
| 18 | #include <zlib.h> |
| 19 | |
| 20 | /* |
| 21 | ** Implementation of the "compress(X)" SQL function. The input X is |
| 22 | ** compressed using zLib and the output is returned. |
| 23 | ** |
| 24 | ** The output is a BLOB that begins with a variable-length integer that |
| 25 | ** is the input size in bytes (the size of X before compression). The |
| 26 | ** variable-length integer is implemented as 1 to 5 bytes. There are |
| 27 | ** seven bits per integer stored in the lower seven bits of each byte. |
| 28 | ** More significant bits occur first. The most significant bit (0x80) |
| 29 | ** is a flag to indicate the end of the integer. |
| 30 | ** |
| 31 | ** This function, SQLAR, and ZIP all use the same "deflate" compression |
| 32 | ** algorithm, but each is subtly different: |
| 33 | ** |
| 34 | ** * ZIP uses raw deflate. |
| 35 | ** |
| 36 | ** * SQLAR uses the "zlib format" which is raw deflate with a two-byte |
| 37 | ** algorithm-identification header and a four-byte checksum at the end. |
| 38 | ** |
| 39 | ** * This utility uses the "zlib format" like SQLAR, but adds the variable- |
| 40 | ** length integer uncompressed size value at the beginning. |
| 41 | ** |
| 42 | ** This function might be extended in the future to support compression |
| 43 | ** formats other than deflate, by providing a different algorithm-id |
| 44 | ** mark following the variable-length integer size parameter. |
| 45 | */ |
| 46 | static void compressFunc( |
| 47 | sqlite3_context *context, |
| 48 | int argc, |
| 49 | sqlite3_value **argv |
| 50 | ){ |
| 51 | const unsigned char *pIn; |
| 52 | unsigned char *pOut; |
| 53 | unsigned int nIn; |
| 54 | unsigned long int nOut; |
| 55 | unsigned char x[8]; |
| 56 | int rc; |
| 57 | int i, j; |
| 58 | |
| 59 | pIn = sqlite3_value_blob(argv[0]); |
| 60 | nIn = sqlite3_value_bytes(argv[0]); |
| 61 | nOut = 13 + nIn + (nIn+999)/1000; |
| 62 | pOut = sqlite3_malloc64( nOut+5 ); |
| 63 | if( pOut==0 ){ |
| 64 | sqlite3_result_error_nomem(context); |
| 65 | return; |
| 66 | } |
| 67 | for(i=4; i>=0; i--){ |
| 68 | x[i] = (nIn >> (7*(4-i)))&0x7f; |
| 69 | } |
| 70 | for(i=0; i<4 && x[i]==0; i++){} |
| 71 | for(j=0; i<=4; i++, j++) pOut[j] = x[i]; |
| 72 | pOut[j-1] |= 0x80; |
| 73 | rc = compress(&pOut[j], &nOut, pIn, nIn); |
| 74 | if( rc==Z_OK ){ |
| 75 | sqlite3_result_blob(context, pOut, nOut+j, sqlite3_free); |
| 76 | }else{ |
| 77 | sqlite3_free(pOut); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | ** Implementation of the "uncompress(X)" SQL function. The argument X |
| 83 | ** is a blob which was obtained from compress(Y). The output will be |
| 84 | ** the value Y. |
| 85 | */ |
| 86 | static void uncompressFunc( |
| 87 | sqlite3_context *context, |
| 88 | int argc, |
| 89 | sqlite3_value **argv |
| 90 | ){ |
| 91 | const unsigned char *pIn; |
| 92 | unsigned char *pOut; |
| 93 | unsigned int nIn; |
| 94 | unsigned long int nOut; |
| 95 | int rc; |
| 96 | unsigned int i; |
| 97 | |
| 98 | pIn = sqlite3_value_blob(argv[0]); |
| 99 | nIn = sqlite3_value_bytes(argv[0]); |
| 100 | nOut = 0; |
| 101 | for(i=0; i<nIn && i<5; i++){ |
| 102 | nOut = (nOut<<7) | (pIn[i]&0x7f); |
| 103 | if( (pIn[i]&0x80)!=0 ){ i++; break; } |
| 104 | } |
| 105 | pOut = sqlite3_malloc64( (sqlite3_uint64)nOut+1 ); |
| 106 | if( pOut==0 ){ |
| 107 | sqlite3_result_error_nomem(context); |
| 108 | return; |
| 109 | } |
| 110 | rc = uncompress(pOut, &nOut, &pIn[i], nIn-i); |
| 111 | if( rc==Z_OK ){ |
| 112 | sqlite3_result_blob(context, pOut, nOut, sqlite3_free); |
| 113 | }else{ |
| 114 | sqlite3_free(pOut); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
| 119 | #ifdef _WIN32 |
| 120 | __declspec(dllexport) |
| 121 | #endif |
| 122 | int sqlite3_compress_init( |
| 123 | sqlite3 *db, |
| 124 | char **pzErrMsg, |
| 125 | const sqlite3_api_routines *pApi |
| 126 | ){ |
| 127 | int rc = SQLITE_OK; |
| 128 | SQLITE_EXTENSION_INIT2(pApi); |
| 129 | (void)pzErrMsg; /* Unused parameter */ |
| 130 | rc = sqlite3_create_function(db, "compress", 1, |
| 131 | SQLITE_UTF8 | SQLITE_INNOCUOUS, |
| 132 | 0, compressFunc, 0, 0); |
| 133 | if( rc==SQLITE_OK ){ |
| 134 | rc = sqlite3_create_function(db, "uncompress", 1, |
| 135 | SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, |
| 136 | 0, uncompressFunc, 0, 0); |
| 137 | } |
| 138 | return rc; |
| 139 | } |