Files
harfbuzz/test/api/test-subset-cff2.c
Behdad EsfahbodandClaude Opus 4.8 fb58fbe259 [subset/cff2] Plan FDSelect from the real CFF2FDSelect type
cff2_subset_plan::create() cast acc.fdSelect (a CFF2FDSelect) to the
CFF1 FDSelect type for hb_plan_subset_cff_fdselect(), whose
get_fd_range() dispatch knows only formats 0 and 3.  A CFF2-only
format-4 FDSelect therefore reported FD 0 for every glyph, so the
planner kept only Font DICT 0: every retained glyph belonging to
another Font DICT lost that DICT's Private DICT and local subrs, and
when it called a local subr it either lost its outline or drew through
FD 0's subrs with the wrong shape.  The call still succeeded, writing a
single-Font-DICT font.  Format 4 is the only FDSelect format with a
16-bit FD index, so every CFF2 font with more than 256 Font DICTs is
affected.

The planner only needs get_fd_range() and format, so give CFF2FDSelect
its own get_fd_range() dispatching formats 0, 3 and 4, make
hb_plan_subset_cff_fdselect a template over the FDSelect type with
explicit instantiations for CFF1FDSelect and CFF2FDSelect (same pattern
as other explicitly instantiated subset helpers), and drop the cast.
The existing format-4 writer in hb_serialize_cff_fdselect is now
reachable.  Because a CFF2 font with >256 retained FDs can now
legitimately plan format 4, serialize_cff2_to_cff1() rejects that case
(CFF1 has no format 4) instead of emitting an invalid CFF table; with
fewer FDs the downgrade is unchanged.

Adds test_subset_cff2_fdselect_format4, which subsets the format-3 and
format-4 variants of a two-Font-DICT font and requires identical CFF2
output and glyph 2's extents; it fails before this change.

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-7RSDR0F0.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-09 13:18:05 -06:00

553 lines
22 KiB
C

