paulo@0: /* paulo@0: * $Id: rx_inflate.c,v 1.10 2004/04/05 07:56:54 hipnod Exp $ paulo@0: * paulo@0: * Copyright (C) 2003 giFT project (gift.sourceforge.net) paulo@0: * paulo@0: * This program is free software; you can redistribute it and/or modify it paulo@0: * under the terms of the GNU General Public License as published by the paulo@0: * Free Software Foundation; either version 2, or (at your option) any paulo@0: * later version. paulo@0: * paulo@0: * This program is distributed in the hope that it will be useful, but paulo@0: * WITHOUT ANY WARRANTY; without even the implied warranty of paulo@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU paulo@0: * General Public License for more details. paulo@0: */ paulo@0: paulo@0: #include "gt_gnutella.h" paulo@0: paulo@0: #include "rx_stack.h" paulo@0: #include "rx_layer.h" paulo@0: paulo@0: #include paulo@0: paulo@0: /*****************************************************************************/ paulo@0: paulo@0: /* this is small for testing purposes */ paulo@0: #define RX_INFLATE_BUFSIZE 256 paulo@0: paulo@0: #define RX_INFLATE(rx) \ paulo@0: ((struct rx_inflate *) (((rx)->udata))) paulo@0: paulo@0: struct rx_inflate paulo@0: { paulo@0: z_stream z; paulo@0: BOOL init_done; paulo@0: }; paulo@0: paulo@0: /*****************************************************************************/ paulo@0: paulo@0: static BOOL rx_inflate_init (struct rx_layer *rx, void *udata) paulo@0: { paulo@0: struct rx_inflate *rx_inflate; paulo@0: paulo@0: if (!(rx_inflate = NEW (struct rx_inflate))) paulo@0: return FALSE; paulo@0: paulo@0: /* inflateInit() may touch these variables */ paulo@0: rx_inflate->z.zalloc = Z_NULL; paulo@0: rx_inflate->z.zfree = Z_NULL; paulo@0: rx_inflate->z.opaque = Z_NULL; paulo@0: rx_inflate->z.next_in = Z_NULL; paulo@0: rx_inflate->z.avail_in = 0; paulo@0: paulo@0: if (inflateInit (&rx_inflate->z) != Z_OK) paulo@0: { paulo@0: gt_rx_stack_abort (rx->stack); paulo@0: return FALSE; paulo@0: } paulo@0: paulo@0: rx->udata = rx_inflate; paulo@0: paulo@0: rx_inflate->init_done = TRUE; paulo@0: paulo@0: return TRUE; paulo@0: } paulo@0: paulo@0: static void rx_inflate_destroy (struct rx_layer *rx) paulo@0: { paulo@0: struct rx_inflate *rx_inflate = RX_INFLATE(rx); paulo@0: paulo@0: /* paulo@0: * We don't check the error here, there could very well be leftover data paulo@0: * and it would be annoying to print a message every time. paulo@0: */ paulo@0: inflateEnd (&rx_inflate->z); paulo@0: rx_inflate->init_done = FALSE; paulo@0: paulo@0: FREE (rx_inflate); paulo@0: } paulo@0: paulo@0: static void rx_inflate_enable (struct rx_layer *rx) paulo@0: { paulo@0: /* nothing -- we only process data when it comes towards us */ paulo@0: } paulo@0: paulo@0: static void rx_inflate_disable (struct rx_layer *rx) paulo@0: { paulo@0: /* nothing */ paulo@0: } paulo@0: paulo@0: /* paulo@0: * Handle the data from the lower layer, decompress it, and pass paulo@0: * it to the upper layer. paulo@0: */ paulo@0: static struct io_buf *read_buf (struct rx_layer *rx, struct io_buf *io_buf) paulo@0: { paulo@0: struct rx_inflate *rx_inflate = RX_INFLATE(rx); paulo@0: struct io_buf *out_msg; paulo@0: z_streamp inz; paulo@0: int ret; paulo@0: size_t uncompressed_size; paulo@0: size_t compressed_read; paulo@0: size_t out_size = RX_INFLATE_BUFSIZE; paulo@0: size_t avail; paulo@0: static size_t running_cnt = 0; paulo@0: static int msg_count = 0; paulo@0: paulo@0: avail = io_buf_read_avail (io_buf); paulo@0: paulo@0: if (avail == 0) paulo@0: return NULL; paulo@0: paulo@0: if (!(out_msg = io_buf_new (out_size))) paulo@0: { paulo@0: GT->dbg (GT, "couldn't allocate memory for recv buf"); paulo@0: gt_rx_stack_abort (rx->stack); paulo@0: return NULL; paulo@0: } paulo@0: paulo@0: assert (rx_inflate->init_done); paulo@0: inz = &rx_inflate->z; paulo@0: paulo@0: inz->next_in = io_buf_read_ptr (io_buf); paulo@0: inz->avail_in = avail; paulo@0: inz->next_out = io_buf_write_ptr (out_msg); paulo@0: inz->avail_out = out_size; paulo@0: paulo@0: ret = inflate (inz, Z_SYNC_FLUSH); paulo@0: paulo@0: if (ret != Z_OK) paulo@0: { paulo@0: if (IO_DEBUG) paulo@0: GT->dbg (GT, "zlib recv error: %d", ret); paulo@0: paulo@0: gt_rx_stack_abort (rx->stack); paulo@0: io_buf_free (out_msg); paulo@0: return NULL; paulo@0: } paulo@0: paulo@0: uncompressed_size = out_size - inz->avail_out; paulo@0: compressed_read = avail - inz->avail_in; paulo@0: paulo@0: running_cnt += uncompressed_size; paulo@0: if (IO_DEBUG && ++msg_count % 50 == 0) paulo@0: { paulo@0: GT->dbg (GT, "uncompressed %u bytes", running_cnt); paulo@0: running_cnt = 0; paulo@0: } paulo@0: paulo@0: /* add the bytes we read to the new messge */ paulo@0: io_buf_push (out_msg, uncompressed_size); paulo@0: paulo@0: /* pop the old bytes we read off the incoming message */ paulo@0: io_buf_pop (io_buf, compressed_read); paulo@0: paulo@0: return out_msg; paulo@0: } paulo@0: paulo@0: /* paulo@0: * Parse the data into buffers, and send it to the upper layers. packets to paulo@0: */ paulo@0: static void rx_inflate_recv (struct rx_layer *rx, struct io_buf *io_buf) paulo@0: { paulo@0: struct io_buf *msg; paulo@0: paulo@0: while (rx->enabled && (msg = read_buf (rx, io_buf))) paulo@0: { paulo@0: assert (msg != NULL); paulo@0: gt_rx_layer_recv (rx, msg); paulo@0: paulo@0: /* paulo@0: * NOTE: gt_rx_layer_recv() may abort the stack here... there's not much paulo@0: * we can do about that, but in practice that doesn't happen and we paulo@0: * won't access freed memory because the stack doesn't get freed until paulo@0: * the lowest rx layer is notified of the abort...pretty hacky. paulo@0: */ paulo@0: } paulo@0: paulo@0: /* we have to free the buffer */ paulo@0: io_buf_free (io_buf); paulo@0: } paulo@0: paulo@0: /*****************************************************************************/ paulo@0: paulo@0: struct rx_layer_ops gt_rx_inflate_ops = paulo@0: { paulo@0: rx_inflate_init, paulo@0: rx_inflate_destroy, paulo@0: rx_inflate_enable, paulo@0: rx_inflate_disable, paulo@0: rx_inflate_recv, paulo@0: };