Serialize invalid buffer to !! (text) or [] (json)

There is no generic deserialize - you have to choose glyphs or unicode - so there is no way to deserialize this buffer.
This commit is contained in:
Simon Cozens 2020-09-25 21:24:41 +01:00 committed by Behdad Esfahbod
parent 5bb88c4f45
commit 7c0bc0bb92
2 changed files with 61 additions and 8 deletions

View File

@ -592,6 +592,35 @@ hb_buffer_serialize_unicode (hb_buffer_t *buffer,
}
}
static unsigned int
_hb_buffer_serialize_invalid (hb_buffer_t *buffer,
unsigned int start,
unsigned int end,
char *buf,
unsigned int buf_size,
unsigned int *buf_consumed,
hb_buffer_serialize_format_t format,
hb_buffer_serialize_flags_t flags)
{
unsigned int sconsumed;
if (!buf_consumed)
buf_consumed = &sconsumed;
if (buf_size < 3)
return 0;
if (format == HB_BUFFER_SERIALIZE_FORMAT_JSON) {
*buf++ = '[';
*buf++ = ']';
*buf = '\0';
} else if (format == HB_BUFFER_SERIALIZE_FORMAT_TEXT) {
*buf++ = '!';
*buf++ = '!';
*buf = '\0';
}
*buf_consumed = 2;
return 0;
}
/**
* hb_buffer_serialize:
* @buffer: an #hb_buffer_t buffer.
@ -629,12 +658,22 @@ hb_buffer_serialize (hb_buffer_t *buffer,
hb_buffer_serialize_format_t format,
hb_buffer_serialize_flags_t flags)
{
if (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS)
return hb_buffer_serialize_glyphs(buffer, start, end, buf, buf_size,
buf_consumed, font, format, flags);
else
return hb_buffer_serialize_unicode(buffer, start, end, buf, buf_size,
switch (buffer->content_type)
{
case HB_BUFFER_CONTENT_TYPE_GLYPHS:
return hb_buffer_serialize_glyphs(buffer, start, end, buf, buf_size,
buf_consumed, font, format, flags);
case HB_BUFFER_CONTENT_TYPE_UNICODE:
return hb_buffer_serialize_unicode(buffer, start, end, buf, buf_size,
buf_consumed, format, flags);
case HB_BUFFER_CONTENT_TYPE_INVALID:
default:
return _hb_buffer_serialize_invalid(buffer, start, end, buf, buf_size,
buf_consumed, format, flags);
}
}
static bool

View File

@ -898,14 +898,28 @@ test_buffer_serialize_deserialize (void)
num_glyphs = hb_buffer_get_length (b);
g_assert_cmpint (num_glyphs, ==, test->num_items);
hb_buffer_serialize_unicode(b, 0, num_glyphs, round_trip,
sizeof(round_trip), &consumed, test->format,
HB_BUFFER_SERIALIZE_FLAG_DEFAULT);
hb_buffer_serialize_unicode (b, 0, num_glyphs, round_trip,
sizeof(round_trip), &consumed, test->format,
HB_BUFFER_SERIALIZE_FLAG_DEFAULT);
g_assert_cmpstr (round_trip, ==, test->contents);
hb_buffer_destroy (b);
}
char test[1024];
unsigned int num_glyphs, consumed;
hb_buffer_t *indeterminate = hb_buffer_get_empty ();
hb_buffer_serialize (indeterminate, 0, hb_buffer_get_length (indeterminate),
test, sizeof(test), &consumed, NULL, HB_BUFFER_SERIALIZE_FORMAT_JSON,
HB_BUFFER_SERIALIZE_FLAG_DEFAULT);
g_assert_cmpstr ( test, ==, "[]");
hb_buffer_serialize (indeterminate, 0, hb_buffer_get_length (indeterminate),
test, sizeof(test), &consumed, NULL, HB_BUFFER_SERIALIZE_FORMAT_TEXT,
HB_BUFFER_SERIALIZE_FLAG_DEFAULT);
g_assert_cmpstr ( test, ==, "!!");
}
int