Commit Graph

356 Commits

Author SHA1 Message Date
Ben Wagner 17545d4bf7 Avoid n^2 scanning for binary data.
When creating a CID parser the location of the 'StartData' or '/sfnts'
tokens needs to be known.  However, the token parser requires that the
entire document be in memory and flattening the entire stream into memory is
to be avoided.

To avoid forcing the entire stream into memory, previously this code would
scan through the stream looking for 'StartData' or '/sfnts' as strings.
However, these strings could have been in a comment or string token, so the
stream would be read into memory up to that point and the parser run to
check that these strings were actually tokens.  This forced a parser restart
from the beginning each time; as a result, data with many 'StartData'
non-tokens would take n^2 time to check.

* src/cid/cidparse.c (cid_parser_new): Change algorithm to make the initial
scan look for the last possible 'StartData' or '/sfnts' string in the
stream.  The stream is read forward instead of backward as a typical normal
CID font will have one 'StartData' toward the beginning of the data and it
it much faster to read the data from beginning to end instead of end to
beginning.  For memory-based fonts the limit is set to the end of the stream
since the stream is already in memory.  Then the parser is run once to look
for 'StartData' or '/sfnts' tokens.  If they are found the parser is re-set
to reflect this new information.

Reported as

  https://issues.chromium.org/issues/40201695
2024-03-08 17:47:43 +01:00
Alexei Podtelezhnikov 47574f7ea4 Update all copyright notices. 2024-01-27 11:11:22 -05:00
Alexei Podtelezhnikov 8d0897b37d [type1, cid, type42] Post-cleanup.
* include/freetype/internal/psaux.h (T1_FIELD_ZERO): Terminating macro.
* src/cid/cidload.c (cid_parse_dict): Use while-loop.
* src/type1/t1load.c (parse_dict): Ditto.
* src/type42/t42parse.c (t42_parse_dict): Ditto.
2023-09-09 22:20:00 -04:00
Alexei Podtelezhnikov 3302e2f60c Update forgotten array termini.
* src/cid/cidload.c (cid_field_records): Account for added `len`.
* src/type1/t1load.c (t1_keywords): Ditto.
* src/type42/t42parse.c (t42_keywords): Ditto.
2023-09-09 17:24:34 -04:00
Alexei Podtelezhnikov 7ad9d57c3b [type1, cid, type42] Streamline dictionary parsing.
When matching the keywords, we avoid calculating their lengths by
checking the stored values. This itself is a sufficient pre-check
before diving into `memcmp`. Therefore, we remove explicit check of
the first characters.

* include/freetype/internal/psaux.h (T1_FieldRec): Store length.
* src/cid/cidload.c (cid_parse_dict): Use `memcmp` and stored length.
* src/type1/t1load.c (parse_dict): Ditto.
* src/type42/t42parse.c (t42_parse_dict): Ditto.
2023-09-09 15:19:06 -04:00
Werner Lemberg e4586d960f * Version 2.13.1 released.
==========================

