source: libwandio/ior-lzma.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: 3.9 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2014 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors:
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 <lzma.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 an lzma reader */
45
46enum err_t {
47        ERR_OK  = 1,
48        ERR_EOF = 0,
49        ERR_ERROR = -1
50};
51
52struct lzma_t {
53        uint8_t inbuff[1024*1024];
54        lzma_stream strm;
55        io_t *parent;
56        int outoffset;
57        enum err_t err;
58};
59
60
61extern io_source_t lzma_source;
62
63#define DATA(io) ((struct lzma_t *)((io)->data))
64#define min(a,b) ((a)<(b) ? (a) : (b))
65
66io_t *lzma_open(io_t *parent)
67{
68        io_t *io;
69        if (!parent)
70                return NULL;
71        io = malloc(sizeof(io_t));
72        io->source = &lzma_source;
73        io->data = malloc(sizeof(struct lzma_t));
74
75        DATA(io)->parent = parent;
76
77        memset(&DATA(io)->strm, 0, sizeof(DATA(io)->strm));
78        DATA(io)->err = ERR_OK;
79
80        if (lzma_auto_decoder(&DATA(io)->strm, UINT64_MAX, 0) != LZMA_OK) {
81            free(io->data);
82            free(io);
83            fprintf(stderr, "auto decoder failed\n");
84            return NULL;
85        }
86
87        return io;
88}
89
90
91static off_t lzma_read(io_t *io, void *buffer, off_t len)
92{
93        if (DATA(io)->err == ERR_EOF)
94                return 0; /* EOF */
95        if (DATA(io)->err == ERR_ERROR) {
96                errno=EIO;
97                return -1; /* ERROR! */
98        }
99
100        DATA(io)->strm.avail_out = len;
101        DATA(io)->strm.next_out = buffer;
102
103        while (DATA(io)->err == ERR_OK && DATA(io)->strm.avail_out > 0) {
104                while (DATA(io)->strm.avail_in <= 0) {
105                        int bytes_read = wandio_read(DATA(io)->parent,
106                                (char*)DATA(io)->inbuff,
107                                sizeof(DATA(io)->inbuff));
108                        if (bytes_read == 0) {
109                                /* EOF */
110                                if (DATA(io)->strm.avail_out == (uint32_t)len) {
111                                        DATA(io)->err = ERR_EOF;
112                                        return 0;
113                                }
114                                /* Return how much data we've managed to read
115                                 * so far. */
116                                return len-DATA(io)->strm.avail_out;
117                        }
118                        if (bytes_read < 0) { /* Error */
119                                /* errno should be set */
120                                DATA(io)->err = ERR_ERROR;
121                                /* Return how much data we managed to read ok */
122                                if (DATA(io)->strm.avail_out != (uint32_t)len) {
123                                        return len-DATA(io)->strm.avail_out;
124                                }
125                                /* Now return error */
126                                return -1;
127                        }
128                        DATA(io)->strm.next_in = DATA(io)->inbuff;
129                        DATA(io)->strm.avail_in = bytes_read;
130                }
131                /* Decompress some data into the output buffer */
132                lzma_ret err=lzma_code(&DATA(io)->strm, LZMA_RUN);
133                switch(err) {
134                        case LZMA_OK:
135                                DATA(io)->err = ERR_OK;
136                                break;
137                        case LZMA_STREAM_END:
138                                DATA(io)->err = ERR_EOF;
139                                break;
140                        default:
141                                errno=EIO;
142                                DATA(io)->err = ERR_ERROR;
143                }
144        }
145        /* Return the number of bytes decompressed */
146        return len-DATA(io)->strm.avail_out;
147}
148
149static void lzma_close(io_t *io)
150{
151        lzma_end(&DATA(io)->strm);
152        wandio_destroy(DATA(io)->parent);
153        free(io->data);
154        free(io);
155}
156
157io_source_t lzma_source = {
158        "lzma",
159        lzma_read,
160        NULL,   /* peek */
161        NULL,   /* tell */
162        NULL,   /* seek */
163        lzma_close
164};
165
Note: See TracBrowser for help on using the repository browser.