-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrehex.c
450 lines (404 loc) · 12.2 KB
/
rehex.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <inttypes.h>
#include <string.h>
#ifndef _AIX
#include <getopt.h>
#endif
#ifdef INTEL
#include <emmintrin.h>
#include <xmmintrin.h>
#endif
/*
* rehex is a tool used to change encoding of a line. If a line
* has characters that can't be easily viewed as plain text (like embedded
* NUL characters, or high-bit set) it will wrap the result in $HEX[]
* to make it more useful.
*
* you can "unhex" the input with the -u flag, but no one should do that.
* Ever. :-)
*
* -S and -U allows you specify which characters are considered for $HEX[]
* encoding. You can specify them as characters "abc", or "a,b,f", as
* decimal values like "0,1,2,3,4,5,10,15", or hex values "0x00,0x1,0x02"
* or as ranges "a-f", "0-10","0x1a-0x1f". -S sets, and -U unsets the
* character as being considered. If any character in a given line contains
* one or more marked values (appearing as a 1 in the map), then the entire
* line is marked as $HEX[] required. You can force no hex conversion
* with -U 0-255.
*/
static char *Version = "$Header: /home/dlr/src/mdfind/RCS/rehex.c,v 1.7 2020/07/31 02:36:55 dlr Exp dlr $";
/*
* $Log: rehex.c,v $
* Revision 1.7 2020/07/31 02:36:55 dlr
* Add -S/-U to allow $HEX[] map forcing.
*
* Revision 1.6 2020/07/30 22:02:47 dlr
* Portability improvements for clang
*
* Revision 1.5 2020/07/29 06:13:42 dlr
* Better support for windows binary i/o
*
* Revision 1.4 2020/07/26 16:53:22 dlr
* wildcard expansion for windows
*
* Revision 1.3 2020/07/25 01:17:14 dlr
* Minor updates for llen
*
* Revision 1.2 2020/07/24 22:18:15 dlr
* Improve version handling
*
* Revision 1.1 2020/07/24 22:15:33 dlr
* Initial revision
*
*/
/* start with a 10k line size. It will expand this as required, if you
* have long lines
*/
#define CACHESIZE 10240
char *Cache;
uint64_t Cachesize;
int Unhex;
int _dowildcard = -1; /* enable wildcard expansion for Windows */
/*
* findeol(pointer, length)
*
* findeol searches for the next eol character (\n, 0x0a) in a string
*
* The Intel version uses SSE to process 64 bits at a time. This only
* is able to work because I ensure that the Fileinmem buffer has adequate
* space (16 bytes) following it to ensure that reading past the end won't
* read memory not available and cause a fault.
*
* This is important to the operation of this program, and care should be
* taken to ensure that the performance of this function is kept fast
*/
#if !defined(POWERPC) && !defined(INTEL)
#define findeol(a,b) memchr(a,10,b)
#endif
#ifdef POWERPC
#define findeol(a,b) memchr(a,10,b)
#endif
#ifdef INTEL
inline char *findeol(char *s, int64_t l) {
unsigned int align, res, f;
__m128i cur, seek;
if (l <=0) return (NULL);
seek = _mm_set1_epi8('\n');
align = ((uint64_t) s) & 0xf;
s = (char *) (((uint64_t) s) & 0xfffffffffffffff0L);
cur = _mm_load_si128((__m128i const *) s);
res = _mm_movemask_epi8(_mm_cmpeq_epi8(seek, cur)) >> align;
f = ffs(res);
res <<= align;
if (f && (f <= l))
return (s + ffs(res) - 1);
s += 16;
l -= (16 - align);
while (l >= 16) {
cur = _mm_load_si128((__m128i const *) s);
res = _mm_movemask_epi8(_mm_cmpeq_epi8(seek, cur));
f = ffs(res);
if (f)
return (s + f - 1);
s += 16;
l -= 16;
}
if (l > 0) {
cur = _mm_load_si128((__m128i const *) s);
res = _mm_movemask_epi8(_mm_cmpeq_epi8(seek, cur));
f = ffs(res);
if (f && (f <= l)) {
return (s + f - 1);
}
}
return (NULL);
}
#endif
char MustHex[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10-1f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20-2f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 30-3f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40-4f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50-5f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60-6f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 70-7f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 80-8f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 90-9f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* a0-af */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* b0-bf */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* c0-cf */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* d0-df */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e0-ef */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};/* f0-ff */
char ToHex[] = "0123456789abcdef";
unsigned char trhex[] = {
17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 16, 16, 17, 16, 16, /* 00-0f */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 10-1f */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 20-2f */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 16, 16, 16, 16, 16, /* 30-3f */
16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 40-4f */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 50-5f */
16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 60-6f */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 70-7f */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 80-8f */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 90-9f */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* a0-af */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* b0-bf */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* c0-cf */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* d0-df */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* e0-ef */
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};/* f0-ff */
int get32(char *iline, char *dest) {
unsigned char c,c1,c2, *line = (unsigned char *)iline;
int cnt;
cnt = 0;
while ((c=*line++)) {
c1 = trhex[c];
c2 = trhex[*line++];
if (c1 > 15 || c2 >15)
break;
cnt++;
*dest++ = (c1<<4) + c2;
}
return(cnt);
}
char *Lbuf;
int64_t Lbufsize;
void process(FILE *fi, char *fn) {
char *in, *eol, *end;
size_t readsize;
int64_t cur,size, len, offset;
int64_t x, needshex, hexlen, llen, ilen;
int Ateof;
if (!Cache || Cachesize == 0) {
Cache = malloc(CACHESIZE + 16);
if (!Cache) {
fprintf(stderr,"Could not allocate %d bytes for cache\n",CACHESIZE);
exit(1);
}
Cachesize = CACHESIZE;
}
readsize = fread(Cache,1,Cachesize,fi);
if (readsize == 0) return;
cur = 0;
size = readsize;
in = &Cache[cur];
end = &Cache[size];
Ateof = 0;
while (!Ateof) {
again:
if (size)
eol = findeol(in,size-cur);
else
eol = NULL;
if (!eol && !Ateof) { /* Can't find end of line */
if (cur >= size) { /* out of buffer */
if (cur > size) cur = size;
len = size - cur;
if (len >0) memcpy(Cache,&Cache[cur],len);
size -= cur;
cur = 0;
readsize = fread(&Cache[size],1,Cachesize - size,fi);
if (readsize == 0) Ateof = 1;
size += readsize;
end = &Cache[size];
in = &Cache[cur];
if (size == cur) break;
goto again;
}
/* Not in the cache. increase size */
Cache = realloc(Cache, (Cachesize *2) + 16);
if (!Cache) {
fprintf(stderr,"Could not expand cache to %"PRIu64" bytes. Make more memory\navailable, or check the input file \"%s\"\n",Cachesize*2,fn);
exit(1);
}
Cachesize = (Cachesize*2);
readsize = fread(&Cache[size],1,Cachesize - size,fi);
if (readsize == 0) Ateof = 1;
size += readsize;
end = &Cache[size];
in = &Cache[cur];
goto again;
}
if (!eol && Ateof) eol = end;
llen = len = eol - in;
if (eol > in && eol[-1] == '\r') llen--;
if (Unhex) {
if (strncmp(in,"$HEX[",5) == 0) {
in[llen] = 0;
hexlen = get32(&in[5],in);
} else
hexlen = llen;
in[hexlen] = '\n';
if (fwrite(in,hexlen+1,1,stdout) != 1) {
fprintf(stderr,"Write error.\n");
perror("stdout");
exit(1);
}
} else {
for (needshex = x = 0; !needshex && x <llen; x++)
needshex |= MustHex[(unsigned char)in[x]];
if (needshex) {
if (Lbufsize < (llen*2+7)) {
if (!Lbuf) {
Lbufsize = (llen*2+7) + CACHESIZE;
Lbuf = malloc(Lbufsize);
if (!Lbuf) {
fprintf(stderr,"Could not allocate memory for local buffer\n");
exit(1);
}
} else {
Lbuf = realloc(Lbuf,Lbufsize + (llen*2+7));
if (!Lbuf) {
fprintf(stderr,"Could not grow local buffer by %"PRIu64" bytes\n",(uint64_t)(llen)*2 + 7L);
exit(1);
}
Lbufsize += (llen*2)+7;
}
}
ilen = 5;
strcpy(Lbuf,"$HEX[");
for (x=0; x < llen; x++) {
Lbuf[ilen++] = ToHex[(in[x] >>4)&0xf];
Lbuf[ilen++] = ToHex[(in[x])&0xf];
}
Lbuf[ilen++] = ']';
llen = ilen; in = Lbuf;
}
in[llen] = '\n';
if (fwrite(in,llen+1,1,stdout) != 1) {
fprintf(stderr,"Write error.\n");
perror("stdout");
exit(1);
}
}
cur += len + 1;
in = &Cache[cur];
}
}
void getval (char **i, int *val) {
char *v = *i;
if (!*v) return;
if (*v == '-' || *v == ',') v++;
if (*v == '0' && v[1] == 'x' && sscanf(v,"0x%x",val) == 1) {
if (*val < 0 || *val > 255) {
fprintf(stderr,"Hex value out of range at: %s\n",*i);
exit(1);
}
v += 2;
while (*v) {
if (*v == ',' || *v == '-') break;
v++;
}
*i = v;
return;
}
if (isdigit(*v)) {
*val = atoi(v);
if (*val < 0 || *val > 255) {
fprintf(stderr,"Invalid number at: %s\n",*i);
exit(1);
}
while (*v && isdigit(*v)) {
v++;
}
*i = v;
return;
}
*val = *v++;
if (*val < 0 || *val > 255) {
fprintf(stderr,"Invalid number at: %s\n",*i);
exit(1);
}
*i = v;
}
int main(int argc,char **argv) {
int ch,x;
FILE *fi;
char *v;
#ifndef _AIX
struct option longopt[] = {
{NULL,0,NULL,0}
};
#endif
Cache = NULL;
Cachesize = 0;
Unhex = 0;
#ifdef _AIX
while ((ch = getopt(argc, argv, "?huU:S:")) != -1) {
#else
while ((ch = getopt_long(argc, argv, "?huU:S:",longopt,NULL)) != -1) {
#endif
switch(ch) {
case 'S':
case 'U':
v = optarg;
while (*v) {
int start,end;
getval(&v,&start);
end = start;
if (*v == '-') getval(&v, &end);
for (x=start; x <= end; x++)
MustHex[x] = (ch =='S')? 1 : 0;
}
fprintf(stderr,"Will map characters to $HEX[] if a 1 in that character position");
for (x=0; x <256; x++) {
if ((x%32) == 0) fprintf(stderr,"\n%02x-%02x:",x,x+31);
fprintf(stderr,"%d",MustHex[x]);
}
fprintf(stderr,"\n");
break;
case 'u':
Unhex = 1;
break;
case 'h':
case '?':
default:
v = Version;
while (*v++ != ' ')
;
while (*v++ !=' ')
;
fprintf(stderr,"rehex Version %s\n\nrehex [-u] [file file...]\nIf no files supplied, reads from stdin. Always writes to stdout\nIf stdin is used as a filename, the actual stdin will read\n",v);
fprintf(stderr,"\t-S exp\t\tSets $HEX[] conversion for char or range\n");
fprintf(stderr,"\t-U exp\t\tResets $HEX[] conversion for char or range\n");
fprintf(stderr,"\t\t\tSpecify a character like a,b,c, or a range like a-f,\n");
fprintf(stderr,"\t\t\t0x61-0x66 or as decimal values line 0-32.\n");
exit(1);
}
}
argc -= optind;
argv += optind;
#ifdef _WIN32
setmode(1,O_BINARY);
#endif
if (argc == 0) {
#ifdef _WIN32
setmode(0,O_BINARY);
#endif
process(stdin,"stdin");
}
for (x=0; x<argc; x++) {
if (strcmp(argv[x],"stdin") == 0) {
fi = stdin;
#ifdef _WIN32
setmode(0,O_BINARY);
#endif
} else
fi = fopen(argv[x],"rb");
if (!fi) {
fprintf(stderr,"Can't open %s, skipping\n",argv[x]);
continue;
}
process(fi,argv[x]);
fclose(fi);
}
return(0);
}