Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/faac/common/faac_endian.h
Line
Count
Source
1
/*
2
 * FAAC - Freeware Advanced Audio Coder
3
 * Copyright (C) 2026 Nils Schimmelmann
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 */
15
16
#ifndef FAAC_COMMON_ENDIAN_H
17
#define FAAC_COMMON_ENDIAN_H
18
19
#include <stdint.h>
20
#include <stdbool.h>
21
#include <string.h>
22
23
#ifdef __cplusplus
24
extern "C" {
25
#endif
26
27
#if defined(__has_builtin)
28
# if __has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)
29
#  define FAAC_HAVE_BSWAP_BUILTINS 1
30
# endif
31
#elif defined(__GNUC__)
32
# define FAAC_HAVE_BSWAP_BUILTINS 1
33
#endif
34
35
#if defined(FAAC_HAVE_BSWAP_BUILTINS)
36
4.28k
# define bswap16(x) __builtin_bswap16((uint16_t)(x))
37
1.18M
# define bswap32(x) __builtin_bswap32((uint32_t)(x))
38
7.45k
# define bswap64(x) __builtin_bswap64((uint64_t)(x))
39
#elif defined(_MSC_VER)
40
# include <stdlib.h>
41
# define bswap16(x) _byteswap_ushort((unsigned short)(x))
42
# define bswap32(x) _byteswap_ulong((unsigned long)(x))
43
# define bswap64(x) _byteswap_uint64((unsigned __int64)(x))
44
#else
45
static inline uint16_t bswap16(uint16_t x) {
46
    return (uint16_t)((x >> 8) | (x << 8));
47
}
48
49
static inline uint32_t bswap32(uint32_t x) {
50
    return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
51
}
52
53
static inline uint64_t bswap64(uint64_t x) {
54
    return ((x >> 56) & 0x00000000000000FFULL) |
55
           ((x >> 40) & 0x000000000000FF00ULL) |
56
           ((x >> 24) & 0x0000000000FF0000ULL) |
57
           ((x >> 8)  & 0x00000000FF000000ULL) |
58
           ((x << 8)  & 0x000000FF00000000ULL) |
59
           ((x << 24) & 0x0000FF0000000000ULL) |
60
           ((x << 40) & 0x00FF000000000000ULL) |
61
           ((x << 56) & 0xFF00000000000000ULL);
62
}
63
#endif
64
65
/* Host byte order as a constant expression where the compiler states it, and
66
 * a probe the optimizer folds to one otherwise. Never use it in #if. */
67
0
static inline bool host_is_big_endian(void) {
68
0
    const uint16_t one = 1;
69
0
    uint8_t first;
70
0
    memcpy(&first, &one, 1);
71
0
    return first == 0;
72
0
}
Unexecuted instantiation: demux.c:host_is_big_endian
Unexecuted instantiation: metadata.c:host_is_big_endian
73
74
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
75
1.19M
# define FAAC_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
76
#elif defined(_MSC_VER)
77
# define FAAC_BIG_ENDIAN 0
78
#else
79
# define FAAC_BIG_ENDIAN host_is_big_endian()
80
#endif
81
82
/* The C library may define these too once its <endian.h> is no longer shadowed. */
83
#undef htobe16
84
#undef htobe32
85
#undef htobe64
86
#undef htole16
87
#undef htole32
88
#undef htole64
89
#undef be16toh
90
#undef be32toh
91
#undef be64toh
92
#undef le16toh
93
#undef le32toh
94
#undef le64toh
95
96
4.28k
#define htobe16(x) ((uint16_t)(FAAC_BIG_ENDIAN ? (uint16_t)(x) : bswap16(x)))
97
1.18M
#define htobe32(x) ((uint32_t)(FAAC_BIG_ENDIAN ? (uint32_t)(x) : bswap32(x)))
98
7.45k
#define htobe64(x) ((uint64_t)(FAAC_BIG_ENDIAN ? (uint64_t)(x) : bswap64(x)))
99
#define htole16(x) ((uint16_t)(FAAC_BIG_ENDIAN ? bswap16(x) : (uint16_t)(x)))
100
#define htole32(x) ((uint32_t)(FAAC_BIG_ENDIAN ? bswap32(x) : (uint32_t)(x)))
101
#define htole64(x) ((uint64_t)(FAAC_BIG_ENDIAN ? bswap64(x) : (uint64_t)(x)))
102
103
4.28k
#define be16toh(x) htobe16(x)
104
1.18M
#define be32toh(x) htobe32(x)
105
7.45k
#define be64toh(x) htobe64(x)
106
#define le16toh(x) htole16(x)
107
#define le32toh(x) htole32(x)
108
#define le64toh(x) htole64(x)
109
110
0
static inline int16_t read_pcm16_le(const int16_t *src) { return (int16_t)le16toh(*src); }
Unexecuted instantiation: demux.c:read_pcm16_le
Unexecuted instantiation: metadata.c:read_pcm16_le
111
0
static inline int16_t read_pcm16_be(const int16_t *src) { return (int16_t)be16toh(*src); }
Unexecuted instantiation: demux.c:read_pcm16_be
Unexecuted instantiation: metadata.c:read_pcm16_be
112
0
static inline int16_t read_pcm16(const int16_t *src, bool bigendian) {
113
0
    return bigendian ? read_pcm16_be(src) : read_pcm16_le(src);
114
0
}
Unexecuted instantiation: demux.c:read_pcm16
Unexecuted instantiation: metadata.c:read_pcm16
115
116
0
static inline int32_t read_pcm24_le(const uint8_t *src) {
117
0
    int32_t s = (int32_t)src[0] | ((int32_t)src[1] << 8) | ((int32_t)src[2] << 16);
118
0
    if (s & 0x800000) s |= (int32_t)0xff000000;
119
0
    return s;
120
0
}
Unexecuted instantiation: demux.c:read_pcm24_le
Unexecuted instantiation: metadata.c:read_pcm24_le
121
122
0
static inline int32_t read_pcm24_be(const uint8_t *src) {
123
0
    int32_t s = ((int32_t)src[0] << 16) | ((int32_t)src[1] << 8) | (int32_t)src[2];
124
0
    if (s & 0x800000) s |= (int32_t)0xff000000;
125
0
    return s;
126
0
}
Unexecuted instantiation: demux.c:read_pcm24_be
Unexecuted instantiation: metadata.c:read_pcm24_be
127
128
0
static inline int32_t read_pcm24(const uint8_t *src, bool bigendian) {
129
0
    return bigendian ? read_pcm24_be(src) : read_pcm24_le(src);
130
0
}
Unexecuted instantiation: demux.c:read_pcm24
Unexecuted instantiation: metadata.c:read_pcm24
131
132
0
static inline void write_pcm24_le(uint8_t *dst, int32_t v) {
133
0
    dst[0] = (uint8_t)v;
134
0
    dst[1] = (uint8_t)(v >> 8);
135
0
    dst[2] = (uint8_t)(v >> 16);
136
0
}
Unexecuted instantiation: demux.c:write_pcm24_le
Unexecuted instantiation: metadata.c:write_pcm24_le
137
138
0
static inline void write_pcm24_be(uint8_t *dst, int32_t v) {
139
0
    dst[0] = (uint8_t)(v >> 16);
140
0
    dst[1] = (uint8_t)(v >> 8);
141
0
    dst[2] = (uint8_t)v;
142
0
}
Unexecuted instantiation: demux.c:write_pcm24_be
Unexecuted instantiation: metadata.c:write_pcm24_be
143
144
0
static inline void write_pcm24(uint8_t *dst, int32_t v, bool bigendian) {
145
0
    if (bigendian) write_pcm24_be(dst, v);
146
0
    else write_pcm24_le(dst, v);
147
0
}
Unexecuted instantiation: demux.c:write_pcm24
Unexecuted instantiation: metadata.c:write_pcm24
148
149
0
static inline int32_t read_pcm32_le(const int32_t *src) { return (int32_t)le32toh(*src); }
Unexecuted instantiation: demux.c:read_pcm32_le
Unexecuted instantiation: metadata.c:read_pcm32_le
150
0
static inline int32_t read_pcm32_be(const int32_t *src) { return (int32_t)be32toh(*src); }
Unexecuted instantiation: demux.c:read_pcm32_be
Unexecuted instantiation: metadata.c:read_pcm32_be
151
0
static inline int32_t read_pcm32(const int32_t *src, bool bigendian) {
152
0
    return bigendian ? read_pcm32_be(src) : read_pcm32_le(src);
153
0
}
Unexecuted instantiation: demux.c:read_pcm32
Unexecuted instantiation: metadata.c:read_pcm32
154
155
/* Endian utilities */
156
4.28k
static inline uint16_t read_u16_be(const uint8_t *b) {
157
4.28k
    uint16_t val;
158
4.28k
    memcpy(&val, b, sizeof(val));
159
4.28k
    return be16toh(val);
160
4.28k
}
demux.c:read_u16_be
Line
Count
Source
156
4.28k
static inline uint16_t read_u16_be(const uint8_t *b) {
157
4.28k
    uint16_t val;
158
4.28k
    memcpy(&val, b, sizeof(val));
159
4.28k
    return be16toh(val);
160
4.28k
}
Unexecuted instantiation: metadata.c:read_u16_be
161
162
1.18M
static inline uint32_t read_u32_be(const uint8_t *b) {
163
1.18M
    uint32_t val;
164
1.18M
    memcpy(&val, b, sizeof(val));
165
1.18M
    return be32toh(val);
166
1.18M
}
demux.c:read_u32_be
Line
Count
Source
162
1.18M
static inline uint32_t read_u32_be(const uint8_t *b) {
163
1.18M
    uint32_t val;
164
1.18M
    memcpy(&val, b, sizeof(val));
165
1.18M
    return be32toh(val);
166
1.18M
}
metadata.c:read_u32_be
Line
Count
Source
162
7.57k
static inline uint32_t read_u32_be(const uint8_t *b) {
163
7.57k
    uint32_t val;
164
7.57k
    memcpy(&val, b, sizeof(val));
165
7.57k
    return be32toh(val);
166
7.57k
}
167
168
7.45k
static inline uint64_t read_u64_be(const uint8_t *b) {
169
7.45k
    uint64_t val;
170
7.45k
    memcpy(&val, b, sizeof(val));
171
7.45k
    return be64toh(val);
172
7.45k
}
demux.c:read_u64_be
Line
Count
Source
168
7.45k
static inline uint64_t read_u64_be(const uint8_t *b) {
169
7.45k
    uint64_t val;
170
7.45k
    memcpy(&val, b, sizeof(val));
171
7.45k
    return be64toh(val);
172
7.45k
}
Unexecuted instantiation: metadata.c:read_u64_be
173
174
0
static inline void write_u16_be(uint8_t *b, uint16_t val) {
175
0
    val = htobe16(val);
176
0
    memcpy(b, &val, sizeof(val));
177
0
}
Unexecuted instantiation: demux.c:write_u16_be
Unexecuted instantiation: metadata.c:write_u16_be
178
179
0
static inline void write_u32_be(uint8_t *b, uint32_t val) {
180
0
    val = htobe32(val);
181
0
    memcpy(b, &val, sizeof(val));
182
0
}
Unexecuted instantiation: demux.c:write_u32_be
Unexecuted instantiation: metadata.c:write_u32_be
183
184
0
static inline void write_u64_be(uint8_t *b, uint64_t val) {
185
0
    val = htobe64(val);
186
0
    memcpy(b, &val, sizeof(val));
187
0
}
Unexecuted instantiation: demux.c:write_u64_be
Unexecuted instantiation: metadata.c:write_u64_be
188
189
#ifdef __cplusplus
190
}
191
#endif
192
193
#endif /* FAAC_COMMON_ENDIAN_H */