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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ByteUtils"
19 
20 #include <media/stagefright/foundation/ByteUtils.h>
21 
22 namespace android {
23 
U16_AT(const uint8_t * ptr)24 uint16_t U16_AT(const uint8_t *ptr) {
25     return ptr[0] << 8 | ptr[1];
26 }
27 
U32_AT(const uint8_t * ptr)28 uint32_t U32_AT(const uint8_t *ptr) {
29     return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
30 }
31 
U64_AT(const uint8_t * ptr)32 uint64_t U64_AT(const uint8_t *ptr) {
33     return ((uint64_t)U32_AT(ptr)) << 32 | U32_AT(ptr + 4);
34 }
35 
U16LE_AT(const uint8_t * ptr)36 uint16_t U16LE_AT(const uint8_t *ptr) {
37     return ptr[0] | (ptr[1] << 8);
38 }
39 
U32LE_AT(const uint8_t * ptr)40 uint32_t U32LE_AT(const uint8_t *ptr) {
41     return ptr[3] << 24 | ptr[2] << 16 | ptr[1] << 8 | ptr[0];
42 }
43 
U64LE_AT(const uint8_t * ptr)44 uint64_t U64LE_AT(const uint8_t *ptr) {
45     return ((uint64_t)U32LE_AT(ptr + 4)) << 32 | U32LE_AT(ptr);
46 }
47 
48 // XXX warning: these won't work on big-endian host.
ntoh64(uint64_t x)49 uint64_t ntoh64(uint64_t x) {
50     return ((uint64_t)ntohl(x & 0xffffffff) << 32) | ntohl(x >> 32);
51 }
52 
hton64(uint64_t x)53 uint64_t hton64(uint64_t x) {
54     return ((uint64_t)htonl(x & 0xffffffff) << 32) | htonl(x >> 32);
55 }
56 
MakeFourCCString(uint32_t x,char * s)57 void MakeFourCCString(uint32_t x, char *s) {
58     s[0] = x >> 24;
59     s[1] = (x >> 16) & 0xff;
60     s[2] = (x >> 8) & 0xff;
61     s[3] = x & 0xff;
62     s[4] = '\0';
63 }
64 
65 }  // namespace android
66