Debian Codegraph @

matio-1.5.0/src/mat5.c

raw
1 /** @file mat5.c
2 * Matlab MAT version 5 file functions
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
33 /* FIXME: Implement Unicode support */
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <math.h>
38 #include <time.h>
39 #include "matio_private.h"
40 #include "mat5.h"
41
42 #define TYPE_FROM_TAG(a) (enum matio_types)((a) & 0x000000ff)
43 #define CLASS_FROM_ARRAY_FLAGS(a) (enum matio_classes)((a) & 0x000000ff)
44 #define CLASS_TYPE_MASK 0x000000ff
45
46 static mat_complex_split_t null_complex_data = {NULL,NULL};
47
48 /*
49 * -------------------------------------------------------------
50 * Private Functions
51 * -------------------------------------------------------------
52 */
53
54 /** @brief determines the number of bytes needed to store the given struct field
55 *
56 * @ingroup mat_internal
57 * @param matvar field of a structure
58 * @return the number of bytes needed to store the struct field
59 */
60 static size_t
61 GetStructFieldBufSize(matvar_t *matvar)
62 {
63 size_t nBytes = 0, data_bytes = 0;
64 size_t tag_size = 8, array_flags_size = 8;
65 int nmemb = 1, i;
66
67 if ( matvar == NULL )
68 return nBytes;
69
70 /* Add the Array Flags tag and space to the number of bytes */
71 nBytes += tag_size + array_flags_size;
72
73 /* In a struct field, the name is just a tag with 0 bytes */
74 nBytes += tag_size;
75
76 /* Add rank and dimensions, padded to an 8 byte block */
77 for ( i = 0; i < matvar->rank; i++ )
78 nmemb *= matvar->dims[i];
79 if ( matvar->rank % 2 )
80 nBytes += tag_size + matvar->rank*4 + 4;
81 else
82 nBytes += tag_size + matvar->rank*4;
83
84 switch ( matvar->class_type ) {
85 case MAT_C_STRUCT:
86 {
87 matvar_t **fields = matvar->data;
88 int i, nfields = 0;
89 size_t maxlen = 0;
90
91 nfields = matvar->internal->num_fields;
92 for ( i = 0; i < nfields; i++ ) {
93 char *fieldname = matvar->internal->fieldnames[i];
94 if ( NULL != fieldname && strlen(fieldname) > maxlen )
95 maxlen = strlen(fieldname);
96 }
97 maxlen++;
98 while ( nfields*maxlen % 8 != 0 )
99 maxlen++;
100
101 nBytes += tag_size + tag_size + maxlen*nfields;
102
103 /* FIXME: Add bytes for the fieldnames */
104 if ( NULL != fields && nfields > 0 ) {
105 for ( i = 0; i < nfields*nmemb; i++ )
106 nBytes += tag_size + GetStructFieldBufSize(fields[i]);
107 }
108 break;
109 }
110 case MAT_C_CELL:
111 {
112 matvar_t **cells = matvar->data;
113 int i, ncells;
114
115 if ( matvar->nbytes == 0 || matvar->data_size == 0 )
116 break;
117
118 ncells = matvar->nbytes / matvar->data_size;
119
120 if ( NULL != cells && ncells > 0 ) {
121 for ( i = 0; i < ncells; i++ )
122 nBytes += tag_size + GetCellArrayFieldBufSize(cells[i]);
123 }
124 break;
125 }
126 case MAT_C_SPARSE:
127 {
128 mat_sparse_t *sparse = matvar->data;
129
130 data_bytes = sparse->nir*sizeof(mat_int32_t);
131 if ( data_bytes % 8 )
132 data_bytes += (8 - (data_bytes % 8));
133 nBytes += tag_size + data_bytes;
134
135 data_bytes = sparse->njc*sizeof(mat_int32_t);
136 if ( data_bytes % 8 )
137 data_bytes += (8 - (data_bytes % 8));
138 nBytes += tag_size + data_bytes;
139
140 data_bytes = sparse->ndata*Mat_SizeOf(matvar->data_type);
141 if ( data_bytes % 8 )
142 data_bytes += (8 - (data_bytes % 8));
143 nBytes += tag_size + data_bytes;
144
145 if ( matvar->isComplex )
146 nBytes += tag_size + data_bytes;
147
148 break;
149 }
150 default:
151 data_bytes = nmemb*Mat_SizeOf(matvar->data_type);
152 if ( data_bytes % 8 )
153 data_bytes += (8 - (data_bytes % 8));
154 nBytes += tag_size + data_bytes;
155 if ( matvar->isComplex )
156 nBytes += tag_size + data_bytes;
157 } /* switch ( matvar->class_type ) */
158
159 return nBytes;
160 }
161
162 /** @brief determines the number of bytes needed to store the cell array element
163 *
164 * @ingroup mat_internal
165 * @param matvar MAT variable
166 * @return the number of bytes needed to store the variable
167 */
168 static size_t
169 GetCellArrayFieldBufSize(matvar_t *matvar)
170 {
171 size_t nBytes = 0, len, data_bytes;
172 size_t tag_size = 8, array_flags_size = 8;
173 int nmemb = 1, i;
174
175 if ( matvar == NULL )
176 return nBytes;
177
178 /* Add the Array Flags tag and space to the number of bytes */
179 nBytes += tag_size + array_flags_size;
180
181 /* In an element of a cell array, the name is just a tag with 0 bytes */
182 nBytes += tag_size;
183
184 /* Add rank and dimensions, padded to an 8 byte block */
185 for ( i = 0, len = 0; i < matvar->rank; i++ )
186 nmemb *= matvar->dims[i];
187 if ( matvar->rank % 2 )
188 nBytes += tag_size + matvar->rank*4 + 4;
189 else
190 nBytes += tag_size + matvar->rank*4;
191
192 switch ( matvar->class_type ) {
193 case MAT_C_STRUCT:
194 {
195 matvar_t **fields = matvar->data;
196 int i, nfields = 0;
197 size_t maxlen = 0;
198
199 nfields = matvar->internal->num_fields;
200 for ( i = 0; i < nfields; i++ ) {
201 char *fieldname = matvar->internal->fieldnames[i];
202 if ( NULL != fieldname && strlen(fieldname) > maxlen )
203 maxlen = strlen(fieldname);
204 }
205 maxlen++;
206 while ( nfields*maxlen % 8 != 0 )
207 maxlen++;
208
209 nBytes += tag_size + tag_size + maxlen*nfields;
210
211 if ( NULL != fields && nfields > 0 ) {
212 for ( i = 0; i < nfields*nmemb; i++ )
213 nBytes += tag_size + GetStructFieldBufSize(fields[i]);
214 }
215 break;
216 }
217 case MAT_C_CELL:
218 {
219 matvar_t **cells = matvar->data;
220 int i, ncells;
221
222 if ( matvar->nbytes == 0 || matvar->data_size == 0 )
223 break;
224
225 ncells = matvar->nbytes / matvar->data_size;
226
227 if ( NULL != cells && ncells > 0 ) {
228 for ( i = 0; i < ncells; i++ )
229 nBytes += tag_size + GetCellArrayFieldBufSize(cells[i]);
230 }
231 break;
232 }
233 case MAT_C_SPARSE:
234 {
235 mat_sparse_t *sparse = matvar->data;
236
237 data_bytes = sparse->nir*sizeof(mat_int32_t);
238 if ( data_bytes % 8 )
239 data_bytes += (8 - (data_bytes % 8));
240 nBytes += tag_size + data_bytes;
241
242 data_bytes = sparse->njc*sizeof(mat_int32_t);
243 if ( data_bytes % 8 )
244 data_bytes += (8 - (data_bytes % 8));
245 nBytes += tag_size + data_bytes;
246
247 data_bytes = sparse->ndata*Mat_SizeOf(matvar->data_type);
248 if ( data_bytes % 8 )
249 data_bytes += (8 - (data_bytes % 8));
250 nBytes += tag_size + data_bytes;
251
252 if ( matvar->isComplex )
253 nBytes += tag_size + data_bytes;
254 break;
255 }
256 default:
257 data_bytes = nmemb*Mat_SizeOf(matvar->data_type);
258 if ( data_bytes % 8 )
259 data_bytes += (8 - (data_bytes % 8));
260 nBytes += tag_size + data_bytes;
261 if ( matvar->isComplex )
262 nBytes += tag_size + data_bytes;
263 } /* switch ( matvar->class_type ) */
264
265 return nBytes;
266 }
267
268 /** @brief determines the number of bytes needed to store the given variable
269 *
270 * @ingroup mat_internal
271 * @param matvar MAT variable
272 * @return the number of bytes needed to store the variable
273 */
274 static size_t
275 GetEmptyMatrixMaxBufSize(const char *name,int rank)
276 {
277 size_t nBytes = 0, len;
278 size_t tag_size = 8, array_flags_size = 8;
279
280 /* Add the Array Flags tag and space to the number of bytes */
281 nBytes += tag_size + array_flags_size;
282
283 /* Get size of variable name, pad it to an 8 byte block, and add it to nBytes */
284 if ( NULL != name )
285 len = strlen(name);
286 else
287 len = 4;
288
289 if ( len <= 4 ) {
290 nBytes += tag_size;
291 } else {
292 if ( len % 8 )
293 len = len + (8 - len % 8);
294 nBytes += tag_size + len;
295 }
296
297 /* Add rank and dimensions, padded to an 8 byte block */
298 if ( rank % 2 )
299 nBytes += tag_size + rank*4 + 4;
300 else
301 nBytes += tag_size + rank*4;
302
303 /* Data tag */
304 nBytes += tag_size;
305
306 return nBytes;
307 }
308
309 /** @brief determines the number of bytes needed to store the given variable
310 *
311 * @ingroup mat_internal
312 * @param matvar MAT variable
313 * @return the number of bytes needed to store the variable
314 */
315 static size_t
316 GetMatrixMaxBufSize(matvar_t *matvar)
317 {
318 size_t nBytes = 0, len, data_bytes;
319 size_t tag_size = 8, array_flags_size = 8;
320 int nmemb = 1, i;
321
322 if ( matvar == NULL )
323 return nBytes;
324
325 /* Add the Array Flags tag and space to the number of bytes */
326 nBytes += tag_size + array_flags_size;
327
328 /* Get size of variable name, pad it to an 8 byte block, and add it to nBytes */
329 if ( NULL != matvar->name )
330 len = strlen(matvar->name);
331 else
332 len=4;
333
334 if ( len <= 4 ) {
335 nBytes += tag_size;
336 } else {
337 if ( len % 8 )
338 len = len + (8 - len % 8);
339 nBytes += tag_size + len;
340 }
341
342 /* Add rank and dimensions, padded to an 8 byte block */
343 for ( i = 0, len = 0; i < matvar->rank; i++ )
344 nmemb *= matvar->dims[i];
345 if ( matvar->rank % 2 )
346 nBytes += tag_size + matvar->rank*4 + 4;
347 else
348 nBytes += tag_size + matvar->rank*4;
349
350 switch ( matvar->class_type ) {
351 case MAT_C_STRUCT:
352 {
353 matvar_t **fields = matvar->data;
354 int i, nfields = 0;
355 size_t maxlen = 0;
356
357 nfields = matvar->internal->num_fields;
358 for ( i = 0; i < nfields; i++ ) {
359 char *fieldname = matvar->internal->fieldnames[i];
360 if ( NULL != fieldname && strlen(fieldname) > maxlen )
361 maxlen = strlen(fieldname);
362 }
363 maxlen++;
364 while ( nfields*maxlen % 8 != 0 )
365 maxlen++;
366
367 nBytes += tag_size + tag_size + maxlen*nfields;
368
369 /* FIXME: Add bytes for the fieldnames */
370 if ( NULL != fields && nfields > 0 ) {
371 for ( i = 0; i < nfields*nmemb; i++ )
372 nBytes += tag_size + GetStructFieldBufSize(fields[i]);
373 }
374 break;
375 }
376 case MAT_C_CELL:
377 {
378 matvar_t **cells = matvar->data;
379 int i, ncells;
380
381 if ( matvar->nbytes == 0 || matvar->data_size == 0 )
382 break;
383
384 ncells = matvar->nbytes / matvar->data_size;
385
386 if ( NULL != cells && ncells > 0 ) {
387 for ( i = 0; i < ncells; i++ )
388 nBytes += tag_size + GetCellArrayFieldBufSize(cells[i]);
389 }
390 break;
391 }
392 case MAT_C_SPARSE:
393 {
394 mat_sparse_t *sparse = matvar->data;
395
396 data_bytes = sparse->nir*sizeof(mat_int32_t);
397 if ( data_bytes % 8 )
398 data_bytes += (8 - (data_bytes % 8));
399 nBytes += tag_size + data_bytes;
400
401 data_bytes = sparse->njc*sizeof(mat_int32_t);
402 if ( data_bytes % 8 )
403 data_bytes += (8 - (data_bytes % 8));
404 nBytes += tag_size + data_bytes;
405
406 data_bytes = sparse->ndata*Mat_SizeOf(matvar->data_type);
407 if ( data_bytes % 8 )
408 data_bytes += (8 - (data_bytes % 8));
409 nBytes += tag_size + data_bytes;
410
411 if ( matvar->isComplex )
412 nBytes += tag_size + data_bytes;
413
414 break;
415 }
416 default:
417 data_bytes = nmemb*Mat_SizeOf(matvar->data_type);
418 if ( data_bytes % 8 )
419 data_bytes += (8 - (data_bytes % 8));
420 nBytes += tag_size + data_bytes;
421 if ( matvar->isComplex )
422 nBytes += tag_size + data_bytes;
423 } /* switch ( matvar->class_type ) */
424
425 return nBytes;
426 }
427
428 /** @if mat_devman
429 * @brief Creates a new Matlab MAT version 5 file
430 *
431 * Tries to create a new Matlab MAT file with the given name and optional
432 * header string. If no header string is given, the default string
433 * is used containing the software, version, and date in it. If a header
434 * string is given, at most the first 116 characters is written to the file.
435 * The given header string need not be the full 116 characters, but MUST be
436 * NULL terminated.
437 * @ingroup MAT
438 * @param matname Name of MAT file to create
439 * @param hdr_str Optional header string, NULL to use default
440 * @return A pointer to the MAT file or NULL if it failed. This is not a
441 * simple FILE * and should not be used as one.
442 * @endif
443 */
444 mat_t *
445 Mat_Create5(const char *matname,const char *hdr_str)
446 {
447 FILE *fp = NULL;
448 mat_int16_t endian = 0, version;
449 mat_t *mat = NULL;
450 size_t err;
451 time_t t;
452
453 fp = fopen(matname,"wb");
454 if ( !fp )
455 return NULL;
456
457 mat = malloc(sizeof(*mat));
458 if ( !mat ) {
459 fclose(fp);
460 return NULL;
461 }
462
463 mat->fp = NULL;
464 mat->header = NULL;
465 mat->subsys_offset = NULL;
466 mat->filename = NULL;
467 mat->version = 0;
468 mat->byteswap = 0;
469 mat->mode = 0;
470 mat->bof = 0;
471 mat->next_index = 0;
472
473 t = time(NULL);
474 mat->fp = fp;
475 mat->filename = strdup_printf("%s",matname);
476 mat->mode = MAT_ACC_RDWR;
477 mat->byteswap = 0;
478 mat->header = calloc(1,128);
479 mat->subsys_offset = calloc(1,16);
480 memset(mat->header,' ',128);
481 if ( hdr_str == NULL ) {
482 err = mat_snprintf(mat->header,116,"MATLAB 5.0 MAT-file, Platform: %s, "
483 "Created By: libmatio v%d.%d.%d on %s", MATIO_PLATFORM,
484 MATIO_MAJOR_VERSION, MATIO_MINOR_VERSION, MATIO_RELEASE_LEVEL,
485 ctime(&t));
486 mat->header[115] = '\0'; /* Just to make sure it's NULL terminated */ } else {
487 err = mat_snprintf(mat->header,116,"%s",hdr_str);
488 }
489 mat->header[err] = ' ';
490 mat_snprintf(mat->subsys_offset,15," ");
491 mat->version = (int)0x0100;
492 endian = 0x4d49;
493
494 version = 0x0100;
495
496 err = fwrite(mat->header,1,116,mat->fp);
497 err = fwrite(mat->subsys_offset,1,8,mat->fp);
498 err = fwrite(&version,2,1,mat->fp);
499 err = fwrite(&endian,2,1,mat->fp);
500
501 return mat;
502 }
503
504 /** @if mat_devman
505 * @brief Writes @c data as character data
506 *
507 * This function uses the knowledge that the data is part of a character class
508 * to avoid some pitfalls with Matlab listed below.
509 * @li Matlab character data cannot be unsigned 8-bit integers, it needs at
510 * least unsigned 16-bit integers
511 *
512 * @ingroup mat_internal
513 * @param mat MAT file pointer
514 * @param data character data to write
515 * @param N Number of elements to write
516 * @param data_type character data type (enum matio_types)
517 * @return number of bytes written
518 * @endif
519 */
520 int
521 WriteCharData(mat_t *mat, void *data, int N,enum matio_types data_type)
522 {
523 int nBytes = 0, bytesread = 0, i;
524 mat_int8_t pad1 = 0;
525
526 switch ( data_type ) {
527 case MAT_T_UINT16:
528 {
529 nBytes = N*2;
530 fwrite(&data_type,4,1,mat->fp);
531 fwrite(&nBytes,4,1,mat->fp);
532 if ( NULL != data && N > 0 )
533 fwrite(data,2,N,mat->fp);
534 if ( nBytes % 8 )
535 for ( i = nBytes % 8; i < 8; i++ )
536 fwrite(&pad1,1,1,mat->fp);
537 break;
538 }
539 case MAT_T_INT8:
540 case MAT_T_UINT8:
541 {
542 mat_uint8_t *ptr;
543 mat_uint16_t c;
544
545 /* Matlab can't read MAT_C_CHAR as uint8, needs uint16 */
546 nBytes = N*2;
547 data_type = MAT_T_UINT16;
548 fwrite(&data_type,4,1,mat->fp);
549 fwrite(&nBytes,4,1,mat->fp);
550 ptr = data;
551 if ( NULL == ptr )
552 break;
553 for ( i = 0; i < N; i++ ) {
554 c = (mat_uint16_t)*(char *)ptr;
555 fwrite(&c,2,1,mat->fp);
556 ptr++;
557 }
558 if ( nBytes % 8 )
559 for ( i = nBytes % 8; i < 8; i++ )
560 fwrite(&pad1,1,1,mat->fp);
561 break;
562 }
563 case MAT_T_UTF8:
564 {
565 mat_uint8_t *ptr;
566
567 nBytes = N;
568 fwrite(&data_type,4,1,mat->fp);
569 fwrite(&nBytes,4,1,mat->fp);
570 ptr = data;
571 if ( NULL != ptr && nBytes > 0 )
572 fwrite(ptr,1,nBytes,mat->fp);
573 if ( nBytes % 8 )
574 for ( i = nBytes % 8; i < 8; i++ )
575 fwrite(&pad1,1,1,mat->fp);
576 break;
577 }
578 }
579 bytesread+=nBytes;
580 return bytesread;
581 }
582
583 #if defined(HAVE_ZLIB)
584 /** @brief Writes @c data as compressed character data
585 *
586 * This function uses the knowledge that the data is part of a character class
587 * to avoid some pitfalls with Matlab listed below.
588 * @li Matlab character data cannot be unsigned 8-bit integers, it needs at
589 * least unsigned 16-bit integers
590 *
591 * @ingroup mat_internal
592 * @param mat MAT file pointer
593 * @param z pointer to the zlib compression stream
594 * @param data character data to write
595 * @param N Number of elements to write
596 * @param data_type character data type (enum matio_types)
597 * @return number of bytes written
598 */
599 static size_t
600 WriteCompressedCharData(mat_t *mat,z_stream *z,void *data,int N,
601 enum matio_types data_type)
602 {
603 int data_size, data_tag[2], err, byteswritten = 0;
604 int buf_size = 1024, i;
605 mat_uint8_t buf[1024], pad[8] = {0,};
606
607 if ((mat == NULL) || (mat->fp == NULL))
608 return 0;
609
610 switch ( data_type ) {
611 case MAT_T_UINT16:
612 {
613 data_size = 2;
614 data_tag[0] = MAT_T_UINT16;
615 data_tag[1] = N*data_size;
616 z->next_in = ZLIB_BYTE_PTR(data_tag);
617 z->avail_in = 8;
618 z->next_out = buf;
619 z->avail_out = buf_size;
620 err = deflate(z,Z_NO_FLUSH);
621 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
622
623 /* exit early if this is a empty data */
624 if ( NULL == data || N < 1 )
625 break;
626
627 z->next_in = data;
628 z->avail_in = data_size*N;
629 do {
630 z->next_out = buf;
631 z->avail_out = buf_size;
632 err = deflate(z,Z_NO_FLUSH);
633 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
634 } while ( z->avail_out == 0 );
635 /* Add/Compress padding to pad to 8-byte boundary */
636 if ( N*data_size % 8 ) {
637 z->next_in = pad;
638 z->avail_in = 8 - (N*data_size % 8);
639 z->next_out = buf;
640 z->avail_out = buf_size;
641 err = deflate(z,Z_NO_FLUSH);
642 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
643 }
644 break;
645 }
646 case MAT_T_INT8:
647 case MAT_T_UINT8:
648 {
649 mat_uint8_t *ptr;
650 mat_uint16_t c;
651
652 /* Matlab can't read MAT_C_CHAR as uint8, needs uint16 */
653 data_size = 2;
654 data_tag[0] = MAT_T_UINT16;
655 data_tag[1] = N*data_size;
656 z->next_in = ZLIB_BYTE_PTR(data_tag);
657 z->avail_in = 8;
658 z->next_out = buf;
659 z->avail_out = buf_size;
660 err = deflate(z,Z_NO_FLUSH);
661 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
662
663 /* exit early if this is a empty data */
664 if ( NULL == data || N < 1 )
665 break;
666
667 z->next_in = data;
668 z->avail_in = data_size*N;
669 ptr = data;
670 for ( i = 0; i < N; i++ ) {
671 c = (mat_uint16_t)*(char *)ptr;
672 z->next_in = ZLIB_BYTE_PTR(&c);
673 z->avail_in = 2;
674 z->next_out = buf;
675 z->avail_out = buf_size;
676 err = deflate(z,Z_NO_FLUSH);
677 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
678 ptr++;
679 }
680 /* Add/Compress padding to pad to 8-byte boundary */
681 if ( N*data_size % 8 ) {
682 z->next_in = pad;
683 z->avail_in = 8 - (N*data_size % 8);
684 z->next_out = buf;
685 z->avail_out = buf_size;
686 err = deflate(z,Z_NO_FLUSH);
687 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
688 }
689 break;
690 }
691 case MAT_T_UTF8:
692 {
693 data_size = 1;
694 data_tag[0] = MAT_T_UTF8;
695 data_tag[1] = N*data_size;
696 z->next_in = ZLIB_BYTE_PTR(data_tag);
697 z->avail_in = 8;
698 z->next_out = buf;
699 z->avail_out = buf_size;
700 err = deflate(z,Z_NO_FLUSH);
701 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
702
703 /* exit early if this is a empty data */
704 if ( NULL == data || N < 1 )
705 break;
706
707 z->next_in = data;
708 z->avail_in = data_size*N;
709 do {
710 z->next_out = buf;
711 z->avail_out = buf_size;
712 err = deflate(z,Z_NO_FLUSH);
713 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
714 } while ( z->avail_out == 0 );
715 /* Add/Compress padding to pad to 8-byte boundary */
716 if ( N*data_size % 8 ) {
717 z->next_in = pad;
718 z->avail_in = 8 - (N*data_size % 8);
719 z->next_out = buf;
720 z->avail_out = buf_size;
721 err = deflate(z,Z_NO_FLUSH);
722 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
723 }
724 break;
725 }
726 }
727 return byteswritten;
728 }
729 #endif
730
731 /** @if mat_devman
732 * @brief Writes empty characters to the MAT file
733 *
734 * This function uses the knowledge that the data is part of a character class
735 * to avoid some pitfalls with Matlab listed below.
736 * @li Matlab character data cannot be unsigned 8-bit integers, it needs at
737 * least unsigned 16-bit integers
738 * @ingroup mat_internal
739 * @param mat MAT file pointer
740 * @param data character data to write
741 * @param N Number of elements to write
742 * @param data_type character data type (enum matio_types)
743 * @return number of bytes written
744 * @endif
745 */
746 static int
747 WriteEmptyCharData(mat_t *mat, int N, enum matio_types data_type)
748 {
749 int nBytes = 0, bytesread = 0, i;
750 mat_int8_t pad1 = 0;
751
752 switch ( data_type ) {
753 case MAT_T_UINT8: /* Matlab MAT_C_CHAR needs uint16 */
754 case MAT_T_INT8: /* Matlab MAT_C_CHAR needs uint16 */
755 data_type = MAT_T_UINT16;
756 case MAT_T_UINT16:
757 {
758 mat_uint16_t u16 = 0;
759 nBytes = N*sizeof(mat_uint16_t);
760 fwrite(&data_type,sizeof(mat_int32_t),1,mat->fp);
761 fwrite(&nBytes,sizeof(mat_int32_t),1,mat->fp);
762 for ( i = 0; i < N; i++ )
763 fwrite(&u16,sizeof(mat_uint16_t),1,mat->fp);
764 if ( nBytes % 8 )
765 for ( i = nBytes % 8; i < 8; i++ )
766 fwrite(&pad1,1,1,mat->fp);
767 break;
768 }
769 case MAT_T_UTF8:
770 {
771 mat_uint8_t u8 = 0;
772 nBytes = N;
773 fwrite(&data_type,sizeof(mat_int32_t),1,mat->fp);
774 fwrite(&nBytes,sizeof(mat_int32_t),1,mat->fp);
775 for ( i = 0; i < N; i++ )
776 fwrite(&u8,sizeof(mat_uint8_t),1,mat->fp);
777 if ( nBytes % 8 )
778 for ( i = nBytes % 8; i < 8; i++ )
779 fwrite(&pad1,1,1,mat->fp);
780 break;
781 }
782 }
783 bytesread+=nBytes;
784 return bytesread;
785 }
786
787 /** @if mat_devman
788 * @brief Writes the data tags and empty data to the file
789 *
790 * Writes the data tags and empty data to the file to save space for the
791 * variable when the actual data is written
792 * @ingroup mat_internal
793 * @param mat MAT file pointer
794 * @param N number of elements to write
795 * @param data_type data type to write
796 * @return Number of bytes written
797 * @endif
798 */
799 static int
800 WriteEmptyData(mat_t *mat,int N,enum matio_types data_type)
801 {
802 int nBytes = 0, data_size, i;
803
804 if ( (mat == NULL) || (mat->fp == NULL) )
805 return 0;
806
807 switch ( data_type ) {
808 case MAT_T_DOUBLE:
809 {
810 double d = 0.0;
811
812 data_size = sizeof(double);
813 nBytes = N*data_size;
814 fwrite(&data_type,4,1,mat->fp);
815 fwrite(&nBytes,4,1,mat->fp);
816 for ( i = 0; i < N; i++ )
817 fwrite(&d,data_size,1,mat->fp);
818 break;
819 }
820 case MAT_T_SINGLE:
821 {
822 float f = 0.0;
823
824 data_size = sizeof(float);
825 nBytes = N*data_size;
826 fwrite(&data_type,4,1,mat->fp);
827 fwrite(&nBytes,4,1,mat->fp);
828 for ( i = 0; i < N; i++ )
829 fwrite(&f,data_size,1,mat->fp);
830 break;
831 }
832 case MAT_T_INT8:
833 {
834 mat_int8_t i8 = 0;
835
836 data_size = sizeof(mat_int8_t);
837 nBytes = N*data_size;
838 fwrite(&data_type,4,1,mat->fp);
839 fwrite(&nBytes,4,1,mat->fp);
840 for ( i = 0; i < N; i++ )
841 fwrite(&i8,data_size,1,mat->fp);
842 break;
843 }
844 case MAT_T_UINT8:
845 {
846 mat_uint8_t ui8 = 0;
847
848 data_size = sizeof(mat_uint8_t);
849 nBytes = N*data_size;
850 fwrite(&data_type,4,1,mat->fp);
851 fwrite(&nBytes,4,1,mat->fp);
852 for ( i = 0; i < N; i++ )
853 fwrite(&ui8,data_size,1,mat->fp);
854 break;
855 }
856 case MAT_T_INT16:
857 {
858 mat_int16_t i16 = 0;
859
860 data_size = sizeof(mat_int16_t);
861 nBytes = N*data_size;
862 fwrite(&data_type,4,1,mat->fp);
863 fwrite(&nBytes,4,1,mat->fp);
864 for ( i = 0; i < N; i++ )
865 fwrite(&i16,data_size,1,mat->fp);
866 break;
867 }
868 case MAT_T_UINT16:
869 {
870 mat_uint16_t ui16 = 0;
871
872 data_size = sizeof(mat_uint16_t);
873 nBytes = N*data_size;
874 fwrite(&data_type,4,1,mat->fp);
875 fwrite(&nBytes,4,1,mat->fp);
876 for ( i = 0; i < N; i++ )
877 fwrite(&ui16,data_size,1,mat->fp);
878 break;
879 }
880 case MAT_T_INT32:
881 {
882 mat_int32_t i32 = 0;
883
884 data_size = sizeof(mat_int32_t);
885 nBytes = N*data_size;
886 fwrite(&data_type,4,1,mat->fp);
887 fwrite(&nBytes,4,1,mat->fp);
888 for ( i = 0; i < N; i++ )
889 fwrite(&i32,data_size,1,mat->fp);
890 break;
891 }
892 case MAT_T_UINT32:
893 {
894 mat_uint32_t ui32 = 0;
895
896 data_size = sizeof(mat_uint32_t);
897 nBytes = N*data_size;
898 fwrite(&data_type,4,1,mat->fp);
899 fwrite(&nBytes,4,1,mat->fp);
900 for ( i = 0; i < N; i++ )
901 fwrite(&ui32,data_size,1,mat->fp);
902 break;
903 }
904 #ifdef HAVE_MAT_INT64_T
905 case MAT_T_INT64:
906 {
907 mat_int64_t i64 = 0;
908
909 data_size = sizeof(mat_int64_t);
910 nBytes = N*data_size;
911 fwrite(&data_type,4,1,mat->fp);
912 fwrite(&nBytes,4,1,mat->fp);
913 for ( i = 0; i < N; i++ )
914 fwrite(&i64,data_size,1,mat->fp);
915 break;
916 }
917 #endif
918 #ifdef HAVE_MAT_UINT64_T
919 case MAT_T_UINT64:
920 {
921 mat_uint64_t ui64 = 0;
922
923 data_size = sizeof(mat_uint64_t);
924 nBytes = N*data_size;
925 fwrite(&data_type,4,1,mat->fp);
926 fwrite(&nBytes,4,1,mat->fp);
927 for ( i = 0; i < N; i++ )
928 fwrite(&ui64,data_size,1,mat->fp);
929 break;
930 }
931 #endif
932 default:
933 nBytes = 0;
934 }
935 return nBytes;
936 }
937
938 #if defined(HAVE_ZLIB)
939 static int
940 WriteCompressedEmptyData(mat_t *mat,z_stream *z,int N,
941 enum matio_types data_type)
942 {
943 int nBytes = 0, data_size, i, err, byteswritten = 0;
944
945 if ( (mat == NULL) || (mat->fp == NULL) )
946 return 0;
947
948 switch ( data_type ) {
949 case MAT_T_DOUBLE:
950 {
951 mat_uint32_t uncomp_buf[32] = {0,};
952 mat_uint32_t comp_buf[32] = {0,};
953 double data_uncomp_buf[4] = {0.0,};
954
955 data_size = sizeof(double);
956 nBytes = N*data_size;
957 uncomp_buf[0] = data_type;
958 uncomp_buf[1] = 0;
959 z->next_out = ZLIB_BYTE_PTR(comp_buf);
960 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
961 z->avail_out = 32*sizeof(*comp_buf);
962 z->avail_in = 8;
963 err = deflate(z,Z_NO_FLUSH);
964 byteswritten += fwrite(comp_buf,1,32*sizeof(*comp_buf)-z->avail_out,mat->fp);
965 for ( i = 0; i < N; i++ ) {
966 z->next_out = ZLIB_BYTE_PTR(comp_buf);
967 z->next_in = ZLIB_BYTE_PTR(data_uncomp_buf);
968 z->avail_out = 32*sizeof(*comp_buf);
969 z->avail_in = 8;
970 err = deflate(z,Z_NO_FLUSH);
971 byteswritten += fwrite(comp_buf,32*sizeof(*comp_buf)-z->avail_out,1,mat->fp);
972 }
973 break;
974 }
975 case MAT_T_SINGLE:
976 {
977 float f = 0.0;
978
979 data_size = sizeof(float);
980 nBytes = N*data_size;
981 fwrite(&data_type,4,1,mat->fp);
982 fwrite(&nBytes,4,1,mat->fp);
983 for ( i = 0; i < N; i++ )
984 fwrite(&f,data_size,1,mat->fp);
985 break;
986 }
987 case MAT_T_INT8:
988 {
989 mat_int8_t i8 = 0;
990
991 data_size = sizeof(mat_int8_t);
992 nBytes = N*data_size;
993 fwrite(&data_type,4,1,mat->fp);
994 fwrite(&nBytes,4,1,mat->fp);
995 for ( i = 0; i < N; i++ )
996 fwrite(&i8,data_size,1,mat->fp);
997 break;
998 }
999 case MAT_T_UINT8:
1000 {
1001 mat_uint8_t ui8 = 0;
1002
1003 data_size = sizeof(mat_uint8_t);
1004 nBytes = N*data_size;
1005 fwrite(&data_type,4,1,mat->fp);
1006 fwrite(&nBytes,4,1,mat->fp);
1007 for ( i = 0; i < N; i++ )
1008 fwrite(&ui8,data_size,1,mat->fp);
1009 break;
1010 }
1011 case MAT_T_INT16:
1012 {
1013 mat_int16_t i16 = 0;
1014
1015 data_size = sizeof(mat_int16_t);
1016 nBytes = N*data_size;
1017 fwrite(&data_type,4,1,mat->fp);
1018 fwrite(&nBytes,4,1,mat->fp);
1019 for ( i = 0; i < N; i++ )
1020 fwrite(&i16,data_size,1,mat->fp);
1021 break;
1022 }
1023 case MAT_T_UINT16:
1024 {
1025 mat_uint16_t ui16 = 0;
1026
1027 data_size = sizeof(mat_uint16_t);
1028 nBytes = N*data_size;
1029 fwrite(&data_type,4,1,mat->fp);
1030 fwrite(&nBytes,4,1,mat->fp);
1031 for ( i = 0; i < N; i++ )
1032 fwrite(&ui16,data_size,1,mat->fp);
1033 break;
1034 }
1035 case MAT_T_INT32:
1036 {
1037 mat_int32_t i32 = 0;
1038
1039 data_size = sizeof(mat_int32_t);
1040 nBytes = N*data_size;
1041 fwrite(&data_type,4,1,mat->fp);
1042 fwrite(&nBytes,4,1,mat->fp);
1043 for ( i = 0; i < N; i++ )
1044 fwrite(&i32,data_size,1,mat->fp);
1045 break;
1046 }
1047 case MAT_T_UINT32:
1048 {
1049 mat_uint32_t ui32 = 0;
1050
1051 data_size = sizeof(mat_uint32_t);
1052 nBytes = N*data_size;
1053 fwrite(&data_type,4,1,mat->fp);
1054 fwrite(&nBytes,4,1,mat->fp);
1055 for ( i = 0; i < N; i++ )
1056 fwrite(&ui32,data_size,1,mat->fp);
1057 break;
1058 }
1059 #ifdef HAVE_MAT_INT64_T
1060 case MAT_T_INT64:
1061 {
1062 mat_int64_t i64 = 0;
1063
1064 data_size = sizeof(mat_int64_t);
1065 nBytes = N*data_size;
1066 fwrite(&data_type,4,1,mat->fp);
1067 fwrite(&nBytes,4,1,mat->fp);
1068 for ( i = 0; i < N; i++ )
1069 fwrite(&i64,data_size,1,mat->fp);
1070 break;
1071 }
1072 #endif
1073 #ifdef HAVE_MAT_UINT64_T
1074 case MAT_T_UINT64:
1075 {
1076 mat_uint64_t ui64 = 0;
1077
1078 data_size = sizeof(mat_uint64_t);
1079 nBytes = N*data_size;
1080 fwrite(&data_type,4,1,mat->fp);
1081 fwrite(&nBytes,4,1,mat->fp);
1082 for ( i = 0; i < N; i++ )
1083 fwrite(&ui64,data_size,1,mat->fp);
1084 break;
1085 }
1086 #endif
1087 default:
1088 nBytes = 0;
1089 }
1090 return byteswritten;
1091 }
1092 #endif
1093
1094 /** @if mat_devman
1095 * @param Writes a 2-D slab of data to the MAT file
1096 *
1097 * @ingroup mat_internal
1098 * @fixme should return the number of bytes written, but currently returns 0
1099 * @param mat MAT file pointer
1100 * @param data pointer to the slab of data
1101 * @param data_type data type of the data (enum matio_types)
1102 * @param dims dimensions of the dataset
1103 * @param start index to start writing the data in each dimension
1104 * @param stride write data every @c stride elements
1105 * @param edge number of elements to write in each dimension
1106 * @return number of byteswritten
1107 * @endif
1108 */
1109 int
1110 WriteDataSlab2(mat_t *mat,void *data,enum matio_types data_type,size_t *dims,
1111 int *start,int *stride,int *edge)
1112 {
1113 int nBytes = 0, data_size, i, j;
1114 long pos, row_stride, col_stride;
1115
1116 if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) ||
1117 (start == NULL) || (stride == NULL) || (edge == NULL) ) {
1118 return 0;
1119 }
1120
1121 switch ( data_type ) {
1122 case MAT_T_DOUBLE:
1123 {
1124 double *ptr;
1125
1126 data_size = sizeof(double);
1127 ptr = (double *)data;
1128 row_stride = (stride[0]-1)*data_size;
1129 col_stride = stride[1]*dims[0]*data_size;
1130
1131 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1132 for ( i = 0; i < edge[1]; i++ ) {
1133 pos = ftell(mat->fp);
1134 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1135 for ( j = 0; j < edge[0]; j++ ) {
1136 fwrite(ptr++,data_size,1,mat->fp);
1137 fseek(mat->fp,row_stride,SEEK_CUR);
1138 }
1139 pos = pos+col_stride-ftell(mat->fp);
1140 fseek(mat->fp,pos,SEEK_CUR);
1141 }
1142 break;
1143 }
1144 case MAT_T_SINGLE:
1145 {
1146 float *ptr;
1147
1148 data_size = sizeof(float);
1149 ptr = (float *)data;
1150 row_stride = (stride[0]-1)*data_size;
1151 col_stride = stride[1]*dims[0]*data_size;
1152
1153 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1154 for ( i = 0; i < edge[1]; i++ ) {
1155 pos = ftell(mat->fp);
1156 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1157 for ( j = 0; j < edge[0]; j++ ) {
1158 fwrite(ptr++,data_size,1,mat->fp);
1159 fseek(mat->fp,row_stride,SEEK_CUR);
1160 }
1161 pos = pos+col_stride-ftell(mat->fp);
1162 fseek(mat->fp,pos,SEEK_CUR);
1163 }
1164 break;
1165 }
1166 #ifdef HAVE_MAT_INT64_T
1167 case MAT_T_INT64:
1168 {
1169 mat_int64_t *ptr;
1170
1171 data_size = sizeof(mat_int64_t);
1172 ptr = (mat_int64_t *)data;
1173 row_stride = (stride[0]-1)*data_size;
1174 col_stride = stride[1]*dims[0]*data_size;
1175
1176 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1177 for ( i = 0; i < edge[1]; i++ ) {
1178 pos = ftell(mat->fp);
1179 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1180 for ( j = 0; j < edge[0]; j++ ) {
1181 fwrite(ptr++,data_size,1,mat->fp);
1182 fseek(mat->fp,row_stride,SEEK_CUR);
1183 }
1184 pos = pos+col_stride-ftell(mat->fp);
1185 fseek(mat->fp,pos,SEEK_CUR);
1186 }
1187 break;
1188 }
1189 #endif
1190 #ifdef HAVE_MAT_UINT64_T
1191 case MAT_T_UINT64:
1192 {
1193 mat_uint64_t *ptr;
1194
1195 data_size = sizeof(mat_uint64_t);
1196 ptr = (mat_uint64_t *)data;
1197 row_stride = (stride[0]-1)*data_size;
1198 col_stride = stride[1]*dims[0]*data_size;
1199
1200 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1201 for ( i = 0; i < edge[1]; i++ ) {
1202 pos = ftell(mat->fp);
1203 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1204 for ( j = 0; j < edge[0]; j++ ) {
1205 fwrite(ptr++,data_size,1,mat->fp);
1206 fseek(mat->fp,row_stride,SEEK_CUR);
1207 }
1208 pos = pos+col_stride-ftell(mat->fp);
1209 fseek(mat->fp,pos,SEEK_CUR);
1210 }
1211 break;
1212 }
1213 #endif
1214 case MAT_T_INT32:
1215 {
1216 mat_int32_t *ptr;
1217
1218 data_size = sizeof(mat_int32_t);
1219 ptr = (mat_int32_t *)data;
1220 row_stride = (stride[0]-1)*data_size;
1221 col_stride = stride[1]*dims[0]*data_size;
1222
1223 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1224 for ( i = 0; i < edge[1]; i++ ) {
1225 pos = ftell(mat->fp);
1226 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1227 for ( j = 0; j < edge[0]; j++ ) {
1228 fwrite(ptr++,data_size,1,mat->fp);
1229 fseek(mat->fp,row_stride,SEEK_CUR);
1230 }
1231 pos = pos+col_stride-ftell(mat->fp);
1232 fseek(mat->fp,pos,SEEK_CUR);
1233 }
1234 break;
1235 }
1236 case MAT_T_UINT32:
1237 {
1238 mat_uint32_t *ptr;
1239
1240 data_size = sizeof(mat_uint32_t);
1241 ptr = (mat_uint32_t *)data;
1242 row_stride = (stride[0]-1)*data_size;
1243 col_stride = stride[1]*dims[0]*data_size;
1244
1245 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1246 for ( i = 0; i < edge[1]; i++ ) {
1247 pos = ftell(mat->fp);
1248 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1249 for ( j = 0; j < edge[0]; j++ ) {
1250 fwrite(ptr++,data_size,1,mat->fp);
1251 fseek(mat->fp,row_stride,SEEK_CUR);
1252 }
1253 pos = pos+col_stride-ftell(mat->fp);
1254 fseek(mat->fp,pos,SEEK_CUR);
1255 }
1256 break;
1257 }
1258 case MAT_T_INT16:
1259 {
1260 mat_int16_t *ptr;
1261
1262 data_size = sizeof(mat_int16_t);
1263 ptr = (mat_int16_t *)data;
1264 row_stride = (stride[0]-1)*data_size;
1265 col_stride = stride[1]*dims[0]*data_size;
1266
1267 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1268 for ( i = 0; i < edge[1]; i++ ) {
1269 pos = ftell(mat->fp);
1270 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1271 for ( j = 0; j < edge[0]; j++ ) {
1272 fwrite(ptr++,data_size,1,mat->fp);
1273 fseek(mat->fp,row_stride,SEEK_CUR);
1274 }
1275 pos = pos+col_stride-ftell(mat->fp);
1276 fseek(mat->fp,pos,SEEK_CUR);
1277 }
1278 break;
1279 }
1280 case MAT_T_UINT16:
1281 {
1282 mat_uint16_t *ptr;
1283
1284 data_size = sizeof(mat_uint16_t);
1285 ptr = (mat_uint16_t *)data;
1286 row_stride = (stride[0]-1)*data_size;
1287 col_stride = stride[1]*dims[0]*data_size;
1288
1289 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1290 for ( i = 0; i < edge[1]; i++ ) {
1291 pos = ftell(mat->fp);
1292 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1293 for ( j = 0; j < edge[0]; j++ ) {
1294 fwrite(ptr++,data_size,1,mat->fp);
1295 fseek(mat->fp,row_stride,SEEK_CUR);
1296 }
1297 pos = pos+col_stride-ftell(mat->fp);
1298 fseek(mat->fp,pos,SEEK_CUR);
1299 }
1300 break;
1301 }
1302 case MAT_T_INT8:
1303 {
1304 mat_int8_t *ptr;
1305
1306 data_size = sizeof(mat_int8_t);
1307 ptr = (mat_int8_t *)data;
1308 row_stride = (stride[0]-1)*data_size;
1309 col_stride = stride[1]*dims[0]*data_size;
1310
1311 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1312 for ( i = 0; i < edge[1]; i++ ) {
1313 pos = ftell(mat->fp);
1314 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1315 for ( j = 0; j < edge[0]; j++ ) {
1316 fwrite(ptr++,data_size,1,mat->fp);
1317 fseek(mat->fp,row_stride,SEEK_CUR);
1318 }
1319 pos = pos+col_stride-ftell(mat->fp);
1320 fseek(mat->fp,pos,SEEK_CUR);
1321 }
1322 break;
1323 }
1324 case MAT_T_UINT8:
1325 {
1326 mat_uint8_t *ptr;
1327
1328 data_size = sizeof(mat_uint8_t);
1329 ptr = (mat_uint8_t *)data;
1330 row_stride = (stride[0]-1)*data_size;
1331 col_stride = stride[1]*dims[0]*data_size;
1332
1333 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1334 for ( i = 0; i < edge[1]; i++ ) {
1335 pos = ftell(mat->fp);
1336 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1337 for ( j = 0; j < edge[0]; j++ ) {
1338 fwrite(ptr++,data_size,1,mat->fp);
1339 fseek(mat->fp,row_stride,SEEK_CUR);
1340 }
1341 pos = pos+col_stride-ftell(mat->fp);
1342 fseek(mat->fp,pos,SEEK_CUR);
1343 }
1344 break;
1345 }
1346 default:
1347 nBytes = 0;
1348 }
1349 return nBytes;
1350 }
1351
1352 /** @if mat_devman
1353 * @param Writes a 2-D slab of character data to the MAT file
1354 *
1355 * This function uses the knowledge that the data is part of a character class
1356 * to avoid some pitfalls with Matlab listed below.
1357 * @li Matlab character data cannot be unsigned 8-bit integers, it needs at
1358 * least unsigned 16-bit integers
1359 * @ingroup mat_internal
1360 * @fixme should return the number of bytes written, but currently returns 0
1361 * @param mat MAT file pointer
1362 * @param data pointer to the slab of data
1363 * @param data_type data type of the data (enum matio_types)
1364 * @param dims dimensions of the dataset
1365 * @param start index to start writing the data in each dimension
1366 * @param stride write data every @c stride elements
1367 * @param edge number of elements to write in each dimension
1368 * @return number of byteswritten
1369 * @endif
1370 */
1371 int
1372 WriteCharDataSlab2(mat_t *mat,void *data,enum matio_types data_type,
1373 size_t *dims,int *start,int *stride,int *edge)
1374 {
1375 int nBytes = 0, data_size, i, j;
1376 long pos, row_stride, col_stride;
1377
1378 if ( (mat == NULL) || (data == NULL) || (mat->fp == NULL) ||
1379 (start == NULL) || (stride == NULL) || (edge == NULL) ) {
1380 return 0;
1381 }
1382
1383 switch ( data_type ) {
1384 case MAT_T_UINT16:
1385 {
1386 mat_uint16_t *ptr;
1387
1388 data_size = sizeof(mat_uint16_t);
1389 ptr = data;
1390 row_stride = (stride[0]-1)*data_size;
1391 col_stride = stride[1]*dims[0]*data_size;
1392
1393 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1394 for ( i = 0; i < edge[1]; i++ ) {
1395 pos = ftell(mat->fp);
1396 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1397 for ( j = 0; j < edge[0]; j++ ) {
1398 fwrite(ptr++,data_size,1,mat->fp);
1399 fseek(mat->fp,row_stride,SEEK_CUR);
1400 }
1401 pos = pos+col_stride-ftell(mat->fp);
1402 fseek(mat->fp,pos,SEEK_CUR);
1403 }
1404 break;
1405 }
1406 case MAT_T_INT8:
1407 case MAT_T_UINT8:
1408 {
1409 /* Matlab can't read MAT_C_CHAR as uint8, needs uint16 */
1410 mat_uint8_t *ptr;
1411 mat_uint16_t c;
1412
1413 data_size = sizeof(mat_uint16_t);
1414 ptr = data;
1415 row_stride = (stride[0]-1)*data_size;
1416 col_stride = stride[1]*dims[0]*data_size;
1417
1418 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1419 for ( i = 0; i < edge[1]; i++ ) {
1420 pos = ftell(mat->fp);
1421 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1422 for ( j = 0; j < edge[0]; j++,ptr++ ) {
1423 c = *ptr;
1424 fwrite(&c,data_size,1,mat->fp);
1425 fseek(mat->fp,row_stride,SEEK_CUR);
1426 }
1427 pos = pos+col_stride-ftell(mat->fp);
1428 fseek(mat->fp,pos,SEEK_CUR);
1429 }
1430 break;
1431 }
1432 case MAT_T_UTF8:
1433 {
1434 mat_uint8_t *ptr;
1435
1436 data_size = sizeof(mat_uint8_t);
1437 ptr = data;
1438 row_stride = (stride[0]-1)*data_size;
1439 col_stride = stride[1]*dims[0]*data_size;
1440
1441 fseek(mat->fp,start[1]*dims[0]*data_size,SEEK_CUR);
1442 for ( i = 0; i < edge[1]; i++ ) {
1443 pos = ftell(mat->fp);
1444 fseek(mat->fp,start[0]*data_size,SEEK_CUR);
1445 for ( j = 0; j < edge[0]; j++,ptr++ ) {
1446 fwrite(ptr,data_size,1,mat->fp);
1447 fseek(mat->fp,row_stride,SEEK_CUR);
1448 }
1449 pos = pos+col_stride-ftell(mat->fp);
1450 fseek(mat->fp,pos,SEEK_CUR);
1451 }
1452 break;
1453 }
1454 default:
1455 nBytes = 0;
1456 }
1457 return nBytes;
1458 }
1459
1460 /** @brief Writes the data buffer to the file
1461 *
1462 * @param mat MAT file pointer
1463 * @param data pointer to the data to write
1464 * @param N number of elements to write
1465 * @param data_type data type of the data
1466 * @return number of bytes written
1467 */
1468 int
1469 WriteData(mat_t *mat,void *data,int N,enum matio_types data_type)
1470 {
1471 int nBytes = 0, data_size;
1472
1473 if ((mat == NULL) || (mat->fp == NULL) || (data == NULL && N > 0))
1474 return 0;
1475
1476 data_size = Mat_SizeOf(data_type);
1477 nBytes = N*data_size;
1478 fwrite(&data_type,4,1,mat->fp);
1479 fwrite(&nBytes,4,1,mat->fp);
1480 fwrite(data,data_size,N,mat->fp);
1481
1482 return nBytes;
1483 }
1484
1485 #if defined(HAVE_ZLIB)
1486 /* Compresses the data buffer and writes it to the file */
1487 static size_t
1488 WriteCompressedData(mat_t *mat,z_stream *z,void *data,int N,
1489 enum matio_types data_type)
1490 {
1491 int nBytes = 0, data_size, data_tag[2], err, byteswritten = 0;
1492 int buf_size = 1024;
1493 mat_uint8_t buf[1024], pad[8] = {0,};
1494
1495 if ((mat == NULL) || (mat->fp == NULL))
1496 return 0;
1497
1498 data_size = Mat_SizeOf(data_type);
1499
1500 data_tag[0] = data_type;
1501 data_tag[1] = data_size*N;
1502 z->next_in = ZLIB_BYTE_PTR(data_tag);
1503 z->avail_in = 8;
1504 z->next_out = buf;
1505 z->avail_out = buf_size;
1506 err = deflate(z,Z_NO_FLUSH);
1507 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
1508
1509 /* exit early if this is a empty data */
1510 if ( NULL == data || N < 1 )
1511 return byteswritten;
1512
1513 z->next_in = data;
1514 z->avail_in = N*data_size;
1515 do {
1516 z->next_out = buf;
1517 z->avail_out = buf_size;
1518 err = deflate(z,Z_NO_FLUSH);
1519 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
1520 } while ( z->avail_out == 0 );
1521 /* Add/Compress padding to pad to 8-byte boundary */
1522 if ( N*data_size % 8 ) {
1523 z->next_in = pad;
1524 z->avail_in = 8 - (N*data_size % 8);
1525 z->next_out = buf;
1526 z->avail_out = buf_size;
1527 err = deflate(z,Z_NO_FLUSH);
1528 byteswritten += fwrite(buf,1,buf_size-z->avail_out,mat->fp);
1529 }
1530 nBytes = byteswritten;
1531 return nBytes;
1532 }
1533 #endif
1534
1535 /** @brief Reads the next cell of the cell array in @c matvar
1536 *
1537 * @ingroup mat_internal
1538 * @param mat MAT file pointer
1539 * @param matvar MAT variable pointer
1540 * @return Number of bytes read
1541 */
1542 static int
1543 ReadNextCell( mat_t *mat, matvar_t *matvar )
1544 {
1545 int ncells, bytesread = 0, i, err;
1546 matvar_t **cells = NULL;
1547
1548 if ( matvar->compression ) {
1549 #if defined(HAVE_ZLIB)
1550 mat_uint32_t uncomp_buf[16] = {0,};
1551 int nbytes;
1552 mat_uint32_t array_flags;
1553
1554 ncells = 1;
1555 for ( i = 0; i < matvar->rank; i++ )
1556 ncells *= matvar->dims[i];
1557 matvar->data_size = sizeof(matvar_t *);
1558 matvar->nbytes = ncells*matvar->data_size;
1559 matvar->data = malloc(matvar->nbytes);
1560 if ( !matvar->data )
1561 return bytesread;
1562 cells = matvar->data;
1563 for ( i = 0; i < ncells; i++ ) {
1564 cells[i] = Mat_VarCalloc();
1565 if ( NULL == cells[i] ) {
1566 Mat_Critical("Couldn't allocate memory for cell %d", i);
1567 continue;
1568 }
1569
1570 cells[i]->internal->fpos = ftell(mat->fp)-matvar->internal->z->avail_in;
1571
1572 /* Read variable tag for cell */
1573 uncomp_buf[0] = 0;
1574 uncomp_buf[1] = 0;
1575 bytesread += InflateVarTag(mat,matvar,uncomp_buf);
1576 if ( mat->byteswap ) {
1577 (void)Mat_uint32Swap(uncomp_buf);
1578 (void)Mat_uint32Swap(uncomp_buf+1);
1579 }
1580 nbytes = uncomp_buf[1];
1581 if ( !nbytes ) {
1582 /* empty cell */
1583 continue;
1584 } else if ( uncomp_buf[0] != MAT_T_MATRIX ) {
1585 Mat_Critical("cells[%d], Uncompressed type not MAT_T_MATRIX",i);
1586 Mat_VarFree(cells[i]);
1587 cells[i] = NULL;
1588 break;
1589 }
1590 cells[i]->compression = 1;
1591 bytesread += InflateArrayFlags(mat,matvar,uncomp_buf);
1592 nbytes -= 16;
1593 if ( mat->byteswap ) {
1594 (void)Mat_uint32Swap(uncomp_buf);
1595 (void)Mat_uint32Swap(uncomp_buf+1);
1596 (void)Mat_uint32Swap(uncomp_buf+2);
1597 (void)Mat_uint32Swap(uncomp_buf+3);
1598 }
1599 /* Array Flags */
1600 if ( uncomp_buf[0] == MAT_T_UINT32 ) {
1601 array_flags = uncomp_buf[2];
1602 cells[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
1603 cells[i]->isComplex = (array_flags & MAT_F_COMPLEX);
1604 cells[i]->isGlobal = (array_flags & MAT_F_GLOBAL);
1605 cells[i]->isLogical = (array_flags & MAT_F_LOGICAL);
1606 if ( cells[i]->class_type == MAT_C_SPARSE ) {
1607 /* Need to find a more appropriate place to store nzmax */
1608 cells[i]->nbytes = uncomp_buf[3];
1609 }
1610 } else {
1611 Mat_Critical("Expected MAT_T_UINT32 for Array Tags, got %d",
1612 uncomp_buf[0]);
1613 bytesread+=InflateSkip(mat,matvar->internal->z,nbytes);
1614 }
1615 bytesread += InflateDimensions(mat,matvar,uncomp_buf);
1616 nbytes -= 8;
1617 if ( mat->byteswap ) {
1618 (void)Mat_uint32Swap(uncomp_buf);
1619 (void)Mat_uint32Swap(uncomp_buf+1);
1620 }
1621 /* Rank and Dimension */
1622 if ( uncomp_buf[0] == MAT_T_INT32 ) {
1623 int j = 0;
1624
1625 cells[i]->rank = uncomp_buf[1];
1626 nbytes -= cells[i]->rank;
1627 cells[i]->rank /= 4;
1628 cells[i]->dims = malloc(cells[i]->rank*sizeof(*cells[i]->dims));
1629 if ( mat->byteswap ) {
1630 for ( j = 0; j < cells[i]->rank; j++ )
1631 cells[i]->dims[j] = Mat_uint32Swap(uncomp_buf+2+j);
1632 } else {
1633 for ( j = 0; j < cells[i]->rank; j++ )
1634 cells[i]->dims[j] = uncomp_buf[2+j];
1635 }
1636 if ( cells[i]->rank % 2 != 0 )
1637 nbytes -= 4;
1638 }
1639 bytesread += InflateVarNameTag(mat,matvar,uncomp_buf);
1640 nbytes -= 8;
1641 cells[i]->internal->z = calloc(1,sizeof(z_stream));
1642 err = inflateCopy(cells[i]->internal->z,matvar->internal->z);
1643 if ( err != Z_OK )
1644 Mat_Critical("inflateCopy returned error %d",err);
1645 cells[i]->internal->datapos = ftell(mat->fp)-matvar->internal->z->avail_in;
1646 if ( cells[i]->class_type == MAT_C_STRUCT )
1647 bytesread+=ReadNextStructField(mat,cells[i]);
1648 else if ( cells[i]->class_type == MAT_C_CELL )
1649 bytesread+=ReadNextCell(mat,cells[i]);
1650 fseek(mat->fp,cells[i]->internal->datapos,SEEK_SET);
1651 bytesread+=InflateSkip(mat,matvar->internal->z,nbytes);
1652 }
1653 #else
1654 Mat_Critical("Not compiled with zlib support");
1655 #endif
1656
1657 } else {
1658 int ncells;
1659 mat_uint32_t buf[16];
1660 int nbytes,nBytes;
1661 mat_uint32_t array_flags;
1662
1663 ncells = 1;
1664 for ( i = 0; i < matvar->rank; i++ )
1665 ncells *= matvar->dims[i];
1666 matvar->data_size = sizeof(matvar_t *);
1667 matvar->nbytes = ncells*matvar->data_size;
1668 matvar->data = malloc(matvar->nbytes);
1669 if ( !matvar->data ) {
1670 Mat_Critical("Couldn't allocate memory for %s->data",matvar->name);
1671 return bytesread;
1672 }
1673 cells = (matvar_t **)matvar->data;
1674 for ( i = 0; i < ncells; i++ ) {
1675 int cell_bytes_read;
1676 cells[i] = Mat_VarCalloc();
1677 if ( !cells[i] ) {
1678 Mat_Critical("Couldn't allocate memory for cell %d", i);
1679 continue;
1680 }
1681
1682 cells[i]->internal->fpos = ftell(mat->fp);
1683
1684 /* Read variable tag for cell */
1685 cell_bytes_read = fread(buf,4,2,mat->fp);
1686
1687 /* Empty cells at the end of a file may cause an EOF */
1688 if ( !cell_bytes_read )
1689 continue;
1690 bytesread += cell_bytes_read;
1691 if ( mat->byteswap ) {
1692 (void)Mat_uint32Swap(buf);
1693 (void)Mat_uint32Swap(buf+1);
1694 }
1695 nBytes = buf[1];
1696 if ( !nBytes ) {
1697 /* empty cell */
1698 continue;
1699 } else if ( buf[0] != MAT_T_MATRIX ) {
1700 Mat_Critical("cells[%d] not MAT_T_MATRIX, fpos = %ld",i,ftell(mat->fp));
1701 Mat_VarFree(cells[i]);
1702 cells[i] = NULL;
1703 break;
1704 }
1705 cells[i]->compression = 0;
1706 #if defined(HAVE_ZLIB)
1707 cells[i]->internal->z = NULL;
1708 #endif
1709
1710 /* Read Array Flags and The Dimensions Tag */
1711 bytesread += fread(buf,4,6,mat->fp);
1712 if ( mat->byteswap ) {
1713 (void)Mat_uint32Swap(buf);
1714 (void)Mat_uint32Swap(buf+1);
1715 (void)Mat_uint32Swap(buf+2);
1716 (void)Mat_uint32Swap(buf+3);
1717 (void)Mat_uint32Swap(buf+4);
1718 (void)Mat_uint32Swap(buf+5);
1719 }
1720 nBytes-=24;
1721 /* Array Flags */
1722 if ( buf[0] == MAT_T_UINT32 ) {
1723 array_flags = buf[2];
1724 cells[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
1725 cells[i]->isComplex = (array_flags & MAT_F_COMPLEX);
1726 cells[i]->isGlobal = (array_flags & MAT_F_GLOBAL);
1727 cells[i]->isLogical = (array_flags & MAT_F_LOGICAL);
1728 if ( cells[i]->class_type == MAT_C_SPARSE ) {
1729 /* Need to find a more appropriate place to store nzmax */
1730 cells[i]->nbytes = buf[3];
1731 }
1732 }
1733 /* Rank and Dimension */
1734 if ( buf[4] == MAT_T_INT32 ) {
1735 int j;
1736 nbytes = buf[5];
1737 nBytes-=nbytes;
1738
1739 cells[i]->rank = nbytes / 4;
1740 cells[i]->dims = malloc(cells[i]->rank*sizeof(*cells[i]->dims));
1741
1742 /* Assumes rank <= 16 */
1743 if ( cells[i]->rank % 2 != 0 ) {
1744 bytesread+=fread(buf,4,cells[i]->rank+1,mat->fp);
1745 nBytes-=4;
1746 } else
1747 bytesread+=fread(buf,4,cells[i]->rank,mat->fp);
1748
1749 if ( mat->byteswap ) {
1750 for ( j = 0; j < cells[i]->rank; j++ )
1751 cells[i]->dims[j] = Mat_uint32Swap(buf+j);
1752 } else {
1753 for ( j = 0; j < cells[i]->rank; j++ )
1754 cells[i]->dims[j] = buf[j];
1755 }
1756 }
1757 /* Variable Name Tag */
1758 bytesread+=fread(buf,1,8,mat->fp);
1759 nBytes-=8;
1760 cells[i]->internal->datapos = ftell(mat->fp);
1761 if ( cells[i]->class_type == MAT_C_STRUCT )
1762 bytesread+=ReadNextStructField(mat,cells[i]);
1763 if ( cells[i]->class_type == MAT_C_CELL )
1764 bytesread+=ReadNextCell(mat,cells[i]);
1765 fseek(mat->fp,cells[i]->internal->datapos+nBytes,SEEK_SET);
1766 }
1767 }
1768
1769 return bytesread;
1770 }
1771
1772 /** @brief Reads the next struct field of the structure in @c matvar
1773 *
1774 * Reads the next struct fields (fieldname length,names,data headers for all
1775 * the fields
1776 * @ingroup mat_internal
1777 * @param mat MAT file pointer
1778 * @param matvar MAT variable pointer
1779 * @return Number of bytes read
1780 */
1781 static int
1782 ReadNextStructField( mat_t *mat, matvar_t *matvar )
1783 {
1784 int fieldname_size,nfields, bytesread = 0, i, err;
1785 matvar_t **fields = NULL;
1786
1787 if ( matvar->compression ) {
1788 #if defined(HAVE_ZLIB)
1789 char *ptr;
1790 mat_uint32_t uncomp_buf[16] = {0,};
1791 int nbytes, j, nmemb = 1;
1792 mat_uint32_t array_flags;
1793
1794 for ( i = 0; i < matvar->rank; i++ )
1795 nmemb *= matvar->dims[i];
1796
1797 /* Inflate Field name length */
1798 bytesread += InflateFieldNameLength(mat,matvar,uncomp_buf);
1799 if ( mat->byteswap ) {
1800 (void)Mat_uint32Swap(uncomp_buf);
1801 (void)Mat_uint32Swap(uncomp_buf+1);
1802 }
1803 if ( (uncomp_buf[0] & 0x0000ffff) == MAT_T_INT32 ) {
1804 fieldname_size = uncomp_buf[1];
1805 } else {
1806 Mat_Warning("Error getting fieldname size");
1807 return bytesread;
1808 }
1809
1810 bytesread += InflateFieldNamesTag(mat,matvar,uncomp_buf);
1811 if ( mat->byteswap ) {
1812 (void)Mat_uint32Swap(uncomp_buf);
1813 (void)Mat_uint32Swap(uncomp_buf+1);
1814 }
1815 nfields = uncomp_buf[1];
1816 nfields = nfields / fieldname_size;
1817 matvar->data_size = sizeof(matvar_t *);
1818
1819 if ( nfields*fieldname_size % 8 != 0 )
1820 i = 8-(nfields*fieldname_size % 8);
1821 else
1822 i = 0;
1823 ptr = malloc(nfields*fieldname_size+i);
1824 bytesread += InflateFieldNames(mat,matvar,ptr,nfields,fieldname_size,i);
1825 matvar->internal->num_fields = nfields;
1826 matvar->internal->fieldnames =
1827 calloc(nfields,sizeof(*matvar->internal->fieldnames));
1828 for ( i = 0; i < nfields; i++ ) {
1829 matvar->internal->fieldnames[i] = malloc(fieldname_size);
1830 memcpy(matvar->internal->fieldnames[i],ptr+i*fieldname_size,
1831 fieldname_size);
1832 matvar->internal->fieldnames[i][fieldname_size-1] = '\0';
1833 }
1834 free(ptr);
1835
1836 matvar->nbytes = nmemb*nfields*matvar->data_size;
1837 if ( !matvar->nbytes )
1838 return bytesread;
1839
1840 matvar->data = malloc(matvar->nbytes);
1841 if ( !matvar->data )
1842 return bytesread;
1843
1844 fields = matvar->data;
1845 for ( i = 0; i < nmemb; i++ ) {
1846 for ( j = 0; j < nfields; j++ ) {
1847 fields[i*nfields+j] = Mat_VarCalloc();
1848 fields[i*nfields+j]->name = strdup(matvar->internal->fieldnames[j]);
1849 }
1850 }
1851
1852 for ( i = 0; i < nmemb*nfields; i++ ) {
1853 fields[i]->internal->fpos = ftell(mat->fp)-matvar->internal->z->avail_in;
1854 /* Read variable tag for struct field */
1855 bytesread += InflateVarTag(mat,matvar,uncomp_buf);
1856 if ( mat->byteswap ) {
1857 (void)Mat_uint32Swap(uncomp_buf);
1858 (void)Mat_uint32Swap(uncomp_buf+1);
1859 }
1860 nbytes = uncomp_buf[1];
1861 if ( uncomp_buf[0] != MAT_T_MATRIX ) {
1862 Mat_Critical("fields[%d], Uncompressed type not MAT_T_MATRIX",i);
1863 Mat_VarFree(fields[i]);
1864 fields[i] = NULL;
1865 continue;
1866 } else if ( nbytes == 0 ) {
1867 fields[i]->rank = 0;
1868 continue;
1869 }
1870 fields[i]->compression = MAT_COMPRESSION_ZLIB;
1871 bytesread += InflateArrayFlags(mat,matvar,uncomp_buf);
1872 nbytes -= 16;
1873 if ( mat->byteswap ) {
1874 (void)Mat_uint32Swap(uncomp_buf);
1875 (void)Mat_uint32Swap(uncomp_buf+1);
1876 (void)Mat_uint32Swap(uncomp_buf+2);
1877 (void)Mat_uint32Swap(uncomp_buf+3);
1878 }
1879 /* Array Flags */
1880 if ( uncomp_buf[0] == MAT_T_UINT32 ) {
1881 array_flags = uncomp_buf[2];
1882 fields[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
1883 fields[i]->isComplex = (array_flags & MAT_F_COMPLEX);
1884 fields[i]->isGlobal = (array_flags & MAT_F_GLOBAL);
1885 fields[i]->isLogical = (array_flags & MAT_F_LOGICAL);
1886 if ( fields[i]->class_type == MAT_C_SPARSE ) {
1887 /* Need to find a more appropriate place to store nzmax */
1888 fields[i]->nbytes = uncomp_buf[3];
1889 }
1890 } else {
1891 Mat_Critical("Expected MAT_T_UINT32 for Array Tags, got %d",
1892 uncomp_buf[0]);
1893 bytesread+=InflateSkip(mat,matvar->internal->z,nbytes);
1894 }
1895 bytesread += InflateDimensions(mat,matvar,uncomp_buf);
1896 nbytes -= 8;
1897 if ( mat->byteswap ) {
1898 (void)Mat_uint32Swap(uncomp_buf);
1899 (void)Mat_uint32Swap(uncomp_buf+1);
1900 }
1901 /* Rank and Dimension */
1902 if ( uncomp_buf[0] == MAT_T_INT32 ) {
1903 int j = 0;
1904
1905 fields[i]->rank = uncomp_buf[1];
1906 nbytes -= fields[i]->rank;
1907 fields[i]->rank /= 4;
1908 fields[i]->dims = malloc(fields[i]->rank*
1909 sizeof(*fields[i]->dims));
1910 if ( mat->byteswap ) {
1911 for ( j = 0; j < fields[i]->rank; j++ )
1912 fields[i]->dims[j] = Mat_uint32Swap(uncomp_buf+2+j);
1913 } else {
1914 for ( j = 0; j < fields[i]->rank; j++ )
1915 fields[i]->dims[j] = uncomp_buf[2+j];
1916 }
1917 if ( fields[i]->rank % 2 != 0 )
1918 nbytes -= 4;
1919 }
1920 bytesread += InflateVarNameTag(mat,matvar,uncomp_buf);
1921 nbytes -= 8;
1922 fields[i]->internal->z = calloc(1,sizeof(z_stream));
1923 err = inflateCopy(fields[i]->internal->z,matvar->internal->z);
1924 if ( err != Z_OK ) {
1925 Mat_Critical("inflateCopy returned error %d",err);
1926 }
1927 fields[i]->internal->datapos = ftell(mat->fp)-matvar->internal->z->avail_in;
1928 if ( fields[i]->class_type == MAT_C_STRUCT )
1929 bytesread+=ReadNextStructField(mat,fields[i]);
1930 else if ( fields[i]->class_type == MAT_C_CELL )
1931 bytesread+=ReadNextCell(mat,fields[i]);
1932 fseek(mat->fp,fields[i]->internal->datapos,SEEK_SET);
1933 bytesread+=InflateSkip(mat,matvar->internal->z,nbytes);
1934 }
1935 #else
1936 Mat_Critical("Not compiled with zlib support");
1937 #endif
1938 } else {
1939 int fieldname_size,nfields;
1940 mat_uint32_t buf[16] = {0,};
1941 int nbytes,nBytes,nmemb=1,j;
1942 mat_uint32_t array_flags;
1943
1944 for ( i = 0; i < matvar->rank; i++ )
1945 nmemb *= matvar->dims[i];
1946
1947 bytesread+=fread(buf,4,2,mat->fp);
1948 if ( mat->byteswap ) {
1949 (void)Mat_uint32Swap(buf);
1950 (void)Mat_uint32Swap(buf+1);
1951 }
1952 if ( (buf[0] & 0x0000ffff) == MAT_T_INT32 ) {
1953 fieldname_size = buf[1];
1954 } else {
1955 Mat_Warning("Error getting fieldname size");
1956 return bytesread;
1957 }
1958 bytesread+=fread(buf,4,2,mat->fp);
1959 if ( mat->byteswap ) {
1960 (void)Mat_uint32Swap(buf);
1961 (void)Mat_uint32Swap(buf+1);
1962 }
1963 nfields = buf[1];
1964 nfields = nfields / fieldname_size;
1965 matvar->data_size = sizeof(matvar_t *);
1966
1967 matvar->internal->num_fields = nfields;
1968 matvar->internal->fieldnames =
1969 calloc(nfields,sizeof(*matvar->internal->fieldnames));
1970 for ( i = 0; i < nfields; i++ ) {
1971 matvar->internal->fieldnames[i] = malloc(fieldname_size);
1972 bytesread+=fread(matvar->internal->fieldnames[i],1,fieldname_size,mat->fp);
1973 matvar->internal->fieldnames[i][fieldname_size-1] = '\0';
1974 }
1975
1976 if ( (nfields*fieldname_size) % 8 ) {
1977 fseek(mat->fp,8-((nfields*fieldname_size) % 8),SEEK_CUR);
1978 bytesread+=8-((nfields*fieldname_size) % 8);
1979 }
1980
1981 matvar->nbytes = nmemb*nfields*matvar->data_size;
1982 if ( !matvar->nbytes )
1983 return bytesread;
1984
1985 matvar->data = malloc(matvar->nbytes);
1986 if ( !matvar->data )
1987 return bytesread;
1988
1989 fields = matvar->data;
1990 for ( i = 0; i < nmemb; i++ ) {
1991 for ( j = 0; j < nfields; j++ ) {
1992 fields[i*nfields+j] = Mat_VarCalloc();
1993 fields[i*nfields+j]->name = strdup(matvar->internal->fieldnames[j]);
1994 }
1995 }
1996
1997 for ( i = 0; i < nmemb*nfields; i++ ) {
1998
1999 fields[i]->internal->fpos = ftell(mat->fp);
2000
2001 /* Read variable tag for struct field */
2002 bytesread += fread(buf,4,2,mat->fp);
2003 if ( mat->byteswap ) {
2004 (void)Mat_uint32Swap(buf);
2005 (void)Mat_uint32Swap(buf+1);
2006 }
2007 nBytes = buf[1];
2008 if ( buf[0] != MAT_T_MATRIX ) {
2009 Mat_Critical("fields[%d] not MAT_T_MATRIX, fpos = %ld",i,ftell(mat->fp));
2010 Mat_VarFree(fields[i]);
2011 fields[i] = NULL;
2012 return bytesread;
2013 } else if ( nBytes == 0 ) {
2014 fields[i]->rank = 0;
2015 continue;
2016 }
2017 fields[i]->compression = 0;
2018 #if defined(HAVE_ZLIB)
2019 fields[i]->internal->z = NULL;
2020 #endif
2021
2022 /* Read Array Flags and The Dimensions Tag */
2023 bytesread += fread(buf,4,6,mat->fp);
2024 if ( mat->byteswap ) {
2025 (void)Mat_uint32Swap(buf);
2026 (void)Mat_uint32Swap(buf+1);
2027 (void)Mat_uint32Swap(buf+2);
2028 (void)Mat_uint32Swap(buf+3);
2029 (void)Mat_uint32Swap(buf+4);
2030 (void)Mat_uint32Swap(buf+5);
2031 }
2032 nBytes-=24;
2033 /* Array Flags */
2034 if ( buf[0] == MAT_T_UINT32 ) {
2035 array_flags = buf[2];
2036 fields[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
2037 fields[i]->isComplex = (array_flags & MAT_F_COMPLEX);
2038 fields[i]->isGlobal = (array_flags & MAT_F_GLOBAL);
2039 fields[i]->isLogical = (array_flags & MAT_F_LOGICAL);
2040 if ( fields[i]->class_type == MAT_C_SPARSE ) {
2041 /* Need to find a more appropriate place to store nzmax */
2042 fields[i]->nbytes = buf[3];
2043 }
2044 }
2045 /* Rank and Dimension */
2046 if ( buf[4] == MAT_T_INT32 ) {
2047 int j;
2048
2049 nbytes = buf[5];
2050 nBytes-=nbytes;
2051
2052 fields[i]->rank = nbytes / 4;
2053 fields[i]->dims = malloc(fields[i]->rank*
2054 sizeof(*fields[i]->dims));
2055
2056 /* Assumes rank <= 16 */
2057 if ( fields[i]->rank % 2 != 0 ) {
2058 bytesread+=fread(buf,4,fields[i]->rank+1,mat->fp);
2059 nBytes-=4;
2060 } else
2061 bytesread+=fread(buf,4,fields[i]->rank,mat->fp);
2062
2063 if ( mat->byteswap ) {
2064 for ( j = 0; j < fields[i]->rank; j++ )
2065 fields[i]->dims[j] = Mat_uint32Swap(buf+j);
2066 } else {
2067 for ( j = 0; j < fields[i]->rank; j++ )
2068 fields[i]->dims[j] = buf[j];
2069 }
2070 }
2071 /* Variable Name Tag */
2072 bytesread+=fread(buf,1,8,mat->fp);
2073 nBytes-=8;
2074 fields[i]->internal->datapos = ftell(mat->fp);
2075 if ( fields[i]->class_type == MAT_C_STRUCT )
2076 bytesread+=ReadNextStructField(mat,fields[i]);
2077 else if ( fields[i]->class_type == MAT_C_CELL )
2078 bytesread+=ReadNextCell(mat,fields[i]);
2079 fseek(mat->fp,fields[i]->internal->datapos+nBytes,SEEK_SET);
2080 }
2081 }
2082
2083 return bytesread;
2084 }
2085
2086 /** @brief Reads the function handle data of the function handle in @c matvar
2087 *
2088 * @ingroup mat_internal
2089 * @param mat MAT file pointer
2090 * @param matvar MAT variable pointer
2091 * @return Number of bytes read
2092 */
2093 static int
2094 ReadNextFunctionHandle(mat_t *mat, matvar_t *matvar)
2095 {
2096 int nfunctions = 1, bytesread = 0, i;
2097 matvar_t **functions = NULL;
2098
2099 for ( i = 0; i < matvar->rank; i++ )
2100 nfunctions *= matvar->dims[i];
2101
2102 matvar->data = malloc(nfunctions*sizeof(matvar_t *));
2103 if ( matvar->data != NULL ) {
2104 matvar->data_size = sizeof(matvar_t *);
2105 matvar->nbytes = nfunctions*matvar->data_size;
2106 functions = matvar->data;
2107 for ( i = 0 ; i < nfunctions; i++ )
2108 functions[i] = Mat_VarReadNextInfo(mat);
2109 } else {
2110 bytesread = 0;
2111 matvar->data_size = 0;
2112 matvar->nbytes = 0;
2113 }
2114
2115 return bytesread;
2116 }
2117
2118 /** @brief Writes the header and blank data for a cell array
2119 *
2120 * @ingroup mat_internal
2121 * @param mat MAT file pointer
2122 * @param matvar pointer to the mat variable
2123 * @return number of bytes written
2124 */
2125 static int
2126 WriteCellArrayFieldInfo(mat_t *mat,matvar_t *matvar)
2127 {
2128 mat_uint32_t array_flags = 0x0;
2129 mat_int16_t array_name_type = MAT_T_INT8;
2130 int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
2131 int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
2132 mat_int8_t pad1 = 0;
2133 int nBytes, i, nmemb = 1;
2134 long start = 0, end = 0;
2135
2136 if ((matvar == NULL) || (mat == NULL))
2137 return 0;
2138
2139 #if 0
2140 nBytes = GetMatrixMaxBufSize(matvar);
2141 #endif
2142
2143 fwrite(&matrix_type,4,1,mat->fp);
2144 fwrite(&pad4,4,1,mat->fp);
2145 start = ftell(mat->fp);
2146
2147 /* Array Flags */
2148 array_flags = matvar->class_type & CLASS_TYPE_MASK;
2149 if ( matvar->isComplex )
2150 array_flags |= MAT_F_COMPLEX;
2151 if ( matvar->isGlobal )
2152 array_flags |= MAT_F_GLOBAL;
2153 if ( matvar->isLogical )
2154 array_flags |= MAT_F_LOGICAL;
2155
2156 if ( mat->byteswap )
2157 array_flags = Mat_int32Swap((mat_int32_t*)&array_flags);
2158 fwrite(&array_flags_type,4,1,mat->fp);
2159 fwrite(&array_flags_size,4,1,mat->fp);
2160 fwrite(&array_flags,4,1,mat->fp);
2161 fwrite(&pad4,4,1,mat->fp);
2162 /* Rank and Dimension */
2163 nBytes = matvar->rank * 4;
2164 fwrite(&dims_array_type,4,1,mat->fp);
2165 fwrite(&nBytes,4,1,mat->fp);
2166 for ( i = 0; i < matvar->rank; i++ ) {
2167 mat_int32_t dim;
2168 dim = matvar->dims[i];
2169 nmemb *= dim;
2170 fwrite(&dim,4,1,mat->fp);
2171 }
2172 if ( matvar->rank % 2 != 0 )
2173 fwrite(&pad4,4,1,mat->fp);
2174 /* Name of variable */
2175 if ( !matvar->name ) {
2176 fwrite(&array_name_type,2,1,mat->fp);
2177 fwrite(&pad1,1,1,mat->fp);
2178 fwrite(&pad1,1,1,mat->fp);
2179 fwrite(&pad4,4,1,mat->fp);
2180 } else if ( strlen(matvar->name) <= 4 ) {
2181 mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
2182 mat_int8_t pad1 = 0;
2183 fwrite(&array_name_type,2,1,mat->fp);
2184 fwrite(&array_name_len,2,1,mat->fp);
2185 fwrite(matvar->name,1,array_name_len,mat->fp);
2186 for ( i = array_name_len; i < 4; i++ )
2187 fwrite(&pad1,1,1,mat->fp);
2188 } else {
2189 mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
2190 mat_int8_t pad1 = 0;
2191
2192 fwrite(&array_name_type,2,1,mat->fp);
2193 fwrite(&pad1,1,1,mat->fp);
2194 fwrite(&pad1,1,1,mat->fp);
2195 fwrite(&array_name_len,4,1,mat->fp);
2196 fwrite(matvar->name,1,array_name_len,mat->fp);
2197 if ( array_name_len % 8 )
2198 for ( i = array_name_len % 8; i < 8; i++ )
2199 fwrite(&pad1,1,1,mat->fp);
2200 }
2201
2202 matvar->internal->datapos = ftell(mat->fp);
2203 switch ( matvar->class_type ) {
2204 case MAT_C_DOUBLE:
2205 case MAT_C_SINGLE:
2206 case MAT_C_INT64:
2207 case MAT_C_UINT64:
2208 case MAT_C_INT32:
2209 case MAT_C_UINT32:
2210 case MAT_C_INT16:
2211 case MAT_C_UINT16:
2212 case MAT_C_INT8:
2213 case MAT_C_UINT8:
2214 nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
2215 if ( nBytes % 8 )
2216 for ( i = nBytes % 8; i < 8; i++ )
2217 fwrite(&pad1,1,1,mat->fp);
2218 if ( matvar->isComplex ) {
2219 nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
2220 if ( nBytes % 8 )
2221 for ( i = nBytes % 8; i < 8; i++ )
2222 fwrite(&pad1,1,1,mat->fp);
2223 }
2224 break;
2225 case MAT_C_CHAR:
2226 {
2227 WriteEmptyCharData(mat,nmemb,matvar->data_type);
2228 break;
2229 }
2230 case MAT_C_CELL:
2231 {
2232 int ncells;
2233 matvar_t **cells = (matvar_t **)matvar->data;
2234
2235 /* Check for an empty cell array */
2236 if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
2237 matvar->data == NULL )
2238 break;
2239 ncells = matvar->nbytes / matvar->data_size;
2240
2241 for ( i = 0; i < ncells; i++ )
2242 WriteCellArrayFieldInfo(mat,cells[i]);
2243 break;
2244 }
2245 /* FIXME: Structures */
2246 }
2247 end = ftell(mat->fp);
2248 nBytes = (int)(end-start);
2249 fseek(mat->fp,(long)-(nBytes+4),SEEK_CUR);
2250 fwrite(&nBytes,4,1,mat->fp);
2251 fseek(mat->fp,end,SEEK_SET);
2252 return 0;
2253 }
2254
2255 /** @brief Writes the header and data for an element of a cell array
2256 *
2257 * @ingroup mat_internal
2258 * @param mat MAT file pointer
2259 * @param matvar pointer to the mat variable
2260 * @retval 0 on success
2261 */
2262 static int
2263 WriteCellArrayField(mat_t *mat,matvar_t *matvar )
2264 {
2265 mat_uint32_t array_flags = 0x0;
2266 mat_int16_t array_name_type = MAT_T_INT8,fieldname_type = MAT_T_INT32,fieldname_data_size=4;
2267 int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
2268 int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
2269 mat_int8_t pad1 = 0;
2270 int nBytes, i, nmemb = 1, nzmax = 0;
2271 long start = 0, end = 0;
2272
2273 if ((matvar == NULL) || (mat == NULL))
2274 return 1;
2275
2276 #if 0
2277 nBytes = GetMatrixMaxBufSize(matvar);
2278 #endif
2279
2280 fwrite(&matrix_type,4,1,mat->fp);
2281 fwrite(&pad4,4,1,mat->fp);
2282 start = ftell(mat->fp);
2283
2284 /* Array Flags */
2285 array_flags = matvar->class_type & CLASS_TYPE_MASK;
2286 if ( matvar->isComplex )
2287 array_flags |= MAT_F_COMPLEX;
2288 if ( matvar->isGlobal )
2289 array_flags |= MAT_F_GLOBAL;
2290 if ( matvar->isLogical )
2291 array_flags |= MAT_F_LOGICAL;
2292 if ( matvar->class_type == MAT_C_SPARSE )
2293 nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
2294
2295 if ( mat->byteswap )
2296 array_flags = Mat_int32Swap((mat_int32_t*)&array_flags);
2297 fwrite(&array_flags_type,4,1,mat->fp);
2298 fwrite(&array_flags_size,4,1,mat->fp);
2299 fwrite(&array_flags,4,1,mat->fp);
2300 fwrite(&nzmax,4,1,mat->fp);
2301 /* Rank and Dimension */
2302 nBytes = matvar->rank * 4;
2303 fwrite(&dims_array_type,4,1,mat->fp);
2304 fwrite(&nBytes,4,1,mat->fp);
2305 for ( i = 0; i < matvar->rank; i++ ) {
2306 mat_int32_t dim;
2307 dim = matvar->dims[i];
2308 nmemb *= dim;
2309 fwrite(&dim,4,1,mat->fp);
2310 }
2311 if ( matvar->rank % 2 != 0 )
2312 fwrite(&pad4,4,1,mat->fp);
2313 /* Name of variable */
2314 if ( !matvar->name ) {
2315 fwrite(&array_name_type,2,1,mat->fp);
2316 fwrite(&pad1,1,1,mat->fp);
2317 fwrite(&pad1,1,1,mat->fp);
2318 fwrite(&pad4,4,1,mat->fp);
2319 } else if ( strlen(matvar->name) <= 4 ) {
2320 mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
2321 mat_int8_t pad1 = 0;
2322 fwrite(&array_name_type,2,1,mat->fp);
2323 fwrite(&array_name_len,2,1,mat->fp);
2324 fwrite(matvar->name,1,array_name_len,mat->fp);
2325 for ( i = array_name_len; i < 4; i++ )
2326 fwrite(&pad1,1,1,mat->fp);
2327 } else {
2328 mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
2329 mat_int8_t pad1 = 0;
2330
2331 fwrite(&array_name_type,2,1,mat->fp);
2332 fwrite(&pad1,1,1,mat->fp);
2333 fwrite(&pad1,1,1,mat->fp);
2334 fwrite(&array_name_len,4,1,mat->fp);
2335 fwrite(matvar->name,1,array_name_len,mat->fp);
2336 if ( array_name_len % 8 )
2337 for ( i = array_name_len % 8; i < 8; i++ )
2338 fwrite(&pad1,1,1,mat->fp);
2339 }
2340
2341 switch ( matvar->class_type ) {
2342 case MAT_C_DOUBLE:
2343 case MAT_C_SINGLE:
2344 case MAT_C_INT64:
2345 case MAT_C_UINT64:
2346 case MAT_C_INT32:
2347 case MAT_C_UINT32:
2348 case MAT_C_INT16:
2349 case MAT_C_UINT16:
2350 case MAT_C_INT8:
2351 case MAT_C_UINT8:
2352 {
2353 if ( matvar->isComplex ) {
2354 mat_complex_split_t *complex_data = matvar->data;
2355
2356 if ( NULL == matvar->data )
2357 complex_data = &null_complex_data;
2358
2359 nBytes=WriteData(mat,complex_data->Re,nmemb,matvar->data_type);
2360 if ( nBytes % 8 )
2361 for ( i = nBytes % 8; i < 8; i++ )
2362 fwrite(&pad1,1,1,mat->fp);
2363 nBytes=WriteData(mat,complex_data->Im,nmemb,matvar->data_type);
2364 if ( nBytes % 8 )
2365 for ( i = nBytes % 8; i < 8; i++ )
2366 fwrite(&pad1,1,1,mat->fp);
2367 } else {
2368 nBytes = WriteData(mat,matvar->data,nmemb,matvar->data_type);
2369 if ( nBytes % 8 )
2370 for ( i = nBytes % 8; i < 8; i++ )
2371 fwrite(&pad1,1,1,mat->fp);
2372 }
2373 break;
2374 }
2375 case MAT_C_CHAR:
2376 WriteCharData(mat,matvar->data,nmemb,matvar->data_type);
2377 break;
2378 case MAT_C_CELL:
2379 {
2380 int ncells;
2381 matvar_t **cells = (matvar_t **)matvar->data;
2382
2383 /* Check for an empty cell array */
2384 if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
2385 matvar->data == NULL )
2386 break;
2387 ncells = matvar->nbytes / matvar->data_size;
2388
2389 for ( i = 0; i < ncells; i++ )
2390 WriteCellArrayField(mat,cells[i]);
2391 break;
2392 }
2393 case MAT_C_STRUCT:
2394 {
2395 char **fieldnames, *padzero;
2396 int fieldname_size, nfields;
2397 size_t maxlen = 0;
2398 matvar_t **fields = (matvar_t **)matvar->data;
2399 unsigned fieldname;
2400
2401 if ( nmemb && matvar->data_size )
2402 nfields = matvar->nbytes / (nmemb*matvar->data_size);
2403 else if ( matvar->data_size )
2404 nfields = matvar->nbytes / matvar->data_size;
2405 else
2406 break;
2407
2408 fieldnames = malloc(nfields*sizeof(char *));
2409 for ( i = 0; i < nfields; i++ ) {
2410 fieldnames[i] = fields[i]->name;
2411 if ( strlen(fieldnames[i]) > maxlen )
2412 maxlen = strlen(fieldnames[i]);
2413 }
2414 maxlen++;
2415 fieldname_size = maxlen;
2416 while ( nfields*fieldname_size % 8 != 0 )
2417 fieldname_size++;
2418 #if 0
2419 fwrite(&fieldname_type,2,1,mat->fp);
2420 fwrite(&fieldname_data_size,2,1,mat->fp);
2421 #else
2422 fieldname = (fieldname_data_size<<16) | fieldname_type;
2423 fwrite(&fieldname,4,1,mat->fp);
2424 #endif
2425 fwrite(&fieldname_size,4,1,mat->fp);
2426 fwrite(&array_name_type,2,1,mat->fp);
2427 fwrite(&pad1,1,1,mat->fp);
2428 fwrite(&pad1,1,1,mat->fp);
2429 nBytes = nfields*fieldname_size;
2430 fwrite(&nBytes,4,1,mat->fp);
2431 padzero = calloc(fieldname_size,1);
2432 for ( i = 0; i < nfields; i++ ) {
2433 fwrite(fieldnames[i],1,strlen(fieldnames[i]),mat->fp);
2434 fwrite(padzero,1,fieldname_size-strlen(fieldnames[i]),mat->fp);
2435 }
2436 free(fieldnames);
2437 free(padzero);
2438 for ( i = 0; i < nmemb*nfields; i++ )
2439 WriteStructField(mat,fields[i]);
2440 break;
2441 }
2442 case MAT_C_SPARSE:
2443 {
2444 mat_sparse_t *sparse = matvar->data;
2445
2446 nBytes = WriteData(mat,sparse->ir,sparse->nir,MAT_T_INT32);
2447 if ( nBytes % 8 )
2448 for ( i = nBytes % 8; i < 8; i++ )
2449 fwrite(&pad1,1,1,mat->fp);
2450 nBytes = WriteData(mat,sparse->jc,sparse->njc,MAT_T_INT32);
2451 if ( nBytes % 8 )
2452 for ( i = nBytes % 8; i < 8; i++ )
2453 fwrite(&pad1,1,1,mat->fp);
2454 nBytes = WriteData(mat,sparse->data,sparse->ndata,matvar->data_type);
2455 if ( nBytes % 8 )
2456 for ( i = nBytes % 8; i < 8; i++ )
2457 fwrite(&pad1,1,1,mat->fp);
2458 }
2459 }
2460 end = ftell(mat->fp);
2461 nBytes = (int)(end-start);
2462 fseek(mat->fp,(long)-(nBytes+4),SEEK_CUR);
2463 fwrite(&nBytes,4,1,mat->fp);
2464 fseek(mat->fp,end,SEEK_SET);
2465 return 0;
2466 }
2467
2468 #if defined(HAVE_ZLIB)
2469 /** @brief Writes the header and data for a field of a compressed cell array
2470 *
2471 * @ingroup mat_internal
2472 * @param mat MAT file pointer
2473 * @param matvar pointer to the mat variable
2474 * @return number of bytes written to the MAT file
2475 */
2476 static size_t
2477 WriteCompressedCellArrayField(mat_t *mat,matvar_t *matvar,z_stream *z)
2478 {
2479 mat_uint32_t array_flags = 0x0;
2480 mat_int16_t array_name_type = MAT_T_INT8;
2481 mat_int16_t fieldname_type = MAT_T_INT32;
2482 mat_int16_t fieldname_data_size = 4;
2483 int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
2484 int array_flags_size = 8, pad4 = 0;
2485 int nBytes, i, nmemb = 1, nzmax = 0;
2486 long start = 0;
2487
2488 mat_uint32_t comp_buf[512];
2489 mat_uint32_t uncomp_buf[512] = {0,};
2490 int buf_size = 512, err;
2491 size_t byteswritten = 0;
2492
2493 if ( NULL == matvar || NULL == mat || NULL == z)
2494 return 0;
2495
2496 start = ftell(mat->fp);
2497
2498 /* Array Flags */
2499 array_flags = matvar->class_type & CLASS_TYPE_MASK;
2500 if ( matvar->isComplex )
2501 array_flags |= MAT_F_COMPLEX;
2502 if ( matvar->isGlobal )
2503 array_flags |= MAT_F_GLOBAL;
2504 if ( matvar->isLogical )
2505 array_flags |= MAT_F_LOGICAL;
2506 if ( matvar->class_type == MAT_C_SPARSE )
2507 nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
2508
2509 uncomp_buf[0] = MAT_T_MATRIX;
2510 uncomp_buf[1] = (int)GetCellArrayFieldBufSize(matvar);
2511 z->next_out = ZLIB_BYTE_PTR(comp_buf);
2512 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
2513 z->avail_out = buf_size*sizeof(*comp_buf);
2514 z->avail_in = 8;
2515 err = deflate(z,Z_NO_FLUSH);
2516 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
2517 mat->fp);
2518 uncomp_buf[0] = array_flags_type;
2519 uncomp_buf[1] = array_flags_size;
2520 uncomp_buf[2] = array_flags;
2521 uncomp_buf[3] = nzmax;
2522 /* Rank and Dimension */
2523 nBytes = matvar->rank * 4;
2524 uncomp_buf[4] = dims_array_type;
2525 uncomp_buf[5] = nBytes;
2526 for ( i = 0; i < matvar->rank; i++ ) {
2527 mat_int32_t dim;
2528 dim = matvar->dims[i];
2529 nmemb *= dim;
2530 uncomp_buf[6+i] = dim;
2531 }
2532 if ( matvar->rank % 2 != 0 ) {
2533 uncomp_buf[6+i] = pad4;
2534 i++;
2535 }
2536
2537 z->next_out = ZLIB_BYTE_PTR(comp_buf);
2538 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
2539 z->avail_out = buf_size*sizeof(*comp_buf);
2540 z->avail_in = (6+i)*sizeof(*uncomp_buf);
2541 err = deflate(z,Z_NO_FLUSH);
2542 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
2543 mat->fp);
2544 /* Name of variable */
2545 uncomp_buf[0] = array_name_type;
2546 uncomp_buf[1] = 0;
2547 z->next_out = ZLIB_BYTE_PTR(comp_buf);
2548 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
2549 z->avail_out = buf_size*sizeof(*comp_buf);
2550 z->avail_in = 8;
2551 err = deflate(z,Z_NO_FLUSH);
2552 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
2553 mat->fp);
2554
2555 matvar->internal->datapos = ftell(mat->fp);
2556 switch ( matvar->class_type ) {
2557 case MAT_C_DOUBLE:
2558 case MAT_C_SINGLE:
2559 case MAT_C_INT64:
2560 case MAT_C_UINT64:
2561 case MAT_C_INT32:
2562 case MAT_C_UINT32:
2563 case MAT_C_INT16:
2564 case MAT_C_UINT16:
2565 case MAT_C_INT8:
2566 case MAT_C_UINT8:
2567 {
2568 /* WriteCompressedData makes sure uncomressed data is aligned
2569 * on an 8-byte boundary */
2570 if ( matvar->isComplex ) {
2571 mat_complex_split_t *complex_data = matvar->data;
2572
2573 if ( NULL == matvar->data )
2574 complex_data = &null_complex_data;
2575
2576 byteswritten += WriteCompressedData(mat,z,
2577 complex_data->Re,nmemb,matvar->data_type);
2578 byteswritten += WriteCompressedData(mat,z,
2579 complex_data->Im,nmemb,matvar->data_type);
2580 } else {
2581 byteswritten += WriteCompressedData(mat,z,
2582 matvar->data,nmemb,matvar->data_type);
2583 }
2584 break;
2585 }
2586 case MAT_C_CHAR:
2587 {
2588 byteswritten += WriteCompressedCharData(mat,z,matvar->data,
2589 nmemb,matvar->data_type);
2590 break;
2591 }
2592 case MAT_C_CELL:
2593 {
2594 int ncells;
2595 matvar_t **cells = (matvar_t **)matvar->data;
2596
2597 /* Check for an empty cell array */
2598 if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
2599 matvar->data == NULL )
2600 break;
2601 ncells = matvar->nbytes / matvar->data_size;
2602 for ( i = 0; i < ncells; i++ )
2603 WriteCompressedCellArrayField(mat,cells[i],z);
2604 break;
2605 }
2606 case MAT_C_STRUCT:
2607 {
2608 char **fieldnames;
2609 unsigned char *padzero;
2610 int fieldname_size, nfields;
2611 size_t maxlen = 0;
2612 mat_int32_t array_name_type = MAT_T_INT8;
2613 matvar_t **fields = (matvar_t **)matvar->data;
2614
2615 /* Check for a structure with no fields */
2616 if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
2617 matvar->data == NULL ) {
2618 fieldname_size = 1;
2619 uncomp_buf[0] = (fieldname_data_size << 16) |
2620 fieldname_type;
2621 uncomp_buf[1] = 1;
2622 uncomp_buf[2] = array_name_type;
2623 uncomp_buf[3] = 0;
2624 z->next_out = ZLIB_BYTE_PTR(comp_buf);
2625 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
2626 z->avail_out = buf_size*sizeof(*comp_buf);
2627 z->avail_in = 16;
2628 err = deflate(z,Z_NO_FLUSH);
2629 byteswritten += fwrite(comp_buf,1,buf_size*
2630 sizeof(*comp_buf)-z->avail_out,mat->fp);
2631 break;
2632 }
2633 if ( nmemb )
2634 nfields = matvar->nbytes / (nmemb*matvar->data_size);
2635 else /* matvar->data_size is checked above */
2636 nfields = matvar->nbytes / matvar->data_size;
2637
2638 fieldnames = malloc(nfields*sizeof(char *));
2639 for ( i = 0; i < nfields; i++ ) {
2640 fieldnames[i] = fields[i]->name;
2641 if ( strlen(fieldnames[i]) > maxlen )
2642 maxlen = strlen(fieldnames[i]);
2643 }
2644 maxlen++;
2645 fieldname_size = maxlen;
2646 while ( nfields*fieldname_size % 8 != 0 )
2647 fieldname_size++;
2648 uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type;
2649 uncomp_buf[1] = fieldname_size;
2650 uncomp_buf[2] = array_name_type;
2651 uncomp_buf[3] = nfields*fieldname_size;
2652
2653 padzero = calloc(fieldname_size,1);
2654 z->next_out = ZLIB_BYTE_PTR(comp_buf);
2655 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
2656 z->avail_out = buf_size*sizeof(*comp_buf);
2657 z->avail_in = 16;
2658 err = deflate(z,Z_NO_FLUSH);
2659 byteswritten += fwrite(comp_buf,1,
2660 buf_size*sizeof(*comp_buf)-z->avail_out,mat->fp);
2661 for ( i = 0; i < nfields; i++ ) {
2662 memset(padzero,'\0',fieldname_size);
2663 memcpy(padzero,fieldnames[i],strlen(fieldnames[i]));
2664 z->next_out = ZLIB_BYTE_PTR(comp_buf);
2665 z->next_in = ZLIB_BYTE_PTR(padzero);
2666 z->avail_out = buf_size*sizeof(*comp_buf);
2667 z->avail_in = fieldname_size;
2668 err = deflate(z,Z_NO_FLUSH);
2669 byteswritten += fwrite(comp_buf,1,
2670 buf_size*sizeof(*comp_buf)-z->avail_out,mat->fp);
2671 }
2672 free(fieldnames);
2673 free(padzero);
2674 for ( i = 0; i < nmemb*nfields; i++ )
2675 byteswritten +=
2676 WriteCompressedStructField(mat,fields[i],z);
2677 break;
2678 }
2679 case MAT_C_SPARSE:
2680 {
2681 mat_sparse_t *sparse = matvar->data;
2682
2683 byteswritten += WriteCompressedData(mat,z,sparse->ir,
2684 sparse->nir,MAT_T_INT32);
2685 byteswritten += WriteCompressedData(mat,z,sparse->jc,
2686 sparse->njc,MAT_T_INT32);
2687 if ( matvar->isComplex ) {
2688 mat_complex_split_t *complex_data = sparse->data;
2689 byteswritten += WriteCompressedData(mat,z,
2690 complex_data->Re,sparse->ndata,matvar->data_type);
2691 byteswritten += WriteCompressedData(mat,z,
2692 complex_data->Im,sparse->ndata,matvar->data_type);
2693 } else {
2694 byteswritten += WriteCompressedData(mat,z,
2695 sparse->data,sparse->ndata,matvar->data_type);
2696 }
2697 break;
2698 }
2699 }
2700 return byteswritten;
2701 }
2702 #endif
2703
2704 /** @brief Writes the header and data for a field of a struct array
2705 *
2706 * @ingroup mat_internal
2707 * @param mat MAT file pointer
2708 * @param matvar pointer to the mat variable
2709 * @retval 0 on success
2710 */
2711 static int
2712 WriteStructField(mat_t *mat,matvar_t *matvar)
2713 {
2714 mat_uint32_t array_flags = 0x0;
2715 mat_int16_t fieldname_type = MAT_T_INT32,fieldname_data_size=4;
2716 mat_int32_t array_name_type = MAT_T_INT8;
2717 int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
2718 int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
2719 mat_int8_t pad1 = 0;
2720 int nBytes, i, nmemb = 1, nzmax = 0;
2721 long start = 0, end = 0;
2722
2723 if ( mat == NULL )
2724 return 1;
2725
2726 if ( NULL == matvar ) {
2727 size_t dims[2] = {0,0};
2728 Mat_WriteEmptyVariable5(mat, NULL, 2, dims);
2729 return 0;
2730 }
2731
2732 fwrite(&matrix_type,4,1,mat->fp);
2733 fwrite(&pad4,4,1,mat->fp);
2734 start = ftell(mat->fp);
2735
2736 /* Array Flags */
2737 array_flags = matvar->class_type & CLASS_TYPE_MASK;
2738 if ( matvar->isComplex )
2739 array_flags |= MAT_F_COMPLEX;
2740 if ( matvar->isGlobal )
2741 array_flags |= MAT_F_GLOBAL;
2742 if ( matvar->isLogical )
2743 array_flags |= MAT_F_LOGICAL;
2744 if ( matvar->class_type == MAT_C_SPARSE )
2745 nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
2746
2747 if ( mat->byteswap )
2748 array_flags = Mat_int32Swap((mat_int32_t*)&array_flags);
2749 fwrite(&array_flags_type,4,1,mat->fp);
2750 fwrite(&array_flags_size,4,1,mat->fp);
2751 fwrite(&array_flags,4,1,mat->fp);
2752 fwrite(&nzmax,4,1,mat->fp);
2753 /* Rank and Dimension */
2754 nBytes = matvar->rank * 4;
2755 fwrite(&dims_array_type,4,1,mat->fp);
2756 fwrite(&nBytes,4,1,mat->fp);
2757 for ( i = 0; i < matvar->rank; i++ ) {
2758 mat_int32_t dim;
2759 dim = matvar->dims[i];
2760 nmemb *= dim;
2761 fwrite(&dim,4,1,mat->fp);
2762 }
2763 if ( matvar->rank % 2 != 0 )
2764 fwrite(&pad4,4,1,mat->fp);
2765
2766 /* Name of variable */
2767 fwrite(&array_name_type,4,1,mat->fp);
2768 fwrite(&pad4,4,1,mat->fp);
2769
2770 switch ( matvar->class_type ) {
2771 case MAT_C_DOUBLE:
2772 case MAT_C_SINGLE:
2773 case MAT_C_INT64:
2774 case MAT_C_UINT64:
2775 case MAT_C_INT32:
2776 case MAT_C_UINT32:
2777 case MAT_C_INT16:
2778 case MAT_C_UINT16:
2779 case MAT_C_INT8:
2780 case MAT_C_UINT8:
2781 {
2782 if ( matvar->isComplex ) {
2783 mat_complex_split_t *complex_data = matvar->data;
2784
2785 if ( NULL == matvar->data )
2786 complex_data = &null_complex_data;
2787
2788 nBytes=WriteData(mat,complex_data->Re,nmemb,matvar->data_type);
2789 if ( nBytes % 8 )
2790 for ( i = nBytes % 8; i < 8; i++ )
2791 fwrite(&pad1,1,1,mat->fp);
2792 nBytes=WriteData(mat,complex_data->Im,nmemb,matvar->data_type);
2793 if ( nBytes % 8 )
2794 for ( i = nBytes % 8; i < 8; i++ )
2795 fwrite(&pad1,1,1,mat->fp);
2796 } else {
2797 nBytes=WriteData(mat,matvar->data,nmemb,matvar->data_type);
2798 if ( nBytes % 8 )
2799 for ( i = nBytes % 8; i < 8; i++ )
2800 fwrite(&pad1,1,1,mat->fp);
2801 }
2802 break;
2803 }
2804 case MAT_C_CHAR:
2805 nBytes=WriteCharData(mat,matvar->data,nmemb,matvar->data_type);
2806 break;
2807 case MAT_C_CELL:
2808 {
2809 int ncells;
2810 matvar_t **cells = (matvar_t **)matvar->data;
2811
2812 /* Check for an empty cell array */
2813 if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
2814 matvar->data == NULL )
2815 break;
2816 ncells = matvar->nbytes / matvar->data_size;
2817
2818 for ( i = 0; i < ncells; i++ )
2819 WriteCellArrayField(mat,cells[i]);
2820 break;
2821 }
2822 case MAT_C_STRUCT:
2823 {
2824 char *padzero;
2825 int fieldname_size, nfields = 0;
2826 size_t maxlen = 0;
2827 matvar_t **fields = (matvar_t **)matvar->data;
2828 unsigned fieldname;
2829
2830 /* nmemb*matvar->data_size can be zero when saving a struct that
2831 * contains an empty struct in one of its fields
2832 * (e.g. x.y = struct('z', {})). If it's zero, we would divide
2833 * by zero.
2834 */
2835 nfields = matvar->internal->num_fields;
2836
2837 for ( i = 0; i < nfields; i++ ) {
2838 size_t len = strlen(matvar->internal->fieldnames[i]);
2839 if ( len > maxlen )
2840 maxlen = len;
2841 }
2842 maxlen++;
2843 fieldname_size = maxlen;
2844 while ( nfields*fieldname_size % 8 != 0 )
2845 fieldname_size++;
2846 #if 0
2847 fwrite(&fieldname_type,2,1,mat->fp);
2848 fwrite(&fieldname_data_size,2,1,mat->fp);
2849 #else
2850 fieldname = (fieldname_data_size<<16) | fieldname_type;
2851 fwrite(&fieldname,4,1,mat->fp);
2852 #endif
2853 fwrite(&fieldname_size,4,1,mat->fp);
2854 fwrite(&array_name_type,4,1,mat->fp);
2855 nBytes = nfields*fieldname_size;
2856 fwrite(&nBytes,4,1,mat->fp);
2857 padzero = calloc(fieldname_size,1);
2858 for ( i = 0; i < nfields; i++ ) {
2859 size_t len = strlen(matvar->internal->fieldnames[i]);
2860 fwrite(matvar->internal->fieldnames[i],1,len,mat->fp);
2861 fwrite(padzero,1,fieldname_size-len,mat->fp);
2862 }
2863 free(padzero);
2864 for ( i = 0; i < nmemb*nfields; i++ )
2865 WriteStructField(mat,fields[i]);
2866 break;
2867 }
2868 case MAT_C_SPARSE:
2869 {
2870 mat_sparse_t *sparse = matvar->data;
2871
2872 nBytes = WriteData(mat,sparse->ir,sparse->nir,MAT_T_INT32);
2873 if ( nBytes % 8 )
2874 for ( i = nBytes % 8; i < 8; i++ )
2875 fwrite(&pad1,1,1,mat->fp);
2876 nBytes = WriteData(mat,sparse->jc,sparse->njc,MAT_T_INT32);
2877 if ( nBytes % 8 )
2878 for ( i = nBytes % 8; i < 8; i++ )
2879 fwrite(&pad1,1,1,mat->fp);
2880 nBytes = WriteData(mat,sparse->data,sparse->ndata,
2881 matvar->data_type);
2882 if ( nBytes % 8 )
2883 for ( i = nBytes % 8; i < 8; i++ )
2884 fwrite(&pad1,1,1,mat->fp);
2885 }
2886 }
2887 end = ftell(mat->fp);
2888 nBytes = (int)(end-start);
2889 fseek(mat->fp,(long)-(nBytes+4),SEEK_CUR);
2890 fwrite(&nBytes,4,1,mat->fp);
2891 fseek(mat->fp,end,SEEK_SET);
2892 return 0;
2893 }
2894
2895 #if defined(HAVE_ZLIB)
2896 /** @brief Writes the header and data for a field of a compressed struct array
2897 *
2898 * @ingroup mat_internal
2899 * @fixme Currently does not work for cell arrays or sparse data
2900 * @param mat MAT file pointer
2901 * @param matvar pointer to the mat variable
2902 * @return number of bytes written to the MAT file
2903 */
2904 static size_t
2905 WriteCompressedStructField(mat_t *mat,matvar_t *matvar,z_stream *z)
2906 {
2907 mat_uint32_t array_flags = 0x0;
2908 mat_int16_t array_name_type = MAT_T_INT8;
2909 mat_int16_t fieldname_type = MAT_T_INT32;
2910 mat_int16_t fieldname_data_size = 4;
2911 int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
2912 int array_flags_size = 8, pad4 = 0;
2913 int nBytes, i, nmemb = 1, nzmax = 0;
2914 long start = 0;
2915
2916 mat_uint32_t comp_buf[512];
2917 mat_uint32_t uncomp_buf[512] = {0,};
2918 int buf_size = 512, err;
2919 size_t byteswritten = 0;
2920
2921 if ( NULL == mat || NULL == z)
2922 return 1;
2923
2924 if ( NULL == matvar ) {
2925 size_t dims[2] = {0,0};
2926 byteswritten = Mat_WriteCompressedEmptyVariable5(mat, NULL, 2, dims, z);
2927 return byteswritten;
2928 }
2929 start = ftell(mat->fp);
2930
2931 /* Array Flags */
2932 array_flags = matvar->class_type & CLASS_TYPE_MASK;
2933 if ( matvar->isComplex )
2934 array_flags |= MAT_F_COMPLEX;
2935 if ( matvar->isGlobal )
2936 array_flags |= MAT_F_GLOBAL;
2937 if ( matvar->isLogical )
2938 array_flags |= MAT_F_LOGICAL;
2939 if ( matvar->class_type == MAT_C_SPARSE )
2940 nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
2941
2942 uncomp_buf[0] = MAT_T_MATRIX;
2943 uncomp_buf[1] = (int)GetStructFieldBufSize(matvar);
2944 z->next_out = ZLIB_BYTE_PTR(comp_buf);
2945 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
2946 z->avail_out = buf_size*sizeof(*comp_buf);
2947 z->avail_in = 8;
2948 err = deflate(z,Z_NO_FLUSH);
2949 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
2950 mat->fp);
2951 uncomp_buf[0] = array_flags_type;
2952 uncomp_buf[1] = array_flags_size;
2953 uncomp_buf[2] = array_flags;
2954 uncomp_buf[3] = nzmax;
2955 /* Rank and Dimension */
2956 nBytes = matvar->rank * 4;
2957 uncomp_buf[4] = dims_array_type;
2958 uncomp_buf[5] = nBytes;
2959 for ( i = 0; i < matvar->rank; i++ ) {
2960 mat_int32_t dim;
2961 dim = matvar->dims[i];
2962 nmemb *= dim;
2963 uncomp_buf[6+i] = dim;
2964 }
2965 if ( matvar->rank % 2 != 0 ) {
2966 uncomp_buf[6+i] = pad4;
2967 i++;
2968 }
2969
2970 z->next_out = ZLIB_BYTE_PTR(comp_buf);
2971 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
2972 z->avail_out = buf_size*sizeof(*comp_buf);
2973 z->avail_in = (6+i)*sizeof(*uncomp_buf);
2974 err = deflate(z,Z_NO_FLUSH);
2975 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
2976 mat->fp);
2977 /* Name of variable */
2978 uncomp_buf[0] = array_name_type;
2979 uncomp_buf[1] = 0;
2980 z->next_out = ZLIB_BYTE_PTR(comp_buf);
2981 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
2982 z->avail_out = buf_size*sizeof(*comp_buf);
2983 z->avail_in = 8;
2984 err = deflate(z,Z_NO_FLUSH);
2985 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-z->avail_out,
2986 mat->fp);
2987
2988 matvar->internal->datapos = ftell(mat->fp);
2989 switch ( matvar->class_type ) {
2990 case MAT_C_DOUBLE:
2991 case MAT_C_SINGLE:
2992 case MAT_C_INT64:
2993 case MAT_C_UINT64:
2994 case MAT_C_INT32:
2995 case MAT_C_UINT32:
2996 case MAT_C_INT16:
2997 case MAT_C_UINT16:
2998 case MAT_C_INT8:
2999 case MAT_C_UINT8:
3000 {
3001 /* WriteCompressedData makes sure uncomressed data is aligned
3002 * on an 8-byte boundary */
3003 if ( matvar->isComplex ) {
3004 mat_complex_split_t *complex_data = matvar->data;
3005
3006 if ( NULL == matvar->data )
3007 complex_data = &null_complex_data;
3008
3009 byteswritten += WriteCompressedData(mat,z,
3010 complex_data->Re,nmemb,matvar->data_type);
3011 byteswritten += WriteCompressedData(mat,z,
3012 complex_data->Im,nmemb,matvar->data_type);
3013 } else {
3014 byteswritten += WriteCompressedData(mat,z,
3015 matvar->data,nmemb,matvar->data_type);
3016 }
3017 break;
3018 }
3019 case MAT_C_CHAR:
3020 {
3021 byteswritten += WriteCompressedCharData(mat,z,matvar->data,
3022 nmemb,matvar->data_type);
3023 break;
3024 }
3025 case MAT_C_CELL:
3026 {
3027 int ncells;
3028 matvar_t **cells = (matvar_t **)matvar->data;
3029
3030 /* Check for an empty cell array */
3031 if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
3032 matvar->data == NULL )
3033 break;
3034 ncells = matvar->nbytes / matvar->data_size;
3035 for ( i = 0; i < ncells; i++ )
3036 WriteCompressedCellArrayField(mat,cells[i],z);
3037 break;
3038 }
3039 case MAT_C_STRUCT:
3040 {
3041 unsigned char *padzero;
3042 int fieldname_size, nfields;
3043 size_t maxlen = 0;
3044 mat_int32_t array_name_type = MAT_T_INT8;
3045 matvar_t **fields = (matvar_t **)matvar->data;
3046
3047 nfields = matvar->internal->num_fields;
3048 /* Check for a structure with no fields */
3049 if ( nfields < 1 ) {
3050 fieldname_size = 1;
3051 uncomp_buf[0] = (fieldname_data_size << 16) |
3052 fieldname_type;
3053 uncomp_buf[1] = 1;
3054 uncomp_buf[2] = array_name_type;
3055 uncomp_buf[3] = 0;
3056 z->next_out = ZLIB_BYTE_PTR(comp_buf);
3057 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
3058 z->avail_out = buf_size*sizeof(*comp_buf);
3059 z->avail_in = 16;
3060 err = deflate(z,Z_NO_FLUSH);
3061 byteswritten += fwrite(comp_buf,1,buf_size*
3062 sizeof(*comp_buf)-z->avail_out,mat->fp);
3063 break;
3064 }
3065 for ( i = 0; i < nfields; i++ ) {
3066 size_t len = strlen(matvar->internal->fieldnames[i]);
3067 if ( len > maxlen )
3068 maxlen = len;
3069 }
3070 maxlen++;
3071 fieldname_size = maxlen;
3072 while ( nfields*fieldname_size % 8 != 0 )
3073 fieldname_size++;
3074 uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type;
3075 uncomp_buf[1] = fieldname_size;
3076 uncomp_buf[2] = array_name_type;
3077 uncomp_buf[3] = nfields*fieldname_size;
3078
3079 padzero = calloc(fieldname_size,1);
3080 z->next_out = ZLIB_BYTE_PTR(comp_buf);
3081 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
3082 z->avail_out = buf_size*sizeof(*comp_buf);
3083 z->avail_in = 16;
3084 err = deflate(z,Z_NO_FLUSH);
3085 byteswritten += fwrite(comp_buf,1,
3086 buf_size*sizeof(*comp_buf)-z->avail_out,mat->fp);
3087 for ( i = 0; i < nfields; i++ ) {
3088 size_t len = strlen(matvar->internal->fieldnames[i]);
3089 memset(padzero,'\0',fieldname_size);
3090 memcpy(padzero,matvar->internal->fieldnames[i],len);
3091 z->next_out = ZLIB_BYTE_PTR(comp_buf);
3092 z->next_in = ZLIB_BYTE_PTR(padzero);
3093 z->avail_out = buf_size*sizeof(*comp_buf);
3094 z->avail_in = fieldname_size;
3095 err = deflate(z,Z_NO_FLUSH);
3096 byteswritten += fwrite(comp_buf,1,
3097 buf_size*sizeof(*comp_buf)-z->avail_out,mat->fp);
3098 }
3099 free(padzero);
3100 for ( i = 0; i < nmemb*nfields; i++ )
3101 byteswritten +=
3102 WriteCompressedStructField(mat,fields[i],z);
3103 break;
3104 }
3105 case MAT_C_SPARSE:
3106 {
3107 mat_sparse_t *sparse = matvar->data;
3108
3109 byteswritten += WriteCompressedData(mat,z,sparse->ir,
3110 sparse->nir,MAT_T_INT32);
3111 byteswritten += WriteCompressedData(mat,z,sparse->jc,
3112 sparse->njc,MAT_T_INT32);
3113 if ( matvar->isComplex ) {
3114 mat_complex_split_t *complex_data = sparse->data;
3115 byteswritten += WriteCompressedData(mat,z,
3116 complex_data->Re,sparse->ndata,matvar->data_type);
3117 byteswritten += WriteCompressedData(mat,z,
3118 complex_data->Im,sparse->ndata,matvar->data_type);
3119 } else {
3120 byteswritten += WriteCompressedData(mat,z,
3121 sparse->data,sparse->ndata,matvar->data_type);
3122 }
3123 break;
3124 }
3125 }
3126
3127 return byteswritten;
3128 }
3129 #endif
3130
3131 static size_t
3132 Mat_WriteEmptyVariable5(mat_t *mat,const char *name,int rank,size_t *dims)
3133 {
3134 mat_uint32_t array_flags = 0x0;
3135 mat_int32_t array_name_type = MAT_T_INT8, matrix_type = MAT_T_MATRIX;
3136 int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
3137 int array_flags_size = 8, pad4 = 0, nBytes, i, nmemb = 1;
3138 mat_int8_t pad1 = 0;
3139 size_t byteswritten = 0;
3140 long start = 0, end = 0;
3141
3142 fwrite(&matrix_type,4,1,mat->fp);
3143 fwrite(&pad4,4,1,mat->fp);
3144
3145 start = ftell(mat->fp);
3146 /* Array Flags */
3147 array_flags = MAT_C_DOUBLE;
3148
3149 if ( mat->byteswap )
3150 array_flags = Mat_int32Swap((mat_int32_t*)&array_flags);
3151 byteswritten += fwrite(&array_flags_type,4,1,mat->fp);
3152 byteswritten += fwrite(&array_flags_size,4,1,mat->fp);
3153 byteswritten += fwrite(&array_flags,4,1,mat->fp);
3154 byteswritten += fwrite(&pad4,4,1,mat->fp);
3155 /* Rank and Dimension */
3156 nBytes = rank * 4;
3157 byteswritten += fwrite(&dims_array_type,4,1,mat->fp);
3158 byteswritten += fwrite(&nBytes,4,1,mat->fp);
3159 for ( i = 0; i < rank; i++ ) {
3160 mat_int32_t dim;
3161 dim = dims[i];
3162 nmemb *= dim;
3163 byteswritten += fwrite(&dim,4,1,mat->fp);
3164 }
3165 if ( rank % 2 != 0 )
3166 byteswritten += fwrite(&pad4,4,1,mat->fp);
3167
3168 if ( NULL == name ) {
3169 /* Name of variable */
3170 byteswritten += fwrite(&array_name_type,4,1,mat->fp);
3171 byteswritten += fwrite(&pad4,4,1,mat->fp);
3172 } else {
3173 mat_int32_t array_name_type = MAT_T_INT8;
3174 mat_int32_t array_name_len = strlen(name);
3175 /* Name of variable */
3176 if ( array_name_len <= 4 ) {
3177 mat_int8_t pad1 = 0;
3178 array_name_type = (array_name_len << 16) | array_name_type;
3179 byteswritten += fwrite(&array_name_type,4,1,mat->fp);
3180 byteswritten += fwrite(name,1,array_name_len,mat->fp);
3181 for ( i = array_name_len; i < 4; i++ )
3182 byteswritten += fwrite(&pad1,1,1,mat->fp);
3183 } else {
3184 byteswritten += fwrite(&array_name_type,4,1,mat->fp);
3185 byteswritten += fwrite(&array_name_len,4,1,mat->fp);
3186 byteswritten += fwrite(name,1,array_name_len,mat->fp);
3187 if ( array_name_len % 8 )
3188 for ( i = array_name_len % 8; i < 8; i++ )
3189 byteswritten += fwrite(&pad1,1,1,mat->fp);
3190 }
3191 }
3192
3193 nBytes = WriteData(mat,NULL,0,MAT_T_DOUBLE);
3194 byteswritten += nBytes;
3195 if ( nBytes % 8 )
3196 for ( i = nBytes % 8; i < 8; i++ )
3197 byteswritten += fwrite(&pad1,1,1,mat->fp);
3198
3199 end = ftell(mat->fp);
3200 nBytes = (int)(end-start);
3201 fseek(mat->fp,(long)-(nBytes+4),SEEK_CUR);
3202 fwrite(&nBytes,4,1,mat->fp);
3203 fseek(mat->fp,end,SEEK_SET);
3204
3205 return byteswritten;
3206 }
3207
3208 #if defined(HAVE_ZLIB)
3209 static size_t
3210 Mat_WriteCompressedEmptyVariable5(mat_t *mat,const char *name,int rank,
3211 size_t *dims,z_stream *z)
3212 {
3213 mat_uint32_t array_flags = 0x0;
3214 mat_int16_t array_name_type = MAT_T_INT8;
3215 int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
3216 int array_flags_size = 8, pad4 = 0;
3217 int nBytes, i, nmemb = 1;
3218
3219 mat_uint32_t comp_buf[512];
3220 mat_uint32_t uncomp_buf[512] = {0,};
3221 int buf_size = 512, err;
3222 size_t byteswritten = 0, buf_size_bytes;
3223
3224 if ( NULL == mat || NULL == z)
3225 return 1;
3226
3227 buf_size_bytes = buf_size*sizeof(*comp_buf);
3228
3229 /* Array Flags */
3230 array_flags = MAT_C_DOUBLE;
3231
3232 uncomp_buf[0] = MAT_T_MATRIX;
3233 uncomp_buf[1] = (int)GetEmptyMatrixMaxBufSize(name,rank);
3234 z->next_out = ZLIB_BYTE_PTR(comp_buf);
3235 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
3236 z->avail_out = buf_size_bytes;
3237 z->avail_in = 8;
3238 err = deflate(z,Z_NO_FLUSH);
3239 byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,mat->fp);
3240 uncomp_buf[0] = array_flags_type;
3241 uncomp_buf[1] = array_flags_size;
3242 uncomp_buf[2] = array_flags;
3243 uncomp_buf[3] = 0;
3244 /* Rank and Dimension */
3245 nBytes = rank * 4;
3246 uncomp_buf[4] = dims_array_type;
3247 uncomp_buf[5] = nBytes;
3248 for ( i = 0; i < rank; i++ ) {
3249 mat_int32_t dim;
3250 dim = dims[i];
3251 nmemb *= dim;
3252 uncomp_buf[6+i] = dim;
3253 }
3254 if ( rank % 2 != 0 ) {
3255 uncomp_buf[6+i] = pad4;
3256 i++;
3257 }
3258
3259 z->next_out = ZLIB_BYTE_PTR(comp_buf);
3260 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
3261 z->avail_out = buf_size_bytes;
3262 z->avail_in = (6+i)*sizeof(*uncomp_buf);
3263 err = deflate(z,Z_NO_FLUSH);
3264 byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,mat->fp);
3265 /* Name of variable */
3266 if ( NULL == name ) {
3267 uncomp_buf[0] = array_name_type;
3268 uncomp_buf[1] = 0;
3269 z->next_out = ZLIB_BYTE_PTR(comp_buf);
3270 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
3271 z->avail_out = buf_size_bytes;
3272 z->avail_in = 8;
3273 err = deflate(z,Z_NO_FLUSH);
3274 byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,mat->fp);
3275 } else {
3276 if ( strlen(name) <= 4 ) {
3277 mat_int16_t array_name_len = (mat_int16_t)strlen(name);
3278 mat_int16_t array_name_type = MAT_T_INT8;
3279
3280 memset(uncomp_buf,0,8);
3281 uncomp_buf[0] = (array_name_len << 16) | array_name_type;
3282 memcpy(uncomp_buf+1,name,array_name_len);
3283 if ( array_name_len % 4 )
3284 array_name_len += 4-(array_name_len % 4);
3285
3286 z->next_out = ZLIB_BYTE_PTR(comp_buf);
3287 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
3288 z->avail_out = buf_size_bytes;
3289 z->avail_in = 8;
3290 err = deflate(z,Z_NO_FLUSH);
3291 byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,
3292 mat->fp);
3293 } else {
3294 mat_int32_t array_name_len = (mat_int32_t)strlen(name);
3295 mat_int32_t array_name_type = MAT_T_INT8;
3296
3297 memset(uncomp_buf,0,buf_size*sizeof(*uncomp_buf));
3298 uncomp_buf[0] = array_name_type;
3299 uncomp_buf[1] = array_name_len;
3300 memcpy(uncomp_buf+2,name,array_name_len);
3301 if ( array_name_len % 8 )
3302 array_name_len += 8-(array_name_len % 8);
3303 z->next_out = ZLIB_BYTE_PTR(comp_buf);
3304 z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
3305 z->avail_out = buf_size_bytes;
3306 z->avail_in = 8+array_name_len;
3307 err = deflate(z,Z_NO_FLUSH);
3308 byteswritten += fwrite(comp_buf,1,buf_size_bytes-z->avail_out,
3309 mat->fp);
3310 }
3311 }
3312
3313 byteswritten += WriteCompressedData(mat,z,NULL,0,MAT_T_DOUBLE);
3314 return byteswritten;
3315 }
3316 #endif
3317
3318 /** @if mat_devman
3319 * @brief Reads the data of a version 5 MAT variable
3320 *
3321 * @ingroup mat_internal
3322 * @param mat MAT file pointer
3323 * @param matvar MAT variable pointer to read the data
3324 * @endif
3325 */
3326 void
3327 Read5(mat_t *mat, matvar_t *matvar)
3328 {
3329 int nBytes, len = 0, i, byteswap, data_in_tag = 0;
3330 enum matio_types packed_type;
3331 long fpos;
3332 mat_uint32_t tag[2];
3333
3334 if ( matvar == NULL )
3335 return;
3336 else if ( matvar->rank == 0 ) /* An empty data set */
3337 return;
3338
3339 fpos = ftell(mat->fp);
3340 len = 1;
3341 byteswap = mat->byteswap;
3342 switch ( matvar->class_type ) {
3343 case MAT_C_EMPTY:
3344 matvar->nbytes = 0;
3345 matvar->data_size = sizeof(double);
3346 matvar->data_type = MAT_T_DOUBLE;
3347 matvar->class_type = MAT_C_EMPTY;
3348 matvar->rank = 2;
3349 matvar->dims = malloc(matvar->rank*sizeof(*(matvar->dims)));
3350 matvar->dims[0] = 0;
3351 matvar->dims[1] = 0;
3352 break;
3353 case MAT_C_DOUBLE:
3354 if ( matvar->compression ) {
3355 #if defined(HAVE_ZLIB)
3356 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
3357
3358 matvar->internal->z->avail_in = 0;
3359 InflateDataType(mat,matvar->internal->z,tag);
3360 if ( byteswap )
3361 (void)Mat_uint32Swap(tag);
3362
3363 packed_type = TYPE_FROM_TAG(tag[0]);
3364 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3365 data_in_tag = 1;
3366 nBytes = (tag[0] & 0xffff0000) >> 16;
3367 } else {
3368 data_in_tag = 0;
3369 InflateDataType(mat,matvar->internal->z,tag+1);
3370 if ( byteswap )
3371 (void)Mat_uint32Swap(tag+1);
3372 nBytes = tag[1];
3373 }
3374 #endif
3375 } else {
3376 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
3377 fread(tag,4,1,mat->fp);
3378 if ( byteswap )
3379 (void)Mat_uint32Swap(tag);
3380 packed_type = TYPE_FROM_TAG(tag[0]);
3381 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3382 data_in_tag = 1;
3383 nBytes = (tag[0] & 0xffff0000) >> 16;
3384 } else {
3385 data_in_tag = 0;
3386 fread(tag+1,4,1,mat->fp);
3387 if ( byteswap )
3388 (void)Mat_uint32Swap(tag+1);
3389 nBytes = tag[1];
3390 }
3391 }
3392 if ( nBytes == 0 ) {
3393 matvar->nbytes = 0;
3394 break;
3395 }
3396 for ( i = 0; i < matvar->rank; i++ )
3397 len *= matvar->dims[i];
3398 matvar->data_size = sizeof(double);
3399 matvar->data_type = MAT_T_DOUBLE;
3400 if ( matvar->isComplex ) {
3401 mat_complex_split_t *complex_data;
3402
3403 matvar->nbytes = len*matvar->data_size;
3404 complex_data = malloc(sizeof(*complex_data));
3405 complex_data->Re = malloc(matvar->nbytes);
3406 complex_data->Im = malloc(matvar->nbytes);
3407 if ( NULL == complex_data || NULL == complex_data->Re ||
3408 NULL == complex_data->Im ) {
3409 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
3410 break;
3411 }
3412 if ( matvar->compression == MAT_COMPRESSION_NONE) {
3413 nBytes = ReadDoubleData(mat,complex_data->Re,
3414 packed_type,len);
3415 /*
3416 * If the data was in the tag we started on a 4-byte
3417 * boundary so add 4 to make it an 8-byte
3418 */
3419 if ( data_in_tag )
3420 nBytes+=4;
3421 if ( (nBytes % 8) != 0 )
3422 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
3423
3424 /* Complex Data Tag */
3425 fread(tag,4,1,mat->fp);
3426 if ( byteswap )
3427 (void)Mat_uint32Swap(tag);
3428 packed_type = TYPE_FROM_TAG(tag[0]);
3429 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3430 data_in_tag = 1;
3431 nBytes = (tag[0] & 0xffff0000) >> 16;
3432 } else {
3433 data_in_tag = 0;
3434 fread(tag+1,4,1,mat->fp);
3435 if ( byteswap )
3436 (void)Mat_uint32Swap(tag+1);
3437 nBytes = tag[1];
3438 }
3439 nBytes = ReadDoubleData(mat,complex_data->Im,packed_type,
3440 len);
3441 #if defined(HAVE_ZLIB)
3442 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
3443 nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
3444 complex_data->Re,packed_type,len);
3445 /*
3446 * If the data was in the tag we started on a 4-byte
3447 * boundary so add 4 to make it an 8-byte
3448 */
3449 if ( data_in_tag )
3450 nBytes+=4;
3451 if ( (nBytes % 8) != 0 )
3452 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3453
3454 /* Complex Data Tag */
3455 InflateDataType(mat,matvar->internal->z,tag);
3456 if ( byteswap )
3457 (void)Mat_uint32Swap(tag);
3458
3459 packed_type = TYPE_FROM_TAG(tag[0]);
3460 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3461 data_in_tag = 1;
3462 nBytes = (tag[0] & 0xffff0000) >> 16;
3463 } else {
3464 data_in_tag = 0;
3465 InflateDataType(mat,matvar->internal->z,tag+1);
3466 if ( byteswap )
3467 (void)Mat_uint32Swap(tag+1);
3468 nBytes = tag[1];
3469 }
3470 nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
3471 complex_data->Im,packed_type,len);
3472 /*
3473 * If the data was in the tag we started on a 4-byte
3474 * boundary so add 4 to make it an 8-byte
3475 */
3476 if ( data_in_tag )
3477 nBytes+=4;
3478 if ( (nBytes % 8) != 0 )
3479 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3480 #endif
3481 }
3482 matvar->data = complex_data;
3483 } else { /* if ( isComplex ) */
3484 matvar->nbytes = len*matvar->data_size;
3485 matvar->data = malloc(matvar->nbytes);
3486 if ( !matvar->data ) {
3487 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
3488 break;
3489 }
3490 if ( matvar->compression == MAT_COMPRESSION_NONE) {
3491 nBytes = ReadDoubleData(mat,(double*)matvar->data,
3492 packed_type,len);
3493 /*
3494 * If the data was in the tag we started on a 4-byte
3495 * boundary so add 4 to make it an 8-byte
3496 */
3497 if ( data_in_tag )
3498 nBytes+=4;
3499 if ( (nBytes % 8) != 0 )
3500 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
3501 #if defined(HAVE_ZLIB)
3502 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
3503 nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
3504 (double*)matvar->data,packed_type,len);
3505 /*
3506 * If the data was in the tag we started on a 4-byte
3507 * boundary so add 4 to make it an 8-byte
3508 */
3509 if ( data_in_tag )
3510 nBytes+=4;
3511 if ( (nBytes % 8) != 0 )
3512 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3513 #endif
3514 }
3515 }
3516 break;
3517 case MAT_C_SINGLE:
3518 if ( matvar->compression ) {
3519 #if defined(HAVE_ZLIB)
3520 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
3521
3522 matvar->internal->z->avail_in = 0;
3523 InflateDataType(mat,matvar->internal->z,tag);
3524 if ( byteswap )
3525 (void)Mat_uint32Swap(tag);
3526
3527 packed_type = TYPE_FROM_TAG(tag[0]);
3528 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3529 data_in_tag = 1;
3530 nBytes = (tag[0] & 0xffff0000) >> 16;
3531 } else {
3532 data_in_tag = 0;
3533 InflateDataType(mat,matvar->internal->z,tag+1);
3534 if ( byteswap )
3535 (void)Mat_uint32Swap(tag+1);
3536 nBytes = tag[1];
3537 }
3538 #endif
3539 } else {
3540 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
3541 fread(tag,4,1,mat->fp);
3542 if ( byteswap )
3543 (void)Mat_uint32Swap(tag);
3544 packed_type = TYPE_FROM_TAG(tag[0]);
3545 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3546 data_in_tag = 1;
3547 nBytes = (tag[0] & 0xffff0000) >> 16;
3548 } else {
3549 data_in_tag = 0;
3550 fread(tag+1,4,1,mat->fp);
3551 if ( byteswap )
3552 (void)Mat_uint32Swap(tag+1);
3553 nBytes = tag[1];
3554 }
3555 }
3556 if ( nBytes == 0 ) {
3557 matvar->nbytes = 0;
3558 break;
3559 }
3560 for ( i = 0; i < matvar->rank; i++ )
3561 len *= matvar->dims[i];
3562 matvar->data_size = sizeof(float);
3563 matvar->data_type = MAT_T_SINGLE;
3564 if ( matvar->isComplex ) {
3565 mat_complex_split_t *complex_data;
3566
3567 matvar->nbytes = len*matvar->data_size;
3568 complex_data = malloc(sizeof(*complex_data));
3569 complex_data->Re = malloc(matvar->nbytes);
3570 complex_data->Im = malloc(matvar->nbytes);
3571 if ( NULL == complex_data || NULL == complex_data->Re ||
3572 NULL == complex_data->Im ) {
3573 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
3574 break;
3575 }
3576 if ( matvar->compression == MAT_COMPRESSION_NONE) {
3577 nBytes = ReadSingleData(mat,complex_data->Re,
3578 packed_type,len);
3579 /*
3580 * If the data was in the tag we started on a 4-byte
3581 * boundary so add 4 to make it an 8-byte
3582 */
3583 if ( data_in_tag )
3584 nBytes+=4;
3585 if ( (nBytes % 8) != 0 )
3586 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
3587
3588 /* Complex Data Tag */
3589 fread(tag,4,1,mat->fp);
3590 if ( byteswap )
3591 (void)Mat_uint32Swap(tag);
3592 packed_type = TYPE_FROM_TAG(tag[0]);
3593 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3594 data_in_tag = 1;
3595 nBytes = (tag[0] & 0xffff0000) >> 16;
3596 } else {
3597 data_in_tag = 0;
3598 fread(tag+1,4,1,mat->fp);
3599 if ( byteswap )
3600 (void)Mat_uint32Swap(tag+1);
3601 nBytes = tag[1];
3602 }
3603 nBytes = ReadSingleData(mat,complex_data->Im,
3604 packed_type,len);
3605 #if defined(HAVE_ZLIB)
3606 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
3607 nBytes = ReadCompressedSingleData(mat,matvar->internal->z,
3608 complex_data->Re,packed_type,len);
3609 /*
3610 * If the data was in the tag we started on a 4-byte
3611 * boundary so add 4 to make it an 8-byte
3612 */
3613 if ( data_in_tag )
3614 nBytes+=4;
3615 if ( (nBytes % 8) != 0 )
3616 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3617
3618 /* Complex Data Tag */
3619 InflateDataType(mat,matvar->internal->z,tag);
3620 if ( byteswap )
3621 (void)Mat_uint32Swap(tag);
3622
3623 packed_type = TYPE_FROM_TAG(tag[0]);
3624 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3625 data_in_tag = 1;
3626 nBytes = (tag[0] & 0xffff0000) >> 16;
3627 } else {
3628 data_in_tag = 0;
3629 InflateDataType(mat,matvar->internal->z,tag+1);
3630 if ( byteswap )
3631 (void)Mat_uint32Swap(tag+1);
3632 nBytes = tag[1];
3633 }
3634 nBytes = ReadCompressedSingleData(mat,matvar->internal->z,
3635 complex_data->Im,packed_type,len);
3636 /*
3637 * If the data was in the tag we started on a 4-byte
3638 * boundary so add 4 to make it an 8-byte
3639 */
3640 if ( data_in_tag )
3641 nBytes+=4;
3642 if ( (nBytes % 8) != 0 )
3643 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3644 #endif
3645 }
3646 matvar->data = complex_data;
3647 } else {
3648 matvar->nbytes = len*matvar->data_size;
3649 matvar->data = malloc(matvar->nbytes);
3650 if ( !matvar->data ) {
3651 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
3652 break;
3653 }
3654 if ( matvar->compression == MAT_COMPRESSION_NONE) {
3655 nBytes = ReadSingleData(mat,(float*)matvar->data,
3656 packed_type,len);
3657 /*
3658 * If the data was in the tag we started on a 4-byte
3659 * boundary so add 4 to make it an 8-byte
3660 */
3661 if ( data_in_tag )
3662 nBytes+=4;
3663 if ( (nBytes % 8) != 0 )
3664 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
3665 #if defined(HAVE_ZLIB)
3666 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
3667 nBytes = ReadCompressedSingleData(mat,matvar->internal->z,
3668 (float*)matvar->data,packed_type,len);
3669 /*
3670 * If the data was in the tag we started on a 4-byte
3671 * boundary so add 4 to make it an 8-byte
3672 */
3673 if ( data_in_tag )
3674 nBytes+=4;
3675 if ( (nBytes % 8) != 0 )
3676 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3677 #endif
3678 }
3679 }
3680 break;
3681 #ifdef HAVE_MAT_INT64_T
3682 case MAT_C_INT64:
3683 if ( matvar->compression ) {
3684 #if defined(HAVE_ZLIB)
3685 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
3686
3687 matvar->internal->z->avail_in = 0;
3688 InflateDataType(mat,matvar->internal->z,tag);
3689 if ( byteswap )
3690 (void)Mat_uint32Swap(tag);
3691
3692 packed_type = TYPE_FROM_TAG(tag[0]);
3693 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3694 data_in_tag = 1;
3695 nBytes = (tag[0] & 0xffff0000) >> 16;
3696 } else {
3697 data_in_tag = 0;
3698 InflateDataType(mat,matvar->internal->z,tag+1);
3699 if ( byteswap )
3700 (void)Mat_uint32Swap(tag+1);
3701 nBytes = tag[1];
3702 }
3703 #endif
3704 } else {
3705 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
3706 fread(tag,4,1,mat->fp);
3707 if ( byteswap )
3708 (void)Mat_uint32Swap(tag);
3709 packed_type = TYPE_FROM_TAG(tag[0]);
3710 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3711 data_in_tag = 1;
3712 nBytes = (tag[0] & 0xffff0000) >> 16;
3713 } else {
3714 data_in_tag = 0;
3715 fread(tag+1,4,1,mat->fp);
3716 if ( byteswap )
3717 (void)Mat_uint32Swap(tag+1);
3718 nBytes = tag[1];
3719 }
3720 }
3721 if ( nBytes == 0 ) {
3722 matvar->nbytes = 0;
3723 break;
3724 }
3725 for ( i = 0; i < matvar->rank; i++ )
3726 len *= matvar->dims[i];
3727 matvar->data_size = sizeof(mat_int64_t);
3728 matvar->data_type = MAT_T_INT64;
3729 if ( matvar->isComplex ) {
3730 mat_complex_split_t *complex_data;
3731
3732 matvar->nbytes = len*matvar->data_size;
3733 complex_data = malloc(sizeof(*complex_data));
3734 complex_data->Re = malloc(matvar->nbytes);
3735 complex_data->Im = malloc(matvar->nbytes);
3736 if ( NULL == complex_data || NULL == complex_data->Re ||
3737 NULL == complex_data->Im ) {
3738 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
3739 break;
3740 }
3741 if ( matvar->compression == MAT_COMPRESSION_NONE) {
3742 nBytes = ReadInt64Data(mat,complex_data->Re,
3743 packed_type,len);
3744 /*
3745 * If the data was in the tag we started on a 4-byte
3746 * boundary so add 4 to make it an 8-byte
3747 */
3748 if ( data_in_tag )
3749 nBytes+=4;
3750 if ( (nBytes % 8) != 0 )
3751 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
3752
3753 /* Complex Data Tag */
3754 fread(tag,4,1,mat->fp);
3755 if ( byteswap )
3756 (void)Mat_uint32Swap(tag);
3757 packed_type = TYPE_FROM_TAG(tag[0]);
3758 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3759 data_in_tag = 1;
3760 nBytes = (tag[0] & 0xffff0000) >> 16;
3761 } else {
3762 data_in_tag = 0;
3763 fread(tag+1,4,1,mat->fp);
3764 if ( byteswap )
3765 (void)Mat_uint32Swap(tag+1);
3766 nBytes = tag[1];
3767 }
3768 nBytes = ReadInt64Data(mat,complex_data->Im,
3769 packed_type,len);
3770 #if defined(HAVE_ZLIB)
3771 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
3772 nBytes = ReadCompressedInt64Data(mat,matvar->internal->z,
3773 complex_data->Re,packed_type,len);
3774 /*
3775 * If the data was in the tag we started on a 4-byte
3776 * boundary so add 4 to make it an 8-byte
3777 */
3778 if ( data_in_tag )
3779 nBytes+=4;
3780 if ( (nBytes % 8) != 0 )
3781 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3782
3783 /* Complex Data Tag */
3784 InflateDataType(mat,matvar->internal->z,tag);
3785 if ( byteswap )
3786 (void)Mat_uint32Swap(tag);
3787
3788 packed_type = TYPE_FROM_TAG(tag[0]);
3789 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3790 data_in_tag = 1;
3791 nBytes = (tag[0] & 0xffff0000) >> 16;
3792 } else {
3793 data_in_tag = 0;
3794 InflateDataType(mat,matvar->internal->z,tag+1);
3795 if ( byteswap )
3796 (void)Mat_uint32Swap(tag+1);
3797 nBytes = tag[1];
3798 }
3799 nBytes = ReadCompressedInt64Data(mat,matvar->internal->z,
3800 complex_data->Im,packed_type,len);
3801 /*
3802 * If the data was in the tag we started on a 4-byte
3803 * boundary so add 4 to make it an 8-byte
3804 */
3805 if ( data_in_tag )
3806 nBytes+=4;
3807 if ( (nBytes % 8) != 0 )
3808 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3809 #endif
3810 }
3811 matvar->data = complex_data;
3812 } else {
3813 matvar->nbytes = len*matvar->data_size;
3814 matvar->data = malloc(matvar->nbytes);
3815 if ( !matvar->data ) {
3816 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
3817 break;
3818 }
3819 if ( matvar->compression == MAT_COMPRESSION_NONE) {
3820 nBytes = ReadInt64Data(mat,(mat_int64_t*)matvar->data,
3821 packed_type,len);
3822 /*
3823 * If the data was in the tag we started on a 4-byte
3824 * boundary so add 4 to make it an 8-byte
3825 */
3826 if ( data_in_tag )
3827 nBytes+=4;
3828 if ( (nBytes % 8) != 0 )
3829 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
3830 #if defined(HAVE_ZLIB)
3831 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
3832 nBytes = ReadCompressedInt64Data(mat,matvar->internal->z,
3833 (mat_int64_t*)matvar->data,packed_type,len);
3834 /*
3835 * If the data was in the tag we started on a 4-byte
3836 * boundary so add 4 to make it an 8-byte
3837 */
3838 if ( data_in_tag )
3839 nBytes+=4;
3840 if ( (nBytes % 8) != 0 )
3841 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3842 #endif
3843 }
3844 }
3845 break;
3846 #endif /* HAVE_MAT_INT64_T */
3847 #ifdef HAVE_MAT_UINT64_T
3848 case MAT_C_UINT64:
3849 if ( matvar->compression ) {
3850 #if defined(HAVE_ZLIB)
3851 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
3852
3853 matvar->internal->z->avail_in = 0;
3854 InflateDataType(mat,matvar->internal->z,tag);
3855 if ( byteswap )
3856 (void)Mat_uint32Swap(tag);
3857 packed_type = TYPE_FROM_TAG(tag[0]);
3858 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3859 data_in_tag = 1;
3860 nBytes = (tag[0] & 0xffff0000) >> 16;
3861 } else {
3862 data_in_tag = 0;
3863 InflateDataType(mat,matvar->internal->z,tag+1);
3864 if ( byteswap )
3865 (void)Mat_uint32Swap(tag+1);
3866 nBytes = tag[1];
3867 }
3868 #endif
3869 } else {
3870 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
3871 fread(tag,4,1,mat->fp);
3872 if ( byteswap )
3873 (void)Mat_uint32Swap(tag);
3874 packed_type = TYPE_FROM_TAG(tag[0]);
3875 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3876 data_in_tag = 1;
3877 nBytes = (tag[0] & 0xffff0000) >> 16;
3878 } else {
3879 data_in_tag = 0;
3880 fread(tag+1,4,1,mat->fp);
3881 if ( byteswap )
3882 (void)Mat_uint32Swap(tag+1);
3883 nBytes = tag[1];
3884 }
3885 }
3886 if ( nBytes == 0 ) {
3887 matvar->nbytes = 0;
3888 break;
3889 }
3890 for ( i = 0; i < matvar->rank; i++ )
3891 len *= matvar->dims[i];
3892 matvar->data_size = sizeof(mat_uint64_t);
3893 matvar->data_type = MAT_T_UINT64;
3894 if ( matvar->isComplex ) {
3895 mat_complex_split_t *complex_data;
3896
3897 matvar->nbytes = len*matvar->data_size;
3898 complex_data = malloc(sizeof(*complex_data));
3899 complex_data->Re = malloc(matvar->nbytes);
3900 complex_data->Im = malloc(matvar->nbytes);
3901 if ( NULL == complex_data || NULL == complex_data->Re ||
3902 NULL == complex_data->Im ) {
3903 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
3904 break;
3905 }
3906 if ( matvar->compression == MAT_COMPRESSION_NONE) {
3907 nBytes = ReadInt64Data(mat,complex_data->Re,
3908 packed_type,len);
3909 /*
3910 * If the data was in the tag we started on a 4-byte
3911 * boundary so add 4 to make it an 8-byte
3912 */
3913 if ( data_in_tag )
3914 nBytes+=4;
3915 if ( (nBytes % 8) != 0 )
3916 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
3917
3918 /* Complex Data Tag */
3919 fread(tag,4,1,mat->fp);
3920 if ( byteswap )
3921 (void)Mat_uint32Swap(tag);
3922 packed_type = TYPE_FROM_TAG(tag[0]);
3923 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3924 data_in_tag = 1;
3925 nBytes = (tag[0] & 0xffff0000) >> 16;
3926 } else {
3927 data_in_tag = 0;
3928 fread(tag+1,4,1,mat->fp);
3929 if ( byteswap )
3930 (void)Mat_uint32Swap(tag+1);
3931 nBytes = tag[1];
3932 }
3933 nBytes = ReadInt64Data(mat,complex_data->Im,
3934 packed_type,len);
3935 #if defined(HAVE_ZLIB)
3936 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
3937 nBytes = ReadCompressedInt64Data(mat,matvar->internal->z,
3938 complex_data->Re,packed_type,len);
3939 /*
3940 * If the data was in the tag we started on a 4-byte
3941 * boundary so add 4 to make it an 8-byte
3942 */
3943 if ( data_in_tag )
3944 nBytes+=4;
3945 if ( (nBytes % 8) != 0 )
3946 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3947
3948 /* Complex Data Tag */
3949 InflateDataType(mat,matvar->internal->z,tag);
3950 if ( byteswap )
3951 (void)Mat_uint32Swap(tag);
3952
3953 packed_type = TYPE_FROM_TAG(tag[0]);
3954 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
3955 data_in_tag = 1;
3956 nBytes = (tag[0] & 0xffff0000) >> 16;
3957 } else {
3958 data_in_tag = 0;
3959 InflateDataType(mat,matvar->internal->z,tag+1);
3960 if ( byteswap )
3961 (void)Mat_uint32Swap(tag+1);
3962 nBytes = tag[1];
3963 }
3964 nBytes = ReadCompressedInt64Data(mat,matvar->internal->z,
3965 complex_data->Im,packed_type,len);
3966 /*
3967 * If the data was in the tag we started on a 4-byte
3968 * boundary so add 4 to make it an 8-byte
3969 */
3970 if ( data_in_tag )
3971 nBytes+=4;
3972 if ( (nBytes % 8) != 0 )
3973 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
3974 #endif
3975 }
3976 matvar->data = complex_data;
3977 } else {
3978 matvar->nbytes = len*matvar->data_size;
3979 matvar->data = malloc(matvar->nbytes);
3980 if ( !matvar->data ) {
3981 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
3982 break;
3983 }
3984 if ( matvar->compression == MAT_COMPRESSION_NONE) {
3985 nBytes = ReadInt64Data(mat,(mat_int64_t*)matvar->data,
3986 packed_type,len);
3987 /*
3988 * If the data was in the tag we started on a 4-byte
3989 * boundary so add 4 to make it an 8-byte
3990 */
3991 if ( data_in_tag )
3992 nBytes+=4;
3993 if ( (nBytes % 8) != 0 )
3994 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
3995 #if defined(HAVE_ZLIB)
3996 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
3997 nBytes = ReadCompressedInt64Data(mat,matvar->internal->z,
3998 (mat_int64_t*)matvar->data,packed_type,len);
3999 /*
4000 * If the data was in the tag we started on a 4-byte
4001 * boundary so add 4 to make it an 8-byte
4002 */
4003 if ( data_in_tag )
4004 nBytes+=4;
4005 if ( (nBytes % 8) != 0 )
4006 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4007 #endif
4008 }
4009 }
4010 break;
4011 #endif /* HAVE_MAT_UINT64_T */
4012 case MAT_C_INT32:
4013 if ( matvar->compression ) {
4014 #if defined(HAVE_ZLIB)
4015 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4016
4017 matvar->internal->z->avail_in = 0;
4018 InflateDataType(mat,matvar->internal->z,tag);
4019 if ( byteswap )
4020 (void)Mat_uint32Swap(tag);
4021
4022 packed_type = TYPE_FROM_TAG(tag[0]);
4023 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4024 data_in_tag = 1;
4025 nBytes = (tag[0] & 0xffff0000) >> 16;
4026 } else {
4027 data_in_tag = 0;
4028 InflateDataType(mat,matvar->internal->z,tag+1);
4029 if ( byteswap )
4030 (void)Mat_uint32Swap(tag+1);
4031 nBytes = tag[1];
4032 }
4033 #endif
4034 } else {
4035 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4036 fread(tag,4,1,mat->fp);
4037 if ( byteswap )
4038 (void)Mat_uint32Swap(tag);
4039 packed_type = TYPE_FROM_TAG(tag[0]);
4040 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4041 data_in_tag = 1;
4042 nBytes = (tag[0] & 0xffff0000) >> 16;
4043 } else {
4044 data_in_tag = 0;
4045 fread(tag+1,4,1,mat->fp);
4046 if ( byteswap )
4047 (void)Mat_uint32Swap(tag+1);
4048 nBytes = tag[1];
4049 }
4050 }
4051 if ( nBytes == 0 ) {
4052 matvar->nbytes = 0;
4053 break;
4054 }
4055 for ( i = 0; i < matvar->rank; i++ )
4056 len *= matvar->dims[i];
4057 matvar->data_size = sizeof(mat_int32_t);
4058 matvar->data_type = MAT_T_INT32;
4059 if ( matvar->isComplex ) {
4060 mat_complex_split_t *complex_data;
4061
4062 matvar->nbytes = len*matvar->data_size;
4063 complex_data = malloc(sizeof(*complex_data));
4064 complex_data->Re = malloc(matvar->nbytes);
4065 complex_data->Im = malloc(matvar->nbytes);
4066 if ( NULL == complex_data || NULL == complex_data->Re ||
4067 NULL == complex_data->Im ) {
4068 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
4069 break;
4070 }
4071 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4072 nBytes = ReadInt32Data(mat,complex_data->Re,
4073 packed_type,len);
4074 /*
4075 * If the data was in the tag we started on a 4-byte
4076 * boundary so add 4 to make it an 8-byte
4077 */
4078 if ( data_in_tag )
4079 nBytes+=4;
4080 if ( (nBytes % 8) != 0 )
4081 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4082
4083 /* Complex Data Tag */
4084 fread(tag,4,1,mat->fp);
4085 if ( byteswap )
4086 (void)Mat_uint32Swap(tag);
4087 packed_type = TYPE_FROM_TAG(tag[0]);
4088 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4089 data_in_tag = 1;
4090 nBytes = (tag[0] & 0xffff0000) >> 16;
4091 } else {
4092 data_in_tag = 0;
4093 fread(tag+1,4,1,mat->fp);
4094 if ( byteswap )
4095 (void)Mat_uint32Swap(tag+1);
4096 nBytes = tag[1];
4097 }
4098 nBytes = ReadInt32Data(mat,complex_data->Im,
4099 packed_type,len);
4100 #if defined(HAVE_ZLIB)
4101 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
4102 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
4103 complex_data->Re,packed_type,len);
4104 /*
4105 * If the data was in the tag we started on a 4-byte
4106 * boundary so add 4 to make it an 8-byte
4107 */
4108 if ( data_in_tag )
4109 nBytes+=4;
4110 if ( (nBytes % 8) != 0 )
4111 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4112
4113 /* Complex Data Tag */
4114 InflateDataType(mat,matvar->internal->z,tag);
4115 if ( byteswap )
4116 (void)Mat_uint32Swap(tag);
4117
4118 packed_type = TYPE_FROM_TAG(tag[0]);
4119 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4120 data_in_tag = 1;
4121 nBytes = (tag[0] & 0xffff0000) >> 16;
4122 } else {
4123 data_in_tag = 0;
4124 InflateDataType(mat,matvar->internal->z,tag+1);
4125 if ( byteswap )
4126 (void)Mat_uint32Swap(tag+1);
4127 nBytes = tag[1];
4128 }
4129 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
4130 complex_data->Im,packed_type,len);
4131 /*
4132 * If the data was in the tag we started on a 4-byte
4133 * boundary so add 4 to make it an 8-byte
4134 */
4135 if ( data_in_tag )
4136 nBytes+=4;
4137 if ( (nBytes % 8) != 0 )
4138 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4139 #endif
4140 }
4141 matvar->data = complex_data;
4142 } else {
4143 matvar->nbytes = len*matvar->data_size;
4144 matvar->data = malloc(matvar->nbytes);
4145 if ( !matvar->data ) {
4146 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
4147 break;
4148 }
4149 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4150 nBytes = ReadInt32Data(mat,(mat_int32_t*)matvar->data,
4151 packed_type,len);
4152 /*
4153 * If the data was in the tag we started on a 4-byte
4154 * boundary so add 4 to make it an 8-byte
4155 */
4156 if ( data_in_tag )
4157 nBytes+=4;
4158 if ( (nBytes % 8) != 0 )
4159 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4160 #if defined(HAVE_ZLIB)
4161 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
4162 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
4163 (mat_int32_t*)matvar->data,packed_type,len);
4164 /*
4165 * If the data was in the tag we started on a 4-byte
4166 * boundary so add 4 to make it an 8-byte
4167 */
4168 if ( data_in_tag )
4169 nBytes+=4;
4170 if ( (nBytes % 8) != 0 )
4171 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4172 #endif
4173 }
4174 }
4175 break;
4176 case MAT_C_UINT32:
4177 if ( matvar->compression ) {
4178 #if defined(HAVE_ZLIB)
4179 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4180
4181 matvar->internal->z->avail_in = 0;
4182 InflateDataType(mat,matvar->internal->z,tag);
4183 if ( byteswap )
4184 (void)Mat_uint32Swap(tag);
4185 packed_type = TYPE_FROM_TAG(tag[0]);
4186 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4187 data_in_tag = 1;
4188 nBytes = (tag[0] & 0xffff0000) >> 16;
4189 } else {
4190 data_in_tag = 0;
4191 InflateDataType(mat,matvar->internal->z,tag+1);
4192 if ( byteswap )
4193 (void)Mat_uint32Swap(tag+1);
4194 nBytes = tag[1];
4195 }
4196 #endif
4197 } else {
4198 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4199 fread(tag,4,1,mat->fp);
4200 if ( byteswap )
4201 (void)Mat_uint32Swap(tag);
4202 packed_type = TYPE_FROM_TAG(tag[0]);
4203 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4204 data_in_tag = 1;
4205 nBytes = (tag[0] & 0xffff0000) >> 16;
4206 } else {
4207 data_in_tag = 0;
4208 fread(tag+1,4,1,mat->fp);
4209 if ( byteswap )
4210 (void)Mat_uint32Swap(tag+1);
4211 nBytes = tag[1];
4212 }
4213 }
4214 if ( nBytes == 0 ) {
4215 matvar->nbytes = 0;
4216 break;
4217 }
4218 for ( i = 0; i < matvar->rank; i++ )
4219 len *= matvar->dims[i];
4220 matvar->data_size = sizeof(mat_uint32_t);
4221 matvar->data_type = MAT_T_UINT32;
4222 if ( matvar->isComplex ) {
4223 mat_complex_split_t *complex_data;
4224
4225 matvar->nbytes = len*matvar->data_size;
4226 complex_data = malloc(sizeof(*complex_data));
4227 complex_data->Re = malloc(matvar->nbytes);
4228 complex_data->Im = malloc(matvar->nbytes);
4229 if ( NULL == complex_data || NULL == complex_data->Re ||
4230 NULL == complex_data->Im ) {
4231 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
4232 break;
4233 }
4234 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4235 nBytes = ReadInt32Data(mat,complex_data->Re,
4236 packed_type,len);
4237 /*
4238 * If the data was in the tag we started on a 4-byte
4239 * boundary so add 4 to make it an 8-byte
4240 */
4241 if ( data_in_tag )
4242 nBytes+=4;
4243 if ( (nBytes % 8) != 0 )
4244 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4245
4246 /* Complex Data Tag */
4247 fread(tag,4,1,mat->fp);
4248 if ( byteswap )
4249 (void)Mat_uint32Swap(tag);
4250 packed_type = TYPE_FROM_TAG(tag[0]);
4251 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4252 data_in_tag = 1;
4253 nBytes = (tag[0] & 0xffff0000) >> 16;
4254 } else {
4255 data_in_tag = 0;
4256 fread(tag+1,4,1,mat->fp);
4257 if ( byteswap )
4258 (void)Mat_uint32Swap(tag+1);
4259 nBytes = tag[1];
4260 }
4261 nBytes = ReadInt32Data(mat,complex_data->Im,
4262 packed_type,len);
4263 #if defined(HAVE_ZLIB)
4264 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
4265 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
4266 complex_data->Re,packed_type,len);
4267 /*
4268 * If the data was in the tag we started on a 4-byte
4269 * boundary so add 4 to make it an 8-byte
4270 */
4271 if ( data_in_tag )
4272 nBytes+=4;
4273 if ( (nBytes % 8) != 0 )
4274 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4275
4276 /* Complex Data Tag */
4277 InflateDataType(mat,matvar->internal->z,tag);
4278 if ( byteswap )
4279 (void)Mat_uint32Swap(tag);
4280
4281 packed_type = TYPE_FROM_TAG(tag[0]);
4282 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4283 data_in_tag = 1;
4284 nBytes = (tag[0] & 0xffff0000) >> 16;
4285 } else {
4286 data_in_tag = 0;
4287 InflateDataType(mat,matvar->internal->z,tag+1);
4288 if ( byteswap )
4289 (void)Mat_uint32Swap(tag+1);
4290 nBytes = tag[1];
4291 }
4292 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
4293 complex_data->Im,packed_type,len);
4294 /*
4295 * If the data was in the tag we started on a 4-byte
4296 * boundary so add 4 to make it an 8-byte
4297 */
4298 if ( data_in_tag )
4299 nBytes+=4;
4300 if ( (nBytes % 8) != 0 )
4301 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4302 #endif
4303 }
4304 matvar->data = complex_data;
4305 } else {
4306 matvar->nbytes = len*matvar->data_size;
4307 matvar->data = malloc(matvar->nbytes);
4308 if ( !matvar->data ) {
4309 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
4310 break;
4311 }
4312 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4313 nBytes = ReadInt32Data(mat,(mat_int32_t*)matvar->data,
4314 packed_type,len);
4315 /*
4316 * If the data was in the tag we started on a 4-byte
4317 * boundary so add 4 to make it an 8-byte
4318 */
4319 if ( data_in_tag )
4320 nBytes+=4;
4321 if ( (nBytes % 8) != 0 )
4322 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4323 #if defined(HAVE_ZLIB)
4324 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
4325 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
4326 (mat_int32_t*)matvar->data,packed_type,len);
4327 /*
4328 * If the data was in the tag we started on a 4-byte
4329 * boundary so add 4 to make it an 8-byte
4330 */
4331 if ( data_in_tag )
4332 nBytes+=4;
4333 if ( (nBytes % 8) != 0 )
4334 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4335 #endif
4336 }
4337 }
4338 break;
4339 case MAT_C_INT16:
4340 if ( matvar->compression ) {
4341 #if defined(HAVE_ZLIB)
4342 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4343
4344 matvar->internal->z->avail_in = 0;
4345 InflateDataType(mat,matvar->internal->z,tag);
4346 if ( byteswap )
4347 (void)Mat_uint32Swap(tag);
4348 packed_type = TYPE_FROM_TAG(tag[0]);
4349 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4350 data_in_tag = 1;
4351 nBytes = (tag[0] & 0xffff0000) >> 16;
4352 } else {
4353 data_in_tag = 0;
4354 InflateDataType(mat,matvar->internal->z,tag+1);
4355 if ( byteswap )
4356 (void)Mat_uint32Swap(tag+1);
4357 nBytes = tag[1];
4358 }
4359 #endif
4360 } else {
4361 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4362 fread(tag,4,1,mat->fp);
4363 if ( byteswap )
4364 (void)Mat_uint32Swap(tag);
4365 packed_type = TYPE_FROM_TAG(tag[0]);
4366 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4367 data_in_tag = 1;
4368 nBytes = (tag[0] & 0xffff0000) >> 16;
4369 } else {
4370 data_in_tag = 0;
4371 fread(tag+1,4,1,mat->fp);
4372 if ( byteswap )
4373 (void)Mat_uint32Swap(tag+1);
4374 nBytes = tag[1];
4375 }
4376 }
4377 if ( nBytes == 0 ) {
4378 matvar->nbytes = 0;
4379 break;
4380 }
4381 for ( i = 0; i < matvar->rank; i++ )
4382 len *= matvar->dims[i];
4383 matvar->data_size = sizeof(mat_int16_t);
4384 matvar->data_type = MAT_T_INT16;
4385 if ( matvar->isComplex ) {
4386 mat_complex_split_t *complex_data;
4387
4388 matvar->nbytes = len*matvar->data_size;
4389 complex_data = malloc(sizeof(*complex_data));
4390 complex_data->Re = malloc(matvar->nbytes);
4391 complex_data->Im = malloc(matvar->nbytes);
4392 if ( NULL == complex_data || NULL == complex_data->Re ||
4393 NULL == complex_data->Im ) {
4394 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
4395 break;
4396 }
4397 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4398 nBytes = ReadInt16Data(mat,complex_data->Re,
4399 packed_type,len);
4400 /*
4401 * If the data was in the tag we started on a 4-byte
4402 * boundary so add 4 to make it an 8-byte
4403 */
4404 if ( data_in_tag )
4405 nBytes+=4;
4406 if ( (nBytes % 8) != 0 )
4407 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4408
4409 /* Complex Data Tag */
4410 fread(tag,4,1,mat->fp);
4411 if ( byteswap )
4412 (void)Mat_uint32Swap(tag);
4413 packed_type = TYPE_FROM_TAG(tag[0]);
4414 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4415 data_in_tag = 1;
4416 nBytes = (tag[0] & 0xffff0000) >> 16;
4417 } else {
4418 data_in_tag = 0;
4419 fread(tag+1,4,1,mat->fp);
4420 if ( byteswap )
4421 (void)Mat_uint32Swap(tag+1);
4422 nBytes = tag[1];
4423 }
4424 nBytes = ReadInt16Data(mat,complex_data->Im,
4425 packed_type,len);
4426 #if defined(HAVE_ZLIB)
4427 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
4428 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
4429 complex_data->Re,packed_type,len);
4430 /*
4431 * If the data was in the tag we started on a 4-byte
4432 * boundary so add 4 to make it an 8-byte
4433 */
4434 if ( data_in_tag )
4435 nBytes+=4;
4436 if ( (nBytes % 8) != 0 )
4437 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4438
4439 /* Complex Data Tag */
4440 InflateDataType(mat,matvar->internal->z,tag);
4441 if ( byteswap )
4442 (void)Mat_uint32Swap(tag);
4443
4444 packed_type = TYPE_FROM_TAG(tag[0]);
4445 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4446 data_in_tag = 1;
4447 nBytes = (tag[0] & 0xffff0000) >> 16;
4448 } else {
4449 data_in_tag = 0;
4450 InflateDataType(mat,matvar->internal->z,tag+1);
4451 if ( byteswap )
4452 (void)Mat_uint32Swap(tag+1);
4453 nBytes = tag[1];
4454 }
4455 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
4456 complex_data->Im,packed_type,len);
4457 /*
4458 * If the data was in the tag we started on a 4-byte
4459 * boundary so add 4 to make it an 8-byte
4460 */
4461 if ( data_in_tag )
4462 nBytes+=4;
4463 if ( (nBytes % 8) != 0 )
4464 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4465 #endif
4466 }
4467 matvar->data = complex_data;
4468 } else {
4469 matvar->nbytes = len*matvar->data_size;
4470 matvar->data = malloc(matvar->nbytes);
4471 if ( !matvar->data ) {
4472 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
4473 break;
4474 }
4475 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4476 nBytes = ReadInt16Data(mat,(mat_int16_t*)matvar->data,
4477 packed_type,len);
4478 /*
4479 * If the data was in the tag we started on a 4-byte
4480 * boundary so add 4 to make it an 8-byte
4481 */
4482 if ( data_in_tag )
4483 nBytes+=4;
4484 if ( (nBytes % 8) != 0 )
4485 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4486 #if defined(HAVE_ZLIB)
4487 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
4488 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
4489 (mat_int16_t*)matvar->data,packed_type,len);
4490 /*
4491 * If the data was in the tag we started on a 4-byte
4492 * boundary so add 4 to make it an 8-byte
4493 */
4494 if ( data_in_tag )
4495 nBytes+=4;
4496 if ( (nBytes % 8) != 0 )
4497 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4498 #endif
4499 }
4500 }
4501 break;
4502 case MAT_C_UINT16:
4503 if ( matvar->compression ) {
4504 #if defined(HAVE_ZLIB)
4505 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4506
4507 matvar->internal->z->avail_in = 0;
4508 InflateDataType(mat,matvar->internal->z,tag);
4509 if ( byteswap )
4510 (void)Mat_uint32Swap(tag);
4511 packed_type = TYPE_FROM_TAG(tag[0]);
4512 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4513 data_in_tag = 1;
4514 nBytes = (tag[0] & 0xffff0000) >> 16;
4515 } else {
4516 data_in_tag = 0;
4517 InflateDataType(mat,matvar->internal->z,tag+1);
4518 if ( byteswap )
4519 (void)Mat_uint32Swap(tag+1);
4520 nBytes = tag[1];
4521 }
4522 #endif
4523 } else {
4524 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4525 fread(tag,4,1,mat->fp);
4526 if ( byteswap )
4527 (void)Mat_uint32Swap(tag);
4528 packed_type = TYPE_FROM_TAG(tag[0]);
4529 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4530 data_in_tag = 1;
4531 nBytes = (tag[0] & 0xffff0000) >> 16;
4532 } else {
4533 data_in_tag = 0;
4534 fread(tag+1,4,1,mat->fp);
4535 if ( byteswap )
4536 (void)Mat_uint32Swap(tag+1);
4537 nBytes = tag[1];
4538 }
4539 }
4540 if ( nBytes == 0 ) {
4541 matvar->nbytes = 0;
4542 break;
4543 }
4544 for ( i = 0; i < matvar->rank; i++ )
4545 len *= matvar->dims[i];
4546 matvar->data_size = sizeof(mat_uint16_t);
4547 matvar->data_type = MAT_T_UINT16;
4548 if ( matvar->isComplex ) {
4549 mat_complex_split_t *complex_data;
4550
4551 matvar->nbytes = len*matvar->data_size;
4552 complex_data = malloc(sizeof(*complex_data));
4553 complex_data->Re = malloc(matvar->nbytes);
4554 complex_data->Im = malloc(matvar->nbytes);
4555 if ( NULL == complex_data || NULL == complex_data->Re ||
4556 NULL == complex_data->Im ) {
4557 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
4558 break;
4559 }
4560 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4561 nBytes = ReadInt16Data(mat,complex_data->Re,
4562 packed_type,len);
4563 /*
4564 * If the data was in the tag we started on a 4-byte
4565 * boundary so add 4 to make it an 8-byte
4566 */
4567 if ( data_in_tag )
4568 nBytes+=4;
4569 if ( (nBytes % 8) != 0 )
4570 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4571
4572 /* Complex Data Tag */
4573 fread(tag,4,1,mat->fp);
4574 if ( byteswap )
4575 (void)Mat_uint32Swap(tag);
4576 packed_type = TYPE_FROM_TAG(tag[0]);
4577 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4578 data_in_tag = 1;
4579 nBytes = (tag[0] & 0xffff0000) >> 16;
4580 } else {
4581 data_in_tag = 0;
4582 fread(tag+1,4,1,mat->fp);
4583 if ( byteswap )
4584 (void)Mat_uint32Swap(tag+1);
4585 nBytes = tag[1];
4586 }
4587 nBytes = ReadInt16Data(mat,complex_data->Im,
4588 packed_type,len);
4589 #if defined(HAVE_ZLIB)
4590 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
4591 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
4592 complex_data->Re,packed_type,len);
4593 /*
4594 * If the data was in the tag we started on a 4-byte
4595 * boundary so add 4 to make it an 8-byte
4596 */
4597 if ( data_in_tag )
4598 nBytes+=4;
4599 if ( (nBytes % 8) != 0 )
4600 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4601
4602 /* Complex Data Tag */
4603 InflateDataType(mat,matvar->internal->z,tag);
4604 if ( byteswap )
4605 (void)Mat_uint32Swap(tag);
4606
4607 packed_type = TYPE_FROM_TAG(tag[0]);
4608 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4609 data_in_tag = 1;
4610 nBytes = (tag[0] & 0xffff0000) >> 16;
4611 } else {
4612 data_in_tag = 0;
4613 InflateDataType(mat,matvar->internal->z,tag+1);
4614 if ( byteswap )
4615 (void)Mat_uint32Swap(tag+1);
4616 nBytes = tag[1];
4617 }
4618 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
4619 complex_data->Im,packed_type,len);
4620 /*
4621 * If the data was in the tag we started on a 4-byte
4622 * boundary so add 4 to make it an 8-byte
4623 */
4624 if ( data_in_tag )
4625 nBytes+=4;
4626 if ( (nBytes % 8) != 0 )
4627 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4628 #endif
4629 }
4630 matvar->data = complex_data;
4631 } else {
4632 matvar->nbytes = len*matvar->data_size;
4633 matvar->data = malloc(matvar->nbytes);
4634 if ( !matvar->data ) {
4635 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
4636 break;
4637 }
4638 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4639 nBytes = ReadInt16Data(mat,(mat_int16_t*)matvar->data,
4640 packed_type,len);
4641 /*
4642 * If the data was in the tag we started on a 4-byte
4643 * boundary so add 4 to make it an 8-byte
4644 */
4645 if ( data_in_tag )
4646 nBytes+=4;
4647 if ( (nBytes % 8) != 0 )
4648 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4649 #if defined(HAVE_ZLIB)
4650 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
4651 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
4652 (mat_int16_t*)matvar->data,packed_type,len);
4653 /*
4654 * If the data was in the tag we started on a 4-byte
4655 * boundary so add 4 to make it an 8-byte
4656 */
4657 if ( data_in_tag )
4658 nBytes+=4;
4659 if ( (nBytes % 8) != 0 )
4660 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4661 #endif
4662 }
4663 }
4664 break;
4665 case MAT_C_INT8:
4666 if ( matvar->compression ) {
4667 #if defined(HAVE_ZLIB)
4668 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4669
4670 matvar->internal->z->avail_in = 0;
4671 InflateDataType(mat,matvar->internal->z,tag);
4672 if ( byteswap )
4673 (void)Mat_uint32Swap(tag);
4674 packed_type = TYPE_FROM_TAG(tag[0]);
4675 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4676 data_in_tag = 1;
4677 nBytes = (tag[0] & 0xffff0000) >> 16;
4678 } else {
4679 data_in_tag = 0;
4680 InflateDataType(mat,matvar->internal->z,tag+1);
4681 if ( byteswap )
4682 (void)Mat_uint32Swap(tag+1);
4683 nBytes = tag[1];
4684 }
4685 #endif
4686 } else {
4687 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4688 fread(tag,4,1,mat->fp);
4689 if ( byteswap )
4690 (void)Mat_uint32Swap(tag);
4691 packed_type = TYPE_FROM_TAG(tag[0]);
4692 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4693 data_in_tag = 1;
4694 nBytes = (tag[0] & 0xffff0000) >> 16;
4695 } else {
4696 data_in_tag = 0;
4697 fread(tag+1,4,1,mat->fp);
4698 if ( byteswap )
4699 (void)Mat_uint32Swap(tag+1);
4700 nBytes = tag[1];
4701 }
4702 }
4703 if ( nBytes == 0 ) {
4704 matvar->nbytes = 0;
4705 break;
4706 }
4707 for ( i = 0; i < matvar->rank; i++ )
4708 len *= matvar->dims[i];
4709 matvar->data_size = sizeof(mat_int8_t);
4710 matvar->data_type = MAT_T_INT8;
4711 if ( matvar->isComplex ) {
4712 mat_complex_split_t *complex_data;
4713
4714 matvar->nbytes = len*matvar->data_size;
4715 complex_data = malloc(sizeof(*complex_data));
4716 complex_data->Re = malloc(matvar->nbytes);
4717 complex_data->Im = malloc(matvar->nbytes);
4718 if ( NULL == complex_data || NULL == complex_data->Re ||
4719 NULL == complex_data->Im ) {
4720 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
4721 break;
4722 }
4723 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4724 nBytes = ReadInt8Data(mat,complex_data->Re,
4725 packed_type,len);
4726 /*
4727 * If the data was in the tag we started on a 4-byte
4728 * boundary so add 4 to make it an 8-byte
4729 */
4730 if ( data_in_tag )
4731 nBytes+=4;
4732 if ( (nBytes % 8) != 0 )
4733 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4734
4735 /* Complex Data Tag */
4736 fread(tag,4,1,mat->fp);
4737 if ( byteswap )
4738 (void)Mat_uint32Swap(tag);
4739 packed_type = TYPE_FROM_TAG(tag[0]);
4740 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4741 data_in_tag = 1;
4742 nBytes = (tag[0] & 0xffff0000) >> 16;
4743 } else {
4744 data_in_tag = 0;
4745 fread(tag+1,4,1,mat->fp);
4746 if ( byteswap )
4747 (void)Mat_uint32Swap(tag+1);
4748 nBytes = tag[1];
4749 }
4750 nBytes = ReadInt8Data(mat,complex_data->Im,
4751 packed_type,len);
4752 #if defined(HAVE_ZLIB)
4753 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
4754 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
4755 complex_data->Re,packed_type,len);
4756 /*
4757 * If the data was in the tag we started on a 4-byte
4758 * boundary so add 4 to make it an 8-byte
4759 */
4760 if ( data_in_tag )
4761 nBytes+=4;
4762 if ( (nBytes % 8) != 0 )
4763 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4764
4765 /* Complex Data Tag */
4766 InflateDataType(mat,matvar->internal->z,tag);
4767 if ( byteswap )
4768 (void)Mat_uint32Swap(tag);
4769
4770 packed_type = TYPE_FROM_TAG(tag[0]);
4771 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4772 data_in_tag = 1;
4773 nBytes = (tag[0] & 0xffff0000) >> 16;
4774 } else {
4775 data_in_tag = 0;
4776 InflateDataType(mat,matvar->internal->z,tag+1);
4777 if ( byteswap )
4778 (void)Mat_uint32Swap(tag+1);
4779 nBytes = tag[1];
4780 }
4781 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
4782 complex_data->Im,packed_type,len);
4783 /*
4784 * If the data was in the tag we started on a 4-byte
4785 * boundary so add 4 to make it an 8-byte
4786 */
4787 if ( data_in_tag )
4788 nBytes+=4;
4789 if ( (nBytes % 8) != 0 )
4790 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4791 #endif
4792 }
4793 matvar->data = complex_data;
4794 } else {
4795 matvar->nbytes = len*matvar->data_size;
4796 matvar->data = malloc(matvar->nbytes);
4797 if ( !matvar->data ) {
4798 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
4799 break;
4800 }
4801 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4802 nBytes = ReadInt8Data(mat,(mat_int8_t*)matvar->data,
4803 packed_type,len);
4804 /*
4805 * If the data was in the tag we started on a 4-byte
4806 * boundary so add 4 to make it an 8-byte
4807 */
4808 if ( data_in_tag )
4809 nBytes+=4;
4810 if ( (nBytes % 8) != 0 )
4811 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4812 #if defined(HAVE_ZLIB)
4813 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
4814 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
4815 (mat_int8_t*)matvar->data,packed_type,len);
4816 /*
4817 * If the data was in the tag we started on a 4-byte
4818 * boundary so add 4 to make it an 8-byte
4819 */
4820 if ( data_in_tag )
4821 nBytes+=4;
4822 if ( (nBytes % 8) != 0 )
4823 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4824 #endif
4825 }
4826 }
4827 break;
4828 case MAT_C_UINT8:
4829 if ( matvar->compression ) {
4830 #if defined(HAVE_ZLIB)
4831 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4832
4833 matvar->internal->z->avail_in = 0;
4834 InflateDataType(mat,matvar->internal->z,tag);
4835 if ( byteswap )
4836 (void)Mat_uint32Swap(tag);
4837 packed_type = TYPE_FROM_TAG(tag[0]);
4838 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4839 data_in_tag = 1;
4840 nBytes = (tag[0] & 0xffff0000) >> 16;
4841 } else {
4842 data_in_tag = 0;
4843 InflateDataType(mat,matvar->internal->z,tag+1);
4844 if ( byteswap )
4845 (void)Mat_uint32Swap(tag+1);
4846 nBytes = tag[1];
4847 }
4848 #endif
4849 } else {
4850 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4851 fread(tag,4,1,mat->fp);
4852 if ( byteswap )
4853 (void)Mat_uint32Swap(tag);
4854 packed_type = TYPE_FROM_TAG(tag[0]);
4855 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4856 data_in_tag = 1;
4857 nBytes = (tag[0] & 0xffff0000) >> 16;
4858 } else {
4859 data_in_tag = 0;
4860 fread(tag+1,4,1,mat->fp);
4861 if ( byteswap )
4862 (void)Mat_uint32Swap(tag+1);
4863 nBytes = tag[1];
4864 }
4865 }
4866 if ( nBytes == 0 ) {
4867 matvar->nbytes = 0;
4868 break;
4869 }
4870 for ( i = 0; i < matvar->rank; i++ )
4871 len *= matvar->dims[i];
4872 matvar->data_size = sizeof(mat_uint8_t);
4873 matvar->data_type = MAT_T_UINT8;
4874 if ( matvar->isComplex ) {
4875 mat_complex_split_t *complex_data;
4876
4877 matvar->nbytes = len*matvar->data_size;
4878 complex_data = malloc(sizeof(*complex_data));
4879 complex_data->Re = malloc(matvar->nbytes);
4880 complex_data->Im = malloc(matvar->nbytes);
4881 if ( NULL == complex_data || NULL == complex_data->Re ||
4882 NULL == complex_data->Im ) {
4883 Mat_Critical("Failed to allocate %d bytes",2*matvar->nbytes);
4884 break;
4885 }
4886 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4887 nBytes = ReadInt8Data(mat,complex_data->Re,
4888 packed_type,len);
4889 /*
4890 * If the data was in the tag we started on a 4-byte
4891 * boundary so add 4 to make it an 8-byte
4892 */
4893 if ( data_in_tag )
4894 nBytes+=4;
4895 if ( (nBytes % 8) != 0 )
4896 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4897
4898 /* Complex Data Tag */
4899 fread(tag,4,1,mat->fp);
4900 if ( byteswap )
4901 (void)Mat_uint32Swap(tag);
4902 packed_type = TYPE_FROM_TAG(tag[0]);
4903 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4904 data_in_tag = 1;
4905 nBytes = (tag[0] & 0xffff0000) >> 16;
4906 } else {
4907 data_in_tag = 0;
4908 fread(tag+1,4,1,mat->fp);
4909 if ( byteswap )
4910 (void)Mat_uint32Swap(tag+1);
4911 nBytes = tag[1];
4912 }
4913 nBytes = ReadInt8Data(mat,complex_data->Im,
4914 packed_type,len);
4915 #if defined(HAVE_ZLIB)
4916 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
4917 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
4918 complex_data->Re,packed_type,len);
4919 /*
4920 * If the data was in the tag we started on a 4-byte
4921 * boundary so add 4 to make it an 8-byte
4922 */
4923 if ( data_in_tag )
4924 nBytes+=4;
4925 if ( (nBytes % 8) != 0 )
4926 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4927
4928 /* Complex Data Tag */
4929 InflateDataType(mat,matvar->internal->z,tag);
4930 if ( byteswap )
4931 (void)Mat_uint32Swap(tag);
4932
4933 packed_type = TYPE_FROM_TAG(tag[0]);
4934 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
4935 data_in_tag = 1;
4936 nBytes = (tag[0] & 0xffff0000) >> 16;
4937 } else {
4938 data_in_tag = 0;
4939 InflateDataType(mat,matvar->internal->z,tag+1);
4940 if ( byteswap )
4941 (void)Mat_uint32Swap(tag+1);
4942 nBytes = tag[1];
4943 }
4944 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
4945 complex_data->Im,packed_type,len);
4946 /*
4947 * If the data was in the tag we started on a 4-byte
4948 * boundary so add 4 to make it an 8-byte
4949 */
4950 if ( data_in_tag )
4951 nBytes+=4;
4952 if ( (nBytes % 8) != 0 )
4953 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4954 #endif
4955 }
4956 matvar->data = complex_data;
4957 } else {
4958 matvar->nbytes = len*matvar->data_size;
4959 matvar->data = malloc(matvar->nbytes);
4960 if ( !matvar->data ) {
4961 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
4962 break;
4963 }
4964 if ( matvar->compression == MAT_COMPRESSION_NONE) {
4965 nBytes = ReadInt8Data(mat,(mat_int8_t*)matvar->data,
4966 packed_type,len);
4967 /*
4968 * If the data was in the tag we started on a 4-byte
4969 * boundary so add 4 to make it an 8-byte
4970 */
4971 if ( data_in_tag )
4972 nBytes+=4;
4973 if ( (nBytes % 8) != 0 )
4974 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
4975 #if defined(HAVE_ZLIB)
4976 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
4977 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
4978 (mat_int8_t*)matvar->data,packed_type,len);
4979 /*
4980 * If the data was in the tag we started on a 4-byte
4981 * boundary so add 4 to make it an 8-byte
4982 */
4983 if ( data_in_tag )
4984 nBytes+=4;
4985 if ( (nBytes % 8) != 0 )
4986 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
4987 #endif
4988 }
4989 }
4990 break;
4991 break;
4992 case MAT_C_CHAR:
4993 if ( matvar->compression ) {
4994 #if defined(HAVE_ZLIB)
4995 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
4996
4997 matvar->internal->z->avail_in = 0;
4998 InflateDataType(mat,matvar->internal->z,tag);
4999 if ( byteswap )
5000 (void)Mat_uint32Swap(tag);
5001 packed_type = TYPE_FROM_TAG(tag[0]);
5002 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5003 data_in_tag = 1;
5004 nBytes = (tag[0] & 0xffff0000) >> 16;
5005 } else {
5006 data_in_tag = 0;
5007 InflateDataType(mat,matvar->internal->z,tag+1);
5008 if ( byteswap )
5009 (void)Mat_uint32Swap(tag+1);
5010 nBytes = tag[1];
5011 }
5012 #endif
5013 } else {
5014 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
5015 fread(tag,4,1,mat->fp);
5016 if ( byteswap )
5017 (void)Mat_uint32Swap(tag);
5018 packed_type = TYPE_FROM_TAG(tag[0]);
5019 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5020 data_in_tag = 1;
5021 nBytes = (tag[0] & 0xffff0000) >> 16;
5022 } else {
5023 data_in_tag = 0;
5024 fread(tag+1,4,1,mat->fp);
5025 if ( byteswap )
5026 (void)Mat_uint32Swap(tag+1);
5027 nBytes = tag[1];
5028 }
5029 }
5030 if ( nBytes == 0 ) {
5031 matvar->nbytes = 0;
5032 break;
5033 }
5034 for ( i = 0; i < matvar->rank; i++ )
5035 len *= matvar->dims[i];
5036 matvar->data_size = sizeof(char);
5037 /* FIXME: */
5038 matvar->data_type = MAT_T_UINT8;
5039 matvar->nbytes = len*matvar->data_size;
5040 matvar->data = calloc(matvar->nbytes+1,1);
5041 if ( !matvar->data ) {
5042 Mat_Critical("Failed to allocate %d bytes",matvar->nbytes);
5043 break;
5044 }
5045 if ( matvar->compression == MAT_COMPRESSION_NONE) {
5046 nBytes = ReadCharData(mat,(char*)matvar->data,packed_type,len);
5047 /*
5048 * If the data was in the tag we started on a 4-byte
5049 * boundary so add 4 to make it an 8-byte
5050 */
5051 if ( data_in_tag )
5052 nBytes+=4;
5053 if ( (nBytes % 8) != 0 )
5054 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
5055 #if defined(HAVE_ZLIB)
5056 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
5057 nBytes = ReadCompressedCharData(mat,matvar->internal->z,
5058 (char*)matvar->data,packed_type,len);
5059 /*
5060 * If the data was in the tag we started on a 4-byte
5061 * boundary so add 4 to make it an 8-byte
5062 */
5063 if ( data_in_tag )
5064 nBytes+=4;
5065 if ( (nBytes % 8) != 0 )
5066 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
5067 #endif
5068 }
5069 break;
5070 case MAT_C_STRUCT:
5071 {
5072 matvar_t **fields;
5073 int nfields = 0;
5074
5075 matvar->data_type = MAT_T_STRUCT;
5076 if ( !matvar->nbytes || !matvar->data_size || NULL == matvar->data )
5077 break;
5078 len = 1;
5079 for ( i = 0; i < matvar->rank; i++ )
5080 len *= matvar->dims[i];
5081 nfields = matvar->internal->num_fields;
5082 fields = (matvar_t **)matvar->data;
5083 for ( i = 0; i < len*nfields; i++ ) {
5084 fields[i]->internal->fp = mat;
5085 Read5(mat,fields[i]);
5086 }
5087 break;
5088 }
5089 case MAT_C_CELL:
5090 {
5091 matvar_t **cells;
5092
5093 if ( !matvar->data ) {
5094 Mat_Critical("Data is NULL for Cell Array %s",matvar->name);
5095 break;
5096 }
5097 for ( i = 0; i < matvar->rank; i++ )
5098 len *= matvar->dims[i];
5099 cells = (matvar_t **)matvar->data;
5100 for ( i = 0; i < len; i++ ) {
5101 cells[i]->internal->fp = mat;
5102 Read5(mat,cells[i]);
5103 }
5104 /* FIXME: */
5105 matvar->data_type = MAT_T_CELL;
5106 break;
5107 }
5108 case MAT_C_SPARSE:
5109 {
5110 int N;
5111 mat_sparse_t *data;
5112
5113 matvar->data_size = sizeof(mat_sparse_t);
5114 matvar->data = malloc(matvar->data_size);
5115 if ( matvar->data == NULL ) {
5116 Mat_Critical("ReadData: Allocation of data pointer failed");
5117 break;
5118 }
5119 data = matvar->data;
5120 data->nzmax = matvar->nbytes;
5121 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
5122 /* Read ir */
5123 if ( matvar->compression ) {
5124 #if defined(HAVE_ZLIB)
5125 matvar->internal->z->avail_in = 0;
5126 InflateDataType(mat,matvar->internal->z,tag);
5127 if ( mat->byteswap )
5128 (void)Mat_uint32Swap(tag);
5129
5130 packed_type = TYPE_FROM_TAG(tag[0]);
5131 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5132 data_in_tag = 1;
5133 N = (tag[0] & 0xffff0000) >> 16;
5134 } else {
5135 data_in_tag = 0;
5136 (void)ReadCompressedInt32Data(mat,matvar->internal->z,
5137 (mat_int32_t*)&N,MAT_T_INT32,1);
5138 }
5139 #endif
5140 } else {
5141 fread(tag,4,1,mat->fp);
5142 if ( mat->byteswap )
5143 (void)Mat_uint32Swap(tag);
5144 packed_type = TYPE_FROM_TAG(tag[0]);
5145 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5146 data_in_tag = 1;
5147 N = (tag[0] & 0xffff0000) >> 16;
5148 } else {
5149 data_in_tag = 0;
5150 fread(&N,4,1,mat->fp);
5151 if ( mat->byteswap )
5152 Mat_int32Swap(&N);
5153 }
5154 }
5155 data->nir = N / 4;
5156 data->ir = malloc(data->nir*sizeof(mat_int32_t));
5157 if ( data->ir != NULL ) {
5158 if ( matvar->compression == MAT_COMPRESSION_NONE) {
5159 nBytes = ReadInt32Data(mat,data->ir,packed_type,data->nir);
5160 /*
5161 * If the data was in the tag we started on a 4-byte
5162 * boundary so add 4 to make it an 8-byte
5163 */
5164 if ( data_in_tag )
5165 nBytes+=4;
5166 if ( (nBytes % 8) != 0 )
5167 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
5168 #if defined(HAVE_ZLIB)
5169 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
5170 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
5171 data->ir,packed_type,data->nir);
5172 /*
5173 * If the data was in the tag we started on a 4-byte
5174 * boundary so add 4 to make it an 8-byte
5175 */
5176 if ( data_in_tag )
5177 nBytes+=4;
5178 if ( (nBytes % 8) != 0 )
5179 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
5180 #endif
5181 }
5182 } else {
5183 Mat_Critical("ReadData: Allocation of ir pointer failed");
5184 break;
5185 }
5186 /* Read jc */
5187 if ( matvar->compression ) {
5188 #if defined(HAVE_ZLIB)
5189 matvar->internal->z->avail_in = 0;
5190 InflateDataType(mat,matvar->internal->z,tag);
5191 if ( mat->byteswap )
5192 Mat_uint32Swap(tag);
5193 packed_type = TYPE_FROM_TAG(tag[0]);
5194 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5195 data_in_tag = 1;
5196 N = (tag[0] & 0xffff0000) >> 16;
5197 } else {
5198 data_in_tag = 0;
5199 (void)ReadCompressedInt32Data(mat,matvar->internal->z,
5200 (mat_int32_t*)&N,MAT_T_INT32,1);
5201 }
5202 #endif
5203 } else {
5204 fread(tag,4,1,mat->fp);
5205 if ( mat->byteswap )
5206 Mat_uint32Swap(tag);
5207 packed_type = TYPE_FROM_TAG(tag[0]);
5208 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5209 data_in_tag = 1;
5210 N = (tag[0] & 0xffff0000) >> 16;
5211 } else {
5212 data_in_tag = 0;
5213 fread(&N,4,1,mat->fp);
5214 if ( mat->byteswap )
5215 Mat_int32Swap(&N);
5216 }
5217 }
5218 data->njc = N / 4;
5219 data->jc = malloc(data->njc*sizeof(mat_int32_t));
5220 if ( data->jc != NULL ) {
5221 if ( matvar->compression == MAT_COMPRESSION_NONE) {
5222 nBytes = ReadInt32Data(mat,data->jc,packed_type,data->njc);
5223 /*
5224 * If the data was in the tag we started on a 4-byte
5225 * boundary so add 4 to make it an 8-byte
5226 */
5227 if ( data_in_tag )
5228 nBytes+=4;
5229 if ( (nBytes % 8) != 0 )
5230 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
5231 #if defined(HAVE_ZLIB)
5232 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
5233 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
5234 data->jc,packed_type,data->njc);
5235 /*
5236 * If the data was in the tag we started on a 4-byte
5237 * boundary so add 4 to make it an 8-byte
5238 */
5239 if ( data_in_tag )
5240 nBytes+=4;
5241 if ( (nBytes % 8) != 0 )
5242 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
5243 #endif
5244 }
5245 } else {
5246 Mat_Critical("ReadData: Allocation of jc pointer failed");
5247 break;
5248 }
5249 /* Read data */
5250 if ( matvar->compression ) {
5251 #if defined(HAVE_ZLIB)
5252 matvar->internal->z->avail_in = 0;
5253 InflateDataType(mat,matvar->internal->z,tag);
5254 if ( mat->byteswap )
5255 Mat_uint32Swap(tag);
5256 packed_type = TYPE_FROM_TAG(tag[0]);
5257 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5258 data_in_tag = 1;
5259 N = (tag[0] & 0xffff0000) >> 16;
5260 } else {
5261 data_in_tag = 0;
5262 (void)ReadCompressedInt32Data(mat,matvar->internal->z,
5263 (mat_int32_t*)&N,MAT_T_INT32,1);
5264 }
5265 #endif
5266 } else {
5267 fread(tag,4,1,mat->fp);
5268 if ( mat->byteswap )
5269 Mat_uint32Swap(tag);
5270 packed_type = TYPE_FROM_TAG(tag[0]);
5271 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5272 data_in_tag = 1;
5273 N = (tag[0] & 0xffff0000) >> 16;
5274 } else {
5275 data_in_tag = 0;
5276 fread(&N,4,1,mat->fp);
5277 if ( mat->byteswap )
5278 Mat_int32Swap(&N);
5279 }
5280 }
5281 #if defined(EXTENDED_SPARSE)
5282 matvar->data_type = packed_type;
5283 #else
5284 matvar->data_type = MAT_T_DOUBLE;
5285 #endif
5286 data->ndata = N / Mat_SizeOf(packed_type);
5287 if ( matvar->isComplex ) {
5288 mat_complex_split_t *complex_data;
5289
5290 complex_data = malloc(sizeof(*complex_data));
5291 complex_data->Re = malloc(data->ndata*
5292 Mat_SizeOf(matvar->data_type));
5293 complex_data->Im = malloc(data->ndata*
5294 Mat_SizeOf(matvar->data_type));
5295 if ( NULL != complex_data && NULL != complex_data->Re &&
5296 NULL != complex_data->Im ) {
5297 if ( matvar->compression == MAT_COMPRESSION_NONE) {
5298 #if defined(EXTENDED_SPARSE)
5299 switch ( matvar->data_type ) {
5300 case MAT_T_DOUBLE:
5301 nBytes = ReadDoubleData(mat,complex_data->Re,
5302 packed_type,data->ndata);
5303 break;
5304 case MAT_T_SINGLE:
5305 nBytes = ReadSingleData(mat,complex_data->Re,
5306 packed_type,data->ndata);
5307 break;
5308 case MAT_T_INT32:
5309 nBytes = ReadInt32Data(mat,complex_data->Re,
5310 packed_type,data->ndata);
5311 break;
5312 case MAT_T_UINT32:
5313 nBytes = ReadInt32Data(mat,complex_data->Re,
5314 packed_type,data->ndata);
5315 break;
5316 case MAT_T_INT16:
5317 nBytes = ReadInt16Data(mat,complex_data->Re,
5318 packed_type,data->ndata);
5319 break;
5320 case MAT_T_UINT16:
5321 nBytes = ReadInt16Data(mat,complex_data->Re,
5322 packed_type,data->ndata);
5323 break;
5324 case MAT_T_INT8:
5325 nBytes = ReadInt8Data(mat,complex_data->Re,
5326 packed_type,data->ndata);
5327 break;
5328 case MAT_T_UINT8:
5329 nBytes = ReadInt8Data(mat,complex_data->Re,
5330 packed_type,data->ndata);
5331 break;
5332 }
5333 #else
5334 nBytes = ReadDoubleData(mat,complex_data->Re,
5335 packed_type,data->ndata);
5336 #endif
5337 if ( data_in_tag )
5338 nBytes+=4;
5339 if ( (nBytes % 8) != 0 )
5340 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
5341
5342 /* Complex Data Tag */
5343 fread(tag,4,1,mat->fp);
5344 if ( byteswap )
5345 (void)Mat_uint32Swap(tag);
5346 packed_type = TYPE_FROM_TAG(tag[0]);
5347 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5348 data_in_tag = 1;
5349 nBytes = (tag[0] & 0xffff0000) >> 16;
5350 } else {
5351 data_in_tag = 0;
5352 fread(tag+1,4,1,mat->fp);
5353 if ( byteswap )
5354 (void)Mat_uint32Swap(tag+1);
5355 nBytes = tag[1];
5356 }
5357 #if defined(EXTENDED_SPARSE)
5358 switch ( matvar->data_type ) {
5359 case MAT_T_DOUBLE:
5360 nBytes = ReadDoubleData(mat,complex_data->Im,
5361 packed_type,data->ndata);
5362 break;
5363 case MAT_T_SINGLE:
5364 nBytes = ReadSingleData(mat,complex_data->Im,
5365 packed_type,data->ndata);
5366 break;
5367 case MAT_T_INT32:
5368 nBytes = ReadInt32Data(mat,complex_data->Im,
5369 packed_type,data->ndata);
5370 break;
5371 case MAT_T_UINT32:
5372 nBytes = ReadUInt32Data(mat,complex_data->Im,
5373 packed_type,data->ndata);
5374 break;
5375 case MAT_T_INT16:
5376 nBytes = ReadInt16Data(mat,complex_data->Im,
5377 packed_type,data->ndata);
5378 break;
5379 case MAT_T_UINT16:
5380 nBytes = ReadUInt16Data(mat,complex_data->Im,
5381 packed_type,data->ndata);
5382 break;
5383 case MAT_T_INT8:
5384 nBytes = ReadInt8Data(mat,complex_data->Im,
5385 packed_type,data->ndata);
5386 break;
5387 case MAT_T_UINT8:
5388 nBytes = ReadUInt8Data(mat,complex_data->Im,
5389 packed_type,data->ndata);
5390 break;
5391 }
5392 #else /* EXTENDED_SPARSE */
5393 nBytes = ReadDoubleData(mat,complex_data->Im,
5394 packed_type,data->ndata);
5395 #endif /* EXTENDED_SPARSE */
5396 if ( data_in_tag )
5397 nBytes+=4;
5398 if ( (nBytes % 8) != 0 )
5399 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
5400 #if defined(HAVE_ZLIB)
5401 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
5402 #if defined(EXTENDED_SPARSE)
5403 switch ( matvar->data_type ) {
5404 case MAT_T_DOUBLE:
5405 nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
5406 complex_data->Re,packed_type,data->ndata);
5407 break;
5408 case MAT_T_SINGLE:
5409 nBytes = ReadCompressedSingleData(mat,matvar->internal->z,
5410 complex_data->Re,packed_type,data->ndata);
5411 break;
5412 case MAT_T_INT32:
5413 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
5414 complex_data->Re,packed_type,data->ndata);
5415 break;
5416 case MAT_T_UINT32:
5417 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
5418 complex_data->Re,packed_type,data->ndata);
5419 break;
5420 case MAT_T_INT16:
5421 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
5422 complex_data->Re,packed_type,data->ndata);
5423 break;
5424 case MAT_T_UINT16:
5425 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
5426 complex_data->Re,packed_type,data->ndata);
5427 break;
5428 case MAT_T_INT8:
5429 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
5430 complex_data->Re,packed_type,data->ndata);
5431 break;
5432 case MAT_T_UINT8:
5433 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
5434 complex_data->Re,packed_type,data->ndata);
5435 break;
5436 }
5437 #else /* EXTENDED_SPARSE */
5438 nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
5439 complex_data->Re,packed_type,data->ndata);
5440 #endif /* EXTENDED_SPARSE */
5441 if ( data_in_tag )
5442 nBytes+=4;
5443 if ( (nBytes % 8) != 0 )
5444 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
5445
5446 /* Complex Data Tag */
5447 InflateDataType(mat,matvar->internal->z,tag);
5448 if ( byteswap )
5449 (void)Mat_uint32Swap(tag);
5450
5451 packed_type = TYPE_FROM_TAG(tag[0]);
5452 if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
5453 data_in_tag = 1;
5454 nBytes = (tag[0] & 0xffff0000) >> 16;
5455 } else {
5456 data_in_tag = 0;
5457 InflateDataType(mat,matvar->internal->z,tag+1);
5458 if ( byteswap )
5459 (void)Mat_uint32Swap(tag+1);
5460 nBytes = tag[1];
5461 }
5462 #if defined(EXTENDED_SPARSE)
5463 switch ( matvar->data_type ) {
5464 case MAT_T_DOUBLE:
5465 nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
5466 complex_data->Im,packed_type,data->ndata);
5467 break;
5468 case MAT_T_SINGLE:
5469 nBytes = ReadCompressedSingleData(mat,matvar->internal->z,
5470 complex_data->Im,packed_type,data->ndata);
5471 break;
5472 case MAT_T_INT32:
5473 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
5474 complex_data->Im,packed_type,data->ndata);
5475 break;
5476 case MAT_T_UINT32:
5477 nBytes = ReadCompressedUInt32Data(mat,matvar->internal->z,
5478 complex_data->Im,packed_type,data->ndata);
5479 break;
5480 case MAT_T_INT16:
5481 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
5482 complex_data->Im,packed_type,data->ndata);
5483 break;
5484 case MAT_T_UINT16:
5485 nBytes = ReadCompressedUInt16Data(mat,matvar->internal->z,
5486 complex_data->Im,packed_type,data->ndata);
5487 break;
5488 case MAT_T_INT8:
5489 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
5490 complex_data->Im,packed_type,data->ndata);
5491 break;
5492 case MAT_T_UINT8:
5493 nBytes = ReadCompressedUInt8Data(mat,matvar->internal->z,
5494 complex_data->Im,packed_type,data->ndata);
5495 break;
5496 }
5497 #else /* EXTENDED_SPARSE */
5498 nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
5499 complex_data->Im,packed_type,data->ndata);
5500 #endif /* EXTENDED_SPARSE */
5501 if ( data_in_tag )
5502 nBytes+=4;
5503 if ( (nBytes % 8) != 0 )
5504 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
5505 #endif /* HAVE_ZLIB */
5506 }
5507 } else {
5508 Mat_Critical("ReadData: Allocation of data pointer failed");
5509 break;
5510 }
5511 data->data = complex_data;
5512 } else { /* isComplex */
5513 data->data = malloc(data->ndata*Mat_SizeOf(MAT_T_DOUBLE));
5514 if ( data->data != NULL ) {
5515 if ( matvar->compression == MAT_COMPRESSION_NONE) {
5516 #if defined(EXTENDED_SPARSE)
5517 switch ( matvar->data_type ) {
5518 case MAT_T_DOUBLE:
5519 nBytes = ReadDoubleData(mat,data->data,
5520 packed_type,data->ndata);
5521 break;
5522 case MAT_T_SINGLE:
5523 nBytes = ReadSingleData(mat,data->data,
5524 packed_type,data->ndata);
5525 break;
5526 case MAT_T_INT32:
5527 nBytes = ReadInt32Data(mat,data->data,
5528 packed_type,data->ndata);
5529 break;
5530 case MAT_T_UINT32:
5531 nBytes = ReadInt32Data(mat,data->data,
5532 packed_type,data->ndata);
5533 break;
5534 case MAT_T_INT16:
5535 nBytes = ReadInt16Data(mat,data->data,
5536 packed_type,data->ndata);
5537 break;
5538 case MAT_T_UINT16:
5539 nBytes = ReadInt16Data(mat,data->data,
5540 packed_type,data->ndata);
5541 break;
5542 case MAT_T_INT8:
5543 nBytes = ReadInt8Data(mat,data->data,
5544 packed_type,data->ndata);
5545 break;
5546 case MAT_T_UINT8:
5547 nBytes = ReadInt8Data(mat,data->data,
5548 packed_type,data->ndata);
5549 break;
5550 }
5551 #else
5552 nBytes = ReadDoubleData(mat,data->data,packed_type,
5553 data->ndata);
5554 #endif
5555 if ( data_in_tag )
5556 nBytes+=4;
5557 if ( (nBytes % 8) != 0 )
5558 fseek(mat->fp,8-(nBytes % 8),SEEK_CUR);
5559 #if defined(HAVE_ZLIB)
5560 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB) {
5561 #if defined(EXTENDED_SPARSE)
5562 switch ( matvar->data_type ) {
5563 case MAT_T_DOUBLE:
5564 nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
5565 data->data,packed_type,data->ndata);
5566 break;
5567 case MAT_T_SINGLE:
5568 nBytes = ReadCompressedSingleData(mat,matvar->internal->z,
5569 data->data,packed_type,data->ndata);
5570 break;
5571 case MAT_T_INT32:
5572 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
5573 data->data,packed_type,data->ndata);
5574 break;
5575 case MAT_T_UINT32:
5576 nBytes = ReadCompressedInt32Data(mat,matvar->internal->z,
5577 data->data,packed_type,data->ndata);
5578 break;
5579 case MAT_T_INT16:
5580 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
5581 data->data,packed_type,data->ndata);
5582 break;
5583 case MAT_T_UINT16:
5584 nBytes = ReadCompressedInt16Data(mat,matvar->internal->z,
5585 data->data,packed_type,data->ndata);
5586 break;
5587 case MAT_T_INT8:
5588 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
5589 data->data,packed_type,data->ndata);
5590 break;
5591 case MAT_T_UINT8:
5592 nBytes = ReadCompressedInt8Data(mat,matvar->internal->z,
5593 data->data,packed_type,data->ndata);
5594 break;
5595 }
5596 #else /* EXTENDED_SPARSE */
5597 nBytes = ReadCompressedDoubleData(mat,matvar->internal->z,
5598 data->data,packed_type,data->ndata);
5599 #endif /* EXTENDED_SPARSE */
5600 if ( data_in_tag )
5601 nBytes+=4;
5602 if ( (nBytes % 8) != 0 )
5603 InflateSkip(mat,matvar->internal->z,8-(nBytes % 8));
5604 #endif /* HAVE_ZLIB */
5605 }
5606 } else {
5607 Mat_Critical("ReadData: Allocation of data pointer failed");
5608 break;
5609 }
5610 }
5611 break;
5612 }
5613 case MAT_C_FUNCTION:
5614 {
5615 matvar_t **functions;
5616 int nfunctions = 0;
5617
5618 if ( !matvar->nbytes || !matvar->data_size )
5619 break;
5620 nfunctions = matvar->nbytes / matvar->data_size;
5621 functions = (matvar_t **)matvar->data;
5622 for ( i = 0; i < nfunctions; i++ ) {
5623 functions[i]->internal->fp = mat;
5624 Read5(mat,functions[i]);
5625 }
5626 /* FIXME: */
5627 matvar->data_type = MAT_T_FUNCTION;
5628 break;
5629 }
5630 default:
5631 Mat_Critical("Read5: %d is not a supported Class", matvar->class_type);
5632 }
5633 fseek(mat->fp,fpos,SEEK_SET);
5634
5635 return;
5636 }
5637
5638 /** @if mat_devman
5639 * @brief Reads a slab of data from the mat variable @c matvar
5640 *
5641 * @ingroup mat_internal
5642 * @param mat MAT file pointer
5643 * @param matvar pointer to the mat variable
5644 * @param data pointer to store the read data in (must be of size
5645 * edge[0]*...edge[rank-1]*Mat_SizeOfClass(matvar->class_type))
5646 * @param start index to start reading data in each dimension
5647 * @param stride write data every @c stride elements in each dimension
5648 * @param edge number of elements to read in each dimension
5649 * @retval 0 on success
5650 * @endif
5651 */
5652 int
5653 ReadData5(mat_t *mat,matvar_t *matvar,void *data,
5654 int *start,int *stride,int *edge)
5655 {
5656 int err = 0,real_bytes;
5657 mat_int32_t tag[2];
5658 #if defined(HAVE_ZLIB)
5659 z_stream z;
5660 #endif
5661
5662 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
5663 if ( matvar->compression == MAT_COMPRESSION_NONE ) {
5664 fread(tag,4,2,mat->fp);
5665 if ( mat->byteswap ) {
5666 Mat_int32Swap(tag);
5667 Mat_int32Swap(tag+1);
5668 }
5669 matvar->data_type = TYPE_FROM_TAG(tag[0]);
5670 if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
5671 fseek(mat->fp,-4,SEEK_CUR);
5672 real_bytes = 4+(tag[0] >> 16);
5673 } else {
5674 real_bytes = 8+tag[1];
5675 }
5676 #if defined(HAVE_ZLIB)
5677 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
5678 err = inflateCopy(&z,matvar->internal->z);
5679 z.avail_in = 0;
5680 InflateDataType(mat,&z,tag);
5681 if ( mat->byteswap ) {
5682 Mat_int32Swap(tag);
5683 }
5684 matvar->data_type = TYPE_FROM_TAG(tag[0]);
5685 if ( !(tag[0] & 0xffff0000) ) {/* Data is NOT packed in the tag */
5686 /* We're cheating, but InflateDataType just inflates 4 bytes */
5687 InflateDataType(mat,&z,tag+1);
5688 if ( mat->byteswap ) {
5689 Mat_int32Swap(tag+1);
5690 }
5691 real_bytes = 8+tag[1];
5692 } else {
5693 real_bytes = 4+(tag[0] >> 16);
5694 }
5695 #endif
5696 }
5697 if ( real_bytes % 8 )
5698 real_bytes += (8-(real_bytes % 8));
5699
5700 if ( matvar->rank == 2 ) {
5701 if ( stride[0]*(edge[0]-1)+start[0]+1 > matvar->dims[0] )
5702 err = 1;
5703 else if ( stride[1]*(edge[1]-1)+start[1]+1 > matvar->dims[1] )
5704 err = 1;
5705 else if ( matvar->compression == MAT_COMPRESSION_NONE ) {
5706 if ( matvar->isComplex ) {
5707 mat_complex_split_t *complex_data = data;
5708
5709 ReadDataSlab2(mat,complex_data->Re,matvar->class_type,
5710 matvar->data_type,matvar->dims,start,stride,edge);
5711 fseek(mat->fp,matvar->internal->datapos+real_bytes,SEEK_SET);
5712 fread(tag,4,2,mat->fp);
5713 if ( mat->byteswap ) {
5714 Mat_int32Swap(tag);
5715 Mat_int32Swap(tag+1);
5716 }
5717 matvar->data_type = TYPE_FROM_TAG(tag[0]);
5718 if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
5719 fseek(mat->fp,-4,SEEK_CUR);
5720 }
5721 ReadDataSlab2(mat,complex_data->Im,matvar->class_type,
5722 matvar->data_type,matvar->dims,start,stride,edge);
5723 } else {
5724 ReadDataSlab2(mat,data,matvar->class_type,
5725 matvar->data_type,matvar->dims,start,stride,edge);
5726 }
5727 }
5728 #if defined(HAVE_ZLIB)
5729 else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
5730 if ( matvar->isComplex ) {
5731 mat_complex_split_t *complex_data = data;
5732
5733 ReadCompressedDataSlab2(mat,&z,complex_data->Re,
5734 matvar->class_type,matvar->data_type,matvar->dims,
5735 start,stride,edge);
5736
5737 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
5738
5739 /* Reset zlib knowledge to before reading real tag */
5740 inflateEnd(&z);
5741 err = inflateCopy(&z,matvar->internal->z);
5742 InflateSkip(mat,&z,real_bytes);
5743 z.avail_in = 0;
5744 InflateDataType(mat,&z,tag);
5745 if ( mat->byteswap ) {
5746 Mat_int32Swap(tag);
5747 }
5748 matvar->data_type = TYPE_FROM_TAG(tag[0]);
5749 if ( !(tag[0] & 0xffff0000) ) {/*Data is NOT packed in the tag*/
5750 InflateSkip(mat,&z,4);
5751 }
5752 ReadCompressedDataSlab2(mat,&z,complex_data->Im,
5753 matvar->class_type,matvar->data_type,matvar->dims,
5754 start,stride,edge);
5755 inflateEnd(&z);
5756 } else {
5757 ReadCompressedDataSlab2(mat,&z,data,matvar->class_type,
5758 matvar->data_type,matvar->dims,start,stride,edge);
5759 }
5760 }
5761 #endif
5762 } else {
5763 if ( matvar->compression == MAT_COMPRESSION_NONE ) {
5764 if ( matvar->isComplex ) {
5765 mat_complex_split_t *complex_data = data;
5766
5767 ReadDataSlabN(mat,complex_data->Re,matvar->class_type,
5768 matvar->data_type,matvar->rank,matvar->dims,
5769 start,stride,edge);
5770
5771 fseek(mat->fp,matvar->internal->datapos+real_bytes,SEEK_SET);
5772 fread(tag,4,2,mat->fp);
5773 if ( mat->byteswap ) {
5774 Mat_int32Swap(tag);
5775 Mat_int32Swap(tag+1);
5776 }
5777 matvar->data_type = TYPE_FROM_TAG(tag[0]);
5778 if ( tag[0] & 0xffff0000 ) { /* Data is packed in the tag */
5779 fseek(mat->fp,-4,SEEK_CUR);
5780 }
5781 ReadDataSlabN(mat,complex_data->Im,matvar->class_type,
5782 matvar->data_type,matvar->rank,matvar->dims,
5783 start,stride,edge);
5784 } else {
5785 ReadDataSlabN(mat,data,matvar->class_type,matvar->data_type,
5786 matvar->rank,matvar->dims,start,stride,edge);
5787 }
5788 }
5789 #if defined(HAVE_ZLIB)
5790 else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
5791 if ( matvar->isComplex ) {
5792 mat_complex_split_t *complex_data = data;
5793
5794 ReadCompressedDataSlabN(mat,&z,complex_data->Re,
5795 matvar->class_type,matvar->data_type,matvar->rank,
5796 matvar->dims,start,stride,edge);
5797
5798 fseek(mat->fp,matvar->internal->datapos,SEEK_SET);
5799 /* Reset zlib knowledge to before reading real tag */
5800 inflateEnd(&z);
5801 err = inflateCopy(&z,matvar->internal->z);
5802 InflateSkip(mat,&z,real_bytes);
5803 z.avail_in = 0;
5804 InflateDataType(mat,&z,tag);
5805 if ( mat->byteswap ) {
5806 Mat_int32Swap(tag);
5807 }
5808 matvar->data_type = TYPE_FROM_TAG(tag[0]);
5809 if ( !(tag[0] & 0xffff0000) ) {/*Data is NOT packed in the tag*/
5810 InflateSkip(mat,&z,4);
5811 }
5812 ReadCompressedDataSlabN(mat,&z,complex_data->Im,
5813 matvar->class_type,matvar->data_type,matvar->rank,
5814 matvar->dims,start,stride,edge);
5815 inflateEnd(&z);
5816 } else {
5817 ReadCompressedDataSlabN(mat,&z,data,matvar->class_type,
5818 matvar->data_type,matvar->rank,matvar->dims,
5819 start,stride,edge);
5820 }
5821 }
5822 #endif
5823 }
5824 if ( err )
5825 return err;
5826
5827 switch(matvar->class_type) {
5828 case MAT_C_DOUBLE:
5829 matvar->data_type = MAT_T_DOUBLE;
5830 matvar->data_size = sizeof(double);
5831 break;
5832 case MAT_C_SINGLE:
5833 matvar->data_type = MAT_T_SINGLE;
5834 matvar->data_size = sizeof(float);
5835 break;
5836 #ifdef HAVE_MAT_INT64_T
5837 case MAT_C_INT64:
5838 matvar->data_type = MAT_T_INT64;
5839 matvar->data_size = sizeof(mat_int64_t);
5840 break;
5841 #endif /* HAVE_MAT_INT64_T */
5842 #ifdef HAVE_MAT_UINT64_T
5843 case MAT_C_UINT64:
5844 matvar->data_type = MAT_T_UINT64;
5845 matvar->data_size = sizeof(mat_uint64_t);
5846 break;
5847 #endif /* HAVE_MAT_UINT64_T */
5848 case MAT_C_INT32:
5849 matvar->data_type = MAT_T_INT32;
5850 matvar->data_size = sizeof(mat_int32_t);
5851 break;
5852 case MAT_C_UINT32:
5853 matvar->data_type = MAT_T_UINT32;
5854 matvar->data_size = sizeof(mat_uint32_t);
5855 break;
5856 case MAT_C_INT16:
5857 matvar->data_type = MAT_T_INT16;
5858 matvar->data_size = sizeof(mat_int16_t);
5859 break;
5860 case MAT_C_UINT16:
5861 matvar->data_type = MAT_T_UINT16;
5862 matvar->data_size = sizeof(mat_uint16_t);
5863 break;
5864 case MAT_C_INT8:
5865 matvar->data_type = MAT_T_INT8;
5866 matvar->data_size = sizeof(mat_int8_t);
5867 break;
5868 case MAT_C_UINT8:
5869 matvar->data_type = MAT_T_UINT8;
5870 matvar->data_size = sizeof(mat_uint8_t);
5871 break;
5872 }
5873
5874 return err;
5875 }
5876
5877 /** @if mat_devman
5878 * @brief Writes a matlab variable to a version 5 matlab file
5879 *
5880 * @ingroup mat_internal
5881 * @param mat MAT file pointer
5882 * @param matvar pointer to the mat variable
5883 * @param compress option to compress the variable
5884 * (only works for numeric types)
5885 * @retval 0 on success
5886 * @endif
5887 */
5888 int
5889 Mat_VarWrite5(mat_t *mat,matvar_t *matvar,int compress)
5890 {
5891 mat_uint32_t array_flags = 0x0;
5892 mat_int16_t fieldname_type = MAT_T_INT32,fieldname_data_size=4;
5893 mat_int8_t pad1 = 0;
5894 int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
5895 int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
5896 int nBytes, i, nmemb = 1,nzmax = 0;
5897 long start = 0, end = 0;
5898
5899 /* FIXME: SEEK_END is not Guaranteed by the C standard */
5900 fseek(mat->fp,0,SEEK_END); /* Always write at end of file */
5901
5902 #if !defined(HAVE_ZLIB)
5903 compress = MAT_COMPRESSION_NONE;
5904 #endif
5905
5906 if ( compress == MAT_COMPRESSION_NONE ) {
5907 fwrite(&matrix_type,4,1,mat->fp);
5908 fwrite(&pad4,4,1,mat->fp);
5909 start = ftell(mat->fp);
5910
5911 /* Array Flags */
5912
5913 array_flags = matvar->class_type & CLASS_TYPE_MASK;
5914 if ( matvar->isComplex )
5915 array_flags |= MAT_F_COMPLEX;
5916 if ( matvar->isGlobal )
5917 array_flags |= MAT_F_GLOBAL;
5918 if ( matvar->isLogical )
5919 array_flags |= MAT_F_LOGICAL;
5920 if ( matvar->class_type == MAT_C_SPARSE )
5921 nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
5922
5923 fwrite(&array_flags_type,4,1,mat->fp);
5924 fwrite(&array_flags_size,4,1,mat->fp);
5925 fwrite(&array_flags,4,1,mat->fp);
5926 fwrite(&nzmax,4,1,mat->fp);
5927 /* Rank and Dimension */
5928 nBytes = matvar->rank * 4;
5929 fwrite(&dims_array_type,4,1,mat->fp);
5930 fwrite(&nBytes,4,1,mat->fp);
5931 for ( i = 0; i < matvar->rank; i++ ) {
5932 mat_int32_t dim;
5933 dim = matvar->dims[i];
5934 nmemb *= dim;
5935 fwrite(&dim,4,1,mat->fp);
5936 }
5937 if ( matvar->rank % 2 != 0 )
5938 fwrite(&pad4,4,1,mat->fp);
5939 /* Name of variable */
5940 if ( strlen(matvar->name) <= 4 ) {
5941 mat_int32_t array_name_type = MAT_T_INT8;
5942 mat_int32_t array_name_len = strlen(matvar->name);
5943 mat_int8_t pad1 = 0;
5944 #if 0
5945 fwrite(&array_name_type,2,1,mat->fp);
5946 fwrite(&array_name_len,2,1,mat->fp);
5947 #else
5948 array_name_type = (array_name_len << 16) | array_name_type;
5949 fwrite(&array_name_type,4,1,mat->fp);
5950 #endif
5951 fwrite(matvar->name,1,array_name_len,mat->fp);
5952 for ( i = array_name_len; i < 4; i++ )
5953 fwrite(&pad1,1,1,mat->fp);
5954 } else {
5955 mat_int32_t array_name_type = MAT_T_INT8;
5956 mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
5957 mat_int8_t pad1 = 0;
5958
5959 fwrite(&array_name_type,4,1,mat->fp);
5960 fwrite(&array_name_len,4,1,mat->fp);
5961 fwrite(matvar->name,1,array_name_len,mat->fp);
5962 if ( array_name_len % 8 )
5963 for ( i = array_name_len % 8; i < 8; i++ )
5964 fwrite(&pad1,1,1,mat->fp);
5965 }
5966
5967 matvar->internal->datapos = ftell(mat->fp);
5968 switch ( matvar->class_type ) {
5969 case MAT_C_DOUBLE:
5970 case MAT_C_SINGLE:
5971 case MAT_C_INT64:
5972 case MAT_C_UINT64:
5973 case MAT_C_INT32:
5974 case MAT_C_UINT32:
5975 case MAT_C_INT16:
5976 case MAT_C_UINT16:
5977 case MAT_C_INT8:
5978 case MAT_C_UINT8:
5979 {
5980 if ( matvar->isComplex ) {
5981 mat_complex_split_t *complex_data = matvar->data;
5982
5983 if ( NULL == complex_data )
5984 complex_data = &null_complex_data;
5985
5986 nBytes = WriteData(mat,complex_data->Re,nmemb,
5987 matvar->data_type);
5988 if ( nBytes % 8 )
5989 for ( i = nBytes % 8; i < 8; i++ )
5990 fwrite(&pad1,1,1,mat->fp);
5991 nBytes = WriteData(mat,complex_data->Im,nmemb,
5992 matvar->data_type);
5993 if ( nBytes % 8 )
5994 for ( i = nBytes % 8; i < 8; i++ )
5995 fwrite(&pad1,1,1,mat->fp);
5996 } else {
5997 nBytes=WriteData(mat,matvar->data,nmemb,matvar->data_type);
5998 if ( nBytes % 8 )
5999 for ( i = nBytes % 8; i < 8; i++ )
6000 fwrite(&pad1,1,1,mat->fp);
6001 }
6002 break;
6003 }
6004 case MAT_C_CHAR:
6005 {
6006 WriteCharData(mat,matvar->data,nmemb,matvar->data_type);
6007 break;
6008 }
6009 case MAT_C_CELL:
6010 {
6011 int ncells;
6012 matvar_t **cells = (matvar_t **)matvar->data;
6013
6014 /* Check for an empty cell array */
6015 if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
6016 matvar->data == NULL )
6017 break;
6018 ncells = matvar->nbytes / matvar->data_size;
6019 for ( i = 0; i < ncells; i++ )
6020 WriteCellArrayField(mat,cells[i]);
6021 break;
6022 }
6023 case MAT_C_STRUCT:
6024 {
6025 char *padzero;
6026 int fieldname_size, nfields;
6027 size_t maxlen = 0;
6028 matvar_t **fields = (matvar_t **)matvar->data;
6029 mat_int32_t array_name_type = MAT_T_INT8;
6030 unsigned fieldname;
6031
6032 /* Check for a structure with no fields */
6033 if ( matvar->internal->num_fields < 1 ) {
6034 #if 0
6035 fwrite(&fieldname_type,2,1,mat->fp);
6036 fwrite(&fieldname_data_size,2,1,mat->fp);
6037 #else
6038 fieldname = (fieldname_data_size<<16) | fieldname_type;
6039 fwrite(&fieldname,4,1,mat->fp);
6040 #endif
6041 fieldname_size = 1;
6042 fwrite(&fieldname_size,4,1,mat->fp);
6043 fwrite(&array_name_type,4,1,mat->fp);
6044 nBytes = 0;
6045 fwrite(&nBytes,4,1,mat->fp);
6046 break;
6047 }
6048 nfields = matvar->internal->num_fields;
6049 for ( i = 0; i < nfields; i++ ) {
6050 size_t len = strlen(matvar->internal->fieldnames[i]);
6051 if ( len > maxlen )
6052 maxlen = len;
6053 }
6054 maxlen++;
6055 fieldname_size = maxlen;
6056 while ( nfields*fieldname_size % 8 != 0 )
6057 fieldname_size++;
6058 #if 0
6059 fwrite(&fieldname_type,2,1,mat->fp);
6060 fwrite(&fieldname_data_size,2,1,mat->fp);
6061 #else
6062 fieldname = (fieldname_data_size<<16) | fieldname_type;
6063 fwrite(&fieldname,4,1,mat->fp);
6064 #endif
6065 fwrite(&fieldname_size,4,1,mat->fp);
6066 fwrite(&array_name_type,4,1,mat->fp);
6067 nBytes = nfields*fieldname_size;
6068 fwrite(&nBytes,4,1,mat->fp);
6069 padzero = calloc(fieldname_size,1);
6070 for ( i = 0; i < nfields; i++ ) {
6071 size_t len = strlen(matvar->internal->fieldnames[i]);
6072 fwrite(matvar->internal->fieldnames[i],1,len,mat->fp);
6073 fwrite(padzero,1,fieldname_size-len,mat->fp);
6074 }
6075 free(padzero);
6076 for ( i = 0; i < nmemb*nfields; i++ )
6077 WriteStructField(mat,fields[i]);
6078 break;
6079 }
6080 case MAT_C_SPARSE:
6081 {
6082 mat_sparse_t *sparse = matvar->data;
6083
6084 nBytes = WriteData(mat,sparse->ir,sparse->nir,MAT_T_INT32);
6085 if ( nBytes % 8 )
6086 for ( i = nBytes % 8; i < 8; i++ )
6087 fwrite(&pad1,1,1,mat->fp);
6088 nBytes = WriteData(mat,sparse->jc,sparse->njc,MAT_T_INT32);
6089 if ( nBytes % 8 )
6090 for ( i = nBytes % 8; i < 8; i++ )
6091 fwrite(&pad1,1,1,mat->fp);
6092 if ( matvar->isComplex ) {
6093 mat_complex_split_t *complex_data = sparse->data;
6094 nBytes = WriteData(mat,complex_data->Re,sparse->ndata,
6095 matvar->data_type);
6096 if ( nBytes % 8 )
6097 for ( i = nBytes % 8; i < 8; i++ )
6098 fwrite(&pad1,1,1,mat->fp);
6099 nBytes = WriteData(mat,complex_data->Im,sparse->ndata,
6100 matvar->data_type);
6101 if ( nBytes % 8 )
6102 for ( i = nBytes % 8; i < 8; i++ )
6103 fwrite(&pad1,1,1,mat->fp);
6104 } else {
6105 nBytes = WriteData(mat,sparse->data,sparse->ndata,matvar->data_type);
6106 if ( nBytes % 8 )
6107 for ( i = nBytes % 8; i < 8; i++ )
6108 fwrite(&pad1,1,1,mat->fp);
6109 }
6110 }
6111 }
6112 #if defined(HAVE_ZLIB)
6113 } else if ( compress == MAT_COMPRESSION_ZLIB ) {
6114 mat_uint32_t comp_buf[512];
6115 mat_uint32_t uncomp_buf[512] = {0,};
6116 int buf_size = 512, err;
6117 size_t byteswritten = 0;
6118
6119 matvar->internal->z = calloc(1,sizeof(*matvar->internal->z));
6120 matvar->internal->z->zalloc = Z_NULL;
6121 matvar->internal->z->zfree = Z_NULL;
6122 err = deflateInit(matvar->internal->z,Z_DEFAULT_COMPRESSION);
6123
6124 matrix_type = MAT_T_COMPRESSED;
6125 fwrite(&matrix_type,4,1,mat->fp);
6126 fwrite(&pad4,4,1,mat->fp);
6127 start = ftell(mat->fp);
6128
6129 /* Array Flags */
6130 array_flags = matvar->class_type & CLASS_TYPE_MASK;
6131 if ( matvar->isComplex )
6132 array_flags |= MAT_F_COMPLEX;
6133 if ( matvar->isGlobal )
6134 array_flags |= MAT_F_GLOBAL;
6135 if ( matvar->isLogical )
6136 array_flags |= MAT_F_LOGICAL;
6137 if ( matvar->class_type == MAT_C_SPARSE )
6138 nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
6139
6140 uncomp_buf[0] = MAT_T_MATRIX;
6141 uncomp_buf[1] = (int)GetMatrixMaxBufSize(matvar);
6142 matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
6143 matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
6144 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6145 matvar->internal->z->avail_in = 8;
6146 err = deflate(matvar->internal->z,Z_NO_FLUSH);
6147 byteswritten += fwrite(comp_buf,1,
6148 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6149 uncomp_buf[0] = array_flags_type;
6150 uncomp_buf[1] = array_flags_size;
6151 uncomp_buf[2] = array_flags;
6152 uncomp_buf[3] = nzmax;
6153 /* Rank and Dimension */
6154 nBytes = matvar->rank * 4;
6155 uncomp_buf[4] = dims_array_type;
6156 uncomp_buf[5] = nBytes;
6157 for ( i = 0; i < matvar->rank; i++ ) {
6158 mat_int32_t dim;
6159 dim = matvar->dims[i];
6160 nmemb *= dim;
6161 uncomp_buf[6+i] = dim;
6162 }
6163 if ( matvar->rank % 2 != 0 ) {
6164 uncomp_buf[6+i] = pad4;
6165 i++;
6166 }
6167
6168 matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
6169 matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
6170 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6171 matvar->internal->z->avail_in = (6+i)*sizeof(*uncomp_buf);
6172 err = deflate(matvar->internal->z,Z_NO_FLUSH);
6173 byteswritten += fwrite(comp_buf,1,
6174 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6175 /* Name of variable */
6176 if ( strlen(matvar->name) <= 4 ) {
6177 mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
6178 mat_int16_t array_name_type = MAT_T_INT8;
6179
6180 memset(uncomp_buf,0,8);
6181 uncomp_buf[0] = (array_name_len << 16) | array_name_type;
6182 memcpy(uncomp_buf+1,matvar->name,array_name_len);
6183 if ( array_name_len % 4 )
6184 array_name_len += 4-(array_name_len % 4);
6185
6186 matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
6187 matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
6188 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6189 matvar->internal->z->avail_in = 8;
6190 err = deflate(matvar->internal->z,Z_NO_FLUSH);
6191 byteswritten += fwrite(comp_buf,1,
6192 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6193 } else {
6194 mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
6195 mat_int32_t array_name_type = MAT_T_INT8;
6196
6197 memset(uncomp_buf,0,buf_size*sizeof(*uncomp_buf));
6198 uncomp_buf[0] = array_name_type;
6199 uncomp_buf[1] = array_name_len;
6200 memcpy(uncomp_buf+2,matvar->name,array_name_len);
6201 if ( array_name_len % 8 )
6202 array_name_len += 8-(array_name_len % 8);
6203 matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
6204 matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
6205 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6206 matvar->internal->z->avail_in = 8+array_name_len;
6207 err = deflate(matvar->internal->z,Z_NO_FLUSH);
6208 byteswritten += fwrite(comp_buf,1,
6209 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6210 }
6211 matvar->internal->datapos = ftell(mat->fp);
6212 switch ( matvar->class_type ) {
6213 case MAT_C_DOUBLE:
6214 case MAT_C_SINGLE:
6215 case MAT_C_INT64:
6216 case MAT_C_UINT64:
6217 case MAT_C_INT32:
6218 case MAT_C_UINT32:
6219 case MAT_C_INT16:
6220 case MAT_C_UINT16:
6221 case MAT_C_INT8:
6222 case MAT_C_UINT8:
6223 {
6224 /* WriteCompressedData makes sure uncomressed data is aligned
6225 * on an 8-byte boundary */
6226 if ( matvar->isComplex ) {
6227 mat_complex_split_t *complex_data = matvar->data;
6228
6229 if ( NULL == matvar->data )
6230 complex_data = &null_complex_data;
6231
6232 byteswritten += WriteCompressedData(mat,matvar->internal->z,
6233 complex_data->Re,nmemb,matvar->data_type);
6234 byteswritten += WriteCompressedData(mat,matvar->internal->z,
6235 complex_data->Im,nmemb,matvar->data_type);
6236 } else {
6237 byteswritten += WriteCompressedData(mat,matvar->internal->z,
6238 matvar->data,nmemb,matvar->data_type);
6239 }
6240 break;
6241 }
6242 case MAT_C_CHAR:
6243 {
6244 byteswritten += WriteCompressedCharData(mat,matvar->internal->z,
6245 matvar->data,nmemb,matvar->data_type);
6246 break;
6247 }
6248 case MAT_C_CELL:
6249 {
6250 int ncells;
6251 matvar_t **cells = (matvar_t **)matvar->data;
6252
6253 /* Check for an empty cell array */
6254 if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
6255 matvar->data == NULL )
6256 break;
6257 ncells = matvar->nbytes / matvar->data_size;
6258 for ( i = 0; i < ncells; i++ )
6259 WriteCompressedCellArrayField(mat,cells[i],matvar->internal->z);
6260 break;
6261 }
6262 case MAT_C_STRUCT:
6263 {
6264 unsigned char *padzero;
6265 int fieldname_size, nfields;
6266 size_t maxlen = 0;
6267 mat_int32_t array_name_type = MAT_T_INT8;
6268 matvar_t **fields = (matvar_t **)matvar->data;
6269
6270 /* Check for a structure with no fields */
6271 if ( matvar->internal->num_fields < 1 ) {
6272 fieldname_size = 1;
6273 uncomp_buf[0] = (fieldname_data_size << 16) |
6274 fieldname_type;
6275 uncomp_buf[1] = 1;
6276 uncomp_buf[2] = array_name_type;
6277 uncomp_buf[3] = 0;
6278 matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
6279 matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
6280 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6281 matvar->internal->z->avail_in = 16;
6282 err = deflate(matvar->internal->z,Z_NO_FLUSH);
6283 byteswritten += fwrite(comp_buf,1,buf_size*
6284 sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6285 break;
6286 }
6287 nfields = matvar->internal->num_fields;
6288 for ( i = 0; i < nfields; i++ ) {
6289 size_t len = strlen(matvar->internal->fieldnames[i]);
6290 if ( len > maxlen )
6291 maxlen = len;
6292 }
6293 maxlen++;
6294 fieldname_size = maxlen;
6295 while ( nfields*fieldname_size % 8 != 0 )
6296 fieldname_size++;
6297 uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type;
6298 uncomp_buf[1] = fieldname_size;
6299 uncomp_buf[2] = array_name_type;
6300 uncomp_buf[3] = nfields*fieldname_size;
6301
6302 padzero = calloc(fieldname_size,1);
6303 matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
6304 matvar->internal->z->next_in = ZLIB_BYTE_PTR(uncomp_buf);
6305 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6306 matvar->internal->z->avail_in = 16;
6307 err = deflate(matvar->internal->z,Z_NO_FLUSH);
6308 byteswritten += fwrite(comp_buf,1,
6309 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6310 for ( i = 0; i < nfields; i++ ) {
6311 size_t len = strlen(matvar->internal->fieldnames[i]);
6312 memset(padzero,'\0',fieldname_size);
6313 memcpy(padzero,matvar->internal->fieldnames[i],len);
6314 matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
6315 matvar->internal->z->next_in = ZLIB_BYTE_PTR(padzero);
6316 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6317 matvar->internal->z->avail_in = fieldname_size;
6318 err = deflate(matvar->internal->z,Z_NO_FLUSH);
6319 byteswritten += fwrite(comp_buf,1,
6320 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,
6321 mat->fp);
6322 }
6323 free(padzero);
6324 for ( i = 0; i < nmemb*nfields; i++ )
6325 byteswritten +=
6326 WriteCompressedStructField(mat,fields[i],matvar->internal->z);
6327 break;
6328 }
6329 case MAT_C_SPARSE:
6330 {
6331 mat_sparse_t *sparse = matvar->data;
6332
6333 byteswritten += WriteCompressedData(mat,matvar->internal->z,sparse->ir,
6334 sparse->nir,MAT_T_INT32);
6335 byteswritten += WriteCompressedData(mat,matvar->internal->z,sparse->jc,
6336 sparse->njc,MAT_T_INT32);
6337 if ( matvar->isComplex ) {
6338 mat_complex_split_t *complex_data = sparse->data;
6339 byteswritten += WriteCompressedData(mat,matvar->internal->z,
6340 complex_data->Re,sparse->ndata,matvar->data_type);
6341 byteswritten += WriteCompressedData(mat,matvar->internal->z,
6342 complex_data->Im,sparse->ndata,matvar->data_type);
6343 } else {
6344 byteswritten += WriteCompressedData(mat,matvar->internal->z,
6345 sparse->data,sparse->ndata,matvar->data_type);
6346 }
6347 break;
6348 }
6349 }
6350 matvar->internal->z->avail_in = 0;
6351 matvar->internal->z->next_in = NULL;
6352 matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
6353 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6354
6355 err = deflate(matvar->internal->z,Z_FINISH);
6356 byteswritten += fwrite(comp_buf,1,
6357 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6358 while ( err != Z_STREAM_END && !matvar->internal->z->avail_out ) {
6359 matvar->internal->z->next_out = ZLIB_BYTE_PTR(comp_buf);
6360 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6361
6362 err = deflate(matvar->internal->z,Z_FINISH);
6363 byteswritten += fwrite(comp_buf,1,
6364 buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6365 }
6366 /* End the compression and set to NULL so Mat_VarFree doesn't try
6367 * to free matvar->internal->z with inflateEnd
6368 */
6369 #if 0
6370 if ( byteswritten % 8 )
6371 for ( i = 0; i < 8-(byteswritten % 8); i++ )
6372 fwrite(&pad1,1,1,mat->fp);
6373 #endif
6374 err = deflateEnd(matvar->internal->z);
6375 free(matvar->internal->z);
6376 matvar->internal->z = NULL;
6377 #endif
6378 }
6379 end = ftell(mat->fp);
6380 nBytes = (int)(end-start);
6381 fseek(mat->fp,(long)-(nBytes+4),SEEK_CUR);
6382 fwrite(&nBytes,4,1,mat->fp);
6383 fseek(mat->fp,end,SEEK_SET);
6384
6385 return 0;
6386 }
6387
6388 /** @if mat_devman
6389 * @brief Writes the variable information and empty data
6390 *
6391 * @ingroup mat_internal
6392 * @param mat MAT file pointer
6393 * @param matvar pointer to the mat variable
6394 * @endif
6395 */
6396 void
6397 WriteInfo5(mat_t *mat, matvar_t *matvar)
6398 {
6399 mat_uint32_t array_flags = 0x0;
6400 mat_int16_t fieldname_type = MAT_T_INT32,fieldname_data_size=4;
6401 mat_int8_t pad1 = 0;
6402 int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32;
6403 int array_flags_size = 8, pad4 = 0, matrix_type = MAT_T_MATRIX;
6404 int nBytes, i, nmemb = 1,nzmax;
6405 long start = 0, end = 0;
6406
6407 /* FIXME: SEEK_END is not Guaranteed by the C standard */
6408 fseek(mat->fp,0,SEEK_END); /* Always write at end of file */
6409
6410
6411 if ( matvar->compression == MAT_COMPRESSION_NONE ) {
6412 fwrite(&matrix_type,4,1,mat->fp);
6413 fwrite(&pad4,4,1,mat->fp);
6414 start = ftell(mat->fp);
6415
6416 /* Array Flags */
6417
6418 array_flags = matvar->class_type & CLASS_TYPE_MASK;
6419 if ( matvar->isComplex )
6420 array_flags |= MAT_F_COMPLEX;
6421 if ( matvar->isGlobal )
6422 array_flags |= MAT_F_GLOBAL;
6423 if ( matvar->isLogical )
6424 array_flags |= MAT_F_LOGICAL;
6425 if ( matvar->class_type == MAT_C_SPARSE )
6426 nzmax = ((mat_sparse_t *)matvar->data)->nzmax;
6427
6428 fwrite(&array_flags_type,4,1,mat->fp);
6429 fwrite(&array_flags_size,4,1,mat->fp);
6430 fwrite(&array_flags,4,1,mat->fp);
6431 fwrite(&nzmax,4,1,mat->fp);
6432 /* Rank and Dimension */
6433 nBytes = matvar->rank * 4;
6434 fwrite(&dims_array_type,4,1,mat->fp);
6435 fwrite(&nBytes,4,1,mat->fp);
6436 for ( i = 0; i < matvar->rank; i++ ) {
6437 mat_int32_t dim;
6438 dim = matvar->dims[i];
6439 nmemb *= dim;
6440 fwrite(&dim,4,1,mat->fp);
6441 }
6442 if ( matvar->rank % 2 != 0 )
6443 fwrite(&pad4,4,1,mat->fp);
6444 /* Name of variable */
6445 if ( strlen(matvar->name) <= 4 ) {
6446 mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
6447 mat_int8_t pad1 = 0;
6448 mat_int16_t array_name_type = MAT_T_INT8;
6449 fwrite(&array_name_type,2,1,mat->fp);
6450 fwrite(&array_name_len,2,1,mat->fp);
6451 fwrite(matvar->name,1,array_name_len,mat->fp);
6452 for ( i = array_name_len; i < 4; i++ )
6453 fwrite(&pad1,1,1,mat->fp);
6454 } else {
6455 mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
6456 mat_int8_t pad1 = 0;
6457 mat_int32_t array_name_type = MAT_T_INT8;
6458
6459 fwrite(&array_name_type,4,1,mat->fp);
6460 fwrite(&array_name_len,4,1,mat->fp);
6461 fwrite(matvar->name,1,array_name_len,mat->fp);
6462 if ( array_name_len % 8 )
6463 for ( i = array_name_len % 8; i < 8; i++ )
6464 fwrite(&pad1,1,1,mat->fp);
6465 }
6466
6467 matvar->internal->datapos = ftell(mat->fp);
6468 switch ( matvar->class_type ) {
6469 case MAT_C_DOUBLE:
6470 case MAT_C_SINGLE:
6471 case MAT_C_INT64:
6472 case MAT_C_UINT64:
6473 case MAT_C_INT32:
6474 case MAT_C_UINT32:
6475 case MAT_C_INT16:
6476 case MAT_C_UINT16:
6477 case MAT_C_INT8:
6478 case MAT_C_UINT8:
6479 nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
6480 if ( nBytes % 8 )
6481 for ( i = nBytes % 8; i < 8; i++ )
6482 fwrite(&pad1,1,1,mat->fp);
6483 if ( matvar->isComplex ) {
6484 nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
6485 if ( nBytes % 8 )
6486 for ( i = nBytes % 8; i < 8; i++ )
6487 fwrite(&pad1,1,1,mat->fp);
6488 }
6489 break;
6490 case MAT_C_CHAR:
6491 {
6492 WriteEmptyCharData(mat,nmemb,matvar->data_type);
6493 break;
6494 }
6495 case MAT_C_CELL:
6496 {
6497 int ncells;
6498 matvar_t **cells = (matvar_t **)matvar->data;
6499
6500 /* Check for an empty cell array */
6501 if ( matvar->nbytes == 0 || matvar->data_size == 0 ||
6502 matvar->data == NULL )
6503 break;
6504 ncells = matvar->nbytes / matvar->data_size;
6505
6506 for ( i = 0; i < ncells; i++ )
6507 WriteCellArrayFieldInfo(mat,cells[i]);
6508 break;
6509 }
6510 case MAT_C_STRUCT:
6511 {
6512 char *padzero;
6513 int maxlen = 0, fieldname_size;
6514 int nfields = matvar->internal->num_fields;
6515 matvar_t **fields = (matvar_t **)matvar->data;
6516 mat_int32_t array_name_type = MAT_T_INT8;
6517 unsigned fieldname;
6518
6519 for ( i = 0; i < nfields; i++ ) {
6520 size_t len = strlen(matvar->internal->fieldnames[i]);
6521 if ( len > maxlen )
6522 maxlen = len;
6523 }
6524 maxlen++;
6525 fieldname_size = maxlen;
6526 while ( nfields*fieldname_size % 8 != 0 )
6527 fieldname_size++;
6528 #if 0
6529 fwrite(&fieldname_type,2,1,mat->fp);
6530 fwrite(&fieldname_data_size,2,1,mat->fp);
6531 #else
6532 fieldname = (fieldname_data_size<<16) | fieldname_type;
6533 fwrite(&fieldname,4,1,mat->fp);
6534 #endif
6535 fwrite(&fieldname_size,4,1,mat->fp);
6536 fwrite(&array_name_type,4,1,mat->fp);
6537 nBytes = nfields*fieldname_size;
6538 fwrite(&nBytes,4,1,mat->fp);
6539 padzero = calloc(fieldname_size,1);
6540 for ( i = 0; i < nfields; i++ ) {
6541 size_t len = strlen(matvar->internal->fieldnames[i]);
6542 fwrite(matvar->internal->fieldnames[i],1,len,mat->fp);
6543 fwrite(padzero,1,fieldname_size-len,mat->fp);
6544 }
6545 free(padzero);
6546 for ( i = 0; i < nfields; i++ )
6547 WriteInfo5(mat,fields[i]);
6548 break;
6549 }
6550 }
6551 /* Does not work.
6552 * Can write empty data, but how to go back and add the real data?
6553 */
6554 #if 0
6555 } else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
6556 #if defined(HAVE_ZLIB)
6557 mat_uint32_t comp_buf[512];
6558 mat_uint32_t uncomp_buf[512] = {0,};
6559 int buf_size = 512, err;
6560 size_t byteswritten = 0;
6561
6562 matvar->internal->z = malloc(sizeof(*matvar->internal->z));
6563 matvar->internal->z->zalloc = Z_NULL;
6564 matvar->internal->z->zfree = Z_NULL;
6565 err = deflateInit(matvar->internal->z,Z_DEFAULT_COMPRESSION);
6566
6567 matrix_type = MAT_T_COMPRESSED;
6568 fwrite(&matrix_type,4,1,mat->fp);
6569 fwrite(&pad4,4,1,mat->fp);
6570 start = ftell(mat->fp);
6571
6572 /* Array Flags */
6573
6574 array_flags = matvar->class_type & MAT_F_CLASS_T;
6575 if ( matvar->isComplex )
6576 array_flags |= MAT_F_COMPLEX;
6577 if ( matvar->isGlobal )
6578 array_flags |= MAT_F_GLOBAL;
6579 if ( matvar->isLogical )
6580 array_flags |= MAT_F_LOGICAL;
6581
6582 uncomp_buf[0] = MAT_T_MATRIX;
6583 uncomp_buf[1] = 448;
6584 matvar->internal->z->next_out = comp_buf;
6585 matvar->internal->z->next_in = uncomp_buf;
6586 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6587 matvar->internal->z->avail_in = 8;
6588 err = deflate(matvar->internal->z,Z_SYNC_FLUSH);
6589 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6590 uncomp_buf[0] = array_flags_type;
6591 uncomp_buf[1] = array_flags_size;
6592 uncomp_buf[2] = array_flags;
6593 uncomp_buf[3] = 0;
6594 /* Rank and Dimension */
6595 nBytes = matvar->rank * 4;
6596 uncomp_buf[4] = dims_array_type;
6597 uncomp_buf[5] = nBytes;
6598 for ( i = 0; i < matvar->rank; i++ ) {
6599 mat_int32_t dim;
6600 dim = matvar->dims[i];
6601 nmemb *= dim;
6602 uncomp_buf[6+i] = dim;
6603 }
6604 if ( matvar->rank % 2 != 0 )
6605 uncomp_buf[6+i] = pad4;
6606
6607 matvar->internal->z->next_out = comp_buf;
6608 matvar->internal->z->next_in = uncomp_buf;
6609 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6610 matvar->internal->z->avail_in = (6+i)*sizeof(*uncomp_buf);
6611 err = deflate(matvar->internal->z,Z_NO_FLUSH);
6612 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6613 /* Name of variable */
6614 if ( strlen(matvar->name) <= 4 ) {
6615 #if 0
6616 mat_int16_t array_name_len = (mat_int16_t)strlen(matvar->name);
6617 mat_int8_t pad1 = 0;
6618
6619 uncomp_buf[0] = (array_name_type << 16) | array_name_len;
6620 memcpy(uncomp_buf+1,matvar->name,array_name_len);
6621
6622 matvar->internal->z->next_out = comp_buf;
6623 matvar->internal->z->next_in = uncomp_buf;
6624 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6625 matvar->internal->z->avail_in = 8;
6626 err = deflate(matvar->internal->z,Z_NO_FLUSH);
6627 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6628 } else {
6629 #endif
6630 mat_int32_t array_name_len = (mat_int32_t)strlen(matvar->name);
6631
6632 memset(uncomp_buf,0,buf_size*sizeof(*uncomp_buf));
6633 uncomp_buf[0] = array_name_type;
6634 uncomp_buf[1] = array_name_len;
6635 memcpy(uncomp_buf+2,matvar->name,array_name_len);
6636 if ( array_name_len % 8 )
6637 array_name_len += array_name_len % 8;
6638 matvar->internal->z->next_out = comp_buf;
6639 matvar->internal->z->next_in = uncomp_buf;
6640 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6641 matvar->internal->z->avail_in = 8+array_name_len;
6642 err = deflate(matvar->internal->z,Z_FULL_FLUSH);
6643 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6644 }
6645 matvar->internal->datapos = ftell(mat->fp);
6646 deflateCopy(&z_save,matvar->internal->z);
6647 switch ( matvar->class_type ) {
6648 case MAT_C_DOUBLE:
6649 case MAT_C_SINGLE:
6650 case MAT_C_INT32:
6651 case MAT_C_UINT32:
6652 case MAT_C_INT16:
6653 case MAT_C_UINT16:
6654 case MAT_C_INT8:
6655 case MAT_C_UINT8:
6656 byteswritten += WriteCompressedEmptyData(mat,matvar->internal->z,nmemb,matvar->data_type);
6657 #if 0
6658 if ( nBytes % 8 )
6659 for ( i = nBytes % 8; i < 8; i++ )
6660 fwrite(&pad1,1,1,mat->fp);
6661 if ( matvar->isComplex ) {
6662 nBytes = WriteEmptyData(mat,nmemb,matvar->data_type);
6663 if ( nBytes % 8 )
6664 for ( i = nBytes % 8; i < 8; i++ )
6665 fwrite(&pad1,1,1,mat->fp);
6666 }
6667 #endif
6668 break;
6669 }
6670 matvar->internal->z->next_out = comp_buf;
6671 matvar->internal->z->next_in = NULL;
6672 matvar->internal->z->avail_out = buf_size*sizeof(*comp_buf);
6673 matvar->internal->z->avail_in = 0;
6674
6675 err = deflate(matvar->internal->z,Z_FINISH);
6676 byteswritten += fwrite(comp_buf,1,buf_size*sizeof(*comp_buf)-matvar->internal->z->avail_out,mat->fp);
6677 if ( byteswritten % 8 )
6678 for ( i = byteswritten % 8; i < 8; i++ )
6679 fwrite(&pad1,1,1,mat->fp);
6680 fprintf(stderr,"deflate Z_FINISH: err = %d,byteswritten = %u\n",err,byteswritten);
6681
6682 err = deflateEnd(matvar->internal->z);
6683 fprintf(stderr,"deflateEnd: err = %d\n",err);
6684 #if 1
6685 err = deflateEnd(matvar->internal->z);
6686 free(matvar->internal->z);
6687 matvar->internal->z = NULL;
6688 #else
6689 memcpy(matvar->internal->z,&z_save,sizeof(*matvar->internal->z));
6690 #endif
6691 #endif
6692 #endif
6693 }
6694 end = ftell(mat->fp);
6695 nBytes = (int)(end-start);
6696 fseek(mat->fp,(long)-(nBytes+4),SEEK_CUR);
6697 fwrite(&nBytes,4,1,mat->fp);
6698 fseek(mat->fp,end,SEEK_SET);
6699 }
6700
6701 /** @if mat_devman
6702 * @brief Reads the header information for the next MAT variable
6703 *
6704 * @ingroup mat_internal
6705 * @param mat MAT file pointer
6706 * @retuen pointer to the MAT variable or NULL
6707 * @endif
6708 */
6709 matvar_t *
6710 Mat_VarReadNextInfo5( mat_t *mat )
6711 {
6712 int err, data_type, nBytes, i;
6713 long fpos;
6714 matvar_t *matvar = NULL;
6715 mat_uint32_t array_flags;
6716 long bytesread = 0;
6717
6718 if( mat == NULL )
6719 return NULL;
6720
6721 fpos = ftell(mat->fp);
6722 err = fread(&data_type,4,1,mat->fp);
6723 if ( !err )
6724 return NULL;
6725 err = fread(&nBytes,4,1,mat->fp);
6726 if ( mat->byteswap ) {
6727 Mat_int32Swap(&data_type);
6728 Mat_int32Swap(&nBytes);
6729 }
6730 switch ( data_type ) {
6731 #if defined(HAVE_ZLIB)
6732 case MAT_T_COMPRESSED:
6733 {
6734 mat_uint32_t uncomp_buf[16] = {0,};
6735 int nbytes;
6736
6737 matvar = Mat_VarCalloc();
6738 matvar->name = NULL;
6739 matvar->data = NULL;
6740 matvar->dims = NULL;
6741 matvar->nbytes = 0;
6742 matvar->data_type = MAT_T_UNKNOWN;
6743 matvar->class_type = MAT_C_EMPTY;
6744 matvar->data_size = 0;
6745 matvar->mem_conserve = 0;
6746 matvar->compression = 1;
6747
6748 matvar->internal->fp = mat;
6749 matvar->internal->fpos = fpos;
6750 matvar->internal->z = calloc(1,sizeof(z_stream));
6751 matvar->internal->z->zalloc = NULL;
6752 matvar->internal->z->zfree = NULL;
6753 matvar->internal->z->opaque = NULL;
6754 matvar->internal->z->next_in = NULL;
6755 matvar->internal->z->next_out = NULL;
6756 matvar->internal->z->avail_in = 0;
6757 matvar->internal->z->avail_out = 0;
6758 err = inflateInit(matvar->internal->z);
6759 if ( err != Z_OK ) {
6760 Mat_Critical("inflateInit2 returned %d",err);
6761 Mat_VarFree(matvar);
6762 break;
6763 }
6764
6765 /* Read Variable tag */
6766 bytesread += InflateVarTag(mat,matvar,uncomp_buf);
6767 if ( mat->byteswap ) {
6768 (void)Mat_uint32Swap(uncomp_buf);
6769 (void)Mat_uint32Swap(uncomp_buf+1);
6770 }
6771 nbytes = uncomp_buf[1];
6772 if ( uncomp_buf[0] != MAT_T_MATRIX ) {
6773 Mat_Critical("Uncompressed type not MAT_T_MATRIX");
6774 fseek(mat->fp,nBytes-bytesread,SEEK_CUR);
6775 Mat_VarFree(matvar);
6776 matvar = NULL;
6777 break;
6778 }
6779 /* Inflate Array Flags */
6780 bytesread += InflateArrayFlags(mat,matvar,uncomp_buf);
6781 if ( mat->byteswap ) {
6782 (void)Mat_uint32Swap(uncomp_buf);
6783 (void)Mat_uint32Swap(uncomp_buf+2);
6784 (void)Mat_uint32Swap(uncomp_buf+3);
6785 }
6786 /* Array Flags */
6787 if ( uncomp_buf[0] == MAT_T_UINT32 ) {
6788 array_flags = uncomp_buf[2];
6789 matvar->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
6790 matvar->isComplex = (array_flags & MAT_F_COMPLEX);
6791 matvar->isGlobal = (array_flags & MAT_F_GLOBAL);
6792 matvar->isLogical = (array_flags & MAT_F_LOGICAL);
6793 if ( matvar->class_type == MAT_C_SPARSE ) {
6794 /* Need to find a more appropriate place to store nzmax */
6795 matvar->nbytes = uncomp_buf[3];
6796 }
6797 }
6798 /* Inflate Dimensions */
6799 bytesread += InflateDimensions(mat,matvar,uncomp_buf);
6800 if ( mat->byteswap ) {
6801 (void)Mat_uint32Swap(uncomp_buf);
6802 (void)Mat_uint32Swap(uncomp_buf+1);
6803 }
6804 /* Rank and Dimension */
6805 if ( uncomp_buf[0] == MAT_T_INT32 ) {
6806 nbytes = uncomp_buf[1];
6807 matvar->rank = nbytes / 4;
6808 matvar->dims = malloc(matvar->rank*sizeof(*matvar->dims));
6809 if ( mat->byteswap ) {
6810 for ( i = 0; i < matvar->rank; i++ )
6811 matvar->dims[i] = Mat_uint32Swap(&(uncomp_buf[2+i]));
6812 } else {
6813 for ( i = 0; i < matvar->rank; i++ )
6814 matvar->dims[i] = uncomp_buf[2+i];
6815 }
6816 }
6817 /* Inflate variable name tag */
6818 bytesread += InflateVarNameTag(mat,matvar,uncomp_buf);
6819 if ( mat->byteswap )
6820 (void)Mat_uint32Swap(uncomp_buf);
6821 /* Name of variable */
6822 if ( uncomp_buf[0] == MAT_T_INT8 ) { /* Name not in tag */
6823 int len;
6824 if ( mat->byteswap )
6825 len = Mat_uint32Swap(uncomp_buf+1);
6826 else
6827 len = uncomp_buf[1];
6828
6829 if ( len % 8 == 0 )
6830 i = len;
6831 else
6832 i = len+(8-(len % 8));
6833 matvar->name = malloc(i+1);
6834 /* Inflate variable name */
6835 bytesread += InflateVarName(mat,matvar,matvar->name,i);
6836 matvar->name[len] = '\0';
6837 } else if ( ((uncomp_buf[0] & 0x0000ffff) == MAT_T_INT8) &&
6838 ((uncomp_buf[0] & 0xffff0000) != 0x00) ) {
6839 /* Name packed in tag */
6840 int len;
6841 len = (uncomp_buf[0] & 0xffff0000) >> 16;
6842 matvar->name = malloc(len+1);
6843 memcpy(matvar->name,uncomp_buf+1,len);
6844 matvar->name[len] = '\0';
6845 }
6846 if ( matvar->class_type == MAT_C_STRUCT )
6847 ReadNextStructField(mat,matvar);
6848 else if ( matvar->class_type == MAT_C_CELL )
6849 ReadNextCell(mat,matvar);
6850 fseek(mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
6851 matvar->internal->datapos = ftell(mat->fp);
6852 fseek(mat->fp,nBytes+8+fpos,SEEK_SET);
6853 break;
6854 }
6855 #endif
6856 case MAT_T_MATRIX:
6857 {
6858 int nbytes;
6859 mat_uint32_t buf[32];
6860 size_t bytesread = 0;
6861
6862 matvar = Mat_VarCalloc();
6863 matvar->internal->fpos = fpos;
6864 matvar->internal->fp = mat;
6865
6866 /* Read Array Flags and The Dimensions Tag */
6867 bytesread += fread(buf,4,6,mat->fp);
6868 if ( mat->byteswap ) {
6869 (void)Mat_uint32Swap(buf);
6870 (void)Mat_uint32Swap(buf+1);
6871 (void)Mat_uint32Swap(buf+2);
6872 (void)Mat_uint32Swap(buf+3);
6873 (void)Mat_uint32Swap(buf+4);
6874 (void)Mat_uint32Swap(buf+5);
6875 }
6876 /* Array Flags */
6877 if ( buf[0] == MAT_T_UINT32 ) {
6878 array_flags = buf[2];
6879 matvar->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags);
6880 matvar->isComplex = (array_flags & MAT_F_COMPLEX);
6881 matvar->isGlobal = (array_flags & MAT_F_GLOBAL);
6882 matvar->isLogical = (array_flags & MAT_F_LOGICAL);
6883 if ( matvar->class_type == MAT_C_SPARSE ) {
6884 /* Need to find a more appropriate place to store nzmax */
6885 matvar->nbytes = buf[3];
6886 }
6887 }
6888 /* Rank and Dimension */
6889 if ( buf[4] == MAT_T_INT32 ) {
6890 nbytes = buf[5];
6891
6892 matvar->rank = nbytes / 4;
6893 matvar->dims = malloc(matvar->rank*sizeof(*matvar->dims));
6894
6895 /* Assumes rank <= 16 */
6896 if ( matvar->rank % 2 != 0 )
6897 bytesread+=fread(buf,4,matvar->rank+1,mat->fp);
6898 else
6899 bytesread+=fread(buf,4,matvar->rank,mat->fp);
6900
6901 if ( mat->byteswap ) {
6902 for ( i = 0; i < matvar->rank; i++ )
6903 matvar->dims[i] = Mat_uint32Swap(buf+i);
6904 } else {
6905 for ( i = 0; i < matvar->rank; i++ )
6906 matvar->dims[i] = buf[i];
6907 }
6908 }
6909 /* Variable Name Tag */
6910 bytesread+=fread(buf,4,2,mat->fp);
6911 if ( mat->byteswap )
6912 (void)Mat_uint32Swap(buf);
6913 /* Name of variable */
6914 if ( buf[0] == MAT_T_INT8 ) { /* Name not in tag */
6915 int len;
6916
6917 if ( mat->byteswap )
6918 len = Mat_uint32Swap(buf+1);
6919 else
6920 len = buf[1];
6921 if ( len % 8 == 0 )
6922 i = len;
6923 else
6924 i = len+(8-(len % 8));
6925 bytesread+=fread(buf,1,i,mat->fp);
6926
6927 matvar->name = malloc(len+1);
6928 memcpy(matvar->name,buf,len);
6929 matvar->name[len] = '\0';
6930 } else if ( ((buf[0] & 0x0000ffff) == MAT_T_INT8) &&
6931 ((buf[0] & 0xffff0000) != 0x00) ) {
6932 /* Name packed in the tag */
6933 int len;
6934
6935 len = (buf[0] & 0xffff0000) >> 16;
6936 matvar->name = malloc(len+1);
6937 memcpy(matvar->name,buf+1,len);
6938 matvar->name[len] = '\0';
6939 }
6940 if ( matvar->class_type == MAT_C_STRUCT )
6941 (void)ReadNextStructField(mat,matvar);
6942 else if ( matvar->class_type == MAT_C_CELL )
6943 (void)ReadNextCell(mat,matvar);
6944 else if ( matvar->class_type == MAT_C_FUNCTION )
6945 (void)ReadNextFunctionHandle(mat,matvar);
6946 matvar->internal->datapos = ftell(mat->fp);
6947 fseek(mat->fp,nBytes+8+fpos,SEEK_SET);
6948 break;
6949 }
6950 default:
6951 Mat_Message("%d is not valid (MAT_T_MATRIX or MAT_T_COMPRESSED", data_type);
6952 return NULL;
6953 }
6954
6955 return matvar;
6956 }