source: libwandio/ior-bzip.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@…>, 6 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: 3.7 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 <bzlib.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 bzip reader */
46
47enum err_t {
48        ERR_OK  = 1,
49        ERR_EOF = 0,
50        ERR_ERROR = -1
51};
52
53struct bz_t {
54        bz_stream strm;
55        char inbuff[1024*1024];
56        int outoffset;
57        io_t *parent;
58        enum err_t err;
59};
60
61
62extern io_source_t bz_source; 
63
64#define DATA(io) ((struct bz_t *)((io)->data))
65#define min(a,b) ((a)<(b) ? (a) : (b))
66
67io_t *bz_open(io_t *parent)
68{
69        io_t *io;
70        if (!parent)
71                return NULL;
72        io = malloc(sizeof(io_t));
73        io->source = &bz_source;
74        io->data = malloc(sizeof(struct bz_t));
75
76        DATA(io)->parent = parent;
77
78        DATA(io)->strm.next_in = NULL;
79        DATA(io)->strm.avail_in = 0;
80        DATA(io)->strm.next_out = NULL;
81        DATA(io)->strm.avail_out = 0;
82        DATA(io)->strm.bzalloc = NULL;
83        DATA(io)->strm.bzfree = NULL;
84        DATA(io)->strm.opaque = NULL;
85        DATA(io)->err = ERR_OK;
86
87        BZ2_bzDecompressInit(&DATA(io)->strm, 
88                0,      /* Verbosity */
89                0);     /* small */
90
91        return io;
92}
93
94
95static off_t bz_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 = 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                                DATA(io)->inbuff,
111                                sizeof(DATA(io)->inbuff));
112                        if (bytes_read == 0) /* EOF */
113                                return len-DATA(io)->strm.avail_out;
114                        if (bytes_read < 0) { /* Error */
115                                /* Errno should already be set */
116                                DATA(io)->err = ERR_ERROR;
117                                /* Return how much data we managed to read ok */
118                                if (DATA(io)->strm.avail_out != (uint32_t)len) {
119                                        return len-DATA(io)->strm.avail_out;
120                                }
121                                /* Now return error */
122                                return -1;
123                        }
124                        DATA(io)->strm.next_in = DATA(io)->inbuff;
125                        DATA(io)->strm.avail_in = bytes_read;
126                }
127                /* Decompress some data into the output buffer */
128                int err=BZ2_bzDecompress(&DATA(io)->strm);
129                switch(err) {
130                        case BZ_OK:
131                                DATA(io)->err = ERR_OK;
132                                break;
133                        case BZ_STREAM_END:
134                                DATA(io)->err = ERR_EOF;
135                                break;
136                        default:
137                                errno=EIO;
138                                DATA(io)->err = ERR_ERROR;
139                }
140        }
141        /* Return the number of bytes decompressed */
142        return len-DATA(io)->strm.avail_out;
143}
144
145static void bz_close(io_t *io)
146{
147        BZ2_bzDecompressEnd(&DATA(io)->strm);
148        wandio_destroy(DATA(io)->parent);
149        free(io->data);
150        free(io);
151}
152
153io_source_t bz_source = {
154        "bzip",
155        bz_read,
156        NULL,   /* peek */
157        NULL,   /* tell */
158        NULL,   /* seek */
159        bz_close
160};
161
Note: See TracBrowser for help on using the repository browser.