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