OSDN Git Service

Some code refactoring.
[slunkcrypt/SlunkCrypt.git] / libslunkcrypt / src / slunkcrypt.c
index 96777bb..eb7ede2 100644 (file)
 #endif
 #endif
 
-const char* const SLUNKCRYPT_VERSION = LIB_VERSION_STRING;
-const char* const SLUNKCRYPT_BUILD   = __DATE__", "__TIME__;
+/* Version info */
+const uint16_t SLUNKCRYPT_VERSION_MAJOR = MY_VERSION_MAJOR;
+const uint16_t SLUNKCRYPT_VERSION_MINOR = MY_VERSION_MINOR;
+const uint16_t SLUNKCRYPT_VERSION_PATCH = MY_VERSION_PATCH;
+const char* const SLUNKCRYPT_BUILD = __DATE__ " " __TIME__;
+
+// ==========================================================================
+// Data structures
+// ==========================================================================
 
 typedef struct
 {
@@ -259,7 +266,7 @@ int slunkcrypt_generate_seed(uint64_t* const seed)
 
 slunkcrypt_t slunkcrypt_alloc(const uint64_t salt, const uint8_t *const passwd, const size_t passwd_len)
 {
-       if ((!passwd) || (passwd_len < 1U))
+       if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX))
        {
                return SLUNKCRYPT_NULL;
        }
@@ -282,7 +289,7 @@ slunkcrypt_t slunkcrypt_alloc(const uint64_t salt, const uint8_t *const passwd,
 int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t salt, const uint8_t *const passwd, const size_t passwd_len)
 {
        crypt_state_t* const state = (crypt_state_t*)context;
-       if ((!state) || (!passwd) || (passwd_len < 1U))
+       if ((!state) || (!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX))
        {
                return SLUNKCRYPT_FAILURE;
        }
@@ -301,10 +308,13 @@ int slunkcrypt_encrypt(const slunkcrypt_t context, const uint8_t* const input, u
        {
                return SLUNKCRYPT_FAILURE;
        }
-       for (size_t i = 0; i < length; ++i)
+       if (length > 0U)
        {
-               output[i] = process_enc(state, input[i]);
-               CHECK_ABORTED();
+               for (size_t i = 0; i < length; ++i)
+               {
+                       output[i] = process_enc(state, input[i]);
+                       CHECK_ABORTED();
+               }
        }
        return SLUNKCRYPT_SUCCESS;
 }
@@ -316,10 +326,13 @@ int slunkcrypt_encrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer
        {
                return SLUNKCRYPT_FAILURE;
        }
-       for (size_t i = 0; i < length; ++i)
+       if (length > 0U)
        {
-               buffer[i] = process_enc(state, buffer[i]);
-               CHECK_ABORTED();
+               for (size_t i = 0; i < length; ++i)
+               {
+                       buffer[i] = process_enc(state, buffer[i]);
+                       CHECK_ABORTED();
+               }
        }
        return SLUNKCRYPT_SUCCESS;
 }
@@ -332,10 +345,13 @@ int slunkcrypt_decrypt(const slunkcrypt_t context, const uint8_t* const input, u
        {
                return SLUNKCRYPT_FAILURE;
        }
-       for (size_t i = 0; i < length; ++i)
+       if (length > 0U)
        {
-               output[i] = process_dec(state, input[i]);
-               CHECK_ABORTED();
+               for (size_t i = 0; i < length; ++i)
+               {
+                       output[i] = process_dec(state, input[i]);
+                       CHECK_ABORTED();
+               }
        }
        return SLUNKCRYPT_SUCCESS;
 }
@@ -347,10 +363,13 @@ int slunkcrypt_decrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer
        {
                return SLUNKCRYPT_FAILURE;
        }
-       for (size_t i = 0; i < length; ++i)
+       if (length > 0U)
        {
-               buffer[i] = process_dec(state, buffer[i]);
-               CHECK_ABORTED();
+               for (size_t i = 0; i < length; ++i)
+               {
+                       buffer[i] = process_dec(state, buffer[i]);
+                       CHECK_ABORTED();
+               }
        }
        return SLUNKCRYPT_SUCCESS;
 }