public void AddType(Type type, byte[] bytes, ref int index) { var context = ContextProvider.GetContext(type); bytes[index++] = this.Version; bytes.AddBytes <int>(context.SpaceId, 4, ref index); // space type bytes.AddBytes <short>(context.TypeId, ref index); // data type }
public void AddBytes(object obj, byte[] bytes, ref int index) { var dateTime = (DateTime)obj; var value = dateTime.ToBinary(); bytes.AddBytes <long>(value, 8, ref index); }
public void AddArrayBytes(object obj, byte[] bytes, ref int index) { var array = (byte[])obj; var length = array.Length; bytes.AddBytes <int>(length, 4, ref index); Buffer.BlockCopy(array, 0, bytes, index, length); index += length; }
public void AddBytes(object obj, byte[] bytes, ref int index) { var header = (RequestHeader)obj; bytes[index++] = (byte)(header.RequestType == RequestType.Request ? 1 : 2); bytes[index++] = (byte)(header.CanBeCached ? 1 : 0); bytes[index++] = header.RezervedFlag; // additional flags bytes.AddBytes <int>(header.RequestId, 4, ref index); // request id }
// add byte array length checks in ValueConverter or maybe byte.Add extensions, tiksliau get. public virtual void AddBytes(object obj, byte[] bytes, ref int index) // byte is 2024 length { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } if (bytes == null) { throw new ArgumentNullException(nameof(bytes)); } var type = obj.GetType(); var converter = ConverterProvider.GetConverter(type, out var converterTypeId); bytes.AddBytes <short>(Version, 2, ref index); bytes.AddBytes <short>(converterTypeId, 2, ref index); converter.AddBytes(obj, bytes, ref index); }
public static void AddBytes(byte[] bytes, object obj, ref int index) { var value = (int)obj; if (value > ushort.MaxValue || value < ushort.MinValue) { throw new Exception($"Enum value is out of range. '{value}'"); } bytes.AddBytes <ushort>((ushort)value, size, ref index); }
public void AddBytes(object obj, byte[] bytes, ref int index) { //byte[] decimalBytes = BitConverterExt.GetBytes(testDecimal); var b = BitConverterExt.GetBytes((decimal)obj); if (b.Length != 16) { throw new ArgumentException($"Decimal bytes lenght should be length of 16 but is {b.Length}"); } bytes.AddBytes(b, ref index); }
public void AddBytes(object obj, byte[] bytes, ref int index) { var byteArray = (ByteArray)obj; var array = byteArray.Bytes; var length = array.Length; if (length > short.MaxValue || length < ushort.MinValue) { throw new Exception($"Array length should be between '{ushort.MinValue}' and '{short.MaxValue}' but is '{length}'"); } bytes.AddBytes <ushort>((ushort)length, 2, ref index); Buffer.BlockCopy(array, 0, bytes, index, length); index += length; }
public void AddBytes(object obj, byte[] bytes, ref int index) { var value = (string)obj; var valueBytes = encodings[defaultEncoding].GetBytes(value); if (valueBytes.Length > ushort.MaxValue) { throw new ArgumentNullException($"String length should not exceed '{ushort.MaxValue}'. Current is '{valueBytes.Length}'"); } bytes[index++] = defaultEncoding; bytes.AddBytes <ushort>((ushort)valueBytes.Length, 2, ref index); Array.Copy(valueBytes, 0, bytes, index, valueBytes.Length); index += valueBytes.Length; }
public WebAuth() { Id = RandomDataGenerator.GetRandomString(WebConfig.SessionIdLength); byte[] saltBytes = GenerateSalt(); // Convert plain text into a byte array. byte[] plainTextBytes = Encoding.UTF8.GetBytes(Id); // Allocate array, which will hold plain text and salt. byte[] plainTextWithSaltBytes = plainTextBytes.AddBytes(saltBytes); /* * // Compute hash value of our plain text with appended salt. * byte[] hashBytes = ComputeHash(plainTextWithSaltBytes); * // Create array which will hold hash and original salt bytes. * byte[] hashWithSaltBytes = hashBytes.AddBytes(saltBytes); */ Handle = Convert.ToBase64String(plainTextWithSaltBytes); Salt = Convert.ToBase64String(saltBytes); }
public static byte[] AddCrLF(this byte[] bytes) { return(bytes.AddBytes("\r\n")); }
public void AddBytes(object obj, byte[] bytes, ref int index) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } var context = ContextProvider.GetContext(obj.GetType()); var fieldProperties = context.Properties; var startIndex = index; index += 4; // rezervuojam ilgiui; bytes[index++] = this.Version; //bytes.AddBytes<short>(1, 2, ref index); // data converter versija bytes.AddBytes <int>(context.SpaceId, 4, ref index); // space type bytes.AddBytes <short>(context.TypeId, 2, ref index); // data type for (int p = 0; p < fieldProperties.Length; p++) { var currentIndex = index; var property = fieldProperties[p]; if (property.Array != null && false) { var objValue = property.PropertyInfo.GetValue(obj); if (objValue == null) { if (property.Array.MinLength > 0 || property.Include.IsRequired) { throw new Exception($"Property '{property.PropertyInfo.Name}' min length is '{property.Array.MinLength}'"); } continue; } Array value = null; if (objValue is ByteArray) { value = ((ByteArray)objValue).Bytes; } else if (property.PropertyInfo.PropertyType.IsArray) { value = (Array)objValue; } else { throw new Exception($"Property '{property.PropertyInfo.Name}' shoul by array"); } if (value.Length > short.MaxValue || value.Length < ushort.MinValue) { throw new Exception($"Property '{property.PropertyInfo.Name}' length should be between '{ushort.MinValue}' and '{short.MaxValue}' but is '{value.Length}'"); } short length = (short)value.Length; short minLength = property.Array.MinLength; short maxLength = property.Array.MaxLength; bool onlyMinOrMax = property.Array.OnlyMinOrMax; if (length < 0) { throw new Exception($"Property '{property.PropertyInfo.Name}' length should be greater than 0"); } if ((bytes.Length - length) < index) { throw new IndexOutOfRangeException("Byte array length is less than needed"); } if (length < minLength) { throw new Exception($"Property '{property.PropertyInfo.Name}' min length is '{minLength}' but is '{length}'"); } if (length > maxLength) { throw new Exception($"Property '{property.PropertyInfo.Name}' max length is '{maxLength}' but is '{length}'"); } if (onlyMinOrMax) { if (length != minLength && length != maxLength) { throw new Exception($"Property '{property.PropertyInfo.Name}' should be '{minLength}' or '{maxLength}' length but is '{length}'"); } } if (length == 0) { if (property.Include.IsRequired) { throw new Exception($"Property '{property.PropertyInfo.Name}' is required'"); } continue; } bytes[index++] = property.Include.Order; bytes.AddBytes <short>(length, 2, ref index); if (value is byte[] || value is bool[]) { Buffer.BlockCopy(value, 0, bytes, index, value.Length); index += value.Length; continue; } var converter = property.Converter; if (converter == null) { throw new KeyNotFoundException($"Converter for property '{property.PropertyInfo.Name}' not found"); } for (int i = 0; i < value.Length; i++) { converter.AddBytes(value.GetValue(i), bytes, ref index); } continue; } else { var objValue = property.PropertyInfo.GetValue(obj); if (objValue == null) { if (property.Include.IsRequired) { throw new ArgumentException($"Property '{property.PropertyInfo.Name}' is required but is null"); } continue; } var converter = property.Converter; if (converter == null) { throw new KeyNotFoundException($"Converter for property '{property.PropertyInfo.Name}' not found"); } bytes[index++] = property.Include.Order; converter.AddBytes(objValue, bytes, ref index); continue; } if (index <= currentIndex) // at least one byte shoud be added per property { throw new Exception("Something went wrong"); } } var count = index - (startIndex + 4); bytes.AddBytes <int>(count, 4, ref startIndex); }
public void AddBytes(object obj, byte[] bytes, ref int index) { var guid = (Guid)obj; bytes.AddBytes(guid.ToByteArray(), ref index); }
/// <summary> /// 添加换行 /// </summary> public static byte[] AddCrLf(this byte[] bytes) => bytes.AddBytes("\r\n");
public static void InsertLength(this byte[] bytes, int length) { bytes.AddBytes <int>((int)length, 4, 0); }