All pastes #399225 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-03-17 21:07 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const char *meow = "meow";

char *baseconv(char num)
{
	static char retbuf[9];
	char *p;

	int base = 2;

	p = &retbuf[sizeof(retbuf)-1];
	*p = '\0';

	int i;
	for (i = 0; i < 8; i++)
	{
		*--p = "0123456789abcdef"[num % base];
		num /= base;
	}

	return p;
}

char *encode(char c)
{
	static char str[9];
	sprintf(str, "%s", baseconv(c));

	int i;
	for (i = 0; i < 8; i++)
	{
		if (str[i] == '1')
			str[i] = toupper(meow[i % strlen(meow)]);
		else
			str[i] = meow[i % strlen(meow)];
	}

	return str;
}

char decode(char *s)
{
	char c;
	char str[9];
	sprintf(str, s);

	int i;
	for (i = 0; i < 8; i++)
	{
		if (isupper(s[i]))
			str[i] = '1';
		else
			str[i] = '0';
	}
	
	return strtoul(str, NULL, 2);
}

int main(int argc, char *argv[])
{
	if (argc == 1 || strcmp(argv[1], "-?") == 0)
	{
		printf("kk: KittyKoder v1.0\n");
		printf("kk: use 'kk <text>' to encode\n");
		printf("kk: use 'kk -d <text>' to decode\n");
		exit(0);
	}
	if (strcmp(argv[1], "-d") == 0)
	{
		// decode
		
		if (strlen(argv[2]) % 8 != 0)
		{
			printf("Invalid kittykode\n");
			exit(1);
		}
		
		char buf[9];
		while (argv[2][0] != '\0')
		{
			strncpy(buf, argv[2], 8);
			buf[8] = '\0';
			printf("%c", decode(buf));
			argv[2] += 8;
		}
	}
	else
	{
		// encode
		int i;
		for (i = 1; i < argc; i++)
		{
			int j;
			for (j = 0; j < strlen(argv[i]); j++)
			{
				printf("%s", encode(argv[i][j]));
			}
			if (i == argc - 1)
				printf("%s", encode(' '));
		}
	}
	printf("\n");
}