1 #ifndef HEXEDIT_H
2 #define HEXEDIT_H
3 
4 /* hexedit.h by Adam Rogoyski <apoc@laker.net> Temperanc on EFNet irc
5  * Copyright (C) 1998, 1999 Adam Rogoyski
6  * --- GNU General Public License Disclamer ---
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "../config.h"
19 #endif
20 
21 #include <ctype.h>
22 
23 #ifdef HAVE_NCURSES_H
24 #include <ncurses.h>
25 #else
26 #ifdef HAVE_CURSES_H
27 #include <curses.h>
28 #endif
29 #endif
30 
31 #include <errno.h>
32 
33 #ifdef HAVE_LIMITS_H
34 #include <limits.h>
35 #endif
36 
37 #ifdef STDC_HEADERS
38 #include <stdlib.h>
39 #include <string.h>
40 #endif
41 
42 #include <stdio.h>
43 #include <signal.h>
44 
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 
49 #ifdef HAVE_SYS_IOCTL_H
50 #include <sys/ioctl.h>
51 #endif
52 
53 #include <sys/stat.h>
54 #include <sys/types.h>
55 
56 #ifdef HAVE_FCNTL_H
57 #include <fcntl.h>
58 #endif
59 
60 
61    /* When we push a change on the Undo Stack, this is what type of change
62     * it was.
63     */
64 #define  INSERT            1
65 #define  DELETE            2
66 #define  CHANGE            3
67 
68    /* Globals.modified takes on 0 if the file is the original (doesn't need
69     * to be saved), 1 if it's modified (needs to be saved), and 2 for
70     * read-only.
71     */
72 #define  MODIFIED          1
73 #define  READ_ONLY         2
74 
75    /* Globals.print_mode decides what in the text mode part of the screen
76     * is printed.  Regular is 7-bit ascii.  High ascii is 8-bit ascii.
77     * All print is all characters.
78     */
79 #define  REGULAR_PRINT     1
80 #define  HIGH_ASCII_PRINT  2
81 #define  ALL_PRINT         3
82 
83    /* These I think are defined in curses as macros but I still use them */
84 #ifndef  __PDCURSES__
85 #define  ALT_V             118
86 #endif
87 #define  BACKSPACE         127
88 #define  CONTROL_A         1
89 #define  CONTROL_B         2
90 #define  CONTROL_C         3
91 #define  CONTROL_D         4
92 #define  CONTROL_E         5
93 #define  CONTROL_F         6
94 #define  CONTROL_G         7
95 #define  CONTROL_H         8
96 #define  CONTROL_I         9
97 #define  CONTROL_L         12
98 #define  CONTROL_N         14
99 #define  CONTROL_O         15
100 #define  CONTROL_P         16
101 #define  CONTROL_R         18
102 #define  CONTROL_T         20
103 #define  CONTROL_U         21
104 #define  CONTROL_V         22
105 #define  CONTROL_W         23
106 #define  CONTROL_X         24
107 #define  CONTROL_Y         25
108 #define  CONTROL_SLASH     31
109 #define  ESCAPE_CHARACTER  27
110 #define  SPACEBAR          32
111 #define  TAB               9
112 
113    /* Prevent me from typing these in wrong */
114 #define  M_0xFFFFFFF0      0xfffffff0
115 #define  M_0xF0            0xf0
116 #define  M_0x10            0x10
117 #define  M_0x0F            0x0f
118 
119    /* Top and bottom lines of the main window are different depending if
120     * curses is broken or not.
121     */
122 #define  MAIN_HEIGHT       (LINES - 2)
123 #define  MAIN_TOP_LINE     1
124 #define  MAIN_BOTTOM_LINE  (LINES - 3)
125 #define  BOTTOM_LINE       (LINES - 2)
126 
127    /* Malloc extra bytes so we can do a lot of inserts without realloc */
128 #define  EXTRA_BUF         1024
129 
130    /* In file selection widget, the filename starts at column 57. */
131 #define  NAME_POS          57
132 
133    /* Globals.mode, which mode of the editor we are in.  They are File
134     * selection widget, normal hex mode, ascii mode (snapshot), and the
135     * calculator.
136     */
137 #define  FILE_MODE         0
138 #define  HEX_MODE          1
139 #define  ASCII_MODE        2
140 #define  CALCULATOR_MODE   3
141 
142    /* Globals.charset, if we are using ASCII or EBCDIC character set to
143     * display text.
144     */
145 #define  ASCII_CHAR_SET    0
146 #define  EBCDIC_CHAR_SET   1
147 
148    /* Buffer size is how much we read of the file at a time from disk and
149     * keep in memory (in filebuf).  Read buffer is how much we read from
150     * disk with each fread () call.
151     * Buffer size must be greater than read buffer, both must be even.
152     */
153 #define  DEFAULT_BUFFER_SIZE  0x4000    /* 16384 */
154 #define  DEFAULT_READ_BUFFER  0x800     /*  2048 */
155 
156 
157    /* Functions prototypes for functions used througout */
158 
159    /* Functions in misc.c */
160 char *       chompWhiteSpace        (char *);
161 int          cursor_ascii_to_ebcdic (int);
162 void         die_horribly           (const char * const, const char * const);
163 void         do_beep                ();
164 void         exitProgram            (void);
165 void         exitSave               (int);
166 int          getHexValue            (wchar_t);
167 char         getAsciiValue          (wchar_t);
168 void         handleInterrupt        (int);
169 #ifndef __PDCURSES__
170    void      handleSigwinch         (int);
171 #endif
172 int          isHexChar              (wchar_t);
173 int          isprintable            (int);
174 int          mappos                 (int);
175 int          mapcur                 (int);
176 void         switchModes            (void);
177 void         usage                  (char *);
178 
179 
180    /* Functions in init.c */
181 void         init (int, char **);
182 void         load_new_file (FILE **);
183 void         popup_Error (const char * const, int);
184 char *       select_new_file (FILE **);
185 
186 
187    /* Functions in hexkeys.c */
188 void         endKey          (void);
189 void         handleArrowKeys (wchar_t);
190 void         hexMode         (wchar_t);
191 void         homeKey         (void);
192 void         pagedown        (void);
193 void         pageup          (void);
194 void         tabcursor       (int *);
195 
196 
197    /* Functions in edit.c */
198 void         deleteByte      (int);
199 void         filebuff_modify (unsigned long, unsigned char);
200 wchar_t      findEBCDIC      (wchar_t);
201 void         insertByte      (unsigned char, int);
202 void         over_write      (wchar_t);
203 
204 
205    /* Functions in filebuf.c */
206 void           initbuffer         (FILE *, unsigned long, unsigned long);
207 unsigned char  filebuffer         (unsigned long);
208 unsigned long  filebuffer_new_buf ();
209 
210 
211    /* Functions in asciikey.c */
212 void           asciiMode (wchar_t);
213 
214 
215 
216    /* Functions in windows.c and widgets.c */
217 struct
218 ret_string * hex_string_box       (WINDOW *, int, int);
219 void         jump_relative_offset (wchar_t);
220 int          popupFileSave        (void);
221 void         popupGotoOffset      (long);
222 void         popupHelp            (void);
223 WINDOW *     popupWindow          (int, int);
224 void         search               (int);
225 char *       stringBox            (WINDOW *, int, int, int, int, char *);
226 
227 
228 
229    /* Functions in print.c */
230 void         drawdump (unsigned long);
231 void         printHelpWindow (void);
232 void         printStatusWindow (void);
233 void         redraw (void);
234 
235 
236    /* Functions in search.c */
237 struct
238 foundit *    boyer_moore_search (unsigned char *, unsigned long,
239                                  unsigned long, unsigned long);
240 unsigned
241 char         bytecmp (const unsigned char *, const unsigned char *,
242                       long);
243 
244 
245    /* Functions in undo.c */
246 void      pushUndo       (int, unsigned long, unsigned char, unsigned char);
247 void      undoLastChange (void);
248 
249 
250    /* Functions in help.c */
251 void         help_initialize (void);
252 void         help_print (WINDOW *, int, int);
253 
254 
255    /* Functions in file.c and filekeys.c */
256 char *             fileSelect (void);
257 void               statWindow    (const char * const);
258 void               helpWindow    (const char * const);
259 struct FileNames * getDirectory  (const char * const);
260 struct FileNames * merge         (struct FileNames *, struct FileNames *);
261 struct FileNames * msort         (struct FileNames *);
262 void               printPage     (const struct FileNames *);
263 void               file_key_up   (int *);
264 void               file_key_down (int *, int);
265 void               file_redraw   (int *);
266 void               file_pagedown (int *, int);
267 void               file_pageup   (int *);
268 void               file_home     (int *);
269 void               file_end      (int *, int);
270 
271 
272    /* functions in calc.c */
273 #define BIN_BOX  1
274 #define OCT_BOX  2
275 #define DEC_BOX  3
276 #define HEX_BOX  4
277 #define BOX_LEN  64
278 
279 struct calcEntryBox;
280 void binary_calculator (void);
281 void calcStatWindow    (void);
282 void drawEntryBox      (WINDOW *, struct calcEntryBox *, int, int, int);
283 void calcDrawBoxes     (struct calcEntryBox *, struct calcEntryBox *,
284                         struct calcEntryBox *, struct calcEntryBox *, int);
285 void calcNumberKey     (struct calcEntryBox *, struct calcEntryBox *,
286                         struct calcEntryBox *, struct calcEntryBox *,
287                         wchar_t, int);
288 void calcArrowKey      (struct calcEntryBox *, struct calcEntryBox *,
289                         struct calcEntryBox *, struct calcEntryBox *,
290                         wchar_t, int *);
291 void calcBinaryString  (struct calcEntryBox *);
292 void calcOctalString   (struct calcEntryBox *);
293 void calcDecimalString (struct calcEntryBox *);
294 void calcHexString     (struct calcEntryBox *);
295 
296 
297    /* Functions in hash.c */
298 void         clear_hash ();
299 void         commit_changes_in_hash (FILE *);
300 void         delete_hash_entry (unsigned long);
301 void         init_hash (void);
302 void         insert_hash_entry (unsigned long, unsigned char);
303 int          hash_lookup (unsigned long, unsigned char *);
304 
305 
306 
307 
308 #ifdef MAIN_C
309 unsigned long offset = 0x00;     /* where in the file you are */
310 int position = 0;                /* where on the line you are */
311 int cursor_x = 10;               /* cursor tracking in hex mode */
312 int cursor_y = MAIN_TOP_LINE;
313 int acursor_x = 0;               /* cursor tracking in ascii mode */
314 int acursor_y = 0;
315 int color_term = 0;              /* is a color terminal */
316 unsigned char *filebuf = NULL;   /* the file in memory */
317 int extrabuf = EXTRA_BUF;        /* always malloc extra bytes for
318                                     inserting text */
319 int *newlines = NULL;            /* used in ascii mode, where to start lines */
320 
321 /* constant strings for printing */
322 const char * const NOT_ENOUGH_MEMORY = "Not enough memory";
323 
324 
325 #else
326 extern unsigned long offset;
327 extern int position;
328 extern int cursor_x;
329 extern int cursor_y;
330 extern int acursor_x;
331 extern int acursor_y;
332 extern int color_term;
333 extern unsigned char *filebuf;
334 extern int extrabuf;
335 extern int *newlines;
336 extern const unsigned char EBCDIC[];
337 extern const unsigned char ASCII_to_EBCDIC[];
338 extern const char * const NOT_ENOUGH_MEMORY;
339 #endif
340 
341 
342    /* Global structure, keep most global variables here. */
343 struct
344 {
345    WINDOW *wmain, *wstatus, *whelp; /* three windows used throughout. */
346    unsigned long filesize;          /* size of the file buffer. */
347    FILE *fp;                        /* File we are editing. */
348    const char *filename;            /* Name of file we are editing. */
349    int mode;                        /* what mode the program is in, such as
350                                      * file widget, hex, ascii, calculator. */
351    int modified;                    /* if the buffer is modified/read-only. */
352    int tabb;                        /* are we editing hex or text side. */
353    int charset;                     /* ascii or ebcdic character set. */
354    int spacing;                     /* byte or word like spacing layout. */
355    int print_mode;                  /* print 7-bit text, 8-bit text, or
356                                      * all text. */
357    unsigned int buffsize;           /* size of buffer read into memory. */
358    int fullsize;                    /* is buffsize holding the full buffer. */
359    int fixed_disk;                  /* file is a fixed disk. */
360    unsigned long buf_front;         /* current *filebuf in memory is the.  */
361    unsigned long buf_end;           /* chunk from offset buf_front through. */
362                                     /* buf end. */
363    int beeping;                     /* Allow beeping or not. */
364    int help_msg_count;              /* Number of messages in help menu. */
365 } Globals;
366 
367 
368 struct foundit
369    /* used for returning from searching */
370 {
371    int flag;
372    unsigned long offset;
373 };
374 
375 
376 struct ret_string
377    /* used for returning from hexstring box */
378 {
379    int len;
380    unsigned char *str;
381 };
382 
383 
384 struct Change
385    /* A node in the ChangeLog stack */
386 {
387    int type;                     /* type of change (insert, delete, modify) */
388    unsigned long offset;         /* location of the change */
389    unsigned char old;            /* the previous overwritten value */
390    unsigned char new;            /* the new value */
391    struct Change *p;             /* next change in the list */
392 };
393 
394 struct ChangeLog
395    /* Stack of changes to use with undo */
396 {
397    int s;
398    struct Change *base;
399    struct Change *top;
400 } UndoStack;
401 
402 
403 struct FileNames
404    /* node for linked list of filenames */
405 {
406    char *filename;
407    struct FileNames *p;
408 };
409 
410 
411 struct calcEntryBox
412 {
413    char *tokens;
414    char s[BOX_LEN + 1];
415    int x;
416    int pos_y;
417    int pos_x;
418    union
419    {
420       int i;
421       unsigned int ui;
422       long l;
423       unsigned long ul;
424    } value;
425 };
426 
427 
428 #endif
429