void read_secret_chat_file (void) { if (binlog_enabled) { return; } int fd = open (get_secret_chat_filename (), O_CREAT | O_RDWR, 0600); if (fd < 0) { return; } int x[2]; if (read (fd, x, 8) < 8) { close (fd); return; } if (x[0] != (int)SECRET_CHAT_FILE_MAGIC) { close (fd); return; } int version = x[1]; assert (version >= 0); int cc; assert (read (fd, &cc, 4) == 4); int i; for (i = 0; i < cc; i++) { peer_t *P = talloc0 (sizeof (*P)); struct secret_chat *E = &P->encr_chat; int t; assert (read (fd, &t, 4) == 4); P->id = MK_ENCR_CHAT (t); assert (read (fd, &P->flags, 4) == 4); assert (read (fd, &t, 4) == 4); assert (t > 0); P->print_name = talloc (t + 1); assert (read (fd, P->print_name, t) == t); P->print_name[t] = 0; peer_insert_name (P); assert (read (fd, &E->state, 4) == 4); assert (read (fd, &E->user_id, 4) == 4); assert (read (fd, &E->admin_id, 4) == 4); assert (read (fd, &E->ttl, 4) == 4); assert (read (fd, &E->access_hash, 8) == 8); if (E->state != sc_waiting) { E->g_key = talloc (256); assert (read (fd, E->g_key, 256) == 256); E->nonce = talloc (256); assert (read (fd, E->nonce, 256) == 256); } assert (read (fd, E->key, 256) == 256); assert (read (fd, &E->key_fingerprint, 8) == 8); insert_encrypted_chat (P); } if (version >= 1) { assert (read (fd, &encr_root, 4) == 4); if (encr_root) { assert (read (fd, &encr_param_version, 4) == 4); encr_prime = talloc (256); assert (read (fd, encr_prime, 256) == 256); } } close (fd); }
void write_secret_chat_file (void) { if (binlog_enabled) { return; } int fd = open (get_secret_chat_filename (), O_CREAT | O_RDWR, 0600); if (fd < 0) { return; } int x[2]; x[0] = SECRET_CHAT_FILE_MAGIC; x[1] = 1; assert (write (fd, x, 8) == 8); int i; int cc = 0; for (i = 0; i < peer_num; i++) if (get_peer_type (Peers[i]->id) == PEER_ENCR_CHAT) { if (Peers[i]->encr_chat.state != sc_none && Peers[i]->encr_chat.state != sc_deleted) { cc ++; } } assert (write (fd, &cc, 4) == 4); for (i = 0; i < peer_num; i++) if (get_peer_type (Peers[i]->id) == PEER_ENCR_CHAT) { if (Peers[i]->encr_chat.state != sc_none && Peers[i]->encr_chat.state != sc_deleted) { int t = get_peer_id (Peers[i]->id); assert (write (fd, &t, 4) == 4); t = Peers[i]->flags; assert (write (fd, &t, 4) == 4); t = strlen (Peers[i]->print_name); assert (write (fd, &t, 4) == 4); assert (write (fd, Peers[i]->print_name, t) == t); assert (write (fd, &Peers[i]->encr_chat.state, 4) == 4); assert (write (fd, &Peers[i]->encr_chat.user_id, 4) == 4); assert (write (fd, &Peers[i]->encr_chat.admin_id, 4) == 4); assert (write (fd, &Peers[i]->encr_chat.ttl, 4) == 4); assert (write (fd, &Peers[i]->encr_chat.access_hash, 8) == 8); if (Peers[i]->encr_chat.state != sc_waiting) { assert (write (fd, Peers[i]->encr_chat.g_key, 256) == 256); } if (Peers[i]->encr_chat.state != sc_waiting) { assert (write (fd, Peers[i]->encr_chat.nonce, 256) == 256); } assert (write (fd, Peers[i]->encr_chat.key, 256) == 256); assert (write (fd, &Peers[i]->encr_chat.key_fingerprint, 8) == 8); } } assert (write (fd, &encr_root, 4) == 4); if (encr_root) { assert (write (fd, &encr_param_version, 4) == 4); assert (write (fd, encr_prime, 256) == 256); } close (fd); }
void read_secret_chat_file (void) { if (binlog_enabled) { return; } int secret_chat_fd = open (get_secret_chat_filename (), O_RDWR, 0600); if (secret_chat_fd < 0) { return; } //assert (secret_chat_fd >= 0); int x; if (read (secret_chat_fd, &x, 4) < 4) { close (secret_chat_fd); return; } if (x != SECRET_CHAT_FILE_MAGIC) { close (secret_chat_fd); return; } int v = 0; assert (read (secret_chat_fd, &v, 4) == 4); assert (v == 0 || v == 1 || v == 2); // version assert (read (secret_chat_fd, &x, 4) == 4); assert (x >= 0); while (x --> 0) { read_secret_chat (secret_chat_fd, v); } close (secret_chat_fd); }
void write_secret_chat_file (void) { if (binlog_enabled) { return; } int secret_chat_fd = open (get_secret_chat_filename (), O_CREAT | O_RDWR, 0600); assert (secret_chat_fd >= 0); int x = SECRET_CHAT_FILE_MAGIC; assert (write (secret_chat_fd, &x, 4) == 4); x = 2; assert (write (secret_chat_fd, &x, 4) == 4); // version assert (write (secret_chat_fd, &x, 4) == 4); // num int y[2]; y[0] = secret_chat_fd; y[1] = 0; tgl_peer_iterator_ex (TLS, write_secret_chat, y); lseek (secret_chat_fd, 8, SEEK_SET); assert (write (secret_chat_fd, &y[1], 4) == 4); close (secret_chat_fd); }