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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
%% ====================================================================
%% Program which resulted in an erl_types crash due to incomplete
%% handling of tuple_sets in function inf_tuples_in_sets/4.
%% Reported by Alexey Romanov on 10/10/2010 and fixed 16/10/2010.
%% Stavros Aronis provided a better fix of the issue on 8/11/2010.
%% ====================================================================
-module(tuple_set_crash).
-export([test/5]).
%% ====================================================================
-define(PREPEND_IF_BIT_SET(BitMap, Bit,
PatternInBinary, PatternInList,
OldRestVar, NewRestVar,
OldAccVar, NewAccVar),
case byteset:contains(Bit, BitMap) of
true ->
<<PatternInBinary, NewRestVar/binary>> = OldRestVar,
NewAccVar = [PatternInList | OldAccVar];
false ->
NewRestVar = OldRestVar,
NewAccVar = OldAccVar
end).
%% ====================================================================
%% Types used in parsing binaries
-define(BITMAP1, 8/integer-big-unsigned).
-define(BYTE, 8/integer-little-unsigned).
-define(WORD, 16/integer-little-unsigned).
-define(DWORD, 32/integer-little-unsigned).
-define(DATE, 16/integer-little-signed).
-define(TIME, 32/float-little-unsigned).
-define(TINY_STRING_M(Var, Size), Size:?BYTE, Var:Size/binary).
-define(SMALL_STRING_M(Var, Size), Size:?WORD, Var:Size/binary).
-type config_change() ::
{device_properties |
video_target |
audio_target |
video_device |
audio_device |
video_output |
audio_output, [{atom(), any()}]}.
-type message_from_server() ::
ok |
{error, atom()} |
config_change() |
{media_item_url_reply, integer(), binary()}.
%% ====================================================================
-spec test(integer(), [integer()], binary(), binary(), binary()) -> {binary(), binary()}.
test(_TargetId, [], _Key, IVT, IVF) ->
{IVT, IVF};
test(TargetId, [Date | DateTail], Key, IVT, IVF) ->
PlayListRequest = play_list_request(TargetId, Date),
{ok, Reply, IVT1, IVF1} = culprit(PlayListRequest, Key, IVT, IVF),
case Reply of
{play_list, _Playlist} ->
test(TargetId, DateTail, Key, IVT1, IVF1);
{error, 16#11} ->
{IVT1, IVF1} %% we can finish early
end.
-spec culprit(binary(), binary(), binary(), binary()) ->
{ok, message_from_server(), binary(), binary()}.
culprit(Message, Key, IVecToServer, IVecFromServer) ->
{Packet, NewIVecToServer} = message_to_packet(Message, Key, IVecToServer),
Message = crypto:block_decrypt(aes_cbc128, Key, IVecFromServer, Packet),
NewIVecFromServer = crypto:next_iv(aes_cbc, Packet),
ParsedMessage = parse_message(Message),
{ok, ParsedMessage, NewIVecToServer, NewIVecFromServer}.
%% ====================================================================
-spec play_list_request(integer(), integer()) -> binary().
play_list_request(TargetId, Date) ->
<<16#06:?WORD, TargetId:?DWORD, Date:?DATE>>.
-spec parse_message(binary()) -> message_from_server().
parse_message(<<MessageID:?WORD, Rest/binary>>) ->
case MessageID of
16#00 -> parse_error_code(Rest);
16#22 -> {device_properties, parse_device_properties(Rest)};
16#24 -> {video_target_info, parse_video_target_info(Rest)};
16#25 -> {audio_target_info, parse_audio_target_info(Rest)};
16#26 -> {video_device_info, parse_av_device_info(Rest)};
16#27 -> {audio_device_info, parse_av_device_info(Rest)};
16#28 -> {video_output_info, parse_video_output_info(Rest)};
16#29 -> {audio_output_info, parse_audio_output_info(Rest)}
end.
-spec parse_error_code(binary()) -> ok | {error, integer()}.
parse_error_code(<<ErrorCode:?BYTE, _Padding/binary>>) ->
case ErrorCode of
0 -> ok;
_ -> {error, ErrorCode}
end.
-spec parse_device_properties(binary()) -> config_change().
parse_device_properties(<<BitMap:?BITMAP1, Rest/binary>>) ->
Acc0 = [],
?PREPEND_IF_BIT_SET(BitMap, 0,
FwVersion:3/binary, {fw_version, FwVersion},
Rest, Rest1, Acc0, Acc1),
?PREPEND_IF_BIT_SET(BitMap, 1,
?TINY_STRING_M(ControllerName, _S1),
{controller_name, ControllerName},
Rest1, Rest2, Acc1, Acc2),
?PREPEND_IF_BIT_SET(BitMap, 2,
?SMALL_STRING_M(ControllerDescription, _S2),
{controller_description, ControllerDescription},
Rest2, Rest3, Acc2, Acc3),
?PREPEND_IF_BIT_SET(BitMap, 3,
ControllerStatus:?BYTE,
{controller_status, ControllerStatus},
Rest3, _Padding, Acc3, Acc4),
Acc4.
-spec parse_video_target_info(binary()) -> config_change().
parse_video_target_info(<<TargetId:?DWORD, Status:?BYTE, _Padding/binary>>) ->
[{target_id, TargetId}, {status, Status}].
-spec parse_audio_target_info(binary()) -> [config_change()].
parse_audio_target_info(<<TargetId:?DWORD, BitMap:?BITMAP1, Rest/binary>>) ->
Acc0 = [{target_id, TargetId}],
?PREPEND_IF_BIT_SET(BitMap, 0,
Status:?BYTE, {status, Status},
Rest, Rest1, Acc0, Acc1),
?PREPEND_IF_BIT_SET(BitMap, 1,
MasterVolume:?WORD, {master_volume, MasterVolume},
Rest1, _Padding, Acc1, Acc2),
Acc2.
-spec parse_av_device_info(binary()) -> [config_change()].
parse_av_device_info(<<DeviceId:?DWORD, BitMap:?BITMAP1, Rest/binary>>) ->
Acc0 = [{device_id, DeviceId}],
?PREPEND_IF_BIT_SET(BitMap, 0,
TargetId:?DWORD, {target_id, TargetId},
Rest, Rest1, Acc0, Acc1),
?PREPEND_IF_BIT_SET(BitMap, 1,
?TINY_STRING_M(Model, _S1), {model, Model},
Rest1, Rest2, Acc1, Acc2),
?PREPEND_IF_BIT_SET(BitMap, 2,
Address:?BYTE, {address, Address},
Rest2, Rest3, Acc2, Acc3),
?PREPEND_IF_BIT_SET(BitMap, 3,
Status:?BYTE, {status, Status},
Rest3, _Padding, Acc3, Acc4),
Acc4.
-spec parse_video_output_info(binary()) -> [config_change()].
parse_video_output_info(<<Output:?DWORD, BitMap:?BITMAP1, Rest/binary>>) ->
Acc0 = [{output_id, Output}],
?PREPEND_IF_BIT_SET(BitMap, 0,
DeviceId:?DWORD, {device_id, DeviceId},
Rest, Rest1, Acc0, Acc1),
?PREPEND_IF_BIT_SET(BitMap, 1,
?TINY_STRING_M(DisplayType, _S1),
{display_type, DisplayType},
Rest1, Rest2, Acc1, Acc2),
?PREPEND_IF_BIT_SET(BitMap, 2,
AudioVolume:?WORD,
{audio_volume, AudioVolume},
Rest2, _Padding, Acc2, Acc3),
Acc3.
-spec parse_audio_output_info(binary()) -> [config_change()].
parse_audio_output_info(<<Output:?DWORD, BitMap:?BITMAP1, Rest/binary>>) ->
Acc0 = [{output_id, Output}],
?PREPEND_IF_BIT_SET(BitMap, 0,
DeviceId:?DWORD, {device_id, DeviceId},
Rest, Rest1, Acc0, Acc1),
?PREPEND_IF_BIT_SET(BitMap, 1,
AudioVolume:?WORD, {audio_volume, AudioVolume},
Rest1, Rest2, Acc1, Acc2),
?PREPEND_IF_BIT_SET(BitMap, 2,
Delay:?WORD, {delay, Delay},
Rest2, _Padding, Acc2, Acc3),
Acc3.
-spec message_to_packet(binary(), binary(), binary()) -> {binary(), binary()}.
message_to_packet(Message, Key, IVec) ->
PaddedMessage = pad_pkcs5(Message),
Packet = crypto:block_encrypt(aes_cbc128, Key, IVec, PaddedMessage),
TotalSize = byte_size(Packet),
NewIVec = crypto:next_iv(aes_cbc, Packet),
{<<TotalSize:?WORD, Packet/binary>>, NewIVec}.
-spec pad_pkcs5(binary()) -> binary().
pad_pkcs5(Message) ->
Size = byte_size(Message),
PaddingSize = case Size rem 16 of
0 -> 0;
Rem -> 16 - Rem
end,
pad_pkcs5(Message, PaddingSize, PaddingSize).
-spec pad_pkcs5(binary(), integer(), integer()) -> binary().
pad_pkcs5(Message, _PaddingSize, 0) ->
Message;
pad_pkcs5(Message, PaddingSize, PaddingSizeRemaining) ->
pad_pkcs5(<<Message/binary, PaddingSize:?BYTE>>,
PaddingSize, PaddingSizeRemaining - 1).
|