/*
* Copyright © 2018 Adobe Inc.
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Adobe Author(s): Michiharu Ariza
*/
#include "hb-test.h"
#include "hb-subset-test.h"
/* Unit tests for CFF2 subsetting */
static void
test_subset_cff2_noop (void)
{
hb_face_t *face_abc = hb_test_open_font_file("fonts/AdobeVFPrototype.abc.otf");
hb_set_t *codepoints = hb_set_create ();
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 'a');
hb_set_add (codepoints, 'b');
hb_set_add (codepoints, 'c');
face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('C','F','F','2'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
}
static void
test_subset_cff2 (void)
{
hb_face_t *face_abc = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.otf");
hb_face_t *face_ac = hb_test_open_font_file ("fonts/AdobeVFPrototype.ac.otf");
hb_set_t *codepoints = hb_set_create ();
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 'a');
hb_set_add (codepoints, 'c');
face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('C','F','F','2'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
hb_face_destroy (face_ac);
}
static void
test_subset_cff2_strip_hints (void)
{
hb_face_t *face_abc = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.otf");
hb_face_t *face_ac = hb_test_open_font_file ("fonts/AdobeVFPrototype.ac.nohints.otf");
hb_set_t *codepoints = hb_set_create ();
hb_subset_input_t *input;
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 'a');
hb_set_add (codepoints, 'c');
input = hb_subset_test_create_input (codepoints);
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_NO_HINTING);
face_abc_subset = hb_subset_test_create_subset (face_abc, input);
hb_set_destroy (codepoints);
hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('C', 'F', 'F', '2'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
hb_face_destroy (face_ac);
}
static void
test_subset_cff2_desubr (void)
{
hb_face_t *face_abc = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.otf");
hb_face_t *face_ac = hb_test_open_font_file ("fonts/AdobeVFPrototype.ac.nosubrs.otf");
hb_set_t *codepoints = hb_set_create ();
hb_subset_input_t *input;
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 'a');
hb_set_add (codepoints, 'c');
input = hb_subset_test_create_input (codepoints);
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_DESUBROUTINIZE);
face_abc_subset = hb_subset_test_create_subset (face_abc, input);
hb_set_destroy (codepoints);
hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('C', 'F', 'F', '2'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
hb_face_destroy (face_ac);
}
static void
test_subset_cff2_desubr_strip_hints (void)
{
hb_face_t *face_abc = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.otf");
hb_face_t *face_ac = hb_test_open_font_file ("fonts/AdobeVFPrototype.ac.nosubrs.nohints.otf");
hb_set_t *codepoints = hb_set_create ();
hb_subset_input_t *input;
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 'a');
hb_set_add (codepoints, 'c');
input = hb_subset_test_create_input (codepoints);
hb_subset_input_set_flags (input,
HB_SUBSET_FLAGS_DESUBROUTINIZE | HB_SUBSET_FLAGS_NO_HINTING);
face_abc_subset = hb_subset_test_create_subset (face_abc, input);
hb_set_destroy (codepoints);
hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('C', 'F', 'F', '2'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
hb_face_destroy (face_ac);
}
static void
test_subset_cff2_retaingids (void)
{
hb_face_t *face_abc = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.otf");
hb_face_t *face_ac = hb_test_open_font_file ("fonts/AdobeVFPrototype.ac.retaingids.otf");
hb_set_t *codepoints = hb_set_create ();
hb_subset_input_t *input;
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 'a');
hb_set_add (codepoints, 'c');
input = hb_subset_test_create_input (codepoints);
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_RETAIN_GIDS);
face_abc_subset = hb_subset_test_create_subset (face_abc, input);
hb_set_destroy (codepoints);
hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('C', 'F', 'F', '2'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
hb_face_destroy (face_ac);
}
static void
test_subset_cff2_fdselect_format4 (void)
{
/* Three glyphs and two Font DICTs: glyphs 0-1 use FD 0, glyph 2 uses FD 1 and
* draws one line through FD 1's local subr 0. The two tables differ only in
* the FDSelect encoding: format 3, or the CFF2-only format 4. */
static const char maxp_data[] = {0, 0, 0x50, 0, 0, 3};
static const char cff2_fdselect3_data[] = {
0x02, 0, 0x05, 0, 0x14, /* CFF2 header */
0x1D, 0, 0, 0, 0x1D, 0x11, 0x1D, 0, 0, 0, 0x6D, 0x0C, 0x24, 0x1D, 0, 0, 0, 0x62, 0x0C, 0x25, /* Top DICT: CharStrings, FDArray, FDSelect */
0, 0, 0, 0, /* Global Subrs INDEX (empty) */
0, 0, 0, 0x03, 0x01, 0x01, 0x16, 0x2B, 0x3D, /* CharStrings INDEX */
0x1C, 0, 0, 0x1C, 0, 0, 0x15, 0x1C, 0, 0x0A, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0, 0x0A, 0x05, /* .notdef */
0x1C, 0, 0x32, 0x1C, 0, 0, 0x15, 0x1C, 0x01, (char) 0xC2, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0x02, (char) 0xBC, 0x05, /* glyph 1 */
0x1C, 0, 0x64, 0x1C, (char) 0xFF, 0x38, 0x15, 0x1C, (char) 0xFF, (char) 0x95, 0x0A, 0x1C, 0, 0, 0x1C, 0x01, 0x2C, 0x05, /* glyph 2: calls FD 1's local subr 0 */
0x03, 0, 0x02, 0, 0, 0, 0, 0x02, 0x01, 0, 0x03, /* FDSelect format 3: glyphs 0-1 -> FD 0, glyph 2 -> FD 1 */
0, 0, 0, 0x02, 0x01, 0x01, 0x0A, 0x13, /* FDArray INDEX */
0x1C, 0, 0x04, 0x1D, 0, 0, 0, (char) 0x87, 0x12, /* Font DICT 0 */
0x1C, 0, 0x0A, 0x1D, 0, 0, 0, (char) 0x8B, 0x12, /* Font DICT 1 */
0x1C, 0, 0x32, 0x0A, /* Private DICT 0 */
0x1C, 0, 0x50, 0x0A, 0x1D, 0, 0, 0, 0x0A, 0x13, /* Private DICT 1: Subrs */
0, 0, 0, 0x01, 0x01, 0x01, 0x08, 0x1C, 0, 0x64, 0x1C, 0, 0x64, 0x05, /* FD 1 local Subrs INDEX */
};
static const char cff2_fdselect4_data[] = {
0x02, 0, 0x05, 0, 0x14, /* CFF2 header */
0x1D, 0, 0, 0, 0x1D, 0x11, 0x1D, 0, 0, 0, 0x77, 0x0C, 0x24, 0x1D, 0, 0, 0, 0x62, 0x0C, 0x25, /* Top DICT: CharStrings, FDArray, FDSelect */
0, 0, 0, 0, /* Global Subrs INDEX (empty) */
0, 0, 0, 0x03, 0x01, 0x01, 0x16, 0x2B, 0x3D, /* CharStrings INDEX */
0x1C, 0, 0, 0x1C, 0, 0, 0x15, 0x1C, 0, 0x0A, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0, 0x0A, 0x05, /* .notdef */
0x1C, 0, 0x32, 0x1C, 0, 0, 0x15, 0x1C, 0x01, (char) 0xC2, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0x02, (char) 0xBC, 0x05, /* glyph 1 */
0x1C, 0, 0x64, 0x1C, (char) 0xFF, 0x38, 0x15, 0x1C, (char) 0xFF, (char) 0x95, 0x0A, 0x1C, 0, 0, 0x1C, 0x01, 0x2C, 0x05, /* glyph 2: calls FD 1's local subr 0 */
0x04, 0, 0, 0, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02, 0, 0x01, 0, 0, 0, 0x03, /* FDSelect format 4: glyphs 0-1 -> FD 0, glyph 2 -> FD 1 */
0, 0, 0, 0x02, 0x01, 0x01, 0x0A, 0x13, /* FDArray INDEX */
0x1C, 0, 0x04, 0x1D, 0, 0, 0, (char) 0x91, 0x12, /* Font DICT 0 */
0x1C, 0, 0x0A, 0x1D, 0, 0, 0, (char) 0x95, 0x12, /* Font DICT 1 */
0x1C, 0, 0x32, 0x0A, /* Private DICT 0 */
0x1C, 0, 0x50, 0x0A, 0x1D, 0, 0, 0, 0x0A, 0x13, /* Private DICT 1: Subrs */
0, 0, 0, 0x01, 0x01, 0x01, 0x08, 0x1C, 0, 0x64, 0x1C, 0, 0x64, 0x05, /* FD 1 local Subrs INDEX */
};
hb_face_t *face_fdselect3 = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face_fdselect3, "maxp", maxp_data);
HB_FACE_ADD_TABLE (face_fdselect3, "CFF2", cff2_fdselect3_data);
hb_face_t *face_fdselect4 = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face_fdselect4, "maxp", maxp_data);
HB_FACE_ADD_TABLE (face_fdselect4, "CFF2", cff2_fdselect4_data);
hb_set_t *glyphs = hb_set_create ();
hb_face_t *face_fdselect3_subset, *face_fdselect4_subset;
hb_set_add_range (glyphs, 0, 2);
face_fdselect3_subset = hb_subset_test_create_subset (face_fdselect3, hb_subset_test_create_input_from_glyphs (glyphs));
face_fdselect4_subset = hb_subset_test_create_subset (face_fdselect4, hb_subset_test_create_input_from_glyphs (glyphs));
hb_set_destroy (glyphs);
/* Both retain FD 1; the format-4 source must not have been planned as FD 0 only. */
hb_subset_test_check (face_fdselect3_subset, face_fdselect4_subset, HB_TAG ('C','F','F','2'));
hb_font_t *font = hb_font_create (face_fdselect4_subset);
hb_glyph_extents_t extents;
g_assert_true (hb_font_get_glyph_extents (font, 2, &extents));
g_assert_cmpint (extents.x_bearing, ==, 100);
g_assert_cmpint (extents.y_bearing, ==, 200);
g_assert_cmpint (extents.width, ==, 100);
g_assert_cmpint (extents.height, ==, -400);
hb_font_destroy (font);
hb_face_destroy (face_fdselect4_subset);
hb_face_destroy (face_fdselect3_subset);
hb_face_destroy (face_fdselect4);
hb_face_destroy (face_fdselect3);
}
#ifdef HB_EXPERIMENTAL_API
static void
test_subset_cff2_iftb_requirements (void)
{
hb_face_t *face_abc = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.otf");
hb_face_t *face_long_off = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.long_off.otf");
hb_set_t *codepoints = hb_set_create();
hb_face_t *face_abc_subset;
hb_set_add (codepoints, 97);
hb_set_add (codepoints, 98);
hb_set_add (codepoints, 99);
hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_IFTB_REQUIREMENTS);
face_abc_subset = hb_subset_test_create_subset (face_abc, input);
hb_set_destroy (codepoints);
hb_subset_test_check (face_long_off, face_abc_subset, HB_TAG ('C','F','F', '2'));
hb_face_destroy (face_abc_subset);
hb_face_destroy (face_abc);
hb_face_destroy (face_long_off);
}
#endif
/* Caller-dependent subroutine numbers cannot be remapped at a shared offset. */
static void
test_subset_cff2_caller_supplied_subr (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/cff2_caller_subr.otf");
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add_range (hb_subset_input_unicode_set (input), 'A', 'B');
for (unsigned i = 0; i < 2; i++)
{
hb_subset_input_set_flags (input, i ? HB_SUBSET_FLAGS_NO_HINTING : HB_SUBSET_FLAGS_DEFAULT);
hb_face_t *subset = hb_subset_or_fail (face, input);
g_assert_null (subset);
}
/* Explicit flattening does not remap caller-supplied operands. */
hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_DESUBROUTINIZE);
hb_face_t *subset = hb_subset_or_fail (face, input);
g_assert_nonnull (subset);
hb_font_t *font = hb_font_create (subset);
hb_glyph_extents_t extents;
g_assert_true (hb_font_get_glyph_extents (font, 1, &extents));
g_assert_cmpint (extents.width, ==, 10);
g_assert_true (hb_font_get_glyph_extents (font, 2, &extents));
g_assert_cmpint (extents.width, ==, 20);
hb_font_destroy (font);
hb_face_destroy (subset);
hb_subset_input_destroy (input);
hb_face_destroy (face);
}
static void
test_subset_cff2_long_private_blends (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/cff2_long_private_blends.otf");
hb_blob_t *source = hb_face_reference_table (face, HB_TAG ('C','F','F','2'));
unsigned source_length;
const char *source_data = hb_blob_get_data (source, &source_length);
/* This fixture's Private DICT occupies [395, 974), including its final
* BlueValues operator. Its live stack never exceeds 513 operands. */
const unsigned private_offset = 395, private_length = 579;
g_assert_cmpuint (private_offset + private_length, <=, source_length);
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add_range (hb_subset_input_unicode_set (input), 'A', 'B');
hb_face_t *subset = hb_subset_or_fail (face, input);
g_assert_nonnull (subset);
hb_blob_t *result = hb_face_reference_table (subset, HB_TAG ('C','F','F','2'));
unsigned length;
const char *data = hb_blob_get_data (result, &length);
gboolean found = FALSE;
for (unsigned i = 0; i + private_length <= length; i++)
if (!memcmp (source_data + private_offset, data + i, private_length))
{
found = TRUE;
break;
}
g_assert_true (found);
hb_blob_destroy (result);
hb_face_destroy (subset);
hb_subset_input_destroy (input);
hb_blob_destroy (source);
hb_face_destroy (face);
}
static gboolean
blob_contains_bytes (hb_blob_t *blob, const unsigned char *needle, unsigned needle_len)
{
unsigned len = 0;
const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &len);
if (len < needle_len)
return FALSE;
for (unsigned i = 0; i + needle_len <= len; i++)
if (!memcmp (data + i, needle, needle_len))
return TRUE;
return FALSE;
}
static void
test_subset_cff2_pinned_private_blend_overflow (void)
{
/* StdVW blend delta of 1e12; full pinning and CFF2->CFF1 downgrade flatten
* Private DICT blends directly without a partial instancer and must clamp
* out-of-range rounded values to INT_MAX instead of invoking float-to-int UB
* (which produces x86 integer indefinite INT_MIN 0x80000000 in release builds). */
/* CFF2 longint encoding of +2147483647 (clamped INT_MAX), followed by
* OpCode_StdVW (0x0b), vs. the longint encoding of -2147483648 that
* unpatched float-to-int UB produces on x86 (which casts the 1e12 blend
* result to INT_MIN 0x80000000). */
static const unsigned char clamped_int_max[6] = {0x1d, 0x7f, 0xff, 0xff, 0xff, 0x0b};
static const unsigned char indef_int_min[6] = {0x1e, 0xe2, 0x14, 0x74, 0x83, 0x6b};
hb_face_t *face = hb_test_open_font_file ("fonts/AdobeVFPrototype.abc.huge-blend.otf");
const hb_subset_flags_t modes[] = {
HB_SUBSET_FLAGS_DEFAULT,
HB_SUBSET_FLAGS_DOWNGRADE_CFF2,
};
for (unsigned m = 0; m < G_N_ELEMENTS (modes); m++)
{
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add (hb_subset_input_unicode_set (input), 0x61);
hb_subset_input_set_flags (input, modes[m]);
g_assert_true (hb_subset_input_pin_axis_location (input, face,
HB_TAG ('w', 'g', 'h', 't'), 600.f));
g_assert_true (hb_subset_input_pin_axis_location (input, face,
HB_TAG ('C', 'N', 'T', 'R'), 100.f));
hb_face_t *subset = hb_subset_or_fail (face, input);
hb_subset_input_destroy (input);
g_assert_nonnull (subset);
hb_blob_t *cff_blob = hb_face_reference_table (subset,
modes[m] == HB_SUBSET_FLAGS_DOWNGRADE_CFF2
? HB_TAG ('C', 'F', 'F', ' ')
: HB_TAG ('C', 'F', 'F', '2'));
g_assert_cmpuint (hb_blob_get_length (cff_blob), >, 0);
g_assert_true (blob_contains_bytes (cff_blob, clamped_int_max, sizeof (clamped_int_max)));
g_assert_false (blob_contains_bytes (cff_blob, indef_int_min, sizeof (indef_int_min)));
hb_blob_destroy (cff_blob);
hb_font_t *font = hb_font_create (subset);
hb_codepoint_t glyph = 0;
hb_glyph_extents_t extents;
g_assert_true (hb_font_get_nominal_glyph (font, 0x61, &glyph));
g_assert_true (hb_font_get_glyph_extents (font, glyph, &extents));
g_assert_cmpint (extents.width, >, 0);
hb_font_destroy (font);
/* Re-subsetting the produced static face must also succeed cleanly. */
hb_subset_input_t *reinput = hb_subset_input_create_or_fail ();
hb_set_add (hb_subset_input_unicode_set (reinput), 0x61);
hb_face_t *resubset = hb_subset_or_fail (subset, reinput);
hb_subset_input_destroy (reinput);
g_assert_nonnull (resubset);
hb_face_destroy (resubset);
hb_face_destroy (subset);
}
hb_face_destroy (face);
}
static hb_face_t *
create_cff2_face_with_top_dict_expansion (unsigned matrix_count, unsigned short_cs_ops)
{
/* Build a valid 1-glyph (.notdef) CFF2 + maxp face whose input Top DICT fits
* within HBUINT16 (topDictSize = 8 * matrix_count + 2 * short_cs_ops + 13 <= 65535),
* while each 2-byte short CharStrings op (0x8b 0x11, overridden by the final real
* CharStrings offset) expands to a 6-byte link4 op during CFF2 Top DICT subset
* serialization (+4 bytes per short_cs_ops). */
const unsigned top_dict_size = 8 * matrix_count + 2 * short_cs_ops + 13;
g_assert_cmpuint (top_dict_size, <=, 0xFFFFu);
const unsigned cs_off = 5 + top_dict_size + 4;
const unsigned fd_off = cs_off + 7;
const unsigned total_len = fd_off + 14;
unsigned char *buf = (unsigned char *) g_malloc0 (total_len);
unsigned p = 0;
buf[p++] = 0x02;
buf[p++] = 0x00;
buf[p++] = 0x05;
buf[p++] = (unsigned char) (top_dict_size >> 8);
buf[p++] = (unsigned char) (top_dict_size & 0xFF);
static const unsigned char font_matrix_op[8] = {
0x8c, 0x8b, 0x8b, 0x8c, 0x8b, 0x8b, 0x0c, 0x07
};
for (unsigned i = 0; i < matrix_count; i++)
{
memcpy (buf + p, font_matrix_op, sizeof (font_matrix_op));
p += sizeof (font_matrix_op);
}
for (unsigned i = 0; i < short_cs_ops; i++)
{
buf[p++] = 0x8b;
buf[p++] = 0x11;
}
/* Final authoritative CharStrings offset (5-byte int + op 17 = 6 bytes) */
buf[p++] = 0x1d;
buf[p++] = (unsigned char) (cs_off >> 24);
buf[p++] = (unsigned char) (cs_off >> 16);
buf[p++] = (unsigned char) (cs_off >> 8);
buf[p++] = (unsigned char) (cs_off & 0xFF);
buf[p++] = 0x11;
/* FDArray offset (5-byte int + op 12 36 = 7 bytes) */
buf[p++] = 0x1d;
buf[p++] = (unsigned char) (fd_off >> 24);
buf[p++] = (unsigned char) (fd_off >> 16);
buf[p++] = (unsigned char) (fd_off >> 8);
buf[p++] = (unsigned char) (fd_off & 0xFF);
buf[p++] = 0x0c;
buf[p++] = 0x24;
g_assert_cmpuint (p, ==, 5 + top_dict_size);
/* GlobalSubrs INDEX: count = 0 (4 bytes) */
p += 4;
g_assert_cmpuint (p, ==, cs_off);
/* CharStrings INDEX: 1 empty .notdef charstring (7 bytes) */
static const unsigned char cs_index[7] = {0, 0, 0, 1, 1, 1, 1};
memcpy (buf + p, cs_index, sizeof (cs_index));
p += sizeof (cs_index);
g_assert_cmpuint (p, ==, fd_off);
/* FDArray INDEX: 1 Font DICT of 7 bytes with 0-byte Private at total_len */
buf[p++] = 0x00; buf[p++] = 0x00; buf[p++] = 0x00; buf[p++] = 0x01;
buf[p++] = 0x01;
buf[p++] = 0x01; buf[p++] = 0x08;
buf[p++] = 0x8b; /* Private size = 0 */
buf[p++] = 0x1d;
buf[p++] = (unsigned char) (total_len >> 24);
buf[p++] = (unsigned char) (total_len >> 16);
buf[p++] = (unsigned char) (total_len >> 8);
buf[p++] = (unsigned char) (total_len & 0xFF);
buf[p++] = 0x12; /* OpCode_Private */
g_assert_cmpuint (p, ==, total_len);
static const unsigned char maxp_data[6] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x01};
hb_face_t *builder = hb_face_builder_create ();
hb_blob_t *cff2_blob = hb_blob_create ((const char *) buf, total_len,
HB_MEMORY_MODE_READONLY, buf, g_free);
hb_blob_t *maxp_blob = hb_blob_create ((const char *) maxp_data, sizeof (maxp_data),
HB_MEMORY_MODE_READONLY, NULL, NULL);
g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('C', 'F', 'F', '2'), cff2_blob));
g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('m', 'a', 'x', 'p'), maxp_blob));
hb_blob_destroy (cff2_blob);
hb_blob_destroy (maxp_blob);
return builder;
}
static void
test_subset_cff2_top_dict_size_overflow (void)
{
/* Positive control: input topDictSize = 32021, output topDictSize = 32037 <= 0xFFFF. */
{
hb_face_t *face = create_cff2_face_with_top_dict_expansion (4000, 4);
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add (hb_subset_input_glyph_set (input), 0);
hb_face_t *subset = hb_subset_or_fail (face, input);
hb_subset_input_destroy (input);
g_assert_nonnull (subset);
hb_blob_t *out_cff2 = hb_face_reference_table (subset, HB_TAG ('C', 'F', 'F', '2'));
unsigned out_len = 0;
const unsigned char *out_data = (const unsigned char *) hb_blob_get_data (out_cff2, &out_len);
g_assert_cmpuint (out_len, >, 32042u);
unsigned out_top_dict_size = ((unsigned) out_data[3] << 8) | out_data[4];
g_assert_cmpuint (out_top_dict_size, ==, 32037u);
hb_blob_destroy (out_cff2);
hb_face_destroy (subset);
hb_face_destroy (face);
}
/* Negative overflow case: input topDictSize = 65525 <= 0xFFFF expands during
* CFF2 Top DICT serialization to 65541 > 0xFFFF, which must fail cleanly
* instead of narrowing cff2->topDictSize modulo 65536 (to 5). */
{
hb_face_t *face = create_cff2_face_with_top_dict_expansion (8188, 4);
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add (hb_subset_input_glyph_set (input), 0);
hb_face_t *subset = hb_subset_or_fail (face, input);
hb_subset_input_destroy (input);
g_assert_null (subset);
hb_face_destroy (face);
}
}
int
main (int argc, char **argv)
{
hb_test_init (&argc, &argv);
hb_test_add (test_subset_cff2_caller_supplied_subr);
hb_test_add (test_subset_cff2_long_private_blends);
hb_test_add (test_subset_cff2_pinned_private_blend_overflow);
hb_test_add (test_subset_cff2_top_dict_size_overflow);
hb_test_add (test_subset_cff2_noop);
hb_test_add (test_subset_cff2);
hb_test_add (test_subset_cff2_strip_hints);
hb_test_add (test_subset_cff2_desubr);
hb_test_add (test_subset_cff2_desubr_strip_hints);
hb_test_add (test_subset_cff2_retaingids);
hb_test_add (test_subset_cff2_fdselect_format4);
#ifdef HB_EXPERIMENTAL_API
hb_test_add (test_subset_cff2_iftb_requirements);
#endif
return hb_test_run ();
}