decompress 18.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright 1998 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * HTTP (c.decompress)
 *
 *  Acorn Computers Ltd. 1998
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Stewart Brodie's avatar
Stewart Brodie committed
24
#include <stddef.h>
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
#include "kernel.h"
#include "swis.h"
#include "sys/errno.h"
#include "module.h"
#include "socklib.h"
#include "cookie.h"
#include "protocol.h"
#include "writedata.h"
#include "header.h"
#include "generic.h"

#ifdef COMPRESSION

int decompress(Session *ses, char *buffer, int bufsize, int toread, int *rawread)
{
        int dataread;
        int orig_toread = toread;

        ses->zstream.next_out = (Bytef *) buffer;
        ses->zstream.avail_out = bufsize;

        for (;;) {

            switch (ses->compstate) {
                case compress_INFLATE:
                    #ifdef TRACE
                    protocol_debug("decompress: INFLATE\n");
                    #endif

                    if (ses->zstream.avail_in) {
                            /* Something left over from last time */
Stewart Brodie's avatar
Stewart Brodie committed
56 57 58 59 60 61 62
                            int ret;

                            #ifdef TRACE
                            protocol_debug("inflate: Next byte in stream is %02x\n", *ses->zstream.next_in);
                            #endif

                            ret = inflate(&ses->zstream, Z_NO_FLUSH);
63 64
                            if (ret == Z_STREAM_END) {
                                    inflateEnd(&ses->zstream);
Stewart Brodie's avatar
Stewart Brodie committed
65 66 67
                                    /*ses->compression = compression_NONE;*/
                                    ses->compstate = compress_READ_GZIP_TRAILER;
	                            ses->zstream.next_in = ses->compbuf;
68 69 70 71 72 73 74 75 76 77 78
                            }
                            else if (ret != Z_OK) {
                                    #ifdef TRACE
                                    protocol_debug("inflate error: %s\n", ses->zstream.msg);
                                    #endif
                                    *rawread = orig_toread - toread;
                                    return 0;
                            }
                    }
                    while (ses->zstream.avail_in == 0 && ses->zstream.avail_out > 0 && toread > 0 && ses->compstate == compress_INFLATE) {
                            int ret;
Stewart Brodie's avatar
Stewart Brodie committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
                            int s;

                            s = toread > ses->compbufsize ? ses->compbufsize : toread;
                            if (s == 0) {
                                    #ifdef TRACE
                                    protocol_debug("No buffer space left!\n");
                                    #endif
                                    errno = EWOULDBLOCK;
                                    dataread = -1;
				    if (toread < orig_toread) {
				            *rawread = orig_toread - toread;
				            return (char *) ses->zstream.next_out - buffer;
				    }
                            }
                            else {
	                            dataread = ses->op->s_recv(ses->sd, ses->compbuf, s, 0);
                            }
96 97 98 99 100 101 102
                            if (dataread > 0) {
                                    toread -= dataread;
                                    ses->zstream.next_in = ses->compbuf;
                                    ses->zstream.avail_in = dataread;
                                    ret = inflate(&ses->zstream, Z_NO_FLUSH);
                                    if (ret == Z_STREAM_END) {
                                            inflateEnd(&ses->zstream);
Stewart Brodie's avatar
Stewart Brodie committed
103 104 105
                                            /*ses->compression = compression_NONE;*/
                                            ses->compstate = compress_READ_GZIP_TRAILER;
		                            ses->zstream.next_in = ses->compbuf;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
                                    }
                                    else if (ret != Z_OK) {
                                            #ifdef TRACE
                                            protocol_debug("inflate error: %s\n", ses->zstream.msg);
                                            #endif
                                            *rawread = orig_toread - toread;
                                            return 0;
                                    }
                            }
                            else if (dataread == 0) {
                                    inflateEnd(&ses->zstream);
                                    ses->compression = compression_NONE;
                                    ses->compstate = compress_FINISHED;
                                    *rawread = orig_toread - toread;
                                    return dataread;
                            }
Stewart Brodie's avatar
Stewart Brodie committed
122 123 124 125 126 127
                            else if (errno == EWOULDBLOCK) {
                                    const ptrdiff_t outsize = (char *) ses->zstream.next_out - buffer;
                                    *rawread = orig_toread - toread;
		                    if (outsize > 0) return outsize;
		                    return -1;
                            }
128 129 130 131 132 133
                            else {
                                    *rawread = orig_toread - toread;
                                    return dataread;
                            }
                    }
                    *rawread = orig_toread - toread;
Stewart Brodie's avatar
Stewart Brodie committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                    #ifdef TRACE
                    protocol_debug("decompress: %d bytes read; return value %d\n", *rawread, (char *) ses->zstream.next_out - buffer);
                    #endif
                    if ((char *)ses->zstream.next_out == buffer && *rawread) {
                            if (ses->compstate == compress_INFLATE) {
	        	            #ifdef TRACE
		                    protocol_debug("decompress: still inflating, therefore returning EWOULDBLOCK\n");
                		    #endif
                        	    errno = EWOULDBLOCK;
	                            return -1;
                            }
                            if (ses->compstate == compress_READ_GZIP_TRAILER) {
                                    #ifdef TRACE
                                    protocol_debug("decompress: still waiting for 8 bytes trailer = therefore returning EWOULDBLOCK\n");
                                    #endif
                                    errno = EWOULDBLOCK;
                                    return -1;
                            }
	        	    #ifdef TRACE
	                    protocol_debug("decompress: inflation has completed - therefore returning zero\n");
               		    #endif
                   }
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
                    return (char *) ses->zstream.next_out - buffer;

                case compress_READ_GZIP_HEADER:
                    #ifdef TRACE
                    protocol_debug("decompress: READ_GZIP_HEADER\n");
                    #endif

                    if (ses->zstream.next_in == Z_NULL) {
                            ses->zstream.next_in = ses->compbuf;
                            ses->zstream.avail_in = 0;
                    }

                    dataread = ses->op->s_recv(ses->sd, ses->zstream.next_in, toread > 10 - ses->zstream.avail_in ? 10 - ses->zstream.avail_in : toread, 0);
                    if (dataread <= 0) {
                            *rawread = orig_toread - toread;
                            return dataread;
                    }

                    toread -= dataread;
                    ses->zstream.avail_in += dataread;
Stewart Brodie's avatar
Stewart Brodie committed
176
                    ses->zstream.next_in += dataread;
177 178 179 180 181 182 183
                    if (ses->zstream.avail_in < 10) {
                            *rawread = orig_toread - toread;
                            errno = EWOULDBLOCK;
                            return -1;
                    }

                    if (ses->compbuf[0] != 0x1F || ses->compbuf[1] != 0x8B ||
Stewart Brodie's avatar
Stewart Brodie committed
184
                        ses->compbuf[2] != 8 || (ses->compbuf[3] & 0xE0) != 0) {
185
                            *rawread = orig_toread - toread;
Stewart Brodie's avatar
Stewart Brodie committed
186 187 188
	                    #ifdef TRACE
        	            protocol_debug("decompress: compbuf wrong (%02x,%02x,%02x,%02x)\n", ses->compbuf[0], ses->compbuf[1], ses->compbuf[2],ses->compbuf[3]);
                	    #endif
189 190 191 192 193 194 195
                            return 0;
                    }

                    ses->compflags = ses->compbuf[3];
                    ses->compstate = compress_READ_GZIP_EXTRA;
                    ses->zstream.next_in = ses->compbuf;
                    ses->zstream.avail_in = 0;
Stewart Brodie's avatar
Stewart Brodie committed
196 197 198 199 200
                    if (toread == 0) {
                            *rawread = orig_toread - toread;
                            errno = EWOULDBLOCK;
                            return -1;
                    }
201 202 203 204 205 206 207 208 209
                    /* Fall through */

                case compress_READ_GZIP_EXTRA:
                    #ifdef TRACE
                    protocol_debug("decompress: READ_GZIP_EXTRA\n");
                    #endif

                    if (ses->compflags & 0x04) {
                            if (ses->zstream.avail_in < 2) {
Stewart Brodie's avatar
Stewart Brodie committed
210 211 212 213 214 215 216
		                    if (toread == 0) {
                		            dataread = -1;
		                            errno = EWOULDBLOCK;
		                    }
                                    else {
                                            dataread = ses->op->s_recv(ses->sd, ses->zstream.next_in, toread > 2 - ses->zstream.avail_in ? 2 - ses->zstream.avail_in : toread, 0);
                                    }
217 218 219 220 221 222
                                    if (dataread <= 0) {
                                            *rawread = orig_toread - toread;
                                            return dataread;
                                    }
                                    toread -= dataread;
                                    ses->zstream.avail_in += dataread;
Stewart Brodie's avatar
Stewart Brodie committed
223
                                    ses->zstream.next_in += dataread;
224 225 226 227 228
                                    if (ses->zstream.avail_in < 2) {
                                            *rawread = orig_toread - toread;
                                            errno = EWOULDBLOCK;
                                            return -1;
                                    }
Stewart Brodie's avatar
Stewart Brodie committed
229 230 231 232
                                    ses->zstream.avail_in = ses->compbuf[0] + 256 * ses->compbuf[1];
                                    #ifdef TRACE
                                    protocol_debug("%d bytes of gzip extra data to discard\n", ses->zstream.avail_in);
                                    #endif
233
                            }
Stewart Brodie's avatar
Stewart Brodie committed
234 235 236 237 238 239 240 241 242 243
                    }
                    ses->compstate = compress_READING_GZIP_EXTRA;
                    /* Fall through */

                case compress_READING_GZIP_EXTRA:
                    #ifdef TRACE
                    protocol_debug("decompress: READING_GZIP_EXTRA\n");
                    #endif
		    if (ses->compflags & 0x04) {
                            int s;
244
                          suck_more:
Stewart Brodie's avatar
Stewart Brodie committed
245
                            s = ses->compbufsize > ses->zstream.avail_in ? ses->zstream.avail_in : ses->compbufsize;
246
                            s = toread > s ? s : toread;
Stewart Brodie's avatar
Stewart Brodie committed
247 248 249 250 251 252 253
			    if (s == 0) {
			            errno = EWOULDBLOCK;
			            dataread = -1;
			    }
                            else {
                                    dataread = ses->op->s_recv(ses->sd, ses->compbuf, s, 0);
                            }
254 255 256 257 258
                            if (dataread <= 0) {
                                    *rawread = orig_toread - toread;
                                    return dataread;
                            }

Stewart Brodie's avatar
Stewart Brodie committed
259 260 261
                            toread -= dataread;
                            ses->zstream.avail_in -= dataread;
                            if (ses->zstream.avail_in > 0)
262
                                    goto suck_more;
Stewart Brodie's avatar
Stewart Brodie committed
263
		    }
264 265 266 267 268 269 270 271 272 273 274 275

                    ses->compstate = compress_READ_GZIP_NAME;
                    ses->zstream.next_in = ses->compbuf;
                    ses->zstream.avail_in = 0;
                    /* Fall through */

                case compress_READ_GZIP_NAME:
                    #ifdef TRACE
                    protocol_debug("decompress: READ_GZIP_NAME\n");
                    #endif

                    if (ses->compflags & 0x08) {
Stewart Brodie's avatar
Stewart Brodie committed
276 277
                            int s;

278
                            suck_more_fname:
Stewart Brodie's avatar
Stewart Brodie committed
279 280 281 282 283 284 285 286
                            s = toread > ses->compbufsize ? ses->compbufsize : toread;
			    if (s == 0) {
			            errno = EWOULDBLOCK;
			            dataread = -1;
			    }
			    else {
	                            dataread = ses->op->s_recv(ses->sd, ses->compbuf, s, 0);
			    }
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
                            if (dataread <= 0) {
                                    *rawread = orig_toread - toread;
                                    return dataread;
                            }
                            toread -= dataread;
                            for (ses->zstream.next_in=ses->compbuf,
                                 ses->zstream.avail_in=dataread;
                                 ses->zstream.avail_in;
                                 ses->zstream.next_in++,
                                 ses->zstream.avail_in--) {
                                    if (*ses->zstream.next_in == 0) goto finished_fname;
                            }

                            goto suck_more_fname;

Stewart Brodie's avatar
Stewart Brodie committed
302 303 304 305
	                    finished_fname:
        	            /* Don't forget to skip over the '\0' */
                	    ses->zstream.next_in++;
	                    ses->zstream.avail_in--;
306 307 308 309 310 311 312 313 314 315
                    }
                    ses->compstate = compress_READ_GZIP_COMMENT;
                    /* Fall through */

                case compress_READ_GZIP_COMMENT:
                    #ifdef TRACE
                    protocol_debug("decompress: READ_GZIP_COMMENT\n");
                    #endif

                    if (ses->compflags & 0x10) {
Stewart Brodie's avatar
Stewart Brodie committed
316
                            int s;
317 318 319 320 321 322
                          suck_more_comment:
                            for ( ; ses->zstream.avail_in;
                                ses->zstream.next_in++,
                                 ses->zstream.avail_in--) {
                                    if (*ses->zstream.next_in == 0) goto finished_comment;
                            }
Stewart Brodie's avatar
Stewart Brodie committed
323 324 325 326 327 328 329 330
                            s = toread > ses->compbufsize - 2 ? ses->compbufsize - 2 : toread;
			    if (s == 0) {
			            errno = EWOULDBLOCK;
			            dataread = -1;
			    }
			    else {
                            	dataread = ses->op->s_recv(ses->sd, ses->compbuf, s, 0); /* Leave room for CRC... */
			    }
331 332 333 334 335 336 337 338 339
                            if (dataread <= 0) {
                                    *rawread = orig_toread - toread;
                                    return dataread;
                            }
                            toread -= dataread;
                            ses->zstream.next_in = ses->compbuf;
                            ses->zstream.avail_in = dataread;
                            goto suck_more_comment;

Stewart Brodie's avatar
Stewart Brodie committed
340 341 342 343
	                  finished_comment:
        	            /* Don't forget to skip over the '\0' */
                	    ses->zstream.next_in++;
	                    ses->zstream.avail_in--;
344 345 346 347 348 349 350 351 352 353 354
                    }
                    ses->compstate = compress_READ_GZIP_HCRC;
                    /* Fall through */

                case compress_READ_GZIP_HCRC:
                    #ifdef TRACE
                    protocol_debug("decompress: READ_GZIP_HCRC\n");
                    #endif

                    if (ses->compflags & 0x02) {
                            if (ses->zstream.avail_in < 2) {
Stewart Brodie's avatar
Stewart Brodie committed
355 356 357 358 359 360 361 362 363
                                    int s;
                                    s = toread > 2 - ses->zstream.avail_in ? 2 - ses->zstream.avail_in : toread;
                                    if (s == 0) {
                                            errno = EWOULDBLOCK;
                                            dataread = -1;
                                    }
                                    else {
	                                    dataread = ses->op->s_recv(ses->sd, ses->zstream.next_in, s, 0);
                                    }
364 365 366 367 368 369 370 371 372 373 374 375 376 377
                                    if (dataread <= 0) {
                                            *rawread = orig_toread - toread;
                                            return dataread;
                                    }

                                    toread -= dataread;
                                    ses->zstream.next_in += dataread;
                                    ses->zstream.avail_in += dataread;
                                    if (ses->zstream.avail_in < 2) {
                                            *rawread = orig_toread - toread;
                                            errno = EWOULDBLOCK;
                                            return -1;
                                    }
                            }
Stewart Brodie's avatar
Stewart Brodie committed
378 379 380
                            else {
                                    ses->zstream.next_in += 2;
                            }
381 382 383 384 385
                            ses->zstream.avail_in -= 2;
                    }
                    ses->compstate = compress_INFLATE;
                    break;

Stewart Brodie's avatar
Stewart Brodie committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
                case compress_READ_GZIP_TRAILER:
                    #ifdef TRACE
                    protocol_debug("decompress: READ_GZIP_TRAILER (avail_in = %d, orig_toread = %d)\n", ses->zstream.avail_in, orig_toread);
                    #endif

		    if (ses->compression != compression_GZIP) {
		            *rawread = orig_toread - toread;
			    ses->compstate = compress_FINISHED;
		            return 0;
		    }
                    else if (ses->zstream.avail_in < 8) {
		    	int s;
                        s = toread > 8 - ses->zstream.avail_in ? 8 - ses->zstream.avail_in : toread;
			if (s == 0) {
			        errno = EWOULDBLOCK;
			        dataread = -1;
			}
			else {
	                        dataread = ses->op->s_recv(ses->sd, ses->compbuf, s, 0);
			}
       	            	if (dataread <= 0) {
                       	    	*rawread = orig_toread - toread;
                            	return dataread;
                        }

                    	toread -= dataread;
                    	ses->zstream.avail_in += dataread;
                        if (ses->zstream.avail_in < 8) {
                                *rawread = orig_toread - toread;
                                errno = EWOULDBLOCK;
                                return -1;
                        }
                    }

		    ses->zstream.avail_in -= 8;
                    ses->compstate = compress_FINISHED;
422
                    /* Fall through */
Stewart Brodie's avatar
Stewart Brodie committed
423

424 425
                case compress_FINISHED:
                    dataread=0;
Stewart Brodie's avatar
Stewart Brodie committed
426 427 428
                    ses->compression = compression_NONE;
		    *rawread = orig_toread - toread;
		    return 0;
429 430 431 432 433 434 435 436
            }
        }

        *rawread = orig_toread - toread;
        return dataread;
}

#endif