|
|
@@ -14,8 +14,10 @@
|
|
|
|
|
|
-module(emqx_base62).
|
|
|
|
|
|
--export([encode/1, decode/1]).
|
|
|
-
|
|
|
+-export([encode/1,
|
|
|
+ encode/2,
|
|
|
+ decode/1,
|
|
|
+ decode/2]).
|
|
|
|
|
|
%% @doc Encode any data to base62 binary
|
|
|
-spec encode(string()
|
|
|
@@ -28,18 +30,23 @@ encode(S) when is_list(S)->
|
|
|
encode(B) when is_binary(B) ->
|
|
|
encode(B, <<>>).
|
|
|
|
|
|
+%% encode(D, string) ->
|
|
|
+%% binary_to_list(encode(D)).
|
|
|
+
|
|
|
%% @doc Decode base62 binary to origin data binary
|
|
|
decode(L) when is_list(L) ->
|
|
|
decode(list_to_binary(L));
|
|
|
decode(B) when is_binary(B) ->
|
|
|
decode(B, <<>>).
|
|
|
|
|
|
-%% encode_base62(<<H:24, Rest/binary>>, Acc) ->
|
|
|
-%% encode_byte_group(H, Acc);
|
|
|
-%% encode_base62(<<H:16, Rest/binary>>, Acc) ->
|
|
|
-%% encode_byte_group(H, Acc);
|
|
|
|
|
|
|
|
|
+%%====================================================================
|
|
|
+%% Internal functions
|
|
|
+%%====================================================================
|
|
|
+
|
|
|
+encode(D, string) ->
|
|
|
+ binary_to_list(encode(D));
|
|
|
encode(<<Index1:6, Index2:6, Index3:6, Index4:6, Rest/binary>>, Acc) ->
|
|
|
CharList = [encode_char(Index1), encode_char(Index2), encode_char(Index3), encode_char(Index4)],
|
|
|
NewAcc = <<Acc/binary,(iolist_to_binary(CharList))/binary>>,
|
|
|
@@ -55,6 +62,10 @@ encode(<<Index1:6, Index2:2>>, Acc) ->
|
|
|
encode(<<>>, Acc) ->
|
|
|
Acc.
|
|
|
|
|
|
+decode(D, integer) ->
|
|
|
+ binary_to_integer(decode(D));
|
|
|
+decode(D, string) ->
|
|
|
+ binary_to_list(decode(D));
|
|
|
decode(<<Head:8, Rest/binary>>, Acc)
|
|
|
when bit_size(Rest) >= 8->
|
|
|
case Head == $9 of
|
|
|
@@ -99,3 +110,4 @@ decode_char(I) when I >= $A andalso I =< $Z->
|
|
|
|
|
|
decode_char(9, I) ->
|
|
|
I + 61 - $A.
|
|
|
+
|