Rev 3685 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3685 | Rev 3846 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Lukas Mejdrech |
2 | * Copyright (c) 2008 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup net |
29 | /** @addtogroup net |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
- | |
34 | * @file |
33 | /** @file |
- | 34 | * A character string with measured length implementation file. |
|
- | 35 | * @see measured_strings.h |
|
35 | */ |
36 | */ |
36 | 37 | ||
37 | #include <errno.h> |
38 | #include <errno.h> |
38 | #include <malloc.h> |
39 | #include <malloc.h> |
39 | #include <stdio.h> |
40 | #include <stdio.h> |
40 | #include <string.h> |
41 | #include <string.h> |
41 | #include <unistd.h> |
42 | #include <unistd.h> |
- | 43 | ||
42 | #include <ipc/ipc.h> |
44 | #include <ipc/ipc.h> |
43 | 45 | ||
44 | #include "err.h" |
46 | #include "err.h" |
45 | #include "measured_strings.h" |
47 | #include "measured_strings.h" |
46 | #include "modules.h" |
48 | #include "modules.h" |
47 | 49 | ||
- | 50 | /** Computes the lengths of the measured strings in the given array. |
|
- | 51 | * @param strings The measured strings array to be processed. Input parameter. |
|
- | 52 | * @param count The measured strings array size. Input parameter. |
|
- | 53 | * @returns The computed sizes array. |
|
- | 54 | * @returns NULL if there is no memory left. |
|
- | 55 | */ |
|
48 | size_t * prepare_lengths( measured_string_ref strings, size_t count ); |
56 | size_t * prepare_lengths( const measured_string_ref strings, size_t count ); |
49 | 57 | ||
50 | measured_string_ref measured_string_create_bulk( const char * string, size_t length ){ |
58 | measured_string_ref measured_string_create_bulk( const char * string, size_t length ){ |
51 | measured_string_ref new; |
59 | measured_string_ref new; |
52 | 60 | ||
53 | if( length == 0 ){ |
61 | if( length == 0 ){ |
54 | while( string[ length ] ) ++ length; |
62 | while( string[ length ] ) ++ length; |
55 | } |
63 | } |
56 | new = ( measured_string_ref ) malloc( sizeof( measured_string_t ) + ( sizeof( char ) * ( length + 1 ))); |
64 | new = ( measured_string_ref ) malloc( sizeof( measured_string_t ) + ( sizeof( char ) * ( length + 1 ))); |
57 | if( ! new ) return NULL; |
65 | if( ! new ) return NULL; |
58 | new->length = length; |
66 | new->length = length; |
59 | new->value = (( char * ) new ) + sizeof( measured_string_t ); |
67 | new->value = (( char * ) new ) + sizeof( measured_string_t ); |
- | 68 | // append terminating zero explicitly - to be safe |
|
60 | memcpy( new->value, string, new->length + 1 ); |
69 | memcpy( new->value, string, new->length ); |
- | 70 | new->value[ new->length ] = '\0'; |
|
61 | return new; |
71 | return new; |
62 | } |
72 | } |
63 | 73 | ||
64 | int measured_strings_receive( measured_string_ref * strings, char ** data, size_t count ){ |
74 | int measured_strings_receive( measured_string_ref * strings, char ** data, size_t count ){ |
65 | ERROR_DECLARE; |
75 | ERROR_DECLARE; |
66 | 76 | ||
67 | size_t * lengths; |
77 | size_t * lengths; |
68 | int index; |
78 | int index; |
69 | size_t length; |
79 | size_t length; |
70 | char * next; |
80 | char * next; |
71 | ipc_callid_t callid; |
81 | ipc_callid_t callid; |
72 | 82 | ||
73 | if(( ! strings ) || ( ! data ) || ( count <= 0 )){ |
83 | if(( ! strings ) || ( ! data ) || ( count <= 0 )){ |
74 | return EINVAL; |
84 | return EINVAL; |
75 | } |
85 | } |
76 | lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); |
86 | lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); |
77 | if( ! lengths ) return ENOMEM; |
87 | if( ! lengths ) return ENOMEM; |
78 | if(( ! ipc_data_write_receive( & callid, & length )) |
88 | if(( ! ipc_data_write_receive( & callid, & length )) |
79 | || ( length != sizeof( size_t ) * ( count + 1 ))){ |
89 | || ( length != sizeof( size_t ) * ( count + 1 ))){ |
80 | free( lengths ); |
90 | free( lengths ); |
81 | return EINVAL; |
91 | return EINVAL; |
82 | } |
92 | } |
83 | if( ERROR_OCCURED( ipc_data_write_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){ |
93 | if( ERROR_OCCURED( ipc_data_write_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){ |
84 | free( lengths ); |
94 | free( lengths ); |
85 | return ERROR_CODE; |
95 | return ERROR_CODE; |
86 | } |
96 | } |
87 | * data = malloc( lengths[ count ] ); |
97 | * data = malloc( lengths[ count ] ); |
88 | if( !( * data )) return ENOMEM; |
98 | if( !( * data )) return ENOMEM; |
89 | ( * data )[ lengths[ count ] - 1 ] = '\0'; |
99 | ( * data )[ lengths[ count ] - 1 ] = '\0'; |
90 | * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count ); |
100 | * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count ); |
91 | if( !( * strings )){ |
101 | if( !( * strings )){ |
92 | free( lengths ); |
102 | free( lengths ); |
93 | free( * data ); |
103 | free( * data ); |
94 | return ENOMEM; |
104 | return ENOMEM; |
95 | } |
105 | } |
96 | next = * data; |
106 | next = * data; |
97 | for( index = 0; index < count; ++ index ){ |
107 | for( index = 0; index < count; ++ index ){ |
98 | ( * strings)[ index ].length = lengths[ index ]; |
108 | ( * strings)[ index ].length = lengths[ index ]; |
99 | if( lengths[ index ] > 0 ){ |
109 | if( lengths[ index ] > 0 ){ |
100 | if(( ! ipc_data_write_receive( & callid, & length )) |
110 | if(( ! ipc_data_write_receive( & callid, & length )) |
101 | || ( length != lengths[ index ] )){ |
111 | || ( length != lengths[ index ] )){ |
102 | free( * data ); |
112 | free( * data ); |
103 | free( * strings ); |
113 | free( * strings ); |
104 | free( lengths ); |
114 | free( lengths ); |
105 | return EINVAL; |
115 | return EINVAL; |
106 | } |
116 | } |
107 | ERROR_PROPAGATE( ipc_data_write_finalize( callid, next, lengths[ index ] )); |
117 | ERROR_PROPAGATE( ipc_data_write_finalize( callid, next, lengths[ index ] )); |
108 | ( * strings)[ index ].value = next; |
118 | ( * strings)[ index ].value = next; |
109 | next += lengths[ index ]; |
119 | next += lengths[ index ]; |
110 | * next = '\0'; |
120 | * next = '\0'; |
111 | ++ next; |
121 | ++ next; |
112 | }else{ |
122 | }else{ |
113 | ( * strings )[ index ].value = NULL; |
123 | ( * strings )[ index ].value = NULL; |
114 | } |
124 | } |
115 | } |
125 | } |
116 | free( lengths ); |
126 | free( lengths ); |
117 | return EOK; |
127 | return EOK; |
118 | } |
128 | } |
119 | 129 | ||
120 | int measured_strings_reply( measured_string_ref strings, size_t count ){ |
130 | int measured_strings_reply( const measured_string_ref strings, size_t count ){ |
121 | ERROR_DECLARE; |
131 | ERROR_DECLARE; |
122 | 132 | ||
123 | size_t * lengths; |
133 | size_t * lengths; |
124 | int index; |
134 | int index; |
125 | size_t length; |
135 | size_t length; |
126 | ipc_callid_t callid; |
136 | ipc_callid_t callid; |
127 | 137 | ||
128 | if(( ! strings ) || ( count <= 0 )){ |
138 | if(( ! strings ) || ( count <= 0 )){ |
129 | return EINVAL; |
139 | return EINVAL; |
130 | } |
140 | } |
131 | lengths = prepare_lengths( strings, count ); |
141 | lengths = prepare_lengths( strings, count ); |
132 | if( ! lengths ) return ENOMEM; |
142 | if( ! lengths ) return ENOMEM; |
133 | if(( ! ipc_data_read_receive( & callid, & length )) |
143 | if(( ! ipc_data_read_receive( & callid, & length )) |
134 | || ( length != sizeof( size_t ) * ( count + 1 ))){ |
144 | || ( length != sizeof( size_t ) * ( count + 1 ))){ |
135 | free( lengths ); |
145 | free( lengths ); |
136 | return EINVAL; |
146 | return EINVAL; |
137 | } |
147 | } |
138 | if( ERROR_OCCURED( ipc_data_read_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){ |
148 | if( ERROR_OCCURED( ipc_data_read_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){ |
139 | free( lengths ); |
149 | free( lengths ); |
140 | return ERROR_CODE; |
150 | return ERROR_CODE; |
141 | } |
151 | } |
142 | free( lengths ); |
152 | free( lengths ); |
143 | for( index = 0; index < count; ++ index ){ |
153 | for( index = 0; index < count; ++ index ){ |
144 | if( strings[ index ].length > 0 ){ |
154 | if( strings[ index ].length > 0 ){ |
145 | if(( ! ipc_data_read_receive( & callid, & length )) |
155 | if(( ! ipc_data_read_receive( & callid, & length )) |
146 | || ( length != strings[ index ].length )){ |
156 | || ( length != strings[ index ].length )){ |
147 | return EINVAL; |
157 | return EINVAL; |
148 | } |
158 | } |
149 | ERROR_PROPAGATE( ipc_data_read_finalize( callid, strings[ index ].value, strings[ index ].length )); |
159 | ERROR_PROPAGATE( ipc_data_read_finalize( callid, strings[ index ].value, strings[ index ].length )); |
150 | } |
160 | } |
151 | } |
161 | } |
152 | return EOK; |
162 | return EOK; |
153 | } |
163 | } |
154 | 164 | ||
155 | int measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count ){ |
165 | int measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count ){ |
156 | ERROR_DECLARE; |
166 | ERROR_DECLARE; |
157 | 167 | ||
158 | size_t * lengths; |
168 | size_t * lengths; |
159 | int index; |
169 | int index; |
160 | char * next; |
170 | char * next; |
161 | 171 | ||
162 | if(( phone <= 0 ) || ( ! strings ) || ( ! data ) || ( count <= 0 )){ |
172 | if(( phone <= 0 ) || ( ! strings ) || ( ! data ) || ( count <= 0 )){ |
163 | return EINVAL; |
173 | return EINVAL; |
164 | } |
174 | } |
165 | lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); |
175 | lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); |
166 | if( ! lengths ) return ENOMEM; |
176 | if( ! lengths ) return ENOMEM; |
167 | if( ERROR_OCCURED( ipc_data_read_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){ |
177 | if( ERROR_OCCURED( ipc_data_read_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){ |
168 | free( lengths ); |
178 | free( lengths ); |
169 | return ERROR_CODE; |
179 | return ERROR_CODE; |
170 | } |
180 | } |
171 | * data = malloc( lengths[ count ] ); |
181 | * data = malloc( lengths[ count ] ); |
172 | if( !( * data )) return ENOMEM; |
182 | if( !( * data )) return ENOMEM; |
173 | * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count ); |
183 | * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count ); |
174 | if( !( * strings )){ |
184 | if( !( * strings )){ |
175 | free( lengths ); |
185 | free( lengths ); |
176 | free( * data ); |
186 | free( * data ); |
177 | return ENOMEM; |
187 | return ENOMEM; |
178 | } |
188 | } |
179 | next = * data; |
189 | next = * data; |
180 | for( index = 0; index < count; ++ index ){ |
190 | for( index = 0; index < count; ++ index ){ |
181 | ( * strings )[ index ].length = lengths[ index ]; |
191 | ( * strings )[ index ].length = lengths[ index ]; |
182 | if( lengths[ index ] > 0 ){ |
192 | if( lengths[ index ] > 0 ){ |
183 | ERROR_PROPAGATE( ipc_data_read_start( phone, next, lengths[ index ] )); |
193 | ERROR_PROPAGATE( ipc_data_read_start( phone, next, lengths[ index ] )); |
184 | ( * strings )[ index ].value = next; |
194 | ( * strings )[ index ].value = next; |
185 | next += lengths[ index ]; |
195 | next += lengths[ index ]; |
186 | * next = '\0'; |
196 | * next = '\0'; |
187 | ++ next; |
197 | ++ next; |
188 | }else{ |
198 | }else{ |
189 | ( * strings )[ index ].value = NULL; |
199 | ( * strings )[ index ].value = NULL; |
190 | } |
200 | } |
191 | } |
201 | } |
192 | free( lengths ); |
202 | free( lengths ); |
193 | return EOK; |
203 | return EOK; |
194 | } |
204 | } |
195 | 205 | ||
196 | int measured_strings_send( int phone, measured_string_ref strings, size_t count ){ |
206 | int measured_strings_send( int phone, const measured_string_ref strings, size_t count ){ |
197 | ERROR_DECLARE; |
207 | ERROR_DECLARE; |
198 | 208 | ||
199 | size_t * lengths; |
209 | size_t * lengths; |
200 | int index; |
210 | int index; |
201 | 211 | ||
202 | if(( phone <= 0 ) || ( ! strings ) || ( count <= 0 )){ |
212 | if(( phone <= 0 ) || ( ! strings ) || ( count <= 0 )){ |
203 | return EINVAL; |
213 | return EINVAL; |
204 | } |
214 | } |
205 | lengths = prepare_lengths( strings, count ); |
215 | lengths = prepare_lengths( strings, count ); |
206 | if( ! lengths ) return ENOMEM; |
216 | if( ! lengths ) return ENOMEM; |
207 | if( ERROR_OCCURED( ipc_data_write_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){ |
217 | if( ERROR_OCCURED( ipc_data_write_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){ |
208 | free( lengths ); |
218 | free( lengths ); |
209 | return ERROR_CODE; |
219 | return ERROR_CODE; |
210 | } |
220 | } |
211 | free( lengths ); |
221 | free( lengths ); |
212 | for( index = 0; index < count; ++ index ){ |
222 | for( index = 0; index < count; ++ index ){ |
213 | if( strings[ index ].length > 0 ){ |
223 | if( strings[ index ].length > 0 ){ |
214 | ERROR_PROPAGATE( ipc_data_write_start( phone, strings[ index ].value, strings[ index ].length )); |
224 | ERROR_PROPAGATE( ipc_data_write_start( phone, strings[ index ].value, strings[ index ].length )); |
215 | } |
225 | } |
216 | } |
226 | } |
217 | return EOK; |
227 | return EOK; |
218 | } |
228 | } |
219 | 229 | ||
220 | size_t * prepare_lengths( measured_string_ref strings, size_t count ){ |
230 | size_t * prepare_lengths( const measured_string_ref strings, size_t count ){ |
221 | size_t * lengths; |
231 | size_t * lengths; |
222 | int index; |
232 | int index; |
223 | size_t length; |
233 | size_t length; |
224 | 234 | ||
225 | lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); |
235 | lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 )); |
226 | if( ! lengths ) return NULL; |
236 | if( ! lengths ) return NULL; |
227 | length = 0; |
237 | length = 0; |
228 | for( index = 0; index < count; ++ index ){ |
238 | for( index = 0; index < count; ++ index ){ |
229 | lengths[ index ] = strings[ index ].length; |
239 | lengths[ index ] = strings[ index ].length; |
230 | length += lengths[ index ] + 1; |
240 | length += lengths[ index ] + 1; |
231 | } |
241 | } |
232 | lengths[ count ] = length; |
242 | lengths[ count ] = length; |
233 | return lengths; |
243 | return lengths; |
234 | } |
244 | } |
235 | 245 | ||
236 | /** @} |
246 | /** @} |
237 | */ |
247 | */ |
238 | 248 | ||
239 | 249 |