summaryrefslogtreecommitdiffstats
path: root/mutt_zstrm.c
blob: 7507594c8abb736b1fdf3078fc83282ba9d56f3c (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (C) 2019 Fabian Groffen <grobian@gentoo.org>
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <zlib.h>
#include <errno.h>

#include "mutt.h"
#include "mutt_socket.h"
#include "mutt_zstrm.h"

typedef struct
{
  struct zstrm_direction {
    z_stream z;
    char *buf;
    unsigned int len;
    unsigned int pos;
    unsigned int conn_eof : 1;
    unsigned int stream_eof : 1;
  } read, write;

  /* underlying stream */
  CONNECTION next_conn;
}
zstrmctx;

/* simple wrapper functions to match zlib interface for calling
 * malloc/free */
static void *mutt_zstrm_malloc (void* op, unsigned int sze, unsigned int v)
{
  return safe_calloc (sze, v);
}

static void mutt_zstrm_free (void* op, void* ptr)
{
  FREE (&ptr);
}

static int mutt_zstrm_open (CONNECTION* conn)
{
  /* cannot open a zlib connection, must wrap an existing one */
  return -1;
}

static int mutt_zstrm_close (CONNECTION* conn)
{
  zstrmctx* zctx = conn->sockdata;
  int rc = zctx->next_conn.conn_close (&zctx->next_conn);

  dprint (4, (debugfile, "zstrm_close: read %llu->%llu (%.1fx) "
	"wrote %llu<-%llu (%.1fx)\n",
	zctx->read.z.total_in, zctx->read.z.total_out,
	(float)zctx->read.z.total_out / (float)zctx->read.z.total_in,
	zctx->write.z.total_in, zctx->write.z.total_out,
	(float)zctx->write.z.total_in / (float)zctx->write.z.total_out));

  conn->sockdata   = zctx->next_conn.sockdata;
  conn->conn_open  = zctx->next_conn.conn_open;
  conn->conn_close = zctx->next_conn.conn_close;
  conn->conn_read  = zctx->next_conn.conn_read;
  conn->conn_write = zctx->next_conn.conn_write;
  conn->conn_poll  = zctx->next_conn.conn_poll;

  inflateEnd (&zctx->read.z);
  deflateEnd (&zctx->write.z);
  FREE (&zctx->read.buf);
  FREE (&zctx->write.buf);
  FREE (&zctx);

  return rc;
}

static int mutt_zstrm_read (CONNECTION* conn, char* buf, size_t len)
{
  zstrmctx* zctx = conn->sockdata;
  int rc = 0;
  int zrc;

retry:
  if (zctx->read.stream_eof)
    return 0;

  /* when avail_out was 0 on last call, we need to call inflate again,
   * because more data might be available using the current input, so
   * avoid callling read on the underlying stream in that case (for it
   * might block) */
  if (zctx->read.pos == 0 && !zctx->read.conn_eof)
  {
    rc = zctx->next_conn.conn_read (&zctx->next_conn,
	zctx->read.buf, zctx->read.len);
    dprint (4, (debugfile, "zstrm_read: consuming data from next "
	"stream: %d bytes\n", rc));
    if (rc < 0)
      return rc;
    else if (rc == 0)
      zctx->read.conn_eof = 1;
    else
      zctx->read.pos += rc;
  }

  zctx->read.z.avail_in = (uInt) zctx->read.pos;
  zctx->read.z.next_in = (Bytef*) zctx->read.buf;
  zctx->read.z.avail_out = (uInt) len;
  zctx->read.z.next_out = (Bytef*) buf;

  zrc = inflate (&zctx->read.z, Z_SYNC_FLUSH);
  dprint (4, (debugfile, "zstrm_read: rc=%d, "
	"consumed %u/%u bytes, produced %u/%u bytes\n", zrc,
	zctx->read.pos - zctx->read.z.avail_in, zctx->read.pos,
	len - zctx->read.z.avail_out, len));

  /* shift any remaining input data to the front of the buffer */
  if ((Bytef*) zctx->read.buf != zctx->read.z.next_in)
  {
    memmove(zctx->read.buf, zctx->read.z.next_in, zctx->read.z.avail_in);
    zctx->read.pos = zctx->read.z.avail_in;
  }

  switch (zrc)
  {
    case Z_OK:          /* progress has been made */
      zrc = len - zctx->read.z.avail_out;  /* "returned" bytes */
      if (zrc == 0)
      {
	/* there was progress, so must have been reading input */
	dprint (4, (debugfile, "zstrm_read: inflate just consumed\n"));
        goto retry;
      }
      break;
    case Z_STREAM_END:  /* everything flushed, nothing remaining */
      dprint (4, (debugfile, "zstrm_read: inflate returned Z_STREAM_END.\n"));
      zrc = len - zctx->read.z.avail_out;  /* "returned" bytes */
      zctx->read.stream_eof = 1;
      break;
    case Z_BUF_ERROR:  /* no progress was possible */
      if (!zctx->read.conn_eof)
      {
	dprint (5, (debugfile, "zstrm_read: inflate returned Z_BUF_ERROR. retrying.\n"));
        goto retry;
      }
      zrc = 0;
      break;
    default:
      /* bail on other rcs, such as Z_DATA_ERROR, or Z_MEM_ERROR */
      dprint (4, (debugfile, "zstrm_read: inflate returned %d. aborting.\n", zrc));
      zrc = -1;
      break;
  }

  return zrc;
}

static int mutt_zstrm_poll (CONNECTION* conn, time_t wait_secs)
{
  zstrmctx* zctx = conn->sockdata;

  dprint (4, (debugfile, "zstrm_poll: %s\n",
	zctx->read.z.avail_out == 0 || zctx->read.pos > 0 ?
	"last read wrote full buffer" : "falling back on next stream"));
  if (zctx->read.z.avail_out == 0 || zctx->read.pos > 0)
    return 1;
  else
    return zctx->next_conn.conn_poll (&zctx->next_conn, wait_secs);
}

static int mutt_zstrm_write (CONNECTION* conn, const char* buf, size_t count)
{
  zstrmctx* zctx = conn->sockdata;
  int rc;
  int zrc;
  char *wbufp;

  zctx->write.z.avail_in = (uInt) count;
  zctx->write.z.next_in = (Bytef*) buf;
  zctx->write.z.avail_out = (uInt) zctx->write.len;
  zctx->write.z.next_out = (Bytef*) zctx->write.buf;

  do
  {
    zrc = deflate (&zctx->write.z, Z_PARTIAL_FLUSH);
    if (zrc == Z_OK)
    {
      /* push out produced data to the underlying stream */
      zctx->write.pos = zctx->write.len - zctx->write.z.avail_out;
      wbufp = zctx->write.buf;
      dprint (4, (debugfile, "zstrm_write: deflate consumed %d/%d bytes\n",
	  count - zctx->write.z.avail_in, count));
      while (zctx->write.pos > 0)
      {
	rc = zctx->next_conn.conn_write (&zctx->next_conn,
	    wbufp, zctx->write.pos);
	dprint (4, (debugfile, "zstrm_write: next stream wrote: %d bytes\n",
	    rc));
	if (rc < 0)
	  return -1;  /* we can't recover from write failure */

	wbufp += rc;
	zctx->write.pos -= rc;
      }

      /* see if there's more for us to do, retry if the output buffer
       * was full (there may be something in zlib buffers), and retry
       * when there is still available input data */
      if (zctx->write.z.avail_out != 0 && zctx->write.z.avail_in == 0)
	break;

      zctx->write.z.avail_out = (uInt) zctx->write.len;
      zctx->write.z.next_out = (Bytef*) zctx->write.buf;
    }
    else
    {
      /* compression went wrong, but this is basically impossible
       * according to the docs */
      return -1;
    }
  } while (1);

  rc = (int) count;
  return rc <= 0 ? 1 : rc;  /* avoid wrong behaviour due to overflow */
}

void mutt_zstrm_wrap_conn (CONNEC