RDK Documentation (Open Sourced RDK Components)
base16.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21  * @file base16.cpp
22  * @brief optimized pair of base16 encode/decode implementations
23  */
24 
25 
26 #include "_base64.h"
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include "AampUtils.h"
31 
32 /**
33  * @brief convert binary data to hascii-encoded equivalent
34  * @retval pointer to malloc'd cstring containing base16-encoded copy
35  * @retval NULL if unsufficient memory to allocate base16-encoded copt
36  * @note caller responsible for freeing returned cstring
37  * @note returned string will always contain an even number of characters
38  */
39 char *base16_Encode(const unsigned char *src, size_t len)
40 {
41  size_t outLen = len*2;
42  char *outData = (char *)malloc(1 + outLen);
43  if( outData )
44  {
45  char *dst = outData;
46  const unsigned char *finish = src + len;
47  while (src < finish)
48  {
49  unsigned char c = *src++;
50  WRITE_HASCII( dst, c );
51  }
52  *dst = 0x00;
53  }
54  return outData;
55 }
56 
57 
58 /**
59  * @brief decode base16 encoded data to binary equivalent
60  * @retval pointer to malloc'd memory containing decoded binary data.
61  * @retval NULL if insufficient memory to allocate base16-decoded data
62  * @note caller responsible for freeing returned data
63  */
64 unsigned char *base16_Decode( const char *srcPtr, size_t srcLen, size_t *len )
65 {
66  static const signed char mBase16CharToIndex[256] = {
67  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
71  -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73  -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
76  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
77  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
79  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
80  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
81  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
82  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
83  };
84  size_t numChars = (srcLen/2); // assumes even
85  unsigned char *outData = (unsigned char *)malloc(numChars);
86  if (outData)
87  {
88  unsigned char *dst = outData;
89  const char *finish = srcPtr + srcLen;
90  while (srcPtr < finish)
91  {
92  *dst = mBase16CharToIndex[(unsigned char)*srcPtr++] << 4;
93  *dst++ |= mBase16CharToIndex[(unsigned char)*srcPtr++];
94  }
95  *len = numChars;
96  }
97  else
98  { // insufficient memory
99  *len = 0;
100  }
101  return outData;
102 }
base16_Encode
char * base16_Encode(const unsigned char *src, size_t len)
convert binary data to hascii-encoded equivalent
Definition: base16.cpp:39
base16_Decode
unsigned char * base16_Decode(const char *srcPtr, size_t srcLen, size_t *len)
decode base16 encoded data to binary equivalent
Definition: base16.cpp:64
_base64.h
base64 source Encoder/Decoder