File: zlibfilter.c

package info (click to toggle)
sketch 0.6.13-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,284 kB
  • ctags: 8,453
  • sloc: python: 34,711; ansic: 16,543; makefile: 83; sh: 26
file content (143 lines) | stat: -rw-r--r-- 3,768 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 *  Copyright (C) 1998, 1999, 2000 by Bernhard Herzog.
 *
 *			All Rights Reserved
 *
 *  Permission to use, copy, modify, and distribute this software and
 *  its documentation for any purpose and without fee is hereby granted,
 *  provided that the above copyright notice appear in all copies and
 *  that both that copyright notice and this permission notice appear in
 *  supporting documentation, and that the name of the author not be
 *  used in advertising or publicity pertaining to distribution of the
 *  software without specific, written prior permission.
 *
 *  THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 *  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 *  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 *  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 *  OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 *  NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 *  WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <zlib.h>
#include "zlibfilter.h"

#define INBUFSIZE 1024

typedef struct {
    char * buffer;
    size_t buffer_length;
    int eod_reached;
    z_stream zstr;
} FlateDecodeState;

static size_t
read_zlib(void* clientdata, PyObject * source, char * buf, size_t length)
{
    size_t bytesread = 0;
    int result;
    FlateDecodeState *state = (FlateDecodeState*)clientdata;

    if (state->eod_reached)
	return 0;

    state->zstr.next_out = buf;
    state->zstr.avail_out = length;
    do
    {
	if (state->zstr.avail_in == 0)
	{
	    state->zstr.next_in = state->buffer;	    
	    bytesread = Filter_Read(source, state->buffer,
				    state->buffer_length);
	    if (bytesread == 0)
	    {
		if (PyErr_Occurred())
		    return 0;
	    }
	    state->zstr.avail_in = bytesread;
	}
	result = inflate(&state->zstr, Z_SYNC_FLUSH);
	if (result == Z_STREAM_END)
	{
	    state->eod_reached = 1;
	}
	else if (result != Z_OK)
	{
	    if (state->zstr.msg == Z_NULL)
		PyErr_Format(PyExc_IOError, "FlateDecode: Error %i", result); 
	    else
		PyErr_Format(PyExc_IOError, "FlateDecode: Error %i: %.200s",
			     result, state->zstr.msg);
	    return 0;
	}
    } while (state->zstr.avail_out == length && !state->eod_reached);
    return length - state->zstr.avail_out;
}


static void
dealloc_zlib(void * clientdata)
{
    FlateDecodeState * state = (FlateDecodeState*)clientdata;
    inflateEnd(&state->zstr); /* XXX error handling */
    PyMem_DEL(state);
}


PyObject *
Filter_FlateDecode(PyObject * self, PyObject * args)
{
    PyObject * target;
    FlateDecodeState * state;
    int result;

    if (!PyArg_ParseTuple(args, "O", &target))
	return NULL;

    state = PyMem_NEW(FlateDecodeState, 1);
    if (!state)
	return PyErr_NoMemory();
    state->buffer = PyMem_NEW(char, INBUFSIZE);
    if (!state->buffer)
    {
	PyMem_DEL(state);
	return PyErr_NoMemory();
    }

    state->buffer_length = INBUFSIZE;
    state->zstr.zalloc = NULL;
    state->zstr.zfree = NULL;
    state->zstr.opaque = NULL;
    state->zstr.next_in = state->buffer;
    state->zstr.avail_in = 0;
    state->eod_reached = 0;

    result = inflateInit(&state->zstr);
    if (result != Z_OK)
    {
	if (result == Z_MEM_ERROR)
	{
	    PyErr_SetString(PyExc_MemoryError,
			    "FlateDecode: No memory for z_stream");
	}
	else 
	{
	    if (state->zstr.msg == Z_NULL)
		PyErr_Format(PyExc_IOError, "FlateDecode: Zlib Error %i",
			     result); 
	    else
		PyErr_Format(PyExc_IOError,
			     "FlateDecode: Zlib Error %i: %.200s",
			     result, state->zstr.msg);  
	}
	PyMem_DEL(state->buffer);
	PyMem_DEL(state);
	return NULL;
    }

    return Filter_NewDecoder(target, "FlateDecode", 0, read_zlib,
			     NULL, dealloc_zlib, state);
}