bio_mem.c
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to. The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  * notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  * must display the following acknowledgement:
32  * "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)"
34  * The word 'cryptographic' can be left out if the rouines from the library
35  * being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  * the apps directory (application code) you must include an acknowledgement:
38  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed. i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/bio.h>
58 
59 #include <limits.h>
60 #include <string.h>
61 
62 #include <openssl/buf.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65 
66 #include "../internal.h"
67 
68 
69 BIO *BIO_new_mem_buf(const void *buf, int len) {
70  BIO *ret;
71  BUF_MEM *b;
72  const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len;
73 
74  if (!buf && len != 0) {
76  return NULL;
77  }
78 
79  ret = BIO_new(BIO_s_mem());
80  if (ret == NULL) {
81  return NULL;
82  }
83 
84  b = (BUF_MEM *)ret->ptr;
85  // BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to.
86  b->data = (void *)buf;
87  b->length = size;
88  b->max = size;
89 
90  ret->flags |= BIO_FLAGS_MEM_RDONLY;
91 
92  // |num| is used to store the value that this BIO will return when it runs
93  // out of data. If it's negative then the retry flags will also be set. Since
94  // this is static data, retrying wont help
95  ret->num = 0;
96 
97  return ret;
98 }
99 
100 static int mem_new(BIO *bio) {
101  BUF_MEM *b;
102 
103  b = BUF_MEM_new();
104  if (b == NULL) {
105  return 0;
106  }
107 
108  // |shutdown| is used to store the close flag: whether the BIO has ownership
109  // of the BUF_MEM.
110  bio->shutdown = 1;
111  bio->init = 1;
112  bio->num = -1;
113  bio->ptr = (char *)b;
114 
115  return 1;
116 }
117 
118 static int mem_free(BIO *bio) {
119  if (!bio->shutdown || !bio->init || bio->ptr == NULL) {
120  return 1;
121  }
122 
123  BUF_MEM *b = (BUF_MEM *)bio->ptr;
124  if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
125  b->data = NULL;
126  }
127  BUF_MEM_free(b);
128  bio->ptr = NULL;
129  return 1;
130 }
131 
132 static int mem_read(BIO *bio, char *out, int outl) {
133  int ret;
134  BUF_MEM *b = (BUF_MEM*) bio->ptr;
135 
137  ret = outl;
138  if (b->length < INT_MAX && ret > (int)b->length) {
139  ret = b->length;
140  }
141 
142  if (ret > 0) {
143  OPENSSL_memcpy(out, b->data, ret);
144  b->length -= ret;
145  if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
146  b->data += ret;
147  } else {
148  OPENSSL_memmove(b->data, &b->data[ret], b->length);
149  }
150  } else if (b->length == 0) {
151  ret = bio->num;
152  if (ret != 0) {
153  BIO_set_retry_read(bio);
154  }
155  }
156  return ret;
157 }
158 
159 static int mem_write(BIO *bio, const char *in, int inl) {
160  int ret = -1;
161  int blen;
162  BUF_MEM *b;
163 
164  b = (BUF_MEM *)bio->ptr;
165 
166  if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
168  goto err;
169  }
170 
172  blen = b->length;
173  if (INT_MAX - blen < inl) {
174  goto err;
175  }
176  if (BUF_MEM_grow_clean(b, blen + inl) != ((size_t) blen) + inl) {
177  goto err;
178  }
179  OPENSSL_memcpy(&b->data[blen], in, inl);
180  ret = inl;
181 
182 err:
183  return ret;
184 }
185 
186 static int mem_gets(BIO *bio, char *buf, int size) {
187  int i, j;
188  char *p;
189  BUF_MEM *b = (BUF_MEM *)bio->ptr;
190 
192  j = b->length;
193  if (size - 1 < j) {
194  j = size - 1;
195  }
196  if (j <= 0) {
197  if (size > 0) {
198  *buf = 0;
199  }
200  return 0;
201  }
202 
203  p = b->data;
204  for (i = 0; i < j; i++) {
205  if (p[i] == '\n') {
206  i++;
207  break;
208  }
209  }
210 
211  // i is now the max num of bytes to copy, either j or up to and including the
212  // first newline
213 
214  i = mem_read(bio, buf, i);
215  if (i > 0) {
216  buf[i] = '\0';
217  }
218  return i;
219 }
220 
221 static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
222  long ret = 1;
223  char **pptr;
224 
225  BUF_MEM *b = (BUF_MEM *)bio->ptr;
226 
227  switch (cmd) {
228  case BIO_CTRL_RESET:
229  if (b->data != NULL) {
230  // For read only case reset to the start again
231  if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
232  b->data -= b->max - b->length;
233  b->length = b->max;
234  } else {
235  OPENSSL_memset(b->data, 0, b->max);
236  b->length = 0;
237  }
238  }
239  break;
240  case BIO_CTRL_EOF:
241  ret = (long)(b->length == 0);
242  break;
244  bio->num = (int)num;
245  break;
246  case BIO_CTRL_INFO:
247  ret = (long)b->length;
248  if (ptr != NULL) {
249  pptr = (char **)ptr;
250  *pptr = (char *)&b->data[0];
251  }
252  break;
253  case BIO_C_SET_BUF_MEM:
254  mem_free(bio);
255  bio->shutdown = (int)num;
256  bio->ptr = ptr;
257  break;
259  if (ptr != NULL) {
260  pptr = (char **)ptr;
261  *pptr = (char *)b;
262  }
263  break;
264  case BIO_CTRL_GET_CLOSE:
265  ret = (long)bio->shutdown;
266  break;
267  case BIO_CTRL_SET_CLOSE:
268  bio->shutdown = (int)num;
269  break;
270 
271  case BIO_CTRL_WPENDING:
272  ret = 0L;
273  break;
274  case BIO_CTRL_PENDING:
275  ret = (long)b->length;
276  break;
277  case BIO_CTRL_FLUSH:
278  ret = 1;
279  break;
280  default:
281  ret = 0;
282  break;
283  }
284  return ret;
285 }
286 
287 static const BIO_METHOD mem_method = {
288  BIO_TYPE_MEM, "memory buffer",
290  NULL /* puts */, mem_gets,
291  mem_ctrl, mem_new,
292  mem_free, NULL /* callback_ctrl */,
293 };
294 
295 const BIO_METHOD *BIO_s_mem(void) { return &mem_method; }
296 
297 int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents,
298  size_t *out_len) {
299  const BUF_MEM *b;
300  if (bio->method != &mem_method) {
301  return 0;
302  }
303 
304  b = (BUF_MEM *)bio->ptr;
305  *out_contents = (uint8_t *)b->data;
306  *out_len = b->length;
307  return 1;
308 }
309 
310 long BIO_get_mem_data(BIO *bio, char **contents) {
311  return BIO_ctrl(bio, BIO_CTRL_INFO, 0, (char *) contents);
312 }
313 
315  return BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, (char *) out);
316 }
317 
318 int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) {
319  return BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, (char *) b);
320 }
321 
322 int BIO_set_mem_eof_return(BIO *bio, int eof_value) {
323  return BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL);
324 }
bio_method_st
Definition: bio.h:808
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
mem_new
static int mem_new(BIO *bio)
Definition: bio_mem.c:100
BIO_CTRL_WPENDING
#define BIO_CTRL_WPENDING
Definition: bio.h:715
BIO_CTRL_INFO
#define BIO_CTRL_INFO
Definition: bio.h:698
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
BIO_get_mem_data
long BIO_get_mem_data(BIO *bio, char **contents)
Definition: bio_mem.c:310
BIO_get_mem_ptr
int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out)
Definition: bio_mem.c:314
bio_st
Definition: bio.h:822
bio_st::ptr
void * ptr
Definition: bio.h:838
bio_st::flags
int flags
Definition: bio.h:832
mem_method
static const BIO_METHOD mem_method
Definition: bio_mem.c:287
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
mem_write
static int mem_write(BIO *bio, const char *in, int inl)
Definition: bio_mem.c:159
bio.h
mem_free
static int mem_free(BIO *bio)
Definition: bio_mem.c:118
string.h
bio_st::init
int init
Definition: bio.h:826
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
error_ref_leak.err
err
Definition: error_ref_leak.py:35
xds_manager.p
p
Definition: xds_manager.py:60
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
BIO_FLAGS_MEM_RDONLY
#define BIO_FLAGS_MEM_RDONLY
Definition: bio.h:771
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
BIO_CTRL_EOF
#define BIO_CTRL_EOF
Definition: bio.h:693
mem_ctrl
static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr)
Definition: bio_mem.c:221
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
xds_interop_client.int
int
Definition: xds_interop_client.py:113
buf.h
BIO_ctrl
#define BIO_ctrl
Definition: boringssl_prefix_symbols.h:779
bio_st::method
const BIO_METHOD * method
Definition: bio.h:823
BIO_C_SET_BUF_MEM_EOF_RETURN
#define BIO_C_SET_BUF_MEM_EOF_RETURN
Definition: bio.h:874
BIO_R_NULL_PARAMETER
#define BIO_R_NULL_PARAMETER
Definition: bio.h:930
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
BIO_new_mem_buf
BIO * BIO_new_mem_buf(const void *buf, int len)
Definition: bio_mem.c:69
BIO_C_GET_BUF_MEM_PTR
#define BIO_C_GET_BUF_MEM_PTR
Definition: bio.h:860
BIO_set_mem_eof_return
int BIO_set_mem_eof_return(BIO *bio, int eof_value)
Definition: bio_mem.c:322
regen-readme.cmd
cmd
Definition: regen-readme.py:21
err.h
BUF_MEM_free
#define BUF_MEM_free
Definition: boringssl_prefix_symbols.h:1008
BUF_MEM_grow_clean
#define BUF_MEM_grow_clean
Definition: boringssl_prefix_symbols.h:1010
BIO_new
#define BIO_new
Definition: boringssl_prefix_symbols.h:814
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
bio_st::shutdown
int shutdown
Definition: bio.h:831
BIO_CTRL_GET_CLOSE
#define BIO_CTRL_GET_CLOSE
Definition: bio.h:702
bio_st::num
int num
Definition: bio.h:836
BIO_R_WRITE_TO_READ_ONLY_BIO
#define BIO_R_WRITE_TO_READ_ONLY_BIO
Definition: bio.h:935
BIO_TYPE_MEM
#define BIO_TYPE_MEM
Definition: bio.h:775
BIO_CTRL_FLUSH
#define BIO_CTRL_FLUSH
Definition: bio.h:712
contents
string_view contents
Definition: elf.cc:597
BIO_CTRL_PENDING
#define BIO_CTRL_PENDING
Definition: bio.h:709
BIO_C_SET_BUF_MEM
#define BIO_C_SET_BUF_MEM
Definition: bio.h:859
BIO_CTRL_SET_CLOSE
#define BIO_CTRL_SET_CLOSE
Definition: bio.h:706
BIO_set_mem_buf
int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership)
Definition: bio_mem.c:318
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
L
lua_State * L
Definition: upb/upb/bindings/lua/main.c:35
xds_manager.num
num
Definition: xds_manager.py:56
OPENSSL_memmove
static void * OPENSSL_memmove(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:827
mem_gets
static int mem_gets(BIO *bio, char *buf, int size)
Definition: bio_mem.c:186
mem_read
static int mem_read(BIO *bio, char *out, int outl)
Definition: bio_mem.c:132
BIO_CTRL_RESET
#define BIO_CTRL_RESET
Definition: bio.h:690
BUF_MEM_new
#define BUF_MEM_new
Definition: boringssl_prefix_symbols.h:1011
mem.h
buf_mem_st
Definition: buf.h:71
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
BIO_set_retry_read
#define BIO_set_retry_read
Definition: boringssl_prefix_symbols.h:853
if
if(p->owned &&p->wrapped !=NULL)
Definition: call.c:42
BIO_mem_contents
int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents, size_t *out_len)
Definition: bio_mem.c:297
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
BIO_clear_retry_flags
#define BIO_clear_retry_flags
Definition: boringssl_prefix_symbols.h:777
BIO_s_mem
const BIO_METHOD * BIO_s_mem(void)
Definition: bio_mem.c:295
google::protobuf.internal.decoder.long
long
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/decoder.py:89


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48