Apply some minor improves on CFFIndex

This commit is contained in:
Ebrahim Byagowi 2019-06-29 00:00:00 +04:30 committed by GitHub
parent ddd29e5594
commit 3f806673fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 25 deletions

View File

@ -92,10 +92,8 @@ struct CFFIndex
static unsigned int calculate_serialized_size (unsigned int offSize_, unsigned int count, static unsigned int calculate_serialized_size (unsigned int offSize_, unsigned int count,
unsigned int dataSize) unsigned int dataSize)
{ {
if (count == 0) if (count == 0) return COUNT::static_size;
return COUNT::static_size; return min_size + calculate_offset_array_size (offSize_, count) + dataSize;
else
return min_size + calculate_offset_array_size (offSize_, count) + dataSize;
} }
bool serialize (hb_serialize_context_t *c, const CFFIndex &src) bool serialize (hb_serialize_context_t *c, const CFFIndex &src)
@ -159,9 +157,7 @@ struct CFFIndex
byteArray.init (); byteArray.init ();
byteArray.resize (buffArray.length); byteArray.resize (buffArray.length);
for (unsigned int i = 0; i < byteArray.length; i++) for (unsigned int i = 0; i < byteArray.length; i++)
{
byteArray[i] = byte_str_t (buffArray[i].arrayZ, buffArray[i].length); byteArray[i] = byte_str_t (buffArray[i].arrayZ, buffArray[i].length);
}
bool result = this->serialize (c, offSize_, byteArray); bool result = this->serialize (c, offSize_, byteArray);
byteArray.fini (); byteArray.fini ();
return result; return result;
@ -192,43 +188,35 @@ struct CFFIndex
unsigned int length_at (unsigned int index) const unsigned int length_at (unsigned int index) const
{ {
if (likely ((offset_at (index + 1) >= offset_at (index)) && if (unlikely ((offset_at (index + 1) < offset_at (index)) ||
(offset_at (index + 1) <= offset_at (count)))) (offset_at (index + 1) > offset_at (count))))
return offset_at (index + 1) - offset_at (index);
else
return 0; return 0;
return offset_at (index + 1) - offset_at (index);
} }
const unsigned char *data_base () const const unsigned char *data_base () const
{ return (const unsigned char *)this + min_size + offset_array_size (); } { return (const unsigned char *) this + min_size + offset_array_size (); }
unsigned int data_size () const { return HBINT8::static_size; } unsigned int data_size () const { return HBINT8::static_size; }
byte_str_t operator [] (unsigned int index) const byte_str_t operator [] (unsigned int index) const
{ {
if (likely (index < count)) if (unlikely (index >= count)) return Null (byte_str_t);
return byte_str_t (data_base () + offset_at (index) - 1, length_at (index)); return byte_str_t (data_base () + offset_at (index) - 1, length_at (index));
else
return Null (byte_str_t);
} }
unsigned int get_size () const unsigned int get_size () const
{ {
if (this != &Null (CFFIndex)) if (this == &Null (CFFIndex)) return 0;
{ if (count > 0)
if (count > 0) return min_size + offset_array_size () + (offset_at (count) - 1);
return min_size + offset_array_size () + (offset_at (count) - 1); return count.static_size; /* empty CFFIndex contains count only */
else
return count.static_size; /* empty CFFIndex contains count only */
}
else
return 0;
} }
bool sanitize (hb_sanitize_context_t *c) const bool sanitize (hb_sanitize_context_t *c) const
{ {
TRACE_SANITIZE (this); TRACE_SANITIZE (this);
return_trace (likely ((count.sanitize (c) && count == 0) || /* empty INDEX */ return_trace (likely ((c->check_struct (this) && count == 0) || /* empty INDEX */
(c->check_struct (this) && offSize >= 1 && offSize <= 4 && (c->check_struct (this) && offSize >= 1 && offSize <= 4 &&
c->check_array (offsets, offSize, count + 1) && c->check_array (offsets, offSize, count + 1) &&
c->check_array ((const HBUINT8*) data_base (), 1, max_offset () - 1)))); c->check_array ((const HBUINT8*) data_base (), 1, max_offset () - 1))));