text
[RRG-proxmark3.git] / common / commonutil.h
blob4ba65d171b9320ba8f3917e0ecd3f72871e0e7d2
1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, Aug 2005
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 // Utility functions used in many places, not specific to any piece of code.
9 //-----------------------------------------------------------------------------
11 #ifndef __COMMONUTIL_H
12 #define __COMMONUTIL_H
14 #include "common.h"
16 // endian change for 16bit
17 #ifdef __GNUC__
18 #ifndef BSWAP_16
19 #define BSWAP_16(x) __builtin_bswap16(x)
20 #endif
21 #else
22 #ifdef _MSC_VER
23 #ifndef BSWAP_16
24 #define BSWAP_16(x) _byteswap_ushort(x)
25 #endif
26 #else
27 #ifndef BSWAP_16
28 # define BSWAP_16(x) ((( ((x) & 0xFF00 ) >> 8))| ( (((x) & 0x00FF) << 8)))
29 #endif
30 #endif
31 #endif
33 #ifndef BITMASK
34 # define BITMASK(X) (1 << (X))
35 #endif
36 #ifndef ARRAYLEN
37 # define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0]))
38 #endif
40 #ifndef NTIME
41 # define NTIME(n) for (int _index = 0; _index < n; _index++)
42 #endif
44 extern struct version_information version_information;
45 void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_info);
47 uint32_t reflect(uint32_t v, int b); // used in crc.c ...
48 uint8_t reflect8(uint8_t b); // dedicated 8bit reversal
49 uint16_t reflect16(uint16_t b); // dedicated 16bit reversal
50 uint32_t reflect32(uint32_t b); // dedicated 32bit reversal
52 void num_to_bytes(uint64_t n, size_t len, uint8_t *dest);
53 uint64_t bytes_to_num(uint8_t *src, size_t len);
55 // rotate left byte array
56 void rol(uint8_t *data, const size_t len);
57 void lsl(uint8_t *data, size_t len);
58 uint32_t le24toh(uint8_t data[3]);
59 void htole24(uint32_t val, uint8_t data[3]);
61 // rol on a u32
62 uint32_t rotl(uint32_t a, uint8_t n);
63 uint32_t rotr(uint32_t a, uint8_t n);
64 #endif