1 /* hexedit -- Hexadecimal Editor for Binary Files
2    Copyright (C) 1998 Pixel (Pascal Rigaux)
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/
17 #include "hexedit.h"
18 
19 
LSEEK_(int fd,INT where)20 int LSEEK_(int fd, INT where)
21 {
22   INT result;
23 
24   result = lseek(fd, where, SEEK_SET);
25   return (result == where) ? 1 : -1;
26 }
27 
LSEEK(int fd,INT where)28 void LSEEK(int fd, INT where)
29 {
30   INT result;
31 
32   result = lseek(fd, where, SEEK_SET);
33   if (result != where) {
34     exitCurses();
35     fprintf(stderr, "the long seek failed (%lld instead of %lld), leaving :(\n", (long long) result, (long long) where);
36     exit(1);
37   }
38 }
39 
40 /*******************************************************************************/
41 /* Functions provided for OSs that don't have them */
42 /*******************************************************************************/
43 #ifndef HAVE_BASENAME
basename(char * file)44 char *basename(char *file) {
45   char *p = strrchr(file, '/');
46   return p ? p + 1 : file;
47 }
48 #endif
49 
50 #ifndef HAVE_STRERROR
strerror(int errnum)51 char *strerror(int errnum) {
52   extern char *sys_errlist[];
53   extern int sys_nerr;
54 
55   if (errnum > 0 && errnum <= sys_nerr)
56     return sys_errlist[errnum];
57   return _("Unknown system error");
58 }
59 #endif
60 
61 #ifndef HAVE_STRDUP
strdup(const char * str)62 char *strdup(const char *str)
63 {
64   size_t len = strlen(str) + 1;
65   void *new = malloc(len);
66   if (new == NULL) return NULL;
67 
68   return (char *) bcopy(str, new, len);
69 }
70 #endif
71 
72 /*******************************************************************************/
73 /* Small common functions */
74 /*******************************************************************************/
streq(const char * s1,const char * s2)75 int streq(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; }
myfloor(INT a,INT b)76 INT myfloor(INT a, INT b) { return a - a % b; }
setLowBits(int p,int val)77 int setLowBits(int p, int val) { return (p & 0xF0) + val; }
setHighBits(int p,int val)78 int setHighBits(int p, int val) { return (p & 0x0F) + val * 0x10; }
79 
strbeginswith(const char * a,const char * prefix)80 int strbeginswith(const char *a, const char *prefix)
81 {
82   return strncmp(a, prefix, strlen(prefix)) == 0;
83 }
84 
strconcat3(char * a,char * b,char * c)85 char *strconcat3(char *a, char *b, char *c)
86 {
87   size_t la = a ? strlen(a) : 0;
88   size_t lb = b ? strlen(b) : 0;
89   size_t lc = c ? strlen(c) : 0;
90   char *p = malloc(la + lb + lc + 1);
91   if (a) memcpy(p, a, la);
92   if (b) memcpy(p + la, b, lb);
93   if (c) memcpy(p + la + lb, c, lc);
94   p[la + lb + lc] = '\0';
95   return p;
96 }
97 
hexCharToInt(int c)98 int hexCharToInt(int c)
99 {
100   if (isdigit(c)) return c - '0';
101   return tolower(c) - 'a' + 10;
102 }
103 
not(int b)104 int not(int b) { return b ? FALSE: TRUE; }
105 
106 #ifndef HAVE_MEMRCHR
memrchr(const void * s,int c,size_t n)107 void *memrchr(const void *s, int c, size_t n)
108 {
109   int i;
110   const char *cs = s;
111   for (i = n - 1; i >= 0; i--) if (cs[i] == c) return (void *) &cs[i];
112   return NULL;
113 }
114 #endif
115 
mymemmem(char * a,int sizea,char * b,int sizeb)116 char *mymemmem(char *a, int sizea, char *b, int sizeb)
117 {
118 #ifdef HAVE_MEMMEM
119   return memmem(a, sizea, b, sizeb);
120 #else
121   char *p;
122   int i = sizea - sizeb + 1;
123 
124   if (i <= 0) return NULL;
125 
126   for (; (p = memchr(a, b[0], i)); i -= p - a + 1, a = p + 1)
127   {
128     if ((memcmp(p + 1, b + 1, sizeb - 1)) == 0) {
129 	 return p;
130     }
131   }
132   return NULL;
133 #endif
134 }
135 
mymemrmem(char * a,int sizea,char * b,int sizeb)136 char *mymemrmem(char *a, int sizea, char *b, int sizeb)
137 {
138 #ifdef HAVE_MEMRMEM
139   return memrmem(a, sizea, b, sizeb);
140 #else
141   char *p;
142   int i = sizea - sizeb + 1;
143 
144   if (i <= 0) return NULL;
145 
146   a += sizea - 1;
147   for (; (p = memrchr(a - i + 1, b[sizeb - 1], i)); i -= a - p + 1, a = p - 1)
148   {
149     if ((memcmp(p - sizeb + 1, b, sizeb - 1)) == 0) return p;
150   }
151   return NULL;
152 #endif
153 }
154 
155 
hexStringToBinString(char * p,int * l)156 int hexStringToBinString(char *p, int *l)
157 {
158   int i;
159   int j;
160 
161   for (i = 0, j = 0; i < *l; i++, j++) {
162     if( isspace(p[i]) ) {
163       j--;
164       continue;
165     }
166     if (!isxdigit(p[i])) {
167       displayMessageAndWaitForKey("Invalid hexa string");
168       return FALSE;
169     }
170     p[j / 2] = ((j % 2) ? setLowBits : setHighBits)(p[j / 2], hexCharToInt(p[i]));
171   }
172 
173   if ((j % 2)) {
174     displayMessageAndWaitForKey("Must be an even number of chars");
175     return FALSE;
176   }
177   *l = j / 2;
178   return TRUE;
179 }
180 
181