Coverage Report

Created: 2026-10-17 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/compat/bswap.h
Line
Count
Source
1
#ifndef COMPAT_BSWAP_H
2
#define COMPAT_BSWAP_H
3
4
/*
5
 * Let's make sure we always have a sane definition for ntohl()/htonl().
6
 * Some libraries define those as a function call, just to perform byte
7
 * shifting, bringing significant overhead to what should be a simple
8
 * operation.
9
 */
10
11
/*
12
 * Default version that the compiler ought to optimize properly with
13
 * constant values.
14
 */
15
static inline uint32_t default_swab32(uint32_t val)
16
0
{
17
0
  return (((val & 0xff000000) >> 24) |
18
0
    ((val & 0x00ff0000) >>  8) |
19
0
    ((val & 0x0000ff00) <<  8) |
20
0
    ((val & 0x000000ff) << 24));
21
0
}
Unexecuted instantiation: procinfo.c:default_swab32
Unexecuted instantiation: fopen.c:default_swab32
Unexecuted instantiation: strlcpy.c:default_swab32
Unexecuted instantiation: qsort_s.c:default_swab32
Unexecuted instantiation: system.c:default_swab32
Unexecuted instantiation: sha1.c:default_swab32
Unexecuted instantiation: ubc_check.c:default_swab32
Unexecuted instantiation: fsm-ipc-unix.c:default_swab32
Unexecuted instantiation: fsm-settings-unix.c:default_swab32
Unexecuted instantiation: fsm-path-utils-linux.c:default_swab32
22
23
static inline uint64_t default_bswap64(uint64_t val)
24
0
{
25
0
  return (((val & (uint64_t)0x00000000000000ffULL) << 56) |
26
0
    ((val & (uint64_t)0x000000000000ff00ULL) << 40) |
27
0
    ((val & (uint64_t)0x0000000000ff0000ULL) << 24) |
28
0
    ((val & (uint64_t)0x00000000ff000000ULL) <<  8) |
29
0
    ((val & (uint64_t)0x000000ff00000000ULL) >>  8) |
30
0
    ((val & (uint64_t)0x0000ff0000000000ULL) >> 24) |
31
0
    ((val & (uint64_t)0x00ff000000000000ULL) >> 40) |
32
0
    ((val & (uint64_t)0xff00000000000000ULL) >> 56));
33
0
}
Unexecuted instantiation: procinfo.c:default_bswap64
Unexecuted instantiation: fopen.c:default_bswap64
Unexecuted instantiation: strlcpy.c:default_bswap64
Unexecuted instantiation: qsort_s.c:default_bswap64
Unexecuted instantiation: system.c:default_bswap64
Unexecuted instantiation: sha1.c:default_bswap64
Unexecuted instantiation: ubc_check.c:default_bswap64
Unexecuted instantiation: fsm-ipc-unix.c:default_bswap64
Unexecuted instantiation: fsm-settings-unix.c:default_bswap64
Unexecuted instantiation: fsm-path-utils-linux.c:default_bswap64
34
35
/*
36
 * __has_builtin is available since Clang 10 and GCC 10.
37
 * Below is a fallback for older compilers.
38
 */
