From f8b900badcc505af67575d17e6c59abc92961327 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 3 Apr 2015 12:00:50 +0200 Subject: foursquare: various cleanup. --- foursquare.c | 189 +++++++++++++++++++++++++++++------------------------------ vigenere.c | 8 +-- 2 files changed, 98 insertions(+), 99 deletions(-) diff --git a/foursquare.c b/foursquare.c index c5ab68c..6fc894e 100644 --- a/foursquare.c +++ b/foursquare.c @@ -14,119 +14,118 @@ #include #include #include +#include #define Q ('Q'-'A') -typedef struct vec2 { - int x; - int y; -} Vec2; +#define CTOI(c) ((c) - 'A') +#define ITOC(c) ((c) + 'A') + +struct vec2 { + int x; + int y; +}; int increinc(int *); -void allupper(char *); +void preprocess(char *); void genkey(char *, char *); -Vec2 *newVec2(const int, const int); +struct 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 x, y, c = 0, i; + char trfullkey[26], blfullkey[26], *trkey, *blkey, *cipher; + struct vec2 trtable[25], bltable[25]; + + if(argc!=4){ + printf("Usage: %s ", *argv); + exit(1); + } + + printf("%s, %s ", *(argv+1), *(argv+2)); + + trkey = argv[1]; + preprocess(trkey); + genkey(trkey, trfullkey); + + blkey = argv[2]; + preprocess(blkey); + genkey(blkey, blfullkey); + + cipher = argv[3]; + preprocess(cipher); + + i = 0; + for(x=0; x<5; x++) + for(y=0; y<5; y++) { + trtable[CTOI(trfullkey[i])].x = x; + trtable[CTOI(trfullkey[i])].y = y; + i++; + } + + i = 0; + for(x=0; x<5; x++) + for(y=0; y<5; y++) { + bltable[CTOI(blfullkey[i])].x = x; + bltable[CTOI(blfullkey[i])].y = y; + i++; + } + + + 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(size_t i = 0; i < strlen(cipher) / 2; i++){ + char c1 = cipher[2*i]; + char c2 = cipher[2*i+1]; + + putchar(basetable[trtable[CTOI(c1)].x][bltable[CTOI(c2)].y]); + putchar(basetable[bltable[CTOI(c2)].x][trtable[CTOI(c1)].y]); + } + putchar('\n'); + + return EXIT_SUCCESS; } -int increinc(int *num) +inline int increinc(int *num) { - (*num)++; - return (*num)++; + (*num)++; + return (*num)++; } -void allupper(char *input) +void preprocess(char *str) { - int i; - for(i=0; ix = x; - v->y = y; - return v; + for (size_t i = 0; i < strlen(input); i++) { + if (!dict[CTOI(input[i])]) { + output[ii++] = input[i]; + dict[CTOI(input[i])] = true; + } + } + + for (unsigned i = 0; i < 26; i++) + if (!dict[i] && i != Q) + output[ii++] = ITOC(i); + + output[ii]='\0'; } diff --git a/vigenere.c b/vigenere.c index 8ed4a6c..5db3e18 100644 --- a/vigenere.c +++ b/vigenere.c @@ -15,13 +15,13 @@ int main(int argc, char **argv) size_t keylength, textlength; if (argc != 4) { - fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } - key = argv[1]; - offset = strtol(argv[2], NULL, 10); - ciphertext = argv[3]; + offset = strtol(argv[1], NULL, 10); + ciphertext = argv[2]; + key = argv[3]; preprocess(key); preprocess(ciphertext); -- cgit v1.2.3-54-g00ecf