-
Notifications
You must be signed in to change notification settings - Fork 168
/
ModelicaMatIO.c
16191 lines (15098 loc) · 588 KB
/
ModelicaMatIO.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
/* ModelicaMatIO.c - MAT file I/O functions
Copyright (C) 2013-2024, Modelica Association and contributors
Copyright (C) 2015-2024, The matio contributors
Copyright (C) 2005-2014, 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.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
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.
*/
/* This file was created by concatenation of the following C source files of the
MAT file I/O library from <http://sourceforge.net/projects/matio/>:
endian.c
inflate.c
io.c
read_data.c
mat.c
mat4.c
mat5.c
mat73.c
matvar_cell.c
matvar_struct.c
*/
/* By default v4 and v6 MAT-files are supported. The v7 and v7.3 MAT-file
formats require additional preprocessor options and third-party libraries.
The following #define's are available.
NO_FILE_SYSTEM: A file system is not present (e.g. on dSPACE or xPC).
NO_LOCALE : locale.h is not present (e.g. on AVR).
HAVE_ZLIB=1 : Enables the support of v7 MAT-files
The zlib (>= v1.2.3) library is required.
HAVE_HDF5=1 : Enables the support of v7.3 MAT-files
The hdf5 (>= v1.8) library is required.
*/
#if !defined(NO_FILE_SYSTEM)
#if defined(__gnu_linux__)
#define _GNU_SOURCE 1
#elif defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO)
#define __USE_MINGW_ANSI_STDIO 1
#endif
#include <stdarg.h>
#include "ModelicaUtilities.h"
/* -------------------------------
* ---------- endian.c
* -------------------------------
*/
/** @file endian.c
* @brief Functions to handle endian specifics
*/
#include <stdlib.h>
#ifndef MATIO_PRIVATE_H
#define MATIO_PRIVATE_H
/* Extended sparse matrix data types */
/* #undef EXTENDED_SPARSE */
/* Define to 1 if you have the `_fseeki64' function. */
#if defined(_MSC_VER) && _MSC_VER >= 1400
#define HAVE__FSEEKI64 1
#else
#undef HAVE__FSEEKI64
#endif
/* Define to 1 if you have the `_ftelli64' function. */
#if defined(_MSC_VER) && _MSC_VER >= 1400
#define HAVE__FTELLI64 1
#else
#undef HAVE__FTELLI64
#endif
/* Define to 1 if you have the <inttypes.h> header file. */
#if defined(_WIN32)
#if defined(_MSC_VER) && _MSC_VER >= 1800
#define HAVE_INTTYPES_H 1
#elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__)
#define HAVE_INTTYPES_H 1
#else
#undef HAVE_INTTYPES_H
#endif
#elif defined(__GNUC__)
#define HAVE_INTTYPES_H 1
#else
#undef HAVE_INTTYPES_H
#endif
/* Define to 1 if you have the <intsafe.h> header file. */
#if defined(_MSC_VER) && _MSC_VER >= 1600
#define HAVE_INTSAFE_H 1
#else
#undef HAVE_INTSAFE_H
#endif
#if !defined(__BORLANDC__) && !defined(__LCC__) && !(defined(_MSC_VER) && _MSC_VER < 1400)
#define HAVE_SAFE_MATH 1
#else
#undef HAVE_SAFE_MATH
#endif
#if !defined(NO_LOCALE)
/* Define to 1 if you have the `localeconv' function. */
#define HAVE_LOCALECONV 1
/* Define to 1 if you have the <locale.h> header file. */
#define HAVE_LOCALE_H 1
#endif
/* Define to 1 if the system has the type `long double'. */
#if defined(_WIN32)
#if defined(__WATCOMC__) || (defined(_MSC_VER) && _MSC_VER >= 1300)
#define HAVE_LONG_DOUBLE 1
#endif
#endif
#if defined(__GNUC__)
#define HAVE_LONG_DOUBLE 1
#endif
/* Define to 1 if the system has the type `long long int'. */
#if defined(_WIN32)
#if defined(__WATCOMC__) || (defined(_MSC_VER) && _MSC_VER >= 1300)
#define HAVE_LONG_LONG_INT 1
#endif
#endif
#if defined(__GNUC__) && __STDC_VERSION__ >= 199901L
#define HAVE_LONG_LONG_INT 1
#endif
/* Define to 1 if the system has the type `ptrdiff_t'. */
#define HAVE_PTRDIFF_T 1
/* Define to 1 if you have a C99 compliant `snprintf' function. */
#if defined(STDC99)
#define HAVE_SNPRINTF 1
#elif defined(__MINGW32__) || defined(__CYGWIN__)
#if __STDC_VERSION__ >= 199901L
#define HAVE_SNPRINTF 1
#endif
#elif defined(__WATCOMC__)
#define HAVE_SNPRINTF 1
#elif defined(__TURBOC__) && __TURBOC__ >= 0x550
#define HAVE_SNPRINTF 1
#elif defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410)
#define HAVE_SNPRINTF 1
#elif defined(_MSC_VER) && _MSC_VER >= 1900
#define HAVE_SNPRINTF 1
#else
#undef HAVE_SNPRINTF
#endif
/* Define to 1 if you have the <stdarg.h> header file. */
#define HAVE_STDARG_H 1
/* Define to 1 if you have the <stddef.h> header file. */
#define HAVE_STDDEF_H 1
/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1
/* Define to 1 if `decimal_point' is member of `struct lconv'. */
#define HAVE_STRUCT_LCONV_DECIMAL_POINT 1
/* Define to 1 if `thousands_sep' is member of `struct lconv'. */
#define HAVE_STRUCT_LCONV_THOUSANDS_SEP 1
/* Define to 1 if the system has the type `unsigned long long int'. */
#if defined(_WIN32)
#if defined(__WATCOMC__) || (defined(_MSC_VER) && _MSC_VER >= 1300)
#define HAVE_UNSIGNED_LONG_LONG_INT 1
#endif
#endif
#if defined(__GNUC__) && __STDC_VERSION__ >= 199901L
#define HAVE_UNSIGNED_LONG_LONG_INT 1
#endif
/* Define to 1 if you have the `va_copy' function or macro. */
#if defined(__GNUC__) && __STDC_VERSION__ >= 199901L
#define HAVE_VA_COPY 1
#elif defined(_MSC_VER) && _MSC_VER >= 1800
#define HAVE_VA_COPY 1
#elif defined(__WATCOMC__)
#define HAVE_VA_COPY 1
#else
#undef HAVE_VA_COPY
#endif
/* Define to 1 if you have the `__va_copy' function or macro. */
#if defined(__GNUC__)
#define HAVE___VA_COPY 1
#elif defined(__WATCOMC__)
#define HAVE___VA_COPY 1
#else
#undef HAVE___VA_COPY
#endif
/* Platform */
#if defined(_MSC_VER) && defined(_WIN64)
#define MATIO_PLATFORM "x86_64-pc-windows"
#elif defined(_MSC_VER) && defined(_WIN32)
#define MATIO_PLATFORM "i686-pc-windows"
#else
#define MATIO_PLATFORM "UNKNOWN"
#endif
/* Fixed types in safe-math.h disabled */
#define PSNIP_SAFE_NO_FIXED 1
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
/* Z prefix */
#undef Z_PREFIX
#include "ModelicaMatIO.h"
#if HAVE_SAFE_MATH
#include "safe-math.h"
#endif
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#if defined(__GLIBC__)
#include <endian.h>
#endif
#if HAVE_INTTYPES_H
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#endif
#if defined(__BORLANDC__) || defined(__MINGW32__) || defined(_MSC_VER)
#if defined(_MSC_VER) && defined(HAVE__FSEEKI64) && defined(HAVE__FTELLI64)
#define MATIO_LFS
#define mat_off_t __int64
#define fseeko _fseeki64
#define ftello _ftelli64
#elif defined(__BORLANDC__) && defined(HAVE__FSEEKI64) && defined(HAVE__FTELLI64)
#define MATIO_LFS
#define mat_off_t __int64
#define fseeko _fseeki64
#define ftello _ftelli64
#elif defined(HAVE_FSEEKO64) && defined(HAVE_FTELLO64)
#define MATIO_LFS
#define mat_off_t __int64
#define fseeko fseeko64
#define ftello ftello64
#endif
#elif defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64 && defined(HAVE_FSEEKO) && \
defined(HAVE_FTELLO)
#define MATIO_LFS
#define mat_off_t off_t
#endif
#if !defined(MATIO_LFS)
#define mat_off_t long
#define ftello ftell
#define fseeko fseek
#endif
#if HAVE_ZLIB
#define ZLIB_BYTE_PTR(a) ((Bytef *)(a))
#include <zlib.h>
#endif
#if HAVE_HDF5
#include <hdf5.h>
#endif
#if ( defined(_WIN64) || defined(_WIN32) ) && !defined(__CYGWIN__)
#include <io.h>
#endif
#if defined(_MSC_VER) || defined(__MINGW32__)
#define SIZE_T_FMTSTR "Iu"
#else
#define SIZE_T_FMTSTR "zu"
#endif
#ifndef INT32_MAX
#define INT32_MAX INT_MAX
#endif
#ifndef UINT32_MAX
#ifdef UINT_MAX
#define UINT32_MAX UINT_MAX
#else
#define UINT32_MAX INT_MAX
#endif
#endif
#if !defined(READ_BLOCK_SIZE)
#define READ_BLOCK_SIZE (8192)
#endif
#define CAT_(X, Y) X##Y
#define CAT(X, Y) CAT_(X, Y)
#if defined(__GLIBC__)
#if ( __BYTE_ORDER == __BIG_ENDIAN )
#define MATIO_BE
#elif (__BYTE_ORDER == __LITTLE_ENDIAN)
#define MATIO_LE
#endif
#elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
#define MATIO_BE
#elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
#define MATIO_LE
#elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || \
defined(__ppc__) || defined(__hpux) || defined(_MIPSEB) || defined(_POWER) || \
defined(__s390__)
#define MATIO_BE
#elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || \
defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || \
defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || \
defined(_M_X64) || defined(__bfin__) || defined(__loongarch64) || defined(__aarch64__)
#define MATIO_LE
#endif
/** @if mat_devman
* @brief Matlab MAT File information
*
* Contains information about a Matlab MAT file
* @ingroup mat_internal
* @endif
*/
struct _mat_t
{
void *fp; /**< File pointer for the MAT file */
char *header; /**< MAT file header string */
char *subsys_offset; /**< Offset */
char *filename; /**< Filename of the MAT file */
int version; /**< MAT file version */
int byteswap; /**< 1 if byte swapping is required, 0 otherwise */
int mode; /**< Access mode */
mat_off_t bof; /**< Beginning of file not including any header */
size_t next_index; /**< Index/File position of next variable to read */
size_t num_datasets; /**< Number of datasets in the file */
#if HAVE_HDF5
hid_t refs_id; /**< Id of the /#refs# group in HDF5 */
#endif
char **dir; /**< Names of the datasets in the file */
};
/** @if mat_devman
* @brief internal structure for MAT variables
* @ingroup mat_internal
* @endif
*/
struct matvar_internal
{
#if HAVE_HDF5
hobj_ref_t hdf5_ref; /**< Reference */
hid_t id; /**< Id */
#endif
mat_off_t datapos; /**< Offset from the beginning of the MAT file to the data */
unsigned num_fields; /**< Number of fields */
char **fieldnames; /**< Pointer to fieldnames */
#if HAVE_ZLIB
z_streamp z; /**< zlib compression state */
void *data; /**< Inflated data array */
#endif
};
/* snprintf.c */
#if !HAVE_SNPRINTF
int rpl_snprintf(char *, size_t, const char *, ...);
#define mat_snprintf rpl_snprintf
#else
#define mat_snprintf snprintf
#endif /* !HAVE_SNPRINTF */
/* endian.c */
static double Mat_doubleSwap(double *a);
static float Mat_floatSwap(float *a);
#ifdef HAVE_MATIO_INT64_T
static mat_int64_t Mat_int64Swap(mat_int64_t *a);
#endif /* HAVE_MATIO_INT64_T */
#ifdef HAVE_MATIO_UINT64_T
static mat_uint64_t Mat_uint64Swap(mat_uint64_t *a);
#endif /* HAVE_MATIO_UINT64_T */
static mat_int32_t Mat_int32Swap(mat_int32_t *a);
static mat_uint32_t Mat_uint32Swap(mat_uint32_t *a);
static mat_int16_t Mat_int16Swap(mat_int16_t *a);
static mat_uint16_t Mat_uint16Swap(mat_uint16_t *a);
/* read_data.c */
static size_t ReadDoubleData(mat_t *mat, double *data, enum matio_types data_type, size_t len);
static size_t ReadSingleData(mat_t *mat, float *data, enum matio_types data_type, size_t len);
#ifdef HAVE_MATIO_INT64_T
static size_t ReadInt64Data(mat_t *mat, mat_int64_t *data, enum matio_types data_type, size_t len);
#endif /* HAVE_MATIO_INT64_T */
#ifdef HAVE_MATIO_UINT64_T
static size_t ReadUInt64Data(mat_t *mat, mat_uint64_t *data, enum matio_types data_type,
size_t len);
#endif /* HAVE_MATIO_UINT64_T */
static size_t ReadInt32Data(mat_t *mat, mat_int32_t *data, enum matio_types data_type, size_t len);
static size_t ReadUInt32Data(mat_t *mat, mat_uint32_t *data, enum matio_types data_type,
size_t len);
static size_t ReadInt16Data(mat_t *mat, mat_int16_t *data, enum matio_types data_type, size_t len);
static size_t ReadUInt16Data(mat_t *mat, mat_uint16_t *data, enum matio_types data_type,
size_t len);
static size_t ReadInt8Data(mat_t *mat, mat_int8_t *data, enum matio_types data_type, size_t len);
static size_t ReadUInt8Data(mat_t *mat, mat_uint8_t *data, enum matio_types data_type, size_t len);
static size_t ReadCharData(mat_t *mat, void *_data, enum matio_types data_type, size_t len);
static int ReadDataSlab1(mat_t *mat, void *data, enum matio_classes class_type,
enum matio_types data_type, int start, int stride, int edge);
static int ReadDataSlab2(mat_t *mat, void *data, enum matio_classes class_type,
enum matio_types data_type, const size_t *dims, const int *start,
const int *stride, const int *edge);
static int ReadDataSlabN(mat_t *mat, void *data, enum matio_classes class_type,
enum matio_types data_type, int rank, const size_t *dims, const int *start,
const int *stride, const int *edge);
#if HAVE_ZLIB
static int ReadCompressedDoubleData(mat_t *mat, z_streamp z, double *data,
enum matio_types data_type, int len);
static int ReadCompressedSingleData(mat_t *mat, z_streamp z, float *data,
enum matio_types data_type, int len);
#ifdef HAVE_MATIO_INT64_T
static int ReadCompressedInt64Data(mat_t *mat, z_streamp z, mat_int64_t *data,
enum matio_types data_type, int len);
#endif /* HAVE_MATIO_INT64_T */
#ifdef HAVE_MATIO_UINT64_T
static int ReadCompressedUInt64Data(mat_t *mat, z_streamp z, mat_uint64_t *data,
enum matio_types data_type, int len);
#endif /* HAVE_MATIO_UINT64_T */
static int ReadCompressedInt32Data(mat_t *mat, z_streamp z, mat_int32_t *data,
enum matio_types data_type, int len);
static int ReadCompressedUInt32Data(mat_t *mat, z_streamp z, mat_uint32_t *data,
enum matio_types data_type, int len);
static int ReadCompressedInt16Data(mat_t *mat, z_streamp z, mat_int16_t *data,
enum matio_types data_type, int len);
static int ReadCompressedUInt16Data(mat_t *mat, z_streamp z, mat_uint16_t *data,
enum matio_types data_type, int len);
static int ReadCompressedInt8Data(mat_t *mat, z_streamp z, mat_int8_t *data,
enum matio_types data_type, int len);
static int ReadCompressedUInt8Data(mat_t *mat, z_streamp z, mat_uint8_t *data,
enum matio_types data_type, int len);
static int ReadCompressedCharData(mat_t *mat, z_streamp z, void *data, enum matio_types data_type,
size_t len);
static int ReadCompressedDataSlab1(mat_t *mat, z_streamp z, void *data,
enum matio_classes class_type, enum matio_types data_type,
int start, int stride, int edge);
static int ReadCompressedDataSlab2(mat_t *mat, z_streamp z, void *data,
enum matio_classes class_type, enum matio_types data_type,
const size_t *dims, const int *start, const int *stride,
const int *edge);
static int ReadCompressedDataSlabN(mat_t *mat, z_streamp z, void *data,
enum matio_classes class_type, enum matio_types data_type,
int rank, const size_t *dims, const int *start,
const int *stride, const int *edge);
/* inflate.c */
static int InflateSkip(mat_t *mat, z_streamp z, int nBytes, size_t *bytesread);
static int InflateSkipData(mat_t *mat, z_streamp z, enum matio_types data_type, int len);
static int InflateRankDims(mat_t *mat, z_streamp z, void *buf, size_t nBytes, mat_uint32_t **dims,
size_t *bytesread);
static int Inflate(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes, size_t *bytesread);
static int InflateData(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes);
#endif
/* mat.c */
static mat_complex_split_t *ComplexMalloc(size_t nbytes);
static void ComplexFree(mat_complex_split_t *complex_data);
static enum matio_types ClassType2DataType(enum matio_classes class_type);
static int Add(size_t *res, size_t a, size_t b);
static int Mul(size_t *res, size_t a, size_t b);
static int Mat_MulDims(const matvar_t *matvar, size_t *nelems);
static int Read(void *buf, size_t size, size_t count, FILE *fp, size_t *bytesread);
static int IsEndOfFile(FILE *fp, mat_off_t *fpos);
static int CheckSeekFile(FILE *fp, mat_off_t offset);
/* io.c */
#if defined(_WIN32) && defined(_MSC_VER)
static wchar_t *utf82u(const char *src);
#endif
#endif
/** @brief swap the bytes @c a and @c b
* @ingroup mat_internal
*/
#define swap(a, b) \
a ^= b; \
b ^= a; \
a ^= b
#ifdef HAVE_MATIO_INT64_T
/** @brief swap the bytes of a 64-bit signed integer
* @ingroup mat_internal
* @param a pointer to integer to swap
* @return the swapped integer
*/
static mat_int64_t
Mat_int64Swap(mat_int64_t *a)
{
union {
mat_int8_t i1[8];
mat_int64_t i8;
} tmp;
tmp.i8 = *a;
swap(tmp.i1[0], tmp.i1[7]);
swap(tmp.i1[1], tmp.i1[6]);
swap(tmp.i1[2], tmp.i1[5]);
swap(tmp.i1[3], tmp.i1[4]);
*a = tmp.i8;
return *a;
}
#endif /* HAVE_MATIO_INT64_T */
#ifdef HAVE_MATIO_UINT64_T
/** @brief swap the bytes of a 64-bit unsigned integer
* @ingroup mat_internal
* @param a pointer to integer to swap
* @return the swapped integer
*/
static mat_uint64_t
Mat_uint64Swap(mat_uint64_t *a)
{
union {
mat_uint8_t i1[8];
mat_uint64_t i8;
} tmp;
tmp.i8 = *a;
swap(tmp.i1[0], tmp.i1[7]);
swap(tmp.i1[1], tmp.i1[6]);
swap(tmp.i1[2], tmp.i1[5]);
swap(tmp.i1[3], tmp.i1[4]);
*a = tmp.i8;
return *a;
}
#endif /* HAVE_MATIO_UINT64_T */
/** @brief swap the bytes of a 32-bit signed integer
* @ingroup mat_internal
* @param a pointer to integer to swap
* @return the swapped integer
*/
static mat_int32_t
Mat_int32Swap(mat_int32_t *a)
{
union {
mat_int8_t i1[4];
mat_int32_t i4;
} tmp;
tmp.i4 = *a;
swap(tmp.i1[0], tmp.i1[3]);
swap(tmp.i1[1], tmp.i1[2]);
*a = tmp.i4;
return *a;
}
/** @brief swap the bytes of a 32-bit unsigned integer
* @ingroup mat_internal
* @param a pointer to integer to swap
* @return the swapped integer
*/
static mat_uint32_t
Mat_uint32Swap(mat_uint32_t *a)
{
union {
mat_uint8_t i1[4];
mat_uint32_t i4;
} tmp;
tmp.i4 = *a;
swap(tmp.i1[0], tmp.i1[3]);
swap(tmp.i1[1], tmp.i1[2]);
*a = tmp.i4;
return *a;
}
/** @brief swap the bytes of a 16-bit signed integer
* @ingroup mat_internal
* @param a pointer to integer to swap
* @return the swapped integer
*/
static mat_int16_t
Mat_int16Swap(mat_int16_t *a)
{
union {
mat_int8_t i1[2];
mat_int16_t i2;
} tmp;
tmp.i2 = *a;
swap(tmp.i1[0], tmp.i1[1]);
*a = tmp.i2;
return *a;
}
/** @brief swap the bytes of a 16-bit unsigned integer
* @ingroup mat_internal
* @param a pointer to integer to swap
* @return the swapped integer
*/
static mat_uint16_t
Mat_uint16Swap(mat_uint16_t *a)
{
union {
mat_uint8_t i1[2];
mat_uint16_t i2;
} tmp;
tmp.i2 = *a;
swap(tmp.i1[0], tmp.i1[1]);
*a = tmp.i2;
return *a;
}
/** @brief swap the bytes of a 4 byte single-precision float
* @ingroup mat_internal
* @param a pointer to integer to swap
* @return the swapped integer
*/
static float
Mat_floatSwap(float *a)
{
union {
char i1[4];
float r4;
} tmp;
tmp.r4 = *a;
swap(tmp.i1[0], tmp.i1[3]);
swap(tmp.i1[1], tmp.i1[2]);
*a = tmp.r4;
return *a;
}
/** @brief swap the bytes of a 4 or 8 byte double-precision float
* @ingroup mat_internal
* @param a pointer to integer to swap
* @return the swapped integer
*/
static double
Mat_doubleSwap(double *a)
{
#ifndef SIZEOF_DOUBLE
#define SIZEOF_DOUBLE 8
#endif
union {
char a[SIZEOF_DOUBLE];
double b;
} tmp;
tmp.b = *a;
#if SIZEOF_DOUBLE == 4
swap(tmp.a[0], tmp.a[3]);
swap(tmp.a[1], tmp.a[2]);
#elif SIZEOF_DOUBLE == 8
swap(tmp.a[0], tmp.a[7]);
swap(tmp.a[1], tmp.a[6]);
swap(tmp.a[2], tmp.a[5]);
swap(tmp.a[3], tmp.a[4]);
#elif SIZEOF_DOUBLE == 16
swap(tmp.a[0], tmp.a[15]);
swap(tmp.a[1], tmp.a[14]);
swap(tmp.a[2], tmp.a[13]);
swap(tmp.a[3], tmp.a[12]);
swap(tmp.a[4], tmp.a[11]);
swap(tmp.a[5], tmp.a[10]);
swap(tmp.a[6], tmp.a[9]);
swap(tmp.a[7], tmp.a[8]);
#endif
*a = tmp.b;
return *a;
}
/* -------------------------------
* ---------- inflate.c
* -------------------------------
*/
/** @file inflate.c
* @brief Functions to inflate data/tags
* @ingroup MAT
*/
#if HAVE_ZLIB
/** @cond mat_devman */
/** @brief Inflate the data until @c nBytes of uncompressed data has been
* inflated
*
* @ingroup mat_internal
* @param mat Pointer to the MAT file
* @param z zlib compression stream
* @param nBytes Number of uncompressed bytes to skip
* @param[out] bytesread Number of bytes read from the file
* @retval 0 on success
*/
static int
InflateSkip(mat_t *mat, z_streamp z, int nBytes, size_t *bytesread)
{
mat_uint8_t comp_buf[READ_BLOCK_SIZE], uncomp_buf[READ_BLOCK_SIZE];
int n, err = MATIO_E_NO_ERROR, cnt = 0;
if ( nBytes < 1 )
return MATIO_E_NO_ERROR;
n = nBytes < READ_BLOCK_SIZE ? nBytes : READ_BLOCK_SIZE;
if ( !z->avail_in ) {
size_t nbytes = fread(comp_buf, 1, n, (FILE *)mat->fp);
if ( 0 == nbytes ) {
return err;
}
if ( NULL != bytesread ) {
*bytesread += nbytes;
}
z->avail_in = (uInt)nbytes;
z->next_in = comp_buf;
}
z->avail_out = n;
z->next_out = uncomp_buf;
err = inflate(z, Z_NO_FLUSH);
if ( err == Z_STREAM_END ) {
return MATIO_E_NO_ERROR;
} else if ( err != Z_OK ) {
Mat_Critical("InflateSkip: inflate returned %s",
zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
return MATIO_E_FILE_FORMAT_VIOLATION;
} else {
err = MATIO_E_NO_ERROR;
}
if ( !z->avail_out ) {
cnt += n;
n = nBytes - cnt;
if ( n > READ_BLOCK_SIZE ) {
n = READ_BLOCK_SIZE;
}
z->avail_out = n;
z->next_out = uncomp_buf;
}
while ( cnt < nBytes ) {
if ( !z->avail_in ) {
size_t nbytes = fread(comp_buf, 1, n, (FILE *)mat->fp);
if ( 0 == nbytes ) {
break;
}
if ( NULL != bytesread ) {
*bytesread += nbytes;
}
z->avail_in = (uInt)nbytes;
z->next_in = comp_buf;
}
err = inflate(z, Z_NO_FLUSH);
if ( err == Z_STREAM_END ) {
err = MATIO_E_NO_ERROR;
break;
} else if ( err != Z_OK ) {
const char *errMsg = zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err);
err = MATIO_E_FILE_FORMAT_VIOLATION;
Mat_Critical("InflateSkip: inflate returned %s", errMsg);
break;
} else {
err = MATIO_E_NO_ERROR;
}
if ( !z->avail_out ) {
cnt += n;
n = nBytes - cnt;
if ( n > READ_BLOCK_SIZE ) {
n = READ_BLOCK_SIZE;
}
z->avail_out = n;
z->next_out = uncomp_buf;
}
}
if ( z->avail_in ) {
const mat_off_t offset = -(mat_off_t)z->avail_in;
(void)fseeko((FILE *)mat->fp, offset, SEEK_CUR);
if ( NULL != bytesread ) {
*bytesread -= z->avail_in;
}
z->avail_in = 0;
}
return err;
}
/** @brief Inflate the data until @c len elements of compressed data with data
* type @c data_type has been inflated
*
* @ingroup mat_internal
* @param mat Pointer to the MAT file
* @param z zlib compression stream
* @param data_type Data type (matio_types enumerations)
* @param len Number of elements of datatype @c data_type to skip
* @retval 0 on success
*/
static int
InflateSkipData(mat_t *mat, z_streamp z, enum matio_types data_type, int len)
{
if ( mat == NULL || z == NULL || len < 1 )
return MATIO_E_BAD_ARGUMENT;
switch ( data_type ) {
case MAT_T_UTF8:
case MAT_T_UTF16:
case MAT_T_UTF32:
return MATIO_E_OPERATION_NOT_SUPPORTED;
default:
break;
}
return InflateSkip(mat, z, (unsigned int)Mat_SizeOf(data_type) * len, NULL);
}
/** @brief Inflates the dimensions tag and the dimensions data
*
* @c buf must hold at least (8+4*rank) bytes where rank is the number of
* dimensions. If the end of the dimensions data is not aligned on an 8-byte
* boundary, this function eats up those bytes and stores then in @c buf.
* @ingroup mat_internal
* @param mat Pointer to the MAT file
* @param z zlib compression stream
* @param buf Pointer to store the dimensions flag and data
* @param nBytes Size of buf in bytes
* @param dims Output buffer to be allocated if (8+4*rank) > nBytes
* @param[out] bytesread Number of bytes read from the file
* @retval 0 on success
*/
static int
InflateRankDims(mat_t *mat, z_streamp z, void *buf, size_t nBytes, mat_uint32_t **dims,
size_t *bytesread)
{
mat_uint32_t tag[2];
mat_uint32_t rank, i;
int err;
size_t nbytes = 0;
if ( buf == NULL )
return MATIO_E_BAD_ARGUMENT;
err = Inflate(mat, z, buf, 8, bytesread);
if ( err ) {
return err;
}
tag[0] = *(mat_uint32_t *)buf;
tag[1] = *((mat_uint32_t *)buf + 1);
if ( mat->byteswap ) {
Mat_uint32Swap(tag);
Mat_uint32Swap(tag + 1);
}
if ( (tag[0] & 0x0000ffff) != MAT_T_INT32 ) {
Mat_Critical("InflateRankDims: Reading dimensions expected type MAT_T_INT32");
return MATIO_E_FILE_FORMAT_VIOLATION;
}
rank = tag[1];
if ( rank % 8 != 0 )
i = 8 - (rank % 8);
else
i = 0;
if ( rank > INT_MAX - i - 2 ) {
Mat_Critical("InflateRankDims: Reading dimensions expected rank in integer range");
return MATIO_E_FILE_FORMAT_VIOLATION;
}
rank += i;
err = Mul(&nbytes, rank + 2, sizeof(mat_uint32_t));
if ( err ) {
Mat_Critical("Integer multiplication overflow");
return err;
}
if ( nbytes <= nBytes ) {
err = Inflate(mat, z, (mat_uint32_t *)buf + 2, rank, bytesread);
} else {
/* Cannot use too small buf, but can allocate output buffer dims */
*dims = (mat_uint32_t *)calloc(rank, sizeof(mat_uint32_t));
if ( NULL != *dims ) {
err = Inflate(mat, z, *dims, rank, bytesread);
} else {
*((mat_uint32_t *)buf + 1) = 0;
Mat_Critical("Error allocating memory for dims");
return MATIO_E_OUT_OF_MEMORY;
}
}
return err;
}
/** @brief Inflates the data
*
* buf must hold at least @c nBytes bytes
* @ingroup mat_internal
* @param mat Pointer to the MAT file
* @param z zlib compression stream
* @param buf Pointer to store the uncompressed data
* @param nBytes Number of uncompressed bytes to inflate
* @param[out] bytesread Number of bytes read from the file
* @retval 0 on success
*/
static int
Inflate(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes, size_t *bytesread)
{
mat_uint8_t comp_buf[4];
int err = MATIO_E_NO_ERROR;
if ( buf == NULL )
return MATIO_E_BAD_ARGUMENT;
if ( !z->avail_in ) {
size_t nbytes = fread(comp_buf, 1, 1, (FILE *)mat->fp);
if ( 0 == nbytes ) {
return err;
}
if ( NULL != bytesread ) {
*bytesread += nbytes;
}
z->avail_in = (uInt)nbytes;
z->next_in = comp_buf;
}
z->avail_out = nBytes;
z->next_out = ZLIB_BYTE_PTR(buf);
err = inflate(z, Z_NO_FLUSH);
if ( err != Z_OK ) {
Mat_Critical("Inflate: inflate returned %s",
zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
return MATIO_E_FILE_FORMAT_VIOLATION;
} else {
err = MATIO_E_NO_ERROR;
}
while ( z->avail_out && !z->avail_in ) {
size_t nbytes = fread(comp_buf, 1, 1, (FILE *)mat->fp);
if ( 0 == nbytes ) {
break;
}
if ( NULL != bytesread ) {
*bytesread += nbytes;
}
z->avail_in = (uInt)nbytes;
z->next_in = comp_buf;
err = inflate(z, Z_NO_FLUSH);
if ( err != Z_OK ) {
Mat_Critical("Inflate: inflate returned %s",
zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));