comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/compat/gzip.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 69:33d812a61356
1 // Copyright (c) 2017 Cloudflare, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #pragma once
23
24 #include <kj/io.h>
25 #include <kj/async-io.h>
26 #include <zlib.h>
27
28 KJ_BEGIN_HEADER
29
30 namespace kj {
31
32 namespace _ { // private
33
34 constexpr size_t KJ_GZ_BUF_SIZE = 4096;
35
36 class GzipOutputContext final {
37 public:
38 GzipOutputContext(kj::Maybe<int> compressionLevel);
39 ~GzipOutputContext() noexcept(false);
40 KJ_DISALLOW_COPY_AND_MOVE(GzipOutputContext);
41
42 void setInput(const void* in, size_t size);
43 kj::Tuple<bool, kj::ArrayPtr<const byte>> pumpOnce(int flush);
44
45 private:
46 bool compressing;
47 z_stream ctx = {};
48 byte buffer[_::KJ_GZ_BUF_SIZE];
49
50 [[noreturn]] void fail(int result);
51 };
52
53 } // namespace _ (private)
54
55 class GzipInputStream final: public InputStream {
56 public:
57 GzipInputStream(InputStream& inner);
58 ~GzipInputStream() noexcept(false);
59 KJ_DISALLOW_COPY_AND_MOVE(GzipInputStream);
60
61 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
62
63 private:
64 InputStream& inner;
65 z_stream ctx = {};
66 bool atValidEndpoint = false;
67
68 byte buffer[_::KJ_GZ_BUF_SIZE];
69
70 size_t readImpl(byte* buffer, size_t minBytes, size_t maxBytes, size_t alreadyRead);
71 };
72
73 class GzipOutputStream final: public OutputStream {
74 public:
75 enum { DECOMPRESS };
76
77 GzipOutputStream(OutputStream& inner, int compressionLevel = Z_DEFAULT_COMPRESSION);
78 GzipOutputStream(OutputStream& inner, decltype(DECOMPRESS));
79 ~GzipOutputStream() noexcept(false);
80 KJ_DISALLOW_COPY_AND_MOVE(GzipOutputStream);
81
82 void write(const void* buffer, size_t size) override;
83 using OutputStream::write;
84
85 inline void flush() {
86 pump(Z_SYNC_FLUSH);
87 }
88
89 private:
90 OutputStream& inner;
91 _::GzipOutputContext ctx;
92
93 void pump(int flush);
94 };
95
96 class GzipAsyncInputStream final: public AsyncInputStream {
97 public:
98 GzipAsyncInputStream(AsyncInputStream& inner);
99 ~GzipAsyncInputStream() noexcept(false);
100 KJ_DISALLOW_COPY_AND_MOVE(GzipAsyncInputStream);
101
102 Promise<size_t> tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
103
104 private:
105 AsyncInputStream& inner;
106 z_stream ctx = {};
107 bool atValidEndpoint = false;
108
109 byte buffer[_::KJ_GZ_BUF_SIZE];
110
111 Promise<size_t> readImpl(byte* buffer, size_t minBytes, size_t maxBytes, size_t alreadyRead);
112 };
113
114 class GzipAsyncOutputStream final: public AsyncOutputStream {
115 public:
116 enum { DECOMPRESS };
117
118 GzipAsyncOutputStream(AsyncOutputStream& inner, int compressionLevel = Z_DEFAULT_COMPRESSION);
119 GzipAsyncOutputStream(AsyncOutputStream& inner, decltype(DECOMPRESS));
120 KJ_DISALLOW_COPY_AND_MOVE(GzipAsyncOutputStream);
121
122 Promise<void> write(const void* buffer, size_t size) override;
123 Promise<void> write(ArrayPtr<const ArrayPtr<const byte>> pieces) override;
124
125 Promise<void> whenWriteDisconnected() override { return inner.whenWriteDisconnected(); }
126
127 inline Promise<void> flush() {
128 return pump(Z_SYNC_FLUSH);
129 }
130 // Call if you need to flush a stream at an arbitrary data point.
131
132 Promise<void> end() {
133 return pump(Z_FINISH);
134 }
135 // Must call to flush and finish the stream, since some data may be buffered.
136 //
137 // TODO(cleanup): This should be a virtual method on AsyncOutputStream.
138
139 private:
140 AsyncOutputStream& inner;
141 _::GzipOutputContext ctx;
142
143 kj::Promise<void> pump(int flush);
144 };
145
146 } // namespace kj
147
148 KJ_END_HEADER