| 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 |
#include "matio_private.h" |
| 34 |
#include "mat5.h" |
| 35 |
#include <stddef.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <math.h> |
| 40 |
#include <time.h> |
| 41 |
#if defined(_MSC_VER) || defined(__MINGW32__) |
| 42 |
#define strdup _strdup |
| 43 |
#endif |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#define TYPE_FROM_TAG(a) \ |
| 48 |
(((a)&0x000000ff) <= MAT_T_FUNCTION) ? (enum matio_types)((a)&0x000000ff) : MAT_T_UNKNOWN |
| 49 |
|
| 50 |
#define CLASS_FROM_ARRAY_FLAGS(a) \ |
| 51 |
(((a)&0x000000ff) <= MAT_C_OPAQUE) ? ((enum matio_classes)((a)&0x000000ff)) : MAT_C_EMPTY |
| 52 |
|
| 53 |
#define CLASS_TYPE_MASK 0x000000ff |
| 54 |
|
| 55 |
|
| 56 |
#if !defined(MAX_READ_SIZE_WITHOUT_EOF_CHECK) |
| 57 |
|
| 58 |
#define MAX_READ_SIZE_WITHOUT_EOF_CHECK (3) |
| 59 |
#endif |
| 60 |
|
| 61 |
static mat_complex_split_t null_complex_data = {NULL, NULL}; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static int GetTypeBufSize(matvar_t *matvar, size_t *size); |
| 68 |
static int GetStructFieldBufSize(matvar_t *matvar, size_t *size); |
| 69 |
static int GetCellArrayFieldBufSize(matvar_t *matvar, size_t *size); |
| 70 |
static void SetFieldNames(matvar_t *matvar, const char *buf, size_t nfields, |
| 71 |
mat_uint32_t fieldname_length); |
| 72 |
static int ReadSparse(mat_t *mat, const matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v); |
| 73 |
#if HAVE_ZLIB |
| 74 |
static int GetMatrixMaxBufSize(matvar_t *matvar, size_t *size); |
| 75 |
#endif |
| 76 |
static int GetEmptyMatrixMaxBufSize(const char *name, int rank, size_t *size); |
| 77 |
static size_t WriteCharData(mat_t *mat, void *data, size_t N, enum matio_types data_type); |
| 78 |
static size_t ReadNextCell(mat_t *mat, matvar_t *matvar); |
| 79 |
static size_t ReadNextStructField(mat_t *mat, matvar_t *matvar); |
| 80 |
static size_t ReadNextFunctionHandle(mat_t *mat, matvar_t *matvar); |
| 81 |
static int ReadRankDims(mat_t *mat, matvar_t *matvar, enum matio_types data_type, |
| 82 |
mat_uint32_t nbytes, size_t *read_bytes); |
| 83 |
static int WriteType(mat_t *mat, matvar_t *matvar); |
| 84 |
static int WriteCellArrayField(mat_t *mat, matvar_t *matvar); |
| 85 |
static int WriteStructField(mat_t *mat, matvar_t *matvar); |
| 86 |
static int WriteData(mat_t *mat, const void *data, size_t N, enum matio_types data_type); |
| 87 |
static size_t Mat_WriteEmptyVariable5(mat_t *mat, const char *name, int rank, const size_t *dims); |
| 88 |
static int Mat_VarReadNumeric5(mat_t *mat, matvar_t *matvar, void *data, size_t N); |
| 89 |
#if HAVE_ZLIB |
| 90 |
static size_t WriteCompressedCharData(mat_t *mat, z_streamp z, void *data, size_t N, |
| 91 |
enum matio_types data_type); |
| 92 |
static size_t WriteCompressedData(mat_t *mat, z_streamp z, void *data, int N, |
| 93 |
enum matio_types data_type); |
| 94 |
static size_t WriteCompressedTypeArrayFlags(mat_t *mat, matvar_t *matvar, z_streamp z); |
| 95 |
static size_t WriteCompressedType(mat_t *mat, matvar_t *matvar, z_streamp z); |
| 96 |
static size_t WriteCompressedCellArrayField(mat_t *mat, matvar_t *matvar, z_streamp z); |
| 97 |
static size_t WriteCompressedStructField(mat_t *mat, matvar_t *matvar, z_streamp z); |
| 98 |
static size_t Mat_WriteCompressedEmptyVariable5(mat_t *mat, const char *name, int rank, |
| 99 |
const size_t *dims, z_streamp z); |
| 100 |
#endif |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static int |
| 110 |
GetTypeBufSize(matvar_t *matvar, size_t *size) |
| 111 |
{ |
| 112 |
int err; |
| 113 |
size_t nBytes, data_bytes; |
| 114 |
size_t tag_size = 8; |
| 115 |
size_t nelems = 1; |
| 116 |
size_t rank_size; |
| 117 |
|
| 118 |
*size = 0; |
| 119 |
|
| 120 |
err = Mat_MulDims(matvar, &nelems); |
| 121 |
if ( err ) |
| 122 |
return err; |
| 123 |
|
| 124 |
|
| 125 |
err = Mul(&rank_size, matvar->rank, 4); |
| 126 |
if ( err ) |
| 127 |
return err; |
| 128 |
|
| 129 |
if ( matvar->rank % 2 ) |
| 130 |
nBytes = tag_size + 4; |
| 131 |
else |
| 132 |
nBytes = tag_size; |
| 133 |
|
| 134 |
err = Add(&nBytes, nBytes, rank_size); |
| 135 |
if ( err ) |
| 136 |
return err; |
| 137 |
|
| 138 |
switch ( matvar->class_type ) { |
| 139 |
case MAT_C_STRUCT: { |
| 140 |
matvar_t **fields = (matvar_t **)matvar->data; |
| 141 |
size_t nfields = matvar->internal->num_fields; |
| 142 |
size_t maxlen = 0, i, field_buf_size; |
| 143 |
|
| 144 |
for ( i = 0; i < nfields; i++ ) { |
| 145 |
const char *fieldname = matvar->internal->fieldnames[i]; |
| 146 |
if ( NULL != fieldname && strlen(fieldname) > maxlen ) |
| 147 |
maxlen = strlen(fieldname); |
| 148 |
} |
| 149 |
maxlen++; |
| 150 |
while ( nfields * maxlen % 8 != 0 ) |
| 151 |
maxlen++; |
| 152 |
|
| 153 |
err = Mul(&field_buf_size, maxlen, nfields); |
| 154 |
if ( err ) |
| 155 |
return err; |
| 156 |
err = Add(&nBytes, nBytes, tag_size + tag_size); |
| 157 |
if ( err ) |
| 158 |
return err; |
| 159 |
err = Add(&nBytes, nBytes, field_buf_size); |
| 160 |
if ( err ) |
| 161 |
return err; |
| 162 |
|
| 163 |
|
| 164 |
if ( NULL != fields && nfields > 0 ) { |
| 165 |
size_t nelems_x_nfields = 1; |
| 166 |
err = Mul(&nelems_x_nfields, nelems, nfields); |
| 167 |
if ( err ) |
| 168 |
return err; |
| 169 |
|
| 170 |
for ( i = 0; i < nelems_x_nfields; i++ ) { |
| 171 |
err = GetStructFieldBufSize(fields[i], &field_buf_size); |
| 172 |
if ( err ) |
| 173 |
return err; |
| 174 |
err = Add(&nBytes, nBytes, tag_size); |
| 175 |
if ( err ) |
| 176 |
return err; |
| 177 |
err = Add(&nBytes, nBytes, field_buf_size); |
| 178 |
if ( err ) |
| 179 |
return err; |
| 180 |
} |
| 181 |
} |
| 182 |
break; |
| 183 |
} |
| 184 |
case MAT_C_CELL: { |
| 185 |
matvar_t **cells = (matvar_t **)matvar->data; |
| 186 |
|
| 187 |
if ( matvar->nbytes == 0 || matvar->data_size == 0 ) |
| 188 |
break; |
| 189 |
|
| 190 |
nelems = matvar->nbytes / matvar->data_size; |
| 191 |
if ( NULL != cells && nelems > 0 ) { |
| 192 |
size_t i, field_buf_size; |
| 193 |
for ( i = 0; i < nelems; i++ ) { |
| 194 |
err = GetCellArrayFieldBufSize(cells[i], &field_buf_size); |
| 195 |
if ( err ) |
| 196 |
return err; |
| 197 |
err = Add(&nBytes, nBytes, tag_size); |
| 198 |
if ( err ) |
| 199 |
return err; |
| 200 |
err = Add(&nBytes, nBytes, field_buf_size); |
| 201 |
if ( err ) |
| 202 |
return err; |
| 203 |
} |
| 204 |
} |
| 205 |
break; |
| 206 |
} |
| 207 |
case MAT_C_SPARSE: { |
| 208 |
const mat_sparse_t *sparse = (const mat_sparse_t *)matvar->data; |
| 209 |
|
| 210 |
err = Mul(&data_bytes, sparse->nir, sizeof(mat_uint32_t)); |
| 211 |
if ( err ) |
| 212 |
return err; |
| 213 |
if ( data_bytes % 8 ) { |
| 214 |
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8); |
| 215 |
if ( err ) |
| 216 |
return err; |
| 217 |
} |
| 218 |
err = Add(&nBytes, nBytes, tag_size); |
| 219 |
if ( err ) |
| 220 |
return err; |
| 221 |
err = Add(&nBytes, nBytes, data_bytes); |
| 222 |
if ( err ) |
| 223 |
return err; |
| 224 |
|
| 225 |
err = Mul(&data_bytes, sparse->njc, sizeof(mat_uint32_t)); |
| 226 |
if ( err ) |
| 227 |
return err; |
| 228 |
if ( data_bytes % 8 ) { |
| 229 |
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8); |
| 230 |
if ( err ) |
| 231 |
return err; |
| 232 |
} |
| 233 |
err = Add(&nBytes, nBytes, tag_size); |
| 234 |
if ( err ) |
| 235 |
return err; |
| 236 |
err = Add(&nBytes, nBytes, data_bytes); |
| 237 |
if ( err ) |
| 238 |
return err; |
| 239 |
|
| 240 |
err = Mul(&data_bytes, sparse->ndata, Mat_SizeOf(matvar->data_type)); |
| 241 |
if ( err ) |
| 242 |
return err; |
| 243 |
if ( data_bytes % 8 ) { |
| 244 |
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8); |
| 245 |
if ( err ) |
| 246 |
return err; |
| 247 |
} |
| 248 |
err = Add(&nBytes, nBytes, tag_size); |
| 249 |
if ( err ) |
| 250 |
return err; |
| 251 |
err = Add(&nBytes, nBytes, data_bytes); |
| 252 |
if ( err ) |
| 253 |
return err; |
| 254 |
|
| 255 |
if ( matvar->isComplex ) { |
| 256 |
err = Add(&nBytes, nBytes, tag_size); |
| 257 |
if ( err ) |
| 258 |
return err; |
| 259 |
err = Add(&nBytes, nBytes, data_bytes); |
| 260 |
if ( err ) |
| 261 |
return err; |
| 262 |
} |
| 263 |
|
| 264 |
break; |
| 265 |
} |
| 266 |
case MAT_C_CHAR: |
| 267 |
if ( MAT_T_INT8 == matvar->data_type ) |
| 268 |
err = Mul(&data_bytes, nelems, Mat_SizeOf(MAT_T_UINT16)); |
| 269 |
else |
| 270 |
err = Mul(&data_bytes, nelems, Mat_SizeOf(matvar->data_type)); |
| 271 |
if ( err ) |
| 272 |
return err; |
| 273 |
if ( data_bytes % 8 ) { |
| 274 |
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8); |
| 275 |
if ( err ) |
| 276 |
return err; |
| 277 |
} |
| 278 |
|
| 279 |
err = Add(&nBytes, nBytes, tag_size); |
| 280 |
if ( err ) |
| 281 |
return err; |
| 282 |
err = Add(&nBytes, nBytes, data_bytes); |
| 283 |
if ( err ) |
| 284 |
return err; |
| 285 |
|
| 286 |
if ( matvar->isComplex ) { |
| 287 |
err = Add(&nBytes, nBytes, tag_size); |
| 288 |
if ( err ) |
| 289 |
return err; |
| 290 |
err = Add(&nBytes, nBytes, data_bytes); |
| 291 |
if ( err ) |
| 292 |
return err; |
| 293 |
} |
| 294 |
|
| 295 |
break; |
| 296 |
default: |
| 297 |
err = Mul(&data_bytes, nelems, Mat_SizeOf(matvar->data_type)); |
| 298 |
if ( err ) |
| 299 |
return err; |
| 300 |
if ( data_bytes % 8 ) { |
| 301 |
err = Add(&data_bytes, data_bytes, 8 - data_bytes % 8); |
| 302 |
if ( err ) |
| 303 |
return err; |
| 304 |
} |
| 305 |
|
| 306 |
err = Add(&nBytes, nBytes, tag_size); |
| 307 |
if ( err ) |
| 308 |
return err; |
| 309 |
err = Add(&nBytes, nBytes, data_bytes); |
| 310 |
if ( err ) |
| 311 |
return err; |
| 312 |
|
| 313 |
if ( matvar->isComplex ) { |
| 314 |
err = Add(&nBytes, nBytes, tag_size); |
| 315 |
if ( err ) |
| 316 |
return err; |
| 317 |
err = Add(&nBytes, nBytes, data_bytes); |
| 318 |
if ( err ) |
| 319 |
return err; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
*size = nBytes; |
| 324 |
return MATIO_E_NO_ERROR; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
static int |
| 335 |
GetStructFieldBufSize(matvar_t *matvar, size_t *size) |
| 336 |
{ |
| 337 |
int err; |
| 338 |
size_t nBytes = 0, type_buf_size; |
| 339 |
size_t tag_size = 8, array_flags_size = 8; |
| 340 |
|
| 341 |
*size = 0; |
| 342 |
|
| 343 |
if ( matvar == NULL ) |
| 344 |
return GetEmptyMatrixMaxBufSize(NULL, 2, size); |
| 345 |
|
| 346 |
|
| 347 |
nBytes += tag_size + array_flags_size; |
| 348 |
|
| 349 |
|
| 350 |
nBytes += tag_size; |
| 351 |
|
| 352 |
err = GetTypeBufSize(matvar, &type_buf_size); |
| 353 |
if ( err ) |
| 354 |
return err; |
| 355 |
err = Add(&nBytes, nBytes, type_buf_size); |
| 356 |
if ( err ) |
| 357 |
return err; |
| 358 |
|
| 359 |
*size = nBytes; |
| 360 |
return MATIO_E_NO_ERROR; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
static int |
| 371 |
GetCellArrayFieldBufSize(matvar_t *matvar, size_t *size) |
| 372 |
{ |
| 373 |
int err; |
| 374 |
size_t nBytes = 0, type_buf_size; |
| 375 |
size_t tag_size = 8, array_flags_size = 8; |
| 376 |
|
| 377 |
*size = 0; |
| 378 |
|
| 379 |
if ( matvar == NULL ) |
| 380 |
return MATIO_E_BAD_ARGUMENT; |
| 381 |
|
| 382 |
|
| 383 |
nBytes += tag_size + array_flags_size; |
| 384 |
|
| 385 |
|
| 386 |
nBytes += tag_size; |
| 387 |
|
| 388 |
err = GetTypeBufSize(matvar, &type_buf_size); |
| 389 |
if ( err ) |
| 390 |
return err; |
| 391 |
err = Add(&nBytes, nBytes, type_buf_size); |
| 392 |
if ( err ) |
| 393 |
return err; |
| 394 |
|
| 395 |
*size = nBytes; |
| 396 |
return MATIO_E_NO_ERROR; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
static int |
| 408 |
GetEmptyMatrixMaxBufSize(const char *name, int rank, size_t *size) |
| 409 |
{ |
| 410 |
int err = 0; |
| 411 |
size_t nBytes = 0, len, rank_size; |
| 412 |
size_t tag_size = 8, array_flags_size = 8; |
| 413 |
|
| 414 |
|
| 415 |
nBytes += tag_size + array_flags_size; |
| 416 |
|
| 417 |
|
| 418 |
if ( NULL != name ) |
| 419 |
len = strlen(name); |
| 420 |
else |
| 421 |
len = 4; |
| 422 |
|
| 423 |
if ( len <= 4 ) { |
| 424 |
nBytes += tag_size; |
| 425 |
} else { |
| 426 |
nBytes += tag_size; |
| 427 |
if ( len % 8 ) { |
| 428 |
err = Add(&len, len, 8 - len % 8); |
| 429 |
if ( err ) |
| 430 |
return err; |
| 431 |
} |
| 432 |
|
| 433 |
err = Add(&nBytes, nBytes, len); |
| 434 |
if ( err ) |
| 435 |
return err; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
err = Mul(&rank_size, rank, 4); |
| 440 |
if ( err ) |
| 441 |
return err; |
| 442 |
if ( rank % 2 ) |
| 443 |
err = Add(&nBytes, nBytes, tag_size + 4); |
| 444 |
else |
| 445 |
err = Add(&nBytes, nBytes, tag_size); |
| 446 |
if ( err ) |
| 447 |
return err; |
| 448 |
|
| 449 |
err = Add(&nBytes, nBytes, rank_size); |
| 450 |
if ( err ) |
| 451 |
return err; |
| 452 |
|
| 453 |
err = Add(&nBytes, nBytes, tag_size); |
| 454 |
if ( err ) |
| 455 |
return err; |
| 456 |
|
| 457 |
*size = nBytes; |
| 458 |
return MATIO_E_NO_ERROR; |
| 459 |
} |
| 460 |
|
| 461 |
static void |
| 462 |
SetFieldNames(matvar_t *matvar, const char *buf, size_t nfields, mat_uint32_t fieldname_length) |
| 463 |
{ |
| 464 |
matvar->internal->num_fields = nfields; |
| 465 |
matvar->internal->fieldnames = (char **)calloc(nfields, sizeof(*matvar->internal->fieldnames)); |
| 466 |
if ( NULL != matvar->internal->fieldnames ) { |
| 467 |
size_t i; |
| 468 |
for ( i = 0; i < nfields; i++ ) { |
| 469 |
matvar->internal->fieldnames[i] = (char *)malloc(fieldname_length); |
| 470 |
if ( NULL != matvar->internal->fieldnames[i] ) { |
| 471 |
memcpy(matvar->internal->fieldnames[i], buf + i * fieldname_length, |
| 472 |
fieldname_length); |
| 473 |
matvar->internal->fieldnames[i][fieldname_length - 1] = '\0'; |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
static int |
| 480 |
ReadSparse(mat_t *mat, const matvar_t *matvar, mat_uint32_t *n, mat_uint32_t **v) |
| 481 |
{ |
| 482 |
int err; |
| 483 |
int data_in_tag = 0; |
| 484 |
enum matio_types packed_type; |
| 485 |
mat_uint32_t N = 0; |
| 486 |
|
| 487 |
if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 488 |
#if HAVE_ZLIB |
| 489 |
size_t readBytes; |
| 490 |
mat_uint32_t tag[2] = {0, 0}; |
| 491 |
matvar->internal->z->avail_in = 0; |
| 492 |
err = Inflate(mat, matvar->internal->z, tag, 4, &readBytes); |
| 493 |
if ( err ) { |
| 494 |
return err; |
| 495 |
} |
| 496 |
if ( mat->byteswap ) |
| 497 |
(void)Mat_uint32Swap(tag); |
| 498 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 499 |
if ( tag[0] & 0xffff0000 ) { |
| 500 |
data_in_tag = 1; |
| 501 |
N = (tag[0] & 0xffff0000) >> 16; |
| 502 |
} else { |
| 503 |
data_in_tag = 0; |
| 504 |
err = ReadCompressedUInt32Data(mat, matvar->internal->z, &N, MAT_T_UINT32, 1); |
| 505 |
} |
| 506 |
#endif |
| 507 |
} else { |
| 508 |
mat_uint32_t tag[2] = {0, 0}; |
| 509 |
err = Read(tag, 4, 1, (FILE *)mat->fp, NULL); |
| 510 |
if ( err ) { |
| 511 |
return err; |
| 512 |
} |
| 513 |
if ( mat->byteswap ) |
| 514 |
(void)Mat_uint32Swap(tag); |
| 515 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 516 |
if ( tag[0] & 0xffff0000 ) { |
| 517 |
data_in_tag = 1; |
| 518 |
N = (tag[0] & 0xffff0000) >> 16; |
| 519 |
} else { |
| 520 |
data_in_tag = 0; |
| 521 |
err = Read(&N, 4, 1, (FILE *)mat->fp, NULL); |
| 522 |
if ( err ) { |
| 523 |
return err; |
| 524 |
} |
| 525 |
if ( mat->byteswap ) |
| 526 |
(void)Mat_uint32Swap(&N); |
| 527 |
} |
| 528 |
} |
| 529 |
if ( 0 == N ) |
| 530 |
return MATIO_E_NO_ERROR; |
| 531 |
*n = N / 4; |
| 532 |
*v = (mat_uint32_t *)calloc(N, 1); |
| 533 |
if ( NULL == *v ) { |
| 534 |
return MATIO_E_OUT_OF_MEMORY; |
| 535 |
} |
| 536 |
if ( matvar->compression == MAT_COMPRESSION_NONE ) { |
| 537 |
size_t nBytes; |
| 538 |
err = ReadUInt32Data(mat, *v, packed_type, *n); |
| 539 |
if ( err ) { |
| 540 |
return err; |
| 541 |
} |
| 542 |
err = Mul(&nBytes, (size_t)*n, Mat_SizeOf(packed_type)); |
| 543 |
if ( err ) { |
| 544 |
return err; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
if ( data_in_tag ) |
| 551 |
nBytes += 4; |
| 552 |
if ( (nBytes % 8) != 0 ) |
| 553 |
(void)fseeko((FILE *)mat->fp, 8 - (nBytes % 8), SEEK_CUR); |
| 554 |
#if HAVE_ZLIB |
| 555 |
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 556 |
size_t nBytes; |
| 557 |
err = ReadCompressedUInt32Data(mat, matvar->internal->z, *v, packed_type, *n); |
| 558 |
if ( err ) { |
| 559 |
return err; |
| 560 |
} |
| 561 |
err = Mul(&nBytes, (size_t)*n, Mat_SizeOf(packed_type)); |
| 562 |
if ( err ) { |
| 563 |
return err; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
if ( data_in_tag ) |
| 570 |
nBytes += 4; |
| 571 |
if ( (nBytes % 8) != 0 ) |
| 572 |
err = InflateSkip(mat, matvar->internal->z, 8 - (nBytes % 8), NULL); |
| 573 |
#endif |
| 574 |
} |
| 575 |
|
| 576 |
return err; |
| 577 |
} |
| 578 |
|
| 579 |
#if HAVE_ZLIB |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
static int |
| 588 |
GetMatrixMaxBufSize(matvar_t *matvar, size_t *size) |
| 589 |
{ |
| 590 |
int err = MATIO_E_NO_ERROR; |
| 591 |
size_t nBytes = 0, len, type_buf_size; |
| 592 |
size_t tag_size = 8, array_flags_size = 8; |
| 593 |
|
| 594 |
if ( matvar == NULL ) |
| 595 |
return MATIO_E_BAD_ARGUMENT; |
| 596 |
|
| 597 |
|
| 598 |
nBytes += tag_size + array_flags_size; |
| 599 |
|
| 600 |
|
| 601 |
if ( NULL != matvar->name ) |
| 602 |
len = strlen(matvar->name); |
| 603 |
else |
| 604 |
len = 4; |
| 605 |
|
| 606 |
if ( len <= 4 ) { |
| 607 |
nBytes += tag_size; |
| 608 |
} else { |
| 609 |
nBytes += tag_size; |
| 610 |
if ( len % 8 ) { |
| 611 |
err = Add(&len, len, 8 - len % 8); |
| 612 |
if ( err ) |
| 613 |
return err; |
| 614 |
} |
| 615 |
|
| 616 |
err = Add(&nBytes, nBytes, len); |
| 617 |
if ( err ) |
| 618 |
return err; |
| 619 |
} |
| 620 |
|
| 621 |
err = GetTypeBufSize(matvar, &type_buf_size); |
| 622 |
if ( err ) |
| 623 |
return err; |
| 624 |
err = Add(&nBytes, nBytes, type_buf_size); |
| 625 |
if ( err ) |
| 626 |
return err; |
| 627 |
|
| 628 |
*size = nBytes; |
| 629 |
return MATIO_E_NO_ERROR; |
| 630 |
} |
| 631 |
#endif |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
mat_t * |
| 650 |
Mat_Create5(const char *matname, const char *hdr_str) |
| 651 |
{ |
| 652 |
FILE *fp; |
| 653 |
mat_int16_t endian = 0, version; |
| 654 |
mat_t *mat = NULL; |
| 655 |
size_t err; |
| 656 |
time_t t; |
| 657 |
|
| 658 |
#if defined(_WIN32) |
| 659 |
wchar_t *wname = utf82u(matname); |
| 660 |
if ( NULL != wname ) { |
| 661 |
fp = _wfopen(wname, L"w+b"); |
| 662 |
free(wname); |
| 663 |
} else { |
| 664 |
fp = NULL; |
| 665 |
} |
| 666 |
#else |
| 667 |
fp = fopen(matname, "w+b"); |
| 668 |
#endif |
| 669 |
if ( !fp ) |
| 670 |
return NULL; |
| 671 |
|
| 672 |
mat = (mat_t *)malloc(sizeof(*mat)); |
| 673 |
if ( mat == NULL ) { |
| 674 |
fclose(fp); |
| 675 |
return NULL; |
| 676 |
} |
| 677 |
|
| 678 |
mat->fp = NULL; |
| 679 |
mat->header = NULL; |
| 680 |
mat->subsys_offset = NULL; |
| 681 |
mat->filename = NULL; |
| 682 |
mat->version = 0; |
| 683 |
mat->byteswap = 0; |
| 684 |
mat->mode = 0; |
| 685 |
mat->bof = 128; |
| 686 |
mat->next_index = 0; |
| 687 |
mat->num_datasets = 0; |
| 688 |
#if defined(MAT73) && MAT73 |
| 689 |
mat->refs_id = -1; |
| 690 |
#endif |
| 691 |
mat->dir = NULL; |
| 692 |
|
| 693 |
t = time(NULL); |
| 694 |
mat->fp = fp; |
| 695 |
mat->filename = strdup(matname); |
| 696 |
mat->mode = MAT_ACC_RDWR; |
| 697 |
mat->byteswap = 0; |
| 698 |
mat->header = (char *)malloc(128 * sizeof(char)); |
| 699 |
mat->subsys_offset = (char *)malloc(8 * sizeof(char)); |
| 700 |
memset(mat->header, ' ', 128); |
| 701 |
if ( hdr_str == NULL ) { |
| 702 |
err = mat_snprintf(mat->header, 116, |
| 703 |
"MATLAB 5.0 MAT-file, Platform: %s, " |
| 704 |
"Created by: libmatio v%d.%d.%d on %s", |
| 705 |
MATIO_PLATFORM, MATIO_MAJOR_VERSION, MATIO_MINOR_VERSION, |
| 706 |
MATIO_RELEASE_LEVEL, ctime(&t)); |
| 707 |
} else { |
| 708 |
err = mat_snprintf(mat->header, 116, "%s", hdr_str); |
| 709 |
} |
| 710 |
if ( err >= 116 ) |
| 711 |
mat->header[115] = '\0'; |
| 712 |
memset(mat->subsys_offset, ' ', 8); |
| 713 |
mat->version = 0x0100; |
| 714 |
endian = 0x4d49; |
| 715 |
|
| 716 |
version = 0x0100; |
| 717 |
|
| 718 |
fwrite(mat->header, 1, 116, (FILE *)mat->fp); |
| 719 |
fwrite(mat->subsys_offset, 1, 8, (FILE *)mat->fp); |
| 720 |
fwrite(&version, 2, 1, (FILE *)mat->fp); |
| 721 |
fwrite(&endian, 2, 1, (FILE *)mat->fp); |
| 722 |
|
| 723 |
return mat; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
static size_t |
| 743 |
WriteCharData(mat_t *mat, void *data, size_t N, enum matio_types data_type) |
| 744 |
{ |
| 745 |
mat_uint32_t nBytes = 0; |
| 746 |
size_t nbytes = 0, i; |
| 747 |
size_t byteswritten = 0; |
| 748 |
const mat_uint8_t pad1 = 0; |
| 749 |
int err; |
| 750 |
|
| 751 |
switch ( data_type ) { |
| 752 |
case MAT_T_UINT8: |
| 753 |
case MAT_T_UINT16: |
| 754 |
case MAT_T_UTF8: |
| 755 |
case MAT_T_UTF16: { |
| 756 |
data_type = MAT_T_UINT8 == data_type ? MAT_T_UTF8 : data_type; |
| 757 |
err = Mul(&nbytes, N, Mat_SizeOf(data_type)); |
| 758 |
if ( err ) { |
| 759 |
return 0; |
| 760 |
} |
| 761 |
nBytes = (mat_uint32_t)nbytes; |
| 762 |
fwrite(&data_type, 4, 1, (FILE *)mat->fp); |
| 763 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 764 |
if ( NULL != data && N > 0 ) |
| 765 |
fwrite(data, 1, nbytes, (FILE *)mat->fp); |
| 766 |
if ( nBytes % 8 ) { |
| 767 |
for ( i = nbytes % 8; i < 8; i++ ) |
| 768 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 769 |
} |
| 770 |
break; |
| 771 |
} |
| 772 |
case MAT_T_INT8: { |
| 773 |
mat_int8_t *ptr; |
| 774 |
mat_uint16_t c; |
| 775 |
|
| 776 |
|
| 777 |
data_type = MAT_T_UINT16; |
| 778 |
err = Mul(&nbytes, N, Mat_SizeOf(data_type)); |
| 779 |
if ( err ) { |
| 780 |
return 0; |
| 781 |
} |
| 782 |
nBytes = (mat_uint32_t)nbytes; |
| 783 |
fwrite(&data_type, 4, 1, (FILE *)mat->fp); |
| 784 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 785 |
ptr = (mat_int8_t *)data; |
| 786 |
if ( NULL == data ) |
| 787 |
break; |
| 788 |
for ( i = 0; i < N; i++ ) { |
| 789 |
c = (mat_uint16_t) * (char *)ptr; |
| 790 |
fwrite(&c, 2, 1, (FILE *)mat->fp); |
| 791 |
ptr++; |
| 792 |
} |
| 793 |
if ( nbytes % 8 ) |
| 794 |
for ( i = nbytes % 8; i < 8; i++ ) |
| 795 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 796 |
break; |
| 797 |
} |
| 798 |
case MAT_T_UNKNOWN: { |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
data_type = MAT_T_UINT16; |
| 803 |
err = Mul(&nbytes, N, Mat_SizeOf(data_type)); |
| 804 |
if ( err ) { |
| 805 |
return 0; |
| 806 |
} |
| 807 |
nBytes = (mat_uint32_t)nbytes; |
| 808 |
fwrite(&data_type, 4, 1, (FILE *)mat->fp); |
| 809 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 810 |
break; |
| 811 |
} |
| 812 |
default: |
| 813 |
nbytes = 0; |
| 814 |
break; |
| 815 |
} |
| 816 |
byteswritten += nbytes; |
| 817 |
return byteswritten; |
| 818 |
} |
| 819 |
|
| 820 |
#if HAVE_ZLIB |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
static size_t |
| 837 |
WriteCompressedCharData(mat_t *mat, z_streamp z, void *data, size_t N, enum matio_types data_type) |
| 838 |
{ |
| 839 |
size_t data_size, byteswritten = 0, nbytes = 0; |
| 840 |
mat_uint32_t data_tag[2] = {0, 0}; |
| 841 |
int buf_size = 1024; |
| 842 |
int err; |
| 843 |
mat_uint8_t buf[1024], pad[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 844 |
|
| 845 |
if ( mat == NULL || mat->fp == NULL ) |
| 846 |
return 0; |
| 847 |
|
| 848 |
if ( data_type == MAT_T_UNKNOWN ) { |
| 849 |
data_size = Mat_SizeOf(MAT_T_UINT16); |
| 850 |
} else { |
| 851 |
data_size = Mat_SizeOf(data_type); |
| 852 |
} |
| 853 |
|
| 854 |
err = Mul(&nbytes, N, data_size); |
| 855 |
if ( err ) { |
| 856 |
return byteswritten; |
| 857 |
} |
| 858 |
|
| 859 |
switch ( data_type ) { |
| 860 |
case MAT_T_UINT8: |
| 861 |
case MAT_T_UINT16: |
| 862 |
case MAT_T_UTF8: |
| 863 |
case MAT_T_UTF16: |
| 864 |
data_tag[0] = MAT_T_UINT8 == data_type ? MAT_T_UTF8 : data_type; |
| 865 |
data_tag[1] = (mat_uint32_t)nbytes; |
| 866 |
z->next_in = ZLIB_BYTE_PTR(data_tag); |
| 867 |
z->avail_in = 8; |
| 868 |
do { |
| 869 |
z->next_out = buf; |
| 870 |
z->avail_out = buf_size; |
| 871 |
deflate(z, Z_NO_FLUSH); |
| 872 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 873 |
} while ( z->avail_out == 0 ); |
| 874 |
|
| 875 |
|
| 876 |
if ( NULL == data || N < 1 ) |
| 877 |
break; |
| 878 |
|
| 879 |
z->next_in = (Bytef *)data; |
| 880 |
z->avail_in = (mat_uint32_t)nbytes; |
| 881 |
do { |
| 882 |
z->next_out = buf; |
| 883 |
z->avail_out = buf_size; |
| 884 |
deflate(z, Z_NO_FLUSH); |
| 885 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 886 |
} while ( z->avail_out == 0 ); |
| 887 |
|
| 888 |
if ( nbytes % 8 ) { |
| 889 |
z->next_in = pad; |
| 890 |
z->avail_in = 8 - (nbytes % 8); |
| 891 |
do { |
| 892 |
z->next_out = buf; |
| 893 |
z->avail_out = buf_size; |
| 894 |
deflate(z, Z_NO_FLUSH); |
| 895 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 896 |
} while ( z->avail_out == 0 ); |
| 897 |
} |
| 898 |
break; |
| 899 |
case MAT_T_INT8: { |
| 900 |
mat_uint8_t *ptr; |
| 901 |
mat_uint16_t c; |
| 902 |
int i; |
| 903 |
|
| 904 |
|
| 905 |
data_size = 2; |
| 906 |
data_tag[0] = MAT_T_UINT16; |
| 907 |
data_tag[1] = N * data_size; |
| 908 |
z->next_in = ZLIB_BYTE_PTR(data_tag); |
| 909 |
z->avail_in = 8; |
| 910 |
do { |
| 911 |
z->next_out = buf; |
| 912 |
z->avail_out = buf_size; |
| 913 |
deflate(z, Z_NO_FLUSH); |
| 914 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 915 |
} while ( z->avail_out == 0 ); |
| 916 |
|
| 917 |
|
| 918 |
if ( NULL == data || N < 1 ) |
| 919 |
break; |
| 920 |
|
| 921 |
z->next_in = (Bytef *)data; |
| 922 |
z->avail_in = data_size * N; |
| 923 |
ptr = (mat_uint8_t *)data; |
| 924 |
for ( i = 0; i < N; i++ ) { |
| 925 |
c = (mat_uint16_t) * (char *)ptr; |
| 926 |
z->next_in = ZLIB_BYTE_PTR(&c); |
| 927 |
z->avail_in = 2; |
| 928 |
do { |
| 929 |
z->next_out = buf; |
| 930 |
z->avail_out = buf_size; |
| 931 |
deflate(z, Z_NO_FLUSH); |
| 932 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 933 |
} while ( z->avail_out == 0 ); |
| 934 |
ptr++; |
| 935 |
} |
| 936 |
|
| 937 |
if ( N * data_size % 8 ) { |
| 938 |
z->next_in = pad; |
| 939 |
z->avail_in = 8 - (N * data_size % 8); |
| 940 |
do { |
| 941 |
z->next_out = buf; |
| 942 |
z->avail_out = buf_size; |
| 943 |
deflate(z, Z_NO_FLUSH); |
| 944 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 945 |
} while ( z->avail_out == 0 ); |
| 946 |
} |
| 947 |
break; |
| 948 |
} |
| 949 |
case MAT_T_UNKNOWN: |
| 950 |
|
| 951 |
data_tag[0] = MAT_T_UINT16; |
| 952 |
data_tag[1] = (mat_uint32_t)nbytes; |
| 953 |
z->next_in = ZLIB_BYTE_PTR(data_tag); |
| 954 |
z->avail_in = 8; |
| 955 |
do { |
| 956 |
z->next_out = buf; |
| 957 |
z->avail_out = buf_size; |
| 958 |
deflate(z, Z_NO_FLUSH); |
| 959 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 960 |
} while ( z->avail_out == 0 ); |
| 961 |
break; |
| 962 |
default: |
| 963 |
break; |
| 964 |
} |
| 965 |
|
| 966 |
return byteswritten; |
| 967 |
} |
| 968 |
#endif |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
static int |
| 979 |
WriteData(mat_t *mat, const void *data, size_t N, enum matio_types data_type) |
| 980 |
{ |
| 981 |
int nBytes = 0, data_size; |
| 982 |
|
| 983 |
if ( mat == NULL || mat->fp == NULL ) |
| 984 |
return 0; |
| 985 |
|
| 986 |
data_size = Mat_SizeOf(data_type); |
| 987 |
nBytes = N * data_size; |
| 988 |
fwrite(&data_type, 4, 1, (FILE *)mat->fp); |
| 989 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 990 |
|
| 991 |
if ( data != NULL && N > 0 ) |
| 992 |
fwrite(data, data_size, N, (FILE *)mat->fp); |
| 993 |
|
| 994 |
return nBytes; |
| 995 |
} |
| 996 |
|
| 997 |
#if HAVE_ZLIB |
| 998 |
|
| 999 |
static size_t |
| 1000 |
WriteCompressedData(mat_t *mat, z_streamp z, void *data, int N, enum matio_types data_type) |
| 1001 |
{ |
| 1002 |
int nBytes = 0, data_size, data_tag[2], byteswritten = 0; |
| 1003 |
int buf_size = 1024; |
| 1004 |
mat_uint8_t buf[1024], pad[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 1005 |
|
| 1006 |
if ( mat == NULL || mat->fp == NULL ) |
| 1007 |
return 0; |
| 1008 |
|
| 1009 |
data_size = Mat_SizeOf(data_type); |
| 1010 |
data_tag[0] = data_type; |
| 1011 |
data_tag[1] = data_size * N; |
| 1012 |
z->next_in = ZLIB_BYTE_PTR(data_tag); |
| 1013 |
z->avail_in = 8; |
| 1014 |
do { |
| 1015 |
z->next_out = buf; |
| 1016 |
z->avail_out = buf_size; |
| 1017 |
deflate(z, Z_NO_FLUSH); |
| 1018 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 1019 |
} while ( z->avail_out == 0 ); |
| 1020 |
|
| 1021 |
|
| 1022 |
if ( NULL == data || N < 1 ) |
| 1023 |
return byteswritten; |
| 1024 |
|
| 1025 |
z->next_in = (Bytef *)data; |
| 1026 |
z->avail_in = N * data_size; |
| 1027 |
do { |
| 1028 |
z->next_out = buf; |
| 1029 |
z->avail_out = buf_size; |
| 1030 |
deflate(z, Z_NO_FLUSH); |
| 1031 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 1032 |
} while ( z->avail_out == 0 ); |
| 1033 |
|
| 1034 |
if ( N * data_size % 8 ) { |
| 1035 |
z->next_in = pad; |
| 1036 |
z->avail_in = 8 - (N * data_size % 8); |
| 1037 |
do { |
| 1038 |
z->next_out = buf; |
| 1039 |
z->avail_out = buf_size; |
| 1040 |
deflate(z, Z_NO_FLUSH); |
| 1041 |
byteswritten += fwrite(buf, 1, buf_size - z->avail_out, (FILE *)mat->fp); |
| 1042 |
} while ( z->avail_out == 0 ); |
| 1043 |
} |
| 1044 |
nBytes = byteswritten; |
| 1045 |
return nBytes; |
| 1046 |
} |
| 1047 |
#endif |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
static size_t |
| 1057 |
ReadNextCell(mat_t *mat, matvar_t *matvar) |
| 1058 |
{ |
| 1059 |
size_t bytesread = 0; |
| 1060 |
int err; |
| 1061 |
matvar_t **cells = NULL; |
| 1062 |
size_t nelems = 1; |
| 1063 |
|
| 1064 |
err = Mat_MulDims(matvar, &nelems); |
| 1065 |
if ( err ) { |
| 1066 |
Mat_Critical("Integer multiplication overflow"); |
| 1067 |
return bytesread; |
| 1068 |
} |
| 1069 |
matvar->data_size = sizeof(matvar_t *); |
| 1070 |
err = Mul(&matvar->nbytes, nelems, matvar->data_size); |
| 1071 |
if ( err ) { |
| 1072 |
Mat_Critical("Integer multiplication overflow"); |
| 1073 |
return bytesread; |
| 1074 |
} |
| 1075 |
|
| 1076 |
matvar->data = calloc(nelems, matvar->data_size); |
| 1077 |
if ( NULL == matvar->data ) { |
| 1078 |
if ( NULL != matvar->name ) |
| 1079 |
Mat_Critical("Couldn't allocate memory for %s->data", matvar->name); |
| 1080 |
return bytesread; |
| 1081 |
} |
| 1082 |
cells = (matvar_t **)matvar->data; |
| 1083 |
|
| 1084 |
if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 1085 |
#if HAVE_ZLIB |
| 1086 |
size_t i; |
| 1087 |
mat_uint32_t uncomp_buf[16]; |
| 1088 |
mat_uint32_t nBytes; |
| 1089 |
mat_uint32_t array_flags; |
| 1090 |
|
| 1091 |
memset(uncomp_buf, 0, sizeof(uncomp_buf)); |
| 1092 |
for ( i = 0; i < nelems; i++ ) { |
| 1093 |
cells[i] = Mat_VarCalloc(); |
| 1094 |
if ( NULL == cells[i] ) { |
| 1095 |
Mat_Critical("Couldn't allocate memory for cell %zu", i); |
| 1096 |
continue; |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
uncomp_buf[0] = 0; |
| 1101 |
uncomp_buf[1] = 0; |
| 1102 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 8, &bytesread); |
| 1103 |
if ( err ) { |
| 1104 |
Mat_VarFree(cells[i]); |
| 1105 |
cells[i] = NULL; |
| 1106 |
break; |
| 1107 |
} |
| 1108 |
if ( mat->byteswap ) { |
| 1109 |
(void)Mat_uint32Swap(uncomp_buf); |
| 1110 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 1111 |
} |
| 1112 |
nBytes = uncomp_buf[1]; |
| 1113 |
if ( 0 == nBytes ) { |
| 1114 |
|
| 1115 |
free(cells[i]->internal); |
| 1116 |
cells[i]->internal = NULL; |
| 1117 |
continue; |
| 1118 |
} else if ( uncomp_buf[0] != MAT_T_MATRIX ) { |
| 1119 |
Mat_VarFree(cells[i]); |
| 1120 |
cells[i] = NULL; |
| 1121 |
Mat_Critical("cells[%zu], Uncompressed type not MAT_T_MATRIX", i); |
| 1122 |
break; |
| 1123 |
} |
| 1124 |
cells[i]->compression = MAT_COMPRESSION_ZLIB; |
| 1125 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 16, &bytesread); |
| 1126 |
if ( err ) { |
| 1127 |
Mat_VarFree(cells[i]); |
| 1128 |
cells[i] = NULL; |
| 1129 |
break; |
| 1130 |
} |
| 1131 |
nBytes -= 16; |
| 1132 |
if ( mat->byteswap ) { |
| 1133 |
(void)Mat_uint32Swap(uncomp_buf); |
| 1134 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 1135 |
(void)Mat_uint32Swap(uncomp_buf + 2); |
| 1136 |
(void)Mat_uint32Swap(uncomp_buf + 3); |
| 1137 |
} |
| 1138 |
|
| 1139 |
if ( uncomp_buf[0] == MAT_T_UINT32 ) { |
| 1140 |
array_flags = uncomp_buf[2]; |
| 1141 |
cells[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags); |
| 1142 |
cells[i]->isComplex = (array_flags & MAT_F_COMPLEX); |
| 1143 |
cells[i]->isGlobal = (array_flags & MAT_F_GLOBAL); |
| 1144 |
cells[i]->isLogical = (array_flags & MAT_F_LOGICAL); |
| 1145 |
if ( cells[i]->class_type == MAT_C_SPARSE ) { |
| 1146 |
|
| 1147 |
cells[i]->nbytes = uncomp_buf[3]; |
| 1148 |
} |
| 1149 |
} else { |
| 1150 |
Mat_Critical("Expected MAT_T_UINT32 for array tags, got %d", uncomp_buf[0]); |
| 1151 |
InflateSkip(mat, matvar->internal->z, nBytes, &bytesread); |
| 1152 |
} |
| 1153 |
if ( cells[i]->class_type != MAT_C_OPAQUE ) { |
| 1154 |
mat_uint32_t *dims = NULL; |
| 1155 |
int do_clean = 0; |
| 1156 |
err = InflateRankDims(mat, matvar->internal->z, uncomp_buf, sizeof(uncomp_buf), |
| 1157 |
&dims, &bytesread); |
| 1158 |
if ( NULL == dims ) { |
| 1159 |
dims = uncomp_buf + 2; |
| 1160 |
} else { |
| 1161 |
do_clean = 1; |
| 1162 |
} |
| 1163 |
if ( err ) { |
| 1164 |
if ( do_clean ) { |
| 1165 |
free(dims); |
| 1166 |
} |
| 1167 |
Mat_VarFree(cells[i]); |
| 1168 |
cells[i] = NULL; |
| 1169 |
break; |
| 1170 |
} |
| 1171 |
nBytes -= 8; |
| 1172 |
if ( mat->byteswap ) { |
| 1173 |
(void)Mat_uint32Swap(uncomp_buf); |
| 1174 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 1175 |
} |
| 1176 |
|
| 1177 |
if ( uncomp_buf[0] == MAT_T_INT32 ) { |
| 1178 |
int j; |
| 1179 |
size_t size; |
| 1180 |
cells[i]->rank = uncomp_buf[1]; |
| 1181 |
nBytes -= cells[i]->rank; |
| 1182 |
cells[i]->rank /= 4; |
| 1183 |
if ( 0 == do_clean && cells[i]->rank > 13 ) { |
| 1184 |
int rank = cells[i]->rank; |
| 1185 |
cells[i]->rank = 0; |
| 1186 |
Mat_Critical("%d is not a valid rank", rank); |
| 1187 |
continue; |
| 1188 |
} |
| 1189 |
err = Mul(&size, cells[i]->rank, sizeof(*cells[i]->dims)); |
| 1190 |
if ( err ) { |
| 1191 |
if ( do_clean ) { |
| 1192 |
free(dims); |
| 1193 |
} |
| 1194 |
Mat_VarFree(cells[i]); |
| 1195 |
cells[i] = NULL; |
| 1196 |
Mat_Critical("Integer multiplication overflow"); |
| 1197 |
continue; |
| 1198 |
} |
| 1199 |
cells[i]->dims = (size_t *)malloc(size); |
| 1200 |
if ( mat->byteswap ) { |
| 1201 |
for ( j = 0; j < cells[i]->rank; j++ ) |
| 1202 |
cells[i]->dims[j] = Mat_uint32Swap(dims + j); |
| 1203 |
} else { |
| 1204 |
for ( j = 0; j < cells[i]->rank; j++ ) |
| 1205 |
cells[i]->dims[j] = dims[j]; |
| 1206 |
} |
| 1207 |
if ( cells[i]->rank % 2 != 0 ) |
| 1208 |
nBytes -= 4; |
| 1209 |
} |
| 1210 |
if ( do_clean ) { |
| 1211 |
free(dims); |
| 1212 |
} |
| 1213 |
|
| 1214 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 8, &bytesread); |
| 1215 |
if ( err ) { |
| 1216 |
Mat_VarFree(cells[i]); |
| 1217 |
cells[i] = NULL; |
| 1218 |
break; |
| 1219 |
} |
| 1220 |
nBytes -= 8; |
| 1221 |
if ( mat->byteswap ) { |
| 1222 |
(void)Mat_uint32Swap(uncomp_buf); |
| 1223 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 1224 |
} |
| 1225 |
|
| 1226 |
if ( uncomp_buf[1] > 0 ) { |
| 1227 |
|
| 1228 |
if ( uncomp_buf[0] == MAT_T_INT8 ) { |
| 1229 |
mat_uint32_t len = uncomp_buf[1]; |
| 1230 |
|
| 1231 |
if ( len % 8 > 0 ) { |
| 1232 |
if ( len < UINT32_MAX - 8 + (len % 8) ) |
| 1233 |
len = len + 8 - (len % 8); |
| 1234 |
else { |
| 1235 |
Mat_VarFree(cells[i]); |
| 1236 |
cells[i] = NULL; |
| 1237 |
break; |
| 1238 |
} |
| 1239 |
} |
| 1240 |
cells[i]->name = (char *)malloc(len + 1); |
| 1241 |
nBytes -= len; |
| 1242 |
if ( NULL != cells[i]->name ) { |
| 1243 |
|
| 1244 |
err = |
| 1245 |
Inflate(mat, matvar->internal->z, cells[i]->name, len, &bytesread); |
| 1246 |
if ( err ) { |
| 1247 |
Mat_VarFree(cells[i]); |
| 1248 |
cells[i] = NULL; |
| 1249 |
break; |
| 1250 |
} |
| 1251 |
cells[i]->name[len] = '\0'; |
| 1252 |
} |
| 1253 |
} else { |
| 1254 |
mat_uint32_t len = (uncomp_buf[0] & 0xffff0000) >> 16; |
| 1255 |
if ( ((uncomp_buf[0] & 0x0000ffff) == MAT_T_INT8) && len > 0 && len <= 4 ) { |
| 1256 |
|
| 1257 |
cells[i]->name = (char *)malloc(len + 1); |
| 1258 |
if ( NULL != cells[i]->name ) { |
| 1259 |
memcpy(cells[i]->name, uncomp_buf + 1, len); |
| 1260 |
cells[i]->name[len] = '\0'; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
} |
| 1264 |
} |
| 1265 |
cells[i]->internal->z = (z_streamp)calloc(1, sizeof(z_stream)); |
| 1266 |
if ( cells[i]->internal->z != NULL ) { |
| 1267 |
err = inflateCopy(cells[i]->internal->z, matvar->internal->z); |
| 1268 |
if ( err == Z_OK ) { |
| 1269 |
cells[i]->internal->datapos = ftello((FILE *)mat->fp); |
| 1270 |
if ( cells[i]->internal->datapos != -1L ) { |
| 1271 |
cells[i]->internal->datapos -= matvar->internal->z->avail_in; |
| 1272 |
if ( cells[i]->class_type == MAT_C_STRUCT ) |
| 1273 |
bytesread += ReadNextStructField(mat, cells[i]); |
| 1274 |
else if ( cells[i]->class_type == MAT_C_CELL ) |
| 1275 |
bytesread += ReadNextCell(mat, cells[i]); |
| 1276 |
else if ( nBytes <= (1 << MAX_WBITS) ) { |
| 1277 |
|
| 1278 |
|
| 1279 |
err = Mat_VarRead5(mat, cells[i]); |
| 1280 |
cells[i]->internal->data = cells[i]->data; |
| 1281 |
cells[i]->data = NULL; |
| 1282 |
} |
| 1283 |
(void)fseeko((FILE *)mat->fp, cells[i]->internal->datapos, SEEK_SET); |
| 1284 |
} else { |
| 1285 |
Mat_Critical("Couldn't determine file position"); |
| 1286 |
} |
| 1287 |
if ( cells[i]->internal->data != NULL || |
| 1288 |
cells[i]->class_type == MAT_C_STRUCT || |
| 1289 |
cells[i]->class_type == MAT_C_CELL || |
| 1290 |
(nBytes <= (1 << MAX_WBITS) && cells[i]->class_type == MAT_C_CHAR) ) { |
| 1291 |
|
| 1292 |
inflateEnd(cells[i]->internal->z); |
| 1293 |
free(cells[i]->internal->z); |
| 1294 |
cells[i]->internal->z = NULL; |
| 1295 |
} |
| 1296 |
} else { |
| 1297 |
Mat_Critical("inflateCopy returned error %s", zError(err)); |
| 1298 |
} |
| 1299 |
} else { |
| 1300 |
Mat_Critical("Couldn't allocate memory"); |
| 1301 |
} |
| 1302 |
} |
| 1303 |
InflateSkip(mat, matvar->internal->z, nBytes, &bytesread); |
| 1304 |
} |
| 1305 |
#else |
| 1306 |
Mat_Critical("Not compiled with zlib support"); |
| 1307 |
#endif |
| 1308 |
|
| 1309 |
} else { |
| 1310 |
size_t i; |
| 1311 |
mat_uint32_t buf[6] = {0, 0, 0, 0, 0, 0}; |
| 1312 |
mat_uint32_t nBytes; |
| 1313 |
mat_uint32_t array_flags; |
| 1314 |
|
| 1315 |
for ( i = 0; i < nelems; i++ ) { |
| 1316 |
size_t nbytes = 0; |
| 1317 |
cells[i] = Mat_VarCalloc(); |
| 1318 |
if ( NULL == cells[i] ) { |
| 1319 |
Mat_Critical("Couldn't allocate memory for cell %zu", i); |
| 1320 |
continue; |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
err = Read(buf, 4, 2, (FILE *)mat->fp, &nbytes); |
| 1325 |
|
| 1326 |
|
| 1327 |
if ( 0 == err && 0 == nbytes ) |
| 1328 |
continue; |
| 1329 |
bytesread += nbytes; |
| 1330 |
if ( err ) { |
| 1331 |
Mat_VarFree(cells[i]); |
| 1332 |
cells[i] = NULL; |
| 1333 |
break; |
| 1334 |
} |
| 1335 |
if ( mat->byteswap ) { |
| 1336 |
(void)Mat_uint32Swap(buf); |
| 1337 |
(void)Mat_uint32Swap(buf + 1); |
| 1338 |
} |
| 1339 |
nBytes = buf[1]; |
| 1340 |
if ( 0 == nBytes ) { |
| 1341 |
|
| 1342 |
free(cells[i]->internal); |
| 1343 |
cells[i]->internal = NULL; |
| 1344 |
continue; |
| 1345 |
} else if ( buf[0] != MAT_T_MATRIX ) { |
| 1346 |
Mat_VarFree(cells[i]); |
| 1347 |
cells[i] = NULL; |
| 1348 |
#if HAVE_LONG_LONG_INT |
| 1349 |
Mat_Critical("cells[%zu] not MAT_T_MATRIX, fpos = %lld", i, |
| 1350 |
(long long)ftello((FILE *)mat->fp)); |
| 1351 |
#else |
| 1352 |
Mat_Critical("cells[%zu] not MAT_T_MATRIX", i); |
| 1353 |
#endif |
| 1354 |
break; |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
err = Read(buf, 4, 6, (FILE *)mat->fp, &bytesread); |
| 1359 |
if ( err ) { |
| 1360 |
Mat_VarFree(cells[i]); |
| 1361 |
cells[i] = NULL; |
| 1362 |
break; |
| 1363 |
} |
| 1364 |
if ( mat->byteswap ) { |
| 1365 |
(void)Mat_uint32Swap(buf); |
| 1366 |
(void)Mat_uint32Swap(buf + 1); |
| 1367 |
(void)Mat_uint32Swap(buf + 2); |
| 1368 |
(void)Mat_uint32Swap(buf + 3); |
| 1369 |
(void)Mat_uint32Swap(buf + 4); |
| 1370 |
(void)Mat_uint32Swap(buf + 5); |
| 1371 |
} |
| 1372 |
nBytes -= 24; |
| 1373 |
|
| 1374 |
if ( buf[0] == MAT_T_UINT32 ) { |
| 1375 |
array_flags = buf[2]; |
| 1376 |
cells[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags); |
| 1377 |
cells[i]->isComplex = (array_flags & MAT_F_COMPLEX); |
| 1378 |
cells[i]->isGlobal = (array_flags & MAT_F_GLOBAL); |
| 1379 |
cells[i]->isLogical = (array_flags & MAT_F_LOGICAL); |
| 1380 |
if ( cells[i]->class_type == MAT_C_SPARSE ) { |
| 1381 |
|
| 1382 |
cells[i]->nbytes = buf[3]; |
| 1383 |
} |
| 1384 |
} |
| 1385 |
|
| 1386 |
nbytes = 0; |
| 1387 |
err = ReadRankDims(mat, cells[i], (enum matio_types)buf[4], buf[5], &nbytes); |
| 1388 |
bytesread += nbytes; |
| 1389 |
nBytes -= nbytes; |
| 1390 |
if ( err ) { |
| 1391 |
Mat_VarFree(cells[i]); |
| 1392 |
cells[i] = NULL; |
| 1393 |
break; |
| 1394 |
} |
| 1395 |
|
| 1396 |
if ( 0 != Read(buf, 1, 8, (FILE *)mat->fp, &bytesread) ) { |
| 1397 |
Mat_VarFree(cells[i]); |
| 1398 |
cells[i] = NULL; |
| 1399 |
break; |
| 1400 |
} |
| 1401 |
nBytes -= 8; |
| 1402 |
if ( mat->byteswap ) { |
| 1403 |
(void)Mat_uint32Swap(buf); |
| 1404 |
(void)Mat_uint32Swap(buf + 1); |
| 1405 |
} |
| 1406 |
if ( buf[1] > 0 ) { |
| 1407 |
|
| 1408 |
if ( buf[0] == MAT_T_INT8 ) { |
| 1409 |
mat_uint32_t name_len = buf[1]; |
| 1410 |
if ( name_len % 8 > 0 ) { |
| 1411 |
if ( name_len < UINT32_MAX - 8 + (name_len % 8) ) { |
| 1412 |
name_len = name_len + 8 - (name_len % 8); |
| 1413 |
} else { |
| 1414 |
Mat_VarFree(cells[i]); |
| 1415 |
cells[i] = NULL; |
| 1416 |
break; |
| 1417 |
} |
| 1418 |
} |
| 1419 |
nBytes -= name_len; |
| 1420 |
(void)fseeko((FILE *)mat->fp, name_len, SEEK_CUR); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
cells[i]->internal->datapos = ftello((FILE *)mat->fp); |
| 1424 |
if ( cells[i]->internal->datapos != -1L ) { |
| 1425 |
if ( cells[i]->class_type == MAT_C_STRUCT ) |
| 1426 |
bytesread += ReadNextStructField(mat, cells[i]); |
| 1427 |
if ( cells[i]->class_type == MAT_C_CELL ) |
| 1428 |
bytesread += ReadNextCell(mat, cells[i]); |
| 1429 |
(void)fseeko((FILE *)mat->fp, cells[i]->internal->datapos + nBytes, SEEK_SET); |
| 1430 |
} else { |
| 1431 |
Mat_Critical("Couldn't determine file position"); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
return bytesread; |
| 1437 |
} |
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
static size_t |
| 1449 |
ReadNextStructField(mat_t *mat, matvar_t *matvar) |
| 1450 |
{ |
| 1451 |
int err; |
| 1452 |
size_t bytesread = 0; |
| 1453 |
matvar_t **fields = NULL; |
| 1454 |
size_t nelems = 1, nelems_x_nfields; |
| 1455 |
|
| 1456 |
err = Mat_MulDims(matvar, &nelems); |
| 1457 |
if ( err ) { |
| 1458 |
Mat_Critical("Integer multiplication overflow"); |
| 1459 |
return bytesread; |
| 1460 |
} |
| 1461 |
if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 1462 |
#if HAVE_ZLIB |
| 1463 |
size_t nfields, i; |
| 1464 |
mat_uint32_t uncomp_buf[16]; |
| 1465 |
mat_uint32_t array_flags, len, fieldname_size; |
| 1466 |
|
| 1467 |
memset(uncomp_buf, 0, sizeof(uncomp_buf)); |
| 1468 |
|
| 1469 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 8, &bytesread); |
| 1470 |
if ( err ) { |
| 1471 |
return bytesread; |
| 1472 |
} |
| 1473 |
if ( mat->byteswap ) { |
| 1474 |
(void)Mat_uint32Swap(uncomp_buf); |
| 1475 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 1476 |
} |
| 1477 |
if ( (uncomp_buf[0] & 0x0000ffff) == MAT_T_INT32 && uncomp_buf[1] > 0 ) { |
| 1478 |
fieldname_size = uncomp_buf[1]; |
| 1479 |
} else { |
| 1480 |
Mat_Critical("Error getting fieldname size"); |
| 1481 |
return bytesread; |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 8, &bytesread); |
| 1486 |
if ( err ) { |
| 1487 |
return bytesread; |
| 1488 |
} |
| 1489 |
if ( mat->byteswap ) |
| 1490 |
(void)Mat_uint32Swap(uncomp_buf); |
| 1491 |
|
| 1492 |
if ( uncomp_buf[0] == MAT_T_INT8 ) { |
| 1493 |
if ( mat->byteswap ) |
| 1494 |
len = Mat_uint32Swap(uncomp_buf + 1); |
| 1495 |
else |
| 1496 |
len = uncomp_buf[1]; |
| 1497 |
nfields = len / fieldname_size; |
| 1498 |
if ( nfields * fieldname_size % 8 != 0 ) |
| 1499 |
i = 8 - (nfields * fieldname_size % 8); |
| 1500 |
else |
| 1501 |
i = 0; |
| 1502 |
if ( nfields ) { |
| 1503 |
char *ptr = (char *)malloc(nfields * fieldname_size + i); |
| 1504 |
if ( NULL != ptr ) { |
| 1505 |
err = Inflate(mat, matvar->internal->z, ptr, |
| 1506 |
(unsigned int)(nfields * fieldname_size + i), &bytesread); |
| 1507 |
if ( MATIO_E_NO_ERROR == err ) { |
| 1508 |
SetFieldNames(matvar, ptr, nfields, fieldname_size); |
| 1509 |
} else { |
| 1510 |
matvar->internal->num_fields = nfields; |
| 1511 |
matvar->internal->fieldnames = NULL; |
| 1512 |
} |
| 1513 |
free(ptr); |
| 1514 |
} |
| 1515 |
} else { |
| 1516 |
matvar->internal->num_fields = 0; |
| 1517 |
matvar->internal->fieldnames = NULL; |
| 1518 |
} |
| 1519 |
} else { |
| 1520 |
len = (uncomp_buf[0] & 0xffff0000) >> 16; |
| 1521 |
if ( ((uncomp_buf[0] & 0x0000ffff) == MAT_T_INT8) && len > 0 && len <= 4 ) { |
| 1522 |
|
| 1523 |
nfields = len / fieldname_size; |
| 1524 |
if ( nfields ) { |
| 1525 |
SetFieldNames(matvar, (char *)(uncomp_buf + 1), nfields, fieldname_size); |
| 1526 |
} else { |
| 1527 |
matvar->internal->num_fields = 0; |
| 1528 |
matvar->internal->fieldnames = NULL; |
| 1529 |
} |
| 1530 |
} else { |
| 1531 |
nfields = 0; |
| 1532 |
} |
| 1533 |
} |
| 1534 |
|
| 1535 |
matvar->data_size = sizeof(matvar_t *); |
| 1536 |
err = Mul(&nelems_x_nfields, nelems, nfields); |
| 1537 |
if ( err ) { |
| 1538 |
Mat_Critical("Integer multiplication overflow"); |
| 1539 |
return bytesread; |
| 1540 |
} |
| 1541 |
err = Mul(&matvar->nbytes, nelems_x_nfields, matvar->data_size); |
| 1542 |
if ( err ) { |
| 1543 |
Mat_Critical("Integer multiplication overflow"); |
| 1544 |
return bytesread; |
| 1545 |
} |
| 1546 |
if ( !matvar->nbytes ) |
| 1547 |
return bytesread; |
| 1548 |
|
| 1549 |
matvar->data = calloc(nelems_x_nfields, matvar->data_size); |
| 1550 |
if ( NULL == matvar->data ) { |
| 1551 |
matvar->nbytes = 0; |
| 1552 |
Mat_Critical("Couldn't allocate memory for the data"); |
| 1553 |
return bytesread; |
| 1554 |
} |
| 1555 |
|
| 1556 |
fields = (matvar_t **)matvar->data; |
| 1557 |
for ( i = 0; i < nelems; i++ ) { |
| 1558 |
size_t k; |
| 1559 |
for ( k = 0; k < nfields; k++ ) { |
| 1560 |
fields[i * nfields + k] = Mat_VarCalloc(); |
| 1561 |
} |
| 1562 |
} |
| 1563 |
if ( NULL != matvar->internal->fieldnames ) { |
| 1564 |
for ( i = 0; i < nelems; i++ ) { |
| 1565 |
size_t k; |
| 1566 |
for ( k = 0; k < nfields; k++ ) { |
| 1567 |
if ( NULL != matvar->internal->fieldnames[k] ) { |
| 1568 |
fields[i * nfields + k]->name = strdup(matvar->internal->fieldnames[k]); |
| 1569 |
} |
| 1570 |
} |
| 1571 |
} |
| 1572 |
} |
| 1573 |
|
| 1574 |
for ( i = 0; i < nelems_x_nfields; i++ ) { |
| 1575 |
mat_uint32_t nBytes; |
| 1576 |
|
| 1577 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 8, &bytesread); |
| 1578 |
if ( err ) { |
| 1579 |
Mat_VarFree(fields[i]); |
| 1580 |
fields[i] = NULL; |
| 1581 |
break; |
| 1582 |
} |
| 1583 |
if ( mat->byteswap ) { |
| 1584 |
(void)Mat_uint32Swap(uncomp_buf); |
| 1585 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 1586 |
} |
| 1587 |
nBytes = uncomp_buf[1]; |
| 1588 |
if ( uncomp_buf[0] != MAT_T_MATRIX ) { |
| 1589 |
Mat_VarFree(fields[i]); |
| 1590 |
fields[i] = NULL; |
| 1591 |
Mat_Critical("fields[%zu], Uncompressed type not MAT_T_MATRIX", i); |
| 1592 |
break; |
| 1593 |
} else if ( 0 == nBytes ) { |
| 1594 |
|
| 1595 |
free(fields[i]->internal); |
| 1596 |
fields[i]->internal = NULL; |
| 1597 |
continue; |
| 1598 |
} |
| 1599 |
fields[i]->compression = MAT_COMPRESSION_ZLIB; |
| 1600 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 16, &bytesread); |
| 1601 |
if ( err ) { |
| 1602 |
Mat_VarFree(fields[i]); |
| 1603 |
fields[i] = NULL; |
| 1604 |
break; |
| 1605 |
} |
| 1606 |
if ( mat->byteswap ) { |
| 1607 |
(void)Mat_uint32Swap(uncomp_buf); |
| 1608 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 1609 |
(void)Mat_uint32Swap(uncomp_buf + 2); |
| 1610 |
(void)Mat_uint32Swap(uncomp_buf + 3); |
| 1611 |
} |
| 1612 |
nBytes -= 16; |
| 1613 |
|
| 1614 |
if ( uncomp_buf[0] == MAT_T_UINT32 ) { |
| 1615 |
array_flags = uncomp_buf[2]; |
| 1616 |
fields[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags); |
| 1617 |
fields[i]->isComplex = (array_flags & MAT_F_COMPLEX); |
| 1618 |
fields[i]->isGlobal = (array_flags & MAT_F_GLOBAL); |
| 1619 |
fields[i]->isLogical = (array_flags & MAT_F_LOGICAL); |
| 1620 |
if ( fields[i]->class_type == MAT_C_SPARSE ) { |
| 1621 |
|
| 1622 |
fields[i]->nbytes = uncomp_buf[3]; |
| 1623 |
} |
| 1624 |
} else { |
| 1625 |
Mat_Critical("Expected MAT_T_UINT32 for array tags, got %d", uncomp_buf[0]); |
| 1626 |
InflateSkip(mat, matvar->internal->z, nBytes, &bytesread); |
| 1627 |
} |
| 1628 |
if ( fields[i]->class_type != MAT_C_OPAQUE ) { |
| 1629 |
mat_uint32_t *dims = NULL; |
| 1630 |
int do_clean = 0; |
| 1631 |
err = InflateRankDims(mat, matvar->internal->z, uncomp_buf, sizeof(uncomp_buf), |
| 1632 |
&dims, &bytesread); |
| 1633 |
if ( NULL == dims ) { |
| 1634 |
dims = uncomp_buf + 2; |
| 1635 |
} else { |
| 1636 |
do_clean = 1; |
| 1637 |
} |
| 1638 |
if ( err ) { |
| 1639 |
if ( do_clean ) { |
| 1640 |
free(dims); |
| 1641 |
} |
| 1642 |
Mat_VarFree(fields[i]); |
| 1643 |
fields[i] = NULL; |
| 1644 |
break; |
| 1645 |
} |
| 1646 |
nBytes -= 8; |
| 1647 |
if ( mat->byteswap ) { |
| 1648 |
(void)Mat_uint32Swap(uncomp_buf); |
| 1649 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 1650 |
} |
| 1651 |
|
| 1652 |
if ( uncomp_buf[0] == MAT_T_INT32 ) { |
| 1653 |
int j; |
| 1654 |
size_t size; |
| 1655 |
fields[i]->rank = uncomp_buf[1]; |
| 1656 |
nBytes -= fields[i]->rank; |
| 1657 |
fields[i]->rank /= 4; |
| 1658 |
if ( 0 == do_clean && fields[i]->rank > 13 ) { |
| 1659 |
int rank = fields[i]->rank; |
| 1660 |
fields[i]->rank = 0; |
| 1661 |
Mat_Critical("%d is not a valid rank", rank); |
| 1662 |
continue; |
| 1663 |
} |
| 1664 |
err = Mul(&size, fields[i]->rank, sizeof(*fields[i]->dims)); |
| 1665 |
if ( err ) { |
| 1666 |
if ( do_clean ) { |
| 1667 |
free(dims); |
| 1668 |
} |
| 1669 |
Mat_VarFree(fields[i]); |
| 1670 |
fields[i] = NULL; |
| 1671 |
Mat_Critical("Integer multiplication overflow"); |
| 1672 |
continue; |
| 1673 |
} |
| 1674 |
fields[i]->dims = (size_t *)malloc(size); |
| 1675 |
if ( mat->byteswap ) { |
| 1676 |
for ( j = 0; j < fields[i]->rank; j++ ) |
| 1677 |
fields[i]->dims[j] = Mat_uint32Swap(dims + j); |
| 1678 |
} else { |
| 1679 |
for ( j = 0; j < fields[i]->rank; j++ ) |
| 1680 |
fields[i]->dims[j] = dims[j]; |
| 1681 |
} |
| 1682 |
if ( fields[i]->rank % 2 != 0 ) |
| 1683 |
nBytes -= 4; |
| 1684 |
} |
| 1685 |
if ( do_clean ) { |
| 1686 |
free(dims); |
| 1687 |
} |
| 1688 |
|
| 1689 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 8, &bytesread); |
| 1690 |
if ( err ) { |
| 1691 |
Mat_VarFree(fields[i]); |
| 1692 |
fields[i] = NULL; |
| 1693 |
break; |
| 1694 |
} |
| 1695 |
nBytes -= 8; |
| 1696 |
fields[i]->internal->z = (z_streamp)calloc(1, sizeof(z_stream)); |
| 1697 |
if ( fields[i]->internal->z != NULL ) { |
| 1698 |
err = inflateCopy(fields[i]->internal->z, matvar->internal->z); |
| 1699 |
if ( err == Z_OK ) { |
| 1700 |
fields[i]->internal->datapos = ftello((FILE *)mat->fp); |
| 1701 |
if ( fields[i]->internal->datapos != -1L ) { |
| 1702 |
fields[i]->internal->datapos -= matvar->internal->z->avail_in; |
| 1703 |
if ( fields[i]->class_type == MAT_C_STRUCT ) |
| 1704 |
bytesread += ReadNextStructField(mat, fields[i]); |
| 1705 |
else if ( fields[i]->class_type == MAT_C_CELL ) |
| 1706 |
bytesread += ReadNextCell(mat, fields[i]); |
| 1707 |
else if ( nBytes <= (1 << MAX_WBITS) ) { |
| 1708 |
|
| 1709 |
|
| 1710 |
Mat_VarRead5(mat, fields[i]); |
| 1711 |
fields[i]->internal->data = fields[i]->data; |
| 1712 |
fields[i]->data = NULL; |
| 1713 |
} |
| 1714 |
(void)fseeko((FILE *)mat->fp, fields[i]->internal->datapos, SEEK_SET); |
| 1715 |
} else { |
| 1716 |
Mat_Critical("Couldn't determine file position"); |
| 1717 |
} |
| 1718 |
if ( fields[i]->internal->data != NULL || |
| 1719 |
fields[i]->class_type == MAT_C_STRUCT || |
| 1720 |
fields[i]->class_type == MAT_C_CELL || |
| 1721 |
(nBytes <= (1 << MAX_WBITS) && fields[i]->class_type == MAT_C_CHAR) ) { |
| 1722 |
|
| 1723 |
inflateEnd(fields[i]->internal->z); |
| 1724 |
free(fields[i]->internal->z); |
| 1725 |
fields[i]->internal->z = NULL; |
| 1726 |
} |
| 1727 |
} else { |
| 1728 |
Mat_Critical("inflateCopy returned error %s", zError(err)); |
| 1729 |
} |
| 1730 |
} else { |
| 1731 |
Mat_Critical("Couldn't allocate memory"); |
| 1732 |
} |
| 1733 |
} |
| 1734 |
InflateSkip(mat, matvar->internal->z, nBytes, &bytesread); |
| 1735 |
} |
| 1736 |
#else |
| 1737 |
Mat_Critical("Not compiled with zlib support"); |
| 1738 |
#endif |
| 1739 |
} else { |
| 1740 |
size_t nfields, i; |
| 1741 |
mat_uint32_t buf[6] = {0, 0, 0, 0, 0, 0}; |
| 1742 |
mat_uint32_t array_flags, len, fieldname_size; |
| 1743 |
|
| 1744 |
err = Read(buf, 4, 2, (FILE *)mat->fp, &bytesread); |
| 1745 |
if ( err ) { |
| 1746 |
return bytesread; |
| 1747 |
} |
| 1748 |
if ( mat->byteswap ) { |
| 1749 |
(void)Mat_uint32Swap(buf); |
| 1750 |
(void)Mat_uint32Swap(buf + 1); |
| 1751 |
} |
| 1752 |
if ( (buf[0] & 0x0000ffff) == MAT_T_INT32 && buf[1] > 0 ) { |
| 1753 |
fieldname_size = buf[1]; |
| 1754 |
} else { |
| 1755 |
Mat_Critical("Error getting fieldname size"); |
| 1756 |
return bytesread; |
| 1757 |
} |
| 1758 |
|
| 1759 |
|
| 1760 |
err = Read(buf, 4, 2, (FILE *)mat->fp, &bytesread); |
| 1761 |
if ( err ) { |
| 1762 |
return bytesread; |
| 1763 |
} |
| 1764 |
if ( mat->byteswap ) |
| 1765 |
(void)Mat_uint32Swap(buf); |
| 1766 |
|
| 1767 |
if ( buf[0] == MAT_T_INT8 ) { |
| 1768 |
if ( mat->byteswap ) |
| 1769 |
len = Mat_uint32Swap(buf + 1); |
| 1770 |
else |
| 1771 |
len = buf[1]; |
| 1772 |
nfields = len / fieldname_size; |
| 1773 |
if ( nfields ) { |
| 1774 |
char *ptr = (char *)malloc(nfields * fieldname_size); |
| 1775 |
if ( NULL != ptr ) { |
| 1776 |
err = Read(ptr, 1, nfields * fieldname_size, (FILE *)mat->fp, &bytesread); |
| 1777 |
if ( 0 == err ) { |
| 1778 |
SetFieldNames(matvar, ptr, nfields, fieldname_size); |
| 1779 |
} else { |
| 1780 |
matvar->internal->num_fields = 0; |
| 1781 |
matvar->internal->fieldnames = NULL; |
| 1782 |
} |
| 1783 |
free(ptr); |
| 1784 |
} |
| 1785 |
if ( (nfields * fieldname_size) % 8 ) { |
| 1786 |
(void)fseeko((FILE *)mat->fp, 8 - ((nfields * fieldname_size) % 8), SEEK_CUR); |
| 1787 |
bytesread += 8 - ((nfields * fieldname_size) % 8); |
| 1788 |
} |
| 1789 |
} else { |
| 1790 |
matvar->internal->num_fields = 0; |
| 1791 |
matvar->internal->fieldnames = NULL; |
| 1792 |
} |
| 1793 |
} else { |
| 1794 |
len = (buf[0] & 0xffff0000) >> 16; |
| 1795 |
if ( ((buf[0] & 0x0000ffff) == MAT_T_INT8) && len > 0 && len <= 4 ) { |
| 1796 |
|
| 1797 |
nfields = len / fieldname_size; |
| 1798 |
if ( nfields ) { |
| 1799 |
SetFieldNames(matvar, (char *)(buf + 1), nfields, fieldname_size); |
| 1800 |
} else { |
| 1801 |
matvar->internal->num_fields = 0; |
| 1802 |
matvar->internal->fieldnames = NULL; |
| 1803 |
} |
| 1804 |
} else { |
| 1805 |
nfields = 0; |
| 1806 |
} |
| 1807 |
} |
| 1808 |
|
| 1809 |
matvar->data_size = sizeof(matvar_t *); |
| 1810 |
err = Mul(&nelems_x_nfields, nelems, nfields); |
| 1811 |
if ( err ) { |
| 1812 |
Mat_Critical("Integer multiplication overflow"); |
| 1813 |
return bytesread; |
| 1814 |
} |
| 1815 |
err = Mul(&matvar->nbytes, nelems_x_nfields, matvar->data_size); |
| 1816 |
if ( err ) { |
| 1817 |
Mat_Critical("Integer multiplication overflow"); |
| 1818 |
return bytesread; |
| 1819 |
} |
| 1820 |
if ( !matvar->nbytes ) |
| 1821 |
return bytesread; |
| 1822 |
|
| 1823 |
matvar->data = calloc(nelems_x_nfields, matvar->data_size); |
| 1824 |
if ( NULL == matvar->data ) { |
| 1825 |
matvar->nbytes = 0; |
| 1826 |
Mat_Critical("Couldn't allocate memory for the data"); |
| 1827 |
return bytesread; |
| 1828 |
} |
| 1829 |
|
| 1830 |
fields = (matvar_t **)matvar->data; |
| 1831 |
for ( i = 0; i < nelems_x_nfields; i++ ) { |
| 1832 |
mat_uint32_t nBytes; |
| 1833 |
|
| 1834 |
fields[i] = Mat_VarCalloc(); |
| 1835 |
if ( NULL == fields[i] ) { |
| 1836 |
Mat_Critical("Couldn't allocate memory for field %zu", i); |
| 1837 |
continue; |
| 1838 |
} |
| 1839 |
|
| 1840 |
|
| 1841 |
err = Read(buf, 4, 2, (FILE *)mat->fp, &bytesread); |
| 1842 |
if ( err ) { |
| 1843 |
Mat_VarFree(fields[i]); |
| 1844 |
fields[i] = NULL; |
| 1845 |
break; |
| 1846 |
} |
| 1847 |
if ( mat->byteswap ) { |
| 1848 |
(void)Mat_uint32Swap(buf); |
| 1849 |
(void)Mat_uint32Swap(buf + 1); |
| 1850 |
} |
| 1851 |
nBytes = buf[1]; |
| 1852 |
if ( buf[0] != MAT_T_MATRIX ) { |
| 1853 |
Mat_VarFree(fields[i]); |
| 1854 |
fields[i] = NULL; |
| 1855 |
#if HAVE_LONG_LONG_INT |
| 1856 |
Mat_Critical("fields[%zu] not MAT_T_MATRIX, fpos = %lld", i, |
| 1857 |
(long long)ftello((FILE *)mat->fp)); |
| 1858 |
#else |
| 1859 |
Mat_Critical("fields[%zu] not MAT_T_MATRIX", i); |
| 1860 |
#endif |
| 1861 |
break; |
| 1862 |
} else if ( 0 == nBytes ) { |
| 1863 |
|
| 1864 |
free(fields[i]->internal); |
| 1865 |
fields[i]->internal = NULL; |
| 1866 |
continue; |
| 1867 |
} |
| 1868 |
|
| 1869 |
|
| 1870 |
err = Read(buf, 4, 6, (FILE *)mat->fp, &bytesread); |
| 1871 |
if ( err ) { |
| 1872 |
Mat_VarFree(fields[i]); |
| 1873 |
fields[i] = NULL; |
| 1874 |
break; |
| 1875 |
} |
| 1876 |
if ( mat->byteswap ) { |
| 1877 |
(void)Mat_uint32Swap(buf); |
| 1878 |
(void)Mat_uint32Swap(buf + 1); |
| 1879 |
(void)Mat_uint32Swap(buf + 2); |
| 1880 |
(void)Mat_uint32Swap(buf + 3); |
| 1881 |
(void)Mat_uint32Swap(buf + 4); |
| 1882 |
(void)Mat_uint32Swap(buf + 5); |
| 1883 |
} |
| 1884 |
nBytes -= 24; |
| 1885 |
|
| 1886 |
if ( buf[0] == MAT_T_UINT32 ) { |
| 1887 |
array_flags = buf[2]; |
| 1888 |
fields[i]->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags); |
| 1889 |
fields[i]->isComplex = (array_flags & MAT_F_COMPLEX); |
| 1890 |
fields[i]->isGlobal = (array_flags & MAT_F_GLOBAL); |
| 1891 |
fields[i]->isLogical = (array_flags & MAT_F_LOGICAL); |
| 1892 |
if ( fields[i]->class_type == MAT_C_SPARSE ) { |
| 1893 |
|
| 1894 |
fields[i]->nbytes = buf[3]; |
| 1895 |
} |
| 1896 |
} |
| 1897 |
|
| 1898 |
{ |
| 1899 |
size_t nbytes = 0; |
| 1900 |
err = ReadRankDims(mat, fields[i], (enum matio_types)buf[4], buf[5], &nbytes); |
| 1901 |
bytesread += nbytes; |
| 1902 |
nBytes -= nbytes; |
| 1903 |
if ( err ) { |
| 1904 |
Mat_VarFree(fields[i]); |
| 1905 |
fields[i] = NULL; |
| 1906 |
break; |
| 1907 |
} |
| 1908 |
} |
| 1909 |
|
| 1910 |
err = Read(buf, 1, 8, (FILE *)mat->fp, &bytesread); |
| 1911 |
if ( err ) { |
| 1912 |
Mat_VarFree(fields[i]); |
| 1913 |
fields[i] = NULL; |
| 1914 |
break; |
| 1915 |
} |
| 1916 |
nBytes -= 8; |
| 1917 |
fields[i]->internal->datapos = ftello((FILE *)mat->fp); |
| 1918 |
if ( fields[i]->internal->datapos != -1L ) { |
| 1919 |
if ( fields[i]->class_type == MAT_C_STRUCT ) |
| 1920 |
bytesread += ReadNextStructField(mat, fields[i]); |
| 1921 |
else if ( fields[i]->class_type == MAT_C_CELL ) |
| 1922 |
bytesread += ReadNextCell(mat, fields[i]); |
| 1923 |
(void)fseeko((FILE *)mat->fp, fields[i]->internal->datapos + nBytes, SEEK_SET); |
| 1924 |
} else { |
| 1925 |
Mat_Critical("Couldn't determine file position"); |
| 1926 |
} |
| 1927 |
} |
| 1928 |
|
| 1929 |
if ( NULL != matvar->internal->fieldnames ) { |
| 1930 |
for ( i = 0; i < nelems; i++ ) { |
| 1931 |
size_t k; |
| 1932 |
for ( k = 0; k < nfields; k++ ) { |
| 1933 |
if ( NULL != matvar->internal->fieldnames[k] && |
| 1934 |
NULL != fields[i * nfields + k] ) { |
| 1935 |
fields[i * nfields + k]->name = strdup(matvar->internal->fieldnames[k]); |
| 1936 |
} |
| 1937 |
} |
| 1938 |
} |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
return bytesread; |
| 1943 |
} |
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
static size_t |
| 1953 |
ReadNextFunctionHandle(mat_t *mat, matvar_t *matvar) |
| 1954 |
{ |
| 1955 |
int err; |
| 1956 |
size_t nelems = 1; |
| 1957 |
|
| 1958 |
err = Mat_MulDims(matvar, &nelems); |
| 1959 |
matvar->data_size = sizeof(matvar_t *); |
| 1960 |
err |= Mul(&matvar->nbytes, nelems, matvar->data_size); |
| 1961 |
if ( err ) |
| 1962 |
return 0; |
| 1963 |
|
| 1964 |
matvar->data = malloc(matvar->nbytes); |
| 1965 |
if ( matvar->data != NULL ) { |
| 1966 |
size_t i; |
| 1967 |
matvar_t **functions = (matvar_t **)matvar->data; |
| 1968 |
for ( i = 0; i < nelems; i++ ) { |
| 1969 |
functions[i] = Mat_VarReadNextInfo(mat); |
| 1970 |
err = NULL == functions[i]; |
| 1971 |
if ( err ) |
| 1972 |
break; |
| 1973 |
} |
| 1974 |
if ( err ) { |
| 1975 |
size_t j; |
| 1976 |
for ( j = 0; j <= i; j++ ) { |
| 1977 |
Mat_VarFree(functions[j]); |
| 1978 |
} |
| 1979 |
free(matvar->data); |
| 1980 |
matvar->data = NULL; |
| 1981 |
matvar->data_size = 0; |
| 1982 |
matvar->nbytes = 0; |
| 1983 |
} |
| 1984 |
} else { |
| 1985 |
matvar->data_size = 0; |
| 1986 |
matvar->nbytes = 0; |
| 1987 |
} |
| 1988 |
|
| 1989 |
return 0; |
| 1990 |
} |
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
static int |
| 2003 |
ReadRankDims(mat_t *mat, matvar_t *matvar, enum matio_types data_type, mat_uint32_t nbytes, |
| 2004 |
size_t *read_bytes) |
| 2005 |
{ |
| 2006 |
int err = MATIO_E_NO_ERROR; |
| 2007 |
|
| 2008 |
if ( data_type == MAT_T_INT32 ) { |
| 2009 |
matvar->rank = nbytes / sizeof(mat_uint32_t); |
| 2010 |
matvar->dims = (size_t *)malloc(matvar->rank * sizeof(*matvar->dims)); |
| 2011 |
if ( NULL != matvar->dims ) { |
| 2012 |
int i; |
| 2013 |
mat_uint32_t buf; |
| 2014 |
|
| 2015 |
for ( i = 0; i < matvar->rank; i++ ) { |
| 2016 |
err = Read(&buf, sizeof(mat_uint32_t), 1, (FILE *)mat->fp, read_bytes); |
| 2017 |
if ( MATIO_E_NO_ERROR == err ) { |
| 2018 |
if ( mat->byteswap ) { |
| 2019 |
matvar->dims[i] = Mat_uint32Swap(&buf); |
| 2020 |
} else { |
| 2021 |
matvar->dims[i] = buf; |
| 2022 |
} |
| 2023 |
} else { |
| 2024 |
free(matvar->dims); |
| 2025 |
matvar->dims = NULL; |
| 2026 |
matvar->rank = 0; |
| 2027 |
return err; |
| 2028 |
} |
| 2029 |
} |
| 2030 |
|
| 2031 |
if ( matvar->rank % 2 != 0 ) { |
| 2032 |
err = Read(&buf, sizeof(mat_uint32_t), 1, (FILE *)mat->fp, read_bytes); |
| 2033 |
if ( err ) { |
| 2034 |
free(matvar->dims); |
| 2035 |
matvar->dims = NULL; |
| 2036 |
matvar->rank = 0; |
| 2037 |
return err; |
| 2038 |
} |
| 2039 |
} |
| 2040 |
} else { |
| 2041 |
matvar->rank = 0; |
| 2042 |
err = MATIO_E_OUT_OF_MEMORY; |
| 2043 |
Mat_Critical("Error allocating memory for dims"); |
| 2044 |
} |
| 2045 |
} |
| 2046 |
return err; |
| 2047 |
} |
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
|
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
static int |
| 2057 |
WriteType(mat_t *mat, matvar_t *matvar) |
| 2058 |
{ |
| 2059 |
int err; |
| 2060 |
const mat_uint8_t pad1 = 0; |
| 2061 |
int nBytes, j; |
| 2062 |
size_t nelems = 1; |
| 2063 |
|
| 2064 |
err = Mat_MulDims(matvar, &nelems); |
| 2065 |
if ( err ) |
| 2066 |
return err; |
| 2067 |
|
| 2068 |
switch ( matvar->class_type ) { |
| 2069 |
case MAT_C_DOUBLE: |
| 2070 |
case MAT_C_SINGLE: |
| 2071 |
case MAT_C_INT64: |
| 2072 |
case MAT_C_UINT64: |
| 2073 |
case MAT_C_INT32: |
| 2074 |
case MAT_C_UINT32: |
| 2075 |
case MAT_C_INT16: |
| 2076 |
case MAT_C_UINT16: |
| 2077 |
case MAT_C_INT8: |
| 2078 |
case MAT_C_UINT8: { |
| 2079 |
if ( matvar->isComplex ) { |
| 2080 |
const mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data; |
| 2081 |
|
| 2082 |
if ( NULL == matvar->data ) |
| 2083 |
complex_data = &null_complex_data; |
| 2084 |
|
| 2085 |
nBytes = WriteData(mat, complex_data->Re, nelems, matvar->data_type); |
| 2086 |
if ( nBytes % 8 ) |
| 2087 |
for ( j = nBytes % 8; j < 8; j++ ) |
| 2088 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2089 |
nBytes = WriteData(mat, complex_data->Im, nelems, matvar->data_type); |
| 2090 |
if ( nBytes % 8 ) |
| 2091 |
for ( j = nBytes % 8; j < 8; j++ ) |
| 2092 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2093 |
} else { |
| 2094 |
nBytes = WriteData(mat, matvar->data, nelems, matvar->data_type); |
| 2095 |
if ( nBytes % 8 ) |
| 2096 |
for ( j = nBytes % 8; j < 8; j++ ) |
| 2097 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2098 |
} |
| 2099 |
break; |
| 2100 |
} |
| 2101 |
case MAT_C_CHAR: |
| 2102 |
if ( matvar->data_type == MAT_T_UTF8 ) { |
| 2103 |
nelems = matvar->nbytes; |
| 2104 |
} |
| 2105 |
WriteCharData(mat, matvar->data, nelems, matvar->data_type); |
| 2106 |
break; |
| 2107 |
case MAT_C_CELL: { |
| 2108 |
size_t i; |
| 2109 |
matvar_t **cells = (matvar_t **)matvar->data; |
| 2110 |
|
| 2111 |
|
| 2112 |
if ( matvar->nbytes == 0 || matvar->data_size == 0 || matvar->data == NULL ) |
| 2113 |
break; |
| 2114 |
nelems = matvar->nbytes / matvar->data_size; |
| 2115 |
for ( i = 0; i < nelems; i++ ) |
| 2116 |
WriteCellArrayField(mat, cells[i]); |
| 2117 |
break; |
| 2118 |
} |
| 2119 |
case MAT_C_STRUCT: { |
| 2120 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 2121 |
const mat_uint32_t fieldname_type = MAT_T_INT32; |
| 2122 |
const mat_uint32_t fieldname_data_size = 4; |
| 2123 |
char *padzero; |
| 2124 |
mat_uint32_t fieldname_size; |
| 2125 |
size_t maxlen = 0, nfields, i, nelems_x_nfields; |
| 2126 |
matvar_t **fields = (matvar_t **)matvar->data; |
| 2127 |
mat_uint32_t fieldname; |
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
|
| 2133 |
|
| 2134 |
nfields = matvar->internal->num_fields; |
| 2135 |
|
| 2136 |
if ( nfields < 1 ) { |
| 2137 |
fieldname = (fieldname_data_size << 16) | fieldname_type; |
| 2138 |
fwrite(&fieldname, 4, 1, (FILE *)mat->fp); |
| 2139 |
fieldname_size = 1; |
| 2140 |
fwrite(&fieldname_size, 4, 1, (FILE *)mat->fp); |
| 2141 |
fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 2142 |
nBytes = 0; |
| 2143 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 2144 |
break; |
| 2145 |
} |
| 2146 |
|
| 2147 |
for ( i = 0; i < nfields; i++ ) { |
| 2148 |
size_t len = strlen(matvar->internal->fieldnames[i]); |
| 2149 |
if ( len > maxlen ) |
| 2150 |
maxlen = len; |
| 2151 |
} |
| 2152 |
maxlen++; |
| 2153 |
fieldname_size = maxlen; |
| 2154 |
while ( nfields * fieldname_size % 8 != 0 ) |
| 2155 |
fieldname_size++; |
| 2156 |
fieldname = (fieldname_data_size << 16) | fieldname_type; |
| 2157 |
fwrite(&fieldname, 4, 1, (FILE *)mat->fp); |
| 2158 |
fwrite(&fieldname_size, 4, 1, (FILE *)mat->fp); |
| 2159 |
fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 2160 |
nBytes = nfields * fieldname_size; |
| 2161 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 2162 |
padzero = (char *)calloc(fieldname_size, 1); |
| 2163 |
for ( i = 0; i < nfields; i++ ) { |
| 2164 |
size_t len = strlen(matvar->internal->fieldnames[i]); |
| 2165 |
fwrite(matvar->internal->fieldnames[i], 1, len, (FILE *)mat->fp); |
| 2166 |
fwrite(padzero, 1, fieldname_size - len, (FILE *)mat->fp); |
| 2167 |
} |
| 2168 |
free(padzero); |
| 2169 |
err = Mul(&nelems_x_nfields, nelems, nfields); |
| 2170 |
if ( err ) |
| 2171 |
break; |
| 2172 |
for ( i = 0; i < nelems_x_nfields; i++ ) |
| 2173 |
WriteStructField(mat, fields[i]); |
| 2174 |
break; |
| 2175 |
} |
| 2176 |
case MAT_C_SPARSE: { |
| 2177 |
mat_sparse_t *sparse = (mat_sparse_t *)matvar->data; |
| 2178 |
|
| 2179 |
nBytes = WriteData(mat, sparse->ir, sparse->nir, MAT_T_UINT32); |
| 2180 |
if ( nBytes % 8 ) |
| 2181 |
for ( j = nBytes % 8; j < 8; j++ ) |
| 2182 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2183 |
nBytes = WriteData(mat, sparse->jc, sparse->njc, MAT_T_UINT32); |
| 2184 |
if ( nBytes % 8 ) |
| 2185 |
for ( j = nBytes % 8; j < 8; j++ ) |
| 2186 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2187 |
if ( matvar->isComplex ) { |
| 2188 |
const mat_complex_split_t *complex_data = (const mat_complex_split_t *)sparse->data; |
| 2189 |
nBytes = WriteData(mat, complex_data->Re, sparse->ndata, matvar->data_type); |
| 2190 |
if ( nBytes % 8 ) |
| 2191 |
for ( j = nBytes % 8; j < 8; j++ ) |
| 2192 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2193 |
nBytes = WriteData(mat, complex_data->Im, sparse->ndata, matvar->data_type); |
| 2194 |
if ( nBytes % 8 ) |
| 2195 |
for ( j = nBytes % 8; j < 8; j++ ) |
| 2196 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2197 |
} else { |
| 2198 |
nBytes = WriteData(mat, sparse->data, sparse->ndata, matvar->data_type); |
| 2199 |
if ( nBytes % 8 ) |
| 2200 |
for ( j = nBytes % 8; j < 8; j++ ) |
| 2201 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2202 |
} |
| 2203 |
break; |
| 2204 |
} |
| 2205 |
case MAT_C_FUNCTION: |
| 2206 |
case MAT_C_OBJECT: |
| 2207 |
case MAT_C_EMPTY: |
| 2208 |
case MAT_C_OPAQUE: |
| 2209 |
break; |
| 2210 |
default: |
| 2211 |
err = MATIO_E_OUTPUT_BAD_DATA; |
| 2212 |
break; |
| 2213 |
} |
| 2214 |
|
| 2215 |
return err; |
| 2216 |
} |
| 2217 |
|
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
static int |
| 2226 |
WriteCellArrayField(mat_t *mat, matvar_t *matvar) |
| 2227 |
{ |
| 2228 |
mat_uint32_t array_flags, nzmax = 0; |
| 2229 |
int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32; |
| 2230 |
int array_flags_size = 8, matrix_type = MAT_T_MATRIX; |
| 2231 |
const mat_uint32_t pad4 = 0; |
| 2232 |
const mat_uint8_t pad1 = 0; |
| 2233 |
int nBytes, i; |
| 2234 |
mat_off_t start = 0, end = 0; |
| 2235 |
|
| 2236 |
if ( matvar == NULL || mat == NULL ) |
| 2237 |
return MATIO_E_BAD_ARGUMENT; |
| 2238 |
|
| 2239 |
fwrite(&matrix_type, 4, 1, (FILE *)mat->fp); |
| 2240 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2241 |
if ( MAT_C_EMPTY == matvar->class_type ) { |
| 2242 |
|
| 2243 |
return MATIO_E_NO_ERROR; |
| 2244 |
} |
| 2245 |
start = ftello((FILE *)mat->fp); |
| 2246 |
|
| 2247 |
|
| 2248 |
array_flags = matvar->class_type & CLASS_TYPE_MASK; |
| 2249 |
if ( matvar->isComplex ) |
| 2250 |
array_flags |= MAT_F_COMPLEX; |
| 2251 |
if ( matvar->isGlobal ) |
| 2252 |
array_flags |= MAT_F_GLOBAL; |
| 2253 |
if ( matvar->isLogical ) |
| 2254 |
array_flags |= MAT_F_LOGICAL; |
| 2255 |
if ( matvar->class_type == MAT_C_SPARSE ) |
| 2256 |
nzmax = ((mat_sparse_t *)matvar->data)->nzmax; |
| 2257 |
|
| 2258 |
if ( mat->byteswap ) |
| 2259 |
array_flags = Mat_int32Swap((mat_int32_t *)&array_flags); |
| 2260 |
fwrite(&array_flags_type, 4, 1, (FILE *)mat->fp); |
| 2261 |
fwrite(&array_flags_size, 4, 1, (FILE *)mat->fp); |
| 2262 |
fwrite(&array_flags, 4, 1, (FILE *)mat->fp); |
| 2263 |
fwrite(&nzmax, 4, 1, (FILE *)mat->fp); |
| 2264 |
|
| 2265 |
nBytes = matvar->rank * 4; |
| 2266 |
fwrite(&dims_array_type, 4, 1, (FILE *)mat->fp); |
| 2267 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 2268 |
for ( i = 0; i < matvar->rank; i++ ) { |
| 2269 |
mat_int32_t dim; |
| 2270 |
dim = matvar->dims[i]; |
| 2271 |
fwrite(&dim, 4, 1, (FILE *)mat->fp); |
| 2272 |
} |
| 2273 |
if ( matvar->rank % 2 != 0 ) |
| 2274 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2275 |
|
| 2276 |
if ( !matvar->name ) { |
| 2277 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 2278 |
fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 2279 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2280 |
} else if ( strlen(matvar->name) <= 4 ) { |
| 2281 |
mat_uint32_t array_name_type = MAT_T_INT8; |
| 2282 |
const mat_uint32_t array_name_len = (mat_uint32_t)strlen(matvar->name); |
| 2283 |
array_name_type |= array_name_len << 16; |
| 2284 |
fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 2285 |
fwrite(matvar->name, 1, array_name_len, (FILE *)mat->fp); |
| 2286 |
for ( i = array_name_len; i < 4; i++ ) |
| 2287 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2288 |
} else { |
| 2289 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 2290 |
const mat_uint32_t array_name_len = (mat_uint32_t)strlen(matvar->name); |
| 2291 |
fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 2292 |
fwrite(&array_name_len, 4, 1, (FILE *)mat->fp); |
| 2293 |
fwrite(matvar->name, 1, array_name_len, (FILE *)mat->fp); |
| 2294 |
if ( array_name_len % 8 ) |
| 2295 |
for ( i = array_name_len % 8; i < 8; i++ ) |
| 2296 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2297 |
} |
| 2298 |
|
| 2299 |
WriteType(mat, matvar); |
| 2300 |
end = ftello((FILE *)mat->fp); |
| 2301 |
if ( start != -1L && end != -1L ) { |
| 2302 |
nBytes = (int)(end - start); |
| 2303 |
(void)fseeko((FILE *)mat->fp, (mat_off_t) - (nBytes + 4), SEEK_CUR); |
| 2304 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 2305 |
(void)fseeko((FILE *)mat->fp, end, SEEK_SET); |
| 2306 |
} else { |
| 2307 |
Mat_Critical("Couldn't determine file position"); |
| 2308 |
} |
| 2309 |
|
| 2310 |
return MATIO_E_NO_ERROR; |
| 2311 |
} |
| 2312 |
|
| 2313 |
#if HAVE_ZLIB |
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
static size_t |
| 2322 |
WriteCompressedTypeArrayFlags(mat_t *mat, matvar_t *matvar, z_streamp z) |
| 2323 |
{ |
| 2324 |
mat_uint32_t array_flags; |
| 2325 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 2326 |
int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32; |
| 2327 |
int array_flags_size = 8; |
| 2328 |
int nBytes, i, nzmax = 0; |
| 2329 |
|
| 2330 |
mat_uint32_t comp_buf[512]; |
| 2331 |
mat_uint32_t uncomp_buf[512]; |
| 2332 |
int buf_size = 512; |
| 2333 |
size_t byteswritten = 0; |
| 2334 |
|
| 2335 |
if ( MAT_C_EMPTY == matvar->class_type ) { |
| 2336 |
|
| 2337 |
return byteswritten; |
| 2338 |
} |
| 2339 |
|
| 2340 |
memset(uncomp_buf, 0, sizeof(uncomp_buf)); |
| 2341 |
|
| 2342 |
array_flags = matvar->class_type & CLASS_TYPE_MASK; |
| 2343 |
if ( matvar->isComplex ) |
| 2344 |
array_flags |= MAT_F_COMPLEX; |
| 2345 |
if ( matvar->isGlobal ) |
| 2346 |
array_flags |= MAT_F_GLOBAL; |
| 2347 |
if ( matvar->isLogical ) |
| 2348 |
array_flags |= MAT_F_LOGICAL; |
| 2349 |
if ( matvar->class_type == MAT_C_SPARSE ) |
| 2350 |
nzmax = ((mat_sparse_t *)matvar->data)->nzmax; |
| 2351 |
uncomp_buf[0] = array_flags_type; |
| 2352 |
uncomp_buf[1] = array_flags_size; |
| 2353 |
uncomp_buf[2] = array_flags; |
| 2354 |
uncomp_buf[3] = nzmax; |
| 2355 |
|
| 2356 |
nBytes = matvar->rank * 4; |
| 2357 |
uncomp_buf[4] = dims_array_type; |
| 2358 |
uncomp_buf[5] = nBytes; |
| 2359 |
for ( i = 0; i < matvar->rank; i++ ) { |
| 2360 |
mat_int32_t dim; |
| 2361 |
dim = matvar->dims[i]; |
| 2362 |
uncomp_buf[6 + i] = dim; |
| 2363 |
} |
| 2364 |
if ( matvar->rank % 2 != 0 ) { |
| 2365 |
const mat_uint32_t pad4 = 0; |
| 2366 |
uncomp_buf[6 + i] = pad4; |
| 2367 |
i++; |
| 2368 |
} |
| 2369 |
|
| 2370 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2371 |
z->avail_in = (6 + i) * sizeof(*uncomp_buf); |
| 2372 |
do { |
| 2373 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2374 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 2375 |
deflate(z, Z_NO_FLUSH); |
| 2376 |
byteswritten += |
| 2377 |
fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); |
| 2378 |
} while ( z->avail_out == 0 ); |
| 2379 |
|
| 2380 |
uncomp_buf[0] = array_name_type; |
| 2381 |
uncomp_buf[1] = 0; |
| 2382 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2383 |
z->avail_in = 8; |
| 2384 |
do { |
| 2385 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2386 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 2387 |
deflate(z, Z_NO_FLUSH); |
| 2388 |
byteswritten += |
| 2389 |
fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); |
| 2390 |
} while ( z->avail_out == 0 ); |
| 2391 |
|
| 2392 |
matvar->internal->datapos = ftello((FILE *)mat->fp); |
| 2393 |
if ( matvar->internal->datapos == -1L ) { |
| 2394 |
Mat_Critical("Couldn't determine file position"); |
| 2395 |
} |
| 2396 |
|
| 2397 |
byteswritten += WriteCompressedType(mat, matvar, z); |
| 2398 |
return byteswritten; |
| 2399 |
} |
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
|
| 2408 |
static size_t |
| 2409 |
WriteCompressedType(mat_t *mat, matvar_t *matvar, z_streamp z) |
| 2410 |
{ |
| 2411 |
int err; |
| 2412 |
mat_uint32_t comp_buf[512]; |
| 2413 |
mat_uint32_t uncomp_buf[512]; |
| 2414 |
size_t byteswritten = 0, nelems = 1; |
| 2415 |
|
| 2416 |
if ( MAT_C_EMPTY == matvar->class_type ) { |
| 2417 |
|
| 2418 |
return byteswritten; |
| 2419 |
} |
| 2420 |
|
| 2421 |
memset(uncomp_buf, 0, sizeof(uncomp_buf)); |
| 2422 |
err = Mat_MulDims(matvar, &nelems); |
| 2423 |
if ( err ) { |
| 2424 |
Mat_Critical("Integer multiplication overflow"); |
| 2425 |
return byteswritten; |
| 2426 |
} |
| 2427 |
|
| 2428 |
switch ( matvar->class_type ) { |
| 2429 |
case MAT_C_DOUBLE: |
| 2430 |
case MAT_C_SINGLE: |
| 2431 |
case MAT_C_INT64: |
| 2432 |
case MAT_C_UINT64: |
| 2433 |
case MAT_C_INT32: |
| 2434 |
case MAT_C_UINT32: |
| 2435 |
case MAT_C_INT16: |
| 2436 |
case MAT_C_UINT16: |
| 2437 |
case MAT_C_INT8: |
| 2438 |
case MAT_C_UINT8: { |
| 2439 |
|
| 2440 |
|
| 2441 |
if ( matvar->isComplex ) { |
| 2442 |
mat_complex_split_t *complex_data = (mat_complex_split_t *)matvar->data; |
| 2443 |
|
| 2444 |
if ( NULL == matvar->data ) |
| 2445 |
complex_data = &null_complex_data; |
| 2446 |
|
| 2447 |
byteswritten += |
| 2448 |
WriteCompressedData(mat, z, complex_data->Re, nelems, matvar->data_type); |
| 2449 |
byteswritten += |
| 2450 |
WriteCompressedData(mat, z, complex_data->Im, nelems, matvar->data_type); |
| 2451 |
} else { |
| 2452 |
byteswritten += |
| 2453 |
WriteCompressedData(mat, z, matvar->data, nelems, matvar->data_type); |
| 2454 |
} |
| 2455 |
break; |
| 2456 |
} |
| 2457 |
case MAT_C_CHAR: { |
| 2458 |
if ( matvar->data_type == MAT_T_UTF8 ) { |
| 2459 |
nelems = matvar->nbytes; |
| 2460 |
} |
| 2461 |
byteswritten += |
| 2462 |
WriteCompressedCharData(mat, z, matvar->data, nelems, matvar->data_type); |
| 2463 |
break; |
| 2464 |
} |
| 2465 |
case MAT_C_CELL: { |
| 2466 |
size_t i; |
| 2467 |
matvar_t **cells = (matvar_t **)matvar->data; |
| 2468 |
|
| 2469 |
|
| 2470 |
if ( matvar->nbytes == 0 || matvar->data_size == 0 || matvar->data == NULL ) |
| 2471 |
break; |
| 2472 |
nelems = matvar->nbytes / matvar->data_size; |
| 2473 |
for ( i = 0; i < nelems; i++ ) |
| 2474 |
WriteCompressedCellArrayField(mat, cells[i], z); |
| 2475 |
break; |
| 2476 |
} |
| 2477 |
case MAT_C_STRUCT: { |
| 2478 |
int buf_size = 512; |
| 2479 |
const mat_uint32_t fieldname_type = MAT_T_INT32; |
| 2480 |
const mat_uint32_t fieldname_data_size = 4; |
| 2481 |
unsigned char *padzero; |
| 2482 |
int fieldname_size; |
| 2483 |
size_t maxlen = 0, nfields, i, nelems_x_nfields; |
| 2484 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 2485 |
matvar_t **fields = (matvar_t **)matvar->data; |
| 2486 |
|
| 2487 |
nfields = matvar->internal->num_fields; |
| 2488 |
|
| 2489 |
if ( nfields < 1 ) { |
| 2490 |
fieldname_size = 1; |
| 2491 |
uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type; |
| 2492 |
uncomp_buf[1] = fieldname_size; |
| 2493 |
uncomp_buf[2] = array_name_type; |
| 2494 |
uncomp_buf[3] = 0; |
| 2495 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2496 |
z->avail_in = 16; |
| 2497 |
do { |
| 2498 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2499 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 2500 |
deflate(z, Z_NO_FLUSH); |
| 2501 |
byteswritten += fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, |
| 2502 |
(FILE *)mat->fp); |
| 2503 |
} while ( z->avail_out == 0 ); |
| 2504 |
break; |
| 2505 |
} |
| 2506 |
|
| 2507 |
for ( i = 0; i < nfields; i++ ) { |
| 2508 |
size_t len = strlen(matvar->internal->fieldnames[i]); |
| 2509 |
if ( len > maxlen ) |
| 2510 |
maxlen = len; |
| 2511 |
} |
| 2512 |
maxlen++; |
| 2513 |
fieldname_size = maxlen; |
| 2514 |
while ( nfields * fieldname_size % 8 != 0 ) |
| 2515 |
fieldname_size++; |
| 2516 |
uncomp_buf[0] = (fieldname_data_size << 16) | fieldname_type; |
| 2517 |
uncomp_buf[1] = fieldname_size; |
| 2518 |
uncomp_buf[2] = array_name_type; |
| 2519 |
uncomp_buf[3] = nfields * fieldname_size; |
| 2520 |
|
| 2521 |
padzero = (unsigned char *)calloc(fieldname_size, 1); |
| 2522 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2523 |
z->avail_in = 16; |
| 2524 |
do { |
| 2525 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2526 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 2527 |
deflate(z, Z_NO_FLUSH); |
| 2528 |
byteswritten += fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, |
| 2529 |
(FILE *)mat->fp); |
| 2530 |
} while ( z->avail_out == 0 ); |
| 2531 |
for ( i = 0; i < nfields; i++ ) { |
| 2532 |
size_t len = strlen(matvar->internal->fieldnames[i]); |
| 2533 |
memset(padzero, '\0', fieldname_size); |
| 2534 |
memcpy(padzero, matvar->internal->fieldnames[i], len); |
| 2535 |
z->next_in = ZLIB_BYTE_PTR(padzero); |
| 2536 |
z->avail_in = fieldname_size; |
| 2537 |
do { |
| 2538 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2539 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 2540 |
deflate(z, Z_NO_FLUSH); |
| 2541 |
byteswritten += fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, |
| 2542 |
(FILE *)mat->fp); |
| 2543 |
} while ( z->avail_out == 0 ); |
| 2544 |
} |
| 2545 |
free(padzero); |
| 2546 |
err = Mul(&nelems_x_nfields, nelems, nfields); |
| 2547 |
if ( err ) { |
| 2548 |
Mat_Critical("Integer multiplication overflow"); |
| 2549 |
return byteswritten; |
| 2550 |
} |
| 2551 |
for ( i = 0; i < nelems_x_nfields; i++ ) |
| 2552 |
byteswritten += WriteCompressedStructField(mat, fields[i], z); |
| 2553 |
break; |
| 2554 |
} |
| 2555 |
case MAT_C_SPARSE: { |
| 2556 |
mat_sparse_t *sparse = (mat_sparse_t *)matvar->data; |
| 2557 |
|
| 2558 |
byteswritten += WriteCompressedData(mat, z, sparse->ir, sparse->nir, MAT_T_UINT32); |
| 2559 |
byteswritten += WriteCompressedData(mat, z, sparse->jc, sparse->njc, MAT_T_UINT32); |
| 2560 |
if ( matvar->isComplex ) { |
| 2561 |
mat_complex_split_t *complex_data = (mat_complex_split_t *)sparse->data; |
| 2562 |
byteswritten += |
| 2563 |
WriteCompressedData(mat, z, complex_data->Re, sparse->ndata, matvar->data_type); |
| 2564 |
byteswritten += |
| 2565 |
WriteCompressedData(mat, z, complex_data->Im, sparse->ndata, matvar->data_type); |
| 2566 |
} else { |
| 2567 |
byteswritten += |
| 2568 |
WriteCompressedData(mat, z, sparse->data, sparse->ndata, matvar->data_type); |
| 2569 |
} |
| 2570 |
break; |
| 2571 |
} |
| 2572 |
case MAT_C_FUNCTION: |
| 2573 |
case MAT_C_OBJECT: |
| 2574 |
case MAT_C_EMPTY: |
| 2575 |
case MAT_C_OPAQUE: |
| 2576 |
break; |
| 2577 |
} |
| 2578 |
|
| 2579 |
return byteswritten; |
| 2580 |
} |
| 2581 |
|
| 2582 |
|
| 2583 |
|
| 2584 |
|
| 2585 |
|
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 2589 |
static size_t |
| 2590 |
WriteCompressedCellArrayField(mat_t *mat, matvar_t *matvar, z_streamp z) |
| 2591 |
{ |
| 2592 |
mat_uint32_t comp_buf[512]; |
| 2593 |
mat_uint32_t uncomp_buf[512]; |
| 2594 |
int buf_size = 512; |
| 2595 |
size_t byteswritten = 0, field_buf_size; |
| 2596 |
|
| 2597 |
if ( NULL == matvar || NULL == mat || NULL == z ) |
| 2598 |
return 0; |
| 2599 |
|
| 2600 |
memset(uncomp_buf, 0, sizeof(uncomp_buf)); |
| 2601 |
uncomp_buf[0] = MAT_T_MATRIX; |
| 2602 |
if ( MAT_C_EMPTY != matvar->class_type ) { |
| 2603 |
int err = GetCellArrayFieldBufSize(matvar, &field_buf_size); |
| 2604 |
if ( err || field_buf_size > UINT32_MAX ) |
| 2605 |
return 0; |
| 2606 |
|
| 2607 |
uncomp_buf[1] = field_buf_size; |
| 2608 |
} else { |
| 2609 |
uncomp_buf[1] = 0; |
| 2610 |
} |
| 2611 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2612 |
z->avail_in = 8; |
| 2613 |
do { |
| 2614 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2615 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 2616 |
deflate(z, Z_NO_FLUSH); |
| 2617 |
byteswritten += |
| 2618 |
fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); |
| 2619 |
} while ( z->avail_out == 0 ); |
| 2620 |
|
| 2621 |
byteswritten += WriteCompressedTypeArrayFlags(mat, matvar, z); |
| 2622 |
return byteswritten; |
| 2623 |
} |
| 2624 |
#endif |
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
static int |
| 2634 |
WriteStructField(mat_t *mat, matvar_t *matvar) |
| 2635 |
{ |
| 2636 |
mat_uint32_t array_flags; |
| 2637 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 2638 |
int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32; |
| 2639 |
int array_flags_size = 8, matrix_type = MAT_T_MATRIX; |
| 2640 |
const mat_uint32_t pad4 = 0; |
| 2641 |
int nBytes, i, nzmax = 0; |
| 2642 |
mat_off_t start = 0, end = 0; |
| 2643 |
|
| 2644 |
if ( mat == NULL ) |
| 2645 |
return MATIO_E_BAD_ARGUMENT; |
| 2646 |
|
| 2647 |
if ( NULL == matvar ) { |
| 2648 |
const size_t dims[2] = {0, 0}; |
| 2649 |
Mat_WriteEmptyVariable5(mat, NULL, 2, dims); |
| 2650 |
return MATIO_E_NO_ERROR; |
| 2651 |
} |
| 2652 |
|
| 2653 |
fwrite(&matrix_type, 4, 1, (FILE *)mat->fp); |
| 2654 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2655 |
if ( MAT_C_EMPTY == matvar->class_type ) { |
| 2656 |
|
| 2657 |
return MATIO_E_NO_ERROR; |
| 2658 |
} |
| 2659 |
start = ftello((FILE *)mat->fp); |
| 2660 |
|
| 2661 |
|
| 2662 |
array_flags = matvar->class_type & CLASS_TYPE_MASK; |
| 2663 |
if ( matvar->isComplex ) |
| 2664 |
array_flags |= MAT_F_COMPLEX; |
| 2665 |
if ( matvar->isGlobal ) |
| 2666 |
array_flags |= MAT_F_GLOBAL; |
| 2667 |
if ( matvar->isLogical ) |
| 2668 |
array_flags |= MAT_F_LOGICAL; |
| 2669 |
if ( matvar->class_type == MAT_C_SPARSE ) |
| 2670 |
nzmax = ((mat_sparse_t *)matvar->data)->nzmax; |
| 2671 |
|
| 2672 |
if ( mat->byteswap ) |
| 2673 |
array_flags = Mat_int32Swap((mat_int32_t *)&array_flags); |
| 2674 |
fwrite(&array_flags_type, 4, 1, (FILE *)mat->fp); |
| 2675 |
fwrite(&array_flags_size, 4, 1, (FILE *)mat->fp); |
| 2676 |
fwrite(&array_flags, 4, 1, (FILE *)mat->fp); |
| 2677 |
fwrite(&nzmax, 4, 1, (FILE *)mat->fp); |
| 2678 |
|
| 2679 |
nBytes = matvar->rank * 4; |
| 2680 |
fwrite(&dims_array_type, 4, 1, (FILE *)mat->fp); |
| 2681 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 2682 |
for ( i = 0; i < matvar->rank; i++ ) { |
| 2683 |
mat_int32_t dim; |
| 2684 |
dim = matvar->dims[i]; |
| 2685 |
fwrite(&dim, 4, 1, (FILE *)mat->fp); |
| 2686 |
} |
| 2687 |
if ( matvar->rank % 2 != 0 ) |
| 2688 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2689 |
|
| 2690 |
|
| 2691 |
fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 2692 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2693 |
|
| 2694 |
WriteType(mat, matvar); |
| 2695 |
end = ftello((FILE *)mat->fp); |
| 2696 |
if ( start != -1L && end != -1L ) { |
| 2697 |
nBytes = (int)(end - start); |
| 2698 |
(void)fseeko((FILE *)mat->fp, (mat_off_t) - (nBytes + 4), SEEK_CUR); |
| 2699 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 2700 |
(void)fseeko((FILE *)mat->fp, end, SEEK_SET); |
| 2701 |
} else { |
| 2702 |
Mat_Critical("Couldn't determine file position"); |
| 2703 |
} |
| 2704 |
|
| 2705 |
return MATIO_E_NO_ERROR; |
| 2706 |
} |
| 2707 |
|
| 2708 |
#if HAVE_ZLIB |
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
static size_t |
| 2718 |
WriteCompressedStructField(mat_t *mat, matvar_t *matvar, z_streamp z) |
| 2719 |
{ |
| 2720 |
mat_uint32_t comp_buf[512]; |
| 2721 |
mat_uint32_t uncomp_buf[512]; |
| 2722 |
int buf_size = 512; |
| 2723 |
size_t byteswritten = 0, field_buf_size; |
| 2724 |
|
| 2725 |
if ( NULL == mat || NULL == z ) |
| 2726 |
return 0; |
| 2727 |
|
| 2728 |
if ( NULL == matvar ) { |
| 2729 |
const size_t dims[2] = {0, 0}; |
| 2730 |
byteswritten = Mat_WriteCompressedEmptyVariable5(mat, NULL, 2, dims, z); |
| 2731 |
return byteswritten; |
| 2732 |
} |
| 2733 |
|
| 2734 |
memset(uncomp_buf, 0, sizeof(uncomp_buf)); |
| 2735 |
uncomp_buf[0] = MAT_T_MATRIX; |
| 2736 |
if ( MAT_C_EMPTY != matvar->class_type ) { |
| 2737 |
int err = GetStructFieldBufSize(matvar, &field_buf_size); |
| 2738 |
if ( err || field_buf_size > UINT32_MAX ) |
| 2739 |
return 0; |
| 2740 |
uncomp_buf[1] = field_buf_size; |
| 2741 |
} else { |
| 2742 |
uncomp_buf[1] = 0; |
| 2743 |
} |
| 2744 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2745 |
z->avail_in = 8; |
| 2746 |
do { |
| 2747 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2748 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 2749 |
deflate(z, Z_NO_FLUSH); |
| 2750 |
byteswritten += |
| 2751 |
fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); |
| 2752 |
} while ( z->avail_out == 0 ); |
| 2753 |
|
| 2754 |
byteswritten += WriteCompressedTypeArrayFlags(mat, matvar, z); |
| 2755 |
return byteswritten; |
| 2756 |
} |
| 2757 |
#endif |
| 2758 |
|
| 2759 |
static size_t |
| 2760 |
Mat_WriteEmptyVariable5(mat_t *mat, const char *name, int rank, const size_t *dims) |
| 2761 |
{ |
| 2762 |
mat_uint32_t array_flags; |
| 2763 |
mat_uint32_t array_name_type = MAT_T_INT8; |
| 2764 |
const mat_uint32_t matrix_type = MAT_T_MATRIX; |
| 2765 |
int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32; |
| 2766 |
int array_flags_size = 8, nBytes, i; |
| 2767 |
const mat_uint32_t pad4 = 0; |
| 2768 |
const mat_uint8_t pad1 = 0; |
| 2769 |
size_t byteswritten = 0; |
| 2770 |
mat_off_t start = 0, end = 0; |
| 2771 |
|
| 2772 |
fwrite(&matrix_type, 4, 1, (FILE *)mat->fp); |
| 2773 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2774 |
start = ftello((FILE *)mat->fp); |
| 2775 |
|
| 2776 |
|
| 2777 |
array_flags = MAT_C_DOUBLE; |
| 2778 |
|
| 2779 |
if ( mat->byteswap ) |
| 2780 |
array_flags = Mat_int32Swap((mat_int32_t *)&array_flags); |
| 2781 |
byteswritten += fwrite(&array_flags_type, 4, 1, (FILE *)mat->fp); |
| 2782 |
byteswritten += fwrite(&array_flags_size, 4, 1, (FILE *)mat->fp); |
| 2783 |
byteswritten += fwrite(&array_flags, 4, 1, (FILE *)mat->fp); |
| 2784 |
byteswritten += fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2785 |
|
| 2786 |
nBytes = rank * 4; |
| 2787 |
byteswritten += fwrite(&dims_array_type, 4, 1, (FILE *)mat->fp); |
| 2788 |
byteswritten += fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 2789 |
for ( i = 0; i < rank; i++ ) { |
| 2790 |
mat_int32_t dim; |
| 2791 |
dim = dims[i]; |
| 2792 |
byteswritten += fwrite(&dim, 4, 1, (FILE *)mat->fp); |
| 2793 |
} |
| 2794 |
if ( rank % 2 != 0 ) |
| 2795 |
byteswritten += fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2796 |
|
| 2797 |
if ( NULL == name ) { |
| 2798 |
|
| 2799 |
byteswritten += fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 2800 |
byteswritten += fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 2801 |
} else { |
| 2802 |
mat_int32_t array_name_len = (mat_int32_t)strlen(name); |
| 2803 |
|
| 2804 |
if ( array_name_len <= 4 ) { |
| 2805 |
array_name_type |= array_name_len << 16; |
| 2806 |
byteswritten += fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 2807 |
byteswritten += fwrite(name, 1, array_name_len, (FILE *)mat->fp); |
| 2808 |
for ( i = array_name_len; i < 4; i++ ) |
| 2809 |
byteswritten += fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2810 |
} else { |
| 2811 |
byteswritten += fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 2812 |
byteswritten += fwrite(&array_name_len, 4, 1, (FILE *)mat->fp); |
| 2813 |
byteswritten += fwrite(name, 1, array_name_len, (FILE *)mat->fp); |
| 2814 |
if ( array_name_len % 8 ) |
| 2815 |
for ( i = array_name_len % 8; i < 8; i++ ) |
| 2816 |
byteswritten += fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2817 |
} |
| 2818 |
} |
| 2819 |
|
| 2820 |
nBytes = WriteData(mat, NULL, 0, MAT_T_DOUBLE); |
| 2821 |
byteswritten += nBytes; |
| 2822 |
if ( nBytes % 8 ) |
| 2823 |
for ( i = nBytes % 8; i < 8; i++ ) |
| 2824 |
byteswritten += fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 2825 |
|
| 2826 |
end = ftello((FILE *)mat->fp); |
| 2827 |
if ( start != -1L && end != -1L ) { |
| 2828 |
nBytes = (int)(end - start); |
| 2829 |
(void)fseeko((FILE *)mat->fp, (mat_off_t) - (nBytes + 4), SEEK_CUR); |
| 2830 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 2831 |
(void)fseeko((FILE *)mat->fp, end, SEEK_SET); |
| 2832 |
} else { |
| 2833 |
Mat_Critical("Couldn't determine file position"); |
| 2834 |
} |
| 2835 |
|
| 2836 |
return byteswritten; |
| 2837 |
} |
| 2838 |
|
| 2839 |
#if HAVE_ZLIB |
| 2840 |
static size_t |
| 2841 |
Mat_WriteCompressedEmptyVariable5(mat_t *mat, const char *name, int rank, const size_t *dims, |
| 2842 |
z_streamp z) |
| 2843 |
{ |
| 2844 |
mat_uint32_t array_flags; |
| 2845 |
int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32; |
| 2846 |
int array_flags_size = 8; |
| 2847 |
int i, err; |
| 2848 |
size_t nBytes, empty_matrix_max_buf_size; |
| 2849 |
|
| 2850 |
mat_uint32_t comp_buf[512]; |
| 2851 |
mat_uint32_t uncomp_buf[512]; |
| 2852 |
int buf_size = 512; |
| 2853 |
size_t byteswritten = 0, buf_size_bytes; |
| 2854 |
|
| 2855 |
if ( NULL == mat || NULL == z ) |
| 2856 |
return byteswritten; |
| 2857 |
|
| 2858 |
buf_size_bytes = buf_size * sizeof(*comp_buf); |
| 2859 |
|
| 2860 |
|
| 2861 |
array_flags = MAT_C_DOUBLE; |
| 2862 |
|
| 2863 |
memset(uncomp_buf, 0, sizeof(uncomp_buf)); |
| 2864 |
uncomp_buf[0] = MAT_T_MATRIX; |
| 2865 |
err = GetEmptyMatrixMaxBufSize(name, rank, &empty_matrix_max_buf_size); |
| 2866 |
if ( err || empty_matrix_max_buf_size > UINT32_MAX ) |
| 2867 |
return byteswritten; |
| 2868 |
uncomp_buf[1] = empty_matrix_max_buf_size; |
| 2869 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2870 |
z->avail_in = 8; |
| 2871 |
do { |
| 2872 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2873 |
z->avail_out = buf_size_bytes; |
| 2874 |
deflate(z, Z_NO_FLUSH); |
| 2875 |
byteswritten += fwrite(comp_buf, 1, buf_size_bytes - z->avail_out, (FILE *)mat->fp); |
| 2876 |
} while ( z->avail_out == 0 ); |
| 2877 |
uncomp_buf[0] = array_flags_type; |
| 2878 |
uncomp_buf[1] = array_flags_size; |
| 2879 |
uncomp_buf[2] = array_flags; |
| 2880 |
uncomp_buf[3] = 0; |
| 2881 |
|
| 2882 |
nBytes = rank * 4; |
| 2883 |
uncomp_buf[4] = dims_array_type; |
| 2884 |
uncomp_buf[5] = nBytes; |
| 2885 |
for ( i = 0; i < rank; i++ ) { |
| 2886 |
mat_int32_t dim; |
| 2887 |
dim = dims[i]; |
| 2888 |
uncomp_buf[6 + i] = dim; |
| 2889 |
} |
| 2890 |
if ( rank % 2 != 0 ) { |
| 2891 |
const mat_uint32_t pad4 = 0; |
| 2892 |
uncomp_buf[6 + i] = pad4; |
| 2893 |
i++; |
| 2894 |
} |
| 2895 |
|
| 2896 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2897 |
z->avail_in = (6 + i) * sizeof(*uncomp_buf); |
| 2898 |
do { |
| 2899 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2900 |
z->avail_out = buf_size_bytes; |
| 2901 |
deflate(z, Z_NO_FLUSH); |
| 2902 |
byteswritten += fwrite(comp_buf, 1, buf_size_bytes - z->avail_out, (FILE *)mat->fp); |
| 2903 |
} while ( z->avail_out == 0 ); |
| 2904 |
|
| 2905 |
if ( NULL == name ) { |
| 2906 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 2907 |
uncomp_buf[0] = array_name_type; |
| 2908 |
uncomp_buf[1] = 0; |
| 2909 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2910 |
z->avail_in = 8; |
| 2911 |
do { |
| 2912 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2913 |
z->avail_out = buf_size_bytes; |
| 2914 |
deflate(z, Z_NO_FLUSH); |
| 2915 |
byteswritten += fwrite(comp_buf, 1, buf_size_bytes - z->avail_out, (FILE *)mat->fp); |
| 2916 |
} while ( z->avail_out == 0 ); |
| 2917 |
} else if ( strlen(name) <= 4 ) { |
| 2918 |
mat_uint32_t array_name_len = (mat_uint32_t)strlen(name); |
| 2919 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 2920 |
|
| 2921 |
memset(uncomp_buf, 0, 8); |
| 2922 |
uncomp_buf[0] = (array_name_len << 16) | array_name_type; |
| 2923 |
memcpy(uncomp_buf + 1, name, array_name_len); |
| 2924 |
|
| 2925 |
|
| 2926 |
|
| 2927 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2928 |
z->avail_in = 8; |
| 2929 |
do { |
| 2930 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2931 |
z->avail_out = buf_size_bytes; |
| 2932 |
deflate(z, Z_NO_FLUSH); |
| 2933 |
byteswritten += fwrite(comp_buf, 1, buf_size_bytes - z->avail_out, (FILE *)mat->fp); |
| 2934 |
} while ( z->avail_out == 0 ); |
| 2935 |
} else { |
| 2936 |
mat_uint32_t array_name_len = (mat_uint32_t)strlen(name); |
| 2937 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 2938 |
|
| 2939 |
memset(uncomp_buf, 0, buf_size * sizeof(*uncomp_buf)); |
| 2940 |
uncomp_buf[0] = array_name_type; |
| 2941 |
uncomp_buf[1] = array_name_len; |
| 2942 |
memcpy(uncomp_buf + 2, name, array_name_len); |
| 2943 |
if ( array_name_len % 8 ) |
| 2944 |
array_name_len += 8 - (array_name_len % 8); |
| 2945 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 2946 |
z->avail_in = 8 + array_name_len; |
| 2947 |
do { |
| 2948 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 2949 |
z->avail_out = buf_size_bytes; |
| 2950 |
deflate(z, Z_NO_FLUSH); |
| 2951 |
byteswritten += fwrite(comp_buf, 1, buf_size_bytes - z->avail_out, (FILE *)mat->fp); |
| 2952 |
} while ( z->avail_out == 0 ); |
| 2953 |
} |
| 2954 |
|
| 2955 |
byteswritten += WriteCompressedData(mat, z, NULL, 0, MAT_T_DOUBLE); |
| 2956 |
return byteswritten; |
| 2957 |
} |
| 2958 |
#endif |
| 2959 |
|
| 2960 |
|
| 2961 |
|
| 2962 |
|
| 2963 |
|
| 2964 |
|
| 2965 |
|
| 2966 |
|
| 2967 |
|
| 2968 |
|
| 2969 |
|
| 2970 |
|
| 2971 |
static int |
| 2972 |
Mat_VarReadNumeric5(mat_t *mat, matvar_t *matvar, void *data, size_t N) |
| 2973 |
{ |
| 2974 |
int data_in_tag = 0, err = MATIO_E_NO_ERROR; |
| 2975 |
size_t nBytes = 0; |
| 2976 |
enum matio_types packed_type = MAT_T_UNKNOWN; |
| 2977 |
mat_uint32_t tag[2] = {0, 0}; |
| 2978 |
|
| 2979 |
if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 2980 |
#if HAVE_ZLIB |
| 2981 |
matvar->internal->z->avail_in = 0; |
| 2982 |
err = Inflate(mat, matvar->internal->z, tag, 4, NULL); |
| 2983 |
if ( err ) { |
| 2984 |
return err; |
| 2985 |
} |
| 2986 |
if ( mat->byteswap ) |
| 2987 |
(void)Mat_uint32Swap(tag); |
| 2988 |
|
| 2989 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 2990 |
if ( tag[0] & 0xffff0000 ) { |
| 2991 |
data_in_tag = 1; |
| 2992 |
nBytes = (tag[0] & 0xffff0000) >> 16; |
| 2993 |
} else { |
| 2994 |
data_in_tag = 0; |
| 2995 |
err = Inflate(mat, matvar->internal->z, tag + 1, 4, NULL); |
| 2996 |
if ( err ) { |
| 2997 |
return err; |
| 2998 |
} |
| 2999 |
if ( mat->byteswap ) |
| 3000 |
(void)Mat_uint32Swap(tag + 1); |
| 3001 |
nBytes = tag[1]; |
| 3002 |
} |
| 3003 |
#endif |
| 3004 |
} else { |
| 3005 |
err = Read(tag, 4, 1, (FILE *)mat->fp, NULL); |
| 3006 |
if ( err ) { |
| 3007 |
return err; |
| 3008 |
} |
| 3009 |
if ( mat->byteswap ) |
| 3010 |
(void)Mat_uint32Swap(tag); |
| 3011 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 3012 |
if ( tag[0] & 0xffff0000 ) { |
| 3013 |
data_in_tag = 1; |
| 3014 |
nBytes = (tag[0] & 0xffff0000) >> 16; |
| 3015 |
} else { |
| 3016 |
data_in_tag = 0; |
| 3017 |
err = Read(tag + 1, 4, 1, (FILE *)mat->fp, NULL); |
| 3018 |
if ( err ) { |
| 3019 |
return err; |
| 3020 |
} |
| 3021 |
if ( mat->byteswap ) |
| 3022 |
(void)Mat_uint32Swap(tag + 1); |
| 3023 |
nBytes = tag[1]; |
| 3024 |
} |
| 3025 |
} |
| 3026 |
if ( nBytes == 0 ) { |
| 3027 |
matvar->nbytes = 0; |
| 3028 |
return err; |
| 3029 |
} |
| 3030 |
|
| 3031 |
if ( matvar->compression == MAT_COMPRESSION_NONE ) { |
| 3032 |
switch ( matvar->class_type ) { |
| 3033 |
case MAT_C_DOUBLE: |
| 3034 |
err = ReadDoubleData(mat, (double *)data, packed_type, N); |
| 3035 |
break; |
| 3036 |
case MAT_C_SINGLE: |
| 3037 |
err = ReadSingleData(mat, (float *)data, packed_type, N); |
| 3038 |
break; |
| 3039 |
case MAT_C_INT64: |
| 3040 |
#ifdef HAVE_MAT_INT64_T |
| 3041 |
err = ReadInt64Data(mat, (mat_int64_t *)data, packed_type, N); |
| 3042 |
#endif |
| 3043 |
break; |
| 3044 |
case MAT_C_UINT64: |
| 3045 |
#ifdef HAVE_MAT_UINT64_T |
| 3046 |
err = ReadUInt64Data(mat, (mat_uint64_t *)data, packed_type, N); |
| 3047 |
#endif |
| 3048 |
break; |
| 3049 |
case MAT_C_INT32: |
| 3050 |
err = ReadInt32Data(mat, (mat_int32_t *)data, packed_type, N); |
| 3051 |
break; |
| 3052 |
case MAT_C_UINT32: |
| 3053 |
err = ReadUInt32Data(mat, (mat_uint32_t *)data, packed_type, N); |
| 3054 |
break; |
| 3055 |
case MAT_C_INT16: |
| 3056 |
err = ReadInt16Data(mat, (mat_int16_t *)data, packed_type, N); |
| 3057 |
break; |
| 3058 |
case MAT_C_UINT16: |
| 3059 |
err = ReadUInt16Data(mat, (mat_uint16_t *)data, packed_type, N); |
| 3060 |
break; |
| 3061 |
case MAT_C_INT8: |
| 3062 |
err = ReadInt8Data(mat, (mat_int8_t *)data, packed_type, N); |
| 3063 |
break; |
| 3064 |
case MAT_C_UINT8: |
| 3065 |
err = ReadUInt8Data(mat, (mat_uint8_t *)data, packed_type, N); |
| 3066 |
break; |
| 3067 |
default: |
| 3068 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 3069 |
break; |
| 3070 |
} |
| 3071 |
if ( err ) { |
| 3072 |
return err; |
| 3073 |
} |
| 3074 |
err = Mul(&nBytes, N, Mat_SizeOf(packed_type)); |
| 3075 |
if ( err ) { |
| 3076 |
return err; |
| 3077 |
} |
| 3078 |
|
| 3079 |
|
| 3080 |
|
| 3081 |
|
| 3082 |
if ( data_in_tag ) |
| 3083 |
nBytes += 4; |
| 3084 |
if ( (nBytes % 8) != 0 ) |
| 3085 |
(void)fseeko((FILE *)mat->fp, 8 - (nBytes % 8), SEEK_CUR); |
| 3086 |
#if HAVE_ZLIB |
| 3087 |
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 3088 |
switch ( matvar->class_type ) { |
| 3089 |
case MAT_C_DOUBLE: |
| 3090 |
err = ReadCompressedDoubleData(mat, matvar->internal->z, (double *)data, |
| 3091 |
packed_type, N); |
| 3092 |
break; |
| 3093 |
case MAT_C_SINGLE: |
| 3094 |
err = ReadCompressedSingleData(mat, matvar->internal->z, (float *)data, packed_type, |
| 3095 |
N); |
| 3096 |
break; |
| 3097 |
case MAT_C_INT64: |
| 3098 |
#ifdef HAVE_MAT_INT64_T |
| 3099 |
err = ReadCompressedInt64Data(mat, matvar->internal->z, (mat_int64_t *)data, |
| 3100 |
packed_type, N); |
| 3101 |
#endif |
| 3102 |
break; |
| 3103 |
case MAT_C_UINT64: |
| 3104 |
#ifdef HAVE_MAT_UINT64_T |
| 3105 |
err = ReadCompressedUInt64Data(mat, matvar->internal->z, (mat_uint64_t *)data, |
| 3106 |
packed_type, N); |
| 3107 |
#endif |
| 3108 |
break; |
| 3109 |
case MAT_C_INT32: |
| 3110 |
err = ReadCompressedInt32Data(mat, matvar->internal->z, (mat_int32_t *)data, |
| 3111 |
packed_type, N); |
| 3112 |
break; |
| 3113 |
case MAT_C_UINT32: |
| 3114 |
err = ReadCompressedUInt32Data(mat, matvar->internal->z, (mat_uint32_t *)data, |
| 3115 |
packed_type, N); |
| 3116 |
break; |
| 3117 |
case MAT_C_INT16: |
| 3118 |
err = ReadCompressedInt16Data(mat, matvar->internal->z, (mat_int16_t *)data, |
| 3119 |
packed_type, N); |
| 3120 |
break; |
| 3121 |
case MAT_C_UINT16: |
| 3122 |
err = ReadCompressedUInt16Data(mat, matvar->internal->z, (mat_uint16_t *)data, |
| 3123 |
packed_type, N); |
| 3124 |
break; |
| 3125 |
case MAT_C_INT8: |
| 3126 |
err = ReadCompressedInt8Data(mat, matvar->internal->z, (mat_int8_t *)data, |
| 3127 |
packed_type, N); |
| 3128 |
break; |
| 3129 |
case MAT_C_UINT8: |
| 3130 |
err = ReadCompressedUInt8Data(mat, matvar->internal->z, (mat_uint8_t *)data, |
| 3131 |
packed_type, N); |
| 3132 |
break; |
| 3133 |
default: |
| 3134 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 3135 |
break; |
| 3136 |
} |
| 3137 |
if ( err ) { |
| 3138 |
return err; |
| 3139 |
} |
| 3140 |
err = Mul(&nBytes, N, Mat_SizeOf(packed_type)); |
| 3141 |
if ( err ) { |
| 3142 |
return err; |
| 3143 |
} |
| 3144 |
|
| 3145 |
|
| 3146 |
|
| 3147 |
|
| 3148 |
if ( data_in_tag ) |
| 3149 |
nBytes += 4; |
| 3150 |
if ( (nBytes % 8) != 0 ) |
| 3151 |
err = InflateSkip(mat, matvar->internal->z, 8 - (nBytes % 8), NULL); |
| 3152 |
#endif |
| 3153 |
} |
| 3154 |
return err; |
| 3155 |
} |
| 3156 |
|
| 3157 |
|
| 3158 |
|
| 3159 |
|
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
int |
| 3167 |
Mat_VarRead5(mat_t *mat, matvar_t *matvar) |
| 3168 |
{ |
| 3169 |
int byteswap, data_in_tag = 0, err; |
| 3170 |
size_t nelems = 1; |
| 3171 |
enum matio_types packed_type = MAT_T_UNKNOWN; |
| 3172 |
mat_off_t fpos; |
| 3173 |
mat_uint32_t tag[2] = {0, 0}; |
| 3174 |
size_t bytesread = 0; |
| 3175 |
|
| 3176 |
if ( matvar == NULL ) |
| 3177 |
return MATIO_E_BAD_ARGUMENT; |
| 3178 |
if ( matvar->rank == 0 ) |
| 3179 |
return MATIO_E_NO_ERROR; |
| 3180 |
#if HAVE_ZLIB |
| 3181 |
if ( NULL != matvar->internal->data ) { |
| 3182 |
|
| 3183 |
matvar->data = matvar->internal->data; |
| 3184 |
matvar->internal->data = NULL; |
| 3185 |
return MATIO_E_NO_ERROR; |
| 3186 |
} |
| 3187 |
#endif |
| 3188 |
fpos = ftello((FILE *)mat->fp); |
| 3189 |
if ( fpos == -1L ) { |
| 3190 |
Mat_Critical("Couldn't determine file position"); |
| 3191 |
return MATIO_E_GENERIC_READ_ERROR; |
| 3192 |
} |
| 3193 |
err = Mat_MulDims(matvar, &nelems); |
| 3194 |
if ( err ) { |
| 3195 |
Mat_Critical("Integer multiplication overflow"); |
| 3196 |
return err; |
| 3197 |
} |
| 3198 |
byteswap = mat->byteswap; |
| 3199 |
switch ( matvar->class_type ) { |
| 3200 |
case MAT_C_EMPTY: |
| 3201 |
matvar->nbytes = 0; |
| 3202 |
matvar->data_size = sizeof(double); |
| 3203 |
matvar->data_type = MAT_T_DOUBLE; |
| 3204 |
matvar->rank = 2; |
| 3205 |
if ( NULL != matvar->dims ) { |
| 3206 |
free(matvar->dims); |
| 3207 |
} |
| 3208 |
matvar->dims = (size_t *)calloc(matvar->rank, sizeof(*(matvar->dims))); |
| 3209 |
break; |
| 3210 |
case MAT_C_DOUBLE: |
| 3211 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3212 |
matvar->data_size = sizeof(double); |
| 3213 |
matvar->data_type = MAT_T_DOUBLE; |
| 3214 |
break; |
| 3215 |
case MAT_C_SINGLE: |
| 3216 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3217 |
matvar->data_size = sizeof(float); |
| 3218 |
matvar->data_type = MAT_T_SINGLE; |
| 3219 |
break; |
| 3220 |
case MAT_C_INT64: |
| 3221 |
#ifdef HAVE_MAT_INT64_T |
| 3222 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3223 |
matvar->data_size = sizeof(mat_int64_t); |
| 3224 |
matvar->data_type = MAT_T_INT64; |
| 3225 |
#endif |
| 3226 |
break; |
| 3227 |
case MAT_C_UINT64: |
| 3228 |
#ifdef HAVE_MAT_UINT64_T |
| 3229 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3230 |
matvar->data_size = sizeof(mat_uint64_t); |
| 3231 |
matvar->data_type = MAT_T_UINT64; |
| 3232 |
#endif |
| 3233 |
break; |
| 3234 |
case MAT_C_INT32: |
| 3235 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3236 |
matvar->data_size = sizeof(mat_int32_t); |
| 3237 |
matvar->data_type = MAT_T_INT32; |
| 3238 |
break; |
| 3239 |
case MAT_C_UINT32: |
| 3240 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3241 |
matvar->data_size = sizeof(mat_uint32_t); |
| 3242 |
matvar->data_type = MAT_T_UINT32; |
| 3243 |
break; |
| 3244 |
case MAT_C_INT16: |
| 3245 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3246 |
matvar->data_size = sizeof(mat_int16_t); |
| 3247 |
matvar->data_type = MAT_T_INT16; |
| 3248 |
break; |
| 3249 |
case MAT_C_UINT16: |
| 3250 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3251 |
matvar->data_size = sizeof(mat_uint16_t); |
| 3252 |
matvar->data_type = MAT_T_UINT16; |
| 3253 |
break; |
| 3254 |
case MAT_C_INT8: |
| 3255 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3256 |
matvar->data_size = sizeof(mat_int8_t); |
| 3257 |
matvar->data_type = MAT_T_INT8; |
| 3258 |
break; |
| 3259 |
case MAT_C_UINT8: |
| 3260 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3261 |
matvar->data_size = sizeof(mat_uint8_t); |
| 3262 |
matvar->data_type = MAT_T_UINT8; |
| 3263 |
break; |
| 3264 |
case MAT_C_CHAR: |
| 3265 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3266 |
if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 3267 |
#if HAVE_ZLIB |
| 3268 |
if ( matvar->internal->z == NULL ) { |
| 3269 |
break; |
| 3270 |
} |
| 3271 |
matvar->internal->z->avail_in = 0; |
| 3272 |
err = Inflate(mat, matvar->internal->z, tag, 4, &bytesread); |
| 3273 |
if ( err ) { |
| 3274 |
break; |
| 3275 |
} |
| 3276 |
if ( byteswap ) |
| 3277 |
(void)Mat_uint32Swap(tag); |
| 3278 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 3279 |
if ( tag[0] & 0xffff0000 ) { |
| 3280 |
data_in_tag = 1; |
| 3281 |
matvar->nbytes = (tag[0] & 0xffff0000) >> 16; |
| 3282 |
} else { |
| 3283 |
data_in_tag = 0; |
| 3284 |
err = Inflate(mat, matvar->internal->z, tag + 1, 4, &bytesread); |
| 3285 |
if ( err ) { |
| 3286 |
break; |
| 3287 |
} |
| 3288 |
if ( byteswap ) |
| 3289 |
(void)Mat_uint32Swap(tag + 1); |
| 3290 |
matvar->nbytes = tag[1]; |
| 3291 |
} |
| 3292 |
#else |
| 3293 |
matvar->nbytes = 0; |
| 3294 |
#endif |
| 3295 |
matvar->data_type = packed_type; |
| 3296 |
matvar->data_size = Mat_SizeOf(matvar->data_type); |
| 3297 |
} else { |
| 3298 |
err = Read(tag, 4, 1, (FILE *)mat->fp, &bytesread); |
| 3299 |
if ( err ) { |
| 3300 |
break; |
| 3301 |
} |
| 3302 |
if ( byteswap ) |
| 3303 |
(void)Mat_uint32Swap(tag); |
| 3304 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 3305 |
if ( tag[0] & 0xffff0000 ) { |
| 3306 |
data_in_tag = 1; |
| 3307 |
matvar->nbytes = (tag[0] & 0xffff0000) >> 16; |
| 3308 |
} else { |
| 3309 |
data_in_tag = 0; |
| 3310 |
err = Read(tag + 1, 4, 1, (FILE *)mat->fp, &bytesread); |
| 3311 |
if ( err ) { |
| 3312 |
break; |
| 3313 |
} |
| 3314 |
if ( byteswap ) |
| 3315 |
(void)Mat_uint32Swap(tag + 1); |
| 3316 |
matvar->nbytes = tag[1]; |
| 3317 |
} |
| 3318 |
matvar->data_type = packed_type; |
| 3319 |
matvar->data_size = Mat_SizeOf(matvar->data_type); |
| 3320 |
} |
| 3321 |
if ( matvar->isComplex ) { |
| 3322 |
break; |
| 3323 |
} |
| 3324 |
if ( 0 == matvar->nbytes ) { |
| 3325 |
break; |
| 3326 |
} |
| 3327 |
matvar->data = calloc(matvar->nbytes, 1); |
| 3328 |
if ( NULL == matvar->data ) { |
| 3329 |
err = MATIO_E_OUT_OF_MEMORY; |
| 3330 |
Mat_Critical("Couldn't allocate memory for the data"); |
| 3331 |
break; |
| 3332 |
} |
| 3333 |
{ |
| 3334 |
size_t nbytes = 0; |
| 3335 |
err = Mul(&nbytes, nelems, matvar->data_size); |
| 3336 |
if ( err || nbytes > matvar->nbytes ) { |
| 3337 |
free(matvar->data); |
| 3338 |
matvar->data = NULL; |
| 3339 |
matvar->nbytes = 0; |
| 3340 |
break; |
| 3341 |
} |
| 3342 |
} |
| 3343 |
if ( matvar->data_type == MAT_T_UTF8 ) { |
| 3344 |
nelems = matvar->nbytes; |
| 3345 |
} |
| 3346 |
if ( matvar->compression == MAT_COMPRESSION_NONE ) { |
| 3347 |
size_t nBytes; |
| 3348 |
err = ReadCharData(mat, matvar->data, matvar->data_type, nelems); |
| 3349 |
if ( err ) { |
| 3350 |
free(matvar->data); |
| 3351 |
matvar->data = NULL; |
| 3352 |
matvar->nbytes = 0; |
| 3353 |
break; |
| 3354 |
} |
| 3355 |
err = Mul(&nBytes, nelems, Mat_SizeOf(matvar->data_type)); |
| 3356 |
if ( err ) { |
| 3357 |
free(matvar->data); |
| 3358 |
matvar->data = NULL; |
| 3359 |
matvar->nbytes = 0; |
| 3360 |
break; |
| 3361 |
} |
| 3362 |
|
| 3363 |
|
| 3364 |
|
| 3365 |
|
| 3366 |
if ( data_in_tag ) |
| 3367 |
nBytes += 4; |
| 3368 |
if ( (nBytes % 8) != 0 ) |
| 3369 |
(void)fseeko((FILE *)mat->fp, 8 - (nBytes % 8), SEEK_CUR); |
| 3370 |
#if HAVE_ZLIB |
| 3371 |
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 3372 |
size_t nBytes; |
| 3373 |
err = ReadCompressedCharData(mat, matvar->internal->z, matvar->data, |
| 3374 |
matvar->data_type, nelems); |
| 3375 |
if ( err ) { |
| 3376 |
free(matvar->data); |
| 3377 |
matvar->data = NULL; |
| 3378 |
matvar->nbytes = 0; |
| 3379 |
break; |
| 3380 |
} |
| 3381 |
err = Mul(&nBytes, nelems, Mat_SizeOf(matvar->data_type)); |
| 3382 |
if ( err ) { |
| 3383 |
free(matvar->data); |
| 3384 |
matvar->data = NULL; |
| 3385 |
matvar->nbytes = 0; |
| 3386 |
break; |
| 3387 |
} |
| 3388 |
|
| 3389 |
|
| 3390 |
|
| 3391 |
|
| 3392 |
if ( data_in_tag ) |
| 3393 |
nBytes += 4; |
| 3394 |
if ( (nBytes % 8) != 0 ) |
| 3395 |
err = InflateSkip(mat, matvar->internal->z, 8 - (nBytes % 8), NULL); |
| 3396 |
if ( err ) { |
| 3397 |
free(matvar->data); |
| 3398 |
matvar->data = NULL; |
| 3399 |
matvar->nbytes = 0; |
| 3400 |
break; |
| 3401 |
} |
| 3402 |
#endif |
| 3403 |
} |
| 3404 |
break; |
| 3405 |
case MAT_C_STRUCT: { |
| 3406 |
matvar_t **fields; |
| 3407 |
size_t i, nelems_x_nfields; |
| 3408 |
|
| 3409 |
matvar->data_type = MAT_T_STRUCT; |
| 3410 |
err = Mul(&nelems_x_nfields, nelems, matvar->internal->num_fields); |
| 3411 |
if ( err || !matvar->nbytes || !matvar->data_size || NULL == matvar->data ) |
| 3412 |
break; |
| 3413 |
fields = (matvar_t **)matvar->data; |
| 3414 |
for ( i = 0; i < nelems_x_nfields; i++ ) { |
| 3415 |
if ( NULL != fields[i] ) { |
| 3416 |
err = Mat_VarRead5(mat, fields[i]); |
| 3417 |
if ( err ) |
| 3418 |
break; |
| 3419 |
} |
| 3420 |
} |
| 3421 |
break; |
| 3422 |
} |
| 3423 |
case MAT_C_CELL: { |
| 3424 |
matvar_t **cells; |
| 3425 |
size_t i; |
| 3426 |
|
| 3427 |
if ( NULL == matvar->data ) { |
| 3428 |
Mat_Critical("Data is NULL for cell array %s", matvar->name); |
| 3429 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 3430 |
break; |
| 3431 |
} |
| 3432 |
cells = (matvar_t **)matvar->data; |
| 3433 |
for ( i = 0; i < nelems; i++ ) { |
| 3434 |
if ( NULL != cells[i] ) { |
| 3435 |
err = Mat_VarRead5(mat, cells[i]); |
| 3436 |
if ( err ) |
| 3437 |
break; |
| 3438 |
} |
| 3439 |
} |
| 3440 |
|
| 3441 |
matvar->data_type = MAT_T_CELL; |
| 3442 |
break; |
| 3443 |
} |
| 3444 |
case MAT_C_SPARSE: { |
| 3445 |
mat_uint32_t N = 0; |
| 3446 |
mat_sparse_t *sparse; |
| 3447 |
|
| 3448 |
matvar->data_size = sizeof(mat_sparse_t); |
| 3449 |
matvar->data = calloc(1, matvar->data_size); |
| 3450 |
if ( matvar->data == NULL ) { |
| 3451 |
err = MATIO_E_OUT_OF_MEMORY; |
| 3452 |
Mat_Critical("Mat_VarRead5: Allocation of data pointer failed"); |
| 3453 |
break; |
| 3454 |
} |
| 3455 |
sparse = (mat_sparse_t *)matvar->data; |
| 3456 |
sparse->nzmax = matvar->nbytes; |
| 3457 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 3458 |
|
| 3459 |
err = ReadSparse(mat, matvar, &sparse->nir, &sparse->ir); |
| 3460 |
if ( err ) { |
| 3461 |
free(sparse->ir); |
| 3462 |
free(matvar->data); |
| 3463 |
matvar->data = NULL; |
| 3464 |
break; |
| 3465 |
} |
| 3466 |
|
| 3467 |
err = ReadSparse(mat, matvar, &sparse->njc, &sparse->jc); |
| 3468 |
if ( err ) { |
| 3469 |
free(sparse->jc); |
| 3470 |
free(sparse->ir); |
| 3471 |
free(matvar->data); |
| 3472 |
matvar->data = NULL; |
| 3473 |
break; |
| 3474 |
} |
| 3475 |
|
| 3476 |
if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 3477 |
#if HAVE_ZLIB |
| 3478 |
matvar->internal->z->avail_in = 0; |
| 3479 |
err = Inflate(mat, matvar->internal->z, tag, 4, &bytesread); |
| 3480 |
if ( err ) { |
| 3481 |
free(matvar->data); |
| 3482 |
matvar->data = NULL; |
| 3483 |
break; |
| 3484 |
} |
| 3485 |
if ( mat->byteswap ) |
| 3486 |
(void)Mat_uint32Swap(tag); |
| 3487 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 3488 |
if ( tag[0] & 0xffff0000 ) { |
| 3489 |
data_in_tag = 1; |
| 3490 |
N = (tag[0] & 0xffff0000) >> 16; |
| 3491 |
} else { |
| 3492 |
data_in_tag = 0; |
| 3493 |
err = ReadCompressedUInt32Data(mat, matvar->internal->z, &N, MAT_T_UINT32, 1); |
| 3494 |
} |
| 3495 |
#endif |
| 3496 |
} else { |
| 3497 |
err = Read(tag, 4, 1, (FILE *)mat->fp, &bytesread); |
| 3498 |
if ( err ) { |
| 3499 |
free(matvar->data); |
| 3500 |
matvar->data = NULL; |
| 3501 |
break; |
| 3502 |
} |
| 3503 |
if ( mat->byteswap ) |
| 3504 |
(void)Mat_uint32Swap(tag); |
| 3505 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 3506 |
if ( tag[0] & 0xffff0000 ) { |
| 3507 |
data_in_tag = 1; |
| 3508 |
N = (tag[0] & 0xffff0000) >> 16; |
| 3509 |
} else { |
| 3510 |
data_in_tag = 0; |
| 3511 |
err = Read(&N, 4, 1, (FILE *)mat->fp, &bytesread); |
| 3512 |
if ( err ) { |
| 3513 |
free(matvar->data); |
| 3514 |
matvar->data = NULL; |
| 3515 |
break; |
| 3516 |
} |
| 3517 |
if ( mat->byteswap ) |
| 3518 |
(void)Mat_uint32Swap(&N); |
| 3519 |
} |
| 3520 |
} |
| 3521 |
if ( matvar->isLogical && packed_type == MAT_T_DOUBLE ) { |
| 3522 |
|
| 3523 |
|
| 3524 |
|
| 3525 |
packed_type = MAT_T_UINT8; |
| 3526 |
} |
| 3527 |
#if defined(EXTENDED_SPARSE) |
| 3528 |
matvar->data_type = packed_type; |
| 3529 |
#else |
| 3530 |
matvar->data_type = MAT_T_DOUBLE; |
| 3531 |
#endif |
| 3532 |
{ |
| 3533 |
size_t s_type = Mat_SizeOf(packed_type); |
| 3534 |
if ( s_type == 0 ) |
| 3535 |
break; |
| 3536 |
sparse->ndata = N / s_type; |
| 3537 |
} |
| 3538 |
if ( matvar->isComplex ) { |
| 3539 |
mat_complex_split_t *complex_data; |
| 3540 |
size_t nBytes = 0; |
| 3541 |
err = Mul(&nBytes, sparse->ndata, Mat_SizeOf(matvar->data_type)); |
| 3542 |
if ( err ) { |
| 3543 |
Mat_Critical("Integer multiplication overflow"); |
| 3544 |
break; |
| 3545 |
} |
| 3546 |
complex_data = ComplexMalloc(nBytes); |
| 3547 |
if ( NULL == complex_data ) { |
| 3548 |
err = MATIO_E_OUT_OF_MEMORY; |
| 3549 |
Mat_Critical("Couldn't allocate memory for the complex sparse data"); |
| 3550 |
break; |
| 3551 |
} |
| 3552 |
if ( matvar->compression == MAT_COMPRESSION_NONE ) { |
| 3553 |
#if defined(EXTENDED_SPARSE) |
| 3554 |
switch ( matvar->data_type ) { |
| 3555 |
case MAT_T_DOUBLE: |
| 3556 |
err = ReadDoubleData(mat, (double *)complex_data->Re, packed_type, |
| 3557 |
sparse->ndata); |
| 3558 |
break; |
| 3559 |
case MAT_T_SINGLE: |
| 3560 |
err = ReadSingleData(mat, (float *)complex_data->Re, packed_type, |
| 3561 |
sparse->ndata); |
| 3562 |
break; |
| 3563 |
case MAT_T_INT64: |
| 3564 |
#ifdef HAVE_MAT_INT64_T |
| 3565 |
err = ReadInt64Data(mat, (mat_int64_t *)complex_data->Re, packed_type, |
| 3566 |
sparse->ndata); |
| 3567 |
#endif |
| 3568 |
break; |
| 3569 |
case MAT_T_UINT64: |
| 3570 |
#ifdef HAVE_MAT_UINT64_T |
| 3571 |
err = ReadUInt64Data(mat, (mat_uint64_t *)complex_data->Re, packed_type, |
| 3572 |
sparse->ndata); |
| 3573 |
#endif |
| 3574 |
break; |
| 3575 |
case MAT_T_INT32: |
| 3576 |
err = ReadInt32Data(mat, (mat_int32_t *)complex_data->Re, packed_type, |
| 3577 |
sparse->ndata); |
| 3578 |
break; |
| 3579 |
case MAT_T_UINT32: |
| 3580 |
err = ReadUInt32Data(mat, (mat_uint32_t *)complex_data->Re, packed_type, |
| 3581 |
sparse->ndata); |
| 3582 |
break; |
| 3583 |
case MAT_T_INT16: |
| 3584 |
err = ReadInt16Data(mat, (mat_int16_t *)complex_data->Re, packed_type, |
| 3585 |
sparse->ndata); |
| 3586 |
break; |
| 3587 |
case MAT_T_UINT16: |
| 3588 |
err = ReadUInt16Data(mat, (mat_uint16_t *)complex_data->Re, packed_type, |
| 3589 |
sparse->ndata); |
| 3590 |
break; |
| 3591 |
case MAT_T_INT8: |
| 3592 |
err = ReadInt8Data(mat, (mat_int8_t *)complex_data->Re, packed_type, |
| 3593 |
sparse->ndata); |
| 3594 |
break; |
| 3595 |
case MAT_T_UINT8: |
| 3596 |
err = ReadUInt8Data(mat, (mat_uint8_t *)complex_data->Re, packed_type, |
| 3597 |
sparse->ndata); |
| 3598 |
break; |
| 3599 |
default: |
| 3600 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 3601 |
break; |
| 3602 |
} |
| 3603 |
#else |
| 3604 |
err = |
| 3605 |
ReadDoubleData(mat, (double *)complex_data->Re, packed_type, sparse->ndata); |
| 3606 |
#endif |
| 3607 |
if ( err ) { |
| 3608 |
ComplexFree(complex_data); |
| 3609 |
break; |
| 3610 |
} |
| 3611 |
err = Mul(&nBytes, sparse->ndata, Mat_SizeOf(packed_type)); |
| 3612 |
if ( err ) { |
| 3613 |
ComplexFree(complex_data); |
| 3614 |
break; |
| 3615 |
} |
| 3616 |
if ( data_in_tag ) |
| 3617 |
nBytes += 4; |
| 3618 |
if ( (nBytes % 8) != 0 ) |
| 3619 |
(void)fseeko((FILE *)mat->fp, 8 - (nBytes % 8), SEEK_CUR); |
| 3620 |
|
| 3621 |
err = Read(tag, 4, 1, (FILE *)mat->fp, &bytesread); |
| 3622 |
if ( err ) { |
| 3623 |
ComplexFree(complex_data); |
| 3624 |
break; |
| 3625 |
} |
| 3626 |
if ( byteswap ) |
| 3627 |
(void)Mat_uint32Swap(tag); |
| 3628 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 3629 |
if ( tag[0] & 0xffff0000 ) { |
| 3630 |
data_in_tag = 1; |
| 3631 |
} else { |
| 3632 |
data_in_tag = 0; |
| 3633 |
err = Read(tag + 1, 4, 1, (FILE *)mat->fp, &bytesread); |
| 3634 |
if ( err ) { |
| 3635 |
ComplexFree(complex_data); |
| 3636 |
break; |
| 3637 |
} |
| 3638 |
if ( byteswap ) |
| 3639 |
(void)Mat_uint32Swap(tag + 1); |
| 3640 |
} |
| 3641 |
#if defined(EXTENDED_SPARSE) |
| 3642 |
switch ( matvar->data_type ) { |
| 3643 |
case MAT_T_DOUBLE: |
| 3644 |
err = ReadDoubleData(mat, (double *)complex_data->Im, packed_type, |
| 3645 |
sparse->ndata); |
| 3646 |
break; |
| 3647 |
case MAT_T_SINGLE: |
| 3648 |
err = ReadSingleData(mat, (float *)complex_data->Im, packed_type, |
| 3649 |
sparse->ndata); |
| 3650 |
break; |
| 3651 |
case MAT_T_INT64: |
| 3652 |
#ifdef HAVE_MAT_INT64_T |
| 3653 |
err = ReadInt64Data(mat, (mat_int64_t *)complex_data->Im, packed_type, |
| 3654 |
sparse->ndata); |
| 3655 |
#endif |
| 3656 |
break; |
| 3657 |
case MAT_T_UINT64: |
| 3658 |
#ifdef HAVE_MAT_UINT64_T |
| 3659 |
err = ReadUInt64Data(mat, (mat_uint64_t *)complex_data->Im, packed_type, |
| 3660 |
sparse->ndata); |
| 3661 |
#endif |
| 3662 |
break; |
| 3663 |
case MAT_T_INT32: |
| 3664 |
err = ReadInt32Data(mat, (mat_int32_t *)complex_data->Im, packed_type, |
| 3665 |
sparse->ndata); |
| 3666 |
break; |
| 3667 |
case MAT_T_UINT32: |
| 3668 |
err = ReadUInt32Data(mat, (mat_uint32_t *)complex_data->Im, packed_type, |
| 3669 |
sparse->ndata); |
| 3670 |
break; |
| 3671 |
case MAT_T_INT16: |
| 3672 |
err = ReadInt16Data(mat, (mat_int16_t *)complex_data->Im, packed_type, |
| 3673 |
sparse->ndata); |
| 3674 |
break; |
| 3675 |
case MAT_T_UINT16: |
| 3676 |
err = ReadUInt16Data(mat, (mat_uint16_t *)complex_data->Im, packed_type, |
| 3677 |
sparse->ndata); |
| 3678 |
break; |
| 3679 |
case MAT_T_INT8: |
| 3680 |
err = ReadInt8Data(mat, (mat_int8_t *)complex_data->Im, packed_type, |
| 3681 |
sparse->ndata); |
| 3682 |
break; |
| 3683 |
case MAT_T_UINT8: |
| 3684 |
err = ReadUInt8Data(mat, (mat_uint8_t *)complex_data->Im, packed_type, |
| 3685 |
sparse->ndata); |
| 3686 |
break; |
| 3687 |
default: |
| 3688 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 3689 |
break; |
| 3690 |
} |
| 3691 |
#else |
| 3692 |
err = |
| 3693 |
ReadDoubleData(mat, (double *)complex_data->Im, packed_type, sparse->ndata); |
| 3694 |
#endif |
| 3695 |
if ( err ) { |
| 3696 |
ComplexFree(complex_data); |
| 3697 |
break; |
| 3698 |
} |
| 3699 |
err = Mul(&nBytes, sparse->ndata, Mat_SizeOf(packed_type)); |
| 3700 |
if ( err ) { |
| 3701 |
ComplexFree(complex_data); |
| 3702 |
break; |
| 3703 |
} |
| 3704 |
if ( data_in_tag ) |
| 3705 |
nBytes += 4; |
| 3706 |
if ( (nBytes % 8) != 0 ) |
| 3707 |
(void)fseeko((FILE *)mat->fp, 8 - (nBytes % 8), SEEK_CUR); |
| 3708 |
#if HAVE_ZLIB |
| 3709 |
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 3710 |
#if defined(EXTENDED_SPARSE) |
| 3711 |
switch ( matvar->data_type ) { |
| 3712 |
case MAT_T_DOUBLE: |
| 3713 |
err = ReadCompressedDoubleData(mat, matvar->internal->z, |
| 3714 |
(double *)complex_data->Re, packed_type, |
| 3715 |
sparse->ndata); |
| 3716 |
break; |
| 3717 |
case MAT_T_SINGLE: |
| 3718 |
err = ReadCompressedSingleData(mat, matvar->internal->z, |
| 3719 |
(float *)complex_data->Re, packed_type, |
| 3720 |
sparse->ndata); |
| 3721 |
break; |
| 3722 |
case MAT_T_INT64: |
| 3723 |
#ifdef HAVE_MAT_INT64_T |
| 3724 |
err = ReadCompressedInt64Data(mat, matvar->internal->z, |
| 3725 |
(mat_int64_t *)complex_data->Re, |
| 3726 |
packed_type, sparse->ndata); |
| 3727 |
#endif |
| 3728 |
break; |
| 3729 |
case MAT_T_UINT64: |
| 3730 |
#ifdef HAVE_MAT_UINT64_T |
| 3731 |
err = ReadCompressedUInt64Data(mat, matvar->internal->z, |
| 3732 |
(mat_uint64_t *)complex_data->Re, |
| 3733 |
packed_type, sparse->ndata); |
| 3734 |
#endif |
| 3735 |
break; |
| 3736 |
case MAT_T_INT32: |
| 3737 |
err = ReadCompressedInt32Data(mat, matvar->internal->z, |
| 3738 |
(mat_int32_t *)complex_data->Re, |
| 3739 |
packed_type, sparse->ndata); |
| 3740 |
break; |
| 3741 |
case MAT_T_UINT32: |
| 3742 |
err = ReadCompressedUInt32Data(mat, matvar->internal->z, |
| 3743 |
(mat_uint32_t *)complex_data->Re, |
| 3744 |
packed_type, sparse->ndata); |
| 3745 |
break; |
| 3746 |
case MAT_T_INT16: |
| 3747 |
err = ReadCompressedInt16Data(mat, matvar->internal->z, |
| 3748 |
(mat_int16_t *)complex_data->Re, |
| 3749 |
packed_type, sparse->ndata); |
| 3750 |
break; |
| 3751 |
case MAT_T_UINT16: |
| 3752 |
err = ReadCompressedUInt16Data(mat, matvar->internal->z, |
| 3753 |
(mat_uint16_t *)complex_data->Re, |
| 3754 |
packed_type, sparse->ndata); |
| 3755 |
break; |
| 3756 |
case MAT_T_INT8: |
| 3757 |
err = ReadCompressedInt8Data(mat, matvar->internal->z, |
| 3758 |
(mat_int8_t *)complex_data->Re, |
| 3759 |
packed_type, sparse->ndata); |
| 3760 |
break; |
| 3761 |
case MAT_T_UINT8: |
| 3762 |
err = ReadCompressedUInt8Data(mat, matvar->internal->z, |
| 3763 |
(mat_uint8_t *)complex_data->Re, |
| 3764 |
packed_type, sparse->ndata); |
| 3765 |
break; |
| 3766 |
default: |
| 3767 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 3768 |
break; |
| 3769 |
} |
| 3770 |
#else |
| 3771 |
err = ReadCompressedDoubleData(mat, matvar->internal->z, |
| 3772 |
(double *)complex_data->Re, packed_type, |
| 3773 |
sparse->ndata); |
| 3774 |
#endif |
| 3775 |
if ( err ) { |
| 3776 |
ComplexFree(complex_data); |
| 3777 |
break; |
| 3778 |
} |
| 3779 |
err = Mul(&nBytes, sparse->ndata, Mat_SizeOf(packed_type)); |
| 3780 |
if ( err ) { |
| 3781 |
ComplexFree(complex_data); |
| 3782 |
break; |
| 3783 |
} |
| 3784 |
if ( data_in_tag ) |
| 3785 |
nBytes += 4; |
| 3786 |
if ( (nBytes % 8) != 0 ) |
| 3787 |
err = InflateSkip(mat, matvar->internal->z, 8 - (nBytes % 8), NULL); |
| 3788 |
if ( err ) { |
| 3789 |
ComplexFree(complex_data); |
| 3790 |
break; |
| 3791 |
} |
| 3792 |
|
| 3793 |
|
| 3794 |
err = Inflate(mat, matvar->internal->z, tag, 4, NULL); |
| 3795 |
if ( err ) { |
| 3796 |
ComplexFree(complex_data); |
| 3797 |
break; |
| 3798 |
} |
| 3799 |
if ( byteswap ) |
| 3800 |
(void)Mat_uint32Swap(tag); |
| 3801 |
|
| 3802 |
packed_type = TYPE_FROM_TAG(tag[0]); |
| 3803 |
if ( tag[0] & 0xffff0000 ) { |
| 3804 |
data_in_tag = 1; |
| 3805 |
} else { |
| 3806 |
data_in_tag = 0; |
| 3807 |
err = Inflate(mat, matvar->internal->z, tag + 1, 4, NULL); |
| 3808 |
if ( err ) { |
| 3809 |
ComplexFree(complex_data); |
| 3810 |
break; |
| 3811 |
} |
| 3812 |
if ( byteswap ) |
| 3813 |
(void)Mat_uint32Swap(tag + 1); |
| 3814 |
} |
| 3815 |
#if defined(EXTENDED_SPARSE) |
| 3816 |
switch ( matvar->data_type ) { |
| 3817 |
case MAT_T_DOUBLE: |
| 3818 |
err = ReadCompressedDoubleData(mat, matvar->internal->z, |
| 3819 |
(double *)complex_data->Im, packed_type, |
| 3820 |
sparse->ndata); |
| 3821 |
break; |
| 3822 |
case MAT_T_SINGLE: |
| 3823 |
err = ReadCompressedSingleData(mat, matvar->internal->z, |
| 3824 |
(float *)complex_data->Im, packed_type, |
| 3825 |
sparse->ndata); |
| 3826 |
break; |
| 3827 |
case MAT_T_INT64: |
| 3828 |
#ifdef HAVE_MAT_INT64_T |
| 3829 |
err = ReadCompressedInt64Data(mat, matvar->internal->z, |
| 3830 |
(mat_int64_t *)complex_data->Im, |
| 3831 |
packed_type, sparse->ndata); |
| 3832 |
#endif |
| 3833 |
break; |
| 3834 |
case MAT_T_UINT64: |
| 3835 |
#ifdef HAVE_MAT_UINT64_T |
| 3836 |
err = ReadCompressedUInt64Data(mat, matvar->internal->z, |
| 3837 |
(mat_uint64_t *)complex_data->Im, |
| 3838 |
packed_type, sparse->ndata); |
| 3839 |
#endif |
| 3840 |
break; |
| 3841 |
case MAT_T_INT32: |
| 3842 |
err = ReadCompressedInt32Data(mat, matvar->internal->z, |
| 3843 |
(mat_int32_t *)complex_data->Im, |
| 3844 |
packed_type, sparse->ndata); |
| 3845 |
break; |
| 3846 |
case MAT_T_UINT32: |
| 3847 |
err = ReadCompressedUInt32Data(mat, matvar->internal->z, |
| 3848 |
(mat_uint32_t *)complex_data->Im, |
| 3849 |
packed_type, sparse->ndata); |
| 3850 |
break; |
| 3851 |
case MAT_T_INT16: |
| 3852 |
err = ReadCompressedInt16Data(mat, matvar->internal->z, |
| 3853 |
(mat_int16_t *)complex_data->Im, |
| 3854 |
packed_type, sparse->ndata); |
| 3855 |
break; |
| 3856 |
case MAT_T_UINT16: |
| 3857 |
err = ReadCompressedUInt16Data(mat, matvar->internal->z, |
| 3858 |
(mat_uint16_t *)complex_data->Im, |
| 3859 |
packed_type, sparse->ndata); |
| 3860 |
break; |
| 3861 |
case MAT_T_INT8: |
| 3862 |
err = ReadCompressedInt8Data(mat, matvar->internal->z, |
| 3863 |
(mat_int8_t *)complex_data->Im, |
| 3864 |
packed_type, sparse->ndata); |
| 3865 |
break; |
| 3866 |
case MAT_T_UINT8: |
| 3867 |
err = ReadCompressedUInt8Data(mat, matvar->internal->z, |
| 3868 |
(mat_uint8_t *)complex_data->Im, |
| 3869 |
packed_type, sparse->ndata); |
| 3870 |
break; |
| 3871 |
default: |
| 3872 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 3873 |
break; |
| 3874 |
} |
| 3875 |
#else |
| 3876 |
err = ReadCompressedDoubleData(mat, matvar->internal->z, |
| 3877 |
(double *)complex_data->Im, packed_type, |
| 3878 |
sparse->ndata); |
| 3879 |
#endif |
| 3880 |
if ( err ) { |
| 3881 |
ComplexFree(complex_data); |
| 3882 |
break; |
| 3883 |
} |
| 3884 |
err = Mul(&nBytes, sparse->ndata, Mat_SizeOf(packed_type)); |
| 3885 |
if ( err ) { |
| 3886 |
ComplexFree(complex_data); |
| 3887 |
break; |
| 3888 |
} |
| 3889 |
if ( data_in_tag ) |
| 3890 |
nBytes += 4; |
| 3891 |
if ( (nBytes % 8) != 0 ) |
| 3892 |
err = InflateSkip(mat, matvar->internal->z, 8 - (nBytes % 8), NULL); |
| 3893 |
if ( err ) { |
| 3894 |
ComplexFree(complex_data); |
| 3895 |
break; |
| 3896 |
} |
| 3897 |
#endif |
| 3898 |
} |
| 3899 |
sparse->data = complex_data; |
| 3900 |
} else { |
| 3901 |
size_t nBytes = 0; |
| 3902 |
err = Mul(&nBytes, sparse->ndata, Mat_SizeOf(matvar->data_type)); |
| 3903 |
if ( err ) { |
| 3904 |
Mat_Critical("Integer multiplication overflow"); |
| 3905 |
break; |
| 3906 |
} |
| 3907 |
if ( nBytes > 0 ) { |
| 3908 |
sparse->data = malloc(nBytes); |
| 3909 |
if ( sparse->data == NULL ) { |
| 3910 |
err = MATIO_E_OUT_OF_MEMORY; |
| 3911 |
Mat_Critical("Couldn't allocate memory for the sparse data"); |
| 3912 |
break; |
| 3913 |
} |
| 3914 |
} |
| 3915 |
if ( matvar->compression == MAT_COMPRESSION_NONE && nBytes > 0 ) { |
| 3916 |
#if defined(EXTENDED_SPARSE) |
| 3917 |
switch ( matvar->data_type ) { |
| 3918 |
case MAT_T_DOUBLE: |
| 3919 |
err = ReadDoubleData(mat, (double *)sparse->data, packed_type, |
| 3920 |
sparse->ndata); |
| 3921 |
break; |
| 3922 |
case MAT_T_SINGLE: |
| 3923 |
err = ReadSingleData(mat, (float *)sparse->data, packed_type, |
| 3924 |
sparse->ndata); |
| 3925 |
break; |
| 3926 |
case MAT_T_INT64: |
| 3927 |
#ifdef HAVE_MAT_INT64_T |
| 3928 |
err = ReadInt64Data(mat, (mat_int64_t *)sparse->data, packed_type, |
| 3929 |
sparse->ndata); |
| 3930 |
#endif |
| 3931 |
break; |
| 3932 |
case MAT_T_UINT64: |
| 3933 |
#ifdef HAVE_MAT_UINT64_T |
| 3934 |
err = ReadUInt64Data(mat, (mat_uint64_t *)sparse->data, packed_type, |
| 3935 |
sparse->ndata); |
| 3936 |
#endif |
| 3937 |
break; |
| 3938 |
case MAT_T_INT32: |
| 3939 |
err = ReadInt32Data(mat, (mat_int32_t *)sparse->data, packed_type, |
| 3940 |
sparse->ndata); |
| 3941 |
break; |
| 3942 |
case MAT_T_UINT32: |
| 3943 |
err = ReadUInt32Data(mat, (mat_uint32_t *)sparse->data, packed_type, |
| 3944 |
sparse->ndata); |
| 3945 |
break; |
| 3946 |
case MAT_T_INT16: |
| 3947 |
err = ReadInt16Data(mat, (mat_int16_t *)sparse->data, packed_type, |
| 3948 |
sparse->ndata); |
| 3949 |
break; |
| 3950 |
case MAT_T_UINT16: |
| 3951 |
err = ReadUInt16Data(mat, (mat_uint16_t *)sparse->data, packed_type, |
| 3952 |
sparse->ndata); |
| 3953 |
break; |
| 3954 |
case MAT_T_INT8: |
| 3955 |
err = ReadInt8Data(mat, (mat_int8_t *)sparse->data, packed_type, |
| 3956 |
sparse->ndata); |
| 3957 |
break; |
| 3958 |
case MAT_T_UINT8: |
| 3959 |
err = ReadUInt8Data(mat, (mat_uint8_t *)sparse->data, packed_type, |
| 3960 |
sparse->ndata); |
| 3961 |
break; |
| 3962 |
default: |
| 3963 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 3964 |
break; |
| 3965 |
} |
| 3966 |
#else |
| 3967 |
err = ReadDoubleData(mat, (double *)sparse->data, packed_type, sparse->ndata); |
| 3968 |
#endif |
| 3969 |
if ( err ) { |
| 3970 |
free(sparse->data); |
| 3971 |
sparse->data = NULL; |
| 3972 |
break; |
| 3973 |
} |
| 3974 |
err = Mul(&nBytes, sparse->ndata, Mat_SizeOf(packed_type)); |
| 3975 |
if ( err ) { |
| 3976 |
free(sparse->data); |
| 3977 |
sparse->data = NULL; |
| 3978 |
break; |
| 3979 |
} |
| 3980 |
if ( data_in_tag ) |
| 3981 |
nBytes += 4; |
| 3982 |
if ( (nBytes % 8) != 0 ) |
| 3983 |
(void)fseeko((FILE *)mat->fp, 8 - (nBytes % 8), SEEK_CUR); |
| 3984 |
#if HAVE_ZLIB |
| 3985 |
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB && nBytes > 0 ) { |
| 3986 |
#if defined(EXTENDED_SPARSE) |
| 3987 |
switch ( matvar->data_type ) { |
| 3988 |
case MAT_T_DOUBLE: |
| 3989 |
err = ReadCompressedDoubleData(mat, matvar->internal->z, |
| 3990 |
(double *)sparse->data, packed_type, |
| 3991 |
sparse->ndata); |
| 3992 |
break; |
| 3993 |
case MAT_T_SINGLE: |
| 3994 |
err = ReadCompressedSingleData(mat, matvar->internal->z, |
| 3995 |
(float *)sparse->data, packed_type, |
| 3996 |
sparse->ndata); |
| 3997 |
break; |
| 3998 |
case MAT_T_INT64: |
| 3999 |
#ifdef HAVE_MAT_INT64_T |
| 4000 |
err = ReadCompressedInt64Data(mat, matvar->internal->z, |
| 4001 |
(mat_int64_t *)sparse->data, packed_type, |
| 4002 |
sparse->ndata); |
| 4003 |
#endif |
| 4004 |
break; |
| 4005 |
case MAT_T_UINT64: |
| 4006 |
#ifdef HAVE_MAT_UINT64_T |
| 4007 |
err = ReadCompressedUInt64Data(mat, matvar->internal->z, |
| 4008 |
(mat_uint64_t *)sparse->data, |
| 4009 |
packed_type, sparse->ndata); |
| 4010 |
#endif |
| 4011 |
break; |
| 4012 |
case MAT_T_INT32: |
| 4013 |
err = ReadCompressedInt32Data(mat, matvar->internal->z, |
| 4014 |
(mat_int32_t *)sparse->data, packed_type, |
| 4015 |
sparse->ndata); |
| 4016 |
break; |
| 4017 |
case MAT_T_UINT32: |
| 4018 |
err = ReadCompressedUInt32Data(mat, matvar->internal->z, |
| 4019 |
(mat_uint32_t *)sparse->data, |
| 4020 |
packed_type, sparse->ndata); |
| 4021 |
break; |
| 4022 |
case MAT_T_INT16: |
| 4023 |
err = ReadCompressedInt16Data(mat, matvar->internal->z, |
| 4024 |
(mat_int16_t *)sparse->data, packed_type, |
| 4025 |
sparse->ndata); |
| 4026 |
break; |
| 4027 |
case MAT_T_UINT16: |
| 4028 |
err = ReadCompressedUInt16Data(mat, matvar->internal->z, |
| 4029 |
(mat_uint16_t *)sparse->data, |
| 4030 |
packed_type, sparse->ndata); |
| 4031 |
break; |
| 4032 |
case MAT_T_INT8: |
| 4033 |
err = ReadCompressedInt8Data(mat, matvar->internal->z, |
| 4034 |
(mat_int8_t *)sparse->data, packed_type, |
| 4035 |
sparse->ndata); |
| 4036 |
break; |
| 4037 |
case MAT_T_UINT8: |
| 4038 |
err = ReadCompressedUInt8Data(mat, matvar->internal->z, |
| 4039 |
(mat_uint8_t *)sparse->data, packed_type, |
| 4040 |
sparse->ndata); |
| 4041 |
break; |
| 4042 |
default: |
| 4043 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 4044 |
break; |
| 4045 |
} |
| 4046 |
#else |
| 4047 |
err = ReadCompressedDoubleData(mat, matvar->internal->z, (double *)sparse->data, |
| 4048 |
packed_type, sparse->ndata); |
| 4049 |
#endif |
| 4050 |
if ( err ) { |
| 4051 |
free(sparse->data); |
| 4052 |
sparse->data = NULL; |
| 4053 |
break; |
| 4054 |
} |
| 4055 |
err = Mul(&nBytes, sparse->ndata, Mat_SizeOf(packed_type)); |
| 4056 |
if ( err ) { |
| 4057 |
free(sparse->data); |
| 4058 |
sparse->data = NULL; |
| 4059 |
break; |
| 4060 |
} |
| 4061 |
if ( data_in_tag ) |
| 4062 |
nBytes += 4; |
| 4063 |
if ( (nBytes % 8) != 0 ) |
| 4064 |
err = InflateSkip(mat, matvar->internal->z, 8 - (nBytes % 8), NULL); |
| 4065 |
if ( err ) { |
| 4066 |
free(sparse->data); |
| 4067 |
sparse->data = NULL; |
| 4068 |
break; |
| 4069 |
} |
| 4070 |
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 4071 |
if ( data_in_tag ) |
| 4072 |
nBytes = 4; |
| 4073 |
if ( (nBytes % 8) != 0 ) |
| 4074 |
err = InflateSkip(mat, matvar->internal->z, 8 - (nBytes % 8), NULL); |
| 4075 |
if ( err ) { |
| 4076 |
free(sparse->data); |
| 4077 |
sparse->data = NULL; |
| 4078 |
break; |
| 4079 |
} |
| 4080 |
#endif |
| 4081 |
} |
| 4082 |
} |
| 4083 |
break; |
| 4084 |
} |
| 4085 |
case MAT_C_FUNCTION: { |
| 4086 |
matvar_t **functions; |
| 4087 |
size_t nfunctions = 0; |
| 4088 |
|
| 4089 |
if ( !matvar->nbytes || !matvar->data_size ) |
| 4090 |
break; |
| 4091 |
nfunctions = matvar->nbytes / matvar->data_size; |
| 4092 |
functions = (matvar_t **)matvar->data; |
| 4093 |
if ( NULL != functions ) { |
| 4094 |
size_t i; |
| 4095 |
for ( i = 0; i < nfunctions; i++ ) { |
| 4096 |
err = Mat_VarRead5(mat, functions[i]); |
| 4097 |
if ( err ) |
| 4098 |
break; |
| 4099 |
} |
| 4100 |
} |
| 4101 |
|
| 4102 |
matvar->data_type = MAT_T_FUNCTION; |
| 4103 |
break; |
| 4104 |
} |
| 4105 |
case MAT_C_OBJECT: |
| 4106 |
Mat_Warning("Mat_VarRead5: %d is not a supported class", matvar->class_type); |
| 4107 |
break; |
| 4108 |
default: |
| 4109 |
err = MATIO_E_OPERATION_NOT_SUPPORTED; |
| 4110 |
Mat_Critical("Mat_VarRead5: %d is not a supported class", matvar->class_type); |
| 4111 |
break; |
| 4112 |
} |
| 4113 |
switch ( matvar->class_type ) { |
| 4114 |
case MAT_C_DOUBLE: |
| 4115 |
case MAT_C_SINGLE: |
| 4116 |
#ifdef HAVE_MAT_INT64_T |
| 4117 |
case MAT_C_INT64: |
| 4118 |
#endif |
| 4119 |
#ifdef HAVE_MAT_UINT64_T |
| 4120 |
case MAT_C_UINT64: |
| 4121 |
#endif |
| 4122 |
case MAT_C_INT32: |
| 4123 |
case MAT_C_UINT32: |
| 4124 |
case MAT_C_INT16: |
| 4125 |
case MAT_C_UINT16: |
| 4126 |
case MAT_C_INT8: |
| 4127 |
case MAT_C_UINT8: |
| 4128 |
if ( matvar->isComplex ) { |
| 4129 |
mat_complex_split_t *complex_data; |
| 4130 |
|
| 4131 |
err = Mul(&matvar->nbytes, nelems, matvar->data_size); |
| 4132 |
if ( err ) { |
| 4133 |
Mat_Critical("Integer multiplication overflow"); |
| 4134 |
break; |
| 4135 |
} |
| 4136 |
|
| 4137 |
complex_data = ComplexMalloc(matvar->nbytes); |
| 4138 |
if ( NULL == complex_data ) { |
| 4139 |
err = MATIO_E_OUT_OF_MEMORY; |
| 4140 |
Mat_Critical("Couldn't allocate memory for the complex data"); |
| 4141 |
break; |
| 4142 |
} |
| 4143 |
|
| 4144 |
err = Mat_VarReadNumeric5(mat, matvar, complex_data->Re, nelems); |
| 4145 |
if ( err ) { |
| 4146 |
ComplexFree(complex_data); |
| 4147 |
break; |
| 4148 |
} |
| 4149 |
err = Mat_VarReadNumeric5(mat, matvar, complex_data->Im, nelems); |
| 4150 |
if ( err ) { |
| 4151 |
ComplexFree(complex_data); |
| 4152 |
break; |
| 4153 |
} |
| 4154 |
matvar->data = complex_data; |
| 4155 |
} else { |
| 4156 |
void *data; |
| 4157 |
err = Mul(&matvar->nbytes, nelems, matvar->data_size); |
| 4158 |
if ( err ) { |
| 4159 |
Mat_Critical("Integer multiplication overflow"); |
| 4160 |
break; |
| 4161 |
} |
| 4162 |
|
| 4163 |
data = malloc(matvar->nbytes); |
| 4164 |
if ( NULL == data ) { |
| 4165 |
err = MATIO_E_OUT_OF_MEMORY; |
| 4166 |
Mat_Critical("Couldn't allocate memory for the data"); |
| 4167 |
break; |
| 4168 |
} |
| 4169 |
err = Mat_VarReadNumeric5(mat, matvar, data, nelems); |
| 4170 |
if ( err ) { |
| 4171 |
free(data); |
| 4172 |
break; |
| 4173 |
} |
| 4174 |
matvar->data = data; |
| 4175 |
} |
| 4176 |
default: |
| 4177 |
break; |
| 4178 |
} |
| 4179 |
(void)fseeko((FILE *)mat->fp, fpos, SEEK_SET); |
| 4180 |
|
| 4181 |
return err; |
| 4182 |
} |
| 4183 |
|
| 4184 |
#if HAVE_ZLIB |
| 4185 |
#define GET_DATA_SLABN_RANK_LOOP \ |
| 4186 |
do { \ |
| 4187 |
for ( j = 1; j < rank; j++ ) { \ |
| 4188 |
cnt[j]++; \ |
| 4189 |
if ( (cnt[j] % edge[j]) == 0 ) { \ |
| 4190 |
cnt[j] = 0; \ |
| 4191 |
if ( (I % dimp[j]) != 0 ) { \ |
| 4192 |
ptr_in += dimp[j] - (I % dimp[j]) + dimp[j - 1] * start[j]; \ |
| 4193 |
I += dimp[j] - (I % dimp[j]) + dimp[j - 1] * start[j]; \ |
| 4194 |
} else if ( start[j] ) { \ |
| 4195 |
ptr_in += dimp[j - 1] * start[j]; \ |
| 4196 |
I += dimp[j - 1] * start[j]; \ |
| 4197 |
} \ |
| 4198 |
} else { \ |
| 4199 |
I += inc[j]; \ |
| 4200 |
ptr_in += inc[j]; \ |
| 4201 |
break; \ |
| 4202 |
} \ |
| 4203 |
} \ |
| 4204 |
} while ( 0 ) |
| 4205 |
|
| 4206 |
#define GET_DATA_SLAB2(T) \ |
| 4207 |
do { \ |
| 4208 |
ptr_in += start[1] * dims[0] + start[0]; \ |
| 4209 |
for ( i = 0; i < edge[1]; i++ ) { \ |
| 4210 |
for ( j = 0; j < edge[0]; j++ ) { \ |
| 4211 |
*ptr = (T)(*(ptr_in + j * stride[0])); \ |
| 4212 |
ptr++; \ |
| 4213 |
} \ |
| 4214 |
ptr_in += stride[1] * dims[0]; \ |
| 4215 |
} \ |
| 4216 |
} while ( 0 ) |
| 4217 |
|
| 4218 |
#define GET_DATA_SLABN(T) \ |
| 4219 |
do { \ |
| 4220 |
inc[0] = stride[0] - 1; \ |
| 4221 |
dimp[0] = (int)dims[0]; \ |
| 4222 |
N = edge[0]; \ |
| 4223 |
I = 0; \ |
| 4224 |
for ( i = 1; i < rank; i++ ) { \ |
| 4225 |
inc[i] = stride[i] - 1; \ |
| 4226 |
dimp[i] = (int)dims[i - 1]; \ |
| 4227 |
for ( j = i; j--; ) { \ |
| 4228 |
inc[i] *= (int)dims[j]; \ |
| 4229 |
dimp[i] *= (int)dims[j + 1]; \ |
| 4230 |
} \ |
| 4231 |
N *= edge[i]; \ |
| 4232 |
I += dimp[i - 1] * start[i]; \ |
| 4233 |
} \ |
| 4234 |
ptr_in += I; \ |
| 4235 |
if ( stride[0] == 1 ) { \ |
| 4236 |
for ( i = 0; i < N; i += edge[0] ) { \ |
| 4237 |
int k; \ |
| 4238 |
if ( start[0] ) { \ |
| 4239 |
ptr_in += start[0]; \ |
| 4240 |
I += start[0]; \ |
| 4241 |
} \ |
| 4242 |
for ( k = 0; k < edge[0]; k++ ) { \ |
| 4243 |
*(ptr + i + k) = (T)(*(ptr_in + k)); \ |
| 4244 |
} \ |
| 4245 |
I += (int)dims[0] - start[0]; \ |
| 4246 |
ptr_in += dims[0] - start[0]; \ |
| 4247 |
GET_DATA_SLABN_RANK_LOOP; \ |
| 4248 |
} \ |
| 4249 |
} else { \ |
| 4250 |
for ( i = 0; i < N; i += edge[0] ) { \ |
| 4251 |
if ( start[0] ) { \ |
| 4252 |
ptr_in += start[0]; \ |
| 4253 |
I += start[0]; \ |
| 4254 |
} \ |
| 4255 |
for ( j = 0; j < edge[0]; j++ ) { \ |
| 4256 |
*(ptr + i + j) = (T)(*ptr_in); \ |
| 4257 |
ptr_in += stride[0]; \ |
| 4258 |
I += stride[0]; \ |
| 4259 |
} \ |
| 4260 |
I += (int)dims[0] - (ptrdiff_t)edge[0] * stride[0] - start[0]; \ |
| 4261 |
ptr_in += dims[0] - (ptrdiff_t)edge[0] * stride[0] - start[0]; \ |
| 4262 |
GET_DATA_SLABN_RANK_LOOP; \ |
| 4263 |
} \ |
| 4264 |
} \ |
| 4265 |
} while ( 0 ) |
| 4266 |
|
| 4267 |
#ifdef HAVE_MAT_INT64_T |
| 4268 |
#define GET_DATA_SLAB2_INT64(T) \ |
| 4269 |
do { \ |
| 4270 |
if ( MAT_T_INT64 == data_type ) { \ |
| 4271 |
mat_int64_t *ptr_in = (mat_int64_t *)data_in; \ |
| 4272 |
GET_DATA_SLAB2(T); \ |
| 4273 |
err = MATIO_E_NO_ERROR; \ |
| 4274 |
} \ |
| 4275 |
} while ( 0 ) |
| 4276 |
#else |
| 4277 |
#define GET_DATA_SLAB2_INT64(T) |
| 4278 |
#endif |
| 4279 |
|
| 4280 |
#ifdef HAVE_MAT_UINT64_T |
| 4281 |
#define GET_DATA_SLAB2_UINT64(T) \ |
| 4282 |
do { \ |
| 4283 |
if ( MAT_T_UINT64 == data_type ) { \ |
| 4284 |
mat_uint64_t *ptr_in = (mat_uint64_t *)data_in; \ |
| 4285 |
GET_DATA_SLAB2(T); \ |
| 4286 |
err = MATIO_E_NO_ERROR; \ |
| 4287 |
} \ |
| 4288 |
} while ( 0 ) |
| 4289 |
#else |
| 4290 |
#define GET_DATA_SLAB2_UINT64(T) |
| 4291 |
#endif |
| 4292 |
|
| 4293 |
#define GET_DATA_SLAB2_TYPE(T) \ |
| 4294 |
do { \ |
| 4295 |
switch ( data_type ) { \ |
| 4296 |
case MAT_T_DOUBLE: { \ |
| 4297 |
double *ptr_in = (double *)data_in; \ |
| 4298 |
GET_DATA_SLAB2(T); \ |
| 4299 |
break; \ |
| 4300 |
} \ |
| 4301 |
case MAT_T_SINGLE: { \ |
| 4302 |
float *ptr_in = (float *)data_in; \ |
| 4303 |
GET_DATA_SLAB2(T); \ |
| 4304 |
break; \ |
| 4305 |
} \ |
| 4306 |
case MAT_T_INT32: { \ |
| 4307 |
mat_int32_t *ptr_in = (mat_int32_t *)data_in; \ |
| 4308 |
GET_DATA_SLAB2(T); \ |
| 4309 |
break; \ |
| 4310 |
} \ |
| 4311 |
case MAT_T_UINT32: { \ |
| 4312 |
mat_uint32_t *ptr_in = (mat_uint32_t *)data_in; \ |
| 4313 |
GET_DATA_SLAB2(T); \ |
| 4314 |
break; \ |
| 4315 |
} \ |
| 4316 |
case MAT_T_INT16: { \ |
| 4317 |
mat_int16_t *ptr_in = (mat_int16_t *)data_in; \ |
| 4318 |
GET_DATA_SLAB2(T); \ |
| 4319 |
break; \ |
| 4320 |
} \ |
| 4321 |
case MAT_T_UINT16: { \ |
| 4322 |
mat_uint16_t *ptr_in = (mat_uint16_t *)data_in; \ |
| 4323 |
GET_DATA_SLAB2(T); \ |
| 4324 |
break; \ |
| 4325 |
} \ |
| 4326 |
case MAT_T_INT8: { \ |
| 4327 |
mat_int8_t *ptr_in = (mat_int8_t *)data_in; \ |
| 4328 |
GET_DATA_SLAB2(T); \ |
| 4329 |
break; \ |
| 4330 |
} \ |
| 4331 |
case MAT_T_UINT8: { \ |
| 4332 |
mat_uint8_t *ptr_in = (mat_uint8_t *)data_in; \ |
| 4333 |
GET_DATA_SLAB2(T); \ |
| 4334 |
break; \ |
| 4335 |
} \ |
| 4336 |
default: \ |
| 4337 |
err = MATIO_E_OPERATION_NOT_SUPPORTED; \ |
| 4338 |
GET_DATA_SLAB2_INT64(T); \ |
| 4339 |
GET_DATA_SLAB2_UINT64(T); \ |
| 4340 |
break; \ |
| 4341 |
} \ |
| 4342 |
} while ( 0 ) |
| 4343 |
|
| 4344 |
#ifdef HAVE_MAT_INT64_T |
| 4345 |
#define GET_DATA_SLABN_INT64(T) \ |
| 4346 |
do { \ |
| 4347 |
if ( MAT_T_INT64 == data_type ) { \ |
| 4348 |
mat_int64_t *ptr_in = (mat_int64_t *)data_in; \ |
| 4349 |
GET_DATA_SLABN(T); \ |
| 4350 |
err = MATIO_E_NO_ERROR; \ |
| 4351 |
} \ |
| 4352 |
} while ( 0 ) |
| 4353 |
#else |
| 4354 |
#define GET_DATA_SLABN_INT64(T) |
| 4355 |
#endif |
| 4356 |
|
| 4357 |
#ifdef HAVE_MAT_UINT64_T |
| 4358 |
#define GET_DATA_SLABN_UINT64(T) \ |
| 4359 |
do { \ |
| 4360 |
if ( MAT_T_UINT64 == data_type ) { \ |
| 4361 |
mat_uint64_t *ptr_in = (mat_uint64_t *)data_in; \ |
| 4362 |
GET_DATA_SLABN(T); \ |
| 4363 |
err = 0; \ |
| 4364 |
} \ |
| 4365 |
} while ( 0 ) |
| 4366 |
#else |
| 4367 |
#define GET_DATA_SLABN_UINT64(T) |
| 4368 |
#endif |
| 4369 |
|
| 4370 |
#define GET_DATA_SLABN_TYPE(T) \ |
| 4371 |
do { \ |
| 4372 |
switch ( data_type ) { \ |
| 4373 |
case MAT_T_DOUBLE: { \ |
| 4374 |
double *ptr_in = (double *)data_in; \ |
| 4375 |
GET_DATA_SLABN(T); \ |
| 4376 |
break; \ |
| 4377 |
} \ |
| 4378 |
case MAT_T_SINGLE: { \ |
| 4379 |
float *ptr_in = (float *)data_in; \ |
| 4380 |
GET_DATA_SLABN(T); \ |
| 4381 |
break; \ |
| 4382 |
} \ |
| 4383 |
case MAT_T_INT32: { \ |
| 4384 |
mat_int32_t *ptr_in = (mat_int32_t *)data_in; \ |
| 4385 |
GET_DATA_SLABN(T); \ |
| 4386 |
break; \ |
| 4387 |
} \ |
| 4388 |
case MAT_T_UINT32: { \ |
| 4389 |
mat_uint32_t *ptr_in = (mat_uint32_t *)data_in; \ |
| 4390 |
GET_DATA_SLABN(T); \ |
| 4391 |
break; \ |
| 4392 |
} \ |
| 4393 |
case MAT_T_INT16: { \ |
| 4394 |
mat_int16_t *ptr_in = (mat_int16_t *)data_in; \ |
| 4395 |
GET_DATA_SLABN(T); \ |
| 4396 |
break; \ |
| 4397 |
} \ |
| 4398 |
case MAT_T_UINT16: { \ |
| 4399 |
mat_uint16_t *ptr_in = (mat_uint16_t *)data_in; \ |
| 4400 |
GET_DATA_SLABN(T); \ |
| 4401 |
break; \ |
| 4402 |
} \ |
| 4403 |
case MAT_T_INT8: { \ |
| 4404 |
mat_int8_t *ptr_in = (mat_int8_t *)data_in; \ |
| 4405 |
GET_DATA_SLABN(T); \ |
| 4406 |
break; \ |
| 4407 |
} \ |
| 4408 |
case MAT_T_UINT8: { \ |
| 4409 |
mat_uint8_t *ptr_in = (mat_uint8_t *)data_in; \ |
| 4410 |
GET_DATA_SLABN(T); \ |
| 4411 |
break; \ |
| 4412 |
} \ |
| 4413 |
default: \ |
| 4414 |
err = MATIO_E_OPERATION_NOT_SUPPORTED; \ |
| 4415 |
GET_DATA_SLABN_INT64(T); \ |
| 4416 |
GET_DATA_SLABN_UINT64(T); \ |
| 4417 |
break; \ |
| 4418 |
} \ |
| 4419 |
} while ( 0 ) |
| 4420 |
|
| 4421 |
static int |
| 4422 |
GetDataSlab(void *data_in, void *data_out, enum matio_classes class_type, |
| 4423 |
enum matio_types data_type, const size_t *dims, const int *start, const int *stride, |
| 4424 |
const int *edge, int rank, size_t nbytes) |
| 4425 |
{ |
| 4426 |
int err = MATIO_E_NO_ERROR; |
| 4427 |
int same_type = 0; |
| 4428 |
if ( (class_type == MAT_C_DOUBLE && data_type == MAT_T_DOUBLE) || |
| 4429 |
(class_type == MAT_C_SINGLE && data_type == MAT_T_SINGLE) || |
| 4430 |
(class_type == MAT_C_INT16 && data_type == MAT_T_INT16) || |
| 4431 |
(class_type == MAT_C_INT32 && data_type == MAT_T_INT32) || |
| 4432 |
(class_type == MAT_C_INT64 && data_type == MAT_T_INT64) || |
| 4433 |
(class_type == MAT_C_INT8 && data_type == MAT_T_INT8) || |
| 4434 |
(class_type == MAT_C_UINT16 && data_type == MAT_T_UINT16) || |
| 4435 |
(class_type == MAT_C_UINT32 && data_type == MAT_T_UINT32) || |
| 4436 |
(class_type == MAT_C_UINT64 && data_type == MAT_T_UINT64) || |
| 4437 |
(class_type == MAT_C_UINT8 && data_type == MAT_T_UINT8) ) |
| 4438 |
same_type = 1; |
| 4439 |
|
| 4440 |
if ( rank == 2 ) { |
| 4441 |
if ( (size_t)stride[0] * (edge[0] - 1) + start[0] + 1 > dims[0] ) |
| 4442 |
err = MATIO_E_BAD_ARGUMENT; |
| 4443 |
else if ( (size_t)stride[1] * (edge[1] - 1) + start[1] + 1 > dims[1] ) |
| 4444 |
err = MATIO_E_BAD_ARGUMENT; |
| 4445 |
else if ( (stride[0] == 1 && (size_t)edge[0] == dims[0]) && (stride[1] == 1) && |
| 4446 |
(same_type == 1) ) |
| 4447 |
memcpy(data_out, data_in, nbytes); |
| 4448 |
else { |
| 4449 |
int i, j; |
| 4450 |
|
| 4451 |
switch ( class_type ) { |
| 4452 |
case MAT_C_DOUBLE: { |
| 4453 |
double *ptr = (double *)data_out; |
| 4454 |
GET_DATA_SLAB2_TYPE(double); |
| 4455 |
break; |
| 4456 |
} |
| 4457 |
case MAT_C_SINGLE: { |
| 4458 |
float *ptr = (float *)data_out; |
| 4459 |
GET_DATA_SLAB2_TYPE(float); |
| 4460 |
break; |
| 4461 |
} |
| 4462 |
#ifdef HAVE_MAT_INT64_T |
| 4463 |
case MAT_C_INT64: { |
| 4464 |
mat_int64_t *ptr = (mat_int64_t *)data_out; |
| 4465 |
GET_DATA_SLAB2_TYPE(mat_int64_t); |
| 4466 |
break; |
| 4467 |
} |
| 4468 |
#endif |
| 4469 |
#ifdef HAVE_MAT_UINT64_T |
| 4470 |
case MAT_C_UINT64: { |
| 4471 |
mat_uint64_t *ptr = (mat_uint64_t *)data_out; |
| 4472 |
GET_DATA_SLAB2_TYPE(mat_uint64_t); |
| 4473 |
break; |
| 4474 |
} |
| 4475 |
#endif |
| 4476 |
case MAT_C_INT32: { |
| 4477 |
mat_int32_t *ptr = (mat_int32_t *)data_out; |
| 4478 |
GET_DATA_SLAB2_TYPE(mat_int32_t); |
| 4479 |
break; |
| 4480 |
} |
| 4481 |
case MAT_C_UINT32: { |
| 4482 |
mat_uint32_t *ptr = (mat_uint32_t *)data_out; |
| 4483 |
GET_DATA_SLAB2_TYPE(mat_uint32_t); |
| 4484 |
break; |
| 4485 |
} |
| 4486 |
case MAT_C_INT16: { |
| 4487 |
mat_int16_t *ptr = (mat_int16_t *)data_out; |
| 4488 |
GET_DATA_SLAB2_TYPE(mat_int16_t); |
| 4489 |
break; |
| 4490 |
} |
| 4491 |
case MAT_C_UINT16: { |
| 4492 |
mat_uint16_t *ptr = (mat_uint16_t *)data_out; |
| 4493 |
GET_DATA_SLAB2_TYPE(mat_uint16_t); |
| 4494 |
break; |
| 4495 |
} |
| 4496 |
case MAT_C_INT8: { |
| 4497 |
mat_int8_t *ptr = (mat_int8_t *)data_out; |
| 4498 |
GET_DATA_SLAB2_TYPE(mat_int8_t); |
| 4499 |
break; |
| 4500 |
} |
| 4501 |
case MAT_C_UINT8: { |
| 4502 |
mat_uint8_t *ptr = (mat_uint8_t *)data_out; |
| 4503 |
GET_DATA_SLAB2_TYPE(mat_uint8_t); |
| 4504 |
break; |
| 4505 |
} |
| 4506 |
default: |
| 4507 |
err = MATIO_E_OPERATION_NOT_SUPPORTED; |
| 4508 |
break; |
| 4509 |
} |
| 4510 |
} |
| 4511 |
} else { |
| 4512 |
int i, j, N, I = 0; |
| 4513 |
int inc[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4514 |
int cnt[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4515 |
int dimp[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 4516 |
|
| 4517 |
switch ( class_type ) { |
| 4518 |
case MAT_C_DOUBLE: { |
| 4519 |
double *ptr = (double *)data_out; |
| 4520 |
GET_DATA_SLABN_TYPE(double); |
| 4521 |
break; |
| 4522 |
} |
| 4523 |
case MAT_C_SINGLE: { |
| 4524 |
float *ptr = (float *)data_out; |
| 4525 |
GET_DATA_SLABN_TYPE(float); |
| 4526 |
break; |
| 4527 |
} |
| 4528 |
#ifdef HAVE_MAT_INT64_T |
| 4529 |
case MAT_C_INT64: { |
| 4530 |
mat_int64_t *ptr = (mat_int64_t *)data_out; |
| 4531 |
GET_DATA_SLABN_TYPE(mat_int64_t); |
| 4532 |
break; |
| 4533 |
} |
| 4534 |
#endif |
| 4535 |
#ifdef HAVE_MAT_UINT64_T |
| 4536 |
case MAT_C_UINT64: { |
| 4537 |
mat_uint64_t *ptr = (mat_uint64_t *)data_out; |
| 4538 |
GET_DATA_SLABN_TYPE(mat_uint64_t); |
| 4539 |
break; |
| 4540 |
} |
| 4541 |
#endif |
| 4542 |
case MAT_C_INT32: { |
| 4543 |
mat_int32_t *ptr = (mat_int32_t *)data_out; |
| 4544 |
GET_DATA_SLABN_TYPE(mat_int32_t); |
| 4545 |
break; |
| 4546 |
} |
| 4547 |
case MAT_C_UINT32: { |
| 4548 |
mat_uint32_t *ptr = (mat_uint32_t *)data_out; |
| 4549 |
GET_DATA_SLABN_TYPE(mat_uint32_t); |
| 4550 |
break; |
| 4551 |
} |
| 4552 |
case MAT_C_INT16: { |
| 4553 |
mat_int16_t *ptr = (mat_int16_t *)data_out; |
| 4554 |
GET_DATA_SLABN_TYPE(mat_int16_t); |
| 4555 |
break; |
| 4556 |
} |
| 4557 |
case MAT_C_UINT16: { |
| 4558 |
mat_uint16_t *ptr = (mat_uint16_t *)data_out; |
| 4559 |
GET_DATA_SLABN_TYPE(mat_uint16_t); |
| 4560 |
break; |
| 4561 |
} |
| 4562 |
case MAT_C_INT8: { |
| 4563 |
mat_int8_t *ptr = (mat_int8_t *)data_out; |
| 4564 |
GET_DATA_SLABN_TYPE(mat_int8_t); |
| 4565 |
break; |
| 4566 |
} |
| 4567 |
case MAT_C_UINT8: { |
| 4568 |
mat_uint8_t *ptr = (mat_uint8_t *)data_out; |
| 4569 |
GET_DATA_SLABN_TYPE(mat_uint8_t); |
| 4570 |
break; |
| 4571 |
} |
| 4572 |
default: |
| 4573 |
err = MATIO_E_OPERATION_NOT_SUPPORTED; |
| 4574 |
break; |
| 4575 |
} |
| 4576 |
} |
| 4577 |
|
| 4578 |
return err; |
| 4579 |
} |
| 4580 |
|
| 4581 |
#undef GET_DATA_SLAB2 |
| 4582 |
#undef GET_DATA_SLAB2_TYPE |
| 4583 |
#undef GET_DATA_SLAB2_INT64 |
| 4584 |
#undef GET_DATA_SLAB2_UINT64 |
| 4585 |
#undef GET_DATA_SLABN |
| 4586 |
#undef GET_DATA_SLABN_TYPE |
| 4587 |
#undef GET_DATA_SLABN_INT64 |
| 4588 |
#undef GET_DATA_SLABN_UINT64 |
| 4589 |
#undef GET_DATA_SLABN_RANK_LOOP |
| 4590 |
|
| 4591 |
#define GET_DATA_LINEAR \ |
| 4592 |
do { \ |
| 4593 |
ptr_in += start; \ |
| 4594 |
if ( !stride ) { \ |
| 4595 |
memcpy(ptr, ptr_in, (size_t)edge * data_size); \ |
| 4596 |
} else { \ |
| 4597 |
int i; \ |
| 4598 |
for ( i = 0; i < edge; i++ ) \ |
| 4599 |
memcpy(ptr++, ptr_in + i * stride, data_size); \ |
| 4600 |
} \ |
| 4601 |
} while ( 0 ) |
| 4602 |
|
| 4603 |
static int |
| 4604 |
GetDataLinear(void *data_in, void *data_out, enum matio_classes class_type, |
| 4605 |
enum matio_types data_type, int start, int stride, int edge) |
| 4606 |
{ |
| 4607 |
int err = MATIO_E_NO_ERROR; |
| 4608 |
size_t data_size = Mat_SizeOf(data_type); |
| 4609 |
|
| 4610 |
switch ( class_type ) { |
| 4611 |
case MAT_C_DOUBLE: { |
| 4612 |
double *ptr = (double *)data_out; |
| 4613 |
double *ptr_in = (double *)data_in; |
| 4614 |
GET_DATA_LINEAR; |
| 4615 |
break; |
| 4616 |
} |
| 4617 |
case MAT_C_SINGLE: { |
| 4618 |
float *ptr = (float *)data_out; |
| 4619 |
float *ptr_in = (float *)data_in; |
| 4620 |
GET_DATA_LINEAR; |
| 4621 |
break; |
| 4622 |
} |
| 4623 |
#ifdef HAVE_MAT_INT64_T |
| 4624 |
case MAT_C_INT64: { |
| 4625 |
mat_int64_t *ptr = (mat_int64_t *)data_out; |
| 4626 |
mat_int64_t *ptr_in = (mat_int64_t *)data_in; |
| 4627 |
GET_DATA_LINEAR; |
| 4628 |
break; |
| 4629 |
} |
| 4630 |
#endif |
| 4631 |
#ifdef HAVE_MAT_UINT64_T |
| 4632 |
case MAT_C_UINT64: { |
| 4633 |
mat_uint64_t *ptr = (mat_uint64_t *)data_out; |
| 4634 |
mat_uint64_t *ptr_in = (mat_uint64_t *)data_in; |
| 4635 |
GET_DATA_LINEAR; |
| 4636 |
break; |
| 4637 |
} |
| 4638 |
#endif |
| 4639 |
case MAT_C_INT32: { |
| 4640 |
mat_int32_t *ptr = (mat_int32_t *)data_out; |
| 4641 |
mat_int32_t *ptr_in = (mat_int32_t *)data_in; |
| 4642 |
GET_DATA_LINEAR; |
| 4643 |
break; |
| 4644 |
} |
| 4645 |
case MAT_C_UINT32: { |
| 4646 |
mat_uint32_t *ptr = (mat_uint32_t *)data_out; |
| 4647 |
mat_uint32_t *ptr_in = (mat_uint32_t *)data_in; |
| 4648 |
GET_DATA_LINEAR; |
| 4649 |
break; |
| 4650 |
} |
| 4651 |
case MAT_C_INT16: { |
| 4652 |
mat_int16_t *ptr = (mat_int16_t *)data_out; |
| 4653 |
mat_int16_t *ptr_in = (mat_int16_t *)data_in; |
| 4654 |
GET_DATA_LINEAR; |
| 4655 |
break; |
| 4656 |
} |
| 4657 |
case MAT_C_UINT16: { |
| 4658 |
mat_uint16_t *ptr = (mat_uint16_t *)data_out; |
| 4659 |
mat_uint16_t *ptr_in = (mat_uint16_t *)data_in; |
| 4660 |
GET_DATA_LINEAR; |
| 4661 |
break; |
| 4662 |
} |
| 4663 |
case MAT_C_INT8: { |
| 4664 |
mat_int8_t *ptr = (mat_int8_t *)data_out; |
| 4665 |
mat_int8_t *ptr_in = (mat_int8_t *)data_in; |
| 4666 |
GET_DATA_LINEAR; |
| 4667 |
break; |
| 4668 |
} |
| 4669 |
case MAT_C_UINT8: { |
| 4670 |
mat_uint8_t *ptr = (mat_uint8_t *)data_out; |
| 4671 |
mat_uint8_t *ptr_in = (mat_uint8_t *)data_in; |
| 4672 |
GET_DATA_LINEAR; |
| 4673 |
break; |
| 4674 |
} |
| 4675 |
default: |
| 4676 |
err = MATIO_E_OPERATION_NOT_SUPPORTED; |
| 4677 |
break; |
| 4678 |
} |
| 4679 |
|
| 4680 |
return err; |
| 4681 |
} |
| 4682 |
|
| 4683 |
#undef GET_DATA_LINEAR |
| 4684 |
#endif |
| 4685 |
|
| 4686 |
|
| 4687 |
|
| 4688 |
|
| 4689 |
|
| 4690 |
|
| 4691 |
|
| 4692 |
|
| 4693 |
|
| 4694 |
|
| 4695 |
|
| 4696 |
|
| 4697 |
|
| 4698 |
|
| 4699 |
|
| 4700 |
int |
| 4701 |
Mat_VarReadData5(mat_t *mat, matvar_t *matvar, void *data, const int *start, const int *stride, |
| 4702 |
const int *edge) |
| 4703 |
{ |
| 4704 |
int err = MATIO_E_NO_ERROR, real_bytes = 0; |
| 4705 |
mat_uint32_t tag[2] = {0, 0}; |
| 4706 |
#if HAVE_ZLIB |
| 4707 |
z_stream z; |
| 4708 |
#endif |
| 4709 |
|
| 4710 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 4711 |
if ( matvar->compression == MAT_COMPRESSION_NONE ) { |
| 4712 |
err = Read(tag, 4, 2, (FILE *)mat->fp, NULL); |
| 4713 |
if ( err ) { |
| 4714 |
return err; |
| 4715 |
} |
| 4716 |
if ( mat->byteswap ) { |
| 4717 |
(void)Mat_uint32Swap(tag); |
| 4718 |
(void)Mat_uint32Swap(tag + 1); |
| 4719 |
} |
| 4720 |
matvar->data_type = TYPE_FROM_TAG(tag[0]); |
| 4721 |
if ( tag[0] & 0xffff0000 ) { |
| 4722 |
(void)fseeko((FILE *)mat->fp, -4, SEEK_CUR); |
| 4723 |
real_bytes = 4 + (tag[0] >> 16); |
| 4724 |
} else { |
| 4725 |
real_bytes = 8 + tag[1]; |
| 4726 |
} |
| 4727 |
#if HAVE_ZLIB |
| 4728 |
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 4729 |
if ( NULL != matvar->internal->data ) { |
| 4730 |
|
| 4731 |
if ( matvar->isComplex ) { |
| 4732 |
mat_complex_split_t *ci, *co; |
| 4733 |
|
| 4734 |
co = (mat_complex_split_t *)data; |
| 4735 |
ci = (mat_complex_split_t *)matvar->internal->data; |
| 4736 |
err = GetDataSlab(ci->Re, co->Re, matvar->class_type, matvar->data_type, |
| 4737 |
matvar->dims, start, stride, edge, matvar->rank, matvar->nbytes); |
| 4738 |
if ( MATIO_E_NO_ERROR == err ) |
| 4739 |
err = GetDataSlab(ci->Im, co->Im, matvar->class_type, matvar->data_type, |
| 4740 |
matvar->dims, start, stride, edge, matvar->rank, |
| 4741 |
matvar->nbytes); |
| 4742 |
return err; |
| 4743 |
} else { |
| 4744 |
return GetDataSlab(matvar->internal->data, data, matvar->class_type, |
| 4745 |
matvar->data_type, matvar->dims, start, stride, edge, |
| 4746 |
matvar->rank, matvar->nbytes); |
| 4747 |
} |
| 4748 |
} |
| 4749 |
|
| 4750 |
err = inflateCopy(&z, matvar->internal->z); |
| 4751 |
if ( err != Z_OK ) { |
| 4752 |
Mat_Critical("inflateCopy returned error %s", zError(err)); |
| 4753 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 4754 |
} |
| 4755 |
z.avail_in = 0; |
| 4756 |
err = Inflate(mat, &z, tag, 4, NULL); |
| 4757 |
if ( err ) { |
| 4758 |
return err; |
| 4759 |
} |
| 4760 |
if ( mat->byteswap ) { |
| 4761 |
(void)Mat_uint32Swap(tag); |
| 4762 |
} |
| 4763 |
matvar->data_type = TYPE_FROM_TAG(tag[0]); |
| 4764 |
if ( !(tag[0] & 0xffff0000) ) { |
| 4765 |
err = Inflate(mat, &z, tag + 1, 4, NULL); |
| 4766 |
if ( err ) { |
| 4767 |
return err; |
| 4768 |
} |
| 4769 |
if ( mat->byteswap ) { |
| 4770 |
(void)Mat_uint32Swap(tag + 1); |
| 4771 |
} |
| 4772 |
real_bytes = 8 + tag[1]; |
| 4773 |
} else { |
| 4774 |
real_bytes = 4 + (tag[0] >> 16); |
| 4775 |
} |
| 4776 |
#endif |
| 4777 |
} |
| 4778 |
if ( real_bytes % 8 ) |
| 4779 |
real_bytes += (8 - (real_bytes % 8)); |
| 4780 |
|
| 4781 |
if ( matvar->rank == 2 ) { |
| 4782 |
if ( (size_t)stride[0] * (edge[0] - 1) + start[0] + 1 > matvar->dims[0] ) |
| 4783 |
err = MATIO_E_BAD_ARGUMENT; |
| 4784 |
else if ( (size_t)stride[1] * (edge[1] - 1) + start[1] + 1 > matvar->dims[1] ) |
| 4785 |
err = MATIO_E_BAD_ARGUMENT; |
| 4786 |
else if ( matvar->compression == MAT_COMPRESSION_NONE ) { |
| 4787 |
if ( matvar->isComplex ) { |
| 4788 |
mat_complex_split_t *complex_data = (mat_complex_split_t *)data; |
| 4789 |
|
| 4790 |
ReadDataSlab2(mat, complex_data->Re, matvar->class_type, matvar->data_type, |
| 4791 |
matvar->dims, start, stride, edge); |
| 4792 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos + real_bytes, SEEK_SET); |
| 4793 |
err = Read(tag, 4, 2, (FILE *)mat->fp, NULL); |
| 4794 |
if ( err ) { |
| 4795 |
return err; |
| 4796 |
} |
| 4797 |
if ( mat->byteswap ) { |
| 4798 |
(void)Mat_uint32Swap(tag); |
| 4799 |
(void)Mat_uint32Swap(tag + 1); |
| 4800 |
} |
| 4801 |
matvar->data_type = TYPE_FROM_TAG(tag[0]); |
| 4802 |
if ( tag[0] & 0xffff0000 ) { |
| 4803 |
(void)fseeko((FILE *)mat->fp, -4, SEEK_CUR); |
| 4804 |
} |
| 4805 |
ReadDataSlab2(mat, complex_data->Im, matvar->class_type, matvar->data_type, |
| 4806 |
matvar->dims, start, stride, edge); |
| 4807 |
} else { |
| 4808 |
ReadDataSlab2(mat, data, matvar->class_type, matvar->data_type, matvar->dims, start, |
| 4809 |
stride, edge); |
| 4810 |
} |
| 4811 |
} |
| 4812 |
#if HAVE_ZLIB |
| 4813 |
else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 4814 |
if ( matvar->isComplex ) { |
| 4815 |
mat_complex_split_t *complex_data = (mat_complex_split_t *)data; |
| 4816 |
|
| 4817 |
ReadCompressedDataSlab2(mat, &z, complex_data->Re, matvar->class_type, |
| 4818 |
matvar->data_type, matvar->dims, start, stride, edge); |
| 4819 |
|
| 4820 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 4821 |
|
| 4822 |
|
| 4823 |
inflateEnd(&z); |
| 4824 |
err = inflateCopy(&z, matvar->internal->z); |
| 4825 |
if ( err != Z_OK ) { |
| 4826 |
Mat_Critical("inflateCopy returned error %s", zError(err)); |
| 4827 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 4828 |
} |
| 4829 |
InflateSkip(mat, &z, real_bytes, NULL); |
| 4830 |
z.avail_in = 0; |
| 4831 |
err = Inflate(mat, &z, tag, 4, NULL); |
| 4832 |
if ( err ) { |
| 4833 |
return err; |
| 4834 |
} |
| 4835 |
if ( mat->byteswap ) { |
| 4836 |
(void)Mat_uint32Swap(tag); |
| 4837 |
} |
| 4838 |
matvar->data_type = TYPE_FROM_TAG(tag[0]); |
| 4839 |
if ( !(tag[0] & 0xffff0000) ) { |
| 4840 |
InflateSkip(mat, &z, 4, NULL); |
| 4841 |
} |
| 4842 |
ReadCompressedDataSlab2(mat, &z, complex_data->Im, matvar->class_type, |
| 4843 |
matvar->data_type, matvar->dims, start, stride, edge); |
| 4844 |
} else { |
| 4845 |
ReadCompressedDataSlab2(mat, &z, data, matvar->class_type, matvar->data_type, |
| 4846 |
matvar->dims, start, stride, edge); |
| 4847 |
} |
| 4848 |
inflateEnd(&z); |
| 4849 |
} |
| 4850 |
#endif |
| 4851 |
} else { |
| 4852 |
if ( matvar->compression == MAT_COMPRESSION_NONE ) { |
| 4853 |
if ( matvar->isComplex ) { |
| 4854 |
mat_complex_split_t *complex_data = (mat_complex_split_t *)data; |
| 4855 |
|
| 4856 |
ReadDataSlabN(mat, complex_data->Re, matvar->class_type, matvar->data_type, |
| 4857 |
matvar->rank, matvar->dims, start, stride, edge); |
| 4858 |
|
| 4859 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos + real_bytes, SEEK_SET); |
| 4860 |
err = Read(tag, 4, 2, (FILE *)mat->fp, NULL); |
| 4861 |
if ( err ) { |
| 4862 |
return err; |
| 4863 |
} |
| 4864 |
if ( mat->byteswap ) { |
| 4865 |
(void)Mat_uint32Swap(tag); |
| 4866 |
(void)Mat_uint32Swap(tag + 1); |
| 4867 |
} |
| 4868 |
matvar->data_type = TYPE_FROM_TAG(tag[0]); |
| 4869 |
if ( tag[0] & 0xffff0000 ) { |
| 4870 |
(void)fseeko((FILE *)mat->fp, -4, SEEK_CUR); |
| 4871 |
} |
| 4872 |
ReadDataSlabN(mat, complex_data->Im, matvar->class_type, matvar->data_type, |
| 4873 |
matvar->rank, matvar->dims, start, stride, edge); |
| 4874 |
} else { |
| 4875 |
ReadDataSlabN(mat, data, matvar->class_type, matvar->data_type, matvar->rank, |
| 4876 |
matvar->dims, start, stride, edge); |
| 4877 |
} |
| 4878 |
} |
| 4879 |
#if HAVE_ZLIB |
| 4880 |
else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 4881 |
if ( matvar->isComplex ) { |
| 4882 |
mat_complex_split_t *complex_data = (mat_complex_split_t *)data; |
| 4883 |
|
| 4884 |
ReadCompressedDataSlabN(mat, &z, complex_data->Re, matvar->class_type, |
| 4885 |
matvar->data_type, matvar->rank, matvar->dims, start, |
| 4886 |
stride, edge); |
| 4887 |
|
| 4888 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 4889 |
|
| 4890 |
inflateEnd(&z); |
| 4891 |
err = inflateCopy(&z, matvar->internal->z); |
| 4892 |
if ( err != Z_OK ) { |
| 4893 |
Mat_Critical("inflateCopy returned error %s", zError(err)); |
| 4894 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 4895 |
} |
| 4896 |
InflateSkip(mat, &z, real_bytes, NULL); |
| 4897 |
z.avail_in = 0; |
| 4898 |
err = Inflate(mat, &z, tag, 4, NULL); |
| 4899 |
if ( err ) { |
| 4900 |
return err; |
| 4901 |
} |
| 4902 |
if ( mat->byteswap ) { |
| 4903 |
(void)Mat_uint32Swap(tag); |
| 4904 |
} |
| 4905 |
matvar->data_type = TYPE_FROM_TAG(tag[0]); |
| 4906 |
if ( !(tag[0] & 0xffff0000) ) { |
| 4907 |
InflateSkip(mat, &z, 4, NULL); |
| 4908 |
} |
| 4909 |
ReadCompressedDataSlabN(mat, &z, complex_data->Im, matvar->class_type, |
| 4910 |
matvar->data_type, matvar->rank, matvar->dims, start, |
| 4911 |
stride, edge); |
| 4912 |
} else { |
| 4913 |
ReadCompressedDataSlabN(mat, &z, data, matvar->class_type, matvar->data_type, |
| 4914 |
matvar->rank, matvar->dims, start, stride, edge); |
| 4915 |
} |
| 4916 |
inflateEnd(&z); |
| 4917 |
} |
| 4918 |
#endif |
| 4919 |
} |
| 4920 |
if ( err == MATIO_E_NO_ERROR ) { |
| 4921 |
matvar->data_type = ClassType2DataType(matvar->class_type); |
| 4922 |
matvar->data_size = Mat_SizeOfClass(matvar->class_type); |
| 4923 |
} |
| 4924 |
return err; |
| 4925 |
} |
| 4926 |
|
| 4927 |
|
| 4928 |
|
| 4929 |
|
| 4930 |
|
| 4931 |
|
| 4932 |
|
| 4933 |
|
| 4934 |
|
| 4935 |
|
| 4936 |
|
| 4937 |
|
| 4938 |
|
| 4939 |
|
| 4940 |
int |
| 4941 |
Mat_VarReadDataLinear5(mat_t *mat, matvar_t *matvar, void *data, int start, int stride, int edge) |
| 4942 |
{ |
| 4943 |
int err = MATIO_E_NO_ERROR, real_bytes = 0; |
| 4944 |
mat_uint32_t tag[2] = {0, 0}; |
| 4945 |
#if HAVE_ZLIB |
| 4946 |
z_stream z; |
| 4947 |
#endif |
| 4948 |
size_t nelems = 1; |
| 4949 |
|
| 4950 |
if ( mat->version == MAT_FT_MAT4 ) |
| 4951 |
return -1; |
| 4952 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 4953 |
if ( matvar->compression == MAT_COMPRESSION_NONE ) { |
| 4954 |
err = Read(tag, 4, 2, (FILE *)mat->fp, NULL); |
| 4955 |
if ( err ) { |
| 4956 |
return err; |
| 4957 |
} |
| 4958 |
if ( mat->byteswap ) { |
| 4959 |
(void)Mat_uint32Swap(tag); |
| 4960 |
(void)Mat_uint32Swap(tag + 1); |
| 4961 |
} |
| 4962 |
matvar->data_type = (enum matio_types)(tag[0] & 0x000000ff); |
| 4963 |
if ( tag[0] & 0xffff0000 ) { |
| 4964 |
(void)fseeko((FILE *)mat->fp, -4, SEEK_CUR); |
| 4965 |
real_bytes = 4 + (tag[0] >> 16); |
| 4966 |
} else { |
| 4967 |
real_bytes = 8 + tag[1]; |
| 4968 |
} |
| 4969 |
#if HAVE_ZLIB |
| 4970 |
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 4971 |
if ( NULL != matvar->internal->data ) { |
| 4972 |
|
| 4973 |
if ( matvar->isComplex ) { |
| 4974 |
mat_complex_split_t *ci, *co; |
| 4975 |
|
| 4976 |
co = (mat_complex_split_t *)data; |
| 4977 |
ci = (mat_complex_split_t *)matvar->internal->data; |
| 4978 |
err = GetDataLinear(ci->Re, co->Re, matvar->class_type, matvar->data_type, start, |
| 4979 |
stride, edge); |
| 4980 |
if ( err == MATIO_E_NO_ERROR ) |
| 4981 |
err = GetDataLinear(ci->Im, co->Im, matvar->class_type, matvar->data_type, |
| 4982 |
start, stride, edge); |
| 4983 |
return err; |
| 4984 |
} else { |
| 4985 |
return GetDataLinear(matvar->internal->data, data, matvar->class_type, |
| 4986 |
matvar->data_type, start, stride, edge); |
| 4987 |
} |
| 4988 |
} |
| 4989 |
|
| 4990 |
matvar->internal->z->avail_in = 0; |
| 4991 |
err = inflateCopy(&z, matvar->internal->z); |
| 4992 |
if ( err != Z_OK ) { |
| 4993 |
Mat_Critical("inflateCopy returned error %s", zError(err)); |
| 4994 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 4995 |
} |
| 4996 |
err = Inflate(mat, &z, tag, 4, NULL); |
| 4997 |
if ( err ) { |
| 4998 |
return err; |
| 4999 |
} |
| 5000 |
if ( mat->byteswap ) { |
| 5001 |
(void)Mat_uint32Swap(tag); |
| 5002 |
(void)Mat_uint32Swap(tag + 1); |
| 5003 |
} |
| 5004 |
matvar->data_type = (enum matio_types)(tag[0] & 0x000000ff); |
| 5005 |
if ( !(tag[0] & 0xffff0000) ) { |
| 5006 |
err = Inflate(mat, &z, tag + 1, 4, NULL); |
| 5007 |
if ( err ) { |
| 5008 |
return err; |
| 5009 |
} |
| 5010 |
if ( mat->byteswap ) { |
| 5011 |
(void)Mat_uint32Swap(tag + 1); |
| 5012 |
} |
| 5013 |
real_bytes = 8 + tag[1]; |
| 5014 |
} else { |
| 5015 |
real_bytes = 4 + (tag[0] >> 16); |
| 5016 |
} |
| 5017 |
#endif |
| 5018 |
} |
| 5019 |
if ( real_bytes % 8 ) |
| 5020 |
real_bytes += (8 - (real_bytes % 8)); |
| 5021 |
|
| 5022 |
err = Mat_MulDims(matvar, &nelems); |
| 5023 |
if ( err ) { |
| 5024 |
Mat_Critical("Integer multiplication overflow"); |
| 5025 |
return err; |
| 5026 |
} |
| 5027 |
|
| 5028 |
if ( (size_t)stride * (edge - 1) + start + 1 > nelems ) { |
| 5029 |
err = MATIO_E_BAD_ARGUMENT; |
| 5030 |
} else if ( matvar->compression == MAT_COMPRESSION_NONE ) { |
| 5031 |
if ( matvar->isComplex ) { |
| 5032 |
mat_complex_split_t *complex_data = (mat_complex_split_t *)data; |
| 5033 |
|
| 5034 |
ReadDataSlab1(mat, complex_data->Re, matvar->class_type, matvar->data_type, start, |
| 5035 |
stride, edge); |
| 5036 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos + real_bytes, SEEK_SET); |
| 5037 |
err = Read(tag, 4, 2, (FILE *)mat->fp, NULL); |
| 5038 |
if ( err ) { |
| 5039 |
return err; |
| 5040 |
} |
| 5041 |
if ( mat->byteswap ) { |
| 5042 |
(void)Mat_uint32Swap(tag); |
| 5043 |
(void)Mat_uint32Swap(tag + 1); |
| 5044 |
} |
| 5045 |
matvar->data_type = (enum matio_types)(tag[0] & 0x000000ff); |
| 5046 |
if ( tag[0] & 0xffff0000 ) { |
| 5047 |
(void)fseeko((FILE *)mat->fp, -4, SEEK_CUR); |
| 5048 |
} |
| 5049 |
ReadDataSlab1(mat, complex_data->Im, matvar->class_type, matvar->data_type, start, |
| 5050 |
stride, edge); |
| 5051 |
} else { |
| 5052 |
ReadDataSlab1(mat, data, matvar->class_type, matvar->data_type, start, stride, edge); |
| 5053 |
} |
| 5054 |
#if HAVE_ZLIB |
| 5055 |
} else if ( matvar->compression == MAT_COMPRESSION_ZLIB ) { |
| 5056 |
if ( matvar->isComplex ) { |
| 5057 |
mat_complex_split_t *complex_data = (mat_complex_split_t *)data; |
| 5058 |
|
| 5059 |
ReadCompressedDataSlab1(mat, &z, complex_data->Re, matvar->class_type, |
| 5060 |
matvar->data_type, start, stride, edge); |
| 5061 |
|
| 5062 |
(void)fseeko((FILE *)mat->fp, matvar->internal->datapos, SEEK_SET); |
| 5063 |
|
| 5064 |
|
| 5065 |
inflateEnd(&z); |
| 5066 |
err = inflateCopy(&z, matvar->internal->z); |
| 5067 |
if ( err != Z_OK ) { |
| 5068 |
Mat_Critical("inflateCopy returned error %s", zError(err)); |
| 5069 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 5070 |
} |
| 5071 |
InflateSkip(mat, &z, real_bytes, NULL); |
| 5072 |
z.avail_in = 0; |
| 5073 |
err = Inflate(mat, &z, tag, 4, NULL); |
| 5074 |
if ( err ) { |
| 5075 |
return err; |
| 5076 |
} |
| 5077 |
if ( mat->byteswap ) { |
| 5078 |
(void)Mat_uint32Swap(tag); |
| 5079 |
} |
| 5080 |
matvar->data_type = (enum matio_types)(tag[0] & 0x000000ff); |
| 5081 |
if ( !(tag[0] & 0xffff0000) ) { |
| 5082 |
InflateSkip(mat, &z, 4, NULL); |
| 5083 |
} |
| 5084 |
ReadCompressedDataSlab1(mat, &z, complex_data->Im, matvar->class_type, |
| 5085 |
matvar->data_type, start, stride, edge); |
| 5086 |
} else { |
| 5087 |
ReadCompressedDataSlab1(mat, &z, data, matvar->class_type, matvar->data_type, start, |
| 5088 |
stride, edge); |
| 5089 |
} |
| 5090 |
inflateEnd(&z); |
| 5091 |
#endif |
| 5092 |
} |
| 5093 |
|
| 5094 |
matvar->data_type = ClassType2DataType(matvar->class_type); |
| 5095 |
matvar->data_size = Mat_SizeOfClass(matvar->class_type); |
| 5096 |
|
| 5097 |
return err; |
| 5098 |
} |
| 5099 |
|
| 5100 |
|
| 5101 |
|
| 5102 |
|
| 5103 |
|
| 5104 |
|
| 5105 |
|
| 5106 |
|
| 5107 |
|
| 5108 |
|
| 5109 |
|
| 5110 |
|
| 5111 |
int |
| 5112 |
Mat_VarWrite5(mat_t *mat, matvar_t *matvar, int compress) |
| 5113 |
{ |
| 5114 |
mat_uint32_t array_flags; |
| 5115 |
int array_flags_type = MAT_T_UINT32, dims_array_type = MAT_T_INT32; |
| 5116 |
int array_flags_size = 8, matrix_type = MAT_T_MATRIX; |
| 5117 |
const mat_uint32_t pad4 = 0; |
| 5118 |
int nBytes, nzmax = 0; |
| 5119 |
mat_off_t start = 0, end = 0; |
| 5120 |
|
| 5121 |
if ( NULL == mat || NULL == matvar ) |
| 5122 |
return MATIO_E_BAD_ARGUMENT; |
| 5123 |
if ( NULL == matvar->name ) |
| 5124 |
return MATIO_E_OUTPUT_BAD_DATA; |
| 5125 |
|
| 5126 |
|
| 5127 |
(void)fseeko((FILE *)mat->fp, 0, SEEK_END); |
| 5128 |
|
| 5129 |
#if HAVE_ZLIB |
| 5130 |
if ( compress == MAT_COMPRESSION_NONE ) { |
| 5131 |
#else |
| 5132 |
{ |
| 5133 |
#endif |
| 5134 |
int i; |
| 5135 |
fwrite(&matrix_type, 4, 1, (FILE *)mat->fp); |
| 5136 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 5137 |
start = ftello((FILE *)mat->fp); |
| 5138 |
|
| 5139 |
|
| 5140 |
array_flags = matvar->class_type & CLASS_TYPE_MASK; |
| 5141 |
if ( matvar->isComplex ) |
| 5142 |
array_flags |= MAT_F_COMPLEX; |
| 5143 |
if ( matvar->isGlobal ) |
| 5144 |
array_flags |= MAT_F_GLOBAL; |
| 5145 |
if ( matvar->isLogical ) |
| 5146 |
array_flags |= MAT_F_LOGICAL; |
| 5147 |
if ( matvar->class_type == MAT_C_SPARSE ) |
| 5148 |
nzmax = ((mat_sparse_t *)matvar->data)->nzmax; |
| 5149 |
|
| 5150 |
fwrite(&array_flags_type, 4, 1, (FILE *)mat->fp); |
| 5151 |
fwrite(&array_flags_size, 4, 1, (FILE *)mat->fp); |
| 5152 |
fwrite(&array_flags, 4, 1, (FILE *)mat->fp); |
| 5153 |
fwrite(&nzmax, 4, 1, (FILE *)mat->fp); |
| 5154 |
|
| 5155 |
nBytes = matvar->rank * 4; |
| 5156 |
fwrite(&dims_array_type, 4, 1, (FILE *)mat->fp); |
| 5157 |
fwrite(&nBytes, 4, 1, (FILE *)mat->fp); |
| 5158 |
for ( i = 0; i < matvar->rank; i++ ) { |
| 5159 |
mat_int32_t dim; |
| 5160 |
dim = matvar->dims[i]; |
| 5161 |
fwrite(&dim, 4, 1, (FILE *)mat->fp); |
| 5162 |
} |
| 5163 |
if ( matvar->rank % 2 != 0 ) |
| 5164 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 5165 |
|
| 5166 |
if ( strlen(matvar->name) <= 4 ) { |
| 5167 |
mat_uint32_t array_name_type = MAT_T_INT8; |
| 5168 |
const mat_uint32_t array_name_len = (mat_uint32_t)strlen(matvar->name); |
| 5169 |
const mat_uint8_t pad1 = 0; |
| 5170 |
array_name_type |= array_name_len << 16; |
| 5171 |
fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 5172 |
fwrite(matvar->name, 1, array_name_len, (FILE *)mat->fp); |
| 5173 |
for ( i = array_name_len; i < 4; i++ ) |
| 5174 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 5175 |
} else { |
| 5176 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 5177 |
const mat_uint32_t array_name_len = (mat_uint32_t)strlen(matvar->name); |
| 5178 |
const mat_uint8_t pad1 = 0; |
| 5179 |
|
| 5180 |
fwrite(&array_name_type, 4, 1, (FILE *)mat->fp); |
| 5181 |
fwrite(&array_name_len, 4, 1, (FILE *)mat->fp); |
| 5182 |
fwrite(matvar->name, 1, array_name_len, (FILE *)mat->fp); |
| 5183 |
if ( array_name_len % 8 ) |
| 5184 |
for ( i = array_name_len % 8; i < 8; i++ ) |
| 5185 |
fwrite(&pad1, 1, 1, (FILE *)mat->fp); |
| 5186 |
} |
| 5187 |
|
| 5188 |
if ( NULL != matvar->internal ) { |
| 5189 |
matvar->internal->datapos = ftello((FILE *)mat->fp); |
| 5190 |
if ( matvar->internal->datapos == -1L ) { |
| 5191 |
Mat_Critical("Couldn't determine file position"); |
| 5192 |
return MATIO_E_GENERIC_READ_ERROR; |
| 5193 |
} |
| 5194 |
} else { |
| 5195 |
|
| 5196 |
matvar->class_type = MAT_C_EMPTY; |
| 5197 |
} |
| 5198 |
WriteType(mat, matvar); |
| 5199 |
#if HAVE_ZLIB |
| 5200 |
} else if ( compress == MAT_COMPRESSION_ZLIB ) { |
| 5201 |
mat_uint32_t comp_buf[512]; |
| 5202 |
mat_uint32_t uncomp_buf[512]; |
| 5203 |
int buf_size = 512, err, i; |
| 5204 |
size_t matrix_max_buf_size; |
| 5205 |
z_streamp z; |
| 5206 |
|
| 5207 |
z = (z_streamp)calloc(1, sizeof(*z)); |
| 5208 |
if ( z == NULL ) |
| 5209 |
return MATIO_E_OUT_OF_MEMORY; |
| 5210 |
err = deflateInit(z, Z_DEFAULT_COMPRESSION); |
| 5211 |
if ( err != Z_OK ) { |
| 5212 |
free(z); |
| 5213 |
Mat_Critical("deflateInit returned %s", zError(err)); |
| 5214 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 5215 |
} |
| 5216 |
|
| 5217 |
matrix_type = MAT_T_COMPRESSED; |
| 5218 |
fwrite(&matrix_type, 4, 1, (FILE *)mat->fp); |
| 5219 |
fwrite(&pad4, 4, 1, (FILE *)mat->fp); |
| 5220 |
start = ftello((FILE *)mat->fp); |
| 5221 |
|
| 5222 |
|
| 5223 |
array_flags = matvar->class_type & CLASS_TYPE_MASK; |
| 5224 |
if ( matvar->isComplex ) |
| 5225 |
array_flags |= MAT_F_COMPLEX; |
| 5226 |
if ( matvar->isGlobal ) |
| 5227 |
array_flags |= MAT_F_GLOBAL; |
| 5228 |
if ( matvar->isLogical ) |
| 5229 |
array_flags |= MAT_F_LOGICAL; |
| 5230 |
if ( matvar->class_type == MAT_C_SPARSE ) |
| 5231 |
nzmax = ((mat_sparse_t *)matvar->data)->nzmax; |
| 5232 |
|
| 5233 |
memset(uncomp_buf, 0, sizeof(uncomp_buf)); |
| 5234 |
uncomp_buf[0] = MAT_T_MATRIX; |
| 5235 |
err = GetMatrixMaxBufSize(matvar, &matrix_max_buf_size); |
| 5236 |
if ( err ) { |
| 5237 |
free(z); |
| 5238 |
return err; |
| 5239 |
} |
| 5240 |
if ( matrix_max_buf_size > UINT32_MAX ) { |
| 5241 |
free(z); |
| 5242 |
return MATIO_E_INDEX_TOO_BIG; |
| 5243 |
} |
| 5244 |
uncomp_buf[1] = matrix_max_buf_size; |
| 5245 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 5246 |
z->avail_in = 8; |
| 5247 |
do { |
| 5248 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 5249 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 5250 |
deflate(z, Z_NO_FLUSH); |
| 5251 |
fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); |
| 5252 |
} while ( z->avail_out == 0 ); |
| 5253 |
uncomp_buf[0] = array_flags_type; |
| 5254 |
uncomp_buf[1] = array_flags_size; |
| 5255 |
uncomp_buf[2] = array_flags; |
| 5256 |
uncomp_buf[3] = nzmax; |
| 5257 |
|
| 5258 |
nBytes = matvar->rank * 4; |
| 5259 |
uncomp_buf[4] = dims_array_type; |
| 5260 |
uncomp_buf[5] = nBytes; |
| 5261 |
for ( i = 0; i < matvar->rank; i++ ) { |
| 5262 |
mat_int32_t dim; |
| 5263 |
dim = matvar->dims[i]; |
| 5264 |
uncomp_buf[6 + i] = dim; |
| 5265 |
} |
| 5266 |
if ( matvar->rank % 2 != 0 ) { |
| 5267 |
uncomp_buf[6 + i] = pad4; |
| 5268 |
i++; |
| 5269 |
} |
| 5270 |
|
| 5271 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 5272 |
z->avail_in = (6 + i) * sizeof(*uncomp_buf); |
| 5273 |
do { |
| 5274 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 5275 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 5276 |
deflate(z, Z_NO_FLUSH); |
| 5277 |
fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); |
| 5278 |
} while ( z->avail_out == 0 ); |
| 5279 |
|
| 5280 |
if ( strlen(matvar->name) <= 4 ) { |
| 5281 |
mat_uint32_t array_name_len = (mat_uint32_t)strlen(matvar->name); |
| 5282 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 5283 |
|
| 5284 |
memset(uncomp_buf, 0, 8); |
| 5285 |
uncomp_buf[0] = (array_name_len << 16) | array_name_type; |
| 5286 |
memcpy(uncomp_buf + 1, matvar->name, array_name_len); |
| 5287 |
|
| 5288 |
|
| 5289 |
|
| 5290 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 5291 |
z->avail_in = 8; |
| 5292 |
do { |
| 5293 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 5294 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 5295 |
deflate(z, Z_NO_FLUSH); |
| 5296 |
fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); |
| 5297 |
} while ( z->avail_out == 0 ); |
| 5298 |
} else { |
| 5299 |
mat_uint32_t array_name_len = (mat_uint32_t)strlen(matvar->name); |
| 5300 |
const mat_uint32_t array_name_type = MAT_T_INT8; |
| 5301 |
|
| 5302 |
memset(uncomp_buf, 0, buf_size * sizeof(*uncomp_buf)); |
| 5303 |
uncomp_buf[0] = array_name_type; |
| 5304 |
uncomp_buf[1] = array_name_len; |
| 5305 |
memcpy(uncomp_buf + 2, matvar->name, array_name_len); |
| 5306 |
if ( array_name_len % 8 ) |
| 5307 |
array_name_len += 8 - (array_name_len % 8); |
| 5308 |
z->next_in = ZLIB_BYTE_PTR(uncomp_buf); |
| 5309 |
z->avail_in = 8 + array_name_len; |
| 5310 |
do { |
| 5311 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 5312 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 5313 |
deflate(z, Z_NO_FLUSH); |
| 5314 |
fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); |
| 5315 |
} while ( z->avail_out == 0 ); |
| 5316 |
} |
| 5317 |
if ( NULL != matvar->internal ) { |
| 5318 |
matvar->internal->datapos = ftello((FILE *)mat->fp); |
| 5319 |
if ( matvar->internal->datapos == -1L ) { |
| 5320 |
free(z); |
| 5321 |
Mat_Critical("Couldn't determine file position"); |
| 5322 |
return MATIO_E_GENERIC_READ_ERROR; |
| 5323 |
} |
| 5324 |
} else { |
| 5325 |
|
| 5326 |
matvar->class_type = MAT_C_EMPTY; |
| 5327 |
} |
| 5328 |
WriteCompressedType(mat, matvar, z); |
| 5329 |
z->next_in = NULL; |
| 5330 |
z->avail_in = 0; |
| 5331 |
do { |
| 5332 |
z->next_out = ZLIB_BYTE_PTR(comp_buf); |
| 5333 |
z->avail_out = buf_size * sizeof(*comp_buf); |
| 5334 |
err = deflate(z, Z_FINISH); |
| 5335 |
fwrite(comp_buf, 1, buf_size * sizeof(*comp_buf) - z->avail_out, (FILE *)mat->fp); |
| 5336 |
} while ( err != Z_STREAM_END && z->avail_out == 0 ); |
| 5337 |
(void)deflateEnd(z); |
| 5338 |
free(z); |
| 5339 |
#endif |
| 5340 |
} |
| 5341 |
end = ftello((FILE *)mat->fp); |
| 5342 |
if ( start != -1L && end != -1L ) { |
| 5343 |
const mat_uint32_t _nBytes = (mat_uint32_t)(end - start); |
| 5344 |
(void)fseeko((FILE *)mat->fp, start - 4, SEEK_SET); |
| 5345 |
fwrite(&_nBytes, 4, 1, (FILE *)mat->fp); |
| 5346 |
(void)fseeko((FILE *)mat->fp, end, SEEK_SET); |
| 5347 |
} else { |
| 5348 |
Mat_Critical("Couldn't determine file position"); |
| 5349 |
} |
| 5350 |
|
| 5351 |
return MATIO_E_NO_ERROR; |
| 5352 |
} |
| 5353 |
|
| 5354 |
|
| 5355 |
|
| 5356 |
|
| 5357 |
|
| 5358 |
|
| 5359 |
|
| 5360 |
|
| 5361 |
|
| 5362 |
matvar_t * |
| 5363 |
Mat_VarReadNextInfo5(mat_t *mat) |
| 5364 |
{ |
| 5365 |
int err; |
| 5366 |
mat_uint32_t data_type, nBytes; |
| 5367 |
mat_off_t fpos; |
| 5368 |
matvar_t *matvar = NULL; |
| 5369 |
|
| 5370 |
if ( mat == NULL || mat->fp == NULL ) |
| 5371 |
return NULL; |
| 5372 |
|
| 5373 |
if ( IsEndOfFile((FILE *)mat->fp, &fpos) ) |
| 5374 |
return NULL; |
| 5375 |
|
| 5376 |
if ( fpos == -1L ) |
| 5377 |
return NULL; |
| 5378 |
|
| 5379 |
{ |
| 5380 |
size_t nbytes = 0; |
| 5381 |
err = Read(&data_type, sizeof(mat_uint32_t), 1, (FILE *)mat->fp, &nbytes); |
| 5382 |
if ( err || 0 == nbytes ) |
| 5383 |
return NULL; |
| 5384 |
} |
| 5385 |
err = Read(&nBytes, sizeof(mat_uint32_t), 1, (FILE *)mat->fp, NULL); |
| 5386 |
if ( err ) |
| 5387 |
return NULL; |
| 5388 |
if ( mat->byteswap ) { |
| 5389 |
(void)Mat_uint32Swap(&data_type); |
| 5390 |
(void)Mat_uint32Swap(&nBytes); |
| 5391 |
} |
| 5392 |
if ( nBytes > UINT32_MAX - 8 ) |
| 5393 |
return NULL; |
| 5394 |
switch ( data_type ) { |
| 5395 |
case MAT_T_COMPRESSED: { |
| 5396 |
#if HAVE_ZLIB |
| 5397 |
mat_uint32_t uncomp_buf[16]; |
| 5398 |
size_t bytesread = 0; |
| 5399 |
|
| 5400 |
memset(uncomp_buf, 0, sizeof(uncomp_buf)); |
| 5401 |
matvar = Mat_VarCalloc(); |
| 5402 |
if ( NULL == matvar ) { |
| 5403 |
Mat_Critical("Couldn't allocate memory"); |
| 5404 |
break; |
| 5405 |
} |
| 5406 |
|
| 5407 |
matvar->compression = MAT_COMPRESSION_ZLIB; |
| 5408 |
matvar->internal->z = (z_streamp)calloc(1, sizeof(z_stream)); |
| 5409 |
err = inflateInit(matvar->internal->z); |
| 5410 |
if ( err != Z_OK ) { |
| 5411 |
Mat_VarFree(matvar); |
| 5412 |
matvar = NULL; |
| 5413 |
Mat_Critical("inflateInit returned %s", zError(err)); |
| 5414 |
break; |
| 5415 |
} |
| 5416 |
|
| 5417 |
|
| 5418 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 8, &bytesread); |
| 5419 |
if ( err ) { |
| 5420 |
Mat_VarFree(matvar); |
| 5421 |
matvar = NULL; |
| 5422 |
break; |
| 5423 |
} |
| 5424 |
if ( mat->byteswap ) { |
| 5425 |
(void)Mat_uint32Swap(uncomp_buf); |
| 5426 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 5427 |
} |
| 5428 |
if ( uncomp_buf[0] != MAT_T_MATRIX ) { |
| 5429 |
(void)fseeko((FILE *)mat->fp, (mat_off_t)(nBytes - bytesread), SEEK_CUR); |
| 5430 |
Mat_VarFree(matvar); |
| 5431 |
matvar = NULL; |
| 5432 |
Mat_Critical("Uncompressed type not MAT_T_MATRIX"); |
| 5433 |
break; |
| 5434 |
} |
| 5435 |
|
| 5436 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 16, &bytesread); |
| 5437 |
if ( err ) { |
| 5438 |
Mat_VarFree(matvar); |
| 5439 |
matvar = NULL; |
| 5440 |
break; |
| 5441 |
} |
| 5442 |
if ( mat->byteswap ) { |
| 5443 |
(void)Mat_uint32Swap(uncomp_buf); |
| 5444 |
(void)Mat_uint32Swap(uncomp_buf + 2); |
| 5445 |
(void)Mat_uint32Swap(uncomp_buf + 3); |
| 5446 |
} |
| 5447 |
|
| 5448 |
if ( uncomp_buf[0] == MAT_T_UINT32 ) { |
| 5449 |
mat_uint32_t array_flags = uncomp_buf[2]; |
| 5450 |
matvar->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags); |
| 5451 |
matvar->isComplex = (array_flags & MAT_F_COMPLEX); |
| 5452 |
matvar->isGlobal = (array_flags & MAT_F_GLOBAL); |
| 5453 |
matvar->isLogical = (array_flags & MAT_F_LOGICAL); |
| 5454 |
if ( matvar->class_type == MAT_C_SPARSE ) { |
| 5455 |
|
| 5456 |
matvar->nbytes = uncomp_buf[3]; |
| 5457 |
} |
| 5458 |
} |
| 5459 |
if ( matvar->class_type != MAT_C_OPAQUE ) { |
| 5460 |
mat_uint32_t *dims = NULL; |
| 5461 |
int do_clean = 0; |
| 5462 |
err = InflateRankDims(mat, matvar->internal->z, uncomp_buf, sizeof(uncomp_buf), |
| 5463 |
&dims, &bytesread); |
| 5464 |
if ( NULL == dims ) { |
| 5465 |
dims = uncomp_buf + 2; |
| 5466 |
} else { |
| 5467 |
do_clean = 1; |
| 5468 |
} |
| 5469 |
if ( err ) { |
| 5470 |
if ( do_clean ) { |
| 5471 |
free(dims); |
| 5472 |
} |
| 5473 |
Mat_VarFree(matvar); |
| 5474 |
matvar = NULL; |
| 5475 |
break; |
| 5476 |
} |
| 5477 |
if ( mat->byteswap ) { |
| 5478 |
(void)Mat_uint32Swap(uncomp_buf); |
| 5479 |
(void)Mat_uint32Swap(uncomp_buf + 1); |
| 5480 |
} |
| 5481 |
|
| 5482 |
if ( uncomp_buf[0] == MAT_T_INT32 ) { |
| 5483 |
int j; |
| 5484 |
size_t size; |
| 5485 |
int nbytes = uncomp_buf[1]; |
| 5486 |
matvar->rank = nbytes / 4; |
| 5487 |
if ( 0 == do_clean && matvar->rank > 13 ) { |
| 5488 |
int rank = matvar->rank; |
| 5489 |
matvar->rank = 0; |
| 5490 |
Mat_Critical("%d is not a valid rank", rank); |
| 5491 |
break; |
| 5492 |
} |
| 5493 |
err = Mul(&size, matvar->rank, sizeof(*matvar->dims)); |
| 5494 |
if ( err ) { |
| 5495 |
if ( do_clean ) { |
| 5496 |
free(dims); |
| 5497 |
} |
| 5498 |
(void)fseeko((FILE *)mat->fp, (mat_off_t)(nBytes - bytesread), SEEK_CUR); |
| 5499 |
Mat_VarFree(matvar); |
| 5500 |
matvar = NULL; |
| 5501 |
Mat_Critical("Integer multiplication overflow"); |
| 5502 |
break; |
| 5503 |
} |
| 5504 |
matvar->dims = (size_t *)malloc(size); |
| 5505 |
if ( NULL == matvar->dims ) { |
| 5506 |
if ( do_clean ) |
| 5507 |
free(dims); |
| 5508 |
(void)fseeko((FILE *)mat->fp, (mat_off_t)(nBytes - bytesread), SEEK_CUR); |
| 5509 |
Mat_VarFree(matvar); |
| 5510 |
matvar = NULL; |
| 5511 |
Mat_Critical("Couldn't allocate memory"); |
| 5512 |
break; |
| 5513 |
} |
| 5514 |
if ( mat->byteswap ) { |
| 5515 |
for ( j = 0; j < matvar->rank; j++ ) |
| 5516 |
matvar->dims[j] = Mat_uint32Swap(dims + j); |
| 5517 |
} else { |
| 5518 |
for ( j = 0; j < matvar->rank; j++ ) |
| 5519 |
matvar->dims[j] = dims[j]; |
| 5520 |
} |
| 5521 |
} |
| 5522 |
if ( do_clean ) { |
| 5523 |
free(dims); |
| 5524 |
} |
| 5525 |
|
| 5526 |
err = Inflate(mat, matvar->internal->z, uncomp_buf, 8, &bytesread); |
| 5527 |
if ( err ) { |
| 5528 |
Mat_VarFree(matvar); |
| 5529 |
matvar = NULL; |
| 5530 |
break; |
| 5531 |
} |
| 5532 |
if ( mat->byteswap ) |
| 5533 |
(void)Mat_uint32Swap(uncomp_buf); |
| 5534 |
|
| 5535 |
if ( uncomp_buf[0] == MAT_T_INT8 ) { |
| 5536 |
mat_uint32_t len, len_pad; |
| 5537 |
if ( mat->byteswap ) |
| 5538 |
len = Mat_uint32Swap(uncomp_buf + 1); |
| 5539 |
else |
| 5540 |
len = uncomp_buf[1]; |
| 5541 |
|
| 5542 |
if ( len % 8 == 0 ) |
| 5543 |
len_pad = len; |
| 5544 |
else if ( len < UINT32_MAX - 8 + (len % 8) ) |
| 5545 |
len_pad = len + 8 - (len % 8); |
| 5546 |
else { |
| 5547 |
Mat_VarFree(matvar); |
| 5548 |
matvar = NULL; |
| 5549 |
break; |
| 5550 |
} |
| 5551 |
matvar->name = (char *)malloc(len_pad + 1); |
| 5552 |
if ( NULL != matvar->name ) { |
| 5553 |
|
| 5554 |
err = Inflate(mat, matvar->internal->z, matvar->name, len_pad, &bytesread); |
| 5555 |
if ( err ) { |
| 5556 |
Mat_VarFree(matvar); |
| 5557 |
matvar = NULL; |
| 5558 |
break; |
| 5559 |
} |
| 5560 |
matvar->name[len] = '\0'; |
| 5561 |
} |
| 5562 |
} else { |
| 5563 |
mat_uint32_t len = (uncomp_buf[0] & 0xffff0000) >> 16; |
| 5564 |
if ( ((uncomp_buf[0] & 0x0000ffff) == MAT_T_INT8) && len > 0 && len <= 4 ) { |
| 5565 |
|
| 5566 |
matvar->name = (char *)malloc(len + 1); |
| 5567 |
if ( NULL != matvar->name ) { |
| 5568 |
memcpy(matvar->name, uncomp_buf + 1, len); |
| 5569 |
matvar->name[len] = '\0'; |
| 5570 |
} |
| 5571 |
} |
| 5572 |
} |
| 5573 |
if ( matvar->class_type == MAT_C_STRUCT ) |
| 5574 |
(void)ReadNextStructField(mat, matvar); |
| 5575 |
else if ( matvar->class_type == MAT_C_CELL ) |
| 5576 |
(void)ReadNextCell(mat, matvar); |
| 5577 |
(void)fseeko((FILE *)mat->fp, -(mat_off_t)matvar->internal->z->avail_in, SEEK_CUR); |
| 5578 |
matvar->internal->datapos = ftello((FILE *)mat->fp); |
| 5579 |
if ( matvar->internal->datapos == -1L ) { |
| 5580 |
Mat_Critical("Couldn't determine file position"); |
| 5581 |
} |
| 5582 |
} |
| 5583 |
(void)fseeko((FILE *)mat->fp, nBytes + 8 + fpos, SEEK_SET); |
| 5584 |
break; |
| 5585 |
#else |
| 5586 |
Mat_Critical( |
| 5587 |
"Compressed variable found in \"%s\", but matio was " |
| 5588 |
"built without zlib support", |
| 5589 |
mat->filename); |
| 5590 |
(void)fseeko((FILE *)mat->fp, nBytes + 8 + fpos, SEEK_SET); |
| 5591 |
return NULL; |
| 5592 |
#endif |
| 5593 |
} |
| 5594 |
case MAT_T_MATRIX: { |
| 5595 |
mat_uint32_t buf[6]; |
| 5596 |
|
| 5597 |
|
| 5598 |
err = Read(buf, 4, 6, (FILE *)mat->fp, NULL); |
| 5599 |
if ( err ) { |
| 5600 |
(void)fseeko((FILE *)mat->fp, fpos, SEEK_SET); |
| 5601 |
break; |
| 5602 |
} |
| 5603 |
if ( mat->byteswap ) { |
| 5604 |
(void)Mat_uint32Swap(buf); |
| 5605 |
(void)Mat_uint32Swap(buf + 1); |
| 5606 |
(void)Mat_uint32Swap(buf + 2); |
| 5607 |
(void)Mat_uint32Swap(buf + 3); |
| 5608 |
(void)Mat_uint32Swap(buf + 4); |
| 5609 |
(void)Mat_uint32Swap(buf + 5); |
| 5610 |
} |
| 5611 |
|
| 5612 |
matvar = Mat_VarCalloc(); |
| 5613 |
if ( NULL == matvar ) { |
| 5614 |
Mat_Critical("Couldn't allocate memory"); |
| 5615 |
break; |
| 5616 |
} |
| 5617 |
|
| 5618 |
|
| 5619 |
if ( buf[0] == MAT_T_UINT32 || buf[0] == MAT_T_INT32 ) { |
| 5620 |
mat_uint32_t array_flags = buf[2]; |
| 5621 |
matvar->class_type = CLASS_FROM_ARRAY_FLAGS(array_flags); |
| 5622 |
matvar->isComplex = (array_flags & MAT_F_COMPLEX); |
| 5623 |
matvar->isGlobal = (array_flags & MAT_F_GLOBAL); |
| 5624 |
matvar->isLogical = (array_flags & MAT_F_LOGICAL); |
| 5625 |
if ( matvar->class_type == MAT_C_SPARSE ) { |
| 5626 |
|
| 5627 |
matvar->nbytes = buf[3]; |
| 5628 |
} |
| 5629 |
} |
| 5630 |
|
| 5631 |
{ |
| 5632 |
size_t nbytes = 0; |
| 5633 |
err = ReadRankDims(mat, matvar, (enum matio_types)buf[4], buf[5], &nbytes); |
| 5634 |
if ( err ) { |
| 5635 |
Mat_VarFree(matvar); |
| 5636 |
matvar = NULL; |
| 5637 |
(void)fseeko((FILE *)mat->fp, fpos, SEEK_SET); |
| 5638 |
break; |
| 5639 |
} |
| 5640 |
} |
| 5641 |
|
| 5642 |
err = Read(buf, 4, 2, (FILE *)mat->fp, NULL); |
| 5643 |
if ( err ) { |
| 5644 |
Mat_VarFree(matvar); |
| 5645 |
matvar = NULL; |
| 5646 |
(void)fseeko((FILE *)mat->fp, fpos, SEEK_SET); |
| 5647 |
break; |
| 5648 |
} |
| 5649 |
if ( mat->byteswap ) |
| 5650 |
(void)Mat_uint32Swap(buf); |
| 5651 |
|
| 5652 |
if ( buf[0] == MAT_T_INT8 ) { |
| 5653 |
mat_uint32_t len, len_pad; |
| 5654 |
if ( mat->byteswap ) |
| 5655 |
len = Mat_uint32Swap(buf + 1); |
| 5656 |
else |
| 5657 |
len = buf[1]; |
| 5658 |
if ( len % 8 == 0 ) |
| 5659 |
len_pad = len; |
| 5660 |
else if ( len < UINT32_MAX - 8 + (len % 8) ) |
| 5661 |
len_pad = len + 8 - (len % 8); |
| 5662 |
else { |
| 5663 |
Mat_VarFree(matvar); |
| 5664 |
matvar = NULL; |
| 5665 |
(void)fseeko((FILE *)mat->fp, fpos, SEEK_SET); |
| 5666 |
break; |
| 5667 |
} |
| 5668 |
if ( len_pad > MAX_READ_SIZE_WITHOUT_EOF_CHECK ) { |
| 5669 |
err = CheckSeekFile((FILE *)mat->fp, (mat_off_t)len_pad); |
| 5670 |
if ( err ) { |
| 5671 |
Mat_VarFree(matvar); |
| 5672 |
matvar = NULL; |
| 5673 |
(void)fseeko((FILE *)mat->fp, fpos, SEEK_SET); |
| 5674 |
break; |
| 5675 |
} |
| 5676 |
} |
| 5677 |
matvar->name = (char *)malloc(len_pad + 1); |
| 5678 |
if ( NULL != matvar->name ) { |
| 5679 |
err = Read(matvar->name, 1, len_pad, (FILE *)mat->fp, NULL); |
| 5680 |
if ( MATIO_E_NO_ERROR == err ) { |
| 5681 |
matvar->name[len] = '\0'; |
| 5682 |
} else { |
| 5683 |
Mat_VarFree(matvar); |
| 5684 |
matvar = NULL; |
| 5685 |
(void)fseeko((FILE *)mat->fp, fpos, SEEK_SET); |
| 5686 |
break; |
| 5687 |
} |
| 5688 |
} |
| 5689 |
} else { |
| 5690 |
mat_uint32_t len = (buf[0] & 0xffff0000) >> 16; |
| 5691 |
if ( ((buf[0] & 0x0000ffff) == MAT_T_INT8) && len > 0 && len <= 4 ) { |
| 5692 |
|
| 5693 |
matvar->name = (char *)malloc(len + 1); |
| 5694 |
if ( NULL != matvar->name ) { |
| 5695 |
memcpy(matvar->name, buf + 1, len); |
| 5696 |
matvar->name[len] = '\0'; |
| 5697 |
} |
| 5698 |
} |
| 5699 |
} |
| 5700 |
if ( matvar->class_type == MAT_C_STRUCT ) |
| 5701 |
(void)ReadNextStructField(mat, matvar); |
| 5702 |
else if ( matvar->class_type == MAT_C_CELL ) |
| 5703 |
(void)ReadNextCell(mat, matvar); |
| 5704 |
else if ( matvar->class_type == MAT_C_FUNCTION ) |
| 5705 |
(void)ReadNextFunctionHandle(mat, matvar); |
| 5706 |
matvar->internal->datapos = ftello((FILE *)mat->fp); |
| 5707 |
if ( matvar->internal->datapos == -1L ) { |
| 5708 |
Mat_Critical("Couldn't determine file position"); |
| 5709 |
} |
| 5710 |
(void)fseeko((FILE *)mat->fp, nBytes + 8 + fpos, SEEK_SET); |
| 5711 |
break; |
| 5712 |
} |
| 5713 |
default: |
| 5714 |
Mat_Critical("%u is not valid (MAT_T_MATRIX or MAT_T_COMPRESSED)", data_type); |
| 5715 |
return NULL; |
| 5716 |
} |
| 5717 |
|
| 5718 |
return matvar; |
| 5719 |
} |