-
Notifications
You must be signed in to change notification settings - Fork 2
/
display.c
276 lines (233 loc) · 7.04 KB
/
display.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
/* 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 have_colors;
int move_cursor(off_t delta)
{
return set_cursor(base + cursor + delta);
}
int set_cursor(off_t loc)
{
if (loc < 0 && base % lineLength)
loc = 0;
if (!tryloc(loc))
return FALSE;
if (loc < base) {
if (loc - base % lineLength < 0)
set_base(0);
else if (!move_base(myfloor(loc - base % lineLength, lineLength) + base % lineLength - base))
return FALSE;
cursor = loc - base;
} else if (loc >= base + page) {
if (!move_base(myfloor(loc - base % lineLength, lineLength) + base % lineLength - page + lineLength - base))
return FALSE;
cursor = loc - base;
} else if (loc > base + nbBytes) {
return FALSE;
} else
cursor = loc - base;
if (mark_set)
updateMarked();
return TRUE;
}
int move_base(off_t delta)
{
if (mode == bySector) {
if (delta > 0 && delta < page)
delta = page;
else if (delta < 0 && delta > -page)
delta = -page;
}
return set_base(base + delta);
}
int set_base(off_t loc)
{
if (loc < 0) loc = 0;
if (!tryloc(loc)) return FALSE;
base = loc;
readFile();
if (mode != bySector && nbBytes < page - lineLength && base != 0) {
base -= myfloor(page - nbBytes - lineLength, lineLength);
if (base < 0) base = 0;
readFile();
}
if (cursor > nbBytes) cursor = nbBytes;
return TRUE;
}
int computeLineSize(void) { return computeCursorXPos(lineLength - 1, 0) + 1; }
int computeCursorXCurrentPos(void) { return computeCursorXPos(cursor, hexOrAscii); }
int computeCursorXPos(int cursor, int hexOrAscii)
{
int r = 11;
int x = cursor % lineLength;
int h = (hexOrAscii ? x : lineLength - 1);
r += normalSpaces * (h % blocSize) + (h / blocSize) * (normalSpaces * blocSize + 1) + (hexOrAscii && cursorOffset);
if (!hexOrAscii) r += x + normalSpaces + 1;
return r;
}
/*******************************************************************************/
/* Curses functions */
/*******************************************************************************/
void initCurses(void)
{
initscr();
have_colors = has_colors();
if (have_colors && colored) {
start_color();
use_default_colors();
init_pair(1, COLOR_RED, -1); /* null zeros */
init_pair(2, COLOR_GREEN, -1); /* control chars */
init_pair(3, COLOR_BLUE, -1); /* extended chars */
}
refresh();
raw();
noecho();
keypad(stdscr, TRUE);
if (mode == bySector) {
lineLength = modes[bySector].lineLength;
page = modes[bySector].page;
page = myfloor((LINES - 1) * lineLength, page);
blocSize = modes[bySector].blocSize;
if (computeLineSize() > COLS) DIE("%s: term is too small for sectored view (width)\n");
if (page == 0) DIE("%s: term is too small for sectored view (height)\n");
} else { /* mode == maximized */
if (LINES <= 4) DIE("%s: term is too small (height)\n");
blocSize = modes[maximized].blocSize;
for (lineLength = blocSize; computeLineSize() <= COLS; lineLength += blocSize);
lineLength -= blocSize;
if (lineLength == 0) DIE("%s: term is too small (width)\n");
page = lineLength * (LINES - 1);
}
colsUsed = computeLineSize();
buffer = malloc(page);
bufferAttr = malloc(page * sizeof(*bufferAttr));
}
void exitCurses(void)
{
close(fd);
clear();
refresh();
endwin();
}
void display(void)
{
int i;
for (i = 0; i < nbBytes; i += lineLength) {
move(i / lineLength, 0);
displayLine(i, nbBytes);
}
for (; i < page; i += lineLength) {
int j;
move(i / lineLength, 0);
for (j = 0; j < colsUsed; j++) printw(" "); /* cleanup the line */
move(i / lineLength, 0);
PRINTW(("%08lX", (int) (base + i)));
}
attrset(NORMAL);
move(LINES - 1, 0);
for (i = 0; i < colsUsed; i++) printw("-");
move(LINES - 1, 0);
if (isReadOnly) i = '%';
else if (edited) i = '*';
else i = '-';
printw("-%c%c %s --0x%llX", i, i, baseName, (long long) base + cursor);
if (MAX(fileSize, lastEditedLoc)) printw("/0x%llX", (long long) getfilesize());
if (mode == bySector) printw("--sector %lld", (long long) (base + cursor) / SECTOR_SIZE);
move(cursor / lineLength, computeCursorXCurrentPos());
}
void displayLine(int offset, int max)
{
int i;
PRINTW(("%08lX ", (int) (base + offset)));
for (i = offset; i < offset + lineLength; i++) {
if (i > offset) MAXATTRPRINTW(bufferAttr[i] & MARKED, (((i - offset) % blocSize) ? " " : " "));
if (i < max) {
ATTRPRINTW(
(!(have_colors && colored) ? 0 :
buffer[i] == 0 ? COLOR_PAIR(1) :
buffer[i] < ' ' ? COLOR_PAIR(2) :
buffer[i] >= 127 ? COLOR_PAIR(3) : 0) |
bufferAttr[i], ("%02X", buffer[i]));
}
else PRINTW((" "));
}
PRINTW((" "));
for (i = offset; i < offset + lineLength; i++) {
if (i >= max) PRINTW((" "));
else if (buffer[i] >= ' ' && buffer[i] < 127) ATTRPRINTW(bufferAttr[i], ("%c", buffer[i]));
else ATTRPRINTW(bufferAttr[i], ("."));
}
}
void clr_line(int line) { move(line, 0); clrtoeol(); }
void displayCentered(char *msg, int line)
{
clr_line(line);
move(line, (COLS - strlen(msg)) / 2);
PRINTW(("%s", msg));
}
void displayOneLineMessage(char *msg)
{
int center = page / lineLength / 2;
clr_line(center - 1);
clr_line(center + 1);
displayCentered(msg, center);
}
void displayTwoLineMessage(char *msg1, char *msg2)
{
int center = page / lineLength / 2;
clr_line(center - 2);
clr_line(center + 1);
displayCentered(msg1, center - 1);
displayCentered(msg2, center);
}
void displayMessageAndWaitForKey(char *msg)
{
displayTwoLineMessage(msg, pressAnyKey);
getch();
}
int displayMessageAndGetString(char *msg, char **last, char *p, int p_size)
{
int ret = TRUE;
displayOneLineMessage(msg);
ungetstr(*last);
echo();
getnstr(p, p_size - 1);
noecho();
if (*p == '\0') {
if (*last) strcpy(p, *last); else ret = FALSE;
} else {
FREE(*last);
*last = strdup(p);
}
return ret;
}
void ungetstr(char *s)
{
char *p;
if (s)
for (p = s + strlen(s) - 1; p >= s; p--) ungetch(*p);
}
int get_number(off_t *i)
{
char tmp[BLOCK_SEARCH_SIZE];
echo();
getnstr(tmp, BLOCK_SEARCH_SIZE - 1);
noecho();
int base = ((tmp[0] == '0' && tmp[1] == 'x') ? 16 : 10);
char *endptr;
unsigned long long val = strtoull(tmp, &endptr, base);
*i = val;
return (base == 16 && (size_t) endptr > ((size_t) tmp) + 2) ||
(base == 10 && endptr != tmp);
}