You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.8 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-base64.c,v 1.2 2006/02/25 10:09:29 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: hero@persua.de
  23. ******************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include "nanohttp-base64.h"
  28. static const char cb64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  29. static const char cd64[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
  30. /**
  31. * encode 3 8-bit binary bytes as 4 '6-bit' characters
  32. */
  33. static void encodeblock(unsigned char in[3], unsigned char out[4], int len)
  34. {
  35. out[0] = cb64[in[0] >> 2];
  36. out[1] = cb64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
  37. out[2] = (unsigned char)(len > 1 ? cb64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)] : '=');
  38. out[3] = (unsigned char)(len > 2 ? cb64[in[2] & 0x3f] : '=');
  39. return;
  40. }
  41. /**
  42. * base64 encode a string.
  43. */
  44. void base64_encode(const unsigned char *instr, unsigned char *outstr)
  45. {
  46. unsigned char in[3], out[4];
  47. int i, len;
  48. while (*instr)
  49. {
  50. len = 0;
  51. for (i = 0; i < 3; i++)
  52. {
  53. if ((in[i] = (unsigned char)*instr))
  54. {
  55. len++;
  56. instr++;
  57. }
  58. }
  59. if (len)
  60. {
  61. encodeblock(in, out, len);
  62. for (i = 0; i < 4; i++)
  63. *outstr++ = out[i];
  64. }
  65. }
  66. }
  67. /**
  68. * decode 4 '6-bit' characters into 3 8-bit binary bytes
  69. */
  70. static void decodeblock(unsigned char in[4], unsigned char out[3])
  71. {
  72. out[0] = (unsigned char)(in[0] << 2 | in[1] >> 4);
  73. out[1] = (unsigned char)(in[1] << 4 | in[2] >> 2);
  74. out[2] = (unsigned char)(((in[2] << 6) & 0xc0) | in[3]);
  75. return;
  76. }
  77. /**
  78. * decode a base64 encoded string (maybe broken...)
  79. */
  80. void base64_decode(const unsigned char *instr, unsigned char *outstr)
  81. {
  82. unsigned char in[4], out[3], v;
  83. int i, len;
  84. while (*instr)
  85. {
  86. for (len = 0, i = 0; i < 4 && *instr; i++)
  87. {
  88. v = 0;
  89. while (*instr && v == 0)
  90. {
  91. v = *instr++;
  92. v = (unsigned char)((v < 43 || v > 122) ? 0 : cd64[v - 43]);
  93. if (v)
  94. v = (unsigned char)((v == '$') ? 0 : v - 61);
  95. }
  96. if (*instr)
  97. {
  98. len++;
  99. if (v)
  100. in[i] = (unsigned char)(v - 1);
  101. }
  102. else
  103. {
  104. in[i] = 0;
  105. }
  106. }
  107. if (len)
  108. {
  109. decodeblock(in, out);
  110. for (i = 0; i < len - 1; i++)
  111. *outstr++ = out[i];
  112. }
  113. }
  114. }
  115. #ifdef BASE64_TEST_CASE_FROM_RFC2617
  116. #include <stdio.h>
  117. int main(int argc, char **argv) {
  118. unsigned char *instr = "QWxhZGRpbjpvcGVuIHNlc2FtZQ==";
  119. unsigned char *result = "Aladdin:open sesame";
  120. unsigned char instr2[80];
  121. unsigned char outstr[80];
  122. bzero(outstr, 80);
  123. base64_decode(instr, outstr);
  124. printf("\"%s\" => \"%s\"\n", instr, outstr);
  125. if (strcmp(outstr, result))
  126. printf("base64_decode failed\n");
  127. strcpy(instr2, outstr);
  128. bzero(outstr, 80);
  129. base64_encode(instr2, outstr);
  130. printf("\"%s\" => \"%s\"\n", instr2, outstr);
  131. if (strcmp(outstr, instr))
  132. printf("base64_encode failed\n");
  133. return 0;
  134. }
  135. #endif