internal NoteData(ulong sectionOffset, ulong sectionSize, SimpleEndianessAwareReader reader) { this.reader = reader; var sectionEnd = (long)(sectionOffset + sectionSize); reader.BaseStream.Seek((long)sectionOffset, SeekOrigin.Begin); var nameSize = ReadSize(); var descriptionSize = ReadSize(); Type = ReadField(); int remainder; var fields = Math.DivRem(nameSize, FieldSize, out remainder); var alignedNameSize = FieldSize * (remainder > 0 ? fields + 1 : fields); // We encountered binaries where nameSize and descriptionSize are // invalid (i.e. significantly larger than the size of the binary itself). // To avoid throwing on such binaries, we only read in name and description // if the sizes are within range of the containing section. if (reader.BaseStream.Position + alignedNameSize <= sectionEnd) { var name = reader.ReadBytes(alignedNameSize); if (nameSize > 0) { Name = Encoding.UTF8.GetString(name, 0, nameSize - 1); // minus one to omit terminating NUL } if (reader.BaseStream.Position + descriptionSize <= sectionEnd) { Description = descriptionSize > 0 ? reader.ReadBytes(descriptionSize) : new byte[0]; } } }
private void ReadCommands(int noOfCommands, Stream stream, SimpleEndianessAwareReader reader) { for (var i = 0; i < noOfCommands; i++) { var loadCommandType = reader.ReadUInt32(); var commandSize = reader.ReadUInt32(); switch ((CommandType)loadCommandType) { case CommandType.SymbolTable: commands[i] = new SymbolTable(reader, stream, is64); break; case CommandType.Main: commands[i] = new EntryPoint(reader, stream); break; case CommandType.Segment: case CommandType.Segment64: commands[i] = new Segment(reader, stream, is64); break; default: reader.ReadBytes((int)commandSize - 8); // 8 bytes is the size of the common command header break; } } }
internal MachO(Stream stream, bool is64, Endianess endianess, bool ownsStream) { this.is64 = is64; using var reader = new SimpleEndianessAwareReader(stream, endianess, ownsStream); Machine = (Machine)reader.ReadInt32(); reader.ReadBytes(4); // we don't support the cpu subtype now FileType = (FileType)reader.ReadUInt32(); var noOfCommands = reader.ReadInt32(); reader.ReadInt32(); // size of commands reader.ReadBytes(4); // we don't support flags now if (is64) { reader.ReadBytes(4); // reserved } commands = new Command[noOfCommands]; ReadCommands(noOfCommands, stream, reader); }
internal Dylib(SimpleEndianessAwareReader reader, Stream stream, uint commandSize) : base(reader, stream) { var offset = reader.ReadUInt32(); var timestamp = reader.ReadInt32(); var currentVersion = reader.ReadUInt32(); var compatibilityVersion = reader.ReadUInt32(); Timestamp = DateTimeOffset.FromUnixTimeSeconds(timestamp).UtcDateTime; CurrentVersion = GetVersion(currentVersion); CompatibilityVersion = GetVersion(compatibilityVersion); Name = GetString(reader.ReadBytes((int)(commandSize - offset))); }