-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhexedit.c
1526 lines (1252 loc) · 36.3 KB
/
hexedit.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************
*** Project Title: Command Post (KFLASH) ***
*** Author: Greg Dietsche ***
*** Date: 07/23/2002 ***
*** Description: An application designed for ***
*** use with the TI-89 and TI-92 Plus Graphing ***
*** calculators during the testing and ***
*** debugging phases of FLASH APPS and RAM ***
*** programs ***
***************************************************/
// $Id: hexedit.c 4 2004-08-05 19:06:59Z dietsche $
#include <tiams.h>
#include "KFLASHr1.h"
#include "KFLASH.h"
#include "undoc.h"
#include "eggs.h"
#include "dasm.h"
#define FIND_STRING 0xFF
typedef enum {SCROLL_UP=-1, SCROLL_DOWN=1 } SCROLL_MODE;
//most internal functions have a window parameter passed to them... this is to save space, and leave room for future implementations which may force the hex editor to use a different window
static void WinPaint(WINDOW *w);
static void Cursor(WINDOW *w);
static BOOL CheckCursorBounds(WINDOW *w);
static void RemoveNull(register char *str, short start, short end);
static void HandlePasteStr(WINDOW *w, HANDLE h);
static void JumpTo(WINDOW *w, char *address);
static char *HexBuff(short width, short offset, unsigned short *h);
static void ScrollVertical(WINDOW *w, SCROLL_MODE dir);
static void RefreshCurLine(WINDOW *w);
static void DoJumpToDialog(WINDOW *w);
static void DoHexEditDialog(WINDOW *w);
static void DoFindDialog(WINDOW *w);
static void *Search(register ULONG value, short width, register char *start, register short dir, register char * stop_addr);
static void FindDir(WINDOW *w, register short dir);
static void SetEditorWidth(WINDOW *w);
static void DoResizeHandleDialog(void);
static void showJumpToVar(WINDOW *w);
static void showJumpToCommonMem(WINDOW *w);
static void *Trap9(unsigned short index);
static CU_STATE cu; //internal cursor flash status
static short ShowWantToDerefPtrQuestion(void) { return DlgMessage(XR_stringPtr(XR_ShortAppName),XR_stringPtr(XR_WantDerefPointerQ),PDB_YES,PDB_NO)==KB_ENTER; }
void HexEditorEventLoop(pFrame self, PEvent e)
{
Access_AMS_Global_Variables;
HEX_DAT *g=&global.hex_dat;
HEX_EDIT_DAT *hed=&global.hex_edit_dat;
WINDOW *w=&appW;
switch(e->command)
{
case CM_ACTIVATE:
WinFont(w, F_6x8);
SetEditorWidth(w);
g->height=(WinHeight(w)>>3)-1;
WinAttr(w, A_REPLACE);
WinPaint(w);
CU_start();
cu=CU_CURSOR_ON;
Cursor(w);
break;
case CM_DEACTIVATE:
if(cu) {
Cursor(w);
cu=CU_CURSOR_OFF;
}
CU_stop();
break;
case CM_CURSOR_FLASH:
Cursor(w);
cu=!cu;
break;
case CM_CUT: //handle a cut the same as a copy
case CM_COPY:
{
WORD result;
void *ptr = &g->top[g->addr];
char buff[50];
if((ULONG)ptr%2)
result=MenuPopup(&pupCopySpecialOdd, -1, -1, 0);
else
result=MenuPopup(&pupCopySpecialEven, -1, -1, 0);
switch(result)
{
case COPY_CHAR:
sprintf(buff, "%u", *(UCHAR *)ptr);
CB_replaceTEXT(buff, strlen(buff), FALSE);
hed->optList[0]=EDIT_CHAR;
break;
case COPY_SHORT:
sprintf(buff, "%u", *(USHORT *)ptr);
CB_replaceTEXT(buff, strlen(buff), FALSE);
hed->optList[0]=EDIT_SHORT;
break;
case COPY_LONG:
sprintf(buff, "%lu", *(ULONG *)ptr);
CB_replaceTEXT(buff, strlen(buff), FALSE);
hed->optList[0]=EDIT_LONG;
break;
case COPY_STRING:
CB_replaceTEXT(&g->top[g->addr], strlen(&g->top[g->addr]), FALSE);
hed->optList[0]=EDIT_STRING;
break;
};
}
break;
case CM_PASTE: //paste special
{
HANDLE hClipboard;
size_t size;
char *clip;
if(CB_fetchTEXT(&hClipboard, &size))
{
hed->dlgtitle=XR_stringPtr(XR_PasteSpecial);
//if(HeapRealloc(hClipboard, ++size)==H_NULL) ER_throw(ER_MEMORY);
hed->dlginitstr=clip=HeapDeref(hClipboard);
clip[size]=0;
if(cu) Cursor(w);
if(size>HEDIT_BUFF_SIZE)DlgNotice(XR_stringPtr(XR_LongAppName), XR_stringPtr(XR_PasteTruncated));
DoHexEditDialog(w);
cu=CU_CURSOR_ON; Cursor(w);
}
}
break;
case CM_PASTE_HANDLE: //used to jump to a particular file selected with the var-link
HandlePasteStr(w, e->info.pasteHandleInfo.pasteHandle);
break;
case ACM_Defaults:
global.hex_dat.byteswide=0; //because SetHexEditorDefaults won't function correctly without it
//... fall through ...
case CM_INSTALL:
SetHexEditorDefaults(w);
break;
case ACM_New_Formats: //the user changed something in the FORMATS dialog
SetEditorWidth(w);
w->Flags|=WF_DIRTY;
CheckCursorBounds(w);
break;
case ACM_Hex_Refresh: //this code is designed to remove any graphical errors
case CM_WPAINT:
w->Flags|=WF_VIRTUAL; //don't show the cleared screen (WinPaint will unset this flag)
WinClr(w); //Clear The ENTIRE window
CheckCursorBounds(w); //force the cursor to be within bounds
WinPaint(w); //redraw the WHOLE window
if(cu) Cursor(w); //if the cursor was showing, then redraw it
break;
case ACM_Hex_JumpTo: //show jump dialog
DoJumpToDialog(w);
HexSTShowMemStr();
break;
case ACM_Hex_Edit: //show edit dialog
if(cu) Cursor(w);
hed->dlgtitle=XR_stringPtr(XR_Edit);
hed->dlginitstr=NULL;
DoHexEditDialog(w);
if(cu)Cursor(w);
break;
case ACM_Hex_JumpVar:
handleVarLinkKey(SDT_ALL);
HexSTShowMemStr();
break;
case ACM_Hex_Find:
DoFindDialog(w);
HexSTShowMemStr();
break;
case ACM_Hex_FindNext:
FindDir(w, DIR_FOREWARD);
HexSTShowMemStr();
break;
case ACM_Hex_FindPrevious:
FindDir(w, DIR_BACKWARD);
HexSTShowMemStr();
break;
case ACM_Hex_ResizeHandle:
DoResizeHandleDialog();
break;
case ACM_Hex_AMS_GlobalVars:
showJumpToVar(w);
HexSTShowMemStr();
break;
case ACM_Hex_JumpCommon:
showJumpToCommonMem(w);
HexSTShowMemStr();
break;
case ACM_Hex_SetBookMark1:
g->kb_left_most=g->addr+g->top;
break;
case ACM_Hex_SetBookMark2:
g->kb_right_most=g->addr+g->top;
break;
case ACM_Hex_JumpBookMark1:
e->command=CM_KEY_PRESS;
e->info.keyInfo.keyCode=KB_TOP;
HexEditorEventLoop(self,e);
break;
case ACM_Hex_JumpBookMark2:
e->command=CM_KEY_PRESS;
e->info.keyInfo.keyCode=KB_BOTTOM;
HexEditorEventLoop(self,e);
break;
case CM_KEY_PRESS:
if(cu)//force the cursor to be off
Cursor(w);
switch(e->info.keyInfo.keyCode)
{
case KB_ENTER: //show hex edit dialog
hed->dlgtitle=XR_stringPtr(XR_Edit);
hed->dlginitstr=NULL;
DoHexEditDialog(w);
break;
case KB_ENTER + KB_OPTION: //show FIND dialog
DoFindDialog(w);
HexSTShowMemStr();
break;
case KB_CLEAR: //find next
FindDir(w, DIR_FOREWARD);
HexSTShowMemStr();
break;
case KB_CLEAR + KB_OPTION: //find previous
FindDir(w, DIR_BACKWARD);
HexSTShowMemStr();
break;
case KB_PGUP: //page up
g->top-=(g->height*g->width);
CheckCursorBounds(w);
WinPaint(w);
break;
case KB_PGDN: //page down
g->top+=(g->height*g->width);
CheckCursorBounds(w);
WinPaint(w);
break;
case KB_UP: //up one line
g->addr-=g->width;
if(CheckCursorBounds(w))
ScrollVertical(w, SCROLL_UP);
HexSTShowMemStr();
break;
case KB_DOWN: //down one line
g->addr+=g->width;
if(CheckCursorBounds(w))
ScrollVertical(w, SCROLL_DOWN);
HexSTShowMemStr();
break;
case KB_LEFT: //move left
case KB_DELETE:
if(g->nibble)
g->nibble=0;
else {
g->nibble=1;
g->addr--;
if(CheckCursorBounds(w))
ScrollVertical(w, SCROLL_UP);
}
HexSTShowMemStr();
break;
case KB_RIGHT: //move right
if(g->nibble) {
g->nibble=0;
g->addr++;
if(CheckCursorBounds(w))
ScrollVertical(w, SCROLL_DOWN);
}
else
g->nibble=1;
HexSTShowMemStr();
break;
case KB_BEGIN: //move to start of current line
g->addr-=(g->addr%g->width);
g->nibble=0;
if(CheckCursorBounds(w))
WinPaint(w);
else
HexSTShowMemStr();
break;
case KB_END: //move to end of current line
g->addr=g->addr-(g->addr%g->width)+g->width-1;
g->nibble=1;
if(CheckCursorBounds(w))
WinPaint(w);
else
HexSTShowMemStr();
break;
case KB_LEFT_MOST: //jump 2 bytes backwards
g->addr-=2;
if(CheckCursorBounds(w))
ScrollVertical(w, SCROLL_UP);
HexSTShowMemStr();
break;
case KB_RIGHT_MOST: //jump 2 bytes foreword
g->addr+=2;
if(CheckCursorBounds(w))
ScrollVertical(w, SCROLL_DOWN);
HexSTShowMemStr();
break;
case KB_TOP: //relative jump to kb_left_most (extra code adds tons of speed when the jump is a long distance)
{
long addr=(g->width*g->height) + (g->width-2);
if(g->kb_left_most > g->top + addr || g->kb_left_most < g->top)
{
JumpTo(w, g->kb_left_most);
}
else
{
g->addr=g->kb_left_most-g->top; //this line
g->nibble=0; //and this line are the only necessary lines of code in this handler
if(CheckCursorBounds(w))
WinPaint(w);
}
}
HexSTShowMemStr();
break;
case KB_BOTTOM: //relative jump to kb_right_most (extra code adds tons of speed when the jump is a long distance)
{
long addr=(g->width*g->height) + (g->width-2);
char *tmptop=(char *)(g->kb_right_most - addr);
if(g->kb_right_most > g->top + addr || g->kb_right_most < g->top)
{//offscreen
JumpTo(w, tmptop);
g->addr=addr;
if((ULONG)tmptop%2)g->addr++; //account for an odd jump address
CheckCursorBounds(w);
}
else
{//on screen relative addr
g->addr=g->kb_right_most-g->top; //this line
g->nibble=0; //and this line are the only necessary lines of code in this handler
if(CheckCursorBounds(w))
WinPaint(w);
}
}
HexSTShowMemStr();
break;
#ifdef _89
case '=':
e->info.keyInfo.keyCode='A';
goto special_break;
case '(':
e->info.keyInfo.keyCode='B';
goto special_break;
case ')':
e->info.keyInfo.keyCode='C';
goto special_break;
case ',':
e->info.keyInfo.keyCode='D';
goto special_break;
case '/':
e->info.keyInfo.keyCode='E';
goto special_break;
case '|':
e->info.keyInfo.keyCode='F';
#endif
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
toupper(e->info.keyInfo.keyCode);
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
special_break: //yay, a good case for a goto?
e->info.keyInfo.keyCode-=0x37; //convert to hex val
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
TRY //handle protected memory violation's so cursor handling does not mess up
e->info.keyInfo.keyCode-=0x30; //convert to hex val
e->info.keyInfo.keyCode&=0x0f; //mask out any garbage
if(g->nibble)
{//apply nibble mask
g->top[g->addr]&=0xf0;
}
else
{
g->top[g->addr]&=0x0f;
e->info.keyInfo.keyCode=e->info.keyInfo.keyCode<<4; //adjust by one nibble
}
g->top[g->addr]|=e->info.keyInfo.keyCode; //write the new value at the current nibble
//update the current display line with the newly edited nibble
RefreshCurLine(w);
//advance to next nibble if there was no error during the write process
pushkey(KB_RIGHT);
ONERR
EV_errorCode=errCode;
ENDTRY
break;
};//end switch(e->info.keyInfo.keyCode)
cu=CU_CURSOR_ON;
Cursor(w);
break;//end case CM_KEYPRESS
};//end switch(e->command)
return;
}
static void WinPaint(WINDOW *w)
{
HEX_DAT d=global.hex_dat;
register short i;
short offset=0;
register unsigned short *h;
h=(unsigned short*)d.top;
w->Flags|=WF_VIRTUAL; //hide the drawing process
for(i=0;i<d.height;i++)
{
WinStrXY(w, 5, 1+(i<<3), HexBuff(d.width, offset, h));
offset++;
}
w->Flags&=((~WF_VIRTUAL) & (~WF_DIRTY));
WinBackupToScr(w); //w->Flags|=WF_DIRTY;
HexSTShowMemStr();
}
//pleasantly slow, but faster than redrawing the whole screen
static void ScrollVertical(WINDOW *w, SCROLL_MODE dir)
{
HEX_DAT *d=&global.hex_dat;
WIN_RECT wr={ 0, 0, 0, 0 };
wr.x1=WinWidth(w);
wr.y1=d->height<<3;
w->Flags|=WF_VIRTUAL; //hide the drawing process
WinScrollV(w, &wr, dir<<3);
d->height--;
if(dir==SCROLL_DOWN)
WinStrXY(w, 5, 1+((d->height)<<3), HexBuff(d->width, d->height, (unsigned short*)d->top));
else {
wr.y0=wr.y1;
WinAttr(w, A_REVERSE);
WinLine(w, &wr); //clean up the bottom row of garbage from scrolling
WinAttr(w, A_REPLACE);
WinStrXY(w, 5, 1, HexBuff(d->width, 0, (unsigned short*)d->top));
}
d->height++;
w->Flags&= ~WF_VIRTUAL;
WinBackupToScr(w);
}
static void RefreshCurLine(WINDOW *w)
{
register HEX_DAT *d=&global.hex_dat;
register short y;
y=d->addr/d->width;
WinStrXY(w, 5, 1+((y)<<3), HexBuff(d->width, y, (unsigned short*)d->top));
}
//using unsigned pointers so the compiler generates optimal code in the sprintf calls
//otherwise it plays around with sign extending etc...
static char * HexBuff(short width, register short offset, register unsigned short *h)
{
register char *buff=gBuff;
register unsigned char *c=(unsigned char *)h;
register short offsetb;
switch(width)
{//handle largest possibility first for speed (hopefully our compiler picks up on this :)
#ifdef _92
case 10:
offset*=5;
offsetb=offset*2;
sprintf(buff, "%04x %04x %04x %04x %04x %c%c%c%c%c%c%c%c%c%c",
h[offset], h[1 + offset], h[2 + offset], h[3 + offset], h[4 + offset],
c[offsetb], c[1 + offsetb], c[2 + offsetb], c[3 + offsetb],
c[4 + offsetb], c[5 + offsetb], c[6 + offsetb], c[7 + offsetb],
c[8 + offsetb], c[9 + offsetb]
);
RemoveNull(buff, 26, 36);
break;
case 8: //this width is normally only availiable if the user picks 8 bytes wide in the format dialog box
offset*=4;
offsetb=offset*2;
sprintf(buff, "%04x %04x %04x %04x %c%c%c%c%c%c%c%c",
h[offset], h[1 + offset], h[2 + offset], h[3 + offset],
c[offsetb], c[1 + offsetb], c[2 + offsetb], c[3 + offsetb],
c[4 + offsetb], c[5 + offsetb], c[6 + offsetb], c[7 + offsetb]
);
RemoveNull(buff, 21, 29);
break;
#endif
case 6:
offset*=3;
offsetb=offset*2;
sprintf(buff, "%04x %04x %04x %c%c%c%c%c%c",
h[offset], h[1 + offset], h[2 + offset],
c[offsetb], c[1 + offsetb], c[2 + offsetb], c[3 + offsetb], c[4 + offsetb], c[5 + offsetb]
);
RemoveNull(buff, 16, 22);
break;
case 4:
offset*=2;
offsetb=offset*2;
sprintf(buff, "%04x %04x %c%c%c%c",
h[offset], h[1 + offset],
c[offsetb], c[1 + offsetb], c[2 + offsetb], c[3 + offsetb]
);
RemoveNull(buff, 11, 15);
offset+=2;
offsetb+=4;
break;
case 2:
offsetb=offset*2;
sprintf(buff, "%04x %c%c",
h[offset],
c[offsetb], c[1 + offsetb]
);
RemoveNull(buff, 6, 8);
break;
};
return buff;
}
static void Cursor(WINDOW *w)
{
register HEX_DAT *d=&global.hex_dat;
WIN_RECT cursor = { 5, 0, 11, 8 };
char buff[100];
register unsigned short offsety;
register unsigned short offsetx;
if(d->addr)//avoid Divide by Zero
{
offsetx=(d->addr%d->width)*2; //x offset -
offsetx+= offsetx/3;
if(offsetx && (!(offsetx%8) || !(offsetx%13) || !(offsetx%18)))offsetx--;
if(offsetx && (!(offsetx%0x15) || !(offsetx%0x17)))offsetx--;
offsetx*=6;
offsety=(d->addr/d->width)<<3;
//y offset |
cursor.x0+=offsetx;
cursor.x1+=offsetx;
cursor.y0+=offsety;
cursor.y1+=offsety;
}
/*if(d->addr<=d->width)
{//clear the very top (horizontal) pixel line of the screen
WIN_RECT topline = {0, 0, 0, 0};
topline.x1=d->width*6;
WinAttr(w, A_REVERSE);
WinLine(w, &topline);
WinAttr(w, A_REPLACE);
}*/
if(d->nibble) {
cursor.x0+=6;
cursor.x1+=6;
}
WinFill(w, &cursor, A_XOR);
sprintf(buff, "0x%08lx ", d->top+d->addr);//each space is 2 pixels wide
WinFont(w, F_4x6);
WinStrXY(w, 5, 1+(d->height<<3), buff);
WinFont(w, F_6x8);
}
//returns TRUE if the window needs to be repainted
static BOOL CheckCursorBounds(WINDOW *w)
{
HEX_DAT *g=&global.hex_dat;
if(g->addr<0)
{//low bound check
do {
g->addr+=g->width;
g->top-=g->width;
}
while(g->addr<0);
return TRUE;
}
else if(g->addr >= g->width*g->height)
{//high bound check
do {
g->addr-=g->width;
g->top+=g->width;
}while(g->addr >= g->width*g->height); //repeat until within bounds ie window change from large to small
return TRUE;
}
return FALSE;
}
//replaces null characters with spaces from str[start] to str[end]
static void RemoveNull(register char *str, register short start, register short end)
{
for(;start<end;start++)
{
if(!str[start])str[start]=' ';
}
}
void SetHexEditorDefaults(WINDOW *w)
{
HEX_DAT *g=&global.hex_dat;
HEX_JUMP_DAT *j=&global.hex_jump_dat;
HEX_FIND_DAT *f=&global.hex_find_dat;
HEX_EDIT_DAT *h=&global.hex_edit_dat;
HEX_JUMP_GLOBAL_VAR *hjgv=&global.hex_jump_global_var;
HEX_JUMP_COMMON_MEM *hjcm=&global.hex_jump_common_mem;
j->optList[0]=JUMP_ADDRESS;
j->txtAddr[0]=0;
h->txtValue[0]=0;
h->optList[0]=EDIT_CHAR;
h->optList[1]=MASK_NONE;
if(!g->byteswide)//if it is initialized, it is because of ResetMagic
#ifdef _92
g->byteswide=TEN_WIDE;
#else
g->byteswide=SIX_WIDE;
#endif
f->txtValue[0]=0;
strcpy(f->txtStopAddr, "0hffffffff");
f->optList[0]=EDIT_CHAR;
f->optList[1]=DIR_FOREWARD;
hjgv->optList[0]=1;
hjgv->txtValue[0]=0;
hjcm->optList[0]=1;
hjcm->txtValue[0]=0;
SetEditorWidth(w);
g->kb_left_most=0;
g->kb_right_most=0;
JumpTo(w, g->top);
}
static void JumpTo(WINDOW *w, char *address)
{
HEX_DAT *g=&global.hex_dat;
if(((unsigned long)address)%2)
{//if the address is odd
g->top=(char*)address-1;
g->addr=1;
}
else
{
g->top=address;
g->addr=0;
}
g->nibble=0;
w->Flags|=WF_DIRTY;
}
static void DoJumpToDialog(WINDOW *w)
{
Access_AMS_Global_Variables;
HEX_JUMP_DAT *j=&global.hex_jump_dat;
HEX_DAT *g=&global.hex_dat;
void *handle;
HANDLE h;
switch(Dialog(&dlgJumpTo, -1, -1, j->txtAddr, j->optList))
{
case KB_ENTER:
switch(j->optList[0])
{
case JUMP_ADDRESS:
JumpTo(w, (char*)((unsigned long)StrToEStackToLong(j->txtAddr)));
break;
case JUMP_REL_ADDR:
JumpTo(w, (char*)(long)global.hex_dat.top+(long)global.hex_dat.addr+((long)StrToEStackToLong(j->txtAddr)));
break;
case JUMP_HANDLE:
h=(HANDLE)StrToEStackToLong(j->txtAddr);
handle=HeapDeref(h);
if(CheckInvalidHandle(handle)) ER_throwFrame(ER_InvalidHandle, pAppObj);
JumpTo(w, handle);
g->kb_left_most=handle;
g->kb_right_most=(void*)((ULONG)handle+(ULONG)HeapSize(h));
break;
};
break;
case DB_MEMFULL: //handle low memory error
EV_errorCode=ER_MEMORY;
break;
}
}
static void DoHexEditDialog(WINDOW *w)
{
Access_AMS_Global_Variables;
WORD popup;
unsigned long newval=0; //to satisfy the compiler
HEX_DAT *g=&global.hex_dat;
HEX_EDIT_DAT *h=&global.hex_edit_dat;
long bytestoend=g->addr;
while(bytestoend>g->width)bytestoend-=g->width;
bytestoend=g->width-bytestoend;
if(h->dlginitstr)
{
memcpy(h->txtValue, h->dlginitstr, HEDIT_BUFF_SIZE);
}
TRY
switch(Dialog(&dlgHexEditCur, -1, -1, h->txtValue, h->optList))
{
case KB_ENTER:
if(h->optList[0]!=EDIT_STRING)
{
if(h->txtValue[0]==0)
strcpy(h->txtValue, "0");
newval=StrToEStackToLong(h->txtValue);
}
switch(h->optList[0]) //speed, not size my friend
{//editing even address
case EDIT_CHAR:
switch(h->optList[1])
{
case MASK_NONE:
g->top[g->addr] = newval; //handle char
break;
case MASK_AND:
g->top[g->addr] &= newval; //handle char
break;
case MASK_OR:
g->top[g->addr] |= newval; //handle char
break;
case MASK_XOR:
g->top[g->addr] ^= newval; //handle char
break;
};
RefreshCurLine(w);
break;
case EDIT_SHORT:
switch(h->optList[1])
{
case MASK_NONE:
*(USHORT*)(&g->top[g->addr]) = newval; //handle word
break;
case MASK_AND:
*(USHORT*)(&g->top[g->addr]) &= newval; //handle word
break;
case MASK_OR:
*(USHORT*)(&g->top[g->addr]) |= newval; //handle word
break;
case MASK_XOR:
*(USHORT*)(&g->top[g->addr]) ^= newval; //handle word
break;
};
RefreshCurLine(w);
break;
case EDIT_LONG:
switch(h->optList[1])
{
case MASK_NONE:
*(ULONG*)(&g->top[g->addr]) = newval; //handle long
break;
case MASK_AND:
*(ULONG*)(&g->top[g->addr]) &= newval; //handle long
break;
case MASK_OR:
*(ULONG*)(&g->top[g->addr]) |= newval; //handle long
break;
case MASK_XOR:
*(ULONG*)(&g->top[g->addr]) ^= newval; //handle long
break;
};
if(bytestoend>3)
RefreshCurLine(w);
else
WinPaint(w);
break;
case EDIT_STRING:
#ifndef NO_EASTER_EGGS
if(!stricmp(h->txtValue,"RedPillNeo")) RedPillNeo();
//if(!stricmp(h->txtValue,"GreenLightGo")) GreenLightGo();
#endif
strcpy(&g->top[g->addr], h->txtValue);
if(bytestoend>strlen(h->txtValue))
RefreshCurLine(w);
else
WinPaint(w);
break;
};
break;
case DB_MEMFULL: //handle low memory error
EV_errorCode=ER_MEMORY;
break;
}
ONERR
EV_errorCode=errCode;
ENDTRY
}
DWORD cbHexEdit(WORD DlgId, DWORD dValue)
{
HEX_DAT *g=&global.hex_dat;
HEX_EDIT_DAT *h=&global.hex_edit_dat;
if(DlgId==DB_GET_TITLE)
{
return (DWORD)h->dlgtitle;
}
if(((ULONG)(&g->top[g->addr])%2))
{
if(h->optList[0]!=EDIT_CHAR && h->optList[0]!=EDIT_STRING)
{
h->optList[0]=EDIT_CHAR;
return DB_REDRAW_AND_CONTINUE;
}
}
if(h->optList[0]==EDIT_STRING)
h->optList[1]=MASK_NONE;
return DB_REDRAW_AND_CONTINUE;
//return TRUE;
}
static void HandlePasteStr(WINDOW *w, HANDLE h)
{//a few extra variables here to make future expansion faster
HEX_DAT *g=&global.hex_dat;
char *ptr;
UCHAR *tag_ptr;
BYTE tokenized[MAX_SYM_LEN];
HSYM hsym;
SYM_ENTRY *sym;
WORD result=0;
ptr=HeapDeref(h);
if((ptr=strchr(ptr,'(')))*ptr=0; //sometimes the var-link adds a ( to the pasted string
hsym=SymFind(StrToTokN(HeapDeref(h), tokenized));
if(sym=DerefSym(hsym))
result=MenuPopup(&pupEditWhat, -1, -1, 0);
if(!result)
return; //low mem, or ESC pressed
switch(result)
{
case JUMP_TO_DATA:
ptr=HeapDeref(sym->hVal);
tag_ptr=HToESI(sym->hVal);
if(*tag_ptr==ASM_PRGM_TAG) EX_patch(ptr+2, tag_ptr);
g->kb_right_most=ptr+(*(unsigned short*)ptr) + 1;
break;
case JUMP_TO_SYM_ENTRY:
ptr=(char *)sym;
g->kb_right_most=ptr+(sizeof(SYM_ENTRY)-1);
break;
};
if(ptr) {
JumpTo(w, ptr);
g->kb_left_most=ptr;
}
}
static void DoFindDialog(WINDOW *w)
{
Access_AMS_Global_Variables;
WORD popup;
HEX_FIND_DAT *f=&global.hex_find_dat;