diff options
author | Badlop <badlop@process-one.net> | 2013-03-14 10:33:02 +0100 |
---|---|---|
committer | Badlop <badlop@process-one.net> | 2013-03-14 10:33:02 +0100 |
commit | 9deb294328bb3f9eb6bd2c0e7cd500732e9b5830 (patch) | |
tree | 7e1066c130250627ee0abab44a135f583a28d07f /src/stringprep | |
parent | list_to_integer/2 only works in OTP R14 and newer (diff) |
Accumulated patch to binarize and indent code
Diffstat (limited to 'src/stringprep')
-rw-r--r-- | src/stringprep/Makefile.in | 2 | ||||
-rw-r--r-- | src/stringprep/stringprep.erl | 68 | ||||
-rw-r--r-- | src/stringprep/stringprep_drv.c | 50 | ||||
-rw-r--r-- | src/stringprep/stringprep_sup.erl | 10 |
4 files changed, 59 insertions, 71 deletions
diff --git a/src/stringprep/Makefile.in b/src/stringprep/Makefile.in index 2b78bc95..7c4997d2 100644 --- a/src/stringprep/Makefile.in +++ b/src/stringprep/Makefile.in @@ -24,7 +24,7 @@ EFLAGS += -pz .. # make debug=true to compile Erlang module with debug informations. ifdef debug - EFLAGS+=+debug_info +export_all + EFLAGS+=+debug_info endif ERLSHLIBS = ../stringprep_drv.so diff --git a/src/stringprep/stringprep.erl b/src/stringprep/stringprep.erl index 6b9ec2be..1b39693c 100644 --- a/src/stringprep/stringprep.erl +++ b/src/stringprep/stringprep.erl @@ -25,89 +25,81 @@ %%%---------------------------------------------------------------------- -module(stringprep). + -author('alexey@process-one.net'). -behaviour(gen_server). --export([start/0, start_link/0, - tolower/1, - nameprep/1, - nodeprep/1, - resourceprep/1]). +-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]). +-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, [], []). + 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 + 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_call(_, _, State) -> {noreply, State}. -handle_cast(_, 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}. +handle_info(_, State) -> {noreply, State}. + +code_change(_OldVsn, State, _Extra) -> {ok, State}. -code_change(_OldVsn, State, _Extra) -> - {ok, State}. +terminate(_Reason, Port) -> Port ! {self, close}, ok. -terminate(_Reason, Port) -> - Port ! {self, close}, - ok. +-spec tolower(binary()) -> binary() | error. +tolower(String) -> control(0, String). +-spec nameprep(binary()) -> binary() | error. -tolower(String) -> - control(0, String). +nameprep(String) -> control(?NAMEPREP_COMMAND, String). -nameprep(String) -> - control(?NAMEPREP_COMMAND, String). +-spec nodeprep(binary()) -> binary() | error. -nodeprep(String) -> - control(?NODEPREP_COMMAND, String). +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 | _] -> error; - [1 | Res] -> Res + <<0, _/binary>> -> error; + <<1, Res/binary>> -> Res end. - - - diff --git a/src/stringprep/stringprep_drv.c b/src/stringprep/stringprep_drv.c index 569b4521..cada9237 100644 --- a/src/stringprep/stringprep_drv.c +++ b/src/stringprep/stringprep_drv.c @@ -50,7 +50,7 @@ static ErlDrvData stringprep_erl_start(ErlDrvPort port, char *buff) stringprep_data* d = (stringprep_data*)driver_alloc(sizeof(stringprep_data)); d->port = port; - //set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY); + set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY); return (ErlDrvData)d; } @@ -147,36 +147,36 @@ static int compose(int ch1, int ch2) if (ruc <= 0x7F) { \ if (pos >= size) { \ size = 2*size + 1; \ - rstring = driver_realloc(rstring, size); \ + rstring = driver_realloc_binary(rstring, size); \ } \ - rstring[pos] = (char) ruc; \ + rstring->orig_bytes[pos] = (char) ruc; \ pos++; \ } else if (ruc <= 0x7FF) { \ if (pos + 1 >= size) { \ size = 2*size + 2; \ - rstring = driver_realloc(rstring, size); \ + rstring = driver_realloc_binary(rstring, size); \ } \ - rstring[pos] = (char) ((ruc >> 6) | 0xC0); \ - rstring[pos+1] = (char) ((ruc | 0x80) & 0xBF); \ + rstring->orig_bytes[pos] = (char) ((ruc >> 6) | 0xC0); \ + rstring->orig_bytes[pos+1] = (char) ((ruc | 0x80) & 0xBF); \ pos += 2; \ } else if (ruc <= 0xFFFF) { \ if (pos + 2 >= size) { \ size = 2*size + 3; \ - rstring = driver_realloc(rstring, size); \ + rstring = driver_realloc_binary(rstring, size); \ } \ - rstring[pos] = (char) ((ruc >> 12) | 0xE0); \ - rstring[pos+1] = (char) (((ruc >> 6) | 0x80) & 0xBF); \ - rstring[pos+2] = (char) ((ruc | 0x80) & 0xBF); \ + rstring->orig_bytes[pos] = (char) ((ruc >> 12) | 0xE0); \ + rstring->orig_bytes[pos+1] = (char) (((ruc >> 6) | 0x80) & 0xBF); \ + rstring->orig_bytes[pos+2] = (char) ((ruc | 0x80) & 0xBF); \ pos += 3; \ } else if (ruc <= 0x1FFFFF) { \ if (pos + 3 >= size) { \ size = 2*size + 4; \ - rstring = driver_realloc(rstring, size); \ + rstring = driver_realloc_binary(rstring, size); \ } \ - rstring[pos] = (char) ((ruc >> 18) | 0xF0); \ - rstring[pos+1] = (char) (((ruc >> 12) | 0x80) & 0xBF); \ - rstring[pos+2] = (char) (((ruc >> 6) | 0x80) & 0xBF); \ - rstring[pos+3] = (char) ((ruc | 0x80) & 0xBF); \ + rstring->orig_bytes[pos] = (char) ((ruc >> 18) | 0xF0); \ + rstring->orig_bytes[pos+1] = (char) (((ruc >> 12) | 0x80) & 0xBF); \ + rstring->orig_bytes[pos+2] = (char) (((ruc >> 6) | 0x80) & 0xBF); \ + rstring->orig_bytes[pos+3] = (char) ((ruc | 0x80) & 0xBF); \ pos += 4; \ } @@ -216,7 +216,7 @@ static ErlDrvSSizeT stringprep_erl_control(ErlDrvData drv_data, int size; int info; int prohibit = 0, tolower = 0; - char *rstring; + ErlDrvBinary *rstring; int *mc; int *str32; int str32len, str32pos = 0; @@ -228,8 +228,8 @@ static ErlDrvSSizeT stringprep_erl_control(ErlDrvData drv_data, size = len + 1; - rstring = driver_alloc(size); - rstring[0] = 0; + rstring = driver_alloc_binary(size); + rstring->orig_bytes[0] = 0; str32len = len + 1; @@ -302,7 +302,7 @@ static ErlDrvSSizeT stringprep_erl_control(ErlDrvData drv_data, } if (bad) { - *rbuf = rstring; + *rbuf = (char *)rstring; driver_free(str32); return 1; } @@ -331,8 +331,8 @@ static ErlDrvSSizeT stringprep_erl_control(ErlDrvData drv_data, } if (str32pos == 0) { - rstring[0] = 1; - *rbuf = rstring; + rstring->orig_bytes[0] = 1; + *rbuf = (char *)rstring; driver_free(str32); return 1; } @@ -373,7 +373,7 @@ static ErlDrvSSizeT stringprep_erl_control(ErlDrvData drv_data, ruc = str32[i]; info = GetUniCharInfo(ruc); if (info & prohibit) { - *rbuf = rstring; + *rbuf = (char *)rstring; driver_free(str32); return 1; } @@ -384,13 +384,13 @@ static ErlDrvSSizeT stringprep_erl_control(ErlDrvData drv_data, } if (have_ral && (!first_ral || !last_ral || have_l)) { - *rbuf = rstring; + *rbuf = (char *)rstring; driver_free(str32); return 1; } - rstring[0] = 1; - *rbuf = rstring; + rstring->orig_bytes[0] = 1; + *rbuf = (char *)rstring; driver_free(str32); return pos; diff --git a/src/stringprep/stringprep_sup.erl b/src/stringprep/stringprep_sup.erl index 373aa10d..828f8549 100644 --- a/src/stringprep/stringprep_sup.erl +++ b/src/stringprep/stringprep_sup.erl @@ -59,10 +59,6 @@ start_link() -> %% specifications. %%-------------------------------------------------------------------- init([]) -> - StringPrep = {stringprep, - {stringprep, start_link, []}, - permanent, - brutal_kill, - worker, - [stringprep]}, - {ok,{{one_for_all,10,1}, [StringPrep]}}. + StringPrep = {stringprep, {stringprep, start_link, []}, + permanent, brutal_kill, worker, [stringprep]}, + {ok, {{one_for_all, 10, 1}, [StringPrep]}}. |