fix one too small
[RRG-proxmark3.git] / client / src / util.h
blob86c7a4f9c11d4cc608fbfed01c98d01c93fd768e
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 // utilities
17 //-----------------------------------------------------------------------------
18 #ifndef __UTIL_H_
19 #define __UTIL_H_
21 #include "common.h"
23 #ifdef ANDROID
24 #include <endian.h>
25 #endif
27 // used for save/load files
28 #ifndef FILE_PATH_SIZE
29 # define FILE_PATH_SIZE 1000
30 #endif
32 extern uint8_t g_debugMode;
33 extern uint8_t g_printAndLog;
34 extern bool g_pendingPrompt;
35 extern int g_numCPUs;
37 typedef struct {
38 char *ptr;
39 size_t size;
40 size_t idx;
41 } grabbed_output;
42 extern grabbed_output g_grabbed_output;
44 #define PRINTANDLOG_PRINT 1
45 #define PRINTANDLOG_LOG 2
46 #define PRINTANDLOG_GRAB 4
48 // Return error
49 #define PM3_RET_ERR(err, ...) { \
50 PrintAndLogEx(ERR, __VA_ARGS__); \
51 return err; \
54 #define PM3_RET_ERR_FREE(err, ...) { \
55 CLIParserFree(ctx); \
56 PrintAndLogEx(ERR, __VA_ARGS__); \
57 return err; \
60 // RETurn IF ERRor
61 #define PM3_RET_IF_ERR(res) if (res != PM3_SUCCESS) { return res; }
62 #define PM3_RET_IF_ERR_WITH_MSG(res, ...) if (res != PM3_SUCCESS) { PrintAndLogEx(ERR, __VA_ARGS__); return res; }
63 #define PM3_RET_IF_ERR_MAYBE_MSG(res, verbose, ...) if (res != PM3_SUCCESS) { if (verbose) PrintAndLogEx(ERR, __VA_ARGS__); return res; }
65 typedef enum {
66 C_NONE,
67 C_GREEN,
68 C_RED,
69 C_YELLOW,
70 } color_t;
72 int kbd_enter_pressed(void);
73 void FillFileNameByUID(char *filenamePrefix, const uint8_t *uid, const char *ext, const int uidlen);
74 // fill buffer from structure [{uint8_t data, size_t length},...]
75 int FillBuffer(uint8_t *data, size_t maxDataLength, size_t *dataLength, ...);
77 bool CheckStringIsHEXValue(const char *value);
78 void ascii_to_buffer(uint8_t *buf, const uint8_t *hex_data, const size_t hex_len,
79 const size_t hex_max_len, const size_t min_str_len);
80 void hex_to_buffer(uint8_t *buf, const uint8_t *hex_data, const size_t hex_len,
81 const size_t hex_max_len, const size_t min_str_len, const size_t spaces_between,
82 bool uppercase);
84 void print_hex(const uint8_t *data, const size_t len);
85 void print_hex_break(const uint8_t *data, const size_t len, const uint8_t breaks);
86 void print_hex_noascii_break(const uint8_t *data, const size_t len, uint8_t breaks);
88 char *sprint_hex(const uint8_t *data, const size_t len);
89 char *sprint_hex_inrow(const uint8_t *data, const size_t len);
90 char *sprint_hex_inrow_ex(const uint8_t *data, const size_t len, const size_t min_str_len);
91 char *sprint_hex_inrow_spaces(const uint8_t *data, const size_t len, size_t spaces_between);
92 char *sprint_bin(const uint8_t *data, const size_t len);
93 char *sprint_bytebits_bin(const uint8_t *data, const size_t len);
94 char *sprint_bytebits_bin_break(const uint8_t *data, const size_t len, const uint8_t breaks);
95 char *sprint_hex_ascii(const uint8_t *data, const size_t len);
96 char *sprint_ascii(const uint8_t *data, const size_t len);
97 char *sprint_ascii_ex(const uint8_t *data, const size_t len, const size_t min_str_len);
99 char *sprint_breakdown_bin(color_t color, const char *bs, int width, int padn, int bits, const char *msg);
101 void print_buffer_with_offset(const uint8_t *data, const size_t len, int offset, bool print_header);
102 void print_buffer(const uint8_t *data, const size_t len, int level);
103 void print_blocks(uint32_t *data, size_t len);
105 int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValueLen);
106 void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest);
107 void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest);
108 void bytes_to_bytebits(const void *src, const size_t srclen, void *dest);
110 // Swap endian on arrays up to 64bytes.
111 uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize);
112 void SwapEndian64ex(const uint8_t *src, const size_t len, const uint8_t blockSize, uint8_t *dest);
114 // parameter helper functions
115 int param_getlength(const char *line, int paramnum);
116 char param_getchar(const char *line, int paramnum);
117 char param_getchar_indx(const char *line, int indx, int paramnum);
118 int param_getptr(const char *line, int *bg, int *en, int paramnum);
119 uint8_t param_get8(const char *line, int paramnum);
120 uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base);
121 uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base);
122 uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base);
123 float param_getfloat(const char *line, int paramnum, float deflt);
124 uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination);
125 uint8_t param_isdec(const char *line, int paramnum);
126 int param_gethex_ex(const char *line, int paramnum, uint8_t *data, int *hexcnt);
127 int param_gethex_to_eol(const char *line, int paramnum, uint8_t *data, int maxdatalen, int *datalen);
128 int param_getbin_to_eol(const char *line, int paramnum, uint8_t *data, int maxdatalen, int *datalen);
129 int param_getstr(const char *line, int paramnum, char *str, size_t buffersize);
131 int hextobinarray(char *target, char *source);
132 int hextobinarray_n(char *target, char *source, int sourcelen);
134 int hextobinstring(char *target, char *source);
135 int hextobinstring_n(char *target, char *source, int sourcelen);
137 int binarray_2_hex(char *target, const size_t targetlen, const char *source, size_t srclen);
138 void binarray_2_binstr(char *target, char *source, int length);
139 int binstr_2_binarray(uint8_t *target, char *source, int length);
141 void bytes_2_binstr(char *target, const uint8_t *source, size_t sourcelen);
142 void binstr_2_bytes(uint8_t *target, size_t *targetlen, const char *src);
144 void hex_xor(uint8_t *d, uint8_t *x, int n);
146 uint8_t GetParity(const uint8_t *bits, uint8_t type, int length);
147 void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length);
148 void wiegand_add_parity_swapped(uint8_t *target, uint8_t *source, uint8_t length);
150 //void xor(unsigned char *dst, unsigned char *src, size_t len);
152 uint32_t PackBits(uint8_t start, uint8_t len, const uint8_t *bits);
153 uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor);
155 int num_CPUs(void);
156 int detect_num_CPUs(void); // number of logical CPUs
158 void str_lower(char *s); // converts string to lower case
159 void str_upper(char *s); // converts string to UPPER case
160 void strn_upper(char *s, size_t n);
162 bool str_startswith(const char *s, const char *pre); // check for prefix in string
163 bool str_endswith(const char *s, const char *suffix); // check for suffix in string
164 void clean_ascii(unsigned char *buf, size_t len);
165 void str_cleanrn(char *buf, size_t len);
166 void str_creplace(char *buf, size_t len, char from, char to);
167 void str_reverse(char *buf, size_t len);
168 void str_inverse_hex(char *buf, size_t len);
169 void str_inverse_bin(char *buf, size_t len);
171 char *str_dup(const char *src);
172 char *str_ndup(const char *src, size_t len);
173 size_t str_nlen(const char *src, size_t maxlen);
174 int hexstring_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const char *str);
175 int binstring_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const char *str);
176 int binarray_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const uint8_t *arr, int arrlen);
178 uint32_t bitcount32(uint32_t a);
179 uint64_t bitcount64(uint64_t a);
180 uint32_t leadingzeros32(uint32_t a);
181 uint64_t leadingzeros64(uint64_t a);
183 int byte_strstr(const uint8_t *src, size_t srclen, const uint8_t *pattern, size_t plen);
184 int byte_strrstr(const uint8_t *src, size_t srclen, const uint8_t *pattern, size_t plen);
186 struct smartbuf {
187 char *ptr;
188 size_t size;
189 size_t idx;
190 } typedef smartbuf;
191 void sb_append_char(smartbuf *sb, unsigned char c);
193 uint8_t get_highest_frequency(const uint8_t *d, uint8_t n);
194 #endif