summaryrefslogtreecommitdiff
path: root/src/idna.erl
blob: dd19ad988fbebdcbdbec8fe232d9d03ca8118b06 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
%%%----------------------------------------------------------------------
%%% File    : idna.erl
%%% Author  : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : Support for IDNA (RFC3490)
%%% Created : 10 Apr 2004 by Alexey Shchepin <alexey@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2015   ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------

-module(idna).

-author('alexey@process-one.net').

-export([domain_utf8_to_ascii/1,
	 domain_ucs2_to_ascii/1,
         utf8_to_ucs2/1]).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

-spec domain_utf8_to_ascii(binary()) -> false | binary().

domain_utf8_to_ascii(Domain) ->
    domain_ucs2_to_ascii(utf8_to_ucs2(Domain)).

utf8_to_ucs2(S) ->
    utf8_to_ucs2(binary_to_list(S), "").

utf8_to_ucs2([], R) -> lists:reverse(R);
utf8_to_ucs2([C | S], R) when C < 128 ->
    utf8_to_ucs2(S, [C | R]);
utf8_to_ucs2([C1, C2 | S], R) when C1 < 224 ->
    utf8_to_ucs2(S, [C1 band 31 bsl 6 bor C2 band 63 | R]);
utf8_to_ucs2([C1, C2, C3 | S], R) when C1 < 240 ->
    utf8_to_ucs2(S,
		 [C1 band 15 bsl 12 bor (C2 band 63 bsl 6) bor C3 band 63
		  | R]).

-spec domain_ucs2_to_ascii(list()) -> false | binary().

domain_ucs2_to_ascii(Domain) ->
    case catch domain_ucs2_to_ascii1(Domain) of
      {'EXIT', _Reason} -> false;
      Res -> iolist_to_binary(Res)
    end.

domain_ucs2_to_ascii1(Domain) ->
    Parts = string:tokens(Domain,
			  [46, 12290, 65294, 65377]),
    ASCIIParts = lists:map(fun (P) -> to_ascii(P) end,
			   Parts),
    string:strip(lists:flatmap(fun (P) -> [$. | P] end,
			       ASCIIParts),
		 left, $.).

%% Domain names are already nameprep'ed in ejabberd, so we skiping this step
to_ascii(Name) ->
    false = lists:any(fun (C)
			      when (0 =< C) and (C =< 44) or
				     (46 =< C) and (C =< 47)
				     or (58 =< C) and (C =< 64)
				     or (91 =< C) and (C =< 96)
				     or (123 =< C) and (C =< 127) ->
			      true;
			  (_) -> false
		      end,
		      Name),
    case Name of
      [H | _] when H /= $- -> true = lists:last(Name) /= $-
    end,
    ASCIIName = case lists:any(fun (C) -> C > 127 end, Name)
		    of
		  true ->
		      true = case Name of
			       "xn--" ++ _ -> false;
			       _ -> true
			     end,
		      "xn--" ++ punycode_encode(Name);
		  false -> Name
		end,
    L = length(ASCIIName),
    true = (1 =< L) and (L =< 63),
    ASCIIName.

%%% PUNYCODE (RFC3492)

-define(BASE, 36).

-define(TMIN, 1).

-define(TMAX, 26).

-define(SKEW, 38).

-define(DAMP, 700).

-define(INITIAL_BIAS, 72).

-define(INITIAL_N, 128).

punycode_encode(Input) ->
    N = (?INITIAL_N),
    Delta = 0,
    Bias = (?INITIAL_BIAS),
    Basic = lists:filter(fun (C) -> C =< 127 end, Input),
    NonBasic = lists:filter(fun (C) -> C > 127 end, Input),
    L = length(Input),
    B = length(Basic),
    SNonBasic = lists:usort(NonBasic),
    Output1 = if B > 0 -> Basic ++ "-";
		 true -> ""
	      end,
    Output2 = punycode_encode1(Input, SNonBasic, B, B, L, N,
			       Delta, Bias, ""),
    Output1 ++ Output2.

punycode_encode1(Input, [M | SNonBasic], B, H, L, N,
		 Delta, Bias, Out)
    when H < L ->
    Delta1 = Delta + (M - N) * (H + 1),
    % let n = m
    {NewDelta, NewBias, NewH, NewOut} = lists:foldl(fun (C,
							 {ADelta, ABias, AH,
							  AOut}) ->
							    if C < M ->
								   {ADelta + 1,
								    ABias, AH,
								    AOut};
							       C == M ->
								   NewOut =
								       punycode_encode_delta(ADelta,
											     ABias,
											     AOut),
								   NewBias =
								       adapt(ADelta,
									     H +
									       1,
									     H
									       ==
									       B),
								   {0, NewBias,
								    AH + 1,
								    NewOut};
							       true ->
								   {ADelta,
								    ABias, AH,
								    AOut}
							    end
						    end,
						    {Delta1, Bias, H, Out},
						    Input),
    punycode_encode1(Input, SNonBasic, B, NewH, L, M + 1,
		     NewDelta + 1, NewBias, NewOut);
punycode_encode1(_Input, _SNonBasic, _B, _H, _L, _N,
		 _Delta, _Bias, Out) ->
    lists:reverse(Out).

punycode_encode_delta(Delta, Bias, Out) ->
    punycode_encode_delta(Delta, Bias, Out, ?BASE).

punycode_encode_delta(Delta, Bias, Out, K) ->
    T = if K =< Bias -> ?TMIN;
	   K >= Bias + (?TMAX) -> ?TMAX;
	   true -> K - Bias
	end,
    if Delta < T -> [codepoint(Delta) | Out];
       true ->
	   C = T + (Delta - T) rem ((?BASE) - T),
	   punycode_encode_delta((Delta - T) div ((?BASE) - T),
				 Bias, [codepoint(C) | Out], K + (?BASE))
    end.

adapt(Delta, NumPoints, FirstTime) ->
    Delta1 = if FirstTime -> Delta div (?DAMP);
		true -> Delta div 2
	     end,
    Delta2 = Delta1 + Delta1 div NumPoints,
    adapt1(Delta2, 0).

adapt1(Delta, K) ->
    if Delta > ((?BASE) - (?TMIN)) * (?TMAX) div 2 ->
	   adapt1(Delta div ((?BASE) - (?TMIN)), K + (?BASE));
       true ->
	   K +
	     ((?BASE) - (?TMIN) + 1) * Delta div (Delta + (?SKEW))
    end.

codepoint(C) ->
    if (0 =< C) and (C =< 25) -> C + 97;
       (26 =< C) and (C =< 35) -> C + 22
    end.

%%%===================================================================
%%% Unit tests
%%%===================================================================
-ifdef(TEST).

acsii_test() ->
    ?assertEqual(<<"test.org">>, domain_utf8_to_ascii(<<"test.org">>)).

utf8_test() ->
    ?assertEqual(
       <<"xn--d1acufc.xn--p1ai">>,
       domain_utf8_to_ascii(
         <<208,180,208,190,208,188,208,181,208,189,46,209,128,209,132>>)).

-endif.