Subversion Repositories HelenOS

Rev

Rev 3666 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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