public void TestLittleEndianOrderBytes() { Guid guid = Guid.NewGuid(); byte[] data = guid.ToByteArray(); byte[] data2 = GuidExtensions.ToLittleEndianBytes(guid); byte[] data3 = EndianOrder.LittleEndian.GetBytes(guid); Guid guid2 = GuidExtensions.ToLittleEndianGuid(data2); Guid guid3 = EndianOrder.LittleEndian.ToGuid(data3, 0); foreach (var b in data) { System.Console.Write("{0:X2} ", b); } System.Console.WriteLine(); foreach (var b in data2) { System.Console.Write("{0:X2} ", b); } System.Console.WriteLine(); foreach (var b in data3) { System.Console.Write("{0:X2} ", b); } System.Console.WriteLine(); System.Console.WriteLine(guid); System.Console.WriteLine(guid2); System.Console.WriteLine(guid3); Assert.AreEqual(guid, guid2); Assert.AreEqual(guid, guid3); Assert.IsTrue(data.SequenceEqual(data2)); Assert.IsTrue(data.SequenceEqual(data3)); }
/// <summary> /// Writes a guid in little endian bytes to the stream /// </summary> /// <param name="stream"></param> /// <param name="value"></param> public static void Write(this Stream stream, Guid value) { Write(stream, GuidExtensions.ToLittleEndianBytes(value)); }
/// <summary> /// This will return a byte array of data that can be written to an archive file. /// </summary> public byte[] GetBytes() { if (!IsFileAllocationTableValid()) { throw new InvalidOperationException("File Allocation Table is invalid"); } byte[] dataBytes = new byte[m_blockSize]; MemoryStream stream = new MemoryStream(dataBytes); BinaryWriter dataWriter = new BinaryWriter(stream); dataWriter.Write(FileAllocationTableHeaderBytes); if (BitConverter.IsLittleEndian) { dataWriter.Write('L'); } else { dataWriter.Write('B'); } dataWriter.Write((byte)(BitMath.CountBitsSet((uint)(m_blockSize - 1)))); dataWriter.Write(FileAllocationReadTableVersion); dataWriter.Write(FileAllocationWriteTableVersion); dataWriter.Write(FileAllocationHeaderVersion); dataWriter.Write((byte)(m_isSimplifiedFileFormat ? 2 : 1)); dataWriter.Write(m_headerBlockCount); dataWriter.Write(m_lastAllocatedBlock); dataWriter.Write(m_snapshotSequenceNumber); dataWriter.Write(m_nextFileId); dataWriter.Write(m_archiveId.ToByteArray()); dataWriter.Write(m_archiveType.ToByteArray()); dataWriter.Write((short)m_files.Count); foreach (SubFileHeader node in m_files) { node.Save(dataWriter); } //Metadata Flags if (m_flags.Count > 0) { Encoding7Bit.WriteInt15(dataWriter.Write, (short)FileHeaderAttributes.FileFlags); Encoding7Bit.WriteInt15(dataWriter.Write, (short)(m_flags.Count * 16)); foreach (var flag in m_flags) { dataWriter.Write(GuidExtensions.ToLittleEndianBytes(flag)); } } if (m_unknownAttributes != null) { foreach (var md in m_unknownAttributes) { Encoding7Bit.WriteInt15(dataWriter.Write, md.Key); Encoding7Bit.WriteInt15(dataWriter.Write, (short)md.Value.Length); dataWriter.Write(md.Value); } } if (m_userAttributes != null) { foreach (var md in m_userAttributes) { Encoding7Bit.WriteInt15(dataWriter.Write, (short)FileHeaderAttributes.UserAttributes); dataWriter.Write(GuidExtensions.ToLittleEndianBytes(md.Key)); Encoding7Bit.WriteInt15(dataWriter.Write, (short)md.Value.Length); dataWriter.Write(md.Value); } } Encoding7Bit.WriteInt15(dataWriter.Write, (short)FileHeaderAttributes.EndOfAttributes); Encoding7Bit.WriteInt15(dataWriter.Write, 0); if (stream.Position + 32 > dataBytes.Length) { throw new Exception("the file size exceedes the allowable size."); } WriteFooterData(dataBytes); return(dataBytes); }