%% Generated by the Erlang ASN.1 BER_V2-compiler version, utilizing bit-syntax:2.0.1
%% Purpose: encoder and decoder to the types in mod PKCS-FRAME

-module('PKCS-FRAME').
-compile(nowarn_unused_vars).
-include("PKCS-FRAME.hrl").
-asn1_info([{vsn,'2.0.1'},
            {module,'PKCS-FRAME'},
            {options,[warnings,ber,errors,
 {cwd,"/net/isildur/ldisk/daily_build/r16b_prebuild_master-opu_o.2013-02-25_20/otp_src_R16B/lib/public_key/asn1"},
 {outdir,"/net/isildur/ldisk/daily_build/r16b_prebuild_master-opu_o.2013-02-25_20/otp_src_R16B/lib/public_key/asn1"},
 asn1config,noobj,compact_bit_string,der,
 {i,"."},
 {i,"/net/isildur/ldisk/daily_build/r16b_prebuild_master-opu_o.2013-02-25_20/otp_src_R16B/lib/public_key/asn1"}]}]).

-export([encoding_rule/0,bit_string_format/0]).
-export([
'enc_RC5-CBC-Parameters'/2,
'enc_RC2-CBC-Parameter'/2,
'enc_PBMAC1-params'/2,
'enc_PBES2-params'/2,
'enc_PBEParameter'/2,
'enc_PBKDF2-params'/2,
'enc_AttributeUsage'/2,
'enc_Context'/2,
'enc_EncryptedData'/2,
'enc_EncryptedPrivateKeyInfo'/2,
'enc_Attributes'/2,
'enc_PrivateKey'/2,
'enc_Version'/2,
'enc_PrivateKeyInfo'/2
]).

-export([
'dec_RC5-CBC-Parameters'/2,
'dec_RC2-CBC-Parameter'/2,
'dec_PBMAC1-params'/2,
'dec_PBES2-params'/2,
'dec_PBEParameter'/2,
'dec_PBKDF2-params'/2,
'dec_AttributeUsage'/2,
'dec_Context'/2,
'dec_EncryptedData'/2,
'dec_EncryptedPrivateKeyInfo'/2,
'dec_Attributes'/2,
'dec_PrivateKey'/2,
'dec_Version'/2,
'dec_PrivateKeyInfo'/2
]).

-export([
'rc5-CBC-PAD'/0,
'rc2CBC'/0,
'des-EDE3-CBC'/0,
'desCBC'/0,
'id-hmacWithSHA1'/0,
'encryptionAlgorithm'/0,
'digestAlgorithm'/0,
'id-PBMAC1'/0,
'id-PBES2'/0,
'pbeWithSHA1AndRC2-CBC'/0,
'pbeWithSHA1AndDES-CBC'/0,
'pbeWithMD5AndRC2-CBC'/0,
'pbeWithMD5AndDES-CBC'/0,
'pbeWithMD2AndRC2-CBC'/0,
'pbeWithMD2AndDES-CBC'/0,
'id-PBKDF2'/0,
'pkcs-5'/0,
'pkcs'/0,
'rsadsi'/0
]).

-export([
'getenc_SupportingAlgorithms'/2,
'getenc_PBMAC1-MACs'/2,
'getenc_PBMAC1-KDFs'/2,
'getenc_PBMAC1Algorithms'/2,
'getenc_PBES2-Encs'/2,
'getenc_PBES2-KDFs'/2,
'getenc_PBES2Algorithms'/2,
'getenc_PBES1Algorithms'/2,
'getenc_PBKDF2-PRFs'/2,
'getenc_PBKDF2-SaltSources'/2,
'getenc_SupportedContexts'/2,
'getenc_KeyEncryptionAlgorithms'/2,
'getenc_PrivateKeyAlgorithms'/2,
'getenc_internal_object_set_argument_9'/2,
'getenc_internal_object_set_argument_8'/2,
'getenc_internal_object_set_argument_7'/2,
'getenc_internal_object_set_argument_6'/2,
'getenc_internal_object_set_argument_5'/2,
'getenc_internal_object_set_argument_4'/2,
'getenc_internal_object_set_argument_3'/2,
'getenc_internal_object_set_argument_2'/2,
'getenc_internal_object_set_argument_1'/2
]).

-export([
'getdec_SupportingAlgorithms'/2,
'getdec_PBMAC1-MACs'/2,
'getdec_PBMAC1-KDFs'/2,
'getdec_PBMAC1Algorithms'/2,
'getdec_PBES2-Encs'/2,
'getdec_PBES2-KDFs'/2,
'getdec_PBES2Algorithms'/2,
'getdec_PBES1Algorithms'/2,
'getdec_PBKDF2-PRFs'/2,
'getdec_PBKDF2-SaltSources'/2,
'getdec_SupportedContexts'/2,
'getdec_KeyEncryptionAlgorithms'/2,
'getdec_PrivateKeyAlgorithms'/2,
'getdec_internal_object_set_argument_9'/2,
'getdec_internal_object_set_argument_8'/2,
'getdec_internal_object_set_argument_7'/2,
'getdec_internal_object_set_argument_6'/2,
'getdec_internal_object_set_argument_5'/2,
'getdec_internal_object_set_argument_4'/2,
'getdec_internal_object_set_argument_3'/2,
'getdec_internal_object_set_argument_2'/2,
'getdec_internal_object_set_argument_1'/2
]).

-export([info/0]).


-export([encode/2,decode/2]).

encoding_rule() -> ber.

bit_string_format() -> compact.

encode(Type,Data) ->
case catch encode_disp(Type,Data) of
  {'EXIT',{error,Reason}} ->
    {error,Reason};
  {'EXIT',Reason} ->
    {error,{asn1,Reason}};
  {Bytes,_Len} ->
    {ok,iolist_to_binary(Bytes)};
  Bytes ->
    {ok,iolist_to_binary(Bytes)}
end.

decode(Type,Data) ->
case catch decode_disp(Type,element(1, ber_decode_nif(Data))) of
  {'EXIT',{error,Reason}} ->
    {error,Reason};
  {'EXIT',Reason} ->
    {error,{asn1,Reason}};
  Result ->
    {ok,Result}
end.

encode_disp('RC5-CBC-Parameters',Data) -> 'enc_RC5-CBC-Parameters'(Data);
encode_disp('RC2-CBC-Parameter',Data) -> 'enc_RC2-CBC-Parameter'(Data);
encode_disp('PBMAC1-params',Data) -> 'enc_PBMAC1-params'(Data);
encode_disp('PBES2-params',Data) -> 'enc_PBES2-params'(Data);
encode_disp('PBEParameter',Data) -> 'enc_PBEParameter'(Data);
encode_disp('PBKDF2-params',Data) -> 'enc_PBKDF2-params'(Data);
encode_disp('AttributeUsage',Data) -> 'enc_AttributeUsage'(Data);
encode_disp('Context',Data) -> 'enc_Context'(Data);
encode_disp('EncryptedData',Data) -> 'enc_EncryptedData'(Data);
encode_disp('EncryptedPrivateKeyInfo',Data) -> 'enc_EncryptedPrivateKeyInfo'(Data);
encode_disp('Attributes',Data) -> 'enc_Attributes'(Data);
encode_disp('PrivateKey',Data) -> 'enc_PrivateKey'(Data);
encode_disp('Version',Data) -> 'enc_Version'(Data);
encode_disp('PrivateKeyInfo',Data) -> 'enc_PrivateKeyInfo'(Data);
encode_disp(Type,_Data) -> exit({error,{asn1,{undefined_type,Type}}}).


decode_disp('RC5-CBC-Parameters',Data) -> 'dec_RC5-CBC-Parameters'(Data);
decode_disp('RC2-CBC-Parameter',Data) -> 'dec_RC2-CBC-Parameter'(Data);
decode_disp('PBMAC1-params',Data) -> 'dec_PBMAC1-params'(Data);
decode_disp('PBES2-params',Data) -> 'dec_PBES2-params'(Data);
decode_disp('PBEParameter',Data) -> 'dec_PBEParameter'(Data);
decode_disp('PBKDF2-params',Data) -> 'dec_PBKDF2-params'(Data);
decode_disp('AttributeUsage',Data) -> 'dec_AttributeUsage'(Data);
decode_disp('Context',Data) -> 'dec_Context'(Data);
decode_disp('EncryptedData',Data) -> 'dec_EncryptedData'(Data);
decode_disp('EncryptedPrivateKeyInfo',Data) -> 'dec_EncryptedPrivateKeyInfo'(Data);
decode_disp('Attributes',Data) -> 'dec_Attributes'(Data);
decode_disp('PrivateKey',Data) -> 'dec_PrivateKey'(Data);
decode_disp('Version',Data) -> 'dec_Version'(Data);
decode_disp('PrivateKeyInfo',Data) -> 'dec_PrivateKeyInfo'(Data);
decode_disp(Type,_Data) -> exit({error,{asn1,{undefined_type,Type}}}).





info() ->
   case ?MODULE:module_info(attributes) of
     Attributes when is_list(Attributes) ->
       case lists:keyfind(asn1_info, 1, Attributes) of
         {_,Info} when is_list(Info) ->
           Info;
         _ ->
           []
       end;
     _ ->
       []
   end.


%%================================
%%  RC5-CBC-Parameters
%%================================
'enc_RC5-CBC-Parameters'(Val) ->
    'enc_RC5-CBC-Parameters'(Val, [<<48>>]).

'enc_RC5-CBC-Parameters'(Val, TagIn) ->
{_,Cindex1, Cindex2, Cindex3, Cindex4} = Val,

%%-------------------------------------------------
%% attribute version(1) with type INTEGER
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_integer(Cindex1, [{'v1-0',16}], [<<2>>]),

