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