Release v4.9237 - Ice Coffee :coffee:
[RRG-proxmark3.git] / common / commonutil.c
blob14f2d58e5fe5977618086278f6a2b7ae947f88b5
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 uint8_t reflect8(uint8_t b) {
64 return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
66 uint16_t reflect16(uint16_t b) {
67 uint16_t v = 0;
68 v |= (b & 0x8000) >> 15;
69 v |= (b & 0x4000) >> 13;
70 v |= (b & 0x2000) >> 11;
71 v |= (b & 0x1000) >> 9;
72 v |= (b & 0x0800) >> 7;
73 v |= (b & 0x0400) >> 5;
74 v |= (b & 0x0200) >> 3;
75 v |= (b & 0x0100) >> 1;
77 v |= (b & 0x0080) << 1;
78 v |= (b & 0x0040) << 3;
79 v |= (b & 0x0020) << 5;
80 v |= (b & 0x0010) << 7;
81 v |= (b & 0x0008) << 9;
82 v |= (b & 0x0004) << 11;
83 v |= (b & 0x0002) << 13;
84 v |= (b & 0x0001) << 15;
85 return v;
88 void num_to_bytes(uint64_t n, size_t len, uint8_t *dest) {
89 while (len--) {
90 dest[len] = (uint8_t) n;
91 n >>= 8;
95 uint64_t bytes_to_num(uint8_t *src, size_t len) {
96 uint64_t num = 0;
97 while (len--) {
98 num = (num << 8) | (*src);
99 src++;
101 return num;
104 // RotateLeft - Ultralight, Desfire
105 void rol(uint8_t *data, const size_t len) {
106 uint8_t first = data[0];
107 for (size_t i = 0; i < len - 1; i++) {
108 data[i] = data[i + 1];
110 data[len - 1] = first;
113 void lsl(uint8_t *data, size_t len) {
114 for (size_t n = 0; n < len - 1; n++) {
115 data[n] = (data[n] << 1) | (data[n + 1] >> 7);
117 data[len - 1] <<= 1;
120 uint32_t le24toh(uint8_t data[3]) {
121 return (data[2] << 16) | (data[1] << 8) | data[0];
124 void htole24(uint32_t val, uint8_t data[3]) {
125 data[0] = (uint8_t) val;
126 data[1] = (uint8_t)(val >> 8);
127 data[2] = (uint8_t)(val >> 16);