Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnBase64.cpp@3:24c5f0f50bf1, 2012-07-31 (annotated)
- Committer:
- uci1
- Date:
- Tue Jul 31 04:59:16 2012 +0000
- Revision:
- 3:24c5f0f50bf1
- Parent:
- 1:e392595b4b76
- Child:
- 84:80b15993944e
Test bench version. Communications not completed. Debugging output present. But will read the local config file and save events that can be used for testing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 3:24c5f0f50bf1 | 1 | #include "SnBase64.h" |
uci1 | 3:24c5f0f50bf1 | 2 | |
uci1 | 3:24c5f0f50bf1 | 3 | #include "SnBase64data.h" |
uci1 | 3:24c5f0f50bf1 | 4 | |
uci1 | 3:24c5f0f50bf1 | 5 | /** |
uci1 | 3:24c5f0f50bf1 | 6 | * \file modp_b64.c |
uci1 | 3:24c5f0f50bf1 | 7 | * <PRE> |
uci1 | 3:24c5f0f50bf1 | 8 | * MODP_B64 - High performance base64 encoder/decoder |
uci1 | 3:24c5f0f50bf1 | 9 | * http://code.google.com/p/stringencoders/ |
uci1 | 3:24c5f0f50bf1 | 10 | * |
uci1 | 3:24c5f0f50bf1 | 11 | * Copyright © 2005, 2006, 2007 Nick Galbreath -- nickg [at] modp [dot] com |
uci1 | 3:24c5f0f50bf1 | 12 | * All rights reserved. |
uci1 | 3:24c5f0f50bf1 | 13 | * |
uci1 | 3:24c5f0f50bf1 | 14 | * Redistribution and use in source and binary forms, with or without |
uci1 | 3:24c5f0f50bf1 | 15 | * modification, are permitted provided that the following conditions are |
uci1 | 3:24c5f0f50bf1 | 16 | * met: |
uci1 | 3:24c5f0f50bf1 | 17 | * |
uci1 | 3:24c5f0f50bf1 | 18 | * Redistributions of source code must retain the above copyright |
uci1 | 3:24c5f0f50bf1 | 19 | * notice, this list of conditions and the following disclaimer. |
uci1 | 3:24c5f0f50bf1 | 20 | * |
uci1 | 3:24c5f0f50bf1 | 21 | * Redistributions in binary form must reproduce the above copyright |
uci1 | 3:24c5f0f50bf1 | 22 | * notice, this list of conditions and the following disclaimer in the |
uci1 | 3:24c5f0f50bf1 | 23 | * documentation and/or other materials provided with the distribution. |
uci1 | 3:24c5f0f50bf1 | 24 | * |
uci1 | 3:24c5f0f50bf1 | 25 | * Neither the name of the modp.com nor the names of its |
uci1 | 3:24c5f0f50bf1 | 26 | * contributors may be used to endorse or promote products derived from |
uci1 | 3:24c5f0f50bf1 | 27 | * this software without specific prior written permission. |
uci1 | 3:24c5f0f50bf1 | 28 | * |
uci1 | 3:24c5f0f50bf1 | 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
uci1 | 3:24c5f0f50bf1 | 30 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
uci1 | 3:24c5f0f50bf1 | 31 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
uci1 | 3:24c5f0f50bf1 | 32 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
uci1 | 3:24c5f0f50bf1 | 33 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
uci1 | 3:24c5f0f50bf1 | 34 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
uci1 | 3:24c5f0f50bf1 | 35 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
uci1 | 3:24c5f0f50bf1 | 36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
uci1 | 3:24c5f0f50bf1 | 37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
uci1 | 3:24c5f0f50bf1 | 38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
uci1 | 3:24c5f0f50bf1 | 39 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
uci1 | 3:24c5f0f50bf1 | 40 | * |
uci1 | 3:24c5f0f50bf1 | 41 | * This is the standard "new" BSD license: |
uci1 | 3:24c5f0f50bf1 | 42 | * http://www.opensource.org/licenses/bsd-license.php |
uci1 | 3:24c5f0f50bf1 | 43 | * </PRE> |
uci1 | 3:24c5f0f50bf1 | 44 | */ |
uci1 | 3:24c5f0f50bf1 | 45 | |
uci1 | 3:24c5f0f50bf1 | 46 | #define BADCHAR 0x01FFFFFF |
uci1 | 3:24c5f0f50bf1 | 47 | |
uci1 | 3:24c5f0f50bf1 | 48 | /** |
uci1 | 3:24c5f0f50bf1 | 49 | * you can control if we use padding by commenting out this |
uci1 | 3:24c5f0f50bf1 | 50 | * next line. However, I highly recommend you use padding and not |
uci1 | 3:24c5f0f50bf1 | 51 | * using it should only be for compatability with a 3rd party. |
uci1 | 3:24c5f0f50bf1 | 52 | * Also, 'no padding' is not tested! |
uci1 | 3:24c5f0f50bf1 | 53 | */ |
uci1 | 3:24c5f0f50bf1 | 54 | #define DOPAD 1 |
uci1 | 3:24c5f0f50bf1 | 55 | |
uci1 | 1:e392595b4b76 | 56 | /* |
uci1 | 3:24c5f0f50bf1 | 57 | * if we aren't doing padding |
uci1 | 3:24c5f0f50bf1 | 58 | * set the pad character to NULL |
uci1 | 3:24c5f0f50bf1 | 59 | */ |
uci1 | 3:24c5f0f50bf1 | 60 | #ifndef DOPAD |
uci1 | 3:24c5f0f50bf1 | 61 | #undef CHARPAD |
uci1 | 3:24c5f0f50bf1 | 62 | #define CHARPAD '\0' |
uci1 | 1:e392595b4b76 | 63 | #endif |
uci1 | 1:e392595b4b76 | 64 | |
uci1 | 3:24c5f0f50bf1 | 65 | int B64::modp_b64_encode(const char* str, const int len, char* dest) |
uci1 | 3:24c5f0f50bf1 | 66 | { |
uci1 | 3:24c5f0f50bf1 | 67 | int i; |
uci1 | 3:24c5f0f50bf1 | 68 | const uint8_t* s = (const uint8_t*) str; |
uci1 | 3:24c5f0f50bf1 | 69 | uint8_t* p = (uint8_t*) dest; |
uci1 | 3:24c5f0f50bf1 | 70 | |
uci1 | 3:24c5f0f50bf1 | 71 | /* unsigned here is important! */ |
uci1 | 3:24c5f0f50bf1 | 72 | /* uint8_t is fastest on G4, amd */ |
uci1 | 3:24c5f0f50bf1 | 73 | /* uint32_t is fastest on Intel */ |
uci1 | 3:24c5f0f50bf1 | 74 | uint32_t t1, t2, t3; |
uci1 | 3:24c5f0f50bf1 | 75 | |
uci1 | 3:24c5f0f50bf1 | 76 | for (i = 0; i < len - 2; i += 3) { |
uci1 | 3:24c5f0f50bf1 | 77 | t1 = s[i]; t2 = s[i+1]; t3 = s[i+2]; |
uci1 | 3:24c5f0f50bf1 | 78 | *p++ = e0[t1]; |
uci1 | 3:24c5f0f50bf1 | 79 | *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
uci1 | 3:24c5f0f50bf1 | 80 | *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)]; |
uci1 | 3:24c5f0f50bf1 | 81 | *p++ = e2[t3]; |
uci1 | 3:24c5f0f50bf1 | 82 | } |
uci1 | 1:e392595b4b76 | 83 | |
uci1 | 3:24c5f0f50bf1 | 84 | switch (len - i) { |
uci1 | 3:24c5f0f50bf1 | 85 | case 0: |
uci1 | 3:24c5f0f50bf1 | 86 | break; |
uci1 | 3:24c5f0f50bf1 | 87 | case 1: |
uci1 | 3:24c5f0f50bf1 | 88 | t1 = s[i]; |
uci1 | 3:24c5f0f50bf1 | 89 | *p++ = e0[t1]; |
uci1 | 3:24c5f0f50bf1 | 90 | *p++ = e1[(t1 & 0x03) << 4]; |
uci1 | 3:24c5f0f50bf1 | 91 | *p++ = CHARPAD; |
uci1 | 3:24c5f0f50bf1 | 92 | *p++ = CHARPAD; |
uci1 | 3:24c5f0f50bf1 | 93 | break; |
uci1 | 3:24c5f0f50bf1 | 94 | default: /* case 2 */ |
uci1 | 3:24c5f0f50bf1 | 95 | t1 = s[i]; t2 = s[i+1]; |
uci1 | 3:24c5f0f50bf1 | 96 | *p++ = e0[t1]; |
uci1 | 3:24c5f0f50bf1 | 97 | *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
uci1 | 3:24c5f0f50bf1 | 98 | *p++ = e2[(t2 & 0x0F) << 2]; |
uci1 | 3:24c5f0f50bf1 | 99 | *p++ = CHARPAD; |
uci1 | 3:24c5f0f50bf1 | 100 | } |
uci1 | 3:24c5f0f50bf1 | 101 | |
uci1 | 3:24c5f0f50bf1 | 102 | *p = '\0'; |
uci1 | 3:24c5f0f50bf1 | 103 | return (int)(p - (uint8_t*)dest); |
uci1 | 1:e392595b4b76 | 104 | } |
uci1 | 1:e392595b4b76 | 105 | |
uci1 | 3:24c5f0f50bf1 | 106 | int B64::modp_b64_decode(const char* src, int len, char* dest) |
uci1 | 3:24c5f0f50bf1 | 107 | { |
uci1 | 3:24c5f0f50bf1 | 108 | int i; |
uci1 | 3:24c5f0f50bf1 | 109 | if (len == 0) return 0; |
uci1 | 3:24c5f0f50bf1 | 110 | |
uci1 | 3:24c5f0f50bf1 | 111 | #ifdef DOPAD |
uci1 | 3:24c5f0f50bf1 | 112 | /* |
uci1 | 3:24c5f0f50bf1 | 113 | * if padding is used, then the message must be at least |
uci1 | 3:24c5f0f50bf1 | 114 | * 4 chars and be a multiple of 4 |
uci1 | 3:24c5f0f50bf1 | 115 | */ |
uci1 | 3:24c5f0f50bf1 | 116 | if (len < 4 || (len % 4 != 0)) return -1; /* error */ |
uci1 | 3:24c5f0f50bf1 | 117 | /* there can be at most 2 pad chars at the end */ |
uci1 | 3:24c5f0f50bf1 | 118 | if (src[len-1] == CHARPAD) { |
uci1 | 3:24c5f0f50bf1 | 119 | len--; |
uci1 | 3:24c5f0f50bf1 | 120 | if (src[len -1] == CHARPAD) { |
uci1 | 3:24c5f0f50bf1 | 121 | len--; |
uci1 | 3:24c5f0f50bf1 | 122 | } |
uci1 | 3:24c5f0f50bf1 | 123 | } |
uci1 | 3:24c5f0f50bf1 | 124 | #endif |
uci1 | 3:24c5f0f50bf1 | 125 | |
uci1 | 3:24c5f0f50bf1 | 126 | int leftover = len % 4; |
uci1 | 3:24c5f0f50bf1 | 127 | int chunks = (leftover == 0) ? len / 4 - 1 : len /4; |
uci1 | 3:24c5f0f50bf1 | 128 | |
uci1 | 3:24c5f0f50bf1 | 129 | uint8_t* p = (uint8_t*) dest; |
uci1 | 3:24c5f0f50bf1 | 130 | uint32_t x = 0; |
uci1 | 3:24c5f0f50bf1 | 131 | uint32_t* destInt = (uint32_t*) p; |
uci1 | 3:24c5f0f50bf1 | 132 | uint32_t* srcInt = (uint32_t*) src; |
uci1 | 3:24c5f0f50bf1 | 133 | uint32_t y = *srcInt++; |
uci1 | 3:24c5f0f50bf1 | 134 | for (i = 0; i < chunks; ++i) { |
uci1 | 3:24c5f0f50bf1 | 135 | x = d0[y & 0xff] | |
uci1 | 3:24c5f0f50bf1 | 136 | d1[(y >> 8) & 0xff] | |
uci1 | 3:24c5f0f50bf1 | 137 | d2[(y >> 16) & 0xff] | |
uci1 | 3:24c5f0f50bf1 | 138 | d3[(y >> 24) & 0xff]; |
uci1 | 3:24c5f0f50bf1 | 139 | |
uci1 | 3:24c5f0f50bf1 | 140 | if (x >= BADCHAR) return -1; |
uci1 | 3:24c5f0f50bf1 | 141 | *destInt = x ; |
uci1 | 3:24c5f0f50bf1 | 142 | p += 3; |
uci1 | 3:24c5f0f50bf1 | 143 | destInt = (uint32_t*)p; |
uci1 | 3:24c5f0f50bf1 | 144 | y = *srcInt++;} |
uci1 | 3:24c5f0f50bf1 | 145 | |
uci1 | 3:24c5f0f50bf1 | 146 | |
uci1 | 3:24c5f0f50bf1 | 147 | switch (leftover) { |
uci1 | 3:24c5f0f50bf1 | 148 | case 0: |
uci1 | 3:24c5f0f50bf1 | 149 | x = d0[y & 0xff] | |
uci1 | 3:24c5f0f50bf1 | 150 | d1[(y >> 8) & 0xff] | |
uci1 | 3:24c5f0f50bf1 | 151 | d2[(y >> 16) & 0xff] | |
uci1 | 3:24c5f0f50bf1 | 152 | d3[(y >> 24) & 0xff]; |
uci1 | 3:24c5f0f50bf1 | 153 | |
uci1 | 3:24c5f0f50bf1 | 154 | if (x >= BADCHAR) return -1; |
uci1 | 3:24c5f0f50bf1 | 155 | *p++ = ((uint8_t*)(&x))[0]; |
uci1 | 3:24c5f0f50bf1 | 156 | *p++ = ((uint8_t*)(&x))[1]; |
uci1 | 3:24c5f0f50bf1 | 157 | *p = ((uint8_t*)(&x))[2]; |
uci1 | 3:24c5f0f50bf1 | 158 | return (chunks+1)*3; |
uci1 | 3:24c5f0f50bf1 | 159 | //break; |
uci1 | 3:24c5f0f50bf1 | 160 | #ifndef DOPAD |
uci1 | 3:24c5f0f50bf1 | 161 | case 1: /* with padding this is an impossible case */ |
uci1 | 3:24c5f0f50bf1 | 162 | x = d0[y & 0xff]; |
uci1 | 3:24c5f0f50bf1 | 163 | *p = *((uint8_t*)(&x)); // i.e. first char/byte in int |
uci1 | 3:24c5f0f50bf1 | 164 | break; |
uci1 | 3:24c5f0f50bf1 | 165 | #endif |
uci1 | 3:24c5f0f50bf1 | 166 | case 2: // * case 2, 1 output byte */ |
uci1 | 3:24c5f0f50bf1 | 167 | x = d0[y & 0xff] | d1[y >> 8 & 0xff]; |
uci1 | 3:24c5f0f50bf1 | 168 | *p = *((uint8_t*)(&x)); // i.e. first char |
uci1 | 3:24c5f0f50bf1 | 169 | break; |
uci1 | 3:24c5f0f50bf1 | 170 | default: /* case 3, 2 output bytes */ |
uci1 | 3:24c5f0f50bf1 | 171 | x = d0[y & 0xff] | |
uci1 | 3:24c5f0f50bf1 | 172 | d1[y >> 8 & 0xff ] | |
uci1 | 3:24c5f0f50bf1 | 173 | d2[y >> 16 & 0xff]; /* 0x3c */ |
uci1 | 3:24c5f0f50bf1 | 174 | *p++ = ((uint8_t*)(&x))[0]; |
uci1 | 3:24c5f0f50bf1 | 175 | *p = ((uint8_t*)(&x))[1]; |
uci1 | 3:24c5f0f50bf1 | 176 | break; |
uci1 | 3:24c5f0f50bf1 | 177 | } |
uci1 | 3:24c5f0f50bf1 | 178 | |
uci1 | 3:24c5f0f50bf1 | 179 | if (x >= BADCHAR) return -1; |
uci1 | 3:24c5f0f50bf1 | 180 | |
uci1 | 3:24c5f0f50bf1 | 181 | return 3*chunks + (6*leftover)/8; |
uci1 | 1:e392595b4b76 | 182 | } |