pwm period is now 200us instead of the default 20ms veml6040 config is now AF_BIT | TRIG_BIT

Dependencies:   mbed MMA8451Q USBDevice WakeUp vt100

Fork of afero_node_suntory_2017_06_15 by Orefatoi

Committer:
Rhyme
Date:
Wed Oct 18 00:31:13 2017 +0000
Revision:
23:e4d2316383a1
Parent:
0:20bce0dcc921
pwm period is now 200us from default 20ms; veml6040->setCOLORCOnf is now AF_BIT | TRIG_BIT from 0x00;

Who changed what in which revision?

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