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
|
%
% Copyright (c) ZeroC, Inc. All rights reserved.
%
classdef EncapsDecoder10 < IceInternal.EncapsDecoder
methods
function obj = EncapsDecoder10(is, encaps, sliceValues, valueFactoryManager, classResolver, classGraphDepthMax)
obj = obj@IceInternal.EncapsDecoder(is, encaps, sliceValues, valueFactoryManager, classResolver, ...
classGraphDepthMax);
obj.sliceType = IceInternal.SliceType.NoSlice;
end
function readPendingValues(obj)
num = obj.is.readSize();
while num > 0
for k = 1:num
obj.readInstance();
end
num = obj.is.readSize();
end
if obj.patchMapLength > 0
%
% If any entries remain in the patch map, the sender has sent an index for an object, but failed
% to supply the object.
%
throw(Ice.MarshalException('', '', 'index for class received, but no instance'));
end
end
function readValue(obj, cb)
%assert(~isempty(cb));
%
% Object references are encoded as a negative integer in 1.0.
%
index = obj.is.readInt();
if index > 0
throw(Ice.MarshalException('', '', 'invalid object id'));
end
index = -index;
if index == 0
cb([]);
else
obj.addPatchEntry(index, cb);
end
end
function throwException(obj)
%assert(obj.sliceType == IceInternal.SliceType.NoSlice);
%
% User exception with the 1.0 encoding start with a boolean flag
% that indicates whether or not the exception has classes.
%
% This allows reading the pending values even if some part of
% the exception was sliced.
%
usesClasses = obj.is.readBool();
obj.sliceType = IceInternal.SliceType.ExceptionSlice;
obj.skipFirstSlice = false;
%
% Read the first slice header.
%
obj.startSlice();
mostDerivedId = obj.typeId;
while true
%
% Use the class resolver to convert the type ID into a constructor.
%
constructor = obj.classResolver.resolve(obj.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.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);
if usesClasses
obj.readPendingValues();
end
ex = ex.icePostUnmarshal();
throw(ex);
else
%
% Slice off what we don't understand.
%
obj.skipSlice();
try
obj.startSlice();
catch ex
if isa(ex, 'Ice.UnmarshalOutOfBoundsException')
%
% An oversight in the 1.0 encoding means there is no marker to indicate
% the last slice of an exception. As a result, we just try to read the
% next type ID, which raises UnmarshalOutOfBoundsException when the
% input buffer underflows.
%
% Set the reason member to a more helpful message.
%
ex.reason = ['unknown exception type `', mostDerivedId, ''''];
end
rethrow(ex);
end
end
end
end
function startInstance(obj, sliceType)
%assert(obj.sliceType == sliceType);
obj.skipFirstSlice = true;
end
function r = endInstance(obj, ~)
%
% Read the Ice::Value slice.
%
if obj.sliceType == IceInternal.SliceType.ValueSlice
obj.startSlice();
sz = obj.is.readSize(); % For compatibility with the old AFM.
if sz ~= 0
throw(Ice.MarshalException('', '', 'invalid Object slice'));
end
obj.endSlice();
end
obj.sliceType = IceInternal.SliceType.NoSlice;
r = [];
end
function r = startSlice(obj)
%
% If first slice, don't read the header, it was already read in
% readInstance or throwException to find the type.
%
if obj.skipFirstSlice
obj.skipFirstSlice = false;
r = obj.typeId;
return;
end
%
% For values, first read the type ID boolean which indicates
% whether or not the type ID is encoded as a string or as an
% index. For exceptions, the type ID is always encoded as a
% string.
%
if obj.sliceType == IceInternal.SliceType.ValueSlice
if obj.is.readBool()
typeIdIndex = obj.is.readSize();
obj.typeIdIndex = typeIdIndex;
obj.typeId = obj.getTypeId(obj.typeIdIndex);
else
[obj.typeIdIndex, obj.typeId] = obj.readTypeId();
end
else
obj.typeId = obj.is.readString();
end
obj.sliceSize = obj.is.readInt();
if obj.sliceSize < 4
throw(Ice.UnmarshalOutOfBoundsException());
end
r = obj.typeId;
end
function endSlice(~)
% Nothing to do
end
function skipSlice(obj)
%obj.is.traceSkipSlice(obj.typeId, obj.sliceType);
%assert(obj.sliceSize >= 4);
obj.is.skip(obj.sliceSize - 4);
end
end
methods(Access=private)
function readInstance(obj)
index = obj.is.readInt();
if index <= 0
throw(Ice.MarshalException('', '', 'invalid object id'));
end
obj.sliceType = IceInternal.SliceType.ValueSlice;
obj.skipFirstSlice = false;
%
% Read the first slice header.
%
obj.startSlice();
mostDerivedId = obj.typeId;
v = [];
while true
%
% For the 1.0 encoding, the type ID for the base Object class
% marks the last slice.
%
if strcmp(obj.typeId, Ice.Value.ice_staticId())
throw(Ice.NoValueFactoryException('', '', '', mostDerivedId));
end
v = obj.newInstance(obj.typeIdIndex, obj.typeId);
%
% We found a factory, we get out of this loop.
%
if ~isempty(v)
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.typeId));
end
%
% Slice off what we don't understand.
%
obj.skipSlice();
obj.startSlice(); % Read next Slice header for next iteration.
end
%
% Compute the biggest class graph depth of this object. To compute this,
% we get the class graph depth of each ancestor from the patch map and
% keep the biggest one.
%
obj.classGraphDepth = 0;
if index <= length(obj.patchMap)
pl = obj.patchMap{index};
for i = 1:length(pl)
entry = pl{i};
classGraphDepth = entry.classGraphDepth;
if classGraphDepth > obj.classGraphDepth
obj.classGraphDepth = classGraphDepth;
end
end
end
obj.classGraphDepth = obj.classGraphDepth + 1;
if obj.classGraphDepth > obj.classGraphDepthMax
throw(Ice.MarshalException('', '', 'maximum class graph depth reached'));
end
%
% Unmarshal the instance and add it to the map of unmarshaled instances.
%
obj.unmarshal(index, v);
end
end
properties(Access=private)
sliceType
skipFirstSlice
sliceSize
typeId
typeIdIndex
end
end
|