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