]> sjero.net Git - wget/blob - src/rbuf.c
[svn] Remove implicit declaration warnings.
[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 /* This is a simple implementation of buffering IO-read functions.  */
21
22 #include <config.h>
23
24 #include <stdio.h>
25
26 #include "wget.h"
27 #include "rbuf.h"
28 #include "connect.h"
29
30 #ifdef HAVE_SSL
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 */
38 #endif /* HAVE_SSL */
39
40 void
41 rbuf_initialize (struct rbuf *rbuf, int fd)
42 {
43   rbuf->fd = fd;
44 #ifdef HAVE_SSL
45 /* pointing ssl to NULL results in an unchanged behaviour */
46   rbuf->ssl = NULL;
47 #endif /* HAVE_SSL */
48   rbuf->buffer_pos = rbuf->buffer;
49   rbuf->buffer_left = 0;
50 }
51
52 int
53 rbuf_initialized_p (struct rbuf *rbuf)
54 {
55   return rbuf->fd != -1;
56 }
57
58 void
59 rbuf_uninitialize (struct rbuf *rbuf)
60 {
61   rbuf->fd = -1;
62 }
63
64 int
65 rbuf_read_bufferful (struct rbuf *rbuf)
66 {
67 #ifdef HAVE_SSL
68   if (rbuf->ssl)
69     return ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
70   else
71 #endif
72     return iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
73 }
74
75 /* Currently unused -- see RBUF_READCHAR.  */
76 #if 0
77 /* Function version of RBUF_READCHAR.  */
78 int
79 rbuf_readchar (struct rbuf *rbuf, char *store)
80 {
81   return RBUF_READCHAR (rbuf, store);
82 }
83 #endif
84
85 /* Like rbuf_readchar(), only don't move the buffer position.  */
86 int
87 rbuf_peek (struct rbuf *rbuf, char *store)
88 {
89   if (!rbuf->buffer_left)
90     {
91       int res;
92       rbuf->buffer_pos = rbuf->buffer;
93       rbuf->buffer_left = 0;
94 #ifdef HAVE_SSL
95                 if (rbuf->ssl != NULL) {
96                 res = ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
97                 } else {
98 #endif /* HAVE_SSL */
99                 res = iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
100 #ifdef HAVE_SSL
101       }
102 #endif /* HAVE_SSL */
103       if (res <= 0)
104         return res;
105       rbuf->buffer_left = res;
106     }
107   *store = *rbuf->buffer_pos;
108   return 1;
109 }
110
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.  */
114 int
115 rbuf_flush (struct rbuf *rbuf, char *where, int maxsize)
116 {
117   if (!rbuf->buffer_left)
118     return 0;
119   else
120     {
121       int howmuch = MINVAL (rbuf->buffer_left, maxsize);
122
123       if (where)
124         memcpy (where, rbuf->buffer_pos, howmuch);
125       rbuf->buffer_left -= howmuch;
126       rbuf->buffer_pos += howmuch;
127       return howmuch;
128     }
129 }
130
131 /* Discard any cached data in RBUF.  */
132 void
133 rbuf_discard (struct rbuf *rbuf)
134 {
135   rbuf->buffer_left = 0;
136   rbuf->buffer_pos = rbuf->buffer;
137 }