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