public CBORObject ToCBORObject(Uri uri) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } string uriString = uri.ToString(); return(CBORObject.FromObjectAndTag(uriString, (int)32)); }
/// <include file='../../docs.xml' /// path='docs/doc[@name="M:PeterO.Cbor.CBORTag37.ToCBORObject(System.Guid)"]/*'/> public CBORObject ToCBORObject(Guid obj) { byte[] bytes = obj.ToByteArray(); var bytes2 = new byte[16]; Array.Copy(bytes, bytes2, 16); // Swap the bytes to conform with the UUID RFC bytes2[0] = bytes[3]; bytes2[1] = bytes[2]; bytes2[2] = bytes[1]; bytes2[3] = bytes[0]; bytes2[4] = bytes[5]; bytes2[5] = bytes[4]; bytes2[6] = bytes[7]; bytes2[7] = bytes[6]; return(CBORObject.FromObjectAndTag(bytes2, (int)37)); }
public CBORObject ToCBORObject(Uri uri) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } string uriString = uri.ToString(); var nonascii = false; for (var i = 0; i < uriString.Length; ++i) { nonascii |= uriString[i] >= 0x80; } int tag = nonascii ? 266 : 32; if (!URIUtility.HasScheme(uriString)) { tag = 267; } return(CBORObject.FromObjectAndTag(uriString, (int)tag)); }
private static CBORObject FromObjectAndInnerTags( object objectValue, CBORObject objectWithTags) { CBORObject newObject = CBORObject.FromObject(objectValue); if (!objectWithTags.IsTagged) { return(newObject); } objectWithTags = objectWithTags.UntagOne(); if (!objectWithTags.IsTagged) { return(newObject); } EInteger[] tags = objectWithTags.GetAllTags(); for (int i = tags.Length - 1; i >= 0; --i) { newObject = CBORObject.FromObjectAndTag(newObject, tags[i]); } return(newObject); }
/// <summary>Converts a date/time in the form of a year, month, day, /// hour, minute, second, fractional seconds, and time offset to a CBOR /// object.</summary> /// <param name='bigYear'>The parameter <paramref name='bigYear'/> is a /// Numbers.EInteger object.</param> /// <param name='lesserFields'>An array that will store the fields /// (other than the year) of the date and time. See the /// TryGetDateTimeFields method for information on the "lesserFields" /// parameter.</param> /// <returns>A CBOR object encoding the given date fields according to /// the conversion type used to create this date converter.</returns> /// <exception cref='ArgumentNullException'>The parameter <paramref /// name='bigYear'/> or <paramref name='lesserFields'/> is /// null.</exception> /// <exception cref='PeterO.Cbor.CBORException'>An error occurred in /// conversion.</exception> public CBORObject DateTimeFieldsToCBORObject(EInteger bigYear, int[] lesserFields) { if (bigYear == null) { throw new ArgumentNullException(nameof(bigYear)); } if (lesserFields == null) { throw new ArgumentNullException(nameof(lesserFields)); } if (lesserFields.Length < 7) { throw new ArgumentException("\"lesserFields\" + \"'s length\" (" + lesserFields.Length + ") is not greater or equal to 7"); } try { switch (this.convType) { case ConversionType.TaggedString: { string str = CBORUtilities.ToAtomDateTimeString(bigYear, lesserFields); return(CBORObject.FromObjectAndTag(str, 0)); } case ConversionType.TaggedNumber: case ConversionType.UntaggedNumber: try { var status = new int[1]; EFloat ef = CBORUtilities.DateTimeToIntegerOrDouble( bigYear, lesserFields, status); if (status[0] == 0) { return(this.convType == ConversionType.TaggedNumber ? CBORObject.FromObjectAndTag(ef.ToEInteger(), 1) : CBORObject.FromObject(ef.ToEInteger())); } else if (status[0] == 1) { return(this.convType == ConversionType.TaggedNumber ? CBORObject.FromFloatingPointBits(ef.ToDoubleBits(), 8).WithTag(1) : CBORObject.FromFloatingPointBits(ef.ToDoubleBits(), 8)); } else { throw new CBORException("Too big or small to fit an integer" + "\u0020or" + "\u0020floating-point number"); } } catch (ArgumentException ex) { throw new CBORException(ex.Message, ex); } default: throw new CBORException("Internal error"); } } catch (ArgumentException ex) { throw new CBORException(ex.Message, ex); } }
internal static CBORObject ParseJSONNumber( string str, int offset, int count, JSONOptions options, int[] endOfNumber) { if (String.IsNullOrEmpty(str) || count <= 0) { return(null); } if (offset < 0 || offset > str.Length) { return(null); } if (count > str.Length || str.Length - offset < count) { return(null); } JSONOptions opt = options ?? DefaultOptions; bool preserveNegativeZero = options.PreserveNegativeZero; JSONOptions.ConversionMode kind = options.NumberConversion; int endPos = offset + count; int initialOffset = offset; var negative = false; if (str[initialOffset] == '-') { ++offset; negative = true; } int numOffset = offset; var haveDecimalPoint = false; var haveDigits = false; var haveDigitsAfterDecimal = false; var haveExponent = false; int i = offset; var decimalPointPos = -1; // Check syntax int k = i; if (endPos - 1 > k && str[k] == '0' && str[k + 1] >= '0' && str[k + 1] <= '9') { if (endOfNumber != null) { endOfNumber[0] = k + 2; } return(null); } for (; k < endPos; ++k) { char c = str[k]; if (c >= '0' && c <= '9') { haveDigits = true; haveDigitsAfterDecimal |= haveDecimalPoint; } else if (c == '.') { if (!haveDigits || haveDecimalPoint) { // no digits before the decimal point, // or decimal point already seen if (endOfNumber != null) { endOfNumber[0] = k; } return(null); } haveDecimalPoint = true; decimalPointPos = k; } else if (c == 'E' || c == 'e') { ++k; haveExponent = true; break; } else { if (endOfNumber != null) { endOfNumber[0] = k; // Check if character can validly appear after a JSON number if (c != ',' && c != ']' && c != '}' && c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09) { return(null); } else { endPos = k; break; } } return(null); } } if (!haveDigits || (haveDecimalPoint && !haveDigitsAfterDecimal)) { if (endOfNumber != null) { endOfNumber[0] = k; } return(null); } if (haveExponent) { haveDigits = false; if (k == endPos) { if (endOfNumber != null) { endOfNumber[0] = k; } return(null); } char c = str[k]; if (c == '+' || c == '-') { ++k; } for (; k < endPos; ++k) { c = str[k]; if (c >= '0' && c <= '9') { haveDigits = true; } else if (endOfNumber != null) { endOfNumber[0] = k; // Check if character can validly appear after a JSON number if (c != ',' && c != ']' && c != '}' && c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09) { return(null); } else { endPos = k; break; } } else { return(null); } } if (!haveDigits) { if (endOfNumber != null) { endOfNumber[0] = k; } return(null); } } if (endOfNumber != null) { endOfNumber[0] = endPos; } if (!haveExponent && !haveDecimalPoint && (endPos - numOffset) <= 16) { // Very common case of all-digit JSON number strings // less than 2^53 (with or without number sign) long v = 0L; int vi = numOffset; for (; vi < endPos; ++vi) { v = (v * 10) + (int)(str[vi] - '0'); } if ((v != 0 || !negative) && v < (1L << 53) - 1) { if (negative) { v = -v; } if (kind == JSONOptions.ConversionMode.Double) { return(CBORObject.FromObject((double)v)); } else if (kind == JSONOptions.ConversionMode.Decimal128) { return(CBORObject.FromObject(EDecimal.FromInt64(v))); } else { return(CBORObject.FromObject(v)); } } } if (kind == JSONOptions.ConversionMode.Full) { if (!haveDecimalPoint && !haveExponent) { EInteger ei = EInteger.FromSubstring(str, initialOffset, endPos); if (preserveNegativeZero && ei.IsZero && negative) { // TODO: In next major version, change to EDecimal.NegativeZero return(CBORObject.FromFloatingPointBits(0x8000, 2)); } return(CBORObject.FromObject(ei)); } if (!haveExponent && haveDecimalPoint && (endPos - numOffset) <= 19) { // No more than 18 digits plus one decimal point (which // should fit a long) long lv = 0L; int expo = -(endPos - (decimalPointPos + 1)); int vi = numOffset; for (; vi < decimalPointPos; ++vi) { lv = checked ((lv * 10) + (int)(str[vi] - '0')); } for (vi = decimalPointPos + 1; vi < endPos; ++vi) { lv = checked ((lv * 10) + (int)(str[vi] - '0')); } if (negative) { lv = -lv; } if (!negative || lv != 0) { CBORObject cbor = CBORObject.NewArray() .Add(expo).Add(lv); return(CBORObject.FromObjectAndTag(cbor, 4)); } } EDecimal ed = EDecimal.FromString( str, initialOffset, endPos - initialOffset); if (ed.IsZero && negative) { if (preserveNegativeZero && ed.Exponent.IsZero) { // TODO: In next major version, use EDecimal return(CBORObject.FromFloatingPointBits(0x8000, 2)); } else if (!preserveNegativeZero) { ed = ed.Negate(); } } return(CBORObject.FromObject(ed)); } else if (kind == JSONOptions.ConversionMode.Double) { double dbl = EFloat.FromString( str, initialOffset, endPos - initialOffset, EContext.Binary64).ToDouble(); if (!preserveNegativeZero && dbl == 0.0) { dbl = 0.0; } return(CBORObject.FromObject(dbl)); } else if (kind == JSONOptions.ConversionMode.Decimal128) { EDecimal ed = EDecimal.FromString( str, initialOffset, endPos - initialOffset, EContext.Decimal128); if (!preserveNegativeZero && ed.IsNegative && ed.IsZero) { ed = ed.Negate(); } return(CBORObject.FromObject(ed)); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble) { double dbl = EFloat.FromString( str, initialOffset, endPos - initialOffset, EContext.Binary64).ToDouble(); if (!Double.IsNaN(dbl) && dbl >= -9007199254740991.0 && dbl <= 9007199254740991.0 && Math.Floor(dbl) == dbl) { var idbl = (long)dbl; return(CBORObject.FromObject(idbl)); } return(CBORObject.FromObject(dbl)); } else if (kind == JSONOptions.ConversionMode.IntOrFloat) { EContext ctx = EContext.Binary64.WithBlankFlags(); double dbl = EFloat.FromString( str, initialOffset, endPos - initialOffset, ctx).ToDouble(); if ((ctx.Flags & EContext.FlagInexact) != 0) { // Inexact conversion to double, meaning that the string doesn't // represent an integer in [-(2^53)+1, 2^53), which is representable // exactly as double, so treat as ConversionMode.Double if (!preserveNegativeZero && dbl == 0.0) { dbl = 0.0; } return(CBORObject.FromObject(dbl)); } else { // Exact conversion; treat as ConversionMode.IntToFloatFromDouble if (!Double.IsNaN(dbl) && dbl >= -9007199254740991.0 && dbl <= 9007199254740991.0 && Math.Floor(dbl) == dbl) { var idbl = (long)dbl; return(CBORObject.FromObject(idbl)); } return(CBORObject.FromObject(dbl)); } } else { throw new ArgumentException("Unsupported conversion kind."); } }
public CBORObject ToCBORObject(DateTime obj) { return(CBORObject.FromObjectAndTag(DateTimeToString(obj), 0)); }
/// <include file='../../docs.xml' /// path='docs/doc[@name="M:PeterO.Cbor.CBORTag37.ToCBORObject(System.Guid)"]/*'/> public CBORObject ToCBORObject(Guid obj) { byte[] bytes = PropertyMap.UUIDToBytes(obj); return(CBORObject.FromObjectAndTag(bytes, (int)37)); }
public CBORObject ReadForFirstByte( int firstbyte, CBORTypeFilter filter) { if (this.depth > 500) { throw new CBORException("Too deeply nested"); } if (firstbyte < 0) { throw new CBORException("Premature end of data"); } if (firstbyte == 0xff) { throw new CBORException("Unexpected break code encountered"); } int type = (firstbyte >> 5) & 0x07; int additional = firstbyte & 0x1f; int expectedLength = CBORObject.GetExpectedLength(firstbyte); // Data checks if (expectedLength == -1) { // if the head byte is invalid throw new CBORException("Unexpected data encountered"); } if (filter != null) { // Check for valid major types if asked if (!filter.MajorTypeMatches(type)) { throw new CBORException("Unexpected data type encountered"); } if (firstbyte >= 0xe0 && firstbyte <= 0xff && firstbyte != 0xf9 && firstbyte != 0xfa && firstbyte != 0xfb) { if (!filter.NonFPSimpleValueAllowed()) { throw new CBORException("Unexpected data type encountered"); } } } // Check if this represents a fixed object CBORObject fixedObject = CBORObject.GetFixedObject(firstbyte); if (fixedObject != null) { return(fixedObject); } // Read fixed-length data byte[] data = null; if (expectedLength != 0) { data = new byte[expectedLength]; // include the first byte because GetFixedLengthObject // will assume it exists for some head bytes data[0] = unchecked ((byte)firstbyte); if (expectedLength > 1 && this.stream.Read(data, 1, expectedLength - 1) != expectedLength - 1) { throw new CBORException("Premature end of data"); } CBORObject cbor = CBORObject.GetFixedLengthObject(firstbyte, data); if (this.stringRefs != null && (type == 2 || type == 3)) { this.stringRefs.AddStringIfNeeded(cbor, expectedLength - 1); } return(cbor); } var uadditional = (long)additional; EInteger bigintAdditional = EInteger.Zero; var hasBigAdditional = false; data = new byte[8]; var lowAdditional = 0; switch (firstbyte & 0x1f) { case 24: { int tmp = this.stream.ReadByte(); if (tmp < 0) { throw new CBORException("Premature end of data"); } lowAdditional = tmp; uadditional = lowAdditional; break; } case 25: { if (this.stream.Read(data, 0, 2) != 2) { throw new CBORException("Premature end of data"); } lowAdditional = ((int)(data[0] & (int)0xff)) << 8; lowAdditional |= (int)(data[1] & (int)0xff); uadditional = lowAdditional; break; } case 26: { if (this.stream.Read(data, 0, 4) != 4) { throw new CBORException("Premature end of data"); } uadditional = ((long)(data[0] & (long)0xff)) << 24; uadditional |= ((long)(data[1] & (long)0xff)) << 16; uadditional |= ((long)(data[2] & (long)0xff)) << 8; uadditional |= (long)(data[3] & (long)0xff); break; } case 27: { if (this.stream.Read(data, 0, 8) != 8) { throw new CBORException("Premature end of data"); } if ((((int)data[0]) & 0x80) != 0) { // Won't fit in a signed 64-bit number var uabytes = new byte[9]; uabytes[0] = data[7]; uabytes[1] = data[6]; uabytes[2] = data[5]; uabytes[3] = data[4]; uabytes[4] = data[3]; uabytes[5] = data[2]; uabytes[6] = data[1]; uabytes[7] = data[0]; uabytes[8] = 0; hasBigAdditional = true; bigintAdditional = EInteger.FromBytes(uabytes, true); } else { uadditional = ((long)(data[0] & (long)0xff)) << 56; uadditional |= ((long)(data[1] & (long)0xff)) << 48; uadditional |= ((long)(data[2] & (long)0xff)) << 40; uadditional |= ((long)(data[3] & (long)0xff)) << 32; uadditional |= ((long)(data[4] & (long)0xff)) << 24; uadditional |= ((long)(data[5] & (long)0xff)) << 16; uadditional |= ((long)(data[6] & (long)0xff)) << 8; uadditional |= (long)(data[7] & (long)0xff); } break; } } // The following doesn't check for major types 0 and 1, // since all of them are fixed-length types and are // handled in the call to GetFixedLengthObject. if (type == 2) // Byte string { if (additional == 31) { // Streaming byte string using (var ms = new MemoryStream()) { // Requires same type as this one while (true) { int nextByte = this.stream.ReadByte(); if (nextByte == 0xff) { // break if the "break" code was read break; } long len = ReadDataLength(this.stream, nextByte, 2); if ((len >> 63) != 0 || len > Int32.MaxValue) { throw new CBORException("Length" + ToUnsignedBigInteger(len) + " is bigger than supported "); } if (nextByte != 0x40) { // NOTE: 0x40 means the empty byte string ReadByteData(this.stream, len, ms); } } if (ms.Position > Int32.MaxValue) { throw new CBORException("Length of bytes to be streamed is bigger than supported "); } data = ms.ToArray(); return(new CBORObject( CBORObject.CBORObjectTypeByteString, data)); } } else { if (hasBigAdditional) { throw new CBORException("Length of " + CBORUtilities.BigIntToString(bigintAdditional) + " is bigger than supported"); } if (uadditional > Int32.MaxValue) { throw new CBORException("Length of " + CBORUtilities.LongToString(uadditional) + " is bigger than supported"); } data = ReadByteData(this.stream, uadditional, null); var cbor = new CBORObject(CBORObject.CBORObjectTypeByteString, data); if (this.stringRefs != null) { int hint = (uadditional > Int32.MaxValue || hasBigAdditional) ? Int32.MaxValue : (int)uadditional; this.stringRefs.AddStringIfNeeded(cbor, hint); } return(cbor); } } if (type == 3) // Text string { if (additional == 31) { // Streaming text string var builder = new StringBuilder(); while (true) { int nextByte = this.stream.ReadByte(); if (nextByte == 0xff) { // break if the "break" code was read break; } long len = ReadDataLength(this.stream, nextByte, 3); if ((len >> 63) != 0 || len > Int32.MaxValue) { throw new CBORException("Length" + ToUnsignedBigInteger(len) + " is bigger than supported"); } if (nextByte != 0x60) { // NOTE: 0x60 means the empty string if (PropertyMap.ExceedsKnownLength(this.stream, len)) { throw new CBORException("Premature end of data"); } switch ( DataUtilities.ReadUtf8( this.stream, (int)len, builder, false)) { case -1: throw new CBORException("Invalid UTF-8"); case -2: throw new CBORException("Premature end of data"); } } } return(new CBORObject( CBORObject.CBORObjectTypeTextString, builder.ToString())); } else { if (hasBigAdditional) { throw new CBORException("Length of " + CBORUtilities.BigIntToString(bigintAdditional) + " is bigger than supported"); } if (uadditional > Int32.MaxValue) { throw new CBORException("Length of " + CBORUtilities.LongToString(uadditional) + " is bigger than supported"); } if (PropertyMap.ExceedsKnownLength(this.stream, uadditional)) { throw new CBORException("Premature end of data"); } var builder = new StringBuilder(); switch ( DataUtilities.ReadUtf8( this.stream, (int)uadditional, builder, false)) { case -1: throw new CBORException("Invalid UTF-8"); case -2: throw new CBORException("Premature end of data"); } var cbor = new CBORObject( CBORObject.CBORObjectTypeTextString, builder.ToString()); if (this.stringRefs != null) { int hint = (uadditional > Int32.MaxValue || hasBigAdditional) ? Int32.MaxValue : (int)uadditional; this.stringRefs.AddStringIfNeeded(cbor, hint); } return(cbor); } } if (type == 4) // Array { CBORObject cbor = CBORObject.NewArray(); if (additional == 31) { var vtindex = 0; // Indefinite-length array while (true) { int headByte = this.stream.ReadByte(); if (headByte < 0) { throw new CBORException("Premature end of data"); } if (headByte == 0xff) { // Break code was read break; } if (filter != null && !filter.ArrayIndexAllowed(vtindex)) { throw new CBORException("Array is too long"); } ++this.depth; CBORObject o = this.ReadForFirstByte( headByte, filter == null ? null : filter.GetSubFilter(vtindex)); --this.depth; cbor.Add(o); ++vtindex; } return(cbor); } if (hasBigAdditional) { throw new CBORException("Length of " + CBORUtilities.BigIntToString(bigintAdditional) + " is bigger than supported"); } if (uadditional > Int32.MaxValue) { throw new CBORException("Length of " + CBORUtilities.LongToString(uadditional) + " is bigger than supported"); } if (filter != null && !filter.ArrayLengthMatches(uadditional)) { throw new CBORException("Array is too long"); } if (PropertyMap.ExceedsKnownLength(this.stream, uadditional)) { throw new CBORException("Remaining data too small for array length"); } ++this.depth; for (long i = 0; i < uadditional; ++i) { cbor.Add( this.Read(filter == null ? null : filter.GetSubFilter(i))); } --this.depth; return(cbor); } if (type == 5) // Map, type 5 { CBORObject cbor = CBORObject.NewMap(); if (additional == 31) { // Indefinite-length map while (true) { int headByte = this.stream.ReadByte(); if (headByte < 0) { throw new CBORException("Premature end of data"); } if (headByte == 0xff) { // Break code was read break; } ++this.depth; CBORObject key = this.ReadForFirstByte(headByte, null); CBORObject value = this.Read(null); --this.depth; if (this.policy == CBORDuplicatePolicy.Disallow) { if (cbor.ContainsKey(key)) { throw new CBORException("Duplicate key already exists: " + key); } } cbor[key] = value; } return(cbor); } if (hasBigAdditional) { throw new CBORException("Length of " + CBORUtilities.BigIntToString(bigintAdditional) + " is bigger than supported"); } if (uadditional > Int32.MaxValue) { throw new CBORException("Length of " + CBORUtilities.LongToString(uadditional) + " is bigger than supported"); } if (PropertyMap.ExceedsKnownLength(this.stream, uadditional)) { throw new CBORException("Remaining data too small for map length"); } for (long i = 0; i < uadditional; ++i) { ++this.depth; CBORObject key = this.Read(null); CBORObject value = this.Read(null); --this.depth; if (this.policy == CBORDuplicatePolicy.Disallow) { if (cbor.ContainsKey(key)) { throw new CBORException("Duplicate key already exists: " + key); } } cbor[key] = value; } return(cbor); } if (type == 6) // Tagged item { ICBORTag taginfo = null; var haveFirstByte = false; var newFirstByte = -1; if (!hasBigAdditional) { if (filter != null && !filter.TagAllowed(uadditional)) { throw new CBORException("Unexpected tag encountered: " + uadditional); } int uad = uadditional >= 257 ? 257 : (uadditional < 0 ? 0 : (int)uadditional); switch (uad) { case 256: // Tag 256: String namespace this.stringRefs = this.stringRefs ?? (new StringRefs()); this.stringRefs.Push(); break; case 25: // String reference if (this.stringRefs == null) { throw new CBORException("No stringref namespace"); } break; case 28: case 29: this.hasSharableObjects = true; break; } taginfo = CBORObject.FindTagConverterLong(uadditional); } else { if (filter != null && !filter.TagAllowed(bigintAdditional)) { throw new CBORException("Unexpected tag encountered: " + uadditional); } taginfo = CBORObject.FindTagConverter(bigintAdditional); } ++this.depth; CBORObject o = haveFirstByte ? this.ReadForFirstByte( newFirstByte, taginfo == null ? null : taginfo.GetTypeFilter()) : this.Read(taginfo == null ? null : taginfo.GetTypeFilter()); --this.depth; if (hasBigAdditional) { return(CBORObject.FromObjectAndTag(o, bigintAdditional)); } if (uadditional < 65536) { int uaddl = uadditional >= 257 ? 257 : (uadditional < 0 ? 0 : (int)uadditional); switch (uaddl) { case 256: // string tag this.stringRefs.Pop(); break; case 25: // stringref tag return(this.stringRefs.GetString(o.AsEInteger())); } return(CBORObject.FromObjectAndTag( o, (int)uadditional)); } return(CBORObject.FromObjectAndTag( o, (EInteger)uadditional)); } throw new CBORException("Unexpected data encountered"); }
public CBORObject ReadForFirstByte(int firstbyte) { if (this.depth > 500) { throw new CBORException("Too deeply nested"); } if (firstbyte < 0) { throw new CBORException("Premature end of data"); } if (firstbyte == 0xff) { throw new CBORException("Unexpected break code encountered"); } int type = (firstbyte >> 5) & 0x07; int additional = firstbyte & 0x1f; long uadditional; CBORObject fixedObject; if (this.options.Ctap2Canonical) { if (additional >= 0x1c) { // NOTE: Includes stop byte and indefinite length data items throw new CBORException("Invalid canonical CBOR encountered"); } // Check if this represents a fixed object (NOTE: All fixed objects // comply with CTAP2 canonical CBOR). fixedObject = CBORObject.GetFixedObject(firstbyte); if (fixedObject != null) { return(fixedObject); } if (type == 6) { throw new CBORException("Tags not allowed in canonical CBOR"); } uadditional = ReadDataLength( this.stream, firstbyte, type, type == 7); if (type == 0) { return((uadditional >> 63) != 0 ? CBORObject.FromObject(ToUnsignedEInteger(uadditional)) : CBORObject.FromObject(uadditional)); } else if (type == 1) { return((uadditional >> 63) != 0 ? CBORObject.FromObject( ToUnsignedEInteger(uadditional).Add(1).Negate()) : CBORObject.FromObject((-uadditional) - 1L)); } else if (type == 7) { if (additional < 24) { return(CBORObject.FromSimpleValue(additional)); } else if (additional == 24 && uadditional < 32) { throw new CBORException("Invalid simple value encoding"); } else if (additional == 24) { return(CBORObject.FromSimpleValue((int)uadditional)); } else if (additional == 25) { return(CBORObject.FromFloatingPointBits(uadditional, 2)); } else if (additional == 26) { return(CBORObject.FromFloatingPointBits(uadditional, 4)); } else if (additional == 27) { return(CBORObject.FromFloatingPointBits(uadditional, 8)); } } else if (type >= 2 && type <= 5) { return(this.ReadStringArrayMap(type, uadditional)); } throw new CBORException("Unexpected data encountered"); } int expectedLength = CBORObject.GetExpectedLength(firstbyte); // Data checks if (expectedLength == -1) { // if the head byte is invalid throw new CBORException("Unexpected data encountered"); } // Check if this represents a fixed object fixedObject = CBORObject.GetFixedObject(firstbyte); if (fixedObject != null) { return(fixedObject); } // Read fixed-length data byte[] data = null; if (expectedLength != 0) { data = new byte[expectedLength]; // include the first byte because GetFixedLengthObject // will assume it exists for some head bytes data[0] = unchecked ((byte)firstbyte); if (expectedLength > 1 && this.stream.Read(data, 1, expectedLength - 1) != expectedLength - 1) { throw new CBORException("Premature end of data"); } CBORObject cbor = CBORObject.GetFixedLengthObject(firstbyte, data); if (this.stringRefs != null && (type == 2 || type == 3)) { this.stringRefs.AddStringIfNeeded(cbor, expectedLength - 1); } return(cbor); } if (additional == 31) { // Indefinite-length for major types 2 to 5 (other major // types were already handled in the call to // GetFixedLengthObject). switch (type) { case 2: { // Streaming byte string using (var ms = new MemoryStream()) { // Requires same type as this one while (true) { int nextByte = this.stream.ReadByte(); if (nextByte == 0xff) { // break if the "break" code was read break; } long len = ReadDataLength(this.stream, nextByte, 2); if ((len >> 63) != 0 || len > Int32.MaxValue) { throw new CBORException("Length" + ToUnsignedEInteger(len) + " is bigger than supported "); } if (nextByte != 0x40) { // NOTE: 0x40 means the empty byte string ReadByteData(this.stream, len, ms); } } if (ms.Position > Int32.MaxValue) { throw new CBORException("Length of bytes to be streamed is bigger" + "\u0020than supported "); } data = ms.ToArray(); return(CBORObject.FromRaw(data)); } } case 3: { // Streaming text string var builder = new StringBuilder(); while (true) { int nextByte = this.stream.ReadByte(); if (nextByte == 0xff) { // break if the "break" code was read break; } long len = ReadDataLength(this.stream, nextByte, 3); if ((len >> 63) != 0 || len > Int32.MaxValue) { throw new CBORException("Length" + ToUnsignedEInteger(len) + " is bigger than supported"); } if (nextByte != 0x60) { // NOTE: 0x60 means the empty string if (PropertyMap.ExceedsKnownLength(this.stream, len)) { throw new CBORException("Premature end of data"); } switch ( DataUtilities.ReadUtf8( this.stream, (int)len, builder, false)) { case -1: throw new CBORException("Invalid UTF-8"); case -2: throw new CBORException("Premature end of data"); } } } return(CBORObject.FromRaw(builder.ToString())); } case 4: { CBORObject cbor = CBORObject.NewArray(); var vtindex = 0; // Indefinite-length array while (true) { int headByte = this.stream.ReadByte(); if (headByte < 0) { throw new CBORException("Premature end of data"); } if (headByte == 0xff) { // Break code was read break; } ++this.depth; CBORObject o = this.ReadForFirstByte( headByte); --this.depth; cbor.Add(o); ++vtindex; } return(cbor); } case 5: { CBORObject cbor = CBORObject.NewMap(); // Indefinite-length map while (true) { int headByte = this.stream.ReadByte(); if (headByte < 0) { throw new CBORException("Premature end of data"); } if (headByte == 0xff) { // Break code was read break; } ++this.depth; CBORObject key = this.ReadForFirstByte(headByte); CBORObject value = this.ReadInternal(); --this.depth; if (!this.options.AllowDuplicateKeys) { if (cbor.ContainsKey(key)) { throw new CBORException("Duplicate key already exists"); } } cbor[key] = value; } return(cbor); } default: throw new CBORException("Unexpected data encountered"); } } EInteger bigintAdditional = EInteger.Zero; uadditional = ReadDataLength(this.stream, firstbyte, type); // The following doesn't check for major types 0 and 1, // since all of them are fixed-length types and are // handled in the call to GetFixedLengthObject. if (type >= 2 && type <= 5) { return(this.ReadStringArrayMap(type, uadditional)); } if (type == 6) // Tagged item { var haveFirstByte = false; var newFirstByte = -1; if (this.options.ResolveReferences && (uadditional >> 32) == 0) { // NOTE: HandleItemTag treats only certain tags up to 256 specially this.HandleItemTag(uadditional); } ++this.depth; CBORObject o = haveFirstByte ? this.ReadForFirstByte( newFirstByte) : this.ReadInternal(); --this.depth; if ((uadditional >> 63) != 0) { return(CBORObject.FromObjectAndTag(o, ToUnsignedEInteger(uadditional))); } if (uadditional < 65536) { if (this.options.ResolveReferences) { int uaddl = uadditional >= 257 ? 257 : (uadditional < 0 ? 0 : (int)uadditional); switch (uaddl) { case 256: // string tag this.stringRefs.Pop(); break; case 25: // stringref tag if (o.IsTagged || o.Type != CBORType.Integer) { throw new CBORException("stringref must be an unsigned" + "\u0020integer"); } return(this.stringRefs.GetString(o.AsEIntegerValue())); } } return(CBORObject.FromObjectAndTag( o, (int)uadditional)); } return(CBORObject.FromObjectAndTag( o, (EInteger)uadditional)); } throw new CBORException("Unexpected data encountered"); }