%%-------------------------------------------------
%% attribute rounds(2) with type INTEGER
%%-------------------------------------------------
   {EncBytes2,EncLen2} = encode_integer(Cindex2, [<<2>>]),

%%-------------------------------------------------
%% attribute blockSizeInBits(3) with type INTEGER
%%-------------------------------------------------
   {EncBytes3,EncLen3} = encode_integer(Cindex3, [<<2>>]),

%%-------------------------------------------------
%% attribute iv(4) with type OCTET STRING OPTIONAL
%%-------------------------------------------------
   {EncBytes4,EncLen4} =  case Cindex4 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            encode_restricted_string(Cindex4, [<<4>>])
       end,

   BytesSoFar = [EncBytes1, EncBytes2, EncBytes3, EncBytes4],
LenSoFar = EncLen1 + EncLen2 + EncLen3 + EncLen4,
encode_tags(TagIn, BytesSoFar, LenSoFar).


'dec_RC5-CBC-Parameters'(Tlv) ->
   'dec_RC5-CBC-Parameters'(Tlv, [16]).

'dec_RC5-CBC-Parameters'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute version(1) with type INTEGER
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_integer(V1,[],[{'v1-0',16}],[2]),

%%-------------------------------------------------
%% attribute rounds(2) with type INTEGER
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = decode_integer(V2,{8,127},[2]),

%%-------------------------------------------------
%% attribute blockSizeInBits(3) with type INTEGER
%%-------------------------------------------------
[V3|Tlv4] = Tlv3, 
Term3 = decode_integer(V3,[64,128],[2]),

%%-------------------------------------------------
%% attribute iv(4) with type OCTET STRING OPTIONAL
%%-------------------------------------------------
{Term4,Tlv5} = case Tlv4 of
[{4,V4}|TempTlv5] ->
    {decode_restricted_string(V4,[]), TempTlv5};
    _ ->
        { asn1_NOVALUE, Tlv4}
end,

case Tlv5 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv5}}}) % extra fields not allowed
end,
   {'RC5-CBC-Parameters', Term1, Term2, Term3, Term4}.



%%================================
%%  RC2-CBC-Parameter
%%================================
'enc_RC2-CBC-Parameter'(Val) ->
    'enc_RC2-CBC-Parameter'(Val, [<<48>>]).

'enc_RC2-CBC-Parameter'(Val, TagIn) ->
{_,Cindex1, Cindex2} = Val,

%%-------------------------------------------------
%% attribute rc2ParameterVersion(1) with type INTEGER OPTIONAL
%%-------------------------------------------------
   {EncBytes1,EncLen1} =  case Cindex1 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            encode_integer(Cindex1, [<<2>>])
       end,

%%-------------------------------------------------
%% attribute iv(2) with type OCTET STRING
%%-------------------------------------------------
   {EncBytes2,EncLen2} = encode_restricted_string(Cindex2, [<<4>>]),

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).


'dec_RC2-CBC-Parameter'(Tlv) ->
   'dec_RC2-CBC-Parameter'(Tlv, [16]).

'dec_RC2-CBC-Parameter'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute rc2ParameterVersion(1) with type INTEGER OPTIONAL
%%-------------------------------------------------
{Term1,Tlv2} = case Tlv1 of
[{2,V1}|TempTlv2] ->
    {decode_integer(V1,[],[]), TempTlv2};
    _ ->
        { asn1_NOVALUE, Tlv1}
end,

%%-------------------------------------------------
%% attribute iv(2) with type OCTET STRING
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = decode_restricted_string(V2,8,[4]),

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'RC2-CBC-Parameter', Term1, Term2}.



%%================================
%%  PBMAC1-params
%%================================
'enc_PBMAC1-params'(Val) ->
    'enc_PBMAC1-params'(Val, [<<48>>]).

'enc_PBMAC1-params'(Val, TagIn) ->
{_,Cindex1, Cindex2} = Val,

%%-------------------------------------------------
%% attribute keyDerivationFunc(1) with type SEQUENCE
%%-------------------------------------------------
   {EncBytes1,EncLen1} = 'enc_PBMAC1-params_keyDerivationFunc'(Cindex1, [<<48>>]),

%%-------------------------------------------------
%% attribute messageAuthScheme(2) with type SEQUENCE
%%-------------------------------------------------
   {EncBytes2,EncLen2} = 'enc_PBMAC1-params_messageAuthScheme'(Cindex2, [<<48>>]),

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).



%%================================
%%  PBMAC1-params_keyDerivationFunc
%%================================
'enc_PBMAC1-params_keyDerivationFunc'(Val, TagIn) ->
   {_,Cindex1, Cindex2} = Val,
Objalgorithm = 
   'PKCS-FRAME':'getenc_internal_object_set_argument_1'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
   {EncBytes2,EncLen2} =  case Cindex2 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            {TmpBytes,_ } = Objalgorithm('Type', Cindex2, []),
   {TmpBytes1,TmpLen} = encode_open_type(TmpBytes, []),
   {TmpBytes1, TmpLen}
       end,

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).
'dec_PBMAC1-params_keyDerivationFunc'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
{Tmpterm1,Tlv3} = case Tlv2 of
[V2|TempTlv3] ->
    {decode_open_type(V2, []), TempTlv3};
    _ ->
        { asn1_NOVALUE, Tlv2}
end,

DecObjalgorithmTerm1 =
   'PKCS-FRAME':'getdec_internal_object_set_argument_1'(id, Term1),
Term2 = 
   case Tmpterm1 of
      asn1_NOVALUE ->asn1_NOVALUE;
      _ ->
         case (catch DecObjalgorithmTerm1('Type', Tmpterm1, [])) of
            {'EXIT', Reason1} ->
               exit({'Type not compatible with table constraint',Reason1});
            Tmpterm2 ->
               Tmpterm2
         end
   end,

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PBMAC1-params_keyDerivationFunc', Term1, Term2}.




%%================================
%%  PBMAC1-params_messageAuthScheme
%%================================
'enc_PBMAC1-params_messageAuthScheme'(Val, TagIn) ->
   {_,Cindex1, Cindex2} = Val,
Objalgorithm = 
   'PKCS-FRAME':'getenc_internal_object_set_argument_2'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
   {EncBytes2,EncLen2} =  case Cindex2 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            {TmpBytes,_ } = Objalgorithm('Type', Cindex2, []),
   {TmpBytes1,TmpLen} = encode_open_type(TmpBytes, []),
   {TmpBytes1, TmpLen}
       end,

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).
'dec_PBMAC1-params_messageAuthScheme'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
{Tmpterm1,Tlv3} = case Tlv2 of
[V2|TempTlv3] ->
    {decode_open_type(V2, []), TempTlv3};
    _ ->
        { asn1_NOVALUE, Tlv2}
end,

DecObjalgorithmTerm1 =
   'PKCS-FRAME':'getdec_internal_object_set_argument_2'(id, Term1),
Term2 = 
   case Tmpterm1 of
      asn1_NOVALUE ->asn1_NOVALUE;
      _ ->
         case (catch DecObjalgorithmTerm1('Type', Tmpterm1, [])) of
            {'EXIT', Reason1} ->
               exit({'Type not compatible with table constraint',Reason1});
            Tmpterm2 ->
               Tmpterm2
         end
   end,

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PBMAC1-params_messageAuthScheme', Term1, Term2}.



'dec_PBMAC1-params'(Tlv) ->
   'dec_PBMAC1-params'(Tlv, [16]).

'dec_PBMAC1-params'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute keyDerivationFunc(1) with type SEQUENCE
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = 'dec_PBMAC1-params_keyDerivationFunc'(V1, [16]),

%%-------------------------------------------------
%% attribute messageAuthScheme(2) with type SEQUENCE
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = 'dec_PBMAC1-params_messageAuthScheme'(V2, [16]),

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PBMAC1-params', Term1, Term2}.



%%================================
%%  PBES2-params
%%================================
'enc_PBES2-params'(Val) ->
    'enc_PBES2-params'(Val, [<<48>>]).

'enc_PBES2-params'(Val, TagIn) ->
{_,Cindex1, Cindex2} = Val,

%%-------------------------------------------------
%% attribute keyDerivationFunc(1) with type SEQUENCE
%%-------------------------------------------------
   {EncBytes1,EncLen1} = 'enc_PBES2-params_keyDerivationFunc'(Cindex1, [<<48>>]),

%%-------------------------------------------------
%% attribute encryptionScheme(2) with type SEQUENCE
%%-------------------------------------------------
   {EncBytes2,EncLen2} = 'enc_PBES2-params_encryptionScheme'(Cindex2, [<<48>>]),

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).



%%================================
%%  PBES2-params_keyDerivationFunc
%%================================
'enc_PBES2-params_keyDerivationFunc'(Val, TagIn) ->
   {_,Cindex1, Cindex2} = Val,
Objalgorithm = 
   'PKCS-FRAME':'getenc_internal_object_set_argument_3'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
   {EncBytes2,EncLen2} =  case Cindex2 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            {TmpBytes,_ } = Objalgorithm('Type', Cindex2, []),
   {TmpBytes1,TmpLen} = encode_open_type(TmpBytes, []),
   {TmpBytes1, TmpLen}
       end,

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).
'dec_PBES2-params_keyDerivationFunc'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
{Tmpterm1,Tlv3} = case Tlv2 of
[V2|TempTlv3] ->
    {decode_open_type(V2, []), TempTlv3};
    _ ->
        { asn1_NOVALUE, Tlv2}
end,

DecObjalgorithmTerm1 =
   'PKCS-FRAME':'getdec_internal_object_set_argument_3'(id, Term1),
