pick up wakaama files from https://github.com/eclipse/wakaama

Committer:
terencez
Date:
Wed Apr 19 11:27:34 2017 +0000
Revision:
0:c2dff8cbb91a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terencez 0:c2dff8cbb91a 1 /*******************************************************************************
terencez 0:c2dff8cbb91a 2 *
terencez 0:c2dff8cbb91a 3 * Copyright (c) 2013, 2014 Intel Corporation and others.
terencez 0:c2dff8cbb91a 4 * All rights reserved. This program and the accompanying materials
terencez 0:c2dff8cbb91a 5 * are made available under the terms of the Eclipse Public License v1.0
terencez 0:c2dff8cbb91a 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
terencez 0:c2dff8cbb91a 7 *
terencez 0:c2dff8cbb91a 8 * The Eclipse Public License is available at
terencez 0:c2dff8cbb91a 9 * http://www.eclipse.org/legal/epl-v10.html
terencez 0:c2dff8cbb91a 10 * The Eclipse Distribution License is available at
terencez 0:c2dff8cbb91a 11 * http://www.eclipse.org/org/documents/edl-v10.php.
terencez 0:c2dff8cbb91a 12 *
terencez 0:c2dff8cbb91a 13 * Contributors:
terencez 0:c2dff8cbb91a 14 * David Navarro, Intel Corporation - initial API and implementation
terencez 0:c2dff8cbb91a 15 * Toby Jaffey - Please refer to git log
terencez 0:c2dff8cbb91a 16 *
terencez 0:c2dff8cbb91a 17 *******************************************************************************/
terencez 0:c2dff8cbb91a 18
terencez 0:c2dff8cbb91a 19 /*
terencez 0:c2dff8cbb91a 20 Copyright (c) 2013, 2014 Intel Corporation
terencez 0:c2dff8cbb91a 21
terencez 0:c2dff8cbb91a 22 Redistribution and use in source and binary forms, with or without modification,
terencez 0:c2dff8cbb91a 23 are permitted provided that the following conditions are met:
terencez 0:c2dff8cbb91a 24
terencez 0:c2dff8cbb91a 25 * Redistributions of source code must retain the above copyright notice,
terencez 0:c2dff8cbb91a 26 this list of conditions and the following disclaimer.
terencez 0:c2dff8cbb91a 27 * Redistributions in binary form must reproduce the above copyright notice,
terencez 0:c2dff8cbb91a 28 this list of conditions and the following disclaimer in the documentation
terencez 0:c2dff8cbb91a 29 and/or other materials provided with the distribution.
terencez 0:c2dff8cbb91a 30 * Neither the name of Intel Corporation nor the names of its contributors
terencez 0:c2dff8cbb91a 31 may be used to endorse or promote products derived from this software
terencez 0:c2dff8cbb91a 32 without specific prior written permission.
terencez 0:c2dff8cbb91a 33
terencez 0:c2dff8cbb91a 34 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
terencez 0:c2dff8cbb91a 35 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
terencez 0:c2dff8cbb91a 36 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
terencez 0:c2dff8cbb91a 37 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
terencez 0:c2dff8cbb91a 38 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
terencez 0:c2dff8cbb91a 39 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
terencez 0:c2dff8cbb91a 40 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
terencez 0:c2dff8cbb91a 41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
terencez 0:c2dff8cbb91a 42 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
terencez 0:c2dff8cbb91a 43 THE POSSIBILITY OF SUCH DAMAGE.
terencez 0:c2dff8cbb91a 44
terencez 0:c2dff8cbb91a 45 David Navarro <david.navarro@intel.com>
terencez 0:c2dff8cbb91a 46
terencez 0:c2dff8cbb91a 47 */
terencez 0:c2dff8cbb91a 48
terencez 0:c2dff8cbb91a 49 #include "internals.h"
terencez 0:c2dff8cbb91a 50 #include <stdlib.h>
terencez 0:c2dff8cbb91a 51 #include <string.h>
terencez 0:c2dff8cbb91a 52 #include <stdio.h>
terencez 0:c2dff8cbb91a 53 #include <float.h>
terencez 0:c2dff8cbb91a 54
terencez 0:c2dff8cbb91a 55
terencez 0:c2dff8cbb91a 56 int utils_plainTextToInt64(uint8_t * buffer,
terencez 0:c2dff8cbb91a 57 int length,
terencez 0:c2dff8cbb91a 58 int64_t * dataP)
terencez 0:c2dff8cbb91a 59 {
terencez 0:c2dff8cbb91a 60 uint64_t result = 0;
terencez 0:c2dff8cbb91a 61 int sign = 1;
terencez 0:c2dff8cbb91a 62 int i = 0;
terencez 0:c2dff8cbb91a 63
terencez 0:c2dff8cbb91a 64 if (0 == length) return 0;
terencez 0:c2dff8cbb91a 65
terencez 0:c2dff8cbb91a 66 if (buffer[0] == '-')
terencez 0:c2dff8cbb91a 67 {
terencez 0:c2dff8cbb91a 68 sign = -1;
terencez 0:c2dff8cbb91a 69 i = 1;
terencez 0:c2dff8cbb91a 70 }
terencez 0:c2dff8cbb91a 71
terencez 0:c2dff8cbb91a 72 while (i < length)
terencez 0:c2dff8cbb91a 73 {
terencez 0:c2dff8cbb91a 74 if ('0' <= buffer[i] && buffer[i] <= '9')
terencez 0:c2dff8cbb91a 75 {
terencez 0:c2dff8cbb91a 76 if (result > (UINT64_MAX / 10)) return 0;
terencez 0:c2dff8cbb91a 77 result *= 10;
terencez 0:c2dff8cbb91a 78 result += buffer[i] - '0';
terencez 0:c2dff8cbb91a 79 }
terencez 0:c2dff8cbb91a 80 else
terencez 0:c2dff8cbb91a 81 {
terencez 0:c2dff8cbb91a 82 return 0;
terencez 0:c2dff8cbb91a 83 }
terencez 0:c2dff8cbb91a 84 i++;
terencez 0:c2dff8cbb91a 85 }
terencez 0:c2dff8cbb91a 86
terencez 0:c2dff8cbb91a 87 if (result > INT64_MAX) return 0;
terencez 0:c2dff8cbb91a 88
terencez 0:c2dff8cbb91a 89 if (sign == -1)
terencez 0:c2dff8cbb91a 90 {
terencez 0:c2dff8cbb91a 91 *dataP = 0 - result;
terencez 0:c2dff8cbb91a 92 }
terencez 0:c2dff8cbb91a 93 else
terencez 0:c2dff8cbb91a 94 {
terencez 0:c2dff8cbb91a 95 *dataP = result;
terencez 0:c2dff8cbb91a 96 }
terencez 0:c2dff8cbb91a 97
terencez 0:c2dff8cbb91a 98 return 1;
terencez 0:c2dff8cbb91a 99 }
terencez 0:c2dff8cbb91a 100
terencez 0:c2dff8cbb91a 101 int utils_plainTextToFloat64(uint8_t * buffer,
terencez 0:c2dff8cbb91a 102 int length,
terencez 0:c2dff8cbb91a 103 double * dataP)
terencez 0:c2dff8cbb91a 104 {
terencez 0:c2dff8cbb91a 105 double result;
terencez 0:c2dff8cbb91a 106 int sign;
terencez 0:c2dff8cbb91a 107 int i;
terencez 0:c2dff8cbb91a 108
terencez 0:c2dff8cbb91a 109 if (0 == length) return 0;
terencez 0:c2dff8cbb91a 110
terencez 0:c2dff8cbb91a 111 if (buffer[0] == '-')
terencez 0:c2dff8cbb91a 112 {
terencez 0:c2dff8cbb91a 113 sign = -1;
terencez 0:c2dff8cbb91a 114 i = 1;
terencez 0:c2dff8cbb91a 115 }
terencez 0:c2dff8cbb91a 116 else
terencez 0:c2dff8cbb91a 117 {
terencez 0:c2dff8cbb91a 118 sign = 1;
terencez 0:c2dff8cbb91a 119 i = 0;
terencez 0:c2dff8cbb91a 120 }
terencez 0:c2dff8cbb91a 121
terencez 0:c2dff8cbb91a 122 result = 0;
terencez 0:c2dff8cbb91a 123 while (i < length && buffer[i] != '.')
terencez 0:c2dff8cbb91a 124 {
terencez 0:c2dff8cbb91a 125 if ('0' <= buffer[i] && buffer[i] <= '9')
terencez 0:c2dff8cbb91a 126 {
terencez 0:c2dff8cbb91a 127 if (result > (DBL_MAX / 10)) return 0;
terencez 0:c2dff8cbb91a 128 result *= 10;
terencez 0:c2dff8cbb91a 129 result += (buffer[i] - '0');
terencez 0:c2dff8cbb91a 130 }
terencez 0:c2dff8cbb91a 131 else
terencez 0:c2dff8cbb91a 132 {
terencez 0:c2dff8cbb91a 133 return 0;
terencez 0:c2dff8cbb91a 134 }
terencez 0:c2dff8cbb91a 135 i++;
terencez 0:c2dff8cbb91a 136 }
terencez 0:c2dff8cbb91a 137 if (buffer[i] == '.')
terencez 0:c2dff8cbb91a 138 {
terencez 0:c2dff8cbb91a 139 double dec;
terencez 0:c2dff8cbb91a 140
terencez 0:c2dff8cbb91a 141 i++;
terencez 0:c2dff8cbb91a 142 if (i == length) return 0;
terencez 0:c2dff8cbb91a 143
terencez 0:c2dff8cbb91a 144 dec = 0.1;
terencez 0:c2dff8cbb91a 145 while (i < length)
terencez 0:c2dff8cbb91a 146 {
terencez 0:c2dff8cbb91a 147 if ('0' <= buffer[i] && buffer[i] <= '9')
terencez 0:c2dff8cbb91a 148 {
terencez 0:c2dff8cbb91a 149 if (result > (DBL_MAX - 1)) return 0;
terencez 0:c2dff8cbb91a 150 result += (buffer[i] - '0') * dec;
terencez 0:c2dff8cbb91a 151 dec /= 10;
terencez 0:c2dff8cbb91a 152 }
terencez 0:c2dff8cbb91a 153 else
terencez 0:c2dff8cbb91a 154 {
terencez 0:c2dff8cbb91a 155 return 0;
terencez 0:c2dff8cbb91a 156 }
terencez 0:c2dff8cbb91a 157 i++;
terencez 0:c2dff8cbb91a 158 }
terencez 0:c2dff8cbb91a 159 }
terencez 0:c2dff8cbb91a 160
terencez 0:c2dff8cbb91a 161 *dataP = result * sign;
terencez 0:c2dff8cbb91a 162 return 1;
terencez 0:c2dff8cbb91a 163 }
terencez 0:c2dff8cbb91a 164
terencez 0:c2dff8cbb91a 165 size_t utils_intToText(int64_t data,
terencez 0:c2dff8cbb91a 166 uint8_t * string,
terencez 0:c2dff8cbb91a 167 size_t length)
terencez 0:c2dff8cbb91a 168 {
terencez 0:c2dff8cbb91a 169 int index;
terencez 0:c2dff8cbb91a 170 bool minus;
terencez 0:c2dff8cbb91a 171 size_t result;
terencez 0:c2dff8cbb91a 172
terencez 0:c2dff8cbb91a 173 if (data < 0)
terencez 0:c2dff8cbb91a 174 {
terencez 0:c2dff8cbb91a 175 minus = true;
terencez 0:c2dff8cbb91a 176 data = 0 - data;
terencez 0:c2dff8cbb91a 177 }
terencez 0:c2dff8cbb91a 178 else
terencez 0:c2dff8cbb91a 179 {
terencez 0:c2dff8cbb91a 180 minus = false;
terencez 0:c2dff8cbb91a 181 }
terencez 0:c2dff8cbb91a 182
terencez 0:c2dff8cbb91a 183 index = length - 1;
terencez 0:c2dff8cbb91a 184 do
terencez 0:c2dff8cbb91a 185 {
terencez 0:c2dff8cbb91a 186 string[index] = '0' + data%10;
terencez 0:c2dff8cbb91a 187 data /= 10;
terencez 0:c2dff8cbb91a 188 index --;
terencez 0:c2dff8cbb91a 189 } while (index >= 0 && data > 0);
terencez 0:c2dff8cbb91a 190
terencez 0:c2dff8cbb91a 191 if (data > 0) return 0;
terencez 0:c2dff8cbb91a 192
terencez 0:c2dff8cbb91a 193 if (minus == true)
terencez 0:c2dff8cbb91a 194 {
terencez 0:c2dff8cbb91a 195 if (index == 0) return 0;
terencez 0:c2dff8cbb91a 196 string[index] = '-';
terencez 0:c2dff8cbb91a 197 }
terencez 0:c2dff8cbb91a 198 else
terencez 0:c2dff8cbb91a 199 {
terencez 0:c2dff8cbb91a 200 index++;
terencez 0:c2dff8cbb91a 201 }
terencez 0:c2dff8cbb91a 202
terencez 0:c2dff8cbb91a 203 result = length - index;
terencez 0:c2dff8cbb91a 204
terencez 0:c2dff8cbb91a 205 if (result < length)
terencez 0:c2dff8cbb91a 206 {
terencez 0:c2dff8cbb91a 207 memmove(string, string + index, result);
terencez 0:c2dff8cbb91a 208 }
terencez 0:c2dff8cbb91a 209
terencez 0:c2dff8cbb91a 210 return result;
terencez 0:c2dff8cbb91a 211 }
terencez 0:c2dff8cbb91a 212
terencez 0:c2dff8cbb91a 213 size_t utils_floatToText(double data,
terencez 0:c2dff8cbb91a 214 uint8_t * string,
terencez 0:c2dff8cbb91a 215 size_t length)
terencez 0:c2dff8cbb91a 216 {
terencez 0:c2dff8cbb91a 217 size_t intLength;
terencez 0:c2dff8cbb91a 218 size_t decLength;
terencez 0:c2dff8cbb91a 219 int64_t intPart;
terencez 0:c2dff8cbb91a 220 double decPart;
terencez 0:c2dff8cbb91a 221
terencez 0:c2dff8cbb91a 222 if (data <= (double)INT64_MIN || data >= (double)INT64_MAX) return 0;
terencez 0:c2dff8cbb91a 223
terencez 0:c2dff8cbb91a 224 intPart = (int64_t)data;
terencez 0:c2dff8cbb91a 225 decPart = data - intPart;
terencez 0:c2dff8cbb91a 226 if (decPart < 0)
terencez 0:c2dff8cbb91a 227 {
terencez 0:c2dff8cbb91a 228 decPart = 1 - decPart;
terencez 0:c2dff8cbb91a 229 }
terencez 0:c2dff8cbb91a 230 else
terencez 0:c2dff8cbb91a 231 {
terencez 0:c2dff8cbb91a 232 decPart = 1 + decPart;
terencez 0:c2dff8cbb91a 233 }
terencez 0:c2dff8cbb91a 234
terencez 0:c2dff8cbb91a 235 if (decPart <= 1 + FLT_EPSILON)
terencez 0:c2dff8cbb91a 236 {
terencez 0:c2dff8cbb91a 237 decPart = 0;
terencez 0:c2dff8cbb91a 238 }
terencez 0:c2dff8cbb91a 239
terencez 0:c2dff8cbb91a 240 if (intPart == 0 && data < 0)
terencez 0:c2dff8cbb91a 241 {
terencez 0:c2dff8cbb91a 242 // deal with numbers between -1 and 0
terencez 0:c2dff8cbb91a 243 if (length < 4) return 0; // "-0.n"
terencez 0:c2dff8cbb91a 244 string[0] = '-';
terencez 0:c2dff8cbb91a 245 string[1] = '0';
terencez 0:c2dff8cbb91a 246 intLength = 2;
terencez 0:c2dff8cbb91a 247 }
terencez 0:c2dff8cbb91a 248 else
terencez 0:c2dff8cbb91a 249 {
terencez 0:c2dff8cbb91a 250 intLength = utils_intToText(intPart, string, length);
terencez 0:c2dff8cbb91a 251 if (intLength == 0) return 0;
terencez 0:c2dff8cbb91a 252 }
terencez 0:c2dff8cbb91a 253 decLength = 0;
terencez 0:c2dff8cbb91a 254 if (decPart >= FLT_EPSILON)
terencez 0:c2dff8cbb91a 255 {
terencez 0:c2dff8cbb91a 256 double noiseFloor;
terencez 0:c2dff8cbb91a 257
terencez 0:c2dff8cbb91a 258 if (intLength >= length - 1) return 0;
terencez 0:c2dff8cbb91a 259
terencez 0:c2dff8cbb91a 260 noiseFloor = FLT_EPSILON;
terencez 0:c2dff8cbb91a 261 do
terencez 0:c2dff8cbb91a 262 {
terencez 0:c2dff8cbb91a 263 decPart *= 10;
terencez 0:c2dff8cbb91a 264 noiseFloor *= 10;
terencez 0:c2dff8cbb91a 265 } while (decPart - (int64_t)decPart > noiseFloor);
terencez 0:c2dff8cbb91a 266
terencez 0:c2dff8cbb91a 267 decLength = utils_intToText(decPart, string + intLength, length - intLength);
terencez 0:c2dff8cbb91a 268 if (decLength <= 1) return 0;
terencez 0:c2dff8cbb91a 269
terencez 0:c2dff8cbb91a 270 // replace the leading 1 with a dot
terencez 0:c2dff8cbb91a 271 string[intLength] = '.';
terencez 0:c2dff8cbb91a 272 }
terencez 0:c2dff8cbb91a 273
terencez 0:c2dff8cbb91a 274 return intLength + decLength;
terencez 0:c2dff8cbb91a 275 }
terencez 0:c2dff8cbb91a 276
terencez 0:c2dff8cbb91a 277 size_t utils_int64ToPlainText(int64_t data,
terencez 0:c2dff8cbb91a 278 uint8_t ** bufferP)
terencez 0:c2dff8cbb91a 279 {
terencez 0:c2dff8cbb91a 280 #define _PRV_STR_LENGTH 32
terencez 0:c2dff8cbb91a 281 uint8_t string[_PRV_STR_LENGTH];
terencez 0:c2dff8cbb91a 282 size_t length;
terencez 0:c2dff8cbb91a 283
terencez 0:c2dff8cbb91a 284 length = utils_intToText(data, string, _PRV_STR_LENGTH);
terencez 0:c2dff8cbb91a 285 if (length == 0) return 0;
terencez 0:c2dff8cbb91a 286
terencez 0:c2dff8cbb91a 287 *bufferP = (uint8_t *)lwm2m_malloc(length);
terencez 0:c2dff8cbb91a 288 if (NULL == *bufferP) return 0;
terencez 0:c2dff8cbb91a 289
terencez 0:c2dff8cbb91a 290 memcpy(*bufferP, string, length);
terencez 0:c2dff8cbb91a 291
terencez 0:c2dff8cbb91a 292 return length;
terencez 0:c2dff8cbb91a 293 }
terencez 0:c2dff8cbb91a 294
terencez 0:c2dff8cbb91a 295
terencez 0:c2dff8cbb91a 296 size_t utils_float64ToPlainText(double data,
terencez 0:c2dff8cbb91a 297 uint8_t ** bufferP)
terencez 0:c2dff8cbb91a 298 {
terencez 0:c2dff8cbb91a 299 uint8_t string[_PRV_STR_LENGTH * 2];
terencez 0:c2dff8cbb91a 300 size_t length;
terencez 0:c2dff8cbb91a 301
terencez 0:c2dff8cbb91a 302 length = utils_floatToText(data, string, _PRV_STR_LENGTH * 2);
terencez 0:c2dff8cbb91a 303 if (length == 0) return 0;
terencez 0:c2dff8cbb91a 304
terencez 0:c2dff8cbb91a 305 *bufferP = (uint8_t *)lwm2m_malloc(length);
terencez 0:c2dff8cbb91a 306 if (NULL == *bufferP) return 0;
terencez 0:c2dff8cbb91a 307
terencez 0:c2dff8cbb91a 308 memcpy(*bufferP, string, length);
terencez 0:c2dff8cbb91a 309
terencez 0:c2dff8cbb91a 310 return length;
terencez 0:c2dff8cbb91a 311 }
terencez 0:c2dff8cbb91a 312
terencez 0:c2dff8cbb91a 313
terencez 0:c2dff8cbb91a 314 size_t utils_boolToPlainText(bool data,
terencez 0:c2dff8cbb91a 315 uint8_t ** bufferP)
terencez 0:c2dff8cbb91a 316 {
terencez 0:c2dff8cbb91a 317 return utils_int64ToPlainText((int64_t)(data?1:0), bufferP);
terencez 0:c2dff8cbb91a 318 }
terencez 0:c2dff8cbb91a 319
terencez 0:c2dff8cbb91a 320 lwm2m_binding_t utils_stringToBinding(uint8_t * buffer,
terencez 0:c2dff8cbb91a 321 size_t length)
terencez 0:c2dff8cbb91a 322 {
terencez 0:c2dff8cbb91a 323 if (length == 0) return BINDING_UNKNOWN;
terencez 0:c2dff8cbb91a 324
terencez 0:c2dff8cbb91a 325 switch (buffer[0])
terencez 0:c2dff8cbb91a 326 {
terencez 0:c2dff8cbb91a 327 case 'U':
terencez 0:c2dff8cbb91a 328 switch (length)
terencez 0:c2dff8cbb91a 329 {
terencez 0:c2dff8cbb91a 330 case 1:
terencez 0:c2dff8cbb91a 331 return BINDING_U;
terencez 0:c2dff8cbb91a 332 case 2:
terencez 0:c2dff8cbb91a 333 switch (buffer[1])
terencez 0:c2dff8cbb91a 334 {
terencez 0:c2dff8cbb91a 335 case 'Q':
terencez 0:c2dff8cbb91a 336 return BINDING_UQ;
terencez 0:c2dff8cbb91a 337 case 'S':
terencez 0:c2dff8cbb91a 338 return BINDING_US;
terencez 0:c2dff8cbb91a 339 default:
terencez 0:c2dff8cbb91a 340 break;
terencez 0:c2dff8cbb91a 341 }
terencez 0:c2dff8cbb91a 342 break;
terencez 0:c2dff8cbb91a 343 case 3:
terencez 0:c2dff8cbb91a 344 if (buffer[1] == 'Q' && buffer[2] == 'S')
terencez 0:c2dff8cbb91a 345 {
terencez 0:c2dff8cbb91a 346 return BINDING_UQS;
terencez 0:c2dff8cbb91a 347 }
terencez 0:c2dff8cbb91a 348 break;
terencez 0:c2dff8cbb91a 349 default:
terencez 0:c2dff8cbb91a 350 break;
terencez 0:c2dff8cbb91a 351 }
terencez 0:c2dff8cbb91a 352 break;
terencez 0:c2dff8cbb91a 353
terencez 0:c2dff8cbb91a 354 case 'S':
terencez 0:c2dff8cbb91a 355 switch (length)
terencez 0:c2dff8cbb91a 356 {
terencez 0:c2dff8cbb91a 357 case 1:
terencez 0:c2dff8cbb91a 358 return BINDING_S;
terencez 0:c2dff8cbb91a 359 case 2:
terencez 0:c2dff8cbb91a 360 if (buffer[1] == 'Q')
terencez 0:c2dff8cbb91a 361 {
terencez 0:c2dff8cbb91a 362 return BINDING_SQ;
terencez 0:c2dff8cbb91a 363 }
terencez 0:c2dff8cbb91a 364 break;
terencez 0:c2dff8cbb91a 365 default:
terencez 0:c2dff8cbb91a 366 break;
terencez 0:c2dff8cbb91a 367 }
terencez 0:c2dff8cbb91a 368 break;
terencez 0:c2dff8cbb91a 369
terencez 0:c2dff8cbb91a 370 default:
terencez 0:c2dff8cbb91a 371 break;
terencez 0:c2dff8cbb91a 372 }
terencez 0:c2dff8cbb91a 373
terencez 0:c2dff8cbb91a 374 return BINDING_UNKNOWN;
terencez 0:c2dff8cbb91a 375 }
terencez 0:c2dff8cbb91a 376
terencez 0:c2dff8cbb91a 377 lwm2m_media_type_t utils_convertMediaType(coap_content_type_t type)
terencez 0:c2dff8cbb91a 378 {
terencez 0:c2dff8cbb91a 379 // Here we just check the content type is a valid value for LWM2M
terencez 0:c2dff8cbb91a 380 switch((uint16_t)type)
terencez 0:c2dff8cbb91a 381 {
terencez 0:c2dff8cbb91a 382 case TEXT_PLAIN:
terencez 0:c2dff8cbb91a 383 return LWM2M_CONTENT_TEXT;
terencez 0:c2dff8cbb91a 384 case APPLICATION_OCTET_STREAM:
terencez 0:c2dff8cbb91a 385 return LWM2M_CONTENT_OPAQUE;
terencez 0:c2dff8cbb91a 386 case LWM2M_CONTENT_TLV_OLD:
terencez 0:c2dff8cbb91a 387 return LWM2M_CONTENT_TLV_OLD;
terencez 0:c2dff8cbb91a 388 case LWM2M_CONTENT_TLV:
terencez 0:c2dff8cbb91a 389 return LWM2M_CONTENT_TLV;
terencez 0:c2dff8cbb91a 390 case LWM2M_CONTENT_JSON_OLD:
terencez 0:c2dff8cbb91a 391 return LWM2M_CONTENT_JSON_OLD;
terencez 0:c2dff8cbb91a 392 case LWM2M_CONTENT_JSON:
terencez 0:c2dff8cbb91a 393 return LWM2M_CONTENT_JSON;
terencez 0:c2dff8cbb91a 394 case APPLICATION_LINK_FORMAT:
terencez 0:c2dff8cbb91a 395 return LWM2M_CONTENT_LINK;
terencez 0:c2dff8cbb91a 396
terencez 0:c2dff8cbb91a 397 default:
terencez 0:c2dff8cbb91a 398 return LWM2M_CONTENT_TEXT;
terencez 0:c2dff8cbb91a 399 }
terencez 0:c2dff8cbb91a 400 }
terencez 0:c2dff8cbb91a 401
terencez 0:c2dff8cbb91a 402 #ifdef LWM2M_CLIENT_MODE
terencez 0:c2dff8cbb91a 403 lwm2m_server_t * utils_findServer(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 404 void * fromSessionH)
terencez 0:c2dff8cbb91a 405 {
terencez 0:c2dff8cbb91a 406 lwm2m_server_t * targetP;
terencez 0:c2dff8cbb91a 407
terencez 0:c2dff8cbb91a 408 targetP = contextP->serverList;
terencez 0:c2dff8cbb91a 409 while (targetP != NULL
terencez 0:c2dff8cbb91a 410 && false == lwm2m_session_is_equal(targetP->sessionH, fromSessionH, contextP->userData))
terencez 0:c2dff8cbb91a 411 {
terencez 0:c2dff8cbb91a 412 targetP = targetP->next;
terencez 0:c2dff8cbb91a 413 }
terencez 0:c2dff8cbb91a 414
terencez 0:c2dff8cbb91a 415 return targetP;
terencez 0:c2dff8cbb91a 416 }
terencez 0:c2dff8cbb91a 417 #endif
terencez 0:c2dff8cbb91a 418
terencez 0:c2dff8cbb91a 419 lwm2m_server_t * utils_findBootstrapServer(lwm2m_context_t * contextP,
terencez 0:c2dff8cbb91a 420 void * fromSessionH)
terencez 0:c2dff8cbb91a 421 {
terencez 0:c2dff8cbb91a 422 #ifdef LWM2M_CLIENT_MODE
terencez 0:c2dff8cbb91a 423
terencez 0:c2dff8cbb91a 424 lwm2m_server_t * targetP;
terencez 0:c2dff8cbb91a 425
terencez 0:c2dff8cbb91a 426 targetP = contextP->bootstrapServerList;
terencez 0:c2dff8cbb91a 427 while (targetP != NULL
terencez 0:c2dff8cbb91a 428 && false == lwm2m_session_is_equal(targetP->sessionH, fromSessionH, contextP->userData))
terencez 0:c2dff8cbb91a 429 {
terencez 0:c2dff8cbb91a 430 targetP = targetP->next;
terencez 0:c2dff8cbb91a 431 }
terencez 0:c2dff8cbb91a 432
terencez 0:c2dff8cbb91a 433 return targetP;
terencez 0:c2dff8cbb91a 434
terencez 0:c2dff8cbb91a 435 #else
terencez 0:c2dff8cbb91a 436
terencez 0:c2dff8cbb91a 437 return NULL;
terencez 0:c2dff8cbb91a 438
terencez 0:c2dff8cbb91a 439 #endif
terencez 0:c2dff8cbb91a 440 }
terencez 0:c2dff8cbb91a 441
terencez 0:c2dff8cbb91a 442 int utils_isAltPathValid(const char * altPath)
terencez 0:c2dff8cbb91a 443 {
terencez 0:c2dff8cbb91a 444 int i;
terencez 0:c2dff8cbb91a 445
terencez 0:c2dff8cbb91a 446 if (altPath == NULL) return 0;
terencez 0:c2dff8cbb91a 447
terencez 0:c2dff8cbb91a 448 if (altPath[0] != '/') return 0;
terencez 0:c2dff8cbb91a 449
terencez 0:c2dff8cbb91a 450 for (i = 1 ; altPath[i] != 0 ; i++)
terencez 0:c2dff8cbb91a 451 {
terencez 0:c2dff8cbb91a 452 // TODO: Support multi-segment alternative path
terencez 0:c2dff8cbb91a 453 if (altPath[i] == '/') return 0;
terencez 0:c2dff8cbb91a 454 // TODO: Check needs for sub-delims, ':' and '@'
terencez 0:c2dff8cbb91a 455 if ((altPath[i] < 'A' || altPath[i] > 'Z') // ALPHA
terencez 0:c2dff8cbb91a 456 && (altPath[i] < 'a' || altPath[i] > 'z')
terencez 0:c2dff8cbb91a 457 && (altPath[i] < '0' || altPath[i] > '9') // DIGIT
terencez 0:c2dff8cbb91a 458 && (altPath[i] != '-') // Other unreserved
terencez 0:c2dff8cbb91a 459 && (altPath[i] != '.')
terencez 0:c2dff8cbb91a 460 && (altPath[i] != '_')
terencez 0:c2dff8cbb91a 461 && (altPath[i] != '~')
terencez 0:c2dff8cbb91a 462 && (altPath[i] != '%')) // pct_encoded
terencez 0:c2dff8cbb91a 463 {
terencez 0:c2dff8cbb91a 464 return 0;
terencez 0:c2dff8cbb91a 465 }
terencez 0:c2dff8cbb91a 466
terencez 0:c2dff8cbb91a 467 }
terencez 0:c2dff8cbb91a 468 return 1;
terencez 0:c2dff8cbb91a 469 }
terencez 0:c2dff8cbb91a 470
terencez 0:c2dff8cbb91a 471 // copy a string in a buffer.
terencez 0:c2dff8cbb91a 472 // return the number of copied bytes or -1 if the buffer is not large enough
terencez 0:c2dff8cbb91a 473 int utils_stringCopy(char * buffer,
terencez 0:c2dff8cbb91a 474 size_t length,
terencez 0:c2dff8cbb91a 475 const char * str)
terencez 0:c2dff8cbb91a 476 {
terencez 0:c2dff8cbb91a 477 size_t i;
terencez 0:c2dff8cbb91a 478
terencez 0:c2dff8cbb91a 479 for (i = 0 ; i < length && str[i] != 0 ; i++)
terencez 0:c2dff8cbb91a 480 {
terencez 0:c2dff8cbb91a 481 buffer[i] = str[i];
terencez 0:c2dff8cbb91a 482 }
terencez 0:c2dff8cbb91a 483
terencez 0:c2dff8cbb91a 484 if (i == length) return -1;
terencez 0:c2dff8cbb91a 485
terencez 0:c2dff8cbb91a 486 buffer[i] = 0;
terencez 0:c2dff8cbb91a 487
terencez 0:c2dff8cbb91a 488 return (int)i;
terencez 0:c2dff8cbb91a 489 }
terencez 0:c2dff8cbb91a 490
terencez 0:c2dff8cbb91a 491 int utils_intCopy(char * buffer,
terencez 0:c2dff8cbb91a 492 size_t length,
terencez 0:c2dff8cbb91a 493 int32_t value)
terencez 0:c2dff8cbb91a 494 {
terencez 0:c2dff8cbb91a 495 #define _PRV_INT32_MAX_STR_LEN 11
terencez 0:c2dff8cbb91a 496 uint8_t str[_PRV_INT32_MAX_STR_LEN];
terencez 0:c2dff8cbb91a 497 size_t len;
terencez 0:c2dff8cbb91a 498
terencez 0:c2dff8cbb91a 499 len = utils_intToText(value, str, _PRV_INT32_MAX_STR_LEN);
terencez 0:c2dff8cbb91a 500 if (len == 0) return -1;
terencez 0:c2dff8cbb91a 501 if (len > length + 1) return -1;
terencez 0:c2dff8cbb91a 502
terencez 0:c2dff8cbb91a 503 memcpy(buffer, str, len);
terencez 0:c2dff8cbb91a 504 buffer[len] = 0;
terencez 0:c2dff8cbb91a 505
terencez 0:c2dff8cbb91a 506 return len;
terencez 0:c2dff8cbb91a 507 }
terencez 0:c2dff8cbb91a 508
terencez 0:c2dff8cbb91a 509 void utils_copyValue(void * dst,
terencez 0:c2dff8cbb91a 510 const void * src,
terencez 0:c2dff8cbb91a 511 size_t len)
terencez 0:c2dff8cbb91a 512 {
terencez 0:c2dff8cbb91a 513 #ifdef LWM2M_BIG_ENDIAN
terencez 0:c2dff8cbb91a 514 memcpy(dst, src, len);
terencez 0:c2dff8cbb91a 515 #else
terencez 0:c2dff8cbb91a 516 #ifdef LWM2M_LITTLE_ENDIAN
terencez 0:c2dff8cbb91a 517 size_t i;
terencez 0:c2dff8cbb91a 518
terencez 0:c2dff8cbb91a 519 for (i = 0; i < len; i++)
terencez 0:c2dff8cbb91a 520 {
terencez 0:c2dff8cbb91a 521 ((uint8_t *)dst)[i] = ((uint8_t *)src)[len - 1 - i];
terencez 0:c2dff8cbb91a 522 }
terencez 0:c2dff8cbb91a 523 #endif
terencez 0:c2dff8cbb91a 524 #endif
terencez 0:c2dff8cbb91a 525 }
terencez 0:c2dff8cbb91a 526
terencez 0:c2dff8cbb91a 527 int utils_opaqueToInt(const uint8_t * buffer,
terencez 0:c2dff8cbb91a 528 size_t buffer_len,
terencez 0:c2dff8cbb91a 529 int64_t * dataP)
terencez 0:c2dff8cbb91a 530 {
terencez 0:c2dff8cbb91a 531 *dataP = 0;
terencez 0:c2dff8cbb91a 532
terencez 0:c2dff8cbb91a 533 switch (buffer_len)
terencez 0:c2dff8cbb91a 534 {
terencez 0:c2dff8cbb91a 535 case 1:
terencez 0:c2dff8cbb91a 536 {
terencez 0:c2dff8cbb91a 537 *dataP = (int8_t)buffer[0];
terencez 0:c2dff8cbb91a 538
terencez 0:c2dff8cbb91a 539 break;
terencez 0:c2dff8cbb91a 540 }
terencez 0:c2dff8cbb91a 541
terencez 0:c2dff8cbb91a 542 case 2:
terencez 0:c2dff8cbb91a 543 {
terencez 0:c2dff8cbb91a 544 int16_t value;
terencez 0:c2dff8cbb91a 545
terencez 0:c2dff8cbb91a 546 utils_copyValue(&value, buffer, buffer_len);
terencez 0:c2dff8cbb91a 547
terencez 0:c2dff8cbb91a 548 *dataP = value;
terencez 0:c2dff8cbb91a 549 break;
terencez 0:c2dff8cbb91a 550 }
terencez 0:c2dff8cbb91a 551
terencez 0:c2dff8cbb91a 552 case 4:
terencez 0:c2dff8cbb91a 553 {
terencez 0:c2dff8cbb91a 554 int32_t value;
terencez 0:c2dff8cbb91a 555
terencez 0:c2dff8cbb91a 556 utils_copyValue(&value, buffer, buffer_len);
terencez 0:c2dff8cbb91a 557
terencez 0:c2dff8cbb91a 558 *dataP = value;
terencez 0:c2dff8cbb91a 559 break;
terencez 0:c2dff8cbb91a 560 }
terencez 0:c2dff8cbb91a 561
terencez 0:c2dff8cbb91a 562 case 8:
terencez 0:c2dff8cbb91a 563 utils_copyValue(dataP, buffer, buffer_len);
terencez 0:c2dff8cbb91a 564 return buffer_len;
terencez 0:c2dff8cbb91a 565
terencez 0:c2dff8cbb91a 566 default:
terencez 0:c2dff8cbb91a 567 return 0;
terencez 0:c2dff8cbb91a 568 }
terencez 0:c2dff8cbb91a 569
terencez 0:c2dff8cbb91a 570 return buffer_len;
terencez 0:c2dff8cbb91a 571 }
terencez 0:c2dff8cbb91a 572
terencez 0:c2dff8cbb91a 573 int utils_opaqueToFloat(const uint8_t * buffer,
terencez 0:c2dff8cbb91a 574 size_t buffer_len,
terencez 0:c2dff8cbb91a 575 double * dataP)
terencez 0:c2dff8cbb91a 576 {
terencez 0:c2dff8cbb91a 577 switch (buffer_len)
terencez 0:c2dff8cbb91a 578 {
terencez 0:c2dff8cbb91a 579 case 4:
terencez 0:c2dff8cbb91a 580 {
terencez 0:c2dff8cbb91a 581 float temp;
terencez 0:c2dff8cbb91a 582
terencez 0:c2dff8cbb91a 583 utils_copyValue(&temp, buffer, buffer_len);
terencez 0:c2dff8cbb91a 584
terencez 0:c2dff8cbb91a 585 *dataP = temp;
terencez 0:c2dff8cbb91a 586 }
terencez 0:c2dff8cbb91a 587 return 4;
terencez 0:c2dff8cbb91a 588
terencez 0:c2dff8cbb91a 589 case 8:
terencez 0:c2dff8cbb91a 590 utils_copyValue(dataP, buffer, buffer_len);
terencez 0:c2dff8cbb91a 591 return 8;
terencez 0:c2dff8cbb91a 592
terencez 0:c2dff8cbb91a 593 default:
terencez 0:c2dff8cbb91a 594 return 0;
terencez 0:c2dff8cbb91a 595 }
terencez 0:c2dff8cbb91a 596 }
terencez 0:c2dff8cbb91a 597
terencez 0:c2dff8cbb91a 598 /**
terencez 0:c2dff8cbb91a 599 * Encode an integer value to a byte representation.
terencez 0:c2dff8cbb91a 600 * Returns the length of the result. For values < 0xff length is 1,
terencez 0:c2dff8cbb91a 601 * for values < 0xffff length is 2 and so on.
terencez 0:c2dff8cbb91a 602 * @param data Input value
terencez 0:c2dff8cbb91a 603 * @param data_buffer Result in data_buffer is in big endian encoding
terencez 0:c2dff8cbb91a 604 * Negative values are represented in two's complement as of
terencez 0:c2dff8cbb91a 605 * OMA-TS-LightweightM2M-V1_0-20160308-D, Appendix C
terencez 0:c2dff8cbb91a 606 */
terencez 0:c2dff8cbb91a 607 size_t utils_encodeInt(int64_t data,
terencez 0:c2dff8cbb91a 608 uint8_t data_buffer[_PRV_64BIT_BUFFER_SIZE])
terencez 0:c2dff8cbb91a 609 {
terencez 0:c2dff8cbb91a 610 size_t length = 0;
terencez 0:c2dff8cbb91a 611
terencez 0:c2dff8cbb91a 612 memset(data_buffer, 0, _PRV_64BIT_BUFFER_SIZE);
terencez 0:c2dff8cbb91a 613
terencez 0:c2dff8cbb91a 614 if (data >= INT8_MIN && data <= INT8_MAX)
terencez 0:c2dff8cbb91a 615 {
terencez 0:c2dff8cbb91a 616 length = 1;
terencez 0:c2dff8cbb91a 617 data_buffer[0] = data;
terencez 0:c2dff8cbb91a 618 }
terencez 0:c2dff8cbb91a 619 else if (data >= INT16_MIN && data <= INT16_MAX)
terencez 0:c2dff8cbb91a 620 {
terencez 0:c2dff8cbb91a 621 int16_t value;
terencez 0:c2dff8cbb91a 622
terencez 0:c2dff8cbb91a 623 value = data;
terencez 0:c2dff8cbb91a 624 length = 2;
terencez 0:c2dff8cbb91a 625 data_buffer[0] = (value >> 8) & 0xFF;
terencez 0:c2dff8cbb91a 626 data_buffer[1] = value & 0xFF;
terencez 0:c2dff8cbb91a 627 }
terencez 0:c2dff8cbb91a 628 else if (data >= INT32_MIN && data <= INT32_MAX)
terencez 0:c2dff8cbb91a 629 {
terencez 0:c2dff8cbb91a 630 int32_t value;
terencez 0:c2dff8cbb91a 631
terencez 0:c2dff8cbb91a 632 value = data;
terencez 0:c2dff8cbb91a 633 length = 4;
terencez 0:c2dff8cbb91a 634 utils_copyValue(data_buffer, &value, length);
terencez 0:c2dff8cbb91a 635 }
terencez 0:c2dff8cbb91a 636 else if (data >= INT64_MIN && data <= INT64_MAX)
terencez 0:c2dff8cbb91a 637 {
terencez 0:c2dff8cbb91a 638 length = 8;
terencez 0:c2dff8cbb91a 639 utils_copyValue(data_buffer, &data, length);
terencez 0:c2dff8cbb91a 640 }
terencez 0:c2dff8cbb91a 641
terencez 0:c2dff8cbb91a 642 return length;
terencez 0:c2dff8cbb91a 643 }
terencez 0:c2dff8cbb91a 644
terencez 0:c2dff8cbb91a 645 size_t utils_encodeFloat(double data,
terencez 0:c2dff8cbb91a 646 uint8_t data_buffer[_PRV_64BIT_BUFFER_SIZE])
terencez 0:c2dff8cbb91a 647 {
terencez 0:c2dff8cbb91a 648 size_t length = 0;
terencez 0:c2dff8cbb91a 649
terencez 0:c2dff8cbb91a 650 memset(data_buffer, 0, _PRV_64BIT_BUFFER_SIZE);
terencez 0:c2dff8cbb91a 651
terencez 0:c2dff8cbb91a 652 if ((data < 0.0 - (double)FLT_MAX) || (data >(double)FLT_MAX))
terencez 0:c2dff8cbb91a 653 {
terencez 0:c2dff8cbb91a 654 length = 8;
terencez 0:c2dff8cbb91a 655 utils_copyValue(data_buffer, &data, 8);
terencez 0:c2dff8cbb91a 656 }
terencez 0:c2dff8cbb91a 657 else
terencez 0:c2dff8cbb91a 658 {
terencez 0:c2dff8cbb91a 659 float value;
terencez 0:c2dff8cbb91a 660
terencez 0:c2dff8cbb91a 661 length = 4;
terencez 0:c2dff8cbb91a 662 value = (float)data;
terencez 0:c2dff8cbb91a 663 utils_copyValue(data_buffer, &value, 4);
terencez 0:c2dff8cbb91a 664 }
terencez 0:c2dff8cbb91a 665
terencez 0:c2dff8cbb91a 666 return length;
terencez 0:c2dff8cbb91a 667 }
terencez 0:c2dff8cbb91a 668
terencez 0:c2dff8cbb91a 669 #define PRV_B64_PADDING '='
terencez 0:c2dff8cbb91a 670
terencez 0:c2dff8cbb91a 671 static char b64Alphabet[64] =
terencez 0:c2dff8cbb91a 672 {
terencez 0:c2dff8cbb91a 673 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
terencez 0:c2dff8cbb91a 674 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
terencez 0:c2dff8cbb91a 675 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
terencez 0:c2dff8cbb91a 676 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
terencez 0:c2dff8cbb91a 677 };
terencez 0:c2dff8cbb91a 678
terencez 0:c2dff8cbb91a 679 static void prv_encodeBlock(uint8_t input[3],
terencez 0:c2dff8cbb91a 680 uint8_t output[4])
terencez 0:c2dff8cbb91a 681 {
terencez 0:c2dff8cbb91a 682 output[0] = b64Alphabet[input[0] >> 2];
terencez 0:c2dff8cbb91a 683 output[1] = b64Alphabet[((input[0] & 0x03) << 4) | (input[1] >> 4)];
terencez 0:c2dff8cbb91a 684 output[2] = b64Alphabet[((input[1] & 0x0F) << 2) | (input[2] >> 6)];
terencez 0:c2dff8cbb91a 685 output[3] = b64Alphabet[input[2] & 0x3F];
terencez 0:c2dff8cbb91a 686 }
terencez 0:c2dff8cbb91a 687
terencez 0:c2dff8cbb91a 688 static uint8_t prv_b64Revert(uint8_t value)
terencez 0:c2dff8cbb91a 689 {
terencez 0:c2dff8cbb91a 690 if (value >= 'A' && value <= 'Z')
terencez 0:c2dff8cbb91a 691 {
terencez 0:c2dff8cbb91a 692 return (value - 'A');
terencez 0:c2dff8cbb91a 693 }
terencez 0:c2dff8cbb91a 694 if (value >= 'a' && value <= 'z')
terencez 0:c2dff8cbb91a 695 {
terencez 0:c2dff8cbb91a 696 return (26 + value - 'a');
terencez 0:c2dff8cbb91a 697 }
terencez 0:c2dff8cbb91a 698 if (value >= '0' && value <= '9')
terencez 0:c2dff8cbb91a 699 {
terencez 0:c2dff8cbb91a 700 return (52 + value - '0');
terencez 0:c2dff8cbb91a 701 }
terencez 0:c2dff8cbb91a 702 switch (value)
terencez 0:c2dff8cbb91a 703 {
terencez 0:c2dff8cbb91a 704 case '+':
terencez 0:c2dff8cbb91a 705 return 62;
terencez 0:c2dff8cbb91a 706 case '/':
terencez 0:c2dff8cbb91a 707 return 63;
terencez 0:c2dff8cbb91a 708 default:
terencez 0:c2dff8cbb91a 709 return 0;
terencez 0:c2dff8cbb91a 710 }
terencez 0:c2dff8cbb91a 711 }
terencez 0:c2dff8cbb91a 712
terencez 0:c2dff8cbb91a 713 static void prv_decodeBlock(uint8_t input[4],
terencez 0:c2dff8cbb91a 714 uint8_t output[3])
terencez 0:c2dff8cbb91a 715 {
terencez 0:c2dff8cbb91a 716 uint8_t tmp[4];
terencez 0:c2dff8cbb91a 717 int i;
terencez 0:c2dff8cbb91a 718
terencez 0:c2dff8cbb91a 719 memset(output, 0, 3);
terencez 0:c2dff8cbb91a 720
terencez 0:c2dff8cbb91a 721 for (i = 0; i < 4; i++)
terencez 0:c2dff8cbb91a 722 {
terencez 0:c2dff8cbb91a 723 tmp[i] = prv_b64Revert(input[i]);
terencez 0:c2dff8cbb91a 724 }
terencez 0:c2dff8cbb91a 725
terencez 0:c2dff8cbb91a 726 output[0] = (tmp[0] << 2) | (tmp[1] >> 4);
terencez 0:c2dff8cbb91a 727 output[1] = (tmp[1] << 4) | (tmp[2] >> 2);
terencez 0:c2dff8cbb91a 728 output[2] = (tmp[2] << 6) | tmp[3];
terencez 0:c2dff8cbb91a 729 }
terencez 0:c2dff8cbb91a 730
terencez 0:c2dff8cbb91a 731 static size_t prv_getBase64Size(size_t dataLen)
terencez 0:c2dff8cbb91a 732 {
terencez 0:c2dff8cbb91a 733 size_t result_len;
terencez 0:c2dff8cbb91a 734
terencez 0:c2dff8cbb91a 735 result_len = 4 * (dataLen / 3);
terencez 0:c2dff8cbb91a 736 if (dataLen % 3) result_len += 4;
terencez 0:c2dff8cbb91a 737
terencez 0:c2dff8cbb91a 738 return result_len;
terencez 0:c2dff8cbb91a 739 }
terencez 0:c2dff8cbb91a 740
terencez 0:c2dff8cbb91a 741 size_t utils_base64Encode(uint8_t * dataP,
terencez 0:c2dff8cbb91a 742 size_t dataLen,
terencez 0:c2dff8cbb91a 743 uint8_t * bufferP,
terencez 0:c2dff8cbb91a 744 size_t bufferLen)
terencez 0:c2dff8cbb91a 745 {
terencez 0:c2dff8cbb91a 746 unsigned int data_index;
terencez 0:c2dff8cbb91a 747 unsigned int result_index;
terencez 0:c2dff8cbb91a 748 size_t result_len;
terencez 0:c2dff8cbb91a 749
terencez 0:c2dff8cbb91a 750 result_len = prv_getBase64Size(dataLen);
terencez 0:c2dff8cbb91a 751
terencez 0:c2dff8cbb91a 752 if (result_len > bufferLen) return 0;
terencez 0:c2dff8cbb91a 753
terencez 0:c2dff8cbb91a 754 data_index = 0;
terencez 0:c2dff8cbb91a 755 result_index = 0;
terencez 0:c2dff8cbb91a 756 while (data_index < dataLen)
terencez 0:c2dff8cbb91a 757 {
terencez 0:c2dff8cbb91a 758 switch (dataLen - data_index)
terencez 0:c2dff8cbb91a 759 {
terencez 0:c2dff8cbb91a 760 case 0:
terencez 0:c2dff8cbb91a 761 // should never happen
terencez 0:c2dff8cbb91a 762 break;
terencez 0:c2dff8cbb91a 763 case 1:
terencez 0:c2dff8cbb91a 764 bufferP[result_index] = b64Alphabet[dataP[data_index] >> 2];
terencez 0:c2dff8cbb91a 765 bufferP[result_index + 1] = b64Alphabet[(dataP[data_index] & 0x03) << 4];
terencez 0:c2dff8cbb91a 766 bufferP[result_index + 2] = PRV_B64_PADDING;
terencez 0:c2dff8cbb91a 767 bufferP[result_index + 3] = PRV_B64_PADDING;
terencez 0:c2dff8cbb91a 768 break;
terencez 0:c2dff8cbb91a 769 case 2:
terencez 0:c2dff8cbb91a 770 bufferP[result_index] = b64Alphabet[dataP[data_index] >> 2];
terencez 0:c2dff8cbb91a 771 bufferP[result_index + 1] = b64Alphabet[(dataP[data_index] & 0x03) << 4 | (dataP[data_index + 1] >> 4)];
terencez 0:c2dff8cbb91a 772 bufferP[result_index + 2] = b64Alphabet[(dataP[data_index + 1] & 0x0F) << 2];
terencez 0:c2dff8cbb91a 773 bufferP[result_index + 3] = PRV_B64_PADDING;
terencez 0:c2dff8cbb91a 774 break;
terencez 0:c2dff8cbb91a 775 default:
terencez 0:c2dff8cbb91a 776 prv_encodeBlock(dataP + data_index, bufferP + result_index);
terencez 0:c2dff8cbb91a 777 break;
terencez 0:c2dff8cbb91a 778 }
terencez 0:c2dff8cbb91a 779 data_index += 3;
terencez 0:c2dff8cbb91a 780 result_index += 4;
terencez 0:c2dff8cbb91a 781 }
terencez 0:c2dff8cbb91a 782
terencez 0:c2dff8cbb91a 783 return result_len;
terencez 0:c2dff8cbb91a 784 }
terencez 0:c2dff8cbb91a 785
terencez 0:c2dff8cbb91a 786 size_t utils_opaqueToBase64(uint8_t * dataP,
terencez 0:c2dff8cbb91a 787 size_t dataLen,
terencez 0:c2dff8cbb91a 788 uint8_t ** bufferP)
terencez 0:c2dff8cbb91a 789 {
terencez 0:c2dff8cbb91a 790 size_t buffer_len;
terencez 0:c2dff8cbb91a 791 size_t result_len;
terencez 0:c2dff8cbb91a 792
terencez 0:c2dff8cbb91a 793 buffer_len = prv_getBase64Size(dataLen);
terencez 0:c2dff8cbb91a 794
terencez 0:c2dff8cbb91a 795 *bufferP = (uint8_t *)lwm2m_malloc(buffer_len);
terencez 0:c2dff8cbb91a 796 if (!*bufferP) return 0;
terencez 0:c2dff8cbb91a 797 memset(*bufferP, 0, buffer_len);
terencez 0:c2dff8cbb91a 798
terencez 0:c2dff8cbb91a 799 result_len = utils_base64Encode(dataP, dataLen, *bufferP, buffer_len);
terencez 0:c2dff8cbb91a 800
terencez 0:c2dff8cbb91a 801 if (result_len == 0)
terencez 0:c2dff8cbb91a 802 {
terencez 0:c2dff8cbb91a 803 lwm2m_free(*bufferP);
terencez 0:c2dff8cbb91a 804 *bufferP = NULL;
terencez 0:c2dff8cbb91a 805 }
terencez 0:c2dff8cbb91a 806
terencez 0:c2dff8cbb91a 807 return result_len;
terencez 0:c2dff8cbb91a 808 }
terencez 0:c2dff8cbb91a 809
terencez 0:c2dff8cbb91a 810 size_t utils_base64ToOpaque(uint8_t * dataP,
terencez 0:c2dff8cbb91a 811 size_t dataLen,
terencez 0:c2dff8cbb91a 812 uint8_t ** bufferP)
terencez 0:c2dff8cbb91a 813 {
terencez 0:c2dff8cbb91a 814 size_t data_index;
terencez 0:c2dff8cbb91a 815 size_t result_index;
terencez 0:c2dff8cbb91a 816 size_t result_len;
terencez 0:c2dff8cbb91a 817
terencez 0:c2dff8cbb91a 818 if (dataLen % 4) return 0;
terencez 0:c2dff8cbb91a 819
terencez 0:c2dff8cbb91a 820 result_len = (dataLen >> 2) * 3;
terencez 0:c2dff8cbb91a 821 *bufferP = (uint8_t *)lwm2m_malloc(result_len);
terencez 0:c2dff8cbb91a 822 if (NULL == *bufferP) return 0;
terencez 0:c2dff8cbb91a 823 memset(*bufferP, 0, result_len);
terencez 0:c2dff8cbb91a 824
terencez 0:c2dff8cbb91a 825 // remove padding
terencez 0:c2dff8cbb91a 826 while (dataP[dataLen - 1] == PRV_B64_PADDING)
terencez 0:c2dff8cbb91a 827 {
terencez 0:c2dff8cbb91a 828 dataLen--;
terencez 0:c2dff8cbb91a 829 }
terencez 0:c2dff8cbb91a 830
terencez 0:c2dff8cbb91a 831 data_index = 0;
terencez 0:c2dff8cbb91a 832 result_index = 0;
terencez 0:c2dff8cbb91a 833 while (data_index < dataLen)
terencez 0:c2dff8cbb91a 834 {
terencez 0:c2dff8cbb91a 835 prv_decodeBlock(dataP + data_index, *bufferP + result_index);
terencez 0:c2dff8cbb91a 836 data_index += 4;
terencez 0:c2dff8cbb91a 837 result_index += 3;
terencez 0:c2dff8cbb91a 838 }
terencez 0:c2dff8cbb91a 839 switch (data_index - dataLen)
terencez 0:c2dff8cbb91a 840 {
terencez 0:c2dff8cbb91a 841 case 0:
terencez 0:c2dff8cbb91a 842 break;
terencez 0:c2dff8cbb91a 843 case 2:
terencez 0:c2dff8cbb91a 844 {
terencez 0:c2dff8cbb91a 845 uint8_t tmp[2];
terencez 0:c2dff8cbb91a 846
terencez 0:c2dff8cbb91a 847 tmp[0] = prv_b64Revert(dataP[dataLen - 2]);
terencez 0:c2dff8cbb91a 848 tmp[1] = prv_b64Revert(dataP[dataLen - 1]);
terencez 0:c2dff8cbb91a 849
terencez 0:c2dff8cbb91a 850 *bufferP[result_index - 3] = (tmp[0] << 2) | (tmp[1] >> 4);
terencez 0:c2dff8cbb91a 851 *bufferP[result_index - 2] = (tmp[1] << 4);
terencez 0:c2dff8cbb91a 852 result_len -= 2;
terencez 0:c2dff8cbb91a 853 }
terencez 0:c2dff8cbb91a 854 break;
terencez 0:c2dff8cbb91a 855 case 3:
terencez 0:c2dff8cbb91a 856 {
terencez 0:c2dff8cbb91a 857 uint8_t tmp[3];
terencez 0:c2dff8cbb91a 858
terencez 0:c2dff8cbb91a 859 tmp[0] = prv_b64Revert(dataP[dataLen - 3]);
terencez 0:c2dff8cbb91a 860 tmp[1] = prv_b64Revert(dataP[dataLen - 2]);
terencez 0:c2dff8cbb91a 861 tmp[2] = prv_b64Revert(dataP[dataLen - 1]);
terencez 0:c2dff8cbb91a 862
terencez 0:c2dff8cbb91a 863 *bufferP[result_index - 3] = (tmp[0] << 2) | (tmp[1] >> 4);
terencez 0:c2dff8cbb91a 864 *bufferP[result_index - 2] = (tmp[1] << 4) | (tmp[2] >> 2);
terencez 0:c2dff8cbb91a 865 *bufferP[result_index - 1] = (tmp[2] << 6);
terencez 0:c2dff8cbb91a 866 result_len -= 1;
terencez 0:c2dff8cbb91a 867 }
terencez 0:c2dff8cbb91a 868 break;
terencez 0:c2dff8cbb91a 869 default:
terencez 0:c2dff8cbb91a 870 // error
terencez 0:c2dff8cbb91a 871 lwm2m_free(*bufferP);
terencez 0:c2dff8cbb91a 872 *bufferP = NULL;
terencez 0:c2dff8cbb91a 873 result_len = 0;
terencez 0:c2dff8cbb91a 874 break;
terencez 0:c2dff8cbb91a 875 }
terencez 0:c2dff8cbb91a 876
terencez 0:c2dff8cbb91a 877 return result_len;
terencez 0:c2dff8cbb91a 878 }
terencez 0:c2dff8cbb91a 879
terencez 0:c2dff8cbb91a 880 lwm2m_data_type_t utils_depthToDatatype(uri_depth_t depth)
terencez 0:c2dff8cbb91a 881 {
terencez 0:c2dff8cbb91a 882 switch (depth)
terencez 0:c2dff8cbb91a 883 {
terencez 0:c2dff8cbb91a 884 case URI_DEPTH_OBJECT:
terencez 0:c2dff8cbb91a 885 return LWM2M_TYPE_OBJECT;
terencez 0:c2dff8cbb91a 886 case URI_DEPTH_OBJECT_INSTANCE:
terencez 0:c2dff8cbb91a 887 return LWM2M_TYPE_OBJECT_INSTANCE;
terencez 0:c2dff8cbb91a 888 default:
terencez 0:c2dff8cbb91a 889 break;
terencez 0:c2dff8cbb91a 890 }
terencez 0:c2dff8cbb91a 891
terencez 0:c2dff8cbb91a 892 return LWM2M_TYPE_UNDEFINED;
terencez 0:c2dff8cbb91a 893 }