To calculate an ID, take the utf-8 message, convert it to utf-16, and then throw away the high bytes. Of course.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3834 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-02-11 02:44:27 +00:00
parent d4135f7133
commit 50bef73200
5 changed files with 63 additions and 10 deletions

View File

@ -512,6 +512,25 @@ static const uint8_t* _utf8_to_cp(const uint8_t* ch, uint32_t* out_cp)
return ch + actual_len;
}
static uint32_t _cp_to_utf16(uint32_t cp, uint16_t* out_h, uint16_t* out_l)
{
if (cp < 0x10000)
{
*out_h = 0;
*out_l = cp & 0xffff;
return cp;
}
else
{
uint32_t t = cp - 0x10000;
uint32_t h = ((t << 12) >> 22) + 0xd800;
uint32_t l = ((t << 22) >> 22) + 0xdc00;
*out_h = h & 0xffff;
*out_l = l & 0xffff;
return (h << 16) | (l & 0xffff);
}
}
void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_id, size_t out_id_size)
{
JSValue idval = JS_JSONStringify(context, message, JS_NULL, JS_NewInt32(context, 2));
@ -525,7 +544,17 @@ void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_
{
uint32_t cp = 0;
p = _utf8_to_cp(p, &cp);
*write_pos++ = (cp & 0xff);
uint16_t h = 0;
uint16_t l = 0;
_cp_to_utf16(cp, &h, &l);
if (h)
{
*write_pos++ = h & 0xff;
}
if (l)
{
*write_pos++ = l & 0xff;
}
}
size_t latin1_len = write_pos - (uint8_t*)latin1;
*write_pos++ = '\0';