-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
H5Odtype.c
2392 lines (2039 loc) · 98.8 KB
/
H5Odtype.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* [email protected]. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "H5Omodule.h" /* This source code file is part of the H5O module */
#define H5T_FRIEND /*prevent warning from including H5Tpkg */
#include "H5private.h" /* Generic Functions */
#include "H5Dprivate.h" /* Datasets */
#include "H5Eprivate.h" /* Error handling */
#include "H5Fprivate.h" /* Files */
#include "H5FLprivate.h" /* Free Lists */
#include "H5MMprivate.h" /* Memory management */
#include "H5Opkg.h" /* Object headers */
#include "H5Tpkg.h" /* Datatypes */
#include "H5VMprivate.h" /* Vectors and arrays */
/* PRIVATE PROTOTYPES */
static herr_t H5O__dtype_encode(H5F_t *f, uint8_t *p, const void *mesg);
static void *H5O__dtype_decode(H5F_t *f, H5O_t *open_oh, unsigned mesg_flags, unsigned *ioflags,
size_t p_size, const uint8_t *p);
static void *H5O__dtype_copy(const void *_mesg, void *_dest);
static size_t H5O__dtype_size(const H5F_t *f, const void *_mesg);
static herr_t H5O__dtype_reset(void *_mesg);
static herr_t H5O__dtype_free(void *_mesg);
static herr_t H5O__dtype_set_share(void *_mesg, const H5O_shared_t *sh);
static htri_t H5O__dtype_can_share(const void *_mesg);
static herr_t H5O__dtype_pre_copy_file(H5F_t *file_src, const void *mesg_src, bool *deleted,
const H5O_copy_t *cpy_info, void *_udata);
static void *H5O__dtype_copy_file(H5F_t *file_src, const H5O_msg_class_t *mesg_type, void *native_src,
H5F_t *file_dst, bool *recompute_size, H5O_copy_t *cpy_info, void *udata);
static herr_t H5O__dtype_shared_post_copy_upd(const H5O_loc_t *src_oloc, const void *mesg_src,
H5O_loc_t *dst_oloc, void *mesg_dst, H5O_copy_t *cpy_info);
static herr_t H5O__dtype_debug(H5F_t *f, const void *_mesg, FILE *stream, int indent, int fwidth);
/* Set up & include shared message "interface" info */
#define H5O_SHARED_TYPE H5O_MSG_DTYPE
#define H5O_SHARED_DECODE H5O__dtype_shared_decode
#define H5O_SHARED_DECODE_REAL H5O__dtype_decode
#define H5O_SHARED_ENCODE H5O__dtype_shared_encode
#define H5O_SHARED_ENCODE_REAL H5O__dtype_encode
#define H5O_SHARED_SIZE H5O__dtype_shared_size
#define H5O_SHARED_SIZE_REAL H5O__dtype_size
#define H5O_SHARED_DELETE H5O__dtype_shared_delete
#undef H5O_SHARED_DELETE_REAL
#define H5O_SHARED_LINK H5O__dtype_shared_link
#undef H5O_SHARED_LINK_REAL
#define H5O_SHARED_COPY_FILE H5O__dtype_shared_copy_file
#define H5O_SHARED_COPY_FILE_REAL H5O__dtype_copy_file
#define H5O_SHARED_POST_COPY_FILE H5O__dtype_shared_post_copy_file
#undef H5O_SHARED_POST_COPY_FILE_REAL
#define H5O_SHARED_POST_COPY_FILE_UPD H5O__dtype_shared_post_copy_upd
#define H5O_SHARED_DEBUG H5O__dtype_shared_debug
#define H5O_SHARED_DEBUG_REAL H5O__dtype_debug
#include "H5Oshared.h" /* Shared Object Header Message Callbacks */
/* Macros to check for the proper version of a datatype */
#ifdef H5_STRICT_FORMAT_CHECKS
/* If the version is too low, give an error. No error if nochange is set
* because in that case we are either debugging or deleting the object header */
#define H5O_DTYPE_CHECK_VERSION(DT, VERS, MIN_VERS, IOF, CLASS, ERR) \
if (((VERS) < (MIN_VERS)) && !(*(IOF)&H5O_DECODEIO_NOCHANGE)) \
HGOTO_ERROR(H5E_DATATYPE, H5E_VERSION, ERR, "incorrect " CLASS " datatype version");
#else /* H5_STRICT_FORMAT_CHECKS */
/* If the version is too low and we are allowed to change the message, upgrade
* it and mark the object header as dirty */
#define H5O_DTYPE_CHECK_VERSION(DT, VERS, MIN_VERS, IOF, CLASS, ERR) \
if (((VERS) < (MIN_VERS)) && !(*(IOF)&H5O_DECODEIO_NOCHANGE)) { \
(VERS) = (MIN_VERS); \
if (H5T__upgrade_version((DT), (VERS)) < 0) \
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTSET, FAIL, "can't upgrade " CLASS " encoding version"); \
*(IOF) |= H5O_DECODEIO_DIRTY; \
} /* end if */
#endif /* H5_STRICT_FORMAT_CHECKS */
/* This message derives from H5O message class */
const H5O_msg_class_t H5O_MSG_DTYPE[1] = {{
H5O_DTYPE_ID, /* message id number */
"datatype", /* message name for debugging */
sizeof(H5T_t), /* native message size */
H5O_SHARE_IS_SHARABLE | H5O_SHARE_IN_OHDR, /* messages are shareable? */
H5O__dtype_shared_decode, /* decode message */
H5O__dtype_shared_encode, /* encode message */
H5O__dtype_copy, /* copy the native value */
H5O__dtype_shared_size, /* size of raw message */
H5O__dtype_reset, /* reset method */
H5O__dtype_free, /* free method */
H5O__dtype_shared_delete, /* file delete method */
H5O__dtype_shared_link, /* link method */
H5O__dtype_set_share, /* set share method */
H5O__dtype_can_share, /* can share method */
H5O__dtype_pre_copy_file, /* pre copy native value to file */
H5O__dtype_shared_copy_file, /* copy native value to file */
H5O__dtype_shared_post_copy_file, /* post copy native value to file */
NULL, /* get creation index */
NULL, /* set creation index */
H5O__dtype_shared_debug /* debug the message */
}};
/*-------------------------------------------------------------------------
* Function: H5O__dtype_decode_helper
*
* Purpose: Decodes a datatype
*
* Return: true if we can upgrade the parent type's version even
* with strict format checks
* false if we cannot
* NEGATIVE on failure
*-------------------------------------------------------------------------
*/
static htri_t
H5O__dtype_decode_helper(unsigned *ioflags /*in,out*/, const uint8_t **pp, H5T_t *dt, bool skip,
const uint8_t *p_end)
{
unsigned flags;
unsigned version;
htri_t ret_value = false;
FUNC_ENTER_PACKAGE
assert(pp && *pp);
assert(dt && dt->shared);
/* XXX NOTE!
*
* H5Tencode() does not take a buffer size, so normal bounds checking in
* that case is impossible.
*
* Instead of using our normal H5_IS_BUFFER_OVERFLOW macro, use
* H5_IS_KNOWN_BUFFER_OVERFLOW, which will skip the check when the
* we're decoding a buffer from H5Tconvert().
*
* Even if this is fixed at some point in the future, as long as we
* support the old, size-less API call, we will need to use the modified
* macros.
*/
/* Version, class & flags */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 4, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
UINT32DECODE(*pp, flags);
version = (flags >> 4) & 0x0f;
if (version < H5O_DTYPE_VERSION_1 || version > H5O_DTYPE_VERSION_LATEST)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTLOAD, FAIL, "bad version number for datatype message");
dt->shared->version = version;
dt->shared->type = (H5T_class_t)(flags & 0x0f);
flags >>= 8;
/* Size */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 4, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
UINT32DECODE(*pp, dt->shared->size);
/* Check for invalid datatype size */
if (dt->shared->size == 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "invalid datatype size");
switch (dt->shared->type) {
case H5T_INTEGER:
/*
* Integer types...
*/
dt->shared->u.atomic.order = (flags & 0x1) ? H5T_ORDER_BE : H5T_ORDER_LE;
dt->shared->u.atomic.lsb_pad = (flags & 0x2) ? H5T_PAD_ONE : H5T_PAD_ZERO;
dt->shared->u.atomic.msb_pad = (flags & 0x4) ? H5T_PAD_ONE : H5T_PAD_ZERO;
dt->shared->u.atomic.u.i.sign = (flags & 0x8) ? H5T_SGN_2 : H5T_SGN_NONE;
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 2 + 2, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
UINT16DECODE(*pp, dt->shared->u.atomic.offset);
UINT16DECODE(*pp, dt->shared->u.atomic.prec);
/* Sanity checks */
if (dt->shared->u.atomic.offset >= (dt->shared->size * 8))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADRANGE, FAIL, "integer offset out of bounds");
if (0 == dt->shared->u.atomic.prec)
HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "precision is zero");
if (((dt->shared->u.atomic.offset + dt->shared->u.atomic.prec) - 1) >= (dt->shared->size * 8))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADRANGE, FAIL, "integer offset+precision out of bounds");
break;
case H5T_FLOAT:
/*
* Floating-point types...
*/
dt->shared->u.atomic.order = (flags & 0x1) ? H5T_ORDER_BE : H5T_ORDER_LE;
if (version >= H5O_DTYPE_VERSION_3) {
/* Unsupported byte order*/
if ((flags & 0x40) && !(flags & 0x1))
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL, "bad byte order for datatype message");
/* VAX order if both 1st and 6th bits are turned on*/
if (flags & 0x40)
dt->shared->u.atomic.order = H5T_ORDER_VAX;
}
dt->shared->u.atomic.lsb_pad = (flags & 0x2) ? H5T_PAD_ONE : H5T_PAD_ZERO;
dt->shared->u.atomic.msb_pad = (flags & 0x4) ? H5T_PAD_ONE : H5T_PAD_ZERO;
dt->shared->u.atomic.u.f.pad = (flags & 0x8) ? H5T_PAD_ONE : H5T_PAD_ZERO;
switch ((flags >> 4) & 0x03) {
case 0:
dt->shared->u.atomic.u.f.norm = H5T_NORM_NONE;
break;
case 1:
dt->shared->u.atomic.u.f.norm = H5T_NORM_MSBSET;
break;
case 2:
dt->shared->u.atomic.u.f.norm = H5T_NORM_IMPLIED;
break;
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL, "unknown floating-point normalization");
}
dt->shared->u.atomic.u.f.sign = (flags >> 8) & 0xff;
if (dt->shared->u.atomic.u.f.sign >= (dt->shared->size * 8))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADRANGE, FAIL, "sign bit position out of bounds");
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 2 + 2, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
UINT16DECODE(*pp, dt->shared->u.atomic.offset);
UINT16DECODE(*pp, dt->shared->u.atomic.prec);
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 1 + 1, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
dt->shared->u.atomic.u.f.epos = *(*pp)++;
dt->shared->u.atomic.u.f.esize = *(*pp)++;
if (dt->shared->u.atomic.u.f.esize == 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "exponent size can't be zero");
if (dt->shared->u.atomic.u.f.epos >= (dt->shared->size * 8))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADRANGE, FAIL, "exponent starting position out of bounds");
if (((dt->shared->u.atomic.u.f.epos + dt->shared->u.atomic.u.f.esize) - 1) >=
(dt->shared->size * 8))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADRANGE, FAIL, "exponent range out of bounds");
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 1 + 1, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
dt->shared->u.atomic.u.f.mpos = *(*pp)++;
dt->shared->u.atomic.u.f.msize = *(*pp)++;
if (dt->shared->u.atomic.u.f.msize == 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "mantissa size can't be zero");
if (dt->shared->u.atomic.u.f.mpos >= (dt->shared->size * 8))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADRANGE, FAIL, "mantissa starting position out of bounds");
if (((dt->shared->u.atomic.u.f.mpos + dt->shared->u.atomic.u.f.msize) - 1) >=
(dt->shared->size * 8))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADRANGE, FAIL, "mantissa range out of bounds");
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 4, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
UINT32DECODE(*pp, dt->shared->u.atomic.u.f.ebias);
/* Sanity check bits don't overlap */
if (H5_RANGE_OVERLAP(dt->shared->u.atomic.u.f.sign, dt->shared->u.atomic.u.f.sign,
dt->shared->u.atomic.u.f.epos,
((dt->shared->u.atomic.u.f.epos + dt->shared->u.atomic.u.f.esize) - 1)))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "exponent and sign positions overlap");
if (H5_RANGE_OVERLAP(dt->shared->u.atomic.u.f.sign, dt->shared->u.atomic.u.f.sign,
dt->shared->u.atomic.u.f.mpos,
((dt->shared->u.atomic.u.f.mpos + dt->shared->u.atomic.u.f.msize) - 1)))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "mantissa and sign positions overlap");
if (H5_RANGE_OVERLAP(dt->shared->u.atomic.u.f.epos,
((dt->shared->u.atomic.u.f.epos + dt->shared->u.atomic.u.f.esize) - 1),
dt->shared->u.atomic.u.f.mpos,
((dt->shared->u.atomic.u.f.mpos + dt->shared->u.atomic.u.f.msize) - 1)))
HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "mantissa and exponent positions overlap");
break;
case H5T_TIME:
/*
* Time datatypes...
*/
dt->shared->u.atomic.order = (flags & 0x1) ? H5T_ORDER_BE : H5T_ORDER_LE;
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 2, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
UINT16DECODE(*pp, dt->shared->u.atomic.prec);
break;
case H5T_STRING:
/*
* Character string types...
*/
dt->shared->u.atomic.order = H5T_ORDER_NONE;
dt->shared->u.atomic.prec = 8 * dt->shared->size;
dt->shared->u.atomic.offset = 0;
dt->shared->u.atomic.lsb_pad = H5T_PAD_ZERO;
dt->shared->u.atomic.msb_pad = H5T_PAD_ZERO;
dt->shared->u.atomic.u.s.pad = (H5T_str_t)(flags & 0x0f);
dt->shared->u.atomic.u.s.cset = (H5T_cset_t)((flags >> 4) & 0x0f);
break;
case H5T_BITFIELD:
/*
* Bit fields...
*/
dt->shared->u.atomic.order = (flags & 0x1) ? H5T_ORDER_BE : H5T_ORDER_LE;
dt->shared->u.atomic.lsb_pad = (flags & 0x2) ? H5T_PAD_ONE : H5T_PAD_ZERO;
dt->shared->u.atomic.msb_pad = (flags & 0x4) ? H5T_PAD_ONE : H5T_PAD_ZERO;
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 2 + 2, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
UINT16DECODE(*pp, dt->shared->u.atomic.offset);
UINT16DECODE(*pp, dt->shared->u.atomic.prec);
break;
case H5T_OPAQUE: {
size_t z;
/*
* Opaque types...
*/
/* The opaque tag flag field must be aligned */
z = flags & (H5T_OPAQUE_TAG_MAX - 1);
if (0 != (z & 0x7))
HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "opaque flag field must be aligned");
if (NULL == (dt->shared->u.opaque.tag = (char *)H5MM_malloc(z + 1)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, z, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
H5MM_memcpy(dt->shared->u.opaque.tag, *pp, z);
dt->shared->u.opaque.tag[z] = '\0';
*pp += z;
break;
}
case H5T_COMPOUND: {
unsigned nmembs; /* Number of compound members */
unsigned offset_nbytes; /* Size needed to encode member offsets */
size_t max_memb_pos = 0; /* Maximum member covered, so far */
unsigned max_version = 0; /* Maximum member version */
unsigned upgrade_to = 0; /* Version number we can "soft" upgrade to */
/* Compute the # of bytes required to store a member offset */
offset_nbytes = H5VM_limit_enc_size((uint64_t)dt->shared->size);
/*
* Compound datatypes...
*/
nmembs = flags & 0xffff;
if (nmembs == 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "invalid number of members: %u", nmembs);
if (NULL ==
(dt->shared->u.compnd.memb = (H5T_cmemb_t *)H5MM_calloc(nmembs * sizeof(H5T_cmemb_t))))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTALLOC, FAIL, "memory allocation failed");
dt->shared->u.compnd.nalloc = nmembs;
if (dt->shared->u.compnd.memb_size != 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_BADVALUE, FAIL, "member size not initialized to zero");
for (dt->shared->u.compnd.nmembs = 0; dt->shared->u.compnd.nmembs < nmembs;
dt->shared->u.compnd.nmembs++) {
size_t actual_name_length = 0; /* Actual length of name */
unsigned ndims = 0; /* Number of dimensions of the array field */
htri_t can_upgrade; /* Whether we can upgrade this type's version */
hsize_t dim[H5O_LAYOUT_NDIMS]; /* Dimensions of the array */
H5T_t *array_dt; /* Temporary pointer to the array datatype */
H5T_t *temp_type; /* Temporary pointer to the field's datatype */
/* Get the length of the field name */
if (!skip) {
/* There is a realistic buffer end, so check bounds */
size_t max = (size_t)(p_end - *pp + 1); /* Max possible name length */
actual_name_length = strnlen((const char *)*pp, max);
if (actual_name_length == max)
HGOTO_ERROR(H5E_OHDR, H5E_NOSPACE, FAIL, "field name not null terminated");
}
else {
/* The buffer end can't be determined when it's an unbounded buffer
* passed via H5Tdecode(), so don't bounds check and hope for
* the best.
*/
actual_name_length = strlen((const char *)*pp);
}
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, actual_name_length, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
/* Decode the field name */
if (NULL == (dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name =
H5MM_xstrdup((const char *)*pp)))
HGOTO_ERROR(H5E_RESOURCE, H5E_CANTCOPY, FAIL,
"can't duplicate compound member name string");
/* Version 3 of the datatype message eliminated the padding to multiple of 8 bytes */
if (version >= H5O_DTYPE_VERSION_3) {
/* Advance past name, including null terminator */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, actual_name_length + 1, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
*pp += actual_name_length + 1;
}
else {
/* Advance multiple of 8 w/ null terminator */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, ((actual_name_length + 8) / 8) * 8, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
*pp += ((actual_name_length + 8) / 8) * 8;
}
/* Decode the field offset */
/* (starting with version 3 of the datatype message, use the minimum # of bytes required) */
if (version >= H5O_DTYPE_VERSION_3) {
H5_GCC_CLANG_DIAG_OFF("type-limits")
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, offset_nbytes, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
H5_GCC_CLANG_DIAG_ON("type-limits")
UINT32DECODE_VAR(*pp, dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].offset,
offset_nbytes);
}
else {
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 4, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
UINT32DECODE(*pp, dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].offset);
}
/* Older versions of the library allowed a field to have
* intrinsic 'arrayness'. Newer versions of the library
* use the separate array datatypes. */
if (version == H5O_DTYPE_VERSION_1) {
/* Decode the number of dimensions */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 1, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
ndims = *(*pp)++;
/* Check that ndims is valid */
if (ndims > 4) {
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name =
H5MM_xfree(dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name);
HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL,
"invalid number of dimensions for array");
}
/* Skip reserved bytes */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 3, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
*pp += 3;
/* Skip dimension permutation */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 4, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
*pp += 4;
/* Skip reserved bytes */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 4, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
*pp += 4;
/* Decode array dimension sizes */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, (4 * 4), p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
for (int i = 0; i < 4; i++)
UINT32DECODE(*pp, dim[i]);
}
/* Allocate space for the field's datatype */
if (NULL == (temp_type = H5T__alloc())) {
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name =
H5MM_xfree(dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name);
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
}
/* Decode the field's datatype information */
if ((can_upgrade = H5O__dtype_decode_helper(ioflags, pp, temp_type, skip, p_end)) < 0) {
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name =
H5MM_xfree(dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name);
if (H5T_close_real(temp_type) < 0)
HDONE_ERROR(H5E_DATATYPE, H5E_CANTRELEASE, FAIL, "can't release datatype info");
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL, "unable to decode member type");
}
if (temp_type->shared->size == 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL, "type size can't be zero");
if ((dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].offset +
temp_type->shared->size) > dt->shared->size) {
if (H5T_close_real(temp_type) < 0)
HDONE_ERROR(H5E_DATATYPE, H5E_CANTRELEASE, FAIL, "can't release datatype info");
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL,
"member type extends outside its parent compound type");
}
/* Upgrade the version if we can and it is necessary */
if (can_upgrade && temp_type->shared->version > version) {
upgrade_to = temp_type->shared->version;
/* Pass "can_upgrade" flag down to parent type */
ret_value = true;
}
/* Go create the array datatype now, for older versions of the datatype message */
if (version == H5O_DTYPE_VERSION_1) {
/* Check if this member is an array field */
if (ndims > 0) {
/* Create the array datatype for the field */
if ((array_dt = H5T__array_create(temp_type, ndims, dim)) == NULL) {
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name =
H5MM_xfree(dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name);
if (H5T_close_real(temp_type) < 0)
HDONE_ERROR(H5E_DATATYPE, H5E_CANTRELEASE, FAIL,
"can't release datatype info");
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, FAIL,
"unable to create array datatype");
}
/* Close the base type for the array */
if (H5T_close_real(temp_type) < 0) {
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name =
H5MM_xfree(dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].name);
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTRELEASE, FAIL, "can't release datatype info");
}
/* Make the array type the type that is set for the field */
temp_type = array_dt;
/* Reset array version if NOCHANGE is specified (i.e. h5debug) */
if (*ioflags & H5O_DECODEIO_NOCHANGE)
temp_type->shared->version = H5O_DTYPE_VERSION_1;
else {
/* Otherwise upgrade the compound version */
if (upgrade_to < temp_type->shared->version)
upgrade_to = temp_type->shared->version;
/* Set the return value to indicate that we should freely
* upgrade parent types */
ret_value = true;
}
}
}
/* Keep track of the maximum member version found */
if (temp_type->shared->version > max_version)
max_version = temp_type->shared->version;
/* Set the "force conversion" flag if VL datatype fields exist in this
* type or any component types
*/
if (temp_type->shared->force_conv == true)
dt->shared->force_conv = true;
/* Member size */
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].size = temp_type->shared->size;
dt->shared->u.compnd.memb_size += temp_type->shared->size;
/* Set the field datatype (finally :-) */
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].type = temp_type;
/* Check if this field overlaps with a prior field
* (probably indicates that the file is corrupt)
*/
if (dt->shared->u.compnd.nmembs > 0 &&
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].offset < max_memb_pos) {
for (unsigned u = 0; u < dt->shared->u.compnd.nmembs; u++)
if ((dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].offset >=
dt->shared->u.compnd.memb[u].offset &&
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].offset <
(dt->shared->u.compnd.memb[u].offset + dt->shared->u.compnd.memb[u].size)) ||
(dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].offset <
dt->shared->u.compnd.memb[u].offset &&
(dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].offset +
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].size) >
dt->shared->u.compnd.memb[u].offset))
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL,
"member overlaps with previous member");
}
/* Update the maximum member position covered */
max_memb_pos =
MAX(max_memb_pos, (dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].offset +
dt->shared->u.compnd.memb[dt->shared->u.compnd.nmembs].size));
}
/* Check if the compound type is packed */
H5T__update_packed(dt);
/* Upgrade the compound if requested */
if (version < upgrade_to) {
version = upgrade_to;
if (H5T__upgrade_version(dt, upgrade_to) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTSET, FAIL, "can't upgrade compound encoding version");
/* We won't mark the message dirty since there were no
* errors in the file, simply type versions that we will no
* longer encode. */
}
/* Check that no member of this compound has a version greater
* than the compound itself. */
H5O_DTYPE_CHECK_VERSION(dt, version, max_version, ioflags, "compound", FAIL)
} break;
case H5T_REFERENCE:
/*
* Reference datatypes...
*/
dt->shared->u.atomic.order = H5T_ORDER_NONE;
dt->shared->u.atomic.prec = 8 * dt->shared->size;
dt->shared->u.atomic.offset = 0;
dt->shared->u.atomic.lsb_pad = H5T_PAD_ZERO;
dt->shared->u.atomic.msb_pad = H5T_PAD_ZERO;
/* Set reference type */
dt->shared->u.atomic.u.r.rtype = (H5R_type_t)(flags & 0x0f);
if (dt->shared->u.atomic.u.r.rtype <= H5R_BADTYPE ||
dt->shared->u.atomic.u.r.rtype >= H5R_MAXTYPE)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL, "invalid reference type");
/* Set generic flag */
if (dt->shared->u.atomic.u.r.rtype == H5R_OBJECT2 ||
dt->shared->u.atomic.u.r.rtype == H5R_DATASET_REGION2 ||
dt->shared->u.atomic.u.r.rtype == H5R_ATTR) {
dt->shared->u.atomic.u.r.opaque = true;
dt->shared->u.atomic.u.r.version = (unsigned)((flags >> 4) & 0x0f);
if (dt->shared->u.atomic.u.r.version != H5R_ENCODE_VERSION)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL, "reference version does not match");
}
else
dt->shared->u.atomic.u.r.opaque = false;
/* This type needs conversion */
dt->shared->force_conv = true;
/* Mark location of this type as undefined for now. The caller
* function should decide the location. */
if (H5T_set_loc(dt, NULL, H5T_LOC_BADLOC) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "invalid datatype location");
break;
case H5T_ENUM: {
unsigned nmembs;
/*
* Enumeration datatypes...
*/
nmembs = flags & 0xffff;
if (NULL == (dt->shared->parent = H5T__alloc()))
HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "can't allocate parent datatype");
if (H5O__dtype_decode_helper(ioflags, pp, dt->shared->parent, skip, p_end) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL, "unable to decode parent datatype");
if (dt->shared->parent->shared->size != dt->shared->size)
HGOTO_ERROR(H5E_DATATYPE, H5E_BADSIZE, FAIL, "ENUM datatype size does not match parent");
/* Check if the parent of this enum has a version greater than the
* enum itself. */
H5O_DTYPE_CHECK_VERSION(dt, version, dt->shared->parent->shared->version, ioflags, "enum", FAIL)
/* Allocate name and value arrays */
if (NULL == (dt->shared->u.enumer.name = (char **)H5MM_calloc(nmembs * sizeof(char *))) ||
NULL == (dt->shared->u.enumer.value =
(uint8_t *)H5MM_calloc(nmembs * dt->shared->parent->shared->size)))
HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "memory allocation failed");
dt->shared->u.enumer.nalloc = nmembs;
/* Names */
for (dt->shared->u.enumer.nmembs = 0; dt->shared->u.enumer.nmembs < nmembs;
dt->shared->u.enumer.nmembs++) {
size_t actual_name_length = 0; /* Actual length of name */
/* Get the length of the enum name */
if (!skip) {
/* There is a realistic buffer end, so check bounds */
size_t max = (size_t)(p_end - *pp + 1); /* Max possible name length */
actual_name_length = strnlen((const char *)*pp, max);
if (actual_name_length == max)
HGOTO_ERROR(H5E_OHDR, H5E_NOSPACE, FAIL, "enum name not null terminated");
}
else {
/* The buffer end can't be determined when it's an unbounded buffer
* passed via H5Tdecode(), so don't bounds check and hope for
* the best.
*/
actual_name_length = strlen((const char *)*pp);
}
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, actual_name_length, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
if (NULL == (dt->shared->u.enumer.name[dt->shared->u.enumer.nmembs] =
H5MM_xstrdup((const char *)*pp)))
HGOTO_ERROR(H5E_RESOURCE, H5E_CANTCOPY, FAIL, "can't duplicate enum name string");
/* Version 3 of the datatype message eliminated the padding to multiple of 8 bytes */
if (version >= H5O_DTYPE_VERSION_3) {
/* Advance past name, including null terminator */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, actual_name_length + 1, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
*pp += actual_name_length + 1;
}
else {
/* Advance multiple of 8 w/ null terminator */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, ((actual_name_length + 8) / 8) * 8, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
*pp += ((actual_name_length + 8) / 8) * 8;
}
}
if (dt->shared->u.enumer.nmembs != nmembs)
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "incorrect number of enum members decoded");
/* Values */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, nmembs * dt->shared->parent->shared->size, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
H5MM_memcpy(dt->shared->u.enumer.value, *pp, nmembs * dt->shared->parent->shared->size);
*pp += nmembs * dt->shared->parent->shared->size;
} break;
case H5T_VLEN:
/*
* Variable length datatypes...
*/
/* Set the type of VL information, either sequence or string */
dt->shared->u.vlen.type = (H5T_vlen_type_t)(flags & 0x0f);
if (dt->shared->u.vlen.type == H5T_VLEN_STRING) {
dt->shared->u.vlen.pad = (H5T_str_t)((flags >> 4) & 0x0f);
dt->shared->u.vlen.cset = (H5T_cset_t)((flags >> 8) & 0x0f);
}
/* Decode base type of VL information */
if (NULL == (dt->shared->parent = H5T__alloc()))
HGOTO_ERROR(H5E_DATATYPE, H5E_NOSPACE, FAIL, "memory allocation failed");
if (H5O__dtype_decode_helper(ioflags, pp, dt->shared->parent, skip, p_end) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL, "unable to decode VL parent type");
/* Check if the parent of this vlen has a version greater than the
* vlen itself. */
H5O_DTYPE_CHECK_VERSION(dt, version, dt->shared->parent->shared->version, ioflags, "vlen", FAIL)
dt->shared->force_conv = true;
/* Mark location this type as undefined for now. The caller function should
* decide the location. */
if (H5T_set_loc(dt, NULL, H5T_LOC_BADLOC) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "invalid datatype location");
break;
case H5T_ARRAY:
/*
* Array datatypes...
*/
/* Decode the number of dimensions */
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 1, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
dt->shared->u.array.ndims = *(*pp)++;
/* Double-check the number of dimensions */
if (dt->shared->u.array.ndims > H5S_MAX_RANK)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTLOAD, FAIL, "too many dimensions for array datatype");
/* Skip reserved bytes, if version has them */
if (version < H5O_DTYPE_VERSION_3) {
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, 3, p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
*pp += 3;
}
/* Decode array dimension sizes & compute number of elements */
dt->shared->u.array.nelem = 1;
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, (dt->shared->u.array.ndims * 4), p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
for (unsigned u = 0; u < dt->shared->u.array.ndims; u++) {
UINT32DECODE(*pp, dt->shared->u.array.dim[u]);
dt->shared->u.array.nelem *= dt->shared->u.array.dim[u];
}
/* Skip array dimension permutations, if version has them */
if (version < H5O_DTYPE_VERSION_3) {
if (H5_IS_KNOWN_BUFFER_OVERFLOW(skip, *pp, (dt->shared->u.array.ndims * 4), p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
*pp += dt->shared->u.array.ndims * 4;
}
/* Decode base type of array */
if (NULL == (dt->shared->parent = H5T__alloc()))
HGOTO_ERROR(H5E_DATATYPE, H5E_NOSPACE, FAIL, "memory allocation failed");
if (H5O__dtype_decode_helper(ioflags, pp, dt->shared->parent, skip, p_end) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL, "unable to decode array parent type");
/* Check if the parent of this array has a version greater than the
* array itself. */
H5O_DTYPE_CHECK_VERSION(dt, version, dt->shared->parent->shared->version, ioflags, "array", FAIL)
/* There should be no array datatypes with version < 2. */
H5O_DTYPE_CHECK_VERSION(dt, version, H5O_DTYPE_VERSION_2, ioflags, "array", FAIL)
/* Set the "force conversion" flag if a VL base datatype is used or
* or if any components of the base datatype are VL types.
*/
if (dt->shared->parent->shared->force_conv == true)
dt->shared->force_conv = true;
break;
case H5T_NO_CLASS:
case H5T_NCLASSES:
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL, "unknown datatype class found");
}
/* Check for numeric type w/unusual # of unused bits */
if (H5T_is_numeric_with_unusual_unused_bits(dt))
/* Throw an error if the object header is not checksummed, unless the
* H5F_RFIC_UNUSUAL_NUM_UNUSED_NUMERIC_BITS flag is set with
* H5Pset_relax_file_integrity_checks() to suppress it.
*/
if (!(*ioflags & H5O_DECODEIO_RFIC_UNUBNT))
HGOTO_ERROR(
H5E_DATATYPE, H5E_BADVALUE, FAIL,
"datatype has unusually large # of unused bits (prec = %zu bits, size = %zu bytes), possibly "
"corrupted file. See documentation for H5Pset_relax_file_integrity_checks for details.",
dt->shared->u.atomic.prec, dt->shared->size);
done:
/* Cleanup on error */
if (ret_value < 0)
/* Release (reset) dt but do not free it - leave it as an empty datatype as was the case on
* function entry */
if (H5T__free(dt) < 0)
HDONE_ERROR(H5E_DATATYPE, H5E_CANTRELEASE, FAIL, "can't release datatype info");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O__dtype_decode_helper() */
/*-------------------------------------------------------------------------
* Function: H5O__dtype_encode_helper
*
* Purpose: Encodes a datatype.
*
* Note: When changing the format of a datatype (or adding a new one),
* remember to change the upgrade version callback
* (H5T_upgrade_version_cb).
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5O__dtype_encode_helper(uint8_t **pp, const H5T_t *dt)
{
unsigned flags = 0;
uint8_t *hdr = (uint8_t *)*pp;
unsigned i;
size_t n, z;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* check args */
assert(pp && *pp);
assert(dt);
/* skip the type and class bit-field for now */
*pp += 4;
UINT32ENCODE(*pp, dt->shared->size);
switch (dt->shared->type) {
case H5T_INTEGER:
/*
* Integer datatypes...
*/
switch (dt->shared->u.atomic.order) {
case H5T_ORDER_LE:
break; /*nothing */
case H5T_ORDER_BE:
flags |= 0x01;
break;
case H5T_ORDER_ERROR:
case H5T_ORDER_VAX:
case H5T_ORDER_MIXED:
case H5T_ORDER_NONE:
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL,
"byte order is not supported in file format yet");
} /* end switch */
switch (dt->shared->u.atomic.lsb_pad) {
case H5T_PAD_ZERO:
break; /*nothing */
case H5T_PAD_ONE:
flags |= 0x02;
break;
case H5T_PAD_ERROR:
case H5T_PAD_BACKGROUND:
case H5T_NPAD:
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL,
"bit padding is not supported in file format yet");
} /* end switch */
switch (dt->shared->u.atomic.msb_pad) {
case H5T_PAD_ZERO:
break; /*nothing */
case H5T_PAD_ERROR:
case H5T_PAD_BACKGROUND:
case H5T_NPAD:
case H5T_PAD_ONE:
flags |= 0x04;
break;
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL,
"bit padding is not supported in file format yet");
} /* end switch */
switch (dt->shared->u.atomic.u.i.sign) {
case H5T_SGN_NONE:
break; /*nothing */
case H5T_SGN_2:
flags |= 0x08;
break;
case H5T_SGN_ERROR:
case H5T_NSGN:
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL,
"sign scheme is not supported in file format yet");
} /* end switch */
UINT16ENCODE(*pp, dt->shared->u.atomic.offset);
UINT16ENCODE(*pp, dt->shared->u.atomic.prec);
break;
case H5T_FLOAT:
/*
* Floating-point types...
*/
switch (dt->shared->u.atomic.order) {
case H5T_ORDER_LE:
break; /*nothing*/
case H5T_ORDER_BE:
flags |= 0x01;
break;
case H5T_ORDER_VAX: /*turn on 1st and 6th (reserved before adding VAX) bits*/
flags |= 0x41;
assert(dt->shared->version >= H5O_DTYPE_VERSION_3);
break;
case H5T_ORDER_MIXED:
case H5T_ORDER_ERROR:
case H5T_ORDER_NONE:
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL,
"byte order is not supported in file format yet");
} /* end switch */
switch (dt->shared->u.atomic.lsb_pad) {
case H5T_PAD_ZERO:
break; /*nothing */
case H5T_PAD_ONE:
flags |= 0x02;
break;
case H5T_PAD_ERROR:
case H5T_PAD_BACKGROUND:
case H5T_NPAD:
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL,
"bit padding is not supported in file format yet");
} /* end switch */
switch (dt->shared->u.atomic.msb_pad) {
case H5T_PAD_ZERO:
break; /*nothing */
case H5T_PAD_ONE:
flags |= 0x04;
break;
case H5T_PAD_ERROR:
case H5T_PAD_BACKGROUND:
case H5T_NPAD:
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL,
"bit padding is not supported in file format yet");