SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Base64Encoder.cpp
Go to the documentation of this file.
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  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
16 #include "slikenet/Base64Encoder.h"
18 
19 const char *Base64Map(void) {return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";}
20 const char *base64Map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
21 
22 // 3/17/2013 must be unsigned char or else it will use negative indices
23 int Base64Encoding(const unsigned char *inputData, int dataLength, char *outputData)
24 {
25  // http://en.wikipedia.org/wiki/Base64
26 
27  int outputOffset, charCount;
28  int write3Count;
29  outputOffset=0;
30  charCount=0;
31  int j;
32 
33  write3Count=dataLength/3;
34  for (j=0; j < write3Count; j++)
35  {
36  // 6 leftmost bits from first byte, shifted to bits 7,8 are 0
37  outputData[outputOffset++]=base64Map[inputData[j*3+0] >> 2];
38  if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
39 
40  // Remaining 2 bits from first byte, placed in position, and 4 high bits from the second byte, masked to ignore bits 7,8
41  outputData[outputOffset++]=base64Map[((inputData[j*3+0] << 4) | (inputData[j*3+1] >> 4)) & 63];
42  if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
43 
44  // 4 low bits from the second byte and the two high bits from the third byte, masked to ignore bits 7,8
45  outputData[outputOffset++]=base64Map[((inputData[j*3+1] << 2) | (inputData[j*3+2] >> 6)) & 63]; // Third 6 bits
46  if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
47 
48  // Last 6 bits from the third byte, masked to ignore bits 7,8
49  outputData[outputOffset++]=base64Map[inputData[j*3+2] & 63];
50  if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
51  }
52 
53  if (dataLength % 3==1)
54  {
55  // One input byte remaining
56  outputData[outputOffset++]=base64Map[inputData[j*3+0] >> 2];
57  if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
58 
59  // Remaining 2 bits from first byte, placed in position, and 4 high bits from the second byte, masked to ignore bits 7,8
60  outputData[outputOffset++]=base64Map[((inputData[j*3+0] << 4) | (inputData[j*3+1] >> 4)) & 63];
61  if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
62 
63  // Pad with two equals
64  outputData[outputOffset++]='=';
65  outputData[outputOffset++]='=';
66  }
67  else if (dataLength % 3==2)
68  {
69  // Two input bytes remaining
70 
71  // 6 leftmost bits from first byte, shifted to bits 7,8 are 0
72  outputData[outputOffset++]=base64Map[inputData[j*3+0] >> 2];
73  if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
74 
75  // Remaining 2 bits from first byte, placed in position, and 4 high bits from the second byte, masked to ignore bits 7,8
76  outputData[outputOffset++]=base64Map[((inputData[j*3+0] << 4) | (inputData[j*3+1] >> 4)) & 63];
77  if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
78 
79  // 4 low bits from the second byte, followed by 00
80  outputData[outputOffset++]=base64Map[(inputData[j*3+1] << 2) & 63]; // Third 6 bits
81  if ((++charCount % 76)==0) {outputData[outputOffset++]='\r'; outputData[outputOffset++]='\n'; charCount=0;}
82 
83  // Pad with one equal
84  outputData[outputOffset++]='=';
85  //outputData[outputOffset++]='=';
86  }
87 
88  // Append \r\n
89  outputData[outputOffset++]='\r';
90  outputData[outputOffset++]='\n';
91  outputData[outputOffset]=0;
92 
93  return outputOffset;
94 }
95 
96 int Base64Encoding(const unsigned char *inputData, int dataLength, char **outputData)
97 {
98  *outputData = (char*) rakMalloc_Ex(dataLength * 2 + 6, _FILE_AND_LINE_);
99  return Base64Encoding(inputData, dataLength, *outputData);
100 }