diff --git a/apps/emqx_exhook/src/emqx_exhook_handler.erl b/apps/emqx_exhook/src/emqx_exhook_handler.erl index e60eadaa7..e03e23813 100644 --- a/apps/emqx_exhook/src/emqx_exhook_handler.erl +++ b/apps/emqx_exhook/src/emqx_exhook_handler.erl @@ -273,7 +273,7 @@ clientinfo(ClientInfo = message(#message{id = Id, qos = Qos, from = From, topic = Topic, payload = Payload, timestamp = Ts}) -> #{node => stringfy(node()), - id => hexstr(Id), + id => emqx_guid:to_hexstr(Id), qos => Qos, from => stringfy(From), topic => Topic, @@ -304,12 +304,6 @@ stringfy(Term) when is_atom(Term) -> stringfy(Term) -> unicode:characters_to_binary((io_lib:format("~0p", [Term]))). -hexstr(B) -> - << <<(hexchar(H)), (hexchar(L))>> || <> <= B>>. - -hexchar(I) when I >= 0 andalso I < 10 -> I + $0; -hexchar(I) -> I - 10 + $A. - %%-------------------------------------------------------------------- %% Acc funcs diff --git a/apps/emqx_exproto/src/emqx_exproto_channel.erl b/apps/emqx_exproto/src/emqx_exproto_channel.erl index 0eec36410..2966cbba9 100644 --- a/apps/emqx_exproto/src/emqx_exproto_channel.erl +++ b/apps/emqx_exproto/src/emqx_exproto_channel.erl @@ -205,7 +205,7 @@ handle_deliver(Delivers, Channel = #channel{clientinfo = ClientInfo}) -> [ClientInfo], Msg), NMsg = emqx_mountpoint:unmount(Mountpoint, Msg1), #{node => NodeStr, - id => hexstr(emqx_message:id(NMsg)), + id => emqx_guid:to_hexstr(emqx_message:id(NMsg)), qos => emqx_message:qos(NMsg), from => fmt_from(emqx_message:from(NMsg)), topic => emqx_message:topic(NMsg), @@ -591,9 +591,6 @@ default_clientinfo(#{peername := {PeerHost, _}, stringfy(Reason) -> unicode:characters_to_binary((io_lib:format("~0p", [Reason]))). -hexstr(Bin) -> - [io_lib:format("~2.16.0B",[X]) || <> <= Bin]. - fmt_from(undefined) -> <<>>; fmt_from(Bin) when is_binary(Bin) -> Bin; fmt_from(T) -> stringfy(T). diff --git a/apps/emqx_lwm2m/src/emqx_lwm2m_cmd_handler.erl b/apps/emqx_lwm2m/src/emqx_lwm2m_cmd_handler.erl index d542acf91..cd22797fa 100644 --- a/apps/emqx_lwm2m/src/emqx_lwm2m_cmd_handler.erl +++ b/apps/emqx_lwm2m/src/emqx_lwm2m_cmd_handler.erl @@ -303,18 +303,7 @@ bin(Float) when is_float(Float) -> float_to_binary(Float). decoding(Datas, <<"hex">>) -> lists:map(fun(Data = #{<<"value">> := Value}) -> - Data#{<<"value">> => hexstr_to_bin(binary_to_list(Value))} + Data#{<<"value">> => emqx_misc:hexstr2bin(Value)} end, Datas); decoding(Datas, _) -> Datas. - -hexstr_to_bin(S) -> - hexstr_to_bin(S, []). -hexstr_to_bin([], Acc) -> - list_to_binary(lists:reverse(Acc)); -hexstr_to_bin([X,Y|T], Acc) -> - {ok, [V], []} = io_lib:fread("~16u", [X,Y]), - hexstr_to_bin(T, [V | Acc]); -hexstr_to_bin([X|T], Acc) -> - {ok, [V], []} = io_lib:fread("~16u", lists:flatten([X,"0"])), - hexstr_to_bin(T, [V | Acc]). \ No newline at end of file diff --git a/apps/emqx_rule_engine/src/emqx_rule_funcs.erl b/apps/emqx_rule_engine/src/emqx_rule_funcs.erl index 55917f751..3e41b2520 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_funcs.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_funcs.erl @@ -512,12 +512,10 @@ map(Data) -> emqx_rule_utils:map(Data). bin2hexstr(Bin) when is_binary(Bin) -> - IntL = binary_to_list(Bin), - list_to_binary([io_lib:format("~2.16.0B", [Int]) || Int <- IntL]). + emqx_misc:bin2hexstr(Bin). hexstr2bin(Str) when is_binary(Str) -> - list_to_binary([binary_to_integer(W, 16) || <> <= Str]). - + emqx_misc:hexstr2bin(Str). %%------------------------------------------------------------------------------ %% NULL Funcs @@ -776,14 +774,7 @@ sha256(S) when is_binary(S) -> hash(sha256, S). hash(Type, Data) -> - hexstring(crypto:hash(Type, Data)). - -hexstring(<>) -> - iolist_to_binary(io_lib:format("~32.16.0b", [X])); -hexstring(<>) -> - iolist_to_binary(io_lib:format("~40.16.0b", [X])); -hexstring(<>) -> - iolist_to_binary(io_lib:format("~64.16.0b", [X])). + emqx_misc:bin2hexstr(crypto:hash(Type, Data)). %%------------------------------------------------------------------------------ %% Data encode and decode Funcs diff --git a/src/emqx_guid.erl b/src/emqx_guid.erl index 79e445d73..7a7657ad1 100644 --- a/src/emqx_guid.erl +++ b/src/emqx_guid.erl @@ -136,11 +136,11 @@ npid() -> PidByte3:8, PidByte4:8>>, NPid. -to_hexstr(<>) -> - list_to_binary(integer_to_list(I, 16)). +to_hexstr(I) when byte_size(I) =:= 16 -> + emqx_misc:bin2hexstr(I). -from_hexstr(S) -> - I = list_to_integer(binary_to_list(S), 16), <>. +from_hexstr(S) when byte_size(S) =:= 32 -> + emqx_misc:hexstr2bin(S). to_base62(<>) -> emqx_base62:encode(I). diff --git a/src/emqx_misc.erl b/src/emqx_misc.erl index 88e3c91a6..a96261f74 100644 --- a/src/emqx_misc.erl +++ b/src/emqx_misc.erl @@ -45,6 +45,10 @@ , index_of/2 ]). +-export([ bin2hexstr/1 + , hexstr2bin/1 + ]). + %% @doc Merge options -spec(merge_opts(Opts, Opts) -> Opts when Opts :: proplists:proplist()). merge_opts(Defaults, Options) -> @@ -233,3 +237,18 @@ index_of(E, I, [E|_]) -> index_of(E, I, [_|L]) -> index_of(E, I+1, L). +-spec(bin2hexstr(binary()) -> binary()). +bin2hexstr(B) when is_binary(B) -> + << <<(int2hexchar(H)), (int2hexchar(L))>> || <> <= B>>. + +int2hexchar(I) when I >= 0 andalso I < 10 -> I + $0; +int2hexchar(I) -> I - 10 + $A. + +-spec(hexstr2bin(binary()) -> binary()). +hexstr2bin(B) when is_binary(B) -> + << <<(hexchar2int(H)*16 + hexchar2int(L))>> || <> <= B>>. + +hexchar2int(I) when I >= $0 andalso I =< $9 -> I - $0; +hexchar2int(I) when I >= $A andalso I =< $F -> I - $A + 10; +hexchar2int(I) when I >= $a andalso I =< $f -> I - $a + 10. + diff --git a/src/emqx_tracer.erl b/src/emqx_tracer.erl index ea9247531..871c3dc81 100644 --- a/src/emqx_tracer.erl +++ b/src/emqx_tracer.erl @@ -161,9 +161,4 @@ handler_name(Bin) -> end. hashstr(Bin) -> - hexstr(crypto:hash(sha, Bin)). - -hexstr(Bin) -> - lists:flatten( - [io_lib:format("~2.16.0B", [Int]) - || Int <- binary_to_list(Bin)]). + binary_to_list(emqx_misc:bin2hexstr(Bin)).