summaryrefslogtreecommitdiff
path: root/src/slices.c
blob: 68e39f93cd26ddd40ab6850ecee331fd48af2098 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <string.h>
#include "slices.h"

int min(int a, int b) { return (a<b)?a:b; }

slice_t *sliceNew(address_t begin, address_t end, sliceStatus_t status, slice_t *next) {
	slice_t *s;

	s = malloc(1*sizeof(slice_t));
	if (s!=NULL) {
		s->begin=begin;
		s->end=end;
		s->status=status;
		s->next=next;
	}	

	return s;
}

// Return the numbers of slices after split (3 in the general case, 2 or 1 in particular cases. -1 is memory error)
int sliceSplit(slices_t *slices, slice_t *initialSlice, address_t splitAt, sliceStatus_t statusBefore, sliceStatus_t statusAt, sliceStatus_t statusAfter) {
	slice_t *secondSlice, *thirdSlice, *rightSlice;
	int splitAfterSingularity, splitBeforeSingularity;

	/* Basically, we want to split the slice in 3 :
	[a;b] shoud be transformed in : [a;splitAt-1], [splitAt;splitAt], [splitAt+1;b]
	There is exceptions and singularities :
	* If splitAt is not within [a;b], bail out, no coherent solution
	* If splitAt==a, the first slice should not exists
	* If splitAt==b, the last slice shoud not exists
	* If a==b (and so, ==splitAt), there is nothing to split, just change status
	But, if statusBefore==statusAt, we don't want an interval [splitAt;splitAt], we want just split in 2.
	This unwanted interval should be kept merged with the first interval.

	For pratical reasons with pointer mess-up, the first action is to split between the second and the last slice
	and then between he first and second if needed.
	*/

	if ( splitAt < initialSlice->begin || splitAt > initialSlice->end ) return 2;
	
	// Test before act because we'll change values of the initialSlice because
	//  it would become the firstSlice or even the second one if the first is zero-lenght
	splitAfterSingularity=(splitAt != initialSlice->end);
	splitBeforeSingularity=(splitAt != initialSlice->begin) && (statusBefore != statusAt);

	if ( splitAfterSingularity ) {
		thirdSlice = sliceNew(splitAt+1, initialSlice->end, statusAfter, initialSlice->next);
		if ( thirdSlice == NULL ) return -1;

		initialSlice->end = splitAt;
		// No status change because we'll split again in 2 parts or not
		initialSlice->next = thirdSlice;
		if ( initialSlice == slices->last ) slices->last = thirdSlice;
		(slices->count)++;

		rightSlice=thirdSlice;
	} else {
		rightSlice=initialSlice->next;
	}

	if ( splitBeforeSingularity ) {
		secondSlice = sliceNew(splitAt, splitAt, statusAt, rightSlice);
		if ( secondSlice == NULL ) return -1;
		
		initialSlice->end = splitAt-1;
		initialSlice->status=statusBefore;
		initialSlice->next = secondSlice;
		if ( initialSlice == slices->last ) slices->last = secondSlice;
		(slices->count)++;
	} else {
		initialSlice->status=statusAt; // Two cases : a==splitAt or statusAt==statusBefore
	}

	
	return 1 + (splitBeforeSingularity?1:0) + (splitAfterSingularity?1:0);
}

slices_t *slicesNew() {
	slices_t *ss = malloc(1*sizeof(slices_t));
	if (ss!=NULL) {
		ss->count=0;
		ss->first=NULL;
		ss->last=NULL;
	}

	return ss;
}

void slicesAppend(slices_t *slices, slice_t *slice) {
	slice->next=NULL; //XXX Could be generalized
	if (slices->first==NULL || slices->last==NULL) {
		slices->first = slice;
	} else {
		slices->last->next=slice;
	}
	slices->last=slice;
	(slices->count)++;
}

slice_t *slicesFindLargest(slices_t *slices, sliceStatus_t status) {
	slice_t *curr, *sMax = NULL;
	address_t i, iMax = 0;
	
	curr = slices->first;
	while (curr != NULL) {
		i=curr->end - curr->begin + 1;
		if ( curr->status == status && i > iMax ) {
			iMax = i;
			sMax = curr;
		}
		curr=curr->next;
	}
	return sMax;
}

slice_t *slicesFindLargestFast(slices_t *slices, address_t *foundMax, sliceStatus_t status, address_t knownMax, slice_t *firstToTry) {
	slice_t *curr, *sMax = NULL;
	address_t i, iMax = 0;
	
	curr = firstToTry;
	while (curr != NULL) {
		i=curr->end - curr->begin + 1;
		if ( curr->status == status ) {
			if ( knownMax == i ) { *foundMax=i; return curr; }
			if ( i > iMax ) {
				iMax = i;
				sMax = curr;
			}
		}
		curr=curr->next;
	}
	curr = slices->first;
	while (curr != firstToTry) {
		i=curr->end - curr->begin + 1;
		if ( curr->status == status && i > iMax ) {
			iMax = i;
			sMax = curr;
		}
		curr=curr->next;
	}
	*foundMax=iMax;
	return sMax;
}

char *slicesDump(slices_t *slices, address_t *blockSize, unsigned int charCount, address_t begin, address_t end) {
	slice_t *curr = slices->first;
	address_t sb,se,i;
	char *dump, ci;

	// If blockSize is 0, try to autodetect to display entire slice chain
	if (*blockSize == 0) {
		*blockSize=(end-begin+1)/(charCount-1);
		// If we have a too big zoom factor, draw it at 1:1 scale
		if (*blockSize==0) *blockSize=1;
	}

	dump = malloc(charCount+1);
	if (dump==NULL) { return NULL; }
	memset(dump, ' ', charCount);
	dump[charCount]=0;

	while (curr != NULL) {
		sb=curr->begin / *blockSize; //FIXME : gérer le max également !
/*
if ( curr->end / *blockSize > charCount -1 )  {
	printf("\nBUG : end/blkSze==%lli, charCount==%i\n", curr->end / *blockSize,charCount-1);
}
*/
		se=min(curr->end / *blockSize,charCount-1);

		switch (curr->status) {
			case S_UNKNOWN:		ci='_'; break;
			case S_UNREADABLE:	ci='!'; break;
			case S_RECOVERED:	ci='.'; break;
			default:		ci='~'; break;
		}

		for (i=sb;i<=se;i++) {
			if (dump[i] == ' ' ) {
				// This is a new information
				dump[i]=ci;
			} else if ( dump[i] == ci || dump[i] == '!' ) {
				// Already the right information or error, don't modify
			} else {
				// Multiple information on the same character
				dump[i]='#';
			}
		}

		curr=curr->next;
	}

	return dump;
}