Home | History | Annotate | Line # | Download | only in libbluetooth
sdp_set.c revision 1.1
      1  1.1  plunky /*	$NetBSD: sdp_set.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_set.c,v 1.1 2009/05/12 10:05:06 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 
     39  1.1  plunky /******************************************************************************
     40  1.1  plunky  *	sdp_set_xxxx(data, value)
     41  1.1  plunky  *
     42  1.1  plunky  * update a value, will fail if data element is not of the correct type
     43  1.1  plunky  */
     44  1.1  plunky 
     45  1.1  plunky bool
     46  1.1  plunky sdp_set_bool(const sdp_data_t *data, bool value)
     47  1.1  plunky {
     48  1.1  plunky 	uint8_t *p = data->next;
     49  1.1  plunky 
     50  1.1  plunky 	if (p + 2 > data->end
     51  1.1  plunky 	    || *p != SDP_DATA_BOOL)
     52  1.1  plunky 		return false;
     53  1.1  plunky 
     54  1.1  plunky 	*p = (value ? 0x01 : 0x00);
     55  1.1  plunky 	return true;
     56  1.1  plunky }
     57  1.1  plunky 
     58  1.1  plunky bool
     59  1.1  plunky sdp_set_uint(const sdp_data_t *data, uintmax_t value)
     60  1.1  plunky {
     61  1.1  plunky 	uint8_t *p = data->next;
     62  1.1  plunky 
     63  1.1  plunky 	if (p + 1 > data->end)
     64  1.1  plunky 		return false;
     65  1.1  plunky 
     66  1.1  plunky 	switch (*p++) {
     67  1.1  plunky 	case SDP_DATA_UINT8:
     68  1.1  plunky 		if (p + 1 > data->end
     69  1.1  plunky 		    || value > UINT8_MAX)
     70  1.1  plunky 			return false;
     71  1.1  plunky 
     72  1.1  plunky 		*p = (uint8_t)value;
     73  1.1  plunky 		break;
     74  1.1  plunky 
     75  1.1  plunky 	case SDP_DATA_UINT16:
     76  1.1  plunky 		if (p + 2 > data->end
     77  1.1  plunky 		    || value > UINT16_MAX)
     78  1.1  plunky 			return false;
     79  1.1  plunky 
     80  1.1  plunky 		be16enc(p, (uint16_t)value);
     81  1.1  plunky 		break;
     82  1.1  plunky 
     83  1.1  plunky 	case SDP_DATA_UINT32:
     84  1.1  plunky 		if (p + 4 > data->end
     85  1.1  plunky 		    || value > UINT32_MAX)
     86  1.1  plunky 			return false;
     87  1.1  plunky 
     88  1.1  plunky 		be32enc(p, (uint32_t)value);
     89  1.1  plunky 		break;
     90  1.1  plunky 
     91  1.1  plunky 	case SDP_DATA_UINT64:
     92  1.1  plunky 		if (p + 8 > data->end
     93  1.1  plunky 		    || value > UINT64_MAX)
     94  1.1  plunky 			return false;
     95  1.1  plunky 
     96  1.1  plunky 		be64enc(p, (uint64_t)value);
     97  1.1  plunky 		break;
     98  1.1  plunky 
     99  1.1  plunky 	case SDP_DATA_UINT128:
    100  1.1  plunky 		if (p + 16 > data->end)
    101  1.1  plunky 			return false;
    102  1.1  plunky 
    103  1.1  plunky 		be64enc(p + 0, (uint64_t)0);
    104  1.1  plunky 		be64enc(p + 8, (uint64_t)value);
    105  1.1  plunky 		break;
    106  1.1  plunky 
    107  1.1  plunky 	default:
    108  1.1  plunky 		return false;
    109  1.1  plunky 	}
    110  1.1  plunky 
    111  1.1  plunky 	return true;
    112  1.1  plunky }
    113  1.1  plunky 
    114  1.1  plunky bool
    115  1.1  plunky sdp_set_int(const sdp_data_t *data, intmax_t value)
    116  1.1  plunky {
    117  1.1  plunky 	uint8_t *p = data->next;
    118  1.1  plunky 
    119  1.1  plunky 	if (p + 1 > data->end)
    120  1.1  plunky 		return false;
    121  1.1  plunky 
    122  1.1  plunky 	switch (*p++) {
    123  1.1  plunky 	case SDP_DATA_INT8:
    124  1.1  plunky 		if (p + 1 > data->end
    125  1.1  plunky 		    || value > INT8_MAX
    126  1.1  plunky 		    || value < INT8_MIN)
    127  1.1  plunky 			return false;
    128  1.1  plunky 
    129  1.1  plunky 		*p = (uint8_t)value;
    130  1.1  plunky 		break;
    131  1.1  plunky 
    132  1.1  plunky 	case SDP_DATA_INT16:
    133  1.1  plunky 		if (p + 2 > data->end
    134  1.1  plunky 		    || value > INT16_MAX
    135  1.1  plunky 		    || value < INT16_MIN)
    136  1.1  plunky 			return false;
    137  1.1  plunky 
    138  1.1  plunky 		be16enc(p, (uint16_t)value);
    139  1.1  plunky 		break;
    140  1.1  plunky 
    141  1.1  plunky 	case SDP_DATA_INT32:
    142  1.1  plunky 		if (p + 4 > data->end
    143  1.1  plunky 		    || value > INT32_MAX
    144  1.1  plunky 		    || value < INT32_MIN)
    145  1.1  plunky 			return false;
    146  1.1  plunky 
    147  1.1  plunky 		be32enc(p, (uint32_t)value);
    148  1.1  plunky 		break;
    149  1.1  plunky 
    150  1.1  plunky 	case SDP_DATA_INT64:
    151  1.1  plunky 		if (p + 8 > data->end
    152  1.1  plunky 		    || value > INT64_MAX
    153  1.1  plunky 		    || value < INT64_MIN)
    154  1.1  plunky 			return false;
    155  1.1  plunky 
    156  1.1  plunky 		be64enc(p, (uint64_t)value);
    157  1.1  plunky 		break;
    158  1.1  plunky 
    159  1.1  plunky 	case SDP_DATA_INT128:
    160  1.1  plunky 		if (p + 16 > data->end)
    161  1.1  plunky 			return false;
    162  1.1  plunky 
    163  1.1  plunky 		be64enc(p + 0, (uint64_t)0);
    164  1.1  plunky 		be64enc(p + 8, (uint64_t)value);
    165  1.1  plunky 		break;
    166  1.1  plunky 
    167  1.1  plunky 	default:
    168  1.1  plunky 		return false;
    169  1.1  plunky 	}
    170  1.1  plunky 
    171  1.1  plunky 	return true;
    172  1.1  plunky }
    173  1.1  plunky 
    174  1.1  plunky static bool
    175  1.1  plunky _sdp_set_ext(uint8_t type, const sdp_data_t *data, ssize_t len)
    176  1.1  plunky {
    177  1.1  plunky 	uint8_t *p = data->next;
    178  1.1  plunky 
    179  1.1  plunky 	if (p + 1 > data->end
    180  1.1  plunky 	    || SDP_DATA_TYPE(*p) != type)
    181  1.1  plunky 		return false;
    182  1.1  plunky 
    183  1.1  plunky 	switch (SDP_DATA_SIZE(*p++)) {
    184  1.1  plunky 	case SDP_DATA_EXT8:
    185  1.1  plunky 		if (len == -1) {
    186  1.1  plunky 			if (p + 1 > data->end)
    187  1.1  plunky 				return false;
    188  1.1  plunky 
    189  1.1  plunky 			len = data->end - p - 1;
    190  1.1  plunky 		} else if (p + 1 + len > data->end)
    191  1.1  plunky 			return false;
    192  1.1  plunky 
    193  1.1  plunky 		if (len > UINT8_MAX)
    194  1.1  plunky 			return false;
    195  1.1  plunky 
    196  1.1  plunky 		*p = (uint8_t)len;
    197  1.1  plunky 		break;
    198  1.1  plunky 
    199  1.1  plunky 	case SDP_DATA_EXT16:
    200  1.1  plunky 		if (len == -1) {
    201  1.1  plunky 			if (p + 2 > data->end)
    202  1.1  plunky 				return false;
    203  1.1  plunky 
    204  1.1  plunky 			len = data->end - p - 2;
    205  1.1  plunky 		} else if (p + 2 + len > data->end)
    206  1.1  plunky 			return false;
    207  1.1  plunky 
    208  1.1  plunky 		if (len > UINT16_MAX)
    209  1.1  plunky 			return false;
    210  1.1  plunky 
    211  1.1  plunky 		be16enc(p, (uint16_t)len);
    212  1.1  plunky 		break;
    213  1.1  plunky 
    214  1.1  plunky 	case SDP_DATA_EXT32:
    215  1.1  plunky 		if (len == -1) {
    216  1.1  plunky 			if (p + 4 > data->end)
    217  1.1  plunky 				return false;
    218  1.1  plunky 
    219  1.1  plunky 			len = data->end - p - 4;
    220  1.1  plunky 		} else if (p + 4 + len > data->end)
    221  1.1  plunky 			return false;
    222  1.1  plunky 
    223  1.1  plunky 		if (len > UINT32_MAX)
    224  1.1  plunky 			return false;
    225  1.1  plunky 
    226  1.1  plunky 		be32enc(p, (uint32_t)len);
    227  1.1  plunky 		break;
    228  1.1  plunky 
    229  1.1  plunky 	default:
    230  1.1  plunky 		return false;
    231  1.1  plunky 	}
    232  1.1  plunky 
    233  1.1  plunky 	return true;
    234  1.1  plunky }
    235  1.1  plunky 
    236  1.1  plunky bool
    237  1.1  plunky sdp_set_seq(const sdp_data_t *data, ssize_t len)
    238  1.1  plunky {
    239  1.1  plunky 
    240  1.1  plunky 	return _sdp_set_ext(SDP_DATA_SEQ, data, len);
    241  1.1  plunky }
    242  1.1  plunky 
    243  1.1  plunky bool
    244  1.1  plunky sdp_set_alt(const sdp_data_t *data, ssize_t len)
    245  1.1  plunky {
    246  1.1  plunky 
    247  1.1  plunky 	return _sdp_set_ext(SDP_DATA_ALT, data, len);
    248  1.1  plunky }
    249