serializer.c 3.86 KB
Newer Older
Erik Faye-Lund's avatar
Erik Faye-Lund committed
1
/* serializer.c -- low-level serializer for pimpconv
2
3
4
5
 * 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
#include "serializer.h"

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

#include <assert.h>
#define ASSERT assert

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

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

	s->data = NULL;
	s->buffer_size = 0;
	s->pos = 0;
25
26
27

	s->relocs = NULL;
	s->num_relocs = 0;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
}

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

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

Erik Faye-Lund's avatar
Erik Faye-Lund committed
50
		s->data = realloc(s->data, s->buffer_size);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
51
52
53
		
		if (NULL == s->data)
		{
Erik Faye-Lund's avatar
Erik Faye-Lund committed
54
55
			fputs("out of memory\n", stderr);
			abort();
Erik Faye-Lund's avatar
Erik Faye-Lund committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
		}
		
		memset(&s->data[old_size], 0, (s->buffer_size - old_size));
	}
}

void serializer_align(struct serializer *s, int alignment)
{
	ASSERT(NULL != s);
	
	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
74
void serialize_byte(struct serializer *s, uint8_t b)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
75
76
77
78
79
80
81
82
83
84
{
	ASSERT(NULL != s);
	
	/* 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
85
void serialize_halfword(struct serializer *s, uint16_t h)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
86
87
88
89
90
91
92
93
94
95
96
97
{
	ASSERT(NULL != s);
	
	/* 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
98
void serialize_word(struct serializer *s, uint32_t w)
Erik Faye-Lund's avatar
Erik Faye-Lund committed
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
{
	ASSERT(NULL != s);
	
	/* 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);
	
	/* 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);
	
	serializer_align(s, 4);
	serializer_check_size(s, 4);
143
144
145
146
147
148

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

Erik Faye-Lund's avatar
Erik Faye-Lund committed
149
	s->relocs = realloc(s->relocs, sizeof(struct reloc) * (s->num_relocs + 1));
150
151
152
153
154
155
156
157
158
159
	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
160
161
162
163
164
}

void serializer_set_pointer(struct serializer *s, void *ptr, int pos)
{
	ASSERT(NULL != s);
165
166
167
168
169
170
171
172
173
174
175
176
177
178

	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
179
180
181
182
183
184
}


void serializer_fixup_pointers(struct serializer *s)
{
	ASSERT(NULL != s);
185
186
187
188
189
190
191

	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
192
193
	}
}