This source file includes following definitions.
- zip_source_buffer
- read_data
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 #include <stdlib.h>
37 #include <string.h>
38
39 #include "zipint.h"
40
41 struct read_data {
42 const char *buf, *data, *end;
43 time_t mtime;
44 int freep;
45 };
46
47 static zip_int64_t read_data(void *, void *, zip_uint64_t, enum zip_source_cmd);
48
49
50
51 ZIP_EXTERN struct zip_source *
52 zip_source_buffer(struct zip *za, const void *data, zip_uint64_t len, int freep)
53 {
54 struct read_data *f;
55 struct zip_source *zs;
56
57 if (za == NULL)
58 return NULL;
59
60 if (data == NULL && len > 0) {
61 _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
62 return NULL;
63 }
64
65 if ((f=(struct read_data *)malloc(sizeof(*f))) == NULL) {
66 _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
67 return NULL;
68 }
69
70 f->data = (const char *)data;
71 f->end = ((const char *)data)+len;
72 f->freep = freep;
73 f->mtime = time(NULL);
74
75 if ((zs=zip_source_function(za, read_data, f)) == NULL) {
76 free(f);
77 return NULL;
78 }
79
80 return zs;
81 }
82
83
84
85 static zip_int64_t
86 read_data(void *state, void *data, zip_uint64_t len, enum zip_source_cmd cmd)
87 {
88 struct read_data *z;
89 char *buf;
90 zip_uint64_t n;
91
92 z = (struct read_data *)state;
93 buf = (char *)data;
94
95 switch (cmd) {
96 case ZIP_SOURCE_OPEN:
97 z->buf = z->data;
98 return 0;
99
100 case ZIP_SOURCE_READ:
101 n = (zip_uint64_t)(z->end - z->buf);
102 if (n > len)
103 n = len;
104
105 if (n) {
106 memcpy(buf, z->buf, n);
107 z->buf += n;
108 }
109
110 return (zip_int64_t)n;
111
112 case ZIP_SOURCE_CLOSE:
113 return 0;
114
115 case ZIP_SOURCE_STAT:
116 {
117 struct zip_stat *st;
118
119 if (len < sizeof(*st))
120 return -1;
121
122 st = (struct zip_stat *)data;
123
124 zip_stat_init(st);
125 st->mtime = z->mtime;
126 st->size = (zip_uint64_t)(z->end - z->data);
127 st->comp_size = st->size;
128 st->comp_method = ZIP_CM_STORE;
129 st->encryption_method = ZIP_EM_NONE;
130 st->valid = ZIP_STAT_MTIME|ZIP_STAT_SIZE|ZIP_STAT_COMP_SIZE
131 |ZIP_STAT_COMP_METHOD|ZIP_STAT_ENCRYPTION_METHOD;
132
133 return sizeof(*st);
134 }
135
136 case ZIP_SOURCE_ERROR:
137 {
138 int *e;
139
140 if (len < sizeof(int)*2)
141 return -1;
142
143 e = (int *)data;
144 e[0] = e[1] = 0;
145 }
146 return sizeof(int)*2;
147
148 case ZIP_SOURCE_FREE:
149 if (z->freep) {
150 free((void *)z->data);
151 z->data = NULL;
152 }
153 free(z);
154 return 0;
155
156 default:
157 ;
158 }
159
160 return -1;
161 }