libbtc
bitcoinclibrary
utils.c
Go to the documentation of this file.
1 /*
2 
3  The MIT License (MIT)
4 
5  Copyright (c) 2015 Douglas J. Bakkum
6  Copyright (c) 2015 Jonas Schnelli
7 
8  Permission is hereby granted, free of charge, to any person obtaining
9  a copy of this software and associated documentation files (the "Software"),
10  to deal in the Software without restriction, including without limitation
11  the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  and/or sell copies of the Software, and to permit persons to whom the
13  Software is furnished to do so, subject to the following conditions:
14 
15  The above copyright notice and this permission notice shall be included
16  in all copies or substantial portions of the Software.
17 
18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
22  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  OTHER DEALINGS IN THE SOFTWARE.
25 
26 */
27 
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <inttypes.h>
32 
33 #include "utils.h"
34 
35 
38 
39 
41 {
44 }
45 
46 void utils_hex_to_bin(const char* str, unsigned char* out, int inLen, int* outLen)
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 }
76 
77 uint8_t* utils_hex_to_uint8(const char* str)
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 }
109 
110 
111 void utils_bin_to_hex(unsigned char* bin_in, size_t inlen, char* hex_out)
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 }
121 
122 
123 char* utils_uint8_to_hex(const uint8_t* bin, size_t l)
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 }
138 
139 
140 void utils_reverse_hex(char* h, int len)
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 }
150 
151 
152 void utils_uint64_to_varint(char* vi, int* l, uint64_t i)
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 }
184 
185 
186 int utils_varint_to_uint64(const char* vi, uint64_t* i)
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 strlens(s)
Definition: utils.h:40
#define VARINT_LEN
Definition: utils.h:38
uint8_t * utils_hex_to_uint8(const char *str)
Definition: utils.c:77
void utils_hex_to_bin(const char *str, unsigned char *out, int inLen, int *outLen)
Definition: utils.c:46
static uint8_t buffer_hex_to_uint8[TO_UINT8_HEX_BUF_LEN]
Definition: utils.c:36
void utils_clear_buffers(void)
Definition: utils.c:40
static char buffer_uint8_to_hex[TO_UINT8_HEX_BUF_LEN]
Definition: utils.c:37
int utils_varint_to_uint64(const char *vi, uint64_t *i)
Definition: utils.c:186
#define TO_UINT8_HEX_BUF_LEN
Definition: utils.h:37
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: utils.c:123
void utils_bin_to_hex(unsigned char *bin_in, size_t inlen, char *hex_out)
Definition: utils.c:111
void utils_uint64_to_varint(char *vi, int *l, uint64_t i)
Definition: utils.c:152