From 8649b9257876d1b016746126c95c9e803a680e5d Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Fri, 22 Jul 2011 16:55:33 +0200 Subject: Extract generic is_nif_loadable function from decode --- lib/asn1/src/asn1rt_ber_bin_v2.erl | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/asn1/src/asn1rt_ber_bin_v2.erl b/lib/asn1/src/asn1rt_ber_bin_v2.erl index 2df289e2b8..4c75b5918f 100644 --- a/lib/asn1/src/asn1rt_ber_bin_v2.erl +++ b/lib/asn1/src/asn1rt_ber_bin_v2.erl @@ -54,6 +54,8 @@ -export([decode_primitive_incomplete/2,decode_selective/2]). +-compile(export_all). + -include("asn1_records.hrl"). % the encoding of class of tag bits 8 and 7 @@ -164,28 +166,36 @@ decode(B) -> %% asn1-1.7 decode(B, nif) -> - case application:get_env(asn1, nif_loadable) of - {ok, true} -> + case is_nif_loadable() of + true -> case asn1rt_nif:decode_ber_tlv(B) of {error, Reason} -> handle_error(Reason, B); Else -> Else end; - {ok, false} -> - decode(B); - undefined -> - case catch code:load_file(asn1rt_nif) of - {module, asn1rt_nif} -> - application:set_env(asn1, nif_loadable, true); - _Else -> - application:set_env(asn1, nif_loadable, false) - end, - decode(B, nif) + false -> + decode(B) end; decode(B,erlang) when is_binary(B) -> decode_primitive(B); decode(Tlv,erlang) -> {Tlv,<<>>}. +%% Have to check this since asn1 is not guaranteed to be available +is_nif_loadable() -> + case application:get_env(asn1, nif_loadable) of + {ok,R} -> + R; + undefined -> + case catch code:load_file(asn1rt_nif) of + {module, asn1rt_nif} -> + application:set_env(asn1, nif_loadable, true), + true; + _Else -> + application:set_env(asn1, nif_loadable, false), + false + end + end. + handle_error([],_)-> exit({error,{asn1,{"memory allocation problem"}}}); handle_error({$1,_},L) -> % error in nif -- cgit v1.2.3