summaryrefslogtreecommitdiffstats
path: root/Modules/audioop.c
blob: ce009758dee4e64787c03aa3a3a52ecbfb98461e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617

/* audioopmodule - Module to detect peak values in arrays */

#include "Python.h"

#if SIZEOF_INT == 4
typedef int Py_Int32;
typedef unsigned int Py_UInt32;
#else
#if SIZEOF_LONG == 4
typedef long Py_Int32;
typedef unsigned long Py_UInt32;
#else
#error "No 4-byte integral type"
#endif
#endif

typedef short PyInt16;

#if defined(__CHAR_UNSIGNED__)
#if defined(signed)
/* This module currently does not work on systems where only unsigned
   characters are available.  Take it out of Setup.  Sorry. */
#endif
#endif

/* Code shamelessly stolen from sox, 12.17.7, g711.c
** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */

/* From g711.c:
 *
 * December 30, 1994:
 * Functions linear2alaw, linear2ulaw have been updated to correctly
 * convert unquantized 16 bit values.
 * Tables for direct u- to A-law and A- to u-law conversions have been
 * corrected.
 * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
 * bli@cpk.auc.dk
 *
 */
#define BIAS 0x84   /* define the add-in bias for 16 bit samples */
#define CLIP 32635
#define SIGN_BIT        (0x80)          /* Sign bit for a A-law byte. */
#define QUANT_MASK      (0xf)           /* Quantization field mask. */
#define SEG_SHIFT       (4)             /* Left shift for segment number. */
#define SEG_MASK        (0x70)          /* Segment field mask. */

static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
                              0x1FF, 0x3FF, 0x7FF, 0xFFF};
static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
                              0x3FF, 0x7FF, 0xFFF, 0x1FFF};

static PyInt16
search(PyInt16 val, PyInt16 *table, int size)
{
        int i;

        for (i = 0; i < size; i++) {
                if (val <= *table++)
                        return (i);
        }
        return (size);
}
#define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
#define st_alaw2linear16(uc) (_st_alaw2linear16[uc])

static PyInt16 _st_ulaw2linear16[256] = {
    -32124,  -31100,  -30076,  -29052,  -28028,  -27004,  -25980,
    -24956,  -23932,  -22908,  -21884,  -20860,  -19836,  -18812,
    -17788,  -16764,  -15996,  -15484,  -14972,  -14460,  -13948,
    -13436,  -12924,  -12412,  -11900,  -11388,  -10876,  -10364,
     -9852,   -9340,   -8828,   -8316,   -7932,   -7676,   -7420,
     -7164,   -6908,   -6652,   -6396,   -6140,   -5884,   -5628,
     -5372,   -5116,   -4860,   -4604,   -4348,   -4092,   -3900,
     -3772,   -3644,   -3516,   -3388,   -3260,   -3132,   -3004,
     -2876,   -2748,   -2620,   -2492,   -2364,   -2236,   -2108,
     -1980,   -1884,   -1820,   -1756,   -1692,   -1628,   -1564,
     -1500,   -1436,   -1372,   -1308,   -1244,   -1180,   -1116,
     -1052,    -988,    -924,    -876,    -844,    -812,    -780,
      -748,    -716,    -684,    -652,    -620,    -588,    -556,
      -524,    -492,    -460,    -428,    -396,    -372,    -356,
      -340,    -324,    -308,    -292,    -276,    -260,    -244,
      -228,    -212,    -196,    -180,    -164,    -148,    -132,
      -120,    -112,    -104,     -96,     -88,     -80,     -72,
       -64,     -56,     -48,     -40,     -32,     -24,     -16,
        -8,       0,   32124,   31100,   30076,   29052,   28028,
     27004,   25980,   24956,   23932,   22908,   21884,   20860,
     19836,   18812,   17788,   16764,   15996,   15484,   14972,
     14460,   13948,   13436,   12924,   12412,   11900,   11388,
     10876,   10364,    9852,    9340,    8828,    8316,    7932,
      7676,    7420,    7164,    6908,    6652,    6396,    6140,
      5884,    5628,    5372,    5116,    4860,    4604,    4348,
      4092,    3900,    3772,    3644,    3516,    3388,    3260,
      3132,    3004,    2876,    2748,    2620,    2492,    2364,
      2236,    2108,    1980,    1884,    1820,    1756,    1692,
      1628,    1564,    1500,    1436,    1372,    1308,    1244,
      1180,    1116,    1052,     988,     924,     876,     844,
       812,     780,     748,     716,     684,     652,     620,
       588,     556,     524,     492,     460,     428,     396,
       372,     356,     340,     324,     308,     292,     276,
       260,     244,     228,     212,     196,     180,     164,
       148,     132,     120,     112,     104,      96,      88,
        80,      72,      64,      56,      48,      40,      32,
        24,      16,       8,       0
};

/*
 * linear2ulaw() accepts a 14-bit signed integer and encodes it as u-law data
 * stored in a unsigned char.  This function should only be called with
 * the data shifted such that it only contains information in the lower
 * 14-bits.
 *
 * In order to simplify the encoding process, the original linear magnitude
 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
 * (33 - 8191). The result can be seen in the following encoding table:
 *
 *      Biased Linear Input Code        Compressed Code
 *      ------------------------        ---------------
 *      00000001wxyza                   000wxyz
 *      0000001wxyzab                   001wxyz
 *      000001wxyzabc                   010wxyz
 *      00001wxyzabcd                   011wxyz
 *      0001wxyzabcde                   100wxyz
 *      001wxyzabcdef                   101wxyz
 *      01wxyzabcdefg                   110wxyz
 *      1wxyzabcdefgh                   111wxyz
 *
 * Each biased linear code has a leading 1 which identifies the segment
 * number. The value of the segment number is equal to 7 minus the number
 * of leading 0's. The quantization interval is directly available as the
 * four bits wxyz.  * The trailing bits (a - h) are ignored.
 *
 * Ordinarily the complement of the resulting code word is used for
 * transmission, and so the code word is complemented before it is returned.
 *
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 * John Wiley & Sons, pps 98-111 and 472-476.
 */
static unsigned char
st_14linear2ulaw(PyInt16 pcm_val)	/* 2's complement (14-bit range) */
{
        PyInt16         mask;
        PyInt16         seg;
        unsigned char   uval;

        /* The original sox code does this in the calling function, not here */
        pcm_val = pcm_val >> 2;

        /* u-law inverts all bits */
        /* Get the sign and the magnitude of the value. */
        if (pcm_val < 0) {
                pcm_val = -pcm_val;
                mask = 0x7F;
        } else {
                mask = 0xFF;
        }
        if ( pcm_val > CLIP ) pcm_val = CLIP;           /* clip the magnitude */
        pcm_val += (BIAS >> 2);

        /* Convert the scaled magnitude to segment number. */
        seg = search(pcm_val, seg_uend, 8);

        /*
         * Combine the sign, segment, quantization bits;
         * and complement the code word.
         */
        if (seg >= 8)           /* out of range, return maximum value. */
                return (unsigned char) (0x7F ^ mask);
        else {
                uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
                return (uval ^ mask);
        }

}

static PyInt16 _st_alaw2linear16[256] = {
     -5504,   -5248,   -6016,   -5760,   -4480,   -4224,   -4992,
     -4736,   -7552,   -7296,   -8064,   -7808,   -6528,   -6272,
     -7040,   -6784,   -2752,   -2624,   -3008,   -2880,   -2240,
     -2112,   -2496,   -2368,   -3776,   -3648,   -4032,   -3904,
     -3264,   -3136,   -3520,   -3392,  -22016,  -20992,  -24064,
    -23040,  -17920,  -16896,  -19968,  -18944,  -30208,  -29184,
    -32256,  -31232,  -26112,  -25088,  -28160,  -27136,  -11008,
    -10496,  -12032,  -11520,   -8960,   -8448,   -9984,   -9472,
    -15104,  -14592,  -16128,  -15616,  -13056,  -12544,  -14080,
    -13568,    -344,    -328,    -376,    -360,    -280,    -264,
      -312,    -296,    -472,    -456,    -504,    -488,    -408,
      -392,    -440,    -424,     -88,     -72,    -120,    -104,
       -24,      -8,     -56,     -40,    -216,    -200,    -248,
      -232,    -152,    -136,    -184,    -168,   -1376,   -1312,
     -1504,   -1440,   -1120,   -1056,   -1248,   -1184,   -1888,
     -1824,   -2016,   -1952,   -1632,   -1568,   -1760,   -1696,
      -688,    -656,    -752,    -720,    -560,    -528,    -624,
      -592,    -944,    -912,   -1008,    -976,    -816,    -784,
      -880,    -848,    5504,    5248,    6016,    5760,    4480,
      4224,    4992,    4736,    7552,    7296,    8064,    7808,
      6528,    6272,    7040,    6784,    2752,    2624,    3008,
      2880,    2240,    2112,    2496,    2368,    3776,    3648,
      4032,    3904,    3264,    3136,    3520,    3392,   22016,
     20992,   24064,   23040,   17920,   16896,   19968,   18944,
     30208,   29184,   32256,   31232,   26112,   25088,   28160,
     27136,   11008,   10496,   12032,   11520,    8960,    8448,
      9984,    9472,   15104,   14592,   16128,   15616,   13056,
     12544,   14080,   13568,     344,     328,     376,     360,
       280,     264,     312,     296,     472,     456,     504,
       488,     408,     392,     440,     424,      88,      72,
       120,     104,      24,       8,      56,      40,     216,
       200,     248,     232,     152,     136,     184,     168,
      1376,    1312,    1504,    1440,    1120,    1056,    1248,
      1184,    1888,    1824,    2016,    1952,    1632,    1568,
      1760,    1696,     688,     656,     752,     720,     560,
       528,     624,     592,     944,     912,    1008,     976,
       816,     784,     880,     848
};

/*
 * linear2alaw() accepts an 13-bit signed integer and encodes it as A-law data
 * stored in a unsigned char.  This function should only be called with
 * the data shifted such that it only contains information in the lower
 * 13-bits.
 *
 *              Linear Input Code       Compressed Code
 *      ------------------------        ---------------
 *      0000000wxyza                    000wxyz
 *      0000001wxyza                    001wxyz
 *      000001wxyzab                    010wxyz
 *      00001wxyzabc                    011wxyz
 *      0001wxyzabcd                    100wxyz
 *      001wxyzabcde                    101wxyz
 *      01wxyzabcdef                    110wxyz
 *      1wxyzabcdefg                    111wxyz
 *
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 * John Wiley & Sons, pps 98-111 and 472-476.
 */
static unsigned char
st_linear2alaw(PyInt16 pcm_val)	/* 2's complement (13-bit range) */
{
        PyInt16         mask;
        short           seg;
        unsigned char   aval;

        /* The original sox code does this in the calling function, not here */
        pcm_val = pcm_val >> 3;

        /* A-law using even bit inversion */
        if (pcm_val >= 0) {
                mask = 0xD5;            /* sign (7th) bit = 1 */
        } else {
                mask = 0x55;            /* sign bit = 0 */
                pcm_val = -pcm_val - 1;
        }

        /* Convert the scaled magnitude to segment number. */
        seg = search(pcm_val, seg_aend, 8);

        /* Combine the sign, segment, and quantization bits. */

        if (seg >= 8)           /* out of range, return maximum value. */
                return (unsigned char) (0x7F ^ mask);
        else {
                aval = (unsigned char) seg << SEG_SHIFT;
                if (seg < 2)
                        aval |= (pcm_val >> 1) & QUANT_MASK;
                else
                        aval |= (pcm_val >> seg) & QUANT_MASK;
                return (aval ^ mask);
        }
}
/* End of code taken from sox */

/* Intel ADPCM step variation table */
static int indexTable[16] = {
        -1, -1, -1, -1, 2, 4, 6, 8,
        -1, -1, -1, -1, 2, 4, 6, 8,
};

static int stepsizeTable[89] = {
        7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
        19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
        50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
        130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
        337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
        876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
        2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
        5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
        15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
    
#define CHARP(cp, i) ((signed char *)(cp+i))
#define SHORTP(cp, i) ((short *)(cp+i))
#define LONGP(cp, i) ((Py_Int32 *)(cp+i))



static PyObject *AudioopError;

static PyObject *
audioop_getsample(PyObject *self, PyObject *args)
{
        signed char *cp;
        int len, size, val = 0;
        int i;

        if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
                return 0;
        if ( size != 1 && size != 2 && size != 4 ) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
        if ( i < 0 || i >= len/size ) {
                PyErr_SetString(AudioopError, "Index out of range");
                return 0;
        }
        if ( size == 1 )      val = (int)*CHARP(cp, i);
        else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
        else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
        return PyInt_FromLong(val);
}

static PyObject *
audioop_max(PyObject *self, PyObject *args)
{
        signed char *cp;
        int len, size, val = 0;
        int i;
        int max = 0;

        if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
                return 0;
        if ( size != 1 && size != 2 && size != 4 ) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
        for ( i=0; i<len; i+= size) {
                if ( size == 1 )      val = (int)*CHARP(cp, i);
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = (int)*LONGP(cp, i);
                if ( val < 0 ) val = (-val);
                if ( val > max ) max = val;
        }
        return PyInt_FromLong(max);
}

static PyObject *
audioop_minmax(PyObject *self, PyObject *args)
{
        signed char *cp;
        int len, size, val = 0;
        int i;
        int min = 0x7fffffff, max = -0x7fffffff;

        if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
                return NULL;
        if (size != 1 && size != 2 && size != 4) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return NULL;
        }
        for (i = 0; i < len; i += size) {
                if (size == 1) val = (int) *CHARP(cp, i);
                else if (size == 2) val = (int) *SHORTP(cp, i);
                else if (size == 4) val = (int) *LONGP(cp, i);
                if (val > max) max = val;
                if (val < min) min = val;
        }
        return Py_BuildValue("(ii)", min, max);
}

static PyObject *
audioop_avg(PyObject *self, PyObject *args)
{
        signed char *cp;
        int len, size, val = 0;
        int i;
        double avg = 0.0;

        if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
                return 0;
        if ( size != 1 && size != 2 && size != 4 ) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
        for ( i=0; i<len; i+= size) {
                if ( size == 1 )      val = (int)*CHARP(cp, i);
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = (int)*LONGP(cp, i);
                avg += val;
        }
        if ( len == 0 )
                val = 0;
        else
                val = (int)(avg / (double)(len/size));
        return PyInt_FromLong(val);
}

static PyObject *
audioop_rms(PyObject *self, PyObject *args)
{
        signed char *cp;
        int len, size, val = 0;
        int i;
        double sum_squares = 0.0;

        if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
                return 0;
        if ( size != 1 && size != 2 && size != 4 ) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
        for ( i=0; i<len; i+= size) {
                if ( size == 1 )      val = (int)*CHARP(cp, i);
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = (int)*LONGP(cp, i);
                sum_squares += (double)val*(double)val;
        }
        if ( len == 0 )
                val = 0;
        else
                val = (int)sqrt(sum_squares / (double)(len/size));
        return PyInt_FromLong(val);
}

static double _sum2(short *a, short *b, int len)
{
        int i;
        double sum = 0.0;

        for( i=0; i<len; i++) {
                sum = sum + (double)a[i]*(double)b[i];
        }
        return sum;
}

/*
** Findfit tries to locate a sample within another sample. Its main use
** is in echo-cancellation (to find the feedback of the output signal in
** the input signal).
** The method used is as follows:
**
** let R be the reference signal (length n) and A the input signal (length N)
** with N > n, and let all sums be over i from 0 to n-1.
**
** Now, for each j in {0..N-n} we compute a factor fj so that -fj*R matches A
** as good as possible, i.e. sum( (A[j+i]+fj*R[i])^2 ) is minimal. This
** equation gives fj = sum( A[j+i]R[i] ) / sum(R[i]^2).
**
** Next, we compute the relative distance between the original signal and
** the modified signal and minimize that over j:
** vj = sum( (A[j+i]-fj*R[i])^2 ) / sum( A[j+i]^2 )  =>
** vj = ( sum(A[j+i]^2)*sum(R[i]^2) - sum(A[j+i]R[i])^2 ) / sum( A[j+i]^2 )
**
** In the code variables correspond as follows:
** cp1          A
** cp2          R
** len1         N
** len2         n
** aj_m1        A[j-1]
** aj_lm1       A[j+n-1]
** sum_ri_2     sum(R[i]^2)
** sum_aij_2    sum(A[i+j]^2)
** sum_aij_ri   sum(A[i+j]R[i])
**
** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri
** is completely recalculated each step.
*/
static PyObject *
audioop_findfit(PyObject *self, PyObject *args)
{
        short *cp1, *cp2;
        int len1, len2;
        int j, best_j;
        double aj_m1, aj_lm1;
        double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;

	/* Passing a short** for an 's' argument is correct only
	   if the string contents is aligned for interpretation
	   as short[]. Due to the definition of PyStringObject,
	   this is currently (Python 2.6) the case. */
        if ( !PyArg_ParseTuple(args, "s#s#:findfit",
	                       (char**)&cp1, &len1, (char**)&cp2, &len2) )
                return 0;
        if ( len1 & 1 || len2 & 1 ) {
                PyErr_SetString(AudioopError, "Strings should be even-sized");
                return 0;
        }
        len1 >>= 1;
        len2 >>= 1;
    
        if ( len1 < len2 ) {
                PyErr_SetString(AudioopError, "First sample should be longer");
                return 0;
        }
        sum_ri_2 = _sum2(cp2, cp2, len2);
        sum_aij_2 = _sum2(cp1, cp1, len2);
        sum_aij_ri = _sum2(cp1, cp2, len2);

        result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;

        best_result = result;
        best_j = 0;
        j = 0;

        for ( j=1; j<=len1-len2; j++) {
                aj_m1 = (double)cp1[j-1];
                aj_lm1 = (double)cp1[j+len2-1];

                sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
                sum_aij_ri = _sum2(cp1+j, cp2, len2);

                result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
                        / sum_aij_2;

                if ( result < best_result ) {
                        best_result = result;
                        best_j = j;
                }
        
        }

        factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
    
        return Py_BuildValue("(if)", best_j, factor);
}

/*
** findfactor finds a factor f so that the energy in A-fB is minimal.
** See the comment for findfit for details.
*/
static PyObject *
audioop_findfactor(PyObject *self, PyObject *args)
{
        short *cp1, *cp2;
        int len1, len2;
        double sum_ri_2, sum_aij_ri, result;

        if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
	                       (char**)&cp1, &len1, (char**)&cp2, &len2) )
                return 0;
        if ( len1 & 1 || len2 & 1 ) {
                PyErr_SetString(AudioopError, "Strings should be even-sized");
                return 0;
        }
        if ( len1 != len2 ) {
                PyErr_SetString(AudioopError, "Samples should be same size");
                return 0;
        }
        len2 >>= 1;
        sum_ri_2 = _sum2(cp2, cp2, len2);
        sum_aij_ri = _sum2(cp1, cp2, len2);

        result = sum_aij_ri / sum_ri_2;

        return PyFloat_FromDouble(result);
}

/*
** findmax returns the index of the n-sized segment of the input sample
** that contains the most energy.
*/
static PyObject *
audioop_findmax(PyObject *self, PyObject *args)
{
        short *cp1;
        int len1, len2;
        int j, best_j;
        double aj_m1, aj_lm1;
        double result, best_result;

        if ( !PyArg_ParseTuple(args, "s#i:findmax", 
			       (char**)&cp1, &len1, &len2) )
                return 0;
        if ( len1 & 1 ) {
                PyErr_SetString(AudioopError, "Strings should be even-sized");
                return 0;
        }
        len1 >>= 1;
    
        if ( len1 < len2 ) {
                PyErr_SetString(AudioopError, "Input sample should be longer");
                return 0;
        }

        result = _sum2(cp1, cp1, len2);

        best_result = result;
        best_j = 0;
        j = 0;

        for ( j=1; j<=len1-len2; j++) {
                aj_m1 = (double)cp1[j-1];
                aj_lm1 = (double)cp1[j+len2-1];

                result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;

                if ( result > best_result ) {
                        best_result = result;
                        best_j = j;
                }
        
        }

        return PyInt_FromLong(best_j);
}

static PyObject *
audioop_avgpp(PyObject *self, PyObject *args)
{
        signed char *cp;
        int len, size, val = 0, prevval = 0, prevextremevalid = 0,
                prevextreme = 0;
        int i;
        double avg = 0.0;
        int diff, prevdiff, extremediff, nextreme = 0;

        if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
                return 0;
        if ( size != 1 && size != 2 && size != 4 ) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
        /* Compute first delta value ahead. Also automatically makes us
        ** skip the first extreme value
        */
        if ( size == 1 )      prevval = (int)*CHARP(cp, 0);
        else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
        else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
        if ( size == 1 )      val = (int)*CHARP(cp, size);
        else if ( size == 2 ) val = (int)*SHORTP(cp, size);
        else if ( size == 4 ) val = (int)*LONGP(cp, size);
        prevdiff = val - prevval;
    
        for ( i=size; i<len; i+= size) {
                if ( size == 1 )      val = (int)*CHARP(cp, i);
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = (int)*LONGP(cp, i);
                diff = val - prevval;
                if ( diff*prevdiff < 0 ) {
                        /* Derivative changed sign. Compute difference to last
                        ** extreme value and remember.
                        */
                        if ( prevextremevalid ) {
                                extremediff = prevval - prevextreme;
                                if ( extremediff < 0 )
                                        extremediff = -extremediff;
                                avg += extremediff;
                                nextreme++;
                        }
                        prevextremevalid = 1;
                        prevextreme = prevval;
                }
                prevval = val;
                if ( diff != 0 )
                        prevdiff = diff;        
        }
        if ( nextreme == 0 )
                val = 0;
        else
                val = (int)(avg / (double)nextreme);
        return PyInt_FromLong(val);
}

static PyObject *
audioop_maxpp(PyObject *self, PyObject *args)
{
        signed char *cp;
        int len, size, val = 0, prevval = 0, prevextremevalid = 0,
                prevextreme = 0;
        int i;
        int max = 0;
        int diff, prevdiff, extremediff;

        if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
                return 0;
        if ( size != 1 && size != 2 && size != 4 ) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
        /* Compute first delta value ahead. Also automatically makes us
        ** skip the first extreme value
        */
        if ( size == 1 )      prevval = (int)*CHARP(cp, 0);
        else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
        else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
        if ( size == 1 )      val = (int)*CHARP(cp, size);
        else if ( size == 2 ) val = (int)*SHORTP(cp, size);
        else if ( size == 4 ) val = (int)*LONGP(cp, size);
        prevdiff = val - prevval;

        for ( i=size; i<len; i+= size) {
                if ( size == 1 )      val = (int)*CHARP(cp, i);
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = (int)*LONGP(cp, i);
                diff = val - prevval;
                if ( diff*prevdiff < 0 ) {
                        /* Derivative changed sign. Compute difference to
                        ** last extreme value and remember.
                        */
                        if ( prevextremevalid ) {
                                extremediff = prevval - prevextreme;
                                if ( extremediff < 0 )
                                        extremediff = -extremediff;
                                if ( extremediff > max )
                                        max = extremediff;
                        }
                        prevextremevalid = 1;
                        prevextreme = prevval;
                }
                prevval = val;
                if ( diff != 0 )
                        prevdiff = diff;
        }
        return PyInt_FromLong(max);
}

