forked from dgiot/dgiot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emqx_base62.erl
106 lines (93 loc) · 3.51 KB
/
emqx_base62.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
%%--------------------------------------------------------------------
%% Copyright (c) 2017-2021 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_base62).
%% APIs
-export([ encode/1
, decode/1
]).
%%--------------------------------------------------------------------
%% APIs
%%--------------------------------------------------------------------
%% @doc Encode any data to base62 binary
-spec encode(string() | integer() | binary()) -> binary().
encode(I) when is_integer(I) ->
encode(integer_to_binary(I));
encode(S) when is_list(S)->
encode(unicode:characters_to_binary(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(B) when is_binary(B) ->
decode(B, <<>>).
%%--------------------------------------------------------------------
%% Interval Functions
%%--------------------------------------------------------------------
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>>,
encode(Rest, NewAcc);
encode(<<Index1:6, Index2:6, Index3:4>>, Acc) ->
CharList = [encode_char(Index1), encode_char(Index2), encode_char(Index3)],
NewAcc = <<Acc/binary,(iolist_to_binary(CharList))/binary>>,
encode(<<>>, NewAcc);
encode(<<Index1:6, Index2:2>>, Acc) ->
CharList = [encode_char(Index1), encode_char(Index2)],
NewAcc = <<Acc/binary,(iolist_to_binary(CharList))/binary>>,
encode(<<>>, NewAcc);
encode(<<>>, Acc) ->
Acc.
decode(<<Head:8, Rest/binary>>, Acc)
when bit_size(Rest) >= 8->
case Head == $9 of
true ->
<<Head1:8, Rest1/binary>> = Rest,
DecodeChar = decode_char(9, Head1),
<<_:2, RestBit:6>> = <<DecodeChar>>,
NewAcc = <<Acc/bitstring, RestBit:6>>,
decode(Rest1, NewAcc);
false ->
DecodeChar = decode_char(Head),
<<_:2, RestBit:6>> = <<DecodeChar>>,
NewAcc = <<Acc/bitstring, RestBit:6>>,
decode(Rest, NewAcc)
end;
decode(<<Head:8, Rest/binary>>, Acc) ->
DecodeChar = decode_char(Head),
LeftBitSize = bit_size(Acc) rem 8,
RightBitSize = 8 - LeftBitSize,
<<_:LeftBitSize, RestBit:RightBitSize>> = <<DecodeChar>>,
NewAcc = <<Acc/bitstring, RestBit:RightBitSize>>,
decode(Rest, NewAcc);
decode(<<>>, Acc) ->
Acc.
encode_char(I) when I < 26 ->
$A + I;
encode_char(I) when I < 52 ->
$a + I - 26;
encode_char(I) when I < 61 ->
$0 + I - 52;
encode_char(I) ->
[$9, $A + I - 61].
decode_char(I) when I >= $a andalso I =< $z ->
I + 26 - $a;
decode_char(I) when I >= $0 andalso I =< $8->
I + 52 - $0;
decode_char(I) when I >= $A andalso I =< $Z->
I - $A.
decode_char(9, I) ->
I + 61 - $A.