| 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 |
#include "matio_private.h" |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <limits.h> |
| 36 |
|
| 37 |
#if HAVE_ZLIB |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
int |
| 53 |
InflateSkip(mat_t *mat, z_streamp z, int nBytes, size_t *bytesread) |
| 54 |
{ |
| 55 |
mat_uint8_t comp_buf[READ_BLOCK_SIZE], uncomp_buf[READ_BLOCK_SIZE]; |
| 56 |
int n, err = MATIO_E_NO_ERROR, cnt = 0; |
| 57 |
|
| 58 |
if ( nBytes < 1 ) |
| 59 |
return MATIO_E_NO_ERROR; |
| 60 |
|
| 61 |
n = nBytes < READ_BLOCK_SIZE ? nBytes : READ_BLOCK_SIZE; |
| 62 |
if ( !z->avail_in ) { |
| 63 |
size_t nbytes = fread(comp_buf, 1, n, (FILE *)mat->fp); |
| 64 |
if ( 0 == nbytes ) { |
| 65 |
return err; |
| 66 |
} |
| 67 |
if ( NULL != bytesread ) { |
| 68 |
*bytesread += nbytes; |
| 69 |
} |
| 70 |
z->avail_in = (uInt)nbytes; |
| 71 |
z->next_in = comp_buf; |
| 72 |
} |
| 73 |
z->avail_out = n; |
| 74 |
z->next_out = uncomp_buf; |
| 75 |
err = inflate(z, Z_NO_FLUSH); |
| 76 |
if ( err == Z_STREAM_END ) { |
| 77 |
z->next_out = NULL; |
| 78 |
return MATIO_E_NO_ERROR; |
| 79 |
} else if ( err != Z_OK ) { |
| 80 |
z->next_out = NULL; |
| 81 |
Mat_Critical("InflateSkip: inflate returned %s", |
| 82 |
zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err)); |
| 83 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 84 |
} else { |
| 85 |
err = MATIO_E_NO_ERROR; |
| 86 |
} |
| 87 |
if ( !z->avail_out ) { |
| 88 |
cnt += n; |
| 89 |
n = nBytes - cnt; |
| 90 |
if ( n > READ_BLOCK_SIZE ) { |
| 91 |
n = READ_BLOCK_SIZE; |
| 92 |
} |
| 93 |
z->avail_out = n; |
| 94 |
z->next_out = uncomp_buf; |
| 95 |
} |
| 96 |
while ( cnt < nBytes ) { |
| 97 |
if ( !z->avail_in ) { |
| 98 |
size_t nbytes = fread(comp_buf, 1, n, (FILE *)mat->fp); |
| 99 |
if ( 0 == nbytes ) { |
| 100 |
break; |
| 101 |
} |
| 102 |
if ( NULL != bytesread ) { |
| 103 |
*bytesread += nbytes; |
| 104 |
} |
| 105 |
z->avail_in = (uInt)nbytes; |
| 106 |
z->next_in = comp_buf; |
| 107 |
} |
| 108 |
err = inflate(z, Z_NO_FLUSH); |
| 109 |
if ( err == Z_STREAM_END ) { |
| 110 |
err = MATIO_E_NO_ERROR; |
| 111 |
break; |
| 112 |
} else if ( err != Z_OK ) { |
| 113 |
const char *errMsg = zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err); |
| 114 |
err = MATIO_E_FILE_FORMAT_VIOLATION; |
| 115 |
Mat_Critical("InflateSkip: inflate returned %s", errMsg); |
| 116 |
break; |
| 117 |
} else { |
| 118 |
err = MATIO_E_NO_ERROR; |
| 119 |
} |
| 120 |
if ( !z->avail_out ) { |
| 121 |
cnt += n; |
| 122 |
n = nBytes - cnt; |
| 123 |
if ( n > READ_BLOCK_SIZE ) { |
| 124 |
n = READ_BLOCK_SIZE; |
| 125 |
} |
| 126 |
z->avail_out = n; |
| 127 |
z->next_out = uncomp_buf; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if ( z->avail_in ) { |
| 132 |
const mat_off_t offset = -(mat_off_t)z->avail_in; |
| 133 |
(void)fseeko((FILE *)mat->fp, offset, SEEK_CUR); |
| 134 |
if ( NULL != bytesread ) { |
| 135 |
*bytesread -= z->avail_in; |
| 136 |
} |
| 137 |
z->avail_in = 0; |
| 138 |
} |
| 139 |
|
| 140 |
z->next_out = NULL; |
| 141 |
|
| 142 |
return err; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
int |
| 157 |
InflateSkipData(mat_t *mat, z_streamp z, enum matio_types data_type, int len) |
| 158 |
{ |
| 159 |
if ( mat == NULL || z == NULL || len < 1 ) |
| 160 |
return MATIO_E_BAD_ARGUMENT; |
| 161 |
|
| 162 |
switch ( data_type ) { |
| 163 |
case MAT_T_UTF8: |
| 164 |
case MAT_T_UTF16: |
| 165 |
case MAT_T_UTF32: |
| 166 |
return MATIO_E_OPERATION_NOT_SUPPORTED; |
| 167 |
default: |
| 168 |
break; |
| 169 |
} |
| 170 |
|
| 171 |
return InflateSkip(mat, z, (unsigned int)Mat_SizeOf(data_type) * len, NULL); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
int |
| 190 |
InflateRankDims(mat_t *mat, z_streamp z, void *buf, size_t nBytes, mat_uint32_t **dims, |
| 191 |
size_t *bytesread) |
| 192 |
{ |
| 193 |
mat_uint32_t tag[2]; |
| 194 |
mat_uint32_t rank, i; |
| 195 |
int err; |
| 196 |
size_t nbytes = 0; |
| 197 |
|
| 198 |
if ( buf == NULL ) |
| 199 |
return MATIO_E_BAD_ARGUMENT; |
| 200 |
|
| 201 |
err = Inflate(mat, z, buf, 8, bytesread); |
| 202 |
if ( err ) { |
| 203 |
return err; |
| 204 |
} |
| 205 |
tag[0] = *(mat_uint32_t *)buf; |
| 206 |
tag[1] = *((mat_uint32_t *)buf + 1); |
| 207 |
if ( mat->byteswap ) { |
| 208 |
Mat_uint32Swap(tag); |
| 209 |
Mat_uint32Swap(tag + 1); |
| 210 |
} |
| 211 |
if ( (tag[0] & 0x0000ffff) != MAT_T_INT32 ) { |
| 212 |
Mat_Critical("InflateRankDims: Reading dimensions expected type MAT_T_INT32"); |
| 213 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 214 |
} |
| 215 |
rank = tag[1]; |
| 216 |
if ( rank % 8 != 0 ) |
| 217 |
i = 8 - (rank % 8); |
| 218 |
else |
| 219 |
i = 0; |
| 220 |
|
| 221 |
if ( rank > INT_MAX - i - 2 ) { |
| 222 |
Mat_Critical("InflateRankDims: Reading dimensions expected rank in integer range"); |
| 223 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 224 |
} |
| 225 |
rank += i; |
| 226 |
|
| 227 |
err = Mul(&nbytes, rank + 2, sizeof(mat_uint32_t)); |
| 228 |
if ( err ) { |
| 229 |
Mat_Critical("Integer multiplication overflow"); |
| 230 |
return err; |
| 231 |
} |
| 232 |
|
| 233 |
if ( nbytes <= nBytes ) { |
| 234 |
err = Inflate(mat, z, (mat_uint32_t *)buf + 2, rank, bytesread); |
| 235 |
} else { |
| 236 |
|
| 237 |
*dims = (mat_uint32_t *)calloc(rank, sizeof(mat_uint32_t)); |
| 238 |
if ( NULL != *dims ) { |
| 239 |
err = Inflate(mat, z, *dims, rank, bytesread); |
| 240 |
} else { |
| 241 |
*((mat_uint32_t *)buf + 1) = 0; |
| 242 |
Mat_Critical("Error allocating memory for dims"); |
| 243 |
return MATIO_E_OUT_OF_MEMORY; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
return err; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
int |
| 263 |
Inflate(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes, size_t *bytesread) |
| 264 |
{ |
| 265 |
mat_uint8_t comp_buf[4]; |
| 266 |
int err = MATIO_E_NO_ERROR; |
| 267 |
|
| 268 |
if ( buf == NULL ) |
| 269 |
return MATIO_E_BAD_ARGUMENT; |
| 270 |
|
| 271 |
if ( !z->avail_in ) { |
| 272 |
size_t nbytes = fread(comp_buf, 1, 1, (FILE *)mat->fp); |
| 273 |
if ( 0 == nbytes ) { |
| 274 |
return err; |
| 275 |
} |
| 276 |
if ( NULL != bytesread ) { |
| 277 |
*bytesread += nbytes; |
| 278 |
} |
| 279 |
z->avail_in = (uInt)nbytes; |
| 280 |
z->next_in = comp_buf; |
| 281 |
} |
| 282 |
z->avail_out = nBytes; |
| 283 |
z->next_out = ZLIB_BYTE_PTR(buf); |
| 284 |
err = inflate(z, Z_NO_FLUSH); |
| 285 |
if ( err != Z_OK ) { |
| 286 |
Mat_Critical("Inflate: inflate returned %s", |
| 287 |
zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err)); |
| 288 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 289 |
} else { |
| 290 |
err = MATIO_E_NO_ERROR; |
| 291 |
} |
| 292 |
while ( z->avail_out && !z->avail_in ) { |
| 293 |
size_t nbytes = fread(comp_buf, 1, 1, (FILE *)mat->fp); |
| 294 |
if ( 0 == nbytes ) { |
| 295 |
break; |
| 296 |
} |
| 297 |
if ( NULL != bytesread ) { |
| 298 |
*bytesread += nbytes; |
| 299 |
} |
| 300 |
z->avail_in = (uInt)nbytes; |
| 301 |
z->next_in = comp_buf; |
| 302 |
err = inflate(z, Z_NO_FLUSH); |
| 303 |
if ( err != Z_OK ) { |
| 304 |
Mat_Critical("Inflate: inflate returned %s", |
| 305 |
zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err)); |
| 306 |
return MATIO_E_FILE_FORMAT_VIOLATION; |
| 307 |
} else { |
| 308 |
err = MATIO_E_NO_ERROR; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
if ( z->avail_in ) { |
| 313 |
const mat_off_t offset = -(mat_off_t)z->avail_in; |
| 314 |
(void)fseeko((FILE *)mat->fp, offset, SEEK_CUR); |
| 315 |
if ( NULL != bytesread ) { |
| 316 |
*bytesread -= z->avail_in; |
| 317 |
} |
| 318 |
z->avail_in = 0; |
| 319 |
} |
| 320 |
|
| 321 |
if ( z->avail_out && feof((FILE *)mat->fp) ) { |
| 322 |
Mat_Warning( |
| 323 |
"Unexpected end-of-file: " |
| 324 |
"Processed %u bytes, expected %u bytes", |
| 325 |
nBytes - z->avail_out, nBytes); |
| 326 |
memset(buf, 0, nBytes); |
| 327 |
} |
| 328 |
|
| 329 |
return err; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
int |
| 344 |
InflateData(mat_t *mat, z_streamp z, void *buf, unsigned int nBytes) |
| 345 |
{ |
| 346 |
mat_uint8_t comp_buf[READ_BLOCK_SIZE]; |
| 347 |
int err = MATIO_E_NO_ERROR; |
| 348 |
unsigned int n; |
| 349 |
size_t bytesread = 0; |
| 350 |
|
| 351 |
if ( buf == NULL ) |
| 352 |
return MATIO_E_BAD_ARGUMENT; |
| 353 |
if ( nBytes == 0 ) { |
| 354 |
return MATIO_E_NO_ERROR; |
| 355 |
} |
| 356 |
|
| 357 |
n = nBytes < READ_BLOCK_SIZE ? nBytes : READ_BLOCK_SIZE; |
| 358 |
if ( !z->avail_in ) { |
| 359 |
size_t nbytes = fread(comp_buf, 1, n, (FILE *)mat->fp); |
| 360 |
if ( 0 == nbytes ) { |
| 361 |
return err; |
| 362 |
} |
| 363 |
bytesread += nbytes; |
| 364 |
z->avail_in = (uInt)nbytes; |
| 365 |
z->next_in = comp_buf; |
| 366 |
} |
| 367 |
z->avail_out = nBytes; |
| 368 |
z->next_out = ZLIB_BYTE_PTR(buf); |
| 369 |
err = inflate(z, Z_NO_FLUSH); |
| 370 |
if ( err == Z_STREAM_END ) { |
| 371 |
return MATIO_E_NO_ERROR; |
| 372 |
} else if ( err != Z_OK ) { |
| 373 |
Mat_Critical("InflateData: inflate returned %s", |
| 374 |
zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err)); |
| 375 |
return MATIO_E_FAIL_TO_IDENTIFY; |
| 376 |
} else { |
| 377 |
err = MATIO_E_NO_ERROR; |
| 378 |
} |
| 379 |
while ( z->avail_out && !z->avail_in ) { |
| 380 |
size_t nbytes; |
| 381 |
if ( nBytes > READ_BLOCK_SIZE + bytesread ) { |
| 382 |
nbytes = fread(comp_buf, 1, READ_BLOCK_SIZE, (FILE *)mat->fp); |
| 383 |
} else if ( nBytes < 1 + bytesread ) { |
| 384 |
nbytes = fread(comp_buf, 1, 1, (FILE *)mat->fp); |
| 385 |
} else { |
| 386 |
nbytes = fread(comp_buf, 1, nBytes - bytesread, (FILE *)mat->fp); |
| 387 |
} |
| 388 |
if ( 0 == nbytes ) { |
| 389 |
break; |
| 390 |
} |
| 391 |
bytesread += nbytes; |
| 392 |
z->avail_in = (uInt)nbytes; |
| 393 |
z->next_in = comp_buf; |
| 394 |
err = inflate(z, Z_NO_FLUSH); |
| 395 |
if ( err == Z_STREAM_END ) { |
| 396 |
err = MATIO_E_NO_ERROR; |
| 397 |
break; |
| 398 |
} else if ( err != Z_OK ) { |
| 399 |
const char *errMsg = zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err); |
| 400 |
err = MATIO_E_FAIL_TO_IDENTIFY; |
| 401 |
Mat_Critical("InflateData: inflate returned %s", errMsg); |
| 402 |
break; |
| 403 |
} else { |
| 404 |
err = MATIO_E_NO_ERROR; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
if ( z->avail_in ) { |
| 409 |
const mat_off_t offset = -(mat_off_t)z->avail_in; |
| 410 |
(void)fseeko((FILE *)mat->fp, offset, SEEK_CUR); |
| 411 |
|
| 412 |
z->avail_in = 0; |
| 413 |
} |
| 414 |
|
| 415 |
if ( z->avail_out && feof((FILE *)mat->fp) ) { |
| 416 |
Mat_Warning("InflateData: Read beyond EOF error: Processed %u bytes, expected %u bytes", |
| 417 |
nBytes - z->avail_out, nBytes); |
| 418 |
memset(buf, 0, nBytes); |
| 419 |
} |
| 420 |
|
| 421 |
return err; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
#endif |