From a8d873d48aa52cf36c4758dc142f5a1888986dc6 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Fri, 25 Dec 2020 00:29:12 +0000
Subject: [PATCH] Fix some windows unsigned warnings

---
 include/mtxclient/crypto/client.hpp    |  4 +-
 include/mtxclient/http/client_impl.hpp |  2 +-
 lib/crypto/client.cpp                  | 92 +++++++++++++-------------
 lib/crypto/encoding.cpp                |  2 +-
 lib/crypto/utils.cpp                   | 22 +++---
 lib/utils.cpp                          |  2 +-
 6 files changed, 62 insertions(+), 62 deletions(-)

diff --git a/include/mtxclient/crypto/client.hpp b/include/mtxclient/crypto/client.hpp
index 4b2934f77..51cd1e527 100644
--- a/include/mtxclient/crypto/client.hpp
+++ b/include/mtxclient/crypto/client.hpp
@@ -76,9 +76,9 @@ std::string
 pickle(typename T::olm_type *object, const std::string &key)
 {
         auto tmp      = create_buffer(T::pickle_length(object));
-        const int ret = T::pickle(object, key.data(), key.size(), tmp.data(), tmp.size());
+        const auto ret = T::pickle(object, key.data(), key.size(), tmp.data(), tmp.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("pickle", object);
 
         return std::string((char *)tmp.data(), tmp.size());
diff --git a/include/mtxclient/http/client_impl.hpp b/include/mtxclient/http/client_impl.hpp
index 067877fe9..4051880a4 100644
--- a/include/mtxclient/http/client_impl.hpp
+++ b/include/mtxclient/http/client_impl.hpp
@@ -120,7 +120,7 @@ mtx::http::Client::prepare_callback(HeadersCallback<Response> callback)
                         // doesn't return an error struct for non 200 requests.
                         try {
                                 response_data = client::utils::deserialize<Response>(body);
-                        } catch (const nlohmann::json::exception &e) {
+                        } catch (const nlohmann::json::exception &) {
                         }
 
                         // The homeserver should return an error struct.
diff --git a/lib/crypto/client.cpp b/lib/crypto/client.cpp
index 53b1ae4a4..d5e83e48c 100644
--- a/lib/crypto/client.cpp
+++ b/lib/crypto/client.cpp
@@ -20,9 +20,9 @@ OlmClient::create_new_account()
         account_ = create_olm_object<AccountObject>();
 
         auto tmp_buf  = create_buffer(olm_create_account_random_length(account_.get()));
-        const int ret = olm_create_account(account_.get(), tmp_buf.data(), tmp_buf.size());
+        auto ret = olm_create_account(account_.get(), tmp_buf.data(), tmp_buf.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("create_new_account", account_.get());
 }
 
@@ -36,10 +36,10 @@ mtx::crypto::IdentityKeys
 OlmClient::identity_keys() const
 {
         auto tmp_buf = create_buffer(olm_account_identity_keys_length(account_.get()));
-        int result =
+        auto ret =
           olm_account_identity_keys(account_.get(), (void *)tmp_buf.data(), tmp_buf.size());
 
-        if (result == -1)
+        if (ret == olm_error())
                 throw olm_exception("identity_keys", account_.get());
 
         return json::parse(std::string(tmp_buf.begin(), tmp_buf.end()));
@@ -80,10 +80,10 @@ OlmClient::generate_one_time_keys(std::size_t number_of_keys)
 
         auto buf = create_buffer(nbytes);
 
-        const int ret = olm_account_generate_one_time_keys(
+        auto ret = olm_account_generate_one_time_keys(
           account_.get(), number_of_keys, buf.data(), buf.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("generate_one_time_keys", account_.get());
 
         return ret;
@@ -94,9 +94,9 @@ OlmClient::one_time_keys()
 {
         auto buf = create_buffer(olm_account_one_time_keys_length(account_.get()));
 
-        const int ret = olm_account_one_time_keys(account_.get(), buf.data(), buf.size());
+        const auto ret = olm_account_one_time_keys(account_.get(), buf.data(), buf.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("one_time_keys", account_.get());
 
         return json::parse(std::string(buf.begin(), buf.end()));
@@ -176,10 +176,10 @@ OlmClient::init_outbound_group_session()
         auto session = create_olm_object<OutboundSessionObject>();
         auto tmp_buf = create_buffer(olm_init_outbound_group_session_random_length(session.get()));
 
-        const int ret =
+        const auto ret =
           olm_init_outbound_group_session(session.get(), tmp_buf.data(), tmp_buf.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("init_outbound_group_session", session.get());
 
         return session;
@@ -191,10 +191,10 @@ OlmClient::init_inbound_group_session(const std::string &session_key)
         auto session = create_olm_object<InboundSessionObject>();
 
         auto temp     = session_key;
-        const int ret = olm_init_inbound_group_session(
+        const auto ret = olm_init_inbound_group_session(
           session.get(), reinterpret_cast<const uint8_t *>(temp.data()), temp.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("init_inbound_group_session", session.get());
 
         return session;
@@ -206,10 +206,10 @@ OlmClient::import_inbound_group_session(const std::string &session_key)
         auto session = create_olm_object<InboundSessionObject>();
 
         auto temp     = session_key;
-        const int ret = olm_import_inbound_group_session(
+        const auto ret = olm_import_inbound_group_session(
           session.get(), reinterpret_cast<const uint8_t *>(temp.data()), temp.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("init_inbound_group_session", session.get());
 
         return session;
@@ -231,14 +231,14 @@ OlmClient::decrypt_group_message(OlmInboundGroupSession *session,
         tmp_msg = create_buffer(message.size());
         std::copy(message.begin(), message.end(), tmp_msg.begin());
 
-        const int nbytes = olm_group_decrypt(session,
+        const std::size_t nbytes = olm_group_decrypt(session,
                                              tmp_msg.data(),
                                              tmp_msg.size(),
                                              plaintext.data(),
                                              plaintext.size(),
                                              &message_index);
 
-        if (nbytes == -1)
+        if (nbytes == olm_error())
                 throw olm_exception("olm_group_decrypt", session);
 
         auto output = create_buffer(nbytes);
@@ -253,13 +253,13 @@ OlmClient::encrypt_group_message(OlmOutboundGroupSession *session, const std::st
         auto encrypted_len     = olm_group_encrypt_message_length(session, plaintext.size());
         auto encrypted_message = create_buffer(encrypted_len);
 
-        const int nbytes = olm_group_encrypt(session,
+        const std::size_t nbytes = olm_group_encrypt(session,
                                              reinterpret_cast<const uint8_t *>(plaintext.data()),
                                              plaintext.size(),
                                              encrypted_message.data(),
                                              encrypted_message.size());
 
-        if (nbytes == -1)
+        if (nbytes == olm_error())
                 throw olm_exception("olm_group_encrypt", session);
 
         return encrypted_message;
@@ -279,10 +279,10 @@ OlmClient::decrypt_message(OlmSession *session,
         auto decrypted = create_buffer(declen);
         std::copy(one_time_key_message.begin(), one_time_key_message.end(), tmp.begin());
 
-        const int nbytes = olm_decrypt(
+        const std::size_t nbytes = olm_decrypt(
           session, msgtype, (void *)tmp.data(), tmp.size(), decrypted.data(), decrypted.size());
 
-        if (nbytes == -1)
+        if (nbytes == olm_error())
                 throw olm_exception("olm_decrypt", session);
 
         // Removing the extra padding from the origial buffer.
@@ -298,14 +298,14 @@ OlmClient::encrypt_message(OlmSession *session, const std::string &msg)
         auto ciphertext = create_buffer(olm_encrypt_message_length(session, msg.size()));
         auto random_buf = create_buffer(olm_encrypt_random_length(session));
 
-        const int ret = olm_encrypt(session,
+        const auto ret = olm_encrypt(session,
                                     msg.data(),
                                     msg.size(),
                                     random_buf.data(),
                                     random_buf.size(),
                                     ciphertext.data(),
                                     ciphertext.size());
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("olm_encrypt", session);
 
         return ciphertext;
@@ -330,19 +330,19 @@ OlmClient::create_inbound_session_from(const std::string &their_curve25519,
         auto tmp = create_buffer(one_time_key_message.size());
         std::copy(one_time_key_message.begin(), one_time_key_message.end(), tmp.begin());
 
-        int ret = olm_create_inbound_session_from(session.get(),
+        std::size_t ret = olm_create_inbound_session_from(session.get(),
                                                   account(),
                                                   their_curve25519.data(),
                                                   their_curve25519.size(),
                                                   (void *)tmp.data(),
                                                   tmp.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("create_inbound_session_from", session.get());
 
         ret = olm_remove_one_time_keys(account_.get(), session.get());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("inbound_session_from_remove_one_time_keys", account_.get());
 
         return session;
@@ -365,15 +365,15 @@ OlmClient::create_inbound_session(const BinaryBuf &one_time_key_message)
         auto tmp = create_buffer(one_time_key_message.size());
         std::copy(one_time_key_message.begin(), one_time_key_message.end(), tmp.begin());
 
-        int ret =
+        std::size_t ret =
           olm_create_inbound_session(session.get(), account(), (void *)tmp.data(), tmp.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("create_inbound_session", session.get());
 
         ret = olm_remove_one_time_keys(account_.get(), session.get());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("inbound_session_remove_one_time_keys", account_.get());
 
         return session;
@@ -385,7 +385,7 @@ OlmClient::create_outbound_session(const std::string &identity_key, const std::s
         auto session    = create_olm_object<SessionObject>();
         auto random_buf = create_buffer(olm_create_outbound_session_random_length(session.get()));
 
-        const int ret = olm_create_outbound_session(session.get(),
+        const auto ret = olm_create_outbound_session(session.get(),
                                                     account(),
                                                     identity_key.data(),
                                                     identity_key.size(),
@@ -394,7 +394,7 @@ OlmClient::create_outbound_session(const std::string &identity_key, const std::s
                                                     random_buf.data(),
                                                     random_buf.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("create_outbound_session", session.get());
 
         return session;
@@ -412,9 +412,9 @@ SAS::SAS()
         this->sas       = create_olm_object<SASObject>();
         auto random_buf = BinaryBuf(olm_create_sas_random_length(sas.get()));
 
-        const int ret = olm_create_sas(this->sas.get(), random_buf.data(), random_buf.size());
+        const auto ret = olm_create_sas(this->sas.get(), random_buf.data(), random_buf.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("create_sas_instance", this->sas.get());
 }
 
@@ -424,10 +424,10 @@ SAS::public_key()
 {
         auto pub_key_buffer = create_buffer(olm_sas_pubkey_length(this->sas.get()));
 
-        const int ret =
+        const auto ret =
           olm_sas_get_pubkey(this->sas.get(), pub_key_buffer.data(), pub_key_buffer.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("get_public_key", this->sas.get());
 
         return to_string(pub_key_buffer);
@@ -439,10 +439,10 @@ SAS::set_their_key(std::string their_public_key)
 {
         auto pub_key_buffer = to_binary_buf(their_public_key);
 
-        const int ret =
+        const auto ret =
           olm_sas_set_their_key(this->sas.get(), pub_key_buffer.data(), pub_key_buffer.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("get_public_key", this->sas.get());
 }
 
@@ -457,13 +457,13 @@ SAS::generate_bytes_decimal(std::string info)
         std::vector<int> output_list;
         output_list.resize(3);
 
-        const int ret = olm_sas_generate_bytes(this->sas.get(),
+        const auto ret = olm_sas_generate_bytes(this->sas.get(),
                                                input_info_buffer.data(),
                                                input_info_buffer.size(),
                                                output_buffer.data(),
                                                output_buffer.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("get_bytes_decimal", this->sas.get());
 
         output_list[0] = (((output_buffer[0] << 5) | (output_buffer[1] >> 3)) + 1000);
@@ -486,13 +486,13 @@ SAS::generate_bytes_emoji(std::string info)
         std::vector<int> output_list;
         output_list.resize(7);
 
-        const int ret = olm_sas_generate_bytes(this->sas.get(),
+        const auto ret = olm_sas_generate_bytes(this->sas.get(),
                                                input_info_buffer.data(),
                                                input_info_buffer.size(),
                                                output_buffer.data(),
                                                output_buffer.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("get_bytes_emoji", this->sas.get());
 
         output_list[0] = (output_buffer[0] >> 2);
@@ -515,7 +515,7 @@ SAS::calculate_mac(std::string input_data, std::string info)
         auto info_buffer       = to_binary_buf(info);
         auto output_buffer     = BinaryBuf(olm_sas_mac_length(this->sas.get()));
 
-        const int ret = olm_sas_calculate_mac(this->sas.get(),
+        const auto ret = olm_sas_calculate_mac(this->sas.get(),
                                               input_data_buffer.data(),
                                               input_data_buffer.size(),
                                               info_buffer.data(),
@@ -523,7 +523,7 @@ SAS::calculate_mac(std::string input_data, std::string info)
                                               output_buffer.data(),
                                               output_buffer.size());
 
-        if (ret == -1)
+        if (ret == olm_error())
                 throw olm_exception("calculate_mac", this->sas.get());
 
         return to_string(output_buffer);
@@ -634,10 +634,10 @@ mtx::crypto::export_session(OlmInboundGroupSession *s)
         const uint32_t index = olm_inbound_group_session_first_known_index(s);
 
         auto session_key = create_buffer(len);
-        const int rc =
+        const std::size_t ret =
           olm_export_inbound_group_session(s, session_key.data(), session_key.size(), index);
 
-        if (rc == -1)
+        if (ret == olm_error())
                 throw olm_exception("session_key", s);
 
         return std::string(session_key.begin(), session_key.end());
@@ -648,10 +648,10 @@ mtx::crypto::import_session(const std::string &session_key)
 {
         auto session = create_olm_object<InboundSessionObject>();
 
-        const int rc = olm_import_inbound_group_session(
+        const std::size_t ret = olm_import_inbound_group_session(
           session.get(), reinterpret_cast<const uint8_t *>(session_key.data()), session_key.size());
 
-        if (rc == -1)
+        if (ret == olm_error())
                 throw olm_exception("import_session", session.get());
 
         return session;
diff --git a/lib/crypto/encoding.cpp b/lib/crypto/encoding.cpp
index 9e43ef0eb..7f1aa5806 100644
--- a/lib/crypto/encoding.cpp
+++ b/lib/crypto/encoding.cpp
@@ -38,7 +38,7 @@ invert_alphabet(std::array<char, N> alphabet)
                 e = 0xff;
 
         for (std::size_t i = 0; i < N; i++) {
-                inverted[static_cast<uint8_t>(alphabet[i])] = i;
+                inverted[static_cast<uint8_t>(alphabet[i])] = static_cast<uint8_t>(i);
         }
 
         return inverted;
diff --git a/lib/crypto/utils.cpp b/lib/crypto/utils.cpp
index 0913c7732..5015fbebb 100644
--- a/lib/crypto/utils.cpp
+++ b/lib/crypto/utils.cpp
@@ -21,7 +21,7 @@ BinaryBuf
 create_buffer(std::size_t nbytes)
 {
         auto buf = BinaryBuf(nbytes);
-        RAND_bytes(buf.data(), buf.size());
+        RAND_bytes(buf.data(), (int)buf.size());
 
         return buf;
 }
@@ -34,12 +34,12 @@ PBKDF2_HMAC_SHA_512(const std::string pass,
 {
         BinaryBuf out(keylen);
         PKCS5_PBKDF2_HMAC(&pass[0],
-                          pass.size(),
+                          (int)pass.size(),
                           salt.data(),
-                          salt.size(),
+                          (int)salt.size(),
                           iterations,
                           EVP_sha512(),
-                          keylen,
+                          (int)keylen,
                           out.data());
 
         return out;
@@ -138,15 +138,15 @@ HKDF_SHA256(const BinaryBuf &key, const BinaryBuf &salt, const BinaryBuf &info)
                 EVP_PKEY_CTX_free(pctx);
                 throw std::runtime_error("HKDF: failed to set digest");
         }
-        if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt.data(), salt.size()) <= 0) {
+        if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt.data(), (int)salt.size()) <= 0) {
                 EVP_PKEY_CTX_free(pctx);
                 throw std::runtime_error("HKDF: failed to set salt");
         }
-        if (EVP_PKEY_CTX_set1_hkdf_key(pctx, key.data(), key.size()) <= 0) {
+        if (EVP_PKEY_CTX_set1_hkdf_key(pctx, key.data(), (int)key.size()) <= 0) {
                 EVP_PKEY_CTX_free(pctx);
                 throw std::runtime_error("HKDF: failed to set key");
         }
-        if (EVP_PKEY_CTX_add1_hkdf_info(pctx, info.data(), info.size()) <= 0) {
+        if (EVP_PKEY_CTX_add1_hkdf_info(pctx, info.data(), (int)info.size()) <= 0) {
                 EVP_PKEY_CTX_free(pctx);
                 throw std::runtime_error("HKDF: failed to set info");
         }
@@ -182,7 +182,7 @@ AES_CTR_256_Encrypt(const std::string plaintext, const BinaryBuf aes256Key, Bina
 
         uint8_t *iv_data = iv.data();
         // need to set bit 63 to 0
-        *(iv_data) &= ~(1UL << 63);
+        iv_data[63%8] &= ~(1UL << (63/8));
 
         /* Create and initialise the context */
         if (!(ctx = EVP_CIPHER_CTX_new())) {
@@ -200,7 +200,7 @@ AES_CTR_256_Encrypt(const std::string plaintext, const BinaryBuf aes256Key, Bina
                                    encrypted.data(),
                                    &len,
                                    reinterpret_cast<const unsigned char *>(&plaintext.c_str()[0]),
-                                   plaintext.size())) {
+                                   (int)plaintext.size())) {
                 // handleErrors();
         }
         ciphertext_len = len;
@@ -253,7 +253,7 @@ AES_CTR_256_Decrypt(const std::string ciphertext, const BinaryBuf aes256Key, Bin
                                    decrypted.data(),
                                    &len,
                                    reinterpret_cast<const unsigned char *>(&ciphertext.data()[0]),
-                                   ciphertext.size())) {
+                                   (int)ciphertext.size())) {
                 // handleErrors();
         }
         plaintext_len = len;
@@ -434,7 +434,7 @@ HMAC_SHA256(const BinaryBuf hmacKey, const BinaryBuf data)
 {
         unsigned int len = SHA256_DIGEST_LENGTH;
         unsigned char digest[SHA256_DIGEST_LENGTH];
-        HMAC(EVP_sha256(), hmacKey.data(), hmacKey.size(), data.data(), data.size(), digest, &len);
+        HMAC(EVP_sha256(), hmacKey.data(), (int)hmacKey.size(), data.data(), data.size(), digest, &len);
         BinaryBuf output(digest, digest + SHA256_DIGEST_LENGTH);
         return output;
 }
diff --git a/lib/utils.cpp b/lib/utils.cpp
index 0ad377b06..3e30cd28a 100644
--- a/lib/utils.cpp
+++ b/lib/utils.cpp
@@ -59,7 +59,7 @@ mtx::client::utils::random_token(uint8_t len, bool with_symbols) noexcept
         const auto chars = with_symbols ? alphanumberic + symbols : alphanumberic;
 
         thread_local std::random_device rng;
-        std::uniform_int_distribution<> index_dist(0, chars.size() - 1);
+        std::uniform_int_distribution<> index_dist(0, (int)chars.size() - 1);
 
         std::string token;
         token.reserve(len);
-- 
GitLab