andy_shev

base64_for_utf16le.c

Aug 13th, 2020 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <iconv.h>
  6.  
  7. char base64decode_lut[] = {
  8.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  9.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  10.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  11.     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  12.     64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  13.     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  14.     64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  15.     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
  16.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  17.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  18.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  19.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  20.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  21.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  22.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  23.     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  24. };
  25.  
  26. static void base64decode(char *src, char *dest, int len)
  27. {
  28.     int i = 0, slen = strlen(src);
  29.  
  30.     for (i = 0; i < slen && i < len; i += 4, src += 4) {
  31.         char c1 = base64decode_lut[*(src + 0)],
  32.              c2 = base64decode_lut[*(src + 1)],
  33.              c3 = base64decode_lut[*(src + 2)],
  34.              c4 = base64decode_lut[*(src + 3)];
  35.  
  36.         *(dest++) = (c1 & 0x3F) << 0x2 | (c2 & 0x30) >> 0x4;
  37.         *(dest++) = (c3 != 64) ? ((c2 & 0xF) << 0x4 | (c3 & 0x3C) >> 0x2) : '\0';
  38.         *(dest++) = (c4 != 64) ? ((c3 & 0x3) << 0x6 | c4 & 0x3F) : '\0';
  39.     }
  40.     *dest = '\0';       // Append terminator
  41. }
  42.  
  43. static int b64_decode_string(char *source)
  44. {
  45.     iconv_t handle;
  46.     size_t dest_size;
  47.     size_t utf_size;
  48.     char *dest, *d;
  49.     char *utf, *u;
  50.  
  51.     dest_size = strlen(source);
  52.     d = dest = calloc(dest_size, sizeof(char));
  53.     if (!dest)
  54.         return -ENOMEM;
  55.  
  56.     base64decode(source, dest, dest_size);
  57.  
  58.     for (int j = 0; j < dest_size; j++)
  59.         printf("%d: %x\n", j, *((unsigned char *)(dest + j)));
  60.  
  61.     printf("Decode: %s\n", dest);
  62.  
  63.     utf_size = dest_size * 4;
  64.     u = utf = malloc(utf_size);
  65.     if (!utf) {
  66.         free(d);
  67.         return -ENOMEM;
  68.     }
  69.  
  70.     handle = iconv_open("UTF-8", "UTF-16LE");
  71.     if (handle < 0)
  72.         perror("Can't open translation");
  73.  
  74.     iconv(handle, &dest, &dest_size, &utf, &utf_size);
  75.  
  76.     iconv_close(handle);
  77.  
  78.     printf("Decode (UTF-8): %s\n", u);
  79.  
  80.     free(u);
  81.     free(d);
  82.     return 0;
  83. }
  84.  
  85. int main()
  86. {
  87.     return b64_decode_string("SABlAGwAbABvAA==");
  88. }
  89.  
  90.  
Add Comment
Please, Sign In to add comment