Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libbluetooth / sdp_set.c
blobd941b948f34a9c60b8ec640c19704c5fcd741d8e
1 /* $NetBSD: sdp_set.c,v 1.1 2009/05/12 10:05:06 plunky Exp $ */
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_set.c,v 1.1 2009/05/12 10:05:06 plunky Exp $");
35 #include <bluetooth.h>
36 #include <limits.h>
37 #include <sdp.h>
39 /******************************************************************************
40 * sdp_set_xxxx(data, value)
42 * update a value, will fail if data element is not of the correct type
45 bool
46 sdp_set_bool(const sdp_data_t *data, bool value)
48 uint8_t *p = data->next;
50 if (p + 2 > data->end
51 || *p != SDP_DATA_BOOL)
52 return false;
54 *p = (value ? 0x01 : 0x00);
55 return true;
58 bool
59 sdp_set_uint(const sdp_data_t *data, uintmax_t value)
61 uint8_t *p = data->next;
63 if (p + 1 > data->end)
64 return false;
66 switch (*p++) {
67 case SDP_DATA_UINT8:
68 if (p + 1 > data->end
69 || value > UINT8_MAX)
70 return false;
72 *p = (uint8_t)value;
73 break;
75 case SDP_DATA_UINT16:
76 if (p + 2 > data->end
77 || value > UINT16_MAX)
78 return false;
80 be16enc(p, (uint16_t)value);
81 break;
83 case SDP_DATA_UINT32:
84 if (p + 4 > data->end
85 || value > UINT32_MAX)
86 return false;
88 be32enc(p, (uint32_t)value);
89 break;
91 case SDP_DATA_UINT64:
92 if (p + 8 > data->end
93 || value > UINT64_MAX)
94 return false;
96 be64enc(p, (uint64_t)value);
97 break;
99 case SDP_DATA_UINT128:
100 if (p + 16 > data->end)
101 return false;
103 be64enc(p + 0, (uint64_t)0);
104 be64enc(p + 8, (uint64_t)value);
105 break;
107 default:
108 return false;
111 return true;
114 bool
115 sdp_set_int(const sdp_data_t *data, intmax_t value)
117 uint8_t *p = data->next;
119 if (p + 1 > data->end)
120 return false;
122 switch (*p++) {
123 case SDP_DATA_INT8:
124 if (p + 1 > data->end
125 || value > INT8_MAX
126 || value < INT8_MIN)
127 return false;
129 *p = (uint8_t)value;
130 break;
132 case SDP_DATA_INT16:
133 if (p + 2 > data->end
134 || value > INT16_MAX
135 || value < INT16_MIN)
136 return false;
138 be16enc(p, (uint16_t)value);
139 break;
141 case SDP_DATA_INT32:
142 if (p + 4 > data->end
143 || value > INT32_MAX
144 || value < INT32_MIN)
145 return false;
147 be32enc(p, (uint32_t)value);
148 break;
150 case SDP_DATA_INT64:
151 if (p + 8 > data->end
152 || value > INT64_MAX
153 || value < INT64_MIN)
154 return false;
156 be64enc(p, (uint64_t)value);
157 break;
159 case SDP_DATA_INT128:
160 if (p + 16 > data->end)
161 return false;
163 be64enc(p + 0, (uint64_t)0);
164 be64enc(p + 8, (uint64_t)value);
165 break;
167 default:
168 return false;
171 return true;
174 static bool
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)
181 return false;
183 switch (SDP_DATA_SIZE(*p++)) {
184 case SDP_DATA_EXT8:
185 if (len == -1) {
186 if (p + 1 > data->end)
187 return false;
189 len = data->end - p - 1;
190 } else if (p + 1 + len > data->end)
191 return false;
193 if (len > UINT8_MAX)
194 return false;
196 *p = (uint8_t)len;
197 break;
199 case SDP_DATA_EXT16:
200 if (len == -1) {
201 if (p + 2 > data->end)
202 return false;
204 len = data->end - p - 2;
205 } else if (p + 2 + len > data->end)
206 return false;
208 if (len > UINT16_MAX)
209 return false;
211 be16enc(p, (uint16_t)len);
212 break;
214 case SDP_DATA_EXT32:
215 if (len == -1) {
216 if (p + 4 > data->end)
217 return false;
219 len = data->end - p - 4;
220 } else if (p + 4 + len > data->end)
221 return false;
223 if ((size_t)len > UINT32_MAX)
224 return false;
226 be32enc(p, (uint32_t)len);
227 break;
229 default:
230 return false;
233 return true;
236 bool
237 sdp_set_seq(const sdp_data_t *data, ssize_t len)
240 return _sdp_set_ext(SDP_DATA_SEQ, data, len);
243 bool
244 sdp_set_alt(const sdp_data_t *data, ssize_t len)
247 return _sdp_set_ext(SDP_DATA_ALT, data, len);