summaryrefslogtreecommitdiffstats
path: root/filter.c
blob: 5b6c158f4fd296d82ef707b552439faa414d6f9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "php.h"
#include "lzf.h"

#define LZF_BLOCKSIZE	(1024 * 64 - 1)

typedef struct {
	char signature[3];
	char usize[2];
} lzf_header_uncompressed;

typedef struct {
	char signature[3];
	char csize[2];
	char usize[2];
} lzf_header_compressed;

typedef struct _php_lzf_compress_filter {
	int persistent;
	char *buffer;
	size_t buffer_pos;
} php_lzf_filter_state;

static void fill_header_compressed(lzf_header_compressed *header, size_t compressed, size_t uncompressed)
{
	/* Copied from liblzf/lzf.c */
	
	header->signature[0] = 'Z';
	header->signature[1] = 'V';
	header->signature[2] = 1;
	header->csize[0] = compressed >> 8;
	header->csize[1] = compressed & 0xff;
	header->usize[0] = uncompressed >> 8;
	header->usize[1] = uncompressed & 0xff;
}

static void fill_header_uncompressed(lzf_header_uncompressed *header, size_t uncompressed)
{
	/* Copied from liblzf/lzf.c */
	
	header->signature[0] = 'Z';
	header->signature[1] = 'V';
	header->signature[2] = 0;
	header->usize[0] = uncompressed >> 8;
	header->usize[1] = uncompressed & 0xff;
}

static int php_lzf_filter_state_ctor(php_lzf_filter_state *inst, int persistent)
{
	inst->persistent = persistent;
	inst->buffer = pemalloc(LZF_BLOCKSIZE, persistent);
	inst->buffer_pos = 0;

	return SUCCESS;
}

static void php_lzf_filter_state_dtor(php_lzf_filter_state *inst TSRMLS_DC)
{
	pefree(inst->buffer, inst->persistent);
}

static int lzf_compress_filter_append_bucket(
	php_stream *stream,
	php_stream_filter_status_t *exit_status,
	php_lzf_filter_state *inst,
	php_stream_bucket_brigade *buckets_out,
	int persistent TSRMLS_DC)
{
	int status;
	size_t buffer_size;
	php_stream_bucket *new_bucket;
	char *output_buffer;

	/* Allocate buffer with a size of data and (larger) header */
	output_buffer = pemalloc(inst->buffer_pos + sizeof(lzf_header_compressed), persistent);

	if (!output_buffer)
		goto fail;
		
	/* Try to compress data. */
	status = lzf_compress(inst->buffer, inst->buffer_pos, output_buffer + sizeof(lzf_header_compressed), inst->buffer_pos);

	/* 
	 * If we were able to compress data, write compressed block. Otherwise 
	 * use uncompressed block.
	 */
	if (status > 0) {
		output_buffer = perealloc(output_buffer, status + sizeof(lzf_header_compressed), persistent);
		fill_header_compressed((lzf_header_compressed *) output_buffer, status, inst->buffer_pos);
		buffer_size = status + sizeof(lzf_header_compressed);
	} else {
		/* Pessimistic case - we still need to memcpy() data */
		output_buffer = perealloc(output_buffer, inst->buffer_pos + sizeof(lzf_header_uncompressed), persistent);
		fill_header_uncompressed((lzf_header_uncompressed *) output_buffer, inst->buffer_pos);
		memcpy(output_buffer + sizeof(lzf_header_uncompressed), inst->buffer, inst->buffer_pos);
		buffer_size = inst->buffer_pos + sizeof(lzf_header_uncompressed);
	}

	/* Create new bucket and append it */
	new_bucket = php_stream_bucket_new(stream, output_buffer, buffer_size, 1, 0 TSRMLS_CC);
	if (!new_bucket)
		goto fail_free_buffer;

	php_stream_bucket_append(buckets_out, new_bucket TSRMLS_CC);

	/* Clear our buffer */
	inst->buffer_pos = 0; 
	
	/* Change exit status */
	*exit_status = PSFS_PASS_ON;
	
	return SUCCESS;

fail_free_buffer:
	pefree(output_buffer, persistent);
fail:
	return FAILURE;
}

