summaryrefslogtreecommitdiff
path: root/src/ZHandle.cc
blob: effebe8ef9e07a0e7e5ac332766645d943fa7476 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "ZHandle.h"
#include "Task.h"
#include "Async.h"
#include "TaskMan.h"

Balau::ZStream::ZStream(const IO<Handle> & h, int level, header_t header) : m_h(h) {
    m_zin.zalloc = m_zout.zalloc = NULL;
    m_zin.zfree = m_zout.zfree = NULL;
    m_zin.opaque = m_zout.opaque = NULL;
    m_zin.next_in = NULL;
    m_zin.avail_in = 0;
    int window = 0;
    switch (header) {
        case ZLIB: window = 15; break;
        case GZIP: window = 31; break;
        case RAW: window = -15; break;
    }
    int r;
    r = inflateInit2(&m_zin, window);
    EAssert(r == Z_OK, "inflateInit2 returned %i", r);
    r = deflateInit2(&m_zout, level, Z_DEFLATED, window, 9, Z_DEFAULT_STRATEGY);
    EAssert(r == Z_OK, "deflateInit2 returned %i", r);
    m_name.set("ZStream(%s)", m_h->getName());
}

void Balau::ZStream::close() throw (GeneralException) {
    switch (m_phase) {
    case IDLE:
    case WRITING_FINISH:
    case COMPRESSING_FINISH:
    case COMPRESSING_FINISH_IDLE:
        if (m_h->canWrite())
            finish();
        inflateEnd(&m_zin);
        deflateEnd(&m_zout);
        if (m_buf) {
            free(m_buf);
            m_buf = NULL;
        }
        m_closed = true;
        m_phase = CLOSING;
    case CLOSING:
        if (!m_detached)
            m_h->close();
        m_phase = IDLE;
        return;
    default:
        AAssert(false, "Wrong phase");
    }
}

bool Balau::ZStream::isClosed() {
    return m_closed;
}

bool Balau::ZStream::isEOF() {
    if (m_closed || m_eof)
        return true;
    return m_h->isEOF();
}

bool Balau::ZStream::canRead() {
    return m_h->canRead();
}

bool Balau::ZStream::canWrite() {
    return m_h->canWrite();
}

const char * Balau::ZStream::getName() {
    return m_name.to_charp();
}

namespace {

class AsyncOpZlib : public Balau::AsyncOperation {
  public:
      AsyncOpZlib(z_stream * z, bool deflate, int flush) : m_z(z), m_deflate(deflate), m_flush(flush) { }
    virtual bool needsMainQueue() { return false; }
    virtual bool needsFinishWorker() { return true; }
    virtual void run() {
        if (m_deflate)
            m_r = deflate(m_z, m_flush);
        else
            m_r = inflate(m_z, Z_SYNC_FLUSH);
    }
    virtual void done() { m_evt.doSignal(); }
    bool gotSignal() { return m_evt.gotSignal(); }
    int getR() { return m_r; }
    void yield() { Balau::Task::operationYield(&m_evt, Balau::Task::INTERRUPTIBLE); }
  private:
    z_stream * m_z;
    int m_r, m_flush;
    bool m_deflate;
    Balau::Events::Custom m_evt;
};

};

bool Balau::ZStream::isPendingComplete() {
    AsyncOpZlib * async = dynamic_cast<AsyncOpZlib *>(m_op);

    switch (m_phase) {
    case READING:
    case WRITING:
    case WRITING_FINISH:
    case CLOSING:
        return m_h->isPendingComplete();
    case COMPRESSING:
    case DECOMPRESSING:
    case COMPRESSING_FINISH:
        IAssert(async, "Shouldn't not have a cbResults here...");
        return async->gotSignal();
    default:
        return true;
    }
}

static const int BLOCK_SIZE = 1024;

ssize_t Balau::ZStream::read(void * buf, size_t count) throw (GeneralException) {
    if (m_closed || m_eof)
        return 0;

    AAssert(m_h->canRead(), "Can't call ZStream::read on a non-readable handle.");

    const int block_size = BLOCK_SIZE * (m_useAsyncOp ? 16 : 1);
    AsyncOpZlib * async = dynamic_cast<AsyncOpZlib *>(m_op);

    switch (m_phase) {
    case IDLE:
        m_total = 0;
        m_count = count;
        m_zin.next_out = (Bytef *) buf;
        m_zin.avail_out = count;
        if (!m_buf) {
            m_zin.next_in = m_buf = (uint8_t *) malloc(block_size);
            m_zin.avail_in = 0;
        }
        while ((m_count != 0) && !m_h->isClosed() && !m_h->isEOF()) {
            if (m_zin.avail_in == 0) {
                m_zin.next_in = m_buf;
                m_phase = READING;
    case READING:
                m_status = m_h->read(m_buf, block_size);
                if (m_status <= 0)
                    return m_total;
                m_zin.avail_in = m_status;
            }
            if (m_useAsyncOp) {
                m_phase = COMPRESSING;
                createAsyncOp(m_op = async = new AsyncOpZlib(&m_zin, false, 0));
                async->yield();
    case COMPRESSING:
                m_status = async->getR();
                delete async;
                m_op = async = NULL;
            } else {
                m_status = inflate(&m_zin, Z_SYNC_FLUSH);
                m_phase = COMPRESSING_IDLE;
                Task::operationYield(NULL, Task::INTERRUPTIBLE);
            }
    case COMPRESSING_IDLE:
            EAssert(m_status == Z_OK || m_status == Z_STREAM_END, "inflate() didn't return Z_OK or Z_STREAM_END but %zi", m_status);
            ssize_t didRead = m_count - m_zin.avail_out;
            m_total += didRead;
            m_count -= didRead;
            if (m_status == Z_STREAM_END) {
                m_eof = true;
                m_phase = IDLE;
                return m_total;
            }
        }
        break;
    default:
        AAssert(false, "Don't call an operation without finishing another.");
    }

    m_phase = IDLE;
    return m_total;
}

