Created
August 10, 2017 06:43
-
-
Save g-andrade/817eea4925ced9c1a061b29269230f88 to your computer and use it in GitHub Desktop.
Safe zlib inflation in Erlang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-spec zlib_safe_uncompress(binary(), non_neg_integer()) -> binary() | no_return(). | |
zlib_safe_uncompress(CompressedData, UncompressedSize) -> | |
Z = zlib:open(), | |
zlib:inflateInit(Z), | |
zlib:setBufSize(Z, UncompressedSize), | |
case zlib:inflateChunk(Z, CompressedData) of | |
{more, _Chunk} -> | |
error({badarg, uncompressed_size_mismatch}); | |
Data -> | |
zlib:inflateEnd(Z), | |
iolist_to_binary(Data) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment