Simple interface for Mbed Cloud Client
Embed:
(wiki syntax)
Show/hide line numbers
common_functions.c
00001 /* 00002 * Copyright (c) 2014-2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 /* 00018 * Most functions can be inlined, and definitions are in common_functions.h. 00019 * Define COMMON_FUNCTIONS_FN before including it to generate external definitions. 00020 */ 00021 #define COMMON_FUNCTIONS_FN extern 00022 00023 #include "common_functions.h" 00024 00025 #include <string.h> 00026 00027 /* Returns mask for <split_value> (0-8) most-significant bits of a byte */ 00028 static inline uint8_t context_split_mask(uint_fast8_t split_value) 00029 { 00030 return (uint8_t) - (0x100u >> split_value); 00031 } 00032 00033 bool bitsequal(const uint8_t *a, const uint8_t *b, uint_fast8_t bits) 00034 { 00035 uint_fast8_t bytes = bits / 8; 00036 bits %= 8; 00037 00038 if (memcmp(a, b, bytes)) { 00039 return false; 00040 } 00041 00042 if (bits) { 00043 uint_fast8_t split_bit = context_split_mask(bits); 00044 if ((a[bytes] & split_bit) != (b[bytes] & split_bit)) { 00045 return false; 00046 } 00047 } 00048 00049 return true; 00050 } 00051 00052 uint8_t *bitcopy(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits) 00053 { 00054 uint_fast8_t bytes = bits / 8; 00055 bits %= 8; 00056 00057 if (bytes) { 00058 dst = (uint8_t *) memcpy(dst, src, bytes) + bytes; 00059 src += bytes; 00060 } 00061 00062 if (bits) { 00063 uint_fast8_t split_bit = context_split_mask(bits); 00064 *dst = (*src & split_bit) | (*dst & ~ split_bit); 00065 } 00066 00067 return dst; 00068 } 00069 00070 uint8_t *bitcopy0(uint8_t *restrict dst, const uint8_t *restrict src, uint_fast8_t bits) 00071 { 00072 uint_fast8_t bytes = bits / 8; 00073 bits %= 8; 00074 00075 if (bytes) { 00076 dst = (uint8_t *) memcpy(dst, src, bytes) + bytes; 00077 src += bytes; 00078 } 00079 00080 if (bits) { 00081 uint_fast8_t split_bit = context_split_mask(bits); 00082 *dst = (*src & split_bit); 00083 } 00084 00085 return dst; 00086 }
Generated on Tue Jul 12 2022 19:01:33 by 1.7.2