2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* This is a simple implementation of buffering IO-read functions. */
31 #include <openssl/bio.h>
32 #include <openssl/crypto.h>
33 #include <openssl/x509.h>
34 #include <openssl/ssl.h>
35 #include <openssl/err.h>
36 #include <openssl/pem.h>
40 rbuf_initialize (struct rbuf *rbuf, int fd)
44 /* pointing ssl to NULL results in an unchanged behaviour */
47 rbuf->buffer_pos = rbuf->buffer;
48 rbuf->buffer_left = 0;
52 rbuf_initialized_p (struct rbuf *rbuf)
54 return rbuf->fd != -1;
58 rbuf_uninitialize (struct rbuf *rbuf)
64 rbuf_read_bufferful (struct rbuf *rbuf)
68 return ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
71 return iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
74 /* Currently unused -- see RBUF_READCHAR. */
76 /* Function version of RBUF_READCHAR. */
78 rbuf_readchar (struct rbuf *rbuf, char *store)
80 return RBUF_READCHAR (rbuf, store);
84 /* Like rbuf_readchar(), only don't move the buffer position. */
86 rbuf_peek (struct rbuf *rbuf, char *store)
88 if (!rbuf->buffer_left)
91 rbuf->buffer_pos = rbuf->buffer;
92 rbuf->buffer_left = 0;
94 if (rbuf->ssl != NULL) {
95 res = ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
98 res = iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
101 #endif /* HAVE_SSL */
104 rbuf->buffer_left = res;
106 *store = *rbuf->buffer_pos;
110 /* Flush RBUF's buffer to WHERE. Flush MAXSIZE bytes at most.
111 Returns the number of bytes actually copied. If the buffer is
112 empty, 0 is returned. */
114 rbuf_flush (struct rbuf *rbuf, char *where, int maxsize)
116 if (!rbuf->buffer_left)
120 int howmuch = MINVAL (rbuf->buffer_left, maxsize);
123 memcpy (where, rbuf->buffer_pos, howmuch);
124 rbuf->buffer_left -= howmuch;
125 rbuf->buffer_pos += howmuch;
130 /* Discard any cached data in RBUF. */
132 rbuf_discard (struct rbuf *rbuf)
134 rbuf->buffer_left = 0;
135 rbuf->buffer_pos = rbuf->buffer;