Debian Codegraph @

matio-1.5.0/src/inflate.c

raw
1 /** @file inflate.c
2 * @brief Functions to inflate data/tags
3 * @ingroup MAT
4 */
5 /*
6 * Copyright (C) 2005-2011 Christopher C. Hulbert
7 *
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,
14 * this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY CHRISTOPHER C. HULBERT ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER C. HULBERT OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 #include <stdlib.h>
33 #include "matio_private.h"
34
35 #if HAVE_ZLIB
36
37 /** @cond mat_devman */
38
39 /** @brief Inflate the data until @c nbytes of uncompressed data has been
40 * inflated
41 *
42 * @ingroup mat_internal
43 * @param mat Pointer to the MAT file
44 * @param z zlib compression stream
45 * @param nbytes Number of uncompressed bytes to skip
46 * @return Number of bytes read from the file
47 */
48 int
49 InflateSkip(mat_t *mat, z_stream *z, int nbytes)
50 {
51 mat_uint8_t comp_buf[512],uncomp_buf[512];
52 int bytesread = 0, n,err, cnt = 0;
53
54 if ( nbytes < 1 )
55 return 0;
56
57 n = (nbytes<512) ? nbytes : 512;
58 if ( !z->avail_in ) {
59 z->next_in = comp_buf;
60 z->avail_in += fread(comp_buf,1,n,mat->fp);
61 bytesread += z->avail_in;
62 }
63 z->avail_out = n;
64 z->next_out = uncomp_buf;
65 err = inflate(z,Z_FULL_FLUSH);
66 if ( err == Z_STREAM_END ) {
67 return bytesread;
68 } else if ( err != Z_OK ) {
69 Mat_Critical("InflateSkip: inflate returned %d",err);
70 return bytesread;
71 }
72 if ( !z->avail_out ) {
73 cnt += n;
74 n = ((nbytes-cnt)<512) ? nbytes-cnt : 512;
75 z->avail_out = n;
76 z->next_out = uncomp_buf;
77 }
78 while ( cnt < nbytes ) {
79 if ( !z->avail_in ) {
80 z->next_in = comp_buf;
81 z->avail_in += fread(comp_buf,1,n,mat->fp);
82 bytesread += z->avail_in;
83 }
84 err = inflate(z,Z_FULL_FLUSH);
85 if ( err == Z_STREAM_END ) {
86 break;
87 } else if ( err != Z_OK ) {
88 Mat_Critical("InflateSkip: inflate returned %d",err);
89 break;
90 }
91 if ( !z->avail_out ) {
92 cnt += n;
93 n = ((nbytes-cnt)<512) ? nbytes-cnt : 512;
94 z->avail_out = n;
95 z->next_out = uncomp_buf;
96 }
97 }
98
99 if ( z->avail_in ) {
100 long offset = -(long)z->avail_in;
101 fseek(mat->fp,offset,SEEK_CUR);
102 bytesread -= z->avail_in;
103 z->avail_in = 0;
104 }
105
106 return bytesread;
107 }
108
109 /** @brief Inflate the data until @c nbytes of compressed data has been
110 * inflated
111 *
112 * @ingroup mat_internal
113 * @param mat Pointer to the MAT file
114 * @param z zlib compression stream
115 * @param nbytes Number of uncompressed bytes to skip
116 * @return Number of bytes read from the file
117 */
118 int
119 InflateSkip2(mat_t *mat, matvar_t *matvar, int nbytes)
120 {
121 mat_uint8_t comp_buf[32],uncomp_buf[32];
122 int bytesread = 0, err, cnt = 0;
123
124 if ( !matvar->internal->z->avail_in ) {
125 matvar->internal->z->avail_in = 1;
126 matvar->internal->z->next_in = comp_buf;
127 bytesread += fread(comp_buf,1,1,mat->fp);
128 }
129 matvar->internal->z->avail_out = 1;
130 matvar->internal->z->next_out = uncomp_buf;
131 err = inflate(matvar->internal->z,Z_NO_FLUSH);
132 if ( err != Z_OK ) {
133 Mat_Critical("InflateSkip2: %s - inflate returned %d",matvar->name,err);
134 return bytesread;
135 }
136 if ( !matvar->internal->z->avail_out ) {
137 matvar->internal->z->avail_out = 1;
138 matvar->internal->z->next_out = uncomp_buf;
139 }
140 while ( cnt < nbytes ) {
141 if ( !matvar->internal->z->avail_in ) {
142 matvar->internal->z->avail_in = 1;
143 matvar->internal->z->next_in = comp_buf;
144 bytesread += fread(comp_buf,1,1,mat->fp);
145 cnt++;
146 }
147 err = inflate(matvar->internal->z,Z_NO_FLUSH);
148 if ( err != Z_OK ) {
149 Mat_Critical("InflateSkip2: %s - inflate returned %d",matvar->name,err);
150 return bytesread;
151 }
152 if ( !matvar->internal->z->avail_out ) {
153 matvar->internal->z->avail_out = 1;
154 matvar->internal->z->next_out = uncomp_buf;
155 }
156 }
157
158 if ( matvar->internal->z->avail_in ) {
159 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
160 bytesread -= matvar->internal->z->avail_in;
161 matvar->internal->z->avail_in = 0;
162 }
163
164 return bytesread;
165 }
166
167 /** @brief Inflate the data until @c len elements of compressed data with data
168 * type @c data_type has been inflated
169 *
170 * @ingroup mat_internal
171 * @param mat Pointer to the MAT file
172 * @param z zlib compression stream
173 * @param data_type Data type (matio_types enumerations)
174 * @param len Number of elements of datatype @c data_type to skip
175 * @return Number of bytes read from the file
176 */
177 int
178 InflateSkipData(mat_t *mat,z_stream *z,enum matio_types data_type,int len)
179 {
180 int data_size = 0;
181
182 if ( (mat == NULL) || (z == NULL) )
183 return 0;
184 else if ( len < 1 )
185 return 0;
186
187 switch ( data_type ) {
188 case MAT_T_DOUBLE:
189 data_size = sizeof(double);
190 break;
191 case MAT_T_SINGLE:
192 data_size = sizeof(float);
193 break;
194 #ifdef HAVE_MAT_INT64_T
195 case MAT_T_INT64:
196 data_size = sizeof(mat_int64_t);
197 break;
198 #endif /* HAVE_MAT_INT64_T */
199 #ifdef HAVE_MAT_UINT64_T
200 case MAT_T_UINT64:
201 data_size = sizeof(mat_uint64_t);
202 break;
203 #endif /* HAVE_MAT_UINT64_T */
204 case MAT_T_INT32:
205 data_size = sizeof(mat_int32_t);
206 break;
207 case MAT_T_UINT32:
208 data_size = sizeof(mat_uint32_t);
209 break;
210 case MAT_T_INT16:
211 data_size = sizeof(mat_int16_t);
212 break;
213 case MAT_T_UINT16:
214 data_size = sizeof(mat_uint16_t);
215 break;
216 case MAT_T_UINT8:
217 data_size = sizeof(mat_uint8_t);
218 break;
219 case MAT_T_INT8:
220 data_size = sizeof(mat_int8_t);
221 break;
222 }
223 InflateSkip(mat,z,len*data_size);
224 return len;
225 }
226
227 /** @brief Inflates the variable's tag.
228 *
229 * @c buf must hold at least 8 bytes
230 * @ingroup mat_internal
231 * @param mat Pointer to the MAT file
232 * @param matvar Pointer to the MAT variable
233 * @param buf Pointer to store the 8-byte variable tag
234 * @return Number of bytes read from the file
235 */
236 int
237 InflateVarTag(mat_t *mat, matvar_t *matvar, void *buf)
238 {
239 mat_uint8_t comp_buf[32];
240 int bytesread = 0, err;
241
242 if (buf == NULL)
243 return 0;
244
245 if ( !matvar->internal->z->avail_in ) {
246 matvar->internal->z->avail_in = 1;
247 matvar->internal->z->next_in = comp_buf;
248 bytesread += fread(comp_buf,1,1,mat->fp);
249 }
250 matvar->internal->z->avail_out = 8;
251 matvar->internal->z->next_out = buf;
252 err = inflate(matvar->internal->z,Z_NO_FLUSH);
253 if ( err != Z_OK ) {
254 Mat_Critical("InflateVarTag: inflate returned %d",err);
255 return bytesread;
256 }
257 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
258 matvar->internal->z->avail_in = 1;
259 matvar->internal->z->next_in = comp_buf;
260 bytesread += fread(comp_buf,1,1,mat->fp);
261 err = inflate(matvar->internal->z,Z_NO_FLUSH);
262 if ( err != Z_OK ) {
263 Mat_Critical("InflateVarTag: inflate returned %d",err);
264 return bytesread;
265 }
266 }
267
268 if ( matvar->internal->z->avail_in ) {
269 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
270 bytesread -= matvar->internal->z->avail_in;
271 matvar->internal->z->avail_in = 0;
272 }
273
274 return bytesread;
275 }
276
277 /** @brief Inflates the Array Flags Tag and the Array Flags data.
278 *
279 * @c buf must hold at least 16 bytes
280 * @ingroup mat_internal
281 * @param mat Pointer to the MAT file
282 * @param matvar Pointer to the MAT variable
283 * @param buf Pointer to store the 16-byte array flags tag and data
284 * @return Number of bytes read from the file
285 */
286 int
287 InflateArrayFlags(mat_t *mat, matvar_t *matvar, void *buf)
288 {
289 mat_uint8_t comp_buf[32];
290 int bytesread = 0, err;
291
292 if (buf == NULL) return 0;
293
294 if ( !matvar->internal->z->avail_in ) {
295 matvar->internal->z->avail_in = 1;
296 matvar->internal->z->next_in = comp_buf;
297 bytesread += fread(comp_buf,1,1,mat->fp);
298 }
299 matvar->internal->z->avail_out = 16;
300 matvar->internal->z->next_out = buf;
301 err = inflate(matvar->internal->z,Z_NO_FLUSH);
302 if ( err != Z_OK ) {
303 Mat_Critical("InflateArrayFlags: inflate returned %d",err);
304 return bytesread;
305 }
306 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
307 matvar->internal->z->avail_in = 1;
308 matvar->internal->z->next_in = comp_buf;
309 bytesread += fread(comp_buf,1,1,mat->fp);
310 err = inflate(matvar->internal->z,Z_NO_FLUSH);
311 if ( err != Z_OK ) {
312 Mat_Critical("InflateArrayFlags: inflate returned %d",err);
313 return bytesread;
314 }
315 }
316
317 if ( matvar->internal->z->avail_in ) {
318 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
319 bytesread -= matvar->internal->z->avail_in;
320 matvar->internal->z->avail_in = 0;
321 }
322
323 return bytesread;
324 }
325
326 /** @brief Inflates the dimensions tag and the dimensions data
327 *
328 * @c buf must hold at least (8+4*rank) bytes where rank is the number of
329 * dimensions. If the end of the dimensions data is not aligned on an 8-byte
330 * boundary, this function eats up those bytes and stores then in @c buf.
331 * @ingroup mat_internal
332 * @param mat Pointer to the MAT file
333 * @param matvar Pointer to the MAT variable
334 * @param buf Pointer to store the dimensions flag and data
335 * @return Number of bytes read from the file
336 */
337 int
338 InflateDimensions(mat_t *mat, matvar_t *matvar, void *buf)
339 {
340 mat_uint8_t comp_buf[32];
341 mat_int32_t tag[2];
342 int bytesread = 0, err, rank, i;
343
344 if ( buf == NULL )
345 return 0;
346
347 if ( !matvar->internal->z->avail_in ) {
348 matvar->internal->z->avail_in = 1;
349 matvar->internal->z->next_in = comp_buf;
350 bytesread += fread(comp_buf,1,1,mat->fp);
351 }
352 matvar->internal->z->avail_out = 8;
353 matvar->internal->z->next_out = buf;
354 err = inflate(matvar->internal->z,Z_NO_FLUSH);
355 if ( err != Z_OK ) {
356 Mat_Critical("InflateDimensions: inflate returned %d",err);
357 return bytesread;
358 }
359 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
360 matvar->internal->z->avail_in = 1;
361 matvar->internal->z->next_in = comp_buf;
362 bytesread += fread(comp_buf,1,1,mat->fp);
363 err = inflate(matvar->internal->z,Z_NO_FLUSH);
364 if ( err != Z_OK ) {
365 Mat_Critical("InflateDimensions: inflate returned %d",err);
366 return bytesread;
367 }
368 }
369 tag[0] = *(int *)buf;
370 tag[1] = *((int *)buf+1);
371 if ( mat->byteswap ) {
372 Mat_int32Swap(tag);
373 Mat_int32Swap(tag+1);
374 }
375 if ( (tag[0] & 0x0000ffff) != MAT_T_INT32 ) {
376 Mat_Critical("InflateDimensions: Reading dimensions expected type MAT_T_INT32");
377 return bytesread;
378 }
379 rank = tag[1];
380 if ( rank % 8 != 0 )
381 i = 8-(rank %8);
382 else
383 i = 0;
384 rank+=i;
385
386 if ( !matvar->internal->z->avail_in ) {
387 matvar->internal->z->avail_in = 1;
388 matvar->internal->z->next_in = comp_buf;
389 bytesread += fread(comp_buf,1,1,mat->fp);
390 }
391 matvar->internal->z->avail_out = rank;
392 matvar->internal->z->next_out = (void *)((mat_int32_t *)buf+2);
393 err = inflate(matvar->internal->z,Z_NO_FLUSH);
394 if ( err != Z_OK ) {
395 Mat_Critical("InflateDimensions: inflate returned %d",err);
396 return bytesread;
397 }
398 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
399 matvar->internal->z->avail_in = 1;
400 matvar->internal->z->next_in = comp_buf;
401 bytesread += fread(comp_buf,1,1,mat->fp);
402 err = inflate(matvar->internal->z,Z_NO_FLUSH);
403 if ( err != Z_OK ) {
404 Mat_Critical("InflateDimensions: inflate returned %d",err);
405 return bytesread;
406 }
407 }
408
409 if ( matvar->internal->z->avail_in ) {
410 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
411 bytesread -= matvar->internal->z->avail_in;
412 matvar->internal->z->avail_in = 0;
413 }
414
415 return bytesread;
416 }
417
418 /** @brief Inflates the variable name tag
419 *
420 * @ingroup mat_internal
421 * @param mat Pointer to the MAT file
422 * @param matvar Pointer to the MAT variable
423 * @param buf Pointer to store the variables name tag
424 * @return Number of bytes read from the file
425 */
426 int
427 InflateVarNameTag(mat_t *mat, matvar_t *matvar, void *buf)
428 {
429 mat_uint8_t comp_buf[32];
430 int bytesread = 0, err;
431
432 if ( buf == NULL )
433 return 0;
434
435 if ( !matvar->internal->z->avail_in ) {
436 matvar->internal->z->avail_in = 1;
437 matvar->internal->z->next_in = comp_buf;
438 bytesread += fread(comp_buf,1,1,mat->fp);
439 }
440 matvar->internal->z->avail_out = 8;
441 matvar->internal->z->next_out = buf;
442 err = inflate(matvar->internal->z,Z_NO_FLUSH);
443 if ( err != Z_OK ) {
444 Mat_Critical("InflateVarNameTag: inflate returned %d",err);
445 return bytesread;
446 }
447 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
448 matvar->internal->z->avail_in = 1;
449 matvar->internal->z->next_in = comp_buf;
450 bytesread += fread(comp_buf,1,1,mat->fp);
451 err = inflate(matvar->internal->z,Z_NO_FLUSH);
452 if ( err != Z_OK ) {
453 Mat_Critical("InflateVarNameTag: inflate returned %d",err);
454 return bytesread;
455 }
456 }
457
458 if ( matvar->internal->z->avail_in ) {
459 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
460 bytesread -= matvar->internal->z->avail_in;
461 matvar->internal->z->avail_in = 0;
462 }
463
464 return bytesread;
465 }
466
467 /** @brief Inflates the variable name
468 *
469 * @ingroup mat_internal
470 * @param mat Pointer to the MAT file
471 * @param matvar Pointer to the MAT variable
472 * @param buf Pointer to store the variables name
473 * @param N Number of characters in the name
474 * @return Number of bytes read from the file
475 */
476 int
477 InflateVarName(mat_t *mat, matvar_t *matvar, void *buf, int N)
478 {
479 mat_uint8_t comp_buf[32];
480 int bytesread = 0, err;
481
482 if ( buf == NULL )
483 return 0;
484
485 if ( !matvar->internal->z->avail_in ) {
486 matvar->internal->z->avail_in = 1;
487 matvar->internal->z->next_in = comp_buf;
488 bytesread += fread(comp_buf,1,1,mat->fp);
489 }
490 matvar->internal->z->avail_out = N;
491 matvar->internal->z->next_out = buf;
492 err = inflate(matvar->internal->z,Z_NO_FLUSH);
493 if ( err != Z_OK ) {
494 Mat_Critical("InflateVarName: inflate returned %d",err);
495 return bytesread;
496 }
497 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
498 matvar->internal->z->avail_in = 1;
499 matvar->internal->z->next_in = comp_buf;
500 bytesread += fread(comp_buf,1,1,mat->fp);
501 err = inflate(matvar->internal->z,Z_NO_FLUSH);
502 if ( err != Z_OK ) {
503 Mat_Critical("InflateVarName: inflate returned %d",err);
504 return bytesread;
505 }
506 }
507
508 if ( matvar->internal->z->avail_in ) {
509 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
510 bytesread -= matvar->internal->z->avail_in;
511 matvar->internal->z->avail_in = 0;
512 }
513
514 return bytesread;
515 }
516
517 /** @brief Inflates the data's tag
518 *
519 * buf must hold at least 8 bytes
520 * @ingroup mat_internal
521 * @param mat Pointer to the MAT file
522 * @param matvar Pointer to the MAT variable
523 * @param buf Pointer to store the data tag
524 * @return Number of bytes read from the file
525 */
526 int
527 InflateDataTag(mat_t *mat, matvar_t *matvar, void *buf)
528 {
529 mat_uint8_t comp_buf[32];
530 int bytesread = 0, err;
531
532 if ( buf == NULL )
533 return 0;
534
535 if ( !matvar->internal->z->avail_in ) {
536 matvar->internal->z->avail_in = 1;
537 matvar->internal->z->next_in = comp_buf;
538 bytesread += fread(comp_buf,1,1,mat->fp);
539 }
540 matvar->internal->z->avail_out = 8;
541 matvar->internal->z->next_out = buf;
542 err = inflate(matvar->internal->z,Z_NO_FLUSH);
543 if ( err == Z_STREAM_END ) {
544 return bytesread;
545 } else if ( err != Z_OK ) {
546 Mat_Critical("InflateDataTag: %s - inflate returned %d",matvar->name,err);
547 return bytesread;
548 }
549 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
550 matvar->internal->z->avail_in = 1;
551 matvar->internal->z->next_in = comp_buf;
552 bytesread += fread(comp_buf,1,1,mat->fp);
553 err = inflate(matvar->internal->z,Z_NO_FLUSH);
554 if ( err == Z_STREAM_END ) {
555 break;
556 } else if ( err != Z_OK ) {
557 Mat_Critical("InflateDataTag: %s - inflate returned %d",matvar->name,err);
558 return bytesread;
559 }
560 }
561
562 if ( matvar->internal->z->avail_in ) {
563 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
564 bytesread -= matvar->internal->z->avail_in;
565 matvar->internal->z->avail_in = 0;
566 }
567
568 return bytesread;
569 }
570
571 /** @brief Inflates the data's type
572 *
573 * buf must hold at least 4 bytes
574 * @ingroup mat_internal
575 * @param mat Pointer to the MAT file
576 * @param matvar Pointer to the MAT variable
577 * @param buf Pointer to store the data type
578 * @return Number of bytes read from the file
579 */
580 int
581 InflateDataType(mat_t *mat, z_stream *z, void *buf)
582 {
583 mat_uint8_t comp_buf[32];
584 int bytesread = 0, err;
585
586 if ( buf == NULL )
587 return 0;
588
589 if ( !z->avail_in ) {
590 z->avail_in = 1;
591 z->next_in = comp_buf;
592 bytesread += fread(comp_buf,1,1,mat->fp);
593 }
594 z->avail_out = 4;
595 z->next_out = buf;
596 err = inflate(z,Z_NO_FLUSH);
597 if ( err != Z_OK ) {
598 Mat_Critical("InflateDataType: inflate returned %d",err);
599 return bytesread;
600 }
601 while ( z->avail_out && !z->avail_in ) {
602 z->avail_in = 1;
603 z->next_in = comp_buf;
604 bytesread += fread(comp_buf,1,1,mat->fp);
605 err = inflate(z,Z_NO_FLUSH);
606 if ( err != Z_OK ) {
607 Mat_Critical("InflateDataType: inflate returned %d",err);
608 return bytesread;
609 }
610 }
611
612 if ( z->avail_in ) {
613 fseek(mat->fp,-(int)z->avail_in,SEEK_CUR);
614 bytesread -= z->avail_in;
615 z->avail_in = 0;
616 }
617
618 return bytesread;
619 }
620
621 /** @brief Inflates the data
622 *
623 * buf must hold at least @c nBytes bytes
624 * @ingroup mat_internal
625 * @param mat Pointer to the MAT file
626 * @param z zlib compression stream
627 * @param buf Pointer to store the data type
628 * @param nBytes Number of bytes to inflate
629 * @return Number of bytes read from the file
630 */
631 int
632 InflateData(mat_t *mat, z_stream *z, void *buf, int nBytes)
633 {
634 mat_uint8_t comp_buf[1024];
635 int bytesread = 0, err;
636
637 if ( buf == NULL )
638 return 0;
639 if ( nBytes < 1 ) {
640 Mat_Critical("InflateData: nBytes must be > 0");
641 return bytesread;
642 }
643
644 if ( !z->avail_in ) {
645 if ( nBytes > 1024 ) {
646 z->avail_in = fread(comp_buf,1,1024,mat->fp);
647 bytesread += z->avail_in;
648 z->next_in = comp_buf;
649 } else {
650 z->avail_in = fread(comp_buf,1,nBytes,mat->fp);
651 bytesread += z->avail_in;
652 z->next_in = comp_buf;
653 }
654 }
655 z->avail_out = nBytes;
656 z->next_out = buf;
657 err = inflate(z,Z_FULL_FLUSH);
658 if ( err == Z_STREAM_END ) {
659 return bytesread;
660 } else if ( err != Z_OK ) {
661 Mat_Critical("InflateData: inflate returned %d",err);
662 return bytesread;
663 }
664 while ( z->avail_out && !z->avail_in ) {
665 if ( (nBytes-bytesread) > 1024 ) {
666 z->avail_in = fread(comp_buf,1,1024,mat->fp);
667 bytesread += z->avail_in;
668 z->next_in = comp_buf;
669 } else if ( (nBytes-bytesread) < 1 ) { /* Read a byte at a time */
670 z->avail_in = fread(comp_buf,1,1,mat->fp);
671 bytesread += z->avail_in;
672 z->next_in = comp_buf;
673 } else {
674 z->avail_in = fread(comp_buf,1,nBytes-bytesread,mat->fp);
675 bytesread += z->avail_in;
676 z->next_in = comp_buf;
677 }
678 err = inflate(z,Z_FULL_FLUSH);
679 if ( err == Z_STREAM_END ) {
680 break;
681 } else if ( err != Z_OK && err != Z_BUF_ERROR ) {
682 Mat_Critical("InflateData: inflate returned %d",err);
683 break;
684 }
685 }
686
687 if ( z->avail_in ) {
688 long offset = -(long)z->avail_in;
689 fseek(mat->fp,offset,SEEK_CUR);
690 bytesread -= z->avail_in;
691 z->avail_in = 0;
692 }
693
694 return bytesread;
695 }
696
697 /** @brief Inflates the structure's fieldname length
698 *
699 * buf must hold at least 8 bytes
700 * @ingroup mat_internal
701 * @param mat Pointer to the MAT file
702 * @param matvar Pointer to the MAT variable
703 * @param buf Pointer to store the fieldname length
704 * @return Number of bytes read from the file
705 */
706 int
707 InflateFieldNameLength(mat_t *mat, matvar_t *matvar, void *buf)
708 {
709 mat_uint8_t comp_buf[32];
710 int bytesread = 0, err;
711
712 if ( buf == NULL )
713 return 0;
714
715 if ( !matvar->internal->z->avail_in ) {
716 matvar->internal->z->avail_in = 1;
717 matvar->internal->z->next_in = comp_buf;
718 bytesread += fread(comp_buf,1,1,mat->fp);
719 }
720 matvar->internal->z->avail_out = 8;
721 matvar->internal->z->next_out = buf;
722 err = inflate(matvar->internal->z,Z_NO_FLUSH);
723 if ( err != Z_OK ) {
724 Mat_Critical("InflateFieldNameLength: inflate returned %d",err);
725 return bytesread;
726 }
727 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
728 matvar->internal->z->avail_in = 1;
729 matvar->internal->z->next_in = comp_buf;
730 bytesread += fread(comp_buf,1,1,mat->fp);
731 err = inflate(matvar->internal->z,Z_NO_FLUSH);
732 if ( err != Z_OK ) {
733 Mat_Critical("InflateFieldNameLength: inflate returned %d",err);
734 return bytesread;
735 }
736 }
737
738 if ( matvar->internal->z->avail_in ) {
739 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
740 bytesread -= matvar->internal->z->avail_in;
741 matvar->internal->z->avail_in = 0;
742 }
743
744 return bytesread;
745 }
746
747 /** @brief Inflates the structure's fieldname tag
748 *
749 * buf must hold at least 8 bytes
750 * @ingroup mat_internal
751 * @param mat Pointer to the MAT file
752 * @param matvar Pointer to the MAT variable
753 * @param buf Pointer to store the fieldname tag
754 * @return Number of bytes read from the file
755 */
756 int
757 InflateFieldNamesTag(mat_t *mat, matvar_t *matvar, void *buf)
758 {
759 mat_uint8_t comp_buf[32];
760 int bytesread = 0, err;
761
762 if ( buf == NULL )
763 return 0;
764
765 if ( !matvar->internal->z->avail_in ) {
766 matvar->internal->z->avail_in = 1;
767 matvar->internal->z->next_in = comp_buf;
768 bytesread += fread(comp_buf,1,1,mat->fp);
769 }
770 matvar->internal->z->avail_out = 8;
771 matvar->internal->z->next_out = buf;
772 err = inflate(matvar->internal->z,Z_NO_FLUSH);
773 if ( err != Z_OK ) {
774 Mat_Critical("InflateFieldNamesTag: inflate returned %d",err);
775 return bytesread;
776 }
777 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
778 matvar->internal->z->avail_in = 1;
779 matvar->internal->z->next_in = comp_buf;
780 bytesread += fread(comp_buf,1,1,mat->fp);
781 err = inflate(matvar->internal->z,Z_NO_FLUSH);
782 if ( err != Z_OK ) {
783 Mat_Critical("InflateFieldNamesTag: inflate returned %d",err);
784 return bytesread;
785 }
786 }
787
788 if ( matvar->internal->z->avail_in ) {
789 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
790 bytesread -= matvar->internal->z->avail_in;
791 matvar->internal->z->avail_in = 0;
792 }
793
794 return bytesread;
795 }
796
797 /*
798 * Inflates the structure's fieldname length. buf must hold at least
799 * nfields*fieldname_length bytes
800 */
801 /** @brief Inflates the structure's fieldnames
802 *
803 * buf must hold at least @c nfields * @c fieldname_length bytes
804 * @ingroup mat_internal
805 * @param mat Pointer to the MAT file
806 * @param matvar Pointer to the MAT variable
807 * @param buf Pointer to store the fieldnames
808 * @param nfields Number of fields
809 * @param fieldname_length Maximum length in bytes of each field
810 * @param padding Number of padding bytes
811 * @return Number of bytes read from the file
812 */
813 int
814 InflateFieldNames(mat_t *mat,matvar_t *matvar,void *buf,int nfields,
815 int fieldname_length,int padding)
816 {
817 mat_uint8_t comp_buf[32];
818 int bytesread = 0, err;
819
820 if ( buf == NULL )
821 return 0;
822
823 if ( !matvar->internal->z->avail_in ) {
824 matvar->internal->z->avail_in = 1;
825 matvar->internal->z->next_in = comp_buf;
826 bytesread += fread(comp_buf,1,1,mat->fp);
827 }
828 matvar->internal->z->avail_out = nfields*fieldname_length+padding;
829 matvar->internal->z->next_out = buf;
830 err = inflate(matvar->internal->z,Z_NO_FLUSH);
831 if ( err != Z_OK ) {
832 Mat_Critical("InflateFieldNames: inflate returned %d",err);
833 return bytesread;
834 }
835 while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
836 matvar->internal->z->avail_in = 1;
837 matvar->internal->z->next_in = comp_buf;
838 bytesread += fread(comp_buf,1,1,mat->fp);
839 err = inflate(matvar->internal->z,Z_NO_FLUSH);
840 if ( err != Z_OK ) {
841 Mat_Critical("InflateFieldNames: inflate returned %d",err);
842 return bytesread;
843 }
844 }
845
846 if ( matvar->internal->z->avail_in ) {
847 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
848 bytesread -= matvar->internal->z->avail_in;
849 matvar->internal->z->avail_in = 0;
850 }
851
852 return bytesread;
853 }
854
855 /** @endcond */
856
857 #endif