|
1
|
/* |
|
2
|
** Copyright (c) 2011 D. Richard Hipp |
|
3
|
** |
|
4
|
** This program is free software; you can redistribute it and/or |
|
5
|
** modify it under the terms of the Simplified BSD License (also |
|
6
|
** known as the "2-Clause License" or "FreeBSD License".) |
|
7
|
|
|
8
|
** This program is distributed in the hope that it will be useful, |
|
9
|
** but without any warranty; without even the implied warranty of |
|
10
|
** merchantability or fitness for a particular purpose. |
|
11
|
** |
|
12
|
** Author contact information: |
|
13
|
** [email protected] |
|
14
|
** http://www.hwaci.com/drh/ |
|
15
|
** |
|
16
|
******************************************************************************* |
|
17
|
** |
|
18
|
** This file contains code used to incrementally generate a GZIP compressed |
|
19
|
** file. The GZIP format is described in RFC-1952. |
|
20
|
** |
|
21
|
** State information is stored in static variables, so this implementation |
|
22
|
** can only be building up a single GZIP file at a time. |
|
23
|
*/ |
|
24
|
#include "config.h" |
|
25
|
#include <assert.h> |
|
26
|
#include </* |
|
27
|
** State information for the GZIP file under construction. |
|
28
|
*/ |
|
29
|
struct gzip_state { |
|
30
|
int eState; /* 0: idle 1: header 2: compressing */ |
|
31
|
unsigned long iCRC; /* The checksum */ |
|
32
|
z_stream stream; /* The working compressor */ |
|
33
|
Blob out; /* Results stored here */ |
|
34
|
} gzip; |
|
35
|
|
|
36
|
/* |
|
37
|
** Write a 32-bit integer as little-endian into the given buffer. |
|
38
|
*/ |
|
39
|
static void put32(char *z, int v){ |
|
40
|
z[0] = v & 0xff; |
|
41
|
z[1] = (v>>8) & 0xff; |
|
42
|
z[2] = (v>>16) & 0xff; |
|
43
|
z[3] = (v>>24) & 0xff; |
|
44
|
} |
|
45
|
|
|
46
|
/* |
|
47
|
** Begin constructing a gzip file. |
|
48
|
*/ |
|
49
|
void gzip_begin(sqlite3_int64 now){ |
|
50
|
char aHdr[10]; |
|
51
|
assert( gzip.eState==0 ); |
|
52
|
blob_zero(&gzip.out); |
|
53
|
aHdr[0] = 0x1f; |
|
54
|
aHdr[1] = 0x8b; |
|
55
|
aHdr[2] = 8; |
|
56
|
aHdr[3] = 0; |
|
57
|
if( now==-1 ){ |
|
58
|
now = db_int64(0, "SELECT (julianday('now') - 2440587.5)*86400.0"); |
|
59
|
} |
|
60
|
put32(&aHdr[4], now&0xffffffff); |
|
61
|
aHdr[8] = 2; |
|
62
|
aHdr[9] = -1; |
|
63
|
blob_append(&gzip.out, aHdr, 10); |
|
64
|
gzip.iCRC = 0; |
|
65
|
gzip.eState = 1; |
|
66
|
} |
|
67
|
|
|
68
|
/* |
|
69
|
** Add nIn bytes of content from pIn to the gzip file. |
|
70
|
*/ |
|
71
|
#define GZIP_BUFSZ 100000 |