39
#ifndef __has_builtin
40
# define __has_builtin(x) 0
41
#endif
42
43
#undef bswap32
44
#undef bswap64
45
46
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64))
47
48
#include <stdlib.h>
49
50
#define bswap32(x) _byteswap_ulong(x)
51
#define bswap64(x) _byteswap_uint64(x)
52
53
#define GIT_LITTLE_ENDIAN 1234
54
#define GIT_BIG_ENDIAN 4321
55
#define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
56
57
#elif __has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)
58
59
0
#define bswap32(x) __builtin_bswap32((x))
60
0
#define bswap64(x) __builtin_bswap64((x))
61
62
#endif
63
64
#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
65
66
# define GIT_BYTE_ORDER __BYTE_ORDER
67
# define GIT_LITTLE_ENDIAN __LITTLE_ENDIAN
68
# define GIT_BIG_ENDIAN __BIG_ENDIAN
69
70
#elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
71
72
# define GIT_BYTE_ORDER BYTE_ORDER
73
# define GIT_LITTLE_ENDIAN LITTLE_ENDIAN
74
# define GIT_BIG_ENDIAN BIG_ENDIAN
75
76
#elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
77
78
# define GIT_BYTE_ORDER __BYTE_ORDER__
79
# define GIT_LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
80
# define GIT_BIG_ENDIAN __ORDER_BIG_ENDIAN__
81
82
#elif !defined(GIT_BYTE_ORDER)
83
84
# define GIT_BIG_ENDIAN 4321
85
# define GIT_LITTLE_ENDIAN 1234
86
87
# if defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
88
#  define GIT_BYTE_ORDER GIT_BIG_ENDIAN
89
# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
90
#  define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
91
# elif defined(__THW_BIG_ENDIAN__) && !defined(__THW_LITTLE_ENDIAN__)
92
#  define GIT_BYTE_ORDER GIT_BIG_ENDIAN
93
# elif defined(__THW_LITTLE_ENDIAN__) && !defined(__THW_BIG_ENDIAN__)
94
#  define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
95
# else
96
#  error "Cannot determine endianness"
97
# endif
98
99
#endif
100
101
#undef ntohl
102
#undef htonl
103
#undef ntohll
104
#undef htonll
105
106
#if GIT_BYTE_ORDER == GIT_BIG_ENDIAN
107
# define ntohl(x) (x)
108
# define htonl(x) (x)
109
# define ntohll(x) (x)
110
# define htonll(x) (x)
111
#else
112
113
# if defined(bswap32)
114
0
#  define ntohl(x) bswap32(x)
115
0
#  define htonl(x) bswap32(x)
116
# else
117
#  define ntohl(x) default_swab32(x)
118
#  define htonl(x) default_swab32(x)
119
# endif
120
121
# if defined(bswap64)
122
0
#  define ntohll(x) bswap64(x)
123
0
#  define htonll(x) bswap64(x)
124
# else
125
#  define ntohll(x) default_bswap64(x)
126
#  define htonll(x) default_bswap64(x)
127
# endif
128
#endif
129
130
static inline uint16_t get_be16(const void *ptr)
131
0
{
132
0
  const unsigned char *p = ptr;
133
0
  return  (uint16_t)p[0] << 8 |
134
0
    (uint16_t)p[1] << 0;
135
0
}
Unexecuted instantiation: procinfo.c:get_be16
Unexecuted instantiation: fopen.c:get_be16
Unexecuted instantiation: strlcpy.c:get_be16
Unexecuted instantiation: qsort_s.c:get_be16
Unexecuted instantiation: read-cache.c:get_be16
Unexecuted instantiation: system.c:get_be16
Unexecuted instantiation: sha1.c:get_be16
Unexecuted instantiation: ubc_check.c:get_be16
Unexecuted instantiation: fsm-ipc-unix.c:get_be16
Unexecuted instantiation: fsm-settings-unix.c:get_be16
Unexecuted instantiation: fsm-path-utils-linux.c:get_be16
136
137
static inline uint32_t get_be32(const void *ptr)
138
0
{
139
0
  const unsigned char *p = ptr;
140
0
  return  (uint32_t)p[0] << 24 |
141
0
    (uint32_t)p[1] << 16 |
142
0
    (uint32_t)p[2] <<  8 |
143
0
    (uint32_t)p[3] <<  0;
144
0
}
Unexecuted instantiation: packfile.c:get_be32
Unexecuted instantiation: procinfo.c:get_be32
Unexecuted instantiation: fopen.c:get_be32
Unexecuted instantiation: strlcpy.c:get_be32
Unexecuted instantiation: qsort_s.c:get_be32
Unexecuted instantiation: bloom.c:get_be32
Unexecuted instantiation: commit-graph.c:get_be32
Unexecuted instantiation: dir.c:get_be32
Unexecuted instantiation: ewah_io.c:get_be32
Unexecuted instantiation: fsmonitor.c:get_be32
Unexecuted instantiation: midx.c:get_be32
Unexecuted instantiation: pack-bitmap.c:get_be32
Unexecuted instantiation: pack-mtimes.c:get_be32
Unexecuted instantiation: pack-revindex.c:get_be32
Unexecuted instantiation: pseudo-merge.c:get_be32
Unexecuted instantiation: read-cache.c:get_be32
Unexecuted instantiation: system.c:get_be32
Unexecuted instantiation: sha1.c:get_be32
Unexecuted instantiation: ubc_check.c:get_be32
Unexecuted instantiation: sha256.c:get_be32
Unexecuted instantiation: fsm-ipc-unix.c:get_be32
Unexecuted instantiation: fsm-settings-unix.c:get_be32
Unexecuted instantiation: fsm-path-utils-linux.c:get_be32
Unexecuted instantiation: chunk-format.c:get_be32
145
146
static inline uint64_t get_be64(const void *ptr)
147
0
{
148
0
  const unsigned char *p = ptr;
149
0
  return  (uint64_t)get_be32(&p[0]) << 32 |
150
0
    (uint64_t)get_be32(&p[4]) <<  0;
151
0
}
Unexecuted instantiation: packfile.c:get_be64
Unexecuted instantiation: procinfo.c:get_be64
Unexecuted instantiation: fopen.c:get_be64
Unexecuted instantiation: strlcpy.c:get_be64
Unexecuted instantiation: qsort_s.c:get_be64
Unexecuted instantiation: commit-graph.c:get_be64
Unexecuted instantiation: fsmonitor.c:get_be64
Unexecuted instantiation: midx.c:get_be64
Unexecuted instantiation: pack-bitmap.c:get_be64
Unexecuted instantiation: pack-revindex.c:get_be64
Unexecuted instantiation: pseudo-merge.c:get_be64
Unexecuted instantiation: system.c:get_be64
Unexecuted instantiation: sha1.c:get_be64
Unexecuted instantiation: ubc_check.c:get_be64
Unexecuted instantiation: fsm-ipc-unix.c:get_be64
Unexecuted instantiation: fsm-settings-unix.c:get_be64
Unexecuted instantiation: fsm-path-utils-linux.c:get_be64
Unexecuted instantiation: chunk-format.c:get_be64
152
153
static inline void put_be32(void *ptr, uint32_t value)
154
0
{
155
0
  unsigned char *p = ptr;
156
0
  p[0] = (value >> 24) & 0xff;
157
0
  p[1] = (value >> 16) & 0xff;
158
0
  p[2] = (value >>  8) & 0xff;
159
0
  p[3] = (value >>  0) & 0xff;
160
0
}
Unexecuted instantiation: packfile.c:put_be32
Unexecuted instantiation: procinfo.c:put_be32
Unexecuted instantiation: fopen.c:put_be32
Unexecuted instantiation: strlcpy.c:put_be32
Unexecuted instantiation: qsort_s.c:put_be32
Unexecuted instantiation: fsmonitor.c:put_be32
Unexecuted instantiation: read-cache.c:put_be32
Unexecuted instantiation: system.c:put_be32
Unexecuted instantiation: sha1.c:put_be32
Unexecuted instantiation: ubc_check.c:put_be32
Unexecuted instantiation: sha256.c:put_be32
Unexecuted instantiation: fsm-ipc-unix.c:put_be32
Unexecuted instantiation: fsm-settings-unix.c:put_be32
Unexecuted instantiation: fsm-path-utils-linux.c:put_be32
161
162
static inline void put_be64(void *ptr, uint64_t value)
163
0
{
164
0
  unsigned char *p = ptr;
165
0
  p[0] = (value >> 56) & 0xff;
166
0
  p[1] = (value >> 48) & 0xff;
167
0
  p[2] = (value >> 40) & 0xff;
168
0
  p[3] = (value >> 32) & 0xff;
169
0
  p[4] = (value >> 24) & 0xff;
170
0
  p[5] = (value >> 16) & 0xff;
171
0
  p[6] = (value >>  8) & 0xff;
172
0
  p[7] = (value >>  0) & 0xff;
173
0
}
Unexecuted instantiation: procinfo.c:put_be64
Unexecuted instantiation: fopen.c:put_be64
Unexecuted instantiation: strlcpy.c:put_be64
Unexecuted instantiation: qsort_s.c:put_be64
Unexecuted instantiation: system.c:put_be64
Unexecuted instantiation: sha1.c:put_be64
Unexecuted instantiation: ubc_check.c:put_be64
Unexecuted instantiation: fsm-ipc-unix.c:put_be64
Unexecuted instantiation: fsm-settings-unix.c:put_be64
Unexecuted instantiation: fsm-path-utils-linux.c:put_be64
174
175
#endif /* COMPAT_BSWAP_H */