Term2 = 
   case Tmpterm1 of
      asn1_NOVALUE ->asn1_NOVALUE;
      _ ->
         case (catch DecObjalgorithmTerm1('Type', Tmpterm1, [])) of
            {'EXIT', Reason1} ->
               exit({'Type not compatible with table constraint',Reason1});
            Tmpterm2 ->
               Tmpterm2
         end
   end,

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PBES2-params_keyDerivationFunc', Term1, Term2}.




%%================================
%%  PBES2-params_encryptionScheme
%%================================
'enc_PBES2-params_encryptionScheme'(Val, TagIn) ->
   {_,Cindex1, Cindex2} = Val,
Objalgorithm = 
   'PKCS-FRAME':'getenc_internal_object_set_argument_4'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
   {EncBytes2,EncLen2} =  case Cindex2 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            {TmpBytes,_ } = Objalgorithm('Type', Cindex2, []),
   {TmpBytes1,TmpLen} = encode_open_type(TmpBytes, []),
   {TmpBytes1, TmpLen}
       end,

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).
'dec_PBES2-params_encryptionScheme'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
{Tmpterm1,Tlv3} = case Tlv2 of
[V2|TempTlv3] ->
    {decode_open_type(V2, []), TempTlv3};
    _ ->
        { asn1_NOVALUE, Tlv2}
end,

DecObjalgorithmTerm1 =
   'PKCS-FRAME':'getdec_internal_object_set_argument_4'(id, Term1),
Term2 = 
   case Tmpterm1 of
      asn1_NOVALUE ->asn1_NOVALUE;
      _ ->
         case (catch DecObjalgorithmTerm1('Type', Tmpterm1, [])) of
            {'EXIT', Reason1} ->
               exit({'Type not compatible with table constraint',Reason1});
            Tmpterm2 ->
               Tmpterm2
         end
   end,

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PBES2-params_encryptionScheme', Term1, Term2}.



'dec_PBES2-params'(Tlv) ->
   'dec_PBES2-params'(Tlv, [16]).

'dec_PBES2-params'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute keyDerivationFunc(1) with type SEQUENCE
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = 'dec_PBES2-params_keyDerivationFunc'(V1, [16]),

%%-------------------------------------------------
%% attribute encryptionScheme(2) with type SEQUENCE
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = 'dec_PBES2-params_encryptionScheme'(V2, [16]),

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PBES2-params', Term1, Term2}.



%%================================
%%  PBEParameter
%%================================
'enc_PBEParameter'(Val) ->
    'enc_PBEParameter'(Val, [<<48>>]).

'enc_PBEParameter'(Val, TagIn) ->
{_,Cindex1, Cindex2} = Val,

%%-------------------------------------------------
%% attribute salt(1) with type OCTET STRING
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_restricted_string(Cindex1, [<<4>>]),

%%-------------------------------------------------
%% attribute iterationCount(2) with type INTEGER
%%-------------------------------------------------
   {EncBytes2,EncLen2} = encode_integer(Cindex2, [<<2>>]),

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).


'dec_PBEParameter'(Tlv) ->
   'dec_PBEParameter'(Tlv, [16]).

'dec_PBEParameter'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute salt(1) with type OCTET STRING
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_restricted_string(V1,8,[4]),

%%-------------------------------------------------
%% attribute iterationCount(2) with type INTEGER
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = decode_integer(V2,[],[2]),

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PBEParameter', Term1, Term2}.



%%================================
%%  PBKDF2-params
%%================================
'enc_PBKDF2-params'(Val) ->
    'enc_PBKDF2-params'(Val, [<<48>>]).

'enc_PBKDF2-params'(Val, TagIn) ->
{_,Cindex1, Cindex2, Cindex3, Cindex4} = Val,

%%-------------------------------------------------
%% attribute salt(1) with type CHOICE
%%-------------------------------------------------
   {EncBytes1,EncLen1} = 'enc_PBKDF2-params_salt'(Cindex1, []),

%%-------------------------------------------------
%% attribute iterationCount(2) with type INTEGER
%%-------------------------------------------------
   {EncBytes2,EncLen2} = encode_integer(Cindex2, [<<2>>]),

%%-------------------------------------------------
%% attribute keyLength(3) with type INTEGER OPTIONAL
%%-------------------------------------------------
   {EncBytes3,EncLen3} =  case Cindex3 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            encode_integer(Cindex3, [<<2>>])
       end,

%%-------------------------------------------------
%% attribute prf(4) with type SEQUENCE DEFAULT = {'PBKDF2-params_prf',{'Externalvaluereference',47,'PKCS5v2-0','id-hmacWithSHA1'},'NULL'}
%%-------------------------------------------------
   {EncBytes4,EncLen4} =  case catch 'check_PBKDF2-params_prf'({'PBKDF2-params_prf',{'Externalvaluereference',47,'PKCS5v2-0','id-hmacWithSHA1'},'NULL'}, Cindex4) of
            true -> {[],0};
         _ ->
            'enc_PBKDF2-params_prf'(Cindex4, [<<48>>])
       end,

   BytesSoFar = [EncBytes1, EncBytes2, EncBytes3, EncBytes4],
LenSoFar = EncLen1 + EncLen2 + EncLen3 + EncLen4,
encode_tags(TagIn, BytesSoFar, LenSoFar).



%%================================
%%  PBKDF2-params_salt
%%================================
'enc_PBKDF2-params_salt'(Val, TagIn) ->
      {EncBytes,EncLen} = case element(1,Val) of
      specified ->
         encode_restricted_string(element(2,Val), [<<4>>]);
      otherSource ->
         'enc_PBKDF2-params_salt_otherSource'(element(2,Val), [<<48>>]);
      Else -> 
         exit({error,{asn1,{invalid_choice_type,Else}}})
   end,

encode_tags(TagIn, EncBytes, EncLen).





%%================================
%%  PBKDF2-params_salt_otherSource
%%================================
'enc_PBKDF2-params_salt_otherSource'(Val, TagIn) ->
   {_,Cindex1, Cindex2} = Val,
Objalgorithm = 
   'PKCS-FRAME':'getenc_internal_object_set_argument_5'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
   {EncBytes2,EncLen2} =  case Cindex2 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            {TmpBytes,_ } = Objalgorithm('Type', Cindex2, []),
   {TmpBytes1,TmpLen} = encode_open_type(TmpBytes, []),
   {TmpBytes1, TmpLen}
       end,

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).
'dec_PBKDF2-params_salt_otherSource'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
{Tmpterm1,Tlv3} = case Tlv2 of
[V2|TempTlv3] ->
    {decode_open_type(V2, []), TempTlv3};
    _ ->
        { asn1_NOVALUE, Tlv2}
end,

DecObjalgorithmTerm1 =
   'PKCS-FRAME':'getdec_internal_object_set_argument_5'(id, Term1),
Term2 = 
   case Tmpterm1 of
      asn1_NOVALUE ->asn1_NOVALUE;
      _ ->
         case (catch DecObjalgorithmTerm1('Type', Tmpterm1, [])) of
            {'EXIT', Reason1} ->
               exit({'Type not compatible with table constraint',Reason1});
            Tmpterm2 ->
               Tmpterm2
         end
   end,

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PBKDF2-params_salt_otherSource', Term1, Term2}.

'dec_PBKDF2-params_salt'(Tlv, TagIn) ->
Tlv1 = match_tags(Tlv, TagIn),
case (case Tlv1 of [CtempTlv1] -> CtempTlv1; _ -> Tlv1 end) of

%% 'specified'
    {4, V1} -> 
        {specified, decode_restricted_string(V1,[])};


%% 'otherSource'
    {16, V1} -> 
        {otherSource, 'dec_PBKDF2-params_salt_otherSource'(V1, [])};

      Else -> 
         exit({error,{asn1,{invalid_choice_tag,Else}}})
   end
.



%%================================
%%  PBKDF2-params_prf
%%================================
'enc_PBKDF2-params_prf'(Val, TagIn) ->
   {_,Cindex1, Cindex2} = Val,
Objalgorithm = 
   'PKCS-FRAME':'getenc_internal_object_set_argument_6'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
   {EncBytes2,EncLen2} =  case Cindex2 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            {TmpBytes,_ } = Objalgorithm('Type', Cindex2, []),
   {TmpBytes1,TmpLen} = encode_open_type(TmpBytes, []),
   {TmpBytes1, TmpLen}
       end,

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).
'dec_PBKDF2-params_prf'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
{Tmpterm1,Tlv3} = case Tlv2 of
[V2|TempTlv3] ->
    {decode_open_type(V2, []), TempTlv3};
    _ ->
        { asn1_NOVALUE, Tlv2}
end,

DecObjalgorithmTerm1 =
   'PKCS-FRAME':'getdec_internal_object_set_argument_6'(id, Term1),
Term2 = 
   case Tmpterm1 of
      asn1_NOVALUE ->asn1_NOVALUE;
      _ ->
         case (catch DecObjalgorithmTerm1('Type', Tmpterm1, [])) of
            {'EXIT', Reason1} ->
               exit({'Type not compatible with table constraint',Reason1});
            Tmpterm2 ->
               Tmpterm2
         end
   end,

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PBKDF2-params_prf', Term1, Term2}.



'dec_PBKDF2-params'(Tlv) ->
   'dec_PBKDF2-params'(Tlv, [16]).

'dec_PBKDF2-params'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute salt(1) with type CHOICE
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = 'dec_PBKDF2-params_salt'(V1, []),

%%-------------------------------------------------
%% attribute iterationCount(2) with type INTEGER
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = decode_integer(V2,{1,'MAX'},[2]),

%%-------------------------------------------------
%% attribute keyLength(3) with type INTEGER OPTIONAL
%%-------------------------------------------------
{Term3,Tlv4} = case Tlv3 of
[{2,V3}|TempTlv4] ->
    {decode_integer(V3,{1,'MAX'},[]), TempTlv4};
    _ ->
        { asn1_NOVALUE, Tlv3}
