aboutsummaryrefslogtreecommitdiffstats
path: root/memory.c
blob: c721268530345356b99f4500ffac079c4eaef390 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


char *
get_string(pid_t pid, unsigned long int addr, size_t *lenp, const char **errorp)
{
	struct iovec inv, outv;
	size_t off = 0, size = 0, page_off, read_size;
	char *out = NULL, *in = (char *)addr, *p;
	page_off = (size_t)addr % sizeof(PAGE_SIZE);
	read_size = PAGE_SIZE - page_off;
	*errorp = NULL;
	for (;; read_size = PAGE_SIZE) {
		out = realloc(out, size + PAGE_SIZE);
		if (!out)
			eprintf("realloc:");
		inv.iov_base  = &in[off];
		inv.iov_len   = read_size;
		outv.iov_base = &out[off];
		outv.iov_len  = read_size;
		if (process_vm_readv(pid, &outv, 1, &inv, 1, 0) != (ssize_t)read_size) {
			*errorp = errno == EFAULT ? "<invalid address>" : "<an error occured during reading of string>";
			*lenp = 0;
			free(out);
			return 0;
		}
		p = memchr(&out[off], 0, read_size);
		if (p) {
			*lenp = (size_t)(p - out);
			return out;
		}
		off += read_size;
	}
}


int
get_struct(pid_t pid, unsigned long int addr, void *out, size_t size, const char **errorp)
{
	struct iovec inv, outv;
	*errorp = NULL;
	inv.iov_base  = (void *)addr;
	inv.iov_len   = size;
	outv.iov_base = out;
	outv.iov_len  = size;
	if (process_vm_readv(pid, &outv, 1, &inv, 1, 0) == (ssize_t)size)
		return 0;
	*errorp = errno == EFAULT ? "<invalid address>" : "<an error occured during reading of string>";
	return -1;
}


char *
get_memory(pid_t pid, unsigned long int addr, size_t n, const char **errorp)
{
	char *out = malloc(n + (size_t)!n);
	if (!out)
		eprintf("malloc:");
	if (get_struct(pid, addr, out, n, errorp)) {
		free(out);
		return NULL;
	}
	return out;
}



static void
add_char(char **strp, size_t *sizep, size_t *lenp, char c)
{
	if (*lenp == *sizep) {
		*strp = realloc(*strp, *sizep += 128);
		if (!*strp)
			eprintf("realloc:");
	}
	(*strp)[(*lenp)++] = c;
}


static size_t
utf8len(char *str)
{
	size_t ext, i, len;
	uint32_t code;
	uint8_t *s = (uint8_t *)str;

	struct {
		uint8_t  lower;
		uint8_t  upper;
		uint8_t  mask;
		uint32_t lowest;
	} lookup[] = {
		{ 0x00, 0x7F, 0x7F, UINT32_C(0x000000) },
		{ 0xC0, 0xDF, 0x1F, UINT32_C(0x000080) },
		{ 0xE0, 0xEF, 0x0F, UINT32_C(0x000800) },
		{ 0xF0, 0xF7, 0x07, UINT32_C(0x010000) }
	};

	for (ext = 0; ext < sizeof(lookup) / sizeof(*lookup); ext++)
		if (lookup[ext].lower <= s[0] && s[0] <= lookup[ext].upper)
			goto found;
	return 0;

found:
	code = s[0] & lookup[ext].mask;
	len = ext + 1;
	for (i = 1; i < len; i++) {
		if ((s[i] & 0xC0) != 0x80)
			return 0;
		code = (code << 6) | (s[i] ^ 0x80);
	}

	if (code < lookup[ext].lowest || (0xD800 <= code && code <= 0xDFFF) || code > UINT32_C(0x10FFFF))
		return 0;
	return len;
}


char *
escape_memory(char *str, size_t m)
{
	char *ret = NULL, *s, *end;
	size_t size = 0;
	size_t len = 0;
	size_t n = 0;
	int need_new_string = 0;
	if (!str) {
		str = strdup("NULL");
		if (!str)
			eprintf("strdup:");
		return str;
	}
	add_char(&ret, &size, &len, '"');
	for (s = str, end = &str[m]; s != end; s++) {
		if (n) {
			add_char(&ret, &size, &len, *s);
			n -= 1;
		} else if (*s == '\r') {
			add_char(&ret, &size, &len, '\\');
			add_char(&ret, &size, &len, 'r');
		} else if (*s == '\t') {
			add_char(&ret, &size, &len, '\\');
			add_char(&ret, &size, &len, 't');
		} else if (*s == '\a') {
			add_char(&ret, &size, &len, '\\');
			add_char(&ret, &size, &len, 'a');
		} else if (*s == '\f') {
			add_char(&ret, &size, &len, '\\');
			add_char(&ret, &size, &len, 'f');
		} else if (*s == '\v') {
			add_char(&ret, &size, &len, '\\');
			add_char(&ret, &size, &len, 'v');
		} else if (*s == '\b') {
			add_char(&ret, &size, &len, '\\');
			add_char(&ret, &size, &len, 'b');
		} else if (*s == '\n') {
			add_char(&ret, &size, &len, '\\');
			add_char(&ret, &size, &len, 'n');
		} else if (*s == '\"') {
			add_char(&ret, &size, &len, '\\');
			add_char(&ret, &size, &len, '"');
		} else if (*s < ' ' || *s >= 127) {
			n = utf8len(s);
			if (n > 1) {
				add_char(&ret, &size, &len, *s);
				n -= 1;
			} else {
				n = 0;
				add_char(&ret, &size, &len, '\\');
				add_char(&ret, &size, &len, 'x');
				add_char(&ret, &size, &len, "0123456789abcdef"[(int)*(unsigned char *)s >> 4]);
				add_char(&ret, &size, &len, "0123456789abcdef"[(int)*(unsigned char *)s & 15]);
				need_new_string = 1;
				continue;
			}
		} else {
			if (need_new_string && isxdigit(*s)) {
				add_char(&ret, &size, &len, '"');
				add_char(&ret, &size, &len, '"');
			}
			add_char(&ret, &size, &len, *s);
		}
		need_new_string = 0;
	}
	add_char(&ret, &size, &len, '"');
	add_char(&ret, &size, &len, '\0');
	free(str);
	return ret;
}