Subversion Repositories HelenOS

Rev

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

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