Skip to content

Instantly share code, notes, and snippets.

@g-andrade
Created August 10, 2017 06:43
Show Gist options
  • Save g-andrade/817eea4925ced9c1a061b29269230f88 to your computer and use it in GitHub Desktop.
Save g-andrade/817eea4925ced9c1a061b29269230f88 to your computer and use it in GitHub Desktop.
Safe zlib inflation in Erlang
-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