source: mainline/uspace/lib/socket/adt/measured_strings.c@ 849ed54

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

Networking work:
Split the networking stack into end-user library (libsocket) and two helper libraries (libnet and libnetif).
Don't use over-the-hand compiling and linking, but rather separation of conserns.
There might be still some issues and the non-modular networking architecture is currently broken, but this will be fixed soon.

  • Property mode set to 100644
File size: 7.3 KB
Line 
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 <net_err.h>
46#include <net_modules.h>
47#include <adt/measured_strings.h>
48
49measured_string_ref measured_string_create_bulk(const char * string, size_t length){
50 measured_string_ref new;
51
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;
60 }
61 new->length = length;
62 new->value = ((char *) new) + sizeof(measured_string_t);
63 // append terminating zero explicitly - to be safe
64 memcpy(new->value, string, new->length);
65 new->value[new->length] = '\0';
66 return new;
67}
68
69measured_string_ref measured_string_copy(measured_string_ref source){
70 measured_string_ref new;
71
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){
79 new->length = source->length;
80 memcpy(new->value, source->value, new->length);
81 new->value[new->length] = '\0';
82 return new;
83 }else{
84 free(new);
85 }
86 }
87 return NULL;
88}
89
90int measured_strings_receive(measured_string_ref * strings, char ** data, size_t count){
91 ERROR_DECLARE;
92
93 size_t * lengths;
94 size_t index;
95 size_t length;
96 char * next;
97 ipc_callid_t callid;
98
99 if((! strings) || (! data) || (count <= 0)){
100 return EINVAL;
101 }
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);
109 return EINVAL;
110 }
111 if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
112 free(lengths);
113 return ERROR_CODE;
114 }
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);
124 return ENOMEM;
125 }
126 next = * data;
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);
135 return EINVAL;
136 }
137 ERROR_PROPAGATE(async_data_write_finalize(callid, next, lengths[index]));
138 (*strings)[index].value = next;
139 next += lengths[index];
140 *next = '\0';
141 ++ next;
142 }else{
143 (*strings)[index].value = NULL;
144 }
145 }
146 free(lengths);
147 return EOK;
148}
149
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 */
156static 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
174int measured_strings_reply(const measured_string_ref strings, size_t count){
175 ERROR_DECLARE;
176
177 size_t * lengths;
178 size_t index;
179 size_t length;
180 ipc_callid_t callid;
181
182 if((! strings) || (count <= 0)){
183 return EINVAL;
184 }
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);
192 return EINVAL;
193 }
194 if(ERROR_OCCURRED(async_data_read_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
195 free(lengths);
196 return ERROR_CODE;
197 }
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)){
203 return EINVAL;
204 }
205 ERROR_PROPAGATE(async_data_read_finalize(callid, strings[index].value, strings[index].length));
206 }
207 }
208 return EOK;
209}
210
211int measured_strings_return(int phone, measured_string_ref * strings, char ** data, size_t count){
212 ERROR_DECLARE;
213
214 size_t * lengths;
215 size_t index;
216 char * next;
217
218 if((phone <= 0) || (! strings) || (! data) || (count <= 0)){
219 return EINVAL;
220 }
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);
227 return ERROR_CODE;
228 }
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);
237 return ENOMEM;
238 }
239 next = * data;
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';
247 ++ next;
248 }else{
249 (*strings)[index].value = NULL;
250 }
251 }
252 free(lengths);
253 return EOK;
254}
255
256int measured_strings_send(int phone, const measured_string_ref strings, size_t count){
257 ERROR_DECLARE;
258
259 size_t * lengths;
260 size_t index;
261
262 if((phone <= 0) || (! strings) || (count <= 0)){
263 return EINVAL;
264 }
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);
271 return ERROR_CODE;
272 }
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));
277 }
278 }
279 return EOK;
280}
281
282/** @}
283 */
284
Note: See TracBrowser for help on using the repository browser.