Skip to content

Commit

Permalink
fix: decode error
Browse files Browse the repository at this point in the history
  • Loading branch information
yjh0502 committed Nov 9, 2024
1 parent 48e707f commit 8b04b9b
Show file tree
Hide file tree
Showing 7 changed files with 11,898 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# .editorconfig file
root = true

[*]
indent_style = space
indent_size = 4
9 changes: 6 additions & 3 deletions c_src/brotli_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
#define ATOMS \
ENTRY(ok) \
ENTRY(error) \
ENTRY(more) \
ENTRY(more_input) \
ENTRY(more_output) \
ENTRY(finished)

#define ENTRY(X) static ERL_NIF_TERM atom_##X;
Expand Down Expand Up @@ -320,11 +321,13 @@ brotli_decoder_decompress_stream(ErlNifEnv *env, int argc,

switch (result) {
case BROTLI_DECODER_RESULT_SUCCESS:
case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
return atom_ok;

case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
return enif_make_tuple2(env, atom_more_input, enif_make_long(env, available_in));

case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
return atom_more;
return enif_make_tuple2(env, atom_more_output, enif_make_long(env, available_in));

default:
return atom_error;
Expand Down
2 changes: 1 addition & 1 deletion src/brotli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ decode(Data, _Opts) ->
{ok, _} = Result ->
case brotli_decoder:is_finished(Decoder) of
true -> Result;
false -> error
false -> Result
end;
_ ->
error
Expand Down
25 changes: 21 additions & 4 deletions src/brotli_decoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,32 @@ new() ->

-spec stream(Decoder :: t(), Data :: iodata()) -> {ok | more, iodata()} | error.
stream(Decoder, Data) ->
case brotli_nif:decoder_decompress_stream(Decoder, Data) of
case stream(Decoder, Data, []) of
{ok, Out} -> {ok, iolist_to_binary(lists:reverse(Out))};
{more, Out} -> {more, iolist_to_binary(lists:reverse(Out))};
Other -> Other
end.

stream(Decoder, In, Out) ->
case brotli_nif:decoder_decompress_stream(Decoder, In) of
ok ->
{ok, brotli_nif:decoder_take_output(Decoder)};
more ->
{more, brotli_nif:decoder_take_output(Decoder)};
{ok, stream_take_output(Decoder, Out)};
{more_output, _N} ->
{more, stream_take_output(Decoder, Out)};
{more_input, N} ->
Out2 = stream_take_output(Decoder, Out),
stream(Decoder, binary:part(In, {byte_size(In) - N, N}), Out2);
Other ->
Other
end.


stream_take_output(Decoder, Data) ->
case brotli_nif:decoder_take_output(Decoder) of
<<>> -> Data;
Output -> stream_take_output(Decoder, [Output | Data])
end.

is_finished(Decoder) ->
brotli_nif:decoder_is_finished(Decoder).

Expand Down
26 changes: 26 additions & 0 deletions test/brotli_decoder_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-module(brotli_decoder_SUITE).

-compile(export_all).

-include_lib("stdlib/include/assert.hrl").
-include_lib("common_test/include/ct.hrl").

all() -> [pull_multiple].

pull_multiple(Config) ->
DataDir = ?config(data_dir, Config),
InFile = filename:join(DataDir, "calendar.br"),
{ok, In} = file:read_file(InFile),
Sha256Sum = <<"42A53DA0F6FA4D69689B8096879EC4CF0D198E03ADBB227CE630830D634AD8A7">>,

{ok, Out} = brotli:decode(In),
?assertEqual(1083692, byte_size(Out)),

Hasher0 = crypto:hash_init(sha256),
Hasher1 = crypto:hash_update(Hasher0, Out),
Hash = crypto:hash_final(Hasher1),

?assertEqual(Sha256Sum, to_hex(Hash)).

to_hex(Bin) ->
<<<<Y>> || <<X:4>> <= Bin, Y <- integer_to_list(X, 16)>>.
Loading

0 comments on commit 8b04b9b

Please sign in to comment.