xref: /third_party/openssl/crypto/bio/bss_mem.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <errno.h>
12e1051a39Sopenharmony_ci#include "bio_local.h"
13e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_cistatic int mem_write(BIO *h, const char *buf, int num);
16e1051a39Sopenharmony_cistatic int mem_read(BIO *h, char *buf, int size);
17e1051a39Sopenharmony_cistatic int mem_puts(BIO *h, const char *str);
18e1051a39Sopenharmony_cistatic int mem_gets(BIO *h, char *str, int size);
19e1051a39Sopenharmony_cistatic long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
20e1051a39Sopenharmony_cistatic int mem_new(BIO *h);
21e1051a39Sopenharmony_cistatic int secmem_new(BIO *h);
22e1051a39Sopenharmony_cistatic int mem_free(BIO *data);
23e1051a39Sopenharmony_cistatic int mem_buf_free(BIO *data);
24e1051a39Sopenharmony_cistatic int mem_buf_sync(BIO *h);
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_cistatic const BIO_METHOD mem_method = {
27e1051a39Sopenharmony_ci    BIO_TYPE_MEM,
28e1051a39Sopenharmony_ci    "memory buffer",
29e1051a39Sopenharmony_ci    bwrite_conv,
30e1051a39Sopenharmony_ci    mem_write,
31e1051a39Sopenharmony_ci    bread_conv,
32e1051a39Sopenharmony_ci    mem_read,
33e1051a39Sopenharmony_ci    mem_puts,
34e1051a39Sopenharmony_ci    mem_gets,
35e1051a39Sopenharmony_ci    mem_ctrl,
36e1051a39Sopenharmony_ci    mem_new,
37e1051a39Sopenharmony_ci    mem_free,
38e1051a39Sopenharmony_ci    NULL,                      /* mem_callback_ctrl */
39e1051a39Sopenharmony_ci};
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_cistatic const BIO_METHOD secmem_method = {
42e1051a39Sopenharmony_ci    BIO_TYPE_MEM,
43e1051a39Sopenharmony_ci    "secure memory buffer",
44e1051a39Sopenharmony_ci    bwrite_conv,
45e1051a39Sopenharmony_ci    mem_write,
46e1051a39Sopenharmony_ci    bread_conv,
47e1051a39Sopenharmony_ci    mem_read,
48e1051a39Sopenharmony_ci    mem_puts,
49e1051a39Sopenharmony_ci    mem_gets,
50e1051a39Sopenharmony_ci    mem_ctrl,
51e1051a39Sopenharmony_ci    secmem_new,
52e1051a39Sopenharmony_ci    mem_free,
53e1051a39Sopenharmony_ci    NULL,                      /* mem_callback_ctrl */
54e1051a39Sopenharmony_ci};
55e1051a39Sopenharmony_ci
56e1051a39Sopenharmony_ci/*
57e1051a39Sopenharmony_ci * BIO memory stores buffer and read pointer
58e1051a39Sopenharmony_ci * however the roles are different for read only BIOs.
59e1051a39Sopenharmony_ci * In that case the readp just stores the original state
60e1051a39Sopenharmony_ci * to be used for reset.
61e1051a39Sopenharmony_ci */
62e1051a39Sopenharmony_citypedef struct bio_buf_mem_st {
63e1051a39Sopenharmony_ci    struct buf_mem_st *buf;   /* allocated buffer */
64e1051a39Sopenharmony_ci    struct buf_mem_st *readp; /* read pointer */
65e1051a39Sopenharmony_ci} BIO_BUF_MEM;
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci/*
68e1051a39Sopenharmony_ci * bio->num is used to hold the value to return on 'empty', if it is 0,
69e1051a39Sopenharmony_ci * should_retry is not set
70e1051a39Sopenharmony_ci */
71e1051a39Sopenharmony_ci
72e1051a39Sopenharmony_ciconst BIO_METHOD *BIO_s_mem(void)
73e1051a39Sopenharmony_ci{
74e1051a39Sopenharmony_ci    return &mem_method;
75e1051a39Sopenharmony_ci}
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_ciconst BIO_METHOD *BIO_s_secmem(void)
78e1051a39Sopenharmony_ci{
79e1051a39Sopenharmony_ci    return(&secmem_method);
80e1051a39Sopenharmony_ci}
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ciBIO *BIO_new_mem_buf(const void *buf, int len)
83e1051a39Sopenharmony_ci{
84e1051a39Sopenharmony_ci    BIO *ret;
85e1051a39Sopenharmony_ci    BUF_MEM *b;
86e1051a39Sopenharmony_ci    BIO_BUF_MEM *bb;
87e1051a39Sopenharmony_ci    size_t sz;
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci    if (buf == NULL) {
90e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
91e1051a39Sopenharmony_ci        return NULL;
92e1051a39Sopenharmony_ci    }
93e1051a39Sopenharmony_ci    sz = (len < 0) ? strlen(buf) : (size_t)len;
94e1051a39Sopenharmony_ci    if ((ret = BIO_new(BIO_s_mem())) == NULL)
95e1051a39Sopenharmony_ci        return NULL;
96e1051a39Sopenharmony_ci    bb = (BIO_BUF_MEM *)ret->ptr;
97e1051a39Sopenharmony_ci    b = bb->buf;
98e1051a39Sopenharmony_ci    /* Cast away const and trust in the MEM_RDONLY flag. */
99e1051a39Sopenharmony_ci    b->data = (void *)buf;
100e1051a39Sopenharmony_ci    b->length = sz;
101e1051a39Sopenharmony_ci    b->max = sz;
102e1051a39Sopenharmony_ci    *bb->readp = *bb->buf;
103e1051a39Sopenharmony_ci    ret->flags |= BIO_FLAGS_MEM_RDONLY;
104e1051a39Sopenharmony_ci    /* Since this is static data retrying won't help */
105e1051a39Sopenharmony_ci    ret->num = 0;
106e1051a39Sopenharmony_ci    return ret;
107e1051a39Sopenharmony_ci}
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_cistatic int mem_init(BIO *bi, unsigned long flags)
110e1051a39Sopenharmony_ci{
111e1051a39Sopenharmony_ci    BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_ci    if (bb == NULL)
114e1051a39Sopenharmony_ci        return 0;
115e1051a39Sopenharmony_ci    if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
116e1051a39Sopenharmony_ci        OPENSSL_free(bb);
117e1051a39Sopenharmony_ci        return 0;
118e1051a39Sopenharmony_ci    }
119e1051a39Sopenharmony_ci    if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
120e1051a39Sopenharmony_ci        BUF_MEM_free(bb->buf);
121e1051a39Sopenharmony_ci        OPENSSL_free(bb);
122e1051a39Sopenharmony_ci        return 0;
123e1051a39Sopenharmony_ci    }
124e1051a39Sopenharmony_ci    *bb->readp = *bb->buf;
125e1051a39Sopenharmony_ci    bi->shutdown = 1;
126e1051a39Sopenharmony_ci    bi->init = 1;
127e1051a39Sopenharmony_ci    bi->num = -1;
128e1051a39Sopenharmony_ci    bi->ptr = (char *)bb;
129e1051a39Sopenharmony_ci    return 1;
130e1051a39Sopenharmony_ci}
131e1051a39Sopenharmony_ci
132e1051a39Sopenharmony_cistatic int mem_new(BIO *bi)
133e1051a39Sopenharmony_ci{
134e1051a39Sopenharmony_ci    return mem_init(bi, 0L);
135e1051a39Sopenharmony_ci}
136e1051a39Sopenharmony_ci
137e1051a39Sopenharmony_cistatic int secmem_new(BIO *bi)
138e1051a39Sopenharmony_ci{
139e1051a39Sopenharmony_ci    return mem_init(bi, BUF_MEM_FLAG_SECURE);
140e1051a39Sopenharmony_ci}
141e1051a39Sopenharmony_ci
142e1051a39Sopenharmony_cistatic int mem_free(BIO *a)
143e1051a39Sopenharmony_ci{
144e1051a39Sopenharmony_ci    BIO_BUF_MEM *bb;
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci    if (a == NULL)
147e1051a39Sopenharmony_ci        return 0;
148e1051a39Sopenharmony_ci
149e1051a39Sopenharmony_ci    bb = (BIO_BUF_MEM *)a->ptr;
150e1051a39Sopenharmony_ci    if (!mem_buf_free(a))
151e1051a39Sopenharmony_ci        return 0;
152e1051a39Sopenharmony_ci    OPENSSL_free(bb->readp);
153e1051a39Sopenharmony_ci    OPENSSL_free(bb);
154e1051a39Sopenharmony_ci    return 1;
155e1051a39Sopenharmony_ci}
156e1051a39Sopenharmony_ci
157e1051a39Sopenharmony_cistatic int mem_buf_free(BIO *a)
158e1051a39Sopenharmony_ci{
159e1051a39Sopenharmony_ci    if (a == NULL)
160e1051a39Sopenharmony_ci        return 0;
161e1051a39Sopenharmony_ci
162e1051a39Sopenharmony_ci    if (a->shutdown && a->init && a->ptr != NULL) {
163e1051a39Sopenharmony_ci        BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
164e1051a39Sopenharmony_ci        BUF_MEM *b = bb->buf;
165e1051a39Sopenharmony_ci
166e1051a39Sopenharmony_ci        if (a->flags & BIO_FLAGS_MEM_RDONLY)
167e1051a39Sopenharmony_ci            b->data = NULL;
168e1051a39Sopenharmony_ci        BUF_MEM_free(b);
169e1051a39Sopenharmony_ci    }
170e1051a39Sopenharmony_ci    return 1;
171e1051a39Sopenharmony_ci}
172e1051a39Sopenharmony_ci
173e1051a39Sopenharmony_ci/*
174e1051a39Sopenharmony_ci * Reallocate memory buffer if read pointer differs
175e1051a39Sopenharmony_ci * NOT FOR RDONLY
176e1051a39Sopenharmony_ci */
177e1051a39Sopenharmony_cistatic int mem_buf_sync(BIO *b)
178e1051a39Sopenharmony_ci{
179e1051a39Sopenharmony_ci    if (b != NULL && b->init != 0 && b->ptr != NULL) {
180e1051a39Sopenharmony_ci        BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
181e1051a39Sopenharmony_ci
182e1051a39Sopenharmony_ci        if (bbm->readp->data != bbm->buf->data) {
183e1051a39Sopenharmony_ci            memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
184e1051a39Sopenharmony_ci            bbm->buf->length = bbm->readp->length;
185e1051a39Sopenharmony_ci            bbm->readp->data = bbm->buf->data;
186e1051a39Sopenharmony_ci        }
187e1051a39Sopenharmony_ci    }
188e1051a39Sopenharmony_ci    return 0;
189e1051a39Sopenharmony_ci}
190e1051a39Sopenharmony_ci
191e1051a39Sopenharmony_cistatic int mem_read(BIO *b, char *out, int outl)
192e1051a39Sopenharmony_ci{
193e1051a39Sopenharmony_ci    int ret = -1;
194e1051a39Sopenharmony_ci    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
195e1051a39Sopenharmony_ci    BUF_MEM *bm = bbm->readp;
196e1051a39Sopenharmony_ci
197e1051a39Sopenharmony_ci    if (b->flags & BIO_FLAGS_MEM_RDONLY)
198e1051a39Sopenharmony_ci        bm = bbm->buf;
199e1051a39Sopenharmony_ci    BIO_clear_retry_flags(b);
200e1051a39Sopenharmony_ci    ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
201e1051a39Sopenharmony_ci    if ((out != NULL) && (ret > 0)) {
202e1051a39Sopenharmony_ci        memcpy(out, bm->data, ret);
203e1051a39Sopenharmony_ci        bm->length -= ret;
204e1051a39Sopenharmony_ci        bm->max -= ret;
205e1051a39Sopenharmony_ci        bm->data += ret;
206e1051a39Sopenharmony_ci    } else if (bm->length == 0) {
207e1051a39Sopenharmony_ci        ret = b->num;
208e1051a39Sopenharmony_ci        if (ret != 0)
209e1051a39Sopenharmony_ci            BIO_set_retry_read(b);
210e1051a39Sopenharmony_ci    }
211e1051a39Sopenharmony_ci    return ret;
212e1051a39Sopenharmony_ci}
213e1051a39Sopenharmony_ci
214e1051a39Sopenharmony_cistatic int mem_write(BIO *b, const char *in, int inl)
215e1051a39Sopenharmony_ci{
216e1051a39Sopenharmony_ci    int ret = -1;
217e1051a39Sopenharmony_ci    int blen;
218e1051a39Sopenharmony_ci    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
219e1051a39Sopenharmony_ci
220e1051a39Sopenharmony_ci    if (b->flags & BIO_FLAGS_MEM_RDONLY) {
221e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
222e1051a39Sopenharmony_ci        goto end;
223e1051a39Sopenharmony_ci    }
224e1051a39Sopenharmony_ci    BIO_clear_retry_flags(b);
225e1051a39Sopenharmony_ci    if (inl == 0)
226e1051a39Sopenharmony_ci        return 0;
227e1051a39Sopenharmony_ci    if (in == NULL) {
228e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
229e1051a39Sopenharmony_ci        goto end;
230e1051a39Sopenharmony_ci    }
231e1051a39Sopenharmony_ci    blen = bbm->readp->length;
232e1051a39Sopenharmony_ci    mem_buf_sync(b);
233e1051a39Sopenharmony_ci    if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
234e1051a39Sopenharmony_ci        goto end;
235e1051a39Sopenharmony_ci    memcpy(bbm->buf->data + blen, in, inl);
236e1051a39Sopenharmony_ci    *bbm->readp = *bbm->buf;
237e1051a39Sopenharmony_ci    ret = inl;
238e1051a39Sopenharmony_ci end:
239e1051a39Sopenharmony_ci    return ret;
240e1051a39Sopenharmony_ci}
241e1051a39Sopenharmony_ci
242e1051a39Sopenharmony_cistatic long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
243e1051a39Sopenharmony_ci{
244e1051a39Sopenharmony_ci    long ret = 1;
245e1051a39Sopenharmony_ci    char **pptr;
246e1051a39Sopenharmony_ci    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
247e1051a39Sopenharmony_ci    BUF_MEM *bm, *bo;            /* bio_mem, bio_other */
248e1051a39Sopenharmony_ci    long off, remain;
249e1051a39Sopenharmony_ci
250e1051a39Sopenharmony_ci    if (b->flags & BIO_FLAGS_MEM_RDONLY) {
251e1051a39Sopenharmony_ci        bm = bbm->buf;
252e1051a39Sopenharmony_ci        bo = bbm->readp;
253e1051a39Sopenharmony_ci    } else {
254e1051a39Sopenharmony_ci        bm = bbm->readp;
255e1051a39Sopenharmony_ci        bo = bbm->buf;
256e1051a39Sopenharmony_ci    }
257e1051a39Sopenharmony_ci    off = (bm->data == bo->data) ? 0 : bm->data - bo->data;
258e1051a39Sopenharmony_ci    remain = bm->length;
259e1051a39Sopenharmony_ci
260e1051a39Sopenharmony_ci    switch (cmd) {
261e1051a39Sopenharmony_ci    case BIO_CTRL_RESET:
262e1051a39Sopenharmony_ci        bm = bbm->buf;
263e1051a39Sopenharmony_ci        if (bm->data != NULL) {
264e1051a39Sopenharmony_ci            if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) {
265e1051a39Sopenharmony_ci                if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) {
266e1051a39Sopenharmony_ci                    memset(bm->data, 0, bm->max);
267e1051a39Sopenharmony_ci                    bm->length = 0;
268e1051a39Sopenharmony_ci                }
269e1051a39Sopenharmony_ci                *bbm->readp = *bbm->buf;
270e1051a39Sopenharmony_ci            } else {
271e1051a39Sopenharmony_ci                /* For read only case just reset to the start again */
272e1051a39Sopenharmony_ci                *bbm->buf = *bbm->readp;
273e1051a39Sopenharmony_ci            }
274e1051a39Sopenharmony_ci        }
275e1051a39Sopenharmony_ci        break;
276e1051a39Sopenharmony_ci    case BIO_C_FILE_SEEK:
277e1051a39Sopenharmony_ci        if (num < 0 || num > off + remain)
278e1051a39Sopenharmony_ci            return -1;   /* Can't see outside of the current buffer */
279e1051a39Sopenharmony_ci
280e1051a39Sopenharmony_ci        bm->data = (num != 0) ? bo->data + num : bo->data;
281e1051a39Sopenharmony_ci        bm->length = bo->length - num;
282e1051a39Sopenharmony_ci        bm->max = bo->max - num;
283e1051a39Sopenharmony_ci        off = num;
284e1051a39Sopenharmony_ci        /* FALLTHRU */
285e1051a39Sopenharmony_ci    case BIO_C_FILE_TELL:
286e1051a39Sopenharmony_ci        ret = off;
287e1051a39Sopenharmony_ci        break;
288e1051a39Sopenharmony_ci    case BIO_CTRL_EOF:
289e1051a39Sopenharmony_ci        ret = (long)(bm->length == 0);
290e1051a39Sopenharmony_ci        break;
291e1051a39Sopenharmony_ci    case BIO_C_SET_BUF_MEM_EOF_RETURN:
292e1051a39Sopenharmony_ci        b->num = (int)num;
293e1051a39Sopenharmony_ci        break;
294e1051a39Sopenharmony_ci    case BIO_CTRL_INFO:
295e1051a39Sopenharmony_ci        ret = (long)bm->length;
296e1051a39Sopenharmony_ci        if (ptr != NULL) {
297e1051a39Sopenharmony_ci            pptr = (char **)ptr;
298e1051a39Sopenharmony_ci            *pptr = (char *)(bm->data);
299e1051a39Sopenharmony_ci        }
300e1051a39Sopenharmony_ci        break;
301e1051a39Sopenharmony_ci    case BIO_C_SET_BUF_MEM:
302e1051a39Sopenharmony_ci        mem_buf_free(b);
303e1051a39Sopenharmony_ci        b->shutdown = (int)num;
304e1051a39Sopenharmony_ci        bbm->buf = ptr;
305e1051a39Sopenharmony_ci        *bbm->readp = *bbm->buf;
306e1051a39Sopenharmony_ci        break;
307e1051a39Sopenharmony_ci    case BIO_C_GET_BUF_MEM_PTR:
308e1051a39Sopenharmony_ci        if (ptr != NULL) {
309e1051a39Sopenharmony_ci            if (!(b->flags & BIO_FLAGS_MEM_RDONLY))
310e1051a39Sopenharmony_ci                mem_buf_sync(b);
311e1051a39Sopenharmony_ci            bm = bbm->buf;
312e1051a39Sopenharmony_ci            pptr = (char **)ptr;
313e1051a39Sopenharmony_ci            *pptr = (char *)bm;
314e1051a39Sopenharmony_ci        }
315e1051a39Sopenharmony_ci        break;
316e1051a39Sopenharmony_ci    case BIO_CTRL_GET_CLOSE:
317e1051a39Sopenharmony_ci        ret = (long)b->shutdown;
318e1051a39Sopenharmony_ci        break;
319e1051a39Sopenharmony_ci    case BIO_CTRL_SET_CLOSE:
320e1051a39Sopenharmony_ci        b->shutdown = (int)num;
321e1051a39Sopenharmony_ci        break;
322e1051a39Sopenharmony_ci    case BIO_CTRL_WPENDING:
323e1051a39Sopenharmony_ci        ret = 0L;
324e1051a39Sopenharmony_ci        break;
325e1051a39Sopenharmony_ci    case BIO_CTRL_PENDING:
326e1051a39Sopenharmony_ci        ret = (long)bm->length;
327e1051a39Sopenharmony_ci        break;
328e1051a39Sopenharmony_ci    case BIO_CTRL_DUP:
329e1051a39Sopenharmony_ci    case BIO_CTRL_FLUSH:
330e1051a39Sopenharmony_ci        ret = 1;
331e1051a39Sopenharmony_ci        break;
332e1051a39Sopenharmony_ci    case BIO_CTRL_PUSH:
333e1051a39Sopenharmony_ci    case BIO_CTRL_POP:
334e1051a39Sopenharmony_ci    default:
335e1051a39Sopenharmony_ci        ret = 0;
336e1051a39Sopenharmony_ci        break;
337e1051a39Sopenharmony_ci    }
338e1051a39Sopenharmony_ci    return ret;
339e1051a39Sopenharmony_ci}
340e1051a39Sopenharmony_ci
341e1051a39Sopenharmony_cistatic int mem_gets(BIO *bp, char *buf, int size)
342e1051a39Sopenharmony_ci{
343e1051a39Sopenharmony_ci    int i, j;
344e1051a39Sopenharmony_ci    int ret = -1;
345e1051a39Sopenharmony_ci    char *p;
346e1051a39Sopenharmony_ci    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
347e1051a39Sopenharmony_ci    BUF_MEM *bm = bbm->readp;
348e1051a39Sopenharmony_ci
349e1051a39Sopenharmony_ci    if (bp->flags & BIO_FLAGS_MEM_RDONLY)
350e1051a39Sopenharmony_ci        bm = bbm->buf;
351e1051a39Sopenharmony_ci    BIO_clear_retry_flags(bp);
352e1051a39Sopenharmony_ci    j = bm->length;
353e1051a39Sopenharmony_ci    if ((size - 1) < j)
354e1051a39Sopenharmony_ci        j = size - 1;
355e1051a39Sopenharmony_ci    if (j <= 0) {
356e1051a39Sopenharmony_ci        *buf = '\0';
357e1051a39Sopenharmony_ci        return 0;
358e1051a39Sopenharmony_ci    }
359e1051a39Sopenharmony_ci    p = bm->data;
360e1051a39Sopenharmony_ci    for (i = 0; i < j; i++) {
361e1051a39Sopenharmony_ci        if (p[i] == '\n') {
362e1051a39Sopenharmony_ci            i++;
363e1051a39Sopenharmony_ci            break;
364e1051a39Sopenharmony_ci        }
365e1051a39Sopenharmony_ci    }
366e1051a39Sopenharmony_ci
367e1051a39Sopenharmony_ci    /*
368e1051a39Sopenharmony_ci     * i is now the max num of bytes to copy, either j or up to
369e1051a39Sopenharmony_ci     * and including the first newline
370e1051a39Sopenharmony_ci     */
371e1051a39Sopenharmony_ci
372e1051a39Sopenharmony_ci    i = mem_read(bp, buf, i);
373e1051a39Sopenharmony_ci    if (i > 0)
374e1051a39Sopenharmony_ci        buf[i] = '\0';
375e1051a39Sopenharmony_ci    ret = i;
376e1051a39Sopenharmony_ci    return ret;
377e1051a39Sopenharmony_ci}
378e1051a39Sopenharmony_ci
379e1051a39Sopenharmony_cistatic int mem_puts(BIO *bp, const char *str)
380e1051a39Sopenharmony_ci{
381e1051a39Sopenharmony_ci    int n, ret;
382e1051a39Sopenharmony_ci
383e1051a39Sopenharmony_ci    n = strlen(str);
384e1051a39Sopenharmony_ci    ret = mem_write(bp, str, n);
385e1051a39Sopenharmony_ci    /* memory semantics is that it will always work */
386e1051a39Sopenharmony_ci    return ret;
387e1051a39Sopenharmony_ci}
388