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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
zip_source_filep.c -- create data source from FILE *
Copyright (C) 1999-2009 Dieter Baron and Thomas Klausner
This file is part of libzip, a library to manipulate ZIP archives.
The authors can be contacted at <libzip@nih.at>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zipint.h"
struct read_file {
char *fname; /* name of file to copy from */
FILE *f; /* file to copy from */
int closep; /* close f */
struct zip_stat st; /* stat information passed in */
zip_uint64_t off; /* start offset of */
zip_int64_t len; /* length of data to copy */
zip_int64_t remain; /* bytes remaining to be copied */
int e[2]; /* error codes */
};
static zip_int64_t read_file(void *state, void *data, zip_uint64_t len,
enum zip_source_cmd cmd);
ZIP_EXTERN struct zip_source *
zip_source_filep(struct zip *za, FILE *file, zip_uint64_t start,
zip_int64_t len)
{
if (za == NULL)
return NULL;
if (file == NULL || len < -1) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
return _zip_source_file_or_p(za, NULL, file, start, len, 1, NULL);
}
struct zip_source *
_zip_source_file_or_p(struct zip *za, const char *fname, FILE *file,
zip_uint64_t start, zip_int64_t len, int closep,
const struct zip_stat *st)
{
struct read_file *f;
struct zip_source *zs;
if (file == NULL && fname == NULL) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
if ((f=(struct read_file *)malloc(sizeof(struct read_file))) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
f->fname = NULL;
if (fname) {
if ((f->fname=strdup(fname)) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
free(f);
return NULL;
}
}
f->f = file;
f->off = start;
f->len = (len ? len : -1);
f->closep = f->fname ? 1 : closep;
if (st)
memcpy(&f->st, st, sizeof(f->st));
else
zip_stat_init(&f->st);
if ((zs=zip_source_function(za, read_file, f)) == NULL) {
free(f);
return NULL;
}
return zs;
}
static zip_int64_t
read_file(void *state, void *data, zip_uint64_t len, enum zip_source_cmd cmd)
{
struct read_file *z;
char *buf;
zip_uint64_t n;
size_t i;
z = (struct read_file *)state;
buf = (char *)data;
switch (cmd) {
case ZIP_SOURCE_OPEN:
if (z->fname) {
if ((z->f=fopen(z->fname, "rb")) == NULL) {
z->e[0] = ZIP_ER_OPEN;
z->e[1] = errno;
return -1;
}
}
if (z->closep && z->off > 0) {
if (fseeko(z->f, (off_t)z->off, SEEK_SET) < 0) {
z->e[0] = ZIP_ER_SEEK;
z->e[1] = errno;
return -1;
}
}
z->remain = z->len;
return 0;
case ZIP_SOURCE_READ:
if (z->remain != -1)
n = len > (zip_uint64_t)z->remain ? (zip_uint64_t)z->remain : len;
else
n = len;
if (n > SIZE_MAX)
n = SIZE_MAX;
if (!z->closep) {
/* we might share this file with others, so let's be safe */
if (fseeko(z->f, (off_t)(z->off + (zip_uint64_t)(z->len-z->remain)), SEEK_SET) < 0) {
z->e[0] = ZIP_ER_SEEK;
z->e[1] = errno;
return -1;
}
}
if ((i=fread(buf, 1, (size_t)n, z->f)) == 0) {
if (ferror(z->f)) {
z->e[0] = ZIP_ER_READ;
z->e[1] = errno;
return -1;
}
}
if (z->remain != -1)
z->remain -= i;
return (zip_int64_t)i;
case ZIP_SOURCE_CLOSE:
if (z->fname) {
fclose(z->f);
z->f = NULL;
}
return 0;
case ZIP_SOURCE_STAT:
{
if (len < sizeof(z->st))
return -1;
if (z->st.valid != 0)
memcpy(data, &z->st, sizeof(z->st));
else {
struct zip_stat *st;
struct stat fst;
int err;
if (z->f)
err = fstat(fileno(z->f), &fst);
else
err = stat(z->fname, &fst);
if (err != 0) {
z->e[0] = ZIP_ER_READ; /* best match */
z->e[1] = errno;
return -1;
}
st = (struct zip_stat *)data;
zip_stat_init(st);
st->mtime = fst.st_mtime;
st->valid |= ZIP_STAT_MTIME;
if (z->len != -1) {
st->size = (zip_uint64_t)z->len;
st->valid |= ZIP_STAT_SIZE;
}
else if ((fst.st_mode&S_IFMT) == S_IFREG) {
st->size = (zip_uint64_t)fst.st_size;
st->valid |= ZIP_STAT_SIZE;
}
}
return sizeof(z->st);
}
case ZIP_SOURCE_ERROR:
if (len < sizeof(int)*2)
return -1;
memcpy(data, z->e, sizeof(int)*2);
return sizeof(int)*2;
case ZIP_SOURCE_FREE:
free(z->fname);
if (z->closep && z->f)
fclose(z->f);
free(z);
return 0;
default:
;
}
return -1;
}
|