65
65
66
66
#include "../internal.h"
67
67
68
- typedef struct bio_buf_mem_st {
69
- struct buf_mem_st * buf ; /* allocated buffer */
70
- size_t read_off ; /* read pointer offset from current buffer position */
71
- } BIO_BUF_MEM ;
72
-
73
68
74
69
BIO * BIO_new_mem_buf (const void * buf , ossl_ssize_t len ) {
75
70
BIO * ret ;
76
71
BUF_MEM * b ;
77
- BIO_BUF_MEM * bbm ;
78
-
79
- const size_t size = (len < 0 || (size_t )len > SIZE_MAX ) ? strlen ((char * )buf ) : (size_t )len ;
72
+ const size_t size = len < 0 ? strlen ((char * )buf ) : (size_t )len ;
80
73
81
74
if (!buf && len != 0 ) {
82
75
OPENSSL_PUT_ERROR (BIO , BIO_R_NULL_PARAMETER );
@@ -88,8 +81,7 @@ BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) {
88
81
return NULL ;
89
82
}
90
83
91
- bbm = (BIO_BUF_MEM * )ret -> ptr ;
92
- b = bbm -> buf ;
84
+ b = (BUF_MEM * )ret -> ptr ;
93
85
// BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to.
94
86
b -> data = (void * )buf ;
95
87
b -> length = size ;
@@ -106,25 +98,19 @@ BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) {
106
98
}
107
99
108
100
static int mem_new (BIO * bio ) {
109
- BIO_BUF_MEM * bbm = OPENSSL_zalloc (sizeof (* bbm ));
110
-
111
- if (bbm == NULL ) {
112
- return 0 ;
113
- }
101
+ BUF_MEM * b ;
114
102
115
- bbm -> buf = BUF_MEM_new ();
116
- if (bbm -> buf == NULL ) {
117
- OPENSSL_free (bbm );
103
+ b = BUF_MEM_new ();
104
+ if (b == NULL ) {
118
105
return 0 ;
119
106
}
120
107
121
108
// |shutdown| is used to store the close flag: whether the BIO has ownership
122
109
// of the BUF_MEM.
123
- bbm -> read_off = 0 ;
124
110
bio -> shutdown = 1 ;
125
111
bio -> init = 1 ;
126
112
bio -> num = -1 ;
127
- bio -> ptr = (char * )bbm ;
113
+ bio -> ptr = (char * )b ;
128
114
129
115
return 1 ;
130
116
}
@@ -134,53 +120,35 @@ static int mem_free(BIO *bio) {
134
120
return 1 ;
135
121
}
136
122
137
- BIO_BUF_MEM * bbm = (BIO_BUF_MEM * )bio -> ptr ;
138
- BUF_MEM * b = bbm -> buf ;
139
-
123
+ BUF_MEM * b = (BUF_MEM * )bio -> ptr ;
140
124
if (bio -> flags & BIO_FLAGS_MEM_RDONLY ) {
141
125
b -> data = NULL ;
142
126
}
143
127
BUF_MEM_free (b );
144
128
bio -> ptr = NULL ;
145
-
146
- OPENSSL_free (bbm );
147
129
return 1 ;
148
130
}
149
131
150
- static void mem_buf_sync (BIO * bio ) {
151
- if (bio -> init != 0 && bio -> ptr != NULL ) {
152
- BIO_BUF_MEM * bbm = (BIO_BUF_MEM * ) bio -> ptr ;
153
- BUF_MEM * b = bbm -> buf ;
154
-
155
- if (b -> data != NULL ) {
156
- if (bio -> flags & BIO_FLAGS_MEM_RDONLY ) {
157
- b -> data += bbm -> read_off ;
158
- } else {
159
- OPENSSL_memmove (b -> data , & b -> data [bbm -> read_off ], b -> length );
160
- }
161
- bbm -> read_off = 0 ;
162
- }
163
- }
164
- }
165
-
166
132
static int mem_read (BIO * bio , char * out , int outl ) {
167
133
BIO_clear_retry_flags (bio );
168
134
if (outl <= 0 ) {
169
135
return 0 ;
170
136
}
171
137
172
- BIO_BUF_MEM * bbm = (BIO_BUF_MEM * ) bio -> ptr ;
173
- BUF_MEM * b = bbm -> buf ;
174
-
138
+ BUF_MEM * b = bio -> ptr ;
175
139
int ret = outl ;
176
140
if ((size_t )ret > b -> length ) {
177
141
ret = (int )b -> length ;
178
142
}
179
143
180
144
if (ret > 0 ) {
181
- OPENSSL_memcpy (out , & b -> data [ bbm -> read_off ] , ret );
145
+ OPENSSL_memcpy (out , b -> data , ret );
182
146
b -> length -= ret ;
183
- bbm -> read_off += ret ;
147
+ if (bio -> flags & BIO_FLAGS_MEM_RDONLY ) {
148
+ b -> data += ret ;
149
+ } else {
150
+ OPENSSL_memmove (b -> data , & b -> data [ret ], b -> length );
151
+ }
184
152
} else if (b -> length == 0 ) {
185
153
ret = bio -> num ;
186
154
if (ret != 0 ) {
@@ -201,11 +169,7 @@ static int mem_write(BIO *bio, const char *in, int inl) {
201
169
return -1 ;
202
170
}
203
171
204
- BIO_BUF_MEM * bbm = (BIO_BUF_MEM * ) bio -> ptr ;
205
- BUF_MEM * b = bbm -> buf ;
206
-
207
- mem_buf_sync (bio );
208
-
172
+ BUF_MEM * b = bio -> ptr ;
209
173
if (!BUF_MEM_append (b , in , inl )) {
210
174
return -1 ;
211
175
}
@@ -221,21 +185,16 @@ static int mem_gets(BIO *bio, char *buf, int size) {
221
185
222
186
// The buffer size includes space for the trailing NUL, so we can read at most
223
187
// one fewer byte.
224
- BIO_BUF_MEM * bbm = (BIO_BUF_MEM * ) bio -> ptr ;
225
- BUF_MEM * b = bbm -> buf ;
188
+ BUF_MEM * b = bio -> ptr ;
226
189
int ret = size - 1 ;
227
190
if ((size_t )ret > b -> length ) {
228
191
ret = (int )b -> length ;
229
192
}
230
193
231
194
// Stop at the first newline.
232
- if (b -> data != NULL ) {
233
- char * readp = & b -> data [bbm -> read_off ];
234
-
235
- const char * newline = OPENSSL_memchr (readp , '\n' , ret );
236
- if (newline != NULL ) {
237
- ret = (int )(newline - readp + 1 );
238
- }
195
+ const char * newline = OPENSSL_memchr (b -> data , '\n' , ret );
196
+ if (newline != NULL ) {
197
+ ret = (int )(newline - b -> data + 1 );
239
198
}
240
199
241
200
ret = mem_read (bio , buf , ret );
@@ -248,8 +207,7 @@ static int mem_gets(BIO *bio, char *buf, int size) {
248
207
static long mem_ctrl (BIO * bio , int cmd , long num , void * ptr ) {
249
208
long ret = 1 ;
250
209
251
- BIO_BUF_MEM * bbm = (BIO_BUF_MEM * ) bio -> ptr ;
252
- BUF_MEM * b = bbm -> buf ;
210
+ BUF_MEM * b = (BUF_MEM * )bio -> ptr ;
253
211
254
212
switch (cmd ) {
255
213
case BIO_CTRL_RESET :
@@ -262,31 +220,7 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
262
220
OPENSSL_cleanse (b -> data , b -> max );
263
221
b -> length = 0 ;
264
222
}
265
- bbm -> read_off = 0 ;
266
- } else {
267
- ret = -1 ;
268
- }
269
- break ;
270
- case BIO_C_FILE_SEEK :
271
- if (b -> data == NULL || num < 0 || (size_t )num > b -> max ) {
272
- ret = -1 ;
273
- break ;
274
- }
275
-
276
- if (bio -> flags & BIO_FLAGS_MEM_RDONLY ) {
277
- b -> data -= b -> max - b -> length ;
278
- b -> length = b -> max - num ;
279
- } else {
280
- if ((size_t )num > bbm -> read_off + b -> length ) {
281
- ret = -1 ;
282
- break ;
283
- }
284
-
285
- b -> length = (b -> length + bbm -> read_off ) - num ;
286
223
}
287
-
288
- bbm -> read_off = num ;
289
- ret = num ;
290
224
break ;
291
225
case BIO_CTRL_EOF :
292
226
ret = (long )(b -> length == 0 );
@@ -298,7 +232,7 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
298
232
ret = (long )b -> length ;
299
233
if (ptr != NULL ) {
300
234
char * * pptr = ptr ;
301
- * pptr = ( b -> data != NULL ) ? & b -> data [ bbm -> read_off ] : NULL ;
235
+ * pptr = b -> data ;
302
236
}
303
237
break ;
304
238
case BIO_C_SET_BUF_MEM :
@@ -308,7 +242,6 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
308
242
break ;
309
243
case BIO_C_GET_BUF_MEM_PTR :
310
244
if (ptr != NULL ) {
311
- mem_buf_sync (bio );
312
245
BUF_MEM * * pptr = ptr ;
313
246
* pptr = b ;
314
247
}
@@ -348,17 +281,14 @@ const BIO_METHOD *BIO_s_mem(void) { return &mem_method; }
348
281
349
282
int BIO_mem_contents (const BIO * bio , const uint8_t * * out_contents ,
350
283
size_t * out_len ) {
284
+ const BUF_MEM * b ;
351
285
if (!bio || bio -> method != & mem_method ) {
352
286
return 0 ;
353
287
}
354
288
355
- BIO_BUF_MEM * bbm = (BIO_BUF_MEM * ) bio -> ptr ;
356
- const BUF_MEM * b = bbm -> buf ;
357
-
358
- mem_buf_sync ((BIO * )bio );
359
-
289
+ b = (BUF_MEM * )bio -> ptr ;
360
290
if (out_contents != NULL ) {
361
- * out_contents = (b -> data != NULL ) ? ( uint8_t * )& b -> data [ bbm -> read_off ] : NULL ;
291
+ * out_contents = (uint8_t * )b -> data ;
362
292
}
363
293
if (out_len ) {
364
294
* out_len = b -> length ;
0 commit comments