Home | History | Annotate | Line # | Download | only in libbluetooth
sdp_put.c revision 1.2
      1  1.2  plunky /*	$NetBSD: sdp_put.c,v 1.2 2009/05/14 19:12:45 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.2  plunky __RCSID("$NetBSD: sdp_put.c,v 1.2 2009/05/14 19:12:45 plunky Exp $");
     34  1.1  plunky 
     35  1.1  plunky #include <bluetooth.h>
     36  1.1  plunky #include <limits.h>
     37  1.1  plunky #include <sdp.h>
     38  1.1  plunky #include <string.h>
     39  1.1  plunky 
     40  1.1  plunky /******************************************************************************
     41  1.1  plunky  *	sdp_put_xxxx(data, value)
     42  1.1  plunky  *
     43  1.1  plunky  * write a value to data space and advance data pointers,
     44  1.1  plunky  * fail if data space is not large enough
     45  1.1  plunky  */
     46  1.1  plunky 
     47  1.1  plunky bool
     48  1.1  plunky sdp_put_data(sdp_data_t *data, sdp_data_t *value)
     49  1.1  plunky {
     50  1.1  plunky 	ssize_t len;
     51  1.1  plunky 
     52  1.1  plunky 	len = value->end - value->next;
     53  1.1  plunky 
     54  1.1  plunky 	if (data->next + len > data->end)
     55  1.1  plunky 		return false;
     56  1.1  plunky 
     57  1.1  plunky 	memcpy(data->next, value->next, (size_t)len);
     58  1.1  plunky 	data->next += len;
     59  1.1  plunky 	return true;
     60  1.1  plunky }
     61  1.1  plunky 
     62  1.1  plunky bool
     63  1.1  plunky sdp_put_attr(sdp_data_t *data, uint16_t attr, sdp_data_t *value)
     64  1.1  plunky {
     65  1.1  plunky 	sdp_data_t d = *data;
     66  1.1  plunky 
     67  1.1  plunky 	if (!sdp_put_uint16(&d, attr)
     68  1.1  plunky 	    || sdp_put_data(&d, value))
     69  1.1  plunky 		return false;
     70  1.1  plunky 
     71  1.1  plunky 	*data = d;
     72  1.1  plunky 	return true;
     73  1.1  plunky }
     74  1.1  plunky 
     75  1.1  plunky bool
     76  1.1  plunky sdp_put_uuid(sdp_data_t *data, const uuid_t *uuid)
     77  1.1  plunky {
     78  1.1  plunky 	uuid_t u = *uuid;
     79  1.1  plunky 
     80  1.1  plunky 	u.time_low = 0;
     81  1.1  plunky 
     82  1.1  plunky 	if (uuid_equal(&u, &BLUETOOTH_BASE_UUID, NULL) == 0)
     83  1.1  plunky 		return sdp_put_uuid128(data, uuid);
     84  1.1  plunky 
     85  1.1  plunky 	if (uuid->time_low > UINT16_MAX)
     86  1.1  plunky 		return sdp_put_uuid32(data, (uint32_t)uuid->time_low);
     87  1.1  plunky 
     88  1.1  plunky 	return sdp_put_uuid16(data, (uint16_t)uuid->time_low);
     89  1.1  plunky }
     90  1.1  plunky 
     91  1.1  plunky bool
     92  1.1  plunky sdp_put_uuid16(sdp_data_t *data, uint16_t uuid)
     93  1.1  plunky {
     94  1.1  plunky 
     95  1.1  plunky 	if (data->next + 3 > data->end)
     96  1.1  plunky 		return false;
     97  1.1  plunky 
     98  1.1  plunky 	data->next[0] = SDP_DATA_UUID16;
     99  1.1  plunky 	be16enc(data->next + 1, uuid);
    100  1.1  plunky 	data->next += 3;
    101  1.1  plunky 	return true;
    102  1.1  plunky }
    103  1.1  plunky 
    104  1.1  plunky bool
    105  1.1  plunky sdp_put_uuid32(sdp_data_t *data, uint32_t uuid)
    106  1.1  plunky {
    107  1.1  plunky 
    108  1.1  plunky 	if (data->next + 5 > data->end)
    109  1.1  plunky 		return false;
    110  1.1  plunky 
    111  1.1  plunky 	data->next[0] = SDP_DATA_UUID32;
    112  1.1  plunky 	be32enc(data->next + 1, uuid);
    113  1.1  plunky 	data->next += 5;
    114  1.1  plunky 	return true;
    115  1.1  plunky }
    116  1.1  plunky 
    117  1.1  plunky bool
    118  1.1  plunky sdp_put_uuid128(sdp_data_t *data, const uuid_t *uuid)
    119  1.1  plunky {
    120  1.1  plunky 
    121  1.1  plunky 	if (data->next + 17 > data->end)
    122  1.1  plunky 		return false;
    123  1.1  plunky 
    124  1.1  plunky 	data->next[0] = SDP_DATA_UUID128;
    125  1.1  plunky 	uuid_enc_be(data->next + 1, uuid);
    126  1.1  plunky 	data->next += 17;
    127  1.1  plunky 	return true;
    128  1.1  plunky }
    129  1.1  plunky 
    130  1.1  plunky bool
    131  1.1  plunky sdp_put_bool(sdp_data_t *data, bool value)
    132  1.1  plunky {
    133  1.1  plunky 
    134  1.1  plunky 	if (data->next + 2 > data->end)
    135  1.1  plunky 		return false;
    136  1.1  plunky 
    137  1.1  plunky 	data->next[0] = SDP_DATA_BOOL;
    138  1.1  plunky 	data->next[1] = (value ? 0x01 : 0x00);
    139  1.1  plunky 	data->next += 2;
    140  1.1  plunky 	return true;
    141  1.1  plunky }
    142  1.1  plunky 
    143  1.1  plunky bool
    144  1.1  plunky sdp_put_uint(sdp_data_t *data, uintmax_t value)
    145  1.1  plunky {
    146  1.1  plunky 
    147  1.1  plunky 	if (value > UINT64_MAX)
    148  1.1  plunky 		return false;
    149  1.1  plunky 
    150  1.1  plunky 	if (value > UINT32_MAX)
    151  1.1  plunky 		return sdp_put_uint64(data, (uint64_t)value);
    152  1.1  plunky 
    153  1.1  plunky 	if (value > UINT16_MAX)
    154  1.1  plunky 		return sdp_put_uint32(data, (uint32_t)value);
    155  1.1  plunky 
    156  1.1  plunky 	if (value > UINT8_MAX)
    157  1.1  plunky 		return sdp_put_uint16(data, (uint16_t)value);
    158  1.1  plunky 
    159  1.1  plunky 	return sdp_put_uint8(data, (uint8_t)value);
    160  1.1  plunky }
    161  1.1  plunky 
    162  1.1  plunky bool
    163  1.1  plunky sdp_put_uint8(sdp_data_t *data, uint8_t value)
    164  1.1  plunky {
    165  1.1  plunky 
    166  1.1  plunky 	if (data->next + 2 > data->end)
    167  1.1  plunky 		return false;
    168  1.1  plunky 
    169  1.1  plunky 	data->next[0] = SDP_DATA_UINT8;
    170  1.1  plunky 	data->next[1] = value;
    171  1.1  plunky 	data->next += 2;
    172  1.1  plunky 	return true;
    173  1.1  plunky }
    174  1.1  plunky 
    175  1.1  plunky bool
    176  1.1  plunky sdp_put_uint16(sdp_data_t *data, uint16_t value)
    177  1.1  plunky {
    178  1.1  plunky 
    179  1.1  plunky 	if (data->next + 3 > data->end)
    180  1.1  plunky 		return false;
    181  1.1  plunky 
    182  1.1  plunky 	data->next[0] = SDP_DATA_UINT16;
    183  1.1  plunky 	be16enc(data->next + 1, value);
    184  1.1  plunky 	data->next += 3;
    185  1.1  plunky 	return true;
    186  1.1  plunky }
    187  1.1  plunky 
    188  1.1  plunky bool
    189  1.1  plunky sdp_put_uint32(sdp_data_t *data, uint32_t value)
    190  1.1  plunky {
    191  1.1  plunky 
    192  1.1  plunky 	if (data->next + 5 > data->end)
    193  1.1  plunky 		return false;
    194  1.1  plunky 
    195  1.1  plunky 	data->next[0] = SDP_DATA_UINT32;
    196  1.1  plunky 	be32enc(data->next + 1, value);
    197  1.1  plunky 	data->next += 5;
    198  1.1  plunky 	return true;
    199  1.1  plunky }
    200  1.1  plunky 
    201  1.1  plunky bool
    202  1.1  plunky sdp_put_uint64(sdp_data_t *data, uint64_t value)
    203  1.1  plunky {
    204  1.1  plunky 
    205  1.1  plunky 	if (data->next + 9 > data->end)
    206  1.1  plunky 		return false;
    207  1.1  plunky 
    208  1.1  plunky 	data->next[0] = SDP_DATA_UINT64;
    209  1.1  plunky 	be64enc(data->next + 1, value);
    210  1.1  plunky 	data->next += 9;
    211  1.1  plunky 	return true;
    212  1.1  plunky }
    213  1.1  plunky 
    214  1.1  plunky bool
    215  1.1  plunky sdp_put_int(sdp_data_t *data, intmax_t value)
    216  1.1  plunky {
    217  1.1  plunky 
    218  1.1  plunky 	if (value > INT64_MAX || value < INT64_MIN)
    219  1.1  plunky 		return false;
    220  1.1  plunky 
    221  1.1  plunky 	if (value > INT32_MAX || value < INT32_MIN)
    222  1.1  plunky 		return sdp_put_int64(data, (int64_t)value);
    223  1.1  plunky 
    224  1.1  plunky 	if (value > INT16_MAX || value < INT16_MIN)
    225  1.1  plunky 		return sdp_put_int32(data, (int32_t)value);
    226  1.1  plunky 
    227  1.1  plunky 	if (value > INT8_MAX || value < INT8_MIN)
    228  1.1  plunky 		return sdp_put_int16(data, (int16_t)value);
    229  1.1  plunky 
    230  1.1  plunky 	return sdp_put_int8(data, (int8_t)value);
    231  1.1  plunky }
    232  1.1  plunky 
    233  1.1  plunky bool
    234  1.1  plunky sdp_put_int8(sdp_data_t *data, int8_t value)
    235  1.1  plunky {
    236  1.1  plunky 
    237  1.1  plunky 	if (data->next + 2 > data->end)
    238  1.1  plunky 		return false;
    239  1.1  plunky 
    240  1.1  plunky 	data->next[0] = SDP_DATA_INT8;
    241  1.1  plunky 	data->next[1] = (uint8_t)value;
    242  1.1  plunky 	data->next += 2;
    243  1.1  plunky 	return true;
    244  1.1  plunky }
    245  1.1  plunky 
    246  1.1  plunky bool
    247  1.1  plunky sdp_put_int16(sdp_data_t *data, int16_t value)
    248  1.1  plunky {
    249  1.1  plunky 
    250  1.1  plunky 	if (data->next + 3 > data->end)
    251  1.1  plunky 		return false;
    252  1.1  plunky 
    253  1.1  plunky 	data->next[0] = SDP_DATA_INT16;
    254  1.1  plunky 	be16enc(data->next + 1, (uint16_t)value);
    255  1.1  plunky 	data->next += 3;
    256  1.1  plunky 	return true;
    257  1.1  plunky }
    258  1.1  plunky 
    259  1.1  plunky bool
    260  1.1  plunky sdp_put_int32(sdp_data_t *data, int32_t value)
    261  1.1  plunky {
    262  1.1  plunky 
    263  1.1  plunky 	if (data->next + 5 > data->end)
    264  1.1  plunky 		return false;
    265  1.1  plunky 
    266  1.1  plunky 	data->next[0] = SDP_DATA_INT32;
    267  1.1  plunky 	be32enc(data->next + 1, (uint32_t)value);
    268  1.1  plunky 	data->next += 5;
    269  1.1  plunky 	return true;
    270  1.1  plunky }
    271  1.1  plunky 
    272  1.1  plunky bool
    273  1.1  plunky sdp_put_int64(sdp_data_t *data, int64_t value)
    274  1.1  plunky {
    275  1.1  plunky 
    276  1.1  plunky 	if (data->next + 9 > data->end)
    277  1.1  plunky 		return false;
    278  1.1  plunky 
    279  1.1  plunky 	data->next[0] = SDP_DATA_INT64;
    280  1.1  plunky 	be64enc(data->next + 1, (uint64_t)value);
    281  1.1  plunky 	data->next += 9;
    282  1.1  plunky 	return true;
    283  1.1  plunky }
    284  1.1  plunky 
    285  1.1  plunky static bool
    286  1.1  plunky _sdp_put_ext(uint8_t type, sdp_data_t *data, ssize_t len)
    287  1.1  plunky {
    288  1.1  plunky 	uint8_t *p = data->next;
    289  1.1  plunky 
    290  1.1  plunky 	if (len == -1) {
    291  1.1  plunky 		if (p + 2 > data->end)
    292  1.1  plunky 			return false;
    293  1.1  plunky 
    294  1.1  plunky 		len = data->end - p - 2;
    295  1.1  plunky 
    296  1.1  plunky 		if (len > UINT8_MAX)
    297  1.1  plunky 			len -= 1;
    298  1.1  plunky 
    299  1.1  plunky 		if (len > UINT16_MAX)
    300  1.1  plunky 			len -= 2;
    301  1.1  plunky 	}
    302  1.1  plunky 
    303  1.2  plunky 	if ((size_t)len > UINT32_MAX)
    304  1.1  plunky 		return false;
    305  1.1  plunky 
    306  1.2  plunky 	if ((size_t)len > UINT16_MAX) {
    307  1.1  plunky 		if (p + 5 + len > data->end)
    308  1.1  plunky 			return false;
    309  1.1  plunky 
    310  1.1  plunky 		p[0] = type | SDP_DATA_EXT32;
    311  1.1  plunky 		be32enc(p + 1, (uint32_t)len);
    312  1.1  plunky 		p += 5;
    313  1.2  plunky 	} else if ((size_t)len > UINT8_MAX) {
    314  1.1  plunky 		if (p + 3 + len > data->end)
    315  1.1  plunky 			return false;
    316  1.1  plunky 
    317  1.1  plunky 		p[0] = type | SDP_DATA_EXT16;
    318  1.1  plunky 		be16enc(p + 1, (uint16_t)len);
    319  1.1  plunky 		p += 3;
    320  1.1  plunky 	} else {
    321  1.1  plunky 		if (p + 2 + len > data->end)
    322  1.1  plunky 			return false;
    323  1.1  plunky 
    324  1.1  plunky 		p[0] = type | SDP_DATA_EXT8;
    325  1.1  plunky 		p[1] = (uint8_t)len;
    326  1.1  plunky 		p += 2;
    327  1.1  plunky 	}
    328  1.1  plunky 
    329  1.1  plunky 	data->next = p;
    330  1.1  plunky 	return true;
    331  1.1  plunky }
    332  1.1  plunky 
    333  1.1  plunky bool
    334  1.1  plunky sdp_put_seq(sdp_data_t *data, ssize_t len)
    335  1.1  plunky {
    336  1.1  plunky 
    337  1.1  plunky 	return _sdp_put_ext(SDP_DATA_SEQ, data, len);
    338  1.1  plunky }
    339  1.1  plunky 
    340  1.1  plunky bool
    341  1.1  plunky sdp_put_alt(sdp_data_t *data, ssize_t len)
    342  1.1  plunky {
    343  1.1  plunky 
    344  1.1  plunky 	return _sdp_put_ext(SDP_DATA_ALT, data, len);
    345  1.1  plunky }
    346  1.1  plunky 
    347  1.1  plunky bool
    348  1.1  plunky sdp_put_str(sdp_data_t *data, const char *str, ssize_t len)
    349  1.1  plunky {
    350  1.1  plunky 
    351  1.1  plunky 	if (len == -1)
    352  1.1  plunky 		len = strlen(str);
    353  1.1  plunky 
    354  1.1  plunky 	if (!_sdp_put_ext(SDP_DATA_STR, data, len))
    355  1.1  plunky 		return false;
    356  1.1  plunky 
    357  1.1  plunky 	memcpy(data->next, str, len);
    358  1.1  plunky 	data->next += len;
    359  1.1  plunky 	return true;
    360  1.1  plunky }
    361  1.1  plunky 
    362  1.1  plunky bool
    363  1.1  plunky sdp_put_url(sdp_data_t *data, const char *url, ssize_t len)
    364  1.1  plunky {
    365  1.1  plunky 
    366  1.1  plunky 	if (len == -1)
    367  1.1  plunky 		len = strlen(url);
    368  1.1  plunky 
    369  1.1  plunky 	if (!_sdp_put_ext(SDP_DATA_URL, data, len))
    370  1.1  plunky 		return false;
    371  1.1  plunky 
    372  1.1  plunky 	memcpy(data->next, url, len);
    373  1.1  plunky 	data->next += len;
    374  1.1  plunky 	return true;
    375  1.1  plunky }
    376