Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed rot-13 for non-ASCII
  • Loading branch information
lcn2 committed Jun 1, 2017
commit fa3a10d0a1ff9d3126296362e27370e4fa6fe505
33 changes: 17 additions & 16 deletions rot-13.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,26 @@

/*************************** HEADER FILES ***************************/
#include <string.h>
#include <ctype.h>
#include "rot-13.h"

/*********************** FUNCTION DEFINITIONS ***********************/
void rot13(char str[])
void rot13(BYTE *buf, size_t len)
{
int case_type, idx, len;
int idx;

for (idx = 0, len = strlen(str); idx < len; idx++) {
// Only process alphabetic characters.
if (str[idx] < 'A' || (str[idx] > 'Z' && str[idx] < 'a') || str[idx] > 'z')
continue;
// Determine if the char is upper or lower case.
if (str[idx] >= 'a')
case_type = 'a';
else
case_type = 'A';
// Rotate the char's value, ensuring it doesn't accidentally "fall off" the end.
str[idx] = (str[idx] + 13) % (case_type + 26);
if (str[idx] < 26)
str[idx] += case_type;
}
for (idx = 0; idx < len; idx++) {

// Only process alphabetic characters
if (isalpha(buf[idx])) {

// lower case rotation
if (islower(buf[idx])) {
buf[idx] = (((buf[idx] - 'a') + 13) % 26) + 'a';
// UPPER case rotation
} else {
buf[idx] = (((buf[idx] - 'A') + 13) % 26) + 'A';
}
}
}
}
9 changes: 8 additions & 1 deletion rot-13.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@

/*************************** HEADER FILES ***************************/
#include <stddef.h>
#include <stdint.h>

/**************************** DATA TYPES ****************************/
#if !defined(CRYPTO_TYPES)
typedef uint8_t BYTE; // 8-bit byte
#define CRYPTO_TYPES
#endif

/*********************** FUNCTION DECLARATIONS **********************/
// Performs IN PLACE rotation of the input. Assumes input is NULL terminated.
// Preserves each charcter's case. Ignores non alphabetic characters.
void rot13(char str[]);
void rot13(BYTE *buf, size_t len);

#endif // ROT13_H
13 changes: 8 additions & 5 deletions rot-13_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ int rot13_test()
char code[] = {"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"};
char buf[1024];
int pass = 1;
size_t len;

// To encode, just apply ROT-13.
strcpy(buf, text);
rot13(buf);
pass = pass && !strcmp(code, buf);
memset(buf, 0, sizeof(buf));
len = sizeof(text);
memcpy(buf, text, len);
rot13((BYTE *)buf, len);
pass = pass && (memcmp(code, buf, len) == 0);

// To decode, just re-apply ROT-13.
rot13(buf);
pass = pass && !strcmp(text, buf);
rot13((BYTE *)buf, len);
pass = pass && (memcmp(text, buf, len) == 0);

return(pass);
}
Expand Down