1 |
|
%% Copyright (c) 2015 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
-module(pcapng_options). |
16 |
|
-export([parse/2]). |
17 |
|
-export([parse/3]). |
18 |
|
|
19 |
|
-export([null_terminated/1]). |
20 |
|
|
21 |
|
|
22 |
|
-spec parse(binary(), fun((binary()) -> integer())) -> map(). |
23 |
|
|
24 |
|
parse(<<>>, _) -> |
25 |
1526 |
#{}; |
26 |
|
|
27 |
|
parse(Packet, I) -> |
28 |
:-( |
parse(Packet, I, #{}). |
29 |
|
|
30 |
|
|
31 |
|
-spec parse(binary(), fun((binary()) -> integer()), map()) -> map(). |
32 |
|
|
33 |
|
parse(<<>>, _, _) -> |
34 |
6 |
#{}; |
35 |
|
|
36 |
|
parse(Packet, I, Mapping) -> |
37 |
28 |
options(Packet, I, maps:merge(Mapping, mapping()), #{}). |
38 |
|
|
39 |
|
|
40 |
|
options(<<0:16, 0:16>>, _, _, Options) -> |
41 |
28 |
Options; |
42 |
|
|
43 |
|
options(<<Code:16/bits, Length:16/bits, Remainder/binary >>, I, Mapping, |
44 |
|
Options) -> |
45 |
82 |
#{body := Value, |
46 |
|
remainder := OtherOptions} = pcapng:variable_length(Remainder, I(Length)), |
47 |
|
|
48 |
82 |
case maps:get(I(Code), Mapping, I(Code)) of |
49 |
|
{Name, #{module := Module, function := Function}} -> |
50 |
8 |
options(OtherOptions, I, Mapping, |
51 |
|
Options#{Name => Module:Function(Value)}); |
52 |
|
|
53 |
|
{Name, Transform} when is_function(Transform) -> |
54 |
37 |
options(OtherOptions, I, Mapping, |
55 |
|
Options#{Name => Transform(Value)}); |
56 |
|
|
57 |
|
Name -> |
58 |
37 |
options(OtherOptions, I, Mapping, maps:put(Name, Value, Options)) |
59 |
|
end. |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
mapping() -> |
64 |
28 |
#{1 => {opt_comment, fun null_terminated/1}}. |
65 |
|
|
66 |
|
-spec null_terminated(binary()) -> binary(). |
67 |
|
|
68 |
|
null_terminated(Comment) -> |
69 |
29 |
ZeroTerminated = byte_size(Comment) - 1, |
70 |
29 |
case binary:match(Comment, <<0>>) of |
71 |
|
{ZeroTerminated, 1} -> |
72 |
9 |
binary:part(Comment, 0, ZeroTerminated); |
73 |
|
nomatch -> |
74 |
20 |
Comment |
75 |
|
end. |