Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2024 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 :
18 : #include <lib/core/TLVVectorWriter.h>
19 :
20 : #include <cstdint>
21 : #include <vector>
22 :
23 : #include <lib/core/CHIPError.h>
24 : #include <lib/core/TLVCommon.h>
25 :
26 : namespace chip {
27 : namespace TLV {
28 :
29 : namespace {
30 :
31 : constexpr uint32_t kIpv6MtuSize = 1280;
32 :
33 : } // namespace
34 :
35 4 : TlvVectorWriter::TlvVectorWriter(std::vector<uint8_t> & buffer) : mVectorBuffer(buffer)
36 : {
37 4 : Init(mVectorBuffer);
38 4 : }
39 :
40 4 : TlvVectorWriter::~TlvVectorWriter() = default;
41 :
42 4 : TlvVectorWriter::TlvVectorBuffer::TlvVectorBuffer(std::vector<uint8_t> & buffer) : mFinalBuffer(buffer) {}
43 :
44 4 : TlvVectorWriter::TlvVectorBuffer::~TlvVectorBuffer() {}
45 :
46 4 : CHIP_ERROR TlvVectorWriter::TlvVectorBuffer::OnInit(TLVWriter & /*writer*/, uint8_t *& bufStart, uint32_t & bufLen)
47 : {
48 4 : VerifyOrReturnError(mFinalBuffer.empty(), CHIP_ERROR_INCORRECT_STATE);
49 :
50 4 : ResizeWriteBuffer(bufStart, bufLen);
51 :
52 4 : return CHIP_NO_ERROR;
53 : }
54 :
55 14 : CHIP_ERROR TlvVectorWriter::TlvVectorBuffer::GetNewBuffer(TLVWriter & /*writer*/, uint8_t *& bufStart, uint32_t & bufLen)
56 : {
57 14 : VerifyOrReturnError(!mFinalBuffer.empty(), CHIP_ERROR_INCORRECT_STATE);
58 14 : VerifyOrReturnError(mWritingBuffer.data() == bufStart, CHIP_ERROR_INCORRECT_STATE);
59 :
60 14 : ResizeWriteBuffer(bufStart, bufLen);
61 :
62 14 : return CHIP_NO_ERROR;
63 : }
64 :
65 18 : CHIP_ERROR TlvVectorWriter::TlvVectorBuffer::FinalizeBuffer(TLVWriter & /*writer*/, uint8_t * bufStart, uint32_t bufLen)
66 : {
67 18 : VerifyOrReturnError(mWritingBuffer.data() == bufStart, CHIP_ERROR_INCORRECT_STATE);
68 18 : VerifyOrReturnError(bufLen <= mWritingBuffer.size(), CHIP_ERROR_BUFFER_TOO_SMALL);
69 :
70 18 : mWritingBuffer.resize(bufLen);
71 :
72 18 : mFinalBuffer.insert(mFinalBuffer.end(), mWritingBuffer.begin(), mWritingBuffer.end());
73 18 : mWritingBuffer.resize(0);
74 :
75 18 : mFinalBuffer.shrink_to_fit();
76 :
77 18 : return CHIP_NO_ERROR;
78 : }
79 :
80 18 : void TlvVectorWriter::TlvVectorBuffer::ResizeWriteBuffer(uint8_t *& bufStart, uint32_t & bufLen)
81 : {
82 18 : VerifyOrReturn(mWritingBuffer.empty());
83 :
84 18 : mWritingBuffer.resize(kIpv6MtuSize);
85 18 : bufStart = mWritingBuffer.data();
86 :
87 18 : auto size = mWritingBuffer.size();
88 18 : VerifyOrReturn(size <= std::numeric_limits<uint32_t>::max());
89 18 : bufLen = static_cast<uint32_t>(size);
90 : }
91 :
92 : } // namespace TLV
93 : } // namespace chip
|