static PyObject *
audioop_cross(PyObject *self, PyObject *args)
{
        signed char *cp;
        int len, size, val = 0;
        int i;
        int prevval, ncross;

        if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
                return 0;
        if ( size != 1 && size != 2 && size != 4 ) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
        ncross = -1;
        prevval = 17; /* Anything <> 0,1 */
        for ( i=0; i<len; i+= size) {
                if ( size == 1 )      val = ((int)*CHARP(cp, i)) >> 7;
                else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
                else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
                val = val & 1;
                if ( val != prevval ) ncross++;
                prevval = val;
        }
        return PyInt_FromLong(ncross);
}

static PyObject *
audioop_mul(PyObject *self, PyObject *args)
{
        signed char *cp, *ncp;
        int len, size, val = 0;
        double factor, fval, maxval;
        PyObject *rv;
        int i;

        if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
                return 0;
    
        if ( size == 1 ) maxval = (double) 0x7f;
        else if ( size == 2 ) maxval = (double) 0x7fff;
        else if ( size == 4 ) maxval = (double) 0x7fffffff;
        else {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, len);
        if ( rv == 0 )
                return 0;
        ncp = (signed char *)PyString_AsString(rv);
    
    
        for ( i=0; i < len; i += size ) {
                if ( size == 1 )      val = (int)*CHARP(cp, i);
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = (int)*LONGP(cp, i);
                fval = (double)val*factor;
                if ( fval > maxval ) fval = maxval;
                else if ( fval < -maxval ) fval = -maxval;
                val = (int)fval;
                if ( size == 1 )      *CHARP(ncp, i) = (signed char)val;
                else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
                else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
        }
        return rv;
}

static PyObject *
audioop_tomono(PyObject *self, PyObject *args)
{
        signed char *cp, *ncp;
        int len, size, val1 = 0, val2 = 0;
        double fac1, fac2, fval, maxval;
        PyObject *rv;
        int i;

        if ( !PyArg_ParseTuple(args, "s#idd:tomono",
	                       &cp, &len, &size, &fac1, &fac2 ) )
                return 0;
    
        if ( size == 1 ) maxval = (double) 0x7f;
        else if ( size == 2 ) maxval = (double) 0x7fff;
        else if ( size == 4 ) maxval = (double) 0x7fffffff;
        else {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, len/2);
        if ( rv == 0 )
                return 0;
        ncp = (signed char *)PyString_AsString(rv);
    
    
        for ( i=0; i < len; i += size*2 ) {
                if ( size == 1 )      val1 = (int)*CHARP(cp, i);
                else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
                if ( size == 1 )      val2 = (int)*CHARP(cp, i+1);
                else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
                else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
                fval = (double)val1*fac1 + (double)val2*fac2;
                if ( fval > maxval ) fval = maxval;
                else if ( fval < -maxval ) fval = -maxval;
                val1 = (int)fval;
                if ( size == 1 )      *CHARP(ncp, i/2) = (signed char)val1;
                else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
                else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
        }
        return rv;
}

static PyObject *
audioop_tostereo(PyObject *self, PyObject *args)
{
        signed char *cp, *ncp;
        int len, size, val1, val2, val = 0;
        double fac1, fac2, fval, maxval;
        PyObject *rv;
        int i;

        if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
	                       &cp, &len, &size, &fac1, &fac2 ) )
                return 0;
    
        if ( size == 1 ) maxval = (double) 0x7f;
        else if ( size == 2 ) maxval = (double) 0x7fff;
        else if ( size == 4 ) maxval = (double) 0x7fffffff;
        else {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, len*2);
        if ( rv == 0 )
                return 0;
        ncp = (signed char *)PyString_AsString(rv);
    
    
        for ( i=0; i < len; i += size ) {
                if ( size == 1 )      val = (int)*CHARP(cp, i);
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = (int)*LONGP(cp, i);

                fval = (double)val*fac1;
                if ( fval > maxval ) fval = maxval;
                else if ( fval < -maxval ) fval = -maxval;
                val1 = (int)fval;

                fval = (double)val*fac2;
                if ( fval > maxval ) fval = maxval;
                else if ( fval < -maxval ) fval = -maxval;
                val2 = (int)fval;

                if ( size == 1 )      *CHARP(ncp, i*2) = (signed char)val1;
                else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
                else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;

                if ( size == 1 )      *CHARP(ncp, i*2+1) = (signed char)val2;
                else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
                else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
        }
        return rv;
}

static PyObject *
audioop_add(PyObject *self, PyObject *args)
{
        signed char *cp1, *cp2, *ncp;
        int len1, len2, size, val1 = 0, val2 = 0, maxval, newval;
        PyObject *rv;
        int i;

        if ( !PyArg_ParseTuple(args, "s#s#i:add",
                          &cp1, &len1, &cp2, &len2, &size ) )
                return 0;

        if ( len1 != len2 ) {
                PyErr_SetString(AudioopError, "Lengths should be the same");
                return 0;
        }
    
        if ( size == 1 ) maxval = 0x7f;
        else if ( size == 2 ) maxval = 0x7fff;
        else if ( size == 4 ) maxval = 0x7fffffff;
        else {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }

        rv = PyString_FromStringAndSize(NULL, len1);
        if ( rv == 0 )
                return 0;
        ncp = (signed char *)PyString_AsString(rv);

        for ( i=0; i < len1; i += size ) {
                if ( size == 1 )      val1 = (int)*CHARP(cp1, i);
                else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
                else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
        
                if ( size == 1 )      val2 = (int)*CHARP(cp2, i);
                else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
                else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);

                newval = val1 + val2;
                /* truncate in case of overflow */
                if (newval > maxval) newval = maxval;
                else if (newval < -maxval) newval = -maxval;
                else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
                        newval = val1 > 0 ? maxval : - maxval;

                if ( size == 1 )      *CHARP(ncp, i) = (signed char)newval;
                else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
                else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
        }
        return rv;
}

static PyObject *
audioop_bias(PyObject *self, PyObject *args)
{
        signed char *cp, *ncp;
        int len, size, val = 0;
        PyObject *rv;
        int i;
        int bias;

        if ( !PyArg_ParseTuple(args, "s#ii:bias",
                          &cp, &len, &size , &bias) )
                return 0;

        if ( size != 1 && size != 2 && size != 4) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, len);
        if ( rv == 0 )
                return 0;
        ncp = (signed char *)PyString_AsString(rv);
    
    
        for ( i=0; i < len; i += size ) {
                if ( size == 1 )      val = (int)*CHARP(cp, i);
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = (int)*LONGP(cp, i);
        
                if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val+bias);
                else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
                else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
        }
        return rv;
}

static PyObject *
audioop_reverse(PyObject *self, PyObject *args)
{
        signed char *cp;
        unsigned char *ncp;
        int len, size, val = 0;
        PyObject *rv;
        int i, j;

        if ( !PyArg_ParseTuple(args, "s#i:reverse",
                          &cp, &len, &size) )
                return 0;

        if ( size != 1 && size != 2 && size != 4 ) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, len);
        if ( rv == 0 )
                return 0;
        ncp = (unsigned char *)PyString_AsString(rv);
    
        for ( i=0; i < len; i += size ) {
                if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;

                j = len - i - size;
        
                if ( size == 1 )      *CHARP(ncp, j) = (signed char)(val >> 8);
                else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
                else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
        }
        return rv;
}

static PyObject *
audioop_lin2lin(PyObject *self, PyObject *args)
{
        signed char *cp;
        unsigned char *ncp;
        int len, size, size2, val = 0;
        PyObject *rv;
        int i, j;

        if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
                          &cp, &len, &size, &size2) )
                return 0;

        if ( (size != 1 && size != 2 && size != 4) ||
             (size2 != 1 && size2 != 2 && size2 != 4)) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, (len/size)*size2);
        if ( rv == 0 )
                return 0;
        ncp = (unsigned char *)PyString_AsString(rv);
    
        for ( i=0, j=0; i < len; i += size, j += size2 ) {
                if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;

                if ( size2 == 1 )  *CHARP(ncp, j) = (signed char)(val >> 8);
                else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
                else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
        }
        return rv;
}

static int
gcd(int a, int b)
{
        while (b > 0) {
                int tmp = a % b;
                a = b;
                b = tmp;
        }
        return a;
}

static PyObject *
audioop_ratecv(PyObject *self, PyObject *args)
{
        char *cp, *ncp;
        int len, size, nchannels, inrate, outrate, weightA, weightB;
        int chan, d, *prev_i, *cur_i, cur_o;
        PyObject *state, *samps, *str, *rv = NULL;
        int bytes_per_frame;

        weightA = 1;
        weightB = 0;
        if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
	                      &nchannels, &inrate, &outrate, &state,
			      &weightA, &weightB))
                return NULL;
        if (size != 1 && size != 2 && size != 4) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return NULL;
        }
        if (nchannels < 1) {
                PyErr_SetString(AudioopError, "# of channels should be >= 1");
                return NULL;
        }
        bytes_per_frame = size * nchannels;
        if (bytes_per_frame / nchannels != size) {
                /* This overflow test is rigorously correct because
                   both multiplicands are >= 1.  Use the argument names
                   from the docs for the error msg. */
                PyErr_SetString(PyExc_OverflowError,
                                "width * nchannels too big for a C int");
                return NULL;
        }
        if (weightA < 1 || weightB < 0) {
                PyErr_SetString(AudioopError,
                        "weightA should be >= 1, weightB should be >= 0");
                return NULL;
        }
        if (len % bytes_per_frame != 0) {
                PyErr_SetString(AudioopError, "not a whole number of frames");
                return NULL;
        }
        if (inrate <= 0 || outrate <= 0) {
                PyErr_SetString(AudioopError, "sampling rate not > 0");
                return NULL;
        }
        /* divide inrate and outrate by their greatest common divisor */
        d = gcd(inrate, outrate);
        inrate /= d;
        outrate /= d;

        prev_i = (int *) malloc(nchannels * sizeof(int));
        cur_i = (int *) malloc(nchannels * sizeof(int));
        if (prev_i == NULL || cur_i == NULL) {
                (void) PyErr_NoMemory();
                goto exit;
        }

        len /= bytes_per_frame; /* # of frames */

        if (state == Py_None) {
                d = -outrate;
                for (chan = 0; chan < nchannels; chan++)
                        prev_i[chan] = cur_i[chan] = 0;
        }
        else {
                if (!PyArg_ParseTuple(state,
                                "iO!;audioop.ratecv: illegal state argument",
                                &d, &PyTuple_Type, &samps))
                        goto exit;
                if (PyTuple_Size(samps) != nchannels) {
                        PyErr_SetString(AudioopError,
                                        "illegal state argument");
                        goto exit;
                }
                for (chan = 0; chan < nchannels; chan++) {
                        if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
                                              "ii:ratecv", &prev_i[chan], 
					                   &cur_i[chan]))
                                goto exit;
                }
        }

        /* str <- Space for the output buffer. */
        {
                /* There are len input frames, so we need (mathematically)
                   ceiling(len*outrate/inrate) output frames, and each frame
                   requires bytes_per_frame bytes.  Computing this
                   without spurious overflow is the challenge; we can
                   settle for a reasonable upper bound, though. */
                int ceiling;   /* the number of output frames */
                int nbytes;    /* the number of output bytes needed */
                int q = len / inrate;
                /* Now len = q * inrate + r exactly (with r = len % inrate),
                   and this is less than q * inrate + inrate = (q+1)*inrate.
                   So a reasonable upper bound on len*outrate/inrate is
                   ((q+1)*inrate)*outrate/inrate =
                   (q+1)*outrate.
                */
                ceiling = (q+1) * outrate;
                nbytes = ceiling * bytes_per_frame;
                /* See whether anything overflowed; if not, get the space. */
                if (q+1 < 0 ||
                    ceiling / outrate != q+1 ||
                    nbytes / bytes_per_frame != ceiling)
                        str = NULL;
                else
                        str = PyString_FromStringAndSize(NULL, nbytes);

                if (str == NULL) {
                        PyErr_SetString(PyExc_MemoryError,
                                "not enough memory for output buffer");
                        goto exit;
                }
        }
        ncp = PyString_AsString(str);

        for (;;) {
                while (d < 0) {
                        if (len == 0) {
                                samps = PyTuple_New(nchannels);
                                if (samps == NULL)
                                        goto exit;
                                for (chan = 0; chan < nchannels; chan++)
                                        PyTuple_SetItem(samps, chan,
                                                Py_BuildValue("(ii)",
                                                              prev_i[chan],
                                                              cur_i[chan]));
                                if (PyErr_Occurred())
                                        goto exit;
                                /* We have checked before that the length
                                 * of the string fits into int. */
                                len = (int)(ncp - PyString_AsString(str));
                                if (len == 0) {
                                        /*don't want to resize to zero length*/
                                        rv = PyString_FromStringAndSize("", 0);
                                        Py_DECREF(str);
                                        str = rv;
                                } else if (_PyString_Resize(&str, len) < 0)
                                        goto exit;
                                rv = Py_BuildValue("(O(iO))", str, d, samps);
                                Py_DECREF(samps);
                                Py_DECREF(str);
                                goto exit; /* return rv */
                        }
                        for (chan = 0; chan < nchannels; chan++) {
                                prev_i[chan] = cur_i[chan];
                                if (size == 1)
                                    cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
                                else if (size == 2)
                                    cur_i[chan] = (int)*SHORTP(cp, 0);
                                else if (size == 4)
                                    cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
                                cp += size;
                                /* implements a simple digital filter */
                                cur_i[chan] =
                                        (weightA * cur_i[chan] +
                                         weightB * prev_i[chan]) /
                                        (weightA + weightB);
                        }
                        len--;
                        d += outrate;
                }
                while (d >= 0) {
                        for (chan = 0; chan < nchannels; chan++) {
                                cur_o = (prev_i[chan] * d +
                                         cur_i[chan] * (outrate - d)) /
                                        outrate;
                                if (size == 1)
                                    *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
                                else if (size == 2)
                                    *SHORTP(ncp, 0) = (short)(cur_o);
                                else if (size == 4)
                                    *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
                                ncp += size;
                        }
                        d -= inrate;
                }
        }
  exit:
        if (prev_i != NULL)
                free(prev_i);
        if (cur_i != NULL)
                free(cur_i);
        return rv;
}

static PyObject *
audioop_lin2ulaw(PyObject *self, PyObject *args)
{
        signed char *cp;
        unsigned char *ncp;
        int len, size, val = 0;
        PyObject *rv;
        int i;

        if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
                               &cp, &len, &size) )
                return 0 ;

        if ( size != 1 && size != 2 && size != 4) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, len/size);
        if ( rv == 0 )
                return 0;
        ncp = (unsigned char *)PyString_AsString(rv);
    
        for ( i=0; i < len; i += size ) {
                if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;

                *ncp++ = st_14linear2ulaw(val);
        }
        return rv;
}

static PyObject *
audioop_ulaw2lin(PyObject *self, PyObject *args)
{
        unsigned char *cp;
        unsigned char cval;
        signed char *ncp;
        int len, size, val;
        PyObject *rv;
        int i;

        if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
                               &cp, &len, &size) )
                return 0;

        if ( size != 1 && size != 2 && size != 4) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, len*size);
        if ( rv == 0 )
                return 0;
        ncp = (signed char *)PyString_AsString(rv);
    
        for ( i=0; i < len*size; i += size ) {
                cval = *cp++;
                val = st_ulaw2linear16(cval);
        
                if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val >> 8);
                else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
                else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
        }
        return rv;
}

static PyObject *
audioop_lin2alaw(PyObject *self, PyObject *args)
{
        signed char *cp;
        unsigned char *ncp;
        int len, size, val = 0;
        PyObject *rv;
        int i;

        if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
                               &cp, &len, &size) )
                return 0;

        if ( size != 1 && size != 2 && size != 4) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, len/size);
        if ( rv == 0 )
                return 0;
        ncp = (unsigned char *)PyString_AsString(rv);
    
        for ( i=0; i < len; i += size ) {
                if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;

                *ncp++ = st_linear2alaw(val);
        }
        return rv;
}

static PyObject *
audioop_alaw2lin(PyObject *self, PyObject *args)
{
        unsigned char *cp;
        unsigned char cval;
        signed char *ncp;
        int len, size, val;
        PyObject *rv;
        int i;

        if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
                               &cp, &len, &size) )
                return 0;

        if ( size != 1 && size != 2 && size != 4) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        rv = PyString_FromStringAndSize(NULL, len*size);
        if ( rv == 0 )
                return 0;
        ncp = (signed char *)PyString_AsString(rv);
    
        for ( i=0; i < len*size; i += size ) {
                cval = *cp++;
                val = st_alaw2linear16(cval);
        
                if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val >> 8);
                else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
                else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
        }
        return rv;
}

static PyObject *
audioop_lin2adpcm(PyObject *self, PyObject *args)
{
        signed char *cp;
        signed char *ncp;
        int len, size, val = 0, step, valpred, delta,
                index, sign, vpdiff, diff;
        PyObject *rv, *state, *str;
        int i, outputbuffer = 0, bufferstep;

        if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
                               &cp, &len, &size, &state) )
                return 0;
    

        if ( size != 1 && size != 2 && size != 4) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        str = PyString_FromStringAndSize(NULL, len/(size*2));
        if ( str == 0 )
                return 0;
        ncp = (signed char *)PyString_AsString(str);

        /* Decode state, should have (value, step) */
        if ( state == Py_None ) {
                /* First time, it seems. Set defaults */
                valpred = 0;
                step = 7;
                index = 0;
        } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
                return 0;

        step = stepsizeTable[index];
        bufferstep = 1;

        for ( i=0; i < len; i += size ) {
                if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
                else if ( size == 2 ) val = (int)*SHORTP(cp, i);
                else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;

                /* Step 1 - compute difference with previous value */
                diff = val - valpred;
                sign = (diff < 0) ? 8 : 0;
                if ( sign ) diff = (-diff);

                /* Step 2 - Divide and clamp */
                /* Note:
                ** This code *approximately* computes:
                **    delta = diff*4/step;
                **    vpdiff = (delta+0.5)*step/4;
                ** but in shift step bits are dropped. The net result of this
                ** is that even if you have fast mul/div hardware you cannot
                ** put it to good use since the fixup would be too expensive.
                */
                delta = 0;
                vpdiff = (step >> 3);
        
                if ( diff >= step ) {
                        delta = 4;
                        diff -= step;
                        vpdiff += step;
                }
                step >>= 1;
                if ( diff >= step  ) {
                        delta |= 2;
                        diff -= step;
                        vpdiff += step;
                }
                step >>= 1;
                if ( diff >= step ) {
                        delta |= 1;
                        vpdiff += step;
                }

                /* Step 3 - Update previous value */
                if ( sign )
                        valpred -= vpdiff;
                else
                        valpred += vpdiff;

                /* Step 4 - Clamp previous value to 16 bits */
                if ( valpred > 32767 )
                        valpred = 32767;
                else if ( valpred < -32768 )
                        valpred = -32768;

                /* Step 5 - Assemble value, update index and step values */
                delta |= sign;
        
                index += indexTable[delta];
                if ( index < 0 ) index = 0;
                if ( index > 88 ) index = 88;
                step = stepsizeTable[index];

                /* Step 6 - Output value */
                if ( bufferstep ) {
                        outputbuffer = (delta << 4) & 0xf0;
                } else {
                        *ncp++ = (delta & 0x0f) | outputbuffer;
                }
                bufferstep = !bufferstep;
        }
        rv = Py_BuildValue("(O(ii))", str, valpred, index);
        Py_DECREF(str);
        return rv;
}

static PyObject *
audioop_adpcm2lin(PyObject *self, PyObject *args)
{
        signed char *cp;
        signed char *ncp;
        int len, size, valpred, step, delta, index, sign, vpdiff;
        PyObject *rv, *str, *state;
        int i, inputbuffer = 0, bufferstep;

        if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
                               &cp, &len, &size, &state) )
                return 0;

        if ( size != 1 && size != 2 && size != 4) {
                PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
                return 0;
        }
    
        /* Decode state, should have (value, step) */
        if ( state == Py_None ) {
                /* First time, it seems. Set defaults */
                valpred = 0;
                step = 7;
                index = 0;
        } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
                return 0;
    
        str = PyString_FromStringAndSize(NULL, len*size*2);
        if ( str == 0 )
                return 0;
        ncp = (signed char *)PyString_AsString(str);

        step = stepsizeTable[index];
        bufferstep = 0;
    
        for ( i=0; i < len*size*2; i += size ) {
                /* Step 1 - get the delta value and compute next index */
                if ( bufferstep ) {
                        delta = inputbuffer & 0xf;
                } else {
                        inputbuffer = *cp++;
                        delta = (inputbuffer >> 4) & 0xf;
                }

                bufferstep = !bufferstep;

                /* Step 2 - Find new index value (for later) */
                index += indexTable[delta];
                if ( index < 0 ) index = 0;
                if ( index > 88 ) index = 88;

                /* Step 3 - Separate sign and magnitude */
                sign = delta & 8;
                delta = delta & 7;

                /* Step 4 - Compute difference and new predicted value */
                /*
                ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
                ** in adpcm_coder.
                */
                vpdiff = step >> 3;
                if ( delta & 4 ) vpdiff += step;
                if ( delta & 2 ) vpdiff += step>>1;
                if ( delta & 1 ) vpdiff += step>>2;

                if ( sign )
                        valpred -= vpdiff;
                else
                        valpred += vpdiff;

                /* Step 5 - clamp output value */
                if ( valpred > 32767 )
                        valpred = 32767;
                else if ( valpred < -32768 )
                        valpred = -32768;

                /* Step 6 - Update step value */
                step = stepsizeTable[index];

                /* Step 6 - Output value */
                if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
                else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
                else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
        }

        rv = Py_BuildValue("(O(ii))", str, valpred, index);
        Py_DECREF(str);
        return rv;
}

