• 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 // Convert objects from and to strings.
18 
19 #include "parse_string.h"
20 
21 #include <android-base/parseint.h>
22 #include <android-base/strings.h>
23 
24 #include "constants-private.h"
25 
26 namespace android {
27 using base::ParseUint;
28 
29 namespace vintf {
30 
31 static const std::string kRequired("required");
32 static const std::string kOptional("optional");
33 static const std::string kConfigPrefix("CONFIG_");
34 
SplitString(const std::string & s,char c)35 std::vector<std::string> SplitString(const std::string &s, char c) {
36     std::vector<std::string> components;
37 
38     size_t startPos = 0;
39     size_t matchPos;
40     while ((matchPos = s.find(c, startPos)) != std::string::npos) {
41         components.push_back(s.substr(startPos, matchPos - startPos));
42         startPos = matchPos + 1;
43     }
44 
45     if (startPos <= s.length()) {
46         components.push_back(s.substr(startPos));
47     }
48     return components;
49 }
50 
51 template <typename T>
operator <<(std::ostream & os,const std::vector<T> objs)52 std::ostream &operator<<(std::ostream &os, const std::vector<T> objs) {
53     bool first = true;
54     for (const T &v : objs) {
55         if (!first) {
56             os << ",";
57         }
58         os << v;
59         first = false;
60     }
61     return os;
62 }
63 
64 template <typename T>
parse(const std::string & s,std::vector<T> * objs)65 bool parse(const std::string &s, std::vector<T> *objs) {
66     std::vector<std::string> v = SplitString(s, ',');
67     objs->resize(v.size());
68     size_t idx = 0;
69     for (const auto &item : v) {
70         T ver;
71         if (!parse(item, &ver)) {
72             return false;
73         }
74         objs->at(idx++) = ver;
75     }
76     return true;
77 }
78 
79 template<typename E, typename Array>
parseEnum(const std::string & s,E * e,const Array & strings)80 bool parseEnum(const std::string &s, E *e, const Array &strings) {
81     for (size_t i = 0; i < strings.size(); ++i) {
82         if (s == strings.at(i)) {
83             *e = static_cast<E>(i);
84             return true;
85         }
86     }
87     return false;
88 }
89 
90 #define DEFINE_PARSE_STREAMIN_FOR_ENUM(ENUM) \
91     bool parse(const std::string &s, ENUM *hf) {                   \
92         return parseEnum(s, hf, g##ENUM##Strings);                 \
93     }                                                              \
94     std::ostream &operator<<(std::ostream &os, ENUM hf) {          \
95         return os << g##ENUM##Strings.at(static_cast<size_t>(hf)); \
96     }                                                              \
97 
98 DEFINE_PARSE_STREAMIN_FOR_ENUM(HalFormat)
99 DEFINE_PARSE_STREAMIN_FOR_ENUM(Transport)
100 DEFINE_PARSE_STREAMIN_FOR_ENUM(Arch)
101 DEFINE_PARSE_STREAMIN_FOR_ENUM(KernelConfigType)
102 DEFINE_PARSE_STREAMIN_FOR_ENUM(Tristate)
103 DEFINE_PARSE_STREAMIN_FOR_ENUM(SchemaType)
104 DEFINE_PARSE_STREAMIN_FOR_ENUM(XmlSchemaFormat)
105 
106 std::ostream &operator<<(std::ostream &os, const KernelConfigTypedValue &kctv) {
107     switch (kctv.mType) {
108         case KernelConfigType::STRING:
109             return os << kctv.mStringValue;
110         case KernelConfigType::INTEGER:
111             return os << to_string(kctv.mIntegerValue);
112         case KernelConfigType::RANGE:
113             return os << to_string(kctv.mRangeValue.first) << "-"
114                       << to_string(kctv.mRangeValue.second);
115         case KernelConfigType::TRISTATE:
116             return os << to_string(kctv.mTristateValue);
117     }
118 }
119 
parse(const std::string & s,Level * l)120 bool parse(const std::string& s, Level* l) {
121     if (s.empty()) {
122         *l = Level::UNSPECIFIED;
123         return true;
124     }
125     if (s == "legacy") {
126         *l = Level::LEGACY;
127         return true;
128     }
129     size_t value;
130     if (!ParseUint(s, &value)) {
131         return false;
132     }
133     *l = static_cast<Level>(value);
134     return true;
135 }
136 
operator <<(std::ostream & os,Level l)137 std::ostream& operator<<(std::ostream& os, Level l) {
138     if (l == Level::UNSPECIFIED) {
139         return os;
140     }
141     if (l == Level::LEGACY) {
142         return os << "legacy";
143     }
144     return os << static_cast<size_t>(l);
145 }
146 
147 // Notice that strtoull is used even though KernelConfigIntValue is signed int64_t,
148 // because strtoull can accept negative values as well.
149 // Notice that according to man strtoul, strtoull can actually accept
150 // -2^64 + 1 to 2^64 - 1, with the 65th bit truncated.
151 // ParseInt / ParseUint are not used because they do not handle signed hex very well.
152 template <typename T>
parseKernelConfigIntHelper(const std::string & s,T * i)153 bool parseKernelConfigIntHelper(const std::string &s, T *i) {
154     char *end;
155     errno = 0;
156     unsigned long long int ulli = strtoull(s.c_str(), &end, 0 /* base */);
157     // It is implementation defined that what value will be returned by strtoull
158     // in the error case, so we are checking errno directly here.
159     if (errno == 0 && s.c_str() != end && *end == '\0') {
160         *i = static_cast<T>(ulli);
161         return true;
162     }
163     return false;
164 }
165 
parseKernelConfigInt(const std::string & s,int64_t * i)166 bool parseKernelConfigInt(const std::string &s, int64_t *i) {
167     return parseKernelConfigIntHelper(s, i);
168 }
169 
parseKernelConfigInt(const std::string & s,uint64_t * i)170 bool parseKernelConfigInt(const std::string &s, uint64_t *i) {
171     return parseKernelConfigIntHelper(s, i);
172 }
173 
parseRange(const std::string & s,KernelConfigRangeValue * range)174 bool parseRange(const std::string &s, KernelConfigRangeValue *range) {
175     auto pos = s.find('-');
176     if (pos == std::string::npos) {
177         return false;
178     }
179     return parseKernelConfigInt(s.substr(0, pos),  &range->first)
180         && parseKernelConfigInt(s.substr(pos + 1), &range->second);
181 }
182 
parse(const std::string & s,KernelConfigKey * key)183 bool parse(const std::string &s, KernelConfigKey *key) {
184     *key = s;
185     return true;
186 }
187 
parseKernelConfigValue(const std::string & s,KernelConfigTypedValue * kctv)188 bool parseKernelConfigValue(const std::string &s, KernelConfigTypedValue *kctv) {
189     switch (kctv->mType) {
190         case KernelConfigType::STRING:
191             kctv->mStringValue = s;
192             return true;
193         case KernelConfigType::INTEGER:
194             return parseKernelConfigInt(s, &kctv->mIntegerValue);
195         case KernelConfigType::RANGE:
196             return parseRange(s, &kctv->mRangeValue);
197         case KernelConfigType::TRISTATE:
198             return parse(s, &kctv->mTristateValue);
199     }
200 }
201 
parseKernelConfigTypedValue(const std::string & s,KernelConfigTypedValue * kctv)202 bool parseKernelConfigTypedValue(const std::string& s, KernelConfigTypedValue* kctv) {
203     if (s.size() > 1 && s[0] == '"' && s.back() == '"') {
204         kctv->mType = KernelConfigType::STRING;
205         kctv->mStringValue = s.substr(1, s.size()-2);
206         return true;
207     }
208     if (parseKernelConfigInt(s, &kctv->mIntegerValue)) {
209         kctv->mType = KernelConfigType::INTEGER;
210         return true;
211     }
212     if (parse(s, &kctv->mTristateValue)) {
213         kctv->mType = KernelConfigType::TRISTATE;
214         return true;
215     }
216     // Do not test for KernelConfigType::RANGE.
217     return false;
218 }
219 
parse(const std::string & s,Version * ver)220 bool parse(const std::string &s, Version *ver) {
221     std::vector<std::string> v = SplitString(s, '.');
222     if (v.size() != 2) {
223         return false;
224     }
225     size_t major, minor;
226     if (!ParseUint(v[0], &major)) {
227         return false;
228     }
229     if (!ParseUint(v[1], &minor)) {
230         return false;
231     }
232     *ver = Version(major, minor);
233     return true;
234 }
235 
operator <<(std::ostream & os,const Version & ver)236 std::ostream &operator<<(std::ostream &os, const Version &ver) {
237     return os << ver.majorVer << "." << ver.minorVer;
238 }
239 
240 // Helper for parsing a VersionRange object. versionParser defines how the first half
241 // (before the '-' character) of the string is parsed.
parseVersionRange(const std::string & s,VersionRange * vr,const std::function<bool (const std::string &,Version *)> & versionParser)242 static bool parseVersionRange(
243     const std::string& s, VersionRange* vr,
244     const std::function<bool(const std::string&, Version*)>& versionParser) {
245     std::vector<std::string> v = SplitString(s, '-');
246     if (v.size() != 1 && v.size() != 2) {
247         return false;
248     }
249     Version minVer;
250     if (!versionParser(v[0], &minVer)) {
251         return false;
252     }
253     if (v.size() == 1) {
254         *vr = VersionRange(minVer.majorVer, minVer.minorVer);
255     } else {
256         size_t maxMinor;
257         if (!ParseUint(v[1], &maxMinor)) {
258             return false;
259         }
260         *vr = VersionRange(minVer.majorVer, minVer.minorVer, maxMinor);
261     }
262     return true;
263 }
264 
parse(const std::string & s,VersionRange * vr)265 bool parse(const std::string& s, VersionRange* vr) {
266     bool (*versionParser)(const std::string&, Version*) = parse;
267     return parseVersionRange(s, vr, versionParser);
268 }
269 
operator <<(std::ostream & os,const VersionRange & vr)270 std::ostream &operator<<(std::ostream &os, const VersionRange &vr) {
271     if (vr.isSingleVersion()) {
272         return os << vr.minVer();
273     }
274     return os << vr.minVer() << "-" << vr.maxMinor;
275 }
276 
277 #pragma clang diagnostic push
278 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
parse(const std::string & s,VndkVersionRange * vr)279 bool parse(const std::string &s, VndkVersionRange *vr) {
280     std::vector<std::string> v = SplitString(s, '-');
281     if (v.size() != 1 && v.size() != 2) {
282         return false;
283     }
284     std::vector<std::string> minVector = SplitString(v[0], '.');
285     if (minVector.size() != 3) {
286         return false;
287     }
288     if (!ParseUint(minVector[0], &vr->sdk) ||
289         !ParseUint(minVector[1], &vr->vndk) ||
290         !ParseUint(minVector[2], &vr->patchMin)) {
291         return false;
292     }
293     if (v.size() == 1) {
294         vr->patchMax = vr->patchMin;
295         return true;
296     } else {
297         return ParseUint(v[1], &vr->patchMax);
298     }
299 }
300 
operator <<(std::ostream & os,const VndkVersionRange & vr)301 std::ostream &operator<<(std::ostream &os, const VndkVersionRange &vr) {
302     os << vr.sdk << "." << vr.vndk << "." << vr.patchMin;
303     if (!vr.isSingleVersion()) {
304         os << "-" << vr.patchMax;
305     }
306     return os;
307 }
308 #pragma clang diagnostic pop
309 
parse(const std::string & s,KernelVersion * kernelVersion)310 bool parse(const std::string &s, KernelVersion *kernelVersion) {
311     std::vector<std::string> v = SplitString(s, '.');
312     if (v.size() != 3) {
313         return false;
314     }
315     size_t version, major, minor;
316     if (!ParseUint(v[0], &version)) {
317         return false;
318     }
319     if (!ParseUint(v[1], &major)) {
320         return false;
321     }
322     if (!ParseUint(v[2], &minor)) {
323         return false;
324     }
325     *kernelVersion = KernelVersion(version, major, minor);
326     return true;
327 }
328 
operator <<(std::ostream & os,const TransportArch & ta)329 std::ostream &operator<<(std::ostream &os, const TransportArch &ta) {
330     return os << to_string(ta.transport) << to_string(ta.arch);
331 }
332 
operator <<(std::ostream & os,const KernelVersion & ver)333 std::ostream &operator<<(std::ostream &os, const KernelVersion &ver) {
334     return os << ver.version << "." << ver.majorRev << "." << ver.minorRev;
335 }
336 
operator <<(std::ostream & os,const ManifestHal & hal)337 std::ostream &operator<<(std::ostream &os, const ManifestHal &hal) {
338     return os << hal.format << "/"
339               << hal.name << "/"
340               << hal.transportArch << "/"
341               << hal.versions;
342 }
343 
expandInstances(const MatrixHal & req,const VersionRange & vr,bool brace)344 std::string expandInstances(const MatrixHal& req, const VersionRange& vr, bool brace) {
345     std::string s;
346     size_t count = 0;
347     req.forEachInstance(vr, [&](const auto& matrixInstance) {
348         if (count > 0) s += " AND ";
349         auto instance = matrixInstance.isRegex() ? matrixInstance.regexPattern()
350                                                  : matrixInstance.exactInstance();
351         switch (req.format) {
352             case HalFormat::AIDL: {
353                 s += toFQNameString(matrixInstance.interface(), instance) + " (@" +
354                      aidlVersionRangeToString(vr) + ")";
355             } break;
356             case HalFormat::HIDL:
357                 [[fallthrough]];
358             case HalFormat::NATIVE: {
359                 s += toFQNameString(vr, matrixInstance.interface(), instance);
360             } break;
361         }
362         count++;
363         return true;
364     });
365     if (count == 0) {
366         s += "@" + to_string(vr);
367     }
368     if (count >= 2 && brace) {
369         s = "(" + s + ")";
370     }
371     return s;
372 }
373 
expandInstances(const MatrixHal & req)374 std::vector<std::string> expandInstances(const MatrixHal& req) {
375     size_t count = req.instancesCount();
376     if (count == 0) {
377         return {};
378     }
379     if (count == 1) {
380         return {expandInstances(req, req.versionRanges.front(), false /* brace */)};
381     }
382     std::vector<std::string> ss;
383     for (const auto& vr : req.versionRanges) {
384         if (!ss.empty()) {
385             ss.back() += " OR";
386         }
387         ss.push_back(expandInstances(req, vr, true /* brace */));
388     }
389     return ss;
390 }
391 
operator <<(std::ostream & os,KernelSepolicyVersion ksv)392 std::ostream &operator<<(std::ostream &os, KernelSepolicyVersion ksv){
393     return os << ksv.value;
394 }
395 
parse(const std::string & s,KernelSepolicyVersion * ksv)396 bool parse(const std::string &s, KernelSepolicyVersion *ksv){
397     return ParseUint(s, &ksv->value);
398 }
399 
dump(const HalManifest & vm)400 std::string dump(const HalManifest &vm) {
401     std::ostringstream oss;
402     bool first = true;
403     for (const auto &hal : vm.getHals()) {
404         if (!first) {
405             oss << ":";
406         }
407         oss << hal;
408         first = false;
409     }
410     return oss.str();
411 }
412 
dump(const RuntimeInfo & ki,bool verbose)413 std::string dump(const RuntimeInfo& ki, bool verbose) {
414     std::ostringstream oss;
415 
416     oss << "kernel = " << ki.osName() << "/" << ki.nodeName() << "/" << ki.osRelease() << "/"
417         << ki.osVersion() << "/" << ki.hardwareId() << ";" << ki.mBootAvbVersion << "/"
418         << ki.mBootVbmetaAvbVersion << ";"
419         << "kernelSepolicyVersion = " << ki.kernelSepolicyVersion() << ";";
420 
421     if (verbose) {
422         oss << "\n\ncpu info:\n" << ki.cpuInfo();
423     }
424 
425     oss << "\n#CONFIG's loaded = " << ki.kernelConfigs().size() << ";\n";
426 
427     if (verbose) {
428         for (const auto& pair : ki.kernelConfigs()) {
429             oss << pair.first << "=" << pair.second << "\n";
430         }
431     }
432 
433     return oss.str();
434 }
435 
toFQNameString(const std::string & package,const std::string & version,const std::string & interface,const std::string & instance)436 std::string toFQNameString(const std::string& package, const std::string& version,
437                            const std::string& interface, const std::string& instance) {
438     std::stringstream ss;
439     ss << package << "@" << version;
440     if (!interface.empty()) {
441         ss << "::" << interface;
442     }
443     if (!instance.empty()) {
444         ss << "/" << instance;
445     }
446     return ss.str();
447 }
448 
toFQNameString(const std::string & package,const Version & version,const std::string & interface,const std::string & instance)449 std::string toFQNameString(const std::string& package, const Version& version,
450                            const std::string& interface, const std::string& instance) {
451     return toFQNameString(package, to_string(version), interface, instance);
452 }
453 
toFQNameString(const Version & version,const std::string & interface,const std::string & instance)454 std::string toFQNameString(const Version& version, const std::string& interface,
455                            const std::string& instance) {
456     return toFQNameString("", version, interface, instance);
457 }
458 
459 // android.hardware.foo@1.0-1::IFoo/default.
460 // Note that the format is extended to support a range of versions.
toFQNameString(const std::string & package,const VersionRange & range,const std::string & interface,const std::string & instance)461 std::string toFQNameString(const std::string& package, const VersionRange& range,
462                            const std::string& interface, const std::string& instance) {
463     return toFQNameString(package, to_string(range), interface, instance);
464 }
465 
toFQNameString(const VersionRange & range,const std::string & interface,const std::string & instance)466 std::string toFQNameString(const VersionRange& range, const std::string& interface,
467                            const std::string& instance) {
468     return toFQNameString("", range, interface, instance);
469 }
470 
toFQNameString(const std::string & interface,const std::string & instance)471 std::string toFQNameString(const std::string& interface, const std::string& instance) {
472     return interface + "/" + instance;
473 }
474 
operator <<(std::ostream & os,const FqInstance & fqInstance)475 std::ostream& operator<<(std::ostream& os, const FqInstance& fqInstance) {
476     return os << fqInstance.string();
477 }
478 
parse(const std::string & s,FqInstance * fqInstance)479 bool parse(const std::string& s, FqInstance* fqInstance) {
480     return fqInstance->setTo(s);
481 }
482 
toAidlFqnameString(const std::string & package,const std::string & interface,const std::string & instance)483 std::string toAidlFqnameString(const std::string& package, const std::string& interface,
484                                const std::string& instance) {
485     std::stringstream ss;
486     ss << package << "." << interface;
487     if (!instance.empty()) {
488         ss << "/" << instance;
489     }
490     return ss.str();
491 }
492 
aidlVersionToString(const Version & v)493 std::string aidlVersionToString(const Version& v) {
494     return to_string(v.minorVer);
495 }
parseAidlVersion(const std::string & s,Version * version)496 bool parseAidlVersion(const std::string& s, Version* version) {
497     version->majorVer = details::kFakeAidlMajorVersion;
498     return android::base::ParseUint(s, &version->minorVer);
499 }
500 
aidlVersionRangeToString(const VersionRange & vr)501 std::string aidlVersionRangeToString(const VersionRange& vr) {
502     if (vr.isSingleVersion()) {
503         return to_string(vr.minMinor);
504     }
505     return to_string(vr.minMinor) + "-" + to_string(vr.maxMinor);
506 }
507 
parseAidlVersionRange(const std::string & s,VersionRange * vr)508 bool parseAidlVersionRange(const std::string& s, VersionRange* vr) {
509     return parseVersionRange(s, vr, parseAidlVersion);
510 }
511 
parseApexName(std::string_view path)512 std::string_view parseApexName(std::string_view path) {
513     if (base::ConsumePrefix(&path, "/apex/")) {
514         return path.substr(0, path.find('/'));
515     }
516     return {};
517 }
518 
519 } // namespace vintf
520 } // namespace android
521