Nut/OS  4.10.3
API Reference
base64_decode.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Thermotemp GmbH. All rights reserved. 
00003  *
00004  * These routines where mainly taken from pro/dencode.c
00005  * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. Neither the name of the copyright holders nor the names of
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00024  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00027  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00028  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00030  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * For additional information see http://www.ethernut.de/
00034  */
00035 
00056 #include <sys/types.h> 
00057 #include <stdint.h>
00058 
00063 
00064 /* Base-64 decoding.  This represents binary data as printable ASCII
00065 ** characters.  Three 8-bit binary bytes are turned into four 6-bit
00066 ** values, like so:
00067 **
00068 **   [11111111]  [22222222]  [33333333]
00069 **
00070 **   [111111] [112222] [222233] [333333]
00071 **
00072 ** Then the 6-bit values are represented using the characters "A-Za-z0-9+/".
00073 */
00074 
00075 /* Since base-64 encodes strings do not have any character above 127,
00076  * we need just the first 128 bytes. Furthermore there is no char
00077  * below 32, so we can save 32 additional bytes of flash.
00078  */
00079 static prog_char base64dtab[96] = {
00080 /*
00081     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00082     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00083 */
00084     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
00085     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
00086     -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
00087     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
00088     -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
00089     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
00090 /*
00091     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00092     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00093     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00094     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00095     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00096     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00097     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
00098     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 */
00099 };
00100 
00113 /*
00114  * Do base-64 decoding on a string. 
00115  */
00116 char *NutDecodeBase64(char * str)
00117 {
00118     /* bug fix from Damian Slee. */
00119     char code;
00120     char *sp;
00121     char *tp;
00122     char last = -1;
00123     char step = 0;
00124 
00125     for (tp = sp = str; *sp; ++sp) {
00126         if (*sp < 32)
00127             continue;
00128         if ((code = PRG_RDB(&base64dtab[(int) *sp - 32])) == (char)-1)
00129             continue;
00130         switch (step++) {
00131         case 1:
00132             *tp++ = ((last << 2) | ((code & 0x30) >> 4));
00133             break;
00134         case 2:
00135             *tp++ = (((last & 0xf) << 4) | ((code & 0x3c) >> 2));
00136             break;
00137         case 3:
00138             *tp++ = (((last & 0x03) << 6) | code);
00139             step = 0;
00140             break;
00141         }
00142         last = code;
00143     }
00144     *tp = 0;
00145     return str;
00146 }
00147