OLD | NEW |
1 /* | 1 /* |
2 ** 2014-06-13 | 2 ** 2014-06-13 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
(...skipping 20 matching lines...) Expand all Loading... |
31 static void compressFunc( | 31 static void compressFunc( |
32 sqlite3_context *context, | 32 sqlite3_context *context, |
33 int argc, | 33 int argc, |
34 sqlite3_value **argv | 34 sqlite3_value **argv |
35 ){ | 35 ){ |
36 const unsigned char *pIn; | 36 const unsigned char *pIn; |
37 unsigned char *pOut; | 37 unsigned char *pOut; |
38 unsigned int nIn; | 38 unsigned int nIn; |
39 unsigned long int nOut; | 39 unsigned long int nOut; |
40 unsigned char x[8]; | 40 unsigned char x[8]; |
| 41 int rc; |
41 int i, j; | 42 int i, j; |
42 | 43 |
43 pIn = sqlite3_value_blob(argv[0]); | 44 pIn = sqlite3_value_blob(argv[0]); |
44 nIn = sqlite3_value_bytes(argv[0]); | 45 nIn = sqlite3_value_bytes(argv[0]); |
45 nOut = 13 + nIn + (nIn+999)/1000; | 46 nOut = 13 + nIn + (nIn+999)/1000; |
46 pOut = sqlite3_malloc( nOut+5 ); | 47 pOut = sqlite3_malloc( nOut+5 ); |
47 for(i=4; i>=0; i--){ | 48 for(i=4; i>=0; i--){ |
48 x[i] = (nIn >> (7*(4-i)))&0x7f; | 49 x[i] = (nIn >> (7*(4-i)))&0x7f; |
49 } | 50 } |
50 for(i=0; i<4 && x[i]==0; i++){} | 51 for(i=0; i<4 && x[i]==0; i++){} |
51 for(j=0; i<=4; i++, j++) pOut[j] = x[i]; | 52 for(j=0; i<=4; i++, j++) pOut[j] = x[i]; |
52 pOut[j-1] |= 0x80; | 53 pOut[j-1] |= 0x80; |
53 compress(&pOut[j], &nOut, pIn, nIn); | 54 rc = compress(&pOut[j], &nOut, pIn, nIn); |
54 sqlite3_result_blob(context, pOut, nOut+j, sqlite3_free); | 55 if( rc==Z_OK ){ |
| 56 sqlite3_result_blob(context, pOut, nOut+j, sqlite3_free); |
| 57 }else{ |
| 58 sqlite3_free(pOut); |
| 59 } |
55 } | 60 } |
56 | 61 |
57 /* | 62 /* |
58 ** Implementation of the "uncompress(X)" SQL function. The argument X | 63 ** Implementation of the "uncompress(X)" SQL function. The argument X |
59 ** is a blob which was obtained from compress(Y). The output will be | 64 ** is a blob which was obtained from compress(Y). The output will be |
60 ** the value Y. | 65 ** the value Y. |
61 */ | 66 */ |
62 static void uncompressFunc( | 67 static void uncompressFunc( |
63 sqlite3_context *context, | 68 sqlite3_context *context, |
64 int argc, | 69 int argc, |
(...skipping 10 matching lines...) Expand all Loading... |
75 nIn = sqlite3_value_bytes(argv[0]); | 80 nIn = sqlite3_value_bytes(argv[0]); |
76 nOut = 0; | 81 nOut = 0; |
77 for(i=0; i<nIn && i<5; i++){ | 82 for(i=0; i<nIn && i<5; i++){ |
78 nOut = (nOut<<7) | (pIn[i]&0x7f); | 83 nOut = (nOut<<7) | (pIn[i]&0x7f); |
79 if( (pIn[i]&0x80)!=0 ){ i++; break; } | 84 if( (pIn[i]&0x80)!=0 ){ i++; break; } |
80 } | 85 } |
81 pOut = sqlite3_malloc( nOut+1 ); | 86 pOut = sqlite3_malloc( nOut+1 ); |
82 rc = uncompress(pOut, &nOut, &pIn[i], nIn-i); | 87 rc = uncompress(pOut, &nOut, &pIn[i], nIn-i); |
83 if( rc==Z_OK ){ | 88 if( rc==Z_OK ){ |
84 sqlite3_result_blob(context, pOut, nOut, sqlite3_free); | 89 sqlite3_result_blob(context, pOut, nOut, sqlite3_free); |
| 90 }else{ |
| 91 sqlite3_free(pOut); |
85 } | 92 } |
86 } | 93 } |
87 | 94 |
88 | 95 |
89 #ifdef _WIN32 | 96 #ifdef _WIN32 |
90 __declspec(dllexport) | 97 __declspec(dllexport) |
91 #endif | 98 #endif |
92 int sqlite3_compress_init( | 99 int sqlite3_compress_init( |
93 sqlite3 *db, | 100 sqlite3 *db, |
94 char **pzErrMsg, | 101 char **pzErrMsg, |
95 const sqlite3_api_routines *pApi | 102 const sqlite3_api_routines *pApi |
96 ){ | 103 ){ |
97 int rc = SQLITE_OK; | 104 int rc = SQLITE_OK; |
98 SQLITE_EXTENSION_INIT2(pApi); | 105 SQLITE_EXTENSION_INIT2(pApi); |
99 (void)pzErrMsg; /* Unused parameter */ | 106 (void)pzErrMsg; /* Unused parameter */ |
100 rc = sqlite3_create_function(db, "compress", 1, SQLITE_UTF8, 0, | 107 rc = sqlite3_create_function(db, "compress", 1, SQLITE_UTF8, 0, |
101 compressFunc, 0, 0); | 108 compressFunc, 0, 0); |
102 if( rc==SQLITE_OK ){ | 109 if( rc==SQLITE_OK ){ |
103 rc = sqlite3_create_function(db, "uncompress", 1, SQLITE_UTF8, 0, | 110 rc = sqlite3_create_function(db, "uncompress", 1, SQLITE_UTF8, 0, |
104 uncompressFunc, 0, 0); | 111 uncompressFunc, 0, 0); |
105 } | 112 } |
106 return rc; | 113 return rc; |
107 } | 114 } |
OLD | NEW |