Description: Use system's sqlcipher shared lib (rather then an old bundled copy).
Author: Mike Gabriel <mike.gabriel@das-netzwerkeam.de>
Forwarded: not needed, Debian-specific

Index: td/CMakeLists.txt
===================================================================
--- td.orig/CMakeLists.txt
+++ td/CMakeLists.txt
@@ -222,8 +222,7 @@ add_subdirectory(tdactor)
 set(TDNET_ENABLE_INSTALL ${TD_INSTALL_STATIC_LIBRARIES} CACHE BOOL "" FORCE)
 add_subdirectory(tdnet)
 
-set(TDSQLITE_ENABLE_INSTALL ${TD_INSTALL_STATIC_LIBRARIES} CACHE BOOL "" FORCE)
-add_subdirectory(sqlite)
+find_package(sqlcipher)
 
 set(TDDB_ENABLE_INSTALL ${TD_INSTALL_STATIC_LIBRARIES} CACHE BOOL "" FORCE)
 add_subdirectory(tddb)
@@ -1453,7 +1452,6 @@ if (TD_INSTALL_STATIC_LIBRARIES)
   generate_pkgconfig(tdactor "Telegram Library - Actor")
   generate_pkgconfig(tde2e "Telegram Library - E2E")
   generate_pkgconfig(tdnet "Telegram Library - Net")
-  generate_pkgconfig(tdsqlite "Telegram Library - SQLite")
   generate_pkgconfig(tddb "Telegram Library - Database")
   if (MEMPROF)
     # generate_pkgconfig(memprof "memprof - simple library for memory usage profiling")
Index: td/tddb/CMakeLists.txt
===================================================================
--- td.orig/tddb/CMakeLists.txt
+++ td/tddb/CMakeLists.txt
@@ -50,7 +50,7 @@ set(TDDB_SOURCE
 
 add_library(tddb STATIC ${TDDB_SOURCE})
 target_include_directories(tddb PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
-target_link_libraries(tddb PUBLIC tdactor tdutils PRIVATE tdsqlite)
+target_link_libraries(tddb PUBLIC tdactor tdutils PRIVATE sqlcipher)
 
 if (NOT CMAKE_CROSSCOMPILING)
   add_executable(binlog_dump td/db/binlog/binlog_dump.cpp)
Index: td/tddb/td/db/SqliteDb.cpp
===================================================================
--- td.orig/tddb/td/db/SqliteDb.cpp
+++ td/tddb/td/db/SqliteDb.cpp
@@ -16,7 +16,7 @@
 #include "td/utils/StringBuilder.h"
 #include "td/utils/Timer.h"
 
-#include "sqlite/sqlite3.h"
+#include <sqlcipher/sqlite3.h>
 
 namespace td {
 
@@ -84,16 +84,16 @@ Status SqliteDb::init(CSlice path, bool
     TRY_STATUS(destroy(path));
   }
 
-  tdsqlite3 *db;
-  CHECK(tdsqlite3_threadsafe() != 0);
+  sqlite3 *db;
+  CHECK(sqlite3_threadsafe() != 0);
   int rc =
-      tdsqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | (allow_creation ? SQLITE_OPEN_CREATE : 0), nullptr);
+      sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | (allow_creation ? SQLITE_OPEN_CREATE : 0), nullptr);
   if (rc != SQLITE_OK) {
     auto res = detail::RawSqliteDb::last_error(db, path);
-    tdsqlite3_close(db);
+    sqlite3_close(db);
     return res;
   }
-  tdsqlite3_busy_timeout(db, 1000 * 5 /* 5 seconds */);
+  sqlite3_busy_timeout(db, 1000 * 5 /* 5 seconds */);
   raw_ = std::make_shared<detail::RawSqliteDb>(db, path.str());
   return Status::OK();
 }
