Skip to content

Commit

Permalink
utils: move functions from caesar_cipher to utils
Browse files Browse the repository at this point in the history
- move functions IsLowerChar, IsUpperChar, IsAlpha to utils
  • Loading branch information
jciberlin committed May 23, 2024
1 parent 245fa92 commit f6ce64e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Inc/crypto/caesar_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
void CaesarCipher_encrypt(char* msg, uint8_t shift);

/**
* @brief Decryption using Caesar cipher algorithm. Time complexity O(N);
* @brief Decryption using Caesar cipher algorithm. Time complexity O(N).
*
* @param[in/out] *msg Pointer to message that will be decrypted.
* @param[in] shift Shift value that will be used for decryption.
Expand Down
26 changes: 26 additions & 0 deletions Inc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,31 @@ uint32_t Utils_Deserialize32LE(const byte_t* buf);
*/
void Utils_DeserializeBlobLE(const byte_t* buf, byte_t* dst, uint32_t size);

/**
* @brief Check if the given character is upper alphabetical letter.
*
* @param[in] input Input character.
*
* @return True if given character is upper alphabetical letter, otherwise false.
*/
bool Utils_isUpperChar(char input);

/**
* @brief Check if the given character is lower alphabetical letter.
*
* @param[in] input Input character.
*
* @return True if given character is lower alphabetical letter, otherwise false.
*/
bool Utils_isLowerChar(char input);

/**
* @brief Check if the given character is alphabetical letter.
*
* @param[in] input Input character.
*
* @return True if given character is alphabetical letter, otherwise false.
*/
bool Utils_isAlpha(char input);

#endif /* UTILITY_UTILS_H_ */
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,6 @@ Join us on the Discord channel https://discord.gg/R6nZxZqDH3
- Utils_Deserialize24LE
- Utils_Deserialize32LE
- Utils_DeserializeBlobLE
- Utils_isUpperChar
- Utils_isLowerChar
- Utils_isAlpha
40 changes: 6 additions & 34 deletions Src/crypto/caesar_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,19 @@

#include <string.h>

#include "utils.h"

#define MAX_SHIFT (26U)
#define UPPER_A_ASCII (65U)
#define UPPER_Z_ASCII (90U)
#define LOWER_A_ASCII (97U)
#define LOWER_Z_ASCII (122U)

static bool
IsUpperChar(char input) {
bool result = false;
uint8_t c = (uint8_t)input;
if ((c >= UPPER_A_ASCII) && (c <= UPPER_Z_ASCII)) {
result = true;
}
return result;
}

static bool
IsLowerChar(char input) {
bool result = false;
uint8_t c = (uint8_t)input;
if ((c >= LOWER_A_ASCII) && (c <= LOWER_Z_ASCII)) {
result = true;
}
return result;
}

static bool
IsAlpha(char input) {
bool is_upper = IsUpperChar(input);
bool is_lower = IsLowerChar(input);

return (is_upper || is_lower);
}

void
CaesarCipher_encrypt(char* msg, uint8_t shift) {
const size_t length = strlen(msg);
for (size_t i = 0U; i < length; ++i) {
if (IsAlpha(msg[i])) {
if (Utils_isAlpha(msg[i])) {
uint8_t encrypted_char;
if (IsUpperChar(msg[i])) {
if (Utils_isUpperChar(msg[i])) {
encrypted_char = (((uint8_t)msg[i] + shift - UPPER_A_ASCII) % MAX_SHIFT) + UPPER_A_ASCII;
} else {
encrypted_char = (((uint8_t)msg[i] + shift - LOWER_A_ASCII) % MAX_SHIFT) + LOWER_A_ASCII;
Expand All @@ -90,9 +62,9 @@ void
CaesarCipher_decrypt(char* msg, uint8_t shift) {
const size_t length = strlen(msg);
for (size_t i = 0U; i < length; ++i) {
if (IsAlpha(msg[i])) {
if (Utils_isAlpha(msg[i])) {
uint8_t decrypted_char;
if (IsUpperChar(msg[i])) {
if (Utils_isUpperChar(msg[i])) {
decrypted_char = (((uint8_t)msg[i] - shift - UPPER_A_ASCII) % MAX_SHIFT) + UPPER_A_ASCII;
} else {
decrypted_char = (((uint8_t)msg[i] - shift - LOWER_A_ASCII) % MAX_SHIFT) + LOWER_A_ASCII;
Expand Down
32 changes: 32 additions & 0 deletions Src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#include "utils.h"

#define MAX_UINT32_POW_10_EXPONENT 10U
#define UPPER_A_ASCII (65U)
#define UPPER_Z_ASCII (90U)
#define LOWER_A_ASCII (97U)
#define LOWER_Z_ASCII (122U)

bool
Utils_StringToUint32(const char* str, uint8_t str_length, uint32_t* integer) {
Expand Down Expand Up @@ -255,3 +259,31 @@ Utils_DeserializeBlobLE(const byte_t* buf, byte_t* dst, uint32_t size) {
++j;
}
}

bool
Utils_isUpperChar(char input) {
bool result = false;
uint8_t c = (uint8_t)input;
if ((c >= UPPER_A_ASCII) && (c <= UPPER_Z_ASCII)) {
result = true;
}
return result;
}

bool
Utils_isLowerChar(char input) {
bool result = false;
uint8_t c = (uint8_t)input;
if ((c >= LOWER_A_ASCII) && (c <= LOWER_Z_ASCII)) {
result = true;
}
return result;
}

bool
Utils_isAlpha(char input) {
bool is_upper = Utils_isUpperChar(input);
bool is_lower = Utils_isLowerChar(input);

return (is_upper || is_lower);
}
16 changes: 16 additions & 0 deletions Tests/test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ TEST_GROUP_RUNNER(Utils) {
RUN_TEST_CASE(Utils, Utils_BigEndianSerializeDeserialize);
RUN_TEST_CASE(Utils, Utils_LittleEndianSerializeDeserialize);

RUN_TEST_CASE(Utils, Utils_Characters);
}

TEST(Utils, Utils_StringToUint32) {
Expand Down Expand Up @@ -398,3 +399,18 @@ TEST(Utils, Utils_LittleEndianSerializeDeserialize) {
TEST_ASSERT_EQUAL_HEX8(result_data_deserialize[i], test_data_1[i]);
}
}

TEST(Utils, Utils_Characters) {
TEST_ASSERT_TRUE(Utils_isLowerChar('a'));
TEST_ASSERT_FALSE(Utils_isLowerChar('B'));
TEST_ASSERT_FALSE(Utils_isLowerChar(' '));

TEST_ASSERT_TRUE(Utils_isUpperChar('D'));
TEST_ASSERT_FALSE(Utils_isUpperChar('c'));
TEST_ASSERT_FALSE(Utils_isUpperChar(','));

TEST_ASSERT_TRUE(Utils_isAlpha('z'));
TEST_ASSERT_TRUE(Utils_isAlpha('e'));
TEST_ASSERT_FALSE(Utils_isAlpha('*'));
TEST_ASSERT_FALSE(Utils_isAlpha('?'));
}

0 comments on commit f6ce64e

Please sign in to comment.