[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 |
|
---|
| 29 | /** @addtogroup net
|
---|
| 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>
|
---|
| 43 | #include <err.h>
|
---|
[7390870] | 44 | #include <async.h>
|
---|
[21580dd] | 45 |
|
---|
[edba2b6f] | 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 | {
|
---|
[aadf01e] | 64 | measured_string_ref new;
|
---|
[21580dd] | 65 |
|
---|
[edba2b6f] | 66 | if (length == 0) {
|
---|
| 67 | while (string[length])
|
---|
| 68 | ++length;
|
---|
[aadf01e] | 69 | }
|
---|
[edba2b6f] | 70 | new = (measured_string_ref) malloc(sizeof(measured_string_t) +
|
---|
| 71 | (sizeof(char) * (length + 1)));
|
---|
| 72 | if (!new)
|
---|
[aadf01e] | 73 | return NULL;
|
---|
[edba2b6f] | 74 |
|
---|
[21580dd] | 75 | new->length = length;
|
---|
[aadf01e] | 76 | new->value = ((char *) new) + sizeof(measured_string_t);
|
---|
[21580dd] | 77 | // append terminating zero explicitly - to be safe
|
---|
[aadf01e] | 78 | memcpy(new->value, string, new->length);
|
---|
| 79 | new->value[new->length] = '\0';
|
---|
[edba2b6f] | 80 |
|
---|
[21580dd] | 81 | return new;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[edba2b6f] | 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 | {
|
---|
[aadf01e] | 93 | measured_string_ref new;
|
---|
[21580dd] | 94 |
|
---|
[edba2b6f] | 95 | if (!source)
|
---|
[aadf01e] | 96 | return NULL;
|
---|
[edba2b6f] | 97 |
|
---|
[aadf01e] | 98 | new = (measured_string_ref) malloc(sizeof(measured_string_t));
|
---|
[edba2b6f] | 99 | if (new) {
|
---|
[aadf01e] | 100 | new->value = (char *) malloc(source->length + 1);
|
---|
[edba2b6f] | 101 | if (new->value) {
|
---|
[21580dd] | 102 | new->length = source->length;
|
---|
[aadf01e] | 103 | memcpy(new->value, source->value, new->length);
|
---|
| 104 | new->value[new->length] = '\0';
|
---|
[21580dd] | 105 | return new;
|
---|
[edba2b6f] | 106 | } else {
|
---|
[aadf01e] | 107 | free(new);
|
---|
[21580dd] | 108 | }
|
---|
| 109 | }
|
---|
[edba2b6f] | 110 |
|
---|
[21580dd] | 111 | return NULL;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[edba2b6f] | 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 | {
|
---|
[21580dd] | 138 | ERROR_DECLARE;
|
---|
| 139 |
|
---|
[edba2b6f] | 140 | size_t *lengths;
|
---|
[aadf01e] | 141 | size_t index;
|
---|
| 142 | size_t length;
|
---|
[edba2b6f] | 143 | char *next;
|
---|
[aadf01e] | 144 | ipc_callid_t callid;
|
---|
[21580dd] | 145 |
|
---|
[edba2b6f] | 146 | if ((!strings) || (!data) || (count <= 0))
|
---|
[21580dd] | 147 | return EINVAL;
|
---|
[edba2b6f] | 148 |
|
---|
[aadf01e] | 149 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
[edba2b6f] | 150 | if (!lengths)
|
---|
[aadf01e] | 151 | return ENOMEM;
|
---|
[edba2b6f] | 152 |
|
---|
| 153 | if ((!async_data_write_receive(&callid, &length)) ||
|
---|
| 154 | (length != sizeof(size_t) * (count + 1))) {
|
---|
[aadf01e] | 155 | free(lengths);
|
---|
[21580dd] | 156 | return EINVAL;
|
---|
| 157 | }
|
---|
[edba2b6f] | 158 | if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths,
|
---|
| 159 | sizeof(size_t) * (count + 1)))) {
|
---|
[aadf01e] | 160 | free(lengths);
|
---|
[21580dd] | 161 | return ERROR_CODE;
|
---|
| 162 | }
|
---|
[aadf01e] | 163 | *data = malloc(lengths[count]);
|
---|
[edba2b6f] | 164 | if (!(*data)) {
|
---|
[aadf01e] | 165 | return ENOMEM;
|
---|
| 166 | }
|
---|
| 167 | (*data)[lengths[count] - 1] = '\0';
|
---|
[edba2b6f] | 168 |
|
---|
| 169 | *strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
|
---|
| 170 | count);
|
---|
| 171 | if (!(*strings)) {
|
---|
[aadf01e] | 172 | free(lengths);
|
---|
| 173 | free(*data);
|
---|
[21580dd] | 174 | return ENOMEM;
|
---|
| 175 | }
|
---|
[edba2b6f] | 176 |
|
---|
| 177 | next = *data;
|
---|
| 178 | for (index = 0; index < count; ++index) {
|
---|
[aadf01e] | 179 | (*strings)[index].length = lengths[index];
|
---|
[edba2b6f] | 180 | if (lengths[index] > 0) {
|
---|
| 181 | if ((!async_data_write_receive(&callid, &length)) ||
|
---|
| 182 | (length != lengths[index])) {
|
---|
[aadf01e] | 183 | free(*data);
|
---|
| 184 | free(*strings);
|
---|
| 185 | free(lengths);
|
---|
[21580dd] | 186 | return EINVAL;
|
---|
| 187 | }
|
---|
[edba2b6f] | 188 | ERROR_PROPAGATE(async_data_write_finalize(callid, next,
|
---|
| 189 | lengths[index]));
|
---|
[aadf01e] | 190 | (*strings)[index].value = next;
|
---|
| 191 | next += lengths[index];
|
---|
| 192 | *next = '\0';
|
---|
[edba2b6f] | 193 | ++next;
|
---|
| 194 | } else {
|
---|
[aadf01e] | 195 | (*strings)[index].value = NULL;
|
---|
[21580dd] | 196 | }
|
---|
| 197 | }
|
---|
[edba2b6f] | 198 |
|
---|
[aadf01e] | 199 | free(lengths);
|
---|
[21580dd] | 200 | return EOK;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[849ed54] | 203 | /** Computes the lengths of the measured strings in the given array.
|
---|
[edba2b6f] | 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.
|
---|
[849ed54] | 209 | */
|
---|
[edba2b6f] | 210 | static size_t *prepare_lengths(const measured_string_ref strings, size_t count)
|
---|
| 211 | {
|
---|
| 212 | size_t *lengths;
|
---|
[849ed54] | 213 | size_t index;
|
---|
| 214 | size_t length;
|
---|
| 215 |
|
---|
| 216 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
[edba2b6f] | 217 | if (!lengths)
|
---|
[849ed54] | 218 | return NULL;
|
---|
[edba2b6f] | 219 |
|
---|
[849ed54] | 220 | length = 0;
|
---|
[edba2b6f] | 221 | for (index = 0; index < count; ++ index) {
|
---|
[849ed54] | 222 | lengths[index] = strings[index].length;
|
---|
| 223 | length += lengths[index] + 1;
|
---|
| 224 | }
|
---|
| 225 | lengths[count] = length;
|
---|
| 226 | return lengths;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[edba2b6f] | 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 | {
|
---|
[21580dd] | 248 | ERROR_DECLARE;
|
---|
| 249 |
|
---|
[edba2b6f] | 250 | size_t *lengths;
|
---|
[aadf01e] | 251 | size_t index;
|
---|
| 252 | size_t length;
|
---|
| 253 | ipc_callid_t callid;
|
---|
[21580dd] | 254 |
|
---|
[edba2b6f] | 255 | if ((!strings) || (count <= 0))
|
---|
[21580dd] | 256 | return EINVAL;
|
---|
[edba2b6f] | 257 |
|
---|
[aadf01e] | 258 | lengths = prepare_lengths(strings, count);
|
---|
[edba2b6f] | 259 | if (!lengths)
|
---|
[aadf01e] | 260 | return ENOMEM;
|
---|
[edba2b6f] | 261 |
|
---|
| 262 | if ((!async_data_read_receive(&callid, &length)) ||
|
---|
| 263 | (length != sizeof(size_t) * (count + 1))) {
|
---|
[aadf01e] | 264 | free(lengths);
|
---|
[21580dd] | 265 | return EINVAL;
|
---|
| 266 | }
|
---|
[edba2b6f] | 267 | if (ERROR_OCCURRED(async_data_read_finalize(callid, lengths,
|
---|
| 268 | sizeof(size_t) * (count + 1)))) {
|
---|
[aadf01e] | 269 | free(lengths);
|
---|
[21580dd] | 270 | return ERROR_CODE;
|
---|
| 271 | }
|
---|
[aadf01e] | 272 | free(lengths);
|
---|
[edba2b6f] | 273 |
|
---|
| 274 | for (index = 0; index < count; ++ index) {
|
---|
| 275 | if (strings[index].length > 0) {
|
---|
| 276 | if((!async_data_read_receive(&callid, &length)) ||
|
---|
| 277 | (length != strings[index].length)) {
|
---|
[21580dd] | 278 | return EINVAL;
|
---|
| 279 | }
|
---|
[edba2b6f] | 280 | ERROR_PROPAGATE(async_data_read_finalize(callid,
|
---|
| 281 | strings[index].value, strings[index].length));
|
---|
[21580dd] | 282 | }
|
---|
| 283 | }
|
---|
[edba2b6f] | 284 |
|
---|
[21580dd] | 285 | return EOK;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[edba2b6f] | 288 | /** Receives a measured strings array from another module.
|
---|
| 289 | *
|
---|
| 290 | * Creates the array and the data memory blocks.
|
---|
| 291 | * This method should be used only following other IPC messages as the array
|
---|
| 292 | * size has to be negotiated in advance.
|
---|
| 293 | *
|
---|
| 294 | * @param[in] phone The other module phone.
|
---|
| 295 | * @param[out] strings The returned measured strings array.
|
---|
| 296 | * @param[out] data The measured strings data. This memory block stores the
|
---|
| 297 | * actual character strings.
|
---|
| 298 | * @param[in] count The size of the measured strings array.
|
---|
| 299 | * @returns EOK on success.
|
---|
| 300 | * @returns EINVAL if the strings or data parameter is NULL.
|
---|
| 301 | * @returns EINVAL if the phone or count parameter is not positive.
|
---|
| 302 | * @returns EINVAL if the sent array differs in size.
|
---|
| 303 | * @returns ENOMEM if there is not enough memory left.
|
---|
| 304 | * @returns Other error codes as defined for the
|
---|
| 305 | * async_data_read_start() function.
|
---|
| 306 | */
|
---|
| 307 | int
|
---|
| 308 | measured_strings_return(int phone, measured_string_ref *strings, char **data,
|
---|
| 309 | size_t count)
|
---|
| 310 | {
|
---|
[21580dd] | 311 | ERROR_DECLARE;
|
---|
| 312 |
|
---|
[edba2b6f] | 313 | size_t *lengths;
|
---|
[aadf01e] | 314 | size_t index;
|
---|
[edba2b6f] | 315 | char *next;
|
---|
[21580dd] | 316 |
|
---|
[edba2b6f] | 317 | if ((phone <= 0) || (!strings) || (!data) || (count <= 0))
|
---|
[21580dd] | 318 | return EINVAL;
|
---|
[edba2b6f] | 319 |
|
---|
[aadf01e] | 320 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
[edba2b6f] | 321 | if (!lengths)
|
---|
[aadf01e] | 322 | return ENOMEM;
|
---|
[edba2b6f] | 323 |
|
---|
| 324 | if (ERROR_OCCURRED(async_data_read_start(phone, lengths,
|
---|
| 325 | sizeof(size_t) * (count + 1)))) {
|
---|
[aadf01e] | 326 | free(lengths);
|
---|
[21580dd] | 327 | return ERROR_CODE;
|
---|
| 328 | }
|
---|
[edba2b6f] | 329 |
|
---|
[aadf01e] | 330 | *data = malloc(lengths[count]);
|
---|
[edba2b6f] | 331 | if (!(*data)) {
|
---|
[aadf01e] | 332 | return ENOMEM;
|
---|
| 333 | }
|
---|
[edba2b6f] | 334 |
|
---|
| 335 | *strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
|
---|
| 336 | count);
|
---|
| 337 | if (!(*strings)) {
|
---|
[aadf01e] | 338 | free(lengths);
|
---|
| 339 | free(*data);
|
---|
[21580dd] | 340 | return ENOMEM;
|
---|
| 341 | }
|
---|
[edba2b6f] | 342 |
|
---|
| 343 | next = *data;
|
---|
| 344 | for (index = 0; index < count; ++ index) {
|
---|
[aadf01e] | 345 | (*strings)[index].length = lengths[index];
|
---|
[edba2b6f] | 346 | if (lengths[index] > 0) {
|
---|
| 347 | ERROR_PROPAGATE(async_data_read_start(phone, next,
|
---|
| 348 | lengths[index]));
|
---|
[aadf01e] | 349 | (*strings)[index].value = next;
|
---|
| 350 | next += lengths[index];
|
---|
| 351 | *next = '\0';
|
---|
[21580dd] | 352 | ++ next;
|
---|
[edba2b6f] | 353 | } else {
|
---|
[aadf01e] | 354 | (*strings)[index].value = NULL;
|
---|
[21580dd] | 355 | }
|
---|
| 356 | }
|
---|
[edba2b6f] | 357 |
|
---|
[aadf01e] | 358 | free(lengths);
|
---|
[21580dd] | 359 | return EOK;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[edba2b6f] | 362 | /** Sends the given measured strings array to another module.
|
---|
| 363 | *
|
---|
| 364 | * This method should be used only following other IPC messages as the array
|
---|
| 365 | * size has to be negotiated in advance.
|
---|
| 366 | *
|
---|
| 367 | * @param[in] phone The other module phone.
|
---|
| 368 | * @param[in] strings The measured strings array to be transferred.
|
---|
| 369 | * @param[in] count The measured strings array size.
|
---|
| 370 | * @returns EOK on success.
|
---|
| 371 | * @returns EINVAL if the strings parameter is NULL.
|
---|
| 372 | * @returns EINVAL if the phone or count parameter is not positive.
|
---|
| 373 | * @returns Other error codes as defined for the
|
---|
| 374 | * async_data_write_start() function.
|
---|
| 375 | */
|
---|
| 376 | int
|
---|
| 377 | measured_strings_send(int phone, const measured_string_ref strings,
|
---|
| 378 | size_t count)
|
---|
| 379 | {
|
---|
[21580dd] | 380 | ERROR_DECLARE;
|
---|
| 381 |
|
---|
[edba2b6f] | 382 | size_t *lengths;
|
---|
[aadf01e] | 383 | size_t index;
|
---|
[21580dd] | 384 |
|
---|
[edba2b6f] | 385 | if ((phone <= 0) || (!strings) || (count <= 0))
|
---|
[21580dd] | 386 | return EINVAL;
|
---|
[edba2b6f] | 387 |
|
---|
[aadf01e] | 388 | lengths = prepare_lengths(strings, count);
|
---|
[edba2b6f] | 389 | if (!lengths)
|
---|
[aadf01e] | 390 | return ENOMEM;
|
---|
[edba2b6f] | 391 |
|
---|
| 392 | if (ERROR_OCCURRED(async_data_write_start(phone, lengths,
|
---|
| 393 | sizeof(size_t) * (count + 1)))) {
|
---|
[aadf01e] | 394 | free(lengths);
|
---|
[21580dd] | 395 | return ERROR_CODE;
|
---|
| 396 | }
|
---|
[edba2b6f] | 397 |
|
---|
[aadf01e] | 398 | free(lengths);
|
---|
[edba2b6f] | 399 |
|
---|
| 400 | for (index = 0; index < count; ++index) {
|
---|
| 401 | if (strings[index].length > 0) {
|
---|
| 402 | ERROR_PROPAGATE(async_data_write_start(phone,
|
---|
| 403 | strings[index].value, strings[index].length));
|
---|
[21580dd] | 404 | }
|
---|
| 405 | }
|
---|
[edba2b6f] | 406 |
|
---|
[21580dd] | 407 | return EOK;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | /** @}
|
---|
| 411 | */
|
---|
| 412 |
|
---|