fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5.  
  6. static const unsigned char pr2six[256] =
  7. {
  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.  
  27. int Base64decode( char * bufplain, const char * bufcoded )
  28. {
  29. int nbytesdecoded;
  30. register const unsigned char *bufin;
  31. register unsigned char *bufout;
  32. register int nprbytes;
  33.  
  34. bufin = (const unsigned char *) bufcoded;
  35. while (pr2six[*(bufin++)] <= 63);
  36. nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
  37. nbytesdecoded = ((nprbytes + 3) / 4) * 3;
  38.  
  39. bufout = (unsigned char *) bufplain;
  40. bufin = (const unsigned char *) bufcoded;
  41.  
  42. while (nprbytes > 4)
  43. {
  44. *(bufout++) = (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  45. *(bufout++) = (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  46. *(bufout++) = (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  47. bufin += 4;
  48. nprbytes -= 4;
  49. }
  50.  
  51. if (nprbytes > 1)
  52. {
  53. *(bufout++) = (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  54. }
  55.  
  56. if (nprbytes > 2)
  57. {
  58. *(bufout++) = (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  59. }
  60.  
  61. if (nprbytes > 3)
  62. {
  63. *(bufout++) = (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  64. }
  65.  
  66. *(bufout++) = '\0';
  67. nbytesdecoded -= (4 - nprbytes) & 3;
  68.  
  69. return nbytesdecoded;
  70. }
  71.  
  72.  
  73. int main( void )
  74. {
  75. char * encoded = "eyJtZXNzYWdlIjoxLCJhZGQiOjEsInZhbHVlIjoiMSJ9";
  76. char decoded[ 100 ] = {0};
  77.  
  78. Base64decode( decoded, encoded );
  79.  
  80. printf("String codificada : %s\n", encoded );
  81. printf("String decodificada: %s\n", decoded );
  82.  
  83. return 0;
  84. }
  85.  
  86. /* fim-de-arquivo */
Success #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
String codificada  : eyJtZXNzYWdlIjoxLCJhZGQiOjEsInZhbHVlIjoiMSJ9
String decodificada: {"message":1,"add":1,"value":"1"}