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 /* XXX: return error if (len > ZIP_INT64_MAX) */
102
103 n = z->end - z->buf;
104 if (n > len)
105 n = len;
106
107 if (n) {
108 memcpy(buf, z->buf, n);
109 z->buf += n;
110 }
111
112 return n;
113
114 case ZIP_SOURCE_CLOSE:
115 return 0;
116
117 case ZIP_SOURCE_STAT:
118 {
119 struct zip_stat *st;
120
121 if (len < sizeof(*st))
122 return -1;
123
124 st = (struct zip_stat *)data;
125
126 zip_stat_init(st);
127 st->mtime = z->mtime;
128 st->size = z->end - z->data;
129 st->comp_size = st->size;
130 st->comp_method = ZIP_CM_STORE;
131 st->encryption_method = ZIP_EM_NONE;
132 st->valid = ZIP_STAT_MTIME|ZIP_STAT_SIZE|ZIP_STAT_COMP_SIZE
133 |ZIP_STAT_COMP_METHOD|ZIP_STAT_ENCRYPTION_METHOD;
134
135 return sizeof(*st);
136 }
137
138 case ZIP_SOURCE_ERROR:
139 {
140 int *e;
141
142 if (len < sizeof(int)*2)
143 return -1;
144
145 e = (int *)data;
146 e[0] = e[1] = 0;
147 }
148 return sizeof(int)*2;
149
150 case ZIP_SOURCE_FREE:
151 if (z->freep) {
152 free((void *)z->data);
153 z->data = NULL;
154 }
155 free(z);
156 return 0;
157
158 default:
159 ;
160 }
161
162 return -1;
163 }
164