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
Diff: SnBase64.cpp
- Revision:
- 3:24c5f0f50bf1
- Parent:
- 1:e392595b4b76
- Child:
- 84:80b15993944e
--- a/SnBase64.cpp Tue Jul 24 02:07:23 2012 +0000
+++ b/SnBase64.cpp Tue Jul 31 04:59:16 2012 +0000
@@ -1,65 +1,182 @@
+#include "SnBase64.h"
+
+#include "SnBase64data.h"
+
+/**
+ * \file modp_b64.c
+ * <PRE>
+ * MODP_B64 - High performance base64 encoder/decoder
+ * http://code.google.com/p/stringencoders/
+ *
+ * Copyright © 2005, 2006, 2007 Nick Galbreath -- nickg [at] modp [dot] com
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the modp.com nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This is the standard "new" BSD license:
+ * http://www.opensource.org/licenses/bsd-license.php
+ * </PRE>
+ */
+
+#define BADCHAR 0x01FFFFFF
+
+/**
+ * you can control if we use padding by commenting out this
+ * next line. However, I highly recommend you use padding and not
+ * using it should only be for compatability with a 3rd party.
+ * Also, 'no padding' is not tested!
+ */
+#define DOPAD 1
+
/*
-Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
-*/
-
-#include "SnBase64.h"
-//Originaly from Rolf's iputil.c
-
-#ifdef __cplusplus
-extern "C" {
+ * if we aren't doing padding
+ * set the pad character to NULL
+ */
+#ifndef DOPAD
+#undef CHARPAD
+#define CHARPAD '\0'
#endif
-#include <string.h>
-/*
-uint32_t base64enc_len(const uint32_t slen) {
- return (((slen-1)/3)+1)<<2;
-}
-*/
-/*
-uint32_t base64enc_len(const int8_t* str) {
- //return (((strlen(str)-1)/3)+1)<<2;
- return base64enc_len(strlen(str));
-}
-*/
-void base64enc(const char *input, uint32_t length, char *output) {
- static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- uint32_t c, c1, c2, c3;
- for(uint32_t i = 0, j = 0; i<length; i+=3u,j+=4u) {
- c1 = ((((uint8_t)*((uint8_t*)&input[i]))));
- c2 = (length>i+1)?((((uint8_t)*((uint8_t *)&input[i+1])))):0;
- c3 = (length>i+2)?((((uint8_t)*((uint8_t *)&input[i+2])))):0;
+int B64::modp_b64_encode(const char* str, const int len, char* dest)
+{
+ int i;
+ const uint8_t* s = (const uint8_t*) str;
+ uint8_t* p = (uint8_t*) dest;
+
+ /* unsigned here is important! */
+ /* uint8_t is fastest on G4, amd */
+ /* uint32_t is fastest on Intel */
+ uint32_t t1, t2, t3;
+
+ for (i = 0; i < len - 2; i += 3) {
+ t1 = s[i]; t2 = s[i+1]; t3 = s[i+2];
+ *p++ = e0[t1];
+ *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
+ *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
+ *p++ = e2[t3];
+ }
- c = ((c1 & 0xFC) >> 2);
- output[j+0] = base64[c];
- c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
- output[j+1] = base64[c];
- c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
- output[j+2] = (length>i+1)?base64[c]:'=';
- c = (c3 & 0x3F);
- output[j+3] = (length>i+2)?base64[c]:'=';
- }
- output[(((length-1)/3)+1)<<2] = '\0';
- //output[base64enc_len(length)] = '\0';
+ switch (len - i) {
+ case 0:
+ break;
+ case 1:
+ t1 = s[i];
+ *p++ = e0[t1];
+ *p++ = e1[(t1 & 0x03) << 4];
+ *p++ = CHARPAD;
+ *p++ = CHARPAD;
+ break;
+ default: /* case 2 */
+ t1 = s[i]; t2 = s[i+1];
+ *p++ = e0[t1];
+ *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
+ *p++ = e2[(t2 & 0x0F) << 2];
+ *p++ = CHARPAD;
+ }
+
+ *p = '\0';
+ return (int)(p - (uint8_t*)dest);
}
-#ifdef __cplusplus
+int B64::modp_b64_decode(const char* src, int len, char* dest)
+{
+ int i;
+ if (len == 0) return 0;
+
+#ifdef DOPAD
+ /*
+ * if padding is used, then the message must be at least
+ * 4 chars and be a multiple of 4
+ */
+ if (len < 4 || (len % 4 != 0)) return -1; /* error */
+ /* there can be at most 2 pad chars at the end */
+ if (src[len-1] == CHARPAD) {
+ len--;
+ if (src[len -1] == CHARPAD) {
+ len--;
+ }
+ }
+#endif
+
+ int leftover = len % 4;
+ int chunks = (leftover == 0) ? len / 4 - 1 : len /4;
+
+ uint8_t* p = (uint8_t*) dest;
+ uint32_t x = 0;
+ uint32_t* destInt = (uint32_t*) p;
+ uint32_t* srcInt = (uint32_t*) src;
+ uint32_t y = *srcInt++;
+ for (i = 0; i < chunks; ++i) {
+ x = d0[y & 0xff] |
+ d1[(y >> 8) & 0xff] |
+ d2[(y >> 16) & 0xff] |
+ d3[(y >> 24) & 0xff];
+
+ if (x >= BADCHAR) return -1;
+ *destInt = x ;
+ p += 3;
+ destInt = (uint32_t*)p;
+ y = *srcInt++;}
+
+
+ switch (leftover) {
+ case 0:
+ x = d0[y & 0xff] |
+ d1[(y >> 8) & 0xff] |
+ d2[(y >> 16) & 0xff] |
+ d3[(y >> 24) & 0xff];
+
+ if (x >= BADCHAR) return -1;
+ *p++ = ((uint8_t*)(&x))[0];
+ *p++ = ((uint8_t*)(&x))[1];
+ *p = ((uint8_t*)(&x))[2];
+ return (chunks+1)*3;
+ //break;
+#ifndef DOPAD
+ case 1: /* with padding this is an impossible case */
+ x = d0[y & 0xff];
+ *p = *((uint8_t*)(&x)); // i.e. first char/byte in int
+ break;
+#endif
+ case 2: // * case 2, 1 output byte */
+ x = d0[y & 0xff] | d1[y >> 8 & 0xff];
+ *p = *((uint8_t*)(&x)); // i.e. first char
+ break;
+ default: /* case 3, 2 output bytes */
+ x = d0[y & 0xff] |
+ d1[y >> 8 & 0xff ] |
+ d2[y >> 16 & 0xff]; /* 0x3c */
+ *p++ = ((uint8_t*)(&x))[0];
+ *p = ((uint8_t*)(&x))[1];
+ break;
+ }
+
+ if (x >= BADCHAR) return -1;
+
+ return 3*chunks + (6*leftover)/8;
}
-#endif
\ No newline at end of file