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