• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #include "idmap2/BinaryStreamVisitor.h"
18 
19 #include <algorithm>
20 #include <cstring>
21 #include <string>
22 
23 #include "android-base/macros.h"
24 
25 namespace android::idmap2 {
26 
Write8(uint8_t value)27 void BinaryStreamVisitor::Write8(uint8_t value) {
28   stream_.write(reinterpret_cast<char*>(&value), sizeof(uint8_t));
29 }
30 
Write16(uint16_t value)31 void BinaryStreamVisitor::Write16(uint16_t value) {
32   uint16_t x = htodl(value);
33   stream_.write(reinterpret_cast<char*>(&x), sizeof(uint16_t));
34 }
35 
Write32(uint32_t value)36 void BinaryStreamVisitor::Write32(uint32_t value) {
37   uint32_t x = htodl(value);
38   stream_.write(reinterpret_cast<char*>(&x), sizeof(uint32_t));
39 }
40 
WriteString(StringPiece value)41 void BinaryStreamVisitor::WriteString(StringPiece value) {
42   // pad with null to nearest word boundary;
43   size_t padding_size = CalculatePadding(value.size());
44   Write32(value.size());
45   stream_.write(value.data(), value.size());
46   stream_.write("\0\0\0\0", padding_size);
47 }
48 
visit(const Idmap & idmap ATTRIBUTE_UNUSED)49 void BinaryStreamVisitor::visit(const Idmap& idmap ATTRIBUTE_UNUSED) {
50   // nothing to do
51 }
52 
visit(const IdmapHeader & header)53 void BinaryStreamVisitor::visit(const IdmapHeader& header) {
54   Write32(header.GetMagic());
55   Write32(header.GetVersion());
56   Write32(header.GetTargetCrc());
57   Write32(header.GetOverlayCrc());
58   Write32(header.GetFulfilledPolicies());
59   Write32(static_cast<uint8_t>(header.GetEnforceOverlayable()));
60   WriteString(header.GetTargetPath());
61   WriteString(header.GetOverlayPath());
62   WriteString(header.GetOverlayName());
63   WriteString(header.GetDebugInfo());
64 }
65 
visit(const IdmapConstraints & constraints)66 void BinaryStreamVisitor::visit(const IdmapConstraints& constraints) {
67   Write32(static_cast<uint32_t>(constraints.constraints.size()));
68   for (const auto& constraint : constraints.constraints) {
69     Write32(constraint.constraint_type);
70     Write32(constraint.constraint_value);
71   }
72 }
73 
visit(const IdmapData & data)74 void BinaryStreamVisitor::visit(const IdmapData& data) {
75   for (const auto& target_entry : data.GetTargetEntries()) {
76     Write32(target_entry.target_id);
77   }
78   for (const auto& target_entry : data.GetTargetEntries()) {
79     Write32(target_entry.overlay_id);
80   }
81 
82   uint32_t current_inline_entry_values_count = 0;
83   for (const auto& target_inline_entry : data.GetTargetInlineEntries()) {
84     Write32(target_inline_entry.target_id);
85   }
86   for (const auto& target_inline_entry : data.GetTargetInlineEntries()) {
87     Write32(current_inline_entry_values_count);
88     Write32(target_inline_entry.values.size());
89     current_inline_entry_values_count += target_inline_entry.values.size();
90   }
91 
92   std::vector<ConfigDescription> configs;
93   configs.reserve(data.GetHeader()->GetConfigCount());
94   for (const auto& target_entry : data.GetTargetInlineEntries()) {
95     for (const auto& target_entry_value : target_entry.values) {
96       auto config_it = std::find(configs.begin(), configs.end(), target_entry_value.first);
97       if (config_it != configs.end()) {
98         Write32(config_it - configs.begin());
99       } else {
100         Write32(configs.size());
101         configs.push_back(target_entry_value.first);
102       }
103       // We're writing a Res_value entry here, and the first 3 bytes of that are
104       // sizeof() + a padding 0 byte
105       static constexpr decltype(android::Res_value::size) kSize = sizeof(android::Res_value);
106       Write16(kSize);
107       Write8(0U);
108       Write8(target_entry_value.second.data_type);
109       Write32(target_entry_value.second.data_value);
110     }
111   }
112 
113   if (!configs.empty()) {
114     stream_.write(reinterpret_cast<const char*>(&configs.front()),
115                   sizeof(configs.front()) * configs.size());
116     if (configs.size() >= 100) {
117       // Let's write a message to future us so that they know when to replace the linear search
118       // in `configs` vector with something more efficient.
119       LOG(WARNING) << "Idmap got " << configs.size()
120                    << " configurations, time to fix the bruteforce search";
121     }
122   }
123 
124   for (const auto& overlay_entry : data.GetOverlayEntries()) {
125     Write32(overlay_entry.overlay_id);
126   }
127   for (const auto& overlay_entry : data.GetOverlayEntries()) {
128     Write32(overlay_entry.target_id);
129   }
130 
131   WriteString(data.GetStringPoolData());
132 }
133 
visit(const IdmapData::Header & header)134 void BinaryStreamVisitor::visit(const IdmapData::Header& header) {
135   Write32(header.GetTargetEntryCount());
136   Write32(header.GetTargetInlineEntryCount());
137   Write32(header.GetTargetInlineEntryValueCount());
138   Write32(header.GetConfigCount());
139   Write32(header.GetOverlayEntryCount());
140   Write32(header.GetStringPoolIndexOffset());
141 }
142 
143 }  // namespace android::idmap2
144