-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhlp_deserialize.m
408 lines (392 loc) · 11.8 KB
/
hlp_deserialize.m
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
function v = hlp_deserialize(m)
% Convert a serialized byte vector back into the corresponding MATLAB data structure.
% Data = hlp_deserialize(Bytes)
%
% In:
% Bytes : a representation of the original data as a byte stream
%
% Out:
% Data : some MATLAB data structure
%
% See also:
% hlp_serialize
%
% Examples:
% bytes = hlp_serialize(mydata);
% ... e.g. transfer the 'bytes' array over the network ...
% mydata = hlp_deserialize(bytes);
%
% Christian Kothe, Swartz Center for Computational Neuroscience, UCSD
% 2010-04-02
%
% adapted from deserialize.m
% (C) 2010 Tim Hutt
% wrap dispatcher
v = deserialize_value(uint8(m(:)),1);
end
% dispatch
function [v,pos] = deserialize_value(m,pos)
switch m(pos)
case {0,200}
[v,pos] = deserialize_string(m,pos);
case 128
[v,pos] = deserialize_struct(m,pos);
case {33,34,35,36,37,38,39}
[v,pos] = deserialize_cell(m,pos);
case {1,2,3,4,5,6,7,8,9,10}
[v,pos] = deserialize_scalar(m,pos);
case 133
[v,pos] = deserialize_logical(m,pos);
case {151,152,153}
[v,pos] = deserialize_handle(m,pos);
case {17,18,19,20,21,22,23,24,25,26}
[v,pos] = deserialize_numeric_simple(m,pos);
case 130
[v,pos] = deserialize_sparse(m,pos);
case 131
[v,pos] = deserialize_complex(m,pos);
case 132
[v,pos] = deserialize_char(m,pos);
case 134
[v,pos] = deserialize_object(m,pos);
otherwise
error('Unknown class');
end
end
% individual scalar
function [v,pos] = deserialize_scalar(m,pos)
classes = {'double','single','int8','uint8','int16','uint16','int32','uint32','int64','uint64'};
sizes = [8,4,1,1,2,2,4,4,8,8];
sz = sizes(m(pos));
% Data.
v = typecast(m(pos+1:pos+sz),classes{m(pos)});
pos = pos + 1 + sz;
end
% standard string
function [v,pos] = deserialize_string(m,pos)
if m(pos) == 0
% horizontal string: tag
pos = pos + 1;
% length (uint32)
nbytes = double(typecast(m(pos:pos+3),'uint32'));
pos = pos + 4;
% data (chars)
v = char(m(pos:pos+nbytes-1))';
pos = pos + nbytes;
else
% proper empty string: tag
[v,pos] = deal('',pos+1);
end
end
% general char array
function [v,pos] = deserialize_char(m,pos)
pos = pos + 1;
% Number of dims
ndms = double(m(pos));
pos = pos + 1;
% Dimensions
dms = double(typecast(m(pos:pos+ndms*4-1),'uint32')');
pos = pos + ndms*4;
nbytes = prod(dms);
% Data.
v = char(m(pos:pos+nbytes-1));
pos = pos + nbytes;
v = reshape(v,[dms 1 1]);
end
% general logical array
function [v,pos] = deserialize_logical(m,pos)
pos = pos + 1;
% Number of dims
ndms = double(m(pos));
pos = pos + 1;
% Dimensions
dms = double(typecast(m(pos:pos+ndms*4-1),'uint32')');
pos = pos + ndms*4;
nbytes = prod(dms);
% Data.
v = logical(m(pos:pos+nbytes-1));
pos = pos + nbytes;
v = reshape(v,[dms 1 1]);
end
% simple numerical matrix
function [v,pos] = deserialize_numeric_simple(m,pos)
classes = {'double','single','int8','uint8','int16','uint16','int32','uint32','int64','uint64'};
sizes = [8,4,1,1,2,2,4,4,8,8];
cls = classes{m(pos)-16};
sz = sizes(m(pos)-16);
pos = pos + 1;
% Number of dims
ndms = double(m(pos));
pos = pos + 1;
% Dimensions
dms = double(typecast(m(pos:pos+ndms*4-1),'uint32')');
pos = pos + ndms*4;
nbytes = prod(dms) * sz;
% Data.
v = typecast(m(pos:pos+nbytes-1),cls);
pos = pos + nbytes;
v = reshape(v,[dms 1 1]);
end
% complex matrix
function [v,pos] = deserialize_complex(m,pos)
pos = pos + 1;
[re,pos] = deserialize_numeric_simple(m,pos);
[im,pos] = deserialize_numeric_simple(m,pos);
v = complex(re,im);
end
% sparse matrix
function [v,pos] = deserialize_sparse(m,pos)
pos = pos + 1;
% matrix dims
u = double(typecast(m(pos:pos+7),'uint64'));
pos = pos + 8;
v = double(typecast(m(pos:pos+7),'uint64'));
pos = pos + 8;
% index vectors
[i,pos] = deserialize_numeric_simple(m,pos);
[j,pos] = deserialize_numeric_simple(m,pos);
if m(pos)
% real
pos = pos+1;
[s,pos] = deserialize_numeric_simple(m,pos);
else
% complex
pos = pos+1;
[re,pos] = deserialize_numeric_simple(m,pos);
[im,pos] = deserialize_numeric_simple(m,pos);
s = complex(re,im);
end
v = sparse(i,j,s,u,v);
end
% struct array
function [v,pos] = deserialize_struct(m,pos)
pos = pos + 1;
% Number of field names.
nfields = double(typecast(m(pos:pos+3),'uint32'));
pos = pos + 4;
% Field name lengths
fnLengths = double(typecast(m(pos:pos+nfields*4-1),'uint32'));
pos = pos + nfields*4;
% Field name char data
fnChars = char(m(pos:pos+sum(fnLengths)-1)).';
pos = pos + length(fnChars);
% Number of dims
ndms = double(typecast(m(pos:pos+3),'uint32'));
pos = pos + 4;
% Dimensions
dms = typecast(m(pos:pos+ndms*4-1),'uint32')';
pos = pos + ndms*4;
% Field names.
fieldNames = cell(length(fnLengths),1);
splits = [0; cumsum(double(fnLengths))];
for k=1:length(splits)-1
fieldNames{k} = fnChars(splits(k)+1:splits(k+1)); end
% Content.
v = reshape(struct(),[dms 1 1]);
if m(pos)
% using struct2cell
pos = pos + 1;
[contents,pos] = deserialize_cell(m,pos);
v = cell2struct(contents,fieldNames,1);
else
% using per-field cell arrays
pos = pos + 1;
for ff = 1:nfields
[contents,pos] = deserialize_cell(m,pos);
[v.(fieldNames{ff})] = deal(contents{:});
end
end
end
% cell array
function [v,pos] = deserialize_cell(m,pos)
kind = m(pos);
pos = pos + 1;
switch kind
case 33 % arbitrary/heterogenous cell array
% Number of dims
ndms = double(m(pos));
pos = pos + 1;
% Dimensions
dms = double(typecast(m(pos:pos+ndms*4-1),'uint32')');
pos = pos + ndms*4;
% Contents
v = cell([dms,1,1]);
for ii = 1:numel(v)
[v{ii},pos] = deserialize_value(m,pos); end
case 34 % cell scalars
[content,pos] = deserialize_value(m,pos);
v = cell(size(content));
for k=1:numel(v)
v{k} = content(k); end
case 35 % mixed-real cell scalars
[content,pos] = deserialize_value(m,pos);
v = cell(size(content));
for k=1:numel(v)
v{k} = content(k); end
[reality,pos] = deserialize_value(m,pos);
v(reality) = real(v(reality));
case 36 % cell array with horizontal or empty strings
[chars,pos] = deserialize_string(m,pos);
[lengths,pos] = deserialize_numeric_simple(m,pos);
[empty,pos] = deserialize_logical(m,pos);
v = cell(size(lengths));
splits = [0 cumsum(double(lengths(:)))'];
for k=1:length(lengths)
v{k} = chars(splits(k)+1:splits(k+1)); end
[v{empty}] = deal('');
case 37 % empty,known type
tag = m(pos);
pos = pos + 1;
switch tag
case 1 % double - []
prot = [];
case 33 % cell - {}
prot = {};
case 128 % struct - struct()
prot = struct();
otherwise
error('Unsupported type tag.');
end
% Number of dims
ndms = double(m(pos));
pos = pos + 1;
% Dimensions
dms = typecast(m(pos:pos+ndms*4-1),'uint32')';
pos = pos + ndms*4;
% Create content
v = repmat({prot},dms);
case 38 % empty, prototype available
% Prototype.
[prot,pos] = deserialize_value(m,pos);
% Number of dims
ndms = double(m(pos));
pos = pos + 1;
% Dimensions
dms = typecast(m(pos:pos+ndms*4-1),'uint32')';
pos = pos + ndms*4;
% Create content
v = repmat({prot},dms);
case 39 % boolean flags
[content,pos] = deserialize_logical(m,pos);
v = cell(size(content));
for k=1:numel(v)
v{k} = content(k); end
otherwise
error('Unsupported cell array type.');
end
end
% object
function [v,pos] = deserialize_object(m,pos)
pos = pos + 1;
% Get class name.
[cls,pos] = deserialize_string(m,pos);
% Get contents
[conts,pos] = deserialize_value(m,pos);
% construct object
try
% try to use the loadobj function
v = eval([cls '.loadobj(conts)']);
catch
try
% pass the struct directly to the constructor
v = eval([cls '(conts)']);
catch
try
% try to set the fields manually
v = feval(cls);
for fn=fieldnames(conts)'
try
set(v,fn{1},conts.(fn{1}));
catch
% Note: if this happens, your deserialized object might not be fully identical
% to the original (if you are lucky, it didn't matter, through). Consider
% relaxing the access rights to this property or add support for loadobj from
% a struct.
warn_once('hlp_deserialize:restricted_access','No permission to set property %s in object of type %s.',fn{1},cls);
end
end
catch
v = conts;
v.hlp_deserialize_failed = ['could not construct class: ' cls];
end
end
end
end
% function handle
function [v,pos] = deserialize_handle(m,pos)
% Tag
kind = m(pos);
pos = pos + 1;
switch kind
case 151 % simple function
persistent db_simple; %#ok<TLEV> % database of simple functions (indexed by name)
% Name
[name,pos] = deserialize_string(m,pos);
try
% look up from table
v = db_simple.(name);
catch
% otherwise generate & fill table
v = str2func(name);
db_simple.(name) = v;
end
case 152 % anonymous function
% Function code
[code,pos] = deserialize_string(m,pos);
% Workspace
[wspace,pos] = deserialize_struct(m,pos);
% Construct
v = restore_function(code,wspace);
case 153 % scoped or nested function
persistent db_nested; %#ok<TLEV> % database of nested functions (indexed by name)
% Parents
[parentage,pos] = deserialize_cell(m,pos);
try
key = sprintf('%s_',parentage{:});
% look up from table
v = db_nested.(key);
catch
% recursively look up from parents, assuming that these support the arg system
v = parentage{end};
for k=length(parentage)-1:-1:1
% Note: if you get an error here, you are trying to deserialize a function handle
% to a nested function. This is not natively supported by MATLAB and can only be made
% to work if your function's parent implements some mechanism to return such a handle.
% The below call assumes that your function uses the BCILAB arg system to do this.
v = arg_report('handle',v,parentage{k});
end
db_nested.(key) = v;
end
end
end
% helper for deserialize_handle
function f = restore_function(decl__,workspace__)
% create workspace
for fn__=fieldnames(workspace__)'
% we use underscore names here to not run into conflicts with names defined in the workspace
eval([fn__{1} ' = workspace__.(fn__{1}) ;']);
end
clear workspace__ fn__;
% evaluate declaration
f = eval(decl__);
end
% emit a specific warning only once (per MATLAB session)
function warn_once(varargin)
persistent displayed_warnings;
% determine the message content
if length(varargin) > 1 && any(varargin{1}==':') && ~any(varargin{1}==' ') && ischar(varargin{2})
message_content = [varargin{1} sprintf(varargin{2:end})];
else
message_content = sprintf(varargin{1:end});
end
% generate a hash of of the message content
str = java.lang.String(message_content);
message_id = sprintf('x%.0f',str.hashCode()+2^31);
% and check if it had been displayed before
if ~isfield(displayed_warnings,message_id)
% emit the warning
warning(varargin{:});
% remember to not display the warning again
displayed_warnings.(message_id) = true;
end
end