From e52aed0890faa5aeb50112a10631a4bc4c6b4bbd Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Sun, 19 Oct 2025 19:49:10 +0200 Subject: [PATCH] Use right buffer type for inflate and deflate. fixes #1 Using a String for zlib decoder/encoder output doesn't work because it can only hold valid utf8. Read into a Vec instead and construct a lua string from it. --- src/api.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/api.rs b/src/api.rs index 6a7ae02..afb95b4 100644 --- a/src/api.rs +++ b/src/api.rs @@ -245,9 +245,13 @@ fn inflate(l: &Lua, compressed: LuaString) -> LuaResult { } let mut decoder = ZlibDecoder::new(compressed_bytes); - let mut decompressed = String::new(); - match decoder.read_to_string(&mut decompressed) { - Ok(_) => Ok(decompressed.into_lua_multi(l).unwrap()), + let mut decompressed = Vec::new(); + match decoder.read_to_end(&mut decompressed) { + Ok(_) => Ok(l + .create_string(&decompressed) + .unwrap() + .into_lua_multi(l) + .unwrap()), Err(e) => Ok((Value::Nil, e.to_string()).into_lua_multi(l).unwrap()), } } @@ -263,9 +267,13 @@ fn deflate(l: &Lua, uncompressed: LuaString) -> LuaResult { } let mut encoder = ZlibEncoder::new(uncompressed_bytes, Compression::fast()); - let mut compressed = String::new(); - match encoder.read_to_string(&mut compressed) { - Ok(_) => Ok(compressed.into_lua_multi(l).unwrap()), + let mut compressed = Vec::new(); + match encoder.read_to_end(&mut compressed) { + Ok(_) => Ok(l + .create_string(&compressed) + .unwrap() + .into_lua_multi(l) + .unwrap()), Err(e) => Ok((Value::Nil, e.to_string()).into_lua_multi(l).unwrap()), } }