I'm having an issue with implementing a GZIP string decompressor. The compressed string is a20d32fdda14b300b28aa6b72982af3b as shown below. However, when running this code, I receive the error:
"System.OverflowException occurred
HResult=0x80131516
Message=Arithmetic operation resulted in an overflow.
StackTrace:
at GZipDecompressor.Decompress.Main(String[] args)
"
when executing the line starting with "byte[] buffer2"
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace GZipDecompressor
{
class Decompress
{
public static void Main(string[] args)
{
string compressedText = "a20d32fdda14b300b28aa6b72982af3b";
int length = compressedText.Length;
byte[] buffer = Convert.FromBase64String(compressedText);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(buffer, 4, buffer.Length - 4);
byte[] buffer2 = new byte[BitConverter.ToInt32(buffer, 0)];
stream.Position = 0;
using (GZipStream stream2 = new GZipStream(stream, CompressionMode.Decompress))
{
stream2.Read(buffer2, 0, buffer2.Length);
}
Console.WriteLine(Encoding.UTF8.GetString(buffer2));
}
}
}
}
Could someone please explain why this is happening and how to go about solving it.
BitConverter.ToInt32(buffer, 0)
produces negative value which is invalid for array allocation.
User contributions licensed under CC BY-SA 3.0