/// <summary> /// Writes a string atom. /// </summary> /// <param name="s">The string to write.</param> private void Write(NullTerminatedString s) { Write(s as Atom, 0x01); var bytes = Encoding.UTF8.GetBytes(s.Content); writer.Write(bytes, 0, bytes.Length); writer.Write((byte)0x00); }
/// <summary> /// Links a null terminated string. /// </summary> /// <param name="s">The string to link.</param> private void Link(NullTerminatedString s) { addresses.Add(s, address); var bytes = Encoding.UTF8.GetBytes(s.Content); stream.Write(bytes, 0, bytes.Length); stream.WriteByte(0x00); address += s.Size + 1; }
/// <summary> /// Creates a copy of the given string. /// </summary> /// <param name="s">The atom to copy.</param> /// <returns>The copy of the <paramref name="s"/>.</returns> private static Atom Copy(NullTerminatedString s) { var copy = new NullTerminatedString(); copy.IsDefined = s.IsDefined; copy.IsGlobal = s.IsGlobal; copy.Name = s.Name; copy.Content = s.Content; return(copy); }
public void Write_NullTerminatedString_StringWritten() { byte[] bytes = { 0x61, 0x74, 0x6F, 0x6D, // Magic number. 0x01, 0x00, // Version 1. 0x01, // The origin is set. 0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20. 0x00, 0x00, 0x00, 0x00, // Base part of the atom. 0x01, // String type. 0x01, // The string is defined. 0x01, // The string is global. 0x54, 0x78, 0x74, 0x00, // The string is called 'Txt'. // String part of the atom. 0x41, 0x62, 0x63, 0x00 // The string is 'Abc'. }; var s = new NullTerminatedString() { IsDefined = true, IsGlobal = true, Name = "Txt", Content = "Abc" }; using (var stream = new MemoryStream()) { var writer = new AtomWriter(); var file = new ObjectFile(); file.IsOriginSet = true; file.Origin = 0x20; file.Atoms.Add(s); writer.Write(file, stream); CollectionAssert.AreEqual(bytes, stream.ToArray()); } }
/// <summary> /// Reads a null terminated string from the object file. /// </summary> /// <returns>The read string.</returns> /// <exception cref="InvalidObjectFileException"> /// Unexpected end of object file. /// </exception> private NullTerminatedString NullTerminatedString() { var s = new NullTerminatedString(); Atom(s); if (EndOfFile) { throw new InvalidObjectFileException("Unexpected end of object file. Expected null terminated string."); } var bytes = new List <byte>(); while (!EndOfFile) { var b = reader.ReadByte(); switch (b) { case 0: break; default: bytes.Add(b); break; } if (b == 0) { break; } } s.Content = Encoding.UTF8.GetString(bytes.ToArray()); return(s); }
public void Write_ProcedureWithReference_ProcedureRead() { byte[] bytes = { 0x61, 0x74, 0x6F, 0x6D, // Magic number. 0x01, 0x00, // Version 1. 0x01, // The origin is set. 0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20. 0x00, 0x00, 0x00, 0x00, // Base part of the atom. 0x00, // Procedure type. 0x01, // The procedure is defined. 0x01, // The procedure is global. 0x50, 0x72, 0x6F, 0x63, 0x00, // The procedure is called 'Proc'. // Procedure part of the atom. 0x01, // This is the main procedure. 0x04, 0x00, 0x00, 0x00, // The size of the code is 4 bytes. 0x00, 0x00, 0x00, 0x00, // The procedure code. 0x01, 0x00, // Number of references. // Reference. 0x01, 0x00, 0x00, 0x00, // It is atom number 1 that is being referenced (index based). 0x01, // The address is in little endian. 0x04, // The size of the address is 4 bytes. 0x00, 0x00, 0x00, 0x00, // The address to relocate. // Null terminated string. 0x01, // Null terminated string type. 0x01, // The string is defined. 0x00, // The string is not global. 0x53, 0x00, // The string is called 'S'. // String part 0x54, 0x78, 0x74, 0x00 // The string is 'Txt'. }; var s = new NullTerminatedString() { IsDefined = true, IsGlobal = false, Name = "S", Content = "Txt" }; var procedure = new Procedure() { IsDefined = true, IsGlobal = true, Name = "Proc", IsMain = true }; procedure.Code.Add(0x00); procedure.Code.Add(0x00); procedure.Code.Add(0x00); procedure.Code.Add(0x00); procedure.References.Add(new Reference(s) { IsAddressInLittleEndian = true, SizeOfAddress = 4, Address = 0x00 }); using (var stream = new MemoryStream()) { var writer = new AtomWriter(); var file = new ObjectFile(); file.IsOriginSet = true; file.Origin = 0x20; file.Atoms.Add(procedure); file.Atoms.Add(s); writer.Write(file, stream); CollectionAssert.AreEqual(bytes, stream.ToArray()); } }