| 1 |
|
%% Copyright (c) 2023 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 |
|
|
| 16 |
|
-module(msmp_null_bitmap). |
| 17 |
|
|
| 18 |
|
|
| 19 |
|
-export([decode/1]). |
| 20 |
|
-export([decode/2]). |
| 21 |
|
-export([encode/0]). |
| 22 |
|
-export([encode/1]). |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
encode() -> |
| 26 |
1 |
?FUNCTION_NAME(0). |
| 27 |
|
|
| 28 |
|
|
| 29 |
|
encode(Offset) -> |
| 30 |
2 |
fun |
| 31 |
|
(Decoded) -> |
| 32 |
2 |
Padding = padding(length(Decoded), Offset), |
| 33 |
|
|
| 34 |
2 |
(narcs_combinator:map_result( |
| 35 |
|
msmp_narcs:sequence( |
| 36 |
|
[narcs_bytes:tag(<<0:Offset>>), |
| 37 |
|
msmp_narcs:many0( |
| 38 |
|
narcs_combinator:map_result( |
| 39 |
|
is_null(), |
| 40 |
|
narcs_bits:into_bit())), |
| 41 |
|
narcs_bytes:tag(<<0:Padding>>)]), |
| 42 |
|
fun erlang:list_to_bitstring/1))(Decoded) |
| 43 |
|
end. |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
is_null() -> |
| 47 |
2 |
fun |
| 48 |
|
(null) -> |
| 49 |
6 |
true; |
| 50 |
|
|
| 51 |
|
(_) -> |
| 52 |
2 |
false |
| 53 |
|
end. |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
padding(N, Offset) -> |
| 57 |
9 |
width(N, Offset) - Offset - N. |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
width(N, Offset) -> |
| 61 |
9 |
((N + 7 + Offset) div 8) * 8. |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
decode(N) -> |
| 65 |
1 |
?FUNCTION_NAME(N, 0). |
| 66 |
|
|
| 67 |
|
decode(N, Offset) -> |
| 68 |
7 |
Padding = padding(N, Offset), |
| 69 |
7 |
fun |
| 70 |
|
(<<_:Offset/bits, Bitmap:N/bits, 0:Padding, Remaining/bytes>>) -> |
| 71 |
7 |
{Remaining, [tof(Bit) || <<Bit:1>> <= Bitmap]}; |
| 72 |
|
|
| 73 |
|
(_) -> |
| 74 |
:-( |
nomatch |
| 75 |
|
end. |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
tof(1) -> |
| 79 |
6 |
true; |
| 80 |
|
tof(0) -> |
| 81 |
7 |
false. |