text
stringlengths
0
357
{
uint32_t last_chat_id = 0, matches = 0, mismatches = 0;
while (sqlite3_step(stmt)==SQLITE_ROW)
{
uint32_t chat_id = sqlite3_column_int(stmt, 0);
uint32_t contact_id = sqlite3_column_int(stmt, 1);
if (chat_id!=last_chat_id) {
if (matches==dc_array_get_cnt(contact_ids) && mismatches==0) {
dc_array_add_id(chat_ids, last_chat_id);
}
last_chat_id = chat_id;
matches = 0;
mismatches = 0;
}
if (contact_id==dc_array_get_id(contact_ids, matches)) {
matches++;
}
else {
mismatches++;
}
}
if (matches==dc_array_get_cnt(contact_ids) && mismatches==0) {
dc_array_add_id(chat_ids, last_chat_id);
}
}
cleanup:
sqlite3_finalize(stmt);
free(contact_ids_str);
dc_array_unref(contact_ids);
sqlite3_free(q3);
return chat_ids;
}
static char* create_adhoc_grp_id(dc_context_t* context, dc_array_t* member_ids /*including SELF*/)
{
/* algorithm:
- sort normalized, lowercased, e-mail addresses alphabetically
- put all e-mail addresses into a single string, separate the addresss by a single comma
- sha-256 this string (without possibly terminating null-characters)
- encode the first 64 bits of the sha-256 output as lowercase hex (results in 16 characters from the set [0-9a-f])
*/
dc_array_t* member_addrs = dc_array_new(context, 23);
char* member_ids_str = dc_array_get_string(member_ids, ",");
sqlite3_stmt* stmt = NULL;
char* q3 = NULL;
char* addr = NULL;
int i = 0;
int iCnt = 0;
char* ret = NULL;
dc_strbuilder_t member_cs;
dc_strbuilder_init(&member_cs, 0);
/* collect all addresses and sort them */
q3 = sqlite3_mprintf("SELECT addr FROM contacts WHERE id IN(%s) AND id!=" DC_STRINGIFY(DC_CONTACT_ID_SELF), member_ids_str);
stmt = dc_sqlite3_prepare(context->sql, q3);
addr = dc_sqlite3_get_config(context->sql, "configured_addr", "no-self");
dc_strlower_in_place(addr);
dc_array_add_ptr(member_addrs, addr);
while (sqlite3_step(stmt)==SQLITE_ROW) {
addr = dc_strdup((const char*)sqlite3_column_text(stmt, 0));
dc_strlower_in_place(addr);
dc_array_add_ptr(member_addrs, addr);
}
dc_array_sort_strings(member_addrs);
/* build a single, comma-separated (cs) string from all addresses */
iCnt = dc_array_get_cnt(member_addrs);
for (i = 0; i < iCnt; i++) {
if (i) { dc_strbuilder_cat(&member_cs, ","); }
dc_strbuilder_cat(&member_cs, (const char*)dc_array_get_ptr(member_addrs, i));
}
/* make sha-256 from the string */
#ifdef DC_USE_RPGP
rpgp_cvec* binary_hash = rpgp_hash_sha256((const uint8_t*)member_cs.buf, strlen(member_cs.buf));
if (binary_hash==NULL) {
goto cleanup;
}
ret = calloc(1, 256);
if (ret==NULL) {
goto cleanup;
}
for (i = 0; i < 8; i++) {
sprintf(&ret[i*2], "%02x", (int)rpgp_cvec_data(binary_hash)[i]);
}
rpgp_cvec_drop(binary_hash);
#else
uint8_t* binary_hash = NULL;
{
pgp_hash_t hasher;
pgp_hash_sha256(&hasher);