mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*
elessair 0:f269e3021894 2 * RFC 1521 base64 encoding/decoding
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
elessair 0:f269e3021894 5 * SPDX-License-Identifier: Apache-2.0
elessair 0:f269e3021894 6 *
elessair 0:f269e3021894 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
elessair 0:f269e3021894 8 * not use this file except in compliance with the License.
elessair 0:f269e3021894 9 * You may obtain a copy of the License at
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 12 *
elessair 0:f269e3021894 13 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
elessair 0:f269e3021894 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 16 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 17 * limitations under the License.
elessair 0:f269e3021894 18 *
elessair 0:f269e3021894 19 * This file is part of mbed TLS (https://tls.mbed.org)
elessair 0:f269e3021894 20 */
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #if !defined(MBEDTLS_CONFIG_FILE)
elessair 0:f269e3021894 23 #include "mbedtls/config.h"
elessair 0:f269e3021894 24 #else
elessair 0:f269e3021894 25 #include MBEDTLS_CONFIG_FILE
elessair 0:f269e3021894 26 #endif
elessair 0:f269e3021894 27
elessair 0:f269e3021894 28 #if defined(MBEDTLS_BASE64_C)
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 #include "mbedtls/base64.h"
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 #include <stdint.h>
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34 #if defined(MBEDTLS_SELF_TEST)
elessair 0:f269e3021894 35 #include <string.h>
elessair 0:f269e3021894 36 #if defined(MBEDTLS_PLATFORM_C)
elessair 0:f269e3021894 37 #include "mbedtls/platform.h"
elessair 0:f269e3021894 38 #else
elessair 0:f269e3021894 39 #include <stdio.h>
elessair 0:f269e3021894 40 #define mbedtls_printf printf
elessair 0:f269e3021894 41 #endif /* MBEDTLS_PLATFORM_C */
elessair 0:f269e3021894 42 #endif /* MBEDTLS_SELF_TEST */
elessair 0:f269e3021894 43
elessair 0:f269e3021894 44 static const unsigned char base64_enc_map[64] =
elessair 0:f269e3021894 45 {
elessair 0:f269e3021894 46 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
elessair 0:f269e3021894 47 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
elessair 0:f269e3021894 48 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
elessair 0:f269e3021894 49 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
elessair 0:f269e3021894 50 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
elessair 0:f269e3021894 51 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
elessair 0:f269e3021894 52 '8', '9', '+', '/'
elessair 0:f269e3021894 53 };
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 static const unsigned char base64_dec_map[128] =
elessair 0:f269e3021894 56 {
elessair 0:f269e3021894 57 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
elessair 0:f269e3021894 58 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
elessair 0:f269e3021894 59 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
elessair 0:f269e3021894 60 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
elessair 0:f269e3021894 61 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
elessair 0:f269e3021894 62 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
elessair 0:f269e3021894 63 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
elessair 0:f269e3021894 64 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
elessair 0:f269e3021894 65 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
elessair 0:f269e3021894 66 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
elessair 0:f269e3021894 67 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
elessair 0:f269e3021894 68 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
elessair 0:f269e3021894 69 49, 50, 51, 127, 127, 127, 127, 127
elessair 0:f269e3021894 70 };
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 #define BASE64_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 /*
elessair 0:f269e3021894 75 * Encode a buffer into base64 format
elessair 0:f269e3021894 76 */
elessair 0:f269e3021894 77 int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
elessair 0:f269e3021894 78 const unsigned char *src, size_t slen )
elessair 0:f269e3021894 79 {
elessair 0:f269e3021894 80 size_t i, n;
elessair 0:f269e3021894 81 int C1, C2, C3;
elessair 0:f269e3021894 82 unsigned char *p;
elessair 0:f269e3021894 83
elessair 0:f269e3021894 84 if( slen == 0 )
elessair 0:f269e3021894 85 {
elessair 0:f269e3021894 86 *olen = 0;
elessair 0:f269e3021894 87 return( 0 );
elessair 0:f269e3021894 88 }
elessair 0:f269e3021894 89
elessair 0:f269e3021894 90 n = slen / 3 + ( slen % 3 != 0 );
elessair 0:f269e3021894 91
elessair 0:f269e3021894 92 if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
elessair 0:f269e3021894 93 {
elessair 0:f269e3021894 94 *olen = BASE64_SIZE_T_MAX;
elessair 0:f269e3021894 95 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
elessair 0:f269e3021894 96 }
elessair 0:f269e3021894 97
elessair 0:f269e3021894 98 n *= 4;
elessair 0:f269e3021894 99
elessair 0:f269e3021894 100 if( ( dlen < n + 1 ) || ( NULL == dst ) )
elessair 0:f269e3021894 101 {
elessair 0:f269e3021894 102 *olen = n + 1;
elessair 0:f269e3021894 103 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
elessair 0:f269e3021894 104 }
elessair 0:f269e3021894 105
elessair 0:f269e3021894 106 n = ( slen / 3 ) * 3;
elessair 0:f269e3021894 107
elessair 0:f269e3021894 108 for( i = 0, p = dst; i < n; i += 3 )
elessair 0:f269e3021894 109 {
elessair 0:f269e3021894 110 C1 = *src++;
elessair 0:f269e3021894 111 C2 = *src++;
elessair 0:f269e3021894 112 C3 = *src++;
elessair 0:f269e3021894 113
elessair 0:f269e3021894 114 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
elessair 0:f269e3021894 115 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
elessair 0:f269e3021894 116 *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
elessair 0:f269e3021894 117 *p++ = base64_enc_map[C3 & 0x3F];
elessair 0:f269e3021894 118 }
elessair 0:f269e3021894 119
elessair 0:f269e3021894 120 if( i < slen )
elessair 0:f269e3021894 121 {
elessair 0:f269e3021894 122 C1 = *src++;
elessair 0:f269e3021894 123 C2 = ( ( i + 1 ) < slen ) ? *src++ : 0;
elessair 0:f269e3021894 124
elessair 0:f269e3021894 125 *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
elessair 0:f269e3021894 126 *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 if( ( i + 1 ) < slen )
elessair 0:f269e3021894 129 *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
elessair 0:f269e3021894 130 else *p++ = '=';
elessair 0:f269e3021894 131
elessair 0:f269e3021894 132 *p++ = '=';
elessair 0:f269e3021894 133 }
elessair 0:f269e3021894 134
elessair 0:f269e3021894 135 *olen = p - dst;
elessair 0:f269e3021894 136 *p = 0;
elessair 0:f269e3021894 137
elessair 0:f269e3021894 138 return( 0 );
elessair 0:f269e3021894 139 }
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141 /*
elessair 0:f269e3021894 142 * Decode a base64-formatted buffer
elessair 0:f269e3021894 143 */
elessair 0:f269e3021894 144 int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
elessair 0:f269e3021894 145 const unsigned char *src, size_t slen )
elessair 0:f269e3021894 146 {
elessair 0:f269e3021894 147 size_t i, n;
elessair 0:f269e3021894 148 uint32_t j, x;
elessair 0:f269e3021894 149 unsigned char *p;
elessair 0:f269e3021894 150
elessair 0:f269e3021894 151 /* First pass: check for validity and get output length */
elessair 0:f269e3021894 152 for( i = n = j = 0; i < slen; i++ )
elessair 0:f269e3021894 153 {
elessair 0:f269e3021894 154 /* Skip spaces before checking for EOL */
elessair 0:f269e3021894 155 x = 0;
elessair 0:f269e3021894 156 while( i < slen && src[i] == ' ' )
elessair 0:f269e3021894 157 {
elessair 0:f269e3021894 158 ++i;
elessair 0:f269e3021894 159 ++x;
elessair 0:f269e3021894 160 }
elessair 0:f269e3021894 161
elessair 0:f269e3021894 162 /* Spaces at end of buffer are OK */
elessair 0:f269e3021894 163 if( i == slen )
elessair 0:f269e3021894 164 break;
elessair 0:f269e3021894 165
elessair 0:f269e3021894 166 if( ( slen - i ) >= 2 &&
elessair 0:f269e3021894 167 src[i] == '\r' && src[i + 1] == '\n' )
elessair 0:f269e3021894 168 continue;
elessair 0:f269e3021894 169
elessair 0:f269e3021894 170 if( src[i] == '\n' )
elessair 0:f269e3021894 171 continue;
elessair 0:f269e3021894 172
elessair 0:f269e3021894 173 /* Space inside a line is an error */
elessair 0:f269e3021894 174 if( x != 0 )
elessair 0:f269e3021894 175 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
elessair 0:f269e3021894 176
elessair 0:f269e3021894 177 if( src[i] == '=' && ++j > 2 )
elessair 0:f269e3021894 178 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
elessair 0:f269e3021894 179
elessair 0:f269e3021894 180 if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
elessair 0:f269e3021894 181 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
elessair 0:f269e3021894 182
elessair 0:f269e3021894 183 if( base64_dec_map[src[i]] < 64 && j != 0 )
elessair 0:f269e3021894 184 return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
elessair 0:f269e3021894 185
elessair 0:f269e3021894 186 n++;
elessair 0:f269e3021894 187 }
elessair 0:f269e3021894 188
elessair 0:f269e3021894 189 if( n == 0 )
elessair 0:f269e3021894 190 {
elessair 0:f269e3021894 191 *olen = 0;
elessair 0:f269e3021894 192 return( 0 );
elessair 0:f269e3021894 193 }
elessair 0:f269e3021894 194
elessair 0:f269e3021894 195 n = ( ( n * 6 ) + 7 ) >> 3;
elessair 0:f269e3021894 196 n -= j;
elessair 0:f269e3021894 197
elessair 0:f269e3021894 198 if( dst == NULL || dlen < n )
elessair 0:f269e3021894 199 {
elessair 0:f269e3021894 200 *olen = n;
elessair 0:f269e3021894 201 return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
elessair 0:f269e3021894 202 }
elessair 0:f269e3021894 203
elessair 0:f269e3021894 204 for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
elessair 0:f269e3021894 205 {
elessair 0:f269e3021894 206 if( *src == '\r' || *src == '\n' || *src == ' ' )
elessair 0:f269e3021894 207 continue;
elessair 0:f269e3021894 208
elessair 0:f269e3021894 209 j -= ( base64_dec_map[*src] == 64 );
elessair 0:f269e3021894 210 x = ( x << 6 ) | ( base64_dec_map[*src] & 0x3F );
elessair 0:f269e3021894 211
elessair 0:f269e3021894 212 if( ++n == 4 )
elessair 0:f269e3021894 213 {
elessair 0:f269e3021894 214 n = 0;
elessair 0:f269e3021894 215 if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
elessair 0:f269e3021894 216 if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
elessair 0:f269e3021894 217 if( j > 2 ) *p++ = (unsigned char)( x );
elessair 0:f269e3021894 218 }
elessair 0:f269e3021894 219 }
elessair 0:f269e3021894 220
elessair 0:f269e3021894 221 *olen = p - dst;
elessair 0:f269e3021894 222
elessair 0:f269e3021894 223 return( 0 );
elessair 0:f269e3021894 224 }
elessair 0:f269e3021894 225
elessair 0:f269e3021894 226 #if defined(MBEDTLS_SELF_TEST)
elessair 0:f269e3021894 227
elessair 0:f269e3021894 228 static const unsigned char base64_test_dec[64] =
elessair 0:f269e3021894 229 {
elessair 0:f269e3021894 230 0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
elessair 0:f269e3021894 231 0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
elessair 0:f269e3021894 232 0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
elessair 0:f269e3021894 233 0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
elessair 0:f269e3021894 234 0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
elessair 0:f269e3021894 235 0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
elessair 0:f269e3021894 236 0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
elessair 0:f269e3021894 237 0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
elessair 0:f269e3021894 238 };
elessair 0:f269e3021894 239
elessair 0:f269e3021894 240 static const unsigned char base64_test_enc[] =
elessair 0:f269e3021894 241 "JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
elessair 0:f269e3021894 242 "swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
elessair 0:f269e3021894 243
elessair 0:f269e3021894 244 /*
elessair 0:f269e3021894 245 * Checkup routine
elessair 0:f269e3021894 246 */
elessair 0:f269e3021894 247 int mbedtls_base64_self_test( int verbose )
elessair 0:f269e3021894 248 {
elessair 0:f269e3021894 249 size_t len;
elessair 0:f269e3021894 250 const unsigned char *src;
elessair 0:f269e3021894 251 unsigned char buffer[128];
elessair 0:f269e3021894 252
elessair 0:f269e3021894 253 if( verbose != 0 )
elessair 0:f269e3021894 254 mbedtls_printf( " Base64 encoding test: " );
elessair 0:f269e3021894 255
elessair 0:f269e3021894 256 src = base64_test_dec;
elessair 0:f269e3021894 257
elessair 0:f269e3021894 258 if( mbedtls_base64_encode( buffer, sizeof( buffer ), &len, src, 64 ) != 0 ||
elessair 0:f269e3021894 259 memcmp( base64_test_enc, buffer, 88 ) != 0 )
elessair 0:f269e3021894 260 {
elessair 0:f269e3021894 261 if( verbose != 0 )
elessair 0:f269e3021894 262 mbedtls_printf( "failed\n" );
elessair 0:f269e3021894 263
elessair 0:f269e3021894 264 return( 1 );
elessair 0:f269e3021894 265 }
elessair 0:f269e3021894 266
elessair 0:f269e3021894 267 if( verbose != 0 )
elessair 0:f269e3021894 268 mbedtls_printf( "passed\n Base64 decoding test: " );
elessair 0:f269e3021894 269
elessair 0:f269e3021894 270 src = base64_test_enc;
elessair 0:f269e3021894 271
elessair 0:f269e3021894 272 if( mbedtls_base64_decode( buffer, sizeof( buffer ), &len, src, 88 ) != 0 ||
elessair 0:f269e3021894 273 memcmp( base64_test_dec, buffer, 64 ) != 0 )
elessair 0:f269e3021894 274 {
elessair 0:f269e3021894 275 if( verbose != 0 )
elessair 0:f269e3021894 276 mbedtls_printf( "failed\n" );
elessair 0:f269e3021894 277
elessair 0:f269e3021894 278 return( 1 );
elessair 0:f269e3021894 279 }
elessair 0:f269e3021894 280
elessair 0:f269e3021894 281 if( verbose != 0 )
elessair 0:f269e3021894 282 mbedtls_printf( "passed\n\n" );
elessair 0:f269e3021894 283
elessair 0:f269e3021894 284 return( 0 );
elessair 0:f269e3021894 285 }
elessair 0:f269e3021894 286
elessair 0:f269e3021894 287 #endif /* MBEDTLS_SELF_TEST */
elessair 0:f269e3021894 288
elessair 0:f269e3021894 289 #endif /* MBEDTLS_BASE64_C */