fix coverity CID 344485, 344482, 344481
[RRG-proxmark3.git] / include / common.h
blob50d0aed698807646b399c0e38641564f005ec724
1 //-----------------------------------------------------------------------------
2 // Hagen Fritsch - June 2010
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //
8 //-----------------------------------------------------------------------------
9 // Interlib Definitions
10 //-----------------------------------------------------------------------------
12 #ifndef __COMMON_H
13 #define __COMMON_H
15 #include <stdint.h>
16 #include <stddef.h>
17 #include <stdbool.h>
19 #define PATHSEP "/"
20 // PM3 share path relative to executable when installed
21 #define PM3_SHARE_RELPATH ".." PATHSEP "share" PATHSEP "proxmark3" PATHSEP
23 // PM3_USER_DIRECTORY will be expanded from $HOME, e.g. ~/.proxmark3/
24 #define PM3_USER_DIRECTORY PATHSEP ".proxmark3" PATHSEP
26 // PM3 subdirectories:
27 #define PYTHON_SCRIPTS_SUBDIR "pyscripts" PATHSEP
28 #define CMD_SCRIPTS_SUBDIR "cmdscripts" PATHSEP
29 #define DICTIONARIES_SUBDIR "dictionaries" PATHSEP
30 #define LUA_LIBRARIES_SUBDIR "lualibs" PATHSEP
31 #define LUA_SCRIPTS_SUBDIR "luascripts" PATHSEP
32 #define RESOURCES_SUBDIR "resources" PATHSEP
33 #define TRACES_SUBDIR "traces" PATHSEP
34 #define LOGS_SUBDIR "logs" PATHSEP
35 #define FIRMWARES_SUBDIR "firmware" PATHSEP
36 #define BOOTROM_SUBDIR "bootrom" PATHSEP "obj" PATHSEP
37 #define FULLIMAGE_SUBDIR "armsrc" PATHSEP "obj" PATHSEP
39 #define PACKED __attribute__((packed))
41 #define VERSION_INFORMATION_MAGIC 0x56334d50 // "PM3V"
42 struct version_information {
43 int magic; /* Magic sequence to identify this as a correct version information structure. Must be VERSION_INFORMATION_MAGIC */
44 char versionversion; /* Must be 1 */
45 char present; /* 1 if the version information could be created at compile time, otherwise 0 and the remaining fields (except for magic) are empty */
46 char clean; /* 1: Tree was clean, no local changes. 0: Tree was unclean. 2: Couldn't be determined */
47 char gitversion[50]; /* String with the git revision */
48 char buildtime[30]; /* string with the build time */
49 } PACKED;
51 // debug
52 #define DBG_NONE 0 // no messages
53 #define DBG_ERROR 1 // errors only
54 #define DBG_INFO 2 // errors + info messages
55 #define DBG_DEBUG 3 // errors + info + debug messages
56 #define DBG_EXTENDED 4 // errors + info + debug + breaking debug messages
57 extern int DBGLEVEL;
59 // tear-off
60 extern uint16_t tearoff_delay_us;
61 extern bool tearoff_enabled;
63 // reader voltage field detector
64 #define MF_MINFIELDV 4000
66 #ifndef MIN
67 # define MIN(a, b) (((a) < (b)) ? (a) : (b))
68 #endif
70 #ifndef MAX
71 # define MAX(a, b) (((a) > (b)) ? (a) : (b))
72 #endif
74 #ifndef ABS
75 # define ABS(a) ( ((a)<0) ? -(a) : (a) )
76 #endif
79 //#define RAMFUNC __attribute((long_call, section(".ramfunc")))
80 #define RAMFUNC __attribute((long_call, section(".ramfunc"))) __attribute__((target("arm")))
82 #ifndef ROTR
83 # define ROTR(x,n) (((uintmax_t)(x) >> (n)) | ((uintmax_t)(x) << ((sizeof(x) * 8) - (n))))
84 #endif
86 #ifndef PM3_ROTL
87 # define PM3_ROTL(x,n) (((uintmax_t)(x) << (n)) | ((uintmax_t)(x) >> ((sizeof(x) * 8) - (n))))
88 #endif
90 // endian change for 64bit
91 #ifdef __GNUC__
92 #ifndef BSWAP_64
93 #define BSWAP_64(x) __builtin_bswap64(x)
94 #endif
95 #else
96 #ifdef _MSC_VER
97 #ifndef BSWAP_64
98 #define BSWAP_64(x) _byteswap_uint64(x)
99 #endif
100 #else
101 #ifndef BSWAP_64
102 #define BSWAP_64(x) \
103 (((uint64_t)(x) << 56) | \
104 (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
105 (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
106 (((uint64_t)(x) << 8) & 0xff00000000ULL) | \
107 (((uint64_t)(x) >> 8) & 0xff000000ULL) | \
108 (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
109 (((uint64_t)(x) >> 40) & 0xff00ULL) | \
110 ((uint64_t)(x) >> 56))
111 #endif
112 #endif
113 #endif
115 // endian change for 32bit
116 #ifdef __GNUC__
117 #ifndef BSWAP_32
118 #define BSWAP_32(x) __builtin_bswap32(x)
119 #endif
120 #else
121 #ifdef _MSC_VER
122 #ifndef BSWAP_32
123 #define BSWAP_32(x) _byteswap_ulong(x)
124 #endif
125 #else
126 #ifndef BSWAP_32
127 # define BSWAP_32(x) \
128 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
129 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
130 #endif
131 #endif
132 #endif
134 // convert 2 bytes to U16
135 #ifndef BYTES2UINT16
136 # define BYTES2UINT16(x) ((x[1] << 8) | (x[0]))
137 #endif
138 // convert 4 bytes to U32
139 #ifndef BYTES2UINT32
140 # define BYTES2UINT32(x) ((x[3] << 24) | (x[2] << 16) | (x[1] << 8) | (x[0]))
141 #endif
143 #define EVEN 0
144 #define ODD 1
146 // Nibble logic
147 #ifndef NIBBLE_HIGH
148 # define NIBBLE_HIGH(b) ( (b & 0xF0) >> 4 )
149 #endif
151 #ifndef NIBBLE_LOW
152 # define NIBBLE_LOW(b) ( b & 0x0F )
153 #endif
155 #ifndef CRUMB
156 # define CRUMB(b,p) (((b & (0x3 << p) ) >> p ) & 0xF)
157 #endif
159 #ifndef SWAP_NIBBLE
160 # define SWAP_NIBBLE(b) ( (NIBBLE_LOW(b)<< 4) | NIBBLE_HIGH(b))
161 #endif
163 // Binary Encoded Digit
164 #ifndef BCD2DEC
165 # define BCD2DEC(bcd) HornerScheme(bcd, 0x10, 10)
166 #endif
168 #ifndef DEC2BCD
169 # define DEC2BCD(dec) HornerScheme(dec, 10, 0x10)
170 #endif
172 // bit stream operations
173 #define TEST_BIT(data, i) (*(data + (i / 8)) >> (7 - (i % 8))) & 1
174 #define SET_BIT(data, i) *(data + (i / 8)) |= (1 << (7 - (i % 8)))
175 #define CLEAR_BIT(data, i) *(data + (i / 8)) &= ~(1 << (7 - (i % 8)))
176 #define FLIP_BIT(data, i) *(data + (i / 8)) ^= (1 << (7 - (i % 8)))
178 #endif