base64.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <stdio.h>
  20. #include <errno.h>
  21. void die2(const char *message) {
  22. if(errno) {
  23. perror(message);
  24. } else {
  25. printf("ERROR: %s\n", message);
  26. }
  27. exit(1);
  28. }
  29. // base64 tables
  30. static char basis_64[] =
  31. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  32. static signed char index_64[128] =
  33. {
  34. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  35. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  36. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
  37. 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
  38. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  39. 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
  40. -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  41. 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
  42. };
  43. #define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
  44. // base64_encode : base64 encode
  45. //
  46. // value : data to encode
  47. // vlen : length of data
  48. // (result) : new char[] - c-str of result
  49. char *base64_encode(const unsigned char *value, int vlen)
  50. {
  51. char *result = (char *)malloc((vlen * 4) / 3 + 5);
  52. if(result == NULL) die2("Memory allocation failed");
  53. char *out = result;
  54. while (vlen >= 3)
  55. {
  56. *out++ = basis_64[value[0] >> 2];
  57. *out++ = basis_64[((value[0] << 4) & 0x30) | (value[1] >> 4)];
  58. *out++ = basis_64[((value[1] << 2) & 0x3C) | (value[2] >> 6)];
  59. *out++ = basis_64[value[2] & 0x3F];
  60. value += 3;
  61. vlen -= 3;
  62. }
  63. if (vlen > 0)
  64. {
  65. *out++ = basis_64[value[0] >> 2];
  66. unsigned char oval = (value[0] << 4) & 0x30;
  67. if (vlen > 1) oval |= value[1] >> 4;
  68. *out++ = basis_64[oval];
  69. *out++ = (vlen < 2) ? '=' : basis_64[(value[1] << 2) & 0x3C];
  70. *out++ = '=';
  71. }
  72. *out = '\0';
  73. return result;
  74. }
  75. // base64_decode : base64 decode
  76. //
  77. // value : c-str to decode
  78. // rlen : length of decoded result
  79. // (result) : new unsigned char[] - decoded result
  80. unsigned char *base64_decode(const char *value, int *rlen)
  81. {
  82. *rlen = 0;
  83. int c1, c2, c3, c4;
  84. int vlen = strlen(value);
  85. unsigned char *result =(unsigned char *)malloc((vlen * 3) / 4 + 1);
  86. if(result == NULL) die2("Memory allocation failed");
  87. unsigned char *out = result;
  88. while (1)
  89. {
  90. if (value[0]==0)
  91. return result;
  92. c1 = value[0];
  93. if (CHAR64(c1) == -1)
  94. goto base64_decode_error;;
  95. c2 = value[1];
  96. if (CHAR64(c2) == -1)
  97. goto base64_decode_error;;
  98. c3 = value[2];
  99. if ((c3 != '=') && (CHAR64(c3) == -1))
  100. goto base64_decode_error;;
  101. c4 = value[3];
  102. if ((c4 != '=') && (CHAR64(c4) == -1))
  103. goto base64_decode_error;;
  104. value += 4;
  105. *out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);
  106. *rlen += 1;
  107. if (c3 != '=')
  108. {
  109. *out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);
  110. *rlen += 1;
  111. if (c4 != '=')
  112. {
  113. *out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);
  114. *rlen += 1;
  115. }
  116. }
  117. }
  118. base64_decode_error:
  119. *result = 0;
  120. *rlen = 0;
  121. return result;
  122. }