summaryrefslogtreecommitdiff
path: root/src/plop_compat.erl
blob: 4d45590ef60b12f00dd46f793d6a442e7cb19c6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
%%% Copyright (c) 2016, NORDUnet A/S.
%%% See LICENSE for licensing information.

-module(plop_compat).
-export([unpack_spki/1, timestamp/0, monotonic_time/1, start_timer/4]).
-include_lib("public_key/include/public_key.hrl").

unpack_spki(SPKI) ->
    unpack_spki(erlang:system_info(otp_release), SPKI).
timestamp() ->
    timestamp(erlang:system_info(otp_release)).
monotonic_time(Unit) ->
    monotonic_time(erlang:system_info(otp_release), Unit).
start_timer(Time, Dest, Msg, Options) ->
    start_timer(erlang:system_info(otp_release), Time, Dest, Msg, Options).

unpack_spki("R16" ++ _, SPKI) ->
    #'SubjectPublicKeyInfo'{subjectPublicKey = {_, Octets},
                            algorithm = Algorithm} = SPKI,
    {Octets, Algorithm};
unpack_spki("17", SPKI) ->
    #'SubjectPublicKeyInfo'{subjectPublicKey = {_, Octets},
                            algorithm = Algorithm} = SPKI,
    {Octets, Algorithm};
unpack_spki("18", SPKI) ->
    #'SubjectPublicKeyInfo'{subjectPublicKey = Octets,
                            algorithm = Algorithm} = SPKI,
    {Octets, Algorithm};
unpack_spki("19", SPKI) ->
    #'SubjectPublicKeyInfo'{subjectPublicKey = Octets,
                            algorithm = Algorithm} = SPKI,
    {Octets, Algorithm}.

%% <=R17: now/0, >=R18 timestamp/0
timestamp("R16" ++ _) ->
    erlang:now();
timestamp("17") ->
    erlang:now();
timestamp("18") ->
    erlang:timestamp();
timestamp("19") ->
    erlang:timestamp().

monotonic_time("R16" ++ _, millisecond) ->
    {MeS, S, MiS} = timestamp(),
    MeS * 1000000 + S * 1000 + MiS;
monotonic_time("17", millisecond) ->
    {MeS, S, MiS} = timestamp(),
    MeS * 1000000 + S * 1000 + MiS;
monotonic_time("18", Unit) ->
    erlang:monotonic_time(Unit);
monotonic_time("19", Unit) ->
    erlang:monotonic_time(Unit).

start_timer("R16" ++ _, Time, Dest, Msg, [{abs, true}]) ->
    erlang:start_timer(max(0, Time - monotonic_time(millisecond)), Dest, Msg);
start_timer("17", Time, Dest, Msg, [{abs, true}]) ->
    erlang:start_timer(max(0, Time - monotonic_time(millisecond)), Dest, Msg);
start_timer("18", Time, Dest, Msg, Options) ->
    erlang:start_timer(Time, Dest, Msg, Options);
start_timer("19", Time, Dest, Msg, Options) ->
    erlang:start_timer(Time, Dest, Msg, Options).