Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2021 Project CHIP Authors
4 : * Licensed under the Apache License, Version 2.0 (the "License");
5 : * you may not use this file except in compliance with the License.
6 : * You may obtain a copy of the License at
7 : *
8 : * http://www.apache.org/licenses/LICENSE-2.0
9 : *
10 : * Unless required by applicable law or agreed to in writing, software
11 : * distributed under the License is distributed on an "AS IS" BASIS,
12 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : * See the License for the specific language governing permissions and
14 : * limitations under the License.
15 : */
16 :
17 : #include "StructParser.h"
18 :
19 : namespace chip {
20 : namespace app {
21 74966 : CHIP_ERROR StructParser::Init(const TLV::TLVReader & aReader)
22 : {
23 74966 : mReader.Init(aReader);
24 74966 : VerifyOrReturnError(TLV::kTLVType_Structure == mReader.GetType(), CHIP_ERROR_WRONG_TLV_TYPE);
25 74966 : ReturnErrorOnFailure(mReader.EnterContainer(mOuterContainerType));
26 74966 : return CheckSchemaOrdering();
27 : }
28 :
29 74966 : CHIP_ERROR StructParser::CheckSchemaOrdering() const
30 : {
31 74966 : CHIP_ERROR err = CHIP_NO_ERROR;
32 74966 : TLV::TLVReader reader;
33 74966 : reader.Init(mReader);
34 74966 : uint32_t preTagNum = 0;
35 74966 : bool first = true;
36 239509 : while (CHIP_NO_ERROR == (err = reader.Next()))
37 : {
38 164543 : if (!TLV::IsContextTag(reader.GetTag()))
39 : {
40 : // Just skip over non-context tags, for forward compat.
41 0 : continue;
42 : }
43 164543 : uint32_t tagNum = TLV::TagNumFromTag(reader.GetTag());
44 164543 : if (first || (preTagNum < tagNum))
45 : {
46 164543 : preTagNum = tagNum;
47 : }
48 : else
49 : {
50 0 : return CHIP_ERROR_INVALID_TLV_TAG;
51 : }
52 164543 : first = false;
53 : }
54 74966 : if (CHIP_END_OF_TLV == err)
55 : {
56 74966 : err = CHIP_NO_ERROR;
57 : }
58 74966 : ReturnErrorOnFailure(err);
59 74966 : return reader.ExitContainer(mOuterContainerType);
60 : }
61 : } // namespace app
62 : } // namespace chip
|