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)
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.*/
19 static int searchA(char **string
, int *sizea
, char *tmp
, int tmp_size
);
20 static void searchB(INT loc
, char *string
);
22 /*******************************************************************************/
23 /* Search functions */
24 /*******************************************************************************/
25 static int searchA(char **string
, int *sizea
, char *tmp
, int tmp_size
)
27 char *msg
= hexOrAscii
? "Hexa string to search: " : "Ascii string to search: ";
28 char **last
= hexOrAscii
? &lastAskHexString
: &lastAskAsciiString
;
30 if (!ask_about_save_and_redisplay()) return FALSE
;
31 if (!displayMessageAndGetString(msg
, last
, tmp
, tmp_size
)) return FALSE
;
34 if (hexOrAscii
) if (!hexStringToBinString(tmp
, sizea
)) return FALSE
;
36 *string
= malloc(*sizea
);
37 memcpy(*string
, tmp
, *sizea
);
39 nodelay(stdscr
, TRUE
);
40 displayTwoLineMessage("searching...", "(press any key to cancel)");
44 static void searchB(INT loc
, char *string
)
46 nodelay(stdscr
, FALSE
);
49 if (loc
>= 0) set_cursor(loc
);
51 if (loc
== -3) displayMessageAndWaitForKey("not found");
55 void search_forward(void)
57 char *p
, *string
, tmp
[BLOCK_SEARCH_SIZE
], tmpstr
[BLOCK_SEARCH_SIZE
];
58 int quit
, sizea
, sizeb
;
61 if (!searchA(&string
, &sizea
, tmp
, sizeof(tmp
))) return;
63 blockstart
= base
+ cursor
- BLOCK_SEARCH_SIZE
+ sizea
;
65 blockstart
+= BLOCK_SEARCH_SIZE
- sizea
+ 1;
66 if (LSEEK_(fd
, blockstart
) == -1) { quit
= -3; break; }
67 if ((sizeb
= read(fd
, tmp
, BLOCK_SEARCH_SIZE
)) < sizea
) quit
= -3;
68 else if (getch() != ERR
) quit
= -2;
69 else if ((p
= mymemmem(tmp
, sizeb
, string
, sizea
))) quit
= p
- tmp
;
71 sprintf(tmpstr
,"searching... 0x%08llX", (long long) blockstart
);
72 nodelay(stdscr
, TRUE
);
73 displayTwoLineMessage(tmpstr
, "(press any key to cancel)");
77 searchB(quit
+ (quit
>= 0 ? blockstart
: 0), string
);
80 void search_backward(void)
82 char *p
, *string
, tmp
[BLOCK_SEARCH_SIZE
], tmpstr
[BLOCK_SEARCH_SIZE
];
83 int quit
, sizea
, sizeb
;
86 if (!searchA(&string
, &sizea
, tmp
, sizeof(tmp
))) return;
88 blockstart
= base
+ cursor
- sizea
+ 1;
90 blockstart
-= BLOCK_SEARCH_SIZE
- sizea
+ 1;
91 sizeb
= BLOCK_SEARCH_SIZE
;
92 if (blockstart
< 0) { sizeb
-= -blockstart
; blockstart
= 0; }
94 if (sizeb
< sizea
) quit
= -3;
96 if (LSEEK_(fd
, blockstart
) == -1) { quit
= -3; break; }
97 if (sizeb
!= read(fd
, tmp
, sizeb
)) quit
= -3;
98 else if (getch() != ERR
) quit
= -2;
99 else if ((p
= mymemrmem(tmp
, sizeb
, string
, sizea
))) quit
= p
- tmp
;
102 sprintf(tmpstr
,"searching... 0x%08llX", (long long) blockstart
);
103 nodelay(stdscr
, TRUE
);
104 displayTwoLineMessage(tmpstr
, "(press any key to cancel)");
106 } while (quit
== -1);
108 searchB(quit
+ (quit
>= 0 ? blockstart
: 0), string
);