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 ++++++++++++++++++++++-------------------- 3 files changed, 90 insertions(+), 85 deletions(-) (limited to 'erts') 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) -> -- cgit v1.2.3