]> sjero.net Git - wget/blob - src/rbuf.c
43d636e3532d19c5840d0c3d140cd03d6dee400d
[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 #ifdef HAVE_STRING_H
26 # include <string.h>
27 #else
28 # include <strings.h>
29 #endif
30
31 #include "wget.h"
32 #include "rbuf.h"
33 #include "connect.h"
34
35 #ifdef HAVE_SSL
36 #include <openssl/bio.h>
37 #include <openssl/crypto.h>
38 #include <openssl/x509.h>
39 #include <openssl/ssl.h>
40 #include <openssl/err.h>
41 #include <openssl/pem.h>
42 #include "gen_sslfunc.h"        /* for ssl_iread */
43 #endif /* HAVE_SSL */
44
45 void
46 rbuf_initialize (struct rbuf *rbuf, int fd)
47 {
48   rbuf->fd = fd;
49 #ifdef HAVE_SSL
50 /* pointing ssl to NULL results in an unchanged behaviour */
51   rbuf->ssl = NULL;
52 #endif /* HAVE_SSL */
53   rbuf->buffer_pos = rbuf->buffer;
54   rbuf->buffer_left = 0;
55 }
56
57 int
58 rbuf_initialized_p (struct rbuf *rbuf)
59 {
60   return rbuf->fd != -1;
61 }
62
63 void
64 rbuf_uninitialize (struct rbuf *rbuf)
65 {
66   rbuf->fd = -1;
67 }
68
69 int
70 rbuf_read_bufferful (struct rbuf *rbuf)
71 {
72 #ifdef HAVE_SSL
73   if (rbuf->ssl)
74     return ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
75   else
76 #endif
77     return iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
78 }
79
80 /* Currently unused -- see RBUF_READCHAR.  */
81 #if 0
82 /* Function version of RBUF_READCHAR.  */
83 int
84 rbuf_readchar (struct rbuf *rbuf, char *store)
85 {
86   return RBUF_READCHAR (rbuf, store);
87 }
88 #endif
89
90 /* Like rbuf_readchar(), only don't move the buffer position.  */
91 int
92 rbuf_peek (struct rbuf *rbuf, char *store)
93 {
94   if (!rbuf->buffer_left)
95     {
96       int res;
97       rbuf->buffer_pos = rbuf->buffer;
98       rbuf->buffer_left = 0;
99 #ifdef HAVE_SSL
100                 if (rbuf->ssl != NULL) {
101                 res = ssl_iread (rbuf->ssl, rbuf->buffer, sizeof (rbuf->buffer));
102                 } else {
103 #endif /* HAVE_SSL */
104                 res = iread (rbuf->fd, rbuf->buffer, sizeof (rbuf->buffer));
105 #ifdef HAVE_SSL
106       }
107 #endif /* HAVE_SSL */
108       if (res <= 0)
109         return res;
110       rbuf->buffer_left = res;
111     }
112   *store = *rbuf->buffer_pos;
113   return 1;
114 }
115
116 /* Flush RBUF's buffer to WHERE.  Flush MAXSIZE bytes at most.
117    Returns the number of bytes actually copied.  If the buffer is
118    empty, 0 is returned.  */
119 int
120 rbuf_flush (struct rbuf *rbuf, char *where, int maxsize)
121 {
122   if (!rbuf->buffer_left)
123     return 0;
124   else
125     {
126       int howmuch = MINVAL (rbuf->buffer_left, maxsize);
127
128       if (where)
129         memcpy (where, rbuf->buffer_pos, howmuch);
130       rbuf->buffer_left -= howmuch;
131       rbuf->buffer_pos += howmuch;
132       return howmuch;
133     }
134 }
135
136 /* Discard any cached data in RBUF.  */
137 void
138 rbuf_discard (struct rbuf *rbuf)
139 {
140   rbuf->buffer_left = 0;
141   rbuf->buffer_pos = rbuf->buffer;
142 }