summaryrefslogtreecommitdiffstats
path: root/17.patch
blob: ff4f26c11b3884bc1875c022185855d5598fc8bf (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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
From e6f5924867dbe40d07a2befa383e49ade425295c Mon Sep 17 00:00:00 2001
From: Remi Collet <remi@remirepo.net>
Date: Mon, 15 Apr 2019 16:34:33 +0200
Subject: [PATCH 1/2] implement Streams support

---
 tests/streams.phpt |  49 +++++++
 zstd.c             | 349 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 398 insertions(+)
 create mode 100644 tests/streams.phpt

diff --git a/tests/streams.phpt b/tests/streams.phpt
new file mode 100644
index 0000000..3f68b99
--- /dev/null
+++ b/tests/streams.phpt
@@ -0,0 +1,49 @@
+--TEST--
+compress.zstd stream
+--FILE--
+<?php
+include(dirname(__FILE__) . '/data.inc');
+
+$file = dirname(__FILE__) . '/data.out';
+
+echo "Compression with defaul level\n";
+
+var_dump(file_put_contents('compress.zstd://' . $file, $data) == strlen($data));
+var_dump($size1 = filesize($file));
+var_dump($size1 > 1 && $size1 < strlen($data));
+
+echo "Compression with specfic level\n";
+
+$ctx = stream_context_create(
+	array(
+		"zstd" => array(
+			"level" => ZSTD_COMPRESS_LEVEL_MAX,
+		)
+	)
+);
+
+var_dump(file_put_contents('compress.zstd://' . $file, $data, 0, $ctx) == strlen($data));
+var_dump($size2 = filesize($file));
+var_dump($size1 > 1 && $size2 <= $size1);
+
+
+echo "Decompression\n";
+
+$decomp = file_get_contents('compress.zstd://' . $file);
+var_dump($decomp == $data);
+
+@unlink($file);
+?>
+===Done===
+--EXPECTF--
+Compression with defaul level
+bool(true)
+int(%d)
+bool(true)
+Compression with specfic level
+bool(true)
+int(%d)
+bool(true)
+Decompression
+bool(true)
+===Done===
diff --git a/zstd.c b/zstd.c
index 92896e2..a8d0663 100644
--- a/zstd.c
+++ b/zstd.c
@@ -363,6 +363,352 @@ ZEND_FUNCTION(zstd_uncompress_dict)
     efree(rBuff);
 }
 
+
+typedef struct _php_zstd_stream_data {
+    char *bufin, *bufout;
+    size_t sizein, sizeout;
+    ZSTD_CCtx* cctx;
+    ZSTD_DCtx* dctx;
+    ZSTD_inBuffer input;
+    ZSTD_outBuffer output;
+    php_stream *stream;
+} php_zstd_stream_data;
+
+
+#define STREAM_DATA_FROM_STREAM() \
+    php_zstd_stream_data *self = (php_zstd_stream_data *) stream->abstract
+
+#define STREAM_NAME "compress.zstd"
+
+static int php_zstd_decomp_close(php_stream *stream, int close_handle TSRMLS_DC)
+{
+    STREAM_DATA_FROM_STREAM();
+
+    if (!self) {
+        return EOF;
+    }
+
+    if (close_handle) {
+        if (self->stream) {
+            php_stream_close(self->stream);
+            self->stream = NULL;
+        }
+    }
+
+    ZSTD_freeDCtx(self->dctx);
+    efree(self->bufin);
+    efree(self->bufout);
+    efree(self);
+    stream->abstract = NULL;
+
+    return EOF;
+}
+
+static int php_zstd_comp_close(php_stream *stream, int close_handle TSRMLS_DC)
+{
+    size_t x, res;
+    STREAM_DATA_FROM_STREAM();
+
+    if (!self) {
+        return EOF;
+    }
+
+    /* Compress remaining data */
+    if (self->input.size)  {
+        self->input.pos = 0;
+        do {
+            self->output.size = self->sizeout;
+            self->output.pos  = 0;
+            res = ZSTD_compressStream(self->cctx, &self->output, &self->input);
+            if (ZSTD_isError(res)) {
+                php_error_docref(NULL TSRMLS_CC, E_WARNING, "libzstd error %s\n", ZSTD_getErrorName(res));
+            }
+            php_stream_write(self->stream, self->bufout, self->output.pos);
+        } while (self->input.pos != self->input.size);
+    }
+
+    /* Flush */
+    do {
+        self->output.size = self->sizeout;
+        self->output.pos  = 0;
+        x = ZSTD_endStream(self->cctx, &self->output);
+        if (ZSTD_isError(x)) {
+            php_error_docref(NULL TSRMLS_CC, E_WARNING, "libzstd error %s\n", ZSTD_getErrorName(x));
+        }
+        php_stream_write(self->stream, self->bufout, self->output.pos);
+    } while (x > 0);
+
+    if (close_handle) {
+        if (self->stream) {
+            php_stream_close(self->stream);
+            self->stream = NULL;
+        }
+    }
+
+    ZSTD_freeCCtx(self->cctx);
+    efree(self->bufin);
+    efree(self->bufout);
+    efree(self);
+    stream->abstract = NULL;
+
+    return EOF;
+}
+
+
+static int php_zstd_flush(php_stream *stream TSRMLS_DC)
+{
+    return 0;
+}
+
+
+static size_t php_zstd_decomp_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
+{
+    size_t x, res, ret = 0;
+    STREAM_DATA_FROM_STREAM();
+
+    while (count > 0) {
+        x = self->output.size - self->output.pos;
+        /* enough available */
+        if (x >= count) {
+            memcpy(buf, self->bufout + self->output.pos, count);
+            self->output.pos += count;
+            ret += count;
+            return ret;
+        }
+        /* take remaining from out  */
+        if (x) {
+            memcpy(buf, self->bufout + self->output.pos, x);
+            self->output.pos += x;
+            ret += x;
+            buf += x;
+            count -= x;
+        }
+        /* decompress */
+        if (self->input.pos < self->input.size) {
+            /* for zstd */
+            self->output.pos = 0;
+            self->output.size = self->sizeout;
+            res = ZSTD_decompressStream(self->dctx, &self->output , &self->input);
+            if (ZSTD_isError(res)) {
+                php_error_docref(NULL TSRMLS_CC, E_WARNING, "libzstd error %s\n", ZSTD_getErrorName(res));
+            }
+            /* for us */
+            self->output.size = self->output.pos;
+            self->output.pos = 0;
+        }  else {
+            /* read */
+            self->input.pos = 0;
+            self->input.size = php_stream_read(self->stream, self->bufin, self->sizein);
+            if (!self->input.size) {
+                /* EOF */
+                count = 0;
+            }
+        }
+    }
+    return ret;
+}
+
+
+static size_t php_zstd_comp_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
+{
+    size_t res, x, ret = 0;
+
+    STREAM_DATA_FROM_STREAM();
+
+    while(count > 0) {
+        /* enough room for full data */
+        if (self->input.size + count < self->sizein) {
+            memcpy(self->bufin + self->input.size, buf, count);
+            self->input.size += count;
+            ret += count;
+            count = 0;
+            break;
+        }
+
+        /* fill input buffer */
+        x = self->sizein - self->input.size;
+        memcpy(self->bufin + self->input.size, buf, x);
+        self->input.size += x;
+        buf += x;
+        count -= x;
+        ret += x;
+
+        /* compress and write */
+        self->input.pos = 0;
+        do {
+            self->output.size = self->sizeout;
+            self->output.pos  = 0;
+            res = ZSTD_compressStream(self->cctx, &self->output, &self->input);
+            if (ZSTD_isError(res)) {
+                php_error_docref(NULL TSRMLS_CC, E_WARNING, "libzstd error %s\n", ZSTD_getErrorName(res));
+            }
+            php_stream_write(self->stream, self->bufout, self->output.pos);
+        } while (self->input.pos != self->input.size);
+
+        self->input.pos = 0;
+        self->input.size = 0;
+    }
+    return ret;
+}
+
+
+static php_stream_ops php_stream_zstd_read_ops = {
+    NULL,    /* write */
+    php_zstd_decomp_read,
+    php_zstd_decomp_close,
+    php_zstd_flush,
+    STREAM_NAME,
+    NULL,    /* seek */
+    NULL,    /* cast */
+    NULL,    /* stat */
+    NULL      /* set_option */
+};
+
+
+static php_stream_ops php_stream_zstd_write_ops = {
+    php_zstd_comp_write,
+    NULL,    /* read */
+    php_zstd_comp_close,
+    php_zstd_flush,
+    STREAM_NAME,
+    NULL,    /* seek */
+    NULL,    /* cast */
+    NULL,    /* stat */
+    NULL      /* set_option */
+};
+
+
+static php_stream *
+php_stream_zstd_opener(
+    php_stream_wrapper *wrapper,
+#if PHP_VERSION_ID < 50600
+    char *path,
+    char *mode,
+#else
+    const char *path,
+    const char *mode,
+#endif
+    int options,
+#if PHP_MAJOR_VERSION < 7
+    char **opened_path,
+#else
+    zend_string **opened_path,
+#endif
+    php_stream_context *context
+    STREAMS_DC TSRMLS_DC)
+{
+    php_zstd_stream_data *self;
+    int level = ZSTD_CLEVEL_DEFAULT;
+
+    if (strncasecmp(STREAM_NAME, path, sizeof(STREAM_NAME)-1) == 0) {
+        path += sizeof(STREAM_NAME)-1;
+        if (strncmp("://", path, 3) == 0) {
+            path += 3;
+        }
+    }
+
+    if (php_check_open_basedir(path)) {
+        return NULL;
+    }
+
+    if (context) {
+#if PHP_MAJOR_VERSION >= 7
+        zval *tmpzval;
+
+        if (NULL != (tmpzval = php_stream_context_get_option(context, "zstd", "level"))) {
+            level = zval_get_long(tmpzval);
+        }
+#else
+        zval **tmpzval;
+
+        if (php_stream_context_get_option(context, "zstd", "level", &tmpzval) == SUCCESS) {
+            convert_to_long_ex(tmpzval);
+            level = Z_LVAL_PP(tmpzval);
+        }
+#endif
+    }
+
+    if (level > ZSTD_maxCLevel()) {
+        php_error_docref(NULL TSRMLS_CC, E_WARNING, "zstd: compression level (%d) must be less than %d", level, ZSTD_maxCLevel());
+        level = ZSTD_maxCLevel();
+    }
+
+    self = ecalloc(sizeof(*self), 1);
+    self->stream = php_stream_open_wrapper(path, mode, REPORT_ERRORS, NULL);
+    /* File */
+    if (!strcmp(mode, "w") || !strcmp(mode, "wb")) {
+        self->dctx = NULL;
+        self->cctx = ZSTD_createCCtx();
+        if (!self->cctx) {
+            php_error_docref(NULL TSRMLS_CC, E_WARNING, "zstd: compression context failed");
+            php_stream_close(self->stream);
+            efree(self);
+            return NULL;
+        }
+        ZSTD_initCStream(self->cctx, level);
+        self->bufin = emalloc(self->sizein = ZSTD_CStreamInSize());
+        self->bufout = emalloc(self->sizeout = ZSTD_CStreamOutSize());
+        self->input.src  = self->bufin;
+        self->input.pos   = 0;
+        self->input.size  = 0;
+        self->output.dst = self->bufout;
+        self->output.pos  = 0;
+        self->output.size = 0;
+
+        return php_stream_alloc(&php_stream_zstd_write_ops, self, NULL, mode);
+
+    } else if (!strcmp(mode, "r") || !strcmp(mode, "rb")) {
+        self->dctx = ZSTD_createDCtx();
+        if (!self->dctx) {
+            php_error_docref(NULL TSRMLS_CC, E_WARNING, "zstd: compression context failed");
+            php_stream_close(self->stream);
+            efree(self);
+            return NULL;
+        }
+        self->cctx = NULL;
+        self->bufin = emalloc(self->sizein = ZSTD_DStreamInSize());
+        self->bufout = emalloc(self->sizeout = ZSTD_DStreamOutSize());
+        ZSTD_initDStream(self->dctx);
+        self->input.src   = self->bufin;
+        self->input.pos   = 0;
+        self->input.size  = 0;
+        self->output.dst  = self->bufout;
+        self->output.pos  = 0;
+        self->output.size = 0;
+
+        return php_stream_alloc(&php_stream_zstd_read_ops, self, NULL, mode);
+
+    } else {
+        php_error_docref(NULL, E_ERROR, "zstd: invalid open mode");
+    }
+    return NULL;
+}
+
+
+static php_stream_wrapper_ops zstd_stream_wops = {
+    php_stream_zstd_opener,
+    NULL,    /* close */
+    NULL,    /* fstat */
+    NULL,    /* stat */
+    NULL,    /* opendir */
+    STREAM_NAME,
+    NULL,    /* unlink */
+    NULL,    /* rename */
+    NULL,    /* mkdir */
+    NULL    /* rmdir */
+#if PHP_VERSION_ID >= 50400
+    , NULL
+#endif
+};
+
+
+php_stream_wrapper php_stream_zstd_wrapper = {
+    &zstd_stream_wops,
+    NULL,
+    0 /* is_url */
+};
+
+
 ZEND_MINIT_FUNCTION(zstd)
 {
     REGISTER_LONG_CONSTANT("ZSTD_COMPRESS_LEVEL_MIN",
@@ -374,6 +720,9 @@ ZEND_MINIT_FUNCTION(zstd)
     REGISTER_LONG_CONSTANT("ZSTD_COMPRESS_LEVEL_DEFAULT",
                            DEFAULT_COMPRESS_LEVEL,
                            CONST_CS | CONST_PERSISTENT);
+
+    php_register_url_stream_wrapper(STREAM_NAME, &php_stream_zstd_wrapper TSRMLS_CC);
+
     return SUCCESS;
 }
 

From 6e68f9d36b0d66f6b36999bf84c5883fbf67c513 Mon Sep 17 00:00:00 2001
From: Remi Collet <remi@remirepo.net>
Date: Mon, 15 Apr 2019 16:57:08 +0200
Subject: [PATCH 2/2] fix ZTS

---
 zstd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/zstd.c b/zstd.c
index a8d0663..1a82bda 100644
--- a/zstd.c
+++ b/zstd.c
@@ -607,7 +607,7 @@ php_stream_zstd_opener(
         }
     }
 
-    if (php_check_open_basedir(path)) {
+    if (php_check_open_basedir(path TSRMLS_CC)) {
         return NULL;
     }
 
@@ -679,7 +679,7 @@ php_stream_zstd_opener(
         return php_stream_alloc(&php_stream_zstd_read_ops, self, NULL, mode);
 
     } else {
-        php_error_docref(NULL, E_ERROR, "zstd: invalid open mode");
+        php_error_docref(NULL TSRMLS_CC, E_ERROR, "zstd: invalid open mode");
     }
     return NULL;
 }