text
stringlengths
0
357
static char* encode_66bits_as_base64(uint32_t v1, uint32_t v2, uint32_t fill /*only the lower 2 bits are used*/)
{
/* encode 66 bits as a base64 string. This is useful for ID generating with short strings as
we save 5 character in each id compared to 64 bit hex encoding, for a typical group ID, these are 10 characters (grpid+msgid):
hex: 64 bit, 4 bits/character, length = 64/4 = 16 characters
base64: 64 bit, 6 bits/character, length = 64/6 = 11 characters (plus 2 additional bits) */
char* ret = malloc(12); if (ret==NULL) { exit(34); }
static const char chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
ret[ 0] = chars[ (v1>>26) & 0x3F ];
ret[ 1] = chars[ (v1>>20) & 0x3F ];
ret[ 2] = chars[ (v1>>14) & 0x3F ];
ret[ 3] = chars[ (v1>> 8) & 0x3F ];
ret[ 4] = chars[ (v1>> 2) & 0x3F ];
ret[ 5] = chars[ ( (v1<< 4) & 0x30) | ( (v2>>28) & 0x0F) ];
ret[ 6] = chars[ (v2>>22) & 0x3F ];
ret[ 7] = chars[ (v2>>16) & 0x3F ];
ret[ 8] = chars[ (v2>>10) & 0x3F ];
ret[ 9] = chars[ (v2>> 4) & 0x3F ];
ret[10] = chars[ ( (v2<< 2) & 0x3C) | (fill & 0x03) ];
ret[11] = 0;
return ret;
}
char* dc_create_id(void)
{
/* generate an id. the generated ID should be as short and as unique as possible:
- short, because it may also used as part of Message-ID headers or in QR codes
- unique as two IDs generated on two devices should not be the same. However, collisions are not world-wide but only by the few contacts.
IDs generated by this function are 66 bit wide and are returned as 11 base64 characters.
If possible, RNG of OpenSSL is used.
Additional information when used as a message-id or group-id:
- for OUTGOING messages this ID is written to the header as `Chat-Group-ID:` and is added to the message ID as Gr.<grpid>.<random>@<random>
- for INCOMING messages, the ID is taken from the Chat-Group-ID-header or from the Message-ID in the In-Reply-To: or References:-Header
- the group-id should be a string with the characters [a-zA-Z0-9\-_] */
uint32_t buf[3];
if (!RAND_bytes((unsigned char*)&buf, sizeof(uint32_t)*3)) {
RAND_pseudo_bytes((unsigned char*)&buf, sizeof(uint32_t)*3);
}
return encode_66bits_as_base64(buf[0], buf[1], buf[2]/*only the lower 2 bits are taken from this value*/);
}
char* dc_create_outgoing_rfc724_mid(const char* grpid, const char* from_addr)
{
/* Function generates a Message-ID that can be used for a new outgoing message.
- this function is called for all outgoing messages.
- the message ID should be globally unique
- do not add a counter or any private data as as this may give unneeded information to the receiver */
char* rand1 = NULL;
char* rand2 = dc_create_id();
char* ret = NULL;
const char* at_hostname = strchr(from_addr, '@');
if (at_hostname==NULL) {
at_hostname = "@nohost";
}
if (grpid) {
ret = dc_mprintf("Gr.%s.%s%s", grpid, rand2, at_hostname);
/* ^^^ `Gr.` must never change as this is used to identify group messages in normal-clients-replies. The dot is choosen as this is normally not used for random ID creation. */
}
else {
rand1 = dc_create_id();
ret = dc_mprintf("Mr.%s.%s%s", rand1, rand2, at_hostname);
/* ^^^ `Mr.` is currently not used, however, this may change in future */
}
free(rand1);
free(rand2);
return ret;
}
char* dc_create_incoming_rfc724_mid(time_t message_timestamp, uint32_t contact_id_from, dc_array_t* contact_ids_to)
{
/* Function generates a Message-ID for incoming messages that lacks one.
- normally, this function is not needed as incoming messages already have an ID
- the generated ID is only for internal use; it should be database-unique
- when fetching the same message again, this function should generate the same Message-ID
*/
if (contact_ids_to==NULL || dc_array_get_cnt(contact_ids_to)==0) {
return NULL;
}
/* find out the largest receiver ID (we could also take the smallest, but it should be unique) */
size_t i = 0;
size_t icnt = dc_array_get_cnt(contact_ids_to);
uint32_t largest_id_to = 0;
for (i = 0; i < icnt; i++) {
uint32_t cur_id = dc_array_get_id(contact_ids_to, i);
if (cur_id > largest_id_to) {
largest_id_to = cur_id;
}
}