1/*
2 * Copyright (C) 2012 Google Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef WebSocketDeflater_h
32#define WebSocketDeflater_h
33
34#if ENABLE(WEB_SOCKETS)
35
36#include <wtf/Noncopyable.h>
37#include <wtf/OwnPtr.h>
38#include <wtf/PassOwnPtr.h>
39#include <wtf/Vector.h>
40
41struct z_stream_s;
42typedef z_stream_s z_stream;
43
44namespace WebCore {
45
46class WebSocketDeflater {
47    WTF_MAKE_FAST_ALLOCATED;
48public:
49    enum ContextTakeOverMode {
50        DoNotTakeOverContext,
51        TakeOverContext
52    };
53    static PassOwnPtr<WebSocketDeflater> create(int windowBits, ContextTakeOverMode = TakeOverContext);
54
55    ~WebSocketDeflater();
56
57    bool initialize();
58    bool addBytes(const char*, size_t);
59    bool finish();
60    const char* data() { return m_buffer.data(); }
61    size_t size() const { return m_buffer.size(); }
62    void reset();
63
64private:
65    WebSocketDeflater(int windowBits, ContextTakeOverMode);
66
67    int m_windowBits;
68    ContextTakeOverMode m_contextTakeOverMode;
69    Vector<char> m_buffer;
70    OwnPtr<z_stream> m_stream;
71};
72
73class WebSocketInflater {
74    WTF_MAKE_FAST_ALLOCATED;
75public:
76    static PassOwnPtr<WebSocketInflater> create(int windowBits = 15);
77
78    ~WebSocketInflater();
79
80    bool initialize();
81    bool addBytes(const char*, size_t);
82    bool finish();
83    const char* data() { return m_buffer.data(); }
84    size_t size() const { return m_buffer.size(); }
85    void reset();
86
87private:
88    explicit WebSocketInflater(int windowBits);
89
90    int m_windowBits;
91    Vector<char> m_buffer;
92    OwnPtr<z_stream> m_stream;
93};
94
95}
96
97#endif // ENABLE(WEB_SOCKETS)
98
99#endif // WebSocketDeflater_h
100