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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
%{
**********************************************************************
Copyright (c) 2003-present ZeroC, Inc. All rights reserved.
This copy of Ice is licensed to you under the terms described in the
ICE_LICENSE file included in this distribution.
**********************************************************************
%}
classdef EncapsDecoder11 < IceInternal.EncapsDecoder
methods
function obj = EncapsDecoder11(is, encaps, sliceValues, valueFactoryManager, classResolver)
obj = obj@IceInternal.EncapsDecoder(is, encaps, sliceValues, valueFactoryManager, classResolver);
obj.current = [];
obj.valueIdIndex = 1;
end
function readValue(obj, cb)
import IceInternal.Protocol;
index = obj.is.readSize();
if index < 0
throw(Ice.MarshalException('', '', 'invalid object id'));
elseif index == 0
if ~isempty(cb)
cb([]);
end
elseif ~isempty(obj.current) && bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_INDIRECTION_TABLE)
%
% When reading a class instance within a slice and there's an
% indirect instance table, always read an indirect reference
% that points to an instance from the indirect instance table
% marshaled at the end of the Slice.
%
% Maintain a list of indirect references. Note that the
% indirect index starts at 1, so we decrement it by one to
% derive an index into the indirection table that we'll read
% at the end of the slice.
%
if ~isempty(cb)
if isempty(obj.current.indirectPatchList) % Lazy initialization
obj.current.indirectPatchList = containers.Map('KeyType', 'int32', 'ValueType', 'any');
end
e = IceInternal.IndirectPatchEntry();
%e.index = index - 1;
e.index = index; % MATLAB indexing starts at 1
e.cb = cb;
sz = length(obj.current.indirectPatchList);
obj.current.indirectPatchList(sz) = e;
end
else
obj.readInstance(index, cb);
end
end
function throwException(obj)
import IceInternal.Protocol;
assert(isempty(obj.current));
% Inlining push()
obj.current = IceInternal.EncapsDecoder11_InstanceData(obj.current);
obj.current.sliceType = IceInternal.SliceType.ExceptionSlice;
obj.current.skipFirstSlice = false;
%
% Read the first slice header.
%
obj.startSlice();
mostDerivedId = obj.current.typeId;
while true
%
% Use the class resolver to convert the type ID into a class constructor.
%
constructor = obj.classResolver.resolve(obj.current.typeId);
%
% Try to instantiate the class.
%
ex = [];
if ~isempty(constructor)
try
ex = constructor(); % Invoke the constructor.
catch e
%
% Instantiation failed.
%
reason = sprintf('exception in constructor for %s', obj.current.typeId);
me = Ice.MarshalException('', reason, reason);
me.addCause(e);
throw(me);
end
end
if ~isempty(ex)
%
% Exceptions are value types so we have to replace 'ex' with its new value after calling methods.
%
ex = ex.iceRead(obj.is);
ex = ex.icePostUnmarshal();
throw(ex);
else
%
% Slice off what we don't understand.
%
obj.skipSlice();
%
% If this is the last slice, raise an exception and stop unmarshaling.
%
if bitand(obj.current.sliceFlags, Protocol.FLAG_IS_LAST_SLICE)
throw(Ice.UnknownUserException('', '', mostDerivedId));
end
obj.startSlice();
end
end
end
function startInstance(obj, sliceType)
assert(obj.current.sliceType == sliceType);
obj.current.skipFirstSlice = true;
end
function r = endInstance(obj, preserve)
slicedData = [];
if preserve
slicedData = obj.readSlicedData();
end
obj.current.slices = {};
obj.current.indirectionTables = {};
obj.current.indirectPatchList = [];
obj.current = obj.current.previous;
r = slicedData;
end
function r = startSlice(obj)
import IceInternal.Protocol;
%
% If first slice, don't read the header, it was already read in
% readInstance or throwException to find the factory.
%
if obj.current.skipFirstSlice
obj.current.skipFirstSlice = false;
r = obj.current.typeId;
return;
end
obj.current.sliceFlags = obj.is.readByte();
%
% Read the type ID, for value slices the type ID is encoded as a
% string or as an index, for exceptions it's always encoded as a
% string.
%
if obj.current.sliceType == IceInternal.SliceType.ValueSlice
% Must be checked first!
if bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_TYPE_ID_COMPACT) == Protocol.FLAG_HAS_TYPE_ID_COMPACT
obj.current.typeId = '';
obj.current.compactId = obj.is.readSize();
elseif bitand(obj.current.sliceFlags, bitor(Protocol.FLAG_HAS_TYPE_ID_STRING, Protocol.FLAG_HAS_TYPE_ID_INDEX))
obj.current.typeId = obj.readTypeId(bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_TYPE_ID_INDEX) > 0);
obj.current.compactId = -1;
else
% Only the most derived slice encodes the type ID for the compact format.
obj.current.typeId = '';
obj.current.compactId = -1;
end
else
obj.current.typeId = obj.is.readString();
end
%
% Read the slice size if necessary.
%
if bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_SLICE_SIZE)
obj.current.sliceSize = obj.is.readInt();
if obj.current.sliceSize < 4
throw(Ice.UnmarshalOutOfBoundsException());
end
else
obj.current.sliceSize = 0;
end
r = obj.current.typeId;
end
function endSlice(obj)
import IceInternal.Protocol;
if bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_OPTIONAL_MEMBERS)
obj.is.skipOptionals();
end
%
% Read the indirection table if one is present and transform the
% indirect patch list into patch entries with direct references.
%
if bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_INDIRECTION_TABLE)
%
% The table is written as a sequence<size> to conserve space.
%
sz = obj.is.readAndCheckSeqSize(1);
indirectionTable = cell(1, sz);
for i = 1:sz
indirectionTable{i} = obj.readInstance(obj.is.readSize(), []);
end
%
% Sanity checks. If there are optional members, it's possible
% that not all instance references were read if they are from
% unknown optional data members.
%
if length(indirectionTable) == 0
throw(Ice.MarshalException('', '', 'empty indirection table'));
end
if isempty(obj.current.indirectPatchList) && ...
bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_OPTIONAL_MEMBERS) == 0
throw(Ice.MarshalException('', '', 'no references to indirection table'));
end
%
% Convert indirect references into direct references.
%
if ~isempty(obj.current.indirectPatchList)
keys = obj.current.indirectPatchList.keys();
for i = 1:length(keys)
e = obj.current.indirectPatchList(keys{i});
assert(e.index > 0); % MATLAB starts indexing at 1
if e.index > length(indirectionTable)
throw(Ice.MarshalException('', '', 'indirection out of range'));
end
obj.addPatchEntry(indirectionTable{e.index}, e.cb);
end
obj.current.indirectPatchList = [];
end
end
end
function skipSlice(obj)
import IceInternal.Protocol;
start = obj.is.getPos();
if bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_SLICE_SIZE)
assert(obj.current.sliceSize >= 4);
obj.is.skip(obj.current.sliceSize - 4);
else
if obj.current.sliceType == IceInternal.SliceType.ValueSlice
reason = ['no value factory found and compact format prevents ', ...
'slicing (the sender should use the sliced format instead)'];
throw(Ice.NoValueFactoryException('', reason, reason, obj.current.typeId));
else
throw(Ice.UnknownUserException('', '', obj.current.typeId));
end
end
%
% Preserve this slice.
%
info = Ice.SliceInfo();
info.typeId = obj.current.typeId;
info.compactId = obj.current.compactId;
info.hasOptionalMembers = bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_OPTIONAL_MEMBERS) > 0;
info.isLastSlice = bitand(obj.current.sliceFlags, Protocol.FLAG_IS_LAST_SLICE) > 0;
if info.hasOptionalMembers
%
% Don't include the optional member end marker. It will be re-written by
% endSlice when the sliced data is re-written.
%
info.bytes = obj.is.getBytes(start, obj.is.getPos() - 2);
else
info.bytes = obj.is.getBytes(start, obj.is.getPos() - 1);
end
%
% Read the indirect instance table. We read the instances or their
% IDs if the instance is a reference to an already unmarshaled
% object.
%
% The SliceInfo object sequence is initialized only if
% readSlicedData is called.
%
if bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_INDIRECTION_TABLE)
sz = obj.is.readAndCheckSeqSize(1);
indirectionTable = cell(1, sz);
for i = 1:sz
indirectionTable{i} = obj.readInstance(obj.is.readSize(), []);
end
obj.current.indirectionTables{end + 1} = indirectionTable;
else
obj.current.indirectionTables{end + 1} = {};
end
obj.current.slices{end + 1} = info;
end
function r = readOptional(obj, readTag, expectedFormat)
import IceInternal.Protocol;
if isempty(obj.current)
r = obj.is.readOptionalImpl(readTag, expectedFormat);
return;
elseif bitand(obj.current.sliceFlags, Protocol.FLAG_HAS_OPTIONAL_MEMBERS)
r = obj.is.readOptionalImpl(readTag, expectedFormat);
return;
end
r = false;
end
end
methods(Access=private)
function r = readInstance(obj, index, cb)
import IceInternal.Protocol;
assert(index > 0);
if index > 1
if ~isempty(cb)
obj.addPatchEntry(index, cb);
end
r = index;
return;
end
% Inlining push()
obj.current = IceInternal.EncapsDecoder11_InstanceData(obj.current);
obj.current.sliceType = IceInternal.SliceType.ValueSlice;
obj.current.skipFirstSlice = false;
%
% Get the instance ID before we start reading slices. If some
% slices are skipped, the indirect instance table is still read and
% might read other instances.
%
obj.valueIdIndex = obj.valueIdIndex + 1;
index = obj.valueIdIndex;
%
% Read the first slice header.
%
obj.startSlice();
mostDerivedId = obj.current.typeId;
v = [];
while true
updateCache = false;
if obj.current.compactId >= 0
updateCache = true;
%
% Translate a compact (numeric) type ID into a class.
%
if isempty(obj.compactIdCache) % Lazy initialization.
obj.compactIdCache = containers.Map('KeyType', 'int32', 'ValueType', 'any');
else
%
% Check the cache to see if we've already translated the compact type ID into a constructor.
%
if obj.compactIdCache.isKey(obj.current.compactId)
constructor = obj.compactIdCache(obj.current.compactId);
try
v = constructor(); % Invoke the constructor.
updateCache = false;
catch e
reason = sprintf('constructor failed for class %s with compact id %d', cls, ...
obj.current.compactId);
ex = Ice.NoValueFactoryException('', reason, reason, '');
ex.addCause(e);
throw(ex);
end
end
end
%
% If we haven't already cached a class for the compact ID, then try to translate the
% compact ID into a type ID.
%
if isempty(v)
obj.current.typeId = '';
if isempty(obj.current.typeId)
obj.current.typeId = obj.resolveCompactId(obj.current.compactId);
end
end
end
if isempty(v) && ~isempty(obj.current.typeId)
v = obj.newInstance(obj.current.typeId);
end
if ~isempty(v)
if updateCache
assert(obj.current.compactId >= 0);
obj.compactIdCache(obj.current.compactId) = str2func(class(v));
end
%
% We have an instance, get out of this loop.
%
break;
end
%
% If slicing is disabled, stop unmarshaling.
%
if ~obj.sliceValues
reason = 'no value factory found and slicing is disabled';
throw(Ice.NoValueFactoryException('', reason, reason, obj.current.typeId));
end
%
% Slice off what we don't understand.
%
obj.skipSlice();
%
% If this is the last slice, keep the instance as an opaque
% UnknownSlicedValue object.
%
if bitand(obj.current.sliceFlags, Protocol.FLAG_IS_LAST_SLICE)
%
% Provide a factory with an opportunity to supply the instance.
% We pass the "::Ice::Object" ID to indicate that this is the
% last chance to preserve the instance.
%
v = obj.newInstance(Ice.Value.ice_staticId());
if isempty(v)
v = Ice.UnknownSlicedValue(mostDerivedId);
end
break;
end
obj.startSlice(); % Read next Slice header for next iteration.
end
%
% Unmarshal the instance.
%
obj.unmarshal(index, v);
if isempty(obj.current) && ~isempty(obj.patchMap)
%
% If any entries remain in the patch map, the sender has sent an index for an instance, but failed
% to supply the instance.
%
throw(Ice.MarshalException('', '', 'index for class received, but no instance'));
end
if ~isempty(cb)
cb(v);
end
r = index;
end
function r = readSlicedData(obj)
if isempty(obj.current.slices) % No preserved slices.
r = [];
end
%
% The _indirectionTables member holds the indirection table for each slice
% in _slices.
%
assert(length(obj.current.slices) == length(obj.current.indirectionTables));
function setInstance(si, n, v)
si.instances{n} = v;
end
for n = 1:length(obj.current.slices)
%
% We use the "instances" list in SliceInfo to hold references
% to the target instances. Note that the instances might not have
% been read yet in the case of a circular reference to an
% enclosing instance.
%
table = obj.current.indirectionTables{n};
info = obj.current.slices{n};
info.instances = cell(1, length(table));
for j = 1:length(info.instances)
obj.addPatchEntry(table{j}, @(v) setInstance(info, j, v));
end
end
r = Ice.SlicedData(obj.current.slices); % Makes a shallow copy
end
function r = resolveCompactId(obj, id)
type = '';
if isempty(type)
prop = sprintf('IceCompactId.TypeId_%d.typeId', id);
try
type = eval(prop);
catch ex
end
end
r = type;
end
end
properties(Access=private)
current
valueIdIndex
compactIdCache
end
end
|