Some build fixes for old MSVC versions.

This commit is contained in:
LoRd_MuldeR 2020-10-28 15:57:34 +01:00
parent f3f97110f6
commit 9e1894b539
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
3 changed files with 47 additions and 33 deletions

View File

@ -139,7 +139,8 @@ static const uint64_t CRC64_TAB[256] =
uint64_t crc64_update(uint64_t crc, const uint8_t *const data, const size_t data_len) uint64_t crc64_update(uint64_t crc, const uint8_t *const data, const size_t data_len)
{ {
for (size_t i = 0U; i < data_len; ++i) size_t i;
for (i = 0U; i < data_len; ++i)
{ {
crc = CRC64_TAB[((crc >> 56) ^ data[i]) & 0xFF] ^ (crc << 8); crc = CRC64_TAB[((crc >> 56) ^ data[i]) & 0xFF] ^ (crc << 8);
} }

View File

@ -148,17 +148,17 @@ uint64_t swap_bytes_u64(const uint64_t value)
uint64_t get_file_size(FILE* const file) uint64_t get_file_size(FILE* const file)
{ {
STAT_T stat; STAT_T stat;
if (FSTAT(FILENO(file), &stat) != 0) if (FSTAT(FILENO(file), &stat) == 0)
{ {
return UINT64_MAX; const uint16_t file_type = stat.st_mode & S_IFMT;
if ((file_type != S_IFDIR) && (file_type != S_IFIFO))
{
const int64_t ssize = stat.st_size;
return (ssize >= 0) ? ((uint64_t)ssize) : 0U;
}
return 0U;
} }
uint16_t file_type = stat.st_mode & S_IFMT; return UINT64_MAX;
if ((file_type != S_IFDIR) && (file_type != S_IFIFO))
{
const int64_t ssize = stat.st_size;
return (ssize >= 0) ? ((uint64_t)ssize) : 0U;
}
return 0U;
} }
const CHR* get_file_name(const CHR* path) const CHR* get_file_name(const CHR* path)

View File

@ -97,13 +97,13 @@ static FORCE_INLINE uint32_t upper_u64(const uint64_t value)
static FORCE_INLINE uint8_t byte_u16(const uint16_t value, const size_t off) static FORCE_INLINE uint8_t byte_u16(const uint16_t value, const size_t off)
{ {
assert(index < sizeof(uint16_t)); assert(off < sizeof(uint16_t));
return (uint8_t)((value >> (CHAR_BIT * off)) & 0xFF); return (uint8_t)((value >> (CHAR_BIT * off)) & 0xFF);
} }
static FORCE_INLINE uint8_t byte_u64(const uint64_t value, const size_t off) static FORCE_INLINE uint8_t byte_u64(const uint64_t value, const size_t off)
{ {
assert(index < sizeof(uint64_t)); assert(off < sizeof(uint64_t));
return (uint8_t)((value >> (CHAR_BIT * off)) & 0xFF); return (uint8_t)((value >> (CHAR_BIT * off)) & 0xFF);
} }
@ -113,7 +113,8 @@ static FORCE_INLINE uint8_t byte_u64(const uint64_t value, const size_t off)
static FORCE_INLINE void hash_update_str(uint64_t* const hash, const uint8_t* const data, const size_t data_len) static FORCE_INLINE void hash_update_str(uint64_t* const hash, const uint8_t* const data, const size_t data_len)
{ {
for (size_t i = 0U; i < data_len; ++i) size_t i;
for (i = 0U; i < data_len; ++i)
{ {
*hash = ((*hash) ^ data[i]) * MAGIC_PRIME; *hash = ((*hash) ^ data[i]) * MAGIC_PRIME;
} }
@ -121,7 +122,8 @@ static FORCE_INLINE void hash_update_str(uint64_t* const hash, const uint8_t* co
static FORCE_INLINE void hash_update_u64(uint64_t* const hash, const uint64_t value) static FORCE_INLINE void hash_update_u64(uint64_t* const hash, const uint64_t value)
{ {
for (size_t i = 0U; i < sizeof(uint64_t); ++i) size_t i;
for (i = 0U; i < sizeof(uint64_t); ++i)
{ {
*hash = ((*hash) ^ byte_u64(value, i)) * MAGIC_PRIME; *hash = ((*hash) ^ byte_u64(value, i)) * MAGIC_PRIME;
} }
@ -129,7 +131,8 @@ static FORCE_INLINE void hash_update_u64(uint64_t* const hash, const uint64_t va
static FORCE_INLINE void hash_update_u16(uint64_t* const hash, const uint16_t value) static FORCE_INLINE void hash_update_u16(uint64_t* const hash, const uint16_t value)
{ {
for (size_t i = 0U; i < sizeof(uint16_t); ++i) size_t i;
for (i = 0U; i < sizeof(uint16_t); ++i)
{ {
*hash = ((*hash) ^ byte_u16(value, i)) * MAGIC_PRIME; *hash = ((*hash) ^ byte_u16(value, i)) * MAGIC_PRIME;
} }
@ -150,7 +153,8 @@ static uint64_t hash_code(const uint64_t salt, const uint16_t pepper, const uint
static FORCE_INLINE uint64_t keygen_loop(uint64_t value, const uint16_t pepper, const uint8_t* const passwd, const size_t passwd_len) static FORCE_INLINE uint64_t keygen_loop(uint64_t value, const uint16_t pepper, const uint8_t* const passwd, const size_t passwd_len)
{ {
for (size_t i = 0U; i < 99971U; ++i) size_t i;
for (i = 0U; i < 99971U; ++i)
{ {
value ^= hash_code(value, pepper, passwd, passwd_len); value ^= hash_code(value, pepper, passwd, passwd_len);
} }
@ -193,10 +197,11 @@ static uint32_t random_next(rand_state_t *const state)
static void random_seed(rand_state_t* const state, const uint64_t salt, const uint16_t pepper, const uint8_t *const passwd, const size_t passwd_len) static void random_seed(rand_state_t* const state, const uint64_t salt, const uint16_t pepper, const uint8_t *const passwd, const size_t passwd_len)
{ {
key_data_t key; key_data_t key;
size_t i;
generate_key(&key, salt, pepper, passwd, passwd_len); generate_key(&key, salt, pepper, passwd, passwd_len);
random_init(state, key.a, key.b); random_init(state, key.a, key.b);
slunkcrypt_bzero(&key, sizeof(key_data_t)); slunkcrypt_bzero(&key, sizeof(key_data_t));
for (size_t i = 0U; i < 97U; ++i) for (i = 0U; i < 97U; ++i)
{ {
UNUSED volatile uint32_t u = random_next(state); UNUSED volatile uint32_t u = random_next(state);
} }
@ -208,16 +213,17 @@ static void random_seed(rand_state_t* const state, const uint64_t salt, const ui
static int initialize_state(crypt_state_t* const crypt_state, const uint64_t nonce, const uint8_t* const passwd, const size_t passwd_len) static int initialize_state(crypt_state_t* const crypt_state, const uint64_t nonce, const uint8_t* const passwd, const size_t passwd_len)
{ {
rand_state_t rand_state;
size_t r, i;
slunkcrypt_bzero(crypt_state, sizeof(crypt_state_t)); slunkcrypt_bzero(crypt_state, sizeof(crypt_state_t));
/* set up wheels and initial rotation */ /* set up wheels and initial rotation */
rand_state_t rand_state; for (r = 0U; r < 256U; ++r)
for (size_t r = 0U; r < 256U; ++r)
{ {
random_seed(&rand_state, nonce, (uint16_t)r, passwd, passwd_len); random_seed(&rand_state, nonce, (uint16_t)r, passwd, passwd_len);
crypt_state->rotation_bwd[0U][255U - r] = crypt_state->rotation_fwd[0U][r] = (uint8_t)random_next(&rand_state); crypt_state->rotation_bwd[0U][255U - r] = crypt_state->rotation_fwd[0U][r] = (uint8_t)random_next(&rand_state);
crypt_state->rotation_bwd[1U][255U - r] = crypt_state->rotation_fwd[1U][r] = 0U; crypt_state->rotation_bwd[1U][255U - r] = crypt_state->rotation_fwd[1U][r] = 0U;
for (size_t i = 0U; i < 256U; ++i) for (i = 0U; i < 256U; ++i)
{ {
const size_t j = random_next(&rand_state) % (i + 1U); const size_t j = random_next(&rand_state) % (i + 1U);
if (j != i) if (j != i)
@ -226,7 +232,7 @@ static int initialize_state(crypt_state_t* const crypt_state, const uint64_t non
} }
crypt_state->wheel_fwd[r][j] = (uint8_t)i; crypt_state->wheel_fwd[r][j] = (uint8_t)i;
} }
for (size_t i = 0U; i < 256U; ++i) for (i = 0U; i < 256U; ++i)
{ {
const size_t j = crypt_state->wheel_fwd[r][i]; const size_t j = crypt_state->wheel_fwd[r][i];
crypt_state->wheel_bwd[255U - r][j] = (uint8_t)i; crypt_state->wheel_bwd[255U - r][j] = (uint8_t)i;
@ -236,7 +242,7 @@ static int initialize_state(crypt_state_t* const crypt_state, const uint64_t non
/* set up stepping */ /* set up stepping */
random_seed(&rand_state, nonce, 256U, passwd, passwd_len); random_seed(&rand_state, nonce, 256U, passwd, passwd_len);
for (size_t i = 0U; i < 256U; ++i) for (i = 0U; i < 256U; ++i)
{ {
const size_t j = random_next(&rand_state) % (i + 1U); const size_t j = random_next(&rand_state) % (i + 1U);
if (j != i) if (j != i)
@ -258,7 +264,8 @@ static int initialize_state(crypt_state_t* const crypt_state, const uint64_t non
static FORCE_INLINE void increment(uint8_t *const arr, const int rev) static FORCE_INLINE void increment(uint8_t *const arr, const int rev)
{ {
for (size_t i = 0U; i < 256U; ++i) size_t i;
for (i = 0U; i < 256U; ++i)
{ {
if (++arr[rev ? (255U - i) : i] != 0U) if (++arr[rev ? (255U - i) : i] != 0U)
{ {
@ -269,7 +276,8 @@ static FORCE_INLINE void increment(uint8_t *const arr, const int rev)
static FORCE_INLINE uint8_t process_enc(crypt_state_t* const crypt_state, uint8_t value) static FORCE_INLINE uint8_t process_enc(crypt_state_t* const crypt_state, uint8_t value)
{ {
for (size_t i = 0U; i < 256U; ++i) size_t i;
for (i = 0U; i < 256U; ++i)
{ {
const uint8_t offset = crypt_state->rotation_fwd[0U][i] + crypt_state->rotation_fwd[1U][i]; const uint8_t offset = crypt_state->rotation_fwd[0U][i] + crypt_state->rotation_fwd[1U][i];
value = crypt_state->wheel_fwd[i][(value + offset) & 0xFF]; value = crypt_state->wheel_fwd[i][(value + offset) & 0xFF];
@ -281,7 +289,8 @@ static FORCE_INLINE uint8_t process_enc(crypt_state_t* const crypt_state, uint8_
static FORCE_INLINE uint8_t process_dec(crypt_state_t* const crypt_state, uint8_t value) static FORCE_INLINE uint8_t process_dec(crypt_state_t* const crypt_state, uint8_t value)
{ {
for (size_t i = 0U; i < 256U; ++i) size_t i;
for (i = 0U; i < 256U; ++i)
{ {
const uint8_t offset = crypt_state->rotation_bwd[0U][i] + crypt_state->rotation_bwd[1U][i]; const uint8_t offset = crypt_state->rotation_bwd[0U][i] + crypt_state->rotation_bwd[1U][i];
value = (crypt_state->wheel_bwd[i][value] - offset) & 0xFF; value = (crypt_state->wheel_bwd[i][value] - offset) & 0xFF;
@ -314,12 +323,12 @@ int slunkcrypt_generate_nonce(uint64_t* const nonce)
slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len) slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len)
{ {
crypt_state_t* state = NULL;
if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX)) if ((!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX))
{ {
return SLUNKCRYPT_NULL; return SLUNKCRYPT_NULL;
} }
crypt_state_t* const state = (crypt_state_t*)malloc(sizeof(crypt_state_t)); if (!(state = (crypt_state_t*)malloc(sizeof(crypt_state_t))))
if (!state)
{ {
return SLUNKCRYPT_NULL; return SLUNKCRYPT_NULL;
} }
@ -337,12 +346,12 @@ slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd,
int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len) int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len)
{ {
crypt_state_t* const state = (crypt_state_t*)context; crypt_state_t* const state = (crypt_state_t*)context;
int result = SLUNKCRYPT_FAILURE;
if ((!state) || (!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX)) if ((!state) || (!passwd) || (passwd_len < SLUNKCRYPT_PWDLEN_MIN) || (passwd_len > SLUNKCRYPT_PWDLEN_MAX))
{ {
return SLUNKCRYPT_FAILURE; return SLUNKCRYPT_FAILURE;
} }
const int result = initialize_state(state, nonce, passwd, passwd_len); if ((result = initialize_state(state, nonce, passwd, passwd_len)) != SLUNKCRYPT_SUCCESS)
if (result != SLUNKCRYPT_SUCCESS)
{ {
slunkcrypt_bzero(state, sizeof(crypt_state_t)); slunkcrypt_bzero(state, sizeof(crypt_state_t));
} }
@ -358,7 +367,8 @@ int slunkcrypt_encrypt(const slunkcrypt_t context, const uint8_t* const input, u
} }
if (length > 0U) if (length > 0U)
{ {
for (size_t i = 0; i < length; ++i) size_t i;
for (i = 0; i < length; ++i)
{ {
output[i] = process_enc(state, input[i]); output[i] = process_enc(state, input[i]);
CHECK_ABORTED(); CHECK_ABORTED();
@ -376,7 +386,8 @@ int slunkcrypt_encrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer
} }
if (length > 0U) if (length > 0U)
{ {
for (size_t i = 0; i < length; ++i) size_t i;
for (i = 0; i < length; ++i)
{ {
buffer[i] = process_enc(state, buffer[i]); buffer[i] = process_enc(state, buffer[i]);
CHECK_ABORTED(); CHECK_ABORTED();
@ -395,7 +406,8 @@ int slunkcrypt_decrypt(const slunkcrypt_t context, const uint8_t* const input, u
} }
if (length > 0U) if (length > 0U)
{ {
for (size_t i = 0; i < length; ++i) size_t i;
for (i = 0; i < length; ++i)
{ {
output[i] = process_dec(state, input[i]); output[i] = process_dec(state, input[i]);
CHECK_ABORTED(); CHECK_ABORTED();
@ -413,7 +425,8 @@ int slunkcrypt_decrypt_inplace(const slunkcrypt_t context, uint8_t* const buffer
} }
if (length > 0U) if (length > 0U)
{ {
for (size_t i = 0; i < length; ++i) size_t i;
for (i = 0; i < length; ++i)
{ {
buffer[i] = process_dec(state, buffer[i]); buffer[i] = process_dec(state, buffer[i]);
CHECK_ABORTED(); CHECK_ABORTED();