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>
37 #include "gen_sslfunc.h" /* for ssl_iread */
41 rbuf_initialize (struct rbuf *rbuf, int fd)
45 /* pointing ssl to NULL results in an unchanged behaviour */
48 rbuf->buffer_pos = rbuf->buffer;
49 rbuf->buffer_left = 0;
53 rbuf_initialized_p (struct rbuf *rbuf)
55 return rbuf->fd != -1;
59 rbuf_uninitialize (struct rbuf *rbuf)
65 rbuf_read_bufferful (struct rbuf *rbuf)
69 return ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
72 return iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
75 /* Currently unused -- see RBUF_READCHAR. */
77 /* Function version of RBUF_READCHAR. */
79 rbuf_readchar (struct rbuf *rbuf, char *store)
81 return RBUF_READCHAR (rbuf, store);
85 /* Like rbuf_readchar(), only don't move the buffer position. */
87 rbuf_peek (struct rbuf *rbuf, char *store)
89 if (!rbuf->buffer_left)
92 rbuf->buffer_pos = rbuf->buffer;
93 rbuf->buffer_left = 0;
95 if (rbuf->ssl != NULL) {
96 res = ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
99 res = iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
102 #endif /* HAVE_SSL */
105 rbuf->buffer_left = res;
107 *store = *rbuf->buffer_pos;
111 /* Flush RBUF's buffer to WHERE. Flush MAXSIZE bytes at most.
112 Returns the number of bytes actually copied. If the buffer is
113 empty, 0 is returned. */
115 rbuf_flush (struct rbuf *rbuf, char *where, int maxsize)
117 if (!rbuf->buffer_left)
121 int howmuch = MINVAL (rbuf->buffer_left, maxsize);
124 memcpy (where, rbuf->buffer_pos, howmuch);
125 rbuf->buffer_left -= howmuch;
126 rbuf->buffer_pos += howmuch;
131 /* Discard any cached data in RBUF. */
133 rbuf_discard (struct rbuf *rbuf)
135 rbuf->buffer_left = 0;
136 rbuf->buffer_pos = rbuf->buffer;