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 "class_data_accessor.h"
17
18 #include "utils/leb128.h"
19 #include "utils/utf.h"
20
21 namespace panda::panda_file {
22
ClassDataAccessor(const File &panda_file, File::EntityId class_id)23 ClassDataAccessor::ClassDataAccessor(const File &panda_file, File::EntityId class_id)
24 : panda_file_(panda_file), class_id_(class_id), name_(), num_fields_(0), num_methods_(0), num_ifaces_(0), size_(0)
25 {
26 ASSERT(!panda_file.IsExternal(class_id));
27 auto sp = panda_file_.GetSpanFromId(class_id_);
28 name_.utf16_length = helpers::ReadULeb128(&sp);
29 name_.data = sp.data();
30
31 size_t size = utf::Mutf8Size(name_.data) + 1; // + 1 for null byte
32 panda_file_.ThrowIfWithCheck(sp.Size() < size, File::INVALID_FILE_OFFSET, File::CLASS_DATA_ACCESSOR);
33 sp = sp.SubSpan(size);
34
35 super_class_off_ = helpers::Read<ID_SIZE>(&sp);
36
37 access_flags_ = helpers::ReadULeb128(&sp);
38 num_fields_ = helpers::ReadULeb128(&sp);
39 num_methods_ = helpers::ReadULeb128(&sp);
40
41 panda_file_.ThrowIfWithCheck(sp.Size() == 0U, File::INVALID_FILE_OFFSET, File::CLASS_DATA_ACCESSOR);
42 auto tag = static_cast<ClassTag>(sp[0]);
43
44 while (tag != ClassTag::NOTHING && tag < ClassTag::SOURCE_LANG) {
45 panda_file_.ThrowIfWithCheck(sp.Size() == 0U, File::INVALID_FILE_OFFSET, File::CLASS_DATA_ACCESSOR);
46 sp = sp.SubSpan(1);
47
48 if (tag == ClassTag::INTERFACES) {
49 num_ifaces_ = helpers::ReadULeb128(&sp);
50 ifaces_offsets_sp_ = sp;
51 size_t scale = IDX_SIZE * num_ifaces_;
52 panda_file_.ThrowIfWithCheck(sp.Size() < scale, File::INVALID_FILE_OFFSET, File::CLASS_DATA_ACCESSOR);
53 sp = sp.SubSpan(scale);
54 }
55
56 panda_file_.ThrowIfWithCheck(sp.Size() == 0U, File::INVALID_FILE_OFFSET, File::CLASS_DATA_ACCESSOR);
57 tag = static_cast<ClassTag>(sp[0]);
58 }
59
60 source_lang_sp_ = sp;
61
62 if (tag == ClassTag::NOTHING) {
63 annotations_sp_ = sp;
64 source_file_sp_ = sp;
65 panda_file_.ThrowIfWithCheck(sp.Size() < TAG_SIZE, File::INVALID_FILE_OFFSET, File::CLASS_DATA_ACCESSOR);
66 fields_sp_ = sp.SubSpan(TAG_SIZE); // skip NOTHING tag
67 }
68 }
69
70 } // namespace panda::panda_file
71