ssize_t Balau::ZStream::write(const void * buf, size_t count) throw (GeneralException) {
    if (m_closed || m_eof)
        return 0;

    AAssert(m_h->canWrite(), "Can't call ZStream::write on a non-writable handle.");

    const int block_size = BLOCK_SIZE * (m_useAsyncOp ? 16 : 1);
    ssize_t w;
    AsyncOpZlib * async = dynamic_cast<AsyncOpZlib *>(m_op);

    switch (m_phase) {
    case IDLE:
        m_total = 0;
        m_count = count;
        m_zout.next_in = (Bytef *) const_cast<void *>(buf);
        m_zout.avail_in = count;
        if (!m_buf)
            m_buf = (uint8_t *) malloc(block_size);
        while ((m_count != 0) && !m_h->isClosed()) {
            m_zout.next_out = (Bytef *) m_buf;
            m_zout.avail_out = block_size;
            if (m_useAsyncOp) {
                m_phase = DECOMPRESSING;
                createAsyncOp(m_op = async = new AsyncOpZlib(&m_zout, true, Z_NO_FLUSH));
                async->yield();
    case DECOMPRESSING:
                m_status = async->getR();
                delete async;
                m_op = async = NULL;
            } else {
                m_status = deflate(&m_zout, Z_NO_FLUSH);
                m_phase = DECOMPRESSING_IDLE;
                Task::operationYield(NULL, Task::INTERRUPTIBLE);
            }
    case DECOMPRESSING_IDLE:
            EAssert(m_status == Z_OK, "deflate() didn't return Z_OK but %zi", m_status);
            m_compressed = block_size - m_zout.avail_out;
            m_phase = WRITING;
            m_wptr = m_buf;
            while (m_compressed) {
    case WRITING:
                w = m_h->write(m_wptr, m_compressed);
                if (w <= 0) {
                    m_phase = IDLE;
                    return m_total;
                }
                m_compressed -= w;
                m_wptr += w;
            }
            size_t didWrite = m_count - m_zout.avail_in;
            m_total += didWrite;
            m_count -= didWrite;
        }
        break;
    default:
        AAssert(false, "Don't call an operation without finishing another.");
    }

    m_phase = IDLE;
    return m_total;
}

void Balau::ZStream::doFlush(bool finish) {
    AAssert(m_h->canWrite(), "Can't call ZStream::doFlush on a non-writable handle.");

    const int block_size = BLOCK_SIZE * (m_useAsyncOp ? 16 : 1);
    void * buf = m_useAsyncOp ? malloc(block_size) : alloca(block_size);
    AsyncOpZlib * async = dynamic_cast<AsyncOpZlib *>(m_op);
    ssize_t w = 0;

    switch (m_phase) {
    case IDLE:
        m_zout.next_in = NULL;
        m_zout.avail_in = 0;
        do {
            m_zout.next_out = (Bytef *) m_buf;
            m_zout.avail_out = block_size;
            if (m_useAsyncOp) {
                m_phase = COMPRESSING_FINISH;
                createAsyncOp(m_op = async = new AsyncOpZlib(&m_zout, true, finish ? Z_FINISH : Z_SYNC_FLUSH));
                async->yield();
    case COMPRESSING_FINISH:
                m_status = async->getR();
                delete async;
                m_op = async = NULL;
            } else {
                m_status = deflate(&m_zout, finish ? Z_FINISH : Z_SYNC_FLUSH);
                m_phase = COMPRESSING_FINISH_IDLE;
                Task::operationYield(NULL, Task::INTERRUPTIBLE);
            }
    case COMPRESSING_FINISH_IDLE:
            EAssert((m_status == Z_OK) || ((m_status == Z_STREAM_END) && finish), "deflate() didn't return Z_OK or Z_STREAM_END, but %zi (finish = %s)", m_status, finish ? "true" : "false");
            m_compressed = block_size - m_zout.avail_out;
            m_phase = WRITING_FINISH;
            m_wptr = m_buf;
            while (m_compressed) {
    case WRITING_FINISH:
                w = m_h->write(m_wptr, m_compressed);
                if (w <= 0) {
                    m_phase = IDLE;
                    return;
                }
                m_compressed -= w;
                m_wptr += w;
            }
        } while (m_status == Z_OK && finish);
        break;
    default:
        AAssert(false, "Don't call an operation without finishing another.");
    }

    m_phase = IDLE;
}