Last active
June 29, 2024 18:13
-
-
Save afmika/1d94e1e280684ddb80824458388217d0 to your computer and use it in GitHub Desktop.
An utility for making quines, basically..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $ gcc pack.c -o pack | |
// $ ./pack e "Hello World!" | ./pack d | |
// Hello World! | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdint.h> | |
#include <inttypes.h> | |
#include <math.h> | |
typedef uint64_t box; | |
int help(int argc) { | |
int exit = 0; | |
if (argc != 1) { | |
printf("Bad command\n"); | |
exit = 1; | |
} | |
printf("Concept: pack a string into a sequence of 64 bits integers.\nAn utility for making quines, basically..\n"); | |
printf("Usage:\n"); | |
printf("pack e \"Hello World\" > out.txt\n"); | |
printf("pack d < out.txt\n"); | |
printf("\nNo-op test:\n"); | |
printf("pack e \"Hello World\" | pack d\n"); | |
return exit; | |
} | |
void encode(const char str[]) { | |
size_t len = strlen(str); | |
size_t i = 0; | |
printf("%i\n", (len / 8) + (len % 8 != 0)); | |
while (i < len) { | |
box enc = 0; | |
for (size_t j = 0; j < 8; j++) { | |
// char c = i + j < len ? 1 : '\0'; // debug 2**(k*8) + 2**((k-1)*8) + .. + 1 | |
char c = i + j < len ? str[i + j] : '\0'; | |
enc = enc | (box)c << j * 8; | |
} | |
i += 8; | |
printf("%" PRIx64 "\n", enc); | |
} | |
} | |
void decode() { | |
size_t count; | |
scanf("%i", &count); | |
while (count--) { | |
uint64_t n; | |
scanf("%" PRIx64 "", &n); | |
for (size_t i = 0; i < 8 * sizeof(box); i += 8) { | |
char c = 0xff & (n >> i); | |
if (c == '\0') | |
break; | |
else | |
putchar(c); | |
} | |
} | |
} | |
int main(int argc, const char* argv[]) { | |
if (argc == 2 && *argv[1] == 'd') { | |
decode(); | |
return 0; | |
} else if (argc == 3 && *argv[1] == 'e') { | |
encode(argv[2]); | |
return 0; | |
} | |
return help(argc); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment