Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/

A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
11/30/2026 EPPlus Software AB Tests for TTC extraction bounds checks
*************************************************************************************************/
using EPPlus.Fonts.OpenType.Scanner;
using EPPlus.Fonts.OpenType.Tests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;

namespace EPPlus.Fonts.OpenType.Tests.FontScanning
{
/// <summary>
/// Tests that DefaultFontFileReader validates a TTC face's table directory against the
/// file size before extracting it. TTC bytes are written to a temporary file because
/// ReadFontBytes reads from disk.
/// </summary>
[TestClass]
public class DefaultFontFileReaderTests : FontTestBase
{
public override TestContext? TestContext { get; set; }

private const int TtcHeaderSize = 16;

private static uint ReadUInt32(byte[] b, int pos)
{
return (uint)(b[pos] << 24 | b[pos + 1] << 16 | b[pos + 2] << 8 | b[pos + 3]);
}

private static void WriteUInt32(byte[] b, int pos, uint v)
{
b[pos] = (byte)(v >> 24);
b[pos + 1] = (byte)(v >> 16);
b[pos + 2] = (byte)(v >> 8);
b[pos + 3] = (byte)v;
}

private static void WriteUInt16(byte[] b, int pos, ushort v)
{
b[pos] = (byte)(v >> 8);
b[pos + 1] = (byte)v;
}

private static void WriteTtcHeader(byte[] b, uint numFonts, uint firstOffset)
{
WriteUInt32(b, 0, 0x74746366); // 'ttcf'
WriteUInt32(b, 4, 0x00010000); // version 1.0
WriteUInt32(b, 8, numFonts);
WriteUInt32(b, 12, firstOffset);
}

/// <summary>
/// Wraps Roboto-Regular.ttf in a single-face TTC: a 16 byte TTC header followed by the
/// font, with every table offset in the font's directory moved forward 16 bytes.
/// </summary>
private static byte[] BuildRobotoTtc(out byte[] originalFont)
{
originalFont = File.ReadAllBytes(Path.Combine(FontFolder, "Roboto-Regular.ttf"));
var ttc = new byte[TtcHeaderSize + originalFont.Length];
WriteTtcHeader(ttc, 1, TtcHeaderSize);
Buffer.BlockCopy(originalFont, 0, ttc, TtcHeaderSize, originalFont.Length);

int numTables = (originalFont[4] << 8) | originalFont[5];
for (int i = 0; i < numTables; i++)
{
int offsetPos = TtcHeaderSize + 12 + i * 16 + 8;
WriteUInt32(ttc, offsetPos, ReadUInt32(ttc, offsetPos) + TtcHeaderSize);
}
return ttc;
}

private static byte[] ReadFromTempFile(byte[] fileBytes, long offsetInFile)
{
string path = Path.GetTempFileName();
try
{
File.WriteAllBytes(path, fileBytes);
var face = new FontFaceInfo { FilePath = path, OffsetInFile = offsetInFile };
return new DefaultFontFileReader().ReadFontBytes(face);
}
finally
{
File.Delete(path);
}
}

[TestMethod]
[Timeout(5000)]
public void ReadFontBytes_ValidTtc_ExtractsLoadableFont()
{
var ttc = BuildRobotoTtc(out var originalBytes);

var extracted = ReadFromTempFile(ttc, TtcHeaderSize);

var original = new OpenTypeFont(originalBytes);
var font = new OpenTypeFont(extracted);
Assert.AreEqual(original.FullName, font.FullName);
Assert.AreEqual(original.MaxpTable.numGlyphs, font.MaxpTable.numGlyphs);

var originalA = original.GlyfTable.GetGlyph(37); // 'A'
var extractedA = font.GlyfTable.GetGlyph(37);
Assert.IsNotNull(extractedA);
CollectionAssert.AreEqual(originalA.SimpleData.XBytes, extractedA.SimpleData.XBytes);
CollectionAssert.AreEqual(originalA.SimpleData.YBytes, extractedA.SimpleData.YBytes);
}

[TestMethod]
[Timeout(5000)]
public void ReadFontBytes_TableLengthBeyondFile_Throws()
{
var ttc = BuildRobotoTtc(out _);
int firstLengthPos = TtcHeaderSize + 12 + 12; // first table record, length field
WriteUInt32(ttc, firstLengthPos, 0xFFFFFFF0);

Assert.ThrowsExactly<InvalidDataException>(() => ReadFromTempFile(ttc, TtcHeaderSize));
}

[TestMethod]
[Timeout(5000)]
public void ReadFontBytes_OverlappingTables_Throws()
{
// A small TTC where 1000 table records all point to the whole file.
const ushort numTables = 1000;
int fileLength = TtcHeaderSize + 12 + numTables * 16;
var ttc = new byte[fileLength];
WriteTtcHeader(ttc, 1, TtcHeaderSize);
WriteUInt32(ttc, TtcHeaderSize, 0x00010000); // sfntVersion
WriteUInt16(ttc, TtcHeaderSize + 4, numTables);

for (int i = 0; i < numTables; i++)
{
int record = TtcHeaderSize + 12 + i * 16;
WriteUInt32(ttc, record, 0x74737431 + (uint)i); // unique tag
WriteUInt32(ttc, record + 8, 0); // offset
WriteUInt32(ttc, record + 12, (uint)fileLength); // length = whole file
}

Assert.ThrowsExactly<InvalidDataException>(() => ReadFromTempFile(ttc, TtcHeaderSize));
}

[TestMethod]
[Timeout(5000)]
public void ReadFontBytes_DirectoryBeyondFile_Throws()
{
// The SFNT header claims 10 tables, but the file ends right after the header.
var ttc = new byte[TtcHeaderSize + 12];
WriteTtcHeader(ttc, 1, TtcHeaderSize);
WriteUInt32(ttc, TtcHeaderSize, 0x00010000);
WriteUInt16(ttc, TtcHeaderSize + 4, 10);

Assert.ThrowsExactly<InvalidDataException>(() => ReadFromTempFile(ttc, TtcHeaderSize));
}
}
}
97 changes: 97 additions & 0 deletions src/EPPlus.Fonts.OpenType.Tests/Helpers/GsubTestFontBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/

A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
12/01/2026 EPPlus Software AB Helper that gives a test font a crafted GSUB table
*************************************************************************************************/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;

namespace EPPlus.Fonts.OpenType.Tests
{
/// <summary>
/// Builds test fonts whose GSUB table is replaced by a crafted one, appended to the end of the
/// font file so it can be as large as needed.
/// </summary>
internal static class GsubTestFontBuilder
{
private static void WriteUInt16(byte[] b, int pos, int v)
{
b[pos] = (byte)(v >> 8);
b[pos + 1] = (byte)v;
}

private static void WriteUInt32(byte[] b, int pos, uint v)
{
b[pos] = (byte)(v >> 24);
b[pos + 1] = (byte)(v >> 16);
b[pos + 2] = (byte)(v >> 8);
b[pos + 3] = (byte)v;
}

/// <summary>Returns the position of the table record with the given tag in the table directory.</summary>
internal static int RecordPosition(byte[] font, string tag)
{
int numTables = (font[4] << 8) | font[5];
for (int i = 0; i < numTables; i++)
{
int record = 12 + i * 16;
if (Encoding.ASCII.GetString(font, record, 4) == tag)
return record;
}
throw new AssertFailedException("Table " + tag + " not found");
}

/// <summary>
/// Returns a copy of <paramref name="font"/> whose GSUB is one Multiple Substitution lookup that
/// replaces the glyph for <paramref name="trigger"/> with <paramref name="sequence"/>. The new
/// GSUB has no ScriptList or FeatureList and is appended to the end of the file.
/// </summary>
internal static byte[] AppendMultipleSubst(byte[] font, char trigger, ushort[] sequence)
{
Assert.IsTrue(new OpenTypeFont(font).CmapTable.TryGetGlyphId(trigger, out ushort triggerGlyph));

int gsub = (font.Length + 3) & ~3;
int length = 36 + 2 + 2 * sequence.Length;
var bytes = new byte[gsub + length];
Array.Copy(font, bytes, font.Length);

int record = RecordPosition(bytes, "GSUB");
WriteUInt32(bytes, record + 8, (uint)gsub);
WriteUInt32(bytes, record + 12, (uint)length);

// GSUB header: version 1.0, no ScriptList or FeatureList, LookupList at 10
WriteUInt16(bytes, gsub, 1);
WriteUInt16(bytes, gsub + 8, 10);
// LookupList (10): one lookup at +4
WriteUInt16(bytes, gsub + 10, 1);
WriteUInt16(bytes, gsub + 12, 4);
// Lookup (14): type 2 (Multiple Substitution), one subtable at +8
WriteUInt16(bytes, gsub + 14, 2);
WriteUInt16(bytes, gsub + 18, 1);
WriteUInt16(bytes, gsub + 20, 8);
// MultipleSubst format 1 (22): coverage at +8, one sequence at +14
WriteUInt16(bytes, gsub + 22, 1);
WriteUInt16(bytes, gsub + 24, 8);
WriteUInt16(bytes, gsub + 26, 1);
WriteUInt16(bytes, gsub + 28, 14);
// Coverage format 1 (30): the trigger glyph
WriteUInt16(bytes, gsub + 30, 1);
WriteUInt16(bytes, gsub + 32, 1);
WriteUInt16(bytes, gsub + 34, triggerGlyph);
// Sequence (36)
WriteUInt16(bytes, gsub + 36, sequence.Length);
for (int i = 0; i < sequence.Length; i++)
WriteUInt16(bytes, gsub + 38 + 2 * i, sequence[i]);

return bytes;
}
}
}
Loading
Loading