X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=RIC-E2-TERMINATION%2Fbase64.cpp;h=373aed856091e12efdfb48ce34a7a98b1885b6a3;hb=9f59b571bff980497eea8bf891e5f0000983a2fe;hp=6e9788ceaf5cdc6cb0a9f2d849d589c0ea89a84c;hpb=1a79bdfd453f4ae8e07074a4cc6669672c7ca2f7;p=ric-plt%2Fe2.git diff --git a/RIC-E2-TERMINATION/base64.cpp b/RIC-E2-TERMINATION/base64.cpp index 6e9788c..373aed8 100644 --- a/RIC-E2-TERMINATION/base64.cpp +++ b/RIC-E2-TERMINATION/base64.cpp @@ -14,6 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + + /* + * This source code is part of the near-RT RIC (RAN Intelligent Controller) + * platform project (RICP). + */ // // Created by adi ENZEL on 9/26/19. @@ -21,17 +26,27 @@ #include "base64.h" -void base64::encode(const unsigned char *src, const int srcLen, char unsigned *dst, int &dstLen) { +int base64::encode(const unsigned char *src, int srcLen, char unsigned *dst, long &dstLen) { unsigned char *pos; const unsigned char *end, *in; - if (dstLen < srcLen) { - mdclog_write(MDCLOG_ERR, "Destination size %d must be at least 140 percent from source size %d", + + if (dstLen <= 0 || srcLen <= 0) { + mdclog_write(MDCLOG_ERR, "source or destination length are 0. dst =%ld source = %d", + dstLen, srcLen); + return -1; + } + if (dstLen < (srcLen * 4 / 3)) { + mdclog_write(MDCLOG_ERR, "Destination size %ld must be at least 140 percent from source size %d", dstLen, srcLen); - return; + return -1; } if (dst == nullptr) { mdclog_write(MDCLOG_ERR, "Destination must be allocated and freed by caller the function not allocate the memory"); - return; + return -1; + } + if (src == nullptr) { + mdclog_write(MDCLOG_ERR, "source is null pointer"); + return -1; } end = src + srcLen; @@ -58,6 +73,77 @@ void base64::encode(const unsigned char *src, const int srcLen, char unsigned *d } *pos = '\0'; - dstLen = pos - dst; + dstLen = pos - dst;return 0; } +int base64::decode(const unsigned char *src, int srcLen, char unsigned *dst, long dstLen) { + unsigned char inv_table[INVERSE_TABLE_SIZE]; + memset(inv_table, 0x80, INVERSE_TABLE_SIZE); + for (ulong i = 0; i < sizeof(base64_table) - 1; i++) { + inv_table[base64_table[i]] = (unsigned char) i; + } + inv_table['='] = 0; + + + if (dstLen == 0 || dstLen < (int)(srcLen / 4 * 3)) { + mdclog_write(MDCLOG_ERR, "Destination size %ld can be up to 40 smaller then source size %d", + dstLen, srcLen); + return -1; + } + if (dst == nullptr) { + mdclog_write(MDCLOG_ERR, "Destination must be allocated and freed by caller the function not allocate the memory"); + return -1; + } + + unsigned char *pos, block[4], tmp; + long i; + int pad = 0; + + size_t count = 0; + + for (i = 0; i < srcLen; i++) { + if (inv_table[src[i]] != 0x80) { + count++; + } + } + + if (count == 0 || count % 4) + return -1; + + pos = dst; + count = 0; + for (i = 0; i < srcLen; i++) { + tmp = inv_table[src[i]]; + if (tmp == 0x80) { + continue; + } + block[count] = tmp; + + if (src[i] == '=') { + pad++; + } + + count++; + if (count == 4) { + *pos++ = (block[0] << 2) | ((unsigned char)block[1] >> (unsigned int)4); + *pos++ = (block[1] << 4) | ((unsigned char)block[2] >> (unsigned int)2); + *pos++ = (block[2] << 6) | block[3]; + count = 0; + if (pad) { + if (pad == 1) { + pos--; + } + else if (pad == 2) { + pos -= 2; + } + else { + return -1; + } + break; + } + } + } + + dstLen = pos - dst; + return 0; +}