• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2017 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef PROPERTY_INFO_PARSER_H
18 #define PROPERTY_INFO_PARSER_H
19 
20 #include <stdint.h>
21 #include <stdlib.h>
22 
23 namespace android {
24 namespace properties {
25 
26 // The below structs intentionally do not end with char name[0] or other tricks to allocate
27 // with a dynamic size, such that they can be added onto in the future without breaking
28 // backwards compatibility.
29 struct PropertyEntry {
30   uint32_t name_offset;
31   uint32_t namelen;
32 
33   // This is the context match for this node_; ~0u if it doesn't correspond to any.
34   uint32_t context_index;
35   // This is the type for this node_; ~0u if it doesn't correspond to any.
36   uint32_t type_index;
37 };
38 
39 struct TrieNodeInternal {
40   // This points to a property entry struct, which includes the name for this node
41   uint32_t property_entry;
42 
43   // Children are a sorted list of child nodes_; binary search them.
44   uint32_t num_child_nodes;
45   uint32_t child_nodes;
46 
47   // Prefixes are terminating prefix matches at this node, sorted longest to smallest
48   // Take the first match sequentially found with StartsWith().
49   uint32_t num_prefixes;
50   uint32_t prefix_entries;
51 
52   // Exact matches are a sorted list of exact matches at this node_; binary search them.
53   uint32_t num_exact_matches;
54   uint32_t exact_match_entries;
55 };
56 
57 struct PropertyInfoAreaHeader {
58   // The current version of this data as created by property service.
59   uint32_t current_version;
60   // The lowest version of libc that can properly parse this data.
61   uint32_t minimum_supported_version;
62   uint32_t size;
63   uint32_t contexts_offset;
64   uint32_t types_offset;
65   uint32_t root_offset;
66 };
67 
68 class SerializedData {
69  public:
size()70   uint32_t size() const {
71     return reinterpret_cast<const PropertyInfoAreaHeader*>(data_base_)->size;
72   }
73 
c_string(uint32_t offset)74   const char* c_string(uint32_t offset) const {
75     if (offset != 0 && offset > size()) return nullptr;
76     return static_cast<const char*>(data_base_ + offset);
77   }
78 
uint32_array(uint32_t offset)79   const uint32_t* uint32_array(uint32_t offset) const {
80     if (offset != 0 && offset > size()) return nullptr;
81     return reinterpret_cast<const uint32_t*>(data_base_ + offset);
82   }
83 
uint32(uint32_t offset)84   uint32_t uint32(uint32_t offset) const {
85     if (offset != 0 && offset > size()) return ~0u;
86     return *reinterpret_cast<const uint32_t*>(data_base_ + offset);
87   }
88 
data_base()89   const char* data_base() const { return data_base_; }
90 
91  private:
92   const char data_base_[0];
93 };
94 
95 class TrieNode {
96  public:
TrieNode()97   TrieNode() : serialized_data_(nullptr), trie_node_base_(nullptr) {}
TrieNode(const SerializedData * data_base,const TrieNodeInternal * trie_node_base)98   TrieNode(const SerializedData* data_base, const TrieNodeInternal* trie_node_base)
99       : serialized_data_(data_base), trie_node_base_(trie_node_base) {}
100 
name()101   const char* name() const {
102     return serialized_data_->c_string(node_property_entry()->name_offset);
103   }
104 
context_index()105   uint32_t context_index() const { return node_property_entry()->context_index; }
type_index()106   uint32_t type_index() const { return node_property_entry()->type_index; }
107 
num_child_nodes()108   uint32_t num_child_nodes() const { return trie_node_base_->num_child_nodes; }
child_node(int n)109   TrieNode child_node(int n) const {
110     uint32_t child_node_offset = serialized_data_->uint32_array(trie_node_base_->child_nodes)[n];
111     const TrieNodeInternal* trie_node_base =
112         reinterpret_cast<const TrieNodeInternal*>(serialized_data_->data_base() + child_node_offset);
113     return TrieNode(serialized_data_, trie_node_base);
114   }
115 
116   bool FindChildForString(const char* input, uint32_t namelen, TrieNode* child) const;
117 
num_prefixes()118   uint32_t num_prefixes() const { return trie_node_base_->num_prefixes; }
prefix(int n)119   const PropertyEntry* prefix(int n) const {
120     uint32_t prefix_entry_offset =
121         serialized_data_->uint32_array(trie_node_base_->prefix_entries)[n];
122     return reinterpret_cast<const PropertyEntry*>(serialized_data_->data_base() +
123                                                   prefix_entry_offset);
124   }
125 
num_exact_matches()126   uint32_t num_exact_matches() const { return trie_node_base_->num_exact_matches; }
exact_match(int n)127   const PropertyEntry* exact_match(int n) const {
128     uint32_t exact_match_entry_offset =
129         serialized_data_->uint32_array(trie_node_base_->exact_match_entries)[n];
130     return reinterpret_cast<const PropertyEntry*>(serialized_data_->data_base() +
131                                                   exact_match_entry_offset);
132   }
133 
134  private:
node_property_entry()135   const PropertyEntry* node_property_entry() const {
136     return reinterpret_cast<const PropertyEntry*>(serialized_data_->data_base() +
137                                                   trie_node_base_->property_entry);
138   }
139 
140   const SerializedData* serialized_data_;
141   const TrieNodeInternal* trie_node_base_;
142 };
143 
144 class PropertyInfoArea : private SerializedData {
145  public:
146   void GetPropertyInfoIndexes(const char* name, uint32_t* context_index, uint32_t* type_index) const;
147   void GetPropertyInfo(const char* property, const char** context, const char** type) const;
148 
149   int FindContextIndex(const char* context) const;
150   int FindTypeIndex(const char* type) const;
151 
context(uint32_t index)152   const char* context(uint32_t index) const {
153     uint32_t context_array_size_offset = contexts_offset();
154     const uint32_t* context_array = uint32_array(context_array_size_offset + sizeof(uint32_t));
155     return data_base() + context_array[index];
156   }
157 
type(uint32_t index)158   const char* type(uint32_t index) const {
159     uint32_t type_array_size_offset = types_offset();
160     const uint32_t* type_array = uint32_array(type_array_size_offset + sizeof(uint32_t));
161     return data_base() + type_array[index];
162   }
163 
current_version()164   uint32_t current_version() const { return header()->current_version; }
minimum_supported_version()165   uint32_t minimum_supported_version() const { return header()->minimum_supported_version; }
166 
size()167   uint32_t size() const { return SerializedData::size(); }
168 
num_contexts()169   uint32_t num_contexts() const { return uint32_array(contexts_offset())[0]; }
num_types()170   uint32_t num_types() const { return uint32_array(types_offset())[0]; }
171 
root_node()172   TrieNode root_node() const { return trie(header()->root_offset); }
173 
174  private:
175   void CheckPrefixMatch(const char* remaining_name, const TrieNode& trie_node,
176                         uint32_t* context_index, uint32_t* type_index) const;
177 
header()178   const PropertyInfoAreaHeader* header() const {
179     return reinterpret_cast<const PropertyInfoAreaHeader*>(data_base());
180   }
contexts_offset()181   uint32_t contexts_offset() const { return header()->contexts_offset; }
contexts_array_offset()182   uint32_t contexts_array_offset() const { return contexts_offset() + sizeof(uint32_t); }
types_offset()183   uint32_t types_offset() const { return header()->types_offset; }
types_array_offset()184   uint32_t types_array_offset() const { return types_offset() + sizeof(uint32_t); }
185 
trie(uint32_t offset)186   TrieNode trie(uint32_t offset) const {
187     if (offset != 0 && offset > size()) return TrieNode();
188     const TrieNodeInternal* trie_node_base =
189         reinterpret_cast<const TrieNodeInternal*>(data_base() + offset);
190     return TrieNode(this, trie_node_base);
191   }
192 };
193 
194 // This is essentially a smart pointer for read only mmap region for property contexts.
195 class PropertyInfoAreaFile {
196  public:
PropertyInfoAreaFile()197   PropertyInfoAreaFile() : mmap_base_(nullptr), mmap_size_(0) {}
~PropertyInfoAreaFile()198   ~PropertyInfoAreaFile() { Reset(); }
199 
200   PropertyInfoAreaFile(const PropertyInfoAreaFile&) = delete;
201   void operator=(const PropertyInfoAreaFile&) = delete;
202   PropertyInfoAreaFile(PropertyInfoAreaFile&&) = default;
203   PropertyInfoAreaFile& operator=(PropertyInfoAreaFile&&) = default;
204 
205   bool LoadDefaultPath();
206   bool LoadPath(const char* filename);
207 
208   const PropertyInfoArea* operator->() const {
209     return reinterpret_cast<const PropertyInfoArea*>(mmap_base_);
210   }
211 
212   explicit operator bool() const { return mmap_base_ != nullptr; }
213 
214   void Reset();
215 
216  private:
217   void* mmap_base_;
218   size_t mmap_size_;
219 };
220 
221 }  // namespace properties
222 }  // namespace android
223 
224 #endif
225