Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnBase64.cpp
00001 #if 0 00002 /* 00003 #include "SnBase64.h" 00004 00005 #include "SnBase64data.h" 00006 00007 /** 00008 * \file modp_b64.c 00009 * <PRE> 00010 * MODP_B64 - High performance base64 encoder/decoder 00011 * http://code.google.com/p/stringencoders/ 00012 * 00013 * Copyright © 2005, 2006, 2007 Nick Galbreath -- nickg [at] modp [dot] com 00014 * All rights reserved. 00015 * 00016 * Redistribution and use in source and binary forms, with or without 00017 * modification, are permitted provided that the following conditions are 00018 * met: 00019 * 00020 * Redistributions of source code must retain the above copyright 00021 * notice, this list of conditions and the following disclaimer. 00022 * 00023 * Redistributions in binary form must reproduce the above copyright 00024 * notice, this list of conditions and the following disclaimer in the 00025 * documentation and/or other materials provided with the distribution. 00026 * 00027 * Neither the name of the modp.com nor the names of its 00028 * contributors may be used to endorse or promote products derived from 00029 * this software without specific prior written permission. 00030 * 00031 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00032 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00033 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00034 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00035 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00036 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00037 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00038 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00039 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00040 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00041 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00042 * 00043 * This is the standard "new" BSD license: 00044 * http://www.opensource.org/licenses/bsd-license.php 00045 * </PRE> 00046 */ 00047 00048 #define BADCHAR 0x01FFFFFF 00049 00050 /** 00051 * you can control if we use padding by commenting out this 00052 * next line. However, I highly recommend you use padding and not 00053 * using it should only be for compatability with a 3rd party. 00054 * Also, 'no padding' is not tested! 00055 */ 00056 #define DOPAD 1 00057 00058 /* 00059 * if we aren't doing padding 00060 * set the pad character to NULL 00061 */ 00062 #ifndef DOPAD 00063 #undef CHARPAD 00064 #define CHARPAD '\0' 00065 #endif 00066 00067 int B64::modp_b64_encode(const char* str, const int len, char* dest) 00068 { 00069 int i; 00070 const uint8_t* s = (const uint8_t*) str; 00071 uint8_t* p = (uint8_t*) dest; 00072 00073 /* unsigned here is important! */ 00074 /* uint8_t is fastest on G4, amd */ 00075 /* uint32_t is fastest on Intel */ 00076 uint32_t t1, t2, t3; 00077 00078 for (i = 0; i < len - 2; i += 3) { 00079 t1 = s[i]; t2 = s[i+1]; t3 = s[i+2]; 00080 *p++ = e0[t1]; 00081 *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; 00082 *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)]; 00083 *p++ = e2[t3]; 00084 } 00085 00086 switch (len - i) { 00087 case 0: 00088 break; 00089 case 1: 00090 t1 = s[i]; 00091 *p++ = e0[t1]; 00092 *p++ = e1[(t1 & 0x03) << 4]; 00093 *p++ = CHARPAD; 00094 *p++ = CHARPAD; 00095 break; 00096 default: /* case 2 */ 00097 t1 = s[i]; t2 = s[i+1]; 00098 *p++ = e0[t1]; 00099 *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; 00100 *p++ = e2[(t2 & 0x0F) << 2]; 00101 *p++ = CHARPAD; 00102 } 00103 00104 *p = '\0'; 00105 return (int)(p - (uint8_t*)dest); 00106 } 00107 00108 int B64::modp_b64_decode(const char* src, int len, char* dest) 00109 { 00110 int i; 00111 if (len == 0) return 0; 00112 00113 #ifdef DOPAD 00114 /* 00115 * if padding is used, then the message must be at least 00116 * 4 chars and be a multiple of 4 00117 */ 00118 if (len < 4 || (len % 4 != 0)) return -1; /* error */ 00119 /* there can be at most 2 pad chars at the end */ 00120 if (src[len-1] == CHARPAD) { 00121 len--; 00122 if (src[len -1] == CHARPAD) { 00123 len--; 00124 } 00125 } 00126 #endif 00127 00128 int leftover = len % 4; 00129 int chunks = (leftover == 0) ? len / 4 - 1 : len /4; 00130 00131 uint8_t* p = (uint8_t*) dest; 00132 uint32_t x = 0; 00133 uint32_t* destInt = (uint32_t*) p; 00134 uint32_t* srcInt = (uint32_t*) src; 00135 uint32_t y = *srcInt++; 00136 for (i = 0; i < chunks; ++i) { 00137 x = d0[y & 0xff] | 00138 d1[(y >> 8) & 0xff] | 00139 d2[(y >> 16) & 0xff] | 00140 d3[(y >> 24) & 0xff]; 00141 00142 if (x >= BADCHAR) return -1; 00143 *destInt = x ; 00144 p += 3; 00145 destInt = (uint32_t*)p; 00146 y = *srcInt++;} 00147 00148 00149 switch (leftover) { 00150 case 0: 00151 x = d0[y & 0xff] | 00152 d1[(y >> 8) & 0xff] | 00153 d2[(y >> 16) & 0xff] | 00154 d3[(y >> 24) & 0xff]; 00155 00156 if (x >= BADCHAR) return -1; 00157 *p++ = ((uint8_t*)(&x))[0]; 00158 *p++ = ((uint8_t*)(&x))[1]; 00159 *p = ((uint8_t*)(&x))[2]; 00160 return (chunks+1)*3; 00161 //break; 00162 #ifndef DOPAD 00163 case 1: /* with padding this is an impossible case */ 00164 x = d0[y & 0xff]; 00165 *p = *((uint8_t*)(&x)); // i.e. first char/byte in int 00166 break; 00167 #endif 00168 case 2: // * case 2, 1 output byte */ 00169 x = d0[y & 0xff] | d1[y >> 8 & 0xff]; 00170 *p = *((uint8_t*)(&x)); // i.e. first char 00171 break; 00172 default: /* case 3, 2 output bytes */ 00173 x = d0[y & 0xff] | 00174 d1[y >> 8 & 0xff ] | 00175 d2[y >> 16 & 0xff]; /* 0x3c */ 00176 *p++ = ((uint8_t*)(&x))[0]; 00177 *p = ((uint8_t*)(&x))[1]; 00178 break; 00179 } 00180 00181 if (x >= BADCHAR) return -1; 00182 00183 return 3*chunks + (6*leftover)/8; 00184 } 00185 */ 00186 #endif
Generated on Thu Jul 14 2022 21:01:57 by
 1.7.2
 1.7.2