]> sjero.net Git - wget/blob - src/rbuf.c
[svn] Update the license to include the OpenSSL exception.
[wget] / src / rbuf.c
1 /* Buffering read.
2    Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU Wget.
5
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.
10
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.
15
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.
19
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables.  You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL".  If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so.  If you do not wish to do
28 so, delete this exception statement from your version.  */
29
30 /* This is a simple implementation of buffering IO-read functions.  */
31
32 #include <config.h>
33
34 #include <stdio.h>
35 #ifdef HAVE_STRING_H
36 # include <string.h>
37 #else
38 # include <strings.h>
39 #endif
40
41 #include "wget.h"
42 #include "rbuf.h"
43 #include "connect.h"
44
45 #ifdef HAVE_SSL
46 #include <openssl/bio.h>
47 #include <openssl/crypto.h>
48 #include <openssl/x509.h>
49 #include <openssl/ssl.h>
50 #include <openssl/err.h>
51 #include <openssl/pem.h>
52 #include "gen_sslfunc.h"        /* for ssl_iread */
53 #endif /* HAVE_SSL */
54
55 void
56 rbuf_initialize (struct rbuf *rbuf, int fd)
57 {
58   rbuf->fd = fd;
59 #ifdef HAVE_SSL
60 /* pointing ssl to NULL results in an unchanged behaviour */
61   rbuf->ssl = NULL;
62 #endif /* HAVE_SSL */
63   rbuf->buffer_pos = rbuf->buffer;
64   rbuf->buffer_left = 0;
65 }
66
67 int
68 rbuf_initialized_p (struct rbuf *rbuf)
69 {
70   return rbuf->fd != -1;
71 }
72
73 void
74 rbuf_uninitialize (struct rbuf *rbuf)
75 {
76   rbuf->fd = -1;
77 }
78
79 int
80 rbuf_read_bufferful (struct rbuf *rbuf)
81 {
82 #ifdef HAVE_SSL
83   if (rbuf->ssl)
84     return ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
85   else
86 #endif
87     return iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
88 }
89
90 /* Currently unused -- see RBUF_READCHAR.  */
91 #if 0
92 /* Function version of RBUF_READCHAR.  */
93 int
94 rbuf_readchar (struct rbuf *rbuf, char *store)
95 {
96   return RBUF_READCHAR (rbuf, store);
97 }
98 #endif
99
100 /* Like rbuf_readchar(), only don't move the buffer position.  */
101 int
102 rbuf_peek (struct rbuf *rbuf, char *store)
103 {
104   if (!rbuf->buffer_left)
105     {
106       int res;
107       rbuf->buffer_pos = rbuf->buffer;
108       rbuf->buffer_left = 0;
109 #ifdef HAVE_SSL
110                 if (rbuf->ssl != NULL) {
111                 res = ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
112                 } else {
113 #endif /* HAVE_SSL */
114                 res = iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
115 #ifdef HAVE_SSL
116       }
117 #endif /* HAVE_SSL */
118       if (res <= 0)
119         return res;
120       rbuf->buffer_left = res;
121     }
122   *store = *rbuf->buffer_pos;
123   return 1;
124 }
125
126 /* Flush RBUF's buffer to WHERE.  Flush MAXSIZE bytes at most.
127    Returns the number of bytes actually copied.  If the buffer is
128    empty, 0 is returned.  */
129 int
130 rbuf_flush (struct rbuf *rbuf, char *where, int maxsize)
131 {
132   if (!rbuf->buffer_left)
133     return 0;
134   else
135     {
136       int howmuch = MINVAL (rbuf->buffer_left, maxsize);
137
138       if (where)
139         memcpy (where, rbuf->buffer_pos, howmuch);
140       rbuf->buffer_left -= howmuch;
141       rbuf->buffer_pos += howmuch;
142       return howmuch;
143     }
144 }
145
146 /* Discard any cached data in RBUF.  */
147 void
148 rbuf_discard (struct rbuf *rbuf)
149 {
150   rbuf->buffer_left = 0;
151   rbuf->buffer_pos = rbuf->buffer;
152 }