store_bytes.h (1864B)
1 /* Copyright 2013 Google Inc. All Rights Reserved. 2 3 Distributed under MIT license. 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5 */ 6 7 /* Helper functions for storing integer values into byte streams. 8 No bounds checking is performed, that is the responsibility of the caller. */ 9 10 #ifndef WOFF2_STORE_BYTES_H_ 11 #define WOFF2_STORE_BYTES_H_ 12 13 #include <inttypes.h> 14 #include <stddef.h> 15 #include <string.h> 16 17 #include "./port.h" 18 19 namespace woff2 { 20 21 inline size_t StoreU32(uint8_t* dst, size_t offset, uint32_t x) { 22 dst[offset] = x >> 24; 23 dst[offset + 1] = x >> 16; 24 dst[offset + 2] = x >> 8; 25 dst[offset + 3] = x; 26 return offset + 4; 27 } 28 29 inline size_t Store16(uint8_t* dst, size_t offset, int x) { 30 #if defined(WOFF_LITTLE_ENDIAN) 31 *reinterpret_cast<uint16_t*>(dst + offset) = 32 ((x & 0xFF) << 8) | ((x & 0xFF00) >> 8); 33 #elif defined(WOFF_BIG_ENDIAN) 34 *reinterpret_cast<uint16_t*>(dst + offset) = static_cast<uint16_t>(x); 35 #else 36 dst[offset] = x >> 8; 37 dst[offset + 1] = x; 38 #endif 39 return offset + 2; 40 } 41 42 inline void StoreU32(uint32_t val, size_t* offset, uint8_t* dst) { 43 dst[(*offset)++] = val >> 24; 44 dst[(*offset)++] = val >> 16; 45 dst[(*offset)++] = val >> 8; 46 dst[(*offset)++] = val; 47 } 48 49 inline void Store16(int val, size_t* offset, uint8_t* dst) { 50 #if defined(WOFF_LITTLE_ENDIAN) 51 *reinterpret_cast<uint16_t*>(dst + *offset) = 52 ((val & 0xFF) << 8) | ((val & 0xFF00) >> 8); 53 *offset += 2; 54 #elif defined(WOFF_BIG_ENDIAN) 55 *reinterpret_cast<uint16_t*>(dst + *offset) = static_cast<uint16_t>(val); 56 *offset += 2; 57 #else 58 dst[(*offset)++] = val >> 8; 59 dst[(*offset)++] = val; 60 #endif 61 } 62 63 inline void StoreBytes(const uint8_t* data, size_t len, 64 size_t* offset, uint8_t* dst) { 65 memcpy(&dst[*offset], data, len); 66 *offset += len; 67 } 68 69 } // namespace woff2 70 71 #endif // WOFF2_STORE_BYTES_H_