-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
mat5.c
5468 lines (5171 loc) · 212 KB
/
mat5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file mat5.c
* Matlab MAT version 5 file functions
* @ingroup MAT
*/
/*
* Copyright (c) 2005-2021, Christopher C. Hulbert
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* FIXME: Implement Unicode support */
#include "matio_private.h"
#include "mat5.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#if defined(_MSC_VER) || defined(__MINGW32__)
#define strdup _strdup
#endif
/** Get type from tag */
#define TYPE_FROM_TAG(a) \
(((a)&0x000000ff) <= MAT_T_FUNCTION) ? (enum matio_types)((a)&0x000000ff) : MAT_T_UNKNOWN
/** Get class from array flag */
#define CLASS_FROM_ARRAY_FLAGS(a) \
(((a)&0x000000ff) <= MAT_C_OPAQUE) ? ((enum matio_classes)((a)&0x000000ff)) : MAT_C_EMPTY
/** Class type mask */
#define CLASS_TYPE_MASK 0x000000ff
static mat_complex_split_t null_complex_data = {NULL, NULL};
/*===========================================================================
* Private functions
*===========================================================================
*/
static int GetTypeBufSize(matvar_t *matvar, size_t *size);
static int GetStructFieldBufSize(matvar_t *matvar, size_t *size);
static int GetCellArrayFieldBufSize(matvar_t *matvar, size_t *size);
static void SetFieldNames(matvar_t *matvar, char *buf, size_t nfields,
mat_uint32_t fieldname_length);
static size_t ReadSparse(mat_t *mat, matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v);
#if HAVE_ZLIB
static int GetMatrixMaxBufSize(matvar_t *matvar, size_t *size);
#endif
static int GetEmptyMatrixMaxBufSize(const char *name, int rank, size_t *size);
static size_t WriteCharData(mat_t *mat, void *data, size_t N, enum matio_types data_type);
static size_t ReadNextCell(mat_t *mat, matvar_t *matvar);
static size_t ReadNextStructField(mat_t *mat, matvar_t *matvar);
static size_t ReadNextFunctionHandle(mat_t *mat, matvar_t *matvar);
static int ReadRankDims(mat_t *mat, matvar_t *matvar, enum matio_types data_type,
mat_uint32_t nbytes, size_t *read_bytes);
static int WriteType(mat_t *mat, matvar_t *matvar);
static int WriteCellArrayField(mat_t *mat, matvar_t *matvar);
static int WriteStructField(mat_t *mat, matvar_t *matvar);
static int WriteData(mat_t *mat, void *data, size_t N, enum matio_types data_type);
static size_t Mat_WriteEmptyVariable5(mat_t *mat, const char *name, int rank, size_t *dims);
static int Mat_VarReadNumeric5(mat_t *mat, matvar_t *matvar, void *data, size_t N);
#if HAVE_ZLIB
static size_t WriteCompressedCharData(mat_t *mat, z_streamp z, void *data, size_t N,
enum matio_types data_type);
static size_t WriteCompressedData(mat_t *mat, z_streamp z, void *data, int N,
enum matio_types data_type);
static size_t WriteCompressedTypeArrayFlags(mat_t *mat, matvar_t *matvar, z_streamp z);
static size_t WriteCompressedType(mat_t *mat, matvar_t *matvar, z_streamp z);
static size_t WriteCompressedCellArrayField(mat_t *mat, matvar_t *matvar, z_streamp z);
static size_t WriteCompressedStructField(mat_t *mat, matvar_t *matvar, z_streamp z);
static size_t Mat_WriteCompressedEmptyVariable5(mat_t *mat, const char *name, int rank,
size_t *dims, z_streamp z);
#endif
/** @brief determines the number of bytes for a given class type
*
* @ingroup mat_internal
* @param matvar MAT variable
* @param size the number of bytes needed to store the MAT variable
* @return 0 on success
*/
static int
GetTypeBufSize(matvar_t *matvar, size_t *size)
{
int err;
size_t nBytes, data_bytes;
size_t tag_size = 8;
size_t nelems = 1;
size_t rank_size;
*size = 0;
err = Mat_MulDims(matvar, &nelems);
if ( err )
return err;
/* Add rank and dimensions, padded to an 8 byte block */
err = Mul(&rank_size, matvar->rank, 4);
if ( err )
return err;
if ( matvar->rank % 2 )
nBytes = tag_size + 4;
else
nBytes = tag_size;
err = Add(&nBytes, nBytes, rank_size);
if ( err )
return err;
switch ( matvar->class_type ) {
case MAT_C_STRUCT: {
matvar_t **fields = (matvar_t **)matvar->data;
size_t nfields = matvar->internal->num_fields;
size_t maxlen = 0, i, field_buf_size;
for ( i = 0; i < nfields; i++ ) {
char *fieldname = matvar->internal->fieldnames[i];
if ( NULL != fieldname && strlen(fieldname) > maxlen )
maxlen = strlen(fieldname);
}
maxlen++;
while ( nfields * maxlen % 8 != 0 )
maxlen++;
err = Mul(&field_buf_size, maxlen, nfields);
if ( err )
return err;
err = Add(&nBytes, nBytes, tag_size + tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, field_buf_size);
if ( err )
return err;
/* FIXME: Add bytes for the fieldnames */
if ( NULL != fields && nfields > 0 ) {
size_t nelems_x_nfields = 1;
err = Mul(&nelems_x_nfields, nelems, nfields);
if ( err )
return err;
for ( i = 0; i < nelems_x_nfields; i++ ) {
err = GetStructFieldBufSize(fields[i], &field_buf_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, field_buf_size);
if ( err )
return err;
}
}
break;
}
case MAT_C_CELL: {
matvar_t **cells = (matvar_t **)matvar->data;
if ( matvar->nbytes == 0 || matvar->data_size == 0 )
break;
nelems = matvar->nbytes / matvar->data_size;
if ( NULL != cells && nelems > 0 ) {
size_t i, field_buf_size;
for ( i = 0; i < nelems; i++ ) {
err = GetCellArrayFieldBufSize(cells[i], &field_buf_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, field_buf_size);
if ( err )
return err;
}
}
break;
}
case MAT_C_SPARSE: {
mat_sparse_t *sparse = (mat_sparse_t *)matvar->data;
err = Mul(&data_bytes, sparse->nir, sizeof(mat_uint32_t));
if ( err )
return err;
if ( data_bytes % 8 ) {
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8);
if ( err )
return err;
}
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, data_bytes);
if ( err )
return err;
err = Mul(&data_bytes, sparse->njc, sizeof(mat_uint32_t));
if ( err )
return err;
if ( data_bytes % 8 ) {
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8);
if ( err )
return err;
}
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, data_bytes);
if ( err )
return err;
err = Mul(&data_bytes, sparse->ndata, Mat_SizeOf(matvar->data_type));
if ( err )
return err;
if ( data_bytes % 8 ) {
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8);
if ( err )
return err;
}
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, data_bytes);
if ( err )
return err;
if ( matvar->isComplex ) {
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, data_bytes);
if ( err )
return err;
}
break;
}
case MAT_C_CHAR:
if ( MAT_T_UINT8 == matvar->data_type || MAT_T_INT8 == matvar->data_type )
err = Mul(&data_bytes, nelems, Mat_SizeOf(MAT_T_UINT16));
else
err = Mul(&data_bytes, nelems, Mat_SizeOf(matvar->data_type));
if ( err )
return err;
if ( data_bytes % 8 ) {
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8);
if ( err )
return err;
}
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, data_bytes);
if ( err )
return err;
if ( matvar->isComplex ) {
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, data_bytes);
if ( err )
return err;
}
break;
default:
err = Mul(&data_bytes, nelems, Mat_SizeOf(matvar->data_type));
if ( err )
return err;
if ( data_bytes % 8 ) {
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8);
if ( err )
return err;
}
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, data_bytes);
if ( err )
return err;
if ( matvar->isComplex ) {
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, data_bytes);
if ( err )
return err;
}
} /* switch ( matvar->class_type ) */
*size = nBytes;
return MATIO_E_NO_ERROR;
}
/** @brief determines the number of bytes needed to store the given struct field
*
* @ingroup mat_internal
* @param matvar field of a structure
* @param size the number of bytes needed to store the struct field
* @return 0 on success
*/
static int
GetStructFieldBufSize(matvar_t *matvar, size_t *size)
{
int err;
size_t nBytes = 0, type_buf_size;
size_t tag_size = 8, array_flags_size = 8;
*size = 0;
if ( matvar == NULL )
return GetEmptyMatrixMaxBufSize(NULL, 2, size);
/* Add the Array Flags tag and space to the number of bytes */
nBytes += tag_size + array_flags_size;
/* In a struct field, the name is just a tag with 0 bytes */
nBytes += tag_size;
err = GetTypeBufSize(matvar, &type_buf_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, type_buf_size);
if ( err )
return err;
*size = nBytes;
return MATIO_E_NO_ERROR;
}
/** @brief determines the number of bytes needed to store the cell array element
*
* @ingroup mat_internal
* @param matvar MAT variable
* @param size the number of bytes needed to store the variable
* @return 0 on success
*/
static int
GetCellArrayFieldBufSize(matvar_t *matvar, size_t *size)
{
int err;
size_t nBytes = 0, type_buf_size;
size_t tag_size = 8, array_flags_size = 8;
*size = 0;
if ( matvar == NULL )
return MATIO_E_BAD_ARGUMENT;
/* Add the Array Flags tag and space to the number of bytes */
nBytes += tag_size + array_flags_size;
/* In an element of a cell array, the name is just a tag with 0 bytes */
nBytes += tag_size;
err = GetTypeBufSize(matvar, &type_buf_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, type_buf_size);
if ( err )
return err;
*size = nBytes;
return MATIO_E_NO_ERROR;
}
/** @brief determines the number of bytes needed to store the given variable
*
* @ingroup mat_internal
* @param name MAT variable
* @param rank rank of the variable
* @param size the number of bytes needed to store the variable
* @return 0 on success
*/
static int
GetEmptyMatrixMaxBufSize(const char *name, int rank, size_t *size)
{
int err = 0;
size_t nBytes = 0, len, rank_size;
size_t tag_size = 8, array_flags_size = 8;
/* Add the Array Flags tag and space to the number of bytes */
nBytes += tag_size + array_flags_size;
/* Get size of variable name, pad it to an 8 byte block, and add it to nBytes */
if ( NULL != name )
len = strlen(name);
else
len = 4;
if ( len <= 4 ) {
nBytes += tag_size;
} else {
nBytes += tag_size;
if ( len % 8 ) {
err = Add(&len, len, 8 - len % 8);
if ( err )
return err;
}
err = Add(&nBytes, nBytes, len);
if ( err )
return err;
}
/* Add rank and dimensions, padded to an 8 byte block */
err = Mul(&rank_size, rank, 4);
if ( err )
return err;
if ( rank % 2 )
err = Add(&nBytes, nBytes, tag_size + 4);
else
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, rank_size);
if ( err )
return err;
/* Data tag */
err = Add(&nBytes, nBytes, tag_size);
if ( err )
return err;
*size = nBytes;
return MATIO_E_NO_ERROR;
}
static void
SetFieldNames(matvar_t *matvar, char *buf, size_t nfields, mat_uint32_t fieldname_length)
{
matvar->internal->num_fields = nfields;
matvar->internal->fieldnames = (char **)calloc(nfields, sizeof(*matvar->internal->fieldnames));
if ( NULL != matvar->internal->fieldnames ) {
size_t i;
for ( i = 0; i < nfields; i++ ) {
matvar->internal->fieldnames[i] = (char *)malloc(fieldname_length);
if ( NULL != matvar->internal->fieldnames[i] ) {
memcpy(matvar->internal->fieldnames[i], buf + i * fieldname_length,
fieldname_length);
matvar->internal->fieldnames[i][fieldname_length - 1] = '\0';
}
}
}
}
static size_t
ReadSparse(mat_t *mat, matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v)
{
int data_in_tag = 0;
enum matio_types packed_type;
mat_uint32_t tag[2];
size_t bytesread = 0;
mat_uint32_t N = 0;
if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
#if HAVE_ZLIB
matvar->internal->z->avail_in = 0;
if ( 0 != Inflate(mat, matvar->internal->z, tag, 4, &bytesread) ) {
return bytesread;
}
if ( mat->byteswap )
(void)Mat_uint32Swap(tag);
packed_type = TYPE_FROM_TAG(tag[0]);
if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
data_in_tag = 1;
N = (tag[0] & 0xffff0000) >> 16;
} else {
data_in_tag = 0;
(void)ReadCompressedUInt32Data(mat, matvar->internal->z, &N, MAT_T_UINT32, 1);
}
#endif
} else {
if ( 0 != Read(tag, 4, 1, (FILE *)mat->fp, &bytesread) ) {
return bytesread;
}
if ( mat->byteswap )
(void)Mat_uint32Swap(tag);
packed_type = TYPE_FROM_TAG(tag[0]);
if ( tag[0] & 0xffff0000 ) { /* Data is in the tag */
data_in_tag = 1;
N = (tag[0] & 0xffff0000) >> 16;
} else {
data_in_tag = 0;
if ( 0 != Read(&N, 4, 1, (FILE *)mat->fp, &bytesread) ) {
return bytesread;
}
if ( mat->byteswap )
(void)Mat_uint32Swap(&N);
}
}
if ( 0 == N )
return bytesread;
*n = N / 4;
*v = (mat_uint32_t *)calloc(N, 1);
if ( NULL != *v ) {
int nBytes;
if ( matvar->compression == MAT_COMPRESSION_NONE ) {
nBytes = ReadUInt32Data(mat, *v, packed_type, *n);
/*
* If the data was in the tag we started on a 4-byte
* boundary so add 4 to make it an 8-byte
*/
nBytes *= Mat_SizeOf(packed_type);
if ( data_in_tag )
nBytes += 4;
if ( (nBytes % 8) != 0 )
(void)fseek((FILE *)mat->fp, 8 - (nBytes % 8), SEEK_CUR);
#if HAVE_ZLIB
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) {
nBytes = ReadCompressedUInt32Data(mat, matvar->internal->z, *v, packed_type, *n);
/*
* If the data was in the tag we started on a 4-byte
* boundary so add 4 to make it an 8-byte
*/
if ( data_in_tag )
nBytes += 4;
if ( (nBytes % 8) != 0 )
InflateSkip(mat, matvar->internal->z, 8 - (nBytes % 8), NULL);
#endif
}
} else {
Mat_Critical("Couldn't allocate memory");
}
return bytesread;
}
#if HAVE_ZLIB
/** @brief determines the number of bytes needed to store the given variable
*
* @ingroup mat_internal
* @param matvar MAT variable
* @param size the number of bytes needed to store the variable
* @return 0 on success
*/
static int
GetMatrixMaxBufSize(matvar_t *matvar, size_t *size)
{
int err = MATIO_E_NO_ERROR;
size_t nBytes = 0, len, type_buf_size;
size_t tag_size = 8, array_flags_size = 8;
if ( matvar == NULL )
return MATIO_E_BAD_ARGUMENT;
/* Add the Array Flags tag and space to the number of bytes */
nBytes += tag_size + array_flags_size;
/* Get size of variable name, pad it to an 8 byte block, and add it to nBytes */
if ( NULL != matvar->name )
len = strlen(matvar->name);
else
len = 4;
if ( len <= 4 ) {
nBytes += tag_size;
} else {
nBytes += tag_size;
if ( len % 8 ) {
err = Add(&len, len, 8 - len % 8);
if ( err )
return err;
}
err = Add(&nBytes, nBytes, len);
if ( err )
return err;
}
err = GetTypeBufSize(matvar, &type_buf_size);
if ( err )
return err;
err = Add(&nBytes, nBytes, type_buf_size);
if ( err )
return err;
*size = nBytes;
return MATIO_E_NO_ERROR;
}
#endif
/** @if mat_devman
* @brief Creates a new Matlab MAT version 5 file
*
* Tries to create a new Matlab MAT file with the given name and optional
* header string. If no header string is given, the default string
* is used containing the software, version, and date in it. If a header
* string is given, at most the first 116 characters is written to the file.
* The given header string need not be the full 116 characters, but MUST be
* NULL terminated.
* @ingroup MAT
* @param matname Name of MAT file to create
* @param hdr_str Optional header string, NULL to use default
* @return A pointer to the MAT file or NULL if it failed. This is not a
* simple FILE * and should not be used as one.
* @endif
*/
mat_t *
Mat_Create5(const char *matname, const char *hdr_str)
{
FILE *fp = NULL;
mat_int16_t endian = 0, version;
mat_t *mat = NULL;
size_t err;
time_t t;
#if defined(_WIN32) && defined(_MSC_VER)
wchar_t *wname = utf82u(matname);
if ( NULL != wname ) {
fp = _wfopen(wname, L"w+b");
free(wname);
}
#else
fp = fopen(matname, "w+b");
#endif
if ( !fp )
return NULL;
mat = (mat_t *)malloc(sizeof(*mat));
if ( mat == NULL ) {
fclose(fp);
return NULL;
}
mat->fp = NULL;
mat->header = NULL;
mat->subsys_offset = NULL;
mat->filename = NULL;
mat->version = 0;
mat->byteswap = 0;
mat->mode = 0;
mat->bof = 128;
mat->next_index = 0;
mat->num_datasets = 0;
#if defined(MAT73) && MAT73
mat->refs_id = -1;
#endif
mat->dir = NULL;
t = time(NULL);
mat->fp = fp;
mat->filename = strdup(matname);
mat->mode = MAT_ACC_RDWR;
mat->byteswap = 0;
mat->header = (char *)malloc(128 * sizeof(char));
mat->subsys_offset = (char *)malloc(8 * sizeof(char));
memset(mat->header, ' ', 128);
if ( hdr_str == NULL ) {
err = mat_snprintf(mat->header, 116,
"MATLAB 5.0 MAT-file, Platform: %s, "
"Created by: libmatio v%d.%d.%d on %s",
MATIO_PLATFORM, MATIO_MAJOR_VERSION, MATIO_MINOR_VERSION,
MATIO_RELEASE_LEVEL, ctime(&t));
} else {
err = mat_snprintf(mat->header, 116, "%s", hdr_str);
}
if ( err >= 116 )
mat->header[115] = '\0'; /* Just to make sure it's NULL terminated */
memset(mat->subsys_offset, ' ', 8);
mat->version = 0x0100;
endian = 0x4d49;
version = 0x0100;
fwrite(mat->header, 1, 116, (FILE *)mat->fp);
fwrite(mat->subsys_offset, 1, 8, (FILE *)mat->fp);
fwrite(&version, 2, 1, (FILE *)mat->fp);
fwrite(&endian, 2, 1, (FILE *)mat->fp);
return mat;
}
/** @if mat_devman
* @brief Writes @c data as character data
*
* This function uses the knowledge that the data is part of a character class
* to avoid some pitfalls with Matlab listed below.
* @li Matlab character data cannot be unsigned 8-bit integers, it needs at
* least unsigned 16-bit integers
*
* @ingroup mat_internal
* @param mat MAT file pointer
* @param data character data to write
* @param N Number of elements to write
* @param data_type character data type (enum matio_types)
* @return number of bytes written
* @endif
*/
static size_t
WriteCharData(mat_t *mat, void *data, size_t N, enum matio_types data_type)
{
mat_uint32_t nBytes = 0;
size_t nbytes, i;
size_t byteswritten = 0;
const mat_uint8_t pad1 = 0;
int err;
switch ( data_type ) {
case MAT_T_UINT8:
case MAT_T_UINT16:
case MAT_T_UTF8:
case MAT_T_UTF16: {
data_type = MAT_T_UINT8 == data_type ? MAT_T_UTF8 : data_type;
err = Mul(&nbytes, N, Mat_SizeOf(data_type));
if ( err ) {
return 0;
}
nBytes = (mat_uint32_t)nbytes;
fwrite(&data_type, 4, 1, (FILE *)mat->fp);
fwrite(&nBytes, 4, 1, (FILE *)mat->fp);
if ( NULL != data && N > 0 )
fwrite(data, 1, nbytes, (FILE *)mat->fp);
if ( nBytes % 8 ) {
for ( i = nbytes % 8; i < 8; i++ )
fwrite(&pad1, 1, 1, (FILE *)mat->fp);
}
break;
}
case MAT_T_INT8: {
mat_int8_t *ptr;
mat_uint16_t c;
/* Matlab can't read MAT_C_CHAR as int8, needs uint16 */
data_type = MAT_T_UINT16;
err = Mul(&nbytes, N, Mat_SizeOf(data_type));
if ( err ) {
return 0;
}
nBytes = (mat_uint32_t)nbytes;
fwrite(&data_type, 4, 1, (FILE *)mat->fp);
fwrite(&nBytes, 4, 1, (FILE *)mat->fp);
ptr = (mat_int8_t *)data;
if ( NULL == data )
break;
for ( i = 0; i < N; i++ ) {
c = (mat_uint16_t) * (char *)ptr;
fwrite(&c, 2, 1, (FILE *)mat->fp);
ptr++;
}
if ( nbytes % 8 )
for ( i = nbytes % 8; i < 8; i++ )
fwrite(&pad1, 1, 1, (FILE *)mat->fp);
break;
}
case MAT_T_UNKNOWN: {
/* Sometimes empty char data will have MAT_T_UNKNOWN, so just write
* a data tag
*/
data_type = MAT_T_UINT16;
err = Mul(&nbytes, N, Mat_SizeOf(data_type));
if ( err ) {
return 0;
}
nBytes = (mat_uint32_t)nbytes;
fwrite(&data_type, 4, 1, (FILE *)mat->fp);
fwrite(&nBytes, 4, 1, (FILE *)mat->fp);
break;
}
default:
nbytes = 0;
break;
}
byteswritten += nbytes;
return byteswritten;
}
#if HAVE_ZLIB
/** @brief Writes @c data as compressed character data
*
* This function uses the knowledge that the data is part of a character class
* to avoid some pitfalls with Matlab listed below.
* @li Matlab character data cannot be unsigned 8-bit integers, it needs at
* least unsigned 16-bit integers
*
* @ingroup mat_internal
* @param mat MAT file pointer
* @param z pointer to the zlib compression stream
* @param data character data to write
* @param N Number of elements to write
* @param data_type character data type (enum matio_types)
* @return number of bytes written
*/
static size_t
WriteCompressedCharData(mat_t *mat, z_streamp z, void *data, size_t N, enum matio_types data_type)
{
size_t data_size, byteswritten = 0, nbytes;
mat_uint32_t data_tag[2];
int buf_size = 1024;
int err;
mat_uint8_t buf[1024], pad[8] = {0, 0, 0, 0, 0, 0, 0, 0};
if ( mat == NULL || mat->fp == NULL )
return 0;
if ( data_type == MAT_T_UNKNOWN ) {
data_size = Mat_SizeOf(MAT_T_UINT16);
} else {
data_size = Mat_SizeOf(data_type);
}
err = Mul(&nbytes, N, data_size);
if ( err ) {
return byteswritten;
}
switch ( data_type ) {
case MAT_T_UINT8:
case MAT_T_UINT16:
case MAT_T_UTF8:
case MAT_T_UTF16:
data_tag[0] = MAT_T_UINT8 == data_type ? MAT_T_UTF8 : data_type;
data_tag[1] = (mat_uint32_t)nbytes;
z->next_in = ZLIB_BYTE_PTR(data_tag);
z->avail_in = 8;
do {
z->next_out = buf;
z->avail_out = buf_size;
deflate(z, Z_NO_FLUSH);
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp);
} while ( z->avail_out == 0 );
/* exit early if this is an empty data */
if ( NULL == data || N < 1 )
break;
z->next_in = (Bytef *)data;
z->avail_in = (mat_uint32_t)nbytes;
do {
z->next_out = buf;
z->avail_out = buf_size;
deflate(z, Z_NO_FLUSH);
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp);
} while ( z->avail_out == 0 );
/* Add/Compress padding to pad to 8-byte boundary */
if ( nbytes % 8 ) {
z->next_in = pad;
z->avail_in = 8 - (nbytes % 8);
do {
z->next_out = buf;
z->avail_out = buf_size;
deflate(z, Z_NO_FLUSH);
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp);
} while ( z->avail_out == 0 );
}
break;
case MAT_T_UNKNOWN:
/* Sometimes empty char data will have MAT_T_UNKNOWN, so just write a data tag */
data_tag[0] = MAT_T_UINT16;
data_tag[1] = (mat_uint32_t)nbytes;
z->next_in = ZLIB_BYTE_PTR(data_tag);
z->avail_in = 8;
do {
z->next_out = buf;
z->avail_out = buf_size;
deflate(z, Z_NO_FLUSH);
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp);
} while ( z->avail_out == 0 );
break;
default:
break;
}
return byteswritten;
}
#endif
/** @brief Writes the data buffer to the file
*
* @param mat MAT file pointer
* @param data pointer to the data to write
* @param N number of elements to write
* @param data_type data type of the data
* @return number of bytes written
*/
static int
WriteData(mat_t *mat, void *data, size_t N, enum matio_types data_type)
{
int nBytes = 0, data_size;
if ( mat == NULL || mat->fp == NULL )
return 0;
data_size = Mat_SizeOf(data_type);
nBytes = N * data_size;
fwrite(&data_type, 4, 1, (FILE *)mat->fp);
fwrite(&nBytes, 4, 1, (FILE *)mat->fp);
if ( data != NULL && N > 0 )
fwrite(data, data_size, N, (FILE *)mat->fp);
return nBytes;
}
#if HAVE_ZLIB
/* Compresses the data buffer and writes it to the file */
static size_t
WriteCompressedData(mat_t *mat, z_streamp z, void *data, int N, enum matio_types data_type)
{
int nBytes = 0, data_size, data_tag[2], byteswritten = 0;
int buf_size = 1024;
mat_uint8_t buf[1024], pad[8] = {0, 0, 0, 0, 0, 0, 0, 0};
if ( mat == NULL || mat->fp == NULL )
return 0;
data_size = Mat_SizeOf(data_type);
data_tag[0] = data_type;
data_tag[1] = data_size * N;
z->next_in = ZLIB_BYTE_PTR(data_tag);
z->avail_in = 8;
do {
z->next_out = buf;
z->avail_out = buf_size;
deflate(z, Z_NO_FLUSH);
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp);
} while ( z->avail_out == 0 );
/* exit early if this is an empty data */
if ( NULL == data || N < 1 )
return byteswritten;
z->next_in = (Bytef *)data;
z->avail_in = N * data_size;
do {
z->next_out = buf;
z->avail_out = buf_size;
deflate(z, Z_NO_FLUSH);
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp);
} while ( z->avail_out == 0 );
/* Add/Compress padding to pad to 8-byte boundary */
if ( N * data_size % 8 ) {
z->next_in = pad;
z->avail_in = 8 - (N * data_size % 8);
do {
z->next_out = buf;
z->avail_out = buf_size;
deflate(z, Z_NO_FLUSH);
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp);
} while ( z->avail_out == 0 );
}
nBytes = byteswritten;
return nBytes;
}
#endif
/** @brief Reads the next cell of the cell array in @c matvar
*
* @ingroup mat_internal
* @param mat MAT file pointer
* @param matvar MAT variable pointer
* @return Number of bytes read
*/
static size_t
ReadNextCell(mat_t *mat, matvar_t *matvar)
{
size_t bytesread = 0, i;
int err;
matvar_t **cells = NULL;
size_t nelems = 1;
err = Mat_MulDims(matvar, &nelems);
if ( err ) {
Mat_Critical("Integer multiplication overflow");
return bytesread;
}
matvar->data_size = sizeof(matvar_t *);
err = Mul(&matvar->nbytes, nelems, matvar->data_size);
if ( err ) {
Mat_Critical("Integer multiplication overflow");
return bytesread;
}
matvar->data = calloc(nelems, matvar->data_size);
if ( NULL == matvar->data ) {
if ( NULL != matvar->name )