1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "annotation_data_accessor.h"
17 #include "file_items.h"
18
19 namespace panda::panda_file {
20
AnnotationDataAccessor(const File & panda_file,File::EntityId annotation_id)21 AnnotationDataAccessor::AnnotationDataAccessor(const File &panda_file, File::EntityId annotation_id)
22 : panda_file_(panda_file), annotation_id_(annotation_id)
23 {
24 auto sp = panda_file_.GetSpanFromId(annotation_id_);
25 auto class_idx = helpers::Read<IDX_SIZE>(&sp);
26 class_off_ = panda_file_.ResolveClassIndex(annotation_id_, class_idx).GetOffset();
27 count_ = helpers::Read<COUNT_SIZE>(&sp);
28 size_ = ID_SIZE + COUNT_SIZE + count_ * (ID_SIZE + VALUE_SIZE) + count_ * TYPE_TAG_SIZE;
29
30 elements_sp_ = sp;
31 size_t size = count_ * (ID_SIZE + VALUE_SIZE);
32 THROW_IF(sp.Size() < size, File::INVALID_FILE_OFFSET);
33 elements_tags_ = sp.SubSpan(size);
34 }
35
GetElement(size_t i) const36 AnnotationDataAccessor::Elem AnnotationDataAccessor::GetElement(size_t i) const
37 {
38 ASSERT(i < count_);
39 auto sp = elements_sp_.SubSpan(i * (ID_SIZE + VALUE_SIZE));
40 uint32_t name = helpers::Read<ID_SIZE>(&sp);
41 uint32_t value = helpers::Read<VALUE_SIZE>(&sp);
42 return AnnotationDataAccessor::Elem(panda_file_, File::EntityId(name), value);
43 }
44
GetTag(size_t i) const45 AnnotationDataAccessor::Tag AnnotationDataAccessor::GetTag(size_t i) const
46 {
47 ASSERT(i < count_);
48 auto sp = elements_tags_.SubSpan(i * TYPE_TAG_SIZE);
49 auto item = static_cast<char>(helpers::Read<TYPE_TAG_SIZE>(&sp));
50 return AnnotationDataAccessor::Tag(item);
51 }
52
53 } // namespace panda::panda_file
54