-
Notifications
You must be signed in to change notification settings - Fork 26
/
dmhex.c
318 lines (285 loc) · 8.24 KB
/
dmhex.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
/* dmhex.c */
/* Copyright 1995 by Steve Kirkendall */
#include "elvis.h"
#ifdef FEATURE_RCSID
char id_dmhex[] = "$Id: dmhex.c,v 2.21 2003/10/17 17:41:23 steve Exp $";
#endif
#ifdef DISPLAY_HEX
#ifdef CHAR16
"hex mode doesn't support 16-bit characters"
#endif
#if USE_PROTOTYPES
static DMINFO *init(WINDOW win);
static void term(DMINFO *info);
static long mark2col(WINDOW w, MARK mark, ELVBOOL cmd);
static MARK move(WINDOW w, MARK from, long linedelta, long column, ELVBOOL cmd);
static MARK setup(WINDOW win, MARK top, long cursor, MARK bottom, DMINFO *info);
static MARK image(WINDOW w, MARK line, DMINFO *info, void (*draw)(CHAR *p, long qty, _char_ font, long offset));
#endif
/* Lines look like this:
_offset____0__1__2__3___4__5__6__7___8__9__a__b___c__d__e__f__0123456789abcdef
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000000000000000
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000000000000000
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000000000000000
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000000000000000
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000000000000000
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0000000000000000
*/
/* This array is used to convert a column number to an offset within a line */
static char col2offset[] = "00000000000011122233334445556667777888999aaabbbbcccdddeeeffff00123456789abcdef";
/* These are the font codes for the column headings and the hex cursor */
static int font_hexheading;
static int font_hexcursor;
static int font_hexoffset;
/* start the mode, and allocate dminfo */
static DMINFO *init(win)
WINDOW win;
{
/* inherit some functions from dmnormal */
if (!dmhex.wordmove)
{
dmhex.wordmove = dmnormal.wordmove;
dmhex.tagatcursor = dmnormal.tagatcursor;
dmhex.tagload = dmnormal.tagload;
dmhex.tagnext = dmnormal.tagnext;
}
/* locate the fonts we'll use */
font_hexheading = colorfind(toCHAR("hexheading"));
colorset(font_hexheading, toCHAR("underlined"), ElvFalse);
font_hexcursor = colorfind(toCHAR("hexcursor"));
font_hexoffset = colorfind(toCHAR("hexoffset"));
return NULL;
}
/* end the mode, and free the modeinfo */
static void term(info)
DMINFO *info; /* window-specific info about mode */
{
}
/* Convert a mark to a screen-relative column number */
static long mark2col(w, mark, cmd)
WINDOW w; /* window where buffer is shown */
MARK mark; /* mark to convert */
ELVBOOL cmd; /* if ElvTrue, we're in command mode; else input mode */
{
return 62 + (markoffset(mark) & 0xf);
}
/* Move vertically, and to a given column (or as close to column as possible) */
static MARK move(w, from, linedelta, column, cmd)
WINDOW w; /* window where buffer is shown */
MARK from; /* old location */
long linedelta; /* line movement */
long column; /* desired column number */
ELVBOOL cmd; /* if ElvTrue, we're in command mode; else input mode */
{
static MARKBUF mark;
long offset;
long coloff;
/* compute line movement first */
offset = (markoffset(from) & ~0xf) + 0x10 * linedelta;
/* convert requested column to nearest buffer offset */
if (column >= (int)strlen(col2offset))
{
coloff = 0xf;
}
else if (col2offset[column] >= '0' && col2offset[column] <= '9')
{
coloff = col2offset[column] - '0';
}
else
{
coloff = col2offset[column] - 'a' + 0xa;
}
offset += coloff;
/* never return an offset past the end of the buffer, or before the
* beginning.
*/
if (offset >= o_bufchars(markbuffer(from)))
{
if (o_bufchars(markbuffer(from)) == 0)
{
offset = 0;
}
else
{
offset = o_bufchars(markbuffer(from)) - 1;
}
}
else if (offset < 0)
{
offset = coloff;
}
return marktmp(mark, markbuffer(from), offset);
}
/* Choose a line to appear at the top of the screen, and return its mark.
* Also, initialize the info for the next line.
*/
static MARK setup(win, top, cursor, bottom, info)
WINDOW win; /* window to be updated */
MARK top; /* where previous image started */
long cursor; /* offset of cursor */
MARK bottom; /* where previous image ended */
DMINFO *info; /* window-specific info about mode */
{
static MARKBUF mark;
long topoff, bottomoff;
/* extract the offsets. Round down to multiple of 16 */
topoff = (top ? (markoffset(top) & ~0xf) : -1);
bottomoff = (bottom ? (markoffset(bottom) & ~0xf) : -1);
cursor &= ~0xf;
/* if cursor is on the screen, or very near the bottom, then
* keep the current top
*/
if (cursor >= topoff && cursor <= bottomoff + 16)
{
return marktmp(mark, markbuffer(top), topoff);
}
/* if the cursor is on the line before the top, then make the cursor's
* line become the new top line.
*/
if (cursor == topoff - 16)
{
return marktmp(mark, markbuffer(top), cursor);
}
/* else it is distantly before or after the the old screen. Center
* the cursor's line in the screen.
*/
topoff = (cursor - (bottomoff - topoff) / 2) & ~0xf;
if (topoff < 0)
{
topoff = 0;
}
return marktmp(mark, markbuffer(top), topoff);
}
static MARK image(w, line, info, draw)
WINDOW w; /* window where drawing will go */
MARK line; /* start of line to draw */
DMINFO *info; /* window-specific info about mode */
void (*draw)P_((CHAR *p, long qty, _char_ font, long offset));
/* function for drawing a single character */
{
char *c8p;
CHAR *cp;
CHAR tmp[1];
CHAR space[1]; /* usually a space character, maybe bracket character */
char buf[10];
int i, j;
/* output headings, if necessary */
if ((markoffset(line) & 0xf0) == 0)
{
c8p = " offset 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef";
while ((tmp[0] = *c8p++) != '\0')
(*draw)(tmp, 1, font_hexheading, -1L);
tmp[0] = '\n';
(*draw)(tmp, 1L, 0, -1L);
}
/* output the line offset */
sprintf(buf, "%08lx", markoffset(line));
for (i = 0; buf[i]; i++)
{
tmp[0] = buf[i];
(*draw)(tmp, 1, font_hexoffset, -1);
}
/* output the hex codes of the line */
j = markoffset(w->cursor) - markoffset(line);
space[0] = ' ';
for ((void)scanalloc(&cp, line), i = 0; i < 16 && cp; scannext(&cp), i++)
{
/* special case: if the last newline was added by elvis
* (not in the file) then hide it unless the cursor is on it.
*/
if (o_partiallastline(markbuffer(line))
&& (o_readeol(markbuffer(line)) == 'b' || o_writeeol == 'b')
&& *cp == '\n'
&& markoffset(line) + i == o_bufchars(markbuffer(line)) - 1
&& j != i)
{
break;
}
if ((i & 0x03) == 0)
{
(*draw)(space, 1, 0, -1);
space[0] = ' ';
}
if (j == i)
{
tmp[0] = '<';
(*draw)(tmp, 1, 0, -1);
space[0] = '>';
}
else
{
(*draw)(space, 1, 0, -1);
space[0] = ' ';
}
sprintf(buf, "%02x", *cp);
tmp[0] = buf[0];
(*draw)(tmp, 1, (char)(j==i ? font_hexcursor : 0), markoffset(line) + i);
tmp[0] = buf[1];
(*draw)(tmp, 1, (char)(j==i ? font_hexcursor : 0), markoffset(line) + i);
}
(*draw)(space, 1, 0, -1);
/* pad with blanks, if necessary */
space[0] = ' ';
while (i < 16)
{
(*draw)(space, ((i & 0x03) == 0) ? -4 : -3, 0, -1);
i++;
}
(*draw)(space, 1, 0, -1);
/* output the characters */
tmp[0] = '.';
for ((void)scanseek(&cp, line), i = 0; i < 16 && cp; scannext(&cp), i++)
{
/* special case: if the last newline was added by elvis
* (not in the file) then hide it unless the cursor is on it.
*/
if (o_partiallastline(markbuffer(line))
&& (o_readeol(markbuffer(line)) == 'b' || o_writeeol == 'b')
&& *cp == '\n'
&& markoffset(line) + i == o_bufchars(markbuffer(line)) - 1
&& j != i)
{
(*draw)(blanks, 1, 0, markoffset(line) + i);
}
else if (*cp < ' ' || *cp == '\177')
{
(*draw)(tmp, 1, 0, markoffset(line) + i);
}
else
{
(*draw)(cp, 1, 0, markoffset(line) + i);
}
}
scanfree(&cp);
tmp[0] = '\n';
(*draw)(tmp, 1L, 0, -1L);
/* output a blank line after every 16th data line */
if ((markoffset(line) & 0xf0) == 0xf0)
{
(*draw)(tmp, 1L, 0, -1L);
}
markoffset(line) += i;
return line;
}
DISPMODE dmhex =
{
"hex",
"Binary hex dump",
ElvFalse, /* display generating can't be optimized */
ElvFalse, /* shouldn't do normal wordwrap */
0, /* no window options */
NULL,
0, /* no global options */
NULL,
NULL,
init,
term,
mark2col,
move,
NULL, /* wordmove will be set to dmnormal.wordmove in init() */
setup,
image,
NULL, /* doesn't need a header */
NULL /* no autoindent */
};
#endif /* DISPLAY_HEX */