Annotation of embedaddon/php/ext/zip/lib/zip_source_buffer.c, revision 1.1.1.1
1.1 misho 1: /*
2: zip_source_buffer.c -- create zip data source from buffer
3: Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
4:
5: This file is part of libzip, a library to manipulate ZIP archives.
6: The authors can be contacted at <libzip@nih.at>
7:
8: Redistribution and use in source and binary forms, with or without
9: modification, are permitted provided that the following conditions
10: are met:
11: 1. Redistributions of source code must retain the above copyright
12: notice, this list of conditions and the following disclaimer.
13: 2. Redistributions in binary form must reproduce the above copyright
14: notice, this list of conditions and the following disclaimer in
15: the documentation and/or other materials provided with the
16: distribution.
17: 3. The names of the authors may not be used to endorse or promote
18: products derived from this software without specific prior
19: written permission.
20:
21: THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22: OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25: DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27: GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29: IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30: OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31: IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 ssize_t read_data(void *state, void *data, size_t len,
48: enum zip_source_cmd cmd);
49:
50:
51:
52: ZIP_EXTERN(struct zip_source *)
53: zip_source_buffer(struct zip *za, const void *data, off_t len, int freep)
54: {
55: struct read_data *f;
56: struct zip_source *zs;
57:
58: if (za == NULL)
59: return NULL;
60:
61: if (len < 0 || (data == NULL && len > 0)) {
62: _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
63: return NULL;
64: }
65:
66: if ((f=(struct read_data *)malloc(sizeof(*f))) == NULL) {
67: _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
68: return NULL;
69: }
70:
71: f->data = (const char *)data;
72: f->end = ((const char *)data)+len;
73: f->freep = freep;
74: f->mtime = time(NULL);
75:
76: if ((zs=zip_source_function(za, read_data, f)) == NULL) {
77: free(f);
78: return NULL;
79: }
80:
81: return zs;
82: }
83:
84:
85:
86: static ssize_t
87: read_data(void *state, void *data, size_t len, enum zip_source_cmd cmd)
88: {
89: struct read_data *z;
90: char *buf;
91: size_t n;
92:
93: z = (struct read_data *)state;
94: buf = (char *)data;
95:
96: switch (cmd) {
97: case ZIP_SOURCE_OPEN:
98: z->buf = z->data;
99: return 0;
100:
101: case ZIP_SOURCE_READ:
102: n = z->end - z->buf;
103: if (n > len)
104: n = len;
105:
106: if (n) {
107: memcpy(buf, z->buf, n);
108: z->buf += n;
109: }
110:
111: return n;
112:
113: case ZIP_SOURCE_CLOSE:
114: return 0;
115:
116: case ZIP_SOURCE_STAT:
117: {
118: struct zip_stat *st;
119:
120: if (len < sizeof(*st))
121: return -1;
122:
123: st = (struct zip_stat *)data;
124:
125: zip_stat_init(st);
126: st->mtime = z->mtime;
127: st->size = z->end - z->data;
128:
129: return sizeof(*st);
130: }
131:
132: case ZIP_SOURCE_ERROR:
133: {
134: int *e;
135:
136: if (len < sizeof(int)*2)
137: return -1;
138:
139: e = (int *)data;
140: e[0] = e[1] = 0;
141: }
142: return sizeof(int)*2;
143:
144: case ZIP_SOURCE_FREE:
145: if (z->freep) {
146: free((void *)z->data);
147: z->data = NULL;
148: }
149: free(z);
150: return 0;
151:
152: default:
153: ;
154: }
155:
156: return -1;
157: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>