source: libwandio/iow-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: 4.4 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 <zlib.h>
37#include "wandio.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 <assert.h>
44
45/* Libtrace IO module implementing a zlib writer */
46
47enum err_t {
48        ERR_OK  = 1,
49        ERR_EOF = 0,
50        ERR_ERROR = -1
51};
52
53struct zlibw_t {
54        z_stream strm;
55        Bytef outbuff[1024*1024];
56        iow_t *child;
57        enum err_t err;
58        int inoffset;
59};
60
61
62extern iow_source_t zlib_wsource; 
63
64#define DATA(iow) ((struct zlibw_t *)((iow)->data))
65#define min(a,b) ((a)<(b) ? (a) : (b))
66
67iow_t *zlib_wopen(iow_t *child, int compress_level)
68{
69        iow_t *iow;
70        if (!child)
71                return NULL;
72        iow = malloc(sizeof(iow_t));
73        iow->source = &zlib_wsource;
74        iow->data = malloc(sizeof(struct zlibw_t));
75
76        DATA(iow)->child = child;
77
78        DATA(iow)->strm.next_in = NULL;
79        DATA(iow)->strm.avail_in = 0;
80        DATA(iow)->strm.next_out = DATA(iow)->outbuff;
81        DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff);
82        DATA(iow)->strm.zalloc = Z_NULL;
83        DATA(iow)->strm.zfree = Z_NULL;
84        DATA(iow)->strm.opaque = NULL;
85        DATA(iow)->err = ERR_OK;
86
87        deflateInit2(&DATA(iow)->strm, 
88                        compress_level, /* Level */
89                        Z_DEFLATED,     /* Method */
90                        15 | 16,        /* 15 bits of windowsize, 16 == use gzip header */
91                        9,              /* Use maximum (fastest) amount of memory usage */
92                        Z_DEFAULT_STRATEGY
93                );
94
95        return iow;
96}
97
98
99static off_t zlib_wwrite(iow_t *iow, const char *buffer, off_t len)
100{
101        if (DATA(iow)->err == ERR_EOF) {
102                return 0; /* EOF */
103        }
104        if (DATA(iow)->err == ERR_ERROR) {
105                return -1; /* ERROR! */
106        }
107
108        DATA(iow)->strm.next_in = (Bytef*)buffer; /* This casts away const, but it's really const
109                                                   * anyway
110                                                   */
111        DATA(iow)->strm.avail_in = len;
112
113        while (DATA(iow)->err == ERR_OK && DATA(iow)->strm.avail_in > 0) {
114                while (DATA(iow)->strm.avail_out <= 0) {
115                        int bytes_written = wandio_wwrite(DATA(iow)->child, 
116                                (char *)DATA(iow)->outbuff,
117                                sizeof(DATA(iow)->outbuff));
118                        if (bytes_written <= 0) { /* Error */
119                                DATA(iow)->err = ERR_ERROR;
120                                /* Return how much data we managed to write ok */
121                                if (DATA(iow)->strm.avail_in != (uint32_t)len) {
122                                        return len-DATA(iow)->strm.avail_in;
123                                }
124                                /* Now return error */
125                                return -1;
126                        }
127                        DATA(iow)->strm.next_out = DATA(iow)->outbuff;
128                        DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff);
129                }
130                /* Decompress some data into the output buffer */
131                int err=deflate(&DATA(iow)->strm, 0);
132                switch(err) {
133                        case Z_OK:
134                                DATA(iow)->err = ERR_OK;
135                                break;
136                        default:
137                                DATA(iow)->err = ERR_ERROR;
138                }
139        }
140        /* Return the number of bytes decompressed */
141        return len-DATA(iow)->strm.avail_in;
142}
143
144static void zlib_wclose(iow_t *iow)
145{
146        int res;
147       
148        while (1) {
149                res = deflate(&DATA(iow)->strm, Z_FINISH);
150
151                if (res == Z_STREAM_END)
152                        break;
153                if (res == Z_STREAM_ERROR) {
154                        fprintf(stderr, "Z_STREAM_ERROR while closing output\n");
155                        break;
156                }
157       
158                wandio_wwrite(DATA(iow)->child, 
159                                (char*)DATA(iow)->outbuff,
160                                sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out);
161                DATA(iow)->strm.next_out = DATA(iow)->outbuff;
162                DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff);
163        }
164
165        deflateEnd(&DATA(iow)->strm);
166        wandio_wwrite(DATA(iow)->child, 
167                        (char *)DATA(iow)->outbuff,
168                        sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out);
169        wandio_wdestroy(DATA(iow)->child);
170        free(iow->data);
171        free(iow);
172}
173
174iow_source_t zlib_wsource = {
175        "zlibw",
176        zlib_wwrite,
177        zlib_wclose
178};
179
Note: See TracBrowser for help on using the repository browser.