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 <err.h>
|
---|
44 | #include <async.h>
|
---|
45 |
|
---|
46 | /** Creates a new measured string bundled with a copy of the given string
|
---|
47 | * itself as one memory block.
|
---|
48 | *
|
---|
49 | * If the measured string is being freed, whole memory block is freed.
|
---|
50 | * The measured string should be used only as a constant.
|
---|
51 | *
|
---|
52 | * @param[in] string The initial character string to be stored.
|
---|
53 | * @param[in] length The length of the given string without the terminating
|
---|
54 | * zero ('\0') character. If the length is zero, the actual
|
---|
55 | * length is computed. The given length is used and
|
---|
56 | * appended with the terminating zero ('\0') character
|
---|
57 | * otherwise.
|
---|
58 | * @returns The new bundled character string with measured length.
|
---|
59 | * @returns NULL if there is not enough memory left.
|
---|
60 | */
|
---|
61 | measured_string_ref
|
---|
62 | measured_string_create_bulk(const char *string, size_t length)
|
---|
63 | {
|
---|
64 | measured_string_ref new;
|
---|
65 |
|
---|
66 | if (length == 0) {
|
---|
67 | while (string[length])
|
---|
68 | length++;
|
---|
69 | }
|
---|
70 | new = (measured_string_ref) malloc(sizeof(measured_string_t) +
|
---|
71 | (sizeof(char) * (length + 1)));
|
---|
72 | if (!new)
|
---|
73 | return NULL;
|
---|
74 |
|
---|
75 | new->length = length;
|
---|
76 | new->value = ((char *) new) + sizeof(measured_string_t);
|
---|
77 | // append terminating zero explicitly - to be safe
|
---|
78 | memcpy(new->value, string, new->length);
|
---|
79 | new->value[new->length] = '\0';
|
---|
80 |
|
---|
81 | return new;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Copies the given measured string with separated header and data parts.
|
---|
85 | *
|
---|
86 | * @param[in] source The source measured string to be copied.
|
---|
87 | * @returns The copy of the given measured string.
|
---|
88 | * @returns NULL if the source parameter is NULL.
|
---|
89 | * @returns NULL if there is not enough memory left.
|
---|
90 | */
|
---|
91 | measured_string_ref measured_string_copy(measured_string_ref source)
|
---|
92 | {
|
---|
93 | measured_string_ref new;
|
---|
94 |
|
---|
95 | if (!source)
|
---|
96 | return NULL;
|
---|
97 |
|
---|
98 | new = (measured_string_ref) malloc(sizeof(measured_string_t));
|
---|
99 | if (new) {
|
---|
100 | new->value = (char *) malloc(source->length + 1);
|
---|
101 | if (new->value) {
|
---|
102 | new->length = source->length;
|
---|
103 | memcpy(new->value, source->value, new->length);
|
---|
104 | new->value[new->length] = '\0';
|
---|
105 | return new;
|
---|
106 | }
|
---|
107 | free(new);
|
---|
108 | }
|
---|
109 |
|
---|
110 | return NULL;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** Receives a measured strings array from a calling module.
|
---|
114 | *
|
---|
115 | * Creates the array and the data memory blocks.
|
---|
116 | * This method should be used only while processing IPC messages as the array
|
---|
117 | * size has to be negotiated in advance.
|
---|
118 | *
|
---|
119 | * @param[out] strings The received measured strings array.
|
---|
120 | * @param[out] data The measured strings data. This memory block stores the
|
---|
121 | * actual character strings.
|
---|
122 | * @param[in] count The size of the measured strings array.
|
---|
123 | * @returns EOK on success.
|
---|
124 | * @returns EINVAL if the strings or data parameter is NULL.
|
---|
125 | * @returns EINVAL if the count parameter is zero (0).
|
---|
126 | * @returns EINVAL if the sent array differs in size.
|
---|
127 | * @returns EINVAL if there is inconsistency in sent measured
|
---|
128 | * strings' lengths (should not occur).
|
---|
129 | * @returns ENOMEM if there is not enough memory left.
|
---|
130 | * @returns Other error codes as defined for the
|
---|
131 | * async_data_write_finalize() function.
|
---|
132 | */
|
---|
133 | int
|
---|
134 | measured_strings_receive(measured_string_ref *strings, char **data,
|
---|
135 | size_t count)
|
---|
136 | {
|
---|
137 | ERROR_DECLARE;
|
---|
138 |
|
---|
139 | size_t *lengths;
|
---|
140 | size_t index;
|
---|
141 | size_t length;
|
---|
142 | char *next;
|
---|
143 | ipc_callid_t callid;
|
---|
144 |
|
---|
145 | if ((!strings) || (!data) || (count <= 0))
|
---|
146 | return EINVAL;
|
---|
147 |
|
---|
148 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
149 | if (!lengths)
|
---|
150 | return ENOMEM;
|
---|
151 |
|
---|
152 | if ((!async_data_write_receive(&callid, &length)) ||
|
---|
153 | (length != sizeof(size_t) * (count + 1))) {
|
---|
154 | free(lengths);
|
---|
155 | return EINVAL;
|
---|
156 | }
|
---|
157 | if (ERROR_OCCURRED(async_data_write_finalize(callid, lengths,
|
---|
158 | length))) {
|
---|
159 | free(lengths);
|
---|
160 | return ERROR_CODE;
|
---|
161 | }
|
---|
162 |
|
---|
163 | *data = malloc(lengths[count]);
|
---|
164 | if (!*data) {
|
---|
165 | free(lengths);
|
---|
166 | return ENOMEM;
|
---|
167 | }
|
---|
168 | (*data)[lengths[count] - 1] = '\0';
|
---|
169 |
|
---|
170 | *strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
|
---|
171 | count);
|
---|
172 | if (!*strings) {
|
---|
173 | free(lengths);
|
---|
174 | free(*data);
|
---|
175 | return ENOMEM;
|
---|
176 | }
|
---|
177 |
|
---|
178 | next = *data;
|
---|
179 | for (index = 0; index < count; index++) {
|
---|
180 | (*strings)[index].length = lengths[index];
|
---|
181 | if (lengths[index] > 0) {
|
---|
182 | if (!async_data_write_receive(&callid, &length) ||
|
---|
183 | (length != lengths[index])) {
|
---|
184 | free(*data);
|
---|
185 | free(*strings);
|
---|
186 | free(lengths);
|
---|
187 | return EINVAL;
|
---|
188 | }
|
---|
189 | ERROR_PROPAGATE(async_data_write_finalize(callid, next,
|
---|
190 | lengths[index]));
|
---|
191 | (*strings)[index].value = next;
|
---|
192 | next += lengths[index];
|
---|
193 | *next++ = '\0';
|
---|
194 | } else {
|
---|
195 | (*strings)[index].value = NULL;
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | free(lengths);
|
---|
200 | return EOK;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /** Computes the lengths of the measured strings in the given array.
|
---|
204 | *
|
---|
205 | * @param[in] strings The measured strings array to be processed.
|
---|
206 | * @param[in] count The measured strings array size.
|
---|
207 | * @returns The computed sizes array.
|
---|
208 | * @returns NULL if there is not enough memory left.
|
---|
209 | */
|
---|
210 | static size_t *prepare_lengths(const measured_string_ref strings, size_t count)
|
---|
211 | {
|
---|
212 | size_t *lengths;
|
---|
213 | size_t index;
|
---|
214 | size_t length;
|
---|
215 |
|
---|
216 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
217 | if (!lengths)
|
---|
218 | return NULL;
|
---|
219 |
|
---|
220 | length = 0;
|
---|
221 | for (index = 0; index < count; index++) {
|
---|
222 | lengths[index] = strings[index].length;
|
---|
223 | length += lengths[index] + 1;
|
---|
224 | }
|
---|
225 | lengths[count] = length;
|
---|
226 | return lengths;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Replies the given measured strings array to a calling module.
|
---|
230 | *
|
---|
231 | * This method should be used only while processing IPC messages as the array
|
---|
232 | * size has to be negotiated in advance.
|
---|
233 | *
|
---|
234 | * @param[in] strings The measured strings array to be transferred.
|
---|
235 | * @param[in] count The measured strings array size.
|
---|
236 | * @returns EOK on success.
|
---|
237 | * @returns EINVAL if the strings parameter is NULL.
|
---|
238 | * @returns EINVAL if the count parameter is zero (0).
|
---|
239 | * @returns EINVAL if the calling module does not accept the given
|
---|
240 | * array size.
|
---|
241 | * @returns EINVAL if there is inconsistency in sent measured
|
---|
242 | * strings' lengths (should not occur).
|
---|
243 | * @returns Other error codes as defined for the
|
---|
244 | * async_data_read_finalize() function.
|
---|
245 | */
|
---|
246 | int measured_strings_reply(const measured_string_ref strings, size_t count)
|
---|
247 | {
|
---|
248 | ERROR_DECLARE;
|
---|
249 |
|
---|
250 | size_t *lengths;
|
---|
251 | size_t index;
|
---|
252 | size_t length;
|
---|
253 | ipc_callid_t callid;
|
---|
254 |
|
---|
255 | if ((!strings) || (count <= 0))
|
---|
256 | return EINVAL;
|
---|
257 |
|
---|
258 | lengths = prepare_lengths(strings, count);
|
---|
259 | if (!lengths)
|
---|
260 | return ENOMEM;
|
---|
261 |
|
---|
262 | if (!async_data_read_receive(&callid, &length) ||
|
---|
263 | (length != sizeof(size_t) * (count + 1))) {
|
---|
264 | free(lengths);
|
---|
265 | return EINVAL;
|
---|
266 | }
|
---|
267 | if (ERROR_OCCURRED(async_data_read_finalize(callid, lengths, length))) {
|
---|
268 | free(lengths);
|
---|
269 | return ERROR_CODE;
|
---|
270 | }
|
---|
271 | free(lengths);
|
---|
272 |
|
---|
273 | for (index = 0; index < count; index++) {
|
---|
274 | if (strings[index].length > 0) {
|
---|
275 | if (!async_data_read_receive(&callid, &length) ||
|
---|
276 | (length != strings[index].length)) {
|
---|
277 | return EINVAL;
|
---|
278 | }
|
---|
279 | ERROR_PROPAGATE(async_data_read_finalize(callid,
|
---|
280 | strings[index].value, strings[index].length));
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | return EOK;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /** Receives a measured strings array from another module.
|
---|
288 | *
|
---|
289 | * Creates the array and the data memory blocks.
|
---|
290 | * This method should be used only following other IPC messages as the array
|
---|
291 | * size has to be negotiated in advance.
|
---|
292 | *
|
---|
293 | * @param[in] phone The other module phone.
|
---|
294 | * @param[out] strings The returned measured strings array.
|
---|
295 | * @param[out] data The measured strings data. This memory block stores the
|
---|
296 | * actual character strings.
|
---|
297 | * @param[in] count The size of the measured strings array.
|
---|
298 | * @returns EOK on success.
|
---|
299 | * @returns EINVAL if the strings or data parameter is NULL.
|
---|
300 | * @returns EINVAL if the phone or count parameter is not positive.
|
---|
301 | * @returns EINVAL if the sent array differs in size.
|
---|
302 | * @returns ENOMEM if there is not enough memory left.
|
---|
303 | * @returns Other error codes as defined for the
|
---|
304 | * async_data_read_start() function.
|
---|
305 | */
|
---|
306 | int
|
---|
307 | measured_strings_return(int phone, measured_string_ref *strings, char **data,
|
---|
308 | size_t count)
|
---|
309 | {
|
---|
310 | ERROR_DECLARE;
|
---|
311 |
|
---|
312 | size_t *lengths;
|
---|
313 | size_t index;
|
---|
314 | char *next;
|
---|
315 |
|
---|
316 | if ((phone < 0) || (!strings) || (!data) || (count <= 0))
|
---|
317 | return EINVAL;
|
---|
318 |
|
---|
319 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
320 | if (!lengths)
|
---|
321 | return ENOMEM;
|
---|
322 |
|
---|
323 | if (ERROR_OCCURRED(async_data_read_start(phone, lengths,
|
---|
324 | sizeof(size_t) * (count + 1)))) {
|
---|
325 | free(lengths);
|
---|
326 | return ERROR_CODE;
|
---|
327 | }
|
---|
328 |
|
---|
329 | *data = malloc(lengths[count]);
|
---|
330 | if (!*data) {
|
---|
331 | free(lengths);
|
---|
332 | return ENOMEM;
|
---|
333 | }
|
---|
334 |
|
---|
335 | *strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
|
---|
336 | count);
|
---|
337 | if (!*strings) {
|
---|
338 | free(lengths);
|
---|
339 | free(*data);
|
---|
340 | return ENOMEM;
|
---|
341 | }
|
---|
342 |
|
---|
343 | next = *data;
|
---|
344 | for (index = 0; index < count; index++) {
|
---|
345 | (*strings)[index].length = lengths[index];
|
---|
346 | if (lengths[index] > 0) {
|
---|
347 | ERROR_PROPAGATE(async_data_read_start(phone, next,
|
---|
348 | lengths[index]));
|
---|
349 | (*strings)[index].value = next;
|
---|
350 | next += lengths[index];
|
---|
351 | *next++ = '\0';
|
---|
352 | } else {
|
---|
353 | (*strings)[index].value = NULL;
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | free(lengths);
|
---|
358 | return EOK;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /** Sends the given measured strings array to another module.
|
---|
362 | *
|
---|
363 | * This method should be used only following other IPC messages as the array
|
---|
364 | * size has to be negotiated in advance.
|
---|
365 | *
|
---|
366 | * @param[in] phone The other module phone.
|
---|
367 | * @param[in] strings The measured strings array to be transferred.
|
---|
368 | * @param[in] count The measured strings array size.
|
---|
369 | * @returns EOK on success.
|
---|
370 | * @returns EINVAL if the strings parameter is NULL.
|
---|
371 | * @returns EINVAL if the phone or count parameter is not positive.
|
---|
372 | * @returns Other error codes as defined for the
|
---|
373 | * async_data_write_start() function.
|
---|
374 | */
|
---|
375 | int
|
---|
376 | measured_strings_send(int phone, const measured_string_ref strings,
|
---|
377 | size_t count)
|
---|
378 | {
|
---|
379 | ERROR_DECLARE;
|
---|
380 |
|
---|
381 | size_t *lengths;
|
---|
382 | size_t index;
|
---|
383 |
|
---|
384 | if ((phone < 0) || (!strings) || (count <= 0))
|
---|
385 | return EINVAL;
|
---|
386 |
|
---|
387 | lengths = prepare_lengths(strings, count);
|
---|
388 | if (!lengths)
|
---|
389 | return ENOMEM;
|
---|
390 |
|
---|
391 | if (ERROR_OCCURRED(async_data_write_start(phone, lengths,
|
---|
392 | sizeof(size_t) * (count + 1)))) {
|
---|
393 | free(lengths);
|
---|
394 | return ERROR_CODE;
|
---|
395 | }
|
---|
396 |
|
---|
397 | free(lengths);
|
---|
398 |
|
---|
399 | for (index = 0; index < count; index++) {
|
---|
400 | if (strings[index].length > 0) {
|
---|
401 | ERROR_PROPAGATE(async_data_write_start(phone,
|
---|
402 | strings[index].value, strings[index].length));
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | return EOK;
|
---|
407 | }
|
---|
408 |
|
---|
409 | /** @}
|
---|
410 | */
|
---|
411 |
|
---|