2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* This is a simple implementation of buffering IO-read functions. */
29 #include <openssl/bio.h>
30 #include <openssl/crypto.h>
31 #include <openssl/x509.h>
32 #include <openssl/ssl.h>
33 #include <openssl/err.h>
34 #include <openssl/pem.h>
38 rbuf_initialize (struct rbuf *rbuf, int fd)
42 /* pointing ssl to NULL results in an unchanged behaviour */
45 rbuf->buffer_pos = rbuf->buffer;
46 rbuf->buffer_left = 0;
50 rbuf_initialized_p (struct rbuf *rbuf)
52 return rbuf->fd != -1;
56 rbuf_uninitialize (struct rbuf *rbuf)
61 /* Currently unused -- see RBUF_READCHAR. */
63 /* Function version of RBUF_READCHAR. */
65 rbuf_readchar (struct rbuf *rbuf, char *store)
67 return RBUF_READCHAR (rbuf, store);
71 /* Like rbuf_readchar(), only don't move the buffer position. */
73 rbuf_peek (struct rbuf *rbuf, char *store)
75 if (!rbuf->buffer_left)
78 rbuf->buffer_pos = rbuf->buffer;
79 rbuf->buffer_left = 0;
81 if (rbuf->ssl != NULL) {
82 res = ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
85 res = iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
91 rbuf->buffer_left = res;
93 *store = *rbuf->buffer_pos;
97 /* Flush RBUF's buffer to WHERE. Flush MAXSIZE bytes at most.
98 Returns the number of bytes actually copied. If the buffer is
99 empty, 0 is returned. */
101 rbuf_flush (struct rbuf *rbuf, char *where, int maxsize)
103 if (!rbuf->buffer_left)
107 int howmuch = MINVAL (rbuf->buffer_left, maxsize);
110 memcpy (where, rbuf->buffer_pos, howmuch);
111 rbuf->buffer_left -= howmuch;
112 rbuf->buffer_pos += howmuch;
117 /* Discard any cached data in RBUF. */
119 rbuf_discard (struct rbuf *rbuf)
121 rbuf->buffer_left = 0;
122 rbuf->buffer_pos = rbuf->buffer;