|
| 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 | + |
| 17 | +#include "base64b.h" |
| 18 | + |
| 19 | +using namespace std; |
| 20 | + |
| 21 | +static char IntToBase64Char(uint8_t intVal) |
| 22 | +{ |
| 23 | + const char *base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 24 | + return base64Digits[intVal & 0x3F]; |
| 25 | +} |
| 26 | + |
| 27 | +#define BASE_64_PAD 0xFF |
| 28 | +static base64_result_e Base64CharToInt(char base64, uint8_t *intVal) |
| 29 | +{ |
| 30 | + if (NULL == intVal) { |
| 31 | + return BASE64_INVALID_PARAMETER; |
| 32 | + } |
| 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 { |
| 47 | + return BASE64_ERROR; |
| 48 | + } |
| 49 | + |
| 50 | + return BASE64_SUCCESS; |
| 51 | +} |
| 52 | + |
| 53 | +base64_result_e trng_DecodeNBase64(const char *string, |
| 54 | + uint32_t stringMaxSize, |
| 55 | + void *buffer, |
| 56 | + uint32_t bufferSize, |
| 57 | + uint32_t *lengthWritten, |
| 58 | + uint32_t *charsProcessed) |
| 59 | +{ |
| 60 | + base64_result_e result = BASE64_SUCCESS; |
| 61 | + uint32_t bitOffset = 0; |
| 62 | + uint8_t *writePtr = (uint8_t *)buffer; |
| 63 | + uint8_t *bufferEnd = (uint8_t *)buffer + bufferSize; |
| 64 | + uint8_t tempVal = 0; |
| 65 | + uint32_t currPos = 0; |
| 66 | + uint32_t localBytesWritten = 0; |
| 67 | + uint32_t localCharsProcessed = 0; |
| 68 | + bool isEndOfString = false; |
| 69 | + |
| 70 | + if ((NULL == string) || (NULL == buffer) || (bufferSize == 0)) { |
| 71 | + return BASE64_INVALID_PARAMETER; |
| 72 | + } |
| 73 | + |
| 74 | + *writePtr = 0; |
| 75 | + while (( currPos < stringMaxSize ) && |
| 76 | + ( string[currPos] != 0 ) && |
| 77 | + ( writePtr < bufferEnd ) && |
| 78 | + ( !isEndOfString )) { |
| 79 | + uint8_t val; |
| 80 | + |
| 81 | + if (string[currPos] == 0) { |
| 82 | + break; |
| 83 | + } |
| 84 | + |
| 85 | + result = Base64CharToInt(string[currPos++], &val); |
| 86 | + if (result != BASE64_SUCCESS) { |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + if (val != BASE_64_PAD) { |
| 91 | + if (bitOffset <= 2) { |
| 92 | + tempVal |= val << (2 - bitOffset); |
| 93 | + if (bitOffset == 2) { |
| 94 | + *writePtr++ = tempVal; |
| 95 | + tempVal = 0; |
| 96 | + } |
| 97 | + } else { |
| 98 | + *writePtr++ = (uint8_t)(tempVal | (val >> (bitOffset - 2))); |
| 99 | + tempVal = (uint8_t)(val << (10 - bitOffset)); |
| 100 | + } |
| 101 | + } else { // found BASE_64_PAD |
| 102 | + // At most two pad characters may occur at the end of the encoded stream |
| 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 | + } |
| 108 | + } |
| 109 | + |
| 110 | + bitOffset = (bitOffset + 6) & 0x7; |
| 111 | + if (bitOffset == 0) { |
| 112 | + localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer); |
| 113 | + localCharsProcessed = currPos; |
| 114 | + } |
| 115 | + } |
| 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 | + } |
| 126 | + |
| 127 | + // Check if additional bytes should have been processed but buffer isn't sufficient. |
| 128 | + if (( result == BASE64_SUCCESS ) && |
| 129 | + ( !isEndOfString ) && |
| 130 | + ( currPos < stringMaxSize ) && |
| 131 | + ( string[currPos] != 0 ) && |
| 132 | + ( string[currPos] != '=' ) ) { |
| 133 | + return BASE64_BUFFER_TOO_SMALL; |
| 134 | + } |
| 135 | + |
| 136 | + if (result != BASE64_SUCCESS) { |
| 137 | + return result; |
| 138 | + } |
| 139 | + |
| 140 | + return BASE64_SUCCESS; |
| 141 | +} |
| 142 | + |
| 143 | +base64_result_e trng_EncodeBase64(const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize) |
| 144 | +{ |
| 145 | + uint32_t bitOffset = 0; |
| 146 | + |
| 147 | + const uint8_t *readPtr = (const uint8_t *)buffer; |
| 148 | + const uint8_t *bufferEnd = (const uint8_t *)buffer + bufferSize; |
| 149 | + |
| 150 | + char *writePtr = string; |
| 151 | + char *stringEnd = string + stringSize - 1; |
| 152 | + |
| 153 | + if ((NULL == string) || (NULL == buffer) || (stringSize == 0)) { |
| 154 | + return BASE64_INVALID_PARAMETER; |
| 155 | + } |
| 156 | + |
| 157 | + stringSize--; |
| 158 | + while (readPtr < bufferEnd && writePtr < stringEnd) { |
| 159 | + uint8_t tempVal = 0; |
| 160 | + switch (bitOffset) { |
| 161 | + case 0: |
| 162 | + *writePtr++ = IntToBase64Char(*readPtr >> 2); // take upper 6 bits |
| 163 | + break; |
| 164 | + case 6: |
| 165 | + tempVal = *readPtr++ << 4; |
| 166 | + if (readPtr < bufferEnd) { |
| 167 | + tempVal |= *readPtr >> 4; |
| 168 | + } |
| 169 | + *writePtr++ = IntToBase64Char(tempVal); |
| 170 | + break; |
| 171 | + case 4: |
| 172 | + tempVal = *readPtr++ << 2; |
| 173 | + if (readPtr < bufferEnd) { |
| 174 | + tempVal |= *readPtr >> 6; |
| 175 | + } |
| 176 | + *writePtr++ = IntToBase64Char(tempVal); |
| 177 | + break; |
| 178 | + case 2: |
| 179 | + *writePtr++ = IntToBase64Char(*readPtr++); |
| 180 | + break; |
| 181 | + default: |
| 182 | + return BASE64_ERROR; // we should never reach this code. |
| 183 | + } |
| 184 | + bitOffset = (bitOffset + 6) & 0x7; |
| 185 | + } |
| 186 | + while (bitOffset > 0 && writePtr < stringEnd) { |
| 187 | + *writePtr++ = '='; |
| 188 | + bitOffset = (bitOffset + 6) & 0x7; |
| 189 | + } |
| 190 | + *writePtr = 0; |
| 191 | + |
| 192 | + if ((readPtr < bufferEnd) || (bitOffset != 0)) { |
| 193 | + return (BASE64_BUFFER_TOO_SMALL); |
| 194 | + } |
| 195 | + |
| 196 | + return (BASE64_SUCCESS); |
| 197 | +} |
0 commit comments