|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright (c) 2020 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#include <app/util/ember-strings.h> |
| 18 | + |
| 19 | +#include <lib/core/CHIPEncoding.h> |
| 20 | + |
| 21 | +using namespace chip; |
| 22 | + |
| 23 | +uint8_t emberAfStringLength(const uint8_t * buffer) |
| 24 | +{ |
| 25 | + // The first byte specifies the length of the string. A length of 0xFF means |
| 26 | + // the string is invalid and there is no character data. |
| 27 | + return (buffer[0] == 0xFF ? 0 : buffer[0]); |
| 28 | +} |
| 29 | + |
| 30 | +uint16_t emberAfLongStringLength(const uint8_t * buffer) |
| 31 | +{ |
| 32 | + // The first two bytes specify the length of the long string. A length of |
| 33 | + // 0xFFFF means the string is invalid and there is no character data. |
| 34 | + uint16_t length = Encoding::LittleEndian::Get16(buffer); |
| 35 | + return (length == 0xFFFF ? 0 : length); |
| 36 | +} |
| 37 | + |
| 38 | +void emberAfCopyString(uint8_t * dest, const uint8_t * src, size_t size) |
| 39 | +{ |
| 40 | + if (src == nullptr) |
| 41 | + { |
| 42 | + dest[0] = 0; // Zero out the length of string |
| 43 | + } |
| 44 | + else if (src[0] == 0xFF) |
| 45 | + { |
| 46 | + dest[0] = src[0]; |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + uint8_t length = emberAfStringLength(src); |
| 51 | + if (size < length) |
| 52 | + { |
| 53 | + // Since we have checked that size < length, size must be able to fit into the type of length. |
| 54 | + length = static_cast<decltype(length)>(size); |
| 55 | + } |
| 56 | + memmove(dest + 1, src + 1, length); |
| 57 | + dest[0] = length; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size) |
| 62 | +{ |
| 63 | + if (src == nullptr) |
| 64 | + { |
| 65 | + dest[0] = dest[1] = 0; // Zero out the length of string |
| 66 | + } |
| 67 | + else if ((src[0] == 0xFF) && (src[1] == 0xFF)) |
| 68 | + { |
| 69 | + dest[0] = 0xFF; |
| 70 | + dest[1] = 0xFF; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + uint16_t length = emberAfLongStringLength(src); |
| 75 | + if (size < length) |
| 76 | + { |
| 77 | + // Since we have checked that size < length, size must be able to fit into the type of length. |
| 78 | + length = static_cast<decltype(length)>(size); |
| 79 | + } |
| 80 | + memmove(dest + 2, src + 2, length); |
| 81 | + Encoding::LittleEndian::Put16(dest, length); |
| 82 | + } |
| 83 | +} |
0 commit comments