/** 
 *  Windows 95 Keygen - 4/23/2022
 *  By Paper
 *  a "rewrite" of my C++ app to vanilla C
 *  truth be told, most of it was C already
 *
 *  skaven is awesome!!
**/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#define CONCAT_INTS(x, y) \
	x * pow(10, log10(y)+1) + y;

int get_sum(int n) {
	int sum;
	for (sum = 0; n > 0; sum += n % 10, n /= 10); // copied this from google. sorry not sorry
	return sum;
}

int win95_nt4_prefix() {
    int num = rand() % 1000;
    while ((num == 333) || (num == 444) || (num == 555) || (num == 666) || (num == 777) || (num == 888) || (num == 999) || ((num % 3) != 0)) {
        num = num + 1;
    }
    return num;
}

int win95_suffix() {
    int first_digits, last_digit, second_segment = 5;
    while (second_segment % 7 != 0) {
        first_digits = rand() % 1000000;
        last_digit = rand() % 10;
        while ((last_digit == 0) || (last_digit >= 8)) {
            last_digit = rand() % 10;
        }
		second_segment = CONCAT_INTS(first_digits, last_digit);
    }
    return second_segment;
}

int office_prefix() {
    int new_site = rand() % 1000;
    int ez_pwned = new_site % 10 + 1;
    while (ez_pwned >= 10) {
        ez_pwned = ez_pwned - 10;
    }
    return CONCAT_INTS(new_site, ez_pwned);
}

int oem_prefix() {
    int years[] = {
        95,
        96,
        97,
        98,
        99,
        00,
        01,
        02,
        03
    };
    return CONCAT_INTS(((rand() % 366) + 1), years[rand() % 9]);
}

int oem_middle() {
    int first_digits, last_digit, second_segment, sum = 1;
    while (sum % 7 != 0) {
        sum = 0;
        first_digits = rand() % 10000;
        last_digit = rand() % 10;
        while ((last_digit == 0) || (last_digit >= 8)) {
            last_digit = rand() % 10;
        }
        second_segment = CONCAT_INTS(first_digits, last_digit);
        get_sum(second_segment);
    }
    return second_segment;
}

int main(int argc, char* argv[]) {
#ifndef __APPLE__
	srand((unsigned) time(NULL)); // magic rand() stuff :p
#else
	sranddev();
#endif
    if (argc >= 2) {
        if (strcmp(argv[1], "--office") == 0) {
			printf("%04d-%07d", office_prefix(), win95_suffix());
			return 0;
        } else if (strcmp(argv[1], "--oem") == 0) {
			printf("%05d-OEM-%07d-%05d", oem_prefix(), oem_middle(), (rand() % 100000));
			return 0;
        } else if (strcmp(argv[1], "--normal") == 0) {
            printf("%03d-%07d", win95_nt4_prefix(), win95_suffix());
			return 0;
        }
    }
	printf("usage: %s [--normal] [--oem] [--office]", argv[0]);
    return 0;
}
