Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 66 lines (51 sloc) 1.36 KB
#ifndef com_sleepless_rot13_cpp
#define com_sleepless_rot13_cpp
// Copyright 2004 Sleepless Software Inc. All Rights Reserved
/*
*************************************************************************
WARNING: THIS CODE HAS A JAVA COUNTERPART. DON'T CHANGE THIS WITHOUT ALSO
MAKING THE CORRESPONDING CHANGES TO THE JAVA VERSION.
*************************************************************************
*/
#include <string.h>
static const unsigned char *rot13(unsigned char *buf, int len)
{
unsigned char c;
unsigned char *bb = buf;
for(int i = 0; i < len; i++)
{
c = *bb;
if((c >= 'A') && (c <= 'Z'))
c = (((c - 'A') + 13) % 26) + 'A';
else
if((c >= 'a' ) && (c <= 'z'))
c = (((c - 'a') + 13) % 26) + 'a';
*bb++ = c;
}
return buf;
}
static const unsigned char *rot13(unsigned char *buf)
{
return rot13(buf, strlen((const char *)buf));
}
static const char *rot13(char *buf)
{
return (const char *)rot13((unsigned char *)buf);
}
#ifdef TEST_ROT13
#include <stdio.h>
int main(int argc, char **argv)
{
unsigned char buf[100];
while(fgets((char *)buf, sizeof(buf), stdin))
{
int l = strlen((char *)buf);
fprintf(stdout, "Input : %s", (char *)buf);
rot13(buf, l);
fprintf(stdout, "Encrypted: %s", (char *)buf);
rot13(buf, l);
fprintf(stdout, "Decrypted: %s", (char *)buf);
}
}
#endif
#endif // com_sleepless_rot13_cpp