reimplment serialize_int using check_assign()

This commit is contained in:
ariza 2020-02-27 17:34:26 -08:00 committed by Ebrahim Byagowi
parent 14b134379d
commit 002f0e20c4
3 changed files with 24 additions and 25 deletions

View File

@ -252,30 +252,27 @@ struct number_t
struct UnsizedByteStr : UnsizedArrayOf <HBUINT8>
{
// encode 2-byte int (Dict/CharString) or 4-byte int (Dict)
template <typename INTTYPE, int minVal, int maxVal>
static bool serialize_int (hb_serialize_context_t *c, op_code_t intOp, int value)
template <typename T, typename V>
static bool serialize_int (hb_serialize_context_t *c, op_code_t intOp, V value)
{
TRACE_SERIALIZE (this);
if (unlikely ((value < minVal || value > maxVal)))
return_trace (false);
HBUINT8 *p = c->allocate_size<HBUINT8> (1);
if (unlikely (p == nullptr)) return_trace (false);
*p = intOp;
INTTYPE *ip = c->allocate_size<INTTYPE> (INTTYPE::static_size);
T *ip = c->allocate_size<T> (T::static_size);
if (unlikely (ip == nullptr)) return_trace (false);
*ip = (unsigned int) value;
return_trace (true);
return_trace (c->check_assign (*ip, value));
}
static bool serialize_int4 (hb_serialize_context_t *c, int value)
{ return serialize_int<HBUINT32, 0, 0x7FFFFFFF> (c, OpCode_longintdict, value); }
template <typename V>
static bool serialize_int4 (hb_serialize_context_t *c, V value)
{ return serialize_int<HBINT32> (c, OpCode_longintdict, value); }
static bool serialize_int2 (hb_serialize_context_t *c, int value)
{ return serialize_int<HBUINT16, 0, 0x7FFF> (c, OpCode_shortint, value); }
template <typename V>
static bool serialize_int2 (hb_serialize_context_t *c, V value)
{ return serialize_int<HBINT16> (c, OpCode_shortint, value); }
/* Defining null_size allows a Null object may be created. Should be safe because:
* A descendent struct Dict uses a Null pointer to indicate a missing table,

View File

@ -352,11 +352,11 @@ struct Dict : UnsizedByteStr
return size;
}
template <typename INTTYPE, int minVal, int maxVal>
static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, int value, op_code_t intOp)
template <typename T, typename V>
static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, V value, op_code_t intOp)
{
// XXX: not sure why but LLVM fails to compile the following 'unlikely' macro invocation
if (/*unlikely*/ (!serialize_int<INTTYPE, minVal, maxVal> (c, intOp, value)))
if (/*unlikely*/ (!serialize_int<T, V> (c, intOp, value)))
return false;
TRACE_SERIALIZE (this);
@ -373,17 +373,19 @@ struct Dict : UnsizedByteStr
return_trace (true);
}
static bool serialize_uint4_op (hb_serialize_context_t *c, op_code_t op, int value)
{ return serialize_int_op<HBUINT32, 0, 0x7FFFFFFF> (c, op, value, OpCode_longintdict); }
template <typename V>
static bool serialize_int4_op (hb_serialize_context_t *c, op_code_t op, V value)
{ return serialize_int_op<HBINT32> (c, op, value, OpCode_longintdict); }
static bool serialize_uint2_op (hb_serialize_context_t *c, op_code_t op, int value)
{ return serialize_int_op<HBUINT16, 0, 0x7FFF> (c, op, value, OpCode_shortint); }
template <typename V>
static bool serialize_int2_op (hb_serialize_context_t *c, op_code_t op, V value)
{ return serialize_int_op<HBINT16> (c, op, value, OpCode_shortint); }
static bool serialize_offset4_op (hb_serialize_context_t *c, op_code_t op, int value)
{ return serialize_uint4_op (c, op, value); }
static bool serialize_offset4_op (hb_serialize_context_t *c, op_code_t op, unsigned value)
{ return serialize_int4_op (c, op, value); }
static bool serialize_offset2_op (hb_serialize_context_t *c, op_code_t op, int value)
{ return serialize_uint2_op (c, op, value); }
static bool serialize_offset2_op (hb_serialize_context_t *c, op_code_t op, unsigned value)
{ return serialize_int2_op (c, op, value); }
};
struct TopDict : Dict {};

View File

@ -247,7 +247,7 @@ struct cff1_font_dict_op_serializer_t : cff_font_dict_op_serializer_t
TRACE_SERIALIZE (this);
if (opstr.op == OpCode_FontName)
return_trace (FontDict::serialize_uint2_op (c, opstr.op, mod.fontName));
return_trace (FontDict::serialize_int2_op (c, opstr.op, mod.fontName));
else
return_trace (SUPER::serialize (c, opstr, mod.privateDictInfo));
}