Guest User

Untitled

a guest
May 13th, 2020
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. /*******************
  5.     0   1   2   3   4   5   6   7   8   9  ' '
  6.     _   .   _   _   .   _   _   _   _   _   .
  7.    |.| ..| ._| ._| |_| |_. |_. ..| |_| |_| ...
  8.    |_| ..| |_. ._| ..| ._| |_| ..| |_| ._| ...
  9.  
  10. 0: 1 101 111
  11. 1: 0 001 001
  12. 2: 1 011 110
  13. 3: 1 011 011
  14. 4: 0 111 001
  15. 5: 1 110 011
  16. 6: 1 110 111
  17. 7: 1 001 001
  18. 8: 1 111 111
  19. 9: 1 111 011
  20.  : 0 000 000
  21.  
  22. |     ||     ||     ||     ||     ||     ||     ||     ||     ||     ||     |
  23. 110111100010011011110101101101110011110011111011110010011111111111101100000000
  24. | 3  || i  || b  ||  1 || t  || z  || z  || 7  ||  y || f  || /  ||  s ||  A |
  25. */
  26.  
  27. unsigned char base64_decode(char inp){
  28.     if(inp == '+')
  29.         return inp+19;
  30.     if(inp == 057)
  31.         return 63;
  32.     if(inp >= 'a')
  33.         return inp - 'a' + 26;
  34.     if(inp < ':')
  35.         return inp - '0' + 52;
  36.     return inp - 'A';
  37. }
  38.  
  39. int getBase64Bit(char *str, int pos){
  40.     //printf("%2d", pos);
  41.     int charNum = pos / 6;
  42.     int bitNum = pos % 6;
  43.     unsigned char c = base64_decode(str[charNum]);
  44.     return c & 1 << (5 - bitNum);
  45. }
  46. typedef struct cons{
  47.     char car;
  48.     unsigned char count;
  49.     struct cons *cdr;
  50. } cons_t;
  51.  
  52. int getBase64Char(char *code, int offset, char c){
  53.     if(c > 47){
  54.         return getBase64Bit(code, offset + (c - '0')*7);
  55.     }
  56.     return getBase64Bit(code, offset + 70);
  57. }
  58.  
  59. void buildChars(char *bt, char *code,int i,  int offset, char c){
  60.     if(i >= 3){
  61.         return;
  62.     }
  63.     bt[i] = getBase64Char(code, offset + i, c) ? (i == 1?'_':'|'):' ';
  64.     buildChars(bt, code, ++i, offset, c);
  65. }
  66.  
  67. void printMidRow(char *code, cons_t *head, int offset){
  68.     if(head == NULL){
  69.         printf("\n");
  70.         return;
  71.     }
  72.     //printf("hc %c", head->car);
  73.     static char bt[3];
  74.     buildChars(bt, code, 0, offset, head->car);
  75.     printf(" %c%c%c", bt[0], bt[1], bt[2]);
  76.     printMidRow(code, head->cdr, offset);
  77. }
  78.  
  79.  
  80. void reverse1(char *code, cons_t *head, cons_t *stack){
  81.     //printf("%c", head->car);
  82.     if(head->car == 0){
  83.         printMidRow(code, stack, 1);
  84.         printMidRow(code, stack, 4);
  85.     }
  86.     else{
  87.         cons_t *nh = head->cdr;
  88.         head->cdr = stack;
  89.         reverse1(code, nh, head);
  90.     }
  91. }
  92.  
  93.  
  94. void printTopRow(char *code, char *str, cons_t *head){
  95.     if(str[0] == '\0'){
  96.         printf("\n");
  97.         reverse1(code, head, NULL);
  98.         return;
  99.     }
  100.     cons_t newcons;
  101.     (&newcons)->cdr = head;
  102.     newcons.car = str[0];
  103.     newcons.count = head->count+1;
  104.     char bt = getBase64Char(code, 0, newcons.car)? '_' : ' ';
  105.     printf(" %c%c ",str[0], bt);
  106.     printTopRow(code, ++str, &newcons);
  107. }
  108.  
  109. void nh (cons_t *h){
  110.     h->car = 0;
  111.     h->count = 0;
  112.     h->count--;
  113.     h->cdr = 0;
  114. }
  115.  
  116. void main(int argc, char** argv){
  117.     char * code = "3ib1tzz7yf/sA";
  118.     cons_t head;
  119.     nh(&head);
  120.     if(argc<2){
  121.         printf("ERROR: Please provide a single string as a command line argument.\nExample:\n./a.out '123 456'\n");
  122.         return;
  123.     }
  124.     char *mstr = malloc(strlen(argv[1])+1);
  125.     mstr[strlen(argv[1])] = '\0';
  126.     for (int i= 0 ; i < strlen(argv[1]); i++){
  127.         mstr[i] = argv[1][i];
  128.     }
  129.  
  130.     printTopRow(code, mstr, &head);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment