Add unused PE font group parser

This commit is contained in:
Matt Nadareski
2022-11-09 20:39:20 -08:00
parent 78d80918aa
commit 98c340d94d
4 changed files with 285 additions and 0 deletions

View File

@@ -441,6 +441,75 @@ namespace BurnOutSharp.Builder
}
}
/// <summary>
/// Read resource data as a font group
/// </summary>
/// <param name="data">Resource data entry to parse into a font group</param>
/// <returns>A filled font group on success, null on error</returns>
public static Models.PortableExecutable.FontGroupHeader AsFontGroup(this Models.PortableExecutable.ResourceDataEntry entry)
{
// If we have an invalid entry, just skip
if (entry?.Data == null)
return null;
// Initialize the iterator
int offset = 0;
// Create the output object
var fontGroupHeader = new Models.PortableExecutable.FontGroupHeader();
fontGroupHeader.NumberOfFonts = entry.Data.ReadUInt16(ref offset);
if (fontGroupHeader.NumberOfFonts > 0)
{
fontGroupHeader.DE = new Models.PortableExecutable.DirEntry[fontGroupHeader.NumberOfFonts];
for (int i = 0; i < fontGroupHeader.NumberOfFonts; i++)
{
var dirEntry = new Models.PortableExecutable.DirEntry();
dirEntry.FontOrdinal = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry = new Models.PortableExecutable.FontDirEntry();
dirEntry.Entry.Version = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.Size = entry.Data.ReadUInt32(ref offset);
dirEntry.Entry.Copyright = entry.Data.ReadBytes(ref offset, 60);
dirEntry.Entry.Type = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.Points = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.VertRes = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.HorizRes = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.Ascent = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.InternalLeading = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.ExternalLeading = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.Italic = entry.Data.ReadByte(ref offset);
dirEntry.Entry.Underline = entry.Data.ReadByte(ref offset);
dirEntry.Entry.StrikeOut = entry.Data.ReadByte(ref offset);
dirEntry.Entry.Weight = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.CharSet = entry.Data.ReadByte(ref offset);
dirEntry.Entry.PixWidth = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.PixHeight = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.PitchAndFamily = entry.Data.ReadByte(ref offset);
dirEntry.Entry.AvgWidth = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.MaxWidth = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.FirstChar = entry.Data.ReadByte(ref offset);
dirEntry.Entry.LastChar = entry.Data.ReadByte(ref offset);
dirEntry.Entry.DefaultChar = entry.Data.ReadByte(ref offset);
dirEntry.Entry.BreakChar = entry.Data.ReadByte(ref offset);
dirEntry.Entry.WidthBytes = entry.Data.ReadUInt16(ref offset);
dirEntry.Entry.Device = entry.Data.ReadUInt32(ref offset);
dirEntry.Entry.Face = entry.Data.ReadUInt32(ref offset);
dirEntry.Entry.Reserved = entry.Data.ReadUInt32(ref offset);
// TODO: Determine how to read these two? Immediately after?
dirEntry.Entry.DeviceName = entry.Data.ReadString(ref offset);
dirEntry.Entry.FaceName = entry.Data.ReadString(ref offset);
fontGroupHeader.DE[i] = dirEntry;
}
}
// TODO: Implement entry parsing
return null;
}
/// <summary>
/// Read resource data as a string table resource
/// </summary>

View File

@@ -0,0 +1,21 @@
namespace BurnOutSharp.Models.PortableExecutable
{
/// <summary>
/// Contains the information necessary for an application to access a specific font. The structure
/// definition provided here is for explanation only; it is not present in any standard header file.
/// </summary>
/// <see href="https://learn.microsoft.com/en-us/windows/win32/menurc/direntry"/>
public class DirEntry
{
/// <summary>
/// A unique ordinal identifier for an individual font in a font resource group.
/// </summary>
public ushort FontOrdinal;
/// <summary>
/// The FONTDIRENTRY structure for the specified font directly follows the DIRENTRY structure
/// for that font.
/// </summary>
public FontDirEntry Entry;
}
}

View File

@@ -0,0 +1,174 @@
namespace BurnOutSharp.Models.PortableExecutable
{
/// <summary>
/// Contains information about an individual font in a font resource group. The structure definition
/// provided here is for explanation only; it is not present in any standard header file.
/// </summary>
/// <see href="https://learn.microsoft.com/en-us/windows/win32/menurc/fontdirentry"/>
public class FontDirEntry
{
/// <summary>
/// A user-defined version number for the resource data that tools can use to read and write
/// resource files.
/// </summary>
public ushort Version;
/// <summary>
/// The size of the file, in bytes.
/// </summary>
public uint Size;
/// <summary>
/// The font supplier's copyright information.
/// </summary>
/// <remarks>60 characters</remarks>
public byte[] Copyright;
/// <summary>
/// The type of font file.
/// </summary>
public ushort Type;
/// <summary>
/// The point size at which this character set looks best.
/// </summary>
public ushort Points;
/// <summary>
/// The vertical resolution, in dots per inch, at which this character set was digitized.
/// </summary>
public ushort VertRes;
/// <summary>
/// The horizontal resolution, in dots per inch, at which this character set was digitized.
/// </summary>
public ushort HorizRes;
/// <summary>
/// The distance from the top of a character definition cell to the baseline of the typographical
/// font.
/// </summary>
public ushort Ascent;
/// <summary>
/// The amount of leading inside the bounds set by the PixHeight member. Accent marks and other
/// diacritical characters can occur in this area.
/// </summary>
public ushort InternalLeading;
/// <summary>
/// The amount of extra leading that the application adds between rows.
/// </summary>
public ushort ExternalLeading;
/// <summary>
/// An italic font if not equal to zero.
/// </summary>
public byte Italic;
/// <summary>
/// An underlined font if not equal to zero.
/// </summary>
public byte Underline;
/// <summary>
/// A strikeout font if not equal to zero.
/// </summary>
public byte StrikeOut;
/// <summary>
/// The weight of the font in the range 0 through 1000. For example, 400 is roman and 700 is bold.
/// If this value is zero, a default weight is used. For additional defined values, see the
/// description of the LOGFONT structure.
/// </summary>
public ushort Weight;
/// <summary>
/// The character set of the font. For predefined values, see the description of the LOGFONT
/// structure.
/// </summary>
public byte CharSet;
/// <summary>
/// The width of the grid on which a vector font was digitized. For raster fonts, if the member
/// is not equal to zero, it represents the width for all the characters in the bitmap. If the
/// member is equal to zero, the font has variable-width characters.
/// </summary>
public ushort PixWidth;
/// <summary>
/// The height of the character bitmap for raster fonts or the height of the grid on which a
/// vector font was digitized.
/// </summary>
public ushort PixHeight;
/// <summary>
/// The pitch and the family of the font. For additional information, see the description of
/// the LOGFONT structure.
/// </summary>
public byte PitchAndFamily;
/// <summary>
/// The average width of characters in the font (generally defined as the width of the letter x).
/// This value does not include the overhang required for bold or italic characters.
/// </summary>
public ushort AvgWidth;
/// <summary>
/// The width of the widest character in the font.
/// </summary>
public ushort MaxWidth;
/// <summary>
/// The first character code defined in the font.
/// </summary>
public byte FirstChar;
/// <summary>
/// The last character code defined in the font.
/// </summary>
public byte LastChar;
/// <summary>
/// The character to substitute for characters not in the font.
/// </summary>
public byte DefaultChar;
/// <summary>
/// The character that will be used to define word breaks for text justification.
/// </summary>
public byte BreakChar;
/// <summary>
/// The number of bytes in each row of the bitmap. This value is always even so that the rows
/// start on word boundaries. For vector fonts, this member has no meaning.
/// </summary>
public ushort WidthBytes;
/// <summary>
/// The offset in the file to a null-terminated string that specifies a device name. For a
/// generic font, this value is zero.
/// </summary>
public uint Device;
/// <summary>
/// The offset in the file to a null-terminated string that names the typeface.
/// </summary>
public uint Face;
/// <summary>
/// This member is reserved.
/// </summary>
public uint Reserved;
/// <summary>
/// The name of the device if this font file is designated for a specific device.
/// </summary>
public string DeviceName;
/// <summary>
/// The typeface name of the font.
/// </summary>
public string FaceName;
}
}

View File

@@ -0,0 +1,21 @@
namespace BurnOutSharp.Models.PortableExecutable
{
/// <summary>
/// Contains the information necessary for an application to access a specific font. The structure
/// definition provided here is for explanation only; it is not present in any standard header file.
/// </summary>
/// <see href="https://learn.microsoft.com/en-us/windows/win32/menurc/fontgrouphdr"/>
public class FontGroupHeader
{
/// <summary>
/// The number of individual fonts associated with this resource.
/// </summary>
public ushort NumberOfFonts;
/// <summary>
/// A structure that contains a unique ordinal identifier for each font in the resource. The DE
/// member is a placeholder for the variable-length array of DIRENTRY structures.
/// </summary>
public DirEntry[] DE;
}
}