end,

%%-------------------------------------------------
%% attribute prf(4) with type SEQUENCE DEFAULT = {'PBKDF2-params_prf',{'Externalvaluereference',47,'PKCS5v2-0','id-hmacWithSHA1'},'NULL'}
%%-------------------------------------------------
{Term4,Tlv5} = case Tlv4 of
[{16,V4}|TempTlv5] ->
    {'dec_PBKDF2-params_prf'(V4, []), TempTlv5};
    _ ->
        {{'PBKDF2-params_prf',{'Externalvaluereference',47,'PKCS5v2-0','id-hmacWithSHA1'},'NULL'},Tlv4}
end,

case Tlv5 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv5}}}) % extra fields not allowed
end,
   {'PBKDF2-params', Term1, Term2, Term3, Term4}.



%%================================
%%  AttributeUsage
%%================================
'enc_AttributeUsage'(Val) ->
    'enc_AttributeUsage'(Val, [<<10>>]).

'enc_AttributeUsage'(Val, TagIn) ->
case Val of
userApplications -> encode_enumerated(0, TagIn);
directoryOperation -> encode_enumerated(1, TagIn);
distributedOperation -> encode_enumerated(2, TagIn);
dSAOperation -> encode_enumerated(3, TagIn);
Enumval1 -> exit({error,{asn1, {enumerated_not_in_range,Enumval1}}})
end.


'dec_AttributeUsage'(Tlv) ->
   'dec_AttributeUsage'(Tlv, [10]).

'dec_AttributeUsage'(Tlv, TagIn) ->
decode_enumerated(Tlv,[{userApplications,0},{directoryOperation,1},{distributedOperation,2},{dSAOperation,3}],TagIn).



%%================================
%%  Context
%%================================
'enc_Context'(Val) ->
    'enc_Context'(Val, [<<48>>]).

'enc_Context'(Val, TagIn) ->
{_,Cindex1, Cindex2, Cindex3} = Val,
ObjcontextType = 
   'PKCS-FRAME':'getenc_SupportedContexts'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute contextType(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute contextValues(2) with type SET OF
%%-------------------------------------------------
   {EncBytes2,EncLen2} = 'enc_Context_contextValues'(Cindex2, [<<49>>], ObjcontextType),

%%-------------------------------------------------
%% attribute fallback(3) with type BOOLEAN DEFAULT = false
%%-------------------------------------------------
   {EncBytes3,EncLen3} =  case catch check_bool(false, Cindex3) of
            true -> {[],0};
         _ ->
            encode_boolean(Cindex3, [<<1>>])
       end,

   BytesSoFar = [EncBytes1, EncBytes2, EncBytes3],
LenSoFar = EncLen1 + EncLen2 + EncLen3,
encode_tags(TagIn, BytesSoFar, LenSoFar).



%%================================
%%  Context_contextValues
%%================================
'enc_Context_contextValues'(Val, TagIn, ObjFun) ->
      {EncBytes,EncLen} = 'enc_Context_contextValues_components'(Val, ObjFun,[],0),
   encode_tags(TagIn, EncBytes, EncLen).

'enc_Context_contextValues_components'([], _, AccBytes, AccLen) -> 
   {dynamicsort_SETOF(AccBytes),AccLen};

'enc_Context_contextValues_components'([H|T], ObjFun,AccBytes, AccLen) ->
   {TmpBytes1,_} = ObjFun('Type', H, []),
   {EncBytes,EncLen} = encode_open_type(TmpBytes1, [])
,
   'enc_Context_contextValues_components'(T, ObjFun,[EncBytes|AccBytes], AccLen + EncLen).

'dec_Context_contextValues'(Tlv, TagIn, ObjFun) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),
[
      begin
         Tmptlv1 = decode_open_type(V1, []),
         case (catch ObjFun('Type', Tmptlv1, [])) of
            {'EXIT',Reason1} ->
               exit({'Type not compatible with table constraint', Reason1});
            Tmpterm1 ->
               Tmpterm1
         end
      end
 || V1 <- Tlv1].




'dec_Context'(Tlv) ->
   'dec_Context'(Tlv, [16]).

'dec_Context'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute contextType(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),
ObjFun = 'PKCS-FRAME':'getdec_SupportedContexts'(id, Term1),

%%-------------------------------------------------
%% attribute contextValues(2) with type SET OF
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = 'dec_Context_contextValues'(V2, [17], ObjFun),

%%-------------------------------------------------
%% attribute fallback(3) with type BOOLEAN DEFAULT = false
%%-------------------------------------------------
{Term3,Tlv4} = case Tlv3 of
[{1,V3}|TempTlv4] ->
    {decode_boolean(V3,[]), TempTlv4};
    _ ->
        {false,Tlv3}
end,

case Tlv4 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv4}}}) % extra fields not allowed
end,
   {'Context', Term1, Term2, Term3}.



%%================================
%%  EncryptedData
%%================================
'enc_EncryptedData'(Val) ->
    'enc_EncryptedData'(Val, [<<4>>]).

'enc_EncryptedData'(Val, TagIn) ->
encode_restricted_string(Val, TagIn).


'dec_EncryptedData'(Tlv) ->
   'dec_EncryptedData'(Tlv, [4]).

'dec_EncryptedData'(Tlv, TagIn) ->
decode_restricted_string(Tlv,TagIn).



%%================================
%%  EncryptedPrivateKeyInfo
%%================================
'enc_EncryptedPrivateKeyInfo'(Val) ->
    'enc_EncryptedPrivateKeyInfo'(Val, [<<48>>]).

'enc_EncryptedPrivateKeyInfo'(Val, TagIn) ->
{_,Cindex1, Cindex2} = Val,

%%-------------------------------------------------
%% attribute encryptionAlgorithm(1) with type SEQUENCE
%%-------------------------------------------------
   {EncBytes1,EncLen1} = 'enc_EncryptedPrivateKeyInfo_encryptionAlgorithm'(Cindex1, [<<48>>]),

%%-------------------------------------------------
%% attribute encryptedData(2) with type OCTET STRING
%%-------------------------------------------------
   {EncBytes2,EncLen2} = encode_restricted_string(Cindex2, [<<4>>]),

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).



%%================================
%%  EncryptedPrivateKeyInfo_encryptionAlgorithm
%%================================
'enc_EncryptedPrivateKeyInfo_encryptionAlgorithm'(Val, TagIn) ->
   {_,Cindex1, Cindex2} = Val,
Objalgorithm = 
   'PKCS-FRAME':'getenc_internal_object_set_argument_7'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
   {EncBytes2,EncLen2} =  case Cindex2 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            {TmpBytes,_ } = Objalgorithm('Type', Cindex2, []),
   {TmpBytes1,TmpLen} = encode_open_type(TmpBytes, []),
   {TmpBytes1, TmpLen}
       end,

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).
'dec_EncryptedPrivateKeyInfo_encryptionAlgorithm'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
{Tmpterm1,Tlv3} = case Tlv2 of
[V2|TempTlv3] ->
    {decode_open_type(V2, []), TempTlv3};
    _ ->
        { asn1_NOVALUE, Tlv2}
end,

DecObjalgorithmTerm1 =
   'PKCS-FRAME':'getdec_internal_object_set_argument_7'(id, Term1),
Term2 = 
   case Tmpterm1 of
      asn1_NOVALUE ->asn1_NOVALUE;
      _ ->
         case (catch DecObjalgorithmTerm1('Type', Tmpterm1, [])) of
            {'EXIT', Reason1} ->
               exit({'Type not compatible with table constraint',Reason1});
            Tmpterm2 ->
               Tmpterm2
         end
   end,

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'EncryptedPrivateKeyInfo_encryptionAlgorithm', Term1, Term2}.



'dec_EncryptedPrivateKeyInfo'(Tlv) ->
   'dec_EncryptedPrivateKeyInfo'(Tlv, [16]).

'dec_EncryptedPrivateKeyInfo'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute encryptionAlgorithm(1) with type SEQUENCE
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = 'dec_EncryptedPrivateKeyInfo_encryptionAlgorithm'(V1, [16]),

%%-------------------------------------------------
%% attribute encryptedData(2) with type OCTET STRING
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = decode_restricted_string(V2,[4]),

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'EncryptedPrivateKeyInfo', Term1, Term2}.



%%================================
%%  Attributes
%%================================
'enc_Attributes'(Val) ->
    'enc_Attributes'(Val, [<<49>>]).

'enc_Attributes'(Val, TagIn) ->
   {EncBytes,EncLen} = 'enc_Attributes_components'(Val,[],0),
   encode_tags(TagIn, EncBytes, EncLen).

'enc_Attributes_components'([], AccBytes, AccLen) -> 
   {dynamicsort_SETOF(AccBytes),AccLen};

'enc_Attributes_components'([H|T],AccBytes, AccLen) ->
   {EncBytes,EncLen} = 'enc_Attributes_PKAttribute'(H, [<<48>>]),
   'enc_Attributes_components'(T,[EncBytes|AccBytes], AccLen + EncLen).




%%================================
%%  Attributes_PKAttribute
%%================================
'enc_Attributes_PKAttribute'(Val, TagIn) ->
   {_,Cindex1, Cindex2, Cindex3} = Val,
