fix const params, logic, casting
[RRG-proxmark3.git] / include / common.h
blobb405b2ac701f3896f44af7ffb0416ed2214dfc59
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Interlib Definitions
17 //-----------------------------------------------------------------------------
19 #ifndef __COMMON_H
20 #define __COMMON_H
22 #include <stdint.h>
23 #include <stddef.h>
24 #include <stdbool.h>
26 #ifdef _WIN32
27 #define ABOVE "../"
28 #define PATHSEP "/"
29 #else
30 #define ABOVE "../"
31 #define PATHSEP "/"
32 #endif
34 // PM3 share path relative to executable when installed
35 #define PM3_SHARE_RELPATH ".." PATHSEP "share" PATHSEP "proxmark3" PATHSEP
37 // PM3_USER_DIRECTORY will be expanded from $HOME, e.g. ~/.proxmark3/
38 #define PM3_USER_DIRECTORY PATHSEP ".proxmark3" PATHSEP
40 // PM3 subdirectories:
41 #define PYTHON_SCRIPTS_SUBDIR "pyscripts" PATHSEP
42 #define CMD_SCRIPTS_SUBDIR "cmdscripts" PATHSEP
43 #define DICTIONARIES_SUBDIR "dictionaries" PATHSEP
44 #define LUA_LIBRARIES_SUBDIR "lualibs" PATHSEP
45 #define LUA_SCRIPTS_SUBDIR "luascripts" PATHSEP
46 #define RESOURCES_SUBDIR "resources" PATHSEP
47 #define TRACES_SUBDIR "traces" PATHSEP
48 #define LOGS_SUBDIR "logs" PATHSEP
49 #define FIRMWARES_SUBDIR "firmware" PATHSEP
50 #define BOOTROM_SUBDIR "bootrom" PATHSEP "obj" PATHSEP
51 #define FULLIMAGE_SUBDIR "armsrc" PATHSEP "obj" PATHSEP
53 #define PACKED __attribute__((packed))
55 #define VERSION_INFORMATION_MAGIC 0x56334d50 // "PM3V"
56 struct version_information_t {
57 int magic; /* Magic sequence to identify this as a correct version information structure. Must be VERSION_INFORMATION_MAGIC */
58 char versionversion; /* Must be 1 */
59 char present; /* 1 if the version information could be created at compile time, otherwise 0 and the remaining fields (except for magic) are empty */
60 char clean; /* 1: Tree was clean, no local changes. 0: Tree was unclean. 2: Couldn't be determined */
61 char gitversion[50]; /* String with the git revision */
62 char buildtime[30]; /* string with the build time */
63 char armsrc[10]; /* sha256sum of sha256sum of armsrc && common_arm files */
64 } PACKED;
66 // debug
67 #define DBG_NONE 0 // no messages
68 #define DBG_ERROR 1 // errors only
69 #define DBG_INFO 2 // errors + info messages
70 #define DBG_DEBUG 3 // errors + info + debug messages
71 #define DBG_EXTENDED 4 // errors + info + debug + breaking debug messages
72 extern int g_dbglevel;
74 // tear-off
75 extern uint16_t g_tearoff_delay_us;
76 extern bool g_tearoff_enabled;
78 // reader voltage field detector
79 #define MF_MINFIELDV 4000
81 #ifndef MIN
82 # define MIN(a, b) (((a) < (b)) ? (a) : (b))
83 #endif
85 #ifndef MAX
86 # define MAX(a, b) (((a) > (b)) ? (a) : (b))
87 #endif
89 #ifndef ABS
90 # define ABS(a) ( ((a)<0) ? -(a) : (a) )
91 #endif
94 //#define RAMFUNC __attribute((long_call, section(".ramfunc")))
95 #define RAMFUNC __attribute((long_call, section(".ramfunc"))) __attribute__((target("arm")))
97 #ifndef ROTR
98 # define ROTR(x,n) (((uintmax_t)(x) >> (n)) | ((uintmax_t)(x) << ((sizeof(x) * 8) - (n))))
99 #endif
101 #ifndef PM3_ROTL
102 # define PM3_ROTL(x,n) (((uintmax_t)(x) << (n)) | ((uintmax_t)(x) >> ((sizeof(x) * 8) - (n))))
103 #endif
105 // endian change for 64bit
106 #ifdef __GNUC__
107 #ifndef BSWAP_64
108 #define BSWAP_64(x) __builtin_bswap64(x)
109 #endif
110 #else
111 #ifdef _MSC_VER
112 #ifndef BSWAP_64
113 #define BSWAP_64(x) _byteswap_uint64(x)
114 #endif
115 #else
116 #ifndef BSWAP_64
117 #define BSWAP_64(x) \
118 (((uint64_t)(x) << 56) | \
119 (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
120 (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
121 (((uint64_t)(x) << 8) & 0xff00000000ULL) | \
122 (((uint64_t)(x) >> 8) & 0xff000000ULL) | \
123 (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
124 (((uint64_t)(x) >> 40) & 0xff00ULL) | \
125 ((uint64_t)(x) >> 56))
126 #endif
127 #endif
128 #endif
130 // endian change for 48bit
131 #ifndef BSWAP_48
132 #define BSWAP_48(x) \
133 (((uint64_t)(x) << 40) & 0x0000ff0000000000ULL) | \
134 (((uint64_t)(x) << 24) & 0x000000ff00000000ULL) | \
135 (((uint64_t)(x) << 8) & 0x00000000ff000000ULL) | \
136 (((uint64_t)(x) >> 8) & 0x000000000ff0000ULL) | \
137 (((uint64_t)(x) >> 24) & 0x00000000000ff00ULL) | \
138 (((uint64_t)(x) >> 40) & 0x0000000000000ffULL)
139 #endif
141 // endian change for 32bit
142 #ifdef __GNUC__
143 #ifndef BSWAP_32
144 #define BSWAP_32(x) __builtin_bswap32(x)
145 #endif
146 #else
147 #ifdef _MSC_VER
148 #ifndef BSWAP_32
149 #define BSWAP_32(x) _byteswap_ulong(x)
150 #endif
151 #else
152 #ifndef BSWAP_32
153 # define BSWAP_32(x) \
154 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
155 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
156 #endif
157 #endif
158 #endif
160 // convert 2 bytes to U16 in little endian
161 #ifndef BYTES2UINT16
162 # define BYTES2UINT16(x) (((x)[1] << 8) | ((x)[0]))
163 #endif
164 // convert 4 bytes to U32 in little endian
165 #ifndef BYTES2UINT32
166 # define BYTES2UINT32(x) (((x)[3] << 24) | ((x)[2] << 16) | ((x)[1] << 8) | ((x)[0]))
167 #endif
169 // convert 4 bytes to U32 in big endian
170 #ifndef BYTES2UINT32_BE
171 # define BYTES2UINT32_BE(x) (((x)[0] << 24) | ((x)[1] << 16) | ((x)[2] << 8) | ((x)[3]))
172 #endif
175 #define EVEN 0
176 #define ODD 1
178 // Nibble logic
179 #ifndef NIBBLE_HIGH
180 # define NIBBLE_HIGH(b) ( ((b) & 0xF0) >> 4 )
181 #endif
183 #ifndef NIBBLE_LOW
184 # define NIBBLE_LOW(b) ((b) & 0x0F )
185 #endif
187 #ifndef CRUMB
188 # define CRUMB(b,p) ((((b) & (0x3 << (p)) ) >> (p) ) & 0xF)
189 #endif
191 #ifndef SWAP_NIBBLE
192 # define SWAP_NIBBLE(b) ( (NIBBLE_LOW(b)<< 4) | NIBBLE_HIGH(b))
193 #endif
195 // Binary Encoded Digit
196 #ifndef BCD2DEC
197 # define BCD2DEC(bcd) HornerScheme((bcd), 0x10, 10)
198 #endif
200 #ifndef DEC2BCD
201 # define DEC2BCD(dec) HornerScheme((dec), 10, 0x10)
202 #endif
204 // bit stream operations
205 #define TEST_BIT(data, i) (*((data) + ((i) / 8)) >> (7 - ((i) % 8))) & 1
206 #define SET_BIT(data, i) *((data) + ((i) / 8)) |= (1 << (7 - ((i) % 8)))
207 #define CLEAR_BIT(data, i) *((data) + ((i) / 8)) &= ~(1 << (7 - ((i) % 8)))
208 #define FLIP_BIT(data, i) *((data) + ((i) / 8)) ^= (1 << (7 - ((i) % 8)))
210 // time for decompressing and loading the image to the FPGA
211 #define FPGA_LOAD_WAIT_TIME (1500)
213 // GCC extension
214 // from client/deps/tinycbor/compilersupport_p.h
215 #ifdef __GNUC__
216 #ifndef likely
217 # define likely(x) __builtin_expect(!!(x), 1)
218 #endif
219 #ifndef unlikely
220 # define unlikely(x) __builtin_expect(!!(x), 0)
221 #endif
222 #else
223 # define likely(x) (x)
224 # define unlikely(x) (x)
225 #endif
227 #endif