1 | /*
|
---|
2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * Character string with measured length implementation.
|
---|
35 | * @see measured_strings.h
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <adt/measured_strings.h>
|
---|
39 | #include <malloc.h>
|
---|
40 | #include <mem.h>
|
---|
41 | #include <unistd.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <async.h>
|
---|
44 |
|
---|
45 | /** Creates a new measured string bundled with a copy of the given string
|
---|
46 | * itself as one memory block.
|
---|
47 | *
|
---|
48 | * If the measured string is being freed, whole memory block is freed.
|
---|
49 | * The measured string should be used only as a constant.
|
---|
50 | *
|
---|
51 | * @param[in] string The initial character string to be stored.
|
---|
52 | * @param[in] length The length of the given string without the terminating
|
---|
53 | * zero ('\0') character. If the length is zero, the actual
|
---|
54 | * length is computed. The given length is used and
|
---|
55 | * appended with the terminating zero ('\0') character
|
---|
56 | * otherwise.
|
---|
57 | * @return The new bundled character string with measured length.
|
---|
58 | * @return NULL if there is not enough memory left.
|
---|
59 | */
|
---|
60 | measured_string_t *
|
---|
61 | measured_string_create_bulk(const uint8_t *string, size_t length)
|
---|
62 | {
|
---|
63 | measured_string_t *new;
|
---|
64 |
|
---|
65 | if (length == 0) {
|
---|
66 | while (string[length])
|
---|
67 | length++;
|
---|
68 | }
|
---|
69 | new = (measured_string_t *) malloc(sizeof(measured_string_t) +
|
---|
70 | (sizeof(uint8_t) * (length + 1)));
|
---|
71 | if (!new)
|
---|
72 | return NULL;
|
---|
73 |
|
---|
74 | new->length = length;
|
---|
75 | new->value = ((uint8_t *) new) + sizeof(measured_string_t);
|
---|
76 | // append terminating zero explicitly - to be safe
|
---|
77 | memcpy(new->value, string, new->length);
|
---|
78 | new->value[new->length] = '\0';
|
---|
79 |
|
---|
80 | return new;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /** Copies the given measured string with separated header and data parts.
|
---|
84 | *
|
---|
85 | * @param[in] source The source measured string to be copied.
|
---|
86 | * @return The copy of the given measured string.
|
---|
87 | * @return NULL if the source parameter is NULL.
|
---|
88 | * @return NULL if there is not enough memory left.
|
---|
89 | */
|
---|
90 | measured_string_t *measured_string_copy(measured_string_t *source)
|
---|
91 | {
|
---|
92 | measured_string_t *new;
|
---|
93 |
|
---|
94 | if (!source)
|
---|
95 | return NULL;
|
---|
96 |
|
---|
97 | new = (measured_string_t *) malloc(sizeof(measured_string_t));
|
---|
98 | if (new) {
|
---|
99 | new->value = (uint8_t *) malloc(source->length + 1);
|
---|
100 | if (new->value) {
|
---|
101 | new->length = source->length;
|
---|
102 | memcpy(new->value, source->value, new->length);
|
---|
103 | new->value[new->length] = '\0';
|
---|
104 | return new;
|
---|
105 | }
|
---|
106 | free(new);
|
---|
107 | }
|
---|
108 |
|
---|
109 | return NULL;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Receives a measured strings array from a calling module.
|
---|
113 | *
|
---|
114 | * Creates the array and the data memory blocks.
|
---|
115 | * This method should be used only while processing IPC messages as the array
|
---|
116 | * size has to be negotiated in advance.
|
---|
117 | *
|
---|
118 | * @param[out] strings The received measured strings array.
|
---|
119 | * @param[out] data The measured strings data. This memory block stores the
|
---|
120 | * actual character strings.
|
---|
121 | * @param[in] count The size of the measured strings array.
|
---|
122 | * @return EOK on success.
|
---|
123 | * @return EINVAL if the strings or data parameter is NULL.
|
---|
124 | * @return EINVAL if the count parameter is zero (0).
|
---|
125 | * @return EINVAL if the sent array differs in size.
|
---|
126 | * @return EINVAL if there is inconsistency in sent measured
|
---|
127 | * strings' lengths (should not occur).
|
---|
128 | * @return ENOMEM if there is not enough memory left.
|
---|
129 | * @return Other error codes as defined for the
|
---|
130 | * async_data_write_finalize() function.
|
---|
131 | */
|
---|
132 | int
|
---|
133 | measured_strings_receive(measured_string_t **strings, uint8_t **data,
|
---|
134 | size_t count)
|
---|
135 | {
|
---|
136 | size_t *lengths;
|
---|
137 | size_t index;
|
---|
138 | size_t length;
|
---|
139 | uint8_t *next;
|
---|
140 | ipc_callid_t callid;
|
---|
141 | int rc;
|
---|
142 |
|
---|
143 | if ((!strings) || (!data) || (count <= 0))
|
---|
144 | return EINVAL;
|
---|
145 |
|
---|
146 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
147 | if (!lengths)
|
---|
148 | return ENOMEM;
|
---|
149 |
|
---|
150 | if ((!async_data_write_receive(&callid, &length)) ||
|
---|
151 | (length != sizeof(size_t) * (count + 1))) {
|
---|
152 | free(lengths);
|
---|
153 | return EINVAL;
|
---|
154 | }
|
---|
155 | rc = async_data_write_finalize(callid, lengths, length);
|
---|
156 | if (rc != EOK) {
|
---|
157 | free(lengths);
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 |
|
---|
161 | *data = malloc(lengths[count]);
|
---|
162 | if (!*data) {
|
---|
163 | free(lengths);
|
---|
164 | return ENOMEM;
|
---|
165 | }
|
---|
166 | (*data)[lengths[count] - 1] = '\0';
|
---|
167 |
|
---|
168 | *strings = (measured_string_t *) malloc(sizeof(measured_string_t) *
|
---|
169 | count);
|
---|
170 | if (!*strings) {
|
---|
171 | free(lengths);
|
---|
172 | free(*data);
|
---|
173 | return ENOMEM;
|
---|
174 | }
|
---|
175 |
|
---|
176 | next = *data;
|
---|
177 | for (index = 0; index < count; index++) {
|
---|
178 | (*strings)[index].length = lengths[index];
|
---|
179 | if (lengths[index] > 0) {
|
---|
180 | if (!async_data_write_receive(&callid, &length) ||
|
---|
181 | (length != lengths[index])) {
|
---|
182 | free(*data);
|
---|
183 | free(*strings);
|
---|
184 | free(lengths);
|
---|
185 | return EINVAL;
|
---|
186 | }
|
---|
187 | rc = async_data_write_finalize(callid, next,
|
---|
188 | lengths[index]);
|
---|
189 | if (rc != EOK) {
|
---|
190 | free(*data);
|
---|
191 | free(*strings);
|
---|
192 | free(lengths);
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 | (*strings)[index].value = next;
|
---|
196 | next += lengths[index];
|
---|
197 | *next++ = '\0';
|
---|
198 | } else {
|
---|
199 | (*strings)[index].value = NULL;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | free(lengths);
|
---|
204 | return EOK;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /** Computes the lengths of the measured strings in the given array.
|
---|
208 | *
|
---|
209 | * @param[in] strings The measured strings array to be processed.
|
---|
210 | * @param[in] count The measured strings array size.
|
---|
211 | * @return The computed sizes array.
|
---|
212 | * @return NULL if there is not enough memory left.
|
---|
213 | */
|
---|
214 | static size_t *prepare_lengths(const measured_string_t *strings, size_t count)
|
---|
215 | {
|
---|
216 | size_t *lengths;
|
---|
217 | size_t index;
|
---|
218 | size_t length;
|
---|
219 |
|
---|
220 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
221 | if (!lengths)
|
---|
222 | return NULL;
|
---|
223 |
|
---|
224 | length = 0;
|
---|
225 | for (index = 0; index < count; index++) {
|
---|
226 | lengths[index] = strings[index].length;
|
---|
227 | length += lengths[index] + 1;
|
---|
228 | }
|
---|
229 | lengths[count] = length;
|
---|
230 | return lengths;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** Replies the given measured strings array to a calling module.
|
---|
234 | *
|
---|
235 | * This method should be used only while processing IPC messages as the array
|
---|
236 | * size has to be negotiated in advance.
|
---|
237 | *
|
---|
238 | * @param[in] strings The measured strings array to be transferred.
|
---|
239 | * @param[in] count The measured strings array size.
|
---|
240 | * @return EOK on success.
|
---|
241 | * @return EINVAL if the strings parameter is NULL.
|
---|
242 | * @return EINVAL if the count parameter is zero (0).
|
---|
243 | * @return EINVAL if the calling module does not accept the given
|
---|
244 | * array size.
|
---|
245 | * @return EINVAL if there is inconsistency in sent measured
|
---|
246 | * strings' lengths (should not occur).
|
---|
247 | * @return Other error codes as defined for the
|
---|
248 | * async_data_read_finalize() function.
|
---|
249 | */
|
---|
250 | int measured_strings_reply(const measured_string_t *strings, size_t count)
|
---|
251 | {
|
---|
252 | size_t *lengths;
|
---|
253 | size_t index;
|
---|
254 | size_t length;
|
---|
255 | ipc_callid_t callid;
|
---|
256 | int rc;
|
---|
257 |
|
---|
258 | if ((!strings) || (count <= 0))
|
---|
259 | return EINVAL;
|
---|
260 |
|
---|
261 | lengths = prepare_lengths(strings, count);
|
---|
262 | if (!lengths)
|
---|
263 | return ENOMEM;
|
---|
264 |
|
---|
265 | if (!async_data_read_receive(&callid, &length) ||
|
---|
266 | (length != sizeof(size_t) * (count + 1))) {
|
---|
267 | free(lengths);
|
---|
268 | return EINVAL;
|
---|
269 | }
|
---|
270 | rc = async_data_read_finalize(callid, lengths, length);
|
---|
271 | if (rc != EOK) {
|
---|
272 | free(lengths);
|
---|
273 | return rc;
|
---|
274 | }
|
---|
275 | free(lengths);
|
---|
276 |
|
---|
277 | for (index = 0; index < count; index++) {
|
---|
278 | if (strings[index].length > 0) {
|
---|
279 | if (!async_data_read_receive(&callid, &length) ||
|
---|
280 | (length != strings[index].length)) {
|
---|
281 | return EINVAL;
|
---|
282 | }
|
---|
283 | rc = async_data_read_finalize(callid,
|
---|
284 | strings[index].value, strings[index].length);
|
---|
285 | if (rc != EOK)
|
---|
286 | return rc;
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | return EOK;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** Receives a measured strings array from another module.
|
---|
294 | *
|
---|
295 | * Creates the array and the data memory blocks.
|
---|
296 | * This method should be used only following other IPC messages as the array
|
---|
297 | * size has to be negotiated in advance.
|
---|
298 | *
|
---|
299 | * @param[in] phone The other module phone.
|
---|
300 | * @param[out] strings The returned measured strings array.
|
---|
301 | * @param[out] data The measured strings data. This memory block stores the
|
---|
302 | * actual character strings.
|
---|
303 | * @param[in] count The size of the measured strings array.
|
---|
304 | * @return EOK on success.
|
---|
305 | * @return EINVAL if the strings or data parameter is NULL.
|
---|
306 | * @return EINVAL if the phone or count parameter is not positive.
|
---|
307 | * @return EINVAL if the sent array differs in size.
|
---|
308 | * @return ENOMEM if there is not enough memory left.
|
---|
309 | * @return Other error codes as defined for the
|
---|
310 | * async_data_read_start() function.
|
---|
311 | */
|
---|
312 | int
|
---|
313 | measured_strings_return(int phone, measured_string_t **strings, uint8_t **data,
|
---|
314 | size_t count)
|
---|
315 | {
|
---|
316 | size_t *lengths;
|
---|
317 | size_t index;
|
---|
318 | uint8_t *next;
|
---|
319 | int rc;
|
---|
320 |
|
---|
321 | if ((phone < 0) || (!strings) || (!data) || (count <= 0))
|
---|
322 | return EINVAL;
|
---|
323 |
|
---|
324 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
325 | if (!lengths)
|
---|
326 | return ENOMEM;
|
---|
327 |
|
---|
328 | rc = async_data_read_start(phone, lengths,
|
---|
329 | sizeof(size_t) * (count + 1));
|
---|
330 | if (rc != EOK) {
|
---|
331 | free(lengths);
|
---|
332 | return rc;
|
---|
333 | }
|
---|
334 |
|
---|
335 | *data = malloc(lengths[count]);
|
---|
336 | if (!*data) {
|
---|
337 | free(lengths);
|
---|
338 | return ENOMEM;
|
---|
339 | }
|
---|
340 |
|
---|
341 | *strings = (measured_string_t *) malloc(sizeof(measured_string_t) *
|
---|
342 | count);
|
---|
343 | if (!*strings) {
|
---|
344 | free(lengths);
|
---|
345 | free(*data);
|
---|
346 | return ENOMEM;
|
---|
347 | }
|
---|
348 |
|
---|
349 | next = *data;
|
---|
350 | for (index = 0; index < count; index++) {
|
---|
351 | (*strings)[index].length = lengths[index];
|
---|
352 | if (lengths[index] > 0) {
|
---|
353 | rc = async_data_read_start(phone, next, lengths[index]);
|
---|
354 | if (rc != EOK) {
|
---|
355 | free(lengths);
|
---|
356 | free(data);
|
---|
357 | free(strings);
|
---|
358 | return rc;
|
---|
359 | }
|
---|
360 | (*strings)[index].value = next;
|
---|
361 | next += lengths[index];
|
---|
362 | *next++ = '\0';
|
---|
363 | } else {
|
---|
364 | (*strings)[index].value = NULL;
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | free(lengths);
|
---|
369 | return EOK;
|
---|
370 | }
|
---|
371 |
|
---|
372 | /** Sends the given measured strings array to another module.
|
---|
373 | *
|
---|
374 | * This method should be used only following other IPC messages as the array
|
---|
375 | * size has to be negotiated in advance.
|
---|
376 | *
|
---|
377 | * @param[in] phone The other module phone.
|
---|
378 | * @param[in] strings The measured strings array to be transferred.
|
---|
379 | * @param[in] count The measured strings array size.
|
---|
380 | * @return EOK on success.
|
---|
381 | * @return EINVAL if the strings parameter is NULL.
|
---|
382 | * @return EINVAL if the phone or count parameter is not positive.
|
---|
383 | * @return Other error codes as defined for the
|
---|
384 | * async_data_write_start() function.
|
---|
385 | */
|
---|
386 | int
|
---|
387 | measured_strings_send(int phone, const measured_string_t *strings,
|
---|
388 | size_t count)
|
---|
389 | {
|
---|
390 | size_t *lengths;
|
---|
391 | size_t index;
|
---|
392 | int rc;
|
---|
393 |
|
---|
394 | if ((phone < 0) || (!strings) || (count <= 0))
|
---|
395 | return EINVAL;
|
---|
396 |
|
---|
397 | lengths = prepare_lengths(strings, count);
|
---|
398 | if (!lengths)
|
---|
399 | return ENOMEM;
|
---|
400 |
|
---|
401 | rc = async_data_write_start(phone, lengths,
|
---|
402 | sizeof(size_t) * (count + 1));
|
---|
403 | if (rc != EOK) {
|
---|
404 | free(lengths);
|
---|
405 | return rc;
|
---|
406 | }
|
---|
407 |
|
---|
408 | free(lengths);
|
---|
409 |
|
---|
410 | for (index = 0; index < count; index++) {
|
---|
411 | if (strings[index].length > 0) {
|
---|
412 | rc = async_data_write_start(phone, strings[index].value,
|
---|
413 | strings[index].length);
|
---|
414 | if (rc != EOK)
|
---|
415 | return rc;
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | return EOK;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /** @}
|
---|
423 | */
|
---|
424 |
|
---|