Objtype = 
   'PKCS-FRAME':'getenc_internal_object_set_argument_8'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute type(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute values(2) with type SET OF
%%-------------------------------------------------
   {EncBytes2,EncLen2} = 'enc_Attributes_PKAttribute_values'(Cindex2, [<<49>>], Objtype),

%%-------------------------------------------------
%% attribute valuesWithContext(3) with type SET OF OPTIONAL
%%-------------------------------------------------
   {EncBytes3,EncLen3} =  case Cindex3 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            'enc_Attributes_PKAttribute_valuesWithContext'(Cindex3, [<<49>>], Objtype)
       end,

   BytesSoFar = [EncBytes1, EncBytes2, EncBytes3],
LenSoFar = EncLen1 + EncLen2 + EncLen3,
encode_tags(TagIn, BytesSoFar, LenSoFar).



%%================================
%%  Attributes_PKAttribute_values
%%================================
'enc_Attributes_PKAttribute_values'(Val, TagIn, ObjFun) ->
      {EncBytes,EncLen} = 'enc_Attributes_PKAttribute_values_components'(Val, ObjFun,[],0),
   encode_tags(TagIn, EncBytes, EncLen).

'enc_Attributes_PKAttribute_values_components'([], _, AccBytes, AccLen) -> 
   {dynamicsort_SETOF(AccBytes),AccLen};

'enc_Attributes_PKAttribute_values_components'([H|T], ObjFun,AccBytes, AccLen) ->
   {TmpBytes1,_} = ObjFun('Type', H, []),
   {EncBytes,EncLen} = encode_open_type(TmpBytes1, [])
,
   'enc_Attributes_PKAttribute_values_components'(T, ObjFun,[EncBytes|AccBytes], AccLen + EncLen).

'dec_Attributes_PKAttribute_values'(Tlv, TagIn, ObjFun) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),
[
      begin
         Tmptlv1 = decode_open_type(V1, []),
         case (catch ObjFun('Type', Tmptlv1, [])) of
            {'EXIT',Reason1} ->
               exit({'Type not compatible with table constraint', Reason1});
            Tmpterm1 ->
               Tmpterm1
         end
      end
 || V1 <- Tlv1].





%%================================
%%  Attributes_PKAttribute_valuesWithContext
%%================================
'enc_Attributes_PKAttribute_valuesWithContext'(Val, TagIn, ObjFun) ->
      {EncBytes,EncLen} = 'enc_Attributes_PKAttribute_valuesWithContext_components'(Val, ObjFun,[],0),
   encode_tags(TagIn, EncBytes, EncLen).

'enc_Attributes_PKAttribute_valuesWithContext_components'([], _, AccBytes, AccLen) -> 
   {dynamicsort_SETOF(AccBytes),AccLen};

'enc_Attributes_PKAttribute_valuesWithContext_components'([H|T], ObjFun,AccBytes, AccLen) ->
   {EncBytes,EncLen} = 'enc_Attributes_PKAttribute_valuesWithContext_SETOF'(H, [<<48>>], ObjFun),
   'enc_Attributes_PKAttribute_valuesWithContext_components'(T, ObjFun,[EncBytes|AccBytes], AccLen + EncLen).




%%================================
%%  Attributes_PKAttribute_valuesWithContext_SETOF
%%================================
'enc_Attributes_PKAttribute_valuesWithContext_SETOF'(Val, TagIn, ObjFun) ->
   {_,Cindex1, Cindex2} = Val,

%%-------------------------------------------------
%% attribute value(1) with type typefieldType
%%-------------------------------------------------
   {TmpBytes1,_} = ObjFun('Type', Cindex1, []),
   {EncBytes1,EncLen1} = encode_open_type(TmpBytes1, [])
,

%%-------------------------------------------------
%% attribute contextList(2) with type SET OF
%%-------------------------------------------------
   {EncBytes2,EncLen2} = 'enc_Attributes_PKAttribute_valuesWithContext_SETOF_contextList'(Cindex2, [<<49>>]),

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).



%%================================
%%  Attributes_PKAttribute_valuesWithContext_SETOF_contextList
%%================================
'enc_Attributes_PKAttribute_valuesWithContext_SETOF_contextList'(Val, TagIn) ->
      {EncBytes,EncLen} = 'enc_Attributes_PKAttribute_valuesWithContext_SETOF_contextList_components'(Val,[],0),
   encode_tags(TagIn, EncBytes, EncLen).

'enc_Attributes_PKAttribute_valuesWithContext_SETOF_contextList_components'([], AccBytes, AccLen) -> 
   {dynamicsort_SETOF(AccBytes),AccLen};

'enc_Attributes_PKAttribute_valuesWithContext_SETOF_contextList_components'([H|T],AccBytes, AccLen) ->
   {EncBytes,EncLen} = 'enc_Context'(H, [<<48>>]),
   'enc_Attributes_PKAttribute_valuesWithContext_SETOF_contextList_components'(T,[EncBytes|AccBytes], AccLen + EncLen).

'dec_Attributes_PKAttribute_valuesWithContext_SETOF_contextList'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),
['dec_Context'(V1, [16]) || V1 <- Tlv1].


'dec_Attributes_PKAttribute_valuesWithContext_SETOF'(Tlv, TagIn, ObjFun) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute value(1) with type typefieldType
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = 
      begin
         Tmptlv1 = decode_open_type(V1, []),
         case (catch ObjFun('Type', Tmptlv1, [])) of
            {'EXIT',Reason1} ->
               exit({'Type not compatible with table constraint', Reason1});
            Tmpterm1 ->
               Tmpterm1
         end
      end
,

%%-------------------------------------------------
%% attribute contextList(2) with type SET OF
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = 'dec_Attributes_PKAttribute_valuesWithContext_SETOF_contextList'(V2, [17]),

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'Attributes_PKAttribute_valuesWithContext_SETOF', Term1, Term2}.

'dec_Attributes_PKAttribute_valuesWithContext'(Tlv, TagIn, ObjFun) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),
['dec_Attributes_PKAttribute_valuesWithContext_SETOF'(V1, [16], ObjFun) || V1 <- Tlv1].


'dec_Attributes_PKAttribute'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute type(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),
ObjFun = 'PKCS-FRAME':'getdec_internal_object_set_argument_8'(id, Term1),

%%-------------------------------------------------
%% attribute values(2) with type SET OF
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = 'dec_Attributes_PKAttribute_values'(V2, [17], ObjFun),

%%-------------------------------------------------
%% attribute valuesWithContext(3) with type SET OF OPTIONAL
%%-------------------------------------------------
{Term3,Tlv4} = case Tlv3 of
[{17,V3}|TempTlv4] ->
    {'dec_Attributes_PKAttribute_valuesWithContext'(V3, [], ObjFun), TempTlv4};
    _ ->
        { asn1_NOVALUE, Tlv3}
end,

case Tlv4 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv4}}}) % extra fields not allowed
end,
   {'PKAttribute', Term1, Term2, Term3}.



'dec_Attributes'(Tlv) ->
   'dec_Attributes'(Tlv, [17]).

'dec_Attributes'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),
['dec_Attributes_PKAttribute'(V1, [16]) || V1 <- Tlv1].




%%================================
%%  PrivateKey
%%================================
'enc_PrivateKey'(Val) ->
    'enc_PrivateKey'(Val, [<<4>>]).

'enc_PrivateKey'(Val, TagIn) ->
encode_restricted_string(Val, TagIn).


'dec_PrivateKey'(Tlv) ->
   'dec_PrivateKey'(Tlv, [4]).

'dec_PrivateKey'(Tlv, TagIn) ->
decode_restricted_string(Tlv,TagIn).



%%================================
%%  Version
%%================================
'enc_Version'(Val) ->
    'enc_Version'(Val, [<<2>>]).

'enc_Version'(Val, TagIn) ->
encode_integer(Val, [{v1,0}], TagIn).


'dec_Version'(Tlv) ->
   'dec_Version'(Tlv, [2]).

'dec_Version'(Tlv, TagIn) ->
decode_integer(Tlv,[],[{v1,0}],TagIn).



%%================================
%%  PrivateKeyInfo
%%================================
'enc_PrivateKeyInfo'(Val) ->
    'enc_PrivateKeyInfo'(Val, [<<48>>]).

'enc_PrivateKeyInfo'(Val, TagIn) ->
{_,Cindex1, Cindex2, Cindex3, Cindex4} = Val,

%%-------------------------------------------------
%% attribute version(1) with type INTEGER
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_integer(Cindex1, [{v1,0}], [<<2>>]),

%%-------------------------------------------------
%% attribute privateKeyAlgorithm(2) with type SEQUENCE
%%-------------------------------------------------
   {EncBytes2,EncLen2} = 'enc_PrivateKeyInfo_privateKeyAlgorithm'(Cindex2, [<<48>>]),

%%-------------------------------------------------
%% attribute privateKey(3) with type OCTET STRING
%%-------------------------------------------------
   {EncBytes3,EncLen3} = encode_restricted_string(Cindex3, [<<4>>]),

%%-------------------------------------------------
%% attribute attributes(4)   External PKCS-FRAME:Attributes OPTIONAL
%%-------------------------------------------------
   {EncBytes4,EncLen4} =  case Cindex4 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            'enc_Attributes'(Cindex4, [<<160>>])
       end,

   BytesSoFar = [EncBytes1, EncBytes2, EncBytes3, EncBytes4],
LenSoFar = EncLen1 + EncLen2 + EncLen3 + EncLen4,
encode_tags(TagIn, BytesSoFar, LenSoFar).



%%================================
%%  PrivateKeyInfo_privateKeyAlgorithm
%%================================
'enc_PrivateKeyInfo_privateKeyAlgorithm'(Val, TagIn) ->
   {_,Cindex1, Cindex2} = Val,
