libbtc
bitcoinclibrary
Macros | Functions
utils.h File Reference
#include <stdint.h>
#include <stddef.h>

Go to the source code of this file.

Macros

#define TO_UINT8_HEX_BUF_LEN   2048
 
#define VARINT_LEN   20
 
#define strlens(s)   (s == NULL ? 0 : strlen(s))
 

Functions

void utils_clear_buffers (void)
 
void utils_hex_to_bin (const char *str, unsigned char *out, int inLen, int *outLen)
 
void utils_bin_to_hex (unsigned char *bin_in, size_t inlen, char *hex_out)
 
uint8_t * utils_hex_to_uint8 (const char *str)
 
char * utils_uint8_to_hex (const uint8_t *bin, size_t l)
 
void utils_reverse_hex (char *h, int len)
 
void utils_uint64_to_varint (char *vi, int *l, uint64_t i)
 
int utils_varint_to_uint64 (const char *vi, uint64_t *i)
 

Macro Definition Documentation

#define strlens (   s)    (s == NULL ? 0 : strlen(s))

Definition at line 40 of file utils.h.

Referenced by btc_hd_generate_key(), and utils_hex_to_uint8().

#define TO_UINT8_HEX_BUF_LEN   2048

Definition at line 37 of file utils.h.

Referenced by utils_clear_buffers(), utils_hex_to_uint8(), and utils_uint8_to_hex().

#define VARINT_LEN   20

Definition at line 38 of file utils.h.

Referenced by utils_uint64_to_varint(), and utils_varint_to_uint64().

Function Documentation

void utils_bin_to_hex ( unsigned char *  bin_in,
size_t  inlen,
char *  hex_out 
)

Definition at line 111 of file utils.c.

112 {
113  static char digits[] = "0123456789abcdef";
114  size_t i;
115  for (i = 0; i < inlen; i++) {
116  hex_out[i * 2] = digits[(bin_in[i] >> 4) & 0xF];
117  hex_out[i * 2 + 1] = digits[bin_in[i] & 0xF];
118  }
119  hex_out[inlen * 2] = '\0';
120 }
void utils_clear_buffers ( void  )

Definition at line 40 of file utils.c.

References buffer_hex_to_uint8, buffer_uint8_to_hex, and TO_UINT8_HEX_BUF_LEN.

41 {
44 }
static uint8_t buffer_hex_to_uint8[TO_UINT8_HEX_BUF_LEN]
Definition: utils.c:36
static char buffer_uint8_to_hex[TO_UINT8_HEX_BUF_LEN]
Definition: utils.c:37
#define TO_UINT8_HEX_BUF_LEN
Definition: utils.h:37
void utils_hex_to_bin ( const char *  str,
unsigned char *  out,
int  inLen,
int *  outLen 
)

Definition at line 46 of file utils.c.

47 {
48  int bLen = inLen / 2;
49  memset(out, 0, bLen);
50  uint8_t c;
51  int i;
52  for (i = 0; i < bLen; i++) {
53  c = 0;
54  if (str[i * 2] >= '0' && str[i * 2] <= '9') {
55  *out = (str[i * 2] - '0') << 4;
56  }
57  if (str[i * 2] >= 'a' && str[i * 2] <= 'f') {
58  *out = (10 + str[i * 2] - 'a') << 4;
59  }
60  if (str[i * 2] >= 'A' && str[i * 2] <= 'F') {
61  *out = (10 + str[i * 2] - 'A') << 4;
62  }
63  if (str[i * 2 + 1] >= '0' && str[i * 2 + 1] <= '9') {
64  *out |= (str[i * 2 + 1] - '0');
65  }
66  if (str[i * 2 + 1] >= 'a' && str[i * 2 + 1] <= 'f') {
67  *out |= (10 + str[i * 2 + 1] - 'a');
68  }
69  if (str[i * 2 + 1] >= 'A' && str[i * 2 + 1] <= 'F') {
70  *out |= (10 + str[i * 2 + 1] - 'A');
71  }
72  out++;
73  }
74  *outLen = i;
75 }
uint8_t* utils_hex_to_uint8 ( const char *  str)

Definition at line 77 of file utils.c.

References buffer_hex_to_uint8, strlens, and TO_UINT8_HEX_BUF_LEN.

