-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathunpacker.ex
More file actions
225 lines (195 loc) · 8.87 KB
/
unpacker.ex
File metadata and controls
225 lines (195 loc) · 8.87 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
defmodule MessagePack.Unpacker do
@spec unpack(binary) :: { :ok, term } | { :error, term }
@spec unpack(binary, Keyword.t) :: { :ok, term } | { :error, term }
def unpack(binary, options \\ []) when is_binary(binary) do
options = parse_options(options)
case do_unpack(binary, options) do
{ :error, _ } = error ->
error
{ result, "" } ->
{ :ok, result }
{ _, bin } when is_binary(bin) ->
{ :error, :not_just_binary }
end
end
@spec unpack!(binary) :: term | no_return
@spec unpack!(binary, Keyword.t) :: term | no_return
def unpack!(binary, options \\ []) when is_binary(binary) do
case unpack(binary, options) do
{ :ok, result } ->
result
{ :error, error } ->
raise ArgumentError, message: inspect(error)
end
end
@spec unpack(binary) :: { :ok, { term, binary } } | { :error, term }
@spec unpack(binary, Keyword.t) :: { :ok, { term, binary } } | { :error, term }
def unpack_once(binary, options \\ []) when is_binary(binary) do
options = parse_options(options)
case do_unpack(binary, options) do
{ :error, _ } = error ->
error
result ->
{ :ok, result }
end
end
@spec unpack!(binary) :: { term, binary } | no_return
@spec unpack!(binary, Keyword.t) :: { term, binary } | no_return
def unpack_once!(binary, options \\ []) when is_binary(binary) do
case unpack_once(binary, options) do
{ :ok, result } ->
result
{ :error, error } ->
raise ArgumentError, message: inspect(error)
end
end
defp parse_options(options) do
enable_string = !!options[:enable_string]
{ packer, unpacker } = case options[:ext] do
nil ->
{ nil, nil }
mod when is_atom(mod) ->
{ &mod.pack/1, &mod.unpack/2 }
list when is_list(list) ->
{ list[:packer], list[:unpacker] }
end
%{enable_string: enable_string, ext_packer: packer, ext_unpacker: unpacker}
end
# positive fixnum
defp do_unpack(<< 0 :: 1, v :: 7, rest :: binary >>, _), do: { v, rest }
# negative fixnum
defp do_unpack(<< 0b111 :: 3, v :: 5, rest :: binary >>, _), do: { v - 0b100000, rest }
# uint
defp do_unpack(<< 0xCC, uint :: 8-unsigned-integer, rest :: binary >>, _), do: { uint, rest }
defp do_unpack(<< 0xCD, uint :: 16-big-unsigned-integer-unit(1), rest :: binary >>, _), do: { uint, rest }
defp do_unpack(<< 0xCE, uint :: 32-big-unsigned-integer-unit(1), rest :: binary >>, _), do: { uint, rest }
defp do_unpack(<< 0xCF, uint :: 64-big-unsigned-integer-unit(1), rest :: binary >>, _), do: { uint, rest }
# int
defp do_unpack(<< 0xD0, int :: 8-signed-integer, rest :: binary >>, _), do: { int, rest }
defp do_unpack(<< 0xD1, int :: 16-big-signed-integer-unit(1), rest :: binary >>, _), do: { int, rest }
defp do_unpack(<< 0xD2, int :: 32-big-signed-integer-unit(1), rest :: binary >>, _), do: { int, rest }
defp do_unpack(<< 0xD3, int :: 64-big-signed-integer-unit(1), rest :: binary >>, _), do: { int, rest }
# nil
defp do_unpack(<< 0xC0, rest :: binary >>, _), do: { nil, rest }
# boolean
defp do_unpack(<< 0xC2, rest :: binary >>, _), do: { false, rest }
defp do_unpack(<< 0xC3, rest :: binary >>, _), do: { true, rest }
# float
defp do_unpack(<< 0xCA, float :: 32-float-unit(1), rest :: binary >>, _), do: { float, rest }
defp do_unpack(<< 0xCB, float :: 64-float-unit(1), rest :: binary >>, _), do: { float, rest }
# old row format
defp do_unpack(<< 0b101 :: 3, len :: 5, binary :: size(len)-binary, rest :: binary >>, %{enable_string: false}), do: { binary, rest }
defp do_unpack(<< 0xD9, len :: 8-unsigned-integer-unit(1), binary :: size(len)-binary, rest :: binary >>, %{enable_string: false}), do: {binary, rest }
defp do_unpack(<< 0xDA, len :: 16-unsigned-integer-unit(1), binary :: size(len)-binary, rest :: binary >>, %{enable_string: false}), do: { binary, rest }
defp do_unpack(<< 0xDB, len :: 32-unsigned-integer-unit(1), binary :: size(len)-binary, rest :: binary >>, %{enable_string: false}), do: { binary, rest }
# string
defp do_unpack(<< 0b101 :: 3, len :: 5, binary :: size(len)-binary, rest :: binary >>, %{enable_string: true}) do
if String.valid?(binary) do
{ binary, rest }
else
{ :error, { :invalid_string, binary } }
end
end
defp do_unpack(<< 0xD9, len :: 8-unsigned-integer-unit(1), binary :: size(len)-binary, rest :: binary >>, %{enable_string: true}) do
if String.valid?(binary) do
{ binary, rest }
else
{ :error, { :invalid_string, binary } }
end
end
defp do_unpack(<< 0xDA, len :: 16-unsigned-integer-unit(1), binary :: size(len)-binary, rest :: binary >>, %{enable_string: true}) do
if String.valid?(binary) do
{ binary, rest }
else
{ :error, { :invalid_string, binary } }
end
end
defp do_unpack(<< 0xDB, len :: 32-unsigned-integer-unit(1), binary :: size(len)-binary, rest :: binary >>, %{enable_string: true}) do
if String.valid?(binary) do
{ binary, rest }
else
{ :error, { :invalid_string, binary } }
end
end
# binary
defp do_unpack(<< 0xC4, len :: 8-unsigned-integer-unit(1), binary :: size(len)-binary, rest :: binary >>, _), do: { binary, rest }
defp do_unpack(<< 0xC5, len :: 16-unsigned-integer-unit(1), binary :: size(len)-binary, rest :: binary >>, _), do: { binary, rest }
defp do_unpack(<< 0xC6, len :: 32-unsigned-integer-unit(1), binary :: size(len)-binary, rest :: binary >>, _), do: { binary, rest }
# array
defp do_unpack(<< 0b1001 :: 4, len :: 4, rest :: binary >>, options), do: unpack_array(rest, len, options)
defp do_unpack(<< 0xDC, len :: 16-big-unsigned-integer-unit(1), rest :: binary >>, options), do: unpack_array(rest, len, options)
defp do_unpack(<< 0xDD, len :: 32-big-unsigned-integer-unit(1), rest :: binary >>, options), do: unpack_array(rest, len, options)
# map
defp do_unpack(<< 0b1000 :: 4, len :: 4, rest :: binary >>, options), do: unpack_map(rest, len, options)
defp do_unpack(<< 0xDE, len :: 16-big-unsigned-integer-unit(1), rest :: binary >>, options), do: unpack_map(rest, len, options)
defp do_unpack(<< 0xDF, len :: 32-big-unsigned-integer-unit(1), rest :: binary >>, options), do: unpack_map(rest, len, options)
defp do_unpack(<< 0xD4, type :: 8, data :: 1-binary, rest :: binary >>, %{ext_unpacker: unpacker}) do
unpack_ext(unpacker, type, data, rest)
end
defp do_unpack(<< 0xD5, type :: 8, data :: 2-binary, rest :: binary >>, %{ext_unpacker: unpacker}) do
unpack_ext(unpacker, type, data, rest)
end
defp do_unpack(<< 0xD6, type :: 8, data :: 4-binary, rest :: binary >>, %{ext_unpacker: unpacker}) do
unpack_ext(unpacker, type, data, rest)
end
defp do_unpack(<< 0xD7, type :: 8, data :: 8-binary, rest :: binary >>, %{ext_unpacker: unpacker}) do
unpack_ext(unpacker, type, data, rest)
end
defp do_unpack(<< 0xD8, type :: 8, data :: 16-binary, rest :: binary >>, %{ext_unpacker: unpacker}) do
unpack_ext(unpacker, type, data, rest)
end
defp do_unpack(<< 0xC7, len :: 8-unsigned-integer-unit(1), type :: 8, data :: size(len)-binary, rest :: binary >>, %{ext_unpacker: unpacker}) do
unpack_ext(unpacker, type, data, rest)
end
defp do_unpack(<< 0xC8, len :: 16-big-unsigned-integer-unit(1), type :: 8, data :: size(len)-binary, rest :: binary >>, %{ext_unpacker: unpacker}) do
unpack_ext(unpacker, type, data, rest)
end
defp do_unpack(<< 0xC9, len :: 32-big-unsigned-integer-unit(1), type :: 8, data :: size(len)-binary, rest :: binary >>, %{ext_unpacker: unpacker}) do
unpack_ext(unpacker, type, data, rest)
end
# invalid prefix
defp do_unpack(<< 0xC1, _ :: binary >>, _), do: { :error, { :invalid_prefix, 0xC1 } }
defp do_unpack(_, _), do: { :error, :incomplete }
defp unpack_array(binary, len, options) do
do_unpack_array(binary, len, [], options)
end
defp do_unpack_array(rest, 0, acc, _) do
{ :lists.reverse(acc), rest }
end
defp do_unpack_array(binary, len, acc, options) do
case do_unpack(binary, options) do
{ :error, _ } = error ->
error
{ term, rest } ->
do_unpack_array(rest, len - 1, [term|acc], options)
end
end
defp unpack_map(binary, len, options) do
do_unpack_map(binary, len, [], options)
end
defp do_unpack_map(rest, 0, acc, _) do
{ Enum.into(acc,%{}), rest }
end
defp do_unpack_map(binary, len, acc, options) do
case do_unpack(binary, options) do
{ :error, _ } = error ->
error
{ key, rest } ->
case do_unpack(rest, options) do
{ :error, _ } = error ->
error
{ value, rest } ->
do_unpack_map(rest, len - 1, [{key, value}|acc], options)
end
end
end
def unpack_ext(nil, _, _, _), do: { :error, :undefined_ext }
def unpack_ext(unpacker, type, data, rest) when is_function(unpacker) do
case unpacker.(type, data) do
{ :ok, term } ->
{ term, rest }
{ :error, _ } = error ->
error
end
end
end