Sync usage with man page.
[netbsd-mini2440.git] / lib / libbluetooth / sdp_data.c
blob9f04dddaeda92e1c557cfeac1b9c1aca6adbae4a
1 /* $NetBSD: sdp_data.c$ */
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Iain Hibbert.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: sdp_data.c$");
35 #include <sdp.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <vis.h>
40 #include "sdp-int.h"
43 /******************************************************************************
44 * sdp_data_type(data)
46 * return SDP data element type
48 int
49 sdp_data_type(const sdp_data_t *data)
52 if (data->next + 1 > data->end)
53 return -1;
55 return data->next[0];
59 /******************************************************************************
60 * sdp_data_size(data)
62 * return the size of SDP data element. This will fail (return -1) if
63 * the data element does not fit into the data space.
65 ssize_t
66 sdp_data_size(const sdp_data_t *data)
68 uint8_t *p = data->next;
70 if (p + 1 > data->end)
71 return -1;
73 switch (*p++) {
74 case SDP_DATA_NIL:
75 break;
77 case SDP_DATA_BOOL:
78 case SDP_DATA_INT8:
79 case SDP_DATA_UINT8:
80 p += 1;
81 break;
83 case SDP_DATA_INT16:
84 case SDP_DATA_UINT16:
85 case SDP_DATA_UUID16:
86 p += 2;
87 break;
89 case SDP_DATA_INT32:
90 case SDP_DATA_UINT32:
91 case SDP_DATA_UUID32:
92 p += 4;
93 break;
95 case SDP_DATA_INT64:
96 case SDP_DATA_UINT64:
97 p += 8;
98 break;
100 case SDP_DATA_INT128:
101 case SDP_DATA_UINT128:
102 case SDP_DATA_UUID128:
103 p += 16;
104 break;
106 case SDP_DATA_ALT8:
107 case SDP_DATA_SEQ8:
108 case SDP_DATA_STR8:
109 case SDP_DATA_URL8:
110 if (p + 1 > data->end)
111 return -1;
113 p += 1 + *p;
114 break;
116 case SDP_DATA_ALT16:
117 case SDP_DATA_SEQ16:
118 case SDP_DATA_STR16:
119 case SDP_DATA_URL16:
120 if (p + 2 > data->end)
121 return -1;
123 p += 2 + be16dec(p);
124 break;
126 case SDP_DATA_ALT32:
127 case SDP_DATA_SEQ32:
128 case SDP_DATA_STR32:
129 case SDP_DATA_URL32:
130 if (p + 4 > data->end)
131 return -1;
133 p += 4 + be32dec(p);
134 break;
136 default:
137 return -1;
140 if (p > data->end)
141 return -1;
143 return (p - data->next);
146 /******************************************************************************
147 * sdp_data_valid(data)
149 * validate an SDP data element list recursively, ensuring elements do not
150 * expand past the claimed length and that there is no invalid data.
152 static bool
153 _sdp_data_valid(uint8_t *ptr, uint8_t *end)
155 size_t len;
157 while (ptr < end) {
158 if (ptr + 1 > end)
159 return false;
161 switch (*ptr++) {
162 case SDP_DATA_NIL:
163 break;
165 case SDP_DATA_BOOL:
166 case SDP_DATA_INT8:
167 case SDP_DATA_UINT8:
168 if (ptr + 1 > end)
169 return false;
171 ptr += 1;
172 break;
174 case SDP_DATA_INT16:
175 case SDP_DATA_UINT16:
176 case SDP_DATA_UUID16:
177 if (ptr + 2 > end)
178 return false;
180 ptr += 2;
181 break;
183 case SDP_DATA_INT32:
184 case SDP_DATA_UINT32:
185 case SDP_DATA_UUID32:
186 if (ptr + 4 > end)
187 return false;
189 ptr += 4;
190 break;
192 case SDP_DATA_INT64:
193 case SDP_DATA_UINT64:
194 if (ptr + 8 > end)
195 return false;
197 ptr += 8;
198 break;
200 case SDP_DATA_INT128:
201 case SDP_DATA_UINT128:
202 case SDP_DATA_UUID128:
203 if (ptr + 16 > end)
204 return false;
206 ptr += 16;
207 break;
209 case SDP_DATA_STR8:
210 case SDP_DATA_URL8:
211 if (ptr + 1 > end)
212 return false;
214 len = *ptr;
215 ptr += 1;
217 if (ptr + len > end)
218 return false;
220 ptr += len;
221 break;
223 case SDP_DATA_STR16:
224 case SDP_DATA_URL16:
225 if (ptr + 2 > end)
226 return false;
228 len = be16dec(ptr);
229 ptr += 2;
231 if (ptr + len > end)
232 return false;
234 ptr += len;
235 break;
237 case SDP_DATA_STR32:
238 case SDP_DATA_URL32:
239 if (ptr + 4 > end)
240 return false;
242 len = be32dec(ptr);
243 ptr += 4;
245 if (ptr + len > end)
246 return false;
248 ptr += len;
249 break;
251 case SDP_DATA_SEQ8:
252 case SDP_DATA_ALT8:
253 if (ptr + 1 > end)
254 return false;
256 len = *ptr;
257 ptr += 1;
259 if (ptr + len > end)
260 return false;
262 if (!_sdp_data_valid(ptr, ptr + len))
263 return false;
265 ptr += len;
266 break;
268 case SDP_DATA_SEQ16:
269 case SDP_DATA_ALT16:
270 if (ptr + 2 > end)
271 return false;
273 len = be16dec(ptr);
274 ptr += 2;
276 if (ptr + len > end)
277 return false;
279 if (!_sdp_data_valid(ptr, ptr + len))
280 return false;
282 ptr += len;
283 break;
285 case SDP_DATA_SEQ32:
286 case SDP_DATA_ALT32:
287 if (ptr + 4 > end)
288 return false;
290 len = be32dec(ptr);
291 ptr += 4;
293 if (ptr + len > end)
294 return false;
296 if (!_sdp_data_valid(ptr, ptr + len))
297 return false;
299 ptr += len;
300 break;
302 default:
303 return false;
307 return true;
310 bool
311 sdp_data_valid(const sdp_data_t *data)
314 if (data->next == NULL || data->end == NULL)
315 return false;
317 if (data->next >= data->end)
318 return false;
320 return _sdp_data_valid(data->next, data->end);
323 /******************************************************************************
324 * sdp_data_print(data, indent)
326 * print out a SDP data element list in human readable format
328 static void
329 _sdp_put(int indent, const char *type, const char *fmt, ...)
331 va_list ap;
333 indent = printf("%*s%s", indent, "", type);
334 indent = 18 - indent;
335 if (indent < 2)
336 indent = 2;
338 printf("%*s", indent, "");
340 va_start(ap, fmt);
341 vprintf(fmt, ap);
342 va_end(ap);
344 printf("\n");
347 static void
348 _sdp_putstr(int indent, int style, const char *type,
349 const uint8_t *str, size_t len)
351 char buf[50], *dst = buf;
353 indent = printf("%*s%s(%zu)", indent, "", type, len);
354 indent = 18 - indent;
355 if (indent < 2)
356 indent = 2;
358 printf("%*s", indent, "");
360 style |= VIS_NL;
362 while (len > 0 && (dst + 5) < (buf + sizeof(buf))) {
363 dst = vis(dst, str[0], style, (len > 0 ? str[1] : 0));
364 str++;
365 len--;
368 printf("\"%s%s\n", buf, (len == 0 ? "\"" : " ..."));
371 bool
372 _sdp_data_print(const uint8_t *next, const uint8_t *end, int indent)
374 size_t len;
376 while (next < end) {
377 if (next + 1 > end)
378 return false;
380 switch (*next++) {
381 case SDP_DATA_NIL:
382 _sdp_put(indent, "nil", "");
383 break;
385 case SDP_DATA_BOOL:
386 if (next + 1 > end)
387 return false;
389 _sdp_put(indent, "bool", "%s",
390 (*next == 0x00 ? "false" : "true"));
392 next += 1;
393 break;
395 case SDP_DATA_INT8:
396 if (next + 1 > end)
397 return false;
399 _sdp_put(indent, "int8", "%" PRId8,
400 *(const int8_t *)next);
401 next += 1;
402 break;
404 case SDP_DATA_UINT8:
405 if (next + 1 > end)
406 return false;
408 _sdp_put(indent, "uint8", "0x%02" PRIx8,
409 *next);
410 next += 1;
411 break;
413 case SDP_DATA_INT16:
414 if (next + 2 > end)
415 return false;
417 _sdp_put(indent, "int16", "%" PRId16,
418 (int16_t)be16dec(next));
419 next += 2;
420 break;
422 case SDP_DATA_UINT16:
423 if (next + 2 > end)
424 return false;
426 _sdp_put(indent, "uint16", "0x%04" PRIx16,
427 be16dec(next));
428 next += 2;
429 break;
431 case SDP_DATA_UUID16:
432 if (next + 2 > end)
433 return false;
435 _sdp_put(indent, "uuid16", "0x%04" PRIx16,
436 be16dec(next));
437 next += 2;
438 break;
440 case SDP_DATA_INT32:
441 if (next + 4 > end)
442 return false;
444 _sdp_put(indent, "int32", "%" PRId32,
445 (int32_t)be32dec(next));
446 next += 4;
447 break;
449 case SDP_DATA_UINT32:
450 if (next + 4 > end)
451 return false;
453 _sdp_put(indent, "uint32", "0x%08" PRIx32,
454 be32dec(next));
455 next += 4;
456 break;
458 case SDP_DATA_UUID32:
459 if (next + 4 > end)
460 return false;
462 _sdp_put(indent, "uuid32", "0x%08" PRIx32,
463 be32dec(next));
464 next += 4;
465 break;
467 case SDP_DATA_INT64:
468 if (next + 8 > end)
469 return false;
471 _sdp_put(indent, "int64", "%" PRId64,
472 (int64_t)be64dec(next));
473 next += 8;
474 break;
476 case SDP_DATA_UINT64:
477 if (next + 8 > end)
478 return false;
480 _sdp_put(indent, "uint64", "0x%016" PRIx64,
481 be64dec(next));
482 next += 8;
483 break;
485 case SDP_DATA_INT128:
486 if (next + 16 > end)
487 return false;
489 _sdp_put(indent, "int128",
490 "0x%02x%02x%02x%02x%02x%02x%02x%02x"
491 "%02x%02x%02x%02x%02x%02x%02x%02x",
492 next[0], next[1], next[2], next[3],
493 next[4], next[5], next[6], next[7],
494 next[8], next[9], next[10], next[11],
495 next[12], next[13], next[14], next[15]);
496 next += 16;
497 break;
499 case SDP_DATA_UINT128:
500 if (next + 16 > end)
501 return false;
503 _sdp_put(indent, "uint128",
504 "0x%02x%02x%02x%02x%02x%02x%02x%02x"
505 "%02x%02x%02x%02x%02x%02x%02x%02x",
506 next[0], next[1], next[2], next[3],
507 next[4], next[5], next[6], next[7],
508 next[8], next[9], next[10], next[11],
509 next[12], next[13], next[14], next[15]);
510 next += 16;
511 break;
513 case SDP_DATA_UUID128:
514 if (next + 16 > end)
515 return false;
517 _sdp_put(indent, "uuid128",
518 "%02x%02x%02x%02x-"
519 "%02x%02x-"
520 "%02x%02x-"
521 "%02x%02x-"
522 "%02x%02x%02x%02x%02x%02x",
523 next[0], next[1], next[2], next[3],
524 next[4], next[5],
525 next[6], next[7],
526 next[8], next[9],
527 next[10], next[11], next[12],
528 next[13], next[14], next[15]);
529 next += 16;
530 break;
532 case SDP_DATA_STR8:
533 if (next + 1 > end)
534 return false;
536 len = *next;
537 next += 1;
539 if (next + len > end)
540 return false;
542 _sdp_putstr(indent, VIS_CSTYLE, "str8", next, len);
543 next += len;
544 break;
546 case SDP_DATA_URL8:
547 if (next + 1 > end)
548 return false;
550 len = *next;
551 next += 1;
553 if (next + len > end)
554 return false;
556 _sdp_putstr(indent, VIS_HTTPSTYLE, "url8", next, len);
557 next += len;
558 break;
560 case SDP_DATA_STR16:
561 if (next + 2 > end)
562 return false;
564 len = be16dec(next);
565 next += 2;
567 if (next + len > end)
568 return false;
570 _sdp_putstr(indent, VIS_CSTYLE, "str16", next, len);
571 next += len;
572 break;
574 case SDP_DATA_URL16:
575 if (next + 2 > end)
576 return false;
578 len = be16dec(next);
579 next += 2;
581 if (next + len > end)
582 return false;
584 _sdp_putstr(indent, VIS_HTTPSTYLE, "url16", next, len);
585 next += len;
586 break;
588 case SDP_DATA_STR32:
589 if (next + 4 > end)
590 return false;
592 len = be32dec(next);
593 next += 4;
595 if (next + len > end)
596 return false;
598 _sdp_putstr(indent, VIS_CSTYLE, "str32", next, len);
599 next += len;
600 break;
602 case SDP_DATA_URL32:
603 if (next + 4 > end)
604 return false;
606 len = be32dec(next);
607 next += 4;
609 if (next + len > end)
610 return false;
612 _sdp_putstr(indent, VIS_HTTPSTYLE, "url32", next, len);
613 next += len;
614 break;
616 case SDP_DATA_SEQ8:
617 if (next + 1 > end)
618 return false;
620 len = *next;
621 next += 1;
623 if (next + len > end)
624 return false;
626 printf("%*sseq8(%zu)\n", indent, "", len);
627 if (!_sdp_data_print(next, next + len, indent + 1))
628 return false;
630 next += len;
631 break;
633 case SDP_DATA_ALT8:
634 if (next + 1 > end)
635 return false;
637 len = *next;
638 next += 1;
640 if (next + len > end)
641 return false;
643 printf("%*salt8(%zu)\n", indent, "", len);
644 if (!_sdp_data_print(next, next + len, indent + 1))
645 return false;
647 next += len;
648 break;
650 case SDP_DATA_SEQ16:
651 if (next + 2 > end)
652 return false;
654 len = be16dec(next);
655 next += 2;
657 if (next + len > end)
658 return false;
660 printf("%*sseq16(%zu)\n", indent, "", len);
661 if (!_sdp_data_print(next, next + len, indent + 1))
662 return false;
664 next += len;
665 break;
667 case SDP_DATA_ALT16:
668 if (next + 2 > end)
669 return false;
671 len = be16dec(next);
672 next += 2;
674 if (next + len > end)
675 return false;
677 printf("%*salt16(%zu)\n", indent, "", len);
678 if (!_sdp_data_print(next, next + len, indent + 1))
679 return false;
681 next += len;
682 break;
684 case SDP_DATA_SEQ32:
685 if (next + 4 > end)
686 return false;
688 len = be32dec(next);
689 next += 4;
691 if (next + len > end)
692 return false;
694 printf("%*sseq32(%zu)\n", indent, "", len);
695 if (!_sdp_data_print(next, next + len, indent + 1))
696 return false;
698 next += len;
699 break;
701 case SDP_DATA_ALT32:
702 if (next + 4 > end)
703 return false;
705 len = be32dec(next);
706 next += 4;
708 if (next + len > end)
709 return false;
711 printf("%*salt32(%zu)\n", indent, "", len);
712 if (!_sdp_data_print(next, next + len, indent + 1))
713 return false;
715 next += len;
716 break;
718 default:
719 return false;
723 return true;
726 void
727 sdp_data_print(const sdp_data_t *data, int indent)
730 if (!_sdp_data_print(data->next, data->end, indent))
731 printf("SDP data error\n");