Debian Codegraph @

matio-1.5.30/src/inflate.c

raw symbols SCIP
1 /** @file inflate.c
2 * @brief Functions to inflate data/tags
3 * @ingroup MAT
4 */
5 /*
6 * Copyright (c) 2015-2026, The matio contributors
7 * Copyright (c) 2005-2014, Christopher C. Hulbert
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice, this
14 * list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "matio_private.h"
33 #include <stdlib.h>
34 #include <string.h>
35 #include <limits.h>
36
37 #if HAVE_ZLIB
38
39 /** @cond mat_devman */
40
41 /** @brief Inflate the data until @c nBytes of uncompressed data has been
42 * inflated
43 *
44 * @ingroup mat_internal
45 * @param mat Pointer to the MAT file
46 * @param z zlib compression stream
47 * @param nBytes Number of uncompressed bytes to skip
48 * @param[out] bytesread Number of bytes read from the file
49 * @retval 0 on success
50
51 */
52 int
53 InflateSkip(mat_t *mat, z_streamp z, int nBytes, size_t *bytesread)
54 {
55 mat_uint8_t comp_buf[READ_BLOCK_SIZE], uncomp_buf[READ_BLOCK_SIZE];
56 int n, err = MATIO_E_NO_ERROR, cnt = 0;
57
58 if ( nBytes < 1 )
59 return MATIO_E_NO_ERROR;
60
61 n = nBytes < READ_BLOCK_SIZE ? nBytes : READ_BLOCK_SIZE;
62 if ( !z->avail_in ) {
63 size_t nbytes = fread(comp_buf, 1, n, (FILE *)mat->fp);
64 if ( 0 == nbytes ) {
65 return err;
66 }
67 if ( NULL != bytesread ) {
68 *bytesread += nbytes;
69 }
70 z->avail_in = (uInt)nbytes;
71 z->next_in = comp_buf;
72 }
73 z->avail_out = n;
74 z->next_out = uncomp_buf;
75 err = inflate(z, Z_NO_FLUSH);
76 if ( err == Z_STREAM_END ) {
77 z->next_out = NULL;
78 return MATIO_E_NO_ERROR;
79 } else if ( err != Z_OK ) {
80 z->next_out = NULL;
81 Mat_Critical("InflateSkip: inflate returned %s",
82 zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
83 return MATIO_E_FILE_FORMAT_VIOLATION;
84 } else {
85 err = MATIO_E_NO_ERROR;
86 }
87 if ( !z->avail_out ) {
88 cnt += n;
89 n = nBytes - cnt;
90 if ( n > READ_BLOCK_SIZE ) {
91 n = READ_BLOCK_SIZE;
92 }
93 z->avail_out = n;
94 z->next_out = uncomp_buf;
95 }
96 while ( cnt < nBytes ) {
97 if ( !z->avail_in ) {
98 size_t nbytes = fread(comp_buf, 1, n, (FILE *)mat->fp);
99 if ( 0 == nbytes ) {
100 break;
101 }
102 if ( NULL != bytesread ) {
103 *bytesread += nbytes;
104 }
105 z->avail_in = (uInt)nbytes;
106 z->next_in = comp_buf;
107 }
108 err = inflate(z, Z_NO_FLUSH);
109 if ( err == Z_STREAM_END ) {
110 err = MATIO_E_NO_ERROR;
111 break;
112 } else if ( err != Z_OK ) {
113 const char *errMsg = zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err);
114 err = MATIO_E_FILE_FORMAT_VIOLATION;
115 Mat_Critical("InflateSkip: inflate returned %s", errMsg);
116 break;
117 } else {
118 err = MATIO_E_NO_ERROR;
119 }
120 if ( !z->avail_out ) {
121 cnt += n;
122 n = nBytes - cnt;
123 if ( n > READ_BLOCK_SIZE ) {
124 n = READ_BLOCK_SIZE;
125 }
126 z->avail_out = n;
127 z->next_out = uncomp_buf;
128 }
129 }
130
131 if ( z->avail_in ) {
132 const mat_off_t offset = -(mat_off_t)z->avail_in;
133 (void)fseeko((FILE *)mat->fp, offset, SEEK_CUR);
134 if ( NULL != bytesread ) {
135 *bytesread -= z->avail_in;
136 }
137 z->avail_in = 0;
138 }
139
140 z->next_out = NULL;
141
142 return err;
143 }
144
145 /** @brief Inflate the data until @c len elements of compressed data with data
146 * type @c data_type has been inflated
147 *
148 * @ingroup mat_internal
149 * @param mat Pointer to the MAT file
150 * @param z zlib compression stream
151 * @param data_type Data type (matio_types enumerations)
152 * @param len Number of elements of datatype @c data_type to skip
153 * @retval 0 on success
154
155 */
156 int
157 InflateSkipData(mat_t *mat, z_streamp z, enum matio_types data_type, int len)
158 {
159 if ( mat == NULL || z == NULL || len < 1 )
160 return MATIO_E_BAD_ARGUMENT;
161
162 switch ( data_type ) {
163 case MAT_T_UTF8:
164 case MAT_T_UTF16:
165 case MAT_T_UTF32:
166 return MATIO_E_OPERATION_NOT_SUPPORTED;
167 default:
168 break;
169 }
170
171 return InflateSkip(mat, z, (unsigned int)Mat_SizeOf(data_type) * len, NULL);
172 }
173
174 /** @brief Inflates the dimensions tag and the dimensions data
175 *
176 * @c buf must hold at least (8+4*rank) bytes where rank is the number of
177 * dimensions. If the end of the dimensions data is not aligned on an 8-byte
178 * boundary, this function eats up those bytes and stores then in @c buf.
179 * @ingroup mat_internal
180 * @param mat Pointer to the MAT file
181 * @param z zlib compression stream
182 * @param buf Pointer to store the dimensions flag and data
183 * @param nBytes Size of buf in bytes
184 * @param dims Output buffer to be allocated if (8+4*rank) > nBytes
185 * @param[out] bytesread Number of bytes read from the file
186 * @retval 0 on success
187
188 */
189 int
190 InflateRankDims(mat_t *mat, z_streamp z, void *buf, size_t nBytes, mat_uint32_t **dims,
191 size_t *bytesread)
192 {
193 mat_uint32_t tag[2];
194 mat_uint32_t rank, i;
195 int err;
196 size_t nbytes = 0;
197
198 if ( buf == NULL )
199 return MATIO_E_BAD_ARGUMENT;
200
201 err = Inflate(mat, z, buf, 8, bytesread);
202 if ( err ) {
203 return err;
204 }
205 tag[0] = *(mat_uint32_t *)buf;
206 tag[1] = *((mat_uint32_t *)buf + 1);
207 if ( mat->byteswap ) {
208 Mat_uint32Swap(tag);
209 Mat_uint32Swap(tag + 1);
210 }
211 if ( (tag[0] & 0x0000ffff) != MAT_T_INT32 ) {
212 Mat_Critical("InflateRankDims: Reading dimensions expected type MAT_T_INT32");
213 return MATIO_E_FILE_FORMAT_VIOLATION;
214 }
215 rank = tag[1];
216 if ( rank % 8 != 0 )
217 i = 8 - (rank % 8);
218 else
219 i = 0;
220
221 if ( rank > INT_MAX - i - 2 ) {
222 Mat_Critical("InflateRankDims: Reading dimensions expected rank in integer range");
223 return MATIO_E_FILE_FORMAT_VIOLATION;
224 }
225 rank += i;
226
227 err = Mul(&nbytes, rank + 2, sizeof(mat_uint32_t));
228 if ( err ) {
229 Mat_Critical("Integer multiplication overflow");
230 return err;
231 }
232
233 if ( nbytes <= nBytes ) {
234 err = Inflate(mat, z, (mat_uint32_t *)buf + 2, rank, bytesread);
235 } else {
236 /* Cannot use too small buf, but can allocate output buffer dims */
237 *dims = (mat_uint32_t *)calloc(rank, sizeof(mat_uint32_t));
238 if ( NULL != *dims ) {
239 err = Inflate(mat, z, *dims, rank, bytesread);
240 } else {
241 *((mat_uint32_t *)buf + 1) = 0;
242 Mat_Critical("Error allocating memory for dims");
243 return MATIO_E_OUT_OF_MEMORY;
244 }
245 }
246
247 return err;
248 }
249
250 /** @brief Inflates the data
251 *
252 * buf must hold at least @c nBytes bytes
253 * @ingroup mat_internal
254 * @param mat Pointer to the MAT file
255 * @param z zlib compression stream
256 * @param buf Pointer to store the uncompressed data
257 * @param nBytes Number of uncompressed bytes to inflate
258 * @param[out] bytesread Number of bytes read from the file
259 * @retval 0 on success
260
261 */
262 int
263 Inflate(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes, size_t *bytesread)
264 {
265 mat_uint8_t comp_buf[4];
266 int err = MATIO_E_NO_ERROR;
267
268 if ( buf == NULL )
269 return MATIO_E_BAD_ARGUMENT;
270
271 if ( !z->avail_in ) {
272 size_t nbytes = fread(comp_buf, 1, 1, (FILE *)mat->fp);
273 if ( 0 == nbytes ) {
274 return err;
275 }
276 if ( NULL != bytesread ) {
277 *bytesread += nbytes;
278 }
279 z->avail_in = (uInt)nbytes;
280 z->next_in = comp_buf;
281 }
282 z->avail_out = nBytes;
283 z->next_out = ZLIB_BYTE_PTR(buf);
284 err = inflate(z, Z_NO_FLUSH);
285 if ( err != Z_OK ) {
286 Mat_Critical("Inflate: inflate returned %s",
287 zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
288 return MATIO_E_FILE_FORMAT_VIOLATION;
289 } else {
290 err = MATIO_E_NO_ERROR;
291 }
292 while ( z->avail_out && !z->avail_in ) {
293 size_t nbytes = fread(comp_buf, 1, 1, (FILE *)mat->fp);
294 if ( 0 == nbytes ) {
295 break;
296 }
297 if ( NULL != bytesread ) {
298 *bytesread += nbytes;
299 }
300 z->avail_in = (uInt)nbytes;
301 z->next_in = comp_buf;
302 err = inflate(z, Z_NO_FLUSH);
303 if ( err != Z_OK ) {
304 Mat_Critical("Inflate: inflate returned %s",
305 zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
306 return MATIO_E_FILE_FORMAT_VIOLATION;
307 } else {
308 err = MATIO_E_NO_ERROR;
309 }
310 }
311
312 if ( z->avail_in ) {
313 const mat_off_t offset = -(mat_off_t)z->avail_in;
314 (void)fseeko((FILE *)mat->fp, offset, SEEK_CUR);
315 if ( NULL != bytesread ) {
316 *bytesread -= z->avail_in;
317 }
318 z->avail_in = 0;
319 }
320
321 if ( z->avail_out && feof((FILE *)mat->fp) ) {
322 Mat_Warning(
323 "Unexpected end-of-file: "
324 "Processed %u bytes, expected %u bytes",
325 nBytes - z->avail_out, nBytes);
326 memset(buf, 0, nBytes);
327 }
328
329 return err;
330 }
331
332 /** @brief Inflates the data in blocks
333 *
334 * buf must hold at least @c nBytes bytes
335 * @ingroup mat_internal
336 * @param mat Pointer to the MAT file
337 * @param z zlib compression stream
338 * @param buf Pointer to store the uncompressed data
339 * @param nBytes Number of uncompressed bytes to inflate
340 * @retval 0 on success
341
342 */
343 int
344 InflateData(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes)
345 {
346 mat_uint8_t comp_buf[READ_BLOCK_SIZE];
347 int err = MATIO_E_NO_ERROR;
348 unsigned int n;
349 size_t bytesread = 0;
350
351 if ( buf == NULL )
352 return MATIO_E_BAD_ARGUMENT;
353 if ( nBytes == 0 ) {
354 return MATIO_E_NO_ERROR;
355 }
356
357 n = nBytes < READ_BLOCK_SIZE ? nBytes : READ_BLOCK_SIZE;
358 if ( !z->avail_in ) {
359 size_t nbytes = fread(comp_buf, 1, n, (FILE *)mat->fp);
360 if ( 0 == nbytes ) {
361 return err;
362 }
363 bytesread += nbytes;
364 z->avail_in = (uInt)nbytes;
365 z->next_in = comp_buf;
366 }
367 z->avail_out = nBytes;
368 z->next_out = ZLIB_BYTE_PTR(buf);
369 err = inflate(z, Z_NO_FLUSH);
370 if ( err == Z_STREAM_END ) {
371 return MATIO_E_NO_ERROR;
372 } else if ( err != Z_OK ) {
373 Mat_Critical("InflateData: inflate returned %s",
374 zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
375 return MATIO_E_FAIL_TO_IDENTIFY;
376 } else {
377 err = MATIO_E_NO_ERROR;
378 }
379 while ( z->avail_out && !z->avail_in ) {
380 size_t nbytes;
381 if ( nBytes > READ_BLOCK_SIZE + bytesread ) {
382 nbytes = fread(comp_buf, 1, READ_BLOCK_SIZE, (FILE *)mat->fp);
383 } else if ( nBytes < 1 + bytesread ) { /* Read a byte at a time */
384 nbytes = fread(comp_buf, 1, 1, (FILE *)mat->fp);
385 } else {
386 nbytes = fread(comp_buf, 1, nBytes - bytesread, (FILE *)mat->fp);
387 }
388 if ( 0 == nbytes ) {
389 break;
390 }
391 bytesread += nbytes;
392 z->avail_in = (uInt)nbytes;
393 z->next_in = comp_buf;
394 err = inflate(z, Z_NO_FLUSH);
395 if ( err == Z_STREAM_END ) {
396 err = MATIO_E_NO_ERROR;
397 break;
398 } else if ( err != Z_OK ) {
399 const char *errMsg = zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err);
400 err = MATIO_E_FAIL_TO_IDENTIFY;
401 Mat_Critical("InflateData: inflate returned %s", errMsg);
402 break;
403 } else {
404 err = MATIO_E_NO_ERROR;
405 }
406 }
407
408 if ( z->avail_in ) {
409 const mat_off_t offset = -(mat_off_t)z->avail_in;
410 (void)fseeko((FILE *)mat->fp, offset, SEEK_CUR);
411 /* bytesread -= z->avail_in; */
412 z->avail_in = 0;
413 }
414
415 if ( z->avail_out && feof((FILE *)mat->fp) ) {
416 Mat_Warning("InflateData: Read beyond EOF error: Processed %u bytes, expected %u bytes",
417 nBytes - z->avail_out, nBytes);
418 memset(buf, 0, nBytes);
419 }
420
421 return err;
422 }
423
424 /** @endcond */
425
426 #endif