serializer.cpp 4.11 KB
Newer Older
1
2
3
4
5
/* serializer.cpp -- low-level serializer for pimpconv
 * Copyright (C) 2005-2006 Jrn Nystad and Erik Faye-Lund
 * For conditions of distribution and use, see copyright notice in LICENSE.TXT
 */

Erik Faye-Lund's avatar
Erik Faye-Lund committed
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
#include "serializer.h"

#include <stdio.h>
#include <string.h>

#include <assert.h>
#define ASSERT assert

#if 0
#define TRACE() printf("AT %s:%d\n", __FILE__, __LINE__);
#else
#define TRACE()
#endif

#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif

void serializer_init(struct serializer *s)
{
	ASSERT(NULL != s);
	TRACE();

	s->data = NULL;
	s->buffer_size = 0;
	s->pos = 0;
32
33
34

	s->relocs = NULL;
	s->num_relocs = 0;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
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
}

void serializer_deinit(struct serializer *s)
{
	ASSERT(NULL != s);
	TRACE();
	
	if (NULL != s->data)
	{
		free(s->data);
		s->data = NULL;
	}
}

void serializer_check_size(struct serializer *s, size_t needed_size)
{
	ASSERT(NULL != s);
	TRACE();
	
	if (s->buffer_size < (s->pos + needed_size))
	{
		int old_size = s->buffer_size;
		TRACE();
		s->buffer_size = MAX(s->buffer_size * 2, s->buffer_size + needed_size);

		s->data = (unsigned char*)realloc(s->data, s->buffer_size);
		
		if (NULL == s->data)
		{
Erik Faye-Lund's avatar
Erik Faye-Lund committed
64
65
			fputs("out of memory\n", stderr);
			abort();
Erik Faye-Lund's avatar
Erik Faye-Lund committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
		}
		
		memset(&s->data[old_size], 0, (s->buffer_size - old_size));
	}
}

void serializer_align(struct serializer *s, int alignment)
{
	ASSERT(NULL != s);
	TRACE();
	
	if ((s->pos % alignment) != 0)
	{
		int align_amt = alignment - (s->pos % alignment);
		serializer_check_size(s, align_amt);
		s->pos += align_amt;
	}
}

Erik Faye-Lund's avatar
Erik Faye-Lund committed
85
void serialize_byte(struct serializer *s, uint8_t b)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
86
87
88
89
90
91
92
93
94
95
96
{
	ASSERT(NULL != s);
	TRACE();
	
	/* no alignment needed, make room for new data */
	serializer_check_size(s, 1);
	
	ASSERT(s->pos < s->buffer_size);
	s->data[s->pos++] = b;
}

Erik Faye-Lund's avatar
Erik Faye-Lund committed
97
void serialize_halfword(struct serializer *s, uint16_t h)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
98
99
100
101
102
103
104
105
106
107
108
109
110
{
	ASSERT(NULL != s);
	TRACE();
	
	/* align and make room for new data */
	serializer_align(s, 2);
	serializer_check_size(s, 2);
	
	/* write data (little endian) */
	s->data[s->pos++] = (unsigned char)(h >> 0);
	s->data[s->pos++] = (unsigned char)(h >> 8);
}

Erik Faye-Lund's avatar
Erik Faye-Lund committed
111
void serialize_word(struct serializer *s, uint32_t w)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
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
{
	ASSERT(NULL != s);
	TRACE();
	
	/* align and make room for new data */
	serializer_align(s, 4);
	serializer_check_size(s, 4);

	/* write data (little endian) */
	s->data[s->pos++] = (unsigned char)(w >> 0);
	s->data[s->pos++] = (unsigned char)(w >> 8);
	s->data[s->pos++] = (unsigned char)(w >> 16);
	s->data[s->pos++] = (unsigned char)(w >> 24);
}

/* why on earth does this routine support zero len? */
void serialize_string(struct serializer *s, const char *str, const size_t len)
{
	size_t real_len, slen;

	ASSERT(NULL != s);
	TRACE();
	
	/* no alignment needed */
	serializer_check_size(s, len);

	/* determine length (min of slen and len) */
	slen = strlen(str) + 1;
	if (len > 0) real_len = (len > slen) ? slen : len;
	else real_len = slen;
	
	for (int i = 0; i < real_len; ++i)
	{
		s->data[s->pos + i] = str[i];
	}
	
	if (len > 0) s->pos += len;
	else s->pos += slen;
}

void serialize_pointer(struct serializer *s, void *ptr)
{
	ASSERT(NULL != s);
	TRACE();
	
	serializer_align(s, 4);
	serializer_check_size(s, 4);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

	if (!ptr) {
		serialize_word(s, 0);
		return;
	}

	s->relocs = (struct reloc *)realloc(s->relocs, sizeof(struct reloc) * (s->num_relocs + 1));
	if (!s->relocs) {
		fputs("out of memory\n", stderr);
		abort();
	}

	s->relocs[s->num_relocs].pos = s->pos;
	s->relocs[s->num_relocs].ptr = ptr;
	s->num_relocs++;

	serialize_word(s, 0xdeadbeef);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
176
177
178
179
180
181
}

void serializer_set_pointer(struct serializer *s, void *ptr, int pos)
{
	ASSERT(NULL != s);
	TRACE();
182
183
184
185
186
187
188
189
190
191
192
193
194
195

	for (int i = 0; i < s->num_relocs; ++i) {
		unsigned int *target;

		if (s->relocs[i].ptr != ptr)
			continue;

		ASSERT(s->relocs[i].pos & 3 == 0); /* location muet be word-aligned */

		target = (unsigned int*)(s->data + s->relocs[i].pos);
		ASSERT(*target == 0xdeadbeef);

		*target = pos - s->relocs[i].pos;
	}
Erik Faye-Lund's avatar
Erik Faye-Lund committed
196
197
198
199
200
201
202
}


void serializer_fixup_pointers(struct serializer *s)
{
	ASSERT(NULL != s);
	TRACE();
203
204
205
206
207
208
209

	for (int i = 0; i < s->num_relocs; ++i) {
		unsigned int *target = (unsigned int*)(s->data + s->relocs[i].pos);
		if (*target == 0xdeadbeef) {
			fputs("reloc not fixed up!\n", stderr);
			abort();
		}
Erik Faye-Lund's avatar
Erik Faye-Lund committed
210
211
	}
}