Rev 3685 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3666 | mejdrech | 1 | /* |
2 | * Copyright (c) 2008 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 | /** |
||
34 | * @file |
||
35 | */ |
||
36 | |||
37 | #include <errno.h> |
||
38 | #include <malloc.h> |
||
39 | #include <string.h> |
||
40 | #include <unistd.h> |
||
41 | #include <ipc/ipc.h> |
||
42 | |||
43 | #include "err.h" |
||
44 | #include "measured_strings.h" |
||
45 | #include "modules.h" |
||
46 | |||
47 | size_t * prepare_lengths( measured_string_ref strings, size_t count ); |
||
48 | |||
49 | measured_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 ] ) ++ length; |
||
54 | } |
||
55 | new = ( measured_string_ref ) malloc( sizeof( measured_string_t ) + ( sizeof( char ) * ( length + 1 ))); |
||
56 | if( ! new ) return NULL; |
||
57 | new->length = length; |
||
58 | new->value = (( char * ) new ) + sizeof( measured_string_t ); |
||
59 | memcpy( new->value, string, new->length + 1 ); |
||
60 | return new; |
||
61 | } |
||
62 | |||
63 | int measured_strings_receive( measured_string_ref * strings, char ** data, size_t count ){ |
||
64 | ERROR_DECLARE; |
||
65 | |||
66 | size_t * lengths; |
||
67 | int index; |
||
68 | size_t length; |
||
69 | char * next; |
||
70 | ipc_callid_t callid; |
||
71 | |||
72 | if(( ! strings ) || ( !( * data )) || ( count <= 0 )){ |
||
73 | return EINVAL; |
||
74 | } |
||
75 | lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); |
||
76 | if( ! lengths ) return ENOMEM; |
||
77 | if( ERROR_OCCURED( ipc_data_write_receive( & callid, & length ))){ |
||
78 | free( lengths ); |
||
79 | return ERROR_CODE; |
||
80 | } |
||
81 | if( length < sizeof( size_t ) * ( count + 1 )){ |
||
82 | free( lengths ); |
||
83 | return EINVAL; |
||
84 | } |
||
85 | if( ERROR_OCCURED( ipc_data_write_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){ |
||
86 | free( lengths ); |
||
87 | return ERROR_CODE; |
||
88 | } |
||
89 | * data = malloc( lengths[ count ] ); |
||
90 | if( !( * data )) return ENOMEM; |
||
91 | * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count ); |
||
92 | if( !( * strings )){ |
||
93 | free( lengths ); |
||
94 | free( * data ); |
||
95 | return ENOMEM; |
||
96 | } |
||
97 | next = * data; |
||
98 | for( index = 0; index < count; ++ index ){ |
||
99 | ( * strings)[ index ].length = lengths[ index ]; |
||
100 | if( lengths[ index ] > 0 ){ |
||
101 | ERROR_PROPAGATE( ipc_data_write_receive( & callid, & length )); |
||
102 | if( length < lengths[ index ] + 1 ){ |
||
103 | free( * data ); |
||
104 | free( * strings ); |
||
105 | free( lengths ); |
||
106 | return EINVAL; |
||
107 | } |
||
108 | ERROR_PROPAGATE( ipc_data_write_finalize( callid, next, lengths[ index ] )); |
||
109 | next += lengths[ index ]; |
||
110 | * next = '\0'; |
||
111 | ++ next; |
||
112 | }else{ |
||
113 | ( * strings )[ index ].value = NULL; |
||
114 | } |
||
115 | } |
||
116 | free( lengths ); |
||
117 | return EOK; |
||
118 | } |
||
119 | |||
120 | int measured_strings_reply( measured_string_ref strings, size_t count ){ |
||
121 | ERROR_DECLARE; |
||
122 | |||
123 | size_t * lengths; |
||
124 | int index; |
||
125 | size_t length; |
||
126 | ipc_callid_t callid; |
||
127 | |||
128 | if(( ! strings ) || ( count <= 0 )){ |
||
129 | return EINVAL; |
||
130 | } |
||
131 | lengths = prepare_lengths( strings, count ); |
||
132 | if( ! lengths ) return ENOMEM; |
||
133 | if( ERROR_OCCURED( ipc_data_read_receive( & callid, & length ))){ |
||
134 | free( lengths ); |
||
135 | return ERROR_CODE; |
||
136 | } |
||
137 | if( length < strings[ index ].length + 1 ){ |
||
138 | free( lengths ); |
||
139 | return EINVAL; |
||
140 | } |
||
141 | if( ERROR_OCCURED( ipc_data_read_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){ |
||
142 | free( lengths ); |
||
143 | return ERROR_CODE; |
||
144 | } |
||
145 | free( lengths ); |
||
146 | for( index = 0; index < count; ++ index ){ |
||
147 | if( strings[ index ].length > 0 ){ |
||
148 | ERROR_PROPAGATE( ipc_data_read_receive( & callid, & length )); |
||
149 | if( length < strings[ index ].length + 1 ){ |
||
150 | return EINVAL; |
||
151 | } |
||
152 | ERROR_PROPAGATE( ipc_data_read_finalize( callid, strings[ index ].value, strings[ index ].length )); |
||
153 | } |
||
154 | } |
||
155 | return EOK; |
||
156 | } |
||
157 | |||
158 | int measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count ){ |
||
159 | ERROR_DECLARE; |
||
160 | |||
161 | size_t * lengths; |
||
162 | int index; |
||
163 | char * next; |
||
164 | |||
165 | if(( phone <= 0 ) || ( ! strings ) || ( !( * data )) || ( count <= 0 )){ |
||
166 | return EINVAL; |
||
167 | } |
||
168 | lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); |
||
169 | if( ! lengths ) return ENOMEM; |
||
170 | if( ERROR_OCCURED( ipc_data_read_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){ |
||
171 | free( lengths ); |
||
172 | return ERROR_CODE; |
||
173 | } |
||
174 | * data = malloc( lengths[ count ] ); |
||
175 | if( !( * data )) return ENOMEM; |
||
176 | * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count ); |
||
177 | if( !( * strings )){ |
||
178 | free( lengths ); |
||
179 | free( * data ); |
||
180 | return ENOMEM; |
||
181 | } |
||
182 | next = * data; |
||
183 | for( index = 0; index < count; ++ index ){ |
||
184 | ( * strings )[ index ].length = lengths[ index ]; |
||
185 | if( lengths[ index ] > 0 ){ |
||
186 | ( * strings )[ index ].value = next; |
||
187 | ERROR_PROPAGATE( ipc_data_read_start( phone, next, lengths[ index ] )); |
||
188 | next += lengths[ index ]; |
||
189 | * next = '\0'; |
||
190 | ++ next; |
||
191 | }else{ |
||
192 | ( * strings )[ index ].value = NULL; |
||
193 | } |
||
194 | } |
||
195 | free( lengths ); |
||
196 | return EOK; |
||
197 | } |
||
198 | |||
199 | int measured_strings_send( int phone, measured_string_ref strings, size_t count ){ |
||
200 | ERROR_DECLARE; |
||
201 | |||
202 | size_t * lengths; |
||
203 | int index; |
||
204 | |||
205 | if(( phone <= 0 ) || ( ! strings ) || ( count <= 0 )){ |
||
206 | return EINVAL; |
||
207 | } |
||
208 | lengths = prepare_lengths( strings, count ); |
||
209 | if( ! lengths ) return ENOMEM; |
||
210 | if( ERROR_OCCURED( ipc_data_write_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){ |
||
211 | free( lengths ); |
||
212 | return ERROR_CODE; |
||
213 | } |
||
214 | free( lengths ); |
||
215 | for( index = 0; index < count; ++ index ){ |
||
216 | if( strings[ index ].length > 0 ){ |
||
217 | ERROR_PROPAGATE( ipc_data_write_start( phone, strings[ index ].value, strings[ index ].length + 1 )); |
||
218 | } |
||
219 | } |
||
220 | return EOK; |
||
221 | } |
||
222 | |||
223 | size_t * prepare_lengths( measured_string_ref strings, size_t count ){ |
||
224 | size_t * lengths; |
||
225 | int index; |
||
226 | size_t length; |
||
227 | |||
228 | lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); |
||
229 | if( ! lengths ) return NULL; |
||
230 | length = 0; |
||
231 | for( index = 0; index < count; ++ index ){ |
||
232 | lengths[ index ] = strings[ index ].length; |
||
233 | length += lengths[ index ] + 1; |
||
234 | } |
||
235 | lengths[ count ] = length; |
||
236 | return lengths; |
||
237 | } |
||
238 | |||
239 | /** @} |
||
240 | */ |
||
241 |