remove line-end witespace noise
[rofl0r-hexedit0r.git] / misc.c
blob8a59dc36cbce8c7ec010661aeecbeebe134ff25d
1 /* hexedit -- Hexadecimal Editor for Binary Files
2 Copyright (C) 1998 Pixel (Pascal Rigaux)
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.
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.
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"
20 int LSEEK_(int fd, off_t where)
22 off_t result;
24 result = lseek(fd, where, SEEK_SET);
25 return (result == where) ? 1 : -1;
28 void LSEEK(int fd, off_t where)
30 off_t result;
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);
40 /*******************************************************************************/
41 /* Small common functions */
42 /*******************************************************************************/
43 int streq(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; }
44 off_t myfloor(off_t a, off_t b) { return a - a % b; }
45 int setLowBits(int p, int val) { return (p & 0xF0) + val; }
46 int setHighBits(int p, int val) { return (p & 0x0F) + val * 0x10; }
48 int strbeginswith(const char *a, const char *prefix)
50 return strncmp(a, prefix, strlen(prefix)) == 0;
53 char *strconcat3(char *a, char *b, char *c)
55 size_t la = a ? strlen(a) : 0;
56 size_t lb = b ? strlen(b) : 0;
57 size_t lc = c ? strlen(c) : 0;
58 char *p = malloc(la + lb + lc + 1);
59 if (a) memcpy(p, a, la);
60 if (b) memcpy(p + la, b, lb);
61 if (c) memcpy(p + la + lb, c, lc);
62 p[la + lb + lc] = '\0';
63 return p;
66 int hexCharToInt(int c)
68 if (isdigit(c)) return c - '0';
69 return tolower(c) - 'a' + 10;
72 int not(int b) { return b ? FALSE: TRUE; }
74 #ifndef HAVE_MEMRCHR
75 void *memrchr(const void *s, int c, size_t n)
77 ssize_t i;
78 const char *cs = s;
79 for (i = n - 1; i >= 0; i--) if (cs[i] == c) return (void *) &cs[i];
80 return NULL;
82 #endif
84 #ifndef HAVE_MEMMEM
85 char *mymemmem(char *a, size_t sizea, char *b, size_t sizeb)
87 char *p;
88 ssize_t i = sizea - sizeb + 1;
89 if (i < 0) return 0;
90 for (; (p = memchr(a, b[0], i)); i -= p - a + 1, a = p + 1)
92 if ((memcmp(p + 1, b + 1, sizeb - 1)) == 0) {
93 return p;
96 return NULL;
98 #endif
100 #ifndef HAVE_MEMRMEM
101 char *mymemrmem(char *a, size_t sizea, char *b, size_t sizeb)
103 char *p;
104 ssize_t i = sizea - sizeb + 1;
105 if (i < 0) return 0;
107 a += sizea - 1;
108 for (; (p = memrchr(a - i + 1, b[sizeb - 1], i)); i -= a - p + 1, a = p - 1)
110 if ((memcmp(p - sizeb + 1, b, sizeb - 1)) == 0) return p;
112 return NULL;
114 #endif
117 int hexStringToBinString(char *p, size_t *l)
119 size_t i;
121 for (i = 0; i < *l; i++) {
122 if (!isxdigit(p[i])) {
123 displayMessageAndWaitForKey("Invalid hexa string");
124 return FALSE;
126 p[i / 2] = ((i % 2) ? setLowBits : setHighBits)(p[i / 2], hexCharToInt(p[i]));
129 if ((*l % 2)) {
130 displayMessageAndWaitForKey("Must be an even number of chars");
131 return FALSE;
133 *l /= 2;
134 return TRUE;