[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
|
---|
| 34 | * Character string with measured length implementation.
|
---|
| 35 | * @see measured_strings.h
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <malloc.h>
|
---|
| 40 | #include <mem.h>
|
---|
| 41 | #include <unistd.h>
|
---|
| 42 |
|
---|
| 43 | #include <ipc/ipc.h>
|
---|
| 44 |
|
---|
| 45 | #include "../err.h"
|
---|
| 46 | #include "../modules.h"
|
---|
| 47 |
|
---|
| 48 | #include "measured_strings.h"
|
---|
| 49 |
|
---|
| 50 | /** Computes the lengths of the measured strings in the given array.
|
---|
| 51 | * @param[in] strings The measured strings array to be processed.
|
---|
| 52 | * @param[in] count The measured strings array size.
|
---|
| 53 | * @returns The computed sizes array.
|
---|
| 54 | * @returns NULL if there is not enough memory left.
|
---|
| 55 | */
|
---|
[aadf01e] | 56 | size_t * prepare_lengths(const measured_string_ref strings, size_t count);
|
---|
[21580dd] | 57 |
|
---|
[aadf01e] | 58 | measured_string_ref measured_string_create_bulk(const char * string, size_t length){
|
---|
| 59 | measured_string_ref new;
|
---|
[21580dd] | 60 |
|
---|
[aadf01e] | 61 | if(length == 0){
|
---|
| 62 | while(string[length]){
|
---|
| 63 | ++ length;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | new = (measured_string_ref) malloc(sizeof(measured_string_t) + (sizeof(char) * (length + 1)));
|
---|
| 67 | if(! new){
|
---|
| 68 | return NULL;
|
---|
[21580dd] | 69 | }
|
---|
| 70 | new->length = length;
|
---|
[aadf01e] | 71 | new->value = ((char *) new) + sizeof(measured_string_t);
|
---|
[21580dd] | 72 | // append terminating zero explicitly - to be safe
|
---|
[aadf01e] | 73 | memcpy(new->value, string, new->length);
|
---|
| 74 | new->value[new->length] = '\0';
|
---|
[21580dd] | 75 | return new;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[aadf01e] | 78 | measured_string_ref measured_string_copy(measured_string_ref source){
|
---|
| 79 | measured_string_ref new;
|
---|
[21580dd] | 80 |
|
---|
[aadf01e] | 81 | if(! source){
|
---|
| 82 | return NULL;
|
---|
| 83 | }
|
---|
| 84 | new = (measured_string_ref) malloc(sizeof(measured_string_t));
|
---|
| 85 | if(new){
|
---|
| 86 | new->value = (char *) malloc(source->length + 1);
|
---|
| 87 | if(new->value){
|
---|
[21580dd] | 88 | new->length = source->length;
|
---|
[aadf01e] | 89 | memcpy(new->value, source->value, new->length);
|
---|
| 90 | new->value[new->length] = '\0';
|
---|
[21580dd] | 91 | return new;
|
---|
| 92 | }else{
|
---|
[aadf01e] | 93 | free(new);
|
---|
[21580dd] | 94 | }
|
---|
| 95 | }
|
---|
| 96 | return NULL;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[aadf01e] | 99 | int measured_strings_receive(measured_string_ref * strings, char ** data, size_t count){
|
---|
[21580dd] | 100 | ERROR_DECLARE;
|
---|
| 101 |
|
---|
[aadf01e] | 102 | size_t * lengths;
|
---|
| 103 | size_t index;
|
---|
| 104 | size_t length;
|
---|
| 105 | char * next;
|
---|
| 106 | ipc_callid_t callid;
|
---|
[21580dd] | 107 |
|
---|
[aadf01e] | 108 | if((! strings) || (! data) || (count <= 0)){
|
---|
[21580dd] | 109 | return EINVAL;
|
---|
| 110 | }
|
---|
[aadf01e] | 111 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
| 112 | if(! lengths){
|
---|
| 113 | return ENOMEM;
|
---|
| 114 | }
|
---|
| 115 | if((! async_data_write_receive(&callid, &length))
|
---|
| 116 | || (length != sizeof(size_t) * (count + 1))){
|
---|
| 117 | free(lengths);
|
---|
[21580dd] | 118 | return EINVAL;
|
---|
| 119 | }
|
---|
[aadf01e] | 120 | if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
|
---|
| 121 | free(lengths);
|
---|
[21580dd] | 122 | return ERROR_CODE;
|
---|
| 123 | }
|
---|
[aadf01e] | 124 | *data = malloc(lengths[count]);
|
---|
| 125 | if(!(*data)){
|
---|
| 126 | return ENOMEM;
|
---|
| 127 | }
|
---|
| 128 | (*data)[lengths[count] - 1] = '\0';
|
---|
| 129 | *strings = (measured_string_ref) malloc(sizeof(measured_string_t) * count);
|
---|
| 130 | if(!(*strings)){
|
---|
| 131 | free(lengths);
|
---|
| 132 | free(*data);
|
---|
[21580dd] | 133 | return ENOMEM;
|
---|
| 134 | }
|
---|
| 135 | next = * data;
|
---|
[aadf01e] | 136 | for(index = 0; index < count; ++ index){
|
---|
| 137 | (*strings)[index].length = lengths[index];
|
---|
| 138 | if(lengths[index] > 0){
|
---|
| 139 | if((! async_data_write_receive(&callid, &length))
|
---|
| 140 | || (length != lengths[index])){
|
---|
| 141 | free(*data);
|
---|
| 142 | free(*strings);
|
---|
| 143 | free(lengths);
|
---|
[21580dd] | 144 | return EINVAL;
|
---|
| 145 | }
|
---|
[aadf01e] | 146 | ERROR_PROPAGATE(async_data_write_finalize(callid, next, lengths[index]));
|
---|
| 147 | (*strings)[index].value = next;
|
---|
| 148 | next += lengths[index];
|
---|
| 149 | *next = '\0';
|
---|
[21580dd] | 150 | ++ next;
|
---|
| 151 | }else{
|
---|
[aadf01e] | 152 | (*strings)[index].value = NULL;
|
---|
[21580dd] | 153 | }
|
---|
| 154 | }
|
---|
[aadf01e] | 155 | free(lengths);
|
---|
[21580dd] | 156 | return EOK;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[aadf01e] | 159 | int measured_strings_reply(const measured_string_ref strings, size_t count){
|
---|
[21580dd] | 160 | ERROR_DECLARE;
|
---|
| 161 |
|
---|
[aadf01e] | 162 | size_t * lengths;
|
---|
| 163 | size_t index;
|
---|
| 164 | size_t length;
|
---|
| 165 | ipc_callid_t callid;
|
---|
[21580dd] | 166 |
|
---|
[aadf01e] | 167 | if((! strings) || (count <= 0)){
|
---|
[21580dd] | 168 | return EINVAL;
|
---|
| 169 | }
|
---|
[aadf01e] | 170 | lengths = prepare_lengths(strings, count);
|
---|
| 171 | if(! lengths){
|
---|
| 172 | return ENOMEM;
|
---|
| 173 | }
|
---|
| 174 | if((! async_data_read_receive(&callid, &length))
|
---|
| 175 | || (length != sizeof(size_t) * (count + 1))){
|
---|
| 176 | free(lengths);
|
---|
[21580dd] | 177 | return EINVAL;
|
---|
| 178 | }
|
---|
[aadf01e] | 179 | if(ERROR_OCCURRED(async_data_read_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
|
---|
| 180 | free(lengths);
|
---|
[21580dd] | 181 | return ERROR_CODE;
|
---|
| 182 | }
|
---|
[aadf01e] | 183 | free(lengths);
|
---|
| 184 | for(index = 0; index < count; ++ index){
|
---|
| 185 | if(strings[index].length > 0){
|
---|
| 186 | if((! async_data_read_receive(&callid, &length))
|
---|
| 187 | || (length != strings[index].length)){
|
---|
[21580dd] | 188 | return EINVAL;
|
---|
| 189 | }
|
---|
[aadf01e] | 190 | ERROR_PROPAGATE(async_data_read_finalize(callid, strings[index].value, strings[index].length));
|
---|
[21580dd] | 191 | }
|
---|
| 192 | }
|
---|
| 193 | return EOK;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[aadf01e] | 196 | int measured_strings_return(int phone, measured_string_ref * strings, char ** data, size_t count){
|
---|
[21580dd] | 197 | ERROR_DECLARE;
|
---|
| 198 |
|
---|
[aadf01e] | 199 | size_t * lengths;
|
---|
| 200 | size_t index;
|
---|
| 201 | char * next;
|
---|
[21580dd] | 202 |
|
---|
[aadf01e] | 203 | if((phone <= 0) || (! strings) || (! data) || (count <= 0)){
|
---|
[21580dd] | 204 | return EINVAL;
|
---|
| 205 | }
|
---|
[aadf01e] | 206 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
| 207 | if(! lengths){
|
---|
| 208 | return ENOMEM;
|
---|
| 209 | }
|
---|
| 210 | if(ERROR_OCCURRED(async_data_read_start(phone, lengths, sizeof(size_t) * (count + 1)))){
|
---|
| 211 | free(lengths);
|
---|
[21580dd] | 212 | return ERROR_CODE;
|
---|
| 213 | }
|
---|
[aadf01e] | 214 | *data = malloc(lengths[count]);
|
---|
| 215 | if(!(*data)){
|
---|
| 216 | return ENOMEM;
|
---|
| 217 | }
|
---|
| 218 | *strings = (measured_string_ref) malloc(sizeof(measured_string_t) * count);
|
---|
| 219 | if(!(*strings)){
|
---|
| 220 | free(lengths);
|
---|
| 221 | free(*data);
|
---|
[21580dd] | 222 | return ENOMEM;
|
---|
| 223 | }
|
---|
| 224 | next = * data;
|
---|
[aadf01e] | 225 | for(index = 0; index < count; ++ index){
|
---|
| 226 | (*strings)[index].length = lengths[index];
|
---|
| 227 | if(lengths[index] > 0){
|
---|
| 228 | ERROR_PROPAGATE(async_data_read_start(phone, next, lengths[index]));
|
---|
| 229 | (*strings)[index].value = next;
|
---|
| 230 | next += lengths[index];
|
---|
| 231 | *next = '\0';
|
---|
[21580dd] | 232 | ++ next;
|
---|
| 233 | }else{
|
---|
[aadf01e] | 234 | (*strings)[index].value = NULL;
|
---|
[21580dd] | 235 | }
|
---|
| 236 | }
|
---|
[aadf01e] | 237 | free(lengths);
|
---|
[21580dd] | 238 | return EOK;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[aadf01e] | 241 | int measured_strings_send(int phone, const measured_string_ref strings, size_t count){
|
---|
[21580dd] | 242 | ERROR_DECLARE;
|
---|
| 243 |
|
---|
[aadf01e] | 244 | size_t * lengths;
|
---|
| 245 | size_t index;
|
---|
[21580dd] | 246 |
|
---|
[aadf01e] | 247 | if((phone <= 0) || (! strings) || (count <= 0)){
|
---|
[21580dd] | 248 | return EINVAL;
|
---|
| 249 | }
|
---|
[aadf01e] | 250 | lengths = prepare_lengths(strings, count);
|
---|
| 251 | if(! lengths){
|
---|
| 252 | return ENOMEM;
|
---|
| 253 | }
|
---|
| 254 | if(ERROR_OCCURRED(async_data_write_start(phone, lengths, sizeof(size_t) * (count + 1)))){
|
---|
| 255 | free(lengths);
|
---|
[21580dd] | 256 | return ERROR_CODE;
|
---|
| 257 | }
|
---|
[aadf01e] | 258 | free(lengths);
|
---|
| 259 | for(index = 0; index < count; ++ index){
|
---|
| 260 | if(strings[index].length > 0){
|
---|
| 261 | ERROR_PROPAGATE(async_data_write_start(phone, strings[index].value, strings[index].length));
|
---|
[21580dd] | 262 | }
|
---|
| 263 | }
|
---|
| 264 | return EOK;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[aadf01e] | 267 | size_t * prepare_lengths(const measured_string_ref strings, size_t count){
|
---|
| 268 | size_t * lengths;
|
---|
| 269 | size_t index;
|
---|
| 270 | size_t length;
|
---|
[21580dd] | 271 |
|
---|
[aadf01e] | 272 | lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
|
---|
| 273 | if(! lengths){
|
---|
| 274 | return NULL;
|
---|
| 275 | }
|
---|
[21580dd] | 276 | length = 0;
|
---|
[aadf01e] | 277 | for(index = 0; index < count; ++ index){
|
---|
| 278 | lengths[index] = strings[index].length;
|
---|
| 279 | length += lengths[index] + 1;
|
---|
[21580dd] | 280 | }
|
---|
[aadf01e] | 281 | lengths[count] = length;
|
---|
[21580dd] | 282 | return lengths;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | /** @}
|
---|
| 286 | */
|
---|
| 287 |
|
---|