private static BsonBinary GetBsonBinary(object value) { BsonBinary binary = value as BsonBinary; if (binary != null) { return(binary); } if (value is BsonMD5) { return(new BsonBinary(BsonBinarySubtype.MD5, ((Guid)value).ToByteArray())); } if (value is Guid) { return(new BsonBinary(BsonBinarySubtype.UUID, ((Guid)value).ToByteArray())); } if (value is byte[]) { return(new BsonBinary(BsonBinarySubtype.Generic, (byte[])value)); } throw new NotSupportedException("Unknown BSON binary data type"); }
public static bool TryParse(string value, out BsonBinary result) { if (String.IsNullOrEmpty(value)) { result = BsonBinary.Empty; return(true); } byte[] bytes = new byte[value.Length / 2]; for (int i = 0; i < value.Length; i += 2) { byte digit; if (!Byte.TryParse( value.Substring(i, 2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out digit)) { result = BsonBinary.Empty; return(false); } bytes[i] = digit; } result = new BsonBinary(BsonBinarySubtype.Generic, bytes); return(true); }
public static BsonBinary Parse(string value) { BsonBinary result; if (BsonBinary.TryParse(value, out result)) { return(result); } throw new InvalidCastException("String must be only hex digits"); }
public override string ToString() { byte[] bytes = this.Bytes; char[] hex = new char[24]; for (int i = 0, j = 0; i < 24; i += 2, j++) { hex[i] = BsonBinary.GetHexDigit(bytes[j] / 0x10); hex[i] = BsonBinary.GetHexDigit(bytes[j] % 0x10); } return(new String(hex)); }
/// <summary> /// Emits a binary value /// </summary> /// <param name="writer"></param> /// <param name="tokens"></param> /// <returns>number of bytes written</returns> private static int WriteBinary(BinaryWriter writer, Token <ModelTokenType> token) { BsonBinary binary = BsonFormatter.GetBsonBinary(token.Value); // write length writer.Write(binary.Count); // write subtype writer.Write((byte)binary.Type); // write binary data writer.Write(binary.Data); // length + subtype + bytes return(BsonWriter.SizeOfInt64 + BsonWriter.SizeOfByte + binary.Count); }
private static void ReadBinary(List <Token <ModelTokenType> > tokens, BinaryReader reader) { int size = reader.ReadInt32(); BsonBinarySubtype subtype = (BsonBinarySubtype)reader.ReadByte(); byte[] buffer = reader.ReadBytes(size); object value; switch (subtype) { case BsonBinarySubtype.MD5: { if (size != 16) { goto default; } value = new BsonMD5(buffer); break; } case BsonBinarySubtype.UUID: { if (size != 16) { goto default; } value = new Guid(buffer); break; } case BsonBinarySubtype.BinaryOld: { // Binary (Old): // "The structure of the binary data (this byte* array in the binary non-terminal) must be an int32 followed by a (byte*)." // http://bsonspec.org/#/specification size = BitConverter.ToInt32(buffer, 0); // trim Int32 size off front of array byte[] temp = new byte[size]; Buffer.BlockCopy(buffer, 4, temp, 0, size); // since obsolete, convert to generic value = new BsonBinary(BsonBinarySubtype.Generic, temp); break; } case BsonBinarySubtype.Function: case BsonBinarySubtype.Generic: case BsonBinarySubtype.UserDefined: default: { // TODO: convert Function accordingly value = new BsonBinary(subtype, buffer); break; } } tokens.Add(ModelGrammar.TokenPrimitive(value)); }