textual
[RRG-proxmark3.git] / common / commonutil.c
blob802f362d80fb36ae24a9c3b6391369c5e620935e
1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, Sept 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 //-----------------------------------------------------------------------------
10 #include "commonutil.h"
11 #include <string.h>
13 /* Similar to FpgaGatherVersion this formats stored version information
14 * into a string representation. It takes a pointer to the struct version_information,
15 * verifies the magic properties, then stores a formatted string, prefixed by
16 * prefix in dst.
18 void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_info) {
19 struct version_information *v = (struct version_information *)version_info;
20 dst[0] = 0;
21 strncat(dst, prefix, len - 1);
22 if (v->magic != VERSION_INFORMATION_MAGIC) {
23 strncat(dst, "Missing/Invalid version information", len - strlen(dst) - 1);
24 return;
26 if (v->versionversion != 1) {
27 strncat(dst, "Version information not understood", len - strlen(dst) - 1);
28 return;
30 if (!v->present) {
31 strncat(dst, "Version information not available", len - strlen(dst) - 1);
32 return;
35 strncat(dst, v->gitversion, len - strlen(dst) - 1);
36 if (v->clean == 0) {
37 strncat(dst, "-unclean", len - strlen(dst) - 1);
38 } else if (v->clean == 2) {
39 strncat(dst, "-suspect", len - strlen(dst) - 1);
42 strncat(dst, " ", len - strlen(dst) - 1);
43 strncat(dst, v->buildtime, len - strlen(dst) - 1);
47 ref http://www.csm.ornl.gov/~dunigan/crc.html
48 Returns the value v with the bottom b [0,32] bits reflected.
49 Example: reflect(0x3e23L,3) == 0x3e26
51 uint32_t reflect(uint32_t v, int b) {
52 uint32_t t = v;
53 for (int i = 0; i < b; ++i) {
54 if (t & 1)
55 v |= BITMASK((b - 1) - i);
56 else
57 v &= ~BITMASK((b - 1) - i);
58 t >>= 1;
60 return v;
63 // https://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
65 // Reverse the bits in a byte with 3 operations (64-bit multiply and modulus division):
66 uint8_t reflect8(uint8_t b) {
67 return (b * 0x0202020202ULL & 0x010884422010ULL) % 1023;
71 // Reverse the bits in a byte with 4 operations (64-bit multiply, no division):
73 uint8_t reflect8(uint8_t b) {
74 return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
78 uint16_t reflect16(uint16_t b) {
79 uint16_t v = 0;
80 v |= (b & 0x8000) >> 15;
81 v |= (b & 0x4000) >> 13;
82 v |= (b & 0x2000) >> 11;
83 v |= (b & 0x1000) >> 9;
84 v |= (b & 0x0800) >> 7;
85 v |= (b & 0x0400) >> 5;
86 v |= (b & 0x0200) >> 3;
87 v |= (b & 0x0100) >> 1;
89 v |= (b & 0x0080) << 1;
90 v |= (b & 0x0040) << 3;
91 v |= (b & 0x0020) << 5;
92 v |= (b & 0x0010) << 7;
93 v |= (b & 0x0008) << 9;
94 v |= (b & 0x0004) << 11;
95 v |= (b & 0x0002) << 13;
96 v |= (b & 0x0001) << 15;
97 return v;
100 uint32_t reflect32(uint32_t b) {
101 // https://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
102 uint32_t v = b; // 32-bit word to reverse bit order
103 // swap odd and even bits
104 v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
105 // swap consecutive pairs
106 v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);
107 // swap nibbles ...
108 v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
109 // swap bytes
110 v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);
111 // swap 2-byte long pairs
112 v = (v >> 16) | (v << 16);
113 return v;
116 void num_to_bytes(uint64_t n, size_t len, uint8_t *dest) {
117 while (len--) {
118 dest[len] = (uint8_t) n;
119 n >>= 8;
123 uint64_t bytes_to_num(uint8_t *src, size_t len) {
124 uint64_t num = 0;
125 while (len--) {
126 num = (num << 8) | (*src);
127 src++;
129 return num;
132 // RotateLeft - Ultralight, Desfire
133 void rol(uint8_t *data, const size_t len) {
134 uint8_t first = data[0];
135 for (size_t i = 0; i < len - 1; i++) {
136 data[i] = data[i + 1];
138 data[len - 1] = first;
141 void lsl(uint8_t *data, size_t len) {
142 for (size_t n = 0; n < len - 1; n++) {
143 data[n] = (data[n] << 1) | (data[n + 1] >> 7);
145 data[len - 1] <<= 1;
149 // BSWAP24 of array[3]
150 uint32_t le24toh(uint8_t data[3]) {
151 return (data[2] << 16) | (data[1] << 8) | data[0];
154 // BSWAP24, take u32, output array
155 void htole24(uint32_t val, uint8_t data[3]) {
156 data[0] = (uint8_t) val;
157 data[1] = (uint8_t)(val >> 8);
158 data[2] = (uint8_t)(val >> 16);
162 // ROL on u32
163 uint32_t rotl(uint32_t a, uint8_t n) {
164 n &= 31;
165 return (a << n) | (a >> (32 - n));
168 // ROR on u32
169 uint32_t rotr(uint32_t a, uint8_t n) {
170 n &= 31;
171 return (a >> n) | (a << (32 - n));