-
Notifications
You must be signed in to change notification settings - Fork 3
/
uint32.h
88 lines (65 loc) · 1.88 KB
/
uint32.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
#ifndef UINT32_H
#define UINT32_H
#include <inttypes.h> // uint32_t, int32_t
#include <stddef.h> // size_t
#ifdef __cplusplus
extern "C" {
#endif
typedef uint32_t uint32;
typedef int32_t int32;
#if (defined(__i386__) || defined(__x86_64__)) && !defined(NO_UINT32_MACROS)
static inline void uint32_pack(char* out,uint32 in) {
*(uint32*)out=in;
}
static inline void uint32_unpack(const char *in,uint32* out) {
*out=*(const uint32*)in;
}
static inline uint32 uint32_read(const char* in) {
return *(const uint32*)in;
}
#if defined(__x86_64__) && defined(__GNUC__)
static inline void uint32_pack_big(char *out,uint32 in) {
*(uint32*)out=__builtin_bswap32(in);
}
static inline void uint32_unpack_big(const char *in,uint32* out) {
*out=__builtin_bswap32(*(const uint32*)in);
}
static inline uint32 uint32_read_big(const char *in) {
return __builtin_bswap32(*(const uint32*)in);
}
#else
void uint32_pack_big(char *out,uint32 in);
void uint32_unpack_big(const char *in,uint32* out);
uint32 uint32_read_big(const char *in);
#endif
#else
void uint32_pack(char *out,uint32 in);
void uint32_pack_big(char *out,uint32 in);
void uint32_unpack(const char *in,uint32* out);
void uint32_unpack_big(const char *in,uint32* out);
uint32 uint32_read(const char *in);
uint32 uint32_read_big(const char *in);
#endif
static inline size_t fmt_uint32(char* out, uint32 in) {
if (out) uint32_pack(out,in);
return 4;
}
static inline size_t fmt_uint32_big(char* out, uint32 in) {
if (out) uint32_pack_big(out,in);
return 4;
}
static inline size_t scan_uint32(const char* in, size_t len, uint32_t* b) {
if (len<4) return 0;
*b = uint32_read(in);
return 4;
}
static inline size_t scan_uint32_big(const char* in, size_t len, uint32_t* b) {
if (len<4) return 0;
*b = uint32_read_big(in);
return 4;
}
#ifdef __cplusplus
}
#endif
#endif