aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1/src
diff options
context:
space:
mode:
authorGustav Simonsson <[email protected]>2012-06-28 14:46:06 +0200
committerGustav Simonsson <[email protected]>2012-06-28 14:54:51 +0200
commit5fdcb7a097a3956ebf52c24721c91f60aeb96aa1 (patch)
treeba880be14ee3615ee3b88930a1ffdeb39d8e0b97 /lib/asn1/src
parent67d2e30e77db74d0852421c21362d1a9374780e1 (diff)
downloadotp-5fdcb7a097a3956ebf52c24721c91f60aeb96aa1.tar.gz
otp-5fdcb7a097a3956ebf52c24721c91f60aeb96aa1.tar.bz2
otp-5fdcb7a097a3956ebf52c24721c91f60aeb96aa1.zip
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
Diffstat (limited to 'lib/asn1/src')
-rw-r--r--lib/asn1/src/asn1rt_per_bin.erl46
-rw-r--r--lib/asn1/src/asn1rt_per_bin_rt2ct.erl39
2 files changed, 36 insertions, 49 deletions
diff --git a/lib/asn1/src/asn1rt_per_bin.erl b/lib/asn1/src/asn1rt_per_bin.erl
index a124c7553d..4e0c12936c 100644
--- a/lib/asn1/src/asn1rt_per_bin.erl
+++ b/lib/asn1/src/asn1rt_per_bin.erl
@@ -18,7 +18,6 @@
%%
%%
-module(asn1rt_per_bin).
-
%% encoding / decoding of PER aligned
-include("asn1_records.hrl").
@@ -57,7 +56,7 @@
encode_NumericString/2, decode_NumericString/2,
encode_ObjectDescriptor/2, decode_ObjectDescriptor/1
]).
--export([complete_bytes/1, getbits/2, getoctets/2]).
+-export([complete_bytes/1, getbits/2, getoctets/2, minimum_bits/1]).
-define('16K',16384).
-define('32K',32768).
@@ -695,21 +694,28 @@ encode_constrained_number({Lb,Ub},Val) when Val >= Lb, Ub >= Val ->
{octets,[Val2]};
Range =< 65536 ->
{octets,<<Val2:16>>};
- Range =< 16#1000000 ->
- Octs = eint_positive(Val2),
- [{bits,2,length(Octs)-1},{octets,Octs}];
- Range =< 16#100000000 ->
- Octs = eint_positive(Val2),
- [{bits,2,length(Octs)-1},{octets,Octs}];
- Range =< 16#10000000000 ->
- Octs = eint_positive(Val2),
- [{bits,3,length(Octs)-1},{octets,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 = minimum_bits(RangeOctsLen - 1),
+ [{bits, LengthBitsNeeded, OctsLen - 1}, {octets, Octs}];
true ->
exit({not_supported,{integer_range,Range}})
end;
encode_constrained_number(Range,Val) ->
exit({error,{asn1,{integer_range,Range,value,Val}}}).
+%% For some reason the minimum bits needed in the length field in encoding of
+%% constrained whole numbers must always be atleast 2?
+minimum_bits(N) when N < 4 -> 2;
+minimum_bits(N) when N < 8 -> 3;
+minimum_bits(N) when N < 16 -> 4;
+minimum_bits(N) when N < 32 -> 5;
+minimum_bits(N) when N < 64 -> 6;
+minimum_bits(N) when N < 128 -> 7;
+minimum_bits(_N) -> 8.
decode_constrained_number(Buffer,{Lb,Ub}) ->
Range = Ub - Lb + 1,
@@ -738,18 +744,12 @@ decode_constrained_number(Buffer,{Lb,Ub}) ->
getoctets(Buffer,1);
Range =< 65536 ->
getoctets(Buffer,2);
- Range =< 16#1000000 ->
- {Len,Bytes2} = decode_length(Buffer,{1,3}),
- {Octs,Bytes3} = getoctets_as_list(Bytes2,Len),
- {dec_pos_integer(Octs),Bytes3};
- Range =< 16#100000000 ->
- {Len,Bytes2} = decode_length(Buffer,{1,4}),
- {Octs,Bytes3} = getoctets_as_list(Bytes2,Len),
- {dec_pos_integer(Octs),Bytes3};
- Range =< 16#10000000000 ->
- {Len,Bytes2} = decode_length(Buffer,{1,5}),
- {Octs,Bytes3} = getoctets_as_list(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_list(Bytes, Len),
+ {binary:decode_unsigned(binary:list_to_bin(Octs)), RestBytes};
true ->
exit({not_supported,{integer_range,Range}})
end,
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,<<Val2:16>>};
[20,2,<<Val2:16>>];
- 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,