-
Notifications
You must be signed in to change notification settings - Fork 4
/
editors.c
301 lines (287 loc) · 8.15 KB
/
editors.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
/**
* @namespace biew
* @file editors.c
* @brief This file contains low level editor implementation of BIEW project.
* @version -
* @remark this source file is part of Binary vIEW project (BIEW).
* The Binary vIEW (BIEW) is copyright (C) 1995 Nickols_K.
* All rights reserved. This software is redistributable under the
* licence given in the file "Licence.en" ("Licence.ru" in russian
* translation) distributed in the BIEW archive.
* @note Requires POSIX compatible development system
*
* @author Nickols_K
* @since 1995
* @note Development, fixes and improvements
**/
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include "colorset.h"
#include "bmfile.h"
#include "tstrings.h"
#include "plugins/disasm.h"
#include "bconsole.h"
#include "beyeutil.h"
#include "beyehelp.h"
#include "editor.h"
#include "libbeye/beyelib.h"
#include "libbeye/kbd_code.h"
#include "libbeye/pmalloc.h"
__fileoff_t edit_cp = 0;
struct tag_emem EditorMem;
int edit_x,edit_y;
unsigned char edit_XX = 0;
void ExtHelp( void )
{
TWindow * using = twUsedWin();
hlpDisplay(2);
twUseWin(using);
}
void __FASTCALL__ PaintETitle( int shift,tBool use_shift )
{
TWindow * using = twUsedWin();
unsigned eidx;
char byte,obyte;
twUseWin(TitleWnd);
twFreezeWin(TitleWnd);
twGotoXY(1,1);
twClrEOL();
twPrintF("%08lX: ",edit_cp + shift);
eidx = use_shift ? (unsigned)shift : edit_y*EditorMem.width+edit_x;
byte = EditorMem.buff[eidx];
obyte = EditorMem.save[eidx];
if(byte != obyte) twSetColorAttr(title_cset.change);
twPrintF("%c %02XH %sH %sB "
,byte ? byte : ' '
,byte & 0x00FF
,Get2SignDig(byte)
,GetBinary(byte));
twSetColorAttr(title_cset.main);
if(byte != obyte)
{
twPrintF("ORIGINAL: %c %02XH %sH %sB "
,obyte ? obyte : ' '
,obyte & 0x00FF
,Get2SignDig(obyte)
,GetBinary(obyte));
}
else
twPrintF(" ");
twPrintF("MASK: %sH"
,Get2Digit(edit_XX));
twRefreshWin(TitleWnd);
twUseWin(using);
}
tBool __FASTCALL__ editInitBuffs(unsigned width,unsigned char *buff,unsigned size)
{
__filesize_t flen,cfp,ssize;
unsigned i,msize;
msize = tvioWidth*tvioHeight;
EditorMem.buff = PMalloc(msize);
EditorMem.save = PMalloc(msize);
EditorMem.alen = PMalloc(tvioHeight);
if((!EditorMem.buff) || (!EditorMem.save) || (!EditorMem.alen))
{
if(EditorMem.buff) PFREE(EditorMem.buff);
if(EditorMem.save) PFREE(EditorMem.save);
if(EditorMem.alen) PFREE(EditorMem.alen);
MemOutBox("Editor initialization");
return False;
}
memset(EditorMem.buff,TWC_DEF_FILLER,msize);
memset(EditorMem.save,TWC_DEF_FILLER,msize);
flen = BMGetFLength();
edit_cp = cfp = BMGetCurrFilePos();
EditorMem.width = width;
if(buff)
{
EditorMem.size = size;
memcpy(EditorMem.buff,buff,size);
}
else
{
EditorMem.size = (unsigned)((__filesize_t)msize > (flen-cfp) ? (flen-cfp) : msize);
BMReadBufferEx(EditorMem.buff,EditorMem.size,cfp,BM_SEEK_SET);
BMSeek(cfp,BM_SEEK_SET);
}
memcpy(EditorMem.save,EditorMem.buff,EditorMem.size);
/** initialize EditorMem.alen */
ssize = flen-cfp;
for(i = 0;i < tvioHeight;i++)
{
EditorMem.alen[i] = ssize >= width ? width : ssize;
ssize -= min(ssize,width);
}
return True;
}
void __FASTCALL__ editDestroyBuffs( void )
{
PFREE(EditorMem.buff);
PFREE(EditorMem.save);
PFREE(EditorMem.alen);
}
void __FASTCALL__ CheckBounds( void )
{
tAbsCoord height = twGetClientHeight(MainWnd);
if(edit_y < 0) edit_y = 0;
if((unsigned)edit_y > height - 1) edit_y = height - 1;
if(!EditorMem.alen[edit_y]) edit_y--;
if(edit_x >= EditorMem.alen[edit_y]) edit_x = EditorMem.alen[edit_y] - 1;
}
void __FASTCALL__ CheckYBounds( void )
{
tAbsCoord height = twGetClientHeight(MainWnd);
if(edit_y < 0) edit_y = 0;
if((unsigned)edit_y > height - 1) edit_y = height - 1;
while(!EditorMem.alen[edit_y]) edit_y--;
}
void __FASTCALL__ CheckXYBounds( void )
{
CheckYBounds();
if(edit_x < 0) edit_x = EditorMem.alen[--edit_y]*2;
if(edit_x >= EditorMem.alen[edit_y]*2) { edit_x = 0; edit_y++; }
CheckYBounds();
}
void __FASTCALL__ editSaveContest( void )
{
BGLOBAL bHandle;
char *fname;
fname = BMName();
bHandle = biewOpenRW(fname,BBIO_SMALL_CACHE_SIZE);
if(bHandle == &bNull)
{
err:
errnoMessageBox(WRITE_FAIL,NULL,errno);
return;
}
bioSeek(bHandle,edit_cp,BIO_SEEK_SET);
if(!bioWriteBuffer(bHandle,(void *)EditorMem.buff,EditorMem.size)) goto err;
bioClose(bHandle);
BMReRead();
}
tBool __FASTCALL__ edit_defaction(int _lastbyte)
{
tBool redraw;
redraw = False;
switch(_lastbyte)
{
case KE_UPARROW : edit_y--; break;
case KE_DOWNARROW: edit_y++; break;
case KE_ENTER:
case KE_LEFTARROW:
case KE_RIGHTARROW: break;
case KE_F(3) :
Get2DigitDlg(INIT_MASK,INPUT_MASK,&edit_XX);
break;
default: redraw = True; break;
}
return redraw;
}
tBool __FASTCALL__ editDefAction(int _lastbyte)
{
tBool redraw = True;
int eidx;
eidx = edit_y*EditorMem.width+edit_x;
switch(_lastbyte)
{
case KE_F(4) : EditorMem.buff[eidx] = ~EditorMem.buff[eidx]; break;
case KE_F(5) : EditorMem.buff[eidx] |= edit_XX; break;
case KE_F(6) : EditorMem.buff[eidx] &= edit_XX; break;
case KE_F(7) : EditorMem.buff[eidx] ^= edit_XX; break;
case KE_F(8) : EditorMem.buff[eidx] = edit_XX; break;
case KE_F(9) : EditorMem.buff[eidx] = EditorMem.save[eidx]; break;
default : redraw = edit_defaction(_lastbyte); edit_x--; break;
}
edit_x++;
return redraw;
}
int __FASTCALL__ FullEdit(TWindow * txtwnd,void (*save_func)(unsigned char *,unsigned))
{
size_t i,j;
unsigned mlen;
unsigned int _lastbyte;
unsigned flags;
tAbsCoord height = twGetClientHeight(MainWnd);
tBool redraw;
char attr = __ESS_HARDEDIT | __ESS_WANTRETURN;
twSetColorAttr(browser_cset.edit.main);
__MsSetState(False);
for(i = 0;i < height;i++)
{
for(j = 0;j < EditorMem.alen[i];j++)
{
unsigned eidx;
eidx = i*EditorMem.width+j;
twSetColorAttr(EditorMem.buff[eidx] == EditorMem.save[eidx] ? browser_cset.edit.main : browser_cset.edit.change);
twDirectWrite(j + 1,i + 1,&EditorMem.buff[eidx],1);
}
if((unsigned)EditorMem.alen[i] + 1 < EditorMem.width)
{
twGotoXY(EditorMem.alen[i] + 1,i + 1); twClrEOL();
}
}
__MsSetState(True);
PaintETitle(edit_y*EditorMem.width + edit_x,0);
twShowWin(twUsedWin());
twSetCursorType(TW_CUR_NORM);
redraw = True;
if(txtwnd)
{
char work[__TVIO_MAXSCREENWIDTH];
int len;
TWindow * using = twUsedWin();
twUseWin(txtwnd);
twSetColorAttr(browser_cset.main);
twFreezeWin(txtwnd);
for(i = 0;i < height;i++)
{
mlen = EditorMem.alen[i];
len = ExpandHex(work,&EditorMem.buff[i*EditorMem.width],mlen,2);
twDirectWrite(11,i + 1,work,len);
if((unsigned)EditorMem.alen[i] + 1 < EditorMem.width)
{
twGotoXY(11+len,i + 1); twClrEOL();
}
}
twRefreshWin(txtwnd);
twUseWin(using);
}
while(1)
{
unsigned eidx;
eidx = edit_y*EditorMem.width;
mlen = EditorMem.alen[edit_y];
flags = attr;
if(!redraw) flags |= __ESS_NOREDRAW;
_lastbyte = eeditstring((char *)&EditorMem.buff[eidx],NULL,&mlen,(unsigned)(edit_y + 1),
(unsigned *)&edit_x,flags,(char *)&EditorMem.save[eidx], NULL);
switch(_lastbyte)
{
case KE_F(1) : ExtHelp(); continue;
case KE_F(2) : save_func?save_func(EditorMem.buff,EditorMem.size):editSaveContest();
case KE_F(10) :
case KE_ESCAPE : goto bye;
case KE_TAB : if(txtwnd) goto bye;
default : redraw = editDefAction(_lastbyte); break;
}
CheckBounds();
if(redraw)
{
if(txtwnd)
{
char work[__TVIO_MAXSCREENWIDTH];
int len;
TWindow * using = twUsedWin();
twUseWin(txtwnd);
len = ExpandHex(work,&EditorMem.buff[edit_y*EditorMem.width],mlen,2);
twDirectWrite(11,edit_y + 1,work,len);
twUseWin(using);
}
}
PaintETitle(edit_y*EditorMem.width + edit_x,0);
}
bye:
twSetCursorType(TW_CUR_OFF);
return _lastbyte;
}