minor refactor of CDR-to-key/keyhash generation

Signed-off-by: Erik Boasson <eb@ilities.com>
This commit is contained in:
Erik Boasson 2018-10-29 13:24:07 +08:00
parent 259e4676dc
commit 7cb80e7851
3 changed files with 22 additions and 36 deletions

View file

@ -37,7 +37,7 @@ void dds_stream_from_serdata_default (dds_stream_t * s, const struct ddsi_serdat
void dds_stream_add_to_serdata_default (dds_stream_t * s, struct ddsi_serdata_default **d);
void dds_stream_write_key (dds_stream_t * os, const char * sample, const struct ddsi_sertopic_default * topic);
void dds_stream_read_sample_write_key (dds_stream_t *os, dds_stream_t *is, const struct ddsi_sertopic_default *topic);
uint32_t dds_stream_extract_key (dds_stream_t *is, dds_stream_t *os, const uint32_t *ops, const bool just_key);
void dds_stream_read_key
(
dds_stream_t * is,

View file

@ -1259,13 +1259,7 @@ void dds_stream_write_key (dds_stream_t * os, const char * sample, const struct
of key hash. Input stream may contain full sample of just key data.
*/
static uint32_t dds_stream_get_keyhash
(
dds_stream_t * is,
dds_stream_t * os,
const uint32_t * ops,
const bool just_key
)
uint32_t dds_stream_extract_key (dds_stream_t *is, dds_stream_t *os, const uint32_t *ops, const bool just_key)
{
uint32_t align;
uint32_t op;
@ -1404,7 +1398,7 @@ static uint32_t dds_stream_get_keyhash
const uint32_t jmp = DDS_OP_ADR_JMP (ops[1]);
while (num--)
{
dds_stream_get_keyhash (is, NULL, jsr_ops, just_key);
dds_stream_extract_key (is, NULL, jsr_ops, just_key);
}
ops += jmp ? (jmp - 2) : 2;
break;
@ -1464,7 +1458,7 @@ static uint32_t dds_stream_get_keyhash
const uint32_t jmp = DDS_OP_ADR_JMP (*ops);
while (num--)
{
dds_stream_get_keyhash (is, NULL, jsr_ops, just_key);
dds_stream_extract_key (is, NULL, jsr_ops, just_key);
}
ops += jmp ? (jmp - 3) : 2;
break;
@ -1540,7 +1534,7 @@ static uint32_t dds_stream_get_keyhash
}
default:
{
dds_stream_get_keyhash (is, NULL, jeq_op + DDS_OP_ADR_JSR (jeq_op[0]), just_key);
dds_stream_extract_key (is, NULL, jeq_op + DDS_OP_ADR_JSR (jeq_op[0]), just_key);
break;
}
}
@ -1561,7 +1555,7 @@ static uint32_t dds_stream_get_keyhash
}
case DDS_OP_JSR: /* Implies nested type */
{
dds_stream_get_keyhash (is, os, ops + DDS_OP_JUMP (op), just_key);
dds_stream_extract_key (is, os, ops + DDS_OP_JUMP (op), just_key);
ops++;
break;
}
@ -1571,25 +1565,6 @@ static uint32_t dds_stream_get_keyhash
return os->m_index - origin;
}
void dds_stream_read_sample_write_key (dds_stream_t *os, dds_stream_t *is, const struct ddsi_sertopic_default *topic)
{
const struct dds_topic_descriptor *desc = (const struct dds_topic_descriptor *) topic->type;
uint32_t nbytes;
os->m_endian = 0;
if (os->m_size < is->m_size)
{
os->m_buffer.p8 = dds_realloc (os->m_buffer.p8, is->m_size);
os->m_size = is->m_size;
}
nbytes = dds_stream_get_keyhash (is, os, desc->m_ops, false);
os->m_index += nbytes;
if (os->m_index < os->m_size)
{
os->m_buffer.p8 = dds_realloc (os->m_buffer.p8, os->m_index);
os->m_size = os->m_index;
}
}
#ifndef NDEBUG
static bool keyhash_is_reset(const dds_key_hash_t *kh)
{
@ -1619,7 +1594,7 @@ void dds_stream_read_keyhash
os.m_buffer.pv = kh->m_hash;
os.m_size = 16;
os.m_endian = 0;
ncheck = dds_stream_get_keyhash (is, &os, desc->m_ops, just_key);
ncheck = dds_stream_extract_key (is, &os, desc->m_ops, just_key);
assert(ncheck <= 16);
(void)ncheck;
}
@ -1630,7 +1605,7 @@ void dds_stream_read_keyhash
kh->m_iskey = 0;
dds_stream_init (&os, 0);
os.m_endian = 0;
dds_stream_get_keyhash (is, &os, desc->m_ops, just_key);
dds_stream_extract_key (is, &os, desc->m_ops, just_key);
md5_init (&md5st);
md5_append (&md5st, os.m_buffer.p8, os.m_index);
md5_finish (&md5st, (unsigned char *) kh->m_hash);

View file

@ -411,15 +411,26 @@ static struct ddsi_serdata *serdata_default_to_topicless (const struct ddsi_serd
d_tl->hdr.identifier = d->hdr.identifier;
serdata_default_append_blob (&d_tl, 1, d->pos, d->data);
}
else if (d->keyhash.m_iskey)
{
d_tl->hdr.identifier = CDR_BE;
serdata_default_append_blob (&d_tl, 1, sizeof (d->keyhash.m_hash), d->keyhash.m_hash);
}
else
{
/* One big hack ... read_sample_write_key goes via keyhash generation ... */
const struct dds_topic_descriptor *desc = tp->type;
dds_stream_t is, os;
uint32_t nbytes;
dds_stream_from_serdata_default (&is, d);
dds_stream_from_serdata_default (&os, d_tl);
dds_stream_read_sample_write_key (&os, &is, tp);
nbytes = dds_stream_extract_key (&is, &os, desc->m_ops, false);
os.m_index += nbytes;
if (os.m_index < os.m_size)
{
os.m_buffer.p8 = dds_realloc (os.m_buffer.p8, os.m_index);
os.m_size = os.m_index;
}
dds_stream_add_to_serdata_default (&os, &d_tl);
d_tl->hdr.identifier = os.m_endian ? CDR_LE : CDR_BE;
}
}
return (struct ddsi_serdata *)d_tl;