-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreturn_formatter.c
240 lines (225 loc) · 7.58 KB
/
return_formatter.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "return_formatter.h"
#include "types_and_utils.h"
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "main.h"
void print_char_with_escape(char c) {
switch (c) {
case '\0':
printf("\\0");
break;
case '\n':
printf("\\n");
break;
case '\r':
printf("\\r");
break;
case '\t':
printf("\\t");
break;
case '\\':
printf("\\\\");
break;
default:
if (isprint(c)) {
printf("%c", c);
} else {
printf("\\x%02x", c);
}
break;
}
}
void print_char_buffer(const char *buffer, size_t length) {
for (size_t i = 0; i < length; i++) {
unsigned char c = buffer[i];
print_char_with_escape(c);
}
}
void hexdump(const void *data, size_t size) {
const unsigned char *byte = (const unsigned char *)data;
size_t i, j;
bool multiline = size > 16;
if (multiline) printf("(Hexvalue)\nOffset\n");
for (i = 0; i < size; i += 16) {
if (multiline) printf("%08zx ", i); // Offset
// Hex bytes
for (j = 0; j < 16; j++) {
if (j==8) printf(" "); // Add space between the two halves of the hexdump
if (i + j < size) {
printf("%02x ", byte[i + j]);
} else {
if (multiline) printf(" "); // Fill space if less than 16 bytes in the line
}
}
if (multiline) printf(" ");
if (!multiline) printf("= ");
// ASCII characters
for (j = 0; j < 16; j++) {
if (i + j < size) {
printf("%c", isprint(byte[i + j]) ? byte[i + j] : '.');
}
}
printf("\n");
}
}
void print_arg_value(const void* value, ArgType type, size_t offset, int pointer_depth) {
if (pointer_depth > 0) {
value = value + offset * sizeof(void*);
for (int j = 0; j < pointer_depth; j++) {
value = *(void**)value;
if (value == NULL) {
printf("(NULL pointer)");
return;
}
}
offset = 0;
}
//bugfix for architectures that throw a bus error when trying to read from an unaligned address
void* alligned_copy = NULL;
if (type!=TYPE_VOID){
size_t size = typeToSize(type, 0);
if (size == 0) {
fprintf(stderr, "Size of type %s is 0\n", typeToString(type));
exit_or_restart(1);
}
if ((size_t)value % size != 0) {
alligned_copy = malloc(size);
memcpy(alligned_copy, value + offset * size, size);
value = alligned_copy;
offset = 0;
}
}
switch (type) {
case TYPE_CHAR:
print_char_with_escape(((char*)value)[offset]);
break;
case TYPE_SHORT:
printf("%hd", ((short*)value)[offset]);
break;
case TYPE_INT:
printf("%d", ((int*)value)[offset]);
break;
case TYPE_LONG:
printf("%ld", ((long*)value)[offset]);
break;
case TYPE_UCHAR:
printf("%u", ((unsigned char*)value)[offset]);
break;
case TYPE_USHORT:
printf("%hu", ((unsigned short*)value)[offset]);
break;
case TYPE_UINT:
printf("%u", ((unsigned int*)value)[offset]);
break;
case TYPE_ULONG:
printf("%lu", ((unsigned long*)value)[offset]);
break;
case TYPE_FLOAT:
printf("%f", ((float*)value)[offset]);
break;
case TYPE_DOUBLE:
printf("%lf", ((double*)value)[offset]);
break;
case TYPE_STRING:
printf("\"");
print_char_buffer(((char**)value)[offset], strlen(((char**)value)[offset]));
printf("\"");
break;
case TYPE_VOIDPOINTER:
// #define HEX_DIGITS (int)(2 * sizeof(void*))
// printf("0x%0*" PRIxPTR, HEX_DIGITS,(uintptr_t)((void**)value)[offset]);
printf("0x%" PRIxPTR, (uintptr_t)((void**)value)[offset]);
break;
case TYPE_POINTER:
fprintf(stderr, "Should not be printing pointer values directly");
exit_or_restart(1);
case TYPE_VOID:
printf("(void)");
break;
case TYPE_STRUCT:
fprintf(stderr, "Should not be printing struct values directly");
exit_or_restart(1);
default:
printf("Unsupported type");
break;
}
if (alligned_copy != NULL) {
free(alligned_copy);
}
}
void format_and_print_arg_type(const ArgInfo* arg) {
if (!arg->is_array) {
printf("%s", typeToString(arg->type));
for (int i = 0; i < arg->pointer_depth; i++) {
printf("*");
}} else { // is an array
printf("%s ", typeToString(arg->type));
for (int i = 0; i < arg->array_value_pointer_depth; i++) {
printf("*");
}
if (arg->pointer_depth > 0) {
printf("(");
for (int i = 0; i < arg->pointer_depth; i++) {
printf("*");
}
printf(")");
}
printf("[%zu]", get_size_for_arginfo_sized_array(arg));
}
}
//this contains an optional override for the value, which is used for printing struct fields
void format_and_print_arg_value(const ArgInfo* arg) { //, char* buffer, size_t buffer_size) {
const void* value;
value = arg->value;
if (arg->pointer_depth > 0) {
for (int j = 0; j < arg->pointer_depth; j++) {
value = *(void**)value;
if (arg->type!=TYPE_STRUCT && value == NULL) {
printf("(NULL pointer)");
return;
}
}
}
if (arg->type==TYPE_STRUCT){
printf("{ ");
StructInfo* struct_info = arg->struct_info;
for (int i = 0; i < struct_info->info.arg_count; i++) {
format_and_print_arg_type(struct_info->info.args[i]);
printf(" ");
// void* override = struct_info->value_ptrs==NULL ? NULL : struct_info->value_ptrs[i];
format_and_print_arg_value(struct_info->info.args[i]);
if (i < struct_info->info.arg_count - 1) {
printf(", ");
}
}
printf(" }");
}
else if (!arg->is_array) {
print_arg_value(value, arg->type, 0, 0);
}
else { // is an array
value = *(void**)value; // because arrays are stored as pointers
size_t array_size = get_size_for_arginfo_sized_array(arg);
if(arg->array_value_pointer_depth==0){
if (arg->type == TYPE_CHAR){
print_char_buffer((const char*)value, array_size);
// print it as a string, with escape characters
} else if (arg->type == TYPE_UCHAR) {
hexdump(value, array_size);
} else goto print_regular;
} else print_regular: {
printf("{ ");
for (size_t i = 0; i < array_size; i++) {
print_arg_value(value, arg->type,i, arg->array_value_pointer_depth);
if (i < array_size - 1) {
printf(", ");
}
}
printf(" }");
}
}
}