File: readline.c

package info (click to toggle)
echoping 6.0.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 9,296 kB
  • ctags: 515
  • sloc: sh: 8,951; ansic: 3,352; makefile: 170
file content (199 lines) | stat: -rw-r--r-- 4,593 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Read a line from a descriptor with fgets 
 *
 * $Id: readline.c 377 2007-03-12 20:48:05Z bortz $
 *
 */

#include "echoping.h"

int
readline(fs, ptr, maxlen, ln)
	FILE           *fs;
	char           *ptr;
	int             maxlen;
	unsigned short  ln;
{
	int             n = 1;
	char           *rc;
	int             r;

	/* Reading with fgets or fread instead of read one-character-at-a-time is
	 * more than ten times faster, on a local server. */
	if (ln) {
		rc = fgets(ptr, maxlen + 1, fs);
		if (rc == NULL) {
			if (timeout_flag)
				return n;
			return (-1);
		}
		n = strlen(rc);
		return n;
	} else {
		while (n < maxlen) {
			r = fread(ptr, 1, maxlen, fs);
			if (timeout_flag)
				return r;
			if (r == 0) {
				if (n == 1)
					return (0);	/* EOF, no data read */
				else
					break;	/* EOF, some data was read */
			}
			n = n + r;
		}
	}
	return (n);
}

#ifdef OPENSSL

char            SSL_buffer[MAXTOREAD];
int             buf_ptr;
int             buf_end;

int
SSL_readline(sslh, ptr, maxlen, ln)
	SSL            *sslh;
	char           *ptr;
	int             maxlen;
	unsigned short  ln;
{
	int             rc = 0;
	int             n = 0;
	int             i, oi;
	if (ln) {
		/* Empty buffer */
		if (buf_end == 0) {
			rc = SSL_read(sslh, SSL_buffer, maxlen > MAXTOREAD ? MAXTOREAD : maxlen);
			if (rc < 0)
				return rc;
			buf_end = rc;
			buf_ptr = 0;
		}
		/* No more data in the buffer */
		else if (buf_ptr == buf_end) {
			buf_ptr = 0;
			rc = SSL_read(sslh, SSL_buffer, maxlen > MAXTOREAD ? MAXTOREAD : maxlen);
			if (rc < 0)
				return rc;
			buf_end = rc;
		} else if (SSL_buffer[buf_end - 1] != '\n') {
			/* We have a probleme here is the first SSL_read sent back a 
			 * text not finished by a \n. See www.SSL.de for an example. 
			 * We get more data. See bug #230384 */
			rc = SSL_read(sslh, SSL_buffer + buf_end,
				      maxlen > MAXTOREAD - buf_end ? MAXTOREAD - buf_end : maxlen);
			if (rc < 0)
				return rc;
			buf_end = buf_end + rc;
		}
		for (oi = buf_ptr, i = buf_ptr;
		     i < buf_end && SSL_buffer[i] != '\n'; i++) {
			*ptr++ = SSL_buffer[i];
			buf_ptr++;
		}
		if (i < buf_end && SSL_buffer[i] == '\n')
			buf_ptr++;
		*ptr = '\0';
		/* if (ln) printf ("SSL_readline returns %d (%s)\n", i - oi,
		 * SSL_buffer); */
		return (i - oi);
	} else {
		/* OpenSSL reads at most 4096 characters */
		rc = 1;		/* Just to avoid exiting too soon */
		while (n < maxlen && rc != 0) {
			if ((buf_end == 0) || (buf_ptr > buf_end)) {
				rc = SSL_read(sslh, ptr, maxlen);
				buf_end = 0;
				buf_ptr = 0;
			} else {
				for (i = buf_ptr; i < maxlen && i <= buf_end; i++) {
					*ptr++ = SSL_buffer[i];
					rc++;
				}
				buf_ptr = i;
			}
			n = n + rc;
		}
		return n;
	}
}
#endif

#ifdef GNUTLS

char            TLS_buffer[MAXTOREAD];
int             buf_ptr;
int             buf_end;

ssize_t
TLS_readline(session, ptr, maxlen, ln)
	gnutls_session  session;
	char           *ptr;
	int             maxlen;
	unsigned short  ln;
{
	ssize_t         rc = 0;
	int             n = 0;
	int             i, oi;
	if (ln) {
		/* Empty buffer */
		if (buf_end == 0) {
			rc = gnutls_record_recv(session, TLS_buffer,
					        maxlen > MAXTOREAD ? MAXTOREAD : maxlen);
			if (rc < 0)
				return rc;
			buf_end = rc;
			buf_ptr = 0;
		}
		/* No more data in the buffer */
		else if (buf_ptr == buf_end) {
			buf_ptr = 0;
			rc = gnutls_record_recv(session, TLS_buffer,
					        maxlen > MAXTOREAD ? MAXTOREAD : maxlen);
			if (rc < 0)
				return rc;
			buf_end = rc;
		} else if (TLS_buffer[buf_end - 1] != '\n') {
			rc = gnutls_record_recv(session, TLS_buffer + buf_end,
						maxlen > MAXTOREAD - buf_end ? MAXTOREAD - buf_end : maxlen);
			if (rc < 0)
				return rc;
			buf_end = buf_end + rc;
		}
		for (oi = buf_ptr, i = buf_ptr;
		     i < buf_end && TLS_buffer[i] != '\n'; i++) {
			*ptr++ = TLS_buffer[i];
			buf_ptr++;
		}
		if (i < buf_end && TLS_buffer[i] == '\n')
			buf_ptr++;
		*ptr = '\0';
		/* printf ("DEBUG: TLS_readline returns %d (%s)\n", i - oi,
		 * TLS_buffer); */
		return (i - oi);
	} else {
		rc = 1;		/* Just to avoid exiting too soon */
		while (n < maxlen && rc > 0) {
			if ((buf_end == 0) || (buf_ptr > buf_end)) {
				rc = gnutls_record_recv(session, ptr, maxlen);
				buf_end = 0;
				buf_ptr = 0;
			} else {
				for (i = buf_ptr; i < maxlen && i <= buf_end; i++) {
					*ptr++ = TLS_buffer[i];
					rc++;
					/* printf ("DEBUG: Now %d chars read\n",
					 * rc); */
				}
				buf_ptr = i;
			}
			if (rc > 0)
				n = n + rc;
			/* printf ("DEBUG: Now %d chars to send\n", n); */
		}
		return n;
	}
}
#endif