]> sjero.net Git - wget/blob - src/rbuf.c
831b4b6cc0b87a0ec7b66c4ccbb6b346af86b5db
[wget] / src / rbuf.c
1 /* Buffering read.
2    Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 /* This is a simple implementation of buffering IO-read functions.  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23
24 #include "wget.h"
25 #include "rbuf.h"
26 #include "connect.h"
27
28 #ifdef HAVE_SSL
29 #include <openssl/bio.h>
30 #include <openssl/crypto.h>
31 #include <openssl/x509.h>
32 #include <openssl/ssl.h>
33 #include <openssl/err.h>
34 #include <openssl/pem.h>
35 #endif /* HAVE_SSL */
36
37 void
38 rbuf_initialize (struct rbuf *rbuf, int fd)
39 {
40   rbuf->fd = fd;
41 #ifdef HAVE_SSL
42 /* pointing ssl to NULL results in an unchanged behaviour */
43   rbuf->ssl = NULL;
44 #endif /* HAVE_SSL */
45   rbuf->buffer_pos = rbuf->buffer;
46   rbuf->buffer_left = 0;
47 }
48
49 int
50 rbuf_initialized_p (struct rbuf *rbuf)
51 {
52   return rbuf->fd != -1;
53 }
54
55 void
56 rbuf_uninitialize (struct rbuf *rbuf)
57 {
58   rbuf->fd = -1;
59 }
60
61 int
62 rbuf_read_bufferful (struct rbuf *rbuf)
63 {
64 #ifdef HAVE_SSL
65   if (rbuf->ssl)
66     return ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
67   else
68 #endif
69     return iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
70 }
71
72 /* Currently unused -- see RBUF_READCHAR.  */
73 #if 0
74 /* Function version of RBUF_READCHAR.  */
75 int
76 rbuf_readchar (struct rbuf *rbuf, char *store)
77 {
78   return RBUF_READCHAR (rbuf, store);
79 }
80 #endif
81
82 /* Like rbuf_readchar(), only don't move the buffer position.  */
83 int
84 rbuf_peek (struct rbuf *rbuf, char *store)
85 {
86   if (!rbuf->buffer_left)
87     {
88       int res;
89       rbuf->buffer_pos = rbuf->buffer;
90       rbuf->buffer_left = 0;
91 #ifdef HAVE_SSL
92                 if (rbuf->ssl != NULL) {
93                 res = ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
94                 } else {
95 #endif /* HAVE_SSL */
96                 res = iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
97 #ifdef HAVE_SSL
98       }
99 #endif /* HAVE_SSL */
100       if (res <= 0)
101         return res;
102       rbuf->buffer_left = res;
103     }
104   *store = *rbuf->buffer_pos;
105   return 1;
106 }
107
108 /* Flush RBUF's buffer to WHERE.  Flush MAXSIZE bytes at most.
109    Returns the number of bytes actually copied.  If the buffer is
110    empty, 0 is returned.  */
111 int
112 rbuf_flush (struct rbuf *rbuf, char *where, int maxsize)
113 {
114   if (!rbuf->buffer_left)
115     return 0;
116   else
117     {
118       int howmuch = MINVAL (rbuf->buffer_left, maxsize);
119
120       if (where)
121         memcpy (where, rbuf->buffer_pos, howmuch);
122       rbuf->buffer_left -= howmuch;
123       rbuf->buffer_pos += howmuch;
124       return howmuch;
125     }
126 }
127
128 /* Discard any cached data in RBUF.  */
129 void
130 rbuf_discard (struct rbuf *rbuf)
131 {
132   rbuf->buffer_left = 0;
133   rbuf->buffer_pos = rbuf->buffer;
134 }