/*
 * Skia < skia AT libskia DOT so >
 *
 * Beerware licensed software - 2016
 *
 *
 * Compile for example with:
 * `arm-linux-gnueabihf-gcc -static -march=armv7 main.c`
 *
 */

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

#define KEY_SIZE 14

char *odd = "GREHACK";
char *even = "radare2";


int check_odd(char c) {
    static int i = 0;
    return odd[i++] == c;
}

int check_even(char c) {
    static int i = 0;
    return even[i++] == c;
}

int check(char *key) {
    int i = 0, valid = 1;
    for (; i < KEY_SIZE; i++) {
        if (i % 2 && !check_odd(key[i])) {
            valid = 0;
        } else if (i % 2 == 0 && !check_even(key[i])) {
            valid = 0;
        }
    }
    return valid;
}

int main(int argc, char** argv)
{
    char key[KEY_SIZE+1] = {0};
    FILE *key_file = fopen("key", "r");
    if (key_file) {
        fread(key, sizeof(char), KEY_SIZE, key_file);
        fclose(key_file);
        printf("Key: %s\n", key);
        if (check(key)) {
            printf("Key valid\n");
        } else {
            printf("Give me a valid key\n");
        }
    } else {
        printf("Give me a key\n");
    }
    return 0;
}

