parse_string.cpp 15 KB

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