static int lzf_compress_append_data(
	php_stream *stream,
	php_stream_filter_status_t *exit_status,
	php_stream_bucket_brigade *buckets_out,
	php_lzf_filter_state *inst,
	const char *input_buffer,
	size_t input_buffer_len,
	size_t *consumed,
	int persistent TSRMLS_DC)
{
	size_t free_buffer;
	size_t bytes_to_copy;
	
	/* As long as there are data in the input buffer... */
	while (input_buffer_len) {
		free_buffer = LZF_BLOCKSIZE - inst->buffer_pos;		/* Free space in buffer */
		bytes_to_copy = MIN(free_buffer, input_buffer_len);	/* Bytes to copy into buffer */

		/* ... copy as many bytes into buffer as possible */
		memcpy(inst->buffer + inst->buffer_pos, input_buffer, bytes_to_copy);
		inst->buffer_pos += bytes_to_copy;
		input_buffer += bytes_to_copy;
		input_buffer_len -= bytes_to_copy;
		(*consumed) += bytes_to_copy;

		/* If the buffer is full, we need to flush it */
		if (inst->buffer_pos == LZF_BLOCKSIZE) {
			if (lzf_compress_filter_append_bucket(stream, exit_status, inst, buckets_out, persistent TSRMLS_CC) != SUCCESS)
				return FAILURE;
		}
	}
	
	return SUCCESS;
}

static php_stream_filter_status_t lzf_compress_filter(
	php_stream *stream,
	php_stream_filter *thisfilter,
	php_stream_bucket_brigade *buckets_in,
	php_stream_bucket_brigade *buckets_out,
	size_t *bytes_consumed,
	int flags TSRMLS_DC)
{
	size_t consumed = 0;
	php_lzf_filter_state *inst = (php_lzf_filter_state *) thisfilter->abstract;
	php_stream_filter_status_t exit_status = PSFS_FEED_ME;
	php_stream_bucket *bucket = NULL;

	while (buckets_in->head) {
		bucket = buckets_in->head;

		php_stream_bucket_unlink(bucket TSRMLS_CC);

		if (lzf_compress_append_data(stream, &exit_status, buckets_out, inst, bucket->buf, bucket->buflen, &consumed, 
				php_stream_is_persistent(stream) TSRMLS_CC) != SUCCESS)
			goto fail_free_bucket;

		php_stream_bucket_delref(bucket TSRMLS_CC);
	}

	if (bytes_consumed)
		*bytes_consumed = consumed;

	if (flags & PSFS_FLAG_FLUSH_CLOSE) {
		if (lzf_compress_filter_append_bucket(stream, &exit_status, inst, buckets_out, php_stream_is_persistent(stream) TSRMLS_CC) != SUCCESS)
			goto fail;
	}

	return exit_status;

fail_free_bucket:
	if (bucket != NULL)
		php_stream_bucket_delref(bucket TSRMLS_CC);
fail:
	return PSFS_ERR_FATAL;
}

static php_stream_filter_status_t lzf_decompress_filter(
	php_stream *stream,
	php_stream_filter *thisfilter,
	php_stream_bucket_brigade *buckets_in,
	php_stream_bucket_brigade *buckets_out,
	size_t *bytes_consumed,
	int flags TSRMLS_DC)
{
	return PSFS_PASS_ON;
}

static void lzf_filter_state_dtor(php_stream_filter *thisfilter TSRMLS_DC)
{
	assert(thisfilter->abstract != NULL);

	php_lzf_filter_state_dtor((php_lzf_filter_state *) thisfilter->abstract TSRMLS_CC);
	pefree(thisfilter->abstract, ((php_lzf_filter_state *) thisfilter->abstract)->persistent);
}

static php_stream_filter_ops lzf_compress_ops = {
	lzf_compress_filter,
	lzf_filter_state_dtor,
	"lzf.compress"
};

static php_stream_filter_ops lzf_decompress_ops = {
	lzf_decompress_filter,
	lzf_filter_state_dtor,
	"lzf.decompress"
};

static php_stream_filter *lzf_compress_filter_create(const char *filtername, zval *filterparams, int persistent TSRMLS_DC)
{
	php_lzf_filter_state *inst;

	inst = pemalloc(sizeof(php_lzf_filter_state), persistent);
	if (inst == NULL)
		return NULL;

	if (php_lzf_filter_state_ctor(inst, persistent) != SUCCESS) {
		pefree(inst, persistent);
		return NULL;
	}

	return php_stream_filter_alloc(&lzf_compress_ops, inst, persistent);
}

static php_stream_filter *lzf_decompress_filter_create(const char *filtername, zval *filterparams, int persistent TSRMLS_DC)
{
	php_lzf_filter_state *inst;

	inst = pemalloc(sizeof(php_lzf_filter_state), persistent);
	if (inst == NULL)
		return NULL;

	if (php_lzf_filter_state_ctor(inst, persistent) != SUCCESS) {
		pefree(inst, persistent);
		return NULL;
	}

	return php_stream_filter_alloc(&lzf_decompress_ops, inst, persistent);
}

php_stream_filter_factory php_lzf_compress_filter_factory = {
	lzf_compress_filter_create
};

php_stream_filter_factory php_lzf_decompress_filter_factory = {
	lzf_decompress_filter_create
};