78 {
79  if (strlens(str) > TO_UINT8_HEX_BUF_LEN) {
80  return NULL;
81  }
83  uint8_t c;
84  size_t i;
85  for (i = 0; i < strlens(str) / 2; i++) {
86  c = 0;
87  if (str[i * 2] >= '0' && str[i * 2] <= '9') {
88  c += (str[i * 2] - '0') << 4;
89  }
90  if (str[i * 2] >= 'a' && str[i * 2] <= 'f') {
91  c += (10 + str[i * 2] - 'a') << 4;
92  }
93  if (str[i * 2] >= 'A' && str[i * 2] <= 'F') {
94  c += (10 + str[i * 2] - 'A') << 4;
95  }
96  if (str[i * 2 + 1] >= '0' && str[i * 2 + 1] <= '9') {
97  c += (str[i * 2 + 1] - '0');
98  }
99  if (str[i * 2 + 1] >= 'a' && str[i * 2 + 1] <= 'f') {
100  c += (10 + str[i * 2 + 1] - 'a');
101  }
102  if (str[i * 2 + 1] >= 'A' && str[i * 2 + 1] <= 'F') {
103  c += (10 + str[i * 2 + 1] - 'A');
104  }
105  buffer_hex_to_uint8[i] = c;
106  }
107  return buffer_hex_to_uint8;
108 }
#define strlens(s)
Definition: utils.h:40
static uint8_t buffer_hex_to_uint8[TO_UINT8_HEX_BUF_LEN]
Definition: utils.c:36
#define TO_UINT8_HEX_BUF_LEN
Definition: utils.h:37
void utils_reverse_hex ( char *  h,
int  len 
)

Definition at line 140 of file utils.c.

References const_buffer::len.

Referenced by utils_uint64_to_varint(), and utils_varint_to_uint64().

141 {
142  char copy[len];
143  strncpy(copy, h, len);
144  int i;
145  for (i = 0; i < len; i += 2) {
146  h[i] = copy[len - i - 2];
147  h[i + 1] = copy[len - i - 1];
148  }
149 }
size_t len
Definition: buffer.h:22
void utils_uint64_to_varint ( char *  vi,
int *  l,
uint64_t  i 
)

Definition at line 152 of file utils.c.

References const_buffer::len, utils_reverse_hex(), and VARINT_LEN.

153 {
154  int len;
155  char v[VARINT_LEN];
156 
157  if (i < 0xfd) {
158  sprintf(v, "%02" PRIx64, i);
159  len = 2;
160  } else if (i <= 0xffff) {
161  sprintf(v, "%04" PRIx64, i);
162  sprintf(vi, "fd");
163  len = 4;
164  } else if (i <= 0xffffffff) {
165  sprintf(v, "%08" PRIx64, i);
166  sprintf(vi, "fe");
167  len = 8;
168  } else {
169  sprintf(v, "%016" PRIx64, i);
170  sprintf(vi, "ff");
171  len = 16;
172  }
173 
174  /* reverse order */
175  if (len > 2) {
176  utils_reverse_hex(v, len);
177  strncat(vi, v, len);
178  } else {
179  strncpy(vi, v, len);
180  }
181 
182  *l = len;
183 }
size_t len
Definition: buffer.h:22
#define VARINT_LEN
Definition: utils.h:38
void utils_reverse_hex(char *h, int len)
Definition: utils.c:140
char* utils_uint8_to_hex ( const uint8_t *  bin,
size_t  l 
)

Definition at line 123 of file utils.c.

References buffer_uint8_to_hex, and TO_UINT8_HEX_BUF_LEN.

124 {
125  if (l > (TO_UINT8_HEX_BUF_LEN / 2 - 1)) {
126  return NULL;
127  }
128  static char digits[] = "0123456789abcdef";
130  size_t i;
131  for (i = 0; i < l; i++) {
132  buffer_uint8_to_hex[i * 2] = digits[(bin[i] >> 4) & 0xF];
133  buffer_uint8_to_hex[i * 2 + 1] = digits[bin[i] & 0xF];
134  }
135  buffer_uint8_to_hex[l * 2] = '\0';
136  return buffer_uint8_to_hex;
137 }
static char buffer_uint8_to_hex[TO_UINT8_HEX_BUF_LEN]
Definition: utils.c:37
#define TO_UINT8_HEX_BUF_LEN
Definition: utils.h:37
int utils_varint_to_uint64 ( const char *  vi,
uint64_t *  i 
)

Definition at line 186 of file utils.c.

References const_buffer::len, utils_reverse_hex(), and VARINT_LEN.

187 {
188  char v[VARINT_LEN] = {0};
189  int len;
190 
191  if (!vi) {
192  len = 0;
193  } else if (!strncmp(vi, "ff", 2)) {
194  len = 16;
195  } else if (!strncmp(vi, "fe", 2)) {
196  len = 8;
197  } else if (!strncmp(vi, "fd", 2)) {
198  len = 4;
199  } else {
200  len = 2;
201  }
202 
203  if (len == 0) {
204  /* continue */
205  } else if (len > 2) {
206  strncpy(v, vi + 2, len);
207  utils_reverse_hex(v, len);
208  } else {
209  strncpy(v, vi, len);
210  }
211  *i = strtoull(v, NULL, 16);
212 
213  return len;
214 }
size_t len
Definition: buffer.h:22
#define VARINT_LEN
Definition: utils.h:38
void utils_reverse_hex(char *h, int len)
Definition: utils.c:140