source: libwandio/ior-zlib.c @ 1d4db58

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 1d4db58 was 60f3c4c, checked in by Shane Alcock <salcock@…>, 9 years ago
  • Shifted wandio into a separate library, seeing as we're going to be exporting the API now
  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34
35#include "wandio.h"
36#include <zlib.h>
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <fcntl.h>
40#include <stdlib.h>
41#include <string.h>
42#include <errno.h>
43
44/* Libtrace IO module implementing a zlib reader */
45
46enum err_t {
47        ERR_OK  = 1,
48        ERR_EOF = 0,
49        ERR_ERROR = -1
50};
51
52struct zlib_t {
53        Bytef inbuff[1024*1024]; /* bytef is what zlib uses for buffer pointers */
54        z_stream strm;
55        io_t *parent;
56        int outoffset;
57        enum err_t err;
58};
59
60
61extern io_source_t zlib_source; 
62
63#define DATA(io) ((struct zlib_t *)((io)->data))
64#define min(a,b) ((a)<(b) ? (a) : (b))
65
66io_t *zlib_open(io_t *parent)
67{
68        io_t *io;
69        if (!parent)
70                return NULL;
71        io = malloc(sizeof(io_t));
72        io->source = &zlib_source;
73        io->data = malloc(sizeof(struct zlib_t));
74
75        DATA(io)->parent = parent;
76
77        DATA(io)->strm.next_in = NULL;
78        DATA(io)->strm.avail_in = 0;
79        DATA(io)->strm.next_out = NULL;
80        DATA(io)->strm.avail_out = 0;
81        DATA(io)->strm.zalloc = Z_NULL;
82        DATA(io)->strm.zfree = Z_NULL;
83        DATA(io)->strm.opaque = NULL;
84        DATA(io)->err = ERR_OK;
85
86        inflateInit2(&DATA(io)->strm, 15 | 32);
87
88        return io;
89}
90
91
92static off_t zlib_read(io_t *io, void *buffer, off_t len)
93{
94        if (DATA(io)->err == ERR_EOF)
95                return 0; /* EOF */
96        if (DATA(io)->err == ERR_ERROR) {
97                errno=EIO;
98                return -1; /* ERROR! */
99        }
100
101        DATA(io)->strm.avail_out = len;
102        DATA(io)->strm.next_out = (Bytef*)buffer;
103
104        while (DATA(io)->err == ERR_OK && DATA(io)->strm.avail_out > 0) {
105                while (DATA(io)->strm.avail_in <= 0) {
106                        int bytes_read = wandio_read(DATA(io)->parent, 
107                                (char*)DATA(io)->inbuff,
108                                sizeof(DATA(io)->inbuff));
109                        if (bytes_read == 0) {
110                                /* EOF */
111                                if (DATA(io)->strm.avail_out == (uint32_t)len) {
112                                        DATA(io)->err = ERR_EOF;
113                                        return 0;
114                                }
115                                /* Return how much data we've managed to read so far. */
116                                return len-DATA(io)->strm.avail_out;
117                        }
118                        if (bytes_read < 0) { /* Error */
119                                /* errno should be set */
120                                DATA(io)->err = ERR_ERROR;
121                                /* Return how much data we managed to read ok */
122                                if (DATA(io)->strm.avail_out != (uint32_t)len) {
123                                        return len-DATA(io)->strm.avail_out;
124                                }
125                                /* Now return error */
126                                return -1;
127                        }
128                        DATA(io)->strm.next_in = DATA(io)->inbuff;
129                        DATA(io)->strm.avail_in = bytes_read;
130                }
131                /* Decompress some data into the output buffer */
132                int err=inflate(&DATA(io)->strm, 0);
133                switch(err) {
134                        case Z_OK:
135                                DATA(io)->err = ERR_OK;
136                                break;
137                        case Z_STREAM_END:
138                                /* You would think that an "EOF" on the stream would mean we'd
139                                 * want to pass on an EOF?  Nope.  Some tools (*cough* corel *cough*)
140                                 * annoyingly close and reopen the gzip stream leaving Z_STREAM_END
141                                 * mines for us to find.
142                                 */
143                                inflateEnd(&DATA(io)->strm);
144                                inflateInit2(&DATA(io)->strm, 15 | 32);
145                                DATA(io)->err = ERR_OK;
146                                break;
147                        default:
148                                errno=EIO;
149                                DATA(io)->err = ERR_ERROR;
150                }
151        }
152        /* Return the number of bytes decompressed */
153        return len-DATA(io)->strm.avail_out;
154}
155
156static void zlib_close(io_t *io)
157{
158        inflateEnd(&DATA(io)->strm);
159        wandio_destroy(DATA(io)->parent);
160        free(io->data);
161        free(io);
162}
163
164io_source_t zlib_source = {
165        "zlib",
166        zlib_read,
167        NULL,   /* peek */
168        NULL,   /* tell */
169        NULL,   /* seek */
170        zlib_close
171};
172
Note: See TracBrowser for help on using the repository browser.