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