public static void CopyBytesGeneric <T>(this EndianBitConverter converter, T t, byte[] buffer, int index) { switch (Type.GetTypeCode(typeof(T))) { case TypeCode.Byte: buffer[index] = (byte)Convert.ChangeType(t, typeof(byte)); break; case TypeCode.Int16: converter.CopyBytes((Int16)Convert.ChangeType(t, typeof(Int16)), buffer, index); break; case TypeCode.Int32: converter.CopyBytes((Int32)Convert.ChangeType(t, typeof(Int32)), buffer, index); break; case TypeCode.UInt16: converter.CopyBytes((UInt16)Convert.ChangeType(t, typeof(UInt16)), buffer, index); break; case TypeCode.UInt32: converter.CopyBytes((UInt32)Convert.ChangeType(t, typeof(UInt32)), buffer, index); break; case TypeCode.Object: if (t is Structures.Address) { Structures.Address address = (Structures.Address)Convert.ChangeType(t, typeof(Structures.Address)); byte[] addressBytes = converter.GetBytes(address.value); buffer[index + 1] = (byte)((buffer[index + 1] & 0xf0) | (addressBytes[1] & 0x0f)); buffer[index + 2] = addressBytes[2]; buffer[index + 3] = addressBytes[3]; break; } if (t is Structures.BranchInstruction) { Structures.BranchInstruction branchInstruction = (Structures.BranchInstruction)Convert.ChangeType(t, typeof(Structures.BranchInstruction)); byte[] instructionBytes = converter.GetBytes(branchInstruction.value); buffer[index + 0] = Constants.BRANCH_MNEMONIC; buffer[index + 1] = instructionBytes[1]; buffer[index + 2] = instructionBytes[2]; buffer[index + 3] = instructionBytes[3]; break; } throw new ApplicationException("CopyBytesGeneric<T>() unsupported type"); default: throw new ApplicationException("CopyBytesGeneric<T>() unsupported type"); } }
public override void Write(IElementWriteTarget target) { if (_absoluteOffset == null) { throw new InvalidOperationException("Delayed resolution has not completed for this element."); } target.Write(_bitConverter.GetBytes(_absoluteOffset.Value), this); }
private static void AppendULongToBuffer(ulong data, ref byte[] buffer, ref int index) { Buffer.BlockCopy(EndianBitConverter.GetBytes(data), 0, buffer, index, 8); index += 8; }
private static void AppendIntToBuffer(int data, ref byte[] buffer, ref int index) { Buffer.BlockCopy(EndianBitConverter.GetBytes(data), 0, buffer, index, 4); index += 4; }
public void GetBytesUShort() { ushort value = 0x0102; byte[] leBytes = EndianBitConverter.GetBytes(value, false); byte[] beBytes = EndianBitConverter.GetBytes(value, true); Assert.That(leBytes, Is.EqualTo(new byte[] { 0x02, 0x01 })); Assert.That(beBytes, Is.EqualTo(new byte[] { 0x01, 0x02 })); }
public void GetBytesUInt() { uint value = 0x01020304; byte[] leBytes = EndianBitConverter.GetBytes(value, false); byte[] beBytes = EndianBitConverter.GetBytes(value, true); Assert.That(leBytes, Is.EqualTo(new byte[] { 0x04, 0x03, 0x02, 0x01 })); Assert.That(beBytes, Is.EqualTo(new byte[] { 0x01, 0x02, 0x03, 0x04 })); }
private BigInteger Pack(UInt32[] hilbertCode) { Byte[] bytes = new Byte[32 * hilbertCode.Length]; for (Int32 i = 0; i < hilbertCode.Length; i++) { EndianBitConverter.GetBytes(hilbertCode[i], ByteOrder.LittleEndian) .CopyTo(bytes, i * 32); } // the resulting "bytes" array is in Little Endian too, as defined by BigInteger constructor return(new BigInteger(bytes)); }
public static OffsetElement Add( this ElementList elementList, EndianBitConverter bitConverter, ushort value, string comment, IDelayedResolutionElement referencedBy = null) { byte[] bytes = bitConverter.GetBytes(value); SimpleElement element = new SimpleElement(bytes, comment, referencedBy); elementList.Add(element); return(element); }
public void SendMessage(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (Encryptor != null) { buffer = Encryptor.Encrypt(buffer); } byte[] dataSizeBytes = EndianBitConverter.GetBytes(buffer.Length); byte[] realBuffer = new byte[buffer.Length + dataSizeBytes.Length]; Array.Copy(dataSizeBytes, 0, realBuffer, 0, dataSizeBytes.Length); Array.Copy(buffer, 0, realBuffer, dataSizeBytes.Length, buffer.Length); Send(realBuffer); }
private void OnConnected(ITransport transport) { try { LoggingService.LogInfo("Transport {0} connected.", transport); if (transport.Encryptor != null) { var dh = new DiffieHellmanManaged(); var keyxBytes = dh.CreateKeyExchange(); transport.Send(dh.CreateKeyExchange(), 0, keyxBytes.Length); keyxBytes = new byte [transport.Encryptor.KeyExchangeLength]; transport.Receive(keyxBytes, 0, transport.Encryptor.KeyExchangeLength); keyxBytes = dh.DecryptKeyExchange(keyxBytes); var keyBytes = new byte[transport.Encryptor.KeySize]; var ivBytes = new byte[transport.Encryptor.IvSize]; Array.Copy(keyxBytes, 0, keyBytes, 0, keyBytes.Length); Array.Copy(keyxBytes, keyBytes.Length, ivBytes, 0, ivBytes.Length); transport.Encryptor.SetKey(keyBytes, ivBytes); } var connectionType = EndianBitConverter.GetBytes(transport.ConnectionType); transport.Send(connectionType, 0, connectionType.Length); var networkId = Common.Utils.SHA512(transport.Network.NetworkName); transport.Send(networkId, 0, networkId.Length); // Ready, Steady, GO! var callback = connectCallbacks [transport]; connectCallbacks.Remove(transport); callback(transport); } catch (Exception ex) { transport.Disconnect(ex); RaiseTransportError(transport, ex); } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> /// <param name="disposing">A value indicating whether disposing is performed on the object.</param> protected override void Dispose(Boolean disposing) { if (_baseStream != null && _imageCount < _maxImageCount) { // write the 0 address of next image file directory _baseStream.Seek(_currentImageFileDirectoryEndPosition, SeekOrigin.Begin); switch (_structure) { case TiffStructure.RegularTiff: _baseStream.Write(EndianBitConverter.GetBytes((UInt32)0), 0, 4); break; case TiffStructure.BigTiff: _baseStream.Write(EndianBitConverter.GetBytes((UInt64)0), 0, 8); break; } } base.Dispose(disposing); }
// Return bytes containing the length of a message packed as Big Indian // appended by a XOR Autokey Cipher with starting key = 171 of the message // which is how the TP-Link devices communicate over a network private static byte[] Encrypt(string source) { int key = 171; //consider using https://stackoverflow.com/questions/51611550/converting-big-endian-to-little-endian-in-c-sharp#51611667 to avoid unnecessary references EndianBitConverter converter = EndianBitConverter.BigEndian; byte[] biglen = converter.GetBytes(source.Length); List <byte> bytes = new List <byte>(); foreach (byte b in biglen) { bytes.Add(b); } for (int c = 0; c < source.Length; c++) { char a = (char)(source[c] ^ key); key = a; bytes.Add(Convert.ToByte(a)); } return(bytes.ToArray()); }
public void WriteShort(short value) { WriteBytes(bitConverter.GetBytes(value)); }
/// <summary> /// Converts the shape into a binary record. /// </summary> /// <param name="shape">The shape.</param> /// <param name="number">The number of the shape used for indexing.</param> /// <returns>The byte array representing the <paramref name="shape" />.</returns> public Byte[] ToRecord(Int32 number) { Byte[] shapeBytes; switch (Type) // compute the size of the array according to the type { case ShapeType.Point: case ShapeType.PointM: shapeBytes = new Byte[28]; break; case ShapeType.PointZ: shapeBytes = new Byte[44]; break; case ShapeType.MultiPoint: case ShapeType.MultiPointM: shapeBytes = new Byte[48 + CoordinateCount * 16]; break; case ShapeType.MultiPointZ: shapeBytes = new Byte[80 + CoordinateCount * 32]; break; case ShapeType.PolyLine: case ShapeType.PolyLineM: case ShapeType.Polygon: case ShapeType.PolygonM: shapeBytes = new Byte[52 + PartCount * 4 + CoordinateCount * 16]; break; case ShapeType.PolyLineZ: case ShapeType.PolygonZ: shapeBytes = new Byte[84 + PartCount * 4 + CoordinateCount * 32]; break; case ShapeType.MultiPatch: shapeBytes = new Byte[84 + PartCount * 8 + CoordinateCount * 32]; break; default: shapeBytes = new Byte[12]; break; } // compute header information of the record EndianBitConverter.CopyBytes(number, shapeBytes, 0, ByteOrder.BigEndian); EndianBitConverter.CopyBytes(shapeBytes.Length / 2 - 4, shapeBytes, 4, ByteOrder.BigEndian); EndianBitConverter.CopyBytes((Int32)_type, shapeBytes, 8, ByteOrder.LittleEndian); // compute envelope coordinates and shape coordinates switch (Type) { case ShapeType.Point: case ShapeType.PointM: EndianBitConverter.GetBytes(Coordinates[0], 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 12); break; case ShapeType.PointZ: EndianBitConverter.GetBytes(Coordinates[0], 3, ByteOrder.LittleEndian).CopyTo(shapeBytes, 12); break; case ShapeType.MultiPoint: case ShapeType.MultiPointM: EndianBitConverter.GetBytes(Envelope.Minimum, 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 12); EndianBitConverter.GetBytes(Envelope.Maximum, 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 28); EndianBitConverter.GetBytes(CoordinateCount, ByteOrder.LittleEndian).CopyTo(shapeBytes, 44); for (Int32 i = 0; i < CoordinateCount; i++) { EndianBitConverter.GetBytes(Coordinates[i], 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 48 + i * 16); } break; case ShapeType.MultiPointZ: EndianBitConverter.GetBytes(Envelope.Minimum, 3, ByteOrder.LittleEndian).CopyTo(shapeBytes, 12); EndianBitConverter.GetBytes(Envelope.Maximum, 3, ByteOrder.LittleEndian).CopyTo(shapeBytes, 28); EndianBitConverter.GetBytes(CoordinateCount, ByteOrder.LittleEndian).CopyTo(shapeBytes, 44); for (Int32 i = 0; i < CoordinateCount; i++) { EndianBitConverter.GetBytes(Coordinates[i], 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 48 + i * 16); } EndianBitConverter.GetBytes(Envelope.Minimum.Z, ByteOrder.LittleEndian).CopyTo(shapeBytes, 48 + CoordinateCount * 16); EndianBitConverter.GetBytes(Envelope.Maximum.Z, ByteOrder.LittleEndian).CopyTo(shapeBytes, 56 + CoordinateCount * 16); for (Int32 i = 0; i < CoordinateCount; i++) { EndianBitConverter.GetBytes(Coordinates[i].Z, ByteOrder.LittleEndian).CopyTo(shapeBytes, 64 + CoordinateCount * 16 + i * 8); } break; case ShapeType.PolyLine: case ShapeType.PolyLineM: case ShapeType.Polygon: case ShapeType.PolygonM: EndianBitConverter.GetBytes(Envelope.Minimum, 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 12); EndianBitConverter.GetBytes(Envelope.Maximum, 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 28); EndianBitConverter.GetBytes(PartCount, ByteOrder.LittleEndian).CopyTo(shapeBytes, 44); EndianBitConverter.GetBytes(CoordinateCount, ByteOrder.LittleEndian).CopyTo(shapeBytes, 48); for (Int32 i = 0; i < PartCount; i++) { EndianBitConverter.GetBytes(_parts[i], ByteOrder.LittleEndian).CopyTo(shapeBytes, 52 + i * 4); } for (Int32 i = 0; i < CoordinateCount; i++) { EndianBitConverter.GetBytes(Coordinates[i], 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 52 + PartCount * 4 + i * 16); } break; case ShapeType.PolyLineZ: case ShapeType.PolygonZ: EndianBitConverter.GetBytes(Envelope.Minimum, 3, ByteOrder.LittleEndian).CopyTo(shapeBytes, 12); EndianBitConverter.GetBytes(Envelope.Maximum, 3, ByteOrder.LittleEndian).CopyTo(shapeBytes, 28); EndianBitConverter.GetBytes(PartCount, ByteOrder.LittleEndian).CopyTo(shapeBytes, 44); EndianBitConverter.GetBytes(CoordinateCount, ByteOrder.LittleEndian).CopyTo(shapeBytes, 48); for (Int32 i = 0; i < PartCount; i++) { EndianBitConverter.GetBytes(_parts[i], ByteOrder.LittleEndian).CopyTo(shapeBytes, 52 + i * 4); } for (Int32 i = 0; i < CoordinateCount; i++) { EndianBitConverter.GetBytes(Coordinates[i], 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 52 + PartCount * 4 + i * 16); } EndianBitConverter.GetBytes(Envelope.Minimum.Z, ByteOrder.LittleEndian).CopyTo(shapeBytes, 52 + PartCount * 4 + CoordinateCount * 16); EndianBitConverter.GetBytes(Envelope.Maximum.Z, ByteOrder.LittleEndian).CopyTo(shapeBytes, 60 + PartCount * 4 + CoordinateCount * 16); for (Int32 i = 0; i < CoordinateCount; i++) { EndianBitConverter.GetBytes(Coordinates[i].Z, ByteOrder.LittleEndian).CopyTo(shapeBytes, 68 + PartCount * 4 + CoordinateCount * 16 + i * 8); } break; case ShapeType.MultiPatch: EndianBitConverter.GetBytes(Envelope.Minimum, 3, ByteOrder.LittleEndian).CopyTo(shapeBytes, 12); EndianBitConverter.GetBytes(Envelope.Maximum, 3, ByteOrder.LittleEndian).CopyTo(shapeBytes, 28); EndianBitConverter.GetBytes(PartCount, ByteOrder.LittleEndian).CopyTo(shapeBytes, 44); EndianBitConverter.GetBytes(CoordinateCount, ByteOrder.LittleEndian).CopyTo(shapeBytes, 48); for (Int32 i = 0; i < PartCount; i++) { EndianBitConverter.GetBytes(_parts[i], ByteOrder.LittleEndian).CopyTo(shapeBytes, 52 + i * 4); } for (Int32 i = 0; i < PartCount; i++) { EndianBitConverter.GetBytes((Int32)PartTypes[i], ByteOrder.LittleEndian).CopyTo(shapeBytes, 52 + PartCount * 4 + i * 4); } for (Int32 i = 0; i < CoordinateCount; i++) { EndianBitConverter.GetBytes(Coordinates[i], 2, ByteOrder.LittleEndian).CopyTo(shapeBytes, 52 + PartCount * 8 + i * 16); } EndianBitConverter.GetBytes(Envelope.Minimum.Z, ByteOrder.LittleEndian).CopyTo(shapeBytes, 52 + PartCount * 8 + CoordinateCount * 16); EndianBitConverter.GetBytes(Envelope.Maximum.Z, ByteOrder.LittleEndian).CopyTo(shapeBytes, 60 + PartCount * 8 + CoordinateCount * 16); for (Int32 i = 0; i < CoordinateCount; i++) { EndianBitConverter.GetBytes(Coordinates[i].Z, ByteOrder.LittleEndian).CopyTo(shapeBytes, 68 + PartCount * 8 + CoordinateCount * 16 + i * 8); } break; } return(shapeBytes); }
/// <summary> /// Writes an image file directory to the stream. /// </summary> /// <param name="imageFileDirectory">The image file directory.</param> private void WriteImageFileDirectory(TiffImageFileDirectory imageFileDirectory) { _currentImageFileDirectoryStartPosition = _baseStream.Position; // write starting position _baseStream.Seek(_currentImageFileDirectoryEndPosition, SeekOrigin.Begin); _baseStream.Write(EndianBitConverter.GetBytes((UInt32)_currentImageFileDirectoryStartPosition), 0, _imageFileDirectoryFieldSize); // compute size of directory (without additional fields) Int64 imageFileDirectorySize = 2 + _imageFileDirectoryEntrySize * imageFileDirectory.Count; _currentImageFileDirectoryEndPosition = _currentImageFileDirectoryStartPosition + imageFileDirectorySize; // position after the IFD to write exceeding values _baseStream.Seek(_currentImageFileDirectoryEndPosition + _imageFileDirectoryFieldSize, SeekOrigin.Begin); // the IFD should be written in one operation Byte[] bytes; if (_imageCount == _maxImageCount - 1) { bytes = new Byte[imageFileDirectorySize + _imageFileDirectoryFieldSize]; } else { bytes = new Byte[imageFileDirectorySize]; } EndianBitConverter.CopyBytes((UInt16)imageFileDirectory.Count, bytes, 0); // number of entries Int32 byteIndex = 2; TiffTagType entryType; UInt16 dataSize; Int64 dataCount; foreach (UInt16 entryTag in imageFileDirectory.Keys) { entryType = TiffTag.GetType(imageFileDirectory[entryTag][0]); dataSize = TiffTag.GetSize(entryType); if (entryType == TiffTagType.ASCII) { dataCount = 0; for (Int32 i = 0; i < imageFileDirectory[entryTag].Length; i++) { dataCount += (imageFileDirectory[entryTag][i] as String).Length; } } else { dataCount = imageFileDirectory[entryTag].Length; } EndianBitConverter.CopyBytes(entryTag, bytes, byteIndex); EndianBitConverter.CopyBytes((UInt16)entryType, bytes, byteIndex + 2); EndianBitConverter.CopyBytes((UInt32)dataCount, bytes, byteIndex + 4); // values exceeding he field size (4) must be written to another position Byte[] dataBytes; Int32 dataStartIndex; if (dataCount * dataSize <= 4) { dataBytes = bytes; dataStartIndex = byteIndex + 8; } else { dataBytes = new Byte[dataCount * dataSize + (dataCount * dataSize) % 2]; dataStartIndex = 0; } for (Int32 valueIndex = 0; valueIndex < imageFileDirectory[entryTag].Length; valueIndex++) { dataStartIndex = TiffTag.SetValue(entryType, imageFileDirectory[entryTag][valueIndex], dataBytes, dataStartIndex); } if (dataCount * dataSize > 4) { Int64 valuePosition = _baseStream.Position; _baseStream.Write(dataBytes, 0, dataBytes.Length); EndianBitConverter.CopyBytes((UInt32)valuePosition, bytes, byteIndex + 8); } byteIndex += _imageFileDirectoryEntrySize; } _currentImageStartPosition = _baseStream.Position; // write the IFD _baseStream.Seek(_currentImageFileDirectoryStartPosition, SeekOrigin.Begin); _baseStream.Write(bytes, 0, bytes.Length); }
//valueIndex = the index (in bits) of the value at which to start copying //index = the index (in bits) in this BitField at which to put the value //length = the number of bits to copy from the value public void Set(ushort value, int valueIndex, int index, int length) { SetBytes(_bitConverter.GetBytes(value), valueIndex, index, length); }
/// <summary> /// Writes the record count. /// </summary> /// <param name="stream">The stream.</param> /// <param name="recordCount">The number of the records.</param> public void WriteRecordCount(Stream stream, Int32 recordCount) { stream.Seek(4, SeekOrigin.Begin); stream.Write(EndianBitConverter.GetBytes(recordCount, ByteOrder.LittleEndian), 0, 4); stream.Seek(0, SeekOrigin.End); }