Guest User

Untitled

a guest
Oct 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. #include "base64.h"
  2.  
  3. #include <math.h>
  4.  
  5. const UInt8 kBase64EncodeTable[64] = {
  6. /* 0 */ 'A', /* 1 */ 'B', /* 2 */ 'C', /* 3 */ 'D',
  7. /* 4 */ 'E', /* 5 */ 'F', /* 6 */ 'G', /* 7 */ 'H',
  8. /* 8 */ 'I', /* 9 */ 'J', /* 10 */ 'K', /* 11 */ 'L',
  9. /* 12 */ 'M', /* 13 */ 'N', /* 14 */ 'O', /* 15 */ 'P',
  10. /* 16 */ 'Q', /* 17 */ 'R', /* 18 */ 'S', /* 19 */ 'T',
  11. /* 20 */ 'U', /* 21 */ 'V', /* 22 */ 'W', /* 23 */ 'X',
  12. /* 24 */ 'Y', /* 25 */ 'Z', /* 26 */ 'a', /* 27 */ 'b',
  13. /* 28 */ 'c', /* 29 */ 'd', /* 30 */ 'e', /* 31 */ 'f',
  14. /* 32 */ 'g', /* 33 */ 'h', /* 34 */ 'i', /* 35 */ 'j',
  15. /* 36 */ 'k', /* 37 */ 'l', /* 38 */ 'm', /* 39 */ 'n',
  16. /* 40 */ 'o', /* 41 */ 'p', /* 42 */ 'q', /* 43 */ 'r',
  17. /* 44 */ 's', /* 45 */ 't', /* 46 */ 'u', /* 47 */ 'v',
  18. /* 48 */ 'w', /* 49 */ 'x', /* 50 */ 'y', /* 51 */ 'z',
  19. /* 52 */ '0', /* 53 */ '1', /* 54 */ '2', /* 55 */ '3',
  20. /* 56 */ '4', /* 57 */ '5', /* 58 */ '6', /* 59 */ '7',
  21. /* 60 */ '8', /* 61 */ '9', /* 62 */ '+', /* 63 */ '/'
  22. };
  23.  
  24. /*
  25. -1 = Base64 end of data marker.
  26. -2 = White space (tabs, cr, lf, space)
  27. -3 = Noise (all non whitespace, non-base64 characters)
  28. -4 = Dangerous noise
  29. -5 = Illegal noise (null byte)
  30. */
  31.  
  32. const SInt8 kBase64DecodeTable[128] = {
  33. /* 0x00 */ -5, /* 0x01 */ -3, /* 0x02 */ -3, /* 0x03 */ -3,
  34. /* 0x04 */ -3, /* 0x05 */ -3, /* 0x06 */ -3, /* 0x07 */ -3,
  35. /* 0x08 */ -3, /* 0x09 */ -2, /* 0x0a */ -2, /* 0x0b */ -2,
  36. /* 0x0c */ -2, /* 0x0d */ -2, /* 0x0e */ -3, /* 0x0f */ -3,
  37. /* 0x10 */ -3, /* 0x11 */ -3, /* 0x12 */ -3, /* 0x13 */ -3,
  38. /* 0x14 */ -3, /* 0x15 */ -3, /* 0x16 */ -3, /* 0x17 */ -3,
  39. /* 0x18 */ -3, /* 0x19 */ -3, /* 0x1a */ -3, /* 0x1b */ -3,
  40. /* 0x1c */ -3, /* 0x1d */ -3, /* 0x1e */ -3, /* 0x1f */ -3,
  41. /* ' ' */ -2, /* '!' */ -3, /* '"' */ -3, /* '#' */ -3,
  42. /* '$' */ -3, /* '%' */ -3, /* '&' */ -3, /* ''' */ -3,
  43. /* '(' */ -3, /* ')' */ -3, /* '*' */ -3, /* '+' */ 62,
  44. /* ',' */ -3, /* '-' */ -3, /* '.' */ -3, /* '/' */ 63,
  45. /* '0' */ 52, /* '1' */ 53, /* '2' */ 54, /* '3' */ 55,
  46. /* '4' */ 56, /* '5' */ 57, /* '6' */ 58, /* '7' */ 59,
  47. /* '8' */ 60, /* '9' */ 61, /* ':' */ -3, /* ';' */ -3,
  48. /* '<' */ -3, /* '=' */ -1, /* '>' */ -3, /* '?' */ -3,
  49. /* '@' */ -3, /* 'A' */ 0, /* 'B' */ 1, /* 'C' */ 2,
  50. /* 'D' */ 3, /* 'E' */ 4, /* 'F' */ 5, /* 'G' */ 6,
  51. /* 'H' */ 7, /* 'I' */ 8, /* 'J' */ 9, /* 'K' */ 10,
  52. /* 'L' */ 11, /* 'M' */ 12, /* 'N' */ 13, /* 'O' */ 14,
  53. /* 'P' */ 15, /* 'Q' */ 16, /* 'R' */ 17, /* 'S' */ 18,
  54. /* 'T' */ 19, /* 'U' */ 20, /* 'V' */ 21, /* 'W' */ 22,
  55. /* 'X' */ 23, /* 'Y' */ 24, /* 'Z' */ 25, /* '[' */ -3,
  56. /* '\' */ -3, /* ']' */ -3, /* '^' */ -3, /* '_' */ -3,
  57. /* '`' */ -3, /* 'a' */ 26, /* 'b' */ 27, /* 'c' */ 28,
  58. /* 'd' */ 29, /* 'e' */ 30, /* 'f' */ 31, /* 'g' */ 32,
  59. /* 'h' */ 33, /* 'i' */ 34, /* 'j' */ 35, /* 'k' */ 36,
  60. /* 'l' */ 37, /* 'm' */ 38, /* 'n' */ 39, /* 'o' */ 40,
  61. /* 'p' */ 41, /* 'q' */ 42, /* 'r' */ 43, /* 's' */ 44,
  62. /* 't' */ 45, /* 'u' */ 46, /* 'v' */ 47, /* 'w' */ 48,
  63. /* 'x' */ 49, /* 'y' */ 50, /* 'z' */ 51, /* '{' */ -3,
  64. /* '|' */ -3, /* '}' */ -3, /* '~' */ -3, /* 0x7f */ -3
  65. };
  66.  
  67. const UInt8 kBits_00000011 = 0x03;
  68. const UInt8 kBits_00001111 = 0x0F;
  69. const UInt8 kBits_00110000 = 0x30;
  70. const UInt8 kBits_00111100 = 0x3C;
  71. const UInt8 kBits_00111111 = 0x3F;
  72. const UInt8 kBits_11000000 = 0xC0;
  73. const UInt8 kBits_11110000 = 0xF0;
  74. const UInt8 kBits_11111100 = 0xFC;
  75.  
  76. size_t EstimateBas64EncodedDataSize(size_t inDataSize)
  77. {
  78. size_t theEncodedDataSize = (int)ceil(inDataSize / 3.0) * 4;
  79. theEncodedDataSize = theEncodedDataSize / 72 * 74 + theEncodedDataSize % 72;
  80. return(theEncodedDataSize);
  81. }
  82.  
  83. size_t EstimateBas64DecodedDataSize(size_t inDataSize)
  84. {
  85. size_t theDecodedDataSize = (int)ceil(inDataSize / 4.0) * 3;
  86. //theDecodedDataSize = theDecodedDataSize / 72 * 74 + theDecodedDataSize % 72;
  87. return(theDecodedDataSize);
  88. }
  89.  
  90. bool Base64EncodeData(const void *inInputData, size_t inInputDataSize, char *outOutputData, size_t *ioOutputDataSize, BOOL wrapped)
  91. {
  92. size_t theEncodedDataSize = EstimateBas64EncodedDataSize(inInputDataSize);
  93. if (*ioOutputDataSize < theEncodedDataSize)
  94. return(false);
  95. *ioOutputDataSize = theEncodedDataSize;
  96. const UInt8 *theInPtr = (const UInt8 *)inInputData;
  97. UInt32 theInIndex = 0, theOutIndex = 0;
  98. for (; theInIndex < (inInputDataSize / 3) * 3; theInIndex += 3)
  99. {
  100. outOutputData[theOutIndex++] = kBase64EncodeTable[(theInPtr[theInIndex] & kBits_11111100) >> 2];
  101. outOutputData[theOutIndex++] = kBase64EncodeTable[(theInPtr[theInIndex] & kBits_00000011) << 4 | (theInPtr[theInIndex + 1] & kBits_11110000) >> 4];
  102. outOutputData[theOutIndex++] = kBase64EncodeTable[(theInPtr[theInIndex + 1] & kBits_00001111) << 2 | (theInPtr[theInIndex + 2] & kBits_11000000) >> 6];
  103. outOutputData[theOutIndex++] = kBase64EncodeTable[(theInPtr[theInIndex + 2] & kBits_00111111) >> 0];
  104. if (wrapped && (theOutIndex % 74 == 72))
  105. {
  106. outOutputData[theOutIndex++] = '\r';
  107. outOutputData[theOutIndex++] = '\n';
  108. }
  109. }
  110. const size_t theRemainingBytes = inInputDataSize - theInIndex;
  111. if (theRemainingBytes == 1)
  112. {
  113. outOutputData[theOutIndex++] = kBase64EncodeTable[(theInPtr[theInIndex] & kBits_11111100) >> 2];
  114. outOutputData[theOutIndex++] = kBase64EncodeTable[(theInPtr[theInIndex] & kBits_00000011) << 4 | (0 & kBits_11110000) >> 4];
  115. outOutputData[theOutIndex++] = '=';
  116. outOutputData[theOutIndex++] = '=';
  117. if (wrapped && (theOutIndex % 74 == 72))
  118. {
  119. outOutputData[theOutIndex++] = '\r';
  120. outOutputData[theOutIndex++] = '\n';
  121. }
  122. }
  123. else if (theRemainingBytes == 2)
  124. {
  125. outOutputData[theOutIndex++] = kBase64EncodeTable[(theInPtr[theInIndex] & kBits_11111100) >> 2];
  126. outOutputData[theOutIndex++] = kBase64EncodeTable[(theInPtr[theInIndex] & kBits_00000011) << 4 | (theInPtr[theInIndex + 1] & kBits_11110000) >> 4];
  127. outOutputData[theOutIndex++] = kBase64EncodeTable[(theInPtr[theInIndex + 1] & kBits_00001111) << 2 | (0 & kBits_11000000) >> 6];
  128. outOutputData[theOutIndex++] = '=';
  129. if (wrapped && (theOutIndex % 74 == 72))
  130. {
  131. outOutputData[theOutIndex++] = '\r';
  132. outOutputData[theOutIndex++] = '\n';
  133. }
  134. }
  135. return(true);
  136. }
  137.  
  138. bool Base64DecodeData(const void *inInputData, size_t inInputDataSize, void *ioOutputData, size_t *ioOutputDataSize)
  139. {
  140. memset(ioOutputData, '.', *ioOutputDataSize);
  141.  
  142. size_t theDecodedDataSize = EstimateBas64DecodedDataSize(inInputDataSize);
  143. if (*ioOutputDataSize < theDecodedDataSize)
  144. return(false);
  145. *ioOutputDataSize = 0;
  146. const UInt8 *theInPtr = (const UInt8 *)inInputData;
  147. UInt8 *theOutPtr = (UInt8 *)ioOutputData;
  148. size_t theInIndex = 0, theOutIndex = 0;
  149. UInt8 theOutputOctet;
  150. size_t theSequence = 0;
  151. for (; theInIndex < inInputDataSize; )
  152. {
  153. SInt8 theSextet = 0;
  154.  
  155. SInt8 theCurrentInputOctet = theInPtr[theInIndex];
  156. theSextet = kBase64DecodeTable[theCurrentInputOctet];
  157. if (theSextet == -1)
  158. break;
  159. while (theSextet == -2)
  160. {
  161. theCurrentInputOctet = theInPtr[++theInIndex];
  162. theSextet = kBase64DecodeTable[theCurrentInputOctet];
  163. }
  164. while (theSextet == -3)
  165. {
  166. theCurrentInputOctet = theInPtr[++theInIndex];
  167. theSextet = kBase64DecodeTable[theCurrentInputOctet];
  168. }
  169. if (theSequence == 0)
  170. {
  171. theOutputOctet = (theSextet >= 0 ? theSextet : 0) << 2 & kBits_11111100;
  172. }
  173. else if (theSequence == 1)
  174. {
  175. theOutputOctet |= (theSextet >- 0 ? theSextet : 0) >> 4 & kBits_00000011;
  176. theOutPtr[theOutIndex++] = theOutputOctet;
  177. }
  178. else if (theSequence == 2)
  179. {
  180. theOutputOctet = (theSextet >= 0 ? theSextet : 0) << 4 & kBits_11110000;
  181. }
  182. else if (theSequence == 3)
  183. {
  184. theOutputOctet |= (theSextet >= 0 ? theSextet : 0) >> 2 & kBits_00001111;
  185. theOutPtr[theOutIndex++] = theOutputOctet;
  186. }
  187. else if (theSequence == 4)
  188. {
  189. theOutputOctet = (theSextet >= 0 ? theSextet : 0) << 6 & kBits_11000000;
  190. }
  191. else if (theSequence == 5)
  192. {
  193. theOutputOctet |= (theSextet >= 0 ? theSextet : 0) >> 0 & kBits_00111111;
  194. theOutPtr[theOutIndex++] = theOutputOctet;
  195. }
  196. theSequence = (theSequence + 1) % 6;
  197. if (theSequence != 2 && theSequence != 4)
  198. theInIndex++;
  199. }
  200. *ioOutputDataSize = theOutIndex;
  201. return(true);
  202. }
Add Comment
Please, Sign In to add comment