source: lib/ior-zlib.c @ a89e2fa

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since a89e2fa was 22a9ccc, checked in by Shane Alcock <salcock@…>, 12 years ago
  • Updated licensing and documentation for all the IO writer modules
  • Got rid of annoying "Write thread leaving" message!
  • Added Id keyword support to all IO modules
  • 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 <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 <errno.h>
43
44/* Libtrace IO module implementing a zlib reader */
45
46enum err_t {
47        ERR_OK  = 1,
48        ERR_EOF = 0,
49        ERR_ERROR = -1
50};
51
52struct zlib_t {
53        z_stream strm;
54        Bytef inbuff[1024*1024]; /* bytef is what zlib uses for buffer pointers */
55        io_t *parent;
56        int outoffset;
57        enum err_t err;
58};
59
60
61extern io_source_t zlib_source; 
62
63#define DATA(io) ((struct zlib_t *)((io)->data))
64#define min(a,b) ((a)<(b) ? (a) : (b))
65
66io_t *zlib_open(io_t *parent)
67{
68        io_t *io;
69        if (!parent)
70                return NULL;
71        io = malloc(sizeof(io_t));
72        io->source = &zlib_source;
73        io->data = malloc(sizeof(struct zlib_t));
74
75        DATA(io)->parent = parent;
76
77        DATA(io)->strm.next_in = NULL;
78        DATA(io)->strm.avail_in = 0;
79        DATA(io)->strm.next_out = NULL;
80        DATA(io)->strm.avail_out = 0;
81        DATA(io)->strm.zalloc = Z_NULL;
82        DATA(io)->strm.zfree = Z_NULL;
83        DATA(io)->strm.opaque = NULL;
84        DATA(io)->err = ERR_OK;
85
86        inflateInit2(&DATA(io)->strm, 15 | 32);
87
88        return io;
89}
90
91
92static off_t zlib_read(io_t *io, void *buffer, off_t len)
93{
94        if (DATA(io)->err == ERR_EOF)
95                return 0; /* EOF */
96        if (DATA(io)->err == ERR_ERROR) {
97                errno=EIO;
98                return -1; /* ERROR! */
99        }
100
101        DATA(io)->strm.avail_out = len;
102        DATA(io)->strm.next_out = (Bytef*)buffer;
103
104        while (DATA(io)->err == ERR_OK && DATA(io)->strm.avail_out > 0) {
105                while (DATA(io)->strm.avail_in <= 0) {
106                        int bytes_read = wandio_read(DATA(io)->parent, 
107                                (char*)DATA(io)->inbuff,
108                                sizeof(DATA(io)->inbuff));
109                        if (bytes_read == 0) /* EOF */
110                                return len-DATA(io)->strm.avail_out;
111                        if (bytes_read < 0) { /* Error */
112                                /* errno should be set */
113                                DATA(io)->err = ERR_ERROR;
114                                /* Return how much data we managed to read ok */
115                                if (DATA(io)->strm.avail_out != len) {
116                                        return len-DATA(io)->strm.avail_out;
117                                }
118                                /* Now return error */
119                                return -1;
120                        }
121                        DATA(io)->strm.next_in = DATA(io)->inbuff;
122                        DATA(io)->strm.avail_in = bytes_read;
123                }
124                /* Decompress some data into the output buffer */
125                int err=inflate(&DATA(io)->strm, 0);
126                switch(err) {
127                        case Z_OK:
128                                DATA(io)->err = ERR_OK;
129                                break;
130                        case Z_STREAM_END:
131                                DATA(io)->err = ERR_EOF;
132                                break;
133                        default:
134                                errno=EIO;
135                                DATA(io)->err = ERR_ERROR;
136                }
137        }
138        /* Return the number of bytes decompressed */
139        return len-DATA(io)->strm.avail_out;
140}
141
142static void zlib_close(io_t *io)
143{
144        inflateEnd(&DATA(io)->strm);
145        wandio_destroy(DATA(io)->parent);
146        free(io->data);
147        free(io);
148}
149
150io_source_t zlib_source = {
151        "zlib",
152        zlib_read,
153        NULL,   /* peek */
154        NULL,   /* tell */
155        NULL,   /* seek */
156        zlib_close
157};
158
Note: See TracBrowser for help on using the repository browser.