Skip to content
This repository has been archived by the owner on Sep 4, 2019. It is now read-only.

Commit

Permalink
Update documentation and specifications of some of the zlib functions
Browse files Browse the repository at this point in the history
The functions zlib:deflateSetDictionary/2 and zlib:inflateSetDictionary/2
accept iodata() as Dictionary.

The functions zlib:crc32/2,3, zlib:adler32/2,3, zlib:compress/1,
zlib:uncompress/1, zlib:zip/1, and zlib:unzip/1 accept iodata() as
data.
  • Loading branch information
uabboli committed Sep 2, 2011
1 parent 9f1e455 commit f06a6f7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 85 deletions.
60 changes: 30 additions & 30 deletions erts/doc/src/zlib.xml
Expand Up @@ -378,31 +378,31 @@ unpack(Z, Compressed, Dict) ->
<name name="crc32" arity="2"/>
<fsummary>Calculate CRC</fsummary>
<desc>
<p>Calculate the CRC checksum for <c><anno>Binary</anno></c>.</p>
<p>Calculate the CRC checksum for <c><anno>Data</anno></c>.</p>
</desc>
</func>
<func>
<name name="crc32" arity="3"/>
<fsummary>Calculate CRC</fsummary>
<desc>
<p>Update a running CRC checksum for <c><anno>Binary</anno></c>.
If <c><anno>Binary</anno></c> is the empty binary, this function returns
<p>Update a running CRC checksum for <c><anno>Data</anno></c>.
If <c><anno>Data</anno></c> is the empty binary or the empty iolist, this function returns
the required initial value for the crc.</p>
<pre>
Crc = lists:foldl(fun(Bin,Crc0) ->
zlib:crc32(Z, Crc0, Bin),
end, zlib:crc32(Z,&lt;&lt; &gt;&gt;), Bins)</pre>
Crc = lists:foldl(fun(Data,Crc0) ->
zlib:crc32(Z, Crc0, Data),
end, zlib:crc32(Z,&lt;&lt; &gt;&gt;), Datas)</pre>
</desc>
</func>
<func>
<name name="crc32_combine" arity="4"/>
<fsummary>Combine two CRC's</fsummary>
<desc>
<p>Combine two CRC checksums into one. For two binaries,
<c>Bin1</c> and <c>Bin2</c> with sizes of <c>Size1</c> and
<p>Combine two CRC checksums into one. For two binaries or iolists,
<c>Data1</c> and <c>Data2</c> with sizes of <c>Size1</c> and
<c><anno>Size2</anno></c>, with CRC checksums <c><anno>CRC1</anno></c> and
<c><anno>CRC2</anno></c>. <c>crc32_combine/4</c> returns the <c><anno>CRC</anno></c>
checksum of <c>&lt;&lt;Bin1/binary,Bin2/binary&gt;&gt;</c>, requiring
checksum of <c>[Data1,Data2]</c>, requiring
only <c><anno>CRC1</anno></c>, <c><anno>CRC2</anno></c>, and <c><anno>Size2</anno></c>.
</p>
</desc>
Expand All @@ -411,75 +411,75 @@ Crc = lists:foldl(fun(Bin,Crc0) ->
<name name="adler32" arity="2"/>
<fsummary>Calculate the adler checksum</fsummary>
<desc>
<p>Calculate the Adler-32 checksum for <c><anno>Binary</anno></c>.</p>
<p>Calculate the Adler-32 checksum for <c><anno>Data</anno></c>.</p>
</desc>
</func>
<func>
<name name="adler32" arity="3"/>
<fsummary>Calculate the adler checksum</fsummary>
<desc>
<p>Update a running Adler-32 checksum for <c><anno>Binary</anno></c>.
If <c><anno>Binary</anno></c> is the empty binary, this function returns
<p>Update a running Adler-32 checksum for <c><anno>Data</anno></c>.
If <c><anno>Data</anno></c> is the empty binary or the empty iolist, this function returns
the required initial value for the checksum.</p>
<pre>
Crc = lists:foldl(fun(Bin,Crc0) ->
zlib:adler32(Z, Crc0, Bin),
end, zlib:adler32(Z,&lt;&lt; &gt;&gt;), Bins)</pre>
Crc = lists:foldl(fun(Data,Crc0) ->
zlib:adler32(Z, Crc0, Data),
end, zlib:adler32(Z,&lt;&lt; &gt;&gt;), Datas)</pre>
</desc>
</func>
<func>
<name name="adler32_combine" arity="4"/>
<fsummary>Combine two Adler-32 checksums</fsummary>
<desc>
<p>Combine two Adler-32 checksums into one. For two binaries,
<c>Bin1</c> and <c>Bin2</c> with sizes of <c>Size1</c> and
<p>Combine two Adler-32 checksums into one. For two binaries or iolists,
<c>Data1</c> and <c>Data2</c> with sizes of <c>Size1</c> and
<c><anno>Size2</anno></c>, with Adler-32 checksums <c><anno>Adler1</anno></c> and
<c><anno>Adler2</anno></c>. <c>adler32_combine/4</c> returns the <c><anno>Adler</anno></c>
checksum of <c>&lt;&lt;Bin1/binary,Bin2/binary&gt;&gt;</c>, requiring
checksum of <c>[Data1,Data2]</c>, requiring
only <c><anno>Adler1</anno></c>, <c><anno>Adler2</anno></c>, and <c><anno>Size2</anno></c>.
</p>
</desc>
</func>
<func>
<name name="compress" arity="1"/>
<fsummary>Compress a binary with standard zlib functionality</fsummary>
<fsummary>Compress data with standard zlib functionality</fsummary>
<desc>
<p>Compress a binary (with zlib headers and checksum).</p>
<p>Compress data (with zlib headers and checksum).</p>
</desc>
</func>
<func>
<name name="uncompress" arity="1"/>
<fsummary>Uncompress a binary with standard zlib functionality</fsummary>
<fsummary>Uncompress data with standard zlib functionality</fsummary>
<desc>
<p>Uncompress a binary (with zlib headers and checksum).</p>
<p>Uncompress data (with zlib headers and checksum).</p>
</desc>
</func>
<func>
<name name="zip" arity="1"/>
<fsummary>Compress a binary without the zlib headers</fsummary>
<fsummary>Compress data without the zlib headers</fsummary>
<desc>
<p>Compress a binary (without zlib headers and checksum).</p>
<p>Compress data (without zlib headers and checksum).</p>
</desc>
</func>
<func>
<name name="unzip" arity="1"/>
<fsummary>Uncompress a binary without the zlib headers</fsummary>
<fsummary>Uncompress data without the zlib headers</fsummary>
<desc>
<p>Uncompress a binary (without zlib headers and checksum).</p>
<p>Uncompress data (without zlib headers and checksum).</p>
</desc>
</func>
<func>
<name name="gzip" arity="1"/>
<fsummary>Compress a binary with gz header</fsummary>
<fsummary>Compress data with gz header</fsummary>
<desc>
<p>Compress a binary (with gz headers and checksum).</p>
<p>Compress data (with gz headers and checksum).</p>
</desc>
</func>
<func>
<name name="gunzip" arity="1"/>
<fsummary>Uncompress a binary with gz header</fsummary>
<fsummary>Uncompress data with gz header</fsummary>
<desc>
<p>Uncompress a binary (with gz headers and checksum).</p>
<p>Uncompress data (with gz headers and checksum).</p>
</desc>
</func>
</funcs>
Expand Down
Binary file modified erts/preloaded/ebin/zlib.beam
Binary file not shown.
115 changes: 60 additions & 55 deletions erts/preloaded/src/zlib.erl
Expand Up @@ -173,7 +173,7 @@ deflateInit(Z, Level, Method, WindowBits, MemLevel, Strategy) ->

-spec deflateSetDictionary(Z, Dictionary) -> Adler32 when
Z :: zstream(),
Dictionary :: binary(),
Dictionary :: iodata(),
Adler32 :: integer().
deflateSetDictionary(Z, Dictionary) ->
call(Z, ?DEFLATE_SETDICT, Dictionary).
Expand Down Expand Up @@ -232,7 +232,7 @@ inflateInit(Z, WindowBits) ->

-spec inflateSetDictionary(Z, Dictionary) -> 'ok' when
Z :: zstream(),
Dictionary :: binary().
Dictionary :: iodata().
inflateSetDictionary(Z, Dictionary) ->
call(Z, ?INFLATE_SETDICT, Dictionary).

Expand Down Expand Up @@ -283,38 +283,36 @@ getBufSize(Z) ->
crc32(Z) ->
call(Z, ?CRC32_0, []).

-spec crc32(Z, Binary) -> CRC when
-spec crc32(Z, Data) -> CRC when
Z :: zstream(),
Binary :: binary(),
Data :: iodata(),
CRC :: integer().
crc32(Z, Binary) ->
call(Z, ?CRC32_1, Binary).
crc32(Z, Data) ->
call(Z, ?CRC32_1, Data).

-spec crc32(Z, PrevCRC, Binary) -> CRC when
-spec crc32(Z, PrevCRC, Data) -> CRC when
Z :: zstream(),
PrevCRC :: integer(),
Binary :: binary(),
Data :: iodata(),
CRC :: integer().
crc32(Z, CRC, Binary) when is_binary(Binary), is_integer(CRC) ->
call(Z, ?CRC32_2, <<CRC:32, Binary/binary>>);
crc32(_Z, _CRC, _Binary) ->
erlang:error(badarg).
crc32(Z, CRC, Data) ->
call(Z, ?CRC32_2, [<<CRC:32>>, Data]).

-spec adler32(Z, Binary) -> CheckSum when
-spec adler32(Z, Data) -> CheckSum when
Z :: zstream(),
Binary :: binary(),
Data :: iodata(),
CheckSum :: integer().
adler32(Z, Binary) ->
call(Z, ?ADLER32_1, Binary).
adler32(Z, Data) ->
call(Z, ?ADLER32_1, Data).

-spec adler32(Z, PrevAdler, Binary) -> CheckSum when
-spec adler32(Z, PrevAdler, Data) -> CheckSum when
Z :: zstream(),
PrevAdler :: integer(),
Binary :: binary(),
Data :: iodata(),
CheckSum :: integer().
adler32(Z, Adler, Binary) when is_binary(Binary), is_integer(Adler) ->
call(Z, ?ADLER32_2, <<Adler:32, Binary/binary>>);
adler32(_Z, _Adler, _Binary) ->
adler32(Z, Adler, Data) when is_integer(Adler) ->
call(Z, ?ADLER32_2, [<<Adler:32>>, Data]);
adler32(_Z, _Adler, _Data) ->
erlang:error(badarg).

-spec crc32_combine(Z, CRC1, CRC2, Size2) -> CRC when
Expand Down Expand Up @@ -346,76 +344,83 @@ getQSize(Z) ->
call(Z, ?GET_QSIZE, []).

%% compress/uncompress zlib with header
-spec compress(Binary) -> Compressed when
Binary :: binary(),
-spec compress(Data) -> Compressed when
Data :: iodata(),
Compressed :: binary().
compress(Binary) ->
compress(Data) ->
Z = open(),
deflateInit(Z, default),
Bs = deflate(Z, Binary,finish),
Bs = deflate(Z, Data, finish),
deflateEnd(Z),
close(Z),
list_to_binary(Bs).
iolist_to_binary(Bs).

-spec uncompress(Binary) -> Decompressed when
Binary :: binary(),
-spec uncompress(Data) -> Decompressed when
Data :: iodata(),
Decompressed :: binary().
uncompress(Binary) when byte_size(Binary) >= 8 ->
Z = open(),
inflateInit(Z),
Bs = inflate(Z, Binary),
inflateEnd(Z),
close(Z),
list_to_binary(Bs);
uncompress(Binary) when is_binary(Binary) -> erlang:error(data_error);
uncompress(_) -> erlang:error(badarg).
uncompress(Data) ->
try iolist_size(Data) of
Size ->
if
Size >= 8 ->
Z = open(),
inflateInit(Z),
Bs = inflate(Z, Data),
inflateEnd(Z),
close(Z),
iolist_to_binary(Bs);
true ->
erlang:error(data_error)
end
catch
_:_ ->
erlang:error(badarg)
end.

%% unzip/zip zlib without header (zip members)
-spec zip(Binary) -> Compressed when
Binary :: binary(),
-spec zip(Data) -> Compressed when
Data :: iodata(),
Compressed :: binary().
zip(Binary) ->
zip(Data) ->
Z = open(),
deflateInit(Z, default, deflated, -?MAX_WBITS, 8, default),
Bs = deflate(Z, Binary, finish),
Bs = deflate(Z, Data, finish),
deflateEnd(Z),
close(Z),
list_to_binary(Bs).
iolist_to_binary(Bs).

-spec unzip(Binary) -> Decompressed when
Binary :: binary(),
-spec unzip(Data) -> Decompressed when
Data :: iodata(),
Decompressed :: binary().
unzip(Binary) ->
unzip(Data) ->
Z = open(),
inflateInit(Z, -?MAX_WBITS),
Bs = inflate(Z, Binary),
Bs = inflate(Z, Data),
inflateEnd(Z),
close(Z),
list_to_binary(Bs).
iolist_to_binary(Bs).

-spec gzip(Data) -> Compressed when
Data :: iodata(),
Compressed :: binary().
gzip(Data) when is_binary(Data); is_list(Data) ->
gzip(Data) ->
Z = open(),
deflateInit(Z, default, deflated, 16+?MAX_WBITS, 8, default),
Bs = deflate(Z, Data, finish),
deflateEnd(Z),
close(Z),
iolist_to_binary(Bs);
gzip(_) -> erlang:error(badarg).
iolist_to_binary(Bs).

-spec gunzip(Binary) -> Decompressed when
Binary :: binary(),
-spec gunzip(Data) -> Decompressed when
Data :: iodata(),
Decompressed :: binary().
gunzip(Data) when is_binary(Data); is_list(Data) ->
gunzip(Data) ->
Z = open(),
inflateInit(Z, 16+?MAX_WBITS),
Bs = inflate(Z, Data),
inflateEnd(Z),
close(Z),
iolist_to_binary(Bs);
gunzip(_) -> erlang:error(badarg).
iolist_to_binary(Bs).

-spec collect(zstream()) -> iolist().
collect(Z) ->
Expand Down
8 changes: 8 additions & 0 deletions lib/kernel/test/zlib_SUITE.erl
Expand Up @@ -412,6 +412,7 @@ api_crc32(Config) when is_list(Config) ->
Compressed = list_to_binary(Compressed1 ++ Compressed2),
CRC1 = ?m( CRC1 when is_integer(CRC1), zlib:crc32(Z1)),
?m(CRC1 when is_integer(CRC1), zlib:crc32(Z1,Bin)),
?m(CRC1 when is_integer(CRC1), zlib:crc32(Z1,binary_to_list(Bin))),
?m(CRC2 when is_integer(CRC2), zlib:crc32(Z1,Compressed)),
CRC2 = ?m(CRC2 when is_integer(CRC2), zlib:crc32(Z1,0,Compressed)),
?m(CRC3 when CRC2 /= CRC3, zlib:crc32(Z1,234,Compressed)),
Expand All @@ -437,6 +438,7 @@ api_adler32(Config) when is_list(Config) ->
Compressed2 = ?m(_, zlib:deflate(Z1, <<>>, finish)),
Compressed = list_to_binary(Compressed1 ++ Compressed2),
?m(ADLER1 when is_integer(ADLER1), zlib:adler32(Z1,Bin)),
?m(ADLER1 when is_integer(ADLER1), zlib:adler32(Z1,binary_to_list(Bin))),
ADLER2 = ?m(ADLER2 when is_integer(ADLER2), zlib:adler32(Z1,Compressed)),
?m(ADLER2 when is_integer(ADLER2), zlib:adler32(Z1,1,Compressed)),
?m(ADLER3 when ADLER2 /= ADLER3, zlib:adler32(Z1,234,Compressed)),
Expand Down Expand Up @@ -464,6 +466,7 @@ api_un_compress(Config) when is_list(Config) ->
?m({'EXIT',{data_error,_}}, zlib:uncompress(<<120,156,3>>)),
?m({'EXIT',{data_error,_}}, zlib:uncompress(<<120,156,3,0>>)),
?m({'EXIT',{data_error,_}}, zlib:uncompress(<<0,156,3,0,0,0,0,1>>)),
?m(Bin, zlib:uncompress(binary_to_list(Comp))),
?m(Bin, zlib:uncompress(Comp)).

api_un_zip(doc) -> "Test zip";
Expand All @@ -472,10 +475,12 @@ api_un_zip(Config) when is_list(Config) ->
?m(?BARG,zlib:zip(not_a_binary)),
Bin = <<1,11,1,23,45>>,
?line Comp = zlib:zip(Bin),
?m(Comp, zlib:zip(binary_to_list(Bin))),
?m(?BARG,zlib:unzip(not_a_binary)),
?m({'EXIT',{data_error,_}}, zlib:unzip(<<171,171,171,171,171>>)),
?m({'EXIT',{data_error,_}}, zlib:unzip(<<>>)),
?m(Bin, zlib:unzip(Comp)),
?m(Bin, zlib:unzip(binary_to_list(Comp))),

%% OTP-6396
B = <<131,104,19,100,0,13,99,95,99,105,100,95,99,115,103,115,110,95,50,97,1,107,0,4,208,161,246,29,107,0,3,237,166,224,107,0,6,66,240,153,0,2,10,1,0,8,97,116,116,97,99,104,101,100,104,2,100,0,22,117,112,100,97,116,101,95,112,100,112,95,99,111,110,116,101,120,116,95,114,101,113,107,0,114,69,3,12,1,11,97,31,113,150,64,104,132,61,64,104,12,3,197,31,113,150,64,104,132,61,64,104,12,1,11,97,31,115,150,64,104,116,73,64,104,0,0,0,0,0,0,65,149,16,61,65,149,16,61,1,241,33,4,5,0,33,4,4,10,6,10,181,4,10,6,10,181,38,15,99,111,109,109,97,110,100,1,114,45,97,112,110,45,49,3,99,111,109,5,109,110,99,57,57,6,109,99,99,50,52,48,4,103,112,114,115,8,0,104,2,104,2,100,0,8,97,99,116,105,118,97,116,101,104,23,100,0,11,112,100,112,95,99,111,110,116,1,120,116,100,0,7,112,114,105,109,97,114,121,97,1,100,0,9,117,110,100,101,102,105,110,101,100,97,1,97,4,97,4,97,7,100,0,9,117,110,100,101,102,105,110,101,100,100,0,9,117,110,100,101,102,105,110,10100,100,0,9,117,110,100,101,102,105,110,101,100,100,0,5,102,97,108,115,101,100,0,9,117,110,100,101,102,105,110,101,100,100,0,9,117,110,100,101,102,105,110,101,100,100,0,9,117,110,100,101,102,105,1,101,100,97,0,100,0,9,117,110,100,101,102,105,110,101,100,107,0,4,16,0,1,144,107,0,4,61,139,186,181,107,0,4,10,8,201,49,100,0,9,117,110,100,101,102,105,110,101,100,100,0,9,117,110,100,101,102,105,0,101,100,100,0,9,117,110,100,101,102,105,110,101,100,104,2,104,3,98,0,0,7,214,97,11,97,20,104,3,97,17,97,16,97,21,106,108,0,0,0,3,104,2,97,1,104,2,104,3,98,0,0,7,214,97,11,97,20,104,3,97,17,97,167,20,104,2,97,4,104,2,104,3,98,0,0,7,214,97,11,97,20,104,3,97,17,97,16,97,21,104,2,97,10,104,2,104,3,98,0,0,7,214,97,11,97,20,104,3,97,17,97,16,97,26,106,100,0,5,118,101,114,57,57,100,0,9,117,110,0,101,102,105,110,101,100,107,0,2,0,244,107,0,4,10,6,102,195,107,0,4,10,6,102,195,100,0,9,117,110,100,101,102,105,110,101,100,100,0,9,117,110,100,101,102,105,110,101,100,107,0,125,248,143,0,203,25115,157,116,65,185,65,172,55,87,164,88,225,50,203,251,115,157,116,65,185,65,172,55,87,164,88,225,50,0,0,82,153,50,0,200,98,87,148,237,193,185,65,149,167,69,144,14,16,153,50,3,81,70,94,13,109,193,1,120,5,181,113,198,118,50,3,81,70,94,13,109,193,185,120,5,181,113,198,118,153,3,81,70,94,13,109,193,185,120,5,181,113,198,118,153,50,16,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,113,92,2,119,128,0,0,108,0,0,1,107,0,114,69,3,12,1,11,97,31,113,150,64,104,132,61,64,104,12,3,11,97,31,113,150,64,104,132,61,64,104,12,1,11,97,31,115,150,64,104,116,73,64,104,0,0,0,0,0,0,65,149,16,61,65,149,16,61,1,241,33,4,0,33,4,4,10,6,10,181,4,10,6,10,181,38,15,99,111,109,109,97,110,100,101,114,45,97,112,110,45,49,3,99,111,109,5,109,110,99,57,57,6,109,99,99,50,52,48,4,103,112,114,115,8,0,106>>,
Expand Down Expand Up @@ -504,10 +509,12 @@ api_g_un_zip(Config) when is_list(Config) ->
?m(?BARG,zlib:gzip(not_a_binary)),
Bin = <<1,11,1,23,45>>,
?line Comp = zlib:gzip(Bin),
?m(Comp, zlib:gzip(binary_to_list(Bin))),
?m(?BARG, zlib:gunzip(not_a_binary)),
?m(?DATA_ERROR, zlib:gunzip(<<171,171,171,171,171>>)),
?m(?DATA_ERROR, zlib:gunzip(<<>>)),
?m(Bin, zlib:gunzip(Comp)),
?m(Bin, zlib:gunzip(binary_to_list(Comp))),

%% Bad CRC; bad length.
BadCrc = bad_crc_data(),
Expand Down Expand Up @@ -844,6 +851,7 @@ dictionary_usage({run}) ->
?m(ok, zlib:inflateInit(Z2)),
?line {'EXIT',{{need_dictionary,DictID},_}} = (catch zlib:inflate(Z2, Compressed)),
?m(ok, zlib:inflateSetDictionary(Z2, Dict)),
?m(ok, zlib:inflateSetDictionary(Z2, binary_to_list(Dict))),
?line Uncompressed = ?m(B when is_list(B), zlib:inflate(Z2, [])),
?m(ok, zlib:inflateEnd(Z2)),
?m(ok, zlib:close(Z2)),
Expand Down

0 comments on commit f06a6f7

Please sign in to comment.