/// <summary> /// Reads the next data item as the start of an array (major type 4). /// </summary> /// <returns> /// The length of the definite-length array, or <see langword="null" /> if the array is indefinite-length. /// </returns> /// <exception cref="InvalidOperationException"> /// the next data item does not have the correct major type. /// </exception> /// <exception cref="CborContentException"> /// the next value has an invalid CBOR encoding. -or- /// there was an unexpected end of CBOR encoding data. -or- /// the next value uses a CBOR encoding that is not valid under the current conformance mode. /// </exception> public int?ReadStartArray() { CborInitialByte header = PeekInitialByte(expectedType: CborMajorType.Array); if (header.AdditionalInfo == CborAdditionalInfo.IndefiniteLength) { if (_isConformanceModeCheckEnabled && CborConformanceModeHelpers.RequiresDefiniteLengthItems(ConformanceMode)) { throw new CborContentException(SR.Format(SR.Cbor_ConformanceMode_IndefiniteLengthItemsNotSupported, ConformanceMode)); } AdvanceBuffer(1); PushDataItem(CborMajorType.Array, null); return(null); } else { ReadOnlySpan <byte> buffer = GetRemainingBytes(); int arrayLength = DecodeDefiniteLength(header, buffer, out int bytesRead); AdvanceBuffer(bytesRead); PushDataItem(CborMajorType.Array, arrayLength); return(arrayLength); } }
public int?ReadStartArray() { CborInitialByte header = PeekInitialByte(expectedType: CborMajorType.Array); if (header.AdditionalInfo == CborAdditionalInfo.IndefiniteLength) { if (_isConformanceLevelCheckEnabled && CborConformanceLevelHelpers.RequiresDefiniteLengthItems(ConformanceLevel)) { throw new FormatException("Indefinite-length items are not supported under the current conformance level."); } AdvanceBuffer(1); PushDataItem(CborMajorType.Array, null); return(null); } else { ulong arrayLength = ReadUnsignedInteger(_buffer.Span, header, out int additionalBytes); if (arrayLength > (ulong)_buffer.Length) { throw new FormatException("Insufficient buffer size for declared definite length in CBOR data item."); } AdvanceBuffer(1 + additionalBytes); PushDataItem(CborMajorType.Array, (int)arrayLength); return((int)arrayLength); } }
internal static void WriteByteStringLength(IncrementalHash hasher, ulong value) { const CborMajorType MajorType = CborMajorType.ByteString; CborInitialByte initialByte; if (value < (byte)CborAdditionalInfo.Additional8BitData) { initialByte = new CborInitialByte(MajorType, (CborAdditionalInfo)value); hasher.AppendData(stackalloc byte[] { initialByte.InitialByte });
public bool TryPeek(out CborInitialByte result) { if (!_buffer.IsEmpty) { result = new CborInitialByte(_buffer[0]); return(true); } result = default; return(false); }
public CborInitialByte Peek(CborMajorType expectedType) { if (_buffer.IsEmpty) { throw new InvalidOperationException("end of buffer"); } var result = new CborInitialByte(_buffer[0]); if (expectedType != result.MajorType) { throw new InvalidOperationException("Data item major type mismatch."); } return(result); }
public void ReadEndArray() { if (_remainingDataItems == null) { CborInitialByte value = PeekInitialByte(); if (value.InitialByte != CborInitialByte.IndefiniteLengthBreakByte) { throw new InvalidOperationException("Not at end of indefinite-length array."); } PopDataItem(expectedType: CborMajorType.Array); AdvanceDataItemCounters(); AdvanceBuffer(1); } else { PopDataItem(expectedType: CborMajorType.Array); AdvanceDataItemCounters(); } }