~phablet-team/ofono/ofono-mwi-and-bugs

« back to all changes in this revision

Viewing changes to gril/parcel.c

  • Committer: Ricardo Salveti de Araujo
  • Date: 2013-10-02 15:57:27 UTC
  • mfrom: (6830.1.2)
  • Revision ID: ricardo.salveti@canonical.com-20131002155727-m181rp9jnkzmb4fo
Merging head of lp:~phablet-team/ofono/rilmodem, containing all the rilmodem related changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Joel Armstrong <jcarmst@sandia.gov>
 
3
 * Copyright (C) 2012 Canonical Ltd.
 
4
 *
 
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.
 
9
 
 
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.
 
14
 *
 
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.
 
18
 *
 
19
 * Based on parcel implementation from https://bitbucket.org/floren/inferno
 
20
 *
 
21
 */
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include <config.h>
 
25
#endif
 
26
 
 
27
#define _GNU_SOURCE
 
28
#include <stdio.h>
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include <errno.h>
 
32
 
 
33
#include <glib.h>
 
34
 
 
35
#include <ofono/log.h>
 
36
 
 
37
/* Parcel-handling code */
 
38
#include <sys/types.h>
 
39
#include <string.h>
 
40
#include <endian.h>
 
41
#include <stdint.h>
 
42
#include <limits.h>
 
43
 
 
44
#include "parcel.h"
 
45
 
 
46
#define PAD_SIZE(s) (((s)+3)&~3)
 
47
 
 
48
typedef uint16_t char16_t;
 
49
 
 
50
void parcel_init(struct parcel *p)
 
51
{
 
52
        p->data = g_malloc0(sizeof(int32_t));
 
53
        p->size = 0;
 
54
        p->capacity = sizeof(int32_t);
 
55
        p->offset = 0;
 
56
}
 
57
 
 
58
void parcel_grow(struct parcel *p, size_t size)
 
59
{
 
60
        char *new = g_realloc(p->data, p->capacity + size);
 
61
        p->data = new;
 
62
        p->capacity += size;
 
63
}
 
64
 
 
65
void parcel_free(struct parcel *p)
 
66
{
 
67
        g_free(p->data);
 
68
        p->size = 0;
 
69
        p->capacity = 0;
 
70
        p->offset = 0;
 
71
}
 
72
 
 
73
int32_t parcel_r_int32(struct parcel *p)
 
74
{
 
75
        int32_t ret;
 
76
        ret = *((int32_t *) (p->data + p->offset));
 
77
        p->offset += sizeof(int32_t);
 
78
        return ret;
 
79
}
 
80
 
 
81
int parcel_w_int32(struct parcel *p, int32_t val)
 
82
{
 
83
        for (;;) {
 
84
 
 
85
                DBG("parcel_w_int32(%d): offset = %d, cap = %d, size = %d\n",
 
86
                    val, (int) p->offset, (int) p->capacity, (int) p->size);
 
87
 
 
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);
 
93
                        break;
 
94
                } else {
 
95
                        /* Grow data and retry */
 
96
                        parcel_grow(p, sizeof(int32_t));
 
97
                }
 
98
        }
 
99
        return 0;
 
100
}
 
101
 
 
102
int parcel_w_string(struct parcel *p, char *str)
 
103
{
 
104
        gunichar2 *gs16;
 
105
        glong gs16_len;
 
106
        size_t len;
 
107
        size_t gs16_size;
 
108
 
 
109
        if (str == NULL) {
 
110
                parcel_w_int32(p, -1);
 
111
                return 0;
 
112
        }
 
113
 
 
114
        gs16 = g_utf8_to_utf16(str, -1, NULL, &gs16_len, NULL);
 
115
 
 
116
        if (parcel_w_int32(p, gs16_len) == -1) {
 
117
                return -1;
 
118
        }
 
119
 
 
120
        gs16_size = gs16_len * sizeof(char16_t);
 
121
        len = gs16_size + sizeof(char16_t);
 
122
        for (;;) {
 
123
                size_t padded = PAD_SIZE(len);
 
124
 
 
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;
 
131
                        p->offset += padded;
 
132
                        p->size += padded;
 
133
                        if (padded != len) {
 
134
 
 
135
                                DBG("Writing %d bytes, padded to %d\n",
 
136
                                    (int) len, (int) padded);
 
137
 
 
138
#if BYTE_ORDER == BIG_ENDIAN
 
139
                                static const uint32_t mask[4] = {
 
140
                                        0x00000000, 0xffffff00,
 
141
                                        0xffff0000, 0xff000000
 
142
                                };
 
143
#endif
 
144
#if BYTE_ORDER == LITTLE_ENDIAN
 
145
                                static const uint32_t mask[4] = {
 
146
                                        0x00000000, 0x00ffffff,
 
147
                                        0x0000ffff, 0x000000ff
 
148
                                };
 
149
#endif
 
150
                                *((uint32_t*)(p->data + p->offset - 4)) &=
 
151
                                                        mask[padded - len];
 
152
                        }
 
153
                        break;
 
154
 
 
155
                } else {
 
156
                        /* Grow data and retry */
 
157
                        parcel_grow(p, padded);
 
158
                }
 
159
        }
 
160
 
 
161
        g_free(gs16);
 
162
        return 0;
 
163
}
 
164
 
 
165
char* parcel_r_string(struct parcel *p)
 
166
{
 
167
        char *ret;
 
168
        int len16 = parcel_r_int32(p);
 
169
 
 
170
        /* This is how a null string is sent */
 
171
        if (len16 < 0)
 
172
                return NULL;
 
173
 
 
174
        ret = g_utf16_to_utf8((gunichar2 *) (p->data + p->offset),
 
175
                                len16, NULL, NULL, NULL);
 
176
        if (ret == NULL)
 
177
                return NULL;
 
178
 
 
179
        p->offset += PAD_SIZE((len16 + 1) * sizeof(char16_t));
 
180
 
 
181
        return ret;
 
182
}
 
183
 
 
184
size_t parcel_data_avail(struct parcel *p)
 
185
{
 
186
        return (p->size - p->offset);
 
187
}