Tools

Cryptography Code

Caesar

some of the code I've written is so stupid I tell you.

this one basically fires up a REPL with a argv[1] as the number of forward caesar cipher steps you want to take.

really not worth running.

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

int upper(int character, int shift) {
	character += shift;
	if (character > 90) {
		character = (character-90)%26 + 64;
	}
	else if (character < 65) {
		character = (65-character)%26;
		if (character == 0) return 65;
		character = 91 - character;
	}
	return character;
}

int lower(int character, int shift) {
	character += shift;
	if (character > 122) {
		character = (character-122)%26 + 96;
	}
	else if (character < 97) {
		character = (97-character)%26;
		if (character == 0) return 97;
		character = 123 - character;
	}
	return character;
}


int main(int argc, char* argv[]) {
	int shift = atoi(argv[1]);
	int character = getchar();
	while (character != EOF) {
		if (character >= 'A' && character <= 'Z') {
			putchar(upper(character, shift));
		}
		else if (character >= 'a' && character <= 'z') {
			putchar(lower(character, shift));
		}
		else putchar(character);
		character = getchar();
	}
}

File XOR

#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE *fi, *fo;
    char *cp;
    int c;

    if ((cp = argv[1]) && *cp!='\0') {
        if ((fi = fopen(argv[2], "rb")) != NULL) {
            if ((fo = fopen(argv[3], "wb")) != NULL) {
                while ((c = getc(fi)) != EOF) {
                    if (!*cp) cp = argv[1];
                    c ^= *(cp++);
                    putc(c,fo);
                }
                fclose(fo);
            }
            fclose(fi);
        }
    }
}

Miscellaneous Exercises over the years

Hexadecimal Converter

I think I made this for integration into Alfred. This needs to be refactored if you want to use it!

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#define N_BITS 16

// Function declarations
void change_base(int base, int user_num);
char *convert_to_binary(int16_t value);

// Convert number to any base (2-36)
void change_base(int base, int user_num)
{
    int i = 0;
    int j = 0;
    int raised, ceiling, floored;
    int length = snprintf(NULL, 0, "%d", base);
    char base_name[20] = "";
    char output[55] = "";
    char floored_str[32] = "";
    char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    for (raised = pow(base,i); user_num % raised < user_num; i++) {
        raised = pow(base, i);
        ceiling = i - 1;
    }
    
    for (j = ceiling; j > -1; j--) {
        floored = floor(user_num / pow(base, j));
        user_num -= floored*pow(base, j); 
        sprintf(floored_str, "%d", floored);
        if (floored > 9) {
            sprintf(floored_str, "%c", alphabet[floored - 10]);
        }    
        strcat(output, floored_str);
    }
    
    if (base == 2) strcpy(base_name, "Binary");
    else if (base == 16) strcpy(base_name, "Hexadecimal");
    else snprintf(base_name, length + 1, "%d", base);
    
    printf("<items><item uid=\"%s\" arg=\"%s\"><title>%s</title><subtitle></subtitle><icon>icon.png</icon></item></items>", base_name, output, output);
}

// Convert number to 16-bit binary
char *convert_to_binary(int16_t value) {
    char *buffer = malloc((N_BITS + 1) * sizeof(char));
    if (!buffer) {
        return NULL;
    }

    for (int i = 0; i < N_BITS; i++) {
        int16_t bit_mask = 1 << (N_BITS - i - 1);
        buffer[i] = (value & bit_mask) ? '1' : '0';
    }

    buffer[N_BITS] = '\0';
    return buffer;
}

void print_usage() {
    printf("Usage: %s <base> <number>\n", "base_changer");
    printf("\nBase must be between 2 and 36\n");
    printf("Number must be between 0 and 2147483647\n");
}

int main(int argc, char *argv[])
{
    if (argc != 3) {
        print_usage();
        return 1;
    }
    
    int base = atoi(argv[1]);
    int user_num = atoi(argv[2]);
    
    if (!(base >= 2 && base <= 36)) {
        print_usage();
        return 2;
    }
    if (user_num > 2147483647) {
        print_usage();
        return 2;
    }
    
    change_base(base, user_num);
    return 0;
}

output

<items><item uid="Hexadecimal" arg="FF"><title>FF</title><subtitle></subtitle><icon>icon.png</icon></item></items>

Binary Converter

Same bullshit with this:

Read more >

Tools

I am using this project as an excuse to learn GO.

Each heading below is a tool I have produced, the Go code is tangled into a repository called 100 Days of Go and repurposed at tools.abaj.ai — which serves these convenience mathematical and programmatic tools in a nice frontend.

The Table of Contents is your friend.

Primality Checker

Nth Fibonacci Number

GCD

LCM

Base Changer