1
+ /*
2
+ * Copyright (c) 2018 ARM Limited. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ * Licensed under the Apache License, Version 2.0 (the License); you may
5
+ * 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, WITHOUT
12
+ * 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
+
1
17
#include " base64b.h"
2
18
3
19
using namespace std ;
4
20
5
21
static char IntToBase64Char (uint8_t intVal)
6
22
{
7
- const char * base64Digits = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
23
+ const char * base64Digits = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
8
24
return base64Digits[intVal & 0x3F ];
9
25
}
10
26
11
27
#define BASE_64_PAD 0xFF
12
- static base64_result_e Base64CharToInt (char base64, uint8_t * intVal)
28
+ static base64_result_e Base64CharToInt (char base64, uint8_t * intVal)
13
29
{
14
- if (NULL == intVal)
15
- {
30
+ if (NULL == intVal) {
16
31
return BASE64_INVALID_PARAMETER;
17
32
}
18
33
19
34
if ((base64 >= ' A' ) && (base64 <= ' Z' ))
20
- *intVal = base64 - ' A' ;
21
- else if ((base64 >= ' a' )&& (base64 <= ' z' ))
22
- *intVal = base64 - ' a' + 26 ;
23
- else if ((base64 >= ' 0' )&& (base64 <= ' 9' ))
24
- *intVal = base64 - ' 0' + 52 ;
35
+ { *intVal = base64 - ' A' ; }
36
+ else if ((base64 >= ' a' ) && (base64 <= ' z' ))
37
+ { *intVal = base64 - ' a' + 26 ; }
38
+ else if ((base64 >= ' 0' ) && (base64 <= ' 9' ))
39
+ { *intVal = base64 - ' 0' + 52 ; }
25
40
else if (base64 == ' +' )
26
- *intVal = 62 ;
41
+ { *intVal = 62 ; }
27
42
else if (base64 == ' /' )
28
- *intVal = 63 ;
43
+ { *intVal = 63 ; }
29
44
else if (base64 == ' =' )
30
- *intVal = BASE_64_PAD;
31
- else
32
- {
45
+ { *intVal = BASE_64_PAD; }
46
+ else {
33
47
return BASE64_ERROR;
34
48
}
35
-
49
+
36
50
return BASE64_SUCCESS;
37
51
}
38
52
39
- base64_result_e esfs_DecodeNBase64 (const char * string,
40
- uint32_t stringMaxSize,
41
- void * buffer,
42
- uint32_t bufferSize,
43
- uint32_t * lengthWritten,
44
- uint32_t * charsProcessed)
53
+ base64_result_e esfs_DecodeNBase64 (const char * string,
54
+ uint32_t stringMaxSize,
55
+ void * buffer,
56
+ uint32_t bufferSize,
57
+ uint32_t * lengthWritten,
58
+ uint32_t * charsProcessed)
45
59
{
46
60
base64_result_e result = BASE64_SUCCESS;
47
- uint32_t bitOffset =0 ;
48
- uint8_t * writePtr = (uint8_t *)buffer;
49
- uint8_t * bufferEnd = (uint8_t *)buffer + bufferSize;
61
+ uint32_t bitOffset = 0 ;
62
+ uint8_t * writePtr = (uint8_t *)buffer;
63
+ uint8_t * bufferEnd = (uint8_t *)buffer + bufferSize;
50
64
uint8_t tempVal = 0 ;
51
65
uint32_t currPos = 0 ;
52
66
uint32_t localBytesWritten = 0 ;
53
67
uint32_t localCharsProcessed = 0 ;
54
68
bool isEndOfString = false ;
55
69
56
- if ((NULL == string) || (NULL == buffer) || (bufferSize == 0 ))
57
- {
70
+ if ((NULL == string) || (NULL == buffer) || (bufferSize == 0 )) {
58
71
return BASE64_INVALID_PARAMETER;
59
72
}
60
73
61
74
*writePtr = 0 ;
62
75
while (( string[currPos] != 0 ) &&
63
76
( currPos < stringMaxSize ) &&
64
77
( writePtr < bufferEnd ) &&
65
- ( !isEndOfString ))
66
- {
78
+ ( !isEndOfString )) {
67
79
uint8_t val;
68
80
69
81
if (string[currPos] == 0 || currPos >= stringMaxSize)
70
- break ;
71
-
82
+ { break ; }
83
+
72
84
result = Base64CharToInt (string[currPos++], &val);
73
85
if (result != BASE64_SUCCESS)
74
- break ;
86
+ { break ; }
75
87
76
- if (val != BASE_64_PAD)
77
- {
78
- if (bitOffset <= 2 )
79
- {
88
+ if (val != BASE_64_PAD) {
89
+ if (bitOffset <= 2 ) {
80
90
tempVal |= val << (2 - bitOffset);
81
- if (bitOffset == 2 )
82
- {
91
+ if (bitOffset == 2 ) {
83
92
*writePtr++ = tempVal;
84
93
tempVal = 0 ;
85
94
}
86
- }
87
- else
88
- {
95
+ } else {
89
96
*writePtr++ = (uint8_t )(tempVal | (val >> (bitOffset - 2 )));
90
97
tempVal = (uint8_t )(val << (10 - bitOffset));
91
98
}
92
- }
93
- else // found BASE_64_PAD
94
- {
99
+ } else { // found BASE_64_PAD
95
100
// At most two pad characters may occur at the end of the encoded stream
96
101
if (bitOffset == 2 )
97
- isEndOfString = true ; // The last padding byte has been processed.
102
+ { isEndOfString = true ; } // The last padding byte has been processed.
98
103
else if (bitOffset != 4 )
99
- return BASE64_ERROR; // Incorrect padding
104
+ { return BASE64_ERROR; } // Incorrect padding
100
105
}
101
-
106
+
102
107
bitOffset = (bitOffset + 6 ) & 0x7 ;
103
- if (bitOffset == 0 )
104
- {
105
- localBytesWritten = (uint32_t )(writePtr - (uint8_t *)buffer);
108
+ if (bitOffset == 0 ) {
109
+ localBytesWritten = (uint32_t )(writePtr - (uint8_t *)buffer);
106
110
localCharsProcessed = currPos;
107
111
}
108
112
}
109
113
if (charsProcessed == NULL )
110
- localBytesWritten = (uint32_t )(writePtr - (uint8_t *)buffer);
111
- else
112
- *charsProcessed = localCharsProcessed;
114
+ { localBytesWritten = (uint32_t )(writePtr - (uint8_t *)buffer); }
115
+ else
116
+ { *charsProcessed = localCharsProcessed; }
113
117
if (lengthWritten != NULL )
114
- *lengthWritten = localBytesWritten;
118
+ { *lengthWritten = localBytesWritten; }
115
119
else if (bufferSize != localBytesWritten)
116
- return BASE64_BUFFER_TOO_SMALL;
120
+ { return BASE64_BUFFER_TOO_SMALL; }
117
121
118
122
// Check if additional bytes should have been processed but buffer isn't sufficient.
119
123
if (( result == BASE64_SUCCESS ) &&
120
- ( !isEndOfString ) &&
121
- ( string[currPos] != ' =' ) &&
122
- ( string[currPos] != 0 ) &&
123
- ( currPos < stringMaxSize) )
124
- return BASE64_BUFFER_TOO_SMALL;
124
+ ( !isEndOfString ) &&
125
+ ( string[currPos] != ' =' ) &&
126
+ ( string[currPos] != 0 ) &&
127
+ ( currPos < stringMaxSize) )
128
+ { return BASE64_BUFFER_TOO_SMALL; }
125
129
126
130
if (result != BASE64_SUCCESS)
127
- return result;
131
+ { return result; }
128
132
129
133
return BASE64_SUCCESS;
130
134
}
131
135
132
- base64_result_e esfs_EncodeBase64 (const void * buffer, uint32_t bufferSize, char * string, uint32_t stringSize)
136
+ base64_result_e esfs_EncodeBase64 (const void * buffer, uint32_t bufferSize, char * string, uint32_t stringSize)
133
137
{
134
138
uint32_t bitOffset = 0 ;
135
139
136
- const uint8_t * readPtr = (const uint8_t *)buffer;
137
- const uint8_t * bufferEnd = (const uint8_t *)buffer + bufferSize;
140
+ const uint8_t * readPtr = (const uint8_t *)buffer;
141
+ const uint8_t * bufferEnd = (const uint8_t *)buffer + bufferSize;
138
142
139
- char * writePtr = string;
140
- char * stringEnd = string + stringSize - 1 ;
143
+ char * writePtr = string;
144
+ char * stringEnd = string + stringSize - 1 ;
141
145
142
146
if ((NULL == string) || (NULL == buffer) || (stringSize == 0 ))
143
- return BASE64_INVALID_PARAMETER;
147
+ { return BASE64_INVALID_PARAMETER; }
144
148
145
149
stringSize--;
146
- while (readPtr < bufferEnd && writePtr < stringEnd)
147
- {
150
+ while (readPtr < bufferEnd && writePtr < stringEnd) {
148
151
uint8_t tempVal = 0 ;
149
- switch (bitOffset)
150
- {
152
+ switch (bitOffset) {
151
153
case 0 :
152
154
*writePtr++ = IntToBase64Char (*readPtr >> 2 ); // take upper 6 bits
153
155
break ;
154
156
case 6 :
155
157
tempVal = *readPtr++ << 4 ;
156
158
if (readPtr < bufferEnd)
157
- tempVal |= *readPtr >> 4 ;
159
+ { tempVal |= *readPtr >> 4 ; }
158
160
*writePtr++ = IntToBase64Char (tempVal);
159
161
break ;
160
162
case 4 :
161
163
tempVal = *readPtr++ << 2 ;
162
164
if (readPtr < bufferEnd)
163
- tempVal |= *readPtr >> 6 ;
165
+ { tempVal |= *readPtr >> 6 ; }
164
166
*writePtr++ = IntToBase64Char (tempVal);
165
167
break ;
166
168
case 2 :
@@ -171,15 +173,14 @@ base64_result_e esfs_EncodeBase64(const void* buffer, uint32_t bufferSize, char*
171
173
}
172
174
bitOffset = (bitOffset + 6 ) & 0x7 ;
173
175
}
174
- while (bitOffset > 0 && writePtr < stringEnd)
175
- {
176
+ while (bitOffset > 0 && writePtr < stringEnd) {
176
177
*writePtr++ = ' =' ;
177
178
bitOffset = (bitOffset + 6 ) & 0x7 ;
178
179
}
179
180
*writePtr = 0 ;
180
181
181
182
if ((readPtr < bufferEnd) || (bitOffset != 0 ))
182
- return (BASE64_BUFFER_TOO_SMALL);
183
+ { return (BASE64_BUFFER_TOO_SMALL); }
183
184
184
- return (BASE64_SUCCESS);
185
+ return (BASE64_SUCCESS);
185
186
}
0 commit comments