]> pd.if.org Git - pdutils/commitdiff
added uuencode implementation
authorNathan Wagner <nw@hydaspes.if.org>
Thu, 23 Apr 2015 11:07:55 +0000 (11:07 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Thu, 30 Apr 2015 02:28:43 +0000 (02:28 +0000)
utils/uuencode/uuencode.c [new file with mode: 0644]

diff --git a/utils/uuencode/uuencode.c b/utils/uuencode/uuencode.c
new file mode 100644 (file)
index 0000000..9f5df43
--- /dev/null
@@ -0,0 +1,116 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+char oldbase[65];
+
+void do_historical(FILE *in, FILE *out, char *decode) {
+       unsigned char input[45];
+       unsigned char output[62];
+       size_t bytes;
+
+       fprintf(out, "begin %s %s\n", "644", decode);
+       do {
+               unsigned int bits;
+               int i;
+               input[0] = 0; input[1] = 0; input[2] = 0;
+               bytes = fread(input, 1, 45, in);
+
+               for (i = bytes; i < 45; i++) {
+                       input[i] = 0;
+               }
+               output[0] = oldbase[bytes];
+               output[1] = 0;
+
+               for (i=0;i<bytes;i+=3) {
+                       bits = (input[i] << 16) | (input[i+1] << 8) | (input[i+2]);
+
+                       output[i/3*4+4] = oldbase[(bits & 0x3f)];
+                       output[i/3*4+3] = oldbase[((bits >> 6) & 0x3f)];
+                       output[i/3*4+2] = oldbase[((bits >> 12) & 0x3f)];
+                       output[i/3*4+1] = oldbase[((bits >> 18) & 0x3f)];
+                       output[i/3*4+5] = 0;
+               }
+               fprintf(out, "%s\n", output);
+       } while (bytes);
+       fprintf(out, "end\n");
+}
+
+int main(int ac, char *av[]) {
+       int mime = 0;
+       char *file = 0;
+       char *decode = 0;
+       FILE *output = stdout;
+       unsigned char input[3];
+       int groups = 0;
+       size_t bytes;
+       int i;
+
+       for (i = 0; i<64; i++) {
+               oldbase[i] = i + 0x20;
+       }
+       oldbase[0] = '`';
+
+       decode = av[ac-1];
+       if (!strcmp(av[1], "-m")) {
+               mime = 1;
+               if (ac == 4) {
+                       file = av[ac-2];
+               }
+       } else if (ac == 3) {
+               file = av[ac-2];
+       }
+
+       if (file) {
+               if (freopen(file, "r", stdin) == 0) {
+                       perror("can't open file:");
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       if (!mime) {
+               do_historical(stdin, output, decode);
+               exit(EXIT_SUCCESS);
+       }
+
+       fprintf(output, "begin-base64 %s %s\n", "644", decode);
+
+       do {
+               unsigned char out[4];
+               unsigned int bits;
+               int i;
+               input[0] = 0; input[1] = 0; input[2] = 0;
+               bytes = fread(input, 1, 3, stdin);
+               if (!bytes) {
+                       continue;
+               }
+               bits = (input[0] << 16) | (input[1] << 8) | (input[2]);
+
+               out[3] = bits & 0x3f;
+               out[2] = (bits >> 6) & 0x3f;
+               out[1] = (bits >> 12) & 0x3f;
+               out[0] = (bits >> 18) & 0x3f;
+               
+               for (i=0;i < bytes+1; i++) {
+                       fputc(base64[out[i]], stdout);
+               }
+
+               switch (bytes) {
+                       case 1:
+                               fputc('=', stdout);
+                       case 2:
+                               fputc('=', stdout);
+               }
+               if (++groups % 15 == 0) {
+                       fputc('\n', stdout);
+               }
+       } while (bytes);
+
+       if (groups % 15 != 0) {
+               fputc('\n', stdout);
+       }
+       fprintf(stdout, "====\n");
+
+       return 0;
+}