[dfont] Solve the mystery +2 offset thing! Previously, ResourceForkHeader was defined as 30 bytes, having the typeCountM1 as last member. There was a mysterious offset-by-2 in the code, derived from FontTools and JDK code this was ported from. In testing, I observed that typeListZ offset is actually 28. Suggesting that the typeCountM1 does NOT actually belong to ResourceForkHeader, but belongs to the array itself. Adjusting for that resolves the mystery +2 offset hack, so everything is clean and good now. This, concludes my dfont hacking. The code looks great now, and I'm happy to leave it. Fuzzers might disagree though, we will see!
diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index 6807973..4f16c7d 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh
@@ -628,6 +628,50 @@ DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ); }; +/* An array storing length-1. */ +template <typename Type, typename LenType=HBUINT16> +struct ArrayOfM1 +{ + inline const Type& operator [] (unsigned int i) const + { + if (unlikely (i > lenM1)) return Null(Type); + return arrayZ[i]; + } + inline Type& operator [] (unsigned int i) + { + if (unlikely (i > lenM1)) return Crap(Type); + return arrayZ[i]; + } + inline unsigned int get_size (void) const + { return lenM1.static_size + (lenM1 + 1) * Type::static_size; } + + template <typename T> + inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const + { + TRACE_SANITIZE (this); + if (unlikely (!sanitize_shallow (c))) return_trace (false); + unsigned int count = lenM1 + 1; + for (unsigned int i = 0; i < count; i++) + if (unlikely (!arrayZ[i].sanitize (c, base, user_data))) + return_trace (false); + return_trace (true); + } + + private: + inline bool sanitize_shallow (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (lenM1.sanitize (c) && + (c->check_array (arrayZ, lenM1 + 1))); + } + + public: + LenType lenM1; + Type arrayZ[VAR]; + public: + DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ); +}; + /* An array with sorted elements. Supports binary searching. */ template <typename Type, typename LenType=HBUINT16> struct SortedArrayOf : ArrayOf<Type, LenType>