remove line-end witespace noise
[rofl0r-hexedit0r.git] / page.c
blob0dcabe73b88be7839bd7f0b68d4fa30b5f32ae29
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)
7 any later version.
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.*/
17 #include "hexedit.h"
20 void setToChar(int i, unsigned char c)
22 if (i >= nbBytes) { /* If appending, always mark as modified */
23 buffer[i] = c;
24 bufferAttr[i] |= MODIFIED;
25 addToEdited(base + i, 1, &c);
26 nbBytes = i+1;
27 } else if (buffer[i] != c) {
28 buffer[i] = c;
29 bufferAttr[i] |= MODIFIED;
30 addToEdited(base + i, 1, &c);
34 /*******************************************************************************/
35 /* Pages handling functions */
36 /*******************************************************************************/
37 void updatelastEditedLoc(void)
39 typePage *p;
40 lastEditedLoc = 0;
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)
49 typePage *p, *q;
50 for (p = edited; p; p = q) {
51 q = p->next;
52 freePage(p);
54 edited = NULL;
55 lastEditedLoc = 0;
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;
67 freePage(p); p = q;
68 if (q == NULL) {
69 p = edited;
70 break;
75 if (q && base <= q->base + q->size && q->base <= base + size) {
76 /* chevauchement (?? how to say it in english ??) */
77 off_t min, max;
78 unsigned char *s;
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;
87 q->next = p->next;
88 freePage(p);
89 } else {
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;
96 q->base = min;
97 q->size = max - min;
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;
104 p->base = base;
105 } else {
106 typePage *r = newPage(base, size);
107 memcpy(r->vals, vals, size);
108 if (q) q->next = r; else edited = r;
109 r->next = p;
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;
122 freePage(p);
123 p = q;
124 } else {
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;
131 } else {
132 q = newPage(base + size, p->base + p->size - base - size);
133 memcpy(q->vals, p->vals + base + size - p->base, q->size);
134 q->next = p->next;
135 p->next = q;
136 p->size -= p->base + p->size - base;
137 break;
140 updatelastEditedLoc();
143 typePage *newPage(off_t base, int size)
145 typePage *p = (typePage *) malloc(sizeof(typePage));
146 p->base = base;
147 p->size = size;
148 p->vals = malloc(size);
149 return p;
152 void freePage(typePage *page)
154 free(page->vals);
155 free(page);