|
9 | 9 |
|
10 | 10 | -export([decompose/1, |
11 | 11 | decompose_from_binary/1, |
12 | | - decompose_from_binary_safe/1, |
13 | 12 | recompose/1, |
14 | 13 | recompose_to_binary/1]). |
15 | 14 |
|
|
33 | 32 | -spec decompose(pid()) -> decomposed_pid(). |
34 | 33 | decompose(Pid) -> |
35 | 34 | Bin = term_to_binary(Pid, [{minor_version, 2}]), |
36 | | - decompose_from_binary(Bin). |
| 35 | + {ok, #{node := NodeBin} = Parts} = decompose_from_binary(Bin), |
| 36 | + %% Safe: the input is a real local pid, so its node atom always exists. |
| 37 | + Parts#{node := binary_to_existing_atom(NodeBin, utf8)}. |
37 | 38 |
|
38 | | --spec decompose_from_binary(binary()) -> decomposed_pid(). |
39 | | -decompose_from_binary(Bin) -> |
40 | | - <<?TTB_PREFIX, ?NEW_PID_EXT, PidData/binary>> = Bin, |
41 | | - {Node, Rest} = case PidData of |
42 | | - <<?ATOM_UTF8_EXT, Len:16/integer, Node0:Len/binary, Rest1/binary>> -> |
43 | | - {Node0, Rest1}; |
44 | | - <<?SMALL_ATOM_UTF8_EXT, Len/integer, Node0:Len/binary, Rest1/binary>> -> |
45 | | - {Node0, Rest1} |
46 | | - end, |
47 | | - <<ID:32/integer, Serial:32/integer, Creation:32/integer>> = Rest, |
48 | | - #{node => binary_to_atom(Node, utf8), |
49 | | - id => ID, |
50 | | - serial => Serial, |
51 | | - creation => Creation}. |
52 | | - |
53 | | -%% Returns the node as a binary and returns 'error' on malformed input. |
54 | | --spec decompose_from_binary_safe(binary()) -> {ok, decomposed_pid_bin()} | error. |
55 | | -decompose_from_binary_safe(<<?TTB_PREFIX, ?NEW_PID_EXT, |
56 | | - ?ATOM_UTF8_EXT, Len:16/integer, Node:Len/binary, |
57 | | - ID:32/integer, Serial:32/integer, |
58 | | - Creation:32/integer>>) -> |
59 | | - {ok, #{node => Node, id => ID, serial => Serial, creation => Creation}}; |
60 | | -decompose_from_binary_safe(<<?TTB_PREFIX, ?NEW_PID_EXT, |
61 | | - ?SMALL_ATOM_UTF8_EXT, Len:8/integer, Node:Len/binary, |
62 | | - ID:32/integer, Serial:32/integer, |
63 | | - Creation:32/integer>>) -> |
64 | | - {ok, #{node => Node, id => ID, serial => Serial, creation => Creation}}; |
65 | | -decompose_from_binary_safe(_) -> |
| 39 | +%% Returns the node name as a binary and 'error' on malformed input. |
| 40 | +-spec decompose_from_binary(binary()) -> {ok, decomposed_pid_bin()} | error. |
| 41 | +decompose_from_binary(<<?TTB_PREFIX, ?NEW_PID_EXT, |
| 42 | + ?ATOM_UTF8_EXT, Len:16/integer, Node:Len/binary, |
| 43 | + ID:32/integer, Serial:32/integer, |
| 44 | + Creation:32/integer>>) -> |
| 45 | + {ok, |
| 46 | + #{node => Node, |
| 47 | + id => ID, |
| 48 | + serial => Serial, |
| 49 | + creation => Creation}}; |
| 50 | +decompose_from_binary(<<?TTB_PREFIX, ?NEW_PID_EXT, |
| 51 | + ?SMALL_ATOM_UTF8_EXT, Len:8/integer, Node:Len/binary, |
| 52 | + ID:32/integer, Serial:32/integer, |
| 53 | + Creation:32/integer>>) -> |
| 54 | + {ok, |
| 55 | + #{node => Node, |
| 56 | + id => ID, |
| 57 | + serial => Serial, |
| 58 | + creation => Creation}}; |
| 59 | +decompose_from_binary(_) -> |
66 | 60 | error. |
67 | 61 |
|
68 | 62 | -spec recompose_to_binary(decomposed_pid()) -> binary(). |
|
0 commit comments