Skip to content

Commit 0893482

Browse files
committed
Remove libdeflate compression path, fix its decompression path
1 parent 8cc676f commit 0893482

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

src/PerMessageDeflate.h

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,6 @@ struct DeflationStream {
150150
/* Deflate and optionally reset. You must not deflate an empty string. */
151151
std::string_view deflate(ZlibContext *zlibContext, std::string_view raw, bool reset) {
152152

153-
#ifdef UWS_USE_LIBDEFLATE
154-
/* Run a fast path in case of shared_compressor */
155-
if (reset) {
156-
size_t written = 0;
157-
static unsigned char buf[1024 + 1];
158-
159-
written = libdeflate_deflate_compress(zlibContext->compressor, raw.data(), raw.length(), buf, 1024);
160-
161-
if (written) {
162-
memcpy(&buf[written], "\x00", 1);
163-
return std::string_view((char *) buf, written + 1);
164-
}
165-
}
166-
#endif
167-
168153
/* Odd place to clear this one, fix */
169154
zlibContext->dynamicDeflationBuffer.clear();
170155

@@ -228,20 +213,19 @@ struct InflationStream {
228213
std::optional<std::string_view> inflate(ZlibContext *zlibContext, std::string_view compressed, size_t maxPayloadLength, bool reset) {
229214

230215
#ifdef UWS_USE_LIBDEFLATE
231-
/* Try fast path first */
232-
size_t written = 0;
233-
static char buf[1024];
234-
235-
/* We have to pad 9 bytes and restore those bytes when done since 9 is more than 6 of next WebSocket message */
236-
char tmp[9];
237-
memcpy(tmp, (char *) compressed.data() + compressed.length(), 9);
238-
memcpy((char *) compressed.data() + compressed.length(), "\x00\x00\xff\xff\x01\x00\x00\xff\xff", 9);
239-
libdeflate_result res = libdeflate_deflate_decompress(zlibContext->decompressor, compressed.data(), compressed.length() + 9, buf, 1024, &written);
240-
memcpy((char *) compressed.data() + compressed.length(), tmp, 9);
241-
242-
if (res == 0) {
243-
/* Fast path wins */
244-
return std::string_view(buf, written);
216+
/* Try fast path first (assuming single DEFLATE block) */
217+
size_t written = 0, consumed;
218+
zlibContext->dynamicInflationBuffer.clear();
219+
zlibContext->dynamicInflationBuffer.reserve(maxPayloadLength);
220+
221+
((char *)compressed.data())[0] |= 0x1; // BFINAL = 1
222+
libdeflate_result res = libdeflate_deflate_decompress_ex(zlibContext->decompressor, compressed.data(), compressed.length(), zlibContext->dynamicInflationBuffer.data(), maxPayloadLength, &consumed, &written);
223+
224+
if (res == 0 && consumed == compressed.length()) {
225+
return std::string_view(zlibContext->dynamicInflationBuffer.data(), written);
226+
} else {
227+
/* We can only end up here if the first DEFLATE block was not the last, so mark it as such */
228+
((char *)compressed.data())[0] &= ~0x1; // BFINAL = 0
245229
}
246230
#endif
247231

0 commit comments

Comments
 (0)