-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.c
212 lines (190 loc) · 4.79 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* MIT License
*
* Copyright (c) 2023 Davidson Francis <davidsondfgl@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <ctype.h>
#include <stdlib.h>
#include "util.h"
/**
* Global buffer, responsible to handle dynamic allocations
* from/to GDB messages.
*/
static char *gbuffer = NULL;
static size_t gbuffer_size = 0;
/**
* @brief Increase the global buffer size to a new value
* if needed.
*
* @param new_size New buffer size.
*/
static void increase_buffer(size_t new_size)
{
char *tmp;
if (gbuffer_size < new_size)
{
tmp = realloc(gbuffer, new_size);
if (!tmp)
errx("Unable to allocate %zu bytes!\n", new_size);
gbuffer = tmp;
gbuffer_size = new_size;
}
}
/**
* @brief For a given nibble, to converts to its
* ascii representative form, ie: 10 -> a.
*
* @param nibble Nibble input value to be converted.
*
* @return Returns the converted value.
*/
static inline char to_digit(int nibble)
{
static const char digits[] = "0123456789abcdef";
return (digits[nibble]);
}
/**
* @brief For a given nibble (in its char form),
* convert to the decimal representation, i.e:
* 'b' -> 11.
*
* @param ch Char nibble to be converted.
*
* @return Returns the converted value.
*/
static inline int to_value(int ch)
{
int c = tolower(ch);
if (c >= '0' && c <= '9')
return (c - '0');
else if (c >= 'a' && c <= 'f')
return (0xA + c - 'a');
else
return (-1);
}
/**
* @brief Encodes a binary data inside @p data to its
* representative form in ascii hex value.
*
* @param data Data to be encoded in ascii-hex form.
* @param len Length of @p data.
*
* @return Returns a buffer containing the encoded
* buffer.
*
* @note Please note that the size of the output
* buffer is twice bigger than the input buffer.
*/
char *encode_hex(const char *data, size_t len)
{
char *tmp;
size_t i;
increase_buffer(len * 2);
for (i = 0, tmp = gbuffer; i < len; i++)
{
*tmp++ = to_digit((data[i] >> 4) & 0xF);
*tmp++ = to_digit((data[i] ) & 0xF);
}
return (gbuffer);
}
/**
* @brief Converts an input buffer containing an ascii
* hex-value representation into the equivalent binary
* form.
*
* @param data Input buffer to be decoded to binary.
* @param len Input buffer length.
*
* @return Returns the buffer containing the binary
* representation of the data.
*/
char *decode_hex(const char *data, size_t len)
{
char *ptr;
size_t i;
increase_buffer(len);
for (i = 0, ptr = gbuffer; i < len * 2; i += 2, ptr++)
{
*ptr = to_value(data[i]);
*ptr <<= 4;
*ptr |= to_value(data[i+1]);
}
return (gbuffer);
}
/**
* @brief Reads a given integer encoded in hex
* and returns it.
*
* @param buffer Buffer containing the integer to be
* read.
* @param len Buffer length. This variable is updated.
*
* @param endptr If not NULL, the position for the first
* non-hex digit character is saved.
*
* @param base Number base, whether 10 or 16.
*
* @return Returns the integer read.
*/
uint32_t read_int(const char *buff, size_t *len,
const char **endptr, int base)
{
int32_t ret;
char c;
int v;
ret = 0;
for (size_t i = 0; i < *len; i++)
{
c = tolower(buff[i]);
if (c >= '0' && c <= '9')
v = (c - '0');
else if (base == 16 && (c >= 'a' && c <= 'f'))
v = (c - 'a' + 10);
else
{
if (endptr)
{
*endptr = buff + i;
*len = *len - i;
}
goto out;
}
ret = ret * base + v;
}
out:
return (ret);
}
/**
* @brief Same behavior as read_int(), but do not
* updates @len nor returns where next non-digit
* char is.
*
* @param buf Buffer to be read.
* @param len Buffer length.
* @param base Number base, whether 10 or 16.
*
* @return Returns the integer read.
*/
uint32_t simple_read_int(const char *buf, size_t len, int base)
{
size_t l = len;
return read_int(buf, &l, NULL, base);
}