Merge remote-tracking branch 'starcraft_cdkey/master'

This commit is contained in:
Connor Olding 2018-10-11 16:45:36 +02:00
commit cddb62a3a0

34
sckey.c Normal file
View file

@ -0,0 +1,34 @@
int validate(const char *key) {
int magic = 3;
int count = 0;
for (char c; (c = *key); key++) {
if (c < '0' || c > '9') continue;
int v = c - '0';
if (++count == 13) {
// final character.
return magic % 10 == v;
} else {
v ^= magic * 2;
magic += v;
}
}
return 0;
}
int main(int argc, char **argv) {
if (argc == 1) {
// self-test.
if (!validate("0000-00000-0003")) return -1;
if (!validate("1234-56789-0123")) return -1;
if (!validate("1337-42069-0008")) return -1;
if (!validate("1998-00000-1997")) return -1;
if (!validate("3333-33333-3333")) return -1;
return 0;
}
int ret = 0;
for (int i = 1; i < argc; i++) {
if (!validate(argv[i])) ret++;
}
return ret;
}