Skip to content

Commit b2e39d0

Browse files
benpeartgitster
authored andcommitted
bswap: add 64 bit endianness helper get_be64
Add a new get_be64 macro to enable 64 bit endian conversions on memory that may or may not be aligned. Signed-off-by: Ben Peart <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6867272 commit b2e39d0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

compat/bswap.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ static inline uint64_t git_bswap64(uint64_t x)
158158

159159
#define get_be16(p) ntohs(*(unsigned short *)(p))
160160
#define get_be32(p) ntohl(*(unsigned int *)(p))
161+
#define get_be64(p) ntohll(*(uint64_t *)(p))
161162
#define put_be32(p, v) do { *(unsigned int *)(p) = htonl(v); } while (0)
163+
#define put_be64(p, v) do { *(uint64_t *)(p) = htonll(v); } while (0)
162164

163165
#else
164166

@@ -178,6 +180,13 @@ static inline uint32_t get_be32(const void *ptr)
178180
(uint32_t)p[3] << 0;
179181
}
180182

183+
static inline uint64_t get_be64(const void *ptr)
184+
{
185+
const unsigned char *p = ptr;
186+
return (uint64_t)get_be32(&p[0]) << 32 |
187+
(uint64_t)get_be32(&p[4]) << 0;
188+
}
189+
181190
static inline void put_be32(void *ptr, uint32_t value)
182191
{
183192
unsigned char *p = ptr;
@@ -187,4 +196,17 @@ static inline void put_be32(void *ptr, uint32_t value)
187196
p[3] = value >> 0;
188197
}
189198

199+
static inline void put_be64(void *ptr, uint64_t value)
200+
{
201+
unsigned char *p = ptr;
202+
p[0] = value >> 56;
203+
p[1] = value >> 48;
204+
p[2] = value >> 40;
205+
p[3] = value >> 32;
206+
p[4] = value >> 24;
207+
p[5] = value >> 16;
208+
p[6] = value >> 8;
209+
p[7] = value >> 0;
210+
}
211+
190212
#endif

0 commit comments

Comments
 (0)