static PyMethodDef audioop_methods[] = {
        { "max", audioop_max, METH_VARARGS },
        { "minmax", audioop_minmax, METH_VARARGS },
        { "avg", audioop_avg, METH_VARARGS },
        { "maxpp", audioop_maxpp, METH_VARARGS },
        { "avgpp", audioop_avgpp, METH_VARARGS },
        { "rms", audioop_rms, METH_VARARGS },
        { "findfit", audioop_findfit, METH_VARARGS },
        { "findmax", audioop_findmax, METH_VARARGS },
        { "findfactor", audioop_findfactor, METH_VARARGS },
        { "cross", audioop_cross, METH_VARARGS },
        { "mul", audioop_mul, METH_VARARGS },
        { "add", audioop_add, METH_VARARGS },
        { "bias", audioop_bias, METH_VARARGS },
        { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
        { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
        { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
        { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
        { "lin2lin", audioop_lin2lin, METH_VARARGS },
        { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
        { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
        { "tomono", audioop_tomono, METH_VARARGS },
        { "tostereo", audioop_tostereo, METH_VARARGS },
        { "getsample", audioop_getsample, METH_VARARGS },
        { "reverse", audioop_reverse, METH_VARARGS },
        { "ratecv", audioop_ratecv, METH_VARARGS },
        { 0,          0 }
};

PyMODINIT_FUNC
initaudioop(void)
{
        PyObject *m, *d;
        m = Py_InitModule("audioop", audioop_methods);
        if (m == NULL)
                return;
        d = PyModule_GetDict(m);
        if (d == NULL)
                return;
        AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
        if (AudioopError != NULL)
             PyDict_SetItemString(d,"error",AudioopError);
}
l opt">, H5T_conv_llong_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ullong_int", native_ullong, native_int, H5T_conv_ullong_int, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ullong_uint", native_ullong, native_uint, H5T_conv_ullong_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "llong_schar", native_llong, native_schar, H5T_conv_llong_schar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "llong_uchar", native_llong, native_uchar, H5T_conv_llong_uchar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ullong_schar", native_ullong, native_schar, H5T_conv_ullong_schar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ullong_uchar", native_ullong, native_uchar, H5T_conv_ullong_uchar, H5AC_dxpl_id); /* From long */ status |= H5T_register(H5T_PERS_HARD, "long_llong", native_long, native_llong, H5T_conv_long_llong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "long_ullong", native_long, native_ullong, H5T_conv_long_ullong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_llong", native_ulong, native_llong, H5T_conv_ulong_llong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_ullong", native_ulong, native_ullong, H5T_conv_ulong_ullong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "long_ulong", native_long, native_ulong, H5T_conv_long_ulong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_long", native_ulong, native_long, H5T_conv_ulong_long, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "long_short", native_long, native_short, H5T_conv_long_short, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "long_ushort", native_long, native_ushort, H5T_conv_long_ushort, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_short", native_ulong, native_short, H5T_conv_ulong_short, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_ushort", native_ulong, native_ushort, H5T_conv_ulong_ushort, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "long_int", native_long, native_int, H5T_conv_long_int, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "long_uint", native_long, native_uint, H5T_conv_long_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_int", native_ulong, native_int, H5T_conv_ulong_int, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_uint", native_ulong, native_uint, H5T_conv_ulong_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "long_schar", native_long, native_schar, H5T_conv_long_schar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "long_uchar", native_long, native_uchar, H5T_conv_long_uchar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_schar", native_ulong, native_schar, H5T_conv_ulong_schar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_uchar", native_ulong, native_uchar, H5T_conv_ulong_uchar, H5AC_dxpl_id); /* From short */ status |= H5T_register(H5T_PERS_HARD, "short_llong", native_short, native_llong, H5T_conv_short_llong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "short_ullong", native_short, native_ullong, H5T_conv_short_ullong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_llong", native_ushort, native_llong, H5T_conv_ushort_llong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_ullong", native_ushort, native_ullong, H5T_conv_ushort_ullong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "short_long", native_short, native_long, H5T_conv_short_long, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "short_ulong", native_short, native_ulong, H5T_conv_short_ulong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_long", native_ushort, native_long, H5T_conv_ushort_long, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_ulong", native_ushort, native_ulong, H5T_conv_ushort_ulong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "short_ushort", native_short, native_ushort, H5T_conv_short_ushort, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_short", native_ushort, native_short, H5T_conv_ushort_short, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "short_int", native_short, native_int, H5T_conv_short_int, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "short_uint", native_short, native_uint, H5T_conv_short_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_int", native_ushort, native_int, H5T_conv_ushort_int, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_uint", native_ushort, native_uint, H5T_conv_ushort_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "short_schar", native_short, native_schar, H5T_conv_short_schar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "short_uchar", native_short, native_uchar, H5T_conv_short_uchar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_schar", native_ushort, native_schar, H5T_conv_ushort_schar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_uchar", native_ushort, native_uchar, H5T_conv_ushort_uchar, H5AC_dxpl_id); /* From int */ status |= H5T_register(H5T_PERS_HARD, "int_llong", native_int, native_llong, H5T_conv_int_llong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "int_ullong", native_int, native_ullong, H5T_conv_int_ullong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_llong", native_uint, native_llong, H5T_conv_uint_llong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_ullong", native_uint, native_ullong, H5T_conv_uint_ullong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "int_long", native_int, native_long, H5T_conv_int_long, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "int_ulong", native_int, native_ulong, H5T_conv_int_ulong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_long", native_uint, native_long, H5T_conv_uint_long, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_ulong", native_uint, native_ulong, H5T_conv_uint_ulong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "int_short", native_int, native_short, H5T_conv_int_short, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "int_ushort", native_int, native_ushort, H5T_conv_int_ushort, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_short", native_uint, native_short, H5T_conv_uint_short, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_ushort", native_uint, native_ushort, H5T_conv_uint_ushort, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "int_uint", native_int, native_uint, H5T_conv_int_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_int", native_uint, native_int, H5T_conv_uint_int, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "int_schar", native_int, native_schar, H5T_conv_int_schar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "int_uchar", native_int, native_uchar, H5T_conv_int_uchar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_schar", native_uint, native_schar, H5T_conv_uint_schar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_uchar", native_uint, native_uchar, H5T_conv_uint_uchar, H5AC_dxpl_id); /* From char */ status |= H5T_register(H5T_PERS_HARD, "schar_llong", native_schar, native_llong, H5T_conv_schar_llong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "schar_ullong", native_schar, native_ullong, H5T_conv_schar_ullong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_llong", native_uchar, native_llong, H5T_conv_uchar_llong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_ullong", native_uchar, native_ullong, H5T_conv_uchar_ullong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "schar_long", native_schar, native_long, H5T_conv_schar_long, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "schar_ulong", native_schar, native_ulong, H5T_conv_schar_ulong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_long", native_uchar, native_long, H5T_conv_uchar_long, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_ulong", native_uchar, native_ulong, H5T_conv_uchar_ulong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "schar_short", native_schar, native_short, H5T_conv_schar_short, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "schar_ushort", native_schar, native_ushort, H5T_conv_schar_ushort, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_short", native_uchar, native_short, H5T_conv_uchar_short, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_ushort", native_uchar, native_ushort, H5T_conv_uchar_ushort, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "schar_int", native_schar, native_int, H5T_conv_schar_int, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "schar_uint", native_schar, native_uint, H5T_conv_schar_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_int", native_uchar, native_int, H5T_conv_uchar_int, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_uint", native_uchar, native_uint, H5T_conv_uchar_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "schar_uchar", native_schar, native_uchar, H5T_conv_schar_uchar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_schar", native_uchar, native_schar, H5T_conv_uchar_schar, H5AC_dxpl_id); /* From char to floats */ status |= H5T_register(H5T_PERS_HARD, "schar_flt", native_schar, native_float, H5T_conv_schar_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "schar_dbl", native_schar, native_double, H5T_conv_schar_double, H5AC_dxpl_id); #if H5_SW_INTEGER_TO_LDOUBLE_WORKS status |= H5T_register(H5T_PERS_HARD, "schar_ldbl", native_schar, native_ldouble, H5T_conv_schar_ldouble, H5AC_dxpl_id); #endif /*H5_SW_INTEGER_TO_LDOUBLE_WORKS*/ /* From unsigned char to floats */ status |= H5T_register(H5T_PERS_HARD, "uchar_flt", native_uchar, native_float, H5T_conv_uchar_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uchar_dbl", native_uchar, native_double, H5T_conv_uchar_double, H5AC_dxpl_id); #if H5_SW_INTEGER_TO_LDOUBLE_WORKS status |= H5T_register(H5T_PERS_HARD, "uchar_ldbl", native_uchar, native_ldouble, H5T_conv_uchar_ldouble, H5AC_dxpl_id); #endif /*H5_SW_INTEGER_TO_LDOUBLE_WORKS*/ /* From short to floats */ status |= H5T_register(H5T_PERS_HARD, "short_flt", native_short, native_float, H5T_conv_short_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "short_dbl", native_short, native_double, H5T_conv_short_double, H5AC_dxpl_id); #if H5_SW_INTEGER_TO_LDOUBLE_WORKS status |= H5T_register(H5T_PERS_HARD, "short_ldbl", native_short, native_ldouble, H5T_conv_short_ldouble, H5AC_dxpl_id); #endif /*H5_SW_INTEGER_TO_LDOUBLE_WORKS*/ /* From unsigned short to floats */ status |= H5T_register(H5T_PERS_HARD, "ushort_flt", native_ushort, native_float, H5T_conv_ushort_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ushort_dbl", native_ushort, native_double, H5T_conv_ushort_double, H5AC_dxpl_id); #if H5_SW_INTEGER_TO_LDOUBLE_WORKS status |= H5T_register(H5T_PERS_HARD, "ushort_ldbl", native_ushort, native_ldouble, H5T_conv_ushort_ldouble, H5AC_dxpl_id); #endif /*H5_SW_INTEGER_TO_LDOUBLE_WORKS*/ /* From int to floats */ status |= H5T_register(H5T_PERS_HARD, "int_flt", native_int, native_float, H5T_conv_int_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "int_dbl", native_int, native_double, H5T_conv_int_double, H5AC_dxpl_id); #if H5_SW_INTEGER_TO_LDOUBLE_WORKS status |= H5T_register(H5T_PERS_HARD, "int_ldbl", native_int, native_ldouble, H5T_conv_int_ldouble, H5AC_dxpl_id); #endif /*H5_SW_INTEGER_TO_LDOUBLE_WORKS*/ /* From unsigned int to floats */ status |= H5T_register(H5T_PERS_HARD, "uint_flt", native_uint, native_float, H5T_conv_uint_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "uint_dbl", native_uint, native_double, H5T_conv_uint_double, H5AC_dxpl_id); #if H5_SW_INTEGER_TO_LDOUBLE_WORKS status |= H5T_register(H5T_PERS_HARD, "uint_ldbl", native_uint, native_ldouble, H5T_conv_uint_ldouble, H5AC_dxpl_id); #endif /*H5_SW_INTEGER_TO_LDOUBLE_WORKS*/ /* From long to floats */ status |= H5T_register(H5T_PERS_HARD, "long_flt", native_long, native_float, H5T_conv_long_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "long_dbl", native_long, native_double, H5T_conv_long_double, H5AC_dxpl_id); #if H5_SW_INTEGER_TO_LDOUBLE_WORKS status |= H5T_register(H5T_PERS_HARD, "long_ldbl", native_long, native_ldouble, H5T_conv_long_ldouble, H5AC_dxpl_id); #endif /*H5_SW_INTEGER_TO_LDOUBLE_WORKS*/ /* From unsigned long to floats */ #if H5_SW_ULONG_TO_FP_BOTTOM_BIT_WORKS status |= H5T_register(H5T_PERS_HARD, "ulong_flt", native_ulong, native_float, H5T_conv_ulong_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ulong_dbl", native_ulong, native_double, H5T_conv_ulong_double, H5AC_dxpl_id); #endif /*H5_SW_ULONG_TO_FP_BOTTOM_BIT_WORKS*/ #if H5_SW_ULONG_TO_FP_BOTTOM_BIT_WORKS && H5_SW_INTEGER_TO_LDOUBLE_WORKS status |= H5T_register(H5T_PERS_HARD, "ulong_ldbl", native_ulong, native_ldouble, H5T_conv_ulong_ldouble, H5AC_dxpl_id); #endif /* H5_SW_ULONG_TO_FP_BOTTOM_BIT_WORKS && H5_SW_INTEGER_TO_LDOUBLE_WORKS*/ /* From long long to floats */ status |= H5T_register(H5T_PERS_HARD, "llong_flt", native_llong, native_float, H5T_conv_llong_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "llong_dbl", native_llong, native_double, H5T_conv_llong_double, H5AC_dxpl_id); #if H5_SW_INTEGER_TO_LDOUBLE_WORKS status |= H5T_register(H5T_PERS_HARD, "llong_ldbl", native_llong, native_ldouble, H5T_conv_llong_ldouble, H5AC_dxpl_id); #endif /*H5_SW_INTEGER_TO_LDOUBLE_WORKS*/ /* From unsigned long long to floats */ #if H5_ULLONG_TO_FP_CAST_WORKS && H5_SW_ULONG_TO_FP_BOTTOM_BIT_WORKS status |= H5T_register(H5T_PERS_HARD, "ullong_flt", native_ullong, native_float, H5T_conv_ullong_float, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "ullong_dbl", native_ullong, native_double, H5T_conv_ullong_double, H5AC_dxpl_id); #endif /*H5_ULLONG_TO_FP_CAST_WORKS && H5_SW_ULONG_TO_FP_BOTTOM_BIT_WORKS*/ #if H5_ULLONG_TO_FP_CAST_WORKS && H5_SW_ULONG_TO_FP_BOTTOM_BIT_WORKS && H5_SW_INTEGER_TO_LDOUBLE_WORKS && H5_ULLONG_TO_LDOUBLE_PRECISION_WORKS status |= H5T_register(H5T_PERS_HARD, "ullong_ldbl", native_ullong, native_ldouble, H5T_conv_ullong_ldouble, H5AC_dxpl_id); #endif /*H5_ULLONG_TO_FP_CAST_WORKS && H5_SW_ULONG_TO_FP_BOTTOM_BIT_WORKS && H5_SW_INTEGER_TO_LDOUBLE_WORKS && H5_ULLONG_TO_LDOUBLE_PRECISION_WORKS*/ /* From floats to char */ status |= H5T_register(H5T_PERS_HARD, "flt_schar", native_float, native_schar, H5T_conv_float_schar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_schar", native_double, native_schar, H5T_conv_double_schar, H5AC_dxpl_id); #if H5_SW_LDOUBLE_TO_INTEGER_WORKS status |= H5T_register(H5T_PERS_HARD, "ldbl_schar", native_ldouble, native_schar, H5T_conv_ldouble_schar, H5AC_dxpl_id); #endif /*H5_SW_LDOUBLE_TO_INTEGER_WORKS*/ /* From floats to unsigned char */ status |= H5T_register(H5T_PERS_HARD, "flt_uchar", native_float, native_uchar, H5T_conv_float_uchar, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_uchar", native_double, native_uchar, H5T_conv_double_uchar, H5AC_dxpl_id); #if H5_SW_LDOUBLE_TO_INTEGER_WORKS status |= H5T_register(H5T_PERS_HARD, "ldbl_uchar", native_ldouble, native_uchar, H5T_conv_ldouble_uchar, H5AC_dxpl_id); #endif /*H5_SW_LDOUBLE_TO_INTEGER_WORKS*/ /* From floats to short */ status |= H5T_register(H5T_PERS_HARD, "flt_short", native_float, native_short, H5T_conv_float_short, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_short", native_double, native_short, H5T_conv_double_short, H5AC_dxpl_id); #if H5_SW_LDOUBLE_TO_INTEGER_WORKS status |= H5T_register(H5T_PERS_HARD, "ldbl_short", native_ldouble, native_short, H5T_conv_ldouble_short, H5AC_dxpl_id); #endif /*H5_SW_LDOUBLE_TO_INTEGER_WORKS*/ /* From floats to unsigned short */ status |= H5T_register(H5T_PERS_HARD, "flt_ushort", native_float, native_ushort, H5T_conv_float_ushort, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_ushort", native_double, native_ushort, H5T_conv_double_ushort, H5AC_dxpl_id); #if H5_SW_LDOUBLE_TO_INTEGER_WORKS status |= H5T_register(H5T_PERS_HARD, "ldbl_ushort", native_ldouble, native_ushort, H5T_conv_ldouble_ushort, H5AC_dxpl_id); #endif /*H5_SW_LDOUBLE_TO_INTEGER_WORKS*/ /* From floats to int */ status |= H5T_register(H5T_PERS_HARD, "flt_int", native_float, native_int, H5T_conv_float_int, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_int", native_double, native_int, H5T_conv_double_int, H5AC_dxpl_id); #if H5_SW_LDOUBLE_TO_INTEGER_WORKS status |= H5T_register(H5T_PERS_HARD, "ldbl_int", native_ldouble, native_int, H5T_conv_ldouble_int, H5AC_dxpl_id); #endif /*H5_SW_LDOUBLE_TO_INTEGER_WORKS*/ /* From floats to unsigned int */ status |= H5T_register(H5T_PERS_HARD, "flt_uint", native_float, native_uint, H5T_conv_float_uint, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_uint", native_double, native_uint, H5T_conv_double_uint, H5AC_dxpl_id); #if H5_SW_LDOUBLE_TO_INTEGER_WORKS && H5_CV_LDOUBLE_TO_UINT_WORKS status |= H5T_register(H5T_PERS_HARD, "ldbl_uint", native_ldouble, native_uint, H5T_conv_ldouble_uint, H5AC_dxpl_id); #endif /*H5_SW_LDOUBLE_TO_INTEGER_WORKS && H5_CV_LDOUBLE_TO_UINT_WORKS*/ status |= H5T_register(H5T_PERS_HARD, "flt_long", native_float, native_long, H5T_conv_float_long, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_long", native_double, native_long, H5T_conv_double_long, H5AC_dxpl_id); #if H5_SW_LDOUBLE_TO_INTEGER_WORKS status |= H5T_register(H5T_PERS_HARD, "ldbl_long", native_ldouble, native_long, H5T_conv_ldouble_long, H5AC_dxpl_id); #endif /*H5_SW_LDOUBLE_TO_INTEGER_WORKS*/ /* From floats to unsigned long */ status |= H5T_register(H5T_PERS_HARD, "flt_ulong", native_float, native_ulong, H5T_conv_float_ulong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_ulong", native_double, native_ulong, H5T_conv_double_ulong, H5AC_dxpl_id); #if H5_SW_LDOUBLE_TO_INTEGER_WORKS status |= H5T_register(H5T_PERS_HARD, "ldbl_ulong", native_ldouble, native_ulong, H5T_conv_ldouble_ulong, H5AC_dxpl_id); #endif /*H5_SW_LDOUBLE_TO_INTEGER_WORKS*/ /* From floats to long long */ #ifndef H5_HW_FP_TO_LLONG_NOT_WORKS status |= H5T_register(H5T_PERS_HARD, "flt_llong", native_float, native_llong, H5T_conv_float_llong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_llong", native_double, native_llong, H5T_conv_double_llong, H5AC_dxpl_id); #endif /*H5_HW_FP_TO_LLONG_NOT_WORKS*/ #if H5_SW_LDOUBLE_TO_INTEGER_WORKS && !H5_HW_FP_TO_LLONG_NOT_WORKS status |= H5T_register(H5T_PERS_HARD, "ldbl_llong", native_ldouble, native_llong, H5T_conv_ldouble_llong, H5AC_dxpl_id); #endif /*H5_SW_LDOUBLE_TO_INTEGER_WORKS && !H5_HW_FP_TO_LLONG_NOT_WORKS*/ /* From floats to unsigned long long */ #if H5_FP_TO_ULLONG_BOTTOM_BIT_WORKS && H5_FP_TO_ULLONG_RIGHT_MAXIMUM status |= H5T_register(H5T_PERS_HARD, "flt_ullong", native_float, native_ullong, H5T_conv_float_ullong, H5AC_dxpl_id); status |= H5T_register(H5T_PERS_HARD, "dbl_ullong", native_double, native_ullong, H5T_conv_double_ullong, H5AC_dxpl_id); #endif /*H5_FP_TO_ULLONG_BOTTOM_BIT_WORKS && H5_FP_TO_ULLONG_RIGHT_MAXIMUM*/ #if H5_FP_TO_ULLONG_BOTTOM_BIT_WORKS && H5_SW_LDOUBLE_TO_INTEGER_WORKS && H5_FP_TO_ULLONG_RIGHT_MAXIMUM status |= H5T_register(H5T_PERS_HARD, "ldbl_ullong", native_ldouble, native_ullong, H5T_conv_ldouble_ullong, H5AC_dxpl_id); #endif /*H5_FP_TO_ULLONG_BOTTOM_BIT_WORKS && H5_SW_LDOUBLE_TO_INTEGER_WORKS && H5_FP_TO_ULLONG_RIGHT_MAXIMUM*/ /* * The special no-op conversion is the fastest, so we list it last. The * data types we use are not important as long as the source and * destination are equal. */ status |= H5T_register(H5T_PERS_HARD, "no-op", native_int, native_int, H5T_conv_noop, H5AC_dxpl_id); /* Initialize the +/- Infinity values for floating-point types */ status |= H5T_init_inf(); if (status<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to register conversion function(s)"); done: /* General cleanup */ if (compound!=NULL) H5T_close(compound); if (enum_type!=NULL) H5T_close(enum_type); if (vlen!=NULL) H5T_close(vlen); if (array!=NULL) H5T_close(array); /* Error cleanup */ if(ret_value<0) { if(dt!=NULL) { /* Check if we should call H5T_close or H5FL_FREE */ if(copied_dtype) H5T_close(dt); else { H5FL_FREE(H5T_shared_t, dt->shared); H5FL_FREE(H5T_t,dt); } } /* end if */ } /* end if */ FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_unlock_cb * * Purpose: Clear the immutable flag for a data type. This function is * called when the library is closing in order to unlock all * registered data types and thus make them free-able. * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Monday, April 27, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static int H5T_unlock_cb (void *_dt, hid_t UNUSED id, void UNUSED *key) { H5T_t *dt = (H5T_t *)_dt; FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5T_unlock_cb); assert (dt); if (H5T_STATE_IMMUTABLE==dt->shared->state) dt->shared->state = H5T_STATE_RDONLY; FUNC_LEAVE_NOAPI(SUCCEED); } /*------------------------------------------------------------------------- * Function: H5T_term_interface * * Purpose: Close this interface. * * Return: Success: Positive if any action might have caused a * change in some other interface; zero * otherwise. * * Failure: Negative * * Programmer: Robb Matzke * Friday, November 20, 1998 * * Modifications: * Robb Matzke, 1998-06-11 * Statistics are only printed for conversion functions that were * called. *------------------------------------------------------------------------- */ int H5T_term_interface(void) { int i, nprint=0, n=0; H5T_path_t *path = NULL; FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5T_term_interface); if (H5_interface_initialize_g) { /* Unregister all conversion functions */ for (i=0; i<H5T_g.npaths; i++) { path = H5T_g.path[i]; assert (path); if (path->func) { H5T_print_stats(path, &nprint/*in,out*/); path->cdata.command = H5T_CONV_FREE; if ((path->func)(FAIL, FAIL, &(path->cdata), (size_t)0, (size_t)0, (size_t)0, NULL, NULL,H5AC_dxpl_id)<0) { #ifdef H5T_DEBUG if (H5DEBUG(T)) { fprintf (H5DEBUG(T), "H5T: conversion function " "0x%08lx failed to free private data for " "%s (ignored)\n", (unsigned long)(path->func), path->name); } #endif H5E_clear_stack(NULL); /*ignore the error*/ } } if(path->src) H5T_close (path->src); if(path->dst) H5T_close (path->dst); H5FL_FREE(H5T_path_t,path); H5T_g.path[i] = NULL; } /* Clear conversion tables */ H5T_g.path = H5MM_xfree(H5T_g.path); H5T_g.npaths = H5T_g.apaths = 0; H5T_g.soft = H5MM_xfree(H5T_g.soft); H5T_g.nsoft = H5T_g.asoft = 0; /* Unlock all datatypes, then free them */ H5I_search (H5I_DATATYPE, H5T_unlock_cb, NULL); H5I_dec_type_ref(H5I_DATATYPE); /* Reset all the datatype IDs */ H5T_IEEE_F32BE_g = FAIL; H5T_IEEE_F32LE_g = FAIL; H5T_IEEE_F64BE_g = FAIL; H5T_IEEE_F64LE_g = FAIL; H5T_STD_I8BE_g = FAIL; H5T_STD_I8LE_g = FAIL; H5T_STD_I16BE_g = FAIL; H5T_STD_I16LE_g = FAIL; H5T_STD_I32BE_g = FAIL; H5T_STD_I32LE_g = FAIL; H5T_STD_I64BE_g = FAIL; H5T_STD_I64LE_g = FAIL; H5T_STD_U8BE_g = FAIL; H5T_STD_U8LE_g = FAIL; H5T_STD_U16BE_g = FAIL; H5T_STD_U16LE_g = FAIL; H5T_STD_U32BE_g = FAIL; H5T_STD_U32LE_g = FAIL; H5T_STD_U64BE_g = FAIL; H5T_STD_U64LE_g = FAIL; H5T_STD_B8BE_g = FAIL; H5T_STD_B8LE_g = FAIL; H5T_STD_B16BE_g = FAIL; H5T_STD_B16LE_g = FAIL; H5T_STD_B32BE_g = FAIL; H5T_STD_B32LE_g = FAIL; H5T_STD_B64BE_g = FAIL; H5T_STD_B64LE_g = FAIL; H5T_STD_REF_OBJ_g = FAIL; H5T_STD_REF_DSETREG_g = FAIL; H5T_UNIX_D32BE_g = FAIL; H5T_UNIX_D32LE_g = FAIL; H5T_UNIX_D64BE_g = FAIL; H5T_UNIX_D64LE_g = FAIL; H5T_C_S1_g = FAIL; H5T_FORTRAN_S1_g = FAIL; H5T_NATIVE_SCHAR_g = FAIL; H5T_NATIVE_UCHAR_g = FAIL; H5T_NATIVE_SHORT_g = FAIL; H5T_NATIVE_USHORT_g = FAIL; H5T_NATIVE_INT_g = FAIL; H5T_NATIVE_UINT_g = FAIL; H5T_NATIVE_LONG_g = FAIL; H5T_NATIVE_ULONG_g = FAIL; H5T_NATIVE_LLONG_g = FAIL; H5T_NATIVE_ULLONG_g = FAIL; H5T_NATIVE_FLOAT_g = FAIL; H5T_NATIVE_DOUBLE_g = FAIL; H5T_NATIVE_LDOUBLE_g = FAIL; H5T_NATIVE_B8_g = FAIL; H5T_NATIVE_B16_g = FAIL; H5T_NATIVE_B32_g = FAIL; H5T_NATIVE_B64_g = FAIL; H5T_NATIVE_OPAQUE_g = FAIL; H5T_NATIVE_HADDR_g = FAIL; H5T_NATIVE_HSIZE_g = FAIL; H5T_NATIVE_HSSIZE_g = FAIL; H5T_NATIVE_HERR_g = FAIL; H5T_NATIVE_HBOOL_g = FAIL; H5T_NATIVE_INT8_g = FAIL; H5T_NATIVE_UINT8_g = FAIL; H5T_NATIVE_INT_LEAST8_g = FAIL; H5T_NATIVE_UINT_LEAST8_g = FAIL; H5T_NATIVE_INT_FAST8_g = FAIL; H5T_NATIVE_UINT_FAST8_g = FAIL; H5T_NATIVE_INT16_g = FAIL; H5T_NATIVE_UINT16_g = FAIL; H5T_NATIVE_INT_LEAST16_g = FAIL; H5T_NATIVE_UINT_LEAST16_g = FAIL; H5T_NATIVE_INT_FAST16_g = FAIL; H5T_NATIVE_UINT_FAST16_g = FAIL; H5T_NATIVE_INT32_g = FAIL; H5T_NATIVE_UINT32_g = FAIL; H5T_NATIVE_INT_LEAST32_g = FAIL; H5T_NATIVE_UINT_LEAST32_g = FAIL; H5T_NATIVE_INT_FAST32_g = FAIL; H5T_NATIVE_UINT_FAST32_g = FAIL; H5T_NATIVE_INT64_g = FAIL; H5T_NATIVE_UINT64_g = FAIL; H5T_NATIVE_INT_LEAST64_g = FAIL; H5T_NATIVE_UINT_LEAST64_g = FAIL; H5T_NATIVE_INT_FAST64_g = FAIL; H5T_NATIVE_UINT_FAST64_g = FAIL; /* Mark interface as closed */ H5_interface_initialize_g = 0; n = 1; /*H5I*/ } FUNC_LEAVE_NOAPI(n); } /*------------------------------------------------------------------------- * Function: H5Tcreate * * Purpose: Create a new type and initialize it to reasonable values. * The type is a member of type class TYPE and is SIZE bytes. * * Return: Success: A new type identifier. * * Failure: Negative * * Errors: * ARGS BADVALUE Invalid size. * DATATYPE CANTINIT Can't create type. * DATATYPE CANTREGISTER Can't register data type atom. * * Programmer: Robb Matzke * Friday, December 5, 1997 * * Modifications: * *------------------------------------------------------------------------- */ hid_t H5Tcreate(H5T_class_t type, size_t size) { H5T_t *dt = NULL; hid_t ret_value; FUNC_ENTER_API(H5Tcreate, FAIL); H5TRACE2("i","Ttz",type,size); /* check args */ if (size == 0) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid size"); /* create the type */ if (NULL == (dt = H5T_create(type, size))) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to create type"); /* Make it an atom */ if ((ret_value = H5I_register(H5I_DATATYPE, dt)) < 0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, FAIL, "unable to register data type atom"); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Topen * * Purpose: Opens a named data type. * * Return: Success: Object ID of the named data type. * * Failure: Negative * * Programmer: Robb Matzke * Monday, June 1, 1998 * * Modifications: * *------------------------------------------------------------------------- */ hid_t H5Topen(hid_t loc_id, const char *name) { H5T_t *type = NULL; H5G_entry_t *loc = NULL; H5G_entry_t ent; hid_t dxpl_id = H5AC_dxpl_id; /* dxpl to use to open datatype */ hid_t ret_value =FAIL; FUNC_ENTER_API(H5Topen, FAIL); H5TRACE2("i","is",loc_id,name); /* Check args */ if (NULL==(loc=H5G_loc (loc_id))) HGOTO_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a location"); if (!name || !*name) HGOTO_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL, "no name"); /* * Find the named data type object header and read the data type message * from it. */ if (H5G_find (loc, name, NULL, &ent/*out*/, dxpl_id)<0) HGOTO_ERROR (H5E_DATATYPE, H5E_NOTFOUND, FAIL, "not found"); /* Check that the object found is the correct type */ if (H5G_get_type(&ent, dxpl_id) != H5G_TYPE) HGOTO_ERROR(H5E_DATASET, H5E_BADTYPE, FAIL, "not a named datatype") /* Open it */ if ((type=H5T_open (&ent, dxpl_id)) ==NULL) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTOPENOBJ, FAIL, "unable to open named data type"); /* Register the type and return the ID */ if ((ret_value=H5I_register (H5I_DATATYPE, type))<0) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTREGISTER, FAIL, "unable to register named data type"); done: if(ret_value<0) if(type!=NULL) H5T_close(type); FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tcopy * * Purpose: Copies a data type. The resulting data type is not locked. * The data type should be closed when no longer needed by * calling H5Tclose(). * * Return: Success: The ID of a new data type. * * Failure: Negative * * Programmer: Robb Matzke * Tuesday, December 9, 1997 * * Modifications: * * Robb Matzke, 4 Jun 1998 * The returned type is always transient and unlocked. If the TYPE_ID * argument is a dataset instead of a data type then this function * returns a transient, modifiable data type which is a copy of the * dataset's data type. * *------------------------------------------------------------------------- */ hid_t H5Tcopy(hid_t type_id) { H5T_t *dt = NULL; H5T_t *new_dt = NULL; H5D_t *dset = NULL; hid_t ret_value; FUNC_ENTER_API(H5Tcopy, FAIL); H5TRACE1("i","i",type_id); switch (H5I_get_type (type_id)) { case H5I_DATATYPE: /* The argument is a data type handle */ if (NULL==(dt=H5I_object (type_id))) HGOTO_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); break; case H5I_DATASET: /* The argument is a dataset handle */ if (NULL==(dset=H5I_object (type_id))) HGOTO_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a dataset"); if (NULL==(dt=H5D_typeof (dset))) HGOTO_ERROR (H5E_DATASET, H5E_CANTINIT, FAIL, "unable to get the dataset data type"); break; default: HGOTO_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type or dataset"); } /* end switch */ /* Copy */ if (NULL == (new_dt = H5T_copy(dt, H5T_COPY_TRANSIENT))) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to copy"); /* Atomize result */ if ((ret_value = H5I_register(H5I_DATATYPE, new_dt)) < 0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, FAIL, "unable to register data type atom"); done: if(ret_value<0) { if(new_dt!=NULL) H5T_close(new_dt); } /* end if */ FUNC_LEAVE_API(ret_value); } /* end H5Tcopy() */ /*------------------------------------------------------------------------- * Function: H5Tclose * * Purpose: Frees a data type and all associated memory. * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Tuesday, December 9, 1997 * * Modifications: * *------------------------------------------------------------------------- */ herr_t H5Tclose(hid_t type_id) { H5T_t *dt = NULL; herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_API(H5Tclose, FAIL); H5TRACE1("e","i",type_id); /* Check args */ if (NULL == (dt = H5I_object_verify(type_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); if (H5T_STATE_IMMUTABLE==dt->shared->state) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "immutable data type"); /* When the reference count reaches zero the resources are freed */ if (H5I_dec_ref(type_id) < 0) HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "problem freeing id"); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tequal * * Purpose: Determines if two data types are equal. * * Return: Success: TRUE if equal, FALSE if unequal * * Failure: Negative * * Errors: * * Programmer: Robb Matzke * Wednesday, December 10, 1997 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5Tequal(hid_t type1_id, hid_t type2_id) { const H5T_t *dt1 = NULL; const H5T_t *dt2 = NULL; htri_t ret_value; FUNC_ENTER_API(H5Tequal, FAIL); H5TRACE2("t","ii",type1_id,type2_id); /* check args */ if (NULL == (dt1 = H5I_object_verify(type1_id,H5I_DATATYPE)) || NULL == (dt2 = H5I_object_verify(type2_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); ret_value = (0 == H5T_cmp(dt1, dt2, FALSE)) ? TRUE : FALSE; done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tlock * * Purpose: Locks a type, making it read only and non-destructable. This * is normally done by the library for predefined data types so * the application doesn't inadvertently change or delete a * predefined type. * * Once a data type is locked it can never be unlocked unless * the entire library is closed. * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Friday, January 9, 1998 * * Modifications: * * Robb Matzke, 1 Jun 1998 * It is illegal to lock a named data type since we must allow named * types to be closed (to release file resources) but locking a type * prevents that. *------------------------------------------------------------------------- */ herr_t H5Tlock(hid_t type_id) { H5T_t *dt = NULL; herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_API(H5Tlock, FAIL); H5TRACE1("e","i",type_id); /* Check args */ if (NULL == (dt = H5I_object_verify(type_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); if (H5T_STATE_NAMED==dt->shared->state || H5T_STATE_OPEN==dt->shared->state) HGOTO_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL, "unable to lock named data type"); if (H5T_lock (dt, TRUE)<0) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to lock transient data type"); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tget_class * * Purpose: Returns the data type class identifier for data type TYPE_ID. * * Return: Success: One of the non-negative data type class * constants. * * Failure: H5T_NO_CLASS (Negative) * * Programmer: Robb Matzke * Monday, December 8, 1997 * * Modifications: * *------------------------------------------------------------------------- */ H5T_class_t H5Tget_class(hid_t type_id) { H5T_t *dt = NULL; H5T_class_t ret_value; /* Return value */ FUNC_ENTER_API(H5Tget_class, H5T_NO_CLASS); H5TRACE1("Tt","i",type_id); /* Check args */ if (NULL == (dt = H5I_object_verify(type_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5T_NO_CLASS, "not a data type"); /* Set return value */ ret_value= H5T_get_class(dt, FALSE); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_get_class * * Purpose: Returns the data type class identifier for a datatype ptr. * * Return: Success: One of the non-negative data type class * constants. * * Failure: H5T_NO_CLASS (Negative) * * Programmer: Robb Matzke * Monday, December 8, 1997 * * Modifications: * Broke out from H5Tget_class - QAK - 6/4/99 * *------------------------------------------------------------------------- */ H5T_class_t H5T_get_class(const H5T_t *dt, htri_t internal) { H5T_class_t ret_value; FUNC_ENTER_NOAPI(H5T_get_class, H5T_NO_CLASS); assert(dt); /* Lie to the user if they have a VL string and tell them it's in the string class */ if(dt->shared->type==H5T_VLEN && dt->shared->u.vlen.type==H5T_VLEN_STRING) ret_value=H5T_STRING; else ret_value=dt->shared->type; /* Externally, a VL string is a string; internally, a VL string is a VL. */ if(internal) { ret_value=dt->shared->type; } else { if(H5T_IS_VL_STRING(dt->shared)) ret_value=H5T_STRING; else ret_value=dt->shared->type; } done: FUNC_LEAVE_NOAPI(ret_value); } /* end H5T_get_class() */ /*------------------------------------------------------------------------- * Function: H5Tdetect_class * * Purpose: Check whether a datatype contains (or is) a certain type of * datatype. * * Return: TRUE (1) or FALSE (0) on success/Negative on failure * * Programmer: Quincey Koziol * Wednesday, November 29, 2000 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5Tdetect_class(hid_t type, H5T_class_t cls) { H5T_t *dt = NULL; htri_t ret_value; /* Return value */ FUNC_ENTER_API(H5Tdetect_class, FAIL); H5TRACE2("t","iTt",type,cls); /* Check args */ if (NULL == (dt = H5I_object_verify(type,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5T_NO_CLASS, "not a data type"); if (!(cls>H5T_NO_CLASS && cls<H5T_NCLASSES)) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, H5T_NO_CLASS, "not a data type class"); /* Set return value. Consider VL string as a string for API, as a VL for * internal use. */ if(H5T_IS_VL_STRING(dt->shared)) ret_value = (H5T_STRING==cls); else ret_value=H5T_detect_class(dt,cls); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_detect_class * * Purpose: Check whether a datatype contains (or is) a certain type of * datatype. * * Return: TRUE (1) or FALSE (0) on success/Negative on failure * * Programmer: Quincey Koziol * Wednesday, November 29, 2000 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5T_detect_class (const H5T_t *dt, H5T_class_t cls) { unsigned i; htri_t ret_value=FALSE; /* Return value */ FUNC_ENTER_NOAPI(H5T_detect_class, FAIL); assert(dt); assert(cls>H5T_NO_CLASS && cls<H5T_NCLASSES); /* Check if this type is the correct type */ if(dt->shared->type==cls) HGOTO_DONE(TRUE); /* check for types that might have the correct type as a component */ switch(dt->shared->type) { case H5T_COMPOUND: for (i=0; i<dt->shared->u.compnd.nmembs; i++) { htri_t nested_ret; /* Return value from nested call */ /* Check if this field's type is the correct type */ if(dt->shared->u.compnd.memb[i].type->shared->type==cls) HGOTO_DONE(TRUE); /* Recurse if it's VL, compound, enum or array */ if(H5T_IS_COMPLEX(dt->shared->u.compnd.memb[i].type->shared->type)) if((nested_ret=H5T_detect_class(dt->shared->u.compnd.memb[i].type,cls))!=FALSE) HGOTO_DONE(nested_ret); } /* end for */ break; case H5T_ARRAY: case H5T_VLEN: case H5T_ENUM: HGOTO_DONE(H5T_detect_class(dt->shared->parent,cls)); default: break; } /* end if */ done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tis_variable_str * * Purpose: Check whether a datatype is a variable-length string * * Return: TRUE (1) or FALSE (0) on success/Negative on failure * * Programmer: Raymond Lu * November 4, 2002 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5Tis_variable_str(hid_t dtype_id) { H5T_t *dt; /* Datatype to query */ htri_t ret_value; /* Return value */ FUNC_ENTER_API(H5Tis_variable_str, FAIL); H5TRACE1("t","i",dtype_id); /* Check args */ if (NULL == (dt = H5I_object_verify(dtype_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); /* Set return value */ ret_value=H5T_IS_VL_STRING(dt->shared); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tget_size * * Purpose: Determines the total size of a datatype in bytes. * * Return: Success: Size of the datatype in bytes. The size of * datatype is the size of an instance of that * datatype. * * Failure: 0 (valid datatypes are never zero size) * * Programmer: Robb Matzke * Monday, December 8, 1997 * * Modifications: * *------------------------------------------------------------------------- */ size_t H5Tget_size(hid_t type_id) { H5T_t *dt = NULL; size_t ret_value; FUNC_ENTER_API(H5Tget_size, 0); H5TRACE1("z","i",type_id); /* Check args */ if (NULL == (dt = H5I_object_verify(type_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, 0, "not a datatype"); /* size */ ret_value = H5T_get_size(dt); done: FUNC_LEAVE_API(ret_value); } /* end H5Tget_size() */ /*------------------------------------------------------------------------- * Function: H5Tset_size * * Purpose: Sets the total size in bytes for a datatype (this operation * is not permitted on reference datatypes). If the size is * decreased so that the significant bits of the datatype * extend beyond the edge of the new size, then the `offset' * property is decreased toward zero. If the `offset' becomes * zero and the significant bits of the datatype still hang * over the edge of the new size, then the number of significant * bits is decreased. * * Adjusting the size of an H5T_STRING automatically sets the * precision to 8*size. * * All data types have a positive size. * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Wednesday, January 7, 1998 * * Modifications: * Robb Matzke, 22 Dec 1998 * Moved the real work into a private function. * *------------------------------------------------------------------------- */ herr_t H5Tset_size(hid_t type_id, size_t size) { H5T_t *dt = NULL; herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_API(H5Tset_size, FAIL); H5TRACE2("e","iz",type_id,size); /* Check args */ if (NULL == (dt = H5I_object_verify(type_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); if (H5T_STATE_TRANSIENT!=dt->shared->state) HGOTO_ERROR(H5E_ARGS, H5E_CANTINIT, FAIL, "data type is read-only"); if (size <= 0 && size!=H5T_VARIABLE) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "size must be positive"); if (size == H5T_VARIABLE && dt->shared->type!=H5T_STRING) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "only strings may be variable length"); if (H5T_ENUM==dt->shared->type && dt->shared->u.enumer.nmembs>0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "operation not allowed after members are defined"); if (H5T_REFERENCE==dt->shared->type) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "operation not defined for this datatype"); if (size==0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "can't adjust size to 0"); /* Do the work */ if (H5T_set_size(dt, size)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to set size for data type"); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tget_super * * Purpose: Returns the type from which TYPE is derived. In the case of * an enumeration type the return value is an integer type. * * Return: Success: Type ID for base data type. * * Failure: negative * * Programmer: Robb Matzke * Wednesday, December 23, 1998 * * Modifications: * *------------------------------------------------------------------------- */ hid_t H5Tget_super(hid_t type) { H5T_t *dt=NULL, *super=NULL; hid_t ret_value; FUNC_ENTER_API(H5Tget_super, FAIL); H5TRACE1("i","i",type); if (NULL==(dt=H5I_object_verify(type,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); if((super=H5T_get_super(dt))==NULL) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "not a data type"); if ((ret_value=H5I_register(H5I_DATATYPE, super))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, FAIL, "unable to register parent data type"); done: if(ret_value<0) { if(super!=NULL) H5T_close(super); } /* end if */ FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_get_super * * Purpose: Private function for H5Tget_super. Returns the type from * which TYPE is derived. In the case of an enumeration type * the return value is an integer type. * * Return: Success: Data type for base data type. * * Failure: NULL * * Programmer: Raymond Lu * October 9, 2002 * * Modifications: * *------------------------------------------------------------------------- */ H5T_t * H5T_get_super(H5T_t *dt) { H5T_t *ret_value=NULL; FUNC_ENTER_NOAPI(H5T_get_super, NULL); assert(dt); if (!dt->shared->parent) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "not a derived data type"); if (NULL==(ret_value=H5T_copy(dt->shared->parent, H5T_COPY_ALL))) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to copy parent data type"); done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_register * * Purpose: Register a hard or soft conversion function for a data type * conversion path. The path is specified by the source and * destination data types SRC_ID and DST_ID (for soft functions * only the class of these types is important). If FUNC is a * hard function then it replaces any previous path; if it's a * soft function then it replaces all existing paths to which it * applies and is used for any new path to which it applies as * long as that path doesn't have a hard function. * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Friday, January 9, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5T_register(H5T_pers_t pers, const char *name, H5T_t *src, H5T_t *dst, H5T_conv_t func, hid_t dxpl_id) { hid_t tmp_sid=-1, tmp_did=-1;/*temporary data type IDs */ H5T_path_t *old_path=NULL; /*existing conversion path */ H5T_path_t *new_path=NULL; /*new conversion path */ H5T_cdata_t cdata; /*temporary conversion data */ int nprint=0; /*number of paths shut down */ int i; /*counter */ herr_t ret_value=SUCCEED; /*return value */ FUNC_ENTER_NOAPI_NOINIT(H5T_register); /* Check args */ assert(src); assert(dst); assert(func); assert(H5T_PERS_HARD==pers || H5T_PERS_SOFT==pers); assert(name && *name); if (H5T_PERS_HARD==pers) { /* Only bother to register the path if it's not a no-op path (for this machine) */ if(H5T_cmp(src, dst, FALSE)) { /* Locate or create a new conversion path */ if (NULL==(new_path=H5T_path_find(src, dst, name, func, dxpl_id))) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to locate/allocate conversion path"); /* * Notify all other functions to recalculate private data since some * functions might cache a list of conversion functions. For * instance, the compound type converter caches a list of conversion * functions for the members, so adding a new function should cause * the list to be recalculated to use the new function. */ for (i=0; i<H5T_g.npaths; i++) { if (new_path != H5T_g.path[i]) H5T_g.path[i]->cdata.recalc = TRUE; } /* end for */ } /* end if */ } else { /* Add function to end of soft list */ if (H5T_g.nsoft>=H5T_g.asoft) { size_t na = MAX(32, 2*H5T_g.asoft); H5T_soft_t *x = H5MM_realloc(H5T_g.soft, na*sizeof(H5T_soft_t)); if (!x) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed"); H5T_g.asoft = (int)na; H5T_g.soft = x; } /* end if */ HDstrncpy (H5T_g.soft[H5T_g.nsoft].name, name, (size_t)H5T_NAMELEN); H5T_g.soft[H5T_g.nsoft].name[H5T_NAMELEN-1] = '\0'; H5T_g.soft[H5T_g.nsoft].src = src->shared->type; H5T_g.soft[H5T_g.nsoft].dst = dst->shared->type; H5T_g.soft[H5T_g.nsoft].func = func; H5T_g.nsoft++; /* * Any existing path (except the no-op path) to which this new soft * conversion function applies should be replaced by a new path that * uses this function. */ for (i=1; i<H5T_g.npaths; i++) { old_path = H5T_g.path[i]; assert(old_path); /* Does the new soft conversion function apply to this path? */ if (old_path->is_hard || old_path->src->shared->type!=src->shared->type || old_path->dst->shared->type!=dst->shared->type) { continue; } if ((tmp_sid = H5I_register(H5I_DATATYPE, H5T_copy(old_path->src, H5T_COPY_ALL)))<0 || (tmp_did = H5I_register(H5I_DATATYPE, H5T_copy(old_path->dst, H5T_COPY_ALL)))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, FAIL, "unable to register data types for conv query"); HDmemset(&cdata, 0, sizeof cdata); cdata.command = H5T_CONV_INIT; if ((func)(tmp_sid, tmp_did, &cdata, (size_t)0, (size_t)0, (size_t)0, NULL, NULL, dxpl_id)<0) { H5I_dec_ref(tmp_sid); H5I_dec_ref(tmp_did); tmp_sid = tmp_did = -1; H5E_clear_stack(NULL); continue; } /* end if */ /* Create a new conversion path */ if (NULL==(new_path=H5FL_CALLOC(H5T_path_t))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed"); HDstrncpy(new_path->name, name, (size_t)H5T_NAMELEN); new_path->name[H5T_NAMELEN-1] = '\0'; if (NULL==(new_path->src=H5T_copy(old_path->src, H5T_COPY_ALL)) || NULL==(new_path->dst=H5T_copy(old_path->dst, H5T_COPY_ALL))) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to copy data types"); new_path->func = func; new_path->is_hard = FALSE; new_path->cdata = cdata; /* Replace previous path */ H5T_g.path[i] = new_path; new_path = NULL; /*so we don't free it on error*/ /* Free old path */ H5T_print_stats(old_path, &nprint); old_path->cdata.command = H5T_CONV_FREE; if ((old_path->func)(tmp_sid, tmp_did, &(old_path->cdata), (size_t)0, (size_t)0, (size_t)0, NULL, NULL, dxpl_id)<0) { #ifdef H5T_DEBUG if (H5DEBUG(T)) { fprintf (H5DEBUG(T), "H5T: conversion function 0x%08lx " "failed to free private data for %s (ignored)\n", (unsigned long)(old_path->func), old_path->name); } #endif } /* end if */ H5T_close(old_path->src); H5T_close(old_path->dst); H5FL_FREE(H5T_path_t,old_path); /* Release temporary atoms */ H5I_dec_ref(tmp_sid); H5I_dec_ref(tmp_did); tmp_sid = tmp_did = -1; /* We don't care about any failures during the freeing process */ H5E_clear_stack(NULL); } /* end for */ } /* end else */ done: if (ret_value<0) { if (new_path) { if (new_path->src) H5T_close(new_path->src); if (new_path->dst) H5T_close(new_path->dst); H5FL_FREE(H5T_path_t,new_path); } /* end if */ if (tmp_sid>=0) H5I_dec_ref(tmp_sid); if (tmp_did>=0) H5I_dec_ref(tmp_did); } /* end if */ FUNC_LEAVE_NOAPI(ret_value); } /* end H5T_register() */ /*------------------------------------------------------------------------- * Function: H5Tregister * * Purpose: Register a hard or soft conversion function for a data type * conversion path. The path is specified by the source and * destination data types SRC_ID and DST_ID (for soft functions * only the class of these types is important). If FUNC is a * hard function then it replaces any previous path; if it's a * soft function then it replaces all existing paths to which it * applies and is used for any new path to which it applies as * long as that path doesn't have a hard function. * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Friday, January 9, 1998 * * Modifications: * *------------------------------------------------------------------------- */ herr_t H5Tregister(H5T_pers_t pers, const char *name, hid_t src_id, hid_t dst_id, H5T_conv_t func) { H5T_t *src; /*source data type descriptor */ H5T_t *dst; /*destination data type desc */ herr_t ret_value=SUCCEED; /*return value */ FUNC_ENTER_API(H5Tregister, FAIL); H5TRACE5("e","Tesiix",pers,name,src_id,dst_id,func); /* Check args */ if (H5T_PERS_HARD!=pers && H5T_PERS_SOFT!=pers) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid function persistence"); if (!name || !*name) HGOTO_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL, "conversion must have a name for debugging"); if (NULL==(src=H5I_object_verify(src_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); if (NULL==(dst=H5I_object_verify(dst_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); if (!func) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "no conversion function specified"); /* Go register the function */ if(H5T_register(pers,name,src,dst,func,H5AC_ind_dxpl_id)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "can't register conversion function"); done: FUNC_LEAVE_API(ret_value); } /* end H5Tregister() */ /*------------------------------------------------------------------------- * Function: H5T_unregister * * Purpose: Removes conversion paths that match the specified criteria. * All arguments are optional. Missing arguments are wild cards. * The special no-op path cannot be removed. * * Return: Succeess: non-negative * * Failure: negative * * Programmer: Robb Matzke * Tuesday, January 13, 1998 * * Modifications: * Adapted to non-API function - QAK, 11/17/99 * *------------------------------------------------------------------------- */ static herr_t H5T_unregister(H5T_pers_t pers, const char *name, H5T_t *src, H5T_t *dst, H5T_conv_t func, hid_t dxpl_id) { H5T_path_t *path = NULL; /*conversion path */ H5T_soft_t *soft = NULL; /*soft conversion information */ int nprint=0; /*number of paths shut down */ int i; /*counter */ herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5T_unregister, FAIL); /* Remove matching entries from the soft list */ if (H5T_PERS_DONTCARE==pers || H5T_PERS_SOFT==pers) { for (i=H5T_g.nsoft-1; i>=0; --i) { soft = H5T_g.soft+i; assert(soft); if (name && *name && HDstrcmp(name, soft->name)) continue; if (src && src->shared->type!=soft->src) continue; if (dst && dst->shared->type!=soft->dst) continue; if (func && func!=soft->func) continue; HDmemmove(H5T_g.soft+i, H5T_g.soft+i+1, (H5T_g.nsoft-(i+1)) * sizeof(H5T_soft_t)); --H5T_g.nsoft; } } /* Remove matching conversion paths, except no-op path */ for (i=H5T_g.npaths-1; i>0; --i) { path = H5T_g.path[i]; assert(path); /* Not a match */ if (((H5T_PERS_SOFT==pers && path->is_hard) || (H5T_PERS_HARD==pers && !path->is_hard)) || (name && *name && HDstrcmp(name, path->name)) || (src && H5T_cmp(src, path->src, FALSE)) || (dst && H5T_cmp(dst, path->dst, FALSE)) || (func && func!=path->func)) { /* * Notify all other functions to recalculate private data since some * functions might cache a list of conversion functions. For * instance, the compound type converter caches a list of conversion * functions for the members, so removing a function should cause * the list to be recalculated to avoid the removed function. */ path->cdata.recalc = TRUE; } /* end if */ else { /* Remove from table */ HDmemmove(H5T_g.path+i, H5T_g.path+i+1, (H5T_g.npaths-(i+1))*sizeof(H5T_path_t*)); --H5T_g.npaths; /* Shut down path */ H5T_print_stats(path, &nprint); path->cdata.command = H5T_CONV_FREE; if ((path->func)(FAIL, FAIL, &(path->cdata), (size_t)0, (size_t)0, (size_t)0, NULL, NULL, dxpl_id)<0) { #ifdef H5T_DEBUG if (H5DEBUG(T)) { fprintf(H5DEBUG(T), "H5T: conversion function 0x%08lx failed " "to free private data for %s (ignored)\n", (unsigned long)(path->func), path->name); } #endif } H5T_close(path->src); H5T_close(path->dst); H5FL_FREE(H5T_path_t,path); H5E_clear_stack(NULL); /*ignore all shutdown errors*/ } /* end else */ } /* end for */ done: FUNC_LEAVE_NOAPI(ret_value); } /* end H5T_unregister() */ /*------------------------------------------------------------------------- * Function: H5Tunregister * * Purpose: Removes conversion paths that match the specified criteria. * All arguments are optional. Missing arguments are wild cards. * The special no-op path cannot be removed. * * Return: Succeess: non-negative * * Failure: negative * * Programmer: Robb Matzke * Tuesday, January 13, 1998 * * Modifications: * Changed to use H5T_unregister wrapper function - QAK, 11/17/99 * *------------------------------------------------------------------------- */ herr_t H5Tunregister(H5T_pers_t pers, const char *name, hid_t src_id, hid_t dst_id, H5T_conv_t func) { H5T_t *src=NULL, *dst=NULL; /*data type descriptors */ herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_API(H5Tunregister, FAIL); H5TRACE5("e","Tesiix",pers,name,src_id,dst_id,func); /* Check arguments */ if (src_id>0 && (NULL==(src=H5I_object_verify(src_id,H5I_DATATYPE)))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "src is not a data type"); if (dst_id>0 && (NULL==(dst=H5I_object_verify(dst_id,H5I_DATATYPE)))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "dst is not a data type"); if (H5T_unregister(pers,name,src,dst,func,H5AC_ind_dxpl_id)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDELETE, FAIL, "internal unregister function failed"); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tfind * * Purpose: Finds a conversion function that can handle a conversion from * type SRC_ID to type DST_ID. The PCDATA argument is a pointer * to a pointer to type conversion data which was created and * initialized by the type conversion function of this path * when the conversion function was installed on the path. * * Return: Success: A pointer to a suitable conversion function. * * Failure: NULL * * Programmer: Robb Matzke * Tuesday, January 13, 1998 * * Modifications: * *------------------------------------------------------------------------- */ H5T_conv_t H5Tfind(hid_t src_id, hid_t dst_id, H5T_cdata_t **pcdata) { H5T_conv_t ret_value; H5T_t *src = NULL, *dst = NULL; H5T_path_t *path = NULL; FUNC_ENTER_API(H5Tfind, NULL); H5TRACE3("x","iix",src_id,dst_id,pcdata); /* Check args */ if (NULL == (src = H5I_object_verify(src_id,H5I_DATATYPE)) || NULL == (dst = H5I_object_verify(dst_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a data type"); if (!pcdata) HGOTO_ERROR (H5E_ARGS, H5E_BADVALUE, NULL, "no address to receive cdata pointer"); /* Find it */ if (NULL==(path=H5T_path_find(src, dst, NULL, NULL, H5AC_ind_dxpl_id))) HGOTO_ERROR(H5E_DATATYPE, H5E_NOTFOUND, NULL, "conversion function not found"); if (pcdata) *pcdata = &(path->cdata); /* Set return value */ ret_value = path->func; done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tconvert * * Purpose: Convert NELMTS elements from type SRC_ID to type DST_ID. The * source elements are packed in BUF and on return the * destination will be packed in BUF. That is, the conversion * is performed in place. The optional background buffer is an * array of NELMTS values of destination type which are merged * with the converted values to fill in cracks (for instance, * BACKGROUND might be an array of structs with the `a' and `b' * fields already initialized and the conversion of BUF supplies * the `c' and `d' field values). The PLIST_ID a dataset transfer * property list which is passed to the conversion functions. (It's * currently only used to pass along the VL datatype custom allocation * information -QAK 7/1/99) * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Wednesday, June 10, 1998 * * Modifications: * Added xfer_parms argument to pass VL datatype custom allocation * information down the chain. - QAK, 7/1/99 * *------------------------------------------------------------------------- */ herr_t H5Tconvert(hid_t src_id, hid_t dst_id, size_t nelmts, void *buf, void *background, hid_t dxpl_id) { H5T_path_t *tpath=NULL; /*type conversion info */ H5T_t *src=NULL, *dst=NULL; /*unatomized types */ herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_API(H5Tconvert, FAIL); H5TRACE6("e","iizxxi",src_id,dst_id,nelmts,buf,background,dxpl_id); /* Check args */ if (NULL==(src=H5I_object_verify(src_id,H5I_DATATYPE)) || NULL==(dst=H5I_object_verify(dst_id,H5I_DATATYPE))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type"); if(H5P_DEFAULT == dxpl_id) dxpl_id = H5P_DATASET_XFER_DEFAULT; else if(TRUE != H5P_isa_class(dxpl_id, H5P_DATASET_XFER)) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not dataset transfer property list"); /* Find the conversion function */ if (NULL==(tpath=H5T_path_find(src, dst, NULL, NULL, dxpl_id))) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to convert between src and dst data types"); if (H5T_convert(tpath, src_id, dst_id, nelmts, (size_t)0, (size_t)0, buf, background, dxpl_id)<0) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTINIT, FAIL, "data type conversion failed"); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tencode * * Purpose: Given an datatype ID, converts the object description into * binary in a buffer. * * Return: Success: non-negative * * Failure: negative * * Programmer: Raymond Lu * slu@ncsa.uiuc.edu * July 14, 2004 * * Modifications: * *------------------------------------------------------------------------- */ herr_t H5Tencode(hid_t obj_id, void *buf, size_t *nalloc) { H5T_t *dtype; herr_t ret_value=SUCCEED; FUNC_ENTER_API (H5Tencode, FAIL); H5TRACE3("e","ix*z",obj_id,buf,nalloc); /* Check argument and retrieve object */ if (NULL==(dtype=H5I_object_verify(obj_id, H5I_DATATYPE))) HGOTO_ERROR (H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype") if (nalloc==NULL) HGOTO_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL, "NULL pointer for buffer size") if(H5T_encode(dtype, buf, nalloc)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTENCODE, FAIL, "can't encode datatype"); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * Function: H5Tdecode * * Purpose: Decode a binary object description and return a new object * handle. * * Return: Success: datatype ID(non-negative) * * Failure: negative * * Programmer: Raymond Lu * slu@ncsa.uiuc.edu * July 14, 2004 * * Modifications: * *------------------------------------------------------------------------- */ hid_t H5Tdecode(const void *buf) { H5T_t *dt; hid_t ret_value; FUNC_ENTER_API (H5Tdecode, FAIL); H5TRACE1("i","x",buf); if (buf==NULL) HGOTO_ERROR (H5E_ARGS, H5E_BADVALUE, FAIL, "empty buffer") if((dt = H5T_decode(buf))==NULL) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, FAIL, "can't decode object"); /* Register the type and return the ID */ if ((ret_value=H5I_register (H5I_DATATYPE, dt))<0) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTREGISTER, FAIL, "unable to register data type"); done: FUNC_LEAVE_API(ret_value); } /*------------------------------------------------------------------------- * API functions are above; library-private functions are below... *------------------------------------------------------------------------- */ /*------------------------------------------------------------------------- * Function: H5T_encode * * Purpose: Private function for H5Tencode. Converts an object * description into binary in a buffer. * * Return: Success: non-negative * * Failure: negative * * Programmer: Raymond Lu * slu@ncsa.uiuc.edu * July 14, 2004 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5T_encode(H5T_t *obj, unsigned char *buf, size_t *nalloc) { size_t buf_size; H5F_t f; herr_t ret_value = SUCCEED; FUNC_ENTER_NOAPI(H5T_encode, FAIL); /* Find out the size of buffer needed */ if((buf_size=H5O_raw_size(H5O_DTYPE_ID, &f, obj))==0) HGOTO_ERROR(H5E_DATATYPE, H5E_BADSIZE, FAIL, "can't find datatype size"); /* Don't encode if buffer size isn't big enough or buffer is empty */ if(!buf || *nalloc<(buf_size+1+1)) { *nalloc = buf_size+1+1; HGOTO_DONE(ret_value); } /* Encode the type of the information */ *buf++ = H5O_DTYPE_ID; /* Encode the version of the dataspace information */ *buf++ = H5T_ENCODE_VERSION; if(H5O_encode(&f, buf, obj, H5O_DTYPE_ID)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTENCODE, FAIL, "can't encode object"); done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_decode * * Purpose: Private function for H5Tdecode. Reconstructs a binary * description of datatype and returns a new object handle. * * Return: Success: datatype ID(non-negative) * * Failure: negative * * Programmer: Raymond Lu * slu@ncsa.uiuc.edu * July 14, 2004 * * Modifications: * *------------------------------------------------------------------------- */ static H5T_t * H5T_decode(const unsigned char *buf) { H5T_t *ret_value; FUNC_ENTER_NOAPI(H5T_decode, NULL); /* Decode the type of the information */ if(*buf++ != H5O_DTYPE_ID) HGOTO_ERROR(H5E_DATATYPE, H5E_BADMESG, NULL, "not an encoded datatype"); /* Decode the version of the datatype information */ if(*buf++ != H5T_ENCODE_VERSION) HGOTO_ERROR(H5E_DATATYPE, H5E_VERSION, NULL, "unknown version of encoded datatype"); if((ret_value = H5O_decode(NULL, buf, H5O_DTYPE_ID))==NULL) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, NULL, "can't decode object"); done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_create * * Purpose: Creates a new data type and initializes it to reasonable * values. The new data type is SIZE bytes and an instance of * the class TYPE. * * Return: Success: Pointer to the new type. * * Failure: NULL * * Programmer: Robb Matzke * Friday, December 5, 1997 * * Modifications: * *------------------------------------------------------------------------- */ H5T_t * H5T_create(H5T_class_t type, size_t size) { H5T_t *dt = NULL; hid_t subtype; H5T_t *ret_value; FUNC_ENTER_NOAPI(H5T_create, NULL); switch (type) { case H5T_INTEGER: case H5T_FLOAT: case H5T_TIME: case H5T_STRING: case H5T_BITFIELD: HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, NULL, "type class is not appropriate - use H5Tcopy()"); case H5T_OPAQUE: case H5T_COMPOUND: if (NULL==(dt = H5FL_CALLOC(H5T_t))) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); if (NULL==(dt->shared = H5FL_CALLOC(H5T_shared_t))) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); dt->shared->type = type; if(type==H5T_COMPOUND) dt->shared->u.compnd.packed=TRUE; /* Start out packed */ else if(type==H5T_OPAQUE) /* Initialize the tag in case it's not set later. A null tag will * cause problems for later operations. */ dt->shared->u.opaque.tag = H5MM_strdup(""); break; case H5T_ENUM: if (sizeof(char)==size) { subtype = H5T_NATIVE_SCHAR_g; } else if (sizeof(short)==size) { subtype = H5T_NATIVE_SHORT_g; } else if (sizeof(int)==size) { subtype = H5T_NATIVE_INT_g; } else if (sizeof(long)==size) { subtype = H5T_NATIVE_LONG_g; } else if (sizeof(long_long)==size) { subtype = H5T_NATIVE_LLONG_g; } else { HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "no applicable native integer type"); } if (NULL==(dt = H5FL_CALLOC(H5T_t))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); if (NULL==(dt->shared = H5FL_CALLOC(H5T_shared_t))) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); dt->shared->type = type; if (NULL==(dt->shared->parent=H5T_copy(H5I_object(subtype), H5T_COPY_ALL))) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to copy base data type"); break; case H5T_VLEN: /* Variable length datatype */ HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, NULL, "base type required - use H5Tvlen_create()"); case H5T_ARRAY: /* Array datatype */ HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, NULL, "base type required - use H5Tarray_create()"); default: HGOTO_ERROR(H5E_INTERNAL, H5E_UNSUPPORTED, NULL, "unknown data type class"); } dt->ent.header = HADDR_UNDEF; dt->shared->size = size; /* Set return value */ ret_value=dt; done: if(ret_value==NULL) { if(dt->shared != NULL) H5FL_FREE(H5T_shared_t, dt->shared); if(dt!=NULL) H5FL_FREE(H5T_t,dt); } /* end if */ FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_isa * * Purpose: Determines if an object has the requisite messages for being * a data type. * * Return: Success: TRUE if the required data type messages are * present; FALSE otherwise. * * Failure: FAIL if the existence of certain messages * cannot be determined. * * Programmer: Robb Matzke * Monday, November 2, 1998 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5T_isa(H5G_entry_t *ent, hid_t dxpl_id) { htri_t ret_value; FUNC_ENTER_NOAPI(H5T_isa, FAIL); assert(ent); if ((ret_value=H5O_exists(ent, H5O_DTYPE_ID, 0, dxpl_id))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to read object header"); done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_open * * Purpose: Open a named data type. * * Return: Success: Ptr to a new data type. * * Failure: NULL * * Programmer: Robb Matzke * Monday, June 1, 1998 * * Modifications: * Changed to use H5T_open_oid - QAK - 3/17/99 * *------------------------------------------------------------------------- */ H5T_t* H5T_open (H5G_entry_t *ent, hid_t dxpl_id) { H5T_shared_t *shared_fo=NULL; H5T_t *dt=NULL; H5T_t *ret_value; FUNC_ENTER_NOAPI(H5T_open, NULL); assert (ent); /* Check if datatype was already open */ if((shared_fo=H5FO_opened(ent->file,ent->header))==NULL) { /* Clear any errors from H5FO_opened() */ H5E_clear_stack(NULL); /* Open the datatype object */ if ((dt=H5T_open_oid(ent, dxpl_id)) ==NULL) HGOTO_ERROR(H5E_DATATYPE, H5E_NOTFOUND, NULL, "not found"); /* Add the datatype to the list of opened objects in the file */ if(H5FO_insert(ent->file, ent->header, dt->shared)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINSERT, NULL, "can't insert datatype into list of open objects") /* Mark any datatypes as being in memory now */ if (H5T_set_loc(dt, NULL, H5T_LOC_MEMORY)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "invalid datatype location") dt->shared->fo_count=1; } else { shared_fo->fo_count++; if(NULL == (dt = H5FL_MALLOC(H5T_t))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "can't allocate space for datatype") dt->shared=shared_fo; /* Shallow copy (take ownership) of the group entry object */ if(H5G_ent_copy(&(dt->ent),ent,H5G_COPY_SHALLOW)<0) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTCOPY, NULL, "can't copy group entry") } ret_value = dt; done: if(ret_value==NULL) { if(dt) { if(shared_fo==NULL) /* Need to free shared fo */ H5FL_FREE(H5T_shared_t, dt->shared); H5FL_FREE(H5T_t, dt); } if(shared_fo) shared_fo->fo_count--; } FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_open_oid * * Purpose: Open a named data type. * * Return: Success: Ptr to a new data type. * * Failure: NULL * * Programmer: Quincey Koziol * Wednesday, March 17, 1999 * * Modifications: * *------------------------------------------------------------------------- */ H5T_t * H5T_open_oid (H5G_entry_t *ent, hid_t dxpl_id) { H5T_t *dt=NULL; H5T_t *ret_value; FUNC_ENTER_NOAPI(H5T_open_oid, NULL); assert (ent); if (H5O_open (ent)<0) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTOPENOBJ, NULL, "unable to open named data type"); if (NULL==(dt=H5O_read (ent, H5O_DTYPE_ID, 0, NULL, dxpl_id))) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to load type message from object header"); /* Mark the type as named and open */ dt->shared->state = H5T_STATE_OPEN; /* Shallow copy (take ownership) of the group entry object */ H5G_ent_copy(&(dt->ent),ent,H5G_COPY_SHALLOW); /* Set return value */ ret_value=dt; done: if(ret_value==NULL) { if(dt==NULL) H5O_close(ent); } /* end if */ FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_copy * * Purpose: Copies datatype OLD_DT. The resulting data type is not * locked and is a transient type. * * Return: Success: Pointer to a new copy of the OLD_DT argument. * * Failure: NULL * * Programmer: Robb Matzke * Thursday, December 4, 1997 * * Modifications: * * Robb Matzke, 4 Jun 1998 * Added the METHOD argument. If it's H5T_COPY_TRANSIENT then the * result will be an unlocked transient type. Otherwise if it's * H5T_COPY_ALL then the result is a named type if the original is a * named type, but the result is not opened. Finally, if it's * H5T_COPY_REOPEN and the original type is a named type then the result * is a named type and the type object header is opened again. The * H5T_COPY_REOPEN method is used when returning a named type to the * application. * * Robb Matzke, 22 Dec 1998 * Now able to copy enumeration data types. * * Robb Matzke, 20 May 1999 * Now able to copy opaque types. * * Pedro Vicente, <pvn@ncsa.uiuc.edu> 21 Sep 2002 * Added a deep copy of the symbol table entry * *------------------------------------------------------------------------- */ H5T_t * H5T_copy(const H5T_t *old_dt, H5T_copy_t method) { H5T_t *new_dt=NULL, *tmp=NULL; H5T_shared_t *reopened_fo; unsigned i; char *s; H5T_t *ret_value; FUNC_ENTER_NOAPI(H5T_copy, NULL); /* check args */ assert(old_dt); /* Allocate space */ if (NULL==(new_dt = H5FL_MALLOC(H5T_t))) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); if (NULL==(new_dt->shared = H5FL_MALLOC(H5T_shared_t))) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); /* Copy actual information */ new_dt->ent = old_dt->ent; *(new_dt->shared) = *(old_dt->shared); /* Copy parent information */ if (new_dt->shared->parent) new_dt->shared->parent = H5T_copy(new_dt->shared->parent, method); /* Check what sort of copy we are making */ switch (method) { case H5T_COPY_TRANSIENT: /* * Return an unlocked transient type. */ new_dt->shared->state = H5T_STATE_TRANSIENT; HDmemset (&(new_dt->ent), 0, sizeof(new_dt->ent)); new_dt->ent.header = HADDR_UNDEF; break; case H5T_COPY_ALL: /* * Return a transient type (locked or unlocked) or an unopened named * type. Immutable transient types are degraded to read-only. */ if (H5T_STATE_OPEN==new_dt->shared->state) { new_dt->shared->state = H5T_STATE_NAMED; } else if (H5T_STATE_IMMUTABLE==new_dt->shared->state) { new_dt->shared->state = H5T_STATE_RDONLY; } break; case H5T_COPY_REOPEN: /* * Return a transient type (locked or unlocked) or an opened named * type. Immutable transient types are degraded to read-only. */ if (H5F_addr_defined(new_dt->ent.header)) { /* Check if the object is already open */ if((reopened_fo=H5FO_opened(new_dt->ent.file,new_dt->ent.header))==NULL) { /* Clear any errors from H5FO_opened() */ H5E_clear_stack(NULL); if (H5O_open (&(new_dt->ent))<0) HGOTO_ERROR (H5E_DATATYPE, H5E_CANTOPENOBJ, NULL, "unable to reopen named data type"); if(H5FO_insert(new_dt->ent.file, new_dt->ent.header,new_dt->shared)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINSERT, NULL, "can't insert datatype into list of open objects") new_dt->shared->fo_count=1; } else { /* The object is already open. Free the H5T_shared_t struct * we had been using and use the one that already exists. * Not terribly efficient. */ H5FL_FREE(H5T_shared_t, new_dt->shared); new_dt->shared = reopened_fo; reopened_fo->fo_count++; } new_dt->shared->state = H5T_STATE_OPEN; } else if (H5T_STATE_IMMUTABLE==new_dt->shared->state) { new_dt->shared->state = H5T_STATE_RDONLY; } break; } /* end switch */ switch(new_dt->shared->type) { case H5T_COMPOUND: { int accum_change=0; /* Amount of change in the offset of the fields */ /* * Copy all member fields to new type, then overwrite the * name and type fields of each new member with copied values. * That is, H5T_copy() is a deep copy. */ new_dt->shared->u.compnd.memb = H5MM_malloc(new_dt->shared->u.compnd.nalloc * sizeof(H5T_cmemb_t)); if (NULL==new_dt->shared->u.compnd.memb) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); HDmemcpy(new_dt->shared->u.compnd.memb, old_dt->shared->u.compnd.memb, new_dt->shared->u.compnd.nmembs * sizeof(H5T_cmemb_t)); for (i=0; i<new_dt->shared->u.compnd.nmembs; i++) { unsigned j; int old_match; s = new_dt->shared->u.compnd.memb[i].name; new_dt->shared->u.compnd.memb[i].name = H5MM_xstrdup(s); tmp = H5T_copy (old_dt->shared->u.compnd.memb[i].type, method); new_dt->shared->u.compnd.memb[i].type = tmp; /* Apply the accumulated size change to the offset of the field */ new_dt->shared->u.compnd.memb[i].offset += accum_change; if(old_dt->shared->u.compnd.sorted != H5T_SORT_VALUE) { for (old_match=-1, j=0; j<old_dt->shared->u.compnd.nmembs; j++) { if(!HDstrcmp(new_dt->shared->u.compnd.memb[i].name,old_dt->shared->u.compnd.memb[j].name)) { old_match=j; break; } /* end if */ } /* end for */ /* check if we couldn't find a match */ if(old_match<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCOPY, NULL, "fields in datatype corrupted"); } /* end if */ else { old_match=i; } /* end else */ /* If the field changed size, add that change to the accumulated size change */ if(new_dt->shared->u.compnd.memb[i].type->shared->size != old_dt->shared->u.compnd.memb[old_match].type->shared->size) { /* Adjust the size of the member */ new_dt->shared->u.compnd.memb[i].size = (old_dt->shared->u.compnd.memb[old_match].size*tmp->shared->size)/old_dt->shared->u.compnd.memb[old_match].type->shared->size; accum_change += (new_dt->shared->u.compnd.memb[i].type->shared->size - old_dt->shared->u.compnd.memb[old_match].type->shared->size); } /* end if */ } /* end for */ /* Apply the accumulated size change to the size of the compound struct */ new_dt->shared->size += accum_change; } break; case H5T_ENUM: /* * Copy all member fields to new type, then overwrite the name fields * of each new member with copied values. That is, H5T_copy() is a * deep copy. */ new_dt->shared->u.enumer.name = H5MM_malloc(new_dt->shared->u.enumer.nalloc * sizeof(char*)); new_dt->shared->u.enumer.value = H5MM_malloc(new_dt->shared->u.enumer.nalloc * new_dt->shared->size); if (NULL==new_dt->shared->u.enumer.value) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); HDmemcpy(new_dt->shared->u.enumer.value, old_dt->shared->u.enumer.value, new_dt->shared->u.enumer.nmembs * new_dt->shared->size); for (i=0; i<new_dt->shared->u.enumer.nmembs; i++) { s = old_dt->shared->u.enumer.name[i]; new_dt->shared->u.enumer.name[i] = H5MM_xstrdup(s); } break; case H5T_VLEN: case H5T_REFERENCE: if(method==H5T_COPY_TRANSIENT || method==H5T_COPY_REOPEN) { /* H5T_copy converts any type into a memory type */ if (H5T_set_loc(new_dt, NULL, H5T_LOC_MEMORY)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "invalid datatype location"); } break; case H5T_OPAQUE: /* * Copy the tag name. */ new_dt->shared->u.opaque.tag = HDstrdup(new_dt->shared->u.opaque.tag); break; case H5T_ARRAY: /* Re-compute the array's size, in case it's base type changed size */ new_dt->shared->size=new_dt->shared->u.array.nelem*new_dt->shared->parent->shared->size; break; default: break; } /* end switch */ /* Deep copy of the symbol table entry, if there was one */ if (H5F_addr_defined(old_dt->ent.header)) if (H5G_ent_copy(&(new_dt->ent), &(old_dt->ent),H5G_COPY_DEEP)<0) HGOTO_ERROR(H5E_SYM, H5E_CANTOPENOBJ, NULL, "unable to copy entry"); /* Set return value */ ret_value=new_dt; done: if(ret_value==NULL) { if(new_dt->shared != NULL) H5FL_FREE(H5T_shared_t, new_dt->shared); if(new_dt!=NULL) H5FL_FREE (H5T_t,new_dt); } /* end if */ FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_lock * * Purpose: Lock a transient data type making it read-only. If IMMUTABLE * is set then the type cannot be closed except when the library * itself closes. * * This function is a no-op if the type is not transient or if * the type is already read-only or immutable. * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Thursday, June 4, 1998 * * Modifications: * *------------------------------------------------------------------------- */ herr_t H5T_lock (H5T_t *dt, hbool_t immutable) { herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5T_lock, FAIL); assert (dt); switch (dt->shared->state) { case H5T_STATE_TRANSIENT: dt->shared->state = immutable ? H5T_STATE_IMMUTABLE : H5T_STATE_RDONLY; break; case H5T_STATE_RDONLY: if (immutable) dt->shared->state = H5T_STATE_IMMUTABLE; break; case H5T_STATE_IMMUTABLE: case H5T_STATE_NAMED: case H5T_STATE_OPEN: /*void*/ break; } done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_free * * Purpose: Frees all memory associated with a datatype, but does not * free the H5T_t or H5T_shared_t structures (which should * be done in H5T_close). * * Return: Non-negative on success/Negative on failure * * Programmer: Quincey Koziol * Monday, January 6, 2003 * * Modifications: * *------------------------------------------------------------------------- */ herr_t H5T_free(H5T_t *dt) { unsigned i; herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5T_free, FAIL); assert(dt && dt->shared); /* * If a named type is being closed then close the object header and * remove from the list of open objects in the file. */ if (H5T_STATE_OPEN==dt->shared->state) { assert (H5F_addr_defined(dt->ent.header)); /* Remove the datatype from the list of opened objects in the file */ if(H5FO_delete(dt->ent.file, H5AC_dxpl_id, dt->ent.header)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTRELEASE, FAIL, "can't remove datatype from list of open objects") if (H5O_close(&(dt->ent))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to close data type object header"); dt->shared->state = H5T_STATE_NAMED; } /* * Don't free locked datatypes. */ if (H5T_STATE_IMMUTABLE==dt->shared->state) HGOTO_ERROR(H5E_DATATYPE, H5E_CLOSEERROR, FAIL, "unable to close immutable datatype"); /* Close the datatype */ switch (dt->shared->type) { case H5T_COMPOUND: for (i=0; i<dt->shared->u.compnd.nmembs; i++) { H5MM_xfree(dt->shared->u.compnd.memb[i].name); H5T_close(dt->shared->u.compnd.memb[i].type); } H5MM_xfree(dt->shared->u.compnd.memb); break; case H5T_ENUM: for (i=0; i<dt->shared->u.enumer.nmembs; i++) H5MM_xfree(dt->shared->u.enumer.name[i]); H5MM_xfree(dt->shared->u.enumer.name); H5MM_xfree(dt->shared->u.enumer.value); break; case H5T_OPAQUE: H5MM_xfree(dt->shared->u.opaque.tag); break; default: break; } /* Free the ID to name info */ H5G_free_ent_name(&(dt->ent)); /* Close the parent */ if (dt->shared->parent && H5T_close(dt->shared->parent)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTCLOSEOBJ, FAIL, "unable to close parent data type"); done: FUNC_LEAVE_NOAPI(ret_value); } /* end H5T_free() */ /*------------------------------------------------------------------------- * Function: H5T_close * * Purpose: Frees a data type and all associated memory. If the data * type is locked then nothing happens. * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Monday, December 8, 1997 * * Modifications: * Robb Matzke, 1999-04-27 * This function fails if the datatype state is IMMUTABLE. * * Robb Matzke, 1999-05-20 * Closes opaque types also. * * Pedro Vicente, <pvn@ncsa.uiuc.edu> 22 Aug 2002 * Added "ID to name" support * * Quincey Koziol, 2003-01-06 * Moved "guts" of function to H5T_free() * *------------------------------------------------------------------------- */ herr_t H5T_close(H5T_t *dt) { herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5T_close, FAIL); assert(dt && dt->shared); if(dt->shared->state != H5T_STATE_OPEN || dt->shared->fo_count == 1) { if(H5T_free(dt)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTFREE, FAIL, "unable to free datatype"); H5FL_FREE(H5T_shared_t, dt->shared); } else { dt->shared->fo_count--; /* Free the ID to name info since we're not calling H5T_free*/ H5G_free_ent_name(&(dt->ent)); } /* Free the datatype struct */ H5FL_FREE(H5T_t,dt); done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_set_size * * Purpose: Sets the total size in bytes for a data type (this operation * is not permitted on reference data types). If the size is * decreased so that the significant bits of the data type * extend beyond the edge of the new size, then the `offset' * property is decreased toward zero. If the `offset' becomes * zero and the significant bits of the data type still hang * over the edge of the new size, then the number of significant * bits is decreased. * * Adjusting the size of an H5T_STRING automatically sets the * precision to 8*size. * * All data types have a positive size. * * Return: Success: non-negative * * Failure: nagative * * Programmer: Robb Matzke * Tuesday, December 22, 1998 * * Modifications: * Robb Matzke, 22 Dec 1998 * Also works with derived data types. * *------------------------------------------------------------------------- */ herr_t H5T_set_size(H5T_t *dt, size_t size) { size_t prec, offset; herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5T_set_size, FAIL); /* Check args */ assert(dt); assert(size!=0); assert(H5T_REFERENCE!=dt->shared->type); assert(!(H5T_ENUM==dt->shared->type && 0==dt->shared->u.enumer.nmembs)); if (dt->shared->parent) { if (H5T_set_size(dt->shared->parent, size)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to set size for parent data type"); /* Adjust size of datatype appropriately */ if(dt->shared->type==H5T_ARRAY) dt->shared->size = dt->shared->parent->shared->size * dt->shared->u.array.nelem; else if(dt->shared->type!=H5T_VLEN) dt->shared->size = dt->shared->parent->shared->size; } else { if (H5T_IS_ATOMIC(dt->shared)) { offset = dt->shared->u.atomic.offset; prec = dt->shared->u.atomic.prec; /* Decrement the offset and precision if necessary */ if (prec > 8*size) offset = 0; else if (offset+prec > 8*size) offset = 8 * size - prec; if (prec > 8*size) prec = 8 * size; } else { prec = offset = 0; } switch (dt->shared->type) { case H5T_INTEGER: case H5T_TIME: case H5T_BITFIELD: case H5T_OPAQUE: /* nothing to check */ break; case H5T_COMPOUND: /* If decreasing size, check the last member isn't being cut. */ if(size<dt->shared->size) { int num_membs; unsigned i, max_index=0; size_t memb_offset, max_offset=0; size_t max_size; if((num_membs = H5T_get_nmembers(dt))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "unable to get number of members"); for(i=0; i<(unsigned)num_membs; i++) { memb_offset = H5T_get_member_offset(dt, i); if(memb_offset > max_offset) { max_offset = memb_offset; max_index = i; } } max_size = H5T_get_member_size(dt, max_index); if(size<(max_offset+max_size)) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "size shrinking will cut off last member "); } break; case H5T_STRING: /* Convert string to variable-length datatype */ if(size==H5T_VARIABLE) { H5T_t *base = NULL; /* base data type */ H5T_cset_t tmp_cset; /* Temp. cset info */ H5T_str_t tmp_strpad; /* Temp. strpad info */ /* Get a copy of unsigned char type as the base/parent type */ if (NULL==(base=H5I_object(H5T_NATIVE_UCHAR))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "invalid base datatype"); dt->shared->parent=H5T_copy(base,H5T_COPY_ALL); /* change this datatype into a VL string */ dt->shared->type = H5T_VLEN; /* * Force conversions (i.e. memory to memory conversions * should duplicate data, not point to the same VL strings) */ dt->shared->force_conv = TRUE; /* Before we mess with the info in the union, extract the * values we need */ tmp_cset=dt->shared->u.atomic.u.s.cset; tmp_strpad=dt->shared->u.atomic.u.s.pad; /* This is a string, not a sequence */ dt->shared->u.vlen.type = H5T_VLEN_STRING; /* Set character set and padding information */ dt->shared->u.vlen.cset = tmp_cset; dt->shared->u.vlen.pad = tmp_strpad; /* Set up VL information */ if (H5T_set_loc(dt, NULL, H5T_LOC_MEMORY)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "invalid datatype location"); } else { prec = 8 * size; offset = 0; } /* end else */ break; case H5T_FLOAT: /* * The sign, mantissa, and exponent fields should be adjusted * first when decreasing the size of a floating point type. */ if (dt->shared->u.atomic.u.f.sign >= prec+offset || dt->shared->u.atomic.u.f.epos + dt->shared->u.atomic.u.f.esize > prec+offset || dt->shared->u.atomic.u.f.mpos + dt->shared->u.atomic.u.f.msize > prec+offset) { HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "adjust sign, mantissa, and exponent fields first"); } break; case H5T_ENUM: case H5T_VLEN: case H5T_ARRAY: assert("can't happen" && 0); case H5T_REFERENCE: assert("invalid type" && 0); default: assert("not implemented yet" && 0); } /* Commit (if we didn't convert this type to a VL string) */ if(dt->shared->type!=H5T_VLEN) { dt->shared->size = size; if (H5T_IS_ATOMIC(dt->shared)) { dt->shared->u.atomic.offset = offset; dt->shared->u.atomic.prec = prec; } } /* end if */ } done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_get_size * * Purpose: Determines the total size of a data type in bytes. * * Return: Success: Size of the data type in bytes. The size of * the data type is the size of an instance of * that data type. * * Failure: 0 (valid data types are never zero size) * * Programmer: Robb Matzke * Tuesday, December 9, 1997 * * Modifications: * *------------------------------------------------------------------------- */ size_t H5T_get_size(const H5T_t *dt) { /* Use FUNC_ENTER_NOAPI_NOINIT_NOFUNC here to avoid performance issues */ FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5T_get_size); /* check args */ assert(dt); FUNC_LEAVE_NOAPI(dt->shared->size); } /*------------------------------------------------------------------------- * Function: H5T_cmp * * Purpose: Compares two data types. * * Return: Success: 0 if DT1 and DT2 are equal. * <0 if DT1 is less than DT2. * >0 if DT1 is greater than DT2. * * Failure: 0, never fails * * Programmer: Robb Matzke * Wednesday, December 10, 1997 * * Modifications: * Robb Matzke, 22 Dec 1998 * Able to compare enumeration data types. * * Robb Matzke, 20 May 1999 * Compares bitfields and opaque types. * * Quincey Koziol, 19 Mar 2005 * Allow an enumerated datatypes to compare equal, if the "superset" * flag is set and dt2 has a superset of the enumerated values in dt1 *------------------------------------------------------------------------- */ int H5T_cmp(const H5T_t *dt1, const H5T_t *dt2, hbool_t superset) { unsigned *idx1 = NULL, *idx2 = NULL; int ret_value = 0; int i, j; unsigned u; int tmp; hbool_t swapped; size_t base_size; FUNC_ENTER_NOAPI(H5T_cmp, 0); /* the easy case */ if (dt1 == dt2) HGOTO_DONE(0); assert(dt1); assert(dt2); /* compare */ if (dt1->shared->type < dt2->shared->type) HGOTO_DONE(-1); if (dt1->shared->type > dt2->shared->type) HGOTO_DONE(1); if (dt1->shared->size < dt2->shared->size) HGOTO_DONE(-1); if (dt1->shared->size > dt2->shared->size) HGOTO_DONE(1); if (dt1->shared->parent && !dt2->shared->parent) HGOTO_DONE(-1); if (!dt1->shared->parent && dt2->shared->parent) HGOTO_DONE(1); if (dt1->shared->parent) { tmp = H5T_cmp(dt1->shared->parent, dt2->shared->parent, superset); if (tmp<0) HGOTO_DONE(-1); if (tmp>0) HGOTO_DONE(1); } switch(dt1->shared->type) { case H5T_COMPOUND: /* * Compound data types... */ if (dt1->shared->u.compnd.nmembs < dt2->shared->u.compnd.nmembs) HGOTO_DONE(-1); if (dt1->shared->u.compnd.nmembs > dt2->shared->u.compnd.nmembs) HGOTO_DONE(1); /* Build an index for each type so the names are sorted */ if (NULL==(idx1 = H5MM_malloc(dt1->shared->u.compnd.nmembs * sizeof(unsigned))) || NULL==(idx2 = H5MM_malloc(dt2->shared->u.compnd.nmembs * sizeof(unsigned)))) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, 0, "memory allocation failed"); for (u=0; u<dt1->shared->u.compnd.nmembs; u++) idx1[u] = idx2[u] = u; if(dt1->shared->u.enumer.nmembs > 1) { for (i=dt1->shared->u.compnd.nmembs-1, swapped=TRUE; swapped && i>=0; --i) for (j=0, swapped=FALSE; j<i; j++) if (HDstrcmp(dt1->shared->u.compnd.memb[idx1[j]].name, dt1->shared->u.compnd.memb[idx1[j+1]].name) > 0) { tmp = idx1[j]; idx1[j] = idx1[j+1]; idx1[j+1] = tmp; swapped = TRUE; } for (i=dt2->shared->u.compnd.nmembs-1, swapped=TRUE; swapped && i>=0; --i) for (j=0, swapped=FALSE; j<i; j++) if (HDstrcmp(dt2->shared->u.compnd.memb[idx2[j]].name, dt2->shared->u.compnd.memb[idx2[j+1]].name) > 0) { tmp = idx2[j]; idx2[j] = idx2[j+1]; idx2[j+1] = tmp; swapped = TRUE; } } /* end if */ #ifdef H5T_DEBUG /* I don't quite trust the code above yet :-) --RPM */ for (u=0; u<dt1->shared->u.compnd.nmembs-1; u++) { assert(HDstrcmp(dt1->shared->u.compnd.memb[idx1[u]].name, dt1->shared->u.compnd.memb[idx1[u + 1]].name)); assert(HDstrcmp(dt2->shared->u.compnd.memb[idx2[u]].name, dt2->shared->u.compnd.memb[idx2[u + 1]].name)); } #endif /* Compare the members */ for (u=0; u<dt1->shared->u.compnd.nmembs; u++) { tmp = HDstrcmp(dt1->shared->u.compnd.memb[idx1[u]].name, dt2->shared->u.compnd.memb[idx2[u]].name); if (tmp < 0) HGOTO_DONE(-1); if (tmp > 0) HGOTO_DONE(1); if (dt1->shared->u.compnd.memb[idx1[u]].offset < dt2->shared->u.compnd.memb[idx2[u]].offset) HGOTO_DONE(-1); if (dt1->shared->u.compnd.memb[idx1[u]].offset > dt2->shared->u.compnd.memb[idx2[u]].offset) HGOTO_DONE(1); if (dt1->shared->u.compnd.memb[idx1[u]].size < dt2->shared->u.compnd.memb[idx2[u]].size) HGOTO_DONE(-1); if (dt1->shared->u.compnd.memb[idx1[u]].size > dt2->shared->u.compnd.memb[idx2[u]].size) HGOTO_DONE(1); tmp = H5T_cmp(dt1->shared->u.compnd.memb[idx1[u]].type, dt2->shared->u.compnd.memb[idx2[u]].type, superset); if (tmp < 0) HGOTO_DONE(-1); if (tmp > 0) HGOTO_DONE(1); } break; case H5T_ENUM: /* * Enumeration data types... */ /* If we are doing a "superset" comparison, dt2 is allowed to have * more members than dt1 */ if(superset) { if (dt1->shared->u.enumer.nmembs > dt2->shared->u.enumer.nmembs) HGOTO_DONE(1); } /* end if */ else { if (dt1->shared->u.enumer.nmembs < dt2->shared->u.enumer.nmembs) HGOTO_DONE(-1); if (dt1->shared->u.enumer.nmembs > dt2->shared->u.enumer.nmembs) HGOTO_DONE(1); } /* end else */ /* Build an index for each type so the names are sorted */ if (NULL==(idx1 = H5MM_malloc(dt1->shared->u.enumer.nmembs * sizeof(unsigned))) || NULL==(idx2 = H5MM_malloc(dt2->shared->u.enumer.nmembs * sizeof(unsigned)))) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, 0, "memory allocation failed"); for (u=0; u<dt1->shared->u.enumer.nmembs; u++) idx1[u] = u; if(dt1->shared->u.enumer.nmembs > 1) for (i=dt1->shared->u.enumer.nmembs-1, swapped=TRUE; swapped && i>=0; --i) for (j=0, swapped=FALSE; j<i; j++) if (HDstrcmp(dt1->shared->u.enumer.name[idx1[j]], dt1->shared->u.enumer.name[idx1[j+1]]) > 0) { tmp = idx1[j]; idx1[j] = idx1[j+1]; idx1[j+1] = tmp; swapped = TRUE; } for (u=0; u<dt2->shared->u.enumer.nmembs; u++) idx2[u] = u; if(dt2->shared->u.enumer.nmembs > 1) for (i=dt2->shared->u.enumer.nmembs-1, swapped=TRUE; swapped && i>=0; --i) for (j=0, swapped=FALSE; j<i; j++) if (HDstrcmp(dt2->shared->u.enumer.name[idx2[j]], dt2->shared->u.enumer.name[idx2[j+1]]) > 0) { tmp = idx2[j]; idx2[j] = idx2[j+1]; idx2[j+1] = tmp; swapped = TRUE; } #ifdef H5T_DEBUG /* I don't quite trust the code above yet :-) --RPM */ for (u=0; u<dt1->shared->u.enumer.nmembs-1; u++) { assert(HDstrcmp(dt1->shared->u.enumer.name[idx1[u]], dt1->shared->u.enumer.name[idx1[u+1]])); assert(HDstrcmp(dt2->shared->u.enumer.name[idx2[u]], dt2->shared->u.enumer.name[idx2[u+1]])); } #endif /* Compare the members */ base_size = dt1->shared->parent->shared->size; for (u=0; u<dt1->shared->u.enumer.nmembs; u++) { unsigned idx; if(superset) { unsigned lt = 0, rt; /* Final, left & right key indices */ int cmp = 1; /* Key comparison value */ /* If a superset is allowed, dt2 may have more members * than dt1, so binary search for matching member name in * dt2 */ rt = dt2->shared->u.enumer.nmembs; while (lt < rt && cmp) { idx = (lt + rt) / 2; /* compare */ if ((cmp = HDstrcmp(dt1->shared->u.enumer.name[idx1[u]], dt2->shared->u.enumer.name[idx2[idx]] ) ) < 0) rt = idx; else lt = idx+1; } /* Leave, if we couldn't find match */ if (cmp) HGOTO_DONE(-1); } /* end if */ else { /* Check for exact member name match when not doing * "superset" comparison */ tmp = HDstrcmp(dt1->shared->u.enumer.name[idx1[u]], dt2->shared->u.enumer.name[idx2[u]]); if (tmp<0) HGOTO_DONE(-1); if (tmp>0) HGOTO_DONE(1); /* Set index value appropriately */ idx = u; } /* end else */ tmp = HDmemcmp(dt1->shared->u.enumer.value+idx1[u]*base_size, dt2->shared->u.enumer.value+idx2[idx]*base_size, base_size); if (tmp<0) HGOTO_DONE(-1); if (tmp>0) HGOTO_DONE(1); } break; case H5T_VLEN: assert(dt1->shared->u.vlen.type>H5T_VLEN_BADTYPE && dt1->shared->u.vlen.type<H5T_VLEN_MAXTYPE); assert(dt2->shared->u.vlen.type>H5T_VLEN_BADTYPE && dt2->shared->u.vlen.type<H5T_VLEN_MAXTYPE); assert(dt1->shared->u.vlen.loc>H5T_LOC_BADLOC && dt1->shared->u.vlen.loc<H5T_LOC_MAXLOC); assert(dt2->shared->u.vlen.loc>H5T_LOC_BADLOC && dt2->shared->u.vlen.loc<H5T_LOC_MAXLOC); /* Arbitrarily sort sequence VL datatypes before string VL datatypes */ if (dt1->shared->u.vlen.type==H5T_VLEN_SEQUENCE && dt2->shared->u.vlen.type==H5T_VLEN_STRING) { HGOTO_DONE(-1); } else if (dt1->shared->u.vlen.type==H5T_VLEN_STRING && dt2->shared->u.vlen.type==H5T_VLEN_SEQUENCE) { HGOTO_DONE(1); } /* Arbitrarily sort VL datatypes in memory before disk */ if (dt1->shared->u.vlen.loc==H5T_LOC_MEMORY && dt2->shared->u.vlen.loc==H5T_LOC_DISK) { HGOTO_DONE(-1); } else if (dt1->shared->u.vlen.loc==H5T_LOC_DISK && dt2->shared->u.vlen.loc==H5T_LOC_MEMORY) { HGOTO_DONE(1); } /* Don't allow VL types in different files to compare as equal */ if (dt1->shared->u.vlen.f < dt2->shared->u.vlen.f) HGOTO_DONE(-1); if (dt1->shared->u.vlen.f > dt2->shared->u.vlen.f) HGOTO_DONE(1); break; case H5T_OPAQUE: if(dt1->shared->u.opaque.tag && dt2->shared->u.opaque.tag) HGOTO_DONE(HDstrcmp(dt1->shared->u.opaque.tag,dt2->shared->u.opaque.tag)); break; case H5T_ARRAY: if (dt1->shared->u.array.ndims < dt2->shared->u.array.ndims) HGOTO_DONE(-1); if (dt1->shared->u.array.ndims > dt2->shared->u.array.ndims) HGOTO_DONE(1); for (j=0; j<dt1->shared->u.array.ndims; j++) { if (dt1->shared->u.array.dim[j] < dt2->shared->u.array.dim[j]) HGOTO_DONE(-1); if (dt1->shared->u.array.dim[j] > dt2->shared->u.array.dim[j]) HGOTO_DONE(1); } for (j=0; j<dt1->shared->u.array.ndims; j++) { if (dt1->shared->u.array.perm[j] < dt2->shared->u.array.perm[j]) HGOTO_DONE(-1); if (dt1->shared->u.array.perm[j] > dt2->shared->u.array.perm[j]) HGOTO_DONE(1); } tmp = H5T_cmp(dt1->shared->parent, dt2->shared->parent, superset); if (tmp < 0) HGOTO_DONE(-1); if (tmp > 0) HGOTO_DONE(1); break; default: /* * Atomic datatypes... */ if (dt1->shared->u.atomic.order < dt2->shared->u.atomic.order) HGOTO_DONE(-1); if (dt1->shared->u.atomic.order > dt2->shared->u.atomic.order) HGOTO_DONE(1); if (dt1->shared->u.atomic.prec < dt2->shared->u.atomic.prec) HGOTO_DONE(-1); if (dt1->shared->u.atomic.prec > dt2->shared->u.atomic.prec) HGOTO_DONE(1); if (dt1->shared->u.atomic.offset < dt2->shared->u.atomic.offset) HGOTO_DONE(-1); if (dt1->shared->u.atomic.offset > dt2->shared->u.atomic.offset) HGOTO_DONE(1); if (dt1->shared->u.atomic.lsb_pad < dt2->shared->u.atomic.lsb_pad) HGOTO_DONE(-1); if (dt1->shared->u.atomic.lsb_pad > dt2->shared->u.atomic.lsb_pad) HGOTO_DONE(1); if (dt1->shared->u.atomic.msb_pad < dt2->shared->u.atomic.msb_pad) HGOTO_DONE(-1); if (dt1->shared->u.atomic.msb_pad > dt2->shared->u.atomic.msb_pad) HGOTO_DONE(1); switch (dt1->shared->type) { case H5T_INTEGER: if (dt1->shared->u.atomic.u.i.sign < dt2->shared->u.atomic.u.i.sign) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.i.sign > dt2->shared->u.atomic.u.i.sign) HGOTO_DONE(1); break; case H5T_FLOAT: if (dt1->shared->u.atomic.u.f.sign < dt2->shared->u.atomic.u.f.sign) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.f.sign > dt2->shared->u.atomic.u.f.sign) HGOTO_DONE(1); if (dt1->shared->u.atomic.u.f.epos < dt2->shared->u.atomic.u.f.epos) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.f.epos > dt2->shared->u.atomic.u.f.epos) HGOTO_DONE(1); if (dt1->shared->u.atomic.u.f.esize < dt2->shared->u.atomic.u.f.esize) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.f.esize > dt2->shared->u.atomic.u.f.esize) HGOTO_DONE(1); if (dt1->shared->u.atomic.u.f.ebias < dt2->shared->u.atomic.u.f.ebias) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.f.ebias > dt2->shared->u.atomic.u.f.ebias) HGOTO_DONE(1); if (dt1->shared->u.atomic.u.f.mpos < dt2->shared->u.atomic.u.f.mpos) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.f.mpos > dt2->shared->u.atomic.u.f.mpos) HGOTO_DONE(1); if (dt1->shared->u.atomic.u.f.msize < dt2->shared->u.atomic.u.f.msize) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.f.msize > dt2->shared->u.atomic.u.f.msize) HGOTO_DONE(1); if (dt1->shared->u.atomic.u.f.norm < dt2->shared->u.atomic.u.f.norm) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.f.norm > dt2->shared->u.atomic.u.f.norm) HGOTO_DONE(1); if (dt1->shared->u.atomic.u.f.pad < dt2->shared->u.atomic.u.f.pad) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.f.pad > dt2->shared->u.atomic.u.f.pad) HGOTO_DONE(1); break; case H5T_TIME: /* order and precision are checked above */ /*void */ break; case H5T_STRING: if (dt1->shared->u.atomic.u.s.cset < dt2->shared->u.atomic.u.s.cset) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.s.cset > dt2->shared->u.atomic.u.s.cset) HGOTO_DONE(1); if (dt1->shared->u.atomic.u.s.pad < dt2->shared->u.atomic.u.s.pad) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.s.pad > dt2->shared->u.atomic.u.s.pad) HGOTO_DONE(1); break; case H5T_BITFIELD: /*void */ break; case H5T_REFERENCE: if (dt1->shared->u.atomic.u.r.rtype < dt2->shared->u.atomic.u.r.rtype) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.r.rtype > dt2->shared->u.atomic.u.r.rtype) HGOTO_DONE(1); switch(dt1->shared->u.atomic.u.r.rtype) { case H5R_OBJECT: if (dt1->shared->u.atomic.u.r.loc < dt2->shared->u.atomic.u.r.loc) HGOTO_DONE(-1); if (dt1->shared->u.atomic.u.r.loc > dt2->shared->u.atomic.u.r.loc) HGOTO_DONE(1); break; case H5R_DATASET_REGION: /* Does this need more to distinguish it? -QAK 11/30/98 */ /*void */ break; default: assert("not implemented yet" && 0); } break; default: assert("not implemented yet" && 0); } break; } /* end switch */ done: if(idx1!=NULL) H5MM_xfree(idx1); if(idx2!=NULL) H5MM_xfree(idx2); FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_path_find * * Purpose: Finds the path which converts type SRC_ID to type DST_ID, * creating a new path if necessary. If FUNC is non-zero then * it is set as the hard conversion function for that path * regardless of whether the path previously existed. Changing * the conversion function of a path causes statistics to be * reset to zero after printing them. The NAME is used only * when creating a new path and is just for debugging. * * If SRC and DST are both null pointers then the special no-op * conversion path is used. This path is always stored as the * first path in the path table. * * Return: Success: Pointer to the path, valid until the path * database is modified. * * Failure: NULL if the path does not exist and no * function can be found to apply to the new * path. * * Programmer: Robb Matzke * Tuesday, January 13, 1998 * * Modifications: * *------------------------------------------------------------------------- */ H5T_path_t * H5T_path_find(const H5T_t *src, const H5T_t *dst, const char *name, H5T_conv_t func, hid_t dxpl_id) { int lt, rt; /*left and right edges */ int md; /*middle */ int cmp; /*comparison result */ int old_npaths; /* Previous number of paths in table */ H5T_path_t *table=NULL; /*path existing in the table */ H5T_path_t *path=NULL; /*new path */ H5T_path_t *ret_value; /*return value */ hid_t src_id=-1, dst_id=-1; /*src and dst type identifiers */ int i; /*counter */ int nprint=0; /*lines of output printed */ FUNC_ENTER_NOAPI(H5T_path_find, NULL); assert((!src && !dst) || (src && dst)); /* * Make sure the first entry in the table is the no-op conversion path. */ if (0==H5T_g.npaths) { if (NULL==(H5T_g.path=H5MM_malloc(128*sizeof(H5T_path_t*)))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for type conversion path table"); H5T_g.apaths = 128; if (NULL==(H5T_g.path[0]=H5FL_CALLOC(H5T_path_t))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for no-op conversion path"); HDstrcpy(H5T_g.path[0]->name, "no-op"); H5T_g.path[0]->func = H5T_conv_noop; H5T_g.path[0]->cdata.command = H5T_CONV_INIT; if (H5T_conv_noop(FAIL, FAIL, &(H5T_g.path[0]->cdata), (size_t)0, (size_t)0, (size_t)0, NULL, NULL, dxpl_id)<0) { #ifdef H5T_DEBUG if (H5DEBUG(T)) { fprintf(H5DEBUG(T), "H5T: unable to initialize no-op " "conversion function (ignored)\n"); } #endif H5E_clear_stack(NULL); /*ignore the error*/ } H5T_g.path[0]->is_noop = TRUE; H5T_g.npaths = 1; } /* * Find the conversion path. If source and destination types are equal * then use entry[0], otherwise do a binary search over the * remaining entries. * * Quincey Koziol, 2 July, 1999 * Only allow the no-op conversion to occur if no "force conversion" flags * are set */ if (src->shared->force_conv==FALSE && dst->shared->force_conv==FALSE && 0==H5T_cmp(src, dst, TRUE)) { table = H5T_g.path[0]; cmp = 0; md = 0; } else { lt = md = 1; rt = H5T_g.npaths; cmp = -1; while (cmp && lt<rt) { md = (lt+rt) / 2; assert(H5T_g.path[md]); cmp = H5T_cmp(src, H5T_g.path[md]->src, FALSE); if (0==cmp) cmp = H5T_cmp(dst, H5T_g.path[md]->dst, FALSE); if (cmp<0) { rt = md; } else if (cmp>0) { lt = md+1; } else { table = H5T_g.path[md]; } } } /* Keep a record of the number of paths in the table, in case one of the * initialization calls below (hard or soft) causes more entries to be * added to the table - QAK, 1/26/02 */ old_npaths=H5T_g.npaths; /* * If we didn't find the path or if the caller is specifying a new hard * conversion function then create a new path and add the new function to * the path. */ if (!table || func) { if (NULL==(path=H5FL_CALLOC(H5T_path_t))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for type conversion path"); if (name && *name) { HDstrncpy(path->name, name, (size_t)H5T_NAMELEN); path->name[H5T_NAMELEN-1] = '\0'; } else { HDstrcpy(path->name, "NONAME"); } if ((src && NULL==(path->src=H5T_copy(src, H5T_COPY_ALL))) || (dst && NULL==(path->dst=H5T_copy(dst, H5T_COPY_ALL)))) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to copy data type for conversion path"); } else { path = table; } /* * If a hard conversion function is specified and none is defined for the * path then add it to the path and initialize its conversion data. */ if (func) { assert(path!=table); assert(NULL==path->func); if (path->src && (src_id=H5I_register(H5I_DATATYPE, H5T_copy(path->src, H5T_COPY_ALL)))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, NULL, "unable to register source conversion type for query"); if (path->dst && (dst_id=H5I_register(H5I_DATATYPE, H5T_copy(path->dst, H5T_COPY_ALL)))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, NULL, "unable to register destination conversion type for query"); path->cdata.command = H5T_CONV_INIT; if ((func)(src_id, dst_id, &(path->cdata), (size_t)0, (size_t)0, (size_t)0, NULL, NULL, dxpl_id)<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "unable to initialize conversion function"); if (src_id>=0) H5I_dec_ref(src_id); if (dst_id>=0) H5I_dec_ref(dst_id); src_id = dst_id = -1; path->func = func; path->is_hard = TRUE; } /* * If the path doesn't have a function by now (because it's a new path * and the caller didn't supply a hard function) then scan the soft list * for an applicable function and add it to the path. This can't happen * for the no-op conversion path. */ assert(path->func || (src && dst)); for (i=H5T_g.nsoft-1; i>=0 && !path->func; --i) { if (src->shared->type!=H5T_g.soft[i].src || dst->shared->type!=H5T_g.soft[i].dst) { continue; } if ((src_id=H5I_register(H5I_DATATYPE, H5T_copy(path->src, H5T_COPY_ALL)))<0 || (dst_id=H5I_register(H5I_DATATYPE, H5T_copy(path->dst, H5T_COPY_ALL)))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTREGISTER, NULL, "unable to register conversion types for query"); path->cdata.command = H5T_CONV_INIT; if ((H5T_g.soft[i].func) (src_id, dst_id, &(path->cdata), (size_t)0, (size_t)0, (size_t)0, NULL, NULL, dxpl_id)<0) { HDmemset (&(path->cdata), 0, sizeof(H5T_cdata_t)); H5E_clear_stack(NULL); /*ignore the error*/ } else { HDstrcpy (path->name, H5T_g.soft[i].name); path->func = H5T_g.soft[i].func; path->is_hard = FALSE; } H5I_dec_ref(src_id); H5I_dec_ref(dst_id); src_id = dst_id = -1; } if (!path->func) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, NULL, "no appropriate function for conversion path"); /* Check if paths were inserted into the table through a recursive call * and re-compute the correct location for this path if so. - QAK, 1/26/02 */ if(old_npaths!=H5T_g.npaths) { lt = md = 1; rt = H5T_g.npaths; cmp = -1; while (cmp && lt<rt) { md = (lt+rt) / 2; assert(H5T_g.path[md]); cmp = H5T_cmp(src, H5T_g.path[md]->src, FALSE); if (0==cmp) cmp = H5T_cmp(dst, H5T_g.path[md]->dst, FALSE); if (cmp<0) { rt = md; } else if (cmp>0) { lt = md+1; } else { table = H5T_g.path[md]; } } } /* end if */ /* Replace an existing table entry or add a new entry */ if (table && path!=table) { assert(table==H5T_g.path[md]); H5T_print_stats(table, &nprint/*in,out*/); table->cdata.command = H5T_CONV_FREE; if ((table->func)(FAIL, FAIL, &(table->cdata), (size_t)0, (size_t)0, (size_t)0, NULL, NULL, dxpl_id)<0) { #ifdef H5T_DEBUG if (H5DEBUG(T)) { fprintf(H5DEBUG(T), "H5T: conversion function 0x%08lx free " "failed for %s (ignored)\n", (unsigned long)(path->func), path->name); } #endif H5E_clear_stack(NULL); /*ignore the failure*/ } if (table->src) H5T_close(table->src); if (table->dst) H5T_close(table->dst); H5FL_FREE(H5T_path_t,table); table = path; H5T_g.path[md] = path; } else if (path!=table) { assert(cmp); if (H5T_g.npaths >= H5T_g.apaths) { size_t na = MAX(128, 2 * H5T_g.apaths); H5T_path_t **x = H5MM_realloc (H5T_g.path, na*sizeof(H5T_path_t*)); if (!x) HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); H5T_g.apaths = (int)na; H5T_g.path = x; } if (cmp>0) md++; HDmemmove(H5T_g.path+md+1, H5T_g.path+md, (H5T_g.npaths-md) * sizeof(H5T_path_t*)); H5T_g.npaths++; H5T_g.path[md] = path; table = path; } /* Set return value */ ret_value = path; done: if (!ret_value && path && path!=table) { if (path->src) H5T_close(path->src); if (path->dst) H5T_close(path->dst); H5FL_FREE(H5T_path_t,path); } if (src_id>=0) H5I_dec_ref(src_id); if (dst_id>=0) H5I_dec_ref(dst_id); FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_path_noop * * Purpose: Is the path the special no-op path? The no-op function can be * set by the application and there might be more than one no-op * path in a multi-threaded application if one thread is using * the no-op path when some other thread changes its definition. * * Return: TRUE/FALSE (can't fail) * * Programmer: Quincey Koziol * Thursday, May 8, 2003 * * Modifications: * *------------------------------------------------------------------------- */ hbool_t H5T_path_noop(const H5T_path_t *p) { FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5T_path_noop); assert(p); FUNC_LEAVE_NOAPI(p->is_noop || (p->is_hard && 0==H5T_cmp(p->src, p->dst, FALSE))); } /* end H5T_path_noop() */ /*------------------------------------------------------------------------- * Function: H5T_path_bkg * * Purpose: Get the "background" flag for the conversion path. * * Return: Background flag (can't fail) * * Programmer: Quincey Koziol * Thursday, May 8, 2003 * * Modifications: * *------------------------------------------------------------------------- */ H5T_bkg_t H5T_path_bkg(const H5T_path_t *p) { FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5T_path_bkg); assert(p); FUNC_LEAVE_NOAPI(p->cdata.need_bkg); } /* end H5T_path_bkg() */ /*------------------------------------------------------------------------- * Function: H5T_convert * * Purpose: Call a conversion function to convert from source to * destination data type and accumulate timing statistics. * * Return: Success: non-negative * * Failure: negative * * Programmer: Robb Matzke * Tuesday, December 15, 1998 * * Modifications: * Robb Matzke, 1999-06-16 * The timers are updated only if H5T debugging is enabled at * runtime in addition to compile time. * * Robb Matzke, 1999-06-16 * Added support for non-zero strides. If BUF_STRIDE is non-zero * then convert one value at each memory location advancing * BUF_STRIDE bytes each time; otherwise assume both source and * destination values are packed. * * Quincey Koziol, 1999-07-01 * Added dataset transfer properties, to allow custom VL * datatype allocation function to be passed down to VL * conversion routine. * * Robb Matzke, 2000-05-17 * Added the BKG_STRIDE argument which gets passed to all the * conversion functions. If BUF_STRIDE is non-zero then each * data element is at a multiple of BUF_STRIDE bytes in BUF * (on both input and output). If BKG_STRIDE is also set then * the BKG buffer is used in such a way that temporary space * for each element is aligned on a BKG_STRIDE byte boundary. * If either BUF_STRIDE or BKG_STRIDE are zero then the BKG * buffer will be accessed as though it were a packed array * of destination datatype. *------------------------------------------------------------------------- */ herr_t H5T_convert(H5T_path_t *tpath, hid_t src_id, hid_t dst_id, size_t nelmts, size_t buf_stride, size_t bkg_stride, void *buf, void *bkg, hid_t dset_xfer_plist) { #ifdef H5T_DEBUG H5_timer_t timer; #endif herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5T_convert, FAIL); #ifdef H5T_DEBUG if (H5DEBUG(T)) H5_timer_begin(&timer); #endif tpath->cdata.command = H5T_CONV_CONV; if ((tpath->func)(src_id, dst_id, &(tpath->cdata), nelmts, buf_stride, bkg_stride, buf, bkg, dset_xfer_plist)<0) HGOTO_ERROR(H5E_ATTR, H5E_CANTENCODE, FAIL, "data type conversion failed"); #ifdef H5T_DEBUG if (H5DEBUG(T)) { H5_timer_end(&(tpath->stats.timer), &timer); tpath->stats.ncalls++; tpath->stats.nelmts += nelmts; } #endif done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_entof * * Purpose: Returns a pointer to the entry for a named data type. * * Return: Success: Ptr directly into named data type * * Failure: NULL * * Programmer: Robb Matzke * Friday, June 5, 1998 * * Modifications: * *------------------------------------------------------------------------- */ H5G_entry_t * H5T_entof (H5T_t *dt) { H5G_entry_t *ret_value = NULL; FUNC_ENTER_NOAPI(H5T_entof, NULL); assert (dt); switch (dt->shared->state) { case H5T_STATE_TRANSIENT: case H5T_STATE_RDONLY: case H5T_STATE_IMMUTABLE: HGOTO_ERROR (H5E_DATATYPE, H5E_CANTINIT, NULL, "not a named data type"); case H5T_STATE_NAMED: case H5T_STATE_OPEN: ret_value = &(dt->ent); break; } done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_is_immutable * * Purpose: Check if a datatype is immutable. * * Return: TRUE * * FALSE * * Programmer: Raymond Lu * Friday, Dec 7, 2001 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5T_is_immutable(const H5T_t *dt) { htri_t ret_value = FALSE; FUNC_ENTER_NOAPI(H5T_is_immutable, FAIL); assert(dt); if(dt->shared->state == H5T_STATE_IMMUTABLE) ret_value = TRUE; done: FUNC_LEAVE_NOAPI(ret_value); } /*------------------------------------------------------------------------- * Function: H5T_is_named * * Purpose: Check if a datatype is named. * * Return: TRUE * * FALSE * * Programmer: Pedro Vicente * Tuesday, Sep 3, 2002 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5T_is_named(const H5T_t *dt) { htri_t ret_value = FALSE; FUNC_ENTER_NOAPI(H5T_is_named, FAIL); assert(dt); if(dt->shared->state == H5T_STATE_OPEN || dt->shared->state == H5T_STATE_NAMED) ret_value = TRUE; done: FUNC_LEAVE_NOAPI(ret_value); } /*-------------------------------------------------------------------------- NAME H5T_get_ref_type PURPOSE Retrieves the type of reference for a datatype USAGE H5R_type_t H5Tget_ref_type(dt) H5T_t *dt; IN: datatype pointer for the reference datatype RETURNS Success: A reference type defined in H5Rpublic.h Failure: H5R_BADTYPE DESCRIPTION Given a reference datatype object, this function returns the reference type of the datatype. GLOBAL VARIABLES COMMENTS, BUGS, ASSUMPTIONS EXAMPLES REVISION LOG --------------------------------------------------------------------------*/ H5R_type_t H5T_get_ref_type(const H5T_t *dt) { H5R_type_t ret_value = H5R_BADTYPE; FUNC_ENTER_NOAPI(H5T_get_ref_type, H5R_BADTYPE); assert(dt); if(dt->shared->type==H5T_REFERENCE) ret_value=dt->shared->u.atomic.u.r.rtype; done: FUNC_LEAVE_NOAPI(ret_value); } /* end H5T_get_ref_type() */ /*------------------------------------------------------------------------- * Function: H5T_is_sensible * * Purpose: Determines if a data type is sensible to store on disk * (i.e. not partially initialized) * * Return: Success: TRUE, FALSE * * Failure: Negative * * Programmer: Quincey Koziol * Tuesday, June 11, 2002 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5T_is_sensible(const H5T_t *dt) { htri_t ret_value; FUNC_ENTER_NOAPI(H5T_is_sensible, FAIL); assert(dt); switch(dt->shared->type) { case H5T_COMPOUND: /* Only allow compound datatypes with at least one member to be stored on disk */ if(dt->shared->u.compnd.nmembs > 0) ret_value=TRUE; else ret_value=FALSE; break; case H5T_ENUM: /* Only allow enum datatypes with at least one member to be stored on disk */ if(dt->shared->u.enumer.nmembs > 0) ret_value=TRUE; else ret_value=FALSE; break; default: /* Assume all other datatype are sensible to store on disk */ ret_value=TRUE; break; } /* end switch */ done: FUNC_LEAVE_NOAPI(ret_value); } /*-------------------------------------------------------------------------- NAME H5T_set_loc PURPOSE Recursively mark any datatypes as on disk/in memory USAGE htri_t H5T_set_loc(dt,f,loc) H5T_t *dt; IN/OUT: Pointer to the datatype to mark H5F_t *f; IN: Pointer to the file the datatype is in H5T_vlen_type_t loc IN: location of type RETURNS One of two values on success: TRUE - If the location of any vlen types changed FALSE - If the location of any vlen types is the same <0 is returned on failure DESCRIPTION Recursively descends any VL or compound datatypes to mark all VL datatypes as either on disk or in memory. GLOBAL VARIABLES COMMENTS, BUGS, ASSUMPTIONS EXAMPLES REVISION LOG --------------------------------------------------------------------------*/ htri_t H5T_set_loc(H5T_t *dt, H5F_t *f, H5T_loc_t loc) { htri_t changed; /* Whether H5T_set_loc changed the type (even if the size didn't change) */ htri_t ret_value = 0; /* Indicate that success, but no location change */ unsigned i; /* Local index variable */ int accum_change; /* Amount of change in the offset of the fields */ size_t old_size; /* Previous size of a field */ FUNC_ENTER_NOAPI(H5T_set_loc, FAIL); assert(dt); assert(loc>H5T_LOC_BADLOC && loc<H5T_LOC_MAXLOC); /* Datatypes can't change in size if the force_conv flag is not set */ if(dt->shared->force_conv) { /* Check the datatype of this element */ switch(dt->shared->type) { case H5T_ARRAY: /* Recurse on VL, compound and array base element type */ /* Recurse if it's VL, compound, enum or array */ /* (If the force_conv flag is _not_ set, the type cannot change in size, so don't recurse) */ if(dt->shared->parent->shared->force_conv && H5T_IS_COMPLEX(dt->shared->parent->shared->type)) { /* Keep the old base element size for later */ old_size=dt->shared->parent->shared->size; /* Mark the VL, compound or array type */ if((changed=H5T_set_loc(dt->shared->parent,f,loc))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "Unable to set VL location"); if(changed>0) ret_value=changed; /* Check if the field changed size */ if(old_size != dt->shared->parent->shared->size) { /* Adjust the size of the array */ dt->shared->size = dt->shared->u.array.nelem*dt->shared->parent->shared->size; } /* end if */ } /* end if */ break; case H5T_COMPOUND: /* Check each field and recurse on VL, compound and array type */ /* Sort the fields based on offsets */ H5T_sort_value(dt,NULL); for (i=0,accum_change=0; i<dt->shared->u.compnd.nmembs; i++) { H5T_t *memb_type; /* Member's datatype pointer */ /* Apply the accumulated size change to the offset of the field */ dt->shared->u.compnd.memb[i].offset += accum_change; /* Set the member type pointer (for convenience) */ memb_type=dt->shared->u.compnd.memb[i].type; /* Recurse if it's VL, compound, enum or array */ /* (If the force_conv flag is _not_ set, the type cannot change in size, so don't recurse) */ if(memb_type->shared->force_conv && H5T_IS_COMPLEX(memb_type->shared->type)) { /* Keep the old field size for later */ old_size=memb_type->shared->size; /* Mark the VL, compound, enum or array type */ if((changed=H5T_set_loc(memb_type,f,loc))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "Unable to set VL location"); if(changed>0) ret_value=changed; /* Check if the field changed size */ if(old_size != memb_type->shared->size) { /* Adjust the size of the member */ dt->shared->u.compnd.memb[i].size = (dt->shared->u.compnd.memb[i].size*memb_type->shared->size)/old_size; /* Add that change to the accumulated size change */ accum_change += (memb_type->shared->size - (int)old_size); } /* end if */ } /* end if */ } /* end for */ /* Apply the accumulated size change to the datatype */ dt->shared->size += accum_change; break; case H5T_VLEN: /* Recurse on the VL information if it's VL, compound or array, then free VL sequence */ /* Recurse if it's VL, compound, enum or array */ /* (If the force_conv flag is _not_ set, the type cannot change in size, so don't recurse) */ if(dt->shared->parent->shared->force_conv && H5T_IS_COMPLEX(dt->shared->parent->shared->type)) { if((changed=H5T_set_loc(dt->shared->parent,f,loc))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "Unable to set VL location"); if(changed>0) ret_value=changed; } /* end if */ /* Mark this VL sequence */ if((changed=H5T_vlen_set_loc(dt,f,loc))<0) HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "Unable to set VL location"); if(changed>0) ret_value=changed; break; case H5T_REFERENCE: /* Only need to change location of object references */ if(dt->shared->u.atomic.u.r.rtype==H5R_OBJECT) { /* Mark this reference */ if(loc!=dt->shared->u.atomic.u.r.loc) { /* Set the location */ dt->shared->u.atomic.u.r.loc = loc; /* Indicate that the location changed */ ret_value=TRUE; } /* end if */ } /* end if */ break; default: break; } /* end switch */ } /* end if */ done: FUNC_LEAVE_NOAPI(ret_value); } /* end H5T_set_loc() */ /*------------------------------------------------------------------------- * Function: H5T_is_relocatable * * Purpose: Check if a datatype will change between disk and memory. * * Notes: Currently, only variable-length and object references change * between disk & memory (see cases where things are changed in * the H5T_set_loc() code above). * * Return: * One of two values on success: * TRUE - If the location of any vlen types changed * FALSE - If the location of any vlen types is the same * <0 is returned on failure * * Programmer: Quincey Koziol * Thursday, June 24, 2004 * * Modifications: * *------------------------------------------------------------------------- */ htri_t H5T_is_relocatable(const H5T_t *dt) { htri_t ret_value = FALSE; FUNC_ENTER_NOAPI(H5T_is_relocatable, FAIL); assert(dt); if(H5T_detect_class(dt, H5T_VLEN)) ret_value = TRUE; else if(H5T_detect_class(dt, H5T_REFERENCE)) ret_value = TRUE; done: FUNC_LEAVE_NOAPI(ret_value); } /* end H5T_is_relocatable() */ /*------------------------------------------------------------------------- * Function: H5T_print_stats * * Purpose: Print statistics about a conversion path. Statistics are * printed only if all the following conditions are true: * * 1. The library was compiled with H5T_DEBUG defined. * 2. Data type debugging is turned on at run time. * 3. The path was called at least one time. * * The optional NPRINT argument keeps track of the number of * conversions paths for which statistics have been shown. If * its value is zero then table headers are printed before the * first line of output. * * Return: Success: non-negative * * Failure: negative * * Programmer: Robb Matzke * Monday, December 14, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5T_print_stats(H5T_path_t UNUSED * path, int UNUSED * nprint/*in,out*/) { #ifdef H5T_DEBUG hsize_t nbytes; char bandwidth[32]; #endif FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5T_print_stats); #ifdef H5T_DEBUG if (H5DEBUG(T) && path->stats.ncalls>0) { if (nprint && 0==(*nprint)++) { HDfprintf (H5DEBUG(T), "H5T: type conversion statistics:\n"); HDfprintf (H5DEBUG(T), " %-16s %10s %10s %8s %8s %8s %10s\n", "Conversion", "Elmts", "Calls", "User", "System", "Elapsed", "Bandwidth"); HDfprintf (H5DEBUG(T), " %-16s %10s %10s %8s %8s %8s %10s\n", "----------", "-----", "-----", "----", "------", "-------", "---------"); } if(path->src && path->dst) nbytes = MAX (H5T_get_size (path->src), H5T_get_size (path->dst)); else if(path->src) nbytes = H5T_get_size (path->src); else if(path->dst) nbytes = H5T_get_size (path->dst); else nbytes = 0; nbytes *= path->stats.nelmts; H5_bandwidth(bandwidth, (double)nbytes, path->stats.timer.etime); HDfprintf (H5DEBUG(T), " %-16s %10Hd %10d %8.2f %8.2f %8.2f %10s\n", path->name, path->stats.nelmts, path->stats.ncalls, path->stats.timer.utime, path->stats.timer.stime, path->stats.timer.etime, bandwidth); } #endif FUNC_LEAVE_NOAPI(SUCCEED); } /*------------------------------------------------------------------------- * Function: H5T_debug * * Purpose: Prints information about a data type. * * Return: Non-negative on success/Negative on failure * * Programmer: Robb Matzke * Wednesday, January 7, 1998 * * Modifications: * *------------------------------------------------------------------------- */ herr_t H5T_debug(const H5T_t *dt, FILE *stream) { const char *s1="", *s2=""; unsigned i; size_t k, base_size; uint64_t tmp; herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5T_debug, FAIL); /* Check args */ assert(dt); assert(stream); switch (dt->shared->type) { case H5T_INTEGER: s1 = "int"; break; case H5T_FLOAT: s1 = "float"; break; case H5T_TIME: s1 = "time"; break; case H5T_STRING: s1 = "str"; break; case H5T_BITFIELD: s1 = "bits"; break; case H5T_OPAQUE: s1 = "opaque"; break; case H5T_COMPOUND: s1 = "struct"; break; case H5T_ENUM: s1 = "enum"; break; default: s1 = ""; break; } switch (dt->shared->state) { case H5T_STATE_TRANSIENT: s2 = "[transient]"; break; case H5T_STATE_RDONLY: s2 = "[constant]"; break; case H5T_STATE_IMMUTABLE: s2 = "[predefined]"; break; case H5T_STATE_NAMED: s2 = "[named,closed]"; break; case H5T_STATE_OPEN: s2 = "[named,open]"; break; } fprintf(stream, "%s%s {nbytes=%lu", s1, s2, (unsigned long)(dt->shared->size)); if (H5T_IS_ATOMIC(dt->shared)) { switch (dt->shared->u.atomic.order) { case H5T_ORDER_BE: s1 = "BE"; break; case H5T_ORDER_LE: s1 = "LE"; break; case H5T_ORDER_VAX: s1 = "VAX"; break; case H5T_ORDER_NONE: s1 = "NONE"; break; default: s1 = "order?"; break; } fprintf(stream, ", %s", s1); if (dt->shared->u.atomic.offset) { fprintf(stream, ", offset=%lu", (unsigned long) (dt->shared->u.atomic.offset)); } if (dt->shared->u.atomic.prec != 8 * dt->shared->size) { fprintf(stream, ", prec=%lu", (unsigned long) (dt->shared->u.atomic.prec)); } switch (dt->shared->type) { case H5T_INTEGER: switch (dt->shared->u.atomic.u.i.sign) { case H5T_SGN_NONE: s1 = "unsigned"; break; case H5T_SGN_2: s1 = NULL; break; default: s1 = "sign?"; break; } if (s1) fprintf(stream, ", %s", s1); break; case H5T_FLOAT: switch (dt->shared->u.atomic.u.f.norm) { case H5T_NORM_IMPLIED: s1 = "implied"; break; case H5T_NORM_MSBSET: s1 = "msbset"; break; case H5T_NORM_NONE: s1 = "no-norm"; break; default: s1 = "norm?"; break; } fprintf(stream, ", sign=%lu+1", (unsigned long) (dt->shared->u.atomic.u.f.sign)); fprintf(stream, ", mant=%lu+%lu (%s)", (unsigned long) (dt->shared->u.atomic.u.f.mpos), (unsigned long) (dt->shared->u.atomic.u.f.msize), s1); fprintf(stream, ", exp=%lu+%lu", (unsigned long) (dt->shared->u.atomic.u.f.epos), (unsigned long) (dt->shared->u.atomic.u.f.esize)); tmp = dt->shared->u.atomic.u.f.ebias >> 32; if (tmp) { size_t hi=(size_t)tmp; size_t lo =(size_t)(dt->shared->u.atomic.u.f.ebias & 0xffffffff); fprintf(stream, " bias=0x%08lx%08lx", (unsigned long)hi, (unsigned long)lo); } else { size_t lo = (size_t)(dt->shared->u.atomic.u.f.ebias & 0xffffffff); fprintf(stream, " bias=0x%08lx", (unsigned long)lo); } break; default: /* No additional info */ break; } } else if (H5T_COMPOUND==dt->shared->type) { /* Compound data type */ for (i=0; i<dt->shared->u.compnd.nmembs; i++) { fprintf(stream, "\n\"%s\" @%lu", dt->shared->u.compnd.memb[i].name, (unsigned long) (dt->shared->u.compnd.memb[i].offset)); #ifdef OLD_WAY if (dt->shared->u.compnd.memb[i].ndims) { fprintf(stream, "["); for (j = 0; j < dt->shared->u.compnd.memb[i].ndims; j++) { fprintf(stream, "%s%lu", j ? ", " : "", (unsigned long)(dt->shared->u.compnd.memb[i].dim[j])); } fprintf(stream, "]"); } #endif /* OLD_WAY */ fprintf(stream, " "); H5T_debug(dt->shared->u.compnd.memb[i].type, stream); } fprintf(stream, "\n"); } else if (H5T_ENUM==dt->shared->type) { /* Enumeration data type */ fprintf(stream, " "); H5T_debug(dt->shared->parent, stream); base_size = dt->shared->parent->shared->size; for (i=0; i<dt->shared->u.enumer.nmembs; i++) { fprintf(stream, "\n\"%s\" = 0x", dt->shared->u.enumer.name[i]); for (k=0; k<base_size; k++) { fprintf(stream, "%02lx", (unsigned long)(dt->shared->u.enumer.value+i*base_size+k)); } } fprintf(stream, "\n"); } else if (H5T_OPAQUE==dt->shared->type) { fprintf(stream, ", tag=\"%s\"", dt->shared->u.opaque.tag); } else { /* Unknown */ fprintf(stream, "unknown class %d\n", (int)(dt->shared->type)); } fprintf(stream, "}"); done: FUNC_LEAVE_NOAPI(ret_value); }