webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
store_bytes.h
Go to the documentation of this file.
1 // Copyright 2013 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Helper functions for storing integer values into byte streams.
16 // No bounds checking is performed, that is the responsibility of the caller.
17 
18 #ifndef WOFF2_STORE_BYTES_H_
19 #define WOFF2_STORE_BYTES_H_
20 
21 #include <inttypes.h>
22 #include <stddef.h>
23 #include <string.h>
24 
25 namespace woff2 {
26 
27 inline size_t StoreU32(uint8_t* dst, size_t offset, uint32_t x) {
28  dst[offset] = x >> 24;
29  dst[offset + 1] = x >> 16;
30  dst[offset + 2] = x >> 8;
31  dst[offset + 3] = x;
32  return offset + 4;
33 }
34 
35 inline size_t Store16(uint8_t* dst, size_t offset, int x) {
36 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
37  *reinterpret_cast<uint16_t*>(dst + offset) =
38  ((x & 0xFF) << 8) | ((x & 0xFF00) >> 8);
39 #elif (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
40  *reinterpret_cast<uint16_t*>(dst + offset) = static_cast<uint16_t>(x);
41 #else
42  dst[offset] = x >> 8;
43  dst[offset + 1] = x;
44 #endif
45  return offset + 2;
46 }
47 
48 inline void StoreU32(uint32_t val, size_t* offset, uint8_t* dst) {
49  dst[(*offset)++] = val >> 24;
50  dst[(*offset)++] = val >> 16;
51  dst[(*offset)++] = val >> 8;
52  dst[(*offset)++] = val;
53 }
54 
55 inline void Store16(int val, size_t* offset, uint8_t* dst) {
56 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
57  *reinterpret_cast<uint16_t*>(dst + *offset) =
58  ((val & 0xFF) << 8) | ((val & 0xFF00) >> 8);
59  *offset += 2;
60 #elif (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
61  *reinterpret_cast<uint16_t*>(dst + *offset) = static_cast<uint16_t>(val);
62  *offset += 2;
63 #else
64  dst[(*offset)++] = val >> 8;
65  dst[(*offset)++] = val;
66 #endif
67 }
68 
69 inline void StoreBytes(const uint8_t* data, size_t len,
70  size_t* offset, uint8_t* dst) {
71  memcpy(&dst[*offset], data, len);
72  *offset += len;
73 }
74 
75 } // namespace woff2
76 
77 #endif // WOFF2_STORE_BYTES_H_
unsigned int uint32_t
Definition: ptypes.h:105
size_t StoreU32(uint8_t *dst, size_t offset, uint32_t x)
Definition: store_bytes.h:27
void StoreBytes(const uint8_t *data, size_t len, size_t *offset, uint8_t *dst)
Definition: store_bytes.h:69
GLenum GLenum dst
Definition: gl2ext.h:304
EGLStreamKHR EGLint EGLint offset
Definition: eglext.h:984
OPENSSL_EXPORT const ASN1_OBJECT int const unsigned char int len
Definition: x509.h:1053
EGLSurface EGLint x
Definition: eglext.h:950
unsigned char uint8_t
Definition: ptypes.h:89
size_t Store16(uint8_t *dst, size_t offset, int x)
Definition: store_bytes.h:35
unsigned short uint16_t
Definition: ptypes.h:97
EGLStreamKHR EGLint EGLint EGLint const void * data
Definition: eglext.h:984
GLuint GLsizei GLsizei GLfloat * val
Definition: gl2ext.h:3301
Definition: buffer.h:45