Exportable version of WizziLab's modem driver.

Dependents:   modem_ref_helper

Committer:
marin_wizzi
Date:
Fri Oct 29 13:54:43 2021 +0000
Revision:
67:e458db8402dc
Parent:
41:6f83174ffed4
had to be commited for Scanner test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jeej 41:6f83174ffed4 1 /// @copyright
Jeej 41:6f83174ffed4 2 /// ========================================================================={{{
Jeej 41:6f83174ffed4 3 /// Copyright (c) 20XX WizziLab /
Jeej 41:6f83174ffed4 4 /// All rights reserved /
Jeej 41:6f83174ffed4 5 /// /
Jeej 41:6f83174ffed4 6 /// IMPORTANT: This Software may not be modified, copied or distributed unless /
Jeej 41:6f83174ffed4 7 /// embedded on a WizziLab product. Other than for the foregoing purpose, this /
Jeej 41:6f83174ffed4 8 /// Software and/or its documentation may not be used, reproduced, copied, /
Jeej 41:6f83174ffed4 9 /// prepared derivative works of, modified, performed, distributed, displayed /
Jeej 41:6f83174ffed4 10 /// or sold for any purpose. For the sole purpose of embedding this Software /
Jeej 41:6f83174ffed4 11 /// on a WizziLab product, copy, modification and distribution of this /
Jeej 41:6f83174ffed4 12 /// Software is granted provided that the following conditions are respected: /
Jeej 41:6f83174ffed4 13 /// /
Jeej 41:6f83174ffed4 14 /// * Redistributions of source code must retain the above copyright notice, /
Jeej 41:6f83174ffed4 15 /// this list of conditions and the following disclaimer /
Jeej 41:6f83174ffed4 16 /// /
Jeej 41:6f83174ffed4 17 /// * Redistributions in binary form must reproduce the above copyright /
Jeej 41:6f83174ffed4 18 /// notice, this list of conditions and the following disclaimer in the /
Jeej 41:6f83174ffed4 19 /// documentation and/or other materials provided with the distribution. /
Jeej 41:6f83174ffed4 20 /// /
Jeej 41:6f83174ffed4 21 /// * The name of WizziLab can not be used to endorse or promote products /
Jeej 41:6f83174ffed4 22 /// derived from this software without specific prior written permission. /
Jeej 41:6f83174ffed4 23 /// /
Jeej 41:6f83174ffed4 24 /// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS /
Jeej 41:6f83174ffed4 25 /// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED /
Jeej 41:6f83174ffed4 26 /// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR /
Jeej 41:6f83174ffed4 27 /// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR /
Jeej 41:6f83174ffed4 28 /// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, /
Jeej 41:6f83174ffed4 29 /// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, /
Jeej 41:6f83174ffed4 30 /// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, /
Jeej 41:6f83174ffed4 31 /// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY /
Jeej 41:6f83174ffed4 32 /// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING /
Jeej 41:6f83174ffed4 33 /// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS /
Jeej 41:6f83174ffed4 34 /// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /
Jeej 41:6f83174ffed4 35 /// WIZZILAB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, /
Jeej 41:6f83174ffed4 36 /// ENHANCEMENTS OR MODIFICATIONS. /
Jeej 41:6f83174ffed4 37 /// /
Jeej 41:6f83174ffed4 38 /// Should you have any questions regarding your right to use this Software, /
Jeej 41:6f83174ffed4 39 /// contact WizziLab at www.wizzilab.com. /
Jeej 41:6f83174ffed4 40 /// /
Jeej 41:6f83174ffed4 41 /// =========================================================================}}}
Jeej 41:6f83174ffed4 42 /// @endcopyright
Jeej 41:6f83174ffed4 43 ///
Jeej 41:6f83174ffed4 44 /// =======================================================================
Jeej 41:6f83174ffed4 45 ///
Jeej 41:6f83174ffed4 46 /// @file kal_codec.c
Jeej 41:6f83174ffed4 47 /// @brief Codec Utilities
Jeej 41:6f83174ffed4 48 ///
Jeej 41:6f83174ffed4 49 /// =======================================================================
Jeej 41:6f83174ffed4 50
Jeej 41:6f83174ffed4 51 #include <string.h>
Jeej 41:6f83174ffed4 52 #include "hal_types.h"
Jeej 41:6f83174ffed4 53 #include "kal_codec.h"
Jeej 41:6f83174ffed4 54
Jeej 41:6f83174ffed4 55
Jeej 41:6f83174ffed4 56 // ======================================================================
Jeej 41:6f83174ffed4 57 //
Jeej 41:6f83174ffed4 58 //
Jeej 41:6f83174ffed4 59 // ASCII to Binary Codec Toolkits
Jeej 41:6f83174ffed4 60 //
Jeej 41:6f83174ffed4 61 //
Jeej 41:6f83174ffed4 62 // ======================================================================
Jeej 41:6f83174ffed4 63
Jeej 41:6f83174ffed4 64 const char k_nybble_chars[] = "0123456789ABCDEF";
Jeej 41:6f83174ffed4 65
Jeej 41:6f83174ffed4 66 //======================================================================
Jeej 41:6f83174ffed4 67 // hex2ascii
Jeej 41:6f83174ffed4 68 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 69 /// @brief Convert hexadecimal number to ascii
Jeej 41:6f83174ffed4 70 /// @param char* in input buffer (hex)
Jeej 41:6f83174ffed4 71 /// @param char* out output buffer (ascii)
Jeej 41:6f83174ffed4 72 /// @param u16 len length of inoput buffer in bytes
Jeej 41:6f83174ffed4 73 //======================================================================
Jeej 41:6f83174ffed4 74 void hex2ascii( char* in, char* out, u16 length)
Jeej 41:6f83174ffed4 75 {
Jeej 41:6f83174ffed4 76 u8 b;
Jeej 41:6f83174ffed4 77 while(length--) {
Jeej 41:6f83174ffed4 78 b = *in++;
Jeej 41:6f83174ffed4 79 *out++ = k_nybble_chars[ ( b >> 4 ) & 0x0F ];
Jeej 41:6f83174ffed4 80 *out++ = k_nybble_chars[ b & 0x0F ];
Jeej 41:6f83174ffed4 81 }
Jeej 41:6f83174ffed4 82 }
Jeej 41:6f83174ffed4 83
Jeej 41:6f83174ffed4 84 //======================================================================
Jeej 41:6f83174ffed4 85 // itoa
Jeej 41:6f83174ffed4 86 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 87 /// @brief Converts an integer value to a null-terminated string using the specified base and stores the result
Jeej 41:6f83174ffed4 88 /// in the array given by result parameter.
Jeej 41:6f83174ffed4 89 /// @param int value integer to convert
Jeej 41:6f83174ffed4 90 /// @param char* result converted string
Jeej 41:6f83174ffed4 91 /// @param int base base used for conversion 2, 8, 10, 16
Jeej 41:6f83174ffed4 92 /// @return A pointer to the resulting null-terminated string, same as parameter result
Jeej 41:6f83174ffed4 93 //======================================================================
Jeej 41:6f83174ffed4 94 char* itoa(int value, char* result, int base)
Jeej 41:6f83174ffed4 95 {
Jeej 41:6f83174ffed4 96 // check that the base if valid
Jeej 41:6f83174ffed4 97 //if (base < 2 || base > 16) { return NULL; }
Jeej 41:6f83174ffed4 98
Jeej 41:6f83174ffed4 99 char* ptr = result, *ptr1 = result, tmp_char;
Jeej 41:6f83174ffed4 100 int tmp_value;
Jeej 41:6f83174ffed4 101
Jeej 41:6f83174ffed4 102 do {
Jeej 41:6f83174ffed4 103 tmp_value = value;
Jeej 41:6f83174ffed4 104 value /= base;
Jeej 41:6f83174ffed4 105 *ptr++ = "fedcba9876543210123456789abcdef" [15 + (tmp_value - value * base)];
Jeej 41:6f83174ffed4 106 } while ( value );
Jeej 41:6f83174ffed4 107
Jeej 41:6f83174ffed4 108 // Apply negative sign
Jeej 41:6f83174ffed4 109 if (tmp_value < 0) *ptr++ = '-';
Jeej 41:6f83174ffed4 110 //*ptr-- = '\0';
Jeej 41:6f83174ffed4 111 result = ptr--;
Jeej 41:6f83174ffed4 112 while(ptr1 < ptr) {
Jeej 41:6f83174ffed4 113 tmp_char = *ptr;
Jeej 41:6f83174ffed4 114 *ptr--= *ptr1;
Jeej 41:6f83174ffed4 115 *ptr1++ = tmp_char;
Jeej 41:6f83174ffed4 116 }
Jeej 41:6f83174ffed4 117 return result;
Jeej 41:6f83174ffed4 118 }
Jeej 41:6f83174ffed4 119
Jeej 41:6f83174ffed4 120 //======================================================================
Jeej 41:6f83174ffed4 121 // atoitok
Jeej 41:6f83174ffed4 122 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 123 /// @brief Extracts from a ASCII string a value (decimal or hex) and
Jeej 41:6f83174ffed4 124 /// convert it to an integer value.
Jeej 41:6f83174ffed4 125 /// - Blank characters are skipped
Jeej 41:6f83174ffed4 126 /// - hexa values must begin with '0x' or '0X'
Jeej 41:6f83174ffed4 127 /// - '-' sign is handled
Jeej 41:6f83174ffed4 128 /// - returns after one value has been parsed or null character
Jeej 41:6f83174ffed4 129 /// or CR character has been found.
Jeej 41:6f83174ffed4 130 /// @param p pointer to pointer to the string to be parsed. The pointer
Jeej 41:6f83174ffed4 131 /// to string is modified: at the end of the call it points to
Jeej 41:6f83174ffed4 132 /// the character following the last parsed one.
Jeej 41:6f83174ffed4 133 //======================================================================
Jeej 41:6f83174ffed4 134 int atoitok(u8 **p)
Jeej 41:6f83174ffed4 135 {
Jeej 41:6f83174ffed4 136 int k = 0;
Jeej 41:6f83174ffed4 137 u8 base16=0;
Jeej 41:6f83174ffed4 138 s8 sign = 1;
Jeej 41:6f83174ffed4 139 while ((**p==' ')||(**p=='\t')||(**p==',')) (*p)++;
Jeej 41:6f83174ffed4 140 if (**p=='-')
Jeej 41:6f83174ffed4 141 {
Jeej 41:6f83174ffed4 142 sign = -1;
Jeej 41:6f83174ffed4 143 (*p)++;
Jeej 41:6f83174ffed4 144 }
Jeej 41:6f83174ffed4 145 if ((**p=='0') && (*(*p+1)=='x'))
Jeej 41:6f83174ffed4 146 {
Jeej 41:6f83174ffed4 147 base16 = 1;
Jeej 41:6f83174ffed4 148 *p += 2;
Jeej 41:6f83174ffed4 149 }
Jeej 41:6f83174ffed4 150 while (**p && **p!=' ' && **p!=0xa && **p!=',') {
Jeej 41:6f83174ffed4 151 if (!base16)
Jeej 41:6f83174ffed4 152 {
Jeej 41:6f83174ffed4 153 k = (k<<3)+(k<<1)+(**p)-'0';
Jeej 41:6f83174ffed4 154 }
Jeej 41:6f83174ffed4 155 else
Jeej 41:6f83174ffed4 156 {
Jeej 41:6f83174ffed4 157 if (**p > '9')
Jeej 41:6f83174ffed4 158 if (**p > 'F')
Jeej 41:6f83174ffed4 159 k = (k<<4)+(10+(**p)-'a');
Jeej 41:6f83174ffed4 160 else
Jeej 41:6f83174ffed4 161 k = (k<<4)+(10+(**p)-'A');
Jeej 41:6f83174ffed4 162 else
Jeej 41:6f83174ffed4 163 k = (k<<4)+(**p)-'0';
Jeej 41:6f83174ffed4 164 }
Jeej 41:6f83174ffed4 165 (*p)++;
Jeej 41:6f83174ffed4 166 }
Jeej 41:6f83174ffed4 167 return sign*k;
Jeej 41:6f83174ffed4 168 }
Jeej 41:6f83174ffed4 169
Jeej 41:6f83174ffed4 170 //======================================================================
Jeej 41:6f83174ffed4 171 // kal_atoi
Jeej 41:6f83174ffed4 172 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 173 /// @brief Extracts from a ASCII string a decimal value and
Jeej 41:6f83174ffed4 174 /// convert it to an integer value.
Jeej 41:6f83174ffed4 175 /// '-' and '+' signs are handled
Jeej 41:6f83174ffed4 176 /// @param s char* string to convert
Jeej 41:6f83174ffed4 177 /// @retval s32 integer value
Jeej 41:6f83174ffed4 178 //======================================================================
Jeej 41:6f83174ffed4 179 s32 kal_atoi(u8* s)
Jeej 41:6f83174ffed4 180 {
Jeej 41:6f83174ffed4 181 s32 k = 0;
Jeej 41:6f83174ffed4 182 s8 sign = 1;
Jeej 41:6f83174ffed4 183
Jeej 41:6f83174ffed4 184 if (*s == '-')
Jeej 41:6f83174ffed4 185 {
Jeej 41:6f83174ffed4 186 sign = -1;
Jeej 41:6f83174ffed4 187 s++;
Jeej 41:6f83174ffed4 188 }
Jeej 41:6f83174ffed4 189 else if (*s == '+')
Jeej 41:6f83174ffed4 190 {
Jeej 41:6f83174ffed4 191 s++;
Jeej 41:6f83174ffed4 192 }
Jeej 41:6f83174ffed4 193
Jeej 41:6f83174ffed4 194 while (*s >= '0' && *s <= '9')
Jeej 41:6f83174ffed4 195 {
Jeej 41:6f83174ffed4 196 k = 10 * k + *s - '0';
Jeej 41:6f83174ffed4 197 s++;
Jeej 41:6f83174ffed4 198 }
Jeej 41:6f83174ffed4 199
Jeej 41:6f83174ffed4 200 return (sign * k);
Jeej 41:6f83174ffed4 201 }
Jeej 41:6f83174ffed4 202
Jeej 41:6f83174ffed4 203 //======================================================================
Jeej 41:6f83174ffed4 204 // kal_atoi_float
Jeej 41:6f83174ffed4 205 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 206 /// @brief Extracts from a ASCII string a float value and
Jeej 41:6f83174ffed4 207 /// convert it to an integer value * 10^number_of_decimals.
Jeej 41:6f83174ffed4 208 /// '-' and '+' signs are handled
Jeej 41:6f83174ffed4 209 /// @param s char* string to convert
Jeej 41:6f83174ffed4 210 /// @param d u8 Number of decimals
Jeej 41:6f83174ffed4 211 /// @retval s32 integer value
Jeej 41:6f83174ffed4 212 //======================================================================
Jeej 41:6f83174ffed4 213 s32 kal_atoi_float(u8* s, u8 d)
Jeej 41:6f83174ffed4 214 {
Jeej 41:6f83174ffed4 215 s32 k_int = 0;
Jeej 41:6f83174ffed4 216 s32 k_dec = 0;
Jeej 41:6f83174ffed4 217 s8 sign = 1;
Jeej 41:6f83174ffed4 218 u8 i;
Jeej 41:6f83174ffed4 219
Jeej 41:6f83174ffed4 220 // Check sign
Jeej 41:6f83174ffed4 221 if (*s == '-')
Jeej 41:6f83174ffed4 222 {
Jeej 41:6f83174ffed4 223 sign = -1;
Jeej 41:6f83174ffed4 224 s++;
Jeej 41:6f83174ffed4 225 }
Jeej 41:6f83174ffed4 226 else if (*s == '+')
Jeej 41:6f83174ffed4 227 {
Jeej 41:6f83174ffed4 228 s++;
Jeej 41:6f83174ffed4 229 }
Jeej 41:6f83174ffed4 230
Jeej 41:6f83174ffed4 231 // Calculate integer part
Jeej 41:6f83174ffed4 232 while (*s >= '0' && *s <= '9')
Jeej 41:6f83174ffed4 233 {
Jeej 41:6f83174ffed4 234 k_int = 10 * k_int + *s - '0';
Jeej 41:6f83174ffed4 235 s++;
Jeej 41:6f83174ffed4 236 }
Jeej 41:6f83174ffed4 237
Jeej 41:6f83174ffed4 238 for (i = 0; i < d; i++)
Jeej 41:6f83174ffed4 239 {
Jeej 41:6f83174ffed4 240 k_int *= 10;
Jeej 41:6f83174ffed4 241 }
Jeej 41:6f83174ffed4 242
Jeej 41:6f83174ffed4 243 // Skip float separator
Jeej 41:6f83174ffed4 244 if (*s == '.')
Jeej 41:6f83174ffed4 245 {
Jeej 41:6f83174ffed4 246 s++;
Jeej 41:6f83174ffed4 247 }
Jeej 41:6f83174ffed4 248 else
Jeej 41:6f83174ffed4 249 {
Jeej 41:6f83174ffed4 250 return (sign * k_int);
Jeej 41:6f83174ffed4 251 }
Jeej 41:6f83174ffed4 252
Jeej 41:6f83174ffed4 253 // Calculate decimal part
Jeej 41:6f83174ffed4 254 while (*s >= '0' && *s <= '9' && d--)
Jeej 41:6f83174ffed4 255 {
Jeej 41:6f83174ffed4 256 k_dec = 10 * k_dec + *s - '0';
Jeej 41:6f83174ffed4 257 s++;
Jeej 41:6f83174ffed4 258 }
Jeej 41:6f83174ffed4 259
Jeej 41:6f83174ffed4 260 // If string is finished but all the decimals aren't there, complete as if they were followed by 0s
Jeej 41:6f83174ffed4 261 for (i = 0; i < d; i++)
Jeej 41:6f83174ffed4 262 {
Jeej 41:6f83174ffed4 263 k_dec *= 10;
Jeej 41:6f83174ffed4 264 }
Jeej 41:6f83174ffed4 265
Jeej 41:6f83174ffed4 266 return (sign * (k_int + k_dec));
Jeej 41:6f83174ffed4 267 }
Jeej 41:6f83174ffed4 268 //======================================================================
Jeej 41:6f83174ffed4 269 // kal_atoi_hex
Jeej 41:6f83174ffed4 270 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 271 /// @brief Extracts from a ASCII string a hex value and
Jeej 41:6f83174ffed4 272 /// convert it to an integer value.
Jeej 41:6f83174ffed4 273 /// @param s char* string to convert
Jeej 41:6f83174ffed4 274 /// @retval u32 integer value
Jeej 41:6f83174ffed4 275 //======================================================================
Jeej 41:6f83174ffed4 276 u32 kal_atoi_hex(u8* s)
Jeej 41:6f83174ffed4 277 {
Jeej 41:6f83174ffed4 278 u32 k = 0;
Jeej 41:6f83174ffed4 279
Jeej 41:6f83174ffed4 280 while (*s != '\0')
Jeej 41:6f83174ffed4 281 {
Jeej 41:6f83174ffed4 282 if (*s > '9')
Jeej 41:6f83174ffed4 283 {
Jeej 41:6f83174ffed4 284 k = 16 * k + 10 + (*s) - ((*s > 'F') ? 'a' : 'A');
Jeej 41:6f83174ffed4 285 }
Jeej 41:6f83174ffed4 286 else
Jeej 41:6f83174ffed4 287 {
Jeej 41:6f83174ffed4 288 k = 16 * k + (*s) - '0';
Jeej 41:6f83174ffed4 289 }
Jeej 41:6f83174ffed4 290 s++;
Jeej 41:6f83174ffed4 291 }
Jeej 41:6f83174ffed4 292
Jeej 41:6f83174ffed4 293 return (k);
Jeej 41:6f83174ffed4 294 }
Jeej 41:6f83174ffed4 295
Jeej 41:6f83174ffed4 296 //======================================================================
Jeej 41:6f83174ffed4 297 // kal_base64_strlen
Jeej 41:6f83174ffed4 298 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 299 /// @brief Real length (excluding padding) of Base64-encoded string
Jeej 41:6f83174ffed4 300 /// Note : the input string always has 4x chars, if not return 0
Jeej 41:6f83174ffed4 301 /// @param in char* string to decode
Jeej 41:6f83174ffed4 302 /// @retval u16 string length
Jeej 41:6f83174ffed4 303 //======================================================================
Jeej 41:6f83174ffed4 304 u16 kal_base64_strlen(const char* in)
Jeej 41:6f83174ffed4 305 {
Jeej 41:6f83174ffed4 306 u16 len, j;
Jeej 41:6f83174ffed4 307 register const char *p;
Jeej 41:6f83174ffed4 308
Jeej 41:6f83174ffed4 309 // Useful strlen
Jeej 41:6f83174ffed4 310 len = strlen(in);
Jeej 41:6f83174ffed4 311
Jeej 41:6f83174ffed4 312 // the input string always has 4x chars
Jeej 41:6f83174ffed4 313 if (len % 4)
Jeej 41:6f83174ffed4 314 {
Jeej 41:6f83174ffed4 315 return 0;
Jeej 41:6f83174ffed4 316 }
Jeej 41:6f83174ffed4 317
Jeej 41:6f83174ffed4 318 // remove padding
Jeej 41:6f83174ffed4 319 for (j = 0, p = in + len - 1; len > 0 && *p == '=' ; j++, p--, len--) {;}
Jeej 41:6f83174ffed4 320
Jeej 41:6f83174ffed4 321 // there is 0, 1 or 2 padding chars
Jeej 41:6f83174ffed4 322 if (j > 2)
Jeej 41:6f83174ffed4 323 {
Jeej 41:6f83174ffed4 324 return 0;
Jeej 41:6f83174ffed4 325 }
Jeej 41:6f83174ffed4 326
Jeej 41:6f83174ffed4 327 return len;
Jeej 41:6f83174ffed4 328 }
Jeej 41:6f83174ffed4 329
Jeej 41:6f83174ffed4 330 //======================================================================
Jeej 41:6f83174ffed4 331 // kal_base64 codec tables
Jeej 41:6f83174ffed4 332 //======================================================================
Jeej 41:6f83174ffed4 333 const char k_kal_base64_encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Jeej 41:6f83174ffed4 334 const u8 k_kal_base64_decode[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeej 41:6f83174ffed4 335 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Jeej 41:6f83174ffed4 336 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63,
Jeej 41:6f83174ffed4 337 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
Jeej 41:6f83174ffed4 338 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
Jeej 41:6f83174ffed4 339 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 63,
Jeej 41:6f83174ffed4 340 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
Jeej 41:6f83174ffed4 341 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
Jeej 41:6f83174ffed4 342 };
Jeej 41:6f83174ffed4 343
Jeej 41:6f83174ffed4 344 //======================================================================
Jeej 41:6f83174ffed4 345 // kal_base64_encode
Jeej 41:6f83174ffed4 346 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 347 /// @brief Encode Base64-encoded buffer. The output buffer is supposed to have enough space
Jeej 41:6f83174ffed4 348 /// Beware, the stream encoding is Big Endian.
Jeej 41:6f83174ffed4 349 /// @param out char* string result
Jeej 41:6f83174ffed4 350 /// @param in u8* binary buffer to encode
Jeej 41:6f83174ffed4 351 /// @param len u16 length of the binary buffer in bytes
Jeej 41:6f83174ffed4 352 /// @retval void
Jeej 41:6f83174ffed4 353 //======================================================================
Jeej 41:6f83174ffed4 354 void kal_base64_encode(char *out, u8* in, u16 len)
Jeej 41:6f83174ffed4 355 {
Jeej 41:6f83174ffed4 356 u16 i = 0;
Jeej 41:6f83174ffed4 357 register u8 a,b,c;
Jeej 41:6f83174ffed4 358
Jeej 41:6f83174ffed4 359 if (len > 1)
Jeej 41:6f83174ffed4 360 {
Jeej 41:6f83174ffed4 361 for (; i < len - 2; i += 3)
Jeej 41:6f83174ffed4 362 {
Jeej 41:6f83174ffed4 363 a = *in++;
Jeej 41:6f83174ffed4 364 b = *in++;
Jeej 41:6f83174ffed4 365 c = *in++;
Jeej 41:6f83174ffed4 366 *out++ = k_kal_base64_encode[(((a >> 2) & 0x3F))];
Jeej 41:6f83174ffed4 367 *out++ = k_kal_base64_encode[(((a << 4) & 0x30) | ((b >> 4) & 0x0F))];
Jeej 41:6f83174ffed4 368 *out++ = k_kal_base64_encode[(((b << 2) & 0x3C) | ((c >> 6) & 0x03))];
Jeej 41:6f83174ffed4 369 *out++ = k_kal_base64_encode[(((c >> 0) & 0x3F))];
Jeej 41:6f83174ffed4 370 }
Jeej 41:6f83174ffed4 371 }
Jeej 41:6f83174ffed4 372
Jeej 41:6f83174ffed4 373 if (i < len)
Jeej 41:6f83174ffed4 374 {
Jeej 41:6f83174ffed4 375 a = *in++;
Jeej 41:6f83174ffed4 376 *out++ = k_kal_base64_encode[(((a >> 2) & 0x3F))];
Jeej 41:6f83174ffed4 377
Jeej 41:6f83174ffed4 378 if (i == (len - 1))
Jeej 41:6f83174ffed4 379 {
Jeej 41:6f83174ffed4 380 *out++ = k_kal_base64_encode[(((a << 4) & 0x30))];
Jeej 41:6f83174ffed4 381 *out++ = '=';
Jeej 41:6f83174ffed4 382 }
Jeej 41:6f83174ffed4 383 else
Jeej 41:6f83174ffed4 384 {
Jeej 41:6f83174ffed4 385 b = *in++;
Jeej 41:6f83174ffed4 386 *out++ = k_kal_base64_encode[(((a << 4) & 0x30) | ((b >> 4) & 0x0F))];
Jeej 41:6f83174ffed4 387 *out++ = k_kal_base64_encode[(((b << 2) & 0x3C))];
Jeej 41:6f83174ffed4 388 }
Jeej 41:6f83174ffed4 389
Jeej 41:6f83174ffed4 390 *out++ = '=';
Jeej 41:6f83174ffed4 391 }
Jeej 41:6f83174ffed4 392
Jeej 41:6f83174ffed4 393 *out++ = '\0';
Jeej 41:6f83174ffed4 394 return;
Jeej 41:6f83174ffed4 395 }
Jeej 41:6f83174ffed4 396
Jeej 41:6f83174ffed4 397 //======================================================================
Jeej 41:6f83174ffed4 398 // kal_base64_decode
Jeej 41:6f83174ffed4 399 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 400 /// @brief Decode Base64-encoded buffer. The output buffer is supposed to have enough space
Jeej 41:6f83174ffed4 401 /// @param out u8* binary buffer result
Jeej 41:6f83174ffed4 402 /// @param in char* string to decode
Jeej 41:6f83174ffed4 403 /// @param len u16 string length, excluding padding
Jeej 41:6f83174ffed4 404 /// @retval void
Jeej 41:6f83174ffed4 405 //======================================================================
Jeej 41:6f83174ffed4 406 void kal_base64_decode(u8 *out, const char* in, u16 len)
Jeej 41:6f83174ffed4 407 {
Jeej 41:6f83174ffed4 408 register u8 a,b,c,d;
Jeej 41:6f83174ffed4 409
Jeej 41:6f83174ffed4 410 // Decode
Jeej 41:6f83174ffed4 411 for (; len > 3; len -= 4)
Jeej 41:6f83174ffed4 412 {
Jeej 41:6f83174ffed4 413 a = k_kal_base64_decode[(u8)*in++];
Jeej 41:6f83174ffed4 414 b = k_kal_base64_decode[(u8)*in++];
Jeej 41:6f83174ffed4 415 c = k_kal_base64_decode[(u8)*in++];
Jeej 41:6f83174ffed4 416 d = k_kal_base64_decode[(u8)*in++];
Jeej 41:6f83174ffed4 417
Jeej 41:6f83174ffed4 418 *out++ = (a << 2) | (b >> 4);
Jeej 41:6f83174ffed4 419 *out++ = (b << 4) | (c >> 2);
Jeej 41:6f83174ffed4 420 *out++ = (c << 6) | (d >> 0);
Jeej 41:6f83174ffed4 421 }
Jeej 41:6f83174ffed4 422
Jeej 41:6f83174ffed4 423 // Note len == 1 is not possible
Jeej 41:6f83174ffed4 424 if (len > 1)
Jeej 41:6f83174ffed4 425 {
Jeej 41:6f83174ffed4 426 a = k_kal_base64_decode[(u8)*in++];
Jeej 41:6f83174ffed4 427 b = k_kal_base64_decode[(u8)*in++];
Jeej 41:6f83174ffed4 428 *out++ = (a << 2) | (b >> 4);
Jeej 41:6f83174ffed4 429 }
Jeej 41:6f83174ffed4 430 if (len > 2)
Jeej 41:6f83174ffed4 431 {
Jeej 41:6f83174ffed4 432 c = k_kal_base64_decode[(u8)*in++];
Jeej 41:6f83174ffed4 433 *out++ = (b << 4) | (c >> 2);
Jeej 41:6f83174ffed4 434 }
Jeej 41:6f83174ffed4 435 }
Jeej 41:6f83174ffed4 436
Jeej 41:6f83174ffed4 437 //======================================================================
Jeej 41:6f83174ffed4 438 // kal_tolower
Jeej 41:6f83174ffed4 439 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 440 /// @brief Changes inplace to lower character a non-constant string
Jeej 41:6f83174ffed4 441 /// @param s u8* String to transform
Jeej 41:6f83174ffed4 442 /// @retval void
Jeej 41:6f83174ffed4 443 //======================================================================
Jeej 41:6f83174ffed4 444 void kal_tolower(u8* s)
Jeej 41:6f83174ffed4 445 {
Jeej 41:6f83174ffed4 446 while (*s != '\0')
Jeej 41:6f83174ffed4 447 {
Jeej 41:6f83174ffed4 448 if (*s >= 'A' && *s <= 'Z')
Jeej 41:6f83174ffed4 449 {
Jeej 41:6f83174ffed4 450 *s += (u8)('a' - 'A');
Jeej 41:6f83174ffed4 451 }
Jeej 41:6f83174ffed4 452 s++;
Jeej 41:6f83174ffed4 453 }
Jeej 41:6f83174ffed4 454 }
Jeej 41:6f83174ffed4 455
Jeej 41:6f83174ffed4 456
Jeej 41:6f83174ffed4 457 //======================================================================
Jeej 41:6f83174ffed4 458 // kal_toupper
Jeej 41:6f83174ffed4 459 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 460 /// @brief Changes inplace to upper character a non-constant string
Jeej 41:6f83174ffed4 461 /// @param s u8* String to transform
Jeej 41:6f83174ffed4 462 /// @retval void
Jeej 41:6f83174ffed4 463 //======================================================================
Jeej 41:6f83174ffed4 464 void kal_toupper(u8* s)
Jeej 41:6f83174ffed4 465 {
Jeej 41:6f83174ffed4 466 while (*s != '\0')
Jeej 41:6f83174ffed4 467 {
Jeej 41:6f83174ffed4 468 if (*s >= 'a' && *s <= 'z')
Jeej 41:6f83174ffed4 469 {
Jeej 41:6f83174ffed4 470 *s -= (u8)('a' - 'A');
Jeej 41:6f83174ffed4 471 }
Jeej 41:6f83174ffed4 472 s++;
Jeej 41:6f83174ffed4 473 }
Jeej 41:6f83174ffed4 474 }
Jeej 41:6f83174ffed4 475
Jeej 41:6f83174ffed4 476 //======================================================================
Jeej 41:6f83174ffed4 477 // kal_crc8
Jeej 41:6f83174ffed4 478 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 479 /// @brief Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial.
Jeej 41:6f83174ffed4 480 /// A table-based algorithm would be faster, but for only a few
Jeej 41:6f83174ffed4 481 /// bytes it isn't worth the code size.
Jeej 41:6f83174ffed4 482 /// @param in u8* input buffer
Jeej 41:6f83174ffed4 483 /// @param len u32 input buffer length in bytes
Jeej 41:6f83174ffed4 484 /// @retval void
Jeej 41:6f83174ffed4 485 //======================================================================
Jeej 41:6f83174ffed4 486 /* Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial. A
Jeej 41:6f83174ffed4 487 * table-based algorithm would be faster, but for only a few bytes it isn't
Jeej 41:6f83174ffed4 488 * worth the code size. */
Jeej 41:6f83174ffed4 489 u8 kal_crc8(u8* in, u32 len)
Jeej 41:6f83174ffed4 490 {
Jeej 41:6f83174ffed4 491 const u8 *data = in;
Jeej 41:6f83174ffed4 492 u32 crc = 0;
Jeej 41:6f83174ffed4 493 u8 i;
Jeej 41:6f83174ffed4 494 u32 j;
Jeej 41:6f83174ffed4 495
Jeej 41:6f83174ffed4 496 for (j = len; j; j--, data++)
Jeej 41:6f83174ffed4 497 {
Jeej 41:6f83174ffed4 498 crc ^= (*data << 8);
Jeej 41:6f83174ffed4 499 for(i = 8; i; i--)
Jeej 41:6f83174ffed4 500 {
Jeej 41:6f83174ffed4 501 if (crc & 0x8000)
Jeej 41:6f83174ffed4 502 {
Jeej 41:6f83174ffed4 503 crc ^= (0x1070 << 3);
Jeej 41:6f83174ffed4 504 }
Jeej 41:6f83174ffed4 505 crc <<= 1;
Jeej 41:6f83174ffed4 506 }
Jeej 41:6f83174ffed4 507 }
Jeej 41:6f83174ffed4 508 return (u8)(crc >> 8);
Jeej 41:6f83174ffed4 509 }
Jeej 41:6f83174ffed4 510
Jeej 41:6f83174ffed4 511 //======================================================================
Jeej 41:6f83174ffed4 512 // kal_ctf_encode
Jeej 41:6f83174ffed4 513 //----------------------------------------------------------------------
Jeej 41:6f83174ffed4 514 /// @brief Compress u32 to D7A Compressed Time format (CTF).
Jeej 41:6f83174ffed4 515 /// The ceil flag is used to define rounding, so that
Jeej 41:6f83174ffed4 516 /// kal_ctf_decode(kal_ctf_encode(val, FALSE) <= val (floor)
Jeej 41:6f83174ffed4 517 /// kal_ctf_decode(kal_ctf_encode(val, TRUE) >= val (ceiling)
Jeej 41:6f83174ffed4 518 /// @param val u32 value to encode
Jeej 41:6f83174ffed4 519 /// @param ceil u8 ceil value when TRUE
Jeej 41:6f83174ffed4 520 /// @retval kal_ctf_t compressed value
Jeej 41:6f83174ffed4 521 //======================================================================
Jeej 41:6f83174ffed4 522 kal_ctf_t kal_ctf_encode(u32 val, u8 ceil)
Jeej 41:6f83174ffed4 523 {
Jeej 41:6f83174ffed4 524 u8 exp;
Jeej 41:6f83174ffed4 525 u32 tmp = val;
Jeej 41:6f83174ffed4 526 kal_ctf_t ctf;
Jeej 41:6f83174ffed4 527
Jeej 41:6f83174ffed4 528 // serch best (smallest) exponent
Jeej 41:6f83174ffed4 529 for (exp = 0; tmp > 31; exp++, tmp >>= 2) {};
Jeej 41:6f83174ffed4 530
Jeej 41:6f83174ffed4 531 if (exp < 8)
Jeej 41:6f83174ffed4 532 {
Jeej 41:6f83174ffed4 533 ctf.bf.exp = exp;
Jeej 41:6f83174ffed4 534 ctf.bf.mant = tmp;
Jeej 41:6f83174ffed4 535
Jeej 41:6f83174ffed4 536 // manage floor
Jeej 41:6f83174ffed4 537 if (ceil)
Jeej 41:6f83174ffed4 538 {
Jeej 41:6f83174ffed4 539 if (kal_ctf_decode(ctf) < val)
Jeej 41:6f83174ffed4 540 {
Jeej 41:6f83174ffed4 541 if (ctf.bf.mant < 31)
Jeej 41:6f83174ffed4 542 {
Jeej 41:6f83174ffed4 543 ctf.bf.mant++;
Jeej 41:6f83174ffed4 544 }
Jeej 41:6f83174ffed4 545 else
Jeej 41:6f83174ffed4 546 {
Jeej 41:6f83174ffed4 547 if (ctf.bf.exp < 7)
Jeej 41:6f83174ffed4 548 {
Jeej 41:6f83174ffed4 549 // mant = 31+1 = 4*8
Jeej 41:6f83174ffed4 550 ctf.bf.exp++;
Jeej 41:6f83174ffed4 551 ctf.bf.mant = 8;
Jeej 41:6f83174ffed4 552 }
Jeej 41:6f83174ffed4 553 else
Jeej 41:6f83174ffed4 554 {
Jeej 41:6f83174ffed4 555 // exp = 7+1 -> overflow
Jeej 41:6f83174ffed4 556 ctf.byte = 0xff;
Jeej 41:6f83174ffed4 557 }
Jeej 41:6f83174ffed4 558 }
Jeej 41:6f83174ffed4 559 }
Jeej 41:6f83174ffed4 560 }
Jeej 41:6f83174ffed4 561 }
Jeej 41:6f83174ffed4 562 else
Jeej 41:6f83174ffed4 563 {
Jeej 41:6f83174ffed4 564 ctf.byte = 0xff;
Jeej 41:6f83174ffed4 565 }
Jeej 41:6f83174ffed4 566
Jeej 41:6f83174ffed4 567 return ctf;
Jeej 41:6f83174ffed4 568 }
Jeej 41:6f83174ffed4 569
Jeej 41:6f83174ffed4 570 // =======================================================================
Jeej 41:6f83174ffed4 571 // kal_ctf_decode
Jeej 41:6f83174ffed4 572 // -----------------------------------------------------------------------
Jeej 41:6f83174ffed4 573 /// @brief Decompress from Compressed Time Format to u32
Jeej 41:6f83174ffed4 574 /// @param ctf kal_ctf_t compressed value in CTF
Jeej 41:6f83174ffed4 575 /// @retval u32 decode result
Jeej 41:6f83174ffed4 576 // =======================================================================
Jeej 41:6f83174ffed4 577 u32 kal_ctf_decode(kal_ctf_t ctf)
Jeej 41:6f83174ffed4 578 {
Jeej 41:6f83174ffed4 579 return ((1 << (2*ctf.bf.exp)) * ctf.bf.mant);
Jeej 41:6f83174ffed4 580 }
Jeej 41:6f83174ffed4 581