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.*/
20 void setToChar(int i
, unsigned char c
)
22 if (i
>= nbBytes
) { /* If appending, always mark as modified */
24 bufferAttr
[i
] |= MODIFIED
;
25 addToEdited(base
+ i
, 1, &c
);
27 } else if (buffer
[i
] != c
) {
29 bufferAttr
[i
] |= MODIFIED
;
30 addToEdited(base
+ i
, 1, &c
);
34 /*******************************************************************************/
35 /* Pages handling functions */
36 /*******************************************************************************/
37 void updatelastEditedLoc(void)
41 for (p
= edited
; p
; p
= p
->next
) {
42 if (p
->base
+ p
->size
> lastEditedLoc
)
43 lastEditedLoc
= p
->base
+ p
->size
;
47 void discardEdited(void)
50 for (p
= edited
; p
; p
= q
) {
56 if (base
+ cursor
> biggestLoc
) set_cursor(biggestLoc
);
57 if (mark_max
>= biggestLoc
) mark_max
= biggestLoc
- 1;
60 void addToEdited(off_t base
, int size
, unsigned char *vals
)
62 typePage
*p
, *q
= NULL
;
63 for (p
= edited
; p
; q
= p
, p
= p
->next
) {
64 if (base
+ size
<= p
->base
) break;
65 if (base
<= p
->base
&& p
->base
+ p
->size
<= base
+ size
) {
66 if (q
) q
->next
= p
->next
; else edited
= p
->next
;
75 if (q
&& base
<= q
->base
+ q
->size
&& q
->base
<= base
+ size
) {
76 /* chevauchement (?? how to say it in english ??) */
79 min
= MIN(q
->base
, base
);
80 if (p
&& base
+ size
== p
->base
) {
81 max
= p
->base
+ p
->size
;
82 s
= malloc(max
- min
);
83 memcpy(s
+ q
->base
- min
, q
->vals
, q
->size
);
84 memcpy(s
+ base
- min
, vals
, size
);
85 memcpy(s
+ p
->base
- min
, p
->vals
, p
->size
);
86 free(q
->vals
); q
->vals
= s
;
90 max
= MAX(q
->base
+ q
->size
, base
+ size
);
91 s
= malloc(max
- min
);
92 memcpy(s
+ q
->base
- min
, q
->vals
, q
->size
);
93 memcpy(s
+ base
- min
, vals
, size
);
94 free(q
->vals
); q
->vals
= s
;
98 } else if (p
&& base
+ size
== p
->base
) {
99 unsigned char *s
= malloc(p
->base
+ p
->size
- base
);
100 memcpy(s
, vals
, size
);
101 memcpy(s
+ p
->base
- base
, p
->vals
, p
->size
);
102 free(p
->vals
); p
->vals
= s
;
103 p
->size
= p
->base
+ p
->size
- base
;
106 typePage
*r
= newPage(base
, size
);
107 memcpy(r
->vals
, vals
, size
);
108 if (q
) q
->next
= r
; else edited
= r
;
111 updatelastEditedLoc();
114 void removeFromEdited(off_t base
, int size
)
116 typePage
*p
, *q
= NULL
;
117 for (p
= edited
; p
; p
? (q
= p
, p
= p
->next
) : (q
= NULL
, p
= edited
)) {
118 if (base
+ size
<= p
->base
) break;
119 if (base
<= p
->base
) {
120 if (p
->base
+ p
->size
<= base
+ size
) {
121 if (q
) q
->next
= p
->next
; else edited
= p
->next
;
125 p
->size
-= base
+ size
- p
->base
;
126 memmove(p
->vals
, p
->vals
+ base
+ size
- p
->base
, p
->size
);
127 p
->base
= base
+ size
;
129 } else if (p
->base
+ p
->size
<= base
+ size
) {
130 if (base
< p
->base
+ p
->size
) p
->size
-= p
->base
+ p
->size
- base
;
132 q
= newPage(base
+ size
, p
->base
+ p
->size
- base
- size
);
133 memcpy(q
->vals
, p
->vals
+ base
+ size
- p
->base
, q
->size
);
136 p
->size
-= p
->base
+ p
->size
- base
;
140 updatelastEditedLoc();
143 typePage
*newPage(off_t base
, int size
)
145 typePage
*p
= (typePage
*) malloc(sizeof(typePage
));
148 p
->vals
= malloc(size
);
152 void freePage(typePage
*page
)