@@ -108,14 +108,14 @@ static int trace_v2_callback(unsigned co
   if (x[0] == '-' && x[1] == '-') {
     trace_callback(ctx, x);
   } else {
-    trace_callback(ctx, tdsqlite3_expanded_sql(static_cast<tdsqlite3_stmt *>(p_raw)));
+    trace_callback(ctx, sqlite3_expanded_sql(static_cast<sqlite3_stmt *>(p_raw)));
   }
 
   return 0;
 }
 
 void SqliteDb::trace(bool flag) {
-  tdsqlite3_trace_v2(raw_->db(), SQLITE_TRACE_STMT, flag ? trace_v2_callback : nullptr, nullptr);
+  sqlite3_trace_v2(raw_->db(), SQLITE_TRACE_STMT, flag ? trace_v2_callback : nullptr, nullptr);
 }
 
 Status SqliteDb::exec(CSlice cmd) {
@@ -124,7 +124,7 @@ Status SqliteDb::exec(CSlice cmd) {
   if (enable_logging_) {
     VLOG(sqlite) << "Start exec " << tag("query", cmd) << tag("database", raw_->db());
   }
-  auto rc = tdsqlite3_exec(raw_->db(), cmd.c_str(), nullptr, nullptr, &msg);
+  auto rc = sqlite3_exec(raw_->db(), cmd.c_str(), nullptr, nullptr, &msg);
   if (rc != SQLITE_OK) {
     CHECK(msg != nullptr);
     if (enable_logging_) {
@@ -310,9 +310,9 @@ Status SqliteDb::destroy(Slice path) {
 }
 
 Result<SqliteStatement> SqliteDb::get_statement(CSlice statement) {
-  tdsqlite3_stmt *stmt = nullptr;
+  sqlite3_stmt *stmt = nullptr;
   auto rc =
-      tdsqlite3_prepare_v2(get_native(), statement.c_str(), static_cast<int>(statement.size()) + 1, &stmt, nullptr);
+      sqlite3_prepare_v2(get_native(), statement.c_str(), static_cast<int>(statement.size()) + 1, &stmt, nullptr);
   if (rc != SQLITE_OK) {
     return Status::Error(PSLICE() << "Failed to prepare SQLite " << tag("statement", statement) << raw_->last_error());
   }
Index: td/tddb/td/db/SqliteDb.h
===================================================================
--- td.orig/tddb/td/db/SqliteDb.h
+++ td/tddb/td/db/SqliteDb.h
@@ -17,8 +17,9 @@
 #include "td/utils/Status.h"
 
 #include <memory>
+#include <sqlcipher/sqlite3.h>
 
-struct tdsqlite3;
+struct sqlite3;
 
 namespace td {
 
@@ -64,7 +65,7 @@ class SqliteDb {
   static Result<SqliteDb> change_key(CSlice path, bool allow_creation, const DbKey &new_db_key,
                                      const DbKey &old_db_key);
 
-  tdsqlite3 *get_native() const {
+  sqlite3 *get_native() const {
     return raw_->db();
   }
 
Index: td/tddb/td/db/SqliteStatement.cpp
===================================================================
--- td.orig/tddb/td/db/SqliteStatement.cpp
+++ td/tddb/td/db/SqliteStatement.cpp
@@ -11,46 +11,46 @@
 #include "td/utils/StackAllocator.h"
 #include "td/utils/StringBuilder.h"
 
-#include "sqlite/sqlite3.h"
+#include <sqlcipher/sqlite3.h>
 
 namespace td {
 
 int VERBOSITY_NAME(sqlite) = VERBOSITY_NAME(DEBUG) + 10;
 
 namespace {
-int printExplainQueryPlan(StringBuilder &sb, tdsqlite3_stmt *pStmt) {
-  const char *zSql = tdsqlite3_sql(pStmt);
+int printExplainQueryPlan(StringBuilder &sb, sqlite3_stmt *pStmt) {
+  const char *zSql = sqlite3_sql(pStmt);
   if (zSql == nullptr) {
     return SQLITE_ERROR;
   }
 
   sb << "Explain query " << zSql;
-  char *zExplain = tdsqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
+  char *zExplain = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zSql);
   if (zExplain == nullptr) {
     return SQLITE_NOMEM;
   }
 
-  tdsqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
-  int rc = tdsqlite3_prepare_v2(tdsqlite3_db_handle(pStmt), zExplain, -1, &pExplain, nullptr);
-  tdsqlite3_free(zExplain);
+  sqlite3_stmt *pExplain; /* Compiled EXPLAIN QUERY PLAN command */
+  int rc = sqlite3_prepare_v2(sqlite3_db_handle(pStmt), zExplain, -1, &pExplain, nullptr);
+  sqlite3_free(zExplain);
   if (rc != SQLITE_OK) {
     return rc;
   }
 
-  while (SQLITE_ROW == tdsqlite3_step(pExplain)) {
-    int iSelectid = tdsqlite3_column_int(pExplain, 0);
-    int iOrder = tdsqlite3_column_int(pExplain, 1);
-    int iFrom = tdsqlite3_column_int(pExplain, 2);
-    const char *zDetail = reinterpret_cast<const char *>(tdsqlite3_column_text(pExplain, 3));
+  while (SQLITE_ROW == sqlite3_step(pExplain)) {
+    int iSelectid = sqlite3_column_int(pExplain, 0);
+    int iOrder = sqlite3_column_int(pExplain, 1);
+    int iFrom = sqlite3_column_int(pExplain, 2);
+    const char *zDetail = reinterpret_cast<const char *>(sqlite3_column_text(pExplain, 3));
 
     sb << '\n' << iSelectid << ' ' << iOrder << ' ' << iFrom << ' ' << zDetail;
   }
 
-  return tdsqlite3_finalize(pExplain);
+  return sqlite3_finalize(pExplain);
 }
 }  // namespace
 
-SqliteStatement::SqliteStatement(tdsqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db)
+SqliteStatement::SqliteStatement(sqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db)
     : stmt_(stmt), db_(std::move(db)) {
   CHECK(stmt != nullptr);
 }
@@ -72,14 +72,14 @@ Result<string> SqliteStatement::explain(
   return sb.as_cslice().str();
 }
 Status SqliteStatement::bind_blob(int id, Slice blob) {
-  auto rc = tdsqlite3_bind_blob(stmt_.get(), id, blob.data(), static_cast<int>(blob.size()), nullptr);
+  auto rc = sqlite3_bind_blob(stmt_.get(), id, blob.data(), static_cast<int>(blob.size()), nullptr);
   if (rc != SQLITE_OK) {
     return last_error();
   }
   return Status::OK();
 }
 Status SqliteStatement::bind_string(int id, Slice str) {
-  auto rc = tdsqlite3_bind_text(stmt_.get(), id, str.data(), static_cast<int>(str.size()), nullptr);
+  auto rc = sqlite3_bind_text(stmt_.get(), id, str.data(), static_cast<int>(str.size()), nullptr);
   if (rc != SQLITE_OK) {
     return last_error();
   }
@@ -87,21 +87,21 @@ Status SqliteStatement::bind_string(int
 }
 
 Status SqliteStatement::bind_int32(int id, int32 value) {
-  auto rc = tdsqlite3_bind_int(stmt_.get(), id, value);
+  auto rc = sqlite3_bind_int(stmt_.get(), id, value);
   if (rc != SQLITE_OK) {
     return last_error();
   }
   return Status::OK();
 }
 Status SqliteStatement::bind_int64(int id, int64 value) {
-  auto rc = tdsqlite3_bind_int64(stmt_.get(), id, value);
+  auto rc = sqlite3_bind_int64(stmt_.get(), id, value);
   if (rc != SQLITE_OK) {
     return last_error();
   }
   return Status::OK();
 }
 Status SqliteStatement::bind_null(int id) {
-  auto rc = tdsqlite3_bind_null(stmt_.get(), id);
+  auto rc = sqlite3_bind_null(stmt_.get(), id);
   if (rc != SQLITE_OK) {
     return last_error();
   }
@@ -127,8 +127,8 @@ StringBuilder &operator<<(StringBuilder
 }
 Slice SqliteStatement::view_blob(int id) {
   LOG_IF(ERROR, view_datatype(id) != Datatype::Blob) << view_datatype(id);
-  auto *data = tdsqlite3_column_blob(stmt_.get(), id);
-  auto size = tdsqlite3_column_bytes(stmt_.get(), id);
+  auto *data = sqlite3_column_blob(stmt_.get(), id);
+  auto size = sqlite3_column_bytes(stmt_.get(), id);
   if (data == nullptr) {
     return Slice();
   }
@@ -136,8 +136,8 @@ Slice SqliteStatement::view_blob(int id)
 }
 Slice SqliteStatement::view_string(int id) {
   LOG_IF(ERROR, view_datatype(id) != Datatype::Text) << view_datatype(id);
-  auto *data = tdsqlite3_column_text(stmt_.get(), id);
-  auto size = tdsqlite3_column_bytes(stmt_.get(), id);
+  auto *data = sqlite3_column_text(stmt_.get(), id);
+  auto size = sqlite3_column_bytes(stmt_.get(), id);
   if (data == nullptr) {
     return Slice();
   }
@@ -145,14 +145,14 @@ Slice SqliteStatement::view_string(int i
 }
 int32 SqliteStatement::view_int32(int id) {
   LOG_IF(ERROR, view_datatype(id) != Datatype::Integer) << view_datatype(id);
-  return tdsqlite3_column_int(stmt_.get(), id);
+  return sqlite3_column_int(stmt_.get(), id);
 }
 int64 SqliteStatement::view_int64(int id) {
   LOG_IF(ERROR, view_datatype(id) != Datatype::Integer) << view_datatype(id);
-  return tdsqlite3_column_int64(stmt_.get(), id);
+  return sqlite3_column_int64(stmt_.get(), id);
 }
 SqliteStatement::Datatype SqliteStatement::view_datatype(int id) {
-  auto type = tdsqlite3_column_type(stmt_.get(), id);
+  auto type = sqlite3_column_type(stmt_.get(), id);
   switch (type) {
     case SQLITE_INTEGER:
       return Datatype::Integer;
@@ -170,7 +170,7 @@ SqliteStatement::Datatype SqliteStatemen
 }
 
 void SqliteStatement::reset() {
-  tdsqlite3_reset(stmt_.get());
+  sqlite3_reset(stmt_.get());
   state_ = State::Start;
 }
 
@@ -178,9 +178,9 @@ Status SqliteStatement::step() {
   if (state_ == State::Finish) {
     return Status::Error("One has to reset statement");
   }
-  VLOG(sqlite) << "Start step " << tag("query", tdsqlite3_sql(stmt_.get())) << tag("statement", stmt_.get())
+  VLOG(sqlite) << "Start step " << tag("query", sqlite3_sql(stmt_.get())) << tag("statement", stmt_.get())
                << tag("database", db_.get());
-  auto rc = tdsqlite3_step(stmt_.get());
+  auto rc = sqlite3_step(stmt_.get());
   VLOG(sqlite) << "Finish step with response " << (rc == SQLITE_ROW ? "ROW" : (rc == SQLITE_DONE ? "DONE" : "ERROR"));
   if (rc == SQLITE_ROW) {
     state_ = State::HaveRow;
@@ -194,8 +194,8 @@ Status SqliteStatement::step() {
   return last_error();
 }
 
-void SqliteStatement::StmtDeleter::operator()(tdsqlite3_stmt *stmt) {
-  tdsqlite3_finalize(stmt);
+void SqliteStatement::StmtDeleter::operator()(sqlite3_stmt *stmt) {
+  sqlite3_finalize(stmt);
 }
 
 Status SqliteStatement::last_error() {
Index: td/tddb/td/db/detail/RawSqliteDb.cpp
===================================================================
--- td.orig/tddb/td/db/detail/RawSqliteDb.cpp
+++ td/tddb/td/db/detail/RawSqliteDb.cpp
@@ -6,8 +6,6 @@
 //
 #include "td/db/detail/RawSqliteDb.h"
 
-#include "sqlite/sqlite3.h"
-
 #include "td/utils/common.h"
 #include "td/utils/logging.h"
 #include "td/utils/misc.h"
@@ -15,14 +13,15 @@
 #include "td/utils/port/Stat.h"
 
 #include <atomic>
+#include <sqlcipher/sqlite3.h>
 
 namespace td {
 namespace detail {
 
 static std::atomic<bool> was_database_destroyed{false};
 
-Status RawSqliteDb::last_error(tdsqlite3 *db, CSlice path) {
-  return Status::Error(PSLICE() << Slice(tdsqlite3_errmsg(db)) << " for database \"" << path << '"');
+Status RawSqliteDb::last_error(sqlite3 *db, CSlice path) {
+  return Status::Error(PSLICE() << Slice(sqlite3_errmsg(db)) << " for database \"" << path << '"');
 }
 
 Status RawSqliteDb::destroy(Slice path) {
@@ -38,7 +37,7 @@ Status RawSqliteDb::destroy(Slice path)
 
 Status RawSqliteDb::last_error() {
   //If database was corrupted, try to delete it.
-  auto code = tdsqlite3_errcode(db_);
+  auto code = sqlite3_errcode(db_);
   if (code == SQLITE_CORRUPT) {
     was_database_destroyed.store(true, std::memory_order_relaxed);
     destroy(path_).ignore();
@@ -52,7 +51,7 @@ bool RawSqliteDb::was_any_database_destr
 }
 
 RawSqliteDb::~RawSqliteDb() {
-  auto rc = tdsqlite3_close(db_);
+  auto rc = sqlite3_close(db_);
   LOG_IF(FATAL, rc != SQLITE_OK) << last_error(db_, path());
 }
 
Index: td/tddb/td/db/detail/RawSqliteDb.h
===================================================================
--- td.orig/tddb/td/db/detail/RawSqliteDb.h
+++ td/tddb/td/db/detail/RawSqliteDb.h
@@ -11,14 +11,14 @@
 #include "td/utils/SliceBuilder.h"
 #include "td/utils/Status.h"
 
-struct tdsqlite3;
+struct sqlite3;
 
 namespace td {
 namespace detail {
 
 class RawSqliteDb {
  public:
-  RawSqliteDb(tdsqlite3 *db, std::string path) : db_(db), path_(std::move(path)) {
+  RawSqliteDb(sqlite3 *db, std::string path) : db_(db), path_(std::move(path)) {
   }
   RawSqliteDb(const RawSqliteDb &) = delete;
   RawSqliteDb(RawSqliteDb &&) = delete;
@@ -35,7 +35,7 @@ class RawSqliteDb {
   }
   static Status destroy(Slice path) TD_WARN_UNUSED_RESULT;
 
-  tdsqlite3 *db() {
+  sqlite3 *db() {
     return db_;
   }
   CSlice path() const {
@@ -43,7 +43,7 @@ class RawSqliteDb {
   }
 
   Status last_error();
-  static Status last_error(tdsqlite3 *db, CSlice path);
+  static Status last_error(sqlite3 *db, CSlice path);
 
   static bool was_any_database_destroyed();
 
@@ -68,7 +68,7 @@ class RawSqliteDb {
   }
 
  private:
-  tdsqlite3 *db_;
+  sqlite3 *db_;
   std::string path_;
   size_t begin_cnt_{0};
   optional<int32> cipher_version_;
Index: td/tddb/td/db/SqliteStatement.h
===================================================================
--- td.orig/tddb/td/db/SqliteStatement.h
+++ td/tddb/td/db/SqliteStatement.h
@@ -16,8 +16,8 @@
 
 #include <memory>
 
-struct tdsqlite3;
-struct tdsqlite3_stmt;
+struct sqlite3;
+struct sqlite3_stmt;
 
 namespace td {
 
@@ -69,17 +69,17 @@ class SqliteStatement {
 
  private:
   friend class SqliteDb;
-  SqliteStatement(tdsqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db);
+  SqliteStatement(sqlite3_stmt *stmt, std::shared_ptr<detail::RawSqliteDb> db);
 
   class StmtDeleter {
    public:
-    void operator()(tdsqlite3_stmt *stmt);
+    void operator()(sqlite3_stmt *stmt);
   };
 
   enum class State { Start, HaveRow, Finish };
   State state_ = State::Start;
 
-  std::unique_ptr<tdsqlite3_stmt, StmtDeleter> stmt_;
+  std::unique_ptr<sqlite3_stmt, StmtDeleter> stmt_;
   std::shared_ptr<detail::RawSqliteDb> db_;
 
   Status last_error();
