Skip to content

Commit d24c732

Browse files
committed
Merge branch 'maint'
* maint: asn1: Fix several bugs in encoding and decoding of REALs
2 parents 64185e7 + 97a9dcf commit d24c732

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

lib/asn1/doc/guides/asn1_getting_started.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,21 @@ It is assigned a value in Erlang as follows:
417417

418418
```text
419419
R1value1 = "2.14",
420-
R1value2 = {256,10,-2},
420+
R1value2 = {256,10,-2}
421421
```
422422

423423
In the last line, notice that the tuple \{256,10,-2\} is the real number 2.56 in
424424
a special notation, which encodes faster than simply stating the number as
425425
`"2.56"`. The arity three tuple is `{Mantissa,Base,Exponent}`, that is,
426-
Mantissa \* Base^Exponent.
426+
`Mantissa * Base^Exponent`.
427+
428+
The following special values are also recognized:
429+
430+
```text
431+
R1value3 = 0,
432+
R1value4 = 'PLUS-INFINITY',
433+
R1value5 = 'MINUS-INFINITY'
434+
```
427435

428436
### NULL
429437

lib/asn1/src/asn1ct_gen_ber_bin_v2.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ gen_encode_prim(_Erules, #type{}=D, DoTag, Value) ->
236236
asn1ct_name:new(realsize),
237237
emit(["begin",nl,
238238
{curr,realval}," = ",
239-
{call,real_common,ber_encode_real,[Value]},com,nl,
239+
{call,real_common,encode_real,[Value]},com,nl,
240240
{curr,realsize}," = ",
241241
{call,erlang,byte_size,[{curr,realval}]},com,nl,
242242
{call,ber,encode_tags,

lib/asn1/src/asn1rtt_real_common.erl

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
%%
2020
-module(asn1rtt_real_common).
2121

22-
-export([encode_real/1,decode_real/1,
23-
ber_encode_real/1]).
22+
-export([encode_real/1,decode_real/1]).
2423

2524
%%============================================================================
2625
%%
@@ -30,14 +29,14 @@
3029
%% encode real value
3130
%%============================================================================
3231

33-
ber_encode_real(0) ->
34-
{[],0};
35-
ber_encode_real('PLUS-INFINITY') ->
36-
{[64],1};
37-
ber_encode_real('MINUS-INFINITY') ->
38-
{[65],1};
39-
ber_encode_real(Val) when is_tuple(Val); is_list(Val) ->
40-
encode_real(Val).
32+
encode_real(0) ->
33+
<<>>;
34+
encode_real('PLUS-INFINITY') ->
35+
<<2#0100_0000>>;
36+
encode_real('MINUS-INFINITY') ->
37+
<<2#0100_0001>>;
38+
encode_real(Val) when is_tuple(Val); is_list(Val) ->
39+
encode_real([], Val).
4140

4241
%%%%%%%%%%%%%%
4342
%% only base 2 encoding!
@@ -73,9 +72,6 @@ ber_encode_real(Val) when is_tuple(Val); is_list(Val) ->
7372
%% bit shifted until it is an odd number. Thus, do this for BER as
7473
%% well.
7574

76-
encode_real(Real) ->
77-
encode_real([], Real).
78-
7975
encode_real(_C, {Mantissa, Base, Exponent}) when Base =:= 2 ->
8076
%% io:format("Mantissa: ~w Base: ~w, Exp: ~w~n",[Man, Base, Exp]),
8177
{Man,ExpAdd} = truncate_zeros(Mantissa), %% DER adjustment
@@ -214,14 +210,16 @@ decode_real(Buffer) ->
214210
{RealVal,<<>>,Sz} = decode_real2(Buffer, [], Sz, 0),
215211
RealVal.
216212

217-
decode_real2(Buffer, _C, 0, _RemBytes) ->
218-
{0,Buffer};
213+
decode_real2(<<>>, _C, 0, _RemBytes) ->
214+
{0,<<>>,0};
219215
decode_real2(Buffer0, _C, Len, RemBytes1) ->
220216
<<First, Buffer2/binary>> = Buffer0,
221217
if
222-
First =:= 2#01000000 -> {'PLUS-INFINITY', Buffer2};
223-
First =:= 2#01000001 -> {'MINUS-INFINITY', Buffer2};
224-
First =:= 1 orelse First =:= 2 orelse First =:= 3 ->
218+
First =:= 2#01000000 ->
219+
{'PLUS-INFINITY', Buffer2, 1};
220+
First =:= 2#01000001 ->
221+
{'MINUS-INFINITY', Buffer2, 1};
222+
First =:= 1; First =:= 2; First =:= 3 ->
225223
%% character string encoding of base 10
226224
{NRx,Rest} = split_binary(Buffer2,Len-1),
227225
{binary_to_list(NRx),Rest,Len};

lib/asn1/test/testPrim.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,14 @@ real(_Rules) ->
226226
%%==========================================================
227227
%% AngleInRadians ::= REAL
228228
%%==========================================================
229-
229+
230+
%% Zero
231+
real_roundtrip('AngleInRadians', 0),
232+
233+
%% Infinities
234+
real_roundtrip('AngleInRadians', 'MINUS-INFINITY'),
235+
real_roundtrip('AngleInRadians', 'PLUS-INFINITY'),
236+
230237
%% Base 2
231238
real_roundtrip('AngleInRadians', {1,2,1}),
232239
real_roundtrip('AngleInRadians', {129,2,1}),

0 commit comments

Comments
 (0)