base64.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. while (vlen >= 3)
  44. {
  45. *out++ = basis_64[value[0] >> 2];
  46. *out++ = basis_64[((value[0] << 4) & 0x30) | (value[1] >> 4)];
  47. *out++ = basis_64[((value[1] << 2) & 0x3C) | (value[2] >> 6)];
  48. *out++ = basis_64[value[2] & 0x3F];
  49. value += 3;
  50. vlen -= 3;
  51. }
  52. if (vlen > 0)
  53. {
  54. *out++ = basis_64[value[0] >> 2];
  55. unsigned char oval = (value[0] << 4) & 0x30;
  56. if (vlen > 1) oval |= value[1] >> 4;
  57. *out++ = basis_64[oval];
  58. *out++ = (vlen < 2) ? '=' : basis_64[(value[1] << 2) & 0x3C];
  59. *out++ = '=';
  60. }
  61. *out = '\0';
  62. return result;
  63. }
  64. // base64_decode : base64 decode
  65. //
  66. // value : c-str to decode
  67. // rlen : length of decoded result
  68. // (result) : new unsigned char[] - decoded result
  69. unsigned char *base64_decode(const char *value, int *rlen)
  70. {
  71. *rlen = 0;
  72. int c1, c2, c3, c4;
  73. int vlen = strlen(value);
  74. unsigned char *result =(unsigned char *)malloc((vlen * 3) / 4 + 1);
  75. unsigned char *out = result;
  76. while (1)
  77. {
  78. if (value[0]==0)
  79. return result;
  80. c1 = value[0];
  81. if (CHAR64(c1) == -1)
  82. goto base64_decode_error;;
  83. c2 = value[1];
  84. if (CHAR64(c2) == -1)
  85. goto base64_decode_error;;
  86. c3 = value[2];
  87. if ((c3 != '=') && (CHAR64(c3) == -1))
  88. goto base64_decode_error;;
  89. c4 = value[3];
  90. if ((c4 != '=') && (CHAR64(c4) == -1))
  91. goto base64_decode_error;;
  92. value += 4;
  93. *out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);
  94. *rlen += 1;
  95. if (c3 != '=')
  96. {
  97. *out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);
  98. *rlen += 1;
  99. if (c4 != '=')
  100. {
  101. *out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);
  102. *rlen += 1;
  103. }
  104. }
  105. }
  106. base64_decode_error:
  107. *result = 0;
  108. *rlen = 0;
  109. return result;
  110. }