[erlang-questions] zlib deflate problem

Edwin Fine erlang-questions_efine@REDACTED
Mon May 19 21:44:03 CEST 2008


Colm,

This works without crashing:
-module(zlib_deflate_problem).
-export([start/0, start/1]).

-define(DATA_SIZE, 4000).

start() ->
 start(?DATA_SIZE).

start(DataSize) ->
  Data = <<0:DataSize/unit:8>>,
  io:format("Raw data -> ~p~n", [size(Data)]),

  Z = zlib:open(),
  ok = zlib:deflateInit(Z),
  Deflated = zlib:deflate(Z, Data, finish),
  ok = zlib:deflateEnd(Z),

  io:format("Deflated -> ~p~n", [Deflated]),

  ok = zlib:inflateInit(Z),
  io:format("Inflate buf size -> ~p~n", [zlib:getBufSize(Z)]),

  Inflated = case catch zlib:inflate(Z, Deflated) of
      {'EXIT', {'data_error', _Backtrace} } ->
          io:format("zlib:inflate data_error~n"),
          [];
      {'EXIT', Reason} ->
          io:format("zlib:inflate error -> [~p]~n", [Reason]),
          [];
      [] ->
          io:format("zlib:inflate empty response~n"), [];
      Iolist ->
          Iolist
  end,

  ok = zlib:inflateEnd(Z),
  ok = zlib:close(Z),

  io:format("Inflated -> ~p~n", [iolist_size(Inflated)]).

Hope this helps.
Ed

On Mon, May 19, 2008 at 1:55 PM, Colm <colm.dougan@REDACTED> wrote:

> Hi,
>
> I'm having a problem with erlang's zlib/deflate.  The problem is that
> if I try to deflate a piece of data that is a multiple of 4000 bytes
> (which also happens to be the zlib internal buffer size, I noticed)
> and then inflate it I get an inflate exception.  Below is some sample
> code.
>
> I realize I can set different internal buffer size using
> zlib:setBufSize but that doesn't really help as I can't predict the
> size of data I will be deflating/inflating.
>
> Am I doing something dumb here or is this a bug in the erlang zlib
> interface?  I strongly suspect it is the former.
>
> Thanks,
> Colm
>
>
>
> -module(zlib_deflate_problem).
> -export([start/0, start/1]).
>
> -define(DATA_SIZE, 4000).
>
> start() ->
>  start(?DATA_SIZE).
>
> start(DataSize) ->
>   % Generate a binary of size "0" x DATA_SIZE
>   Data = list_to_binary(
>       lists:map(fun(_) -> 0 end, lists:seq(1, DataSize))
>   ),

fyi, this would be MUCH easier as
Data = <<0:?DATA_SIZE/unit:8>>.

>
>   io:format("Raw data -> ~p~n", [size(Data)]),
>
>   ZInflate = zlib:open(),
>   ok = zlib:inflateInit(ZInflate),
>
>   ZDeflate = zlib:open(),
>   ok = zlib:deflateInit(ZDeflate),
>
>   Deflated = zlib:deflate(ZDeflate, Data, sync),
>   io:format("Deflated -> ~p~n", [Deflated]),
>
>   Inflated = case catch zlib:inflate(ZInflate, Deflated) of
>       {'EXIT', {'data_error', _Backtrace} } ->
>           io:format("zlib:inflate data_error~n"),
>           [];
>       {'EXIT', Reason} ->
>           io:format("zlib:inflate error -> [~p]~n", [Reason]),
>           [];
>       [] ->
>           io:format("zlib:inflate empty response~n"), [];
>       Iolist ->
>           Iolist
>   end,
>
>   io:format("Inflated -> ~p~n", [iolist_size(Inflated)]),
>
>   ok.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080519/6b84b21e/attachment.htm>


More information about the erlang-questions mailing list