1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BYTE_UTILS_H
18 
19 #define BYTE_UTILS_H
20 
21 #include <arpa/inet.h>
22 
23 namespace android {
24 
FOURCC(unsigned char c1,unsigned char c2,unsigned char c3,unsigned char c4)25 constexpr int FOURCC(unsigned char c1, unsigned char c2, unsigned char c3, unsigned char c4) {
26     return ((c1) << 24 | (c2) << 16 | (c3) << 8 | (c4));
27 }
28 
29 template <size_t N>
FOURCC(const char (& s)[N])30 constexpr int32_t FOURCC(const char (&s) [N]) {
31     static_assert(N == 5, "fourcc: wrong length");
32     return
33             (unsigned char) s[0] << 24 |
34             (unsigned char) s[1] << 16 |
35             (unsigned char) s[2] << 8 |
36             (unsigned char) s[3] << 0;
37 }
38 
39 
40 uint16_t U16_AT(const uint8_t *ptr);
41 uint32_t U32_AT(const uint8_t *ptr);
42 uint64_t U64_AT(const uint8_t *ptr);
43 
44 uint16_t U16LE_AT(const uint8_t *ptr);
45 uint32_t U32LE_AT(const uint8_t *ptr);
46 uint64_t U64LE_AT(const uint8_t *ptr);
47 
48 uint64_t ntoh64(uint64_t x);
49 uint64_t hton64(uint64_t x);
50 
51 void MakeFourCCString(uint32_t x, char *s);
52 
53 }  // namespace android
54 
55 #endif  // BYTE_UTILS_H
56 
57