source: mainline/uspace/lib/c/generic/adt/measured_strings.c@ b77ce84

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b77ce84 was 28a3e74, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Fix comment style.

  • Property mode set to 100644
File size: 11.5 KB
RevLine 
[21580dd]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
[2544442]29/** @addtogroup libc
[21580dd]30 * @{
31 */
32
33/** @file
[edba2b6f]34 * Character string with measured length implementation.
35 * @see measured_strings.h
[21580dd]36 */
37
[7390870]38#include <adt/measured_strings.h>
[21580dd]39#include <malloc.h>
40#include <mem.h>
41#include <unistd.h>
[c5b59ce]42#include <errno.h>
[7390870]43#include <async.h>
[21580dd]44
[edba2b6f]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
[d2b1040]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
[edba2b6f]56 * otherwise.
[1bfd3d3]57 * @return The new bundled character string with measured length.
58 * @return NULL if there is not enough memory left.
[edba2b6f]59 */
[4eca056]60measured_string_t *
[61bfc370]61measured_string_create_bulk(const uint8_t *string, size_t length)
[edba2b6f]62{
[4eca056]63 measured_string_t *new;
[21580dd]64
[edba2b6f]65 if (length == 0) {
66 while (string[length])
[d2b1040]67 length++;
[aadf01e]68 }
[4eca056]69 new = (measured_string_t *) malloc(sizeof(measured_string_t) +
[61bfc370]70 (sizeof(uint8_t) * (length + 1)));
[edba2b6f]71 if (!new)
[aadf01e]72 return NULL;
[edba2b6f]73
[21580dd]74 new->length = length;
[61bfc370]75 new->value = ((uint8_t *) new) + sizeof(measured_string_t);
[28a3e74]76 /* Append terminating zero explicitly - to be safe */
[aadf01e]77 memcpy(new->value, string, new->length);
78 new->value[new->length] = '\0';
[edba2b6f]79
[21580dd]80 return new;
81}
82
[edba2b6f]83/** Copies the given measured string with separated header and data parts.
84 *
85 * @param[in] source The source measured string to be copied.
[1bfd3d3]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.
[edba2b6f]89 */
[4eca056]90measured_string_t *measured_string_copy(measured_string_t *source)
[edba2b6f]91{
[4eca056]92 measured_string_t *new;
[21580dd]93
[edba2b6f]94 if (!source)
[aadf01e]95 return NULL;
[edba2b6f]96
[4eca056]97 new = (measured_string_t *) malloc(sizeof(measured_string_t));
[edba2b6f]98 if (new) {
[61bfc370]99 new->value = (uint8_t *) malloc(source->length + 1);
[edba2b6f]100 if (new->value) {
[21580dd]101 new->length = source->length;
[aadf01e]102 memcpy(new->value, source->value, new->length);
103 new->value[new->length] = '\0';
[21580dd]104 return new;
105 }
[d2b1040]106 free(new);
[21580dd]107 }
[edba2b6f]108
[21580dd]109 return NULL;
110}
111
[edba2b6f]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.
[1bfd3d3]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
[edba2b6f]127 * strings' lengths (should not occur).
[1bfd3d3]128 * @return ENOMEM if there is not enough memory left.
129 * @return Other error codes as defined for the
[edba2b6f]130 * async_data_write_finalize() function.
131 */
132int
[61bfc370]133measured_strings_receive(measured_string_t **strings, uint8_t **data,
[edba2b6f]134 size_t count)
135{
136 size_t *lengths;
[aadf01e]137 size_t index;
138 size_t length;
[61bfc370]139 uint8_t *next;
[aadf01e]140 ipc_callid_t callid;
[278b97b1]141 int rc;
[21580dd]142
[edba2b6f]143 if ((!strings) || (!data) || (count <= 0))
[21580dd]144 return EINVAL;
[edba2b6f]145
[aadf01e]146 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
[edba2b6f]147 if (!lengths)
[aadf01e]148 return ENOMEM;
[edba2b6f]149
150 if ((!async_data_write_receive(&callid, &length)) ||
151 (length != sizeof(size_t) * (count + 1))) {
[aadf01e]152 free(lengths);
[21580dd]153 return EINVAL;
154 }
[278b97b1]155 rc = async_data_write_finalize(callid, lengths, length);
156 if (rc != EOK) {
[aadf01e]157 free(lengths);
[278b97b1]158 return rc;
[21580dd]159 }
[0402bda5]160
[aadf01e]161 *data = malloc(lengths[count]);
[d2b1040]162 if (!*data) {
[0402bda5]163 free(lengths);
[aadf01e]164 return ENOMEM;
165 }
166 (*data)[lengths[count] - 1] = '\0';
[edba2b6f]167
[4eca056]168 *strings = (measured_string_t *) malloc(sizeof(measured_string_t) *
[edba2b6f]169 count);
[d2b1040]170 if (!*strings) {
[aadf01e]171 free(lengths);
172 free(*data);
[21580dd]173 return ENOMEM;
174 }
[edba2b6f]175
176 next = *data;
[d2b1040]177 for (index = 0; index < count; index++) {
[aadf01e]178 (*strings)[index].length = lengths[index];
[edba2b6f]179 if (lengths[index] > 0) {
[d2b1040]180 if (!async_data_write_receive(&callid, &length) ||
[edba2b6f]181 (length != lengths[index])) {
[aadf01e]182 free(*data);
183 free(*strings);
184 free(lengths);
[21580dd]185 return EINVAL;
186 }
[278b97b1]187 rc = async_data_write_finalize(callid, next,
188 lengths[index]);
189 if (rc != EOK) {
[f4c8a83f]190 free(*data);
191 free(*strings);
192 free(lengths);
[278b97b1]193 return rc;
[f4c8a83f]194 }
[aadf01e]195 (*strings)[index].value = next;
196 next += lengths[index];
[d2b1040]197 *next++ = '\0';
[edba2b6f]198 } else {
[aadf01e]199 (*strings)[index].value = NULL;
[21580dd]200 }
201 }
[edba2b6f]202
[aadf01e]203 free(lengths);
[21580dd]204 return EOK;
205}
206
[849ed54]207/** Computes the lengths of the measured strings in the given array.
[edba2b6f]208 *
209 * @param[in] strings The measured strings array to be processed.
210 * @param[in] count The measured strings array size.
[1bfd3d3]211 * @return The computed sizes array.
212 * @return NULL if there is not enough memory left.
[849ed54]213 */
[4eca056]214static size_t *prepare_lengths(const measured_string_t *strings, size_t count)
[edba2b6f]215{
216 size_t *lengths;
[849ed54]217 size_t index;
218 size_t length;
219
220 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
[edba2b6f]221 if (!lengths)
[849ed54]222 return NULL;
[edba2b6f]223
[849ed54]224 length = 0;
[d2b1040]225 for (index = 0; index < count; index++) {
[849ed54]226 lengths[index] = strings[index].length;
227 length += lengths[index] + 1;
228 }
229 lengths[count] = length;
230 return lengths;
231}
232
[edba2b6f]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.
[1bfd3d3]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
[edba2b6f]244 * array size.
[1bfd3d3]245 * @return EINVAL if there is inconsistency in sent measured
[edba2b6f]246 * strings' lengths (should not occur).
[1bfd3d3]247 * @return Other error codes as defined for the
[edba2b6f]248 * async_data_read_finalize() function.
249 */
[4eca056]250int measured_strings_reply(const measured_string_t *strings, size_t count)
[edba2b6f]251{
252 size_t *lengths;
[aadf01e]253 size_t index;
254 size_t length;
255 ipc_callid_t callid;
[278b97b1]256 int rc;
[21580dd]257
[edba2b6f]258 if ((!strings) || (count <= 0))
[21580dd]259 return EINVAL;
[edba2b6f]260
[aadf01e]261 lengths = prepare_lengths(strings, count);
[edba2b6f]262 if (!lengths)
[aadf01e]263 return ENOMEM;
[edba2b6f]264
[d2b1040]265 if (!async_data_read_receive(&callid, &length) ||
[edba2b6f]266 (length != sizeof(size_t) * (count + 1))) {
[aadf01e]267 free(lengths);
[21580dd]268 return EINVAL;
269 }
[278b97b1]270 rc = async_data_read_finalize(callid, lengths, length);
271 if (rc != EOK) {
[aadf01e]272 free(lengths);
[278b97b1]273 return rc;
[21580dd]274 }
[aadf01e]275 free(lengths);
[edba2b6f]276
[d2b1040]277 for (index = 0; index < count; index++) {
[edba2b6f]278 if (strings[index].length > 0) {
[d2b1040]279 if (!async_data_read_receive(&callid, &length) ||
[edba2b6f]280 (length != strings[index].length)) {
[21580dd]281 return EINVAL;
282 }
[278b97b1]283 rc = async_data_read_finalize(callid,
284 strings[index].value, strings[index].length);
285 if (rc != EOK)
286 return rc;
[21580dd]287 }
288 }
[edba2b6f]289
[21580dd]290 return EOK;
291}
292
[edba2b6f]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.
[1bfd3d3]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
[edba2b6f]310 * async_data_read_start() function.
311 */
312int
[61bfc370]313measured_strings_return(int phone, measured_string_t **strings, uint8_t **data,
[edba2b6f]314 size_t count)
315{
316 size_t *lengths;
[aadf01e]317 size_t index;
[61bfc370]318 uint8_t *next;
[278b97b1]319 int rc;
[21580dd]320
[d2b1040]321 if ((phone < 0) || (!strings) || (!data) || (count <= 0))
[21580dd]322 return EINVAL;
[edba2b6f]323
[aadf01e]324 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
[edba2b6f]325 if (!lengths)
[aadf01e]326 return ENOMEM;
[edba2b6f]327
[278b97b1]328 rc = async_data_read_start(phone, lengths,
329 sizeof(size_t) * (count + 1));
330 if (rc != EOK) {
[aadf01e]331 free(lengths);
[278b97b1]332 return rc;
[21580dd]333 }
[edba2b6f]334
[aadf01e]335 *data = malloc(lengths[count]);
[d2b1040]336 if (!*data) {
[0402bda5]337 free(lengths);
[aadf01e]338 return ENOMEM;
339 }
[edba2b6f]340
[4eca056]341 *strings = (measured_string_t *) malloc(sizeof(measured_string_t) *
[edba2b6f]342 count);
[d2b1040]343 if (!*strings) {
[aadf01e]344 free(lengths);
345 free(*data);
[21580dd]346 return ENOMEM;
347 }
[edba2b6f]348
349 next = *data;
[d2b1040]350 for (index = 0; index < count; index++) {
[aadf01e]351 (*strings)[index].length = lengths[index];
[edba2b6f]352 if (lengths[index] > 0) {
[278b97b1]353 rc = async_data_read_start(phone, next, lengths[index]);
354 if (rc != EOK) {
[f4c8a83f]355 free(lengths);
356 free(data);
357 free(strings);
[278b97b1]358 return rc;
[f4c8a83f]359 }
[aadf01e]360 (*strings)[index].value = next;
361 next += lengths[index];
[d2b1040]362 *next++ = '\0';
[edba2b6f]363 } else {
[aadf01e]364 (*strings)[index].value = NULL;
[21580dd]365 }
366 }
[edba2b6f]367
[aadf01e]368 free(lengths);
[21580dd]369 return EOK;
370}
371
[edba2b6f]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.
[1bfd3d3]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
[edba2b6f]384 * async_data_write_start() function.
385 */
386int
[4eca056]387measured_strings_send(int phone, const measured_string_t *strings,
[edba2b6f]388 size_t count)
389{
390 size_t *lengths;
[aadf01e]391 size_t index;
[278b97b1]392 int rc;
[21580dd]393
[d2b1040]394 if ((phone < 0) || (!strings) || (count <= 0))
[21580dd]395 return EINVAL;
[edba2b6f]396
[aadf01e]397 lengths = prepare_lengths(strings, count);
[edba2b6f]398 if (!lengths)
[aadf01e]399 return ENOMEM;
[edba2b6f]400
[278b97b1]401 rc = async_data_write_start(phone, lengths,
402 sizeof(size_t) * (count + 1));
403 if (rc != EOK) {
[aadf01e]404 free(lengths);
[278b97b1]405 return rc;
[21580dd]406 }
[edba2b6f]407
[aadf01e]408 free(lengths);
[edba2b6f]409
[d2b1040]410 for (index = 0; index < count; index++) {
[edba2b6f]411 if (strings[index].length > 0) {
[278b97b1]412 rc = async_data_write_start(phone, strings[index].value,
413 strings[index].length);
414 if (rc != EOK)
415 return rc;
[21580dd]416 }
417 }
[edba2b6f]418
[21580dd]419 return EOK;
420}
421
422/** @}
423 */
424
Note: See TracBrowser for help on using the repository browser.