Tag sources with `VER-2-13-1'.

* docs/VERSION.TXT: Add entry for version 2.13.1.
* docs/CHANGES: Updated.
* docs/release, docs/README, builds/macs/README: Updated.

* README, src/base/ftver.rc, builds/windows/vc2010/index.html,
builds/windows/visualc/index.html, builds/windows/visualce/index.html,
builds/wince/vc2005-ce/index.html, builds/wince/vc2008-ce/index.html,
docs/freetype-config.1: s/2.13.0/2.13/1/, s/2130/2131/.

* include/freetype/freetype.h (FREETYPE_PATCH): Set to 1.

* builds/unix/configure.raw (version_info): Set to 26:0:20.
* CMakeLists.txt (VERSION_PATCH): Set to 1.

* subprojects/libpng.wrap, subprojects/zlib.wrap, subprojects/dlg: Updated.
2023-06-24 08:11:05 +02:00
suzuki toshiya 5c00a46805 Comment fixes. 2023-06-09 07:36:49 +02:00
Ben Wagner 2fb8eda5d6 */*: Remove many function pointer casts.
In C it is undefined behavior to call a function through a function pointer
of a different type.  This is now detected by the Control Flow Integrity
Sanitizer.  All known issues have already been fixed.  Prevent any
accidental re-introduction by removing function pointer casts when defining
services.  The services will call the service functions through the function
pointers on the service.  As a result the functions must have the same type
so there should be no need to cast.  Removing the casts allows compilers to
warn about assignment to an incompatible function pointer type.
2023-06-03 06:58:09 +02:00
suzuki toshiya 6ca0a9356f [t1cid] Set FT_FACE_FLAG_CID_KEYED.
* cidobjs.c (cid_face_init): Set FT_FACE_FLAG_CID_KEYED.

* cidriver.c (cid_get_is_cid): Comment about the case that
is_cid cannot guarantee the glyph collection specification.
2023-05-11 14:31:23 +00:00
Werner Lemberg 3297a7a9e0 * src/cid/cidload.c: Signature fixes. 2023-05-07 15:23:46 +02:00
Werner Lemberg b9ce8e33a7 * src/cid/cidriver.c: Clean up interface.
Ensure that all driver functions use the signature of the service or driver.
This avoids pointer mismatches, which are technically undefined behaviour.
Recent compilers are more picky in catching them as part of Control Flow
Integrity tests.
2023-05-07 14:59:50 +02:00
Werner Lemberg b5e57b041b [cid] Improve tracing messages; formatting. 2023-05-06 16:41:13 +02:00
suzuki toshiya be15811c46 [t1cid] Improve cid_get_cid_from_glyph_index().
Update cid_get_cid_from_glyph_index() to
return an error and CID=0 in the case that
the specified glyph index points to an invalid
entry.

cidgload.h (cid_compute_fd_and_offsets):
Declare new helper function to set the
fd_select and 2 offsets to access the glyph
description data.

cidgload.c (cid_compute_fd_and_offsets):
Move the part loading fd_select and 2 offsets
from cid_load_glyph() to here.  If the loaded
parameters are broken, return the Invalid_Offset
error.  This function does not load the glyph
data, only fills these parameters.

(cid_load_glyph): Use new helper function in above.

cidriver.c (cid_get_cid_from_glyph_index):
Check whether the requested glyph index points
to a valid entry, by calling cid_compute_fd_and_offsets().
If it is valid, fill the cid by the glyph
index (=CID).  If it is invalid, return an
error and fill the cid by 0.
2023-05-03 01:31:37 +00:00
Werner Lemberg 4908c1e84d * src/cid/cidgload.c (cid_load_glyph): Fix compiler warnings. 2023-04-28 06:54:25 +02:00
suzuki toshiya f312b3402a [t1cid] Change the trace messages of the charstrings retrieval errors.
The t1cid driver catches 3 types of errors in
the charstrings retrieval;
A) The invalid FD number, there are 2 subtypes;
  A-1) FD number is the maximum number fitting to FDBytes.
  A-2) FD number is greater than num_dicts.
B) Declared length is overrunning.
C) Declared length is invalid (its end is before its head).

Considering that some widely distributed fonts
(e.g., "CJKV" book by O'Reilly) have A-1 errors
in the unimplemented glyphs, the trace level for
A-1 is calmed to level 1.

The errors A-2, B, and C would be irregular;
their trace levels are kept at level 0, but
the updated trace messages include the CID number.
2023-04-24 14:06:09 +09:00
Alexei Podtelezhnikov 5ac6276af1 [cff,cid,type1] Shorten de-referencing.
* src/cff/cffobjs.c (cff_clot_init): Use immediate library reference.
* src/cid/cidobjs.c (cid_slot_init): Ditto.
* src/type1/t1objs.c (T1_GlyphSlot_Init): Ditto.
2023-03-03 17:21:10 +00:00
Ben Wagner 09b326fa2b Avoid strtol on non-null-terminated data.
Technically, `strtol` can only be used with C strings terminated with
`\0`.  CID data is not generally null-terminated and often does not
contain a `\0` if it is hex-encoded.  AddressSanitizer with `ASAN_OPTIONS`
containing `strict_string_checks=1` verifies this by using an adversarial
`strtol` that always reads to the terminating `\0`.

To avoid undefined behavior from `strtol` in `cid_parser_new`, use the
parser to parse the tokens instead of attempting to parse them ad-hoc.
This will internally use `PS_Conv_Strtol` to parse the integer, which
respects the parser's limits and directly implements the PostScript
parsing rules for integers.

* src/cid/cidparse.c (cid_parser_new): Use the parser to parse the
tokens.

Fixes: https://bugs.chromium.org/p/chromium/issues/detail?id=1420329
2023-03-02 20:56:41 +01:00
Werner Lemberg 663486a7ac Fix `FT_LOCAL` and `FT_LOCAL_DEF` tags. 2023-02-08 21:09:32 +01:00
Werner Lemberg 65f8523706 Update all copyright notices. 2023-01-17 09:18:25 +01:00
Werner Lemberg d0cfb4e1b2 Update all copyright notices. 2022-01-11 10:54:10 +01:00
Werner Lemberg 3cabd142ce Update `CHANGES` files, other minor whitespace and documentation issues. 2021-11-22 19:58:21 +01:00
Werner Lemberg 8ef8072ba1 [bdf, cid, pfr, winfonts] Improve rejection of other font formats.
This is mainly for better diagnostics of malformed fonts.

* src/bdf/bdflib.c (_bfd_readstream): Stop font format testing if the first
input line is too long or doesn't end with `\r` or `\n`.

* src/cid/cidparse.c (cid_parser_new): Don't handle too short input as an
error but as an unknown format.

* src/pfr/pfrobjs.c (pfr_face_init): Ditto.

* src/winfonts/winfnt.c (fnt_font_load, fnt_face_get_dll_font): Ditto.
2021-10-19 22:59:46 +02:00
Alexei Podtelezhnikov e294a95ca8 * src/cid/cidload.c (parse_fd_array): Protect against trancation. 2021-10-11 23:25:29 -04:00
Alexei Podtelezhnikov 3b036820d6 [cid] Signedness revisions.
Unsigned checks are simpler.

* include/freetype/t1tables.h (CID_FaceInfoRec): Change to unsignd
`num_dicts`.
* src/cid/cidparse.h (CID_Parser): Change to unsigned `num_dict`.

* src/cid/cidgload.c (cid_load_glyph): Updated.
* src/cid/cidload.c (cid_load_keyword, parse_fd_array,
parse_expansion_factor, parse_font_name, cid_read_subrs,
cid_face_open): Updated.
* src/cid/cidobjs.c (cid_face_done): Updated.
* src/cid/cidparse.c (cid_parser_new): Updated.
2021-10-10 23:11:10 -04:00
Alexei Podtelezhnikov 012b4f2d48 * src/cid/cidload.c (cid_face_open): Streamline CIDCount check. 2021-10-08 22:14:12 -04:00
Alexei Podtelezhnikov 946df22165 * src/cid/cidload.c (cid_face_open): Streamline SubrCount check. 2021-10-07 22:44:53 -04:00
Alexei Podtelezhnikov 0313a11c4c * src/cid/cidgload.c (cid_load_glyph): Fortify incremental loading. 2021-10-07 22:43:12 -04:00
Alexei Podtelezhnikov 22befeef82 Signedness revisions.
This eliminates explicit casting by switching to unsigned fields.
The revisions mostly impact the handling of CID fonts.

* include/freetype/fttypes.h (FT_Data): Change to unsigned `length`.
* include/freetype/t1tables.h (CID_FaceDictRec): Ditto for `sd_bytes`.
(CID_FaceInfoRec): Ditto for `gd_bytes` and `gd_bytes`.
* include/freetype/internal/tttypes.h (TT_LoaderRec): Ditto for
`byte_len`.

* src/cid/cidgload.c (cid_load_glyph): Updated.
* src/cid/cidload.h (cid_get_offset): Update argument.
* src/cid/cidload.c (cid_get_offset, cid_read_subrs, cid_face_open):
Updated.
* src/cff/cffgload.c (cff_get_glyph_data, cff_free_glyph_data):
Updated.
* src/psaux/psft.c (cf2_getT1SeacComponent): Updated.
* src/truetype/ttgload.c (TT_Process_Composite_Glyph,
load_truetype_glyph): Updated.
2021-10-07 22:41:56 -04:00
Alexei Podtelezhnikov 6190360967 Minor. 2021-09-20 22:18:29 -04:00
Werner Lemberg f11f3ed15b [base] Fix ppem size overflow.
Fixes #1086.

* src/base/ftobjs.c (FT_Request_Metrics): Add return value.
Check whether ppem values fit into unsigned short values.
(FT_Request_Size): Updated.

* include/freetype/internal/ftobjs.h: Updated.

* src/cff/cffobjs.c (cff_size_request), src/cid/cidobjs.c
(cid_size_request), src/truetype/ttdriver.c (tt_size_request),
src/type1/t1objs.c (T1_Size_Request): Updated.
2021-08-18 07:27:02 +02:00
Werner Lemberg 08f66322e3 More various documentation improvements and fixes. 2021-06-08 18:23:16 +02:00
Werner Lemberg 6e253b26b2 Typos in previous commit. 2021-05-29 11:07:10 +02:00
Werner Lemberg a50c39aa8e Fix compilation errors and (some) warnings for clang++.
* src/autofit/afmodule.c (AF_GlyphHintsRec): Make it static.

* src/cache/ftcache.c (FTC_Cache_NewNode), src/cache/ftcsbits.c
(ftc_snode_compare): Remove semicolon.

* src/cff/cffparse.c (cff_parser_run): Add `break` statement.

* src/cid/cidload.c (cid_hex_to_binary): Add cast.

* src/sdf/ftbsdf.c (CHECK_NEIGHBOR): Use `do {} while(0)` loop.
(bsdf_init_distance_map, finalize_sdf, bsdf_raster_render): Add
casts.
* src/sdf/ftsdf.c (sdf_generate_bounding_box,
sdf_generate_with_overlaps): Ditto.
* src/sdf/ftsdfcommon.h (square_root): Ditto.
* src/sdf/ftsdfrend.c (sdf_property_get, ft_sdf_render,
ft_bsdf_render): Ditto.

* src/sfnt/ttcolr.c (find_base_glyph_record,
find_base_glyph_v1_record): Fix variable signedness.
(read_color_line): Add cast.
(read_paint): Add casts.
Fix signedness issue.
(tt_face_get_colorline_stops) Fix signedness issues.

* src/sfnt/ttpost.c (load_format_20): Add casts.

* src/truetype/ttsubpix.c (TWEAK_RULES, TWEAK_RULES_EXCEPTIONS):
Remove final semicolons.
2021-05-29 09:51:04 +02:00
Alexei Podtelezhnikov 998c7c0f1c * src/cid/cidload.c (cid_read_subrs): Tweak allocation macro. 2021-05-06 22:51:37 -04:00
Ben Wagner 82fd32d674 * src/cid/cidload.c (cid_hex_to_binary): Improve return value.
Add argument to return the actual number of bytes that were decoded.
The actual number of bytes decoded can be quite variable depending
on the number of ignored 'whitespace' bytes or early termination
with `>`.
(cid_face_open): Updated to use this calculated value.  This avoids
trusting `parser->binary_length` is always be correct and reading
uninitialized bits if fewer are actually decoded.

First reported as

  https://crbug.com/1203240
2021-05-04 08:56:10 +02:00
Alexei Podtelezhnikov d911cb5392 * src/cid/cidload.c (cid_read_subrs): Tweak allocaton macro. 2021-05-01 23:49:11 -04:00
Alexei Podtelezhnikov c213c8a9bb * src/cid/cidgload.c (cid_load_glyph): Restore the glyph_length check. 2021-04-27 08:54:19 -04:00
Alexei Podtelezhnikov baa0f71ddf [cid] s/FT_ALLOC/FT_QALLOC/ and clean up.
* src/cid/cidgload.c (cid_load_glyph): Do not zero out the buffer.
* src/cid/cidload.c (cid_face_open, cid_read_subrs): Ditto.
2021-04-23 13:48:34 -04:00
Werner Lemberg d924a66a5c * src/*: Don't use more than one '\n' in `FT_TRACE` and `FT_ERROR`.
This ensures good logging output, with all lines having a proper
prefix (if requested).

This is a continuation of a similar patch from 2020-12-02, which
missed some locations.
2021-02-04 07:46:46 +01:00
Werner Lemberg b6e8a71266 Update all copyright notices. 2021-01-17 07:18:48 +01:00
Werner Lemberg a6adb25626 * src/*: Don't use more than one '\n' in `FT_TRACE` and `FT_ERROR`.
This ensures good logging output, with all lines having a proper
prefix (if requested).
2020-12-05 09:43:45 +01:00
Alexei Podtelezhnikov 5abd252a0a [cff,cid,type1] Demote old engine for lack of CFF2.
* src/cff/cffobjs.c (cff_iriver_init): Always default to Adobe engine.
* src/cid/cidobjs.c (cid_driver_init): Ditto.
* src/type1/t1objs.c (T1_Driver_Init): Ditto.
2020-11-20 15:24:42 -05:00
Priyesh Kumar 53be1753de Fix `-Wformat' compiler warnings.
* src/*: Fix format specifiers.

* builds/unix/ftsystem.c (FT_Stream_Open): Ditto.
2020-07-28 07:33:40 +02:00
Sebastian Rasmussen 8ed5a2477e [cff, cid] Fix segfaults in case of error (#58621).
* src/cff/cffobjs.c (cff_slot_done), src/cid/cidobjs.c
(cid_slot_done): If `ft_glyphslot_init' fails to allocate
`internal', then the class' `done_slot' callback (called by
`ft_glyphslot_done') must not dereference the pointer to `internal'.
2020-06-19 10:37:50 +02:00
Werner Lemberg 1658685967 Remove redundant inclusion of `ft2build.h'.
* */*: Remove `#include <ft2build.h>' where possible.

* include/freetype/freetype.h: Remove cpp error about missing
inclusion of `ft2build.h'.
2020-06-13 21:15:45 +02:00
David Turner e13391333f Make macros for header file names optional.
We no longer have to take care of the 8.3 file name limit; this
allows us (a) to introduce longer, meaningful file names, and (b) to
avoid macro names in `#include' lines altogether since some
compilers (most notably Visual C++) doesn't support this properly.

*/*: Replace

   #include FOO_H

with

   #include <freetype/foo.h>

or something similar.  Also update the documentation.
2020-06-08 13:31:55 +02:00
David Turner 4eee13404d Remove Jamfile files from the tree.
These have not been used in a very, very long time, so better remove
them.  A corresponding patch will be submitted to the
`freetype2-demos' repository.

* src/Jamfile, src/*/Jamfile, Jamrules: Delete.
2020-05-18 17:56:06 +02:00
Werner Lemberg e5038be704 Update all copyright notices. 2020-01-19 17:05:19 +01:00
Weiyi Wu d5522ac060 * src/cid/cidload.c (cid_hex_to_binary): Fix typo (#56653). 2019-07-22 09:49:22 +02:00
Andrei Alexeyev 2f4b740ce4 Fix invalid function pointer casts.
This change should allow Freetype to work on WASM/Emscripten without
needing `-s EMULATE_FUNCTION_POINTER_CASTS=1'.

* src/autofit/afdummy.c (af_dummy_hints_apply): Fix signature.

* src/cid/cidload.c (cid_parse_font_matrix, parse_fd_array,
parse_expansion_factor, parse_font_name): Return `void', not
`FT_Error'.

* include/freetype/internal/ftobjs.h (FT_CMap_CharVarIsDefaultFunc):
Fix signature.
2019-03-07 09:43:05 +01:00