commit b2993441279601fa7e0fe65839c19f09e8396070 Author: Gergely Polonkai Date: Wed Apr 27 20:12:02 2016 +0200 Initial version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..527af68 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +a.out +bin2xpm diff --git a/bin2xpm.c b/bin2xpm.c new file mode 100644 index 0000000..3da3161 --- /dev/null +++ b/bin2xpm.c @@ -0,0 +1,145 @@ +/* + * This file was originally written by Gergely Polonkai, and is + * licensed inder the DBAD: + * + * Do whatever you like with the original work, just don't be a dick. + * + * Being a dick includes - but is not limited to - the following + * instances: + + * 1a. Outright copyright infringement - Don't just copy this and + * change the name. + * 1b. Selling the unmodified original with no work done + * what-so-ever, that's REALLY being a dick. + * 1c. Modifying the original work to contain hidden harmful + * content. That would make you a PROPER dick. + * + * If you become rich through modifications, related works/services, + * or supporting the original work, share the love. Only a dick would + * make loads off this work and not buy the original work's creator(s) + * a pint. + * + * Code is provided with no warranty. Using somebody else's code and + * bitching when it goes wrong makes you a DONKEY dick. Fix the + * problem yourself. A non-dick would submit the fix back. + */ + +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) +{ + FILE *binfile; + struct stat ret; + int i; + signed int found_width = -1; + unsigned int less_height = -1; + int found_height; + void *filemap; + off_t a; + char *xpm_line; + + static int valid_widths[] = {320, 640, 800, 1024, 1280, 1600}; + + if (argc < 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, + "\tWhere is a binary representation of a file,\n" + "created with bindump\n"); + + return 1; + } + + if ((binfile = fopen(argv[1], "r")) == NULL) { + fprintf(stderr, "Unable to open file '%s'\n", argv[1]); + + return 1; + } + + if (stat(argv[1], &ret) != 0) { + fprintf(stderr, "Unable to stat file '%s'\n", argv[1]); + + return 1; + } + + for (i = 0; i < sizeof(valid_widths) / sizeof(int); i++) { + int height; + int best_height; + int diff; + + best_height = (int)ceil(valid_widths[i] * 0.75); + height = (int)ceil(ret.st_size / valid_widths[i]); + diff = abs(best_height - height); + + if (diff < less_height) { + less_height = diff; + found_width = i; + } + } + + if (found_width == -1) { + fprintf(stderr, + "Couldn't find the best size for the image\n" + "(VERY strange... should never happen)\n"); + + return 1; + } + + found_height = (int)ceil((float)ret.st_size + / (float)valid_widths[found_width]); + + filemap = mmap(NULL, + ret.st_size, + PROT_READ, + MAP_PRIVATE, + fileno(binfile), + 0); + + if (filemap == NULL) { + fprintf(stderr, "Unable to mmap file '%s'\n", argv[1]); + return 1; + } + + fprintf(stdout, "/* XPM */\n"); + fprintf(stdout, "static char * data[] = {\n"); + fprintf(stdout, + "\"%d %d 3 1\",\n", + valid_widths[found_width], + found_height); + fprintf(stdout, "\"0\tc #000000\",\n"); + fprintf(stdout, "\"1\tc #ffffff\",\n"); + fprintf(stdout, "\"2\tc #ff00ff\""); + + xpm_line = malloc(valid_widths[found_width] + 1); + + for (a = 0; a < ret.st_size; a++) { + if (a % valid_widths[found_width] == 0) { + if (a != 0) { + fprintf(stdout, ",\n"); + fprintf(stdout, "\"%s\"", xpm_line); + } + + memset(xpm_line, 0, valid_widths[found_width] + 1); + } + + xpm_line[a % valid_widths[found_width]] = ((char *)filemap)[a]; + } + + if (*xpm_line != 0) { + for (i = strlen(xpm_line); i < valid_widths[found_width]; i++) { + xpm_line[i] = '2'; + } + + fprintf(stdout, ",\n\"%s\"};\n", xpm_line); + } + + munmap(filemap, ret.st_size); + + return 0; +} diff --git a/bindump.c b/bindump.c new file mode 100644 index 0000000..db86a27 --- /dev/null +++ b/bindump.c @@ -0,0 +1,52 @@ +/* + * This file was originally written by Gergely Polonkai, and is + * licensed inder the DBAD: + * + * Do whatever you like with the original work, just don't be a dick. + * + * Being a dick includes - but is not limited to - the following + * instances: + + * 1a. Outright copyright infringement - Don't just copy this and + * change the name. + * 1b. Selling the unmodified original with no work done + * what-so-ever, that's REALLY being a dick. + * 1c. Modifying the original work to contain hidden harmful + * content. That would make you a PROPER dick. + * + * If you become rich through modifications, related works/services, + * or supporting the original work, share the love. Only a dick would + * make loads off this work and not buy the original work's creator(s) + * a pint. + * + * Code is provided with no warranty. Using somebody else's code and + * bitching when it goes wrong makes you a DONKEY dick. Fix the + * problem yourself. A non-dick would submit the fix back. + */ + +#include + +int +main(int argc, char **argv) +{ + FILE *file; + int c; + + if ((file = fopen(argv[1], "r")) == NULL) { + fprintf(stderr, "Unable to open file!\n"); + + return 1; + } + + while ((c = fgetc(file)) != EOF) { + int i; + + // For each byte in a file, print the byte as a binary string + // (a bunch of '0' and '1' characters + for (i = 7; i>= 0; i--) { + printf(((c & (2 ^i)) != 0) ? "0" : "1"); + } + } + + return 0; +} diff --git a/i-love-you.c b/i-love-you.c new file mode 100644 index 0000000..e8de03e --- /dev/null +++ b/i-love-you.c @@ -0,0 +1,9 @@ +#include + +int +main(int argc, char **argv) +{ + printf("I love you!\n"); + + return 0; +} diff --git a/programmer.c b/programmer.c new file mode 100644 index 0000000..137cd95 --- /dev/null +++ b/programmer.c @@ -0,0 +1,9 @@ +#include + +int +main(int argc, char **argv) +{ + printf("I'm a C programmer on GNU/Linux.\n"); + + return 0; +}