@@ -31,26 +31,26 @@ static base64_result_e Base64CharToInt(char base64, uint8_t *intVal)
31
31
return BASE64_INVALID_PARAMETER;
32
32
}
33
33
34
- if ((base64 >= ' A' ) && (base64 <= ' Z' ))
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 ; }
40
- else if (base64 == ' +' )
41
- { *intVal = 62 ; }
42
- else if (base64 == ' /' )
43
- { *intVal = 63 ; }
44
- else if (base64 == ' =' )
45
- { *intVal = BASE_64_PAD; }
46
- else {
34
+ if ((base64 >= ' A' ) && (base64 <= ' Z' )) {
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 ;
40
+ } else if (base64 == ' +' ) {
41
+ *intVal = 62 ;
42
+ } else if (base64 == ' /' ) {
43
+ *intVal = 63 ;
44
+ } else if (base64 == ' =' ) {
45
+ *intVal = BASE_64_PAD;
46
+ } else {
47
47
return BASE64_ERROR;
48
48
}
49
49
50
50
return BASE64_SUCCESS;
51
51
}
52
52
53
- base64_result_e esfs_DecodeNBase64 (const char *string,
53
+ base64_result_e trng_DecodeNBase64 (const char *string,
54
54
uint32_t stringMaxSize,
55
55
void *buffer,
56
56
uint32_t bufferSize,
@@ -72,18 +72,20 @@ base64_result_e esfs_DecodeNBase64(const char *string,
72
72
}
73
73
74
74
*writePtr = 0 ;
75
- while (( string[ currPos] != 0 ) &&
76
- ( currPos < stringMaxSize ) &&
75
+ while (( currPos < stringMaxSize ) &&
76
+ ( string[ currPos] != 0 ) &&
77
77
( writePtr < bufferEnd ) &&
78
78
( !isEndOfString )) {
79
79
uint8_t val;
80
80
81
- if (string[currPos] == 0 || currPos >= stringMaxSize)
82
- { break ; }
81
+ if (string[currPos] == 0 ) {
82
+ break ;
83
+ }
83
84
84
85
result = Base64CharToInt (string[currPos++], &val);
85
- if (result != BASE64_SUCCESS)
86
- { break ; }
86
+ if (result != BASE64_SUCCESS) {
87
+ break ;
88
+ }
87
89
88
90
if (val != BASE_64_PAD) {
89
91
if (bitOffset <= 2 ) {
@@ -98,10 +100,11 @@ base64_result_e esfs_DecodeNBase64(const char *string,
98
100
}
99
101
} else { // found BASE_64_PAD
100
102
// At most two pad characters may occur at the end of the encoded stream
101
- if (bitOffset == 2 )
102
- { isEndOfString = true ; } // The last padding byte has been processed.
103
- else if (bitOffset != 4 )
104
- { return BASE64_ERROR; } // Incorrect padding
103
+ if (bitOffset == 2 ) {
104
+ isEndOfString = true ; // The last padding byte has been processed.
105
+ } else if (bitOffset != 4 ) {
106
+ return BASE64_ERROR; // Incorrect padding
107
+ }
105
108
}
106
109
107
110
bitOffset = (bitOffset + 6 ) & 0x7 ;
@@ -110,30 +113,34 @@ base64_result_e esfs_DecodeNBase64(const char *string,
110
113
localCharsProcessed = currPos;
111
114
}
112
115
}
113
- if (charsProcessed == NULL )
114
- { localBytesWritten = (uint32_t )(writePtr - (uint8_t *)buffer); }
115
- else
116
- { *charsProcessed = localCharsProcessed; }
117
- if (lengthWritten != NULL )
118
- { *lengthWritten = localBytesWritten; }
119
- else if (bufferSize != localBytesWritten)
120
- { return BASE64_BUFFER_TOO_SMALL; }
116
+ if (charsProcessed == NULL ) {
117
+ localBytesWritten = (uint32_t )(writePtr - (uint8_t *)buffer);
118
+ } else {
119
+ *charsProcessed = localCharsProcessed;
120
+ }
121
+ if (lengthWritten != NULL ) {
122
+ *lengthWritten = localBytesWritten;
123
+ } else if (bufferSize != localBytesWritten) {
124
+ return BASE64_BUFFER_TOO_SMALL;
125
+ }
121
126
122
127
// Check if additional bytes should have been processed but buffer isn't sufficient.
123
128
if (( result == BASE64_SUCCESS ) &&
124
129
( !isEndOfString ) &&
125
- ( string[ currPos] != ' = ' ) &&
130
+ ( currPos < stringMaxSize ) &&
126
131
( string[currPos] != 0 ) &&
127
- ( currPos < stringMaxSize) )
128
- { return BASE64_BUFFER_TOO_SMALL; }
132
+ ( string[currPos] != ' =' ) ) {
133
+ return BASE64_BUFFER_TOO_SMALL;
134
+ }
129
135
130
- if (result != BASE64_SUCCESS)
131
- { return result; }
136
+ if (result != BASE64_SUCCESS) {
137
+ return result;
138
+ }
132
139
133
140
return BASE64_SUCCESS;
134
141
}
135
142
136
- base64_result_e esfs_EncodeBase64 (const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize)
143
+ base64_result_e trng_EncodeBase64 (const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize)
137
144
{
138
145
uint32_t bitOffset = 0 ;
139
146
@@ -143,8 +150,9 @@ base64_result_e esfs_EncodeBase64(const void *buffer, uint32_t bufferSize, char
143
150
char *writePtr = string;
144
151
char *stringEnd = string + stringSize - 1 ;
145
152
146
- if ((NULL == string) || (NULL == buffer) || (stringSize == 0 ))
147
- { return BASE64_INVALID_PARAMETER; }
153
+ if ((NULL == string) || (NULL == buffer) || (stringSize == 0 )) {
154
+ return BASE64_INVALID_PARAMETER;
155
+ }
148
156
149
157
stringSize--;
150
158
while (readPtr < bufferEnd && writePtr < stringEnd) {
@@ -155,14 +163,16 @@ base64_result_e esfs_EncodeBase64(const void *buffer, uint32_t bufferSize, char
155
163
break ;
156
164
case 6 :
157
165
tempVal = *readPtr++ << 4 ;
158
- if (readPtr < bufferEnd)
159
- { tempVal |= *readPtr >> 4 ; }
166
+ if (readPtr < bufferEnd) {
167
+ tempVal |= *readPtr >> 4 ;
168
+ }
160
169
*writePtr++ = IntToBase64Char (tempVal);
161
170
break ;
162
171
case 4 :
163
172
tempVal = *readPtr++ << 2 ;
164
- if (readPtr < bufferEnd)
165
- { tempVal |= *readPtr >> 6 ; }
173
+ if (readPtr < bufferEnd) {
174
+ tempVal |= *readPtr >> 6 ;
175
+ }
166
176
*writePtr++ = IntToBase64Char (tempVal);
167
177
break ;
168
178
case 2 :
@@ -179,8 +189,9 @@ base64_result_e esfs_EncodeBase64(const void *buffer, uint32_t bufferSize, char
179
189
}
180
190
*writePtr = 0 ;
181
191
182
- if ((readPtr < bufferEnd) || (bitOffset != 0 ))
183
- { return (BASE64_BUFFER_TOO_SMALL); }
192
+ if ((readPtr < bufferEnd) || (bitOffset != 0 )) {
193
+ return (BASE64_BUFFER_TOO_SMALL);
194
+ }
184
195
185
196
return (BASE64_SUCCESS);
186
197
}
0 commit comments