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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/*
*
* OBEX library with GLib integration
*
* Copyright (C) 2012 Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "gobex-apparam.h"
#include "gobex-debug.h"
struct _GObexApparam {
GHashTable *tags;
};
struct apparam_tag {
guint8 id;
guint8 len;
union {
/* Data is stored in network order */
char string[0];
guint8 data[0];
guint8 u8;
guint16 u16;
guint32 u32;
guint64 u64;
} value;
} __attribute__ ((packed));
static struct apparam_tag *tag_new(guint8 id, guint8 len, const void *data)
{
struct apparam_tag *tag;
tag = g_malloc0(2 + len);
tag->id = id;
tag->len = len;
memcpy(tag->value.data, data, len);
return tag;
}
static GObexApparam *g_obex_apparam_new(void)
{
GObexApparam *apparam;
apparam = g_new0(GObexApparam, 1);
apparam->tags = g_hash_table_new_full(g_direct_hash, g_direct_equal,
NULL, g_free);
return apparam;
}
static struct apparam_tag *apparam_tag_decode(const void *data, gsize size,
gsize *parsed)
{
struct apparam_tag *tag;
const guint8 *ptr = data;
guint8 id;
guint8 len;
if (size < 2)
return NULL;
id = ptr[0];
len = ptr[1];
if (len > size - 2)
return NULL;
tag = tag_new(id, len, ptr + 2);
if (tag == NULL)
return NULL;
*parsed = 2 + tag->len;
return tag;
}
GObexApparam *g_obex_apparam_decode(const void *data, gsize size)
{
GObexApparam *apparam;
GHashTable *tags;
gsize count = 0;
if (size < 2)
return NULL;
apparam = g_obex_apparam_new();
tags = apparam->tags;
while (count < size) {
struct apparam_tag *tag;
gsize parsed;
guint id;
tag = apparam_tag_decode(data + count, size - count, &parsed);
if (tag == NULL)
break;
id = tag->id;
g_hash_table_insert(tags, GUINT_TO_POINTER(id), tag);
count += parsed;
}
if (count != size) {
g_obex_apparam_free(apparam);
return NULL;
}
return apparam;
}
static gssize tag_encode(struct apparam_tag *tag, void *buf, gsize len)
{
gsize count = 2 + tag->len;
if (len < count)
return -ENOBUFS;
memcpy(buf, tag, count);
return count;
}
gssize g_obex_apparam_encode(GObexApparam *apparam, void *buf, gsize len)
{
gsize count = 0;
gssize ret;
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init(&iter, apparam->tags);
while (g_hash_table_iter_next(&iter, &key, &value)) {
struct apparam_tag *tag = value;
ret = tag_encode(tag, buf + count, len - count);
if (ret < 0)
return ret;
count += ret;
}
return count;
}
GObexApparam *g_obex_apparam_set_bytes(GObexApparam *apparam, guint8 id,
const void *value, gsize len)
{
struct apparam_tag *tag;
guint uid = id;
if (apparam == NULL)
apparam = g_obex_apparam_new();
tag = tag_new(id, len, value);
g_hash_table_replace(apparam->tags, GUINT_TO_POINTER(uid), tag);
return apparam;
}
GObexApparam *g_obex_apparam_set_uint8(GObexApparam *apparam, guint8 id,
guint8 value)
{
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %u", id, value);
return g_obex_apparam_set_bytes(apparam, id, &value, 1);
}
GObexApparam *g_obex_apparam_set_uint16(GObexApparam *apparam, guint8 id,
guint16 value)
{
guint16 num = g_htons(value);
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %u", id, value);
return g_obex_apparam_set_bytes(apparam, id, &num, 2);
}
GObexApparam *g_obex_apparam_set_uint32(GObexApparam *apparam, guint8 id,
guint32 value)
{
guint32 num = g_htonl(value);
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %u", id, value);
return g_obex_apparam_set_bytes(apparam, id, &num, 4);
}
GObexApparam *g_obex_apparam_set_uint64(GObexApparam *apparam, guint8 id,
guint64 value)
{
guint64 num = GUINT64_TO_BE(value);
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %"
G_GUINT64_FORMAT, id, value);
return g_obex_apparam_set_bytes(apparam, id, &num, 8);
}
GObexApparam *g_obex_apparam_set_string(GObexApparam *apparam, guint8 id,
const char *value)
{
gsize len;
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x value %s", id, value);
len = strlen(value) + 1;
if (len > G_MAXUINT8) {
((char *) value)[G_MAXUINT8 - 1] = '\0';
len = G_MAXUINT8;
}
return g_obex_apparam_set_bytes(apparam, id, value, len);
}
static struct apparam_tag *g_obex_apparam_find_tag(GObexApparam *apparam,
guint id)
{
return g_hash_table_lookup(apparam->tags, GUINT_TO_POINTER(id));
}
gboolean g_obex_apparam_get_uint8(GObexApparam *apparam, guint8 id,
guint8 *dest)
{
struct apparam_tag *tag;
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
tag = g_obex_apparam_find_tag(apparam, id);
if (tag == NULL)
return FALSE;
*dest = tag->value.u8;
g_obex_debug(G_OBEX_DEBUG_APPARAM, "%u", *dest);
return TRUE;
}
gboolean g_obex_apparam_get_uint16(GObexApparam *apparam, guint8 id,
guint16 *dest)
{
struct apparam_tag *tag;
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
tag = g_obex_apparam_find_tag(apparam, id);
if (tag == NULL)
return FALSE;
if (tag->len < sizeof(*dest))
return FALSE;
*dest = g_ntohs(tag->value.u16);
g_obex_debug(G_OBEX_DEBUG_APPARAM, "%u", *dest);
return TRUE;
}
gboolean g_obex_apparam_get_uint32(GObexApparam *apparam, guint8 id,
guint32 *dest)
{
struct apparam_tag *tag;
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
tag = g_obex_apparam_find_tag(apparam, id);
if (tag == NULL)
return FALSE;
if (tag->len < sizeof(*dest))
return FALSE;
*dest = g_ntohl(tag->value.u32);
g_obex_debug(G_OBEX_DEBUG_APPARAM, "%u", *dest);
return TRUE;
}
gboolean g_obex_apparam_get_uint64(GObexApparam *apparam, guint8 id,
guint64 *dest)
{
struct apparam_tag *tag;
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
tag = g_obex_apparam_find_tag(apparam, id);
if (tag == NULL)
return FALSE;
if (tag->len < sizeof(*dest))
return FALSE;
*dest = GUINT64_FROM_BE(tag->value.u64);
g_obex_debug(G_OBEX_DEBUG_APPARAM, "%" G_GUINT64_FORMAT, *dest);
return TRUE;
}
char *g_obex_apparam_get_string(GObexApparam *apparam, guint8 id)
{
struct apparam_tag *tag;
char *string;
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
tag = g_obex_apparam_find_tag(apparam, id);
if (tag == NULL)
return NULL;
string = g_strndup(tag->value.string, tag->len);
g_obex_debug(G_OBEX_DEBUG_APPARAM, "%s", string);
return string;
}
gboolean g_obex_apparam_get_bytes(GObexApparam *apparam, guint8 id,
const guint8 **val, gsize *len)
{
struct apparam_tag *tag;
g_obex_debug(G_OBEX_DEBUG_APPARAM, "tag 0x%02x", id);
tag = g_obex_apparam_find_tag(apparam, id);
if (tag == NULL)
return FALSE;
*len = tag->len;
*val = tag->value.data;
return TRUE;
}
void g_obex_apparam_free(GObexApparam *apparam)
{
g_hash_table_unref(apparam->tags);
g_free(apparam);
}
|