xref: /PHP-5.6/ext/zip/lib/zip_source_buffer.c (revision 5dc37b35)
1 /*
2   zip_source_buffer.c -- create zip data source from buffer
3   Copyright (C) 1999-2009 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 zip_int64_t read_data(void *, void *, zip_uint64_t, enum zip_source_cmd);
48 
49 
50 
51 ZIP_EXTERN struct zip_source *
zip_source_buffer(struct zip * za,const void * data,zip_uint64_t len,int freep)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
read_data(void * state,void * data,zip_uint64_t len,enum zip_source_cmd cmd)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 }
162