leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16
leothedragon 0:8f0bb79ddd48 17 /*
leothedragon 0:8f0bb79ddd48 18 * Most functions can be inlined, and definitions are in common_functions.h.
leothedragon 0:8f0bb79ddd48 19 * Define COMMON_FUNCTIONS_FN before including it to generate external definitions.
leothedragon 0:8f0bb79ddd48 20 */
leothedragon 0:8f0bb79ddd48 21 #define COMMON_FUNCTIONS_FN extern
leothedragon 0:8f0bb79ddd48 22
leothedragon 0:8f0bb79ddd48 23 #include "common_functions.h"
leothedragon 0:8f0bb79ddd48 24
leothedragon 0:8f0bb79ddd48 25 #include <string.h>
leothedragon 0:8f0bb79ddd48 26
leothedragon 0:8f0bb79ddd48 27 /* Returns mask for <split_value> (0-8) most-significant bits of a byte */
leothedragon 0:8f0bb79ddd48 28 static inline uint8_t context_split_mask(uint_fast8_t split_value)
leothedragon 0:8f0bb79ddd48 29 {
leothedragon 0:8f0bb79ddd48 30 return (uint8_t) - (0x100u >> split_value);
leothedragon 0:8f0bb79ddd48 31 }
leothedragon 0:8f0bb79ddd48 32
leothedragon 0:8f0bb79ddd48 33 bool bitsequal(const uint8_t *a, const uint8_t *b, uint_fast8_t bits)
leothedragon 0:8f0bb79ddd48 34 {
leothedragon 0:8f0bb79ddd48 35 uint_fast8_t bytes = bits / 8;
leothedragon 0:8f0bb79ddd48 36 bits %= 8;
leothedragon 0:8f0bb79ddd48 37
leothedragon 0:8f0bb79ddd48 38 if (bytes && memcmp(a, b, bytes)) {
leothedragon 0:8f0bb79ddd48 39 return false;
leothedragon 0:8f0bb79ddd48 40 }
leothedragon 0:8f0bb79ddd48 41
leothedragon 0:8f0bb79ddd48 42 if (bits) {
leothedragon 0:8f0bb79ddd48 43 uint_fast8_t split_bit = context_split_mask(bits);
leothedragon 0:8f0bb79ddd48 44 if ((a[bytes] & split_bit) != (b[bytes] & split_bit)) {
leothedragon 0:8f0bb79ddd48 45 return false;
leothedragon 0:8f0bb79ddd48 46 }
leothedragon 0:8f0bb79ddd48 47 }
leothedragon 0:8f0bb79ddd48 48
leothedragon 0:8f0bb79ddd48 49 return true;
leothedragon 0:8f0bb79ddd48 50 }
leothedragon 0:8f0bb79ddd48 51
leothedragon 0:8f0bb79ddd48 52 uint8_t *bitcopy(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits)
leothedragon 0:8f0bb79ddd48 53 {
leothedragon 0:8f0bb79ddd48 54 uint_fast8_t bytes = bits / 8;
leothedragon 0:8f0bb79ddd48 55 bits %= 8;
leothedragon 0:8f0bb79ddd48 56
leothedragon 0:8f0bb79ddd48 57 if (bytes) {
leothedragon 0:8f0bb79ddd48 58 dst = (uint8_t *) memcpy(dst, src, bytes) + bytes;
leothedragon 0:8f0bb79ddd48 59 src += bytes;
leothedragon 0:8f0bb79ddd48 60 }
leothedragon 0:8f0bb79ddd48 61
leothedragon 0:8f0bb79ddd48 62 if (bits) {
leothedragon 0:8f0bb79ddd48 63 uint_fast8_t split_bit = context_split_mask(bits);
leothedragon 0:8f0bb79ddd48 64 *dst = (*src & split_bit) | (*dst & ~ split_bit);
leothedragon 0:8f0bb79ddd48 65 }
leothedragon 0:8f0bb79ddd48 66
leothedragon 0:8f0bb79ddd48 67 return dst;
leothedragon 0:8f0bb79ddd48 68 }
leothedragon 0:8f0bb79ddd48 69
leothedragon 0:8f0bb79ddd48 70 uint8_t *bitcopy0(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits)
leothedragon 0:8f0bb79ddd48 71 {
leothedragon 0:8f0bb79ddd48 72 uint_fast8_t bytes = bits / 8;
leothedragon 0:8f0bb79ddd48 73 bits %= 8;
leothedragon 0:8f0bb79ddd48 74
leothedragon 0:8f0bb79ddd48 75 if (bytes) {
leothedragon 0:8f0bb79ddd48 76 dst = (uint8_t *) memcpy(dst, src, bytes) + bytes;
leothedragon 0:8f0bb79ddd48 77 src += bytes;
leothedragon 0:8f0bb79ddd48 78 }
leothedragon 0:8f0bb79ddd48 79
leothedragon 0:8f0bb79ddd48 80 if (bits) {
leothedragon 0:8f0bb79ddd48 81 uint_fast8_t split_bit = context_split_mask(bits);
leothedragon 0:8f0bb79ddd48 82 *dst = (*src & split_bit);
leothedragon 0:8f0bb79ddd48 83 }
leothedragon 0:8f0bb79ddd48 84
leothedragon 0:8f0bb79ddd48 85 return dst;
leothedragon 0:8f0bb79ddd48 86 }