summaryrefslogtreecommitdiffstats
path: root/src/H5Pencdec.c
blob: d7ebac6a96ff5eda1a61b57cc1658f7234be8a8b (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* Programmer:  Quincey Koziol
 *
 * Purpose:	Generic Property Functions
 */

/****************/
/* Module Setup */
/****************/

#include "H5Pmodule.h" /* This source code file is part of the H5P module */

/***********/
/* Headers */
/***********/
#include "H5private.h"   /* Generic Functions			*/
#include "H5Eprivate.h"  /* Error handling		  	*/
#include "H5Fprivate.h"  /* Files		  	        */
#include "H5Iprivate.h"  /* IDs			  		*/
#include "H5MMprivate.h" /* Memory management			*/
#include "H5Ppkg.h"      /* Property lists		  	*/
#include "H5VMprivate.h" /* Vector functions			*/

/****************/
/* Local Macros */
/****************/

/* Version # of encoded property lists */
#define H5P_ENCODE_VERS 0

/******************/
/* Local Typedefs */
/******************/

/* Typedef for iterator when encoding a property list */
typedef struct {
    hbool_t encode;       /* Whether the property list should be encoded */
    size_t *enc_size_ptr; /* Pointer to size of encoded buffer */
    void  **pp;           /* Pointer to encoding buffer pointer */
} H5P_enc_iter_ud_t;

/********************/
/* Local Prototypes */
/********************/

/*********************/
/* Package Variables */
/*********************/

/*****************************/
/* Library Private Variables */
/*****************************/

/*******************/
/* Local Variables */
/*******************/

/*-------------------------------------------------------------------------
 * Function:       H5P__encode_size_t
 *
 * Purpose:        Generic encoding callback routine for 'size_t' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Quincey Koziol
 *                 Sunday, July 29, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__encode_size_t(const void *value, void **_pp, size_t *size)
{
    uint64_t  enc_value = (uint64_t) * (const size_t *)value; /* Property value to encode */
    uint8_t **pp        = (uint8_t **)_pp;
    unsigned  enc_size  = H5VM_limit_enc_size(enc_value); /* Size of encoded property */

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity checks */
    HDcompile_assert(sizeof(size_t) <= sizeof(uint64_t));
    HDassert(enc_size < 256);
    HDassert(size);

    if (NULL != *pp) {
        /* Encode the size */
        *(*pp)++ = (uint8_t)enc_size;

        /* Encode the value */
        UINT64ENCODE_VAR(*pp, enc_value, enc_size);
    } /* end if */

    /* Set size needed for encoding */
    *size += (1 + enc_size);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5P__encode_size_t() */

/*-------------------------------------------------------------------------
 * Function:       H5P__encode_hsize_t
 *
 * Purpose:        Generic encoding callback routine for 'hsize_t' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Mohamad Chaarawi
 *                 August 07, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__encode_hsize_t(const void *value, void **_pp, size_t *size)
{
    uint64_t  enc_value = (uint64_t) * (const hsize_t *)value; /* Property value to encode */
    unsigned  enc_size  = H5VM_limit_enc_size(enc_value);      /* Size of encoded property */
    uint8_t **pp        = (uint8_t **)_pp;

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity checks */
    HDcompile_assert(sizeof(hsize_t) <= sizeof(uint64_t));
    HDassert(enc_size < 256);
    HDassert(size);

    if (NULL != *pp) {
        *(*pp)++ = (uint8_t)enc_size;

        /* Encode the value */
        UINT64ENCODE_VAR(*pp, enc_value, enc_size);
    } /* end if */

    /* Set size needed for encoding */
    *size += (1 + enc_size);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5P__encode_hsize_t() */

/*-------------------------------------------------------------------------
 * Function:       H5P__encode_unsigned
 *
 * Purpose:        Generic encoding callback routine for 'unsigned' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Quincey Koziol
 *                 Sunday, July 29, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__encode_unsigned(const void *value, void **_pp, size_t *size)
{
    uint8_t **pp = (uint8_t **)_pp;

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity checks */
    HDassert(value);
    HDassert(size);

    if (NULL != *pp) {
        /* Encode the size */
        *(*pp)++ = (uint8_t)sizeof(unsigned);

        /* Encode the value */
        H5_ENCODE_UNSIGNED(*pp, *(const unsigned *)value)
    } /* end if */

    /* Set size needed for encoding */
    *size += (1 + sizeof(unsigned));

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5P__encode_unsigned() */

/*-------------------------------------------------------------------------
 * Function:       H5P__encode_uint8_t
 *
 * Purpose:        Generic encoding callback routine for 'uint8_t' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Mohamad Chaarawi
 *                 August 07, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__encode_uint8_t(const void *value, void **_pp, size_t *size)
{
    uint8_t **pp = (uint8_t **)_pp;

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity checks */
    HDassert(value);
    HDassert(size);

    if (NULL != *pp) {
        /* Encode the value */
        *(*pp)++ = *(const uint8_t *)value;
    } /* end if */

    /* Set size needed for encoding */
    *size += 1;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5P__encode_uint8_t() */

/*-------------------------------------------------------------------------
 * Function:       H5P__encode_hbool_t
 *
 * Purpose:        Generic encoding callback routine for 'hbool_t' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Quincey Koziol
 *                 August 15, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__encode_hbool_t(const void *value, void **_pp, size_t *size)
{
    uint8_t **pp = (uint8_t **)_pp;

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity checks */
    HDassert(value);
    HDassert(size);

    if (NULL != *pp)
        /* Encode the value */
        *(*pp)++ = (uint8_t) * (const hbool_t *)value;

    /* Set size needed for encoding */
    *size += 1;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5P__encode_hbool_t() */

/*-------------------------------------------------------------------------
 * Function:       H5P__encode_double
 *
 * Purpose:        Generic encoding callback routine for 'double' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Quincey Koziol
 *                 Sunday, July 29, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__encode_double(const void *value, void **_pp, size_t *size)
{
    uint8_t **pp = (uint8_t **)_pp;

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity checks */
    HDassert(value);
    HDassert(size);

    if (NULL != *pp) {
        /* Encode the size */
        *(*pp)++ = (uint8_t)sizeof(double);

        /* Encode the value */
        H5_ENCODE_DOUBLE(*pp, *(const double *)value)
    } /* end if */

    /* Set size needed for encoding */
    *size += (1 + sizeof(double));

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5P__encode_double() */

/*--------------------------------------------------------------------------
 NAME
    H5P__encode_cb
 PURPOSE
    Internal callback routine when iterating over properties while encoding
    a property list.
 USAGE
    int H5P__encode_cb(item, key, udata)
        H5P_genprop_t *prop;        IN: Pointer to the property
        void *udata;                IN/OUT: Pointer to iteration data from user
 RETURNS
    Success: H5_ITER_CONT
    Fail: H5_ITER_ERROR
 DESCRIPTION
    This routine encodes a property in a property list
 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
 EXAMPLES
 REVISION LOG
--------------------------------------------------------------------------*/
static int
H5P__encode_cb(H5P_genprop_t *prop, void *_udata)
{
    H5P_enc_iter_ud_t *udata     = (H5P_enc_iter_ud_t *)_udata; /* Pointer to user data */
    int                ret_value = H5_ITER_CONT;                /* Return value */

    FUNC_ENTER_PACKAGE

    /* Sanity check */
    HDassert(prop);
    HDassert(udata);

    /* Check if this property can be encoded */
    if (prop->encode) {
        size_t prop_name_len;  /* Length of property's name */
        size_t prop_value_len; /* Encoded size of property's value */

        /* Encode (or not, if the 'encode' flag is off) the property's name */
        prop_name_len = HDstrlen(prop->name) + 1;
        if (udata->encode) {
            HDstrcpy((char *)*(udata->pp), prop->name);
            *(uint8_t **)(udata->pp) += prop_name_len;
        } /* end if */
        *(udata->enc_size_ptr) += prop_name_len;

        /* Encode (or not, if *(udata->pp) is NULL) the property value */
        prop_value_len = 0;
        if ((prop->encode)(prop->value, udata->pp, &prop_value_len) < 0)
            HGOTO_ERROR(H5E_PLIST, H5E_CANTENCODE, H5_ITER_ERROR, "property encoding routine failed")
        *(udata->enc_size_ptr) += prop_value_len;
    } /* end if */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5P__encode_cb() */

/*-------------------------------------------------------------------------
 NAME
    H5P__encode
 PURPOSE
    Internal routine to encode a property list into a binary buffer.
 USAGE
    herr_t H5P__encode(plist, enc_all_prop, buf, nalloc)
        const H5P_genplist_t *plist;  IN: Property list to encode
        hbool_t enc_all_prop;  IN: Whether to encode all properties (TRUE),
                or just non-default (i.e. changed) properties (FALSE).
        uint8_t *buf;    OUT: buffer to hold the encoded plist
        size_t *nalloc;  IN/OUT: size of buffer needed to encode plist
 RETURNS
    Returns non-negative on success, negative on failure.
 DESCRIPTION
    Encodes a property list into a binary buffer. If the buffer is NULL, then
    the call will set the size needed to encode the plist in nalloc. Otherwise
    the routine will encode the plist in buf.
 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
 EXAMPLES
 REVISION LOG
--------------------------------------------------------------------------*/
herr_t
H5P__encode(const H5P_genplist_t *plist, hbool_t enc_all_prop, void *buf, size_t *nalloc)
{
    H5P_enc_iter_ud_t udata;                 /* User data for property iteration callback */
    uint8_t          *p = (uint8_t *)buf;    /* Temporary pointer to encoding buffer */
    int               idx;                   /* Index of property to start at */
    size_t            encode_size = 0;       /* Size of buffer needed to encode properties */
    hbool_t           encode      = TRUE;    /* Whether the property list should be encoded */
    herr_t            ret_value   = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Sanity check */
    if (NULL == nalloc)
        HGOTO_ERROR(H5E_PLIST, H5E_BADVALUE, FAIL, "bad allocation size pointer")

    /* If the buffer is NULL, then this call to H5P__encode will return how much
     * space is needed to encode a property.
     */
    if (NULL == p)
        encode = FALSE;

    /* Encode property list description info */
    if (encode) {
        /* Version # of property list encoding */
        *p++ = (uint8_t)H5P_ENCODE_VERS;

        /* Type of property list */
        *p++ = (uint8_t)plist->pclass->type;
    } /* end if */
    encode_size += 2;

    /* Initialize user data for iteration callback */
    udata.encode       = encode;
    udata.enc_size_ptr = &encode_size;
    udata.pp           = (void **)&p;

    /* Iterate over all properties in property list, encoding them */
    idx = 0;
    if (H5P__iterate_plist(plist, enc_all_prop, &idx, H5P__encode_cb, &udata) < 0)
        HGOTO_ERROR(H5E_PLIST, H5E_BADITER, FAIL, "can't iterate over properties")

    /* Encode a terminator for list of properties */
    if (encode)
        *p++ = 0;
    encode_size++;

    /* Set the size of the buffer needed/used to encode the property list */
    *nalloc = encode_size;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5P__encode() */

/*-------------------------------------------------------------------------
 * Function:       H5P__decode_size_t
 *
 * Purpose:        Generic decoding callback routine for 'size_t' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Quincey Koziol
 *                 Thursday, August 2, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__decode_size_t(const void **_pp, void *_value)
{
    size_t         *value = (size_t *)_value; /* Property value to return */
    const uint8_t **pp    = (const uint8_t **)_pp;
    uint64_t        enc_value; /* Decoded property value */
    unsigned        enc_size;  /* Size of encoded property */

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity check */
    HDcompile_assert(sizeof(size_t) <= sizeof(uint64_t));
    HDassert(pp);
    HDassert(*pp);
    HDassert(value);

    /* Decode the size */
    enc_size = *(*pp)++;
    HDassert(enc_size < 256);

    /* Decode the value */
    UINT64DECODE_VAR(*pp, enc_value, enc_size);
    H5_CHECKED_ASSIGN(*value, size_t, enc_value, uint64_t);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5P__decode_size_t() */

/*-------------------------------------------------------------------------
 * Function:       H5P__decode_hsize_t
 *
 * Purpose:        Generic decoding callback routine for 'hsize_t' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Mohamad Chaarawi
 *                 August 07, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__decode_hsize_t(const void **_pp, void *_value)
{
    hsize_t        *value = (hsize_t *)_value; /* Property value to return */
    const uint8_t **pp    = (const uint8_t **)_pp;
    uint64_t        enc_value; /* Decoded property value */
    unsigned        enc_size;  /* Size of encoded property */

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity check */
    HDcompile_assert(sizeof(hsize_t) <= sizeof(uint64_t));
    HDassert(pp);
    HDassert(*pp);
    HDassert(value);

    /* Decode the size */
    enc_size = *(*pp)++;
    HDassert(enc_size < 256);

    /* Decode the value */
    UINT64DECODE_VAR(*pp, enc_value, enc_size);
    H5_CHECKED_ASSIGN(*value, hsize_t, enc_value, uint64_t);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5P__decode_hsize_t() */

/*-------------------------------------------------------------------------
 * Function:       H5P__decode_unsigned
 *
 * Purpose:        Generic decoding callback routine for 'unsigned' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Quincey Koziol
 *                 Thursday, August 2, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__decode_unsigned(const void **_pp, void *_value)
{
    unsigned       *value = (unsigned *)_value; /* Property value to return */
    const uint8_t **pp    = (const uint8_t **)_pp;
    unsigned        enc_size;            /* Size of encoded property */
    herr_t          ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Sanity checks */
    HDassert(pp);
    HDassert(*pp);
    HDassert(value);

    /* Decode the size */
    enc_size = *(*pp)++;
    if (enc_size != sizeof(unsigned))
        HGOTO_ERROR(H5E_PLIST, H5E_BADVALUE, FAIL, "unsigned value can't be decoded")

    H5_DECODE_UNSIGNED(*pp, *value)

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5P__decode_unsigned() */

/*-------------------------------------------------------------------------
 * Function:       H5P__decode_uint8_t
 *
 * Purpose:        Generic decoding callback routine for 'uint8_t' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Quincey Koziol
 *                 Thursday, August 2, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__decode_uint8_t(const void **_pp, void *_value)
{
    uint8_t        *value     = (uint8_t *)_value; /* Property value to return */
    const uint8_t **pp        = (const uint8_t **)_pp;
    herr_t          ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity checks */
    HDassert(pp);
    HDassert(*pp);
    HDassert(value);

    /* Decode the value */
    *value = *(*pp)++;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5P__decode_uint8_t() */

/*-------------------------------------------------------------------------
 * Function:       H5P__decode_hbool_t
 *
 * Purpose:        Generic decoding callback routine for 'hbool_t' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Quincey Koziol
 *                 Wednesday, August 15, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__decode_hbool_t(const void **_pp, void *_value)
{
    hbool_t        *value     = (hbool_t *)_value; /* Property value to return */
    const uint8_t **pp        = (const uint8_t **)_pp;
    herr_t          ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE_NOERR

    /* Sanity checks */
    HDassert(pp);
    HDassert(*pp);
    HDassert(value);

    /* Decode the value */
    *value = (hbool_t) * (*pp)++;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5P__decode_hbool_t() */

/*-------------------------------------------------------------------------
 * Function:       H5P__decode_double
 *
 * Purpose:        Generic decoding callback routine for 'double' properties.
 *
 * Return:	   Success:	Non-negative
 *		   Failure:	Negative
 *
 * Programmer:     Quincey Koziol
 *                 Thursday, August 2, 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5P__decode_double(const void **_pp, void *_value)
{
    double         *value = (double *)_value; /* Property value to return */
    const uint8_t **pp    = (const uint8_t **)_pp;
    unsigned        enc_size;            /* Size of encoded property */
    herr_t          ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Sanity checks */
    HDassert(pp);
    HDassert(*pp);
    HDassert(value);

    /* Decode the size */
    enc_size = *(*pp)++;
    if (enc_size != sizeof(double))
        HGOTO_ERROR(H5E_PLIST, H5E_BADVALUE, FAIL, "double value can't be decoded")

    H5_DECODE_DOUBLE(*pp, *value)

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5P__decode_double() */

/*-------------------------------------------------------------------------
 NAME
    H5P__decode
 PURPOSE
    Internal routine to decode a property list from a binary buffer.
 USAGE
    H5P_genplist_t *H5P__decode(buf)
        const void *buf;    IN: buffer that holds the encoded plist
 RETURNS
    Returns non-negative ID of new property list object on success, negative
        on failure.
 DESCRIPTION
     Decodes a property list from a binary buffer. The contents of the buffer
     contain the values for the corresponding properties of the plist. The decode
     callback of a certain property decodes its value from the buffer and sets it
     in the property list.
 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
     Properties in the property list that are not encoded in the serialized
     form retain their default value.
 EXAMPLES
 REVISION LOG
--------------------------------------------------------------------------*/
hid_t
H5P__decode(const void *buf)
{
    H5P_genplist_t  *plist;                            /* Property list to decode into */
    void            *value_buf = NULL;                 /* Pointer to buffer to use when decoding values */
    const uint8_t   *p         = (const uint8_t *)buf; /* Current pointer into buffer */
    H5P_plist_type_t type;                             /* Type of encoded property list */
    hid_t            plist_id       = -1;              /* ID of new property list */
    size_t           value_buf_size = 0;               /* Size of current value buffer */
    uint8_t          vers;                             /* Version of encoded property list */
    hid_t            ret_value = H5I_INVALID_HID;      /* Return value */

    FUNC_ENTER_PACKAGE

    /* Sanity check */
    if (NULL == p)
        HGOTO_ERROR(H5E_PLIST, H5E_BADVALUE, FAIL, "decode buffer is NULL")

    /* Get the version number of the encoded property list */
    vers = (uint8_t)*p++;
    if ((uint8_t)H5P_ENCODE_VERS != vers)
        HGOTO_ERROR(H5E_PLIST, H5E_VERSION, FAIL, "bad version # of encoded information, expected %u, got %u",
                    (unsigned)H5P_ENCODE_VERS, (unsigned)vers)

    /* Get the type of the property list */
    type = (H5P_plist_type_t)*p++;
    if (type <= H5P_TYPE_USER || type >= H5P_TYPE_MAX_TYPE)
        HGOTO_ERROR(H5E_PLIST, H5E_BADRANGE, FAIL, "bad type of encoded information: %u", (unsigned)type)

    /* Create new property list of the specified type */
    if ((plist_id = H5P__new_plist_of_type(type)) < 0)
        HGOTO_ERROR(H5E_PLIST, H5E_VERSION, FAIL, "can't create property list of type: %u\n", (unsigned)type);

    /* Get the property list object */
    if (NULL == (plist = (H5P_genplist_t *)H5I_object(plist_id)))
        HGOTO_ERROR(H5E_PLIST, H5E_BADTYPE, FAIL, "not a property class")

    /* Loop over encoded properties, deserializing their values */
    while (p) {
        H5P_genprop_t *prop; /* Pointer to property with same name */
        const char    *name; /* Pointer to property list name */

        /* Check for end of serialized list of properties */
        if (0 == *p)
            break;

        /* Get property list name */
        name = (const char *)p;
        p += HDstrlen(name) + 1;

        /* Find property with name */
        if (NULL == (prop = H5P__find_prop_plist(plist, name)))
            HGOTO_ERROR(H5E_PLIST, H5E_NOTFOUND, FAIL, "property doesn't exist: '%s'", name)

        /* Check if we should increase the size of the value buffer */
        if (prop->size > value_buf_size) {
            if (NULL == (value_buf = H5MM_realloc(value_buf, prop->size)))
                HGOTO_ERROR(H5E_PLIST, H5E_CANTALLOC, FAIL, "decoding buffer allocation failed")
            value_buf_size = prop->size;
        } /* end if */

        /* Decode serialized value */
        if (prop->decode) {
            if ((prop->decode)((const void **)&p, value_buf) < 0)
                HGOTO_ERROR(H5E_PLIST, H5E_CANTDECODE, FAIL,
                            "property decoding routine failed, property: '%s'", name)
        } /* end if */
        else
            HGOTO_ERROR(H5E_PLIST, H5E_NOTFOUND, FAIL, "no decode callback for property: '%s'", name)

        /* Set the value for the property */
        if (H5P_poke(plist, name, value_buf) < 0)
            HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "unable to set value for property: '%s'", name)
    } /* end while */

    /* Set return value */
    ret_value = plist_id;

done:
    /* Release resources */
    if (value_buf)
        value_buf = H5MM_xfree(value_buf);

    /* Cleanup on error */
    if (ret_value < 0) {
        if (plist_id > 0 && H5I_dec_ref(plist_id) < 0)
            HDONE_ERROR(H5E_PLIST, H5E_CANTCLOSEOBJ, FAIL,
                        "unable to close partially initialized property list")
    } /* end if */

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5P__decode() */