2
* Copyright (C) 2011 Joel Armstrong <jcarmst@sandia.gov>
3
* Copyright (C) 2012 Canonical Ltd.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License (`GPL') as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
* Based on parcel implementation from https://bitbucket.org/floren/inferno
35
#include <ofono/log.h>
37
/* Parcel-handling code */
38
#include <sys/types.h>
46
#define PAD_SIZE(s) (((s)+3)&~3)
48
typedef uint16_t char16_t;
50
void parcel_init(struct parcel *p)
52
p->data = g_malloc0(sizeof(int32_t));
54
p->capacity = sizeof(int32_t);
58
void parcel_grow(struct parcel *p, size_t size)
60
char *new = g_realloc(p->data, p->capacity + size);
65
void parcel_free(struct parcel *p)
73
int32_t parcel_r_int32(struct parcel *p)
76
ret = *((int32_t *) (p->data + p->offset));
77
p->offset += sizeof(int32_t);
81
int parcel_w_int32(struct parcel *p, int32_t val)
85
DBG("parcel_w_int32(%d): offset = %d, cap = %d, size = %d\n",
86
val, (int) p->offset, (int) p->capacity, (int) p->size);
88
if (p->offset + sizeof(int32_t) < p->capacity) {
89
/* There's enough space */
90
*((int32_t *) (p->data + p->offset)) = val;
91
p->offset += sizeof(int32_t);
92
p->size += sizeof(int32_t);
95
/* Grow data and retry */
96
parcel_grow(p, sizeof(int32_t));
102
int parcel_w_string(struct parcel *p, char *str)
110
parcel_w_int32(p, -1);
114
gs16 = g_utf8_to_utf16(str, -1, NULL, &gs16_len, NULL);
116
if (parcel_w_int32(p, gs16_len) == -1) {
120
gs16_size = gs16_len * sizeof(char16_t);
121
len = gs16_size + sizeof(char16_t);
123
size_t padded = PAD_SIZE(len);
125
DBG("parcel_w_string(\"%s\"): len %d offset %d, cap %d, size %d",
126
str, (int) len, (int) p->offset, (int) p->capacity, (int) p->size);
127
if (p->offset + len < p->capacity) {
128
/* There's enough space */
129
memcpy(p->data + p->offset, gs16, gs16_size);
130
*((char16_t *) (p->data + p->offset + gs16_size)) = 0;
135
DBG("Writing %d bytes, padded to %d\n",
136
(int) len, (int) padded);
138
#if BYTE_ORDER == BIG_ENDIAN
139
static const uint32_t mask[4] = {
140
0x00000000, 0xffffff00,
141
0xffff0000, 0xff000000
144
#if BYTE_ORDER == LITTLE_ENDIAN
145
static const uint32_t mask[4] = {
146
0x00000000, 0x00ffffff,
147
0x0000ffff, 0x000000ff
150
*((uint32_t*)(p->data + p->offset - 4)) &=
156
/* Grow data and retry */
157
parcel_grow(p, padded);
165
char* parcel_r_string(struct parcel *p)
168
int len16 = parcel_r_int32(p);
170
/* This is how a null string is sent */
174
ret = g_utf16_to_utf8((gunichar2 *) (p->data + p->offset),
175
len16, NULL, NULL, NULL);
179
p->offset += PAD_SIZE((len16 + 1) * sizeof(char16_t));
184
size_t parcel_data_avail(struct parcel *p)
186
return (p->size - p->offset);