dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.45k stars 4.76k forks source link

gzipstream throws InvalidDataException on specific string #30179

Closed timerickson closed 4 years ago

timerickson commented 5 years ago

When decompressing the compressed value of a certain string -- EdisMax2 -- via GzipStream on netcore2.2 on macOS Sierra there is a System.IO.InvalidDataException: "The archive entry was compressed using an unsupported compression method."

Simply changing the value to EDisMax2 fixes the behavior. I was going to try debugging this myself but am unable to build corefx on my mac due to openssl issues in cmake :-(

I have attached my repro code (though note this was on macOS Sierra. Unknown if it is reproducible anywhere else.

GzipTest.zip

vcsjones commented 5 years ago

unable to build corefx on my mac due to openssl issues in cmake

There are some things you need to do to get corefx building on macOS w.r.t. OpenSSL documented here. If you are on macOS Catalina beta, there are known issues at dotnet/runtime#30109 with work arounds. If you are unable to get it building, please file a new issue with build output so folks can help.

stephentoub commented 5 years ago

This isn't an issue with GZipStream. Your repro is trying to use Encoding.Unicode.GetString to create a string from the compressed bytes, and then Encoding.Unicode.GetBytes to roundtrip back from that string to the compressed bytes. That will not roundtrip as you expect, and as such you're trying to decompress invalid data.

If you replace the:

var gzippedString = encoding.GetString(gzippedBytes);
...
var gzippedBytes = encoding.GetBytes(value);

with

var gzippedString = Convert.ToBase64String(gzippedBytes);
...
var gzippedBytes = Convert.FromBase64String(value);

your repro runs fine.

timerickson commented 5 years ago

Yup - figured it was the simpler answer but was confused that a test was passing using Encoding strategy. Thanks and sorry for the bad q... And thanks for the heads-up on how to compile the code....