/src/mozilla-central/modules/woff2/src/store_bytes.h
Line | Count | Source (jump to first uncovered line) |
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 | 0 | inline size_t StoreU32(uint8_t* dst, size_t offset, uint32_t x) { |
22 | 0 | dst[offset] = x >> 24; |
23 | 0 | dst[offset + 1] = x >> 16; |
24 | 0 | dst[offset + 2] = x >> 8; |
25 | 0 | dst[offset + 3] = x; |
26 | 0 | return offset + 4; |
27 | 0 | } |
28 | | |
29 | 0 | inline size_t Store16(uint8_t* dst, size_t offset, int x) { |
30 | 0 | #if defined(WOFF_LITTLE_ENDIAN) |
31 | 0 | *reinterpret_cast<uint16_t*>(dst + offset) = |
32 | 0 | ((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 | 0 | } |
41 | | |
42 | 0 | inline void StoreU32(uint32_t val, size_t* offset, uint8_t* dst) { |
43 | 0 | dst[(*offset)++] = val >> 24; |
44 | 0 | dst[(*offset)++] = val >> 16; |
45 | 0 | dst[(*offset)++] = val >> 8; |
46 | 0 | dst[(*offset)++] = val; |
47 | 0 | } |
48 | | |
49 | 0 | inline void Store16(int val, size_t* offset, uint8_t* dst) { |
50 | 0 | #if defined(WOFF_LITTLE_ENDIAN) |
51 | 0 | *reinterpret_cast<uint16_t*>(dst + *offset) = |
52 | 0 | ((val & 0xFF) << 8) | ((val & 0xFF00) >> 8); |
53 | 0 | *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 | 0 | size_t* offset, uint8_t* dst) { |
65 | 0 | memcpy(&dst[*offset], data, len); |
66 | 0 | *offset += len; |
67 | 0 | } |
68 | | |
69 | | } // namespace woff2 |
70 | | |
71 | | #endif // WOFF2_STORE_BYTES_H_ |