commit 013548194256c3f186ca8c739d0a02e281f3dddc
Author: Michael Constantine Dimopoulos <mk@mcdim.xyz>
Date: Tue, 5 Dec 2023 01:03:30 +0000
hextools initial commit
Diffstat:
A | Makefile | | | 30 | ++++++++++++++++++++++++++++++ |
A | README.txt | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | bin.c | | | 42 | ++++++++++++++++++++++++++++++++++++++++++ |
A | bin2hex | | | 10 | ++++++++++ |
A | cyclic/cyclic.c | | | 109 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | hex.c | | | 26 | ++++++++++++++++++++++++++ |
A | unbin.c | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
A | unhex.c | | | 33 | +++++++++++++++++++++++++++++++++ |
8 files changed, 344 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,30 @@
+CC=c99
+DIR="${HOME}/.local/bin"# change to /bin for global installation
+
+all:
+ $(CC) hex.c -o hex
+ $(CC) bin.c -o bin
+ $(CC) unhex.c -o unhex
+ $(CC) unbin.c -o unbin
+ $(CC) cyclic/cyclic.c -o cyclic/cyclic
+
+install:
+ cp hex $(DIR)/hex
+ cp bin $(DIR)/bin
+ cp unhex $(DIR)/unhex
+ cp unbin $(DIR)/unbin
+ cp cyclic/cyclic $(DIR)/cyclic
+
+uninstall:
+ rm $(DIR)/hex
+ rm $(DIR)/bin
+ rm $(DIR)/unhex
+ rm $(DIR)/unbin
+ rm $(DIR)/cyclic
+
+clean:
+ rm hex
+ rm bin
+ rm unhex
+ rm unbin
+ rm cyclic/cyclic
diff --git a/README.txt b/README.txt
@@ -0,0 +1,55 @@
+ __#^
+ _####"
+ ]######_*##"
+ /#######_# _____
+ **#" ""####_ _########_ ___
+ ""##___########*#___################__,
+ ..._ #"" "###############" "########"
+ ^**#__-=.._ *_ "##
+ """*####"#**^"____ "#_
+ "##***#"""~ I#
+ #__#*_____._##_-<.__ ___
+ *###########""^**#C__-=#""~#__ __## -
+ """#,_"##c """*########"""*##c _
+ #_### "^^"^####*_ {## *
+ *#^ "#_#### "
+ " ###*# _ = -
+ ~ . `
+ Witch by Katie Hoeppner
+
+hextools v1
+===========
+
+Repository with simple QOL Unix-style tools for base conversion that I
+wrote to help with low-level development work or reverse engineering.
+They're optimised to be used with the largest representable numbers in
+your machine. Portable, modular & simple.
+
+hex - reads a number in decimal and prints it in hexadecimal
+bin - reads a number in decimal and prints it in binary
+unhex - reads a number in hexadecimal and prints it in decimal
+unbin - reads a number in binary and prints it in decimal
+bin2hex - script to convert binary to hexadecimal (dep. hex, unbin)
+hex2bin - script to convert hexadecimal to binary (dep. hex, unbin)
+
+
+Usage
+=====
+
+They're dead-simple so they don't require a help panel or a mangpage.
+You can give the input number as an argument:
+
+ $ bin 7
+ 111
+
+Alternatively, if there are no arguments, it reads from stdin:
+
+ $ echo -n "10" | bin
+ 1010
+
+
+Authorship & License
+====================
+
+By MK mcdim.xyz
+Code is licensed under GNU GPL v3+
diff --git a/bin.c b/bin.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#define BUFFER_SIZE 64
+
+static void
+printbits(uintmax_t n)
+{
+ int prec = 0;
+ for (size_t i = 8 * sizeof(uintmax_t); i-- != 0;) {
+ char c;
+ if ((n & (1UL << i)) != 0) {
+ prec = 1;
+ printf("1");
+ } else if (prec != 0) {
+ printf("0");
+ }
+ }
+ printf("\n");
+}
+
+int
+main(int argc, char **argv)
+{
+ if (argc == 2) {
+ uintmax_t n = atoi(argv[1]);
+ printbits(n);
+
+ return 0;
+ }
+
+ char buffer[BUFFER_SIZE];
+ while (fgets(buffer, sizeof(buffer), stdin)) {
+ strtok(buffer, "\n");
+ uintmax_t n = atoi(buffer);
+ printbits(n);
+ }
+
+ return 0;
+}
diff --git a/bin2hex b/bin2hex
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+if [ "$#" -eq 1 ]; then
+ unbin $1 | hex
+ exit 0;
+fi
+
+while read bin; do
+ echo -n "$bin" | unbin | hex;
+done < /dev/stdin
diff --git a/cyclic/cyclic.c b/cyclic/cyclic.c
@@ -0,0 +1,109 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define VERSION "v0.1"
+
+#define LOOPS 4
+
+int max = 10;
+int word[LOOPS];
+int word_count = 0;
+char strword[5];
+
+static int *
+str2word(char *str)
+{
+ int len = strlen(str);
+ for (int i=0; i<len; i++) {
+ word[i] = str[i];
+ }
+ return word;
+}
+
+static int
+equal(int *a, int *b, size_t s)
+{
+ for (int i=0; i<s; i++) {
+ if (a[i] != b[i])
+ return 0;
+ }
+ return 1;
+}
+
+static void
+search(int level, int *indices, int *word)
+{
+ if (level == LOOPS) {
+ word_count++;
+ if (equal(indices, word, 4) != 1) {
+ return;
+ }
+ printf("[words] %d (%d with \"%s\")\n", word_count-1,
+ word_count, strword);
+ printf("[bytes] %d (%d with \"%s\")\n", 4*word_count-LOOPS,
+ LOOPS*word_count, strword);
+ exit(0);
+ }
+
+ for (int i = 'a'; i < 'a'+26; ++i) {
+ indices[level] = i;
+ search(level + 1, indices, word);
+ }
+}
+
+static void
+gen(int level, int *indices)
+{
+ if (level == LOOPS) {
+ word_count++;
+ for (int i = 0; i < LOOPS; ++i) {
+ printf("%c", indices[i]);
+ }
+
+ if (word_count >= max) {
+ printf("\n");
+ exit(0);
+ }
+ } else {
+ for (int i = 'a'; i < 'a'+26; ++i) {
+ indices[level] = i;
+ gen(level + 1, indices);
+ }
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ int indices[LOOPS];
+
+ if (argc == 2) {
+ if (strncmp(argv[1],"-v", 2) == 0) {
+ printf("hextools-cyclic-%s\n", VERSION);
+ return 0;
+ } else if (strncmp(argv[1],"-h", 2) == 0) {
+ printf("hextools-cyclic %s\n"
+ "by Michael Constantine Dimopoulos\n"
+ "https://mcdim.xyz <mk@mcdim.xyz>\n"
+ "\nusage: %s 256"
+ "\n %s -s abcd\n",
+ VERSION, argv[0], argv[0]);
+ return 0;
+ } else {
+ max = atoi(argv[1]);
+ }
+ }
+
+ if (argc == 3 && strncmp(argv[1],"-s", 2) == 0) {
+ strncpy(strword, argv[2], 5);
+ int *word = str2word(argv[2]);
+ search(0, indices, word);
+ return 0;
+ }
+
+ gen(0, indices);
+ printf("\n");
+
+ return 0;
+}
diff --git a/hex.c b/hex.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#define BUFFER_SIZE 128
+
+int
+main(int argc, char **argv)
+{
+ if (argc == 2) {
+ uintmax_t n = atoi(argv[1]);
+ printf("%" PRIxMAX "\n", n);
+
+ return 0;
+ }
+
+ char buffer[BUFFER_SIZE];
+ while (fgets(buffer, sizeof(buffer), stdin)) {
+ strtok(buffer, "\n");
+ uintmax_t n = atoi(buffer);
+ printf("%" PRIxMAX "\n", n);
+ }
+
+ return 0;
+}
diff --git a/unbin.c b/unbin.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#define BUFFER_SIZE 256
+
+static uintmax_t
+bin2int(char *str)
+{
+ uintmax_t f=1;
+ uintmax_t sum=0;
+ for (int i = strlen(str) - 1; i>=0; i--) {
+ if (str[i] == 'b') break;
+ sum = sum + f * (str[i] - '0');
+ f *= 2;
+ }
+ return sum;
+}
+
+int
+main(int argc, char **argv)
+{
+ if (argc == 2) {
+ uintmax_t n = bin2int(argv[1]);
+ printf("%"PRIuMAX"\n", n);
+
+ return 0;
+ }
+
+ char buffer[BUFFER_SIZE];
+ while (fgets(buffer, sizeof(buffer), stdin)) {
+ strtok(buffer, "\n");
+ uintmax_t n = bin2int(buffer);
+ printf("%"PRIuMAX"\n", n);
+ }
+
+ return 0;
+}
diff --git a/unhex.c b/unhex.c
@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#define BUFFER_SIZE 128
+
+static uintmax_t
+hex2int(char *str)
+{
+ uintmax_t n;
+ sscanf(str, "%"SCNxMAX, &n);
+ return n;
+}
+
+int
+main(int argc, char **argv)
+{
+ if (argc == 2) {
+ uintmax_t n = hex2int(argv[1]);
+ printf("%"PRIuMAX"\n", n);
+
+ return 0;
+ }
+
+ char buffer[BUFFER_SIZE];
+ while (fgets(buffer, sizeof(buffer), stdin)) {
+ uintmax_t n = hex2int(buffer);
+ printf("%"PRIuMAX"\n", n);
+ }
+
+ return 0;
+}