Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/misc/compress.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / misc / compress.c` 139 lines ยท 4008 bytes Raw Blame History 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 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