source: libwandio/ior-zlib.c @ e4eff86

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since e4eff86 was e4eff86, checked in by Shane Alcock <salcock@…>, 5 years ago

Ensure all libwandio code includes config.h

This fixes a nasty bug on 32 bit machines where the size of off_t will
change between functions, causing all sorts of havoc. The reason the size
changes is because FILE_OFFSET_BITS is defined to 64 inside config.h so
any source files that include config.h will have 64 bit off_t's whereas
any files that don't include it will end up with a 32 bit off_t (on a 32 bit
machine).

  • Property mode set to 100644
File size: 5.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 "config.h"
36#include "wandio.h"
37#include <zlib.h>
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <fcntl.h>
41#include <stdlib.h>
42#include <string.h>
43#include <errno.h>
44
45/* Libtrace IO module implementing a zlib reader */
46
47enum err_t {
48        ERR_OK  = 1,
49        ERR_EOF = 0,
50        ERR_ERROR = -1
51};
52
53struct zlib_t {
54        Bytef inbuff[1024*1024]; /* bytef is what zlib uses for buffer pointers */
55        z_stream strm;
56        io_t *parent;
57        int outoffset;
58        enum err_t err;
59        size_t sincelastend;
60};
61
62
63extern io_source_t zlib_source; 
64
65#define DATA(io) ((struct zlib_t *)((io)->data))
66#define min(a,b) ((a)<(b) ? (a) : (b))
67
68io_t *zlib_open(io_t *parent)
69{
70        io_t *io;
71        if (!parent)
72                return NULL;
73        io = malloc(sizeof(io_t));
74        io->source = &zlib_source;
75        io->data = malloc(sizeof(struct zlib_t));
76
77        DATA(io)->parent = parent;
78
79        DATA(io)->strm.next_in = NULL;
80        DATA(io)->strm.avail_in = 0;
81        DATA(io)->strm.next_out = NULL;
82        DATA(io)->strm.avail_out = 0;
83        DATA(io)->strm.zalloc = Z_NULL;
84        DATA(io)->strm.zfree = Z_NULL;
85        DATA(io)->strm.opaque = NULL;
86        DATA(io)->err = ERR_OK;
87        DATA(io)->sincelastend = 1;
88
89        inflateInit2(&DATA(io)->strm, 15 | 32);
90
91        return io;
92}
93
94
95static off_t zlib_read(io_t *io, void *buffer, off_t len)
96{
97        if (DATA(io)->err == ERR_EOF)
98                return 0; /* EOF */
99        if (DATA(io)->err == ERR_ERROR) {
100                errno=EIO;
101                return -1; /* ERROR! */
102        }
103
104        DATA(io)->strm.avail_out = len;
105        DATA(io)->strm.next_out = (Bytef*)buffer;
106
107        while (DATA(io)->err == ERR_OK && DATA(io)->strm.avail_out > 0) {
108                while (DATA(io)->strm.avail_in <= 0) {
109                        int bytes_read = wandio_read(DATA(io)->parent, 
110                                (char*)DATA(io)->inbuff,
111                                sizeof(DATA(io)->inbuff));
112                        if (bytes_read == 0) {
113                                /* If we get EOF immediately after a
114                                 * Z_STREAM_END, then we assume we've reached
115                                 * the end of the file. If there was data
116                                 * between the Z_STREAM_END and the EOF, the
117                                 * file has more likely been truncated.
118                                 */
119                                if (DATA(io)->sincelastend > 0) {
120                                        fprintf(stderr, "Unexpected EOF while reading compressed file -- file is probably incomplete\n");
121                                        errno = EIO;
122                                        DATA(io)->err = ERR_ERROR;
123                                        return -1;
124                                }
125
126                                /* EOF */
127                                if (DATA(io)->strm.avail_out == (uint32_t)len) {
128                                        DATA(io)->err = ERR_EOF;
129                                        return 0;
130                                }
131                                /* Return how much data we've managed to read so far. */
132                                return len-DATA(io)->strm.avail_out;
133                        }
134                        if (bytes_read < 0) { /* Error */
135                                /* errno should be set */
136                                DATA(io)->err = ERR_ERROR;
137                                /* Return how much data we managed to read ok */
138                                if (DATA(io)->strm.avail_out != (uint32_t)len) {
139                                        return len-DATA(io)->strm.avail_out;
140                                }
141                                /* Now return error */
142                                return -1;
143                        }
144                        DATA(io)->strm.next_in = DATA(io)->inbuff;
145                        DATA(io)->strm.avail_in = bytes_read;
146                        DATA(io)->sincelastend += bytes_read;
147                }
148                /* Decompress some data into the output buffer */
149                int err=inflate(&DATA(io)->strm, 0);
150                switch(err) {
151                        case Z_OK:
152                                DATA(io)->err = ERR_OK;
153                                break;
154                        case Z_STREAM_END:
155                                /* You would think that an "EOF" on the stream would mean we'd
156                                 * want to pass on an EOF?  Nope.  Some tools (*cough* corel *cough*)
157                                 * annoyingly close and reopen the gzip stream leaving Z_STREAM_END
158                                 * mines for us to find.
159                                 */
160                                inflateEnd(&DATA(io)->strm);
161                                inflateInit2(&DATA(io)->strm, 15 | 32);
162                                DATA(io)->err = ERR_OK;
163                                DATA(io)->sincelastend = 0;
164                                break;
165                        default:
166                                errno=EIO;
167                                DATA(io)->err = ERR_ERROR;
168                }
169        }
170        /* Return the number of bytes decompressed */
171        return len-DATA(io)->strm.avail_out;
172}
173
174static void zlib_close(io_t *io)
175{
176        inflateEnd(&DATA(io)->strm);
177        wandio_destroy(DATA(io)->parent);
178        free(io->data);
179        free(io);
180}
181
182io_source_t zlib_source = {
183        "zlib",
184        zlib_read,
185        NULL,   /* peek */
186        NULL,   /* tell */
187        NULL,   /* seek */
188        zlib_close
189};
190
Note: See TracBrowser for help on using the repository browser.