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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2004-2006 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "sdp-xml.h"
#include <stdio.h>
#include <string.h>
#include <bluetooth/sdp_lib.h>
#include <malloc.h>
#include <limits.h>
#define STRBUFSIZE 256
#define MAXINDENT 64
static void convert_raw_data_to_xml(sdp_data_t *value, int indent_level,
void *data,
void (*appender) (void *, const char *))
{
int i, hex;
char buf[STRBUFSIZE];
char indent[MAXINDENT];
char next_indent[MAXINDENT];
if (!value)
return;
if (indent_level >= MAXINDENT)
indent_level = MAXINDENT - 2;
for (i = 0; i < indent_level; i++) {
indent[i] = '\t';
next_indent[i] = '\t';
}
indent[i] = '\0';
next_indent[i] = '\t';
next_indent[i + 1] = '\0';
buf[STRBUFSIZE - 1] = '\0';
switch (value->dtd) {
case SDP_DATA_NIL:
appender(data, indent);
appender(data, "<nil/>\n");
break;
case SDP_BOOL:
appender(data, indent);
appender(data, "<boolean value=\"");
appender(data, value->val.uint8 ? "true" : "false");
appender(data, "\" />\n");
break;
case SDP_UINT8:
appender(data, indent);
appender(data, "<uint8 value=\"");
snprintf(buf, STRBUFSIZE - 1, "0x%02x", value->val.uint8);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_UINT16:
appender(data, indent);
appender(data, "<uint16 value=\"");
snprintf(buf, STRBUFSIZE - 1, "0x%04x", value->val.uint16);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_UINT32:
appender(data, indent);
appender(data, "<uint32 value=\"");
snprintf(buf, STRBUFSIZE - 1, "0x%08x", value->val.uint32);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_UINT64:
appender(data, indent);
appender(data, "<uint64 value=\"");
snprintf(buf, STRBUFSIZE - 1, "0x%016jx", value->val.uint64);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_UINT128:
appender(data, indent);
appender(data, "<uint128 value=\"");
for (i = 0; i < 16; i++) {
sprintf(&buf[i * 2], "%02x",
(unsigned char) value->val.uint128.data[i]);
}
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_INT8:
appender(data, indent);
appender(data, "<int8 value=\"");
snprintf(buf, STRBUFSIZE - 1, "%d", value->val.int8);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_INT16:
appender(data, indent);
appender(data, "<int16 value=\"");
snprintf(buf, STRBUFSIZE - 1, "%d", value->val.int16);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_INT32:
appender(data, indent);
appender(data, "<int32 value=\"");
snprintf(buf, STRBUFSIZE - 1, "%d", value->val.int32);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_INT64:
appender(data, indent);
appender(data, "<int64 value=\"");
snprintf(buf, STRBUFSIZE - 1, "%jd", value->val.int64);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_INT128:
appender(data, indent);
appender(data, "<int128 value=\"");
for (i = 0; i < 16; i++) {
sprintf(&buf[i * 2], "%02x",
(unsigned char) value->val.int128.data[i]);
}
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_UUID16:
appender(data, indent);
appender(data, "<uuid value=\"");
snprintf(buf, STRBUFSIZE - 1, "0x%04x",
value->val.uuid.value.uuid16);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_UUID32:
appender(data, indent);
appender(data, "<uuid value=\"");
snprintf(buf, STRBUFSIZE - 1, "0x%08x",
value->val.uuid.value.uuid32);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_UUID128:
appender(data, indent);
appender(data, "<uuid value=\"");
snprintf(buf, STRBUFSIZE - 1,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
(unsigned char) value->val.uuid.value.
uuid128.data[0],
(unsigned char) value->val.uuid.value.
uuid128.data[1],
(unsigned char) value->val.uuid.value.
uuid128.data[2],
(unsigned char) value->val.uuid.value.
uuid128.data[3],
(unsigned char) value->val.uuid.value.
uuid128.data[4],
(unsigned char) value->val.uuid.value.
uuid128.data[5],
(unsigned char) value->val.uuid.value.
uuid128.data[6],
(unsigned char) value->val.uuid.value.
uuid128.data[7],
(unsigned char) value->val.uuid.value.
uuid128.data[8],
(unsigned char) value->val.uuid.value.
uuid128.data[9],
(unsigned char) value->val.uuid.value.
uuid128.data[10],
(unsigned char) value->val.uuid.value.
uuid128.data[11],
(unsigned char) value->val.uuid.value.
uuid128.data[12],
(unsigned char) value->val.uuid.value.
uuid128.data[13],
(unsigned char) value->val.uuid.value.
uuid128.data[14],
(unsigned char) value->val.uuid.value.
uuid128.data[15]);
appender(data, buf);
appender(data, "\" />\n");
break;
case SDP_TEXT_STR8:
case SDP_TEXT_STR16:
case SDP_TEXT_STR32:
{
hex = 0;
int num_chars_to_escape = 0;
for (i = 0; i < value->unitSize; i++) {
if (i == (value->unitSize - 1)
&& value->val.str[i] == '\0')
break;
if (!isprint(value->val.str[i])) {
hex = 1;
break;
}
/* XML is evil, must do this... */
if ((value->val.str[i] == '<') ||
(value->val.str[i] == '>') ||
(value->val.str[i] == '"') ||
(value->val.str[i] == '&'))
num_chars_to_escape++;
}
appender(data, indent);
appender(data, "<text ");
char *strBuf = 0;
if (hex) {
appender(data, "encoding=\"hex\" ");
strBuf = (char *) malloc(sizeof(char)
* (value->unitSize * 2 + 1));
/* Unit Size seems to include the size for dtd
It is thus off by 1
This is safe for Normal strings, but not
hex encoded data */
for (i = 0; i < (value->unitSize-1); i++)
sprintf(&strBuf[i * sizeof (char) * 2],
"%02x",
(unsigned char) value->val.str[i]);
strBuf[value->unitSize * 2] = '\0';
}
else {
int j;
/* escape the XML disallowed chars */
strBuf = (char *)
malloc(sizeof(char) *
(value->unitSize + 1 + num_chars_to_escape * 4));
for (i = 0, j = 0; i < value->unitSize; i++) {
if (value->val.str[i] == '&') {
strBuf[j++] = '&';
strBuf[j++] = 'a';
strBuf[j++] = 'm';
strBuf[j++] = 'p';
}
else if (value->val.str[i] == '<') {
strBuf[j++] = '&';
strBuf[j++] = 'l';
strBuf[j++] = 't';
}
else if (value->val.str[i] == '>') {
strBuf[j++] = '&';
strBuf[j++] = 'g';
strBuf[j++] = 't';
}
else if (value->val.str[i] == '"') {
strBuf[j++] = '&';
strBuf[j++] = 'q';
strBuf[j++] = 'u';
strBuf[j++] = 'o';
strBuf[j++] = 't';
}
else {
strBuf[j++] = value->val.str[i];
}
}
strBuf[j] = '\0';
}
appender(data, "value=\"");
appender(data, strBuf);
appender(data, "\" />\n");
free(strBuf);
break;
}
case SDP_URL_STR8:
case SDP_URL_STR16:
case SDP_URL_STR32:
appender(data, indent);
appender(data, "<url value=\"");
appender(data, value->val.str);
appender(data, "\" />\n");
break;
case SDP_SEQ8:
case SDP_SEQ16:
case SDP_SEQ32:
appender(data, indent);
appender(data, "<sequence>\n");
convert_raw_data_to_xml(value->val.dataseq,
indent_level + 1, data,
appender);
appender(data, indent);
appender(data, "</sequence>\n");
break;
case SDP_ALT8:
case SDP_ALT16:
case SDP_ALT32:
appender(data, indent);
appender(data, "<alternate>\n");
convert_raw_data_to_xml(value->val.dataseq,
indent_level + 1, data,
appender);
appender(data, indent);
appender(data, "</alternate>\n");
break;
default:
break;
}
convert_raw_data_to_xml(value->next, indent_level, data,
appender);
}
struct conversion_data
{
void *data;
void (*appender) (void *data, const char *);
};
static void convert_raw_attr_to_xml_func(void *val, void *data)
{
struct conversion_data *cd = (struct conversion_data *) data;
sdp_data_t *value = (sdp_data_t *) val;
char buf[STRBUFSIZE];
buf[STRBUFSIZE - 1] = '\0';
snprintf(buf, STRBUFSIZE - 1, "\t<attribute id=\"0x%04x\">\n",
value->attrId);
cd->appender(cd->data, buf);
if (data)
convert_raw_data_to_xml(value, 2, cd->data,
cd->appender);
else
cd->appender(cd->data, "\t\tNULL\n");
cd->appender(cd->data, "\t</attribute>\n");
}
/*
Will convert the sdp record to XML. The appender and data can be used
to control where to output the record (e.g. file or a data buffer). The
appender will be called repeatedly with data and the character buffer
(containing parts of the generated XML) to append.
*/
void convert_sdp_record_to_xml(sdp_record_t *rec,
void *data,
void (*appender) (void *, const char *))
{
struct conversion_data cd;
cd.data = data;
cd.appender = appender;
if (rec && rec->attrlist) {
appender(data, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n");
appender(data, "<record>\n");
sdp_list_foreach(rec->attrlist,
convert_raw_attr_to_xml_func, &cd);
appender(data, "</record>\n");
}
}
|