summaryrefslogtreecommitdiffstats
path: root/main.c
blob: c6b55853657dbf84e495a24c87209eda5157506f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*	Copyright (C) 2021 Gentoo-libre Install

	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void decodeEmail(char encoded[])
	{
	int len = strlen(encoded);

	char temp[3];
	char email[len/2];//the encoded string/2 gives the length of the result email+1

	memcpy(temp, encoded, 2);
	temp[2] = '\0';

	char xorKey = strtol(temp, NULL, 16);//the value of the xorKey is the first two bytes in hex, encoded in UTF-8

	int j = 0;
	for (int i = 2; i < len; i += 2, ++j)//decode the rest of the two bytes
		{
		memcpy(temp, encoded+i,2);
		temp[2] = '\0';

		email[j] = strtol(temp, NULL, 16) ^ xorKey;//xor two bytes with the xorkey to get a UTF-8 character
		}
	email[j] = '\0';//terminate the string

	printf("Decoded email: %s\n", email);
	}



int main(int argc, char *argv[])
	{
	if (!argv[1])
		{
		char input[512+1];//as the maximun length of an email should be 255 chars, we allocate 255*2+2 chars for the longest valid input (+1 for the newline)
		fprintf(stderr,"Input email to decode (string within data-cfemail=\"\"): ");

		ssize_t length = read(0, input, 512);
		if (length < 5){fprintf(stderr,"Invalid input.\n"); exit(1);}

		input[length-1] = '\0';//overwrite newline with null char

		decodeEmail(input);
		}
	else
		{
		if (strlen(argv[1]) < 4 || strlen(argv[1]) > 512){fprintf(stderr,"Invalid input.\n"); exit(1);}
		decodeEmail(argv[1]);
		}

	//test string: e4908d9497a4908b9696818a90829681858fca878b89
	}