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 THROW_IF(sp.Size() < size, File::INVALID_FILE_OFFSET);
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 ClassTag tag = ClassTag::NOTHING;
42 THROW_IF(sp.Size() == 0U, File::INVALID_FILE_OFFSET);
43 tag = static_cast<ClassTag>(sp[0]);
44
45 while (tag != ClassTag::NOTHING && tag < ClassTag::SOURCE_LANG) {
46 THROW_IF(sp.Size() == 0U, File::INVALID_FILE_OFFSET);
47 sp = sp.SubSpan(1);
48
49 if (tag == ClassTag::INTERFACES) {
50 num_ifaces_ = helpers::ReadULeb128(&sp);
51 ifaces_offsets_sp_ = sp;
52 size_t size = IDX_SIZE * num_ifaces_;
53 THROW_IF(sp.Size() < size, File::INVALID_FILE_OFFSET);
54 sp = sp.SubSpan(size);
55 }
56
57 THROW_IF(sp.Size() == 0U, File::INVALID_FILE_OFFSET);
58 tag = static_cast<ClassTag>(sp[0]);
59 }
60
61 source_lang_sp_ = sp;
62
63 if (tag == ClassTag::NOTHING) {
64 annotations_sp_ = sp;
65 source_file_sp_ = sp;
66 THROW_IF(sp.Size() < TAG_SIZE, File::INVALID_FILE_OFFSET);
67 fields_sp_ = sp.SubSpan(TAG_SIZE); // skip NOTHING tag
68 }
69 }
70
71 } // namespace panda::panda_file
72