/* * A Four Square cipher decoder. (Don't look directly at the mess) * * Copyright (C) 2014 Tomasz Kramkowski * * This program is free software. It is licensed under version 3 of the * GNU General Public License. * * 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 #include #include #include #define Q ('Q'-'A') typedef struct vec2 { int x; int y; } Vec2; int increinc(int *); void allupper(char *); void genkey(char *, char *); Vec2 *newVec2(const int, const int); int main(int argc, char **argv) { int x, y, c=0; if(argc!=4){ printf("Usage: %s ", *argv); exit(1); } printf("%s, %s ", *(argv+1), *(argv+2)); char *trkey = *(argv+1); allupper(trkey); char *trfullkey = malloc(26); genkey(trkey, trfullkey); char *blkey = *(argv+2); allupper(blkey); char *blfullkey = malloc(26); genkey(blkey, blfullkey); char *cipher = *(argv+3); allupper(cipher); int i=0; Vec2 *trtable[25]; for(x=0; x<5; x++) for(y=0; y<5; y++) trtable[trfullkey[i++]-'A'] = newVec2(x, y); free(trfullkey); i = 0; Vec2 *bltable[25]; for(x=0; x<5; x++) for(y=0; y<5; y++) bltable[blfullkey[i++]-'A'] = newVec2(x, y); free(blfullkey); char basetable[5][5]; for(x=0; x<5; x++) for(y=0; y<5; y++){ if(c==Q) c++; basetable[x][y] = 'A' + c++; } for(i=0; ix][bltable[c2-'A']->y]); putchar(basetable[bltable[c2-'A']->x][trtable[c1-'A']->y]); } putchar('\n'); for(i=0; i++; i<25){ free(trtable[i]); free(bltable[i]); } return 0; } int increinc(int *num) { (*num)++; return (*num)++; } void allupper(char *input) { int i; for(i=0; ix = x; v->y = y; return v; }