Base64Encoder.cpp 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "Base64Encoder.h"
  11. #include "RakMemoryOverride.h"
  12. const char *Base64Map(void) {return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";}
  13. const char *base64Map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  14. // 3/17/2013 must be unsigned char or else it will use negative indices
  15. int Base64Encoding(const unsigned char *inputData, int dataLength, char *outputData)
  16. {
  17. // http://en.wikipedia.org/wiki/Base64
  18. int outputOffset, charCount;
  19. int write3Count;
  20. outputOffset=0;
  21. charCount=0;
  22. int j;
  23. write3Count=dataLength/3;
  24. for (j=0; j < write3Count; j++)
  25. {
  26. // 6 leftmost bits from first byte, shifted to bits 7,8 are 0
  27. outputData[outputOffset++]=base64Map[inputData[j*3+0] >> 2];
  28. if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
  29. // Remaining 2 bits from first byte, placed in position, and 4 high bits from the second byte, masked to ignore bits 7,8
  30. outputData[outputOffset++]=base64Map[((inputData[j*3+0] << 4) | (inputData[j*3+1] >> 4)) & 63];
  31. if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
  32. // 4 low bits from the second byte and the two high bits from the third byte, masked to ignore bits 7,8
  33. outputData[outputOffset++]=base64Map[((inputData[j*3+1] << 2) | (inputData[j*3+2] >> 6)) & 63]; // Third 6 bits
  34. if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
  35. // Last 6 bits from the third byte, masked to ignore bits 7,8
  36. outputData[outputOffset++]=base64Map[inputData[j*3+2] & 63];
  37. if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
  38. }
  39. if (dataLength % 3==1)
  40. {
  41. // One input byte remaining
  42. outputData[outputOffset++]=base64Map[inputData[j*3+0] >> 2];
  43. if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
  44. // Remaining 2 bits from first byte, placed in position, and 4 high bits from the second byte, masked to ignore bits 7,8
  45. outputData[outputOffset++]=base64Map[((inputData[j*3+0] << 4) | (inputData[j*3+1] >> 4)) & 63];
  46. if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
  47. // Pad with two equals
  48. outputData[outputOffset++]='=';
  49. outputData[outputOffset++]='=';
  50. }
  51. else if (dataLength % 3==2)
  52. {
  53. // Two input bytes remaining
  54. // 6 leftmost bits from first byte, shifted to bits 7,8 are 0
  55. outputData[outputOffset++]=base64Map[inputData[j*3+0] >> 2];
  56. if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
  57. // Remaining 2 bits from first byte, placed in position, and 4 high bits from the second byte, masked to ignore bits 7,8
  58. outputData[outputOffset++]=base64Map[((inputData[j*3+0] << 4) | (inputData[j*3+1] >> 4)) & 63];
  59. if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
  60. // 4 low bits from the second byte, followed by 00
  61. outputData[outputOffset++]=base64Map[(inputData[j*3+1] << 2) & 63]; // Third 6 bits
  62. if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
  63. // Pad with one equal
  64. outputData[outputOffset++]='=';
  65. //outputData[outputOffset++]='=';
  66. }
  67. // Append \r\n
  68. outputData[outputOffset++]='\r';
  69. outputData[outputOffset++]='\n';
  70. outputData[outputOffset]=0;
  71. return outputOffset;
  72. }
  73. int Base64Encoding(const unsigned char *inputData, int dataLength, char **outputData)
  74. {
  75. *outputData = (char*) rakMalloc_Ex(dataLength * 2 + 6, _FILE_AND_LINE_);
  76. return Base64Encoding(inputData, dataLength, *outputData);
  77. }
粤ICP备19079148号