Objalgorithm = 
   'PKCS-FRAME':'getenc_internal_object_set_argument_9'(id, 
                                   Cindex1),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
   {EncBytes1,EncLen1} = encode_object_identifier(Cindex1, [<<6>>]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
   {EncBytes2,EncLen2} =  case Cindex2 of
         asn1_NOVALUE -> {<<>>,0};
         _ ->
            {TmpBytes,_ } = Objalgorithm('Type', Cindex2, []),
   {TmpBytes1,TmpLen} = encode_open_type(TmpBytes, []),
   {TmpBytes1, TmpLen}
       end,

   BytesSoFar = [EncBytes1, EncBytes2],
LenSoFar = EncLen1 + EncLen2,
encode_tags(TagIn, BytesSoFar, LenSoFar).
'dec_PrivateKeyInfo_privateKeyAlgorithm'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute algorithm(1) with type fixedtypevaluefieldidtypetagUNIVERSAL6IMPLICIT0OBJECT IDENTIFIERno
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_object_identifier(V1,[6]),

%%-------------------------------------------------
%% attribute parameters(2) with type typefieldType OPTIONAL
%%-------------------------------------------------
{Tmpterm1,Tlv3} = case Tlv2 of
[V2|TempTlv3] ->
    {decode_open_type(V2, []), TempTlv3};
    _ ->
        { asn1_NOVALUE, Tlv2}
end,

DecObjalgorithmTerm1 =
   'PKCS-FRAME':'getdec_internal_object_set_argument_9'(id, Term1),
Term2 = 
   case Tmpterm1 of
      asn1_NOVALUE ->asn1_NOVALUE;
      _ ->
         case (catch DecObjalgorithmTerm1('Type', Tmpterm1, [])) of
            {'EXIT', Reason1} ->
               exit({'Type not compatible with table constraint',Reason1});
            Tmpterm2 ->
               Tmpterm2
         end
   end,

case Tlv3 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv3}}}) % extra fields not allowed
end,
   {'PrivateKeyInfo_privateKeyAlgorithm', Term1, Term2}.



'dec_PrivateKeyInfo'(Tlv) ->
   'dec_PrivateKeyInfo'(Tlv, [16]).

'dec_PrivateKeyInfo'(Tlv, TagIn) ->
   %%-------------------------------------------------
   %% decode tag and length 
   %%-------------------------------------------------
Tlv1 = match_tags(Tlv, TagIn),

%%-------------------------------------------------
%% attribute version(1) with type INTEGER
%%-------------------------------------------------
[V1|Tlv2] = Tlv1, 
Term1 = decode_integer(V1,[],[{v1,0}],[2]),

%%-------------------------------------------------
%% attribute privateKeyAlgorithm(2) with type SEQUENCE
%%-------------------------------------------------
[V2|Tlv3] = Tlv2, 
Term2 = 'dec_PrivateKeyInfo_privateKeyAlgorithm'(V2, [16]),

%%-------------------------------------------------
%% attribute privateKey(3) with type OCTET STRING
%%-------------------------------------------------
[V3|Tlv4] = Tlv3, 
Term3 = decode_restricted_string(V3,[4]),

%%-------------------------------------------------
%% attribute attributes(4)   External PKCS-FRAME:Attributes OPTIONAL
%%-------------------------------------------------
{Term4,Tlv5} = case Tlv4 of
[{131072,V4}|TempTlv5] ->
    {'dec_Attributes'(V4, []), TempTlv5};
    _ ->
        { asn1_NOVALUE, Tlv4}
end,

case Tlv5 of
[] -> true;_ -> exit({error,{asn1, {unexpected,Tlv5}}}) % extra fields not allowed
end,
   {'PrivateKeyInfo', Term1, Term2, Term3, Term4}.

'rc5-CBC-PAD'() ->
{1,2,840,113549,3,9}.

'rc2CBC'() ->
{1,2,840,113549,3,2}.

'des-EDE3-CBC'() ->
{1,2,840,113549,3,7}.

'desCBC'() ->
{1,3,14,3,2,7}.

'id-hmacWithSHA1'() ->
{1,2,840,113549,2,7}.

'encryptionAlgorithm'() ->
{1,2,840,113549,3}.

'digestAlgorithm'() ->
{1,2,840,113549,2}.

'id-PBMAC1'() ->
{1,2,840,113549,1,5,14}.

'id-PBES2'() ->
{1,2,840,113549,1,5,13}.

'pbeWithSHA1AndRC2-CBC'() ->
{1,2,840,113549,1,5,11}.

'pbeWithSHA1AndDES-CBC'() ->
{1,2,840,113549,1,5,10}.

'pbeWithMD5AndRC2-CBC'() ->
{1,2,840,113549,1,5,6}.

'pbeWithMD5AndDES-CBC'() ->
{1,2,840,113549,1,5,3}.

'pbeWithMD2AndRC2-CBC'() ->
{1,2,840,113549,1,5,4}.

'pbeWithMD2AndDES-CBC'() ->
{1,2,840,113549,1,5,1}.

'id-PBKDF2'() ->
{1,2,840,113549,1,5,12}.

'pkcs-5'() ->
{1,2,840,113549,1,5}.

'pkcs'() ->
{1,2,840,113549,1}.

'rsadsi'() ->
{1,2,840,113549}.




