-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathckpt_helper.h
213 lines (176 loc) · 5.91 KB
/
ckpt_helper.h
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#define MY_MAX_LEN 200
struct MemoryRegion
{
void *startAddr;
void *endAddr;
int isReadable;
int isWriteable;
int isExecutabl;
char location[150];
};
char* my_strconcat(char c1[], char c2[]);
char* getLine(int fd);
struct MemoryRegion* parseLineToMemoryRegion(char* lineToParse);
void* getAddressFromLine(char* lineToParse, int charactersRead, char delimeter);
int convertHexToLongLongInt(unsigned long long int *memoryAddressInLongLongInt, char* memoryAddressString);
void parseAndSetPermissions(char *lineToParse, int readFrom, struct MemoryRegion* memoryRegion);
char* getLine(int fileDescriptor){
int i=0;
char* lineToReturn = malloc( (sizeof(char) * (MY_MAX_LEN) ));
char value;
int bytesRead = 0;
while(1){
bytesRead = read(fileDescriptor, &value, 1);
//printf("bytesRead = %d", bytesRead);
if(bytesRead < 0){
lineToReturn[i] = '\0';
break;
}else{
lineToReturn[i++] = value;
}
if(bytesRead == 0 || value=='\n' || value=='\0'){
lineToReturn[i] = '\n';
if( value == -1){
lineToReturn[0] = '\0';
}
//printf("val at 0=%c\n", lineToReturn[0]);
break;
}
}
return lineToReturn;
}
struct MemoryRegion* parseLineToMemoryRegion(char *lineToParse){
struct MemoryRegion* memoryRegion = malloc(sizeof(struct MemoryRegion));
void *address;
int charactersRead = 0;
/*
int j=0;
//TODO: strange behavior. lineToParse[j] cannot be compared to \n as it reaches a gibberish character before
// therefore, comparing it to -1 instead to figure out the end of line;
for(j=0;lineToParse[j] > 0; j++){
printf("%c", lineToParse[j]);
}
printf("len=%d", j);
*/
if( (address = getAddressFromLine(lineToParse, charactersRead, '-')) != NULL ){
memoryRegion->startAddr = address;
}
while(lineToParse[charactersRead++] != '-');
//charactersRead += 1;
if( (address = getAddressFromLine(lineToParse, charactersRead, ' ')) != NULL ){
memoryRegion->endAddr = address;
}
while(lineToParse[charactersRead++] != ' ');
//charactersRead += 1;
parseAndSetPermissions(lineToParse, charactersRead, memoryRegion);
while(lineToParse[charactersRead++] != ' ');
charactersRead += 1;
int i=0;
char temp[150];
//printf("chars read=%d\n", charactersRead);
//TODO: same reason as mentioned in previous todo. :(
//printf("!!note this!!\n");
while(lineToParse[charactersRead] >0 ){
//printf("char is: %c and charactersRead=%d \n", lineToParse[charactersRead], charactersRead);
//printf("%c", lineToParse[charactersRead]);
temp[i++] = lineToParse[charactersRead++];
}
temp[i] = '\0';
//printf("\n");
//printf("location should be=%s\n", temp);
//memoryRegion->location = malloc(sizeof(char)*150);
strcpy(memoryRegion->location,temp);
//memoryRegion->location[] = temp;
//printf("but location now =%s\n", memoryRegion->location);
return memoryRegion;
}
void* getAddressFromLine(char *lineToParse, int charactersRead, char delimeter){
void *address = NULL;
char memoryAddressString[110];
unsigned long long int memoryAddress;
int i, converted;
//printf("charactersRead++=%d", ++*charactersRead);
if(lineToParse[charactersRead] == '\n'){
return address;
}
for(i=0; lineToParse[charactersRead] != delimeter; i++){
memoryAddressString[i] = lineToParse[charactersRead];
charactersRead = (charactersRead) + 1;
}
//printf("*charactersRead=%d", charactersRead);
memoryAddressString[i] = '\0';
//printf("Memory address: %s\n", memoryAddressString);
converted = convertHexToLongLongInt(&memoryAddress, memoryAddressString);
if(converted < 0){
printf("Failed to convert hex to int.\n");
return NULL;
}
address = (void*) memoryAddress;
//printf("address = %llu\n", (unsigned long long int) address);
return address;
}
int convertHexToLongLongInt(unsigned long long int* memoryAddressInLongLongInt, char* memoryAddressString){
int hexCharToIntTable[] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1, 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,-1,10,11,12,13,14,15,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
};
int success = -1;
int longLongIntArray[100];
int i,j;
unsigned long long int currentValue, calculatedValue, placeValue = 1;
calculatedValue = 0;
for(i = 0; memoryAddressString[i] != '\0'; i++){
//printf("ascii val at i= %d is = %d\n",i, (int)memoryAddressString[i]);
success = hexCharToIntTable[(int)memoryAddressString[i]];
if(success == -1){
return success;
}
longLongIntArray[i] = success;
}
for(j=i-1; j>=0; j--){
currentValue = longLongIntArray[j] * placeValue;
calculatedValue += currentValue;
placeValue *= 16;
}
*memoryAddressInLongLongInt = calculatedValue;
success = 1;
return success;
}
void parseAndSetPermissions(char *lineToParse, int readFrom, struct MemoryRegion* memoryRegion){
//printf("chu giri. read=%c, write=%c, exec=%c", lineToParse[readFrom], lineToParse[readFrom+1], lineToParse[readFrom+2]);
if(lineToParse[readFrom] == 'r'){
memoryRegion->isReadable = 1;
}else{
memoryRegion->isReadable = 0;
}
if(lineToParse[readFrom+1] == 'w'){
memoryRegion->isWriteable = 1;
}else{
memoryRegion->isWriteable = 0;
}
if(lineToParse[readFrom+2] == 'x'){
memoryRegion->isExecutabl = 1;
}else{
memoryRegion->isExecutabl = 0;
}
}
char* my_strconcat(char c1[], char c2[]){
int i, j;
i = j = 0;
char * temp;
while(c1[i] != '\0'){
i++;
}
while( (c1[i++] = c2[j++]) != '\0' );
temp = c1;
return temp;
}