Home | History | Annotate | Line # | Download | only in libbluetooth
sdp_get.c revision 1.1
      1  1.1  plunky /*	$NetBSD: sdp_get.c,v 1.1 2009/05/12 10:05:06 plunky Exp $	*/
      2  1.1  plunky 
      3  1.1  plunky /*-
      4  1.1  plunky  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.1  plunky  * All rights reserved.
      6  1.1  plunky  *
      7  1.1  plunky  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  plunky  * by Iain Hibbert.
      9  1.1  plunky  *
     10  1.1  plunky  * Redistribution and use in source and binary forms, with or without
     11  1.1  plunky  * modification, are permitted provided that the following conditions
     12  1.1  plunky  * are met:
     13  1.1  plunky  * 1. Redistributions of source code must retain the above copyright
     14  1.1  plunky  *    notice, this list of conditions and the following disclaimer.
     15  1.1  plunky  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  plunky  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  plunky  *    documentation and/or other materials provided with the distribution.
     18  1.1  plunky  *
     19  1.1  plunky  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  plunky  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  plunky  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  plunky  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  plunky  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  plunky  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  plunky  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  plunky  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  plunky  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  plunky  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  plunky  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  plunky  */
     31  1.1  plunky 
     32  1.1  plunky #include <sys/cdefs.h>
     33  1.1  plunky __RCSID("$NetBSD: sdp_get.c,v 1.1 2009/05/12 10:05:06 plunky Exp $");
     34  1.1  plunky 
     35  1.1  plunky #include <sdp.h>
     36  1.1  plunky #include <limits.h>
     37  1.1  plunky 
     38  1.1  plunky /******************************************************************************
     39  1.1  plunky  *	sdp_get_xxxx(data, value)
     40  1.1  plunky  *
     41  1.1  plunky  * examine first SDP data element in list for xxx type, extracting to given
     42  1.1  plunky  * storage and advancing pointer if found.
     43  1.1  plunky  * - these functions will not modify data pointer unless the value was
     44  1.1  plunky  *   extracted successfully
     45  1.1  plunky  * - these functions always update the data pointer before the value pointer,
     46  1.1  plunky  *   so where the value is a sdp_data_t the data struct can be discarded.
     47  1.1  plunky  */
     48  1.1  plunky 
     49  1.1  plunky bool
     50  1.1  plunky sdp_get_data(sdp_data_t *data, sdp_data_t *value)
     51  1.1  plunky {
     52  1.1  plunky 	uint8_t *p = data->next;
     53  1.1  plunky 	ssize_t l = sdp_data_size(data);
     54  1.1  plunky 
     55  1.1  plunky 	if (l == -1
     56  1.1  plunky 	    || p + l > data->end)
     57  1.1  plunky 		return false;
     58  1.1  plunky 
     59  1.1  plunky 	data->next = p + l;
     60  1.1  plunky 	value->next = p;
     61  1.1  plunky 	value->end = p + l;
     62  1.1  plunky 	return true;
     63  1.1  plunky }
     64  1.1  plunky 
     65  1.1  plunky bool
     66  1.1  plunky sdp_get_attr(sdp_data_t *data, uint16_t *attr, sdp_data_t *value)
     67  1.1  plunky {
     68  1.1  plunky 	sdp_data_t v, d = *data;
     69  1.1  plunky 	uintmax_t a;
     70  1.1  plunky 
     71  1.1  plunky 	if (sdp_data_type(&d) != SDP_DATA_UINT16
     72  1.1  plunky 	    || !sdp_get_uint(&d, &a)
     73  1.1  plunky 	    || !sdp_get_data(&d, &v))
     74  1.1  plunky 		return false;
     75  1.1  plunky 
     76  1.1  plunky 	*attr = (uint16_t)a;
     77  1.1  plunky 	*data = d;
     78  1.1  plunky 	*value = v;
     79  1.1  plunky 	return true;
     80  1.1  plunky }
     81  1.1  plunky 
     82  1.1  plunky bool
     83  1.1  plunky sdp_get_uuid(sdp_data_t *data, uuid_t *uuid)
     84  1.1  plunky {
     85  1.1  plunky 	uint8_t *p = data->next;
     86  1.1  plunky 
     87  1.1  plunky 	if (p + 1 > data->end)
     88  1.1  plunky 		return false;
     89  1.1  plunky 
     90  1.1  plunky 	switch (*p++) {
     91  1.1  plunky 	case SDP_DATA_UUID16:
     92  1.1  plunky 		if (p + 2 > data->end)
     93  1.1  plunky 			return false;
     94  1.1  plunky 
     95  1.1  plunky 		*uuid = BLUETOOTH_BASE_UUID;
     96  1.1  plunky 		uuid->time_low = be16dec(p);
     97  1.1  plunky 		p += 2;
     98  1.1  plunky 		break;
     99  1.1  plunky 
    100  1.1  plunky 	case SDP_DATA_UUID32:
    101  1.1  plunky 		if (p + 4 > data->end)
    102  1.1  plunky 			return false;
    103  1.1  plunky 
    104  1.1  plunky 		*uuid = BLUETOOTH_BASE_UUID;
    105  1.1  plunky 		uuid->time_low = be32dec(p);
    106  1.1  plunky 		p += 4;
    107  1.1  plunky 		break;
    108  1.1  plunky 
    109  1.1  plunky 	case SDP_DATA_UUID128:
    110  1.1  plunky 		if (p + 16 > data->end)
    111  1.1  plunky 			return false;
    112  1.1  plunky 
    113  1.1  plunky 		uuid_dec_be(p, uuid);
    114  1.1  plunky 		p += 16;
    115  1.1  plunky 		break;
    116  1.1  plunky 
    117  1.1  plunky 	default:
    118  1.1  plunky 		return false;
    119  1.1  plunky 	}
    120  1.1  plunky 
    121  1.1  plunky 	data->next = p;
    122  1.1  plunky 	return true;
    123  1.1  plunky }
    124  1.1  plunky 
    125  1.1  plunky bool
    126  1.1  plunky sdp_get_bool(sdp_data_t *data, bool *value)
    127  1.1  plunky {
    128  1.1  plunky 	uint8_t *p = data->next;
    129  1.1  plunky 	uint8_t v;
    130  1.1  plunky 
    131  1.1  plunky 	if (p + 1 > data->end)
    132  1.1  plunky 		return false;
    133  1.1  plunky 
    134  1.1  plunky 	switch (*p++) {
    135  1.1  plunky 	case SDP_DATA_BOOL:
    136  1.1  plunky 		if (p + 1 > data->end)
    137  1.1  plunky 			return false;
    138  1.1  plunky 
    139  1.1  plunky 		v = *p;
    140  1.1  plunky 		p += 1;
    141  1.1  plunky 		break;
    142  1.1  plunky 
    143  1.1  plunky 	default:
    144  1.1  plunky 		return false;
    145  1.1  plunky 	}
    146  1.1  plunky 
    147  1.1  plunky 	data->next = p;
    148  1.1  plunky 	*value = ((v != 0) ? true : false);
    149  1.1  plunky 	return true;
    150  1.1  plunky }
    151  1.1  plunky 
    152  1.1  plunky bool
    153  1.1  plunky sdp_get_uint(sdp_data_t *data, uintmax_t *value)
    154  1.1  plunky {
    155  1.1  plunky 	uint8_t *p = data->next;
    156  1.1  plunky 	uint64_t v, x;
    157  1.1  plunky 
    158  1.1  plunky 	if (p + 1 > data->end)
    159  1.1  plunky 		return false;
    160  1.1  plunky 
    161  1.1  plunky 	switch (*p++) {
    162  1.1  plunky 	case SDP_DATA_UINT8:
    163  1.1  plunky 		if (p + 1 > data->end)
    164  1.1  plunky 			return false;
    165  1.1  plunky 
    166  1.1  plunky 		v = *p;
    167  1.1  plunky 		p += 1;
    168  1.1  plunky 		break;
    169  1.1  plunky 
    170  1.1  plunky 	case SDP_DATA_UINT16:
    171  1.1  plunky 		if (p + 2 > data->end)
    172  1.1  plunky 			return false;
    173  1.1  plunky 
    174  1.1  plunky 		v = be16dec(p);
    175  1.1  plunky 		p += 2;
    176  1.1  plunky 		break;
    177  1.1  plunky 
    178  1.1  plunky 	case SDP_DATA_UINT32:
    179  1.1  plunky 		if (p + 4 > data->end)
    180  1.1  plunky 			return false;
    181  1.1  plunky 
    182  1.1  plunky 		v = be32dec(p);
    183  1.1  plunky 		p += 4;
    184  1.1  plunky 		break;
    185  1.1  plunky 
    186  1.1  plunky 	case SDP_DATA_UINT64:
    187  1.1  plunky 		if (p + 8 > data->end)
    188  1.1  plunky 			return false;
    189  1.1  plunky 
    190  1.1  plunky 		v = be64dec(p);
    191  1.1  plunky 		if (v > UINTMAX_MAX)
    192  1.1  plunky 			return false;
    193  1.1  plunky 
    194  1.1  plunky 		p += 8;
    195  1.1  plunky 		break;
    196  1.1  plunky 
    197  1.1  plunky 	case SDP_DATA_UINT128:
    198  1.1  plunky 		if (p + 16 > data->end)
    199  1.1  plunky 			return false;
    200  1.1  plunky 
    201  1.1  plunky 		x = be64dec(p);
    202  1.1  plunky 		v = be64dec(p + 8);
    203  1.1  plunky 		if (x != 0 || v > UINTMAX_MAX)
    204  1.1  plunky 			return false;
    205  1.1  plunky 
    206  1.1  plunky 		p += 16;
    207  1.1  plunky 		break;
    208  1.1  plunky 
    209  1.1  plunky 	default:
    210  1.1  plunky 		return false;
    211  1.1  plunky 	}
    212  1.1  plunky 
    213  1.1  plunky 	data->next = p;
    214  1.1  plunky 	*value = (uintmax_t)v;
    215  1.1  plunky 	return true;
    216  1.1  plunky }
    217  1.1  plunky 
    218  1.1  plunky bool
    219  1.1  plunky sdp_get_int(sdp_data_t *data, intmax_t *value)
    220  1.1  plunky {
    221  1.1  plunky 	uint8_t *p = data->next;
    222  1.1  plunky 	int64_t v, x;
    223  1.1  plunky 
    224  1.1  plunky 	if (p + 1 > data->end)
    225  1.1  plunky 		return false;
    226  1.1  plunky 
    227  1.1  plunky 	switch (*p++) {
    228  1.1  plunky 	case SDP_DATA_INT8:
    229  1.1  plunky 		if (p + 1 > data->end)
    230  1.1  plunky 			return false;
    231  1.1  plunky 
    232  1.1  plunky 		v = *(int8_t *)p;
    233  1.1  plunky 		p += 1;
    234  1.1  plunky 		break;
    235  1.1  plunky 
    236  1.1  plunky 	case SDP_DATA_INT16:
    237  1.1  plunky 		if (p + 2 > data->end)
    238  1.1  plunky 			return false;
    239  1.1  plunky 
    240  1.1  plunky 		v = (int16_t)be16dec(p);
    241  1.1  plunky 		p += 2;
    242  1.1  plunky 		break;
    243  1.1  plunky 
    244  1.1  plunky 	case SDP_DATA_INT32:
    245  1.1  plunky 		if (p + 4 > data->end)
    246  1.1  plunky 			return false;
    247  1.1  plunky 
    248  1.1  plunky 		v = (int32_t)be32dec(p);
    249  1.1  plunky 		p += 4;
    250  1.1  plunky 		break;
    251  1.1  plunky 
    252  1.1  plunky 	case SDP_DATA_INT64:
    253  1.1  plunky 		if (p + 8 > data->end)
    254  1.1  plunky 			return false;
    255  1.1  plunky 
    256  1.1  plunky 		v = (int64_t)be64dec(p);
    257  1.1  plunky 		if (v > INTMAX_MAX || v < INTMAX_MIN)
    258  1.1  plunky 			return false;
    259  1.1  plunky 
    260  1.1  plunky 		p += 8;
    261  1.1  plunky 		break;
    262  1.1  plunky 
    263  1.1  plunky 	case SDP_DATA_INT128:
    264  1.1  plunky 		if (p + 16 > data->end)
    265  1.1  plunky 			return false;
    266  1.1  plunky 
    267  1.1  plunky 		x = (int64_t)be64dec(p);
    268  1.1  plunky 		v = (int64_t)be64dec(p + 8);
    269  1.1  plunky 		if (x == 0) {
    270  1.1  plunky 			if (v > INTMAX_MAX)
    271  1.1  plunky 				return false;
    272  1.1  plunky 		} else if (x == -1) {
    273  1.1  plunky 			if (v < INTMAX_MIN)
    274  1.1  plunky 				return false;
    275  1.1  plunky 		} else {
    276  1.1  plunky 			return false;
    277  1.1  plunky 		}
    278  1.1  plunky 
    279  1.1  plunky 		p += 16;
    280  1.1  plunky 		break;
    281  1.1  plunky 
    282  1.1  plunky 	default:
    283  1.1  plunky 		return false;
    284  1.1  plunky 	}
    285  1.1  plunky 
    286  1.1  plunky 	data->next = p;
    287  1.1  plunky 	*value = (intmax_t)v;
    288  1.1  plunky 	return true;
    289  1.1  plunky }
    290  1.1  plunky 
    291  1.1  plunky static bool
    292  1.1  plunky _sdp_get_ext(uint8_t type, sdp_data_t *data, sdp_data_t *ext)
    293  1.1  plunky {
    294  1.1  plunky 	uint8_t *p = data->next;
    295  1.1  plunky 	uint32_t l;
    296  1.1  plunky 
    297  1.1  plunky 	if (p + 1 > data->end
    298  1.1  plunky 	    || SDP_DATA_TYPE(*p) != type)
    299  1.1  plunky 		return false;
    300  1.1  plunky 
    301  1.1  plunky 	switch (SDP_DATA_SIZE(*p++)) {
    302  1.1  plunky 	case SDP_DATA_EXT8:
    303  1.1  plunky 		if (p + 1 > data->end)
    304  1.1  plunky 			return false;
    305  1.1  plunky 
    306  1.1  plunky 		l = *p;
    307  1.1  plunky 		p += 1;
    308  1.1  plunky 		break;
    309  1.1  plunky 
    310  1.1  plunky 	case SDP_DATA_EXT16:
    311  1.1  plunky 		if (p + 2 > data->end)
    312  1.1  plunky 			return false;
    313  1.1  plunky 
    314  1.1  plunky 		l = be16dec(p);
    315  1.1  plunky 		p += 2;
    316  1.1  plunky 		break;
    317  1.1  plunky 
    318  1.1  plunky 	case SDP_DATA_EXT32:
    319  1.1  plunky 		if (p + 4 > data->end)
    320  1.1  plunky 			return false;
    321  1.1  plunky 
    322  1.1  plunky 		l = be32dec(p);
    323  1.1  plunky 		p += 4;
    324  1.1  plunky 		break;
    325  1.1  plunky 
    326  1.1  plunky 	default:
    327  1.1  plunky 		return false;
    328  1.1  plunky 	}
    329  1.1  plunky 
    330  1.1  plunky 	if (p + l > data->end)
    331  1.1  plunky 		return false;
    332  1.1  plunky 
    333  1.1  plunky 	data->next = p + l;
    334  1.1  plunky 	ext->next = p;
    335  1.1  plunky 	ext->end = p + l;
    336  1.1  plunky 	return true;
    337  1.1  plunky }
    338  1.1  plunky 
    339  1.1  plunky bool
    340  1.1  plunky sdp_get_seq(sdp_data_t *data, sdp_data_t *seq)
    341  1.1  plunky {
    342  1.1  plunky 
    343  1.1  plunky 	return _sdp_get_ext(SDP_DATA_SEQ, data, seq);
    344  1.1  plunky }
    345  1.1  plunky 
    346  1.1  plunky bool
    347  1.1  plunky sdp_get_alt(sdp_data_t *data, sdp_data_t *alt)
    348  1.1  plunky {
    349  1.1  plunky 
    350  1.1  plunky 	return _sdp_get_ext(SDP_DATA_ALT, data, alt);
    351  1.1  plunky }
    352  1.1  plunky 
    353  1.1  plunky bool
    354  1.1  plunky sdp_get_str(sdp_data_t *data, char **str, size_t *len)
    355  1.1  plunky {
    356  1.1  plunky 	sdp_data_t s;
    357  1.1  plunky 
    358  1.1  plunky 	if (!_sdp_get_ext(SDP_DATA_STR, data, &s))
    359  1.1  plunky 		return false;
    360  1.1  plunky 
    361  1.1  plunky 	*str = (char *)s.next;
    362  1.1  plunky 	*len = s.end - s.next;
    363  1.1  plunky 	return true;
    364  1.1  plunky }
    365  1.1  plunky 
    366  1.1  plunky bool
    367  1.1  plunky sdp_get_url(sdp_data_t *data, char **url, size_t *len)
    368  1.1  plunky {
    369  1.1  plunky 	sdp_data_t u;
    370  1.1  plunky 
    371  1.1  plunky 	if (!_sdp_get_ext(SDP_DATA_URL, data, &u))
    372  1.1  plunky 		return false;
    373  1.1  plunky 
    374  1.1  plunky 	*url = (char *)u.next;
    375  1.1  plunky 	*len = u.end - u.next;
    376  1.1  plunky 	return true;
    377  1.1  plunky }
    378