-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathphp_bsdiff.c
More file actions
394 lines (342 loc) · 12.7 KB
/
php_bsdiff.c
File metadata and controls
394 lines (342 loc) · 12.7 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
+----------------------------------------------------------------------+
| bsdiff |
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Demin Yin <deminy@deminy.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "zend_exceptions.h"
#include "bsdiff.h"
#include "bspatch.h"
#include "php_bsdiff.h"
#include "php_bsdiff_arginfo.h"
#include <bzlib.h>
zend_class_entry *ce_bsdiff_exception;
void offtout(int64_t x,uint8_t *buf);
int64_t offtin(uint8_t *buf);
/* Wrappers around PHP's emalloc/efree for use as function pointers.
* emalloc/efree are macros, so they cannot be assigned directly. */
static void *php_bsdiff_emalloc(size_t size)
{
return emalloc(size);
}
static void php_bsdiff_efree(void *ptr)
{
efree(ptr);
}
static int bz2_write(struct bsdiff_stream* stream, const void* buffer, int size)
{
int bz2err;
BZFILE* bz2;
bz2 = (BZFILE*)stream->opaque;
BZ2_bzWrite(&bz2err, bz2, (void*)buffer, size);
if (bz2err != BZ_STREAM_END && bz2err != BZ_OK)
return -1;
return 0;
}
static int bz2_read(const struct bspatch_stream* stream, void* buffer, int length)
{
int n;
int bz2err;
BZFILE* bz2;
bz2 = (BZFILE*)stream->opaque;
n = BZ2_bzRead(&bz2err, bz2, buffer, length);
if (n != length)
return -1;
return 0;
}
/* {{{ void bsdiff_diff( string $old_file, string $new_file, string $patch_file ) */
PHP_FUNCTION(bsdiff_diff)
{
char *old_file, *new_file, *diff_file;
size_t old_file_len, new_file_len, diff_file_len;
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_PATH(old_file, old_file_len)
Z_PARAM_PATH(new_file, new_file_len)
Z_PARAM_PATH(diff_file, diff_file_len)
ZEND_PARSE_PARAMETERS_END();
int bz2err;
int64_t oldsize, newsize;
uint8_t buf[8];
FILE *pf = NULL;
BZFILE *bz2 = NULL;
struct bsdiff_stream stream;
zend_string *old_str = NULL;
zend_string *new_str = NULL;
php_stream *php_s;
php_stream *diff_s = NULL;
stream.malloc = php_bsdiff_emalloc;
stream.free = php_bsdiff_efree;
stream.write = bz2_write;
/* Read old file into a managed zend_string */
php_s = php_stream_open_wrapper(old_file, "rb", 0, NULL);
if (!php_s) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to open the old file \"%s\"", old_file);
goto cleanup;
}
old_str = php_stream_copy_to_mem(php_s, PHP_STREAM_COPY_ALL, 0);
php_stream_close(php_s);
/* Read new file into a managed zend_string */
php_s = php_stream_open_wrapper(new_file, "rb", 0, NULL);
if (!php_s) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to open the new file \"%s\"", new_file);
goto cleanup;
}
new_str = php_stream_copy_to_mem(php_s, PHP_STREAM_COPY_ALL, 0);
php_stream_close(php_s);
oldsize = old_str ? (int64_t)ZSTR_LEN(old_str) : 0;
newsize = new_str ? (int64_t)ZSTR_LEN(new_str) : 0;
/* Create the patch file.
*
* The diff file is opened as a php_stream so that PHP's resource tracker
* will close it automatically on request shutdown — even if emalloc
* triggers a fatal error (longjmp) inside the bsdiff library, skipping
* our cleanup block. The FILE* needed by bzip2 is obtained via
* php_stream_cast(); plain file streams are backed by a FILE* internally,
* so the cast simply returns the underlying handle with no overhead.
*
* The "b" flag is required for correctness on Windows when the extension
* is loaded by a host that has not set the MSVC global `_fmode = _O_BINARY`.
* The PHP CLI and CGI SAPIs both set it at startup, so in practice every
* PHP process on Windows already opens streams in binary mode regardless
* of this flag, but specifying it explicitly removes that dependency and
* documents the intent. The flag is a no-op on POSIX systems. */
diff_s = php_stream_open_wrapper(diff_file, "wb", 0, NULL);
if (!diff_s) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Cannot open the diff file \"%s\" in write mode", diff_file);
goto cleanup;
}
if (php_stream_cast(diff_s, PHP_STREAM_AS_STDIO, (void **)&pf, 0) == FAILURE) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Cannot open the diff file \"%s\" in write mode", diff_file);
goto cleanup;
}
/* Write header (signature+newsize) */
offtout(newsize, buf);
if (fwrite("ENDSLEY/BSDIFF43", 16, 1, pf) != 1 ||
fwrite(buf, sizeof(buf), 1, pf) != 1) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to write header to the diff file");
goto cleanup;
}
if (NULL == (bz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0))) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to prepare to write data to the diff file (bz2err=%d)", bz2err);
goto cleanup;
}
stream.opaque = bz2;
if (bsdiff(
old_str ? (const uint8_t *)ZSTR_VAL(old_str) : (const uint8_t *)"",
oldsize,
new_str ? (const uint8_t *)ZSTR_VAL(new_str) : (const uint8_t *)"",
newsize,
&stream)) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to create diff data");
goto cleanup;
}
BZ2_bzWriteClose(&bz2err, bz2, 0, NULL, NULL);
bz2 = NULL;
if (bz2err != BZ_OK) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to complete writing data to the diff file (bz2err=%d)", bz2err);
goto cleanup;
}
php_stream_close(diff_s);
diff_s = NULL;
cleanup:
if (bz2) {
BZ2_bzWriteClose(&bz2err, bz2, 0, NULL, NULL);
}
if (diff_s) {
php_stream_close(diff_s);
}
if (new_str) {
zend_string_release(new_str);
}
if (old_str) {
zend_string_release(old_str);
}
if (EG(exception)) {
RETURN_THROWS();
}
}
/* }}} */
/* {{{ void bsdiff_patch( string $old_file, string $new_file, string $patch_file ) */
PHP_FUNCTION(bsdiff_patch)
{
char *old_file, *new_file, *diff_file;
size_t old_file_len, new_file_len, diff_file_len;
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_PATH(old_file, old_file_len)
Z_PARAM_PATH(new_file, new_file_len)
Z_PARAM_PATH(diff_file, diff_file_len)
ZEND_PARSE_PARAMETERS_END();
int bz2err;
uint8_t header[24];
uint8_t *new_buf = NULL;
int64_t oldsize, newsize;
FILE *f = NULL;
BZFILE *bz2 = NULL;
struct bspatch_stream stream;
zend_stat_t sb;
zend_string *old_str = NULL;
php_stream *php_s;
php_stream *diff_s = NULL;
/* Open patch file.
*
* The diff file is opened as a php_stream (see the matching comment in bsdiff_diff() for the rationale).
* The FILE* needed by bzip2 is obtained via php_stream_cast(). */
diff_s = php_stream_open_wrapper(diff_file, "rb", 0, NULL);
if (!diff_s) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Cannot open diff file \"%s\" in read mode", diff_file);
goto cleanup;
}
if (php_stream_cast(diff_s, PHP_STREAM_AS_STDIO, (void **)&f, 0) == FAILURE) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Cannot open diff file \"%s\" in read mode", diff_file);
goto cleanup;
}
/* Read header */
if (fread(header, 1, 24, f) != 24) {
if (feof(f)) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "The diff file is corrupted (missing header information)");
} else {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to read data from the diff file");
}
goto cleanup;
}
/* Check for appropriate magic */
if (memcmp(header, "ENDSLEY/BSDIFF43", 16) != 0) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "The diff file is corrupted (invalid header information)");
goto cleanup;
}
/* Read lengths from header */
newsize = offtin(header + 16);
if (newsize < 0) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "The diff file is corrupted (invalid length information)");
goto cleanup;
}
/* Stat the old file to preserve its permissions on the output */
if (VCWD_STAT(old_file, &sb) != 0) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to stat the old file \"%s\"", old_file);
goto cleanup;
}
/* Read old file into a managed zend_string */
php_s = php_stream_open_wrapper(old_file, "rb", 0, NULL);
if (!php_s) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to open the old file \"%s\"", old_file);
goto cleanup;
}
old_str = php_stream_copy_to_mem(php_s, PHP_STREAM_COPY_ALL, 0);
php_stream_close(php_s);
oldsize = old_str ? (int64_t)ZSTR_LEN(old_str) : 0;
/* Allocate buffer for patched output; bspatch() writes into it */
new_buf = emalloc(newsize + 1);
if (NULL == (bz2 = BZ2_bzReadOpen(&bz2err, f, 0, 0, NULL, 0))) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to read data from the diff file (bz2err=%d)", bz2err);
goto cleanup;
}
stream.read = bz2_read;
stream.opaque = bz2;
if (bspatch(
old_str ? (const uint8_t *)ZSTR_VAL(old_str) : (const uint8_t *)"",
oldsize,
new_buf,
newsize,
&stream)) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to apply diff data");
goto cleanup;
}
/* Clean up the bzip2 reads and diff file before writing output */
BZ2_bzReadClose(&bz2err, bz2);
bz2 = NULL;
php_stream_close(diff_s);
diff_s = NULL;
/* Write the new file */
php_s = php_stream_open_wrapper(new_file, "wb", 0, NULL);
if (!php_s) {
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to create the new file \"%s\"", new_file);
goto cleanup;
}
if (newsize > 0 && (size_t)php_stream_write(php_s, (const char *)new_buf, (size_t)newsize) != (size_t)newsize) {
php_stream_close(php_s);
zend_throw_exception_ex(ce_bsdiff_exception, 0, "Failed to write to the new file \"%s\"", new_file);
goto cleanup;
}
php_stream_close(php_s);
/* Preserve original file permissions */
VCWD_CHMOD(new_file, sb.st_mode);
cleanup:
if (bz2) {
BZ2_bzReadClose(&bz2err, bz2);
}
if (diff_s) {
php_stream_close(diff_s);
}
if (new_buf) {
efree(new_buf);
}
if (old_str) {
zend_string_release(old_str);
}
if (EG(exception)) {
RETURN_THROWS();
}
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(bsdiff)
{
#if defined(ZTS) && defined(COMPILE_DL_BSDIFF)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(bsdiff)
{
ce_bsdiff_exception = register_class_BsdiffException(zend_ce_exception);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(bsdiff)
{
php_info_print_table_start();
php_info_print_table_header(2, "bsdiff support", "enabled");
php_info_print_table_row(2, "bsdiff version", PHP_BSDIFF_VERSION);
php_info_print_table_row(2, "BZip2 version", (char *) BZ2_bzlibVersion());
php_info_print_table_end();
}
/* }}} */
/* {{{ bsdiff_module_entry */
zend_module_entry bsdiff_module_entry = {
STANDARD_MODULE_HEADER,
"bsdiff", /* Extension name */
ext_functions, /* zend_function_entry */
PHP_MINIT(bsdiff), /* PHP_MINIT - Module initialization */
NULL, /* PHP_MSHUTDOWN - Module shutdown */
PHP_RINIT(bsdiff), /* PHP_RINIT - Request initialization */
NULL, /* PHP_RSHUTDOWN - Request shutdown */
PHP_MINFO(bsdiff), /* PHP_MINFO - Module info */
PHP_BSDIFF_VERSION, /* Version */
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_BSDIFF
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(bsdiff)
#endif