source: libwandio/iow-zlib.c @ 2a7047c

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