/// <summary> /// Reads a string /// </summary> /// <param name="reader">Reader</param> /// <param name="offset">Offset of string</param> /// <returns>The string or <c>null</c> if we could not read it</returns> private static string ReadString(IBinaryReader reader, uint offset) { reader.Position = offset; if (!reader.CanRead(2)) { return(null); } int size = reader.ReadUInt16(); int sizeInBytes = size * 2; if (!reader.CanRead(sizeInBytes)) { return(null); } var stringData = reader.ReadBytes(sizeInBytes); try { return(Encoding.Unicode.GetString(stringData)); } catch { return(null); } }
/// <summary> /// Reads the directory header and initializes <see cref="ResourceDirectory.directories"/> and /// <see cref="ResourceDirectory.data"/>. /// </summary> /// <param name="reader"></param> void Initialize(IBinaryReader reader) { if (depth > MAX_DIR_DEPTH || !reader.CanRead(16)) { InitializeDefault(); return; } characteristics = reader.ReadUInt32(); timeDateStamp = reader.ReadUInt32(); majorVersion = reader.ReadUInt16(); minorVersion = reader.ReadUInt16(); ushort numNamed = reader.ReadUInt16(); ushort numIds = reader.ReadUInt16(); int total = numNamed + numIds; if (!reader.CanRead(total * 8)) { InitializeDefault(); return; } dataInfos = new List <EntryInfo>(); dirInfos = new List <EntryInfo>(); long offset = reader.Position; for (int i = 0; i < total; i++, offset += 8) { reader.Position = offset; uint nameOrId = reader.ReadUInt32(); uint dataOrDirectory = reader.ReadUInt32(); ResourceName name; if ((nameOrId & 0x80000000) != 0) { name = new ResourceName(ReadString(reader, nameOrId & 0x7FFFFFFF) ?? string.Empty); } else { name = new ResourceName((int)nameOrId); } if ((dataOrDirectory & 0x80000000) == 0) { dataInfos.Add(new EntryInfo(name, dataOrDirectory)); } else { dirInfos.Add(new EntryInfo(name, dataOrDirectory & 0x7FFFFFFF)); } } directories = new LazyList <ResourceDirectory>(dirInfos.Count, null, (ctx, i) => ReadResourceDirectory((int)i)); data = new LazyList <ResourceData>(dataInfos.Count, null, (ctx, i) => ReadResourceData((int)i)); }
protected virtual IEnumerable <IInstruction> GetInstructions(IBinaryReader binaryReader, Context.IOperandReaderContext context) { int index = 0; while (binaryReader.CanRead()) { yield return(new Instruction(index++, binaryReader, context)); } }
/// <summary> /// Returns true if it's possibly resources file data /// </summary> /// <param name="reader">Reader</param> /// <returns></returns> public static bool CouldBeResourcesFile(IBinaryReader reader) { return(reader.CanRead(4) && reader.ReadUInt32() == 0xBEEFCACE); }
/// <summary> /// Returns true if it's possibly resources file data /// </summary> /// <param name="reader">Reader</param> /// <returns></returns> public static bool CouldBeResourcesFile(IBinaryReader reader) { return reader.CanRead(4) && reader.ReadUInt32() == 0xBEEFCACE; }
/// <summary> /// Reads a string /// </summary> /// <param name="reader">Reader</param> /// <param name="offset">Offset of string</param> /// <returns>The string or <c>null</c> if we could not read it</returns> static string ReadString(IBinaryReader reader, uint offset) { reader.Position = offset; if (!reader.CanRead(2)) return null; int size = reader.ReadUInt16(); int sizeInBytes = size * 2; if (!reader.CanRead(sizeInBytes)) return null; var stringData = reader.ReadBytes(sizeInBytes); try { return Encoding.Unicode.GetString(stringData); } catch { return null; } }
/// <summary> /// Reads the directory header and initializes <see cref="ResourceDirectory.directories"/> and /// <see cref="ResourceDirectory.data"/>. /// </summary> /// <param name="reader"></param> void Initialize(IBinaryReader reader) { if (depth > MAX_DIR_DEPTH || !reader.CanRead(16)) { InitializeDefault(); return; } characteristics = reader.ReadUInt32(); timeDateStamp = reader.ReadUInt32(); majorVersion = reader.ReadUInt16(); minorVersion = reader.ReadUInt16(); ushort numNamed = reader.ReadUInt16(); ushort numIds = reader.ReadUInt16(); int total = numNamed + numIds; if (!reader.CanRead(total * 8)) { InitializeDefault(); return; } dataInfos = new List<EntryInfo>(); dirInfos = new List<EntryInfo>(); long offset = reader.Position; for (int i = 0; i < total; i++, offset += 8) { reader.Position = offset; uint nameOrId = reader.ReadUInt32(); uint dataOrDirectory = reader.ReadUInt32(); ResourceName name; if ((nameOrId & 0x80000000) != 0) name = new ResourceName(ReadString(reader, nameOrId & 0x7FFFFFFF) ?? string.Empty); else name = new ResourceName((int)nameOrId); if ((dataOrDirectory & 0x80000000) == 0) dataInfos.Add(new EntryInfo(name, dataOrDirectory)); else dirInfos.Add(new EntryInfo(name, dataOrDirectory & 0x7FFFFFFF)); } directories = new LazyList<ResourceDirectory>(dirInfos.Count, null, (ctx, i) => ReadResourceDirectory((int)i)); data = new LazyList<ResourceData>(dataInfos.Count, null, (ctx, i) => ReadResourceData((int)i)); }
protected bool CanRead() { return(binaryReader.CanRead()); }