source: libwandio/iow-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@…>, 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: 4.1 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#include "config.h"
35#include "wandio.h"
36#include <bzlib.h>
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <fcntl.h>
40#include <stdlib.h>
41#include <string.h>
42
43/* Libtrace IO module implement a bzip writer */
44
45enum err_t {
46        ERR_OK  = 1,
47        ERR_EOF = 0,
48        ERR_ERROR = -1
49};
50
51struct bzw_t {
52        bz_stream strm;
53        char outbuff[1024*1024];
54        int inoffset;
55        iow_t *child;
56        enum err_t err;
57};
58
59
60extern iow_source_t bz_wsource; 
61
62#define DATA(iow) ((struct bzw_t *)((iow)->data))
63#define min(a,b) ((a)<(b) ? (a) : (b))
64
65iow_t *bz_wopen(iow_t *child, int compress_level)
66{
67        iow_t *iow;
68        if (!child)
69                return NULL;
70        iow = malloc(sizeof(iow_t));
71        iow->source = &bz_wsource;
72        iow->data = malloc(sizeof(struct bzw_t));
73
74        DATA(iow)->child = child;
75
76        DATA(iow)->strm.next_in = NULL;
77        DATA(iow)->strm.avail_in = 0;
78        DATA(iow)->strm.next_out = DATA(iow)->outbuff;
79        DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff);
80        DATA(iow)->strm.bzalloc = NULL;
81        DATA(iow)->strm.bzfree = NULL;
82        DATA(iow)->strm.opaque = NULL;
83        DATA(iow)->err = ERR_OK;
84
85        BZ2_bzCompressInit(&DATA(iow)->strm, 
86                        compress_level, /* Block size */
87                        0,              /* Verbosity */
88                        30);            /* Work factor */
89
90        return iow;
91}
92
93
94static off_t bz_wwrite(iow_t *iow, const char *buffer, off_t len)
95{
96        if (DATA(iow)->err == ERR_EOF) {
97                return 0; /* EOF */
98        }
99        if (DATA(iow)->err == ERR_ERROR) {
100                return -1; /* ERROR! */
101        }
102
103        DATA(iow)->strm.next_in = (char*)buffer;
104        DATA(iow)->strm.avail_in = len;
105
106        while (DATA(iow)->err == ERR_OK && DATA(iow)->strm.avail_in > 0) {
107                while (DATA(iow)->strm.avail_out <= 0) {
108                        int bytes_written = wandio_wwrite(DATA(iow)->child, 
109                                DATA(iow)->outbuff,
110                                sizeof(DATA(iow)->outbuff));
111                        if (bytes_written <= 0) { /* Error */
112                                DATA(iow)->err = ERR_ERROR;
113                                /* Return how much data we managed to write ok */
114                                if (DATA(iow)->strm.avail_in != (uint32_t)len) {
115                                        return len-DATA(iow)->strm.avail_in;
116                                }
117                                /* Now return error */
118                                return -1;
119                        }
120                        DATA(iow)->strm.next_out = DATA(iow)->outbuff;
121                        DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff);
122                }
123                /* Decompress some data into the output buffer */
124                int err=BZ2_bzCompress(&DATA(iow)->strm, 0);
125                switch(err) {
126                        case BZ_RUN_OK:
127                        case BZ_OK:
128                                DATA(iow)->err = ERR_OK;
129                                break;
130                        default:
131                                DATA(iow)->err = ERR_ERROR;
132                                break;
133                }
134        }
135        /* Return the number of bytes compressed */
136        return len-DATA(iow)->strm.avail_in;
137}
138
139static void bz_wclose(iow_t *iow)
140{
141        while (BZ2_bzCompress(&DATA(iow)->strm, BZ_FINISH) == BZ_OK) {
142                /* Need to flush the output buffer */
143                wandio_wwrite(DATA(iow)->child, 
144                                DATA(iow)->outbuff,
145                                sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out);
146                DATA(iow)->strm.next_out = DATA(iow)->outbuff;
147                DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff);
148        }
149        BZ2_bzCompressEnd(&DATA(iow)->strm);
150        wandio_wwrite(DATA(iow)->child, 
151                        DATA(iow)->outbuff,
152                        sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out);
153        wandio_wdestroy(DATA(iow)->child);
154        free(iow->data);
155        free(iow);
156}
157
158iow_source_t bz_wsource = {
159        "bzw",
160        bz_wwrite,
161        bz_wclose
162};
163
Note: See TracBrowser for help on using the repository browser.