1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2002-2017. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20%% 21-module(asn1rt_nif). 22 23%% Nif interface for asn1 24 25-export([encode_per_complete/1, 26 decode_ber_tlv/1, 27 encode_ber_tlv/1]). 28 29-compile(no_native). 30-on_load(load_nif/0). 31 32-define(ASN1_NIF_VSN,1). 33 34load_nif() -> 35 LibBaseName = "asn1rt_nif", 36 PrivDir = code:priv_dir(asn1), 37 LibName = case erlang:system_info(build_type) of 38 opt -> 39 LibBaseName; 40 Type -> 41 LibTypeName = LibBaseName ++ "." ++ atom_to_list(Type), 42 case (filelib:wildcard( 43 filename:join( 44 [PrivDir, 45 "lib", 46 LibTypeName ++ "*"])) /= []) orelse 47 (filelib:wildcard( 48 filename:join( 49 [PrivDir, 50 "lib", 51 erlang:system_info(system_architecture), 52 LibTypeName ++ "*"])) /= []) of 53 true -> LibTypeName; 54 false -> LibBaseName 55 end 56 end, 57 Lib = filename:join([PrivDir, "lib", LibName]), 58 Status = case erlang:load_nif(Lib, ?ASN1_NIF_VSN) of 59 ok -> ok; 60 {error, {load_failed, _}}=Error1 -> 61 ArchLibDir = 62 filename:join([PrivDir, "lib", 63 erlang:system_info(system_architecture)]), 64 Candidate = 65 filelib:wildcard(filename:join([ArchLibDir,LibName ++ "*" ])), 66 case Candidate of 67 [] -> Error1; 68 _ -> 69 ArchLib = filename:join([ArchLibDir, LibName]), 70 erlang:load_nif(ArchLib, ?ASN1_NIF_VSN) 71 end; 72 Error1 -> Error1 73 end, 74 case Status of 75 ok -> ok; 76 {error, {E, Str}} -> 77 error_logger:error_msg("Unable to load asn1 nif library. " 78 "Failed with error:~n\"~p, ~s\"~n",[E,Str]), 79 Status 80 end. 81 82decode_ber_tlv(Binary) -> 83 case decode_ber_tlv_raw(Binary) of 84 {error,Reason} -> 85 exit({error,{asn1,Reason}}); 86 Other -> 87 Other 88 end. 89 90encode_per_complete(TagValueList) -> 91 case encode_per_complete_raw(TagValueList) of 92 {error,Reason} -> handle_error(Reason, TagValueList); 93 Other when is_binary(Other) -> Other 94 end. 95 96handle_error([], _)-> 97 exit({error,{asn1,enomem}}); 98handle_error($1, L) -> % error in complete in driver 99 exit({error,{asn1,L}}); 100handle_error(ErrL, L) -> 101 exit({error,{asn1,ErrL,L}}). 102 103encode_per_complete_raw(_TagValueList) -> 104 erlang:nif_error({nif_not_loaded,module,?MODULE,line,?LINE}). 105 106decode_ber_tlv_raw(_Binary) -> 107 erlang:nif_error({nif_not_loaded,module,?MODULE,line,?LINE}). 108 109encode_ber_tlv(_TagValueList) -> 110 erlang:nif_error({nif_not_loaded,module,?MODULE,line,?LINE}). 111