From 5fdcb7a097a3956ebf52c24721c91f60aeb96aa1 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Thu, 28 Jun 2012 14:46:06 +0200 Subject: Add support for larger integer ranges in per encode/decode Encoding and decoding of integer ranges can now be done with an upper bound larger than the previous limit of 16^10. The new upper bound in per encoding and decodings for constrained whole numbers is 2^2040 (close to 16^508) which is the limit if the length field encoding in the encoding of a constrained whole number is limited to a single octet. Related support seq: seq12060 --- lib/asn1/src/asn1rt_per_bin_rt2ct.erl | 39 ++++++++++++----------------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'lib/asn1/src/asn1rt_per_bin_rt2ct.erl') diff --git a/lib/asn1/src/asn1rt_per_bin_rt2ct.erl b/lib/asn1/src/asn1rt_per_bin_rt2ct.erl index 750b59aba6..f2e93cf096 100644 --- a/lib/asn1/src/asn1rt_per_bin_rt2ct.erl +++ b/lib/asn1/src/asn1rt_per_bin_rt2ct.erl @@ -18,7 +18,6 @@ %% %% -module(asn1rt_per_bin_rt2ct). - %% encoding / decoding of PER aligned -include("asn1_records.hrl"). @@ -605,19 +604,13 @@ encode_constrained_number({Lb,Ub},Val) when Val >= Lb, Ub >= Val -> Range =< 65536 -> % Size = {octets,<>}; [20,2,<>]; - Range =< 16#1000000 -> - Octs = eint_positive(Val2), -% [{bits,2,length(Octs)-1},{octets,Octs}]; - Len = length(Octs), - [10,2,Len-1,20,Len,Octs]; - Range =< 16#100000000 -> - Octs = eint_positive(Val2), - Len = length(Octs), - [10,2,Len-1,20,Len,Octs]; - Range =< 16#10000000000 -> - Octs = eint_positive(Val2), - Len = length(Octs), - [10,3,Len-1,20,Len,Octs]; + Range =< (1 bsl (255*8)) -> + Octs = binary:bin_to_list(binary:encode_unsigned(Val2)), + RangeOcts = binary:bin_to_list(binary:encode_unsigned(Range - 1)), + OctsLen = length(Octs), + RangeOctsLen = length(RangeOcts), + LengthBitsNeeded = asn1rt_per_bin:minimum_bits(RangeOctsLen - 1), + [10,LengthBitsNeeded,OctsLen-1,20,OctsLen,Octs]; true -> exit({not_supported,{integer_range,Range}}) end; @@ -661,18 +654,12 @@ decode_constrained_number(Buffer,{Lb,_Ub},Range) -> getoctets(Buffer,1); Range =< 65536 -> getoctets(Buffer,2); - Range =< 16#1000000 -> - {Len,Bytes2} = decode_length(Buffer,{1,3}), - {Octs,Bytes3} = getoctets_as_bin(Bytes2,Len), - {dec_pos_integer(Octs),Bytes3}; - Range =< 16#100000000 -> - {Len,Bytes2} = decode_length(Buffer,{1,4}), - {Octs,Bytes3} = getoctets_as_bin(Bytes2,Len), - {dec_pos_integer(Octs),Bytes3}; - Range =< 16#10000000000 -> - {Len,Bytes2} = decode_length(Buffer,{1,5}), - {Octs,Bytes3} = getoctets_as_bin(Bytes2,Len), - {dec_pos_integer(Octs),Bytes3}; + Range =< (1 bsl (255*8)) -> + OList = binary:bin_to_list(binary:encode_unsigned(Range - 1)), + RangeOctLen = length(OList), + {Len, Bytes} = decode_length(Buffer, {1, RangeOctLen}), + {Octs, RestBytes} = getoctets_as_bin(Bytes, Len), + {binary:decode_unsigned(Octs), RestBytes}; true -> exit({not_supported,{integer_range,Range}}) end, -- cgit v1.2.3 From 49a4c58106ca913328b5f28e7124e820b15e4b43 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Tue, 3 Jul 2012 17:17:00 +0200 Subject: Keep encoded octets of constrained whole number value and range as binaries and use erlang:byte_size to get length of octets and range. For speed. --- lib/asn1/src/asn1rt_per_bin_rt2ct.erl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/asn1/src/asn1rt_per_bin_rt2ct.erl') diff --git a/lib/asn1/src/asn1rt_per_bin_rt2ct.erl b/lib/asn1/src/asn1rt_per_bin_rt2ct.erl index f2e93cf096..46d4bcb065 100644 --- a/lib/asn1/src/asn1rt_per_bin_rt2ct.erl +++ b/lib/asn1/src/asn1rt_per_bin_rt2ct.erl @@ -605,10 +605,10 @@ encode_constrained_number({Lb,Ub},Val) when Val >= Lb, Ub >= Val -> % Size = {octets,<>}; [20,2,<>]; Range =< (1 bsl (255*8)) -> - Octs = binary:bin_to_list(binary:encode_unsigned(Val2)), - RangeOcts = binary:bin_to_list(binary:encode_unsigned(Range - 1)), - OctsLen = length(Octs), - RangeOctsLen = length(RangeOcts), + Octs = binary:encode_unsigned(Val2), + RangeOcts = binary:encode_unsigned(Range - 1), + OctsLen = erlang:byte_size(Octs), + RangeOctsLen = erlang:byte_size(RangeOcts), LengthBitsNeeded = asn1rt_per_bin:minimum_bits(RangeOctsLen - 1), [10,LengthBitsNeeded,OctsLen-1,20,OctsLen,Octs]; true -> -- cgit v1.2.3