From f06a6f75ed02e0780d65d22498926af2eafc947a Mon Sep 17 00:00:00 2001 From: Hans Bolinder Date: Wed, 31 Aug 2011 09:26:43 +0200 Subject: Update documentation and specifications of some of the zlib functions 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. --- erts/doc/src/zlib.xml | 60 ++++++++++----------- erts/preloaded/ebin/zlib.beam | Bin 12148 -> 11876 bytes erts/preloaded/src/zlib.erl | 115 +++++++++++++++++++++-------------------- lib/kernel/test/zlib_SUITE.erl | 8 +++ 4 files changed, 98 insertions(+), 85 deletions(-) diff --git a/erts/doc/src/zlib.xml b/erts/doc/src/zlib.xml index 47a649af02..8917ab5c3a 100644 --- a/erts/doc/src/zlib.xml +++ b/erts/doc/src/zlib.xml @@ -378,31 +378,31 @@ unpack(Z, Compressed, Dict) -> Calculate CRC -

Calculate the CRC checksum for Binary.

+

Calculate the CRC checksum for Data.

Calculate CRC -

Update a running CRC checksum for Binary. - If Binary is the empty binary, this function returns +

Update a running CRC checksum for Data. + If Data is the empty binary or the empty iolist, this function returns the required initial value for the crc.

-Crc = lists:foldl(fun(Bin,Crc0) ->  
-                      zlib:crc32(Z, Crc0, Bin),
-                  end, zlib:crc32(Z,<< >>), Bins)
+Crc = lists:foldl(fun(Data,Crc0) -> + zlib:crc32(Z, Crc0, Data), + end, zlib:crc32(Z,<< >>), Datas)
Combine two CRC's -

Combine two CRC checksums into one. For two binaries, - Bin1 and Bin2 with sizes of Size1 and +

Combine two CRC checksums into one. For two binaries or iolists, + Data1 and Data2 with sizes of Size1 and Size2, with CRC checksums CRC1 and CRC2. crc32_combine/4 returns the CRC - checksum of <<Bin1/binary,Bin2/binary>>, requiring + checksum of [Data1,Data2], requiring only CRC1, CRC2, and Size2.

@@ -411,75 +411,75 @@ Crc = lists:foldl(fun(Bin,Crc0) -> Calculate the adler checksum -

Calculate the Adler-32 checksum for Binary.

+

Calculate the Adler-32 checksum for Data.

Calculate the adler checksum -

Update a running Adler-32 checksum for Binary. - If Binary is the empty binary, this function returns +

Update a running Adler-32 checksum for Data. + If Data is the empty binary or the empty iolist, this function returns the required initial value for the checksum.

-Crc = lists:foldl(fun(Bin,Crc0) ->  
-                      zlib:adler32(Z, Crc0, Bin),
-                  end, zlib:adler32(Z,<< >>), Bins)
+Crc = lists:foldl(fun(Data,Crc0) -> + zlib:adler32(Z, Crc0, Data), + end, zlib:adler32(Z,<< >>), Datas)
Combine two Adler-32 checksums -

Combine two Adler-32 checksums into one. For two binaries, - Bin1 and Bin2 with sizes of Size1 and +

Combine two Adler-32 checksums into one. For two binaries or iolists, + Data1 and Data2 with sizes of Size1 and Size2, with Adler-32 checksums Adler1 and Adler2. adler32_combine/4 returns the Adler - checksum of <<Bin1/binary,Bin2/binary>>, requiring + checksum of [Data1,Data2], requiring only Adler1, Adler2, and Size2.

- Compress a binary with standard zlib functionality + Compress data with standard zlib functionality -

Compress a binary (with zlib headers and checksum).

+

Compress data (with zlib headers and checksum).

- Uncompress a binary with standard zlib functionality + Uncompress data with standard zlib functionality -

Uncompress a binary (with zlib headers and checksum).

+

Uncompress data (with zlib headers and checksum).

- Compress a binary without the zlib headers + Compress data without the zlib headers -

Compress a binary (without zlib headers and checksum).

+

Compress data (without zlib headers and checksum).

- Uncompress a binary without the zlib headers + Uncompress data without the zlib headers -

Uncompress a binary (without zlib headers and checksum).

+

Uncompress data (without zlib headers and checksum).

- Compress a binary with gz header + Compress data with gz header -

Compress a binary (with gz headers and checksum).

+

Compress data (with gz headers and checksum).

- Uncompress a binary with gz header + Uncompress data with gz header -

Uncompress a binary (with gz headers and checksum).

+

Uncompress data (with gz headers and checksum).

diff --git a/erts/preloaded/ebin/zlib.beam b/erts/preloaded/ebin/zlib.beam index d400269ed0..cda53f7692 100644 Binary files a/erts/preloaded/ebin/zlib.beam and b/erts/preloaded/ebin/zlib.beam differ diff --git a/erts/preloaded/src/zlib.erl b/erts/preloaded/src/zlib.erl index 6cc7b27114..210532edac 100644 --- a/erts/preloaded/src/zlib.erl +++ b/erts/preloaded/src/zlib.erl @@ -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). @@ -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). @@ -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, <>); -crc32(_Z, _CRC, _Binary) -> - erlang:error(badarg). +crc32(Z, CRC, Data) -> + call(Z, ?CRC32_2, [<>, 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, <>); -adler32(_Z, _Adler, _Binary) -> +adler32(Z, Adler, Data) when is_integer(Adler) -> + call(Z, ?ADLER32_2, [<>, Data]); +adler32(_Z, _Adler, _Data) -> erlang:error(badarg). -spec crc32_combine(Z, CRC1, CRC2, Size2) -> CRC when @@ -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) -> diff --git a/lib/kernel/test/zlib_SUITE.erl b/lib/kernel/test/zlib_SUITE.erl index 9eb84c9167..4ad9c6923d 100644 --- a/lib/kernel/test/zlib_SUITE.erl +++ b/lib/kernel/test/zlib_SUITE.erl @@ -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)), @@ -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)), @@ -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"; @@ -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>>, @@ -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(), @@ -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)), -- cgit v1.2.3