source: mainline/uspace/srv/net/structures/measured_strings.c@ aadf01e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since aadf01e was aadf01e, checked in by Lukas Mejdrech <lukasmejdrech@…>, 15 years ago

Coding style (no functional change)

  • Property mode set to 100644
File size: 7.4 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 "../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 */
56size_t * prepare_lengths(const measured_string_ref strings, size_t count);
57
58measured_string_ref measured_string_create_bulk(const char * string, size_t length){
59 measured_string_ref new;
60
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;
69 }
70 new->length = length;
71 new->value = ((char *) new) + sizeof(measured_string_t);
72 // append terminating zero explicitly - to be safe
73 memcpy(new->value, string, new->length);
74 new->value[new->length] = '\0';
75 return new;
76}
77
78measured_string_ref measured_string_copy(measured_string_ref source){
79 measured_string_ref new;
80
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){
88 new->length = source->length;
89 memcpy(new->value, source->value, new->length);
90 new->value[new->length] = '\0';
91 return new;
92 }else{
93 free(new);
94 }
95 }
96 return NULL;
97}
98
99int measured_strings_receive(measured_string_ref * strings, char ** data, size_t count){
100 ERROR_DECLARE;
101
102 size_t * lengths;
103 size_t index;
104 size_t length;
105 char * next;
106 ipc_callid_t callid;
107
108 if((! strings) || (! data) || (count <= 0)){
109 return EINVAL;
110 }
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);
118 return EINVAL;
119 }
120 if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
121 free(lengths);
122 return ERROR_CODE;
123 }
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);
133 return ENOMEM;
134 }
135 next = * data;
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);
144 return EINVAL;
145 }
146 ERROR_PROPAGATE(async_data_write_finalize(callid, next, lengths[index]));
147 (*strings)[index].value = next;
148 next += lengths[index];
149 *next = '\0';
150 ++ next;
151 }else{
152 (*strings)[index].value = NULL;
153 }
154 }
155 free(lengths);
156 return EOK;
157}
158
159int measured_strings_reply(const measured_string_ref strings, size_t count){
160 ERROR_DECLARE;
161
162 size_t * lengths;
163 size_t index;
164 size_t length;
165 ipc_callid_t callid;
166
167 if((! strings) || (count <= 0)){
168 return EINVAL;
169 }
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);
177 return EINVAL;
178 }
179 if(ERROR_OCCURRED(async_data_read_finalize(callid, lengths, sizeof(size_t) * (count + 1)))){
180 free(lengths);
181 return ERROR_CODE;
182 }
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)){
188 return EINVAL;
189 }
190 ERROR_PROPAGATE(async_data_read_finalize(callid, strings[index].value, strings[index].length));
191 }
192 }
193 return EOK;
194}
195
196int measured_strings_return(int phone, measured_string_ref * strings, char ** data, size_t count){
197 ERROR_DECLARE;
198
199 size_t * lengths;
200 size_t index;
201 char * next;
202
203 if((phone <= 0) || (! strings) || (! data) || (count <= 0)){
204 return EINVAL;
205 }
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);
212 return ERROR_CODE;
213 }
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);
222 return ENOMEM;
223 }
224 next = * data;
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';
232 ++ next;
233 }else{
234 (*strings)[index].value = NULL;
235 }
236 }
237 free(lengths);
238 return EOK;
239}
240
241int measured_strings_send(int phone, const measured_string_ref strings, size_t count){
242 ERROR_DECLARE;
243
244 size_t * lengths;
245 size_t index;
246
247 if((phone <= 0) || (! strings) || (count <= 0)){
248 return EINVAL;
249 }
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);
256 return ERROR_CODE;
257 }
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));
262 }
263 }
264 return EOK;
265}
266
267size_t * prepare_lengths(const measured_string_ref strings, size_t count){
268 size_t * lengths;
269 size_t index;
270 size_t length;
271
272 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
273 if(! lengths){
274 return NULL;
275 }
276 length = 0;
277 for(index = 0; index < count; ++ index){
278 lengths[index] = strings[index].length;
279 length += lengths[index] + 1;
280 }
281 lengths[count] = length;
282 return lengths;
283}
284
285/** @}
286 */
287
Note: See TracBrowser for help on using the repository browser.