aboutsummaryrefslogtreecommitdiff
path: root/src/stringprep/stringprep.erl
blob: 1b39693c28049dc9bd5794035745e3278df306c6 (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
%%%----------------------------------------------------------------------
%%% File    : stringprep.erl
%%% Author  : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : Interface to stringprep_drv
%%% Created : 16 Feb 2003 by Alexey Shchepin <alexey@proces-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2013   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., 59 Temple Place, Suite 330, Boston, MA
%%% 02111-1307 USA
%%%
%%%----------------------------------------------------------------------

-module(stringprep).

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

-behaviour(gen_server).

-export([start/0, start_link/0, tolower/1, nameprep/1,
	 nodeprep/1, resourceprep/1]).

%% Internal exports, call-back functions.
-export([init/1, handle_call/3, handle_cast/2,
	 handle_info/2, code_change/3, terminate/2]).

-define(STRINGPREP_PORT, stringprep_port).

-define(NAMEPREP_COMMAND, 1).

-define(NODEPREP_COMMAND, 2).

-define(RESOURCEPREP_COMMAND, 3).

start() ->
    gen_server:start({local, ?MODULE}, ?MODULE, [], []).

start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [],
			  []).

init([]) ->
    case erl_ddll:load_driver(ejabberd:get_so_path(),
			      stringprep_drv)
	of
      ok -> ok;
      {error, already_loaded} -> ok
    end,
    Port = open_port({spawn, "stringprep_drv"}, []),
    register(?STRINGPREP_PORT, Port),
    {ok, Port}.

%%% --------------------------------------------------------
%%% The call-back functions.
%%% --------------------------------------------------------

handle_call(_, _, State) -> {noreply, State}.

handle_cast(_, State) -> {noreply, State}.

handle_info({'EXIT', Port, Reason}, Port) ->
    {stop, {port_died, Reason}, Port};
handle_info({'EXIT', _Pid, _Reason}, Port) ->
    {noreply, Port};
handle_info(_, State) -> {noreply, State}.

code_change(_OldVsn, State, _Extra) -> {ok, State}.

terminate(_Reason, Port) -> Port ! {self, close}, ok.

-spec tolower(binary()) -> binary() | error.

tolower(String) -> control(0, String).

-spec nameprep(binary()) -> binary() | error.

nameprep(String) -> control(?NAMEPREP_COMMAND, String).

-spec nodeprep(binary()) -> binary() | error.

nodeprep(String) -> control(?NODEPREP_COMMAND, String).

-spec resourceprep(binary()) -> binary() | error.

resourceprep(String) ->
    control(?RESOURCEPREP_COMMAND, String).

control(Command, String) ->
    case port_control(?STRINGPREP_PORT, Command, String) of
      <<0, _/binary>> -> error;
      <<1, Res/binary>> -> Res
    end.