%%================================
%%  SupportingAlgorithms
%%================================
'getenc_SupportingAlgorithms'(id,{1,2,840,113549,2,7}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            encode_null(Val, [<<5>>])
      end
   end;
'getenc_SupportingAlgorithms'(id,{1,2,840,113549,3,2}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_RC2-CBC-Parameter'(Val)
      end
   end;
'getenc_SupportingAlgorithms'(id,{1,2,840,113549,3,7}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            encode_restricted_string(Val, [<<4>>])
      end
   end;
'getenc_SupportingAlgorithms'(id,{1,2,840,113549,3,9}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_RC5-CBC-Parameters'(Val)
      end
   end;
'getenc_SupportingAlgorithms'(id,{1,3,14,3,2,7}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            encode_restricted_string(Val, [<<4>>])
      end
   end;
'getenc_SupportingAlgorithms'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_SupportingAlgorithms'(id,{1,2,840,113549,2,7}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            decode_null(Bytes,[5])
      end
   end;
'getdec_SupportingAlgorithms'(id,{1,2,840,113549,3,2}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_RC2-CBC-Parameter'(Bytes)
      end
   end;
'getdec_SupportingAlgorithms'(id,{1,2,840,113549,3,7}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            decode_restricted_string(Bytes,8,[4])
      end
   end;
'getdec_SupportingAlgorithms'(id,{1,2,840,113549,3,9}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_RC5-CBC-Parameters'(Bytes)
      end
   end;
'getdec_SupportingAlgorithms'(id,{1,3,14,3,2,7}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            decode_restricted_string(Bytes,8,[4])
      end
   end;
'getdec_SupportingAlgorithms'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PBMAC1-MACs
%%================================
'getenc_PBMAC1-MACs'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PBMAC1-MACs'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PBMAC1-KDFs
%%================================
'getenc_PBMAC1-KDFs'(id,{1,2,840,113549,1,5,12}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBKDF2-params'(Val)
      end
   end;
'getenc_PBMAC1-KDFs'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PBMAC1-KDFs'(id,{1,2,840,113549,1,5,12}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBKDF2-params'(Bytes)
      end
   end;
'getdec_PBMAC1-KDFs'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PBMAC1Algorithms
%%================================
'getenc_PBMAC1Algorithms'(id,{1,2,840,113549,1,5,14}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBMAC1-params'(Val)
      end
   end;
'getenc_PBMAC1Algorithms'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PBMAC1Algorithms'(id,{1,2,840,113549,1,5,14}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBMAC1-params'(Bytes)
      end
   end;
'getdec_PBMAC1Algorithms'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PBES2-Encs
%%================================
'getenc_PBES2-Encs'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PBES2-Encs'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PBES2-KDFs
%%================================
'getenc_PBES2-KDFs'(id,{1,2,840,113549,1,5,12}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBKDF2-params'(Val)
      end
   end;
'getenc_PBES2-KDFs'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PBES2-KDFs'(id,{1,2,840,113549,1,5,12}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBKDF2-params'(Bytes)
      end
   end;
'getdec_PBES2-KDFs'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PBES2Algorithms
%%================================
'getenc_PBES2Algorithms'(id,{1,2,840,113549,1,5,13}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBES2-params'(Val)
      end
   end;
'getenc_PBES2Algorithms'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PBES2Algorithms'(id,{1,2,840,113549,1,5,13}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBES2-params'(Bytes)
      end
   end;
'getdec_PBES2Algorithms'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PBES1Algorithms
%%================================
'getenc_PBES1Algorithms'(id,{1,2,840,113549,1,5,1}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBEParameter'(Val)
      end
   end;
'getenc_PBES1Algorithms'(id,{1,2,840,113549,1,5,3}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBEParameter'(Val)
      end
   end;
'getenc_PBES1Algorithms'(id,{1,2,840,113549,1,5,4}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBEParameter'(Val)
      end
   end;
'getenc_PBES1Algorithms'(id,{1,2,840,113549,1,5,6}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBEParameter'(Val)
      end
   end;
'getenc_PBES1Algorithms'(id,{1,2,840,113549,1,5,10}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBEParameter'(Val)
      end
   end;
'getenc_PBES1Algorithms'(id,{1,2,840,113549,1,5,11}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBEParameter'(Val)
      end
   end;
'getenc_PBES1Algorithms'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PBES1Algorithms'(id,{1,2,840,113549,1,5,1}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBEParameter'(Bytes)
      end
   end;
'getdec_PBES1Algorithms'(id,{1,2,840,113549,1,5,3}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBEParameter'(Bytes)
      end
   end;
'getdec_PBES1Algorithms'(id,{1,2,840,113549,1,5,4}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBEParameter'(Bytes)
      end
   end;
'getdec_PBES1Algorithms'(id,{1,2,840,113549,1,5,6}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBEParameter'(Bytes)
      end
   end;
'getdec_PBES1Algorithms'(id,{1,2,840,113549,1,5,10}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBEParameter'(Bytes)
      end
   end;
'getdec_PBES1Algorithms'(id,{1,2,840,113549,1,5,11}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBEParameter'(Bytes)
      end
   end;
'getdec_PBES1Algorithms'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PBKDF2-PRFs
%%================================
'getenc_PBKDF2-PRFs'(id,{1,2,840,113549,2,7}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            encode_null(Val, [<<5>>])
      end
   end;
'getenc_PBKDF2-PRFs'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PBKDF2-PRFs'(id,{1,2,840,113549,2,7}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            decode_null(Bytes,[5])
      end
   end;
'getdec_PBKDF2-PRFs'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PBKDF2-SaltSources
%%================================
'getenc_PBKDF2-SaltSources'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PBKDF2-SaltSources'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  SupportedContexts
%%================================
'getenc_SupportedContexts'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_SupportedContexts'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  KeyEncryptionAlgorithms
%%================================
'getenc_KeyEncryptionAlgorithms'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_KeyEncryptionAlgorithms'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  PrivateKeyAlgorithms
%%================================
'getenc_PrivateKeyAlgorithms'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_PrivateKeyAlgorithms'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  internal_object_set_argument_9
%%================================
'getenc_internal_object_set_argument_9'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_internal_object_set_argument_9'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  internal_object_set_argument_8
%%================================
'getenc_internal_object_set_argument_8'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_internal_object_set_argument_8'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  internal_object_set_argument_7
%%================================
'getenc_internal_object_set_argument_7'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_internal_object_set_argument_7'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  internal_object_set_argument_6
%%================================
'getenc_internal_object_set_argument_6'(id,{1,2,840,113549,2,7}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            encode_null(Val, [<<5>>])
      end
   end;
'getenc_internal_object_set_argument_6'(id, ErrV) ->
   fun(C,V,_) -> exit({'Type not compatible with table constraint',{component,C},{value,V}, {unique_name_and_value,id, ErrV}}) end.

'getdec_internal_object_set_argument_6'(id,{1,2,840,113549,2,7}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            decode_null(Bytes,[5])
      end
   end;
'getdec_internal_object_set_argument_6'(id, ErrV) ->
  fun(C,V,_) -> exit({{component,C},{value,V},{unique_name_and_value,id, ErrV}}) end.





%%================================
%%  internal_object_set_argument_5
%%================================
'getenc_internal_object_set_argument_5'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_internal_object_set_argument_5'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  internal_object_set_argument_4
%%================================
'getenc_internal_object_set_argument_4'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_internal_object_set_argument_4'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  internal_object_set_argument_3
%%================================
'getenc_internal_object_set_argument_3'(id,{1,2,840,113549,1,5,12}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBKDF2-params'(Val)
      end
   end;
'getenc_internal_object_set_argument_3'(id, ErrV) ->
   fun(C,V,_) -> exit({'Type not compatible with table constraint',{component,C},{value,V}, {unique_name_and_value,id, ErrV}}) end.

'getdec_internal_object_set_argument_3'(id,{1,2,840,113549,1,5,12}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBKDF2-params'(Bytes)
      end
   end;
'getdec_internal_object_set_argument_3'(id, ErrV) ->
  fun(C,V,_) -> exit({{component,C},{value,V},{unique_name_and_value,id, ErrV}}) end.





%%================================
%%  internal_object_set_argument_2
%%================================
'getenc_internal_object_set_argument_2'(_, _) ->
   fun(_, Val, _RestPrimFieldName) ->
      Len = case Val of
         Bin when is_binary(Bin) -> byte_size(Bin);
         _ -> length(Val)
      end,      {Val,Len}
   end.

'getdec_internal_object_set_argument_2'(_, _) ->
  fun(_,Bytes, _RestPrimFieldName) ->
    case Bytes of
      Bin when is_binary(Bin) -> 
        Bin;
      _ ->
        ber_encode(Bytes)
    end
  end.





%%================================
%%  internal_object_set_argument_1
%%================================
'getenc_internal_object_set_argument_1'(id,{1,2,840,113549,1,5,12}) ->
   fun(Type, Val, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'enc_PBKDF2-params'(Val)
      end
   end;
'getenc_internal_object_set_argument_1'(id, ErrV) ->
   fun(C,V,_) -> exit({'Type not compatible with table constraint',{component,C},{value,V}, {unique_name_and_value,id, ErrV}}) end.

'getdec_internal_object_set_argument_1'(id,{1,2,840,113549,1,5,12}) ->
   fun(Type, Bytes, _RestPrimFieldName) ->
      case Type of
         'Type' ->
            'dec_PBKDF2-params'(Bytes)
      end
   end;
'getdec_internal_object_set_argument_1'(id, ErrV) ->
  fun(C,V,_) -> exit({{component,C},{value,V},{unique_name_and_value,id, ErrV}}) end.


'check_PBKDF2-params_prf'(_V,asn1_DEFAULT) ->
   true;
'check_PBKDF2-params_prf'(V,V) ->
   true;
'check_PBKDF2-params_prf'(V,{_,V}) ->
   true;
'check_PBKDF2-params_prf'(DefaultValue,Value) ->
   check_objectidentifier(element(2,DefaultValue), element(2,Value)),
   if
      element(3,DefaultValue) == element(3,Value) -> true;
      true -> throw({error,{asn1_open_type}})
   end
.


%%%
%%% Run-time functions.
%%%

ber_decode_nif(B) ->
    asn1rt_nif:decode_ber_tlv(B).

ber_encode([Tlv]) ->
    ber_encode(Tlv);
ber_encode(Tlv) when is_binary(Tlv) ->
    Tlv;
ber_encode(Tlv) ->
    asn1rt_nif:encode_ber_tlv(Tlv).

check_bool(_Bool, asn1_DEFAULT) ->
    true;
check_bool(Bool, Bool) when is_boolean(Bool) ->
    true;
check_bool(_Bool1, Bool2) ->
    throw({error,Bool2}).

check_objectidentifier(_, asn1_DEFAULT) ->
    true;
check_objectidentifier(OI, OI) ->
    true;
check_objectidentifier(DOI, OI) when is_tuple(DOI), is_tuple(OI) ->
    check_objectidentifier1(tuple_to_list(DOI), tuple_to_list(OI));
check_objectidentifier(_, OI) ->
    throw({error,OI}).

check_objectidentifier1([V|Rest1], [V|Rest2]) ->
    check_objectidentifier1(Rest1, Rest2, V);
check_objectidentifier1([V1|Rest1], [V2|Rest2]) ->
    case reserved_objectid(V2, []) of
        V1 ->
            check_objectidentifier1(Rest1, Rest2, [V1]);
        V ->
            throw({error,V})
    end.

check_objectidentifier1([V|Rest1], [V|Rest2], Above) ->
    check_objectidentifier1(Rest1, Rest2, [V|Above]);
check_objectidentifier1([V1|Rest1], [V2|Rest2], Above) ->
    case reserved_objectid(V2, Above) of
        V1 ->
            check_objectidentifier1(Rest1, Rest2, [V1|Above]);
        V ->
            throw({error,V})
    end;
check_objectidentifier1([], [], _) ->
    true;
check_objectidentifier1(_, V, _) ->
    throw({error,object,identifier,V}).

check_restricted_string(Val, StrLen, Range) ->
    case Range of
        {Lb,Ub} when StrLen >= Lb, Ub >= StrLen ->
            Val;
        {{Lb,_Ub},[]} when StrLen >= Lb ->
            Val;
        {{Lb,_Ub},_Ext = [Min|_]} when StrLen >= Lb; StrLen >= Min ->
            Val;
        {{Lb1,Ub1},{Lb2,Ub2}}
            when
                StrLen >= Lb1, StrLen =< Ub1;
                StrLen =< Ub2, StrLen >= Lb2 ->
            Val;
        StrLen ->
            Val;
        {_,_} ->
            exit({error,{asn1,{length,Range,Val}}});
        _Len when is_integer(_Len) ->
            exit({error,{asn1,{length,Range,Val}}});
        _ ->
            Val
    end.

collect_parts(TlvList) ->
    collect_parts(TlvList, []).

collect_parts([{_,L}|Rest], Acc) when is_list(L) ->
    collect_parts(Rest, [collect_parts(L)|Acc]);
collect_parts([{3,<<Unused,Bits/binary>>}|Rest], _Acc) ->
    collect_parts_bit(Rest, [Bits], Unused);
collect_parts([{_T,V}|Rest], Acc) ->
    collect_parts(Rest, [V|Acc]);
collect_parts([], Acc) ->
    list_to_binary(lists:reverse(Acc)).

collect_parts_bit([{3,<<Unused,Bits/binary>>}|Rest], Acc, Uacc) ->
    collect_parts_bit(Rest, [Bits|Acc], Unused + Uacc);
collect_parts_bit([], Acc, Uacc) ->
    list_to_binary([Uacc|lists:reverse(Acc)]).

dec_subidentifiers(<<>>, _Av, Al) ->
    lists:reverse(Al);
dec_subidentifiers(<<1:1,H:7,T/binary>>, Av, Al) ->
    dec_subidentifiers(T, Av bsl 7 + H, Al);
dec_subidentifiers(<<H,T/binary>>, Av, Al) ->
    dec_subidentifiers(T, 0, [Av bsl 7 + H|Al]).

decode_boolean(Tlv, TagIn) ->
    Val = match_tags(Tlv, TagIn),
    case Val of
        <<0:8>> ->
            false;
        <<_:8>> ->
            true;
        _ ->
            exit({error,{asn1,{decode_boolean,Val}}})
    end.

decode_enumerated(Tlv, NamedNumberList, Tags) ->
    Buffer = match_tags(Tlv, Tags),
    decode_enumerated_notag(Buffer, NamedNumberList, Tags).

decode_enumerated1(Val, NamedNumberList) ->
    case lists:keyfind(Val, 2, NamedNumberList) of
        {NamedVal,_} ->
            NamedVal;
        _ ->
            {asn1_enum,Val}
    end.

decode_enumerated_notag(Buffer, {NamedNumberList,ExtList}, _Tags) ->
    IVal = decode_integer(Buffer),
    case decode_enumerated1(IVal, NamedNumberList) of
        {asn1_enum,IVal} ->
            decode_enumerated1(IVal, ExtList);
        EVal ->
            EVal
    end;
decode_enumerated_notag(Buffer, NNList, _Tags) ->
    IVal = decode_integer(Buffer),
    case decode_enumerated1(IVal, NNList) of
        {asn1_enum,_} ->
            exit({error,{asn1,{illegal_enumerated,IVal}}});
        EVal ->
            EVal
    end.

decode_integer(Bin) ->
    Len = byte_size(Bin),
    <<Int:Len/signed-unit:8>> = Bin,
    Int.

decode_integer(Tlv, Range, NamedNumberList, TagIn) ->
    V = match_tags(Tlv, TagIn),
    Int = range_check_integer(decode_integer(V), Range),
    number2name(Int, NamedNumberList).

decode_integer(Tlv, Range, TagIn) ->
    V = match_tags(Tlv, TagIn),
    Int = decode_integer(V),
    range_check_integer(Int, Range).

decode_null(Tlv, Tags) ->
    Val = match_tags(Tlv, Tags),
    case Val of
        <<>> ->
            'NULL';
        _ ->
            exit({error,{asn1,{decode_null,Val}}})
    end.

decode_object_identifier(Tlv, Tags) ->
    Val = match_tags(Tlv, Tags),
    [AddedObjVal|ObjVals] = dec_subidentifiers(Val, 0, []),
    {Val1,Val2} =
        if
            AddedObjVal < 40 ->
                {0,AddedObjVal};
            AddedObjVal < 80 ->
                {1,AddedObjVal - 40};
            true ->
                {2,AddedObjVal - 80}
        end,
    list_to_tuple([Val1,Val2|ObjVals]).

decode_open_type(Tlv, TagIn) ->
    case match_tags(Tlv, TagIn) of
        Bin when is_binary(Bin) ->
            {InnerTlv,_} = ber_decode_nif(Bin),
            InnerTlv;
        TlvBytes ->
            TlvBytes
    end.

decode_restricted_string(Tlv, Range, TagsIn) ->
    Bin = match_and_collect(Tlv, TagsIn),
    check_restricted_string(binary_to_list(Bin), byte_size(Bin), Range).

decode_restricted_string(Tlv, TagsIn) ->
    Bin = match_and_collect(Tlv, TagsIn),
    binary_to_list(Bin).

dynamicsort_SETOF(ListOfEncVal) ->
    BinL =
        lists:map(fun(L) when is_list(L) ->
                         list_to_binary(L);
                     (B) ->
                         B
                  end,
                  ListOfEncVal),
    lists:sort(BinL).

e_object_identifier({'OBJECT IDENTIFIER',V}) ->
    e_object_identifier(V);
e_object_identifier(V) when is_tuple(V) ->
    e_object_identifier(tuple_to_list(V));
e_object_identifier([E1,E2|Tail]) ->
    Head = 40 * E1 + E2,
    {H,Lh} = mk_object_val(Head),
    {R,Lr} = lists:mapfoldl(fun enc_obj_id_tail/2, 0, Tail),
    {[H|R],Lh + Lr}.

enc_obj_id_tail(H, Len) ->
    {B,L} = mk_object_val(H),
    {B,Len + L}.

encode_boolean(true, TagIn) ->
    encode_tags(TagIn, [255], 1);
encode_boolean(false, TagIn) ->
    encode_tags(TagIn, [0], 1);
encode_boolean(X, _) ->
    exit({error,{asn1,{encode_boolean,X}}}).

encode_enumerated(Val, TagIn) when is_integer(Val) ->
    encode_tags(TagIn, encode_integer(Val)).

encode_integer(Val) ->
    Bytes =
        if
            Val >= 0 ->
                encode_integer_pos(Val, []);
            true ->
                encode_integer_neg(Val, [])
        end,
    {Bytes,length(Bytes)}.

encode_integer(Val, NamedNumberList, Tag) when is_atom(Val) ->
    case lists:keyfind(Val, 1, NamedNumberList) of
        {_,NewVal} ->
            encode_tags(Tag, encode_integer(NewVal));
        _ ->
            exit({error,{asn1,{encode_integer_namednumber,Val}}})
    end;
encode_integer(Val, _NamedNumberList, Tag) ->
    encode_tags(Tag, encode_integer(Val)).

encode_integer(Val, Tag) when is_integer(Val) ->
    encode_tags(Tag, encode_integer(Val));
encode_integer(Val, _Tag) ->
    exit({error,{asn1,{encode_integer,Val}}}).

encode_integer_neg(- 1, [B1|_T] = L) when B1 > 127 ->
    L;
encode_integer_neg(N, Acc) ->
    encode_integer_neg(N bsr 8, [N band 255|Acc]).

encode_integer_pos(0, [B|_Acc] = L) when B < 128 ->
    L;
encode_integer_pos(N, Acc) ->
    encode_integer_pos(N bsr 8, [N band 255|Acc]).

encode_length(L) when L =< 127 ->
    {[L],1};
encode_length(L) ->
    Oct = minimum_octets(L),
    Len = length(Oct),
    if
        Len =< 126 ->
            {[128 bor Len|Oct],Len + 1};
        true ->
            exit({error,{asn1,too_long_length_oct,Len}})
    end.

encode_null(_Val, TagIn) ->
    encode_tags(TagIn, [], 0).

encode_object_identifier(Val, TagIn) ->
    encode_tags(TagIn, e_object_identifier(Val)).

encode_open_type(Val, T) when is_list(Val) ->
    encode_open_type(list_to_binary(Val), T);
encode_open_type(Val, []) ->
    {Val,byte_size(Val)};
encode_open_type(Val, Tag) ->
    encode_tags(Tag, Val, byte_size(Val)).

encode_restricted_string(OctetList, TagIn) when is_binary(OctetList) ->
    encode_tags(TagIn, OctetList, byte_size(OctetList));
encode_restricted_string(OctetList, TagIn) when is_list(OctetList) ->
    encode_tags(TagIn, OctetList, length(OctetList)).

encode_tags(TagIn, {BytesSoFar,LenSoFar}) ->
    encode_tags(TagIn, BytesSoFar, LenSoFar).

encode_tags([Tag|Trest], BytesSoFar, LenSoFar) ->
    {Bytes2,L2} = encode_length(LenSoFar),
    encode_tags(Trest,
                [Tag,Bytes2|BytesSoFar],
                LenSoFar + byte_size(Tag) + L2);
encode_tags([], BytesSoFar, LenSoFar) ->
    {BytesSoFar,LenSoFar}.

match_and_collect(Tlv, TagsIn) ->
    Val = match_tags(Tlv, TagsIn),
    case Val of
        [_|_] = PartList ->
            collect_parts(PartList);
        Bin when is_binary(Bin) ->
            Bin
    end.

match_tags({T,V}, [T]) ->
    V;
match_tags({T,V}, [T|Tt]) ->
    match_tags(V, Tt);
match_tags([{T,V}], [T|Tt]) ->
    match_tags(V, Tt);
match_tags([{T,_V}|_] = Vlist, [T]) ->
    Vlist;
match_tags(Tlv, []) ->
    Tlv;
match_tags({Tag,_V} = Tlv, [T|_Tt]) ->
    exit({error,{asn1,{wrong_tag,{{expected,T},{got,Tag,Tlv}}}}}).

minimum_octets(0, Acc) ->
    Acc;
minimum_octets(Val, Acc) ->
    minimum_octets(Val bsr 8, [Val band 255|Acc]).

minimum_octets(Val) ->
    minimum_octets(Val, []).

mk_object_val(0, Ack, Len) ->
    {Ack,Len};
mk_object_val(Val, Ack, Len) ->
    mk_object_val(Val bsr 7, [Val band 127 bor 128|Ack], Len + 1).

mk_object_val(Val) when Val =< 127 ->
    {[255 band Val],1};
mk_object_val(Val) ->
    mk_object_val(Val bsr 7, [Val band 127], 1).

number2name(Int, []) ->
    Int;
number2name(Int, NamedNumberList) ->
    case lists:keyfind(Int, 2, NamedNumberList) of
        {NamedVal,_} ->
            NamedVal;
        _ ->
            Int
    end.

range_check_integer(Int, Range) ->
    case Range of
        [] ->
            Int;
        {Lb,Ub} when Int >= Lb, Ub >= Int ->
            Int;
        {_,_} ->
            exit({error,{asn1,{integer_range,Range,Int}}});
        Int ->
            Int;
        SingleValue when is_integer(SingleValue) ->
            exit({error,{asn1,{integer_range,Range,Int}}});
        _ ->
            Int
    end.

reserved_objectid('itu-t', []) ->
    0;
reserved_objectid(ccitt, []) ->
    0;
reserved_objectid(recommendation, [0]) ->
    0;
reserved_objectid(question, [0]) ->
    1;
reserved_objectid(administration, [0]) ->
    2;
reserved_objectid('network-operator', [0]) ->
    3;
reserved_objectid('identified-organization', [0]) ->
    4;
reserved_objectid(iso, []) ->
    1;
reserved_objectid(standard, [1]) ->
    0;
reserved_objectid('member-body', [1]) ->
    2;
reserved_objectid('identified-organization', [1]) ->
    3;
reserved_objectid('joint-iso-itu-t', []) ->
    2;
reserved_objectid('joint-iso-ccitt', []) ->
    2;
reserved_objectid(_, _) ->
    false.
