base64.c 3.7 KB

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