| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Ice.Codec.Internal; |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using static IceRpc.Ice.Codec.Internal.IceEncodingDefinitions; |
| | | 10 | | |
| | | 11 | | namespace IceRpc.Ice.Codec; |
| | | 12 | | |
| | | 13 | | /// <summary>Provides methods to decode data encoded with Ice.</summary> |
| | | 14 | | public ref partial struct IceDecoder |
| | | 15 | | { |
| | | 16 | | /// <summary>Decodes a class instance.</summary> |
| | | 17 | | /// <typeparam name="T">The class type.</typeparam> |
| | | 18 | | /// <returns>The class instance, or <see langword="null" />.</returns> |
| | | 19 | | public T? DecodeClass<T>() where T : IceClass |
| | 298 | 20 | | { |
| | 298 | 21 | | IceClass? obj = DecodeClass(); |
| | | 22 | | |
| | 196 | 23 | | if (obj is T result) |
| | 70 | 24 | | { |
| | 70 | 25 | | return result; |
| | | 26 | | } |
| | 126 | 27 | | else if (obj is null) |
| | 126 | 28 | | { |
| | 126 | 29 | | return null; |
| | | 30 | | } |
| | 0 | 31 | | throw new InvalidDataException( |
| | 0 | 32 | | $"Decoded instance of type '{obj.GetType()}' but expected instance of type '{typeof(T)}'."); |
| | 196 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Decodes an Ice exception.</summary> |
| | | 36 | | /// <param name="message">The error message. It's used only when this method fails to find an exception class to |
| | | 37 | | /// instantiate.</param> |
| | | 38 | | /// <returns>The decoded Ice exception.</returns> |
| | | 39 | | public IceException DecodeException(string? message = null) |
| | 31 | 40 | | { |
| | 31 | 41 | | Debug.Assert(_classContext.Current.InstanceType == InstanceType.None); |
| | 31 | 42 | | _classContext.Current.InstanceType = InstanceType.Exception; |
| | | 43 | | |
| | | 44 | | // We can decode the indirection table (if there is one) immediately after decoding each slice header |
| | | 45 | | // because the indirection table cannot reference the exception itself. |
| | | 46 | | // Each slice contains its type ID as a string. |
| | | 47 | | |
| | 31 | 48 | | string? mostDerivedTypeId = null; |
| | 31 | 49 | | IActivator activator = _activator ?? _defaultActivator; |
| | | 50 | | IceException? iceException; |
| | | 51 | | |
| | | 52 | | do |
| | 34 | 53 | | { |
| | | 54 | | // The type ID is always decoded for an exception and cannot be null. |
| | 34 | 55 | | string? typeId = DecodeSliceHeaderIntoCurrent(); |
| | 34 | 56 | | Debug.Assert(typeId is not null); |
| | 34 | 57 | | mostDerivedTypeId ??= typeId; |
| | | 58 | | |
| | 34 | 59 | | DecodeIndirectionTableIntoCurrent(); // we decode the indirection table immediately. |
| | | 60 | | |
| | 34 | 61 | | iceException = activator.CreateInstance(typeId) as IceException; |
| | 34 | 62 | | if (iceException is null && SkipSlice(typeId)) |
| | 1 | 63 | | { |
| | | 64 | | // Cannot decode this exception. The message should be set only when the exception was received over |
| | | 65 | | // icerpc. |
| | 1 | 66 | | throw new InvalidDataException( |
| | 1 | 67 | | message is null || message.Length == 0 ? |
| | 1 | 68 | | $"The dispatch returned an Ice exception with type ID '{mostDerivedTypeId}' that the configured acti |
| | 1 | 69 | | $"The dispatch returned an Ice exception with type ID '{mostDerivedTypeId}' that the configured acti |
| | | 70 | | } |
| | 33 | 71 | | } |
| | 33 | 72 | | while (iceException is null); |
| | | 73 | | |
| | 30 | 74 | | _classContext.Current.FirstSlice = true; |
| | 30 | 75 | | iceException.Decode(ref this); |
| | 30 | 76 | | _classContext.Current = default; |
| | 30 | 77 | | return iceException; |
| | 30 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary>Tells the decoder the end of a class or exception slice was reached.</summary> |
| | | 81 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 82 | | public void EndSlice() |
| | 185 | 83 | | { |
| | | 84 | | // Note that EndSlice is not called when we call SkipSlice. |
| | 185 | 85 | | Debug.Assert(_classContext.Current.InstanceType != InstanceType.None); |
| | | 86 | | |
| | 185 | 87 | | if ((_classContext.Current.SliceFlags & SliceFlags.HasTaggedFields) != 0) |
| | 27 | 88 | | { |
| | 27 | 89 | | SkipTagged(); |
| | 27 | 90 | | } |
| | 185 | 91 | | if ((_classContext.Current.SliceFlags & SliceFlags.HasIndirectionTable) != 0) |
| | 18 | 92 | | { |
| | 18 | 93 | | Debug.Assert(_classContext.Current.PosAfterIndirectionTable is not null && |
| | 18 | 94 | | _classContext.Current.IndirectionTable is not null); |
| | | 95 | | |
| | 18 | 96 | | _reader.Advance(_classContext.Current.PosAfterIndirectionTable.Value - _reader.Consumed); |
| | 18 | 97 | | _classContext.Current.PosAfterIndirectionTable = null; |
| | 18 | 98 | | _classContext.Current.IndirectionTable = null; |
| | 18 | 99 | | } |
| | 185 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary>Marks the start of the decoding of a class or remote exception slice.</summary> |
| | | 103 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 104 | | public void StartSlice() |
| | 285 | 105 | | { |
| | 285 | 106 | | Debug.Assert(_classContext.Current.InstanceType != InstanceType.None); |
| | 285 | 107 | | if (_classContext.Current.FirstSlice) |
| | 185 | 108 | | { |
| | 185 | 109 | | _classContext.Current.FirstSlice = false; |
| | 185 | 110 | | } |
| | | 111 | | else |
| | 100 | 112 | | { |
| | 100 | 113 | | _ = DecodeSliceHeaderIntoCurrent(); |
| | 100 | 114 | | DecodeIndirectionTableIntoCurrent(); |
| | 100 | 115 | | } |
| | 285 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary>Decodes a class instance.</summary> |
| | | 119 | | /// <returns>The class instance. Can be <see langword="null" />.</returns> |
| | | 120 | | private IceClass? DecodeClass() |
| | 298 | 121 | | { |
| | 298 | 122 | | int index = DecodeSize(); |
| | 298 | 123 | | if (index < 0) |
| | 0 | 124 | | { |
| | 0 | 125 | | throw new InvalidDataException($"Found invalid index {index} while decoding a class."); |
| | | 126 | | } |
| | 298 | 127 | | else if (index == 0) |
| | 126 | 128 | | { |
| | 126 | 129 | | return null; |
| | | 130 | | } |
| | 172 | 131 | | else if (_classContext.Current.InstanceType != InstanceType.None && |
| | 172 | 132 | | (_classContext.Current.SliceFlags & SliceFlags.HasIndirectionTable) != 0) |
| | 21 | 133 | | { |
| | | 134 | | // When decoding an instance within a slice and there is an indirection table, we have an index within |
| | | 135 | | // this indirection table. |
| | | 136 | | // We need to decrement index since position 0 in the indirection table corresponds to index 1. |
| | 21 | 137 | | index--; |
| | | 138 | | |
| | | 139 | | // If the right-hand side is null, the comparison simply evaluates to false. |
| | 21 | 140 | | if (index < _classContext.Current.IndirectionTable?.Length) |
| | 21 | 141 | | { |
| | 21 | 142 | | return _classContext.Current.IndirectionTable[index]; |
| | | 143 | | } |
| | | 144 | | else |
| | 0 | 145 | | { |
| | 0 | 146 | | throw new InvalidDataException("The index is too big for the indirection table."); |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | else |
| | 151 | 150 | | { |
| | 151 | 151 | | return DecodeInstance(index); |
| | | 152 | | } |
| | 196 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary>Decodes an indirection table without updating _current.</summary> |
| | | 156 | | /// <returns>The indirection table.</returns> |
| | | 157 | | private IceClass[] DecodeIndirectionTable() |
| | 28 | 158 | | { |
| | 28 | 159 | | int size = DecodeSize(); |
| | 28 | 160 | | if (size == 0) |
| | 0 | 161 | | { |
| | 0 | 162 | | throw new InvalidDataException("Invalid empty indirection table."); |
| | | 163 | | } |
| | 28 | 164 | | IncreaseCollectionAllocation(size, Unsafe.SizeOf<IceClass>()); |
| | 28 | 165 | | var indirectionTable = new IceClass[size]; |
| | 116 | 166 | | for (int i = 0; i < indirectionTable.Length; ++i) |
| | 30 | 167 | | { |
| | 30 | 168 | | int index = DecodeSize(); |
| | 30 | 169 | | if (index < 1) |
| | 0 | 170 | | { |
| | 0 | 171 | | throw new InvalidDataException($"Found invalid index {index} decoding the indirection table."); |
| | | 172 | | } |
| | 30 | 173 | | indirectionTable[i] = DecodeInstance(index); |
| | 30 | 174 | | } |
| | 28 | 175 | | return indirectionTable; |
| | 28 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary>Decodes the indirection table into _current's fields if there is an indirection table. |
| | | 179 | | /// Precondition: called after decoding the header of the current slice. This method does not change _pos. |
| | | 180 | | /// </summary> |
| | | 181 | | private void DecodeIndirectionTableIntoCurrent() |
| | 289 | 182 | | { |
| | 289 | 183 | | Debug.Assert(_classContext.Current.IndirectionTable is null); |
| | 289 | 184 | | if ((_classContext.Current.SliceFlags & SliceFlags.HasIndirectionTable) != 0) |
| | 20 | 185 | | { |
| | 20 | 186 | | if ((_classContext.Current.SliceFlags & SliceFlags.HasSliceSize) == 0) |
| | 0 | 187 | | { |
| | 0 | 188 | | throw new InvalidDataException("The Ice has indirection table flag but has not size flag."); |
| | | 189 | | } |
| | | 190 | | |
| | 20 | 191 | | long savedPos = _reader.Consumed; |
| | 20 | 192 | | _reader.Advance(_classContext.Current.SliceSize); |
| | 20 | 193 | | _classContext.Current.IndirectionTable = DecodeIndirectionTable(); |
| | 20 | 194 | | _classContext.Current.PosAfterIndirectionTable = _reader.Consumed; |
| | 20 | 195 | | _reader.Rewind(_reader.Consumed - savedPos); |
| | 20 | 196 | | } |
| | 289 | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary>Decodes a class instance.</summary> |
| | | 200 | | /// <param name="index">The index of the class instance. If greater than 1, it's a reference to a previously |
| | | 201 | | /// seen class; if 1, the class instance's bytes are next. Cannot be 0 or less.</param> |
| | | 202 | | private IceClass DecodeInstance(int index) |
| | 181 | 203 | | { |
| | 181 | 204 | | Debug.Assert(index > 0); |
| | | 205 | | |
| | 181 | 206 | | if (index > 1) |
| | 17 | 207 | | { |
| | 17 | 208 | | if (_classContext.InstanceMap is not null && _classContext.InstanceMap.Count > index - 2) |
| | 17 | 209 | | { |
| | 17 | 210 | | return _classContext.InstanceMap[index - 2]; |
| | | 211 | | } |
| | 0 | 212 | | throw new InvalidDataException($"Cannot find instance index {index} in the instance map."); |
| | | 213 | | } |
| | | 214 | | |
| | 164 | 215 | | if (++_currentDepth > _maxDepth) |
| | 1 | 216 | | { |
| | 1 | 217 | | throw new InvalidDataException("The maximum decoder depth was reached while decoding a class."); |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | // Save current in case we're decoding a nested instance. |
| | 163 | 221 | | InstanceData previousCurrent = _classContext.Current; |
| | 163 | 222 | | _classContext.Current = default; |
| | 163 | 223 | | _classContext.Current.InstanceType = InstanceType.Class; |
| | | 224 | | |
| | 163 | 225 | | IceClass? instance = null; |
| | 163 | 226 | | _classContext.InstanceMap ??= new List<IceClass>(); |
| | | 227 | | |
| | 163 | 228 | | bool decodeIndirectionTable = true; |
| | 163 | 229 | | IActivator activator = _activator ?? _defaultActivator; |
| | | 230 | | do |
| | 181 | 231 | | { |
| | | 232 | | // Decode the slice header. |
| | 181 | 233 | | string? typeId = DecodeSliceHeaderIntoCurrent(); |
| | | 234 | | |
| | | 235 | | // We cannot decode the indirection table at this point as it may reference the new instance that is |
| | | 236 | | // not created yet. |
| | 181 | 237 | | if (typeId is not null) |
| | 181 | 238 | | { |
| | 181 | 239 | | instance = activator.CreateInstance(typeId) as IceClass; |
| | 181 | 240 | | } |
| | | 241 | | |
| | 181 | 242 | | if (instance is null && SkipSlice(typeId)) |
| | 7 | 243 | | { |
| | | 244 | | // Ice off what we don't understand. |
| | 7 | 245 | | instance = new UnknownIceClass(); |
| | | 246 | | // Don't decode the indirection table as it's the last entry in DeferredIndirectionTableList. |
| | 7 | 247 | | decodeIndirectionTable = false; |
| | 7 | 248 | | } |
| | 180 | 249 | | } |
| | 180 | 250 | | while (instance is null); |
| | | 251 | | |
| | | 252 | | // Add the instance to the map/list of instances. This must be done before decoding the instances (for |
| | | 253 | | // circular references). |
| | 162 | 254 | | _classContext.InstanceMap!.Add(instance); |
| | | 255 | | |
| | | 256 | | // Decode all the deferred indirection tables now that the instance is inserted in _instanceMap. |
| | 162 | 257 | | if (_classContext.Current.DeferredIndirectionTableList?.Count > 0) |
| | 13 | 258 | | { |
| | 13 | 259 | | long savedPos = _reader.Consumed; |
| | | 260 | | |
| | 13 | 261 | | Debug.Assert(_classContext.Current.Slices?.Count == |
| | 13 | 262 | | _classContext.Current.DeferredIndirectionTableList.Count); |
| | 76 | 263 | | for (int i = 0; i < _classContext.Current.DeferredIndirectionTableList.Count; ++i) |
| | 25 | 264 | | { |
| | 25 | 265 | | long pos = _classContext.Current.DeferredIndirectionTableList[i]; |
| | 25 | 266 | | if (pos > 0) |
| | 8 | 267 | | { |
| | 8 | 268 | | long distance = pos - _reader.Consumed; |
| | 8 | 269 | | if (distance > 0) |
| | 2 | 270 | | { |
| | 2 | 271 | | _reader.Advance(distance); |
| | 2 | 272 | | } |
| | | 273 | | else |
| | 6 | 274 | | { |
| | 6 | 275 | | _reader.Rewind(-distance); |
| | 6 | 276 | | } |
| | 8 | 277 | | _classContext.Current.Slices[i].Instances = DecodeIndirectionTable(); |
| | 8 | 278 | | } |
| | | 279 | | // else remains empty |
| | 25 | 280 | | } |
| | | 281 | | |
| | 13 | 282 | | _reader.Advance(savedPos - _reader.Consumed); |
| | 13 | 283 | | } |
| | | 284 | | |
| | 162 | 285 | | if (decodeIndirectionTable) |
| | 155 | 286 | | { |
| | 155 | 287 | | DecodeIndirectionTableIntoCurrent(); |
| | 155 | 288 | | } |
| | | 289 | | |
| | 162 | 290 | | instance.UnknownSlices = _classContext.Current.Slices?.ToImmutableList() ?? ImmutableList<SliceInfo>.Empty; |
| | 162 | 291 | | _classContext.Current.FirstSlice = true; |
| | 162 | 292 | | instance.Decode(ref this); |
| | | 293 | | |
| | 62 | 294 | | _classContext.Current = previousCurrent; |
| | 62 | 295 | | --_currentDepth; |
| | 62 | 296 | | return instance; |
| | 79 | 297 | | } |
| | | 298 | | |
| | | 299 | | /// <summary>Decodes the header of the current slice into _current.</summary> |
| | | 300 | | /// <returns>The type ID or the compact ID of the current slice.</returns> |
| | | 301 | | private string? DecodeSliceHeaderIntoCurrent() |
| | 315 | 302 | | { |
| | 315 | 303 | | _classContext.Current.SliceFlags = (SliceFlags)DecodeByte(); |
| | | 304 | | |
| | | 305 | | string? typeId; |
| | | 306 | | // Decode the type ID. For class slices, the type ID is encoded as a string or as an index or as a compact |
| | | 307 | | // ID, for exceptions it's always encoded as a string. |
| | 315 | 308 | | if (_classContext.Current.InstanceType == InstanceType.Class) |
| | 276 | 309 | | { |
| | 276 | 310 | | typeId = DecodeTypeId(_classContext.Current.SliceFlags.GetTypeIdKind()); |
| | | 311 | | |
| | 276 | 312 | | if (typeId is null) |
| | 62 | 313 | | { |
| | 62 | 314 | | if ((_classContext.Current.SliceFlags & SliceFlags.HasSliceSize) != 0) |
| | 0 | 315 | | { |
| | 0 | 316 | | throw new InvalidDataException( |
| | 0 | 317 | | "Invalid Ice flags; an Ice in compact format cannot carry a size."); |
| | | 318 | | } |
| | 62 | 319 | | } |
| | 276 | 320 | | } |
| | | 321 | | else |
| | 39 | 322 | | { |
| | | 323 | | // Exception slices always include the type ID, even when using the compact format. |
| | 39 | 324 | | typeId = DecodeString(); |
| | 39 | 325 | | } |
| | | 326 | | |
| | | 327 | | // Decode the slice size if available. |
| | 315 | 328 | | if ((_classContext.Current.SliceFlags & SliceFlags.HasSliceSize) != 0) |
| | 129 | 329 | | { |
| | 129 | 330 | | _classContext.Current.SliceSize = DecodeSliceSize(); |
| | 129 | 331 | | } |
| | | 332 | | else |
| | 186 | 333 | | { |
| | 186 | 334 | | _classContext.Current.SliceSize = 0; |
| | 186 | 335 | | } |
| | | 336 | | |
| | | 337 | | // Clear other per-slice fields: |
| | 315 | 338 | | _classContext.Current.IndirectionTable = null; |
| | 315 | 339 | | _classContext.Current.PosAfterIndirectionTable = null; |
| | | 340 | | |
| | 315 | 341 | | return typeId; |
| | 315 | 342 | | } |
| | | 343 | | |
| | | 344 | | /// <summary>Decodes the size of the current slice.</summary> |
| | | 345 | | /// <returns>The slice of the current slice, not including the size length.</returns> |
| | | 346 | | private int DecodeSliceSize() |
| | 149 | 347 | | { |
| | 149 | 348 | | int size = DecodeInt(); |
| | 149 | 349 | | if (size < 4) |
| | 0 | 350 | | { |
| | 0 | 351 | | throw new InvalidDataException($"Invalid Ice size: {size}."); |
| | | 352 | | } |
| | | 353 | | // The encoded size includes the size length. |
| | 149 | 354 | | return size - 4; |
| | 149 | 355 | | } |
| | | 356 | | |
| | | 357 | | /// <summary>Decodes the type ID of a class instance.</summary> |
| | | 358 | | /// <param name="typeIdKind">The kind of type ID to decode.</param> |
| | | 359 | | /// <returns>The type ID or the compact ID, if any.</returns> |
| | | 360 | | private string? DecodeTypeId(TypeIdKind typeIdKind) |
| | 296 | 361 | | { |
| | 296 | 362 | | _classContext.TypeIdMap ??= new List<string>(); |
| | | 363 | | |
| | 296 | 364 | | switch (typeIdKind) |
| | | 365 | | { |
| | | 366 | | case TypeIdKind.Index: |
| | 131 | 367 | | int index = DecodeSize(); |
| | 131 | 368 | | if (index > 0 && index - 1 < _classContext.TypeIdMap.Count) |
| | 131 | 369 | | { |
| | | 370 | | // The encoded type-id indexes start at 1, not 0. |
| | 131 | 371 | | return _classContext.TypeIdMap[index - 1]; |
| | | 372 | | } |
| | 0 | 373 | | throw new InvalidDataException($"Decoded invalid type ID index {index}."); |
| | | 374 | | |
| | | 375 | | case TypeIdKind.String: |
| | 70 | 376 | | string typeId = DecodeString(); |
| | | 377 | | |
| | | 378 | | // The typeIds of slices in indirection tables can be decoded several times: when we skip the |
| | | 379 | | // indirection table and later on when we decode it. We only want to add this type ID to the list and |
| | | 380 | | // assign it an index when it's the first time we decode it, so we save the largest position we |
| | | 381 | | // decode to figure out when to add to the list. |
| | 70 | 382 | | if (_reader.Consumed > _classContext.PosAfterLatestInsertedTypeId) |
| | 62 | 383 | | { |
| | 62 | 384 | | _classContext.PosAfterLatestInsertedTypeId = _reader.Consumed; |
| | 62 | 385 | | _classContext.TypeIdMap.Add(typeId); |
| | 62 | 386 | | } |
| | 70 | 387 | | return typeId; |
| | | 388 | | |
| | | 389 | | case TypeIdKind.CompactId: |
| | 33 | 390 | | return DecodeSize().ToString(CultureInfo.InvariantCulture); |
| | | 391 | | |
| | | 392 | | default: |
| | | 393 | | // TypeIdKind has only 4 possible values. |
| | 62 | 394 | | Debug.Assert(typeIdKind == TypeIdKind.None); |
| | 62 | 395 | | return null; |
| | | 396 | | } |
| | 296 | 397 | | } |
| | | 398 | | |
| | | 399 | | /// <summary>Skips the indirection table. The caller must save the current position before calling |
| | | 400 | | /// SkipIndirectionTable (to decode the indirection table at a later point) except when the caller is |
| | | 401 | | /// SkipIndirectionTable itself.</summary> |
| | | 402 | | private void SkipIndirectionTable() |
| | 11 | 403 | | { |
| | | 404 | | // We should never skip an exception's indirection table |
| | 11 | 405 | | Debug.Assert(_classContext.Current.InstanceType == InstanceType.Class); |
| | | 406 | | |
| | 11 | 407 | | int tableSize = DecodeSize(); |
| | 38 | 408 | | for (int i = 0; i < tableSize; ++i) |
| | 11 | 409 | | { |
| | 11 | 410 | | int index = DecodeSize(); |
| | 11 | 411 | | if (index <= 0) |
| | 0 | 412 | | { |
| | 0 | 413 | | throw new InvalidDataException($"Decoded invalid index {index} in indirection table."); |
| | | 414 | | } |
| | 11 | 415 | | if (index == 1) |
| | 9 | 416 | | { |
| | 9 | 417 | | if (++_currentDepth > _maxDepth) |
| | 1 | 418 | | { |
| | 1 | 419 | | throw new InvalidDataException("Maximum decoder depth reached while decoding a class."); |
| | | 420 | | } |
| | | 421 | | |
| | | 422 | | // Decode/skip this instance |
| | | 423 | | SliceFlags sliceFlags; |
| | | 424 | | do |
| | 20 | 425 | | { |
| | 20 | 426 | | sliceFlags = (SliceFlags)DecodeByte(); |
| | | 427 | | |
| | | 428 | | // Skip type ID - can update _typeIdMap |
| | 20 | 429 | | _ = DecodeTypeId(sliceFlags.GetTypeIdKind()); |
| | | 430 | | |
| | | 431 | | // Decode the slice size, then skip the slice |
| | 20 | 432 | | if ((sliceFlags & SliceFlags.HasSliceSize) == 0) |
| | 0 | 433 | | { |
| | 0 | 434 | | throw new InvalidDataException("The Ice size flag is missing."); |
| | | 435 | | } |
| | 20 | 436 | | _reader.Advance(DecodeSliceSize()); |
| | | 437 | | |
| | | 438 | | // If this slice has an indirection table, skip it too. |
| | 20 | 439 | | if ((sliceFlags & SliceFlags.HasIndirectionTable) != 0) |
| | 2 | 440 | | { |
| | 2 | 441 | | SkipIndirectionTable(); |
| | 0 | 442 | | } |
| | 18 | 443 | | } |
| | 18 | 444 | | while ((sliceFlags & SliceFlags.IsLastSlice) == 0); |
| | 6 | 445 | | _currentDepth--; |
| | 6 | 446 | | } |
| | 8 | 447 | | } |
| | 8 | 448 | | } |
| | | 449 | | |
| | | 450 | | /// <summary>Skips and saves the body of the current slice (save only for classes); also skips and save the |
| | | 451 | | /// indirection table (if any).</summary> |
| | | 452 | | /// <param name="typeId">The type ID or compact ID of the current slice.</param> |
| | | 453 | | /// <returns><see langword="true" /> when the current slice is the last slice; otherwise, <see langword="false" />. |
| | | 454 | | /// </returns> |
| | | 455 | | private bool SkipSlice(string? typeId) |
| | 30 | 456 | | { |
| | 30 | 457 | | if (typeId is null) |
| | 0 | 458 | | { |
| | 0 | 459 | | throw new InvalidDataException("Cannot skip a class slice with no type ID."); |
| | | 460 | | } |
| | | 461 | | |
| | 30 | 462 | | if ((_classContext.Current.SliceFlags & SliceFlags.HasSliceSize) == 0) |
| | 0 | 463 | | { |
| | 0 | 464 | | throw new InvalidDataException( |
| | 0 | 465 | | $"The configured activator cannot find a class for type ID '{typeId}' and the compact format prevents sl |
| | | 466 | | } |
| | | 467 | | |
| | 30 | 468 | | bool hasTaggedFields = (_classContext.Current.SliceFlags & SliceFlags.HasTaggedFields) != 0; |
| | | 469 | | byte[] bytes; |
| | 30 | 470 | | if (hasTaggedFields) |
| | 4 | 471 | | { |
| | | 472 | | // Don't include the tag end marker. It will be re-written by IceEncoder.EndSlice when the sliced data |
| | | 473 | | // is re-written. |
| | 4 | 474 | | bytes = new byte[_classContext.Current.SliceSize - 1]; |
| | 4 | 475 | | CopyTo(bytes.AsSpan()); |
| | 4 | 476 | | Skip(1); |
| | 4 | 477 | | } |
| | | 478 | | else |
| | 26 | 479 | | { |
| | 26 | 480 | | bytes = new byte[_classContext.Current.SliceSize]; |
| | 26 | 481 | | CopyTo(bytes.AsSpan()); |
| | 26 | 482 | | } |
| | | 483 | | |
| | 30 | 484 | | bool hasIndirectionTable = (_classContext.Current.SliceFlags & SliceFlags.HasIndirectionTable) != 0; |
| | | 485 | | |
| | | 486 | | // SkipSlice for a class skips the indirection table and preserves its position in |
| | | 487 | | // _current.DeferredIndirectionTableList for later decoding. |
| | 30 | 488 | | if (_classContext.Current.InstanceType == InstanceType.Class) |
| | 26 | 489 | | { |
| | 26 | 490 | | _classContext.Current.DeferredIndirectionTableList ??= new List<long>(); |
| | 26 | 491 | | if (hasIndirectionTable) |
| | 9 | 492 | | { |
| | 9 | 493 | | long savedPos = _reader.Consumed; |
| | 9 | 494 | | SkipIndirectionTable(); |
| | | 495 | | |
| | | 496 | | // we want to later read the deepest first |
| | 8 | 497 | | _classContext.Current.DeferredIndirectionTableList.Add(savedPos); |
| | 8 | 498 | | } |
| | | 499 | | else |
| | 17 | 500 | | { |
| | 17 | 501 | | _classContext.Current.DeferredIndirectionTableList.Add(0); // keep a slot for each slice |
| | 17 | 502 | | } |
| | | 503 | | |
| | 25 | 504 | | var info = new SliceInfo( |
| | 25 | 505 | | typeId, |
| | 25 | 506 | | new ReadOnlyMemory<byte>(bytes), |
| | 25 | 507 | | _classContext.Current.IndirectionTable ?? Array.Empty<IceClass>(), |
| | 25 | 508 | | hasTaggedFields); |
| | | 509 | | |
| | 25 | 510 | | _classContext.Current.Slices ??= new List<SliceInfo>(); |
| | 25 | 511 | | _classContext.Current.Slices.Add(info); |
| | 25 | 512 | | } |
| | 4 | 513 | | else if (hasIndirectionTable) |
| | 2 | 514 | | { |
| | 2 | 515 | | Debug.Assert(_classContext.Current.PosAfterIndirectionTable is not null); |
| | | 516 | | |
| | | 517 | | // Move past indirection table |
| | 2 | 518 | | _reader.Advance(_classContext.Current.PosAfterIndirectionTable.Value - _reader.Consumed); |
| | 2 | 519 | | _classContext.Current.PosAfterIndirectionTable = null; |
| | 2 | 520 | | } |
| | | 521 | | |
| | | 522 | | // If we decoded the indirection table previously, we don't need it anymore since we're skipping this slice. |
| | 29 | 523 | | _classContext.Current.IndirectionTable = null; |
| | | 524 | | |
| | 29 | 525 | | return (_classContext.Current.SliceFlags & SliceFlags.IsLastSlice) != 0; |
| | 29 | 526 | | } |
| | | 527 | | |
| | | 528 | | /// <summary>Holds various fields used for class and exception decoding.</summary> |
| | | 529 | | private struct ClassContext |
| | | 530 | | { |
| | | 531 | | // Data for the class or exception instance that is currently getting decoded. |
| | | 532 | | internal InstanceData Current; |
| | | 533 | | |
| | | 534 | | // Map of class instance ID to class instance. |
| | | 535 | | // When decoding a buffer: |
| | | 536 | | // - Instance ID = 0 means null |
| | | 537 | | // - Instance ID = 1 means the instance is encoded inline afterwards |
| | | 538 | | // - Instance ID > 1 means a reference to a previously decoded instance, found in this map. |
| | | 539 | | // Since the map is actually a list, we use instance ID - 2 to lookup an instance. |
| | | 540 | | internal List<IceClass>? InstanceMap; |
| | | 541 | | |
| | | 542 | | // See DecodeTypeId. |
| | | 543 | | internal long PosAfterLatestInsertedTypeId; |
| | | 544 | | |
| | | 545 | | // Map of type ID index to type ID sequence, used only for classes. |
| | | 546 | | // We assign a type ID index (starting with 1) to each type ID (type ID sequence) we decode, in order. |
| | | 547 | | // Since this map is a list, we lookup a previously assigned type ID (type ID sequence) with |
| | | 548 | | // _typeIdMap[index - 1]. |
| | | 549 | | internal List<string>? TypeIdMap; |
| | | 550 | | } |
| | | 551 | | |
| | | 552 | | private struct InstanceData |
| | | 553 | | { |
| | | 554 | | // Instance fields |
| | | 555 | | |
| | | 556 | | internal List<long>? DeferredIndirectionTableList; |
| | | 557 | | internal InstanceType InstanceType; |
| | | 558 | | internal List<SliceInfo>? Slices; // Preserved slices. |
| | | 559 | | |
| | | 560 | | // Slice fields |
| | | 561 | | |
| | | 562 | | internal bool FirstSlice; |
| | | 563 | | internal IceClass[]? IndirectionTable; // Indirection table of the current slice |
| | | 564 | | internal long? PosAfterIndirectionTable; |
| | | 565 | | |
| | | 566 | | internal SliceFlags SliceFlags; |
| | | 567 | | internal int SliceSize; |
| | | 568 | | } |
| | | 569 | | |
| | | 570 | | private enum InstanceType : byte |
| | | 571 | | { |
| | | 572 | | None = 0, |
| | | 573 | | Class, |
| | | 574 | | Exception |
| | | 575 | | } |
| | | 576 | | } |