3 * Copyright (c) 2020 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 bool SerializedQNameIterator::Next()
26 return mIsValid && Next(true);
29 bool SerializedQNameIterator::Next(bool followIndirectPointers)
38 assert(mValidData.Contains(mCurrentPosition));
40 const uint8_t length = *mCurrentPosition;
41 if (*mCurrentPosition == 0)
43 // Done with all items
47 if ((length & kPtrMask) == kPtrMask)
49 if (!followIndirectPointers)
51 // Stop at first indirect pointer
55 // PTR contains 2 bytes
56 if (!mValidData.Contains(mCurrentPosition + 1))
62 size_t offset = ((*mCurrentPosition & 0x3F) << 8) | *(mCurrentPosition + 1);
63 if (offset > mLookBehindMax)
65 // Potential infinite recursion.
69 if (offset > mValidData.Size())
76 mLookBehindMax = offset;
77 mCurrentPosition = mValidData.Start() + offset;
81 // This branch handles non-pointer data. This will be string of size [length].
82 if (length > kMaxValueSize)
84 // value is too large (larger than RFC limit)
89 if (!mValidData.Contains(mCurrentPosition + 1 + length))
91 // string outside valid data
96 memcpy(mValue, mCurrentPosition + 1, length);
97 mValue[length] = '\0';
98 mCurrentPosition = mCurrentPosition + length + 1;
104 const uint8_t * SerializedQNameIterator::FindDataEnd()
108 // nothing to do, just advance
117 if (*mCurrentPosition == 0)
119 // mCurrentPosition MUST already be valid
120 return mCurrentPosition + 1;
123 // ends with a dataptr
124 if ((*mCurrentPosition & kPtrMask) == kPtrMask)
126 if (!mValidData.Contains(mCurrentPosition + 1))
130 return mCurrentPosition + 2;
137 } // namespace Minimal