forked from albertobsd/keyhunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexcharstoraw.c
251 lines (237 loc) · 6.31 KB
/
hexcharstoraw.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
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
251
/*
Develop by Luis Alberto
email: [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "util.h"
int MAXLENGTHADDRESS = 32;
void _sort(char *arr,int n);
void _insertionsort(char *arr, int n);
void _introsort(char *arr,int depthLimit, int n);
void swap(char *a,char *b);
int partition(char *arr, int n);
void heapsort(char *arr, int n);
void heapify(char *arr, int n, int i);
int main(int argc,char **argv) {
int readed;
char *line_input,*line_output,*aux;
FILE *input,*output;
int len,diff,i,count = 0;
if(argc != 3) {
printf("Usage:\n%s <input hex file> <output binay file>\n",argv[0]);
exit(0);
}
input = fopen(argv[1],"r");
output = fopen(argv[2],"wb");
line_input = malloc(1024);
line_output = malloc(MAXLENGTHADDRESS);
if(input == NULL || output == NULL || line_input== NULL || line_output == NULL ) {
printf("error fopen or malloc\n");
}
while(!feof(input)) {
aux = fgets(line_input,1024,input);
if(aux == line_input) {
trim(line_input,"\t\n\r ");
len = strlen(line_input);
if(len <= 64){
if(isValidHex(line_input)) {
if(len < 64) {
aux = malloc(65);
diff = 64 - len;
strcpy(aux+diff,line_input);
memset(aux,'0',diff);
memcpy(line_input,aux,65);
free(aux);
}
hexs2bin(line_input,(unsigned char *)line_output);
fwrite(line_output,1,MAXLENGTHADDRESS,output);
count++;
}
else {
printf("Ignoring invalid hexadecimal line: %s\n",line_input);
}
}
else {
printf("Ignoring invalid length line: %s\n",line_input);
}
}
}
fclose(input);
fclose(output);
free(line_input);
output = fopen(argv[2],"rb");
printf("Allocating memory...\n");
do {
line_input = malloc(count*MAXLENGTHADDRESS);
} while(line_input == NULL);
i = 0;
while(i < count) {
readed = fread(line_input+(i*32),1,MAXLENGTHADDRESS,output);
if(readed != MAXLENGTHADDRESS) {
fprintf(stderr,"error fread()\n");
}
i++;
}
fclose(output);
output = fopen(argv[2],"wb");
printf("File %s was create with %u records\n",argv[2],count);
printf("Sorting once... ");
_sort(line_input,count);
_insertionsort(line_input,count);
i = 0;
while(i < count) {
fwrite(line_input+(i*32),1,32,output);
i++;
}
fclose(output);
printf("ready\n");
free(line_output);
return 0;
}
void swap(char *a,char *b) {
char t[MAXLENGTHADDRESS];
memcpy(t,a,MAXLENGTHADDRESS);
memcpy(a,b,MAXLENGTHADDRESS);
memcpy(b,t,MAXLENGTHADDRESS);
}
void _sort(char *arr,int n) {
int depthLimit = ((int) ceil(log(n))) * 2;
_introsort(arr,depthLimit,n);
}
void _introsort(char *arr,int depthLimit, int n) {
int p;
if(n > 1) {
if(n <= 16) {
_insertionsort(arr,n);
}
else {
if(depthLimit == 0) {
heapsort(arr,n);
}
else {
p = partition(arr,n);
if(p >= 2) {
_introsort(arr , depthLimit-1 , p);
}
if((n - (p + 1)) >= 2 ) {
_introsort(arr + ((p+1) *MAXLENGTHADDRESS) , depthLimit-1 , n - (p + 1));
}
}
}
}
}
void _insertionsort(char *arr, int n) {
char *arrj,*temp;
char key[MAXLENGTHADDRESS];
int j,i;
for(i = 1; i < n ; i++ ) {
j= i-1;
memcpy(key,arr + (i*MAXLENGTHADDRESS),MAXLENGTHADDRESS);
arrj = arr + (j*MAXLENGTHADDRESS);
while(j >= 0 && memcmp(arrj,key,MAXLENGTHADDRESS) > 0) {
memcpy(arr + ((j+1)*MAXLENGTHADDRESS),arrj,MAXLENGTHADDRESS);
j--;
arrj = arr + (j*MAXLENGTHADDRESS);
}
memcpy(arr + ((j+1)*MAXLENGTHADDRESS),key,MAXLENGTHADDRESS);
}
}
int partition(char *arr, int n) {
char pivot[MAXLENGTHADDRESS];
int j,i,t, r = (int) n/2,jaux = -1,iaux = -1, iflag, jflag;
char *a,*b,*hextemp,*hextemp_pivot;
i = - 1;
memcpy(pivot,arr + (r*MAXLENGTHADDRESS),MAXLENGTHADDRESS);
i = 0;
j = n-1;
do {
iflag = 1;
jflag = 1;
t = memcmp(arr + (i*MAXLENGTHADDRESS),pivot,MAXLENGTHADDRESS);
iflag = (t <= 0);
while(i < j && iflag) {
i++;
t = memcmp(arr + (i*MAXLENGTHADDRESS),pivot,MAXLENGTHADDRESS);
iflag = (t <= 0);
}
t = memcmp(arr + (j*MAXLENGTHADDRESS),pivot,MAXLENGTHADDRESS);
jflag = (t > 0);
while(i < j && jflag) {
j--;
t = memcmp(arr + (j*MAXLENGTHADDRESS),pivot,MAXLENGTHADDRESS);
jflag = (t > 0);
}
if(i < j) {
if(i == r ) {
r = j;
}
else {
if(j == r ) {
r = i;
}
}
swap(arr + (i*MAXLENGTHADDRESS),arr + (j*MAXLENGTHADDRESS) );
jaux = j;
iaux = i;
j--;
i++;
}
} while(j > i );
if(jaux != -1 && iaux != -1) {
if(iflag || jflag) {
if(iflag) {
if(r != j)
swap(arr + (r*MAXLENGTHADDRESS),arr + ((j )*MAXLENGTHADDRESS) );
jaux = j;
}
if(jflag) {
if(r != j-1)
swap(arr + (r*MAXLENGTHADDRESS),arr + ((j-1 )*MAXLENGTHADDRESS) );
jaux = j-1;
}
}
else{
if(r != j)
swap(arr + (r*MAXLENGTHADDRESS),arr + ((j )*MAXLENGTHADDRESS) );
jaux = j;
}
}
else {
if(iflag && jflag) {
jaux = r;
}
else {
if(iflag ) {
swap(arr + (r*MAXLENGTHADDRESS),arr + ((j)*MAXLENGTHADDRESS) );
jaux = j;
}
}
}
return jaux;
}
void heapify(char *arr, int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && memcmp(arr +(l*MAXLENGTHADDRESS),arr +(largest * MAXLENGTHADDRESS),MAXLENGTHADDRESS) > 0)
largest = l;
if (r < n && memcmp(arr +(r*MAXLENGTHADDRESS),arr +(largest *MAXLENGTHADDRESS),MAXLENGTHADDRESS) > 0)
largest = r;
if (largest != i) {
swap(arr +(i*MAXLENGTHADDRESS), arr +(largest*MAXLENGTHADDRESS));
heapify(arr, n, largest);
}
}
void heapsort(char *arr, int n) {
int i;
for ( i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for ( i = n - 1; i > 0; i--) {
swap(arr , arr +(i*MAXLENGTHADDRESS));
heapify(arr, i, 0);
}
}