4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 ******************************************************************************
13 ** This SQLite extension implements SQL compression functions
14 ** compress() and uncompress() using ZLIB.
16 #include "sqlite3ext.h"
17 SQLITE_EXTENSION_INIT1
21 ** Implementation of the "compress(X)" SQL function. The input X is
22 ** compressed using zLib and the output is returned.
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.
31 ** This function, SQLAR, and ZIP all use the same "deflate" compression
32 ** algorithm, but each is subtly different:
34 ** * ZIP uses raw deflate.
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.
39 ** * This utility uses the "zlib format" like SQLAR, but adds the variable-
40 ** length integer uncompressed size value at the beginning.
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.
46 static void compressFunc(
47 sqlite3_context
*context
,
51 const unsigned char *pIn
;
54 unsigned long int nOut
;
59 pIn
= sqlite3_value_blob(argv
[0]);
60 nIn
= sqlite3_value_bytes(argv
[0]);
61 nOut
= 13 + nIn
+ (nIn
+999)/1000;
62 pOut
= sqlite3_malloc( nOut
+5 );
64 x
[i
] = (nIn
>> (7*(4-i
)))&0x7f;
66 for(i
=0; i
<4 && x
[i
]==0; i
++){}
67 for(j
=0; i
<=4; i
++, j
++) pOut
[j
] = x
[i
];
69 rc
= compress(&pOut
[j
], &nOut
, pIn
, nIn
);
71 sqlite3_result_blob(context
, pOut
, nOut
+j
, sqlite3_free
);
78 ** Implementation of the "uncompress(X)" SQL function. The argument X
79 ** is a blob which was obtained from compress(Y). The output will be
82 static void uncompressFunc(
83 sqlite3_context
*context
,
87 const unsigned char *pIn
;
90 unsigned long int nOut
;
94 pIn
= sqlite3_value_blob(argv
[0]);
95 nIn
= sqlite3_value_bytes(argv
[0]);
97 for(i
=0; i
<nIn
&& i
<5; i
++){
98 nOut
= (nOut
<<7) | (pIn
[i
]&0x7f);
99 if( (pIn
[i
]&0x80)!=0 ){ i
++; break; }
101 pOut
= sqlite3_malloc( nOut
+1 );
102 rc
= uncompress(pOut
, &nOut
, &pIn
[i
], nIn
-i
);
104 sqlite3_result_blob(context
, pOut
, nOut
, sqlite3_free
);
112 __declspec(dllexport
)
114 int sqlite3_compress_init(
117 const sqlite3_api_routines
*pApi
120 SQLITE_EXTENSION_INIT2(pApi
);
121 (void)pzErrMsg
; /* Unused parameter */
122 rc
= sqlite3_create_function(db
, "compress", 1, SQLITE_UTF8
, 0,
125 rc
= sqlite3_create_function(db
, "uncompress", 1, SQLITE_UTF8
, 0,
126 uncompressFunc
, 0, 0);