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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6b82009 was 6b82009, checked in by Martin Decky <martin@…>, 14 years ago

networking stack: convert to the new async framework

  • Property mode set to 100644
File size: 11.4 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 *
[6b82009]299 * @param[in] exch Exchange.
[edba2b6f]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.
[6b82009]306 * @return EINVAL if the exch or count parameter is invalid.
[1bfd3d3]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 */
[6b82009]312int measured_strings_return(async_exch_t *exch, measured_string_t **strings,
313 uint8_t **data, size_t count)
[edba2b6f]314{
315 size_t *lengths;
[aadf01e]316 size_t index;
[61bfc370]317 uint8_t *next;
[278b97b1]318 int rc;
[21580dd]319
[6b82009]320 if ((exch == NULL) || (!strings) || (!data) || (count <= 0))
[21580dd]321 return EINVAL;
[edba2b6f]322
[aadf01e]323 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
[edba2b6f]324 if (!lengths)
[aadf01e]325 return ENOMEM;
[edba2b6f]326
[6b82009]327 rc = async_data_read_start(exch, lengths,
[278b97b1]328 sizeof(size_t) * (count + 1));
329 if (rc != EOK) {
[aadf01e]330 free(lengths);
[278b97b1]331 return rc;
[21580dd]332 }
[edba2b6f]333
[aadf01e]334 *data = malloc(lengths[count]);
[d2b1040]335 if (!*data) {
[0402bda5]336 free(lengths);
[aadf01e]337 return ENOMEM;
338 }
[edba2b6f]339
[4eca056]340 *strings = (measured_string_t *) malloc(sizeof(measured_string_t) *
[edba2b6f]341 count);
[d2b1040]342 if (!*strings) {
[aadf01e]343 free(lengths);
344 free(*data);
[21580dd]345 return ENOMEM;
346 }
[edba2b6f]347
348 next = *data;
[d2b1040]349 for (index = 0; index < count; index++) {
[aadf01e]350 (*strings)[index].length = lengths[index];
[edba2b6f]351 if (lengths[index] > 0) {
[6b82009]352 rc = async_data_read_start(exch, next, lengths[index]);
[278b97b1]353 if (rc != EOK) {
[f4c8a83f]354 free(lengths);
355 free(data);
356 free(strings);
[278b97b1]357 return rc;
[f4c8a83f]358 }
[aadf01e]359 (*strings)[index].value = next;
360 next += lengths[index];
[d2b1040]361 *next++ = '\0';
[edba2b6f]362 } else {
[aadf01e]363 (*strings)[index].value = NULL;
[21580dd]364 }
365 }
[edba2b6f]366
[aadf01e]367 free(lengths);
[21580dd]368 return EOK;
369}
370
[edba2b6f]371/** Sends the given measured strings array to another module.
372 *
373 * This method should be used only following other IPC messages as the array
374 * size has to be negotiated in advance.
375 *
[6b82009]376 * @param[in] exch Exchange.
[edba2b6f]377 * @param[in] strings The measured strings array to be transferred.
378 * @param[in] count The measured strings array size.
[1bfd3d3]379 * @return EOK on success.
380 * @return EINVAL if the strings parameter is NULL.
[6b82009]381 * @return EINVAL if the exch or count parameter is invalid.
[1bfd3d3]382 * @return Other error codes as defined for the
[edba2b6f]383 * async_data_write_start() function.
384 */
[6b82009]385int measured_strings_send(async_exch_t *exch, const measured_string_t *strings,
[edba2b6f]386 size_t count)
387{
388 size_t *lengths;
[aadf01e]389 size_t index;
[278b97b1]390 int rc;
[21580dd]391
[6b82009]392 if ((exch == NULL) || (!strings) || (count <= 0))
[21580dd]393 return EINVAL;
[edba2b6f]394
[aadf01e]395 lengths = prepare_lengths(strings, count);
[edba2b6f]396 if (!lengths)
[aadf01e]397 return ENOMEM;
[edba2b6f]398
[6b82009]399 rc = async_data_write_start(exch, lengths,
[278b97b1]400 sizeof(size_t) * (count + 1));
401 if (rc != EOK) {
[aadf01e]402 free(lengths);
[278b97b1]403 return rc;
[21580dd]404 }
[edba2b6f]405
[aadf01e]406 free(lengths);
[edba2b6f]407
[d2b1040]408 for (index = 0; index < count; index++) {
[edba2b6f]409 if (strings[index].length > 0) {
[6b82009]410 rc = async_data_write_start(exch, strings[index].value,
[278b97b1]411 strings[index].length);
412 if (rc != EOK)
413 return rc;
[21580dd]414 }
415 }
[edba2b6f]416
[21580dd]417 return EOK;
418}
419
420/** @}
421 */
Note: See TracBrowser for help on using the repository browser.