-
Notifications
You must be signed in to change notification settings - Fork 42
/
misc.c
180 lines (150 loc) · 4.31 KB
/
misc.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
/* hexedit -- Hexadecimal Editor for Binary Files
Copyright (C) 1998 Pixel (Pascal Rigaux)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/
#include "hexedit.h"
int LSEEK_(int fd, INT where)
{
INT result;
result = lseek(fd, where, SEEK_SET);
return (result == where) ? 1 : -1;
}
void LSEEK(int fd, INT where)
{
INT result;
result = lseek(fd, where, SEEK_SET);
if (result != where) {
exitCurses();
fprintf(stderr, "the long seek failed (%lld instead of %lld), leaving :(\n", (long long) result, (long long) where);
exit(1);
}
}
/*******************************************************************************/
/* Functions provided for OSs that don't have them */
/*******************************************************************************/
#ifndef HAVE_BASENAME
char *basename(char *file) {
char *p = strrchr(file, '/');
return p ? p + 1 : file;
}
#endif
#ifndef HAVE_STRERROR
char *strerror(int errnum) {
extern char *sys_errlist[];
extern int sys_nerr;
if (errnum > 0 && errnum <= sys_nerr)
return sys_errlist[errnum];
return _("Unknown system error");
}
#endif
#ifndef HAVE_STRDUP
char *strdup(const char *str)
{
size_t len = strlen(str) + 1;
void *new = malloc(len);
if (new == NULL) return NULL;
return (char *) bcopy(str, new, len);
}
#endif
/*******************************************************************************/
/* Small common functions */
/*******************************************************************************/
int streq(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; }
INT myfloor(INT a, INT b) { return a - a % b; }
int setLowBits(int p, int val) { return (p & 0xF0) + val; }
int setHighBits(int p, int val) { return (p & 0x0F) + val * 0x10; }
int strbeginswith(const char *a, const char *prefix)
{
return strncmp(a, prefix, strlen(prefix)) == 0;
}
char *strconcat3(char *a, char *b, char *c)
{
size_t la = a ? strlen(a) : 0;
size_t lb = b ? strlen(b) : 0;
size_t lc = c ? strlen(c) : 0;
char *p = malloc(la + lb + lc + 1);
if (a) memcpy(p, a, la);
if (b) memcpy(p + la, b, lb);
if (c) memcpy(p + la + lb, c, lc);
p[la + lb + lc] = '\0';
return p;
}
int hexCharToInt(int c)
{
if (isdigit(c)) return c - '0';
return tolower(c) - 'a' + 10;
}
int not(int b) { return b ? FALSE: TRUE; }
#ifndef HAVE_MEMRCHR
void *memrchr(const void *s, int c, size_t n)
{
int i;
const char *cs = s;
for (i = n - 1; i >= 0; i--) if (cs[i] == c) return (void *) &cs[i];
return NULL;
}
#endif
char *mymemmem(char *a, int sizea, char *b, int sizeb)
{
#ifdef HAVE_MEMMEM
return memmem(a, sizea, b, sizeb);
#else
char *p;
int i = sizea - sizeb + 1;
if (i <= 0) return NULL;
for (; (p = memchr(a, b[0], i)); i -= p - a + 1, a = p + 1)
{
if ((memcmp(p + 1, b + 1, sizeb - 1)) == 0) {
return p;
}
}
return NULL;
#endif
}
char *mymemrmem(char *a, int sizea, char *b, int sizeb)
{
#ifdef HAVE_MEMRMEM
return memrmem(a, sizea, b, sizeb);
#else
char *p;
int i = sizea - sizeb + 1;
if (i <= 0) return NULL;
a += sizea - 1;
for (; (p = memrchr(a - i + 1, b[sizeb - 1], i)); i -= a - p + 1, a = p - 1)
{
if ((memcmp(p - sizeb + 1, b, sizeb - 1)) == 0) return p;
}
return NULL;
#endif
}
int hexStringToBinString(char *p, int *l)
{
int i;
int j;
for (i = 0, j = 0; i < *l; i++, j++) {
if( isspace(p[i]) ) {
j--;
continue;
}
if (!isxdigit(p[i])) {
displayMessageAndWaitForKey("Invalid hexa string");
return FALSE;
}
p[j / 2] = ((j % 2) ? setLowBits : setHighBits)(p[j / 2], hexCharToInt(p[i]));
}
if ((j % 2)) {
displayMessageAndWaitForKey("Must be an even number of chars");
return FALSE;
}
*l = j / 2;
return TRUE;
}