-
Notifications
You must be signed in to change notification settings - Fork 20
/
util.cpp
250 lines (196 loc) · 3.78 KB
/
util.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright (c) 2011-2013 by naehrwert
* This file is released under the GPLv2.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "types.h"
#include "util.h"
#include "zlib.h"
#include "mt19937.h"
void _hexdump(FILE *fp, const char *name, u32 offset, u8 *buf, int len, BOOL print_addr)
{
int i, j, align = strlen(name) + 1;
fprintf(fp, "%s ", name);
if(print_addr == TRUE)
fprintf(fp, "%08x: ", offset);
for(i = 0; i < len; i++)
{
if(i % 16 == 0 && i != 0)
{
fprintf(fp, "\n");
for(j = 0; j < align; j++)
putchar(' ');
if(print_addr == TRUE)
fprintf(fp, "%08X: ", offset + i);
}
fprintf(fp, "%02X ", buf[i]);
}
fprintf(fp, "\n");
}
void _print_align(FILE *fp, const s8 *str, s32 align, s32 len)
{
s32 i, tmp;
tmp = align - len;
if(tmp < 0)
tmp = 0;
for(i = 0; i < tmp; i++)
fputs(str, fp);
}
u8 *_read_buffer(const s8 *file, u32 *length)
{
FILE *fp;
u32 size;
if((fp = fopen(file, "rb")) == NULL)
return NULL;
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
u8 *buffer = (u8 *)malloc(sizeof(u8) * size);
fread(buffer, sizeof(u8), size, fp);
if(length != NULL)
*length = size;
fclose(fp);
return buffer;
}
int _write_buffer(const s8 *file, u8 *buffer, u32 length)
{
FILE *fp;
if((fp = fopen(file, "wb")) == NULL)
return 0;
/**/
while(length > 0)
{
u32 wrlen = 1024;
if(length < 1024)
wrlen = length;
fwrite(buffer, sizeof(u8), wrlen, fp);
length -= wrlen;
buffer += 1024;
}
/**/
//fwrite(buffer, sizeof(u8), length, fp);
fclose(fp);
return 1;
}
const s8 *_get_name(id_to_name_t *tab, u64 id)
{
u32 i = 0;
while(!(tab[i].name == NULL && tab[i].id == 0))
{
if(tab[i].id == id)
return tab[i].name;
i++;
}
return NULL;
}
u64 _get_id(id_to_name_t *tab, const s8 *name)
{
u32 i = 0;
while(!(tab[i].name == NULL && tab[i].id == 0))
{
if(strcmp(tab[i].name, name) == 0)
return tab[i].id;
i++;
}
return (u64)(-1);
}
void _zlib_inflate(u8 *in, u64 len_in, u8 *out, u64 len_out)
{
z_stream s;
memset(&s, 0, sizeof(z_stream));
s.zalloc = Z_NULL;
s.zfree = Z_NULL;
s.opaque = Z_NULL;
inflateInit(&s);
s.avail_in = len_in;
s.next_in = in;
s.avail_out = len_out;
s.next_out = out;
inflate(&s, Z_FINISH);
inflateEnd(&s);
}
void _zlib_deflate(u8 *in, u64 len_in, u8 *out, u64 len_out)
{
z_stream s;
memset(&s, 0, sizeof(z_stream));
s.zalloc = Z_NULL;
s.zfree = Z_NULL;
s.opaque = Z_NULL;
deflateInit(&s, Z_BEST_COMPRESSION);
s.avail_in = len_in;
s.next_in = in;
s.avail_out = len_out;
s.next_out = out;
deflate(&s, Z_FINISH);
deflateEnd(&s);
}
static mt19937_ctxt_t _mt19937_ctxt;
static BOOL _mt_init = FALSE;
u8 _get_rand_byte()
{
if(_mt_init == FALSE)
{
_mt_init = TRUE;
mt19937_init(&_mt19937_ctxt, clock());
}
return (u8)(mt19937_update(&_mt19937_ctxt) & 0xFF);
}
void _fill_rand_bytes(u8 *dst, u32 len)
{
u32 i;
for(i = 0; i < len; i++)
dst[i] = _get_rand_byte();
}
void _memcpy_inv(u8 *dst, u8 *src, u32 len)
{
u32 i;
for (i = 0; i < len; i++)
dst[i] = ~src[i];
}
void *_memdup(void *ptr, u32 size)
{
void *res = malloc(size);
if(res != NULL)
memcpy(res, ptr, size);
return res;
}
u64 _x_to_u64(const s8 *hex)
{
u64 t = 0, res = 0;
u32 len = strlen(hex);
char c;
while(len--)
{
c = *hex++;
if(c >= '0' && c <= '9')
t = c - '0';
else if(c >= 'a' && c <= 'f')
t = c - 'a' + 10;
else if(c >= 'A' && c <= 'F')
t = c - 'A' + 10;
else
t = 0;
res |= t << (len * 4);
}
return res;
}
u8 *_x_to_u8_buffer(const s8 *hex)
{
u32 len = strlen(hex);
s8 xtmp[3] = {0, 0, 0};
//Must be aligned to 2.
if(len % 2 != 0)
return NULL;
u8 *res = (u8 *)malloc(sizeof(u8) * len);
u8 *ptr = res;
while(len--)
{
xtmp[0] = *hex++;
xtmp[1] = *hex++;
*ptr++ = (u8)_x_to_u64(xtmp);
}
return res;
}