-
Notifications
You must be signed in to change notification settings - Fork 42
/
page.c
165 lines (144 loc) · 4.45 KB
/
page.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* hexedit -- Hexadecimal Editor for Binary Files
Copyright (C) 1998 Pixel (Pascal Rigaux)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/
#include "hexedit.h"
void setToChar(int i, unsigned char c)
{
if (i >= nbBytes) { /* If appending, always mark as modified */
buffer[i] = c;
bufferAttr[i] |= MODIFIED;
addToEdited(base + i, 1, &c);
nbBytes = i+1;
} else if (buffer[i] != c) {
buffer[i] = c;
bufferAttr[i] |= MODIFIED;
addToEdited(base + i, 1, &c);
}
}
/*******************************************************************************/
/* Pages handling functions */
/*******************************************************************************/
void updatelastEditedLoc(void)
{
typePage *p;
lastEditedLoc = 0;
for (p = edited; p; p = p->next) {
if (p->base + p->size > lastEditedLoc)
lastEditedLoc = p->base + p->size;
}
}
void discardEdited(void)
{
typePage *p, *q;
for (p = edited; p; p = q) {
q = p->next;
freePage(p);
}
edited = NULL;
lastEditedLoc = 0;
if (base + cursor > biggestLoc) set_cursor(biggestLoc);
if (mark_max >= biggestLoc) mark_max = biggestLoc - 1;
}
void addToEdited(INT base, int size, unsigned char *vals)
{
typePage *p, *q = NULL;
for (p = edited; p; q = p, p = p->next) {
if (base + size <= p->base) break;
if (base <= p->base && p->base + p->size <= base + size) {
if (q) q->next = p->next; else edited = p->next;
freePage(p); p = q;
if (q == NULL) {
p = edited;
break;
}
}
}
if (q && base <= q->base + q->size && q->base <= base + size) {
/* chevauchement (?? how to say it in english ??) */
INT min, max;
unsigned char *s;
min = MIN(q->base, base);
if (p && base + size == p->base) {
max = p->base + p->size;
s = malloc(max - min);
memcpy(s + q->base - min, q->vals, q->size);
memcpy(s + base - min, vals, size);
memcpy(s + p->base - min, p->vals, p->size);
free(q->vals); q->vals = s;
q->next = p->next;
freePage(p);
} else {
max = MAX(q->base + q->size, base + size);
s = malloc(max - min);
memcpy(s + q->base - min, q->vals, q->size);
memcpy(s + base - min, vals, size);
free(q->vals); q->vals = s;
}
q->base = min;
q->size = max - min;
} else if (p && base + size == p->base) {
unsigned char *s = malloc(p->base + p->size - base);
memcpy(s, vals, size);
memcpy(s + p->base - base, p->vals, p->size);
free(p->vals); p->vals = s;
p->size = p->base + p->size - base;
p->base = base;
} else {
typePage *r = newPage(base, size);
memcpy(r->vals, vals, size);
if (q) q->next = r; else edited = r;
r->next = p;
}
updatelastEditedLoc();
}
void removeFromEdited(INT base, int size)
{
typePage *p, *q = NULL;
for (p = edited; p; p ? (q = p, p = p->next) : (q = NULL, p = edited)) {
if (base + size <= p->base) break;
if (base <= p->base) {
if (p->base + p->size <= base + size) {
if (q) q->next = p->next; else edited = p->next;
freePage(p);
p = q;
} else {
p->size -= base + size - p->base;
memmove(p->vals, p->vals + base + size - p->base, p->size);
p->base = base + size;
}
} else if (p->base + p->size <= base + size) {
if (base < p->base + p->size) p->size -= p->base + p->size - base;
} else {
q = newPage(base + size, p->base + p->size - base - size);
memcpy(q->vals, p->vals + base + size - p->base, q->size);
q->next = p->next;
p->next = q;
p->size -= p->base + p->size - base;
break;
}
}
updatelastEditedLoc();
}
typePage *newPage(INT base, int size)
{
typePage *p = (typePage *) malloc(sizeof(typePage));
p->base = base;
p->size = size;
p->vals = malloc(size);
return p;
}
void freePage(typePage *page)
{
free(page->vals);
free(page);
}