summaryrefslogtreecommitdiffstats
path: root/user_call/user_io.c
blob: 3bd6a26da90b8f4fa544ae078a70888ab7ee9db1 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#include "config.h"
#include "user_io.h"

#define BUFLEN	8192

int compression;
int paclen_in		= 256;
int paclen_out		= 256;

/* This is for select_loop() */
static unsigned char buf[BUFLEN];

#ifdef HAVE_ZLIB_H
#include <zlib.h>

/* Error in (de)compression happened? */
static int compression_error;

/* These are for the (de)compressor */
static unsigned char input_buffer[BUFLEN];
static unsigned char output_buffer[BUFLEN];

static z_stream incoming_stream;
static z_stream outgoing_stream;
#endif

#define min(a,b)	((a) < (b) ? (a) : (b))

void err(char *message)
{
	user_write(STDOUT_FILENO, message, strlen(message));
	exit(1);
}

void init_compress(void)
{
#ifdef HAVE_ZLIB_H
	inflateInit(&incoming_stream);
	deflateInit(&outgoing_stream, 9);

	incoming_stream.next_in = input_buffer;
#else
	err("*** Compression support not available!!!\r\n");
#endif
}

void end_compress(void)
{
#ifdef HAVE_ZLIB_H
	inflateEnd(&incoming_stream);
	deflateEnd(&outgoing_stream);
#endif
}

static int flush_output(int fd, const void *buf, size_t count)
{
	const unsigned char *byte = buf;
	int cnt = count;

	while (cnt > 0) {
		if (write(fd, byte, min(paclen_out, cnt)) < 0)
			return -1;
		byte += paclen_out;
		cnt -= paclen_out;
	}

	return count;
}

int user_write(int fd, const void *buf, size_t count)
{
#ifdef HAVE_ZLIB_H
	int status;
#endif

	if (count == 0)
		return 0;

#ifndef HAVE_ZLIB_H
	return flush_output(fd, buf, count);
#else
	/* Only output to stdout can be compressed */
	if (fd != STDOUT_FILENO || !compression)
		return flush_output(fd, buf, count);

	if (compression_error) {
		errno = 0;
		return -1;
	}

	/* Input is the contents of the input buffer. */
	outgoing_stream.next_in = (unsigned char *)buf;
	outgoing_stream.avail_in = count;

	/* Loop compressing until deflate() returns with avail_out != 0. */
	do {
		/* Set up fixed-size output buffer. */
		outgoing_stream.next_out = output_buffer;
		outgoing_stream.avail_out = BUFLEN;

		/* Compress as much data into the buffer as possible. */
		status = deflate(&outgoing_stream, Z_PARTIAL_FLUSH);

		if (status != Z_OK) {
			compression_error = status;
			errno = 0;
			return -1;
		}

		/* Now send the compressed data */
		if (flush_output(fd, output_buffer, BUFLEN - outgoing_stream.avail_out) < 0)
			return -1;
	} while (outgoing_stream.avail_out == 0);

	return count;
#endif
}

int user_read(int fd, void *buf, size_t count)
{
#ifdef HAVE_ZLIB_H
	int status, len;
#endif

	if (count == 0)
		return 0;

#ifndef HAVE_ZLIB_H
	return read(fd, buf, count);
#else
	/* Only input from stdin can be compressed */
	if (fd != STDIN_FILENO || !compression)
		return read(fd, buf, count);

	if (compression_error) {
		errno = 0;
		return -1;
	}

	incoming_stream.next_out = buf;
	incoming_stream.avail_out = count;

	for (;;) {
		status = inflate(&incoming_stream, Z_SYNC_FLUSH);

		if (count - incoming_stream.avail_out > 0)
			return count - incoming_stream.avail_out;

		if (status != Z_OK && status != Z_BUF_ERROR) {
			compression_error = status;
			errno = 0;
			return -1;
		}

		incoming_stream.next_in = input_buffer;
		incoming_stream.avail_in = 0;

		len = read(fd, input_buffer, BUFLEN);
		if (len <= 0)
			return len;

		incoming_stream.avail_in = len;
	}

	return 0;
#endif
}

int select_loop(int s)
{
	fd_set read_fd;
	int n;

	if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
		close(s);
		return -1;
	}
	if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) {
		close(s);
		return -1;
	}

	/*
	 * Loop until one end of the connection goes away.
	 */
	for (;;) {
		FD_ZERO(&read_fd);
		FD_SET(STDIN_FILENO, &read_fd);
		FD_SET(s, &read_fd);

		select(s + 1, &read_fd, NULL, NULL, NULL);

		if (FD_ISSET(s, &read_fd)) {
			while ((n = user_read(s, buf, BUFLEN)) > 0)
				user_write(STDOUT_FILENO, buf, n);
			if (n == 0 || (n < 0 && errno != EAGAIN)) {
				close(s);
				break;
			}
		}

		if (FD_ISSET(STDIN_FILENO, &read_fd)) {
			while ((n = user_read(STDIN_FILENO, buf, BUFLEN)) > 0)
				user_write(s, buf, n);
			if (n == 0 || (n < 0 && errno != EAGAIN)) {
				close(s);
				break;
			}
		}
	}

	return 0;
}