Start cleaning up get_size()

So we know when the size is static and when dynamic.
This commit is contained in:
Behdad Esfahbod 2010-05-06 19:33:31 -04:00
parent b157617644
commit e45d3f86f9
5 changed files with 55 additions and 44 deletions

View File

@ -47,20 +47,19 @@ struct TTCHeader;
typedef struct TableDirectory
{
static inline unsigned int get_size () { return sizeof (TableDirectory); }
inline bool sanitize (hb_sanitize_context_t *context) {
TRACE_SANITIZE ();
return context->check_struct (this);
}
DEFINE_SIZE_STATIC (16);
Tag tag; /* 4-byte identifier. */
CheckSum checkSum; /* CheckSum for this table. */
ULONG offset; /* Offset from beginning of TrueType font
* file. */
ULONG length; /* Length of this table. */
} OpenTypeTable;
ASSERT_SIZE (TableDirectory, 16);
typedef struct OffsetTable
{
@ -101,7 +100,7 @@ typedef struct OffsetTable
inline bool sanitize (hb_sanitize_context_t *context) {
TRACE_SANITIZE ();
return context->check_struct (this)
&& context->check_array (tableDir, TableDirectory::get_size (), numTables);
&& context->check_array (tableDir, TableDirectory::static_size, numTables);
}
private:

View File

@ -85,6 +85,29 @@ inline Type& StructAfter(TObject &X)
/*
* Size checking
*/
#define DEFINE_SIZE_STATIC(size) \
inline void _size_assertion (void) const \
{ ASSERT_STATIC (sizeof (*this) == (size)); } \
static inline unsigned int get_size (void) { return (size); } \
static const unsigned int static_size = (size); \
static const unsigned int min_size = (size)
#define DEFINE_SIZE_VAR(size, _var_type) \
inline void _size_assertion (void) const \
{ ASSERT_STATIC (sizeof (*this) == (size) + VAR0 * sizeof (_var_type)); } \
static const unsigned int min_size = (size)
#define DEFINE_SIZE_VAR2(_type, size, _var_type1, _var_type2) \
inline void _size_assertion (void) const \
{ ASSERT_STATIC (sizeof (*this) == (size) + VAR0 * sizeof (_var_type1) + VAR0 * sizeof (_var_type2)); } \
static const unsigned int min_size = (size)
/*
* Null objects
*/
@ -348,7 +371,6 @@ class BEInt<Type, 4>
template <typename Type>
struct IntType
{
static inline unsigned int get_size () { return sizeof (Type); }
inline void set (Type i) { v = i; }
inline operator Type(void) const { return v; }
inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
@ -357,6 +379,7 @@ struct IntType
TRACE_SANITIZE ();
return context->check_struct (this);
}
DEFINE_SIZE_STATIC (sizeof (Type));
private: BEInt<Type, sizeof (Type)> v;
};
@ -365,11 +388,6 @@ typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */
typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */
typedef IntType<int32_t> LONG; /* 32-bit signed integer. */
ASSERT_SIZE (USHORT, 2);
ASSERT_SIZE (SHORT, 2);
ASSERT_SIZE (ULONG, 4);
ASSERT_SIZE (LONG, 4);
/* Array of four uint8s (length = 32 bits) used to identify a script, language
* system, feature, or baseline */
struct Tag : ULONG
@ -397,7 +415,7 @@ struct CheckSum : ULONG
static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
{
uint32_t Sum = 0L;
ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::get_size ();
ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
while (Table < EndPtr)
Sum += *Table++;
@ -463,7 +481,7 @@ struct GenericOffsetTo : OffsetType
private:
/* Set the offset to Null */
inline bool neuter (hb_sanitize_context_t *context) {
if (context->can_edit (CharP(this), this->get_size ())) {
if (context->can_edit (CharP(this), this->static_size)) {
this->set (0); /* 0 is Null offset */
return true;
}
@ -508,7 +526,7 @@ struct GenericArrayOf
return array()[i];
}
inline unsigned int get_size () const
{ return len.get_size () + len * Type::get_size (); }
{ return len.static_size + len * Type::static_size; }
inline bool sanitize (hb_sanitize_context_t *context) {
TRACE_SANITIZE ();
@ -551,7 +569,7 @@ struct GenericArrayOf
inline bool sanitize_shallow (hb_sanitize_context_t *context) {
TRACE_SANITIZE ();
return context->check_struct (this)
&& context->check_array (this, Type::get_size (), len);
&& context->check_array (this, Type::static_size, len);
}
public:
@ -615,11 +633,11 @@ struct HeadlessArrayOf
return array()[i-1];
}
inline unsigned int get_size () const
{ return len.get_size () + (len ? len - 1 : 0) * Type::get_size (); }
{ return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
inline bool sanitize_shallow (hb_sanitize_context_t *context) {
return context->check_struct (this)
&& context->check_array (this, Type::get_size (), len);
&& context->check_array (this, Type::static_size, len);
}
inline bool sanitize (hb_sanitize_context_t *context) {

View File

@ -51,14 +51,14 @@
template <typename Type>
struct Record
{
static inline unsigned int get_size () { return sizeof (Record<Type>); }
inline bool sanitize (hb_sanitize_context_t *context, void *base) {
TRACE_SANITIZE ();
return context->check_struct (this)
&& offset.sanitize (context, base);
}
DEFINE_SIZE_STATIC (6);
Tag tag; /* 4-byte Tag identifier */
OffsetTo<Type>
offset; /* Offset from beginning of object holding
@ -350,8 +350,6 @@ struct CoverageRangeRecord
{
friend struct CoverageFormat2;
static inline unsigned int get_size () { return sizeof (CoverageRangeRecord); }
private:
inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
{
@ -366,13 +364,14 @@ struct CoverageRangeRecord
return context->check_struct (this);
}
DEFINE_SIZE_STATIC (6);
private:
GlyphID start; /* First GlyphID in the range */
GlyphID end; /* Last GlyphID in the range */
USHORT startCoverageIndex; /* Coverage Index of first GlyphID in
* range */
};
ASSERT_SIZE (CoverageRangeRecord, 6);
DEFINE_NULL_DATA (CoverageRangeRecord, 6, "\000\001");
struct CoverageFormat2
@ -472,8 +471,6 @@ struct ClassRangeRecord
{
friend struct ClassDefFormat2;
static inline unsigned int get_size () { return sizeof (ClassRangeRecord); }
private:
inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
{
@ -488,12 +485,13 @@ struct ClassRangeRecord
return context->check_struct (this);
}
DEFINE_SIZE_STATIC (6);
private:
GlyphID start; /* First GlyphID in the range */
GlyphID end; /* Last GlyphID in the range */
USHORT classValue; /* Applied to all glyphs in the range */
};
ASSERT_SIZE (ClassRangeRecord, 6);
DEFINE_NULL_DATA (ClassRangeRecord, 6, "\000\001");
struct ClassDefFormat2

View File

@ -90,7 +90,7 @@ struct ValueFormat : USHORT
inline unsigned int get_len () const
{ return _hb_popcount32 ((unsigned int) *this); }
inline unsigned int get_size () const
{ return get_len () * Value::get_size (); }
{ return get_len () * Value::static_size; }
void apply_value (hb_ot_layout_context_t *layout,
const char *base,
@ -349,7 +349,7 @@ struct AnchorMatrix
if (!context->check_struct (this)) return false;
if (unlikely (cols >= ((unsigned int) -1) / rows)) return false;
unsigned int count = rows * cols;
if (!context->check_array (matrix, matrix[0].get_size (), count)) return false;
if (!context->check_array (matrix, matrix[0].static_size, count)) return false;
for (unsigned int i = 0; i < count; i++)
if (!matrix[i].sanitize (context, this)) return false;
return true;
@ -368,21 +368,19 @@ struct MarkRecord
{
friend struct MarkArray;
static inline unsigned int get_size () { return sizeof (MarkRecord); }
inline bool sanitize (hb_sanitize_context_t *context, void *base) {
TRACE_SANITIZE ();
return context->check_struct (this)
&& markAnchor.sanitize (context, base);
}
DEFINE_SIZE_STATIC (4);
private:
USHORT klass; /* Class defined for this mark */
OffsetTo<Anchor>
markAnchor; /* Offset to Anchor table--from
* beginning of MarkArray table */
};
ASSERT_SIZE (MarkRecord, 4);
struct MarkArray
{
@ -565,7 +563,7 @@ struct PairSet
TRACE_SANITIZE ();
if (!context->check_struct (this)) return false;
unsigned int count = (1 + format_len) * len;
return context->check_array (array, USHORT::get_size (), count);
return context->check_array (array, USHORT::static_size, count);
}
private:
@ -602,7 +600,7 @@ struct PairPosFormat1
unsigned int len1 = valueFormat1.get_len ();
unsigned int len2 = valueFormat2.get_len ();
unsigned int record_size = USHORT::get_size () * (1 + len1 + len2);
unsigned int record_size = USHORT::static_size * (1 + len1 + len2);
const PairSet &pair_set = this+pairSet[index];
unsigned int count = pair_set.len;
@ -795,14 +793,14 @@ struct PairPos
struct EntryExitRecord
{
static inline unsigned int get_size () { return sizeof (EntryExitRecord); }
inline bool sanitize (hb_sanitize_context_t *context, void *base) {
TRACE_SANITIZE ();
return entryAnchor.sanitize (context, base)
&& exitAnchor.sanitize (context, base);
}
DEFINE_SIZE_STATIC (4);
OffsetTo<Anchor>
entryAnchor; /* Offset to EntryAnchor table--from
* beginning of CursivePos
@ -812,7 +810,6 @@ struct EntryExitRecord
* beginning of CursivePos
* subtable--may be NULL */
};
ASSERT_SIZE (EntryExitRecord, 4);
struct CursivePosFormat1
{

View File

@ -172,19 +172,18 @@ static inline bool match_lookahead (hb_apply_context_t *context,
struct LookupRecord
{
static inline unsigned int get_size () { return sizeof (LookupRecord); }
inline bool sanitize (hb_sanitize_context_t *context) {
TRACE_SANITIZE ();
return context->check_struct (this);
}
DEFINE_SIZE_STATIC (4);
USHORT sequenceIndex; /* Index into current glyph
* sequence--first glyph = 0 */
USHORT lookupListIndex; /* Lookup to apply to that
* position--zero--based */
};
ASSERT_SIZE (LookupRecord, 4);
static inline bool apply_lookup (hb_apply_context_t *context,
unsigned int count, /* Including the first glyph */
@ -277,7 +276,7 @@ struct Rule
inline bool apply (hb_apply_context_t *context, ContextLookupContext &lookup_context) const
{
TRACE_APPLY ();
const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].get_size () * (inputCount ? inputCount - 1 : 0));
const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (input, input[0].static_size * (inputCount ? inputCount - 1 : 0));
return context_lookup (context,
inputCount, input,
lookupCount, lookupRecord,
@ -290,8 +289,8 @@ struct Rule
return inputCount.sanitize (context)
&& lookupCount.sanitize (context)
&& context->check_range (input,
input[0].get_size () * inputCount
+ lookupRecordX[0].get_size () * lookupCount);
input[0].static_size * inputCount
+ lookupRecordX[0].static_size * lookupCount);
}
private:
@ -430,7 +429,7 @@ struct ContextFormat3
if (likely (index == NOT_COVERED))
return false;
const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].get_size () * glyphCount);
const LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * glyphCount);
struct ContextLookupContext lookup_context = {
{match_coverage, apply_func},
CharP(this)
@ -445,11 +444,11 @@ struct ContextFormat3
TRACE_SANITIZE ();
if (!context->check_struct (this)) return false;
unsigned int count = glyphCount;
if (!context->check_array (coverage, OffsetTo<Coverage>::get_size (), count)) return false;
if (!context->check_array (coverage, coverage[0].static_size, count)) return false;
for (unsigned int i = 0; i < count; i++)
if (!coverage[i].sanitize (context, this)) return false;
LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, OffsetTo<Coverage>::get_size () * count);
return context->check_array (lookupRecord, LookupRecord::get_size (), lookupCount);
LookupRecord *lookupRecord = &StructAtOffset<LookupRecord> (coverage, coverage[0].static_size * count);
return context->check_array (lookupRecord, lookupRecord[0].static_size, lookupCount);
}
private: