-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.c
120 lines (104 loc) · 3.1 KB
/
stream.c
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
#include "php_libarchive.h"
#include "zend_variables.h"
#include <archive.h>
#if PHP_VERSION_ID < 70400
typedef size_t stream_ret_t;
#define STREAM_ERR_RET 0
#else
typedef ssize_t stream_ret_t;
#define STREAM_ERR_RET 1
#endif
static stream_ret_t php_arch_ops_write(php_stream *stream,
const char *buf,
size_t count)
{
(void)stream;
(void)buf;
(void)count;
php_error_docref(NULL, E_WARNING,
"Write operation not supported for libarchive streams.");
return STREAM_ERR_RET;
}
static inline struct archive *nullable arch_from_stream(php_stream *stream)
{
zval *arch_zv = stream->abstract;
if (!arch_zv) {
php_error_docref(NULL, E_WARNING, "Stream already closed");
return NULL;
}
arch_object *obj = arch_object_from_zv(arch_zv);
struct archive *arch = obj->archive;
if (!arch) {
php_error_docref(NULL, E_WARNING,
"libarchive handle has been disposed of already");
}
return arch;
}
static stream_ret_t php_arch_ops_read(php_stream *stream, char *buf, size_t count)
{
struct archive *arch = arch_from_stream(stream);
if (!arch) {
return STREAM_ERR_RET;
}
if (UNEXPECTED(count == 0)) {
return 0;
}
la_ssize_t read = archive_read_data(arch, buf, count);
if (read < 0) {
php_error_docref(NULL, E_WARNING, "Error reading data: %s [%d]",
archive_error_string(arch), archive_errno(arch));
return STREAM_ERR_RET;
}
return (stream_ret_t)read;
}
static int php_arch_ops_close(php_stream *stream, int close_handle)
{
if (!close_handle) {
// should only happen if there was a cast
php_error_docref(NULL, E_WARNING, "Preserving handle not supported");
}
zval *arch = stream->abstract;
if (!arch) {
return EOF;
}
zval_ptr_dtor(arch);
stream->abstract = NULL;
return EOF;
}
static int php_arch_ops_flush(php_stream *stream)
{
(void)stream;
return 0;
}
static int php_arch_ops_seek(php_stream *stream, zend_off_t offset,
int whence, zend_off_t *newoffset)
{
struct archive *arch = arch_from_stream(stream);
if (!arch) {
return -1;
}
la_int64_t res = archive_seek_data(arch, offset, whence);
if (res < 0) {
php_error_docref(NULL, E_WARNING, "Error seeking in entry: %s [%d]",
archive_error_string(arch), archive_errno(arch));
return -1;
}
*newoffset = (zend_off_t)res;
return 0; // SUCCESS
}
static php_stream_ops php_stream_arch_ops = {
.write = php_arch_ops_write,
.read = php_arch_ops_read,
.close = php_arch_ops_close,
.flush = php_arch_ops_flush,
.label = "libarchive",
.seek = php_arch_ops_seek,
};
// TODO: stream should be invalidated once we move to the next entry
php_stream *php_stream_arch_cur_entry_stream(zval *nonnull arch_zv)
{
zval_add_ref(arch_zv);
php_stream *stream = php_stream_alloc(
&php_stream_arch_ops, arch_zv, NULL, "rb");
return stream;
}