1 /* $NetBSD: sdp_set.c,v 1.1 2009/05/12 10:05:06 plunky Exp $ */
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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_set.c,v 1.1 2009/05/12 10:05:06 plunky Exp $");
35 #include <bluetooth.h>
39 /******************************************************************************
40 * sdp_set_xxxx(data, value)
42 * update a value, will fail if data element is not of the correct type
46 sdp_set_bool(const sdp_data_t
*data
, bool value
)
48 uint8_t *p
= data
->next
;
51 || *p
!= SDP_DATA_BOOL
)
54 *p
= (value
? 0x01 : 0x00);
59 sdp_set_uint(const sdp_data_t
*data
, uintmax_t value
)
61 uint8_t *p
= data
->next
;
63 if (p
+ 1 > data
->end
)
77 || value
> UINT16_MAX
)
80 be16enc(p
, (uint16_t)value
);
85 || value
> UINT32_MAX
)
88 be32enc(p
, (uint32_t)value
);
93 || value
> UINT64_MAX
)
96 be64enc(p
, (uint64_t)value
);
99 case SDP_DATA_UINT128
:
100 if (p
+ 16 > data
->end
)
103 be64enc(p
+ 0, (uint64_t)0);
104 be64enc(p
+ 8, (uint64_t)value
);
115 sdp_set_int(const sdp_data_t
*data
, intmax_t value
)
117 uint8_t *p
= data
->next
;
119 if (p
+ 1 > data
->end
)
124 if (p
+ 1 > data
->end
133 if (p
+ 2 > data
->end
135 || value
< INT16_MIN
)
138 be16enc(p
, (uint16_t)value
);
142 if (p
+ 4 > data
->end
144 || value
< INT32_MIN
)
147 be32enc(p
, (uint32_t)value
);
151 if (p
+ 8 > data
->end
153 || value
< INT64_MIN
)
156 be64enc(p
, (uint64_t)value
);
159 case SDP_DATA_INT128
:
160 if (p
+ 16 > data
->end
)
163 be64enc(p
+ 0, (uint64_t)0);
164 be64enc(p
+ 8, (uint64_t)value
);
175 _sdp_set_ext(uint8_t type
, const sdp_data_t
*data
, ssize_t len
)
177 uint8_t *p
= data
->next
;
179 if (p
+ 1 > data
->end
180 || SDP_DATA_TYPE(*p
) != type
)
183 switch (SDP_DATA_SIZE(*p
++)) {
186 if (p
+ 1 > data
->end
)
189 len
= data
->end
- p
- 1;
190 } else if (p
+ 1 + len
> data
->end
)
201 if (p
+ 2 > data
->end
)
204 len
= data
->end
- p
- 2;
205 } else if (p
+ 2 + len
> data
->end
)
208 if (len
> UINT16_MAX
)
211 be16enc(p
, (uint16_t)len
);
216 if (p
+ 4 > data
->end
)
219 len
= data
->end
- p
- 4;
220 } else if (p
+ 4 + len
> data
->end
)
223 if ((size_t)len
> UINT32_MAX
)
226 be32enc(p
, (uint32_t)len
);
237 sdp_set_seq(const sdp_data_t
*data
, ssize_t len
)
240 return _sdp_set_ext(SDP_DATA_SEQ
, data
, len
);
244 sdp_set_alt(const sdp_data_t
*data
, ssize_t len
)
247 return _sdp_set_ext(SDP_DATA_ALT
, data
, len
);