Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * RFC 1521 base64 encoding/decoding
- *
- * Copyright (C) 2006-2010, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include "base64.h"
- #include <stdio.h>
- #include <uart.h>
- #include <system/peripherals.h>
- #include <libacc.h>
- // im Feld "base64_test_dec" werden 237 Byte initialisiert!
- //#define MSG_SIZE 237
- //#define ENC_MSG_SIZE 316
- #define MSG_SIZE 40
- #define ENC_MSG_SIZE 56
- //unsigned char buf[64];
- static const unsigned char base64_enc_map[64] =
- {
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
- 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
- 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
- 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
- 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', '+', '/'
- };
- static const unsigned char base64_dec_map[128] =
- {
- 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
- 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
- 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
- 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
- 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
- 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
- 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
- 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
- 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
- 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
- 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
- 49, 50, 51, 127, 127, 127, 127, 127
- };
- // Das Feld "base64_test_dec" wird hier mit 237 Byte initialisiert!
- static const unsigned char base64_test_dec[MSG_SIZE] =
- {
- 0x53, 0x6f, 0x2b, 0x4c, 0x6f, 0x6e, 0x67, 0x2c,
- 0x2b, 0x61, 0x6e, 0x64, 0x2b, 0x54, 0x68, 0x61,
- 0x6e, 0x6b, 0x73, 0x2b, 0x66, 0x6f, 0x72, 0x2b,
- 0x41, 0x6c, 0x6c, 0x2b, 0x74, 0x68, 0x65, 0x2b,
- 0x46, 0x69, 0x73, 0x68, 0x00, 0x00, 0x00, 0x00
- // 0x41, 0x6E, 0x2B, 0x43, 0x68, 0x61, 0x72, 0x6C,
- // 0x6F, 0x74, 0x74, 0x65, 0x2B, 0x76, 0x6F, 0x6E,
- // 0x2B, 0x53, 0x74, 0x65, 0x69, 0x6E, 0x2B, 0x2B,
- // 0x57, 0x6F, 0x68, 0x65, 0x72, 0x2B, 0x73, 0x69,
- // 0x6E, 0x64, 0x2B, 0x77, 0x69, 0x72, 0x2B, 0x67,
- // 0x65, 0x62, 0x6F, 0x72, 0x65, 0x6E, 0x2B,
- // 0x41, 0x75, 0x73, 0x2B, 0x4C, 0x69, 0x65, 0x62,
- // 0x2B, 0x57, 0x69, 0x65, 0x2B, 0x77, 0x61,
- // 0x65, 0x72, 0x65, 0x6E, 0x2B, 0x77, 0x69, 0x72,
- // 0x2B, 0x76, 0x65, 0x72, 0x6C, 0x6F, 0x72, 0x65,
- // 0x6E, 0x2B, 0x4F, 0x68, 0x6E, 0x2B, 0x4C,
- // 0x69, 0x65, 0x62, 0x2B, 0x57, 0x61, 0x73,
- // 0x2B, 0x68, 0x69, 0x6C, 0x66, 0x74, 0x2B, 0x75,
- // 0x6E, 0x73, 0x2B, 0x75, 0x65, 0x62, 0x65, 0x72,
- // 0x77, 0x69, 0x6E, 0x64, 0x65, 0x6E, 0x2B,
- // 0x44, 0x69, 0x65, 0x2B, 0x4C, 0x69, 0x65, 0x62,
- // 0x2B, 0x4B, 0x61, 0x6E, 0x6E, 0x2B, 0x6D,
- // 0x61, 0x6E, 0x2B, 0x61, 0x75, 0x63, 0x68, 0x2B,
- // 0x4C, 0x69, 0x65, 0x62, 0x65, 0x2B, 0x66, 0x69,
- // 0x6E, 0x64, 0x65, 0x6E, 0x2B, 0x44, 0x75,
- // 0x72, 0x63, 0x68, 0x2B, 0x4C, 0x69, 0x65, 0x62,
- // 0x2B, 0x57, 0x61, 0x73, 0x2B, 0x6C, 0x61,
- // 0x65, 0x73, 0x73, 0x74, 0x2B, 0x6E, 0x69, 0x63,
- // 0x68, 0x74, 0x2B, 0x6C, 0x61, 0x6E, 0x67, 0x65,
- // 0x2B, 0x77, 0x65, 0x69, 0x6E, 0x65, 0x6E, 0x2B,
- // 0x44, 0x69, 0x65, 0x2B, 0x4C, 0x69, 0x65,
- // 0x62, 0x2B, 0x57, 0x61, 0x73, 0x2B, 0x73,
- // 0x6F, 0x6C, 0x6C, 0x2B, 0x75, 0x6E, 0x73, 0x2B,
- // 0x73, 0x74, 0x65, 0x74, 0x73, 0x2B, 0x76, 0x65,
- // 0x72, 0x65, 0x69, 0x6E, 0x65, 0x6E, 0x2B,
- // 0x44, 0x69, 0x65, 0x2B, 0x4C, 0x69, 0x65, 0x62
- };
- static const unsigned char base64_test_enc[] = "U28rTG9uZywrYW5kK1RoYW5rcytmb3IrQWxsK3RoZStGaXNoAAAAAA==";
- // "QW4rQ2hhcmxvdHRlK3ZvbitTdGVpbisrV29oZXIrc2luZCt3aX"
- // "IrZ2Vib3JlbitBdXMrTGllYitXaWUrd2FlcmVuK3dpcit2ZXJsb"
- // "3JlbitPaG4rTGllYitXYXMraGlsZnQrdW5zK3VlYmVyd2luZGVuK"
- // "0RpZStMaWViK0thbm4rbWFuK2F1Y2grTGllYmUrZmluZGVuK0R1"
- // "cmNoK0xpZWIrV2FzK2xhZXNzdCtuaWNodCtsYW5nZSt3ZWluZW4r"
- // "RGllK0xpZWIrV2FzK3NvbGwrdW5zK3N0ZXRzK3ZlcmVpbmVuK0"
- // "RpZStMaWVi";
- // "U28rTG9uZywrYW5kK1RoYW5rcytmb3IrQWxsK3RoZStGaXNo"
- // "So+Long,+and+Thanks+for+All+the+Fish"
- // 0x53, 0x6f, 0x2b, 0x4c, 0x6f, 0x6e, 0x67, 0x2c,
- // 0x2b, 0x61, 0x6e, 0x64, 0x2b, 0x54, 0x68, 0x61,
- // 0x6e, 0x6b, 0x73, 0x2b, 0x66, 0x6f, 0x72, 0x2b,
- // 0x41, 0x6c, 0x6c, 0x2b, 0x74, 0x68, 0x65, 0x2b,
- // 0x46, 0x69, 0x73, 0x68
- /*
- * Encode a buffer into base64 format
- */
- int base64_encode( unsigned char *dst, uint32_t *dlen,
- const unsigned char *src, uint32_t slen)
- {
- uint32_t i, n;
- unsigned int C1, C2, C3;
- unsigned char *p;
- if( slen == 0 )
- return( 0 );
- //printf("\r\nslen: %d n: %d\r\n",slen,n);
- n = (slen << 3);
- //printf("\r\nn: %d\r\n",n);
- n = n / 6;
- //printf("\r\nn: %d\r\n",n);
- switch( (slen << 3) - (n * 6) )
- {
- case 2: n += 3; break;
- case 4: n += 2; break;
- default: break;
- }
- //printf("\r\nn: %d\r\n",n);
- //printf("\r\n*dlen: %d n: %d\r\n",*dlen,n);
- if( *dlen < n + 1 )
- {
- printf("\r\n *dlen %d, n %d",*dlen,n);
- *dlen = n + 1;
- return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
- }
- n = (slen / 3) * 3;
- //printf("\r\nnew n: %d\r\n",n);
- //printf("\r\nbuf: %X ",buf);
- p = dst;
- //printf("p: %X \r\n",p);
- //return 1;
- for( i = 0; i < n; i += 3 )
- {
- //NO_SYNTHESIS;
- C1 = *src++;
- C2 = *src++;
- C3 = *src++;
- *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
- *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
- *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
- *p++ = base64_enc_map[C3 & 0x3F];
- // *p++ = base64_enc_map[i];
- // *p++ = base64_enc_map[i+1];
- // *p++ = base64_enc_map[i+2];
- // *p++ = base64_enc_map[i+3];
- //printf("last C1: %d C2: %d\r\n",C1,C2);
- // printf("last loop p: %X *p: %d\r\n",p,*p);
- }
- if( i < slen )
- {
- //printf("i:%d slen:%d \r\n",i,slen);
- C1 = *src++;
- C2 = ((i + 1) < slen) ? *src++ : 0;
- *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
- *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
- //printf("last p: %X *p: %d\r\n",p,*p);
- if( (i + 1) < slen )
- *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
- else *p++ = '=';
- *p++ = '=';
- //printf("very last p: %X *p: %d\r\n",p,*p);
- }
- *dlen = p - dst;
- *p = 0;
- return( 0 );
- }
- /*
- * Decode a base64-formatted buffer
- */
- int base64_decode(unsigned char* dst, uint32_t *dlen, const unsigned char *src, uint32_t slen)
- {
- uint32_t n;
- uint32_t j;
- int i;
- unsigned int reg_h,reg_l,tmp;
- unsigned char *p;
- unsigned char value;
- for( i = j = n = 0; i < slen; i++ )
- {
- //NO_SYNTHESIS;
- if( ( slen - i ) >= 2 &&
- src[i] == '\r' && src[i + 1] == '\n' )
- continue;
- if( src[i] == '\n' )
- continue;
- if( src[i] == '=' && ++j > 2 ) {
- //printf("\r\n err 1 i: %d",i);
- return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
- }
- if( src[i] > 127 || base64_dec_map[src[i]] == 127 ) {
- //printf("\r\n err 2 i: %d ",i);
- return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
- }
- if( base64_dec_map[src[i]] < 64 && j != 0 ) {
- //printf("\r\n err 3 i: %d",i);
- return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
- }
- n++;
- }
- if( n == 0 ) {
- printf("\r\n No String found");
- return( 0 );
- }
- n = ((n * 6) + 7) >> 3;
- if( *dlen < n )
- {
- //printf("\r\n dlen: %d n: %d\r\n",*dlen, n);
- *dlen = n;
- return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
- }
- //printf("return chk\r\n");
- reg_l = 0;
- reg_h = 0;
- tmp = 0;
- //printf("start decode...\r\n");
- p = dst;
- j = 3;
- n = 0;
- for(i = slen ; i > 0; i--, src++ )
- {
- //printf ("i: %d\n\r", i);
- //NO_SYNTHESIS;
- if( *src == '\r' || *src == '\n' )
- continue;
- //
- // //printf("\r\ndecmap: %d %d\r\n",dec_map[81],dec_map[87]);
- // //printf("\r\nadr decmap: %X\r\n",base64_dec_map);
- //
- value = base64_dec_map[*src];
- j -= ( value == 64 );
- reg_h = reg_h << 6;
- tmp = reg_l >> 12;
- reg_h = reg_h | tmp;
- reg_l = (reg_l << 6) | ( value & 0x3F );
- //
- // //printf("regh %X, regl %X, tmp ", reg_h, reg_l);
- // //printf("%X value d'%d",tmp, value);
- // printf(" src %X src* %d\r\n",src, *src);
- //
- //n++;
- if( ++n == 4 )
- {
- n = 0;
- if( j > 0 ) {
- tmp = reg_l >> 16;
- tmp = (reg_h << 2) | tmp;
- value = tmp & 0xFF;
- //printf(">0 %X",value);
- *p++ = value;
- }
- if( j > 1 ) {
- value = (reg_l >> 8) & 0xFF;
- //printf(">1 %X",value);
- *p++ = value;
- }
- if( j > 2 ) {
- value = reg_l & 0xFF;
- //printf(">2 %X",value);
- *p++ = value;
- }
- }
- }
- *dlen = p - dst;
- printf("dst: %s\r\n",dst);
- printf("p: %s\r\n",p);
- return 0;
- }
- /*
- * main routine
- */
- int main()
- {
- stdio_uart_open(UART_LIGHT_0);
- int err;
- uint32_t len;
- unsigned char *src;
- unsigned char *ptr;
- unsigned char buf[64];
- TIMER_0->limit = 131072; //2^17 = 0x20000
- TIMER_1->limit = 262143; //2^18 -1 = 0x3ffff
- TIMER_0->control |= TIMER_CNT_RST;
- TIMER_0->control &= !TIMER_CNT_RST;
- TIMER_1->control |= TIMER_CNT_RST;
- TIMER_1->control &= !TIMER_CNT_RST;
- printf("\r\n\n=== Base64 Encoding/Decoding Test ===\r\n");
- int i;
- //len = sizeof( buffer );
- src = (unsigned char *) base64_test_dec;
- ptr = (unsigned char *) base64_test_dec;
- // HW execution
- libacc_enable_accels();
- printf("\r\nExample Original message:");
- printf("\r\n---------------------------------------------------\r\n");
- for(i=0;i<MSG_SIZE;i++) {
- printf("%X ",ptr[i]);
- }
- printf("%s",ptr);
- printf("\r\n---------------------------------------------------\r\n");
- ptr = (unsigned char *) base64_test_enc;
- printf("\r\nExample Encoded message:");
- printf("\r\n---------------------------------------------------\r\n");
- printf("%s",ptr);
- printf("\r\n---------------------------------------------------\r\n");
- // invalidate buffer
- for (i=0;i<sizeof(buf);i++) {
- NO_SYNTHESIS;
- buf[i] = 0;
- }
- len = sizeof( buf );
- printf("\r\nHW encoding...");
- // encode
- TIMER_0->control |= TIMER_CNT_RST;
- TIMER_0->control &= !TIMER_CNT_RST;
- TIMER_1->control |= TIMER_CNT_RST;
- TIMER_1->control &= !TIMER_CNT_RST;
- TIMER_1->control = TIMER_EN;
- TIMER_0->control = TIMER_EN;
- //printf("buffer: %X *: %d\r\n",buf,*buf);
- err = base64_encode(buf, &len, src, MSG_SIZE);
- TIMER_0->control = 0;
- TIMER_1->control = 0;
- if(err) {
- printf(" failed\r\n");
- }
- //ptr = (unsigned char *) buf;
- printf("\r\nHW Encoded message:");
- printf("\r\n---------------------------------------------------\r\n");
- printf("%s",buf);
- printf("\r\n---------------------------------------------------\r\n");
- //len = sizeof( buffer );
- if(err) {
- printf(" failed\r\n");
- }
- len = sizeof(buf);
- src = (unsigned char *) base64_test_enc;
- printf("\r\n\nHW decoding...");
- // invalidate buffer
- for (i=0;i<sizeof(buf);i++) {
- NO_SYNTHESIS;
- buf[i] = 0;
- }
- // decode
- TIMER_1->control = TIMER_EN;
- TIMER_0->control = TIMER_EN;
- err = base64_decode(buf, &len, src, ENC_MSG_SIZE);
- TIMER_0->control = 0;
- TIMER_1->control = 0;
- if(err) {
- printf("failed: %d\r\n", err);
- }
- //ptr = (unsigned char *) buf;
- printf("\r\nHW Decoded message:");
- printf("\r\n---------------------------------------------------\r\n");
- printf("%s",buf);
- printf("\r\n---------------------------------------------------\r\n\n");
- printf("\r\n HW timer value: %6u",TIMER_0->value);
- printf("\r\n HW overflows: %6u\r\n\n",TIMER_1->value);
- printf("\r\nHW ready\r\n");
- //=========================================================================
- // SW Execution
- // invalidate buffer
- for (i=0;i<sizeof(buf);i++) {
- NO_SYNTHESIS;
- buf[i] = 0;
- }
- len = sizeof(buf);
- src = (unsigned char *) base64_test_dec;
- ptr = (unsigned char *) base64_test_dec;
- printf("\r\nSW encoding...");
- // encode
- TIMER_0->control |= TIMER_CNT_RST;
- TIMER_0->control &= !TIMER_CNT_RST;
- TIMER_1->control |= TIMER_CNT_RST;
- TIMER_1->control &= !TIMER_CNT_RST;
- TIMER_1->control = TIMER_EN;
- TIMER_0->control = TIMER_EN;
- err = base64_encode(buf, &len, src, MSG_SIZE);
- TIMER_0->control = 0;
- TIMER_1->control = 0;
- if(err) {
- printf(" failed\r\n");
- }
- //ptr = (unsigned char *) buf;
- printf("\r\nSW Encoded message:");
- printf("\r\n---------------------------------------------------\r\n");
- printf("%s",buf);
- printf("\r\n---------------------------------------------------\r\n");
- len = sizeof( buf );
- src = (unsigned char *) base64_test_enc;
- printf("\r\n\nSW decoding...");
- len = sizeof(buf);
- // invalidate buffer
- for (i=0;i<sizeof(buf);i++) {
- NO_SYNTHESIS;
- buf[i] = 0;
- }
- // decode
- TIMER_1->control = TIMER_EN;
- TIMER_0->control = TIMER_EN;
- //err = base64_decode_chk( &len, src, ENC_MSG_SIZE, base64_dec_map);
- err = base64_decode(buf, &len, src, ENC_MSG_SIZE);
- TIMER_0->control = 0;
- TIMER_1->control = 0;
- if(err) {
- printf("failed: %d\r\n", err);
- }
- //ptr = (unsigned char *) buf;
- printf("\r\nSW Decoded message:");
- printf("\r\n---------------------------------------------------\r\n");
- printf("%s",buf);
- printf("\r\n---------------------------------------------------\r\n\n");
- printf("\r\nSW timer value: %6u",TIMER_0->value);
- printf("\r\nSW overflows: %6u\r\n\n",TIMER_1->value);
- printf("\r\nSW ready\r\n");
- while(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement