base64.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright (c) 2006-2008 Apple Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. **/
  16. #include "base64.h"
  17. #include <stdlib.h>
  18. #include <string.h>
  19. // base64 tables
  20. static char basis_64[] =
  21. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  22. static signed char index_64[128] =
  23. {
  24. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  25. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  26. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
  27. 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
  28. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  29. 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
  30. -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  31. 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
  32. };
  33. #define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
  34. // base64_encode : base64 encode
  35. //
  36. // value : data to encode
  37. // vlen : length of data
  38. // (result) : new char[] - c-str of result
  39. char *base64_encode(const unsigned char *value, int vlen)
  40. {
  41. char *result = (char *)malloc((vlen * 4) / 3 + 5);
  42. char *out = result;
  43. unsigned char oval;
  44. while (vlen >= 3)
  45. {
  46. *out++ = basis_64[value[0] >> 2];
  47. *out++ = basis_64[((value[0] << 4) & 0x30) | (value[1] >> 4)];
  48. *out++ = basis_64[((value[1] << 2) & 0x3C) | (value[2] >> 6)];
  49. *out++ = basis_64[value[2] & 0x3F];
  50. value += 3;
  51. vlen -= 3;
  52. }
  53. if (vlen > 0)
  54. {
  55. *out++ = basis_64[value[0] >> 2];
  56. oval = (value[0] << 4) & 0x30;
  57. if (vlen > 1) oval |= value[1] >> 4;
  58. *out++ = basis_64[oval];
  59. *out++ = (vlen < 2) ? '=' : basis_64[(value[1] << 2) & 0x3C];
  60. *out++ = '=';
  61. }
  62. *out = '\0';
  63. return result;
  64. }
  65. // base64_decode : base64 decode
  66. //
  67. // value : c-str to decode
  68. // rlen : length of decoded result
  69. // (result) : new unsigned char[] - decoded result
  70. unsigned char *base64_decode(const char *value, int *rlen)
  71. {
  72. int c1, c2, c3, c4;
  73. int vlen = (int)strlen(value);
  74. unsigned char *result =(unsigned char *)malloc((vlen * 3) / 4 + 1);
  75. unsigned char *out = result;
  76. *rlen = 0;
  77. while (1)
  78. {
  79. if (value[0]==0)
  80. return result;
  81. c1 = value[0];
  82. if (CHAR64(c1) == -1)
  83. goto base64_decode_error;;
  84. c2 = value[1];
  85. if (CHAR64(c2) == -1)
  86. goto base64_decode_error;;
  87. c3 = value[2];
  88. if ((c3 != '=') && (CHAR64(c3) == -1))
  89. goto base64_decode_error;;
  90. c4 = value[3];
  91. if ((c4 != '=') && (CHAR64(c4) == -1))
  92. goto base64_decode_error;;
  93. value += 4;
  94. *out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);
  95. *rlen += 1;
  96. if (c3 != '=')
  97. {
  98. *out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);
  99. *rlen += 1;
  100. if (c4 != '=')
  101. {
  102. *out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);
  103. *rlen += 1;
  104. }
  105. }
  106. }
  107. base64_decode_error:
  108. *result = 0;
  109. *rlen = 0;
  110. return result;
  111. }