-
-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathH5Znbit.c
More file actions
1628 lines (1376 loc) · 66.2 KB
/
Copy pathH5Znbit.c
File metadata and controls
1628 lines (1376 loc) · 66.2 KB
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 LICENSE 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 *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "H5Zmodule.h" /* This source code file is part of the H5Z module */
#include "H5private.h" /* Generic Functions */
#include "H5Eprivate.h" /* Error handling */
#include "H5Iprivate.h" /* IDs */
#include "H5MMprivate.h" /* Memory management */
#include "H5Ppublic.h" /* Property lists */
#include "H5Sprivate.h" /* Dataspaces */
#include "H5Tprivate.h" /* Datatypes */
#include "H5Zpkg.h" /* Data filters */
/* Struct of parameters needed for compressing/decompressing
* one nbit atomic datatype: integer or floating-point
*/
typedef struct {
unsigned size; /* size of datatype */
unsigned order; /* datatype endianness order */
unsigned precision; /* datatype precision */
unsigned offset; /* datatype offset */
} parms_atomic;
/* Local function prototypes */
static htri_t H5Z__can_apply_nbit(hid_t dcpl_id, hid_t type_id, hid_t space_id);
static herr_t H5Z__set_local_nbit(hid_t dcpl_id, hid_t type_id, hid_t space_id);
static size_t H5Z__filter_nbit(unsigned flags, size_t cd_nelmts, const unsigned cd_values[], size_t nbytes,
size_t *buf_size, void **buf);
static void H5Z__calc_parms_nooptype(size_t *cd_values_actual_nparms);
static void H5Z__calc_parms_atomic(size_t *cd_values_actual_nparms);
static herr_t H5Z__calc_parms_array(const H5T_t *type, size_t *cd_values_actual_nparms);
static herr_t H5Z__calc_parms_compound(const H5T_t *type, size_t *cd_values_actual_nparms);
static herr_t H5Z__set_parms_nooptype(const H5T_t *type, unsigned *cd_values_index, unsigned cd_values[]);
static herr_t H5Z__set_parms_atomic(const H5T_t *type, unsigned *cd_values_index, unsigned cd_values[],
bool *need_not_compress);
static herr_t H5Z__set_parms_array(const H5T_t *type, unsigned *cd_values_index, unsigned cd_values[],
bool *need_not_compress);
static herr_t H5Z__set_parms_compound(const H5T_t *type, unsigned *cd_values_index, unsigned cd_values[],
bool *need_not_compress);
static void H5Z__nbit_next_byte(size_t *j, size_t *buf_len);
static herr_t H5Z__nbit_decompress_one_byte(unsigned char *data, size_t data_offset, unsigned k,
unsigned begin_i, unsigned end_i, const unsigned char *buffer,
size_t buffer_size, size_t *j, size_t *buf_len,
const parms_atomic *p, size_t datatype_len);
static void H5Z__nbit_compress_one_byte(const unsigned char *data, size_t data_offset, unsigned k,
unsigned begin_i, unsigned end_i, unsigned char *buffer, size_t *j,
size_t *buf_len, const parms_atomic *p, size_t datatype_len);
static herr_t H5Z__nbit_decompress_one_nooptype(unsigned char *data, size_t data_offset,
const unsigned char *buffer, size_t buffer_size, size_t *j,
size_t *buf_len, unsigned size);
static herr_t H5Z__nbit_decompress_one_atomic(unsigned char *data, size_t data_offset, unsigned char *buffer,
size_t buffer_size, size_t *j, size_t *buf_len,
const parms_atomic *p);
static herr_t H5Z__nbit_decompress_one_array(unsigned char *data, size_t data_offset, unsigned char *buffer,
size_t buffer_size, size_t *j, size_t *buf_len,
const unsigned parms[], unsigned *parms_index);
static herr_t H5Z__nbit_decompress_one_compound(unsigned char *data, size_t data_offset,
unsigned char *buffer, size_t buffer_size, size_t *j,
size_t *buf_len, const unsigned parms[],
unsigned *parms_index);
static herr_t H5Z__nbit_decompress(unsigned char *data, unsigned d_nelmts, unsigned char *buffer,
size_t buffer_size, const unsigned parms[]);
static void H5Z__nbit_compress_one_nooptype(const unsigned char *data, size_t data_offset,
unsigned char *buffer, size_t *j, size_t *buf_len, unsigned size);
static void H5Z__nbit_compress_one_array(unsigned char *data, size_t data_offset, unsigned char *buffer,
size_t *j, size_t *buf_len, const unsigned parms[],
unsigned *parms_index);
static void H5Z__nbit_compress_one_compound(unsigned char *data, size_t data_offset, unsigned char *buffer,
size_t *j, size_t *buf_len, const unsigned parms[],
unsigned *parms_index);
static void H5Z__nbit_compress(unsigned char *data, unsigned d_nelmts, unsigned char *buffer,
size_t *buffer_size, const unsigned parms[]);
/* This message derives from H5Z */
H5Z_class2_t H5Z_NBIT[1] = {{
H5Z_CLASS_T_VERS, /* H5Z_class_t version */
H5Z_FILTER_NBIT, /* Filter id number */
1, /* Assume encoder present: check before registering */
1, /* decoder_present flag (set to true) */
"nbit", /* Filter name for debugging */
H5Z__can_apply_nbit, /* The "can apply" callback */
H5Z__set_local_nbit, /* The "set local" callback */
H5Z__filter_nbit, /* The actual filter function */
}};
/* Local macros */
#define H5Z_NBIT_ATOMIC 1 /* Atomic datatype class: integer/floating-point */
#define H5Z_NBIT_ARRAY 2 /* Array datatype class */
#define H5Z_NBIT_COMPOUND 3 /* Compound datatype class */
#define H5Z_NBIT_NOOPTYPE 4 /* Other datatype class: nbit does no compression */
#define H5Z_NBIT_MAX_NPARMS 4096 /* Max number of parameters for filter */
#define H5Z_NBIT_ORDER_LE 0 /* Little endian for datatype byte order */
#define H5Z_NBIT_ORDER_BE 1 /* Big endian for datatype byte order */
/* Local variables */
/*-------------------------------------------------------------------------
* Function: H5Z__can_apply_nbit
*
* Purpose: Check the parameters for nbit compression for validity and
* whether they fit a particular dataset.
*
* Return: Success: Non-negative
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
static htri_t
H5Z__can_apply_nbit(hid_t H5_ATTR_UNUSED dcpl_id, hid_t type_id, hid_t H5_ATTR_UNUSED space_id)
{
const H5T_t *type; /* Datatype */
htri_t ret_value = true; /* Return value */
FUNC_ENTER_PACKAGE
/* Get datatype */
if (NULL == (type = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE)))
HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype");
/* Get datatype's class, for checking the "datatype class" */
if (H5T_get_class(type, true) == H5T_NO_CLASS)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype class");
/* Get datatype's size, for checking the "datatype size" */
if (H5T_get_size(type) == 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype size");
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z__can_apply_nbit() */
/*-------------------------------------------------------------------------
* Function: H5Z__calc_parms_nooptype
*
* Purpose: Calculate the number of parameters of array cd_values[]
* of datatype that is not integer, nor floating-point, nor
* compound, and nor array.
*
*-------------------------------------------------------------------------
*/
static void
H5Z__calc_parms_nooptype(size_t *cd_values_actual_nparms)
{
/* Store datatype class code */
*cd_values_actual_nparms += 1;
/* Store datatype size */
*cd_values_actual_nparms += 1;
}
/*-------------------------------------------------------------------------
* Function: H5Z__calc_parms_atomic
*
* Purpose: Calculate the number of parameters of array cd_values[]
* of atomic datatype whose datatype class is integer
* or floating point
*
*-------------------------------------------------------------------------
*/
static void
H5Z__calc_parms_atomic(size_t *cd_values_actual_nparms)
{
/* Store datatype class code */
*cd_values_actual_nparms += 1;
/* Store datatype size */
*cd_values_actual_nparms += 1;
/* Store datatype endianness */
*cd_values_actual_nparms += 1;
/* Store datatype's precision */
*cd_values_actual_nparms += 1;
/* Store datatype's offset */
*cd_values_actual_nparms += 1;
}
/*-------------------------------------------------------------------------
* Function: H5Z__calc_parms_array
*
* Purpose: Calculate the number of parameters of array cd_values[]
* for a given datatype identifier type_id
* if its datatype class is array datatype
*
* Return: Success: Non-negative
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
static herr_t
H5Z__calc_parms_array(const H5T_t *type, size_t *cd_values_actual_nparms)
{
H5T_t *dtype_base = NULL; /* Array datatype's base datatype */
H5T_class_t dtype_base_class; /* Array datatype's base datatype's class */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Store datatype class code */
*cd_values_actual_nparms += 1;
/* Store array datatype's size */
*cd_values_actual_nparms += 1;
/* Get array datatype's base datatype */
if (NULL == (dtype_base = H5T_get_super(type)))
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad base datatype");
/* Get base datatype's class */
if ((dtype_base_class = H5T_get_class(dtype_base, true)) == H5T_NO_CLASS)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad base datatype class");
/* Calculate number of the rest parameters according to base datatype's class */
switch (dtype_base_class) {
case H5T_INTEGER:
case H5T_FLOAT:
H5Z__calc_parms_atomic(cd_values_actual_nparms);
break;
case H5T_ARRAY:
if (H5Z__calc_parms_array(dtype_base, cd_values_actual_nparms) == FAIL)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot compute parameters for datatype");
break;
case H5T_COMPOUND:
if (H5Z__calc_parms_compound(dtype_base, cd_values_actual_nparms) == FAIL)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot compute parameters for datatype");
break;
case H5T_TIME:
case H5T_STRING:
case H5T_BITFIELD:
case H5T_OPAQUE:
case H5T_REFERENCE:
case H5T_ENUM:
case H5T_VLEN:
case H5T_COMPLEX:
/* Other datatype classes: nbit does no compression */
H5Z__calc_parms_nooptype(cd_values_actual_nparms);
break;
case H5T_NO_CLASS:
case H5T_NCLASSES:
default:
/* Badness */
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit received bad datatype");
break;
} /* end switch */
done:
if (dtype_base)
if (H5T_close_real(dtype_base) < 0)
HDONE_ERROR(H5E_PLINE, H5E_CLOSEERROR, FAIL, "Unable to close base datatype");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z__calc_parms_array() */
/*-------------------------------------------------------------------------
* Function: H5Z__calc_parms_compound
*
* Purpose: Calculate the number of parameters of array cd_values[]
* for a given datatype identifier type_id
* if its datatype class is compound datatype
*
* Return: Success: Non-negative
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
static herr_t
H5Z__calc_parms_compound(const H5T_t *type, size_t *cd_values_actual_nparms)
{
int nmembers; /* Compound datatype's number of members */
H5T_t *dtype_member = NULL; /* Compound datatype's member datatype */
unsigned u; /* Local index variable */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Store compound datatype class code */
*cd_values_actual_nparms += 1;
/* Store compound datatype's size */
*cd_values_actual_nparms += 1;
/* Get number of members */
if ((nmembers = H5T_get_nmembers(type)) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype number of members");
/* Store number of members */
*cd_values_actual_nparms += 1;
/* For each member, calculate parameters */
for (u = 0; u < (unsigned)nmembers; u++) {
H5T_class_t dtype_member_class; /* Compound datatype's member datatype's class */
/* Get member datatype */
if (NULL == (dtype_member = H5T_get_member_type(type, u)))
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad member datatype");
/* Get member datatype's class */
if ((dtype_member_class = H5T_get_class(dtype_member, true)) == H5T_NO_CLASS)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad member datatype class");
/* Store member offset */
*cd_values_actual_nparms += 1;
/* Calculate parameters according to member's datatype class */
switch (dtype_member_class) {
case H5T_INTEGER:
case H5T_FLOAT:
H5Z__calc_parms_atomic(cd_values_actual_nparms);
break;
case H5T_ARRAY:
if (H5Z__calc_parms_array(dtype_member, cd_values_actual_nparms) == FAIL)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot compute parameters for datatype");
break;
case H5T_COMPOUND:
if (H5Z__calc_parms_compound(dtype_member, cd_values_actual_nparms) == FAIL)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot compute parameters for datatype");
break;
case H5T_TIME:
case H5T_STRING:
case H5T_BITFIELD:
case H5T_OPAQUE:
case H5T_REFERENCE:
case H5T_ENUM:
case H5T_VLEN:
case H5T_COMPLEX:
/* Other datatype classes: nbit does no compression */
H5Z__calc_parms_nooptype(cd_values_actual_nparms);
break;
case H5T_NO_CLASS:
case H5T_NCLASSES:
default:
/* Badness */
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit received bad datatype");
break;
} /* end switch */
/* Close member datatype */
if (H5T_close_real(dtype_member) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_CLOSEERROR, FAIL, "Unable to close member datatype");
dtype_member = NULL;
} /* end for */
done:
if (dtype_member)
if (H5T_close_real(dtype_member) < 0)
HDONE_ERROR(H5E_PLINE, H5E_CLOSEERROR, FAIL, "Unable to close member datatype");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z_calc_params_compound */
/*-------------------------------------------------------------------------
* Function: H5Z__set_parms_nooptype
*
* Purpose: Set the array cd_values[] for a given datatype identifier
* type_id if its datatype class is not integer, nor
* floating-point, nor array, nor compound, nor VL datatype,
* and nor VL string
*
* Return: Success: Non-negative
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
static herr_t
H5Z__set_parms_nooptype(const H5T_t *type, unsigned *cd_values_index, unsigned cd_values[])
{
size_t dtype_size; /* No-op datatype's size (in bytes) */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Set datatype class code */
cd_values[(*cd_values_index)++] = H5Z_NBIT_NOOPTYPE;
/* Get datatype's size */
if ((dtype_size = H5T_get_size(type)) == 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype size");
/* Set "local" parameter for datatype size */
H5_CHECK_OVERFLOW(dtype_size, size_t, unsigned);
cd_values[(*cd_values_index)++] = (unsigned)dtype_size;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z__set_parms_nooptype() */
/*-------------------------------------------------------------------------
* Function: H5Z__set_parms_atomic
*
* Purpose: Set the array cd_values[] for a given datatype identifier
* type_id if its datatype class is integer or floating point
*
* Return: Success: Non-negative
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
static herr_t
H5Z__set_parms_atomic(const H5T_t *type, unsigned *cd_values_index, unsigned cd_values[],
bool *need_not_compress)
{
H5T_order_t dtype_order; /* Atomic datatype's endianness order */
size_t dtype_size; /* Atomic datatype's size (in bytes) */
size_t dtype_precision; /* Atomic datatype's precision (in bits) */
int sdtype_offset; /* Atomic datatype's offset (in bits) */
unsigned dtype_offset; /* Atomic datatype's offset (in bits) */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Set datatype class code */
cd_values[(*cd_values_index)++] = H5Z_NBIT_ATOMIC;
/* Get datatype's size */
if ((dtype_size = H5T_get_size(type)) == 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype size");
/* Set "local" parameter for datatype size */
H5_CHECK_OVERFLOW(dtype_size, size_t, unsigned);
cd_values[(*cd_values_index)++] = (unsigned)dtype_size;
/* Get datatype's endianness order */
if ((dtype_order = H5T_get_order(type)) == H5T_ORDER_ERROR)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype endianness order");
/* Set "local" parameter for datatype endianness */
switch (dtype_order) {
case H5T_ORDER_LE: /* Little-endian byte order */
cd_values[(*cd_values_index)++] = H5Z_NBIT_ORDER_LE;
break;
case H5T_ORDER_BE: /* Big-endian byte order */
cd_values[(*cd_values_index)++] = H5Z_NBIT_ORDER_BE;
break;
case H5T_ORDER_VAX:
case H5T_ORDER_MIXED:
case H5T_ORDER_ERROR:
case H5T_ORDER_NONE:
default:
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype endianness order");
} /* end switch */
/* Get datatype's precision */
if ((dtype_precision = H5T_get_precision(type)) == 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype precision");
/* Get datatype's offset */
if ((sdtype_offset = H5T_get_offset(type)) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype offset");
dtype_offset = (unsigned)sdtype_offset;
/* Check values of precision and offset */
if (dtype_precision > dtype_size * 8 || (dtype_precision + dtype_offset) > dtype_size * 8)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "invalid datatype precision/offset");
/* Set "local" parameter for datatype precision */
H5_CHECK_OVERFLOW(dtype_precision, size_t, unsigned);
cd_values[(*cd_values_index)++] = (unsigned)dtype_precision;
/* Set "local" parameter for datatype offset */
cd_values[(*cd_values_index)++] = dtype_offset;
/* If before this point, there is no need to compress, check the need to
* compress at this point. If current datatype is not full-precision,
* flag need_not_compress should be set to false.
*/
if (*need_not_compress) /* so far no need to compress */
if (dtype_offset != 0 || dtype_precision != dtype_size * 8)
*need_not_compress = false;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z__set_parms_atomic() */
/*-------------------------------------------------------------------------
* Function: H5Z__set_parms_array
*
* Purpose: Set the array cd_values[] for a given datatype identifier
* type_id if its datatype class is array datatype
*
* Return: Success: Non-negative
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
static herr_t
H5Z__set_parms_array(const H5T_t *type, unsigned *cd_values_index, unsigned cd_values[],
bool *need_not_compress)
{
H5T_t *dtype_base = NULL; /* Array datatype's base datatype */
H5T_class_t dtype_base_class; /* Array datatype's base datatype's class */
size_t dtype_size; /* Array datatype's size (in bytes) */
htri_t is_vlstring; /* flag indicating if datatype is variable-length string */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Set datatype class code */
cd_values[(*cd_values_index)++] = H5Z_NBIT_ARRAY;
/* Get array datatype's size */
if ((dtype_size = H5T_get_size(type)) == 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype size");
/* Set "local" parameter for array datatype's size */
H5_CHECK_OVERFLOW(dtype_size, size_t, unsigned);
cd_values[(*cd_values_index)++] = (unsigned)dtype_size;
/* Get array datatype's base datatype */
if (NULL == (dtype_base = H5T_get_super(type)))
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad base datatype");
/* Get base datatype's class */
if ((dtype_base_class = H5T_get_class(dtype_base, true)) == H5T_NO_CLASS)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad base datatype class");
/* Call appropriate function according to base datatype's class */
switch (dtype_base_class) {
case H5T_INTEGER:
case H5T_FLOAT:
if (H5Z__set_parms_atomic(dtype_base, cd_values_index, cd_values, need_not_compress) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_ARRAY:
if (H5Z__set_parms_array(dtype_base, cd_values_index, cd_values, need_not_compress) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_COMPOUND:
if (H5Z__set_parms_compound(dtype_base, cd_values_index, cd_values, need_not_compress) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_VLEN:
/* Check if base datatype is a variable-length string */
if ((is_vlstring = H5T_is_variable_str(dtype_base)) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL,
"cannot determine if datatype is a variable-length string");
/* base datatype of VL or VL-string is not supported */
if (dtype_base_class == H5T_VLEN || is_vlstring)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "datatype not supported by nbit");
if (H5Z__set_parms_nooptype(dtype_base, cd_values_index, cd_values) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_TIME:
case H5T_STRING:
case H5T_BITFIELD:
case H5T_OPAQUE:
case H5T_REFERENCE:
case H5T_ENUM:
case H5T_COMPLEX:
if (H5Z__set_parms_nooptype(dtype_base, cd_values_index, cd_values) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_NO_CLASS:
case H5T_NCLASSES:
default:
/* Badness */
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit received bad datatype");
break;
} /* end switch */
done:
if (dtype_base)
if (H5T_close_real(dtype_base) < 0)
HDONE_ERROR(H5E_PLINE, H5E_CLOSEERROR, FAIL, "Unable to close base datatype");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z__set_parms_array() */
/*-------------------------------------------------------------------------
* Function: H5Z__set_parms_compound
*
* Purpose: Set the array cd_values[] for a given datatype identifier
* type_id if its datatype class is compound datatype
*
* Return: Success: Non-negative
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
static herr_t
H5Z__set_parms_compound(const H5T_t *type, unsigned *cd_values_index, unsigned cd_values[],
bool *need_not_compress)
{
int snmembers; /* Compound datatype's number of members */
unsigned nmembers; /* Compound datatype's number of members */
H5T_t *dtype_member = NULL; /* Compound datatype's member datatype */
H5T_class_t dtype_member_class; /* Compound datatype's member datatype's class */
size_t dtype_member_offset; /* Compound datatype's current member datatype's offset (in bytes) */
size_t dtype_next_member_offset; /* Compound datatype's next member datatype's offset (in bytes) */
size_t dtype_size; /* Compound datatype's size (in bytes) */
htri_t is_vlstring; /* flag indicating if datatype is variable-length string */
unsigned u; /* Local index variable */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Set "local" parameter for compound datatype class code */
cd_values[(*cd_values_index)++] = H5Z_NBIT_COMPOUND;
/* Get datatype's size */
if ((dtype_size = H5T_get_size(type)) == 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype size");
/* Set "local" parameter for compound datatype size */
H5_CHECK_OVERFLOW(dtype_size, size_t, unsigned);
cd_values[(*cd_values_index)++] = (unsigned)dtype_size;
/* Get number of members */
if ((snmembers = H5T_get_nmembers(type)) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype number of members");
nmembers = (unsigned)snmembers;
/* Set "local" parameter for number of members */
cd_values[(*cd_values_index)++] = nmembers;
/* For each member, set parameters */
for (u = 0; u < nmembers; u++) {
/* Get member datatype */
if (NULL == (dtype_member = H5T_get_member_type(type, u)))
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad member datatype");
/* Get member datatype's class */
if ((dtype_member_class = H5T_get_class(dtype_member, true)) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad member datatype class");
/* Get member offset, success if H5T_get_class() success */
dtype_member_offset = H5T_get_member_offset(type, u);
/* Set "local" parameter for member offset */
H5_CHECK_OVERFLOW(dtype_member_offset, size_t, unsigned);
cd_values[(*cd_values_index)++] = (unsigned)dtype_member_offset;
/* Call appropriate function according to member's datatype class */
switch (dtype_member_class) {
case H5T_INTEGER:
case H5T_FLOAT:
if (H5Z__set_parms_atomic(dtype_member, cd_values_index, cd_values, need_not_compress) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_ARRAY:
if (H5Z__set_parms_array(dtype_member, cd_values_index, cd_values, need_not_compress) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_COMPOUND:
if (H5Z__set_parms_compound(dtype_member, cd_values_index, cd_values, need_not_compress) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_VLEN:
/* Check if datatype is a variable-length string */
if ((is_vlstring = H5T_is_variable_str(dtype_member)) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL,
"cannot determine if datatype is a variable-length string");
/* Because for some no-op datatype (VL datatype and VL string datatype), its
* size can not be retrieved correctly by using function call H5T_get_size,
* special handling is needed for getting the size. Here the difference between
* adjacent member offset is used (if alignment is present, the result can be
* larger, but it does not affect the nbit filter's correctness).
*/
if (dtype_member_class == H5T_VLEN || is_vlstring) {
/* Set datatype class code */
cd_values[(*cd_values_index)++] = H5Z_NBIT_NOOPTYPE;
if (u != nmembers - 1)
dtype_next_member_offset = H5T_get_member_offset(type, u + 1);
else /* current member is the last member */
dtype_next_member_offset = dtype_size;
/* Set "local" parameter for datatype size */
H5_CHECK_OVERFLOW(dtype_member_offset, size_t, unsigned);
H5_CHECK_OVERFLOW(dtype_next_member_offset, size_t, unsigned);
cd_values[(*cd_values_index)++] =
(unsigned)dtype_next_member_offset - (unsigned)dtype_member_offset;
}
break;
case H5T_TIME:
case H5T_STRING:
case H5T_BITFIELD:
case H5T_OPAQUE:
case H5T_REFERENCE:
case H5T_ENUM:
case H5T_COMPLEX:
/* other datatype that nbit does no compression */
if (H5Z__set_parms_nooptype(dtype_member, cd_values_index, cd_values) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_NO_CLASS:
case H5T_NCLASSES:
default:
/* Badness */
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit was passed bad datatype");
break;
} /* end switch */
/* Close member datatype */
if (H5T_close_real(dtype_member) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_CLOSEERROR, FAIL, "Unable to close member datatype");
dtype_member = NULL;
} /* end for */
done:
if (dtype_member)
if (H5T_close_real(dtype_member) < 0)
HDONE_ERROR(H5E_PLINE, H5E_CLOSEERROR, FAIL, "Unable to close member datatype");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z_set_params_compound */
/*-------------------------------------------------------------------------
* Function: H5Z__set_local_nbit
*
* Purpose: Set the "local" dataset parameters for nbit compression.
*
* Return: Success: Non-negative
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
static herr_t
H5Z__set_local_nbit(hid_t dcpl_id, hid_t type_id, hid_t space_id)
{
H5P_genplist_t *dcpl_plist; /* Property list pointer */
const H5T_t *type; /* Datatype */
const H5S_t *ds; /* Dataspace */
unsigned flags; /* Filter flags */
unsigned cd_values_index; /* Index of array cd_values */
size_t cd_values_actual_nparms; /* Number of parameters in array cd_values[] */
size_t cd_nelmts = H5Z_NBIT_USER_NPARMS; /* Number of filter parameters */
unsigned *cd_values = NULL; /* Filter parameters */
hssize_t npoints; /* Number of points in the dataspace */
H5T_class_t dtype_class; /* Datatype's class */
bool need_not_compress; /* Flag if true indicating no need to do nbit compression */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Get datatype */
if (NULL == (type = (H5T_t *)H5I_object_verify(type_id, H5I_DATATYPE)))
HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype");
/* Get datatype's class */
if ((dtype_class = H5T_get_class(type, true)) == H5T_NO_CLASS)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "bad datatype class");
/* Calculate how many parameters will fill the cd_values array
* First three parameters reserved for:
* 1. number of parameters in array cd_values
* 2. flag if true indicating no need to do nbit compression
* 3. number of elements in the chunk
*/
cd_values_actual_nparms = 3;
switch (dtype_class) {
case H5T_INTEGER:
case H5T_FLOAT:
H5Z__calc_parms_atomic(&cd_values_actual_nparms);
break;
case H5T_ARRAY:
if (H5Z__calc_parms_array(type, &cd_values_actual_nparms) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot compute parameters for datatype");
break;
case H5T_COMPOUND:
if (H5Z__calc_parms_compound(type, &cd_values_actual_nparms) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot compute parameters for datatype");
break;
case H5T_TIME:
case H5T_STRING:
case H5T_BITFIELD:
case H5T_OPAQUE:
case H5T_REFERENCE:
case H5T_ENUM:
case H5T_VLEN:
case H5T_COMPLEX:
/* No need to calculate other datatypes at top level */
break;
case H5T_NO_CLASS:
case H5T_NCLASSES:
default:
/* Badness */
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit received bad datatype");
break;
} /* end switch */
/* Check if the number of parameters exceed what cd_values[] can store */
if (cd_values_actual_nparms > H5Z_NBIT_MAX_NPARMS)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "datatype needs too many nbit parameters");
/* Allocate memory space for cd_values[] */
if (NULL == (cd_values = (unsigned *)H5MM_malloc(cd_values_actual_nparms * sizeof(unsigned))))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed for cd_values[]");
/* Get the plist structure */
if (NULL == (dcpl_plist = H5P_object_verify(dcpl_id, H5P_DATASET_CREATE, false)))
HGOTO_ERROR(H5E_ID, H5E_BADID, FAIL, "can't find object for ID");
/* Get the filter's current parameters */
if (H5P_get_filter_by_id(dcpl_plist, H5Z_FILTER_NBIT, &flags, &cd_nelmts, cd_values, (size_t)0, NULL,
NULL) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_CANTGET, FAIL, "can't get nbit parameters");
/* Get dataspace */
if (NULL == (ds = (H5S_t *)H5I_object_verify(space_id, H5I_DATASPACE)))
HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a dataspace");
/* Get total number of elements in the chunk */
if ((npoints = H5S_GET_EXTENT_NPOINTS(ds)) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_CANTGET, FAIL, "unable to get number of points in the dataspace");
assert(npoints);
/* Initialize index for cd_values array starting from the third entry */
cd_values_index = 2;
/* Set "local" parameter for number of elements in the chunk */
H5_CHECK_OVERFLOW(npoints, hssize_t, unsigned);
cd_values[cd_values_index++] = (unsigned)npoints;
/* Assume no need to compress now, will be changed to false later if not */
need_not_compress = true;
/* Call appropriate function according to the datatype class */
switch (dtype_class) {
case H5T_INTEGER:
case H5T_FLOAT:
if (H5Z__set_parms_atomic(type, &cd_values_index, cd_values, &need_not_compress) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_ARRAY:
if (H5Z__set_parms_array(type, &cd_values_index, cd_values, &need_not_compress) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_COMPOUND:
if (H5Z__set_parms_compound(type, &cd_values_index, cd_values, &need_not_compress) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit cannot set parameters for datatype");
break;
case H5T_TIME:
case H5T_STRING:
case H5T_BITFIELD:
case H5T_OPAQUE:
case H5T_REFERENCE:
case H5T_ENUM:
case H5T_VLEN:
case H5T_COMPLEX:
/* No need to set parameters for other datatypes at top level */
break;
case H5T_NO_CLASS:
case H5T_NCLASSES:
default:
/* Badness */
HGOTO_ERROR(H5E_PLINE, H5E_BADTYPE, FAIL, "nbit received bad datatype");
break;
} /* end switch */
/* Check if calculation of parameters matches with setting of parameters */
assert(cd_values_actual_nparms == cd_values_index);
/* Finally set the first two entries of cd_values[] */
H5_CHECK_OVERFLOW(cd_values_actual_nparms, size_t, unsigned);
cd_values[0] = (unsigned)cd_values_actual_nparms;
cd_values[1] = (unsigned)need_not_compress;
/* Modify the filter's parameters for this dataset */
if (H5P_modify_filter(dcpl_plist, H5Z_FILTER_NBIT, flags, cd_values_actual_nparms, cd_values) < 0)
HGOTO_ERROR(H5E_PLINE, H5E_CANTSET, FAIL, "can't set local nbit parameters");
done:
if (cd_values)
H5MM_xfree(cd_values);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z__set_local_nbit() */
/*-------------------------------------------------------------------------
* Function: H5Z__filter_nbit
*
* Purpose: Implement an I/O filter for storing packed nbit data
*
* Return: Success: Size of buffer filtered
* Failure: 0
*
*-------------------------------------------------------------------------
*/
static size_t
H5Z__filter_nbit(unsigned flags, size_t cd_nelmts, const unsigned cd_values[], size_t nbytes,
size_t *buf_size, void **buf)
{
unsigned char *outbuf; /* pointer to new output buffer */
size_t size_out = 0; /* size of output buffer */
unsigned d_nelmts = 0; /* number of elements in the chunk */
size_t ret_value = 0; /* return value */
FUNC_ENTER_PACKAGE
/* check arguments
* cd_values[0] stores actual number of parameters in cd_values[]
*
* The filter header always occupies cd_values[0..4] (number of
* parameters, no-compression flag, number of elements, datatype class
* and datatype size), all of which are read unconditionally below.
* Reject a NULL array or one shorter than that to avoid a bad
* dereference.
*/
if (cd_values == NULL || cd_nelmts < 5)
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, 0, "invalid nbit parameters");
if (cd_nelmts != cd_values[0])
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, 0, "invalid nbit aggression level");
/* check if need to do nbit compress or decompress
* cd_values[1] stores the flag if true indicating no need to compress
*/
if (cd_values[1])
HGOTO_DONE(*buf_size);
/* copy a filter parameter to d_nelmts */
d_nelmts = cd_values[2];
/* input; decompress */
if (flags & H5Z_FLAG_REVERSE) {
size_out = d_nelmts * (size_t)cd_values[4]; /* cd_values[4] stores datatype size */
/* allocate memory space for decompressed buffer */
if (NULL == (outbuf = (unsigned char *)H5MM_malloc(size_out)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, 0, "memory allocation failed for nbit decompression");
/* decompress the buffer */
if (H5Z__nbit_decompress(outbuf, d_nelmts, (unsigned char *)*buf, nbytes, cd_values) < 0) {
H5MM_xfree(outbuf);
HGOTO_ERROR(H5E_PLINE, H5E_CANTFILTER, 0, "can't decompress buffer");
}
} /* end if */
/* output; compress */
else {
assert(nbytes == (size_t)d_nelmts * (size_t)cd_values[4]);
size_out = nbytes;
/* allocate memory space for compressed buffer */
if (NULL == (outbuf = (unsigned char *)H5MM_malloc(size_out)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, 0, "memory allocation failed for nbit compression");
/* compress the buffer, size_out will be changed */
H5Z__nbit_compress((unsigned char *)*buf, d_nelmts, outbuf, &size_out, cd_values);
} /* end else */
/* free the input buffer */
H5MM_xfree(*buf);
/* set return values */
*buf = outbuf;
*buf_size = size_out;
ret_value = size_out;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Z__filter_nbit() */
/* ======== Nbit Algorithm ===============================================
* assume one byte has 8 bit
* assume padding bit is 0