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
, size_t *sizea
, char *tmp
, size_t tmp_size
);
20 static void searchB(off_t loc
, char *string
);
22 /*******************************************************************************/
23 /* Search functions */
24 /*******************************************************************************/
25 static int searchA(char **string
, size_t *sizea
, char *tmp
, size_t 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(off_t 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
];
62 if (!searchA(&string
, &sizea
, tmp
, sizeof(tmp
))) return;
64 blockstart
= base
+ cursor
- BLOCK_SEARCH_SIZE
+ sizea
;
66 blockstart
+= BLOCK_SEARCH_SIZE
- sizea
+ 1;
67 if (LSEEK_(fd
, blockstart
) == -1) { quit
= -3; break; }
68 if ((sizeb
= read(fd
, tmp
, BLOCK_SEARCH_SIZE
)) < sizea
) quit
= -3;
69 else if (getch() != ERR
) quit
= -2;
70 else if ((p
= mymemmem(tmp
, sizeb
, string
, sizea
))) quit
= p
- tmp
;
72 sprintf(tmpstr
,"searching... 0x%08llX", (long long) blockstart
);
73 nodelay(stdscr
, TRUE
);
74 displayTwoLineMessage(tmpstr
, "(press any key to cancel)");
78 searchB(quit
+ (quit
>= 0 ? blockstart
: 0), string
);
81 void search_backward(void)
83 char *p
, *string
, tmp
[BLOCK_SEARCH_SIZE
], tmpstr
[BLOCK_SEARCH_SIZE
];
88 if (!searchA(&string
, &sizea
, tmp
, sizeof(tmp
))) return;
90 blockstart
= base
+ cursor
- sizea
+ 1;
92 blockstart
-= BLOCK_SEARCH_SIZE
- sizea
+ 1;
93 sizeb
= BLOCK_SEARCH_SIZE
;
94 if (blockstart
< 0) { sizeb
-= -blockstart
; blockstart
= 0; }
96 if (sizeb
< sizea
) quit
= -3;
98 if (LSEEK_(fd
, blockstart
) == -1) { quit
= -3; break; }
99 if ((ssize_t
) sizeb
!= read(fd
, tmp
, sizeb
)) quit
= -3;
100 else if (getch() != ERR
) quit
= -2;
101 else if ((p
= mymemrmem(tmp
, sizeb
, string
, sizea
))) quit
= p
- tmp
;
104 sprintf(tmpstr
,"searching... 0x%08llX", (long long) blockstart
);
105 nodelay(stdscr
, TRUE
);
106 displayTwoLineMessage(tmpstr
, "(press any key to cancel)");
108 } while (quit
== -1);
110 searchB(quit
+ (quit
>= 0 ? blockstart
: 0), string
);