Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-18 09:16:01

0001 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
0002  * Copyright by The HDF Group.                                               *
0003  * All rights reserved.                                                      *
0004  *                                                                           *
0005  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
0006  * terms governing use, modification, and redistribution, is contained in    *
0007  * the COPYING file, which can be found at the root of the source code       *
0008  * distribution tree, or in https://www.hdfgroup.org/licenses.               *
0009  * If you do not have access to either file, you may request a copy from     *
0010  * help@hdfgroup.org.                                                        *
0011  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
0012 
0013 /*
0014  * File-independent encode/decode routines
0015  */
0016 
0017 #ifndef H5encode_H
0018 #define H5encode_H
0019 
0020 /***********/
0021 /* Headers */
0022 /***********/
0023 #include "H5MMprivate.h" /* Memory management                        */
0024 
0025 /**************************/
0026 /* Library Private Macros */
0027 /**************************/
0028 
0029 /*
0030  * Encode and decode macros for file meta-data.
0031  * Currently, all file meta-data is little-endian.
0032  */
0033 
0034 #define INT16ENCODE(p, i)                                                                                    \
0035     do {                                                                                                     \
0036         *(p) = (uint8_t)((unsigned)(i)&0xff);                                                                \
0037         (p)++;                                                                                               \
0038         *(p) = (uint8_t)(((unsigned)(i) >> 8) & 0xff);                                                       \
0039         (p)++;                                                                                               \
0040     } while (0)
0041 
0042 #define UINT16ENCODE(p, i)                                                                                   \
0043     do {                                                                                                     \
0044         *(p) = (uint8_t)((unsigned)(i)&0xff);                                                                \
0045         (p)++;                                                                                               \
0046         *(p) = (uint8_t)(((unsigned)(i) >> 8) & 0xff);                                                       \
0047         (p)++;                                                                                               \
0048     } while (0)
0049 
0050 #define INT32ENCODE(p, i)                                                                                    \
0051     do {                                                                                                     \
0052         *(p) = (uint8_t)((uint32_t)(i)&0xff);                                                                \
0053         (p)++;                                                                                               \
0054         *(p) = (uint8_t)(((uint32_t)(i) >> 8) & 0xff);                                                       \
0055         (p)++;                                                                                               \
0056         *(p) = (uint8_t)(((uint32_t)(i) >> 16) & 0xff);                                                      \
0057         (p)++;                                                                                               \
0058         *(p) = (uint8_t)(((uint32_t)(i) >> 24) & 0xff);                                                      \
0059         (p)++;                                                                                               \
0060     } while (0)
0061 
0062 #define UINT32ENCODE(p, i)                                                                                   \
0063     do {                                                                                                     \
0064         *(p) = (uint8_t)((i)&0xff);                                                                          \
0065         (p)++;                                                                                               \
0066         *(p) = (uint8_t)(((i) >> 8) & 0xff);                                                                 \
0067         (p)++;                                                                                               \
0068         *(p) = (uint8_t)(((i) >> 16) & 0xff);                                                                \
0069         (p)++;                                                                                               \
0070         *(p) = (uint8_t)(((i) >> 24) & 0xff);                                                                \
0071         (p)++;                                                                                               \
0072     } while (0)
0073 
0074 /* Encode an unsigned integer into a variable-sized buffer */
0075 /* (Assumes that the high bits of the integer are zero) */
0076 #define ENCODE_VAR(p, typ, n, l)                                                                             \
0077     do {                                                                                                     \
0078         typ      _n = (n);                                                                                   \
0079         size_t   _i;                                                                                         \
0080         uint8_t *_p = (uint8_t *)(p);                                                                        \
0081                                                                                                              \
0082         for (_i = 0; _i < l; _i++, _n >>= 8)                                                                 \
0083             *_p++ = (uint8_t)(_n & 0xff);                                                                    \
0084         (p) = (uint8_t *)(p) + l;                                                                            \
0085     } while (0)
0086 
0087 /* Encode a 32-bit unsigned integer into a variable-sized buffer */
0088 /* (Assumes that the high bits of the integer are zero) */
0089 #define UINT32ENCODE_VAR(p, n, l) ENCODE_VAR(p, uint32_t, n, l)
0090 
0091 #define INT64ENCODE(p, n)                                                                                    \
0092     do {                                                                                                     \
0093         int64_t  _n = (n);                                                                                   \
0094         size_t   _i;                                                                                         \
0095         uint8_t *_p = (uint8_t *)(p);                                                                        \
0096                                                                                                              \
0097         for (_i = 0; _i < sizeof(int64_t); _i++, _n >>= 8)                                                   \
0098             *_p++ = (uint8_t)(_n & 0xff);                                                                    \
0099         for (/*void*/; _i < 8; _i++)                                                                         \
0100             *_p++ = (uint8_t)((n) < 0 ? 0xff : 0);                                                           \
0101         (p) = (uint8_t *)(p) + 8;                                                                            \
0102     } while (0)
0103 
0104 #define UINT64ENCODE(p, n)                                                                                   \
0105     do {                                                                                                     \
0106         uint64_t _n = (n);                                                                                   \
0107         size_t   _i;                                                                                         \
0108         uint8_t *_p = (uint8_t *)(p);                                                                        \
0109                                                                                                              \
0110         for (_i = 0; _i < sizeof(uint64_t); _i++, _n >>= 8)                                                  \
0111             *_p++ = (uint8_t)(_n & 0xff);                                                                    \
0112         for (/*void*/; _i < 8; _i++)                                                                         \
0113             *_p++ = 0;                                                                                       \
0114         (p) = (uint8_t *)(p) + 8;                                                                            \
0115     } while (0)
0116 
0117 /* Encode a 64-bit unsigned integer into a variable-sized buffer */
0118 /* (Assumes that the high bits of the integer are zero) */
0119 #define UINT64ENCODE_VAR(p, n, l) ENCODE_VAR(p, uint64_t, n, l)
0120 
0121 #define H5_ENCODE_UNSIGNED(p, n)                                                                             \
0122     do {                                                                                                     \
0123         HDcompile_assert(sizeof(unsigned) == sizeof(uint32_t));                                              \
0124         UINT32ENCODE(p, n);                                                                                  \
0125     } while (0)
0126 
0127 /* Assumes the endianness of uint64_t is the same as double */
0128 #define H5_ENCODE_DOUBLE(p, n)                                                                               \
0129     do {                                                                                                     \
0130         uint64_t _n;                                                                                         \
0131         size_t   _u;                                                                                         \
0132         uint8_t *_p = (uint8_t *)(p);                                                                        \
0133                                                                                                              \
0134         HDcompile_assert(sizeof(double) == 8);                                                               \
0135         HDcompile_assert(sizeof(double) == sizeof(uint64_t));                                                \
0136         H5MM_memcpy(&_n, &n, sizeof(double));                                                                \
0137         for (_u = 0; _u < sizeof(uint64_t); _u++, _n >>= 8)                                                  \
0138             *_p++ = (uint8_t)(_n & 0xff);                                                                    \
0139         (p) = (uint8_t *)(p) + 8;                                                                            \
0140     } while (0)
0141 
0142 /* DECODE converts little endian bytes pointed by p to integer values and store
0143  * it in i.  For signed values, need to do sign-extension when converting
0144  * the last byte which carries the sign bit.
0145  * The macros does not require i be of a certain byte sizes.  It just requires
0146  * i be big enough to hold the intended value range.  E.g. INT16DECODE works
0147  * correctly even if i is actually a 64bit int like in a Cray.
0148  */
0149 
0150 #define INT16DECODE(p, i)                                                                                    \
0151     do {                                                                                                     \
0152         (i) = (int16_t)((*(p)&0xff));                                                                        \
0153         (p)++;                                                                                               \
0154         (i) |= (int16_t)(((*(p)&0xff) << 8) | ((*(p)&0x80) ? ~0xffff : 0x0));                                \
0155         (p)++;                                                                                               \
0156     } while (0)
0157 
0158 #define UINT16DECODE(p, i)                                                                                   \
0159     do {                                                                                                     \
0160         (i) = (uint16_t)(*(p)&0xff);                                                                         \
0161         (p)++;                                                                                               \
0162         (i) |= (uint16_t)((*(p)&0xff) << 8);                                                                 \
0163         (p)++;                                                                                               \
0164     } while (0)
0165 
0166 #define INT32DECODE(p, i)                                                                                    \
0167     do {                                                                                                     \
0168         (i) = ((int32_t)(*(p)&0xff));                                                                        \
0169         (p)++;                                                                                               \
0170         (i) |= ((int32_t)(*(p)&0xff) << 8);                                                                  \
0171         (p)++;                                                                                               \
0172         (i) |= ((int32_t)(*(p)&0xff) << 16);                                                                 \
0173         (p)++;                                                                                               \
0174         (i) |= ((int32_t)(((*(p) & (unsigned)0xff) << 24) | ((*(p)&0x80) ? ~0xffffffffULL : 0x0ULL)));       \
0175         (p)++;                                                                                               \
0176     } while (0)
0177 
0178 #define UINT32DECODE(p, i)                                                                                   \
0179     do {                                                                                                     \
0180         (i) = (uint32_t)(*(p)&0xff);                                                                         \
0181         (p)++;                                                                                               \
0182         (i) |= ((uint32_t)(*(p)&0xff) << 8);                                                                 \
0183         (p)++;                                                                                               \
0184         (i) |= ((uint32_t)(*(p)&0xff) << 16);                                                                \
0185         (p)++;                                                                                               \
0186         (i) |= ((uint32_t)(*(p)&0xff) << 24);                                                                \
0187         (p)++;                                                                                               \
0188     } while (0)
0189 
0190 /* Decode a variable-sized buffer */
0191 /* (Assumes that the high bits of the integer will be zero) */
0192 #define DECODE_VAR(p, n, l)                                                                                  \
0193     do {                                                                                                     \
0194         size_t _i;                                                                                           \
0195                                                                                                              \
0196         n = 0;                                                                                               \
0197         (p) += l;                                                                                            \
0198         for (_i = 0; _i < l; _i++)                                                                           \
0199             n = (n << 8) | *(--p);                                                                           \
0200         (p) += l;                                                                                            \
0201     } while (0)
0202 
0203 /* Decode a variable-sized buffer into a 32-bit unsigned integer */
0204 /* (Assumes that the high bits of the integer will be zero) */
0205 #define UINT32DECODE_VAR(p, n, l) DECODE_VAR(p, n, l)
0206 
0207 #define INT64DECODE(p, n)                                                                                    \
0208     do {                                                                                                     \
0209         /* WE DON'T CHECK FOR OVERFLOW! */                                                                   \
0210         size_t _i;                                                                                           \
0211                                                                                                              \
0212         n = 0;                                                                                               \
0213         (p) += 8;                                                                                            \
0214         for (_i = 0; _i < sizeof(int64_t); _i++)                                                             \
0215             n = (int64_t)(((uint64_t)n << 8) | *(--p));                                                      \
0216         (p) += 8;                                                                                            \
0217     } while (0)
0218 
0219 #define UINT64DECODE(p, n)                                                                                   \
0220     do {                                                                                                     \
0221         /* WE DON'T CHECK FOR OVERFLOW! */                                                                   \
0222         size_t _i;                                                                                           \
0223                                                                                                              \
0224         n = 0;                                                                                               \
0225         (p) += 8;                                                                                            \
0226         for (_i = 0; _i < sizeof(uint64_t); _i++)                                                            \
0227             n = (uint64_t)(((uint64_t)n << 8) | *(--p));                                                     \
0228         (p) += 8;                                                                                            \
0229     } while (0)
0230 
0231 /* Decode a variable-sized buffer into a 64-bit unsigned integer */
0232 /* (Assumes that the high bits of the integer will be zero) */
0233 #define UINT64DECODE_VAR(p, n, l) DECODE_VAR(p, n, l)
0234 
0235 #define H5_DECODE_UNSIGNED(p, n)                                                                             \
0236     do {                                                                                                     \
0237         HDcompile_assert(sizeof(unsigned) == sizeof(uint32_t));                                              \
0238         UINT32DECODE(p, n);                                                                                  \
0239     } while (0)
0240 
0241 /* Assumes the endianness of uint64_t is the same as double */
0242 #define H5_DECODE_DOUBLE(p, n)                                                                               \
0243     do {                                                                                                     \
0244         uint64_t _n;                                                                                         \
0245         size_t   _u;                                                                                         \
0246                                                                                                              \
0247         HDcompile_assert(sizeof(double) == 8);                                                               \
0248         HDcompile_assert(sizeof(double) == sizeof(uint64_t));                                                \
0249         _n = 0;                                                                                              \
0250         (p) += 8;                                                                                            \
0251         for (_u = 0; _u < sizeof(uint64_t); _u++)                                                            \
0252             _n = (_n << 8) | *(--p);                                                                         \
0253         H5MM_memcpy(&(n), &_n, sizeof(double));                                                              \
0254         (p) += 8;                                                                                            \
0255     } while (0)
0256 
0257 /* Macros to encode/decode offset/length's for storing in the file */
0258 #define H5_ENCODE_LENGTH_LEN(p, l, s)                                                                        \
0259     do {                                                                                                     \
0260         switch (s) {                                                                                         \
0261             case 4:                                                                                          \
0262                 UINT32ENCODE(p, l);                                                                          \
0263                 break;                                                                                       \
0264             case 8:                                                                                          \
0265                 UINT64ENCODE(p, l);                                                                          \
0266                 break;                                                                                       \
0267             case 2:                                                                                          \
0268                 UINT16ENCODE(p, l);                                                                          \
0269                 break;                                                                                       \
0270             default:                                                                                         \
0271                 assert("bad sizeof size" && 0);                                                              \
0272         }                                                                                                    \
0273     } while (0)
0274 
0275 #define H5_DECODE_LENGTH_LEN(p, l, s)                                                                        \
0276     do {                                                                                                     \
0277         switch (s) {                                                                                         \
0278             case 4:                                                                                          \
0279                 UINT32DECODE(p, l);                                                                          \
0280                 break;                                                                                       \
0281             case 8:                                                                                          \
0282                 UINT64DECODE(p, l);                                                                          \
0283                 break;                                                                                       \
0284             case 2:                                                                                          \
0285                 UINT16DECODE(p, l);                                                                          \
0286                 break;                                                                                       \
0287             default:                                                                                         \
0288                 assert("bad sizeof size" && 0);                                                              \
0289         }                                                                                                    \
0290     } while (0)
0291 
0292 #endif /* H5encode_H */