1 /* 2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the License); you may 5 * not use this file except in compliance with the License. 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 */ 9 10 11 #ifndef GMSSL_ENDIAN_H 12 #define GMSSL_ENDIAN_H 13 14 15 /* Big Endian R/W */ 16 17 #define GETU16(p) \ 18 ((uint16_t)(p)[0] << 8 | \ 19 (uint16_t)(p)[1]) 20 21 #define GETU32(p) \ 22 ((uint32_t)(p)[0] << 24 | \ 23 (uint32_t)(p)[1] << 16 | \ 24 (uint32_t)(p)[2] << 8 | \ 25 (uint32_t)(p)[3]) 26 27 #define GETU64(p) \ 28 ((uint64_t)(p)[0] << 56 | \ 29 (uint64_t)(p)[1] << 48 | \ 30 (uint64_t)(p)[2] << 40 | \ 31 (uint64_t)(p)[3] << 32 | \ 32 (uint64_t)(p)[4] << 24 | \ 33 (uint64_t)(p)[5] << 16 | \ 34 (uint64_t)(p)[6] << 8 | \ 35 (uint64_t)(p)[7]) 36 37 38 // 注意:PUTU32(buf, val++) 会出错! 39 #define PUTU16(p,V) \ 40 ((p)[0] = (uint8_t)((V) >> 8), \ 41 (p)[1] = (uint8_t)(V)) 42 43 #define PUTU32(p,V) \ 44 ((p)[0] = (uint8_t)((V) >> 24), \ 45 (p)[1] = (uint8_t)((V) >> 16), \ 46 (p)[2] = (uint8_t)((V) >> 8), \ 47 (p)[3] = (uint8_t)(V)) 48 49 #define PUTU64(p,V) \ 50 ((p)[0] = (uint8_t)((V) >> 56), \ 51 (p)[1] = (uint8_t)((V) >> 48), \ 52 (p)[2] = (uint8_t)((V) >> 40), \ 53 (p)[3] = (uint8_t)((V) >> 32), \ 54 (p)[4] = (uint8_t)((V) >> 24), \ 55 (p)[5] = (uint8_t)((V) >> 16), \ 56 (p)[6] = (uint8_t)((V) >> 8), \ 57 (p)[7] = (uint8_t)(V)) 58 59 /* Little Endian R/W */ 60 61 #define GETU16_LE(p) (*(const uint16_t *)(p)) 62 #define GETU32_LE(p) (*(const uint32_t *)(p)) 63 #define GETU64_LE(p) (*(const uint64_t *)(p)) 64 65 #define PUTU16_LE(p,V) *(uint16_t *)(p) = (V) 66 #define PUTU32_LE(p,V) *(uint32_t *)(p) = (V) 67 #define PUTU64_LE(p,V) *(uint64_t *)(p) = (V) 68 69 /* Rotate */ 70 71 #define ROL32(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) 72 #define ROL64(a,n) (((a)<<(n))|((a)>>(64-(n)))) 73 74 #define ROR32(a,n) ROL32((a),32-(n)) 75 #define ROR64(a,n) ROL64(a,64-n) 76 77 78 #endif 79