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