1 /*
2 zip_source_window.c -- return part of lower source
3 Copyright (C) 2012-2015 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 window {
42 zip_uint64_t skip;
43 zip_uint64_t len;
44 zip_uint64_t left;
45 int e[2];
46 };
47
48 static zip_int64_t window_read(struct zip_source *, void *, void *,
49 zip_uint64_t, enum zip_source_cmd);
50
51
52
53 struct zip_source *
zip_source_window(struct zip * za,struct zip_source * src,zip_uint64_t start,zip_uint64_t len)54 zip_source_window(struct zip *za, struct zip_source *src, zip_uint64_t start, zip_uint64_t len)
55 {
56 struct window *ctx;
57
58 if (src == NULL) {
59 _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
60 return NULL;
61 }
62
63 if ((ctx=(struct window *)malloc(sizeof(*ctx))) == NULL) {
64 _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
65 return NULL;
66 }
67
68 ctx->skip = start;
69 ctx->len = len;
70 ctx->left = len;
71
72 return zip_source_layered(za, src, window_read, ctx);
73 }
74
75
76
77 static zip_int64_t
window_read(struct zip_source * src,void * _ctx,void * data,zip_uint64_t len,enum zip_source_cmd cmd)78 window_read(struct zip_source *src, void *_ctx, void *data,
79 zip_uint64_t len, enum zip_source_cmd cmd)
80 {
81 struct window *ctx;
82 zip_int64_t ret;
83 zip_uint64_t n, i;
84 char b[8192];
85
86 ctx = (struct window *)_ctx;
87
88 switch (cmd) {
89 case ZIP_SOURCE_OPEN:
90 for (n=0; n<ctx->skip; n+=(zip_uint64_t)ret) {
91 i = (ctx->skip-n > sizeof(b) ? sizeof(b) : ctx->skip-n);
92 if ((ret=zip_source_read(src, b, i)) < 0)
93 return ZIP_SOURCE_ERR_LOWER;
94 if (ret==0) {
95 ctx->e[0] = ZIP_ER_EOF;
96 ctx->e[1] = 0;
97 return -1;
98 }
99 }
100 return 0;
101
102 case ZIP_SOURCE_READ:
103 if (len > ctx->left)
104 len = ctx->left;
105
106 if (len == 0)
107 return 0;
108
109 if ((ret=zip_source_read(src, data, len)) < 0)
110 return ZIP_SOURCE_ERR_LOWER;
111
112 ctx->left -= (zip_uint64_t)ret;
113
114 if (ret == 0) {
115 if (ctx->left > 0) {
116 ctx->e[0] = ZIP_ER_EOF;
117 ctx->e[1] = 0;
118 return -1;
119 }
120 }
121 return ret;
122
123 case ZIP_SOURCE_CLOSE:
124 return 0;
125
126 case ZIP_SOURCE_STAT:
127 {
128 struct zip_stat *st;
129
130 st = (struct zip_stat *)data;
131
132 st->size = ctx->len;
133 st->valid |= ZIP_STAT_SIZE;
134 st->valid &= ~(ZIP_STAT_CRC|ZIP_STAT_COMP_SIZE);
135 }
136 return 0;
137
138 case ZIP_SOURCE_ERROR:
139 memcpy(data, ctx->e, sizeof(ctx->e));
140 return 0;
141
142 case ZIP_SOURCE_FREE:
143 free(ctx);
144 return 0;
145
146 default:
147 return -1;
148 }
149
150 }
151