summaryrefslogtreecommitdiffstats
path: root/gril/parcel.c
blob: 1cfc68a51d11fdc2213f728693bd37ffe190ce90 (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright (C) 2011 Joel Armstrong <jcarmst@sandia.gov>
 * Copyright (C) 2012 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (`GPL') as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Based on parcel implementation from https://bitbucket.org/floren/inferno
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <glib.h>

#include <ofono/log.h>

/* Parcel-handling code */
#include <sys/types.h>
#include <string.h>
#include <endian.h>
#include <stdint.h>
#include <limits.h>

#include "parcel.h"

#define PAD_SIZE(s) (((s)+3)&~3)

typedef uint16_t char16_t;

void parcel_init(struct parcel *p)
{
	p->data = g_malloc0(sizeof(int32_t));
	p->size = 0;
	p->capacity = sizeof(int32_t);
	p->offset = 0;
	p->malformed = 0;
}

void parcel_grow(struct parcel *p, size_t size)
{
	char *new = g_realloc(p->data, p->capacity + size);
	p->data = new;
	p->capacity += size;
}

void parcel_free(struct parcel *p)
{
	g_free(p->data);
	p->size = 0;
	p->capacity = 0;
	p->offset = 0;
}

int32_t parcel_r_int32(struct parcel *p)
{
	int32_t ret;

	if (p->malformed)
		return 0;

	if (p->offset + sizeof(int32_t) > p->size) {
		ofono_error("%s: parcel is too small", __func__);
		p->malformed = 1;
		return 0;
	}

	ret = *((int32_t *) (void *) (p->data + p->offset));
	p->offset += sizeof(int32_t);
	return ret;
}

int parcel_w_int32(struct parcel *p, int32_t val)
{
	for (;;) {

		if (p->offset + sizeof(int32_t) < p->capacity) {
			/* There's enough space */
			*((int32_t *) (void *) (p->data + p->offset)) = val;
			p->offset += sizeof(int32_t);
			p->size += sizeof(int32_t);
			break;
		} else {
			/* Grow data and retry */
			parcel_grow(p, sizeof(int32_t));
		}
	}
	return 0;
}

int parcel_w_string(struct parcel *p, const char *str)
{
	gunichar2 *gs16;
	glong gs16_len;
	size_t len;
	size_t gs16_size;

	if (str == NULL) {
		parcel_w_int32(p, -1);
		return 0;
	}

	gs16 = g_utf8_to_utf16(str, -1, NULL, &gs16_len, NULL);

	if (parcel_w_int32(p, gs16_len) == -1)
		return -1;

	gs16_size = gs16_len * sizeof(char16_t);
	len = gs16_size + sizeof(char16_t);
	for (;;) {
		size_t padded = PAD_SIZE(len);

		if (p->offset + len < p->capacity) {
			/* There's enough space */
			memcpy(p->data + p->offset, gs16, gs16_size);
			*((char16_t *) (void *)
				(p->data + p->offset + gs16_size)) = 0;
			p->offset += padded;
			p->size += padded;
			if (padded != len) {

#if BYTE_ORDER == BIG_ENDIAN
				static const uint32_t mask[4] = {
					0x00000000, 0xffffff00,
					0xffff0000, 0xff000000
				};
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
				static const uint32_t mask[4] = {
					0x00000000, 0x00ffffff,
					0x0000ffff, 0x000000ff
				};
#endif

				*((uint32_t *) (void *)
					(p->data + p->offset - 4)) &=
							mask[padded - len];
			}
			break;

		} else {
			/* Grow data and retry */
			parcel_grow(p, padded);
		}
	}

	g_free(gs16);
	return 0;
}

char *parcel_r_string(struct parcel *p)
{
	char *ret;
	int len16 = parcel_r_int32(p);
	int strbytes;

	if (p->malformed)
		return NULL;

	/* This is how a null string is sent */
	if (len16 < 0)
		return NULL;

	strbytes = PAD_SIZE((len16 + 1) * sizeof(char16_t));
	if (p->offset + strbytes > p->size) {
		ofono_error("%s: parcel is too small", __func__);
		p->malformed = 1;
		return NULL;
	}

	ret = g_utf16_to_utf8((gunichar2 *) (void *) (p->data + p->offset),
				len16, NULL, NULL, NULL);
	if (ret == NULL) {
		ofono_error("%s: wrong UTF16 coding", __func__);
		p->malformed = 1;
		return NULL;
	}

	p->offset += strbytes;

	return ret;
}

void parcel_skip_string(struct parcel *p)
{
	int len16 = parcel_r_int32(p);
	int strbytes;

	if (p->malformed)
		return;

	/* This is how a null string is sent */
	if (len16 < 0)
		return;

	strbytes = PAD_SIZE((len16 + 1) * sizeof(char16_t));
	if (p->offset + strbytes > p->size) {
		p->malformed = 1;
		return;
	}

	p->offset += strbytes;
}

int parcel_w_raw(struct parcel *p, const void *data, size_t len)
{
	if (data == NULL) {
		parcel_w_int32(p, -1);
		return 0;
	}

	parcel_w_int32(p, len);

	for (;;) {

		if (p->offset + len < p->capacity) {
			/* There's enough space */
			memcpy(p->data + p->offset, data, len);
			p->offset += len;
			p->size += len;
			break;
		} else {
			/* Grow data and retry */
			parcel_grow(p, len);
		}
	}
	return 0;
}

void *parcel_r_raw(struct parcel *p, int *len)
{
	char *ret;

	*len = parcel_r_int32(p);

	if (p->malformed || *len <= 0)
		return NULL;

	if (p->offset + *len > p->size) {
		ofono_error("%s: parcel is too small", __func__);
		p->malformed = 1;
		return NULL;
	}

	ret = g_try_malloc0(*len);
	if (ret == NULL) {
		ofono_error("%s: out of memory (%d bytes)", __func__, *len);
		return NULL;
	}

	memcpy(ret, p->data + p->offset, *len);
	p->offset += *len;

	return ret;
}

size_t parcel_data_avail(struct parcel *p)
{
	return p->size - p->offset;
}

char **parcel_r_strv(struct parcel *p)
{
	int i;
	int num_str = parcel_r_int32(p);
	char **strv;

	if (p->malformed || num_str <= 0)
		return NULL;

	strv = g_new0(char *, num_str + 1);

	for (i = 0; i < num_str; i++)
		strv[i] = parcel_r_string(p);

	if (p->malformed) {
		g_strfreev(strv);
		strv = NULL;
	}

	return strv;
}