textual
[RRG-proxmark3.git] / client / src / util.h
blob2730ed44fd3ddf4a8f031e28ba2877c3a489d7e7
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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 // utilities
9 //-----------------------------------------------------------------------------
10 #ifndef __UTIL_H_
11 #define __UTIL_H_
13 #include "common.h"
15 #ifdef ANDROID
16 #include <endian.h>
17 #endif
19 // used for save/load files
20 #ifndef FILE_PATH_SIZE
21 # define FILE_PATH_SIZE 1000
22 #endif
24 extern uint8_t g_debugMode;
25 extern uint8_t g_printAndLog;
26 extern bool g_pendingPrompt;
28 #define PRINTANDLOG_PRINT 1
29 #define PRINTANDLOG_LOG 2
31 int kbd_enter_pressed(void);
32 void FillFileNameByUID(char *filenamePrefix, const uint8_t *uid, const char *ext, const int uidlen);
33 // fill buffer from structure [{uint8_t data, size_t length},...]
34 int FillBuffer(uint8_t *data, size_t maxDataLength, size_t *dataLength, ...);
36 bool CheckStringIsHEXValue(const char *value);
37 void hex_to_buffer(const uint8_t *buf, const uint8_t *hex_data, const size_t hex_len,
38 const size_t hex_max_len, const size_t min_str_len, const size_t spaces_between,
39 bool uppercase);
41 void print_hex(const uint8_t *data, const size_t len);
42 void print_hex_break(const uint8_t *data, const size_t len, const uint8_t breaks);
43 char *sprint_hex(const uint8_t *data, const size_t len);
44 char *sprint_hex_inrow(const uint8_t *data, const size_t len);
45 char *sprint_hex_inrow_ex(const uint8_t *data, const size_t len, const size_t min_str_len);
46 char *sprint_hex_inrow_spaces(const uint8_t *data, const size_t len, size_t spaces_between);
47 char *sprint_bin(const uint8_t *data, const size_t len);
48 char *sprint_bin_break(const uint8_t *data, const size_t len, const uint8_t breaks);
49 char *sprint_hex_ascii(const uint8_t *data, const size_t len);
50 char *sprint_ascii(const uint8_t *data, const size_t len);
51 char *sprint_ascii_ex(const uint8_t *data, const size_t len, const size_t min_str_len);
53 void print_buffer(const uint8_t *data, const size_t len, int level);
54 void print_blocks(uint32_t *data, size_t len);
56 int hex_to_bytes(const char *hexValue, uint8_t *bytesValue, size_t maxBytesValueLen);
57 void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest);
58 void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest);
59 void bytes_to_bytebits(void *src, size_t srclen, void *dest);
61 // Swap endian on arrays up to 64bytes.
62 uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize);
63 void SwapEndian64ex(const uint8_t *src, const size_t len, const uint8_t blockSize, uint8_t *dest);
65 // parameter helper functions
66 int param_getlength(const char *line, int paramnum);
67 char param_getchar(const char *line, int paramnum);
68 char param_getchar_indx(const char *line, int indx, int paramnum);
69 int param_getptr(const char *line, int *bg, int *en, int paramnum);
70 uint8_t param_get8(const char *line, int paramnum);
71 uint8_t param_get8ex(const char *line, int paramnum, int deflt, int base);
72 uint32_t param_get32ex(const char *line, int paramnum, int deflt, int base);
73 uint64_t param_get64ex(const char *line, int paramnum, int deflt, int base);
74 float param_getfloat(const char *line, int paramnum, float deflt);
75 uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination);
76 uint8_t param_isdec(const char *line, int paramnum);
77 int param_gethex(const char *line, int paramnum, uint8_t *data, int hexcnt);
78 int param_gethex_ex(const char *line, int paramnum, uint8_t *data, int *hexcnt);
79 int param_gethex_to_eol(const char *line, int paramnum, uint8_t *data, int maxdatalen, int *datalen);
80 int param_getbin_to_eol(const char *line, int paramnum, uint8_t *data, int maxdatalen, int *datalen);
81 int param_getstr(const char *line, int paramnum, char *str, size_t buffersize);
83 int hextobinarray(char *target, char *source);
84 int hextobinstring(char *target, char *source);
85 int binarraytohex(char *target, const size_t targetlen, char *source, size_t srclen);
86 void binarraytobinstring(char *target, char *source, int length);
87 int binstring2binarray(uint8_t *target, char *source, int length);
89 uint8_t GetParity(uint8_t *bits, uint8_t type, int length);
90 void wiegand_add_parity(uint8_t *target, uint8_t *source, uint8_t length);
91 void wiegand_add_parity_swapped(uint8_t *target, uint8_t *source, uint8_t length);
93 //void xor(unsigned char *dst, unsigned char *src, size_t len);
95 uint32_t PackBits(uint8_t start, uint8_t len, uint8_t *bits);
96 uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor);
98 int num_CPUs(void); // number of logical CPUs
100 void str_lower(char *s); // converts string to lower case
101 bool str_startswith(const char *s, const char *pre); // check for prefix in string
102 bool str_endswith(const char *s, const char *suffix); // check for suffix in string
103 void clean_ascii(unsigned char *buf, size_t len);
104 void strcleanrn(char *buf, size_t len);
105 void strcreplace(char *buf, size_t len, char from, char to);
106 char *str_dup(const char *src);
107 char *str_ndup(const char *src, size_t len);
108 int hexstring_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const char *str);
109 int binstring_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, const char *str);
110 int binarray_to_u96(uint32_t *hi2, uint32_t *hi, uint32_t *lo, uint8_t *arr, int arrlen);
112 uint32_t bitcount32(uint32_t a);
113 uint64_t bitcount64(uint64_t a);
114 uint32_t leadingzeros32(uint32_t a);
115 uint64_t leadingzeros64(uint64_t a);
117 #endif