plist.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /**************************************************************************/
  2. /* plist.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "plist.h"
  31. #include "core/crypto/crypto_core.h"
  32. #include "core/os/time.h"
  33. PList::PLNodeType PListNode::get_type() const {
  34. return data_type;
  35. }
  36. Variant PListNode::get_value() const {
  37. switch (data_type) {
  38. case PList::PL_NODE_TYPE_NIL: {
  39. return Variant();
  40. } break;
  41. case PList::PL_NODE_TYPE_STRING: {
  42. return String::utf8(data_string.get_data());
  43. } break;
  44. case PList::PL_NODE_TYPE_ARRAY: {
  45. Array arr;
  46. for (const Ref<PListNode> &E : data_array) {
  47. arr.push_back(E);
  48. }
  49. return arr;
  50. } break;
  51. case PList::PL_NODE_TYPE_DICT: {
  52. Dictionary dict;
  53. for (const KeyValue<String, Ref<PListNode>> &E : data_dict) {
  54. dict[E.key] = E.value;
  55. }
  56. return dict;
  57. } break;
  58. case PList::PL_NODE_TYPE_BOOLEAN: {
  59. return data_bool;
  60. } break;
  61. case PList::PL_NODE_TYPE_INTEGER: {
  62. return data_int;
  63. } break;
  64. case PList::PL_NODE_TYPE_REAL: {
  65. return data_real;
  66. } break;
  67. case PList::PL_NODE_TYPE_DATA: {
  68. int strlen = data_string.length();
  69. size_t arr_len = 0;
  70. Vector<uint8_t> buf;
  71. {
  72. buf.resize(strlen / 4 * 3 + 1);
  73. uint8_t *w = buf.ptrw();
  74. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)data_string.get_data(), strlen) != OK, Vector<uint8_t>());
  75. }
  76. buf.resize(arr_len);
  77. return buf;
  78. } break;
  79. case PList::PL_NODE_TYPE_DATE: {
  80. return String(data_string.get_data());
  81. } break;
  82. }
  83. return Variant();
  84. }
  85. Ref<PListNode> PListNode::new_node(const Variant &p_value) {
  86. Ref<PListNode> node;
  87. node.instantiate();
  88. switch (p_value.get_type()) {
  89. case Variant::NIL: {
  90. node->data_type = PList::PL_NODE_TYPE_NIL;
  91. } break;
  92. case Variant::BOOL: {
  93. node->data_type = PList::PL_NODE_TYPE_BOOLEAN;
  94. node->data_bool = p_value;
  95. } break;
  96. case Variant::INT: {
  97. node->data_type = PList::PL_NODE_TYPE_INTEGER;
  98. node->data_int = p_value;
  99. } break;
  100. case Variant::FLOAT: {
  101. node->data_type = PList::PL_NODE_TYPE_REAL;
  102. node->data_real = p_value;
  103. } break;
  104. case Variant::STRING_NAME:
  105. case Variant::STRING: {
  106. node->data_type = PList::PL_NODE_TYPE_STRING;
  107. node->data_string = p_value.operator String().utf8();
  108. } break;
  109. case Variant::DICTIONARY: {
  110. node->data_type = PList::PL_NODE_TYPE_DICT;
  111. Dictionary dict = p_value;
  112. const Variant *next = dict.next(nullptr);
  113. while (next) {
  114. Ref<PListNode> sub_node = dict[*next];
  115. ERR_FAIL_COND_V_MSG(sub_node.is_null(), Ref<PListNode>(), "Invalid dictionary element, should be PListNode.");
  116. node->data_dict[*next] = sub_node;
  117. next = dict.next(next);
  118. }
  119. } break;
  120. case Variant::ARRAY: {
  121. node->data_type = PList::PL_NODE_TYPE_ARRAY;
  122. Array ar = p_value;
  123. for (int i = 0; i < ar.size(); i++) {
  124. Ref<PListNode> sub_node = ar[i];
  125. ERR_FAIL_COND_V_MSG(sub_node.is_null(), Ref<PListNode>(), "Invalid array element, should be PListNode.");
  126. node->data_array.push_back(sub_node);
  127. }
  128. } break;
  129. case Variant::PACKED_BYTE_ARRAY: {
  130. node->data_type = PList::PL_NODE_TYPE_DATA;
  131. PackedByteArray buf = p_value;
  132. node->data_string = CryptoCore::b64_encode_str(buf.ptr(), buf.size()).utf8();
  133. } break;
  134. default: {
  135. ERR_FAIL_V_MSG(Ref<PListNode>(), "Unsupported data type.");
  136. } break;
  137. }
  138. return node;
  139. }
  140. Ref<PListNode> PListNode::new_array() {
  141. Ref<PListNode> node = memnew(PListNode());
  142. ERR_FAIL_COND_V(node.is_null(), Ref<PListNode>());
  143. node->data_type = PList::PLNodeType::PL_NODE_TYPE_ARRAY;
  144. return node;
  145. }
  146. Ref<PListNode> PListNode::new_dict() {
  147. Ref<PListNode> node = memnew(PListNode());
  148. ERR_FAIL_COND_V(node.is_null(), Ref<PListNode>());
  149. node->data_type = PList::PLNodeType::PL_NODE_TYPE_DICT;
  150. return node;
  151. }
  152. Ref<PListNode> PListNode::new_string(const String &p_string) {
  153. Ref<PListNode> node = memnew(PListNode());
  154. ERR_FAIL_COND_V(node.is_null(), Ref<PListNode>());
  155. node->data_type = PList::PLNodeType::PL_NODE_TYPE_STRING;
  156. node->data_string = p_string.utf8();
  157. return node;
  158. }
  159. Ref<PListNode> PListNode::new_data(const String &p_string) {
  160. Ref<PListNode> node = memnew(PListNode());
  161. ERR_FAIL_COND_V(node.is_null(), Ref<PListNode>());
  162. node->data_type = PList::PLNodeType::PL_NODE_TYPE_DATA;
  163. node->data_string = p_string.utf8();
  164. return node;
  165. }
  166. Ref<PListNode> PListNode::new_date(const String &p_string) {
  167. Ref<PListNode> node = memnew(PListNode());
  168. ERR_FAIL_COND_V(node.is_null(), Ref<PListNode>());
  169. node->data_type = PList::PLNodeType::PL_NODE_TYPE_DATE;
  170. node->data_string = p_string.utf8();
  171. node->data_real = (double)Time::get_singleton()->get_unix_time_from_datetime_string(p_string) - 978307200.0;
  172. return node;
  173. }
  174. Ref<PListNode> PListNode::new_bool(bool p_bool) {
  175. Ref<PListNode> node = memnew(PListNode());
  176. ERR_FAIL_COND_V(node.is_null(), Ref<PListNode>());
  177. node->data_type = PList::PLNodeType::PL_NODE_TYPE_BOOLEAN;
  178. node->data_bool = p_bool;
  179. return node;
  180. }
  181. Ref<PListNode> PListNode::new_int(int64_t p_int) {
  182. Ref<PListNode> node = memnew(PListNode());
  183. ERR_FAIL_COND_V(node.is_null(), Ref<PListNode>());
  184. node->data_type = PList::PLNodeType::PL_NODE_TYPE_INTEGER;
  185. node->data_int = p_int;
  186. return node;
  187. }
  188. Ref<PListNode> PListNode::new_real(double p_real) {
  189. Ref<PListNode> node = memnew(PListNode());
  190. ERR_FAIL_COND_V(node.is_null(), Ref<PListNode>());
  191. node->data_type = PList::PLNodeType::PL_NODE_TYPE_REAL;
  192. node->data_real = p_real;
  193. return node;
  194. }
  195. bool PListNode::push_subnode(const Ref<PListNode> &p_node, const String &p_key) {
  196. ERR_FAIL_COND_V(p_node.is_null(), false);
  197. if (data_type == PList::PLNodeType::PL_NODE_TYPE_DICT) {
  198. ERR_FAIL_COND_V(p_key.is_empty(), false);
  199. ERR_FAIL_COND_V(data_dict.has(p_key), false);
  200. data_dict[p_key] = p_node;
  201. return true;
  202. } else if (data_type == PList::PLNodeType::PL_NODE_TYPE_ARRAY) {
  203. data_array.push_back(p_node);
  204. return true;
  205. } else {
  206. ERR_FAIL_V_MSG(false, "PList: Invalid parent node type, should be DICT or ARRAY.");
  207. }
  208. }
  209. size_t PListNode::get_asn1_size(uint8_t p_len_octets) const {
  210. // Get size of all data, excluding type and size information.
  211. switch (data_type) {
  212. case PList::PLNodeType::PL_NODE_TYPE_NIL: {
  213. return 0;
  214. } break;
  215. case PList::PLNodeType::PL_NODE_TYPE_DATA:
  216. case PList::PLNodeType::PL_NODE_TYPE_DATE: {
  217. ERR_FAIL_V_MSG(0, "PList: DATE and DATA nodes are not supported by ASN.1 serialization.");
  218. } break;
  219. case PList::PLNodeType::PL_NODE_TYPE_STRING: {
  220. return data_string.length();
  221. } break;
  222. case PList::PLNodeType::PL_NODE_TYPE_BOOLEAN: {
  223. return 1;
  224. } break;
  225. case PList::PLNodeType::PL_NODE_TYPE_INTEGER:
  226. case PList::PLNodeType::PL_NODE_TYPE_REAL: {
  227. return 4;
  228. } break;
  229. case PList::PLNodeType::PL_NODE_TYPE_ARRAY: {
  230. size_t size = 0;
  231. for (int i = 0; i < data_array.size(); i++) {
  232. size += 1 + _asn1_size_len(p_len_octets) + data_array[i]->get_asn1_size(p_len_octets);
  233. }
  234. return size;
  235. } break;
  236. case PList::PLNodeType::PL_NODE_TYPE_DICT: {
  237. size_t size = 0;
  238. for (const KeyValue<String, Ref<PListNode>> &E : data_dict) {
  239. size += 1 + _asn1_size_len(p_len_octets); // Sequence.
  240. size += 1 + _asn1_size_len(p_len_octets) + E.key.utf8().length(); //Key.
  241. size += 1 + _asn1_size_len(p_len_octets) + E.value->get_asn1_size(p_len_octets); // Value.
  242. }
  243. return size;
  244. } break;
  245. default: {
  246. return 0;
  247. } break;
  248. }
  249. }
  250. int PListNode::_asn1_size_len(uint8_t p_len_octets) {
  251. if (p_len_octets > 1) {
  252. return p_len_octets + 1;
  253. } else {
  254. return 1;
  255. }
  256. }
  257. void PListNode::store_asn1_size(PackedByteArray &p_stream, uint8_t p_len_octets) const {
  258. uint32_t size = get_asn1_size(p_len_octets);
  259. if (p_len_octets > 1) {
  260. p_stream.push_back(0x80 + p_len_octets);
  261. }
  262. for (int i = p_len_octets - 1; i >= 0; i--) {
  263. uint8_t x = (size >> i * 8) & 0xFF;
  264. p_stream.push_back(x);
  265. }
  266. }
  267. bool PListNode::store_asn1(PackedByteArray &p_stream, uint8_t p_len_octets) const {
  268. // Convert to binary ASN1 stream.
  269. bool valid = true;
  270. switch (data_type) {
  271. case PList::PLNodeType::PL_NODE_TYPE_NIL: {
  272. // Nothing to store.
  273. } break;
  274. case PList::PLNodeType::PL_NODE_TYPE_DATE:
  275. case PList::PLNodeType::PL_NODE_TYPE_DATA: {
  276. ERR_FAIL_V_MSG(false, "PList: DATE and DATA nodes are not supported by ASN.1 serialization.");
  277. } break;
  278. case PList::PLNodeType::PL_NODE_TYPE_STRING: {
  279. p_stream.push_back(0x0C);
  280. store_asn1_size(p_stream, p_len_octets);
  281. for (int i = 0; i < data_string.size(); i++) {
  282. p_stream.push_back(data_string[i]);
  283. }
  284. } break;
  285. case PList::PLNodeType::PL_NODE_TYPE_BOOLEAN: {
  286. p_stream.push_back(0x01);
  287. store_asn1_size(p_stream, p_len_octets);
  288. if (data_bool) {
  289. p_stream.push_back(0x01);
  290. } else {
  291. p_stream.push_back(0x00);
  292. }
  293. } break;
  294. case PList::PLNodeType::PL_NODE_TYPE_INTEGER: {
  295. p_stream.push_back(0x02);
  296. store_asn1_size(p_stream, p_len_octets);
  297. for (int i = 4; i >= 0; i--) {
  298. uint8_t x = (data_int >> i * 8) & 0xFF;
  299. p_stream.push_back(x);
  300. }
  301. } break;
  302. case PList::PLNodeType::PL_NODE_TYPE_REAL: {
  303. p_stream.push_back(0x03);
  304. store_asn1_size(p_stream, p_len_octets);
  305. for (int i = 4; i >= 0; i--) {
  306. uint8_t x = (data_int >> i * 8) & 0xFF;
  307. p_stream.push_back(x);
  308. }
  309. } break;
  310. case PList::PLNodeType::PL_NODE_TYPE_ARRAY: {
  311. p_stream.push_back(0x30); // Sequence.
  312. store_asn1_size(p_stream, p_len_octets);
  313. for (int i = 0; i < data_array.size(); i++) {
  314. valid = valid && data_array[i]->store_asn1(p_stream, p_len_octets);
  315. }
  316. } break;
  317. case PList::PLNodeType::PL_NODE_TYPE_DICT: {
  318. p_stream.push_back(0x31); // Set.
  319. store_asn1_size(p_stream, p_len_octets);
  320. for (const KeyValue<String, Ref<PListNode>> &E : data_dict) {
  321. CharString cs = E.key.utf8();
  322. uint32_t size = cs.length();
  323. // Sequence.
  324. p_stream.push_back(0x30);
  325. uint32_t seq_size = 2 * (1 + _asn1_size_len(p_len_octets)) + size + E.value->get_asn1_size(p_len_octets);
  326. if (p_len_octets > 1) {
  327. p_stream.push_back(0x80 + p_len_octets);
  328. }
  329. for (int i = p_len_octets - 1; i >= 0; i--) {
  330. uint8_t x = (seq_size >> i * 8) & 0xFF;
  331. p_stream.push_back(x);
  332. }
  333. // Key.
  334. p_stream.push_back(0x0C);
  335. if (p_len_octets > 1) {
  336. p_stream.push_back(0x80 + p_len_octets);
  337. }
  338. for (int i = p_len_octets - 1; i >= 0; i--) {
  339. uint8_t x = (size >> i * 8) & 0xFF;
  340. p_stream.push_back(x);
  341. }
  342. for (uint32_t i = 0; i < size; i++) {
  343. p_stream.push_back(cs[i]);
  344. }
  345. // Value.
  346. valid = valid && E.value->store_asn1(p_stream, p_len_octets);
  347. }
  348. } break;
  349. }
  350. return valid;
  351. }
  352. void PListNode::store_text(String &p_stream, uint8_t p_indent) const {
  353. // Convert to text XML stream.
  354. switch (data_type) {
  355. case PList::PLNodeType::PL_NODE_TYPE_NIL: {
  356. // Nothing to store.
  357. } break;
  358. case PList::PLNodeType::PL_NODE_TYPE_DATA: {
  359. p_stream += String("\t").repeat(p_indent);
  360. p_stream += "<data>\n";
  361. p_stream += String("\t").repeat(p_indent);
  362. // Data should be Base64 (i.e. ASCII only).
  363. p_stream += String::ascii(data_string) + "\n";
  364. p_stream += String("\t").repeat(p_indent);
  365. p_stream += "</data>\n";
  366. } break;
  367. case PList::PLNodeType::PL_NODE_TYPE_DATE: {
  368. p_stream += String("\t").repeat(p_indent);
  369. p_stream += "<date>";
  370. // Data should be ISO 8601 (i.e. ASCII only).
  371. p_stream += String::ascii(data_string);
  372. p_stream += "</date>\n";
  373. } break;
  374. case PList::PLNodeType::PL_NODE_TYPE_STRING: {
  375. p_stream += String("\t").repeat(p_indent);
  376. p_stream += "<string>";
  377. p_stream += String::utf8(data_string);
  378. p_stream += "</string>\n";
  379. } break;
  380. case PList::PLNodeType::PL_NODE_TYPE_BOOLEAN: {
  381. p_stream += String("\t").repeat(p_indent);
  382. if (data_bool) {
  383. p_stream += "<true/>\n";
  384. } else {
  385. p_stream += "<false/>\n";
  386. }
  387. } break;
  388. case PList::PLNodeType::PL_NODE_TYPE_INTEGER: {
  389. p_stream += String("\t").repeat(p_indent);
  390. p_stream += "<integer>";
  391. p_stream += itos(data_int);
  392. p_stream += "</integer>\n";
  393. } break;
  394. case PList::PLNodeType::PL_NODE_TYPE_REAL: {
  395. p_stream += String("\t").repeat(p_indent);
  396. p_stream += "<real>";
  397. p_stream += rtos(data_real);
  398. p_stream += "</real>\n";
  399. } break;
  400. case PList::PLNodeType::PL_NODE_TYPE_ARRAY: {
  401. p_stream += String("\t").repeat(p_indent);
  402. p_stream += "<array>\n";
  403. for (int i = 0; i < data_array.size(); i++) {
  404. data_array[i]->store_text(p_stream, p_indent + 1);
  405. }
  406. p_stream += String("\t").repeat(p_indent);
  407. p_stream += "</array>\n";
  408. } break;
  409. case PList::PLNodeType::PL_NODE_TYPE_DICT: {
  410. p_stream += String("\t").repeat(p_indent);
  411. p_stream += "<dict>\n";
  412. for (const KeyValue<String, Ref<PListNode>> &E : data_dict) {
  413. p_stream += String("\t").repeat(p_indent + 1);
  414. p_stream += "<key>";
  415. p_stream += E.key;
  416. p_stream += "</key>\n";
  417. E.value->store_text(p_stream, p_indent + 1);
  418. }
  419. p_stream += String("\t").repeat(p_indent);
  420. p_stream += "</dict>\n";
  421. } break;
  422. }
  423. }
  424. /*************************************************************************/
  425. PList::PList() {
  426. root = PListNode::new_dict();
  427. }
  428. PList::PList(const String &p_string) {
  429. String err_str;
  430. bool ok = load_string(p_string, err_str);
  431. ERR_FAIL_COND_MSG(!ok, vformat("PList: %s.", err_str));
  432. }
  433. uint64_t PList::read_bplist_var_size_int(Ref<FileAccess> p_file, uint8_t p_size) {
  434. uint64_t pos = p_file->get_position();
  435. uint64_t ret = 0;
  436. switch (p_size) {
  437. case 1: {
  438. ret = p_file->get_8();
  439. } break;
  440. case 2: {
  441. ret = BSWAP16(p_file->get_16());
  442. } break;
  443. case 3: {
  444. ret = BSWAP32(p_file->get_32() & 0x00FFFFFF);
  445. } break;
  446. case 4: {
  447. ret = BSWAP32(p_file->get_32());
  448. } break;
  449. case 5: {
  450. ret = BSWAP64(p_file->get_64() & 0x000000FFFFFFFFFF);
  451. } break;
  452. case 6: {
  453. ret = BSWAP64(p_file->get_64() & 0x0000FFFFFFFFFFFF);
  454. } break;
  455. case 7: {
  456. ret = BSWAP64(p_file->get_64() & 0x00FFFFFFFFFFFFFF);
  457. } break;
  458. case 8: {
  459. ret = BSWAP64(p_file->get_64());
  460. } break;
  461. default: {
  462. ret = 0;
  463. }
  464. }
  465. p_file->seek(pos + p_size);
  466. return ret;
  467. }
  468. Ref<PListNode> PList::read_bplist_obj(Ref<FileAccess> p_file, uint64_t p_offset_idx) {
  469. Ref<PListNode> node;
  470. node.instantiate();
  471. uint64_t ot_off = trailer.offset_table_start + p_offset_idx * trailer.offset_size;
  472. p_file->seek(ot_off);
  473. uint64_t marker_off = read_bplist_var_size_int(p_file, trailer.offset_size);
  474. ERR_FAIL_COND_V_MSG(marker_off == 0, Ref<PListNode>(), "Invalid marker size.");
  475. p_file->seek(marker_off);
  476. uint8_t marker = p_file->get_8();
  477. uint8_t marker_type = marker & 0xF0;
  478. uint64_t marker_size = marker & 0x0F;
  479. switch (marker_type) {
  480. case 0x00: {
  481. if (marker_size == 0x00) {
  482. node->data_type = PL_NODE_TYPE_NIL;
  483. } else if (marker_size == 0x08) {
  484. node->data_type = PL_NODE_TYPE_BOOLEAN;
  485. node->data_bool = false;
  486. } else if (marker_size == 0x09) {
  487. node->data_type = PL_NODE_TYPE_BOOLEAN;
  488. node->data_bool = true;
  489. } else {
  490. ERR_FAIL_V_MSG(Ref<PListNode>(), "Invalid nil/bool marker value.");
  491. }
  492. } break;
  493. case 0x10: {
  494. node->data_type = PL_NODE_TYPE_INTEGER;
  495. node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, pow(2, marker_size)));
  496. } break;
  497. case 0x20: {
  498. node->data_type = PL_NODE_TYPE_REAL;
  499. node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, pow(2, marker_size)));
  500. } break;
  501. case 0x30: {
  502. node->data_type = PL_NODE_TYPE_DATE;
  503. node->data_int = BSWAP64(p_file->get_64());
  504. node->data_string = Time::get_singleton()->get_datetime_string_from_unix_time(node->data_real + 978307200.0).utf8();
  505. } break;
  506. case 0x40: {
  507. if (marker_size == 0x0F) {
  508. uint8_t ext = p_file->get_8() & 0xF;
  509. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  510. }
  511. node->data_type = PL_NODE_TYPE_DATA;
  512. PackedByteArray buf;
  513. buf.resize(marker_size + 1);
  514. p_file->get_buffer(reinterpret_cast<uint8_t *>(buf.ptrw()), marker_size);
  515. node->data_string = CryptoCore::b64_encode_str(buf.ptr(), buf.size()).utf8();
  516. } break;
  517. case 0x50: {
  518. if (marker_size == 0x0F) {
  519. uint8_t ext = p_file->get_8() & 0xF;
  520. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  521. }
  522. node->data_type = PL_NODE_TYPE_STRING;
  523. node->data_string.resize(marker_size + 1);
  524. p_file->get_buffer(reinterpret_cast<uint8_t *>(node->data_string.ptrw()), marker_size);
  525. } break;
  526. case 0x60: {
  527. if (marker_size == 0x0F) {
  528. uint8_t ext = p_file->get_8() & 0xF;
  529. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  530. }
  531. Char16String cs16;
  532. cs16.resize(marker_size + 1);
  533. for (uint64_t i = 0; i < marker_size; i++) {
  534. cs16[i] = BSWAP16(p_file->get_16());
  535. }
  536. node->data_type = PL_NODE_TYPE_STRING;
  537. node->data_string = String::utf16(cs16.ptr(), cs16.length()).utf8();
  538. } break;
  539. case 0x80: {
  540. node->data_type = PL_NODE_TYPE_INTEGER;
  541. node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, marker_size + 1));
  542. } break;
  543. case 0xA0:
  544. case 0xC0: {
  545. if (marker_size == 0x0F) {
  546. uint8_t ext = p_file->get_8() & 0xF;
  547. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  548. }
  549. uint64_t pos = p_file->get_position();
  550. node->data_type = PL_NODE_TYPE_ARRAY;
  551. for (uint64_t i = 0; i < marker_size; i++) {
  552. p_file->seek(pos + trailer.ref_size * i);
  553. uint64_t ref = read_bplist_var_size_int(p_file, trailer.ref_size);
  554. Ref<PListNode> element = read_bplist_obj(p_file, ref);
  555. ERR_FAIL_COND_V(element.is_null(), Ref<PListNode>());
  556. node->data_array.push_back(element);
  557. }
  558. } break;
  559. case 0xD0: {
  560. if (marker_size == 0x0F) {
  561. uint8_t ext = p_file->get_8() & 0xF;
  562. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  563. }
  564. uint64_t pos = p_file->get_position();
  565. node->data_type = PL_NODE_TYPE_DICT;
  566. for (uint64_t i = 0; i < marker_size; i++) {
  567. p_file->seek(pos + trailer.ref_size * i);
  568. uint64_t key_ref = read_bplist_var_size_int(p_file, trailer.ref_size);
  569. p_file->seek(pos + trailer.ref_size * (i + marker_size));
  570. uint64_t obj_ref = read_bplist_var_size_int(p_file, trailer.ref_size);
  571. Ref<PListNode> element_key = read_bplist_obj(p_file, key_ref);
  572. ERR_FAIL_COND_V(element_key.is_null() || element_key->data_type != PL_NODE_TYPE_STRING, Ref<PListNode>());
  573. Ref<PListNode> element = read_bplist_obj(p_file, obj_ref);
  574. ERR_FAIL_COND_V(element.is_null(), Ref<PListNode>());
  575. node->data_dict[String::utf8(element_key->data_string.ptr(), element_key->data_string.length())] = element;
  576. }
  577. } break;
  578. default: {
  579. ERR_FAIL_V_MSG(Ref<PListNode>(), "Invalid marker type.");
  580. }
  581. }
  582. return node;
  583. }
  584. bool PList::load_file(const String &p_filename) {
  585. root = Ref<PListNode>();
  586. Ref<FileAccess> fb = FileAccess::open(p_filename, FileAccess::READ);
  587. if (fb.is_null()) {
  588. return false;
  589. }
  590. unsigned char magic[8];
  591. fb->get_buffer(magic, 8);
  592. if (String::ascii(Span((const char *)magic, 8)) == "bplist00") {
  593. fb->seek_end(-26);
  594. trailer.offset_size = fb->get_8();
  595. trailer.ref_size = fb->get_8();
  596. trailer.object_num = BSWAP64(fb->get_64());
  597. trailer.root_offset_idx = BSWAP64(fb->get_64());
  598. trailer.offset_table_start = BSWAP64(fb->get_64());
  599. root = read_bplist_obj(fb, trailer.root_offset_idx);
  600. return root.is_valid();
  601. } else {
  602. // Load text plist.
  603. Error err;
  604. Vector<uint8_t> array = FileAccess::get_file_as_bytes(p_filename, &err);
  605. ERR_FAIL_COND_V(err != OK, false);
  606. String err_str;
  607. bool ok = load_string(String::utf8((const char *)array.ptr(), array.size()), err_str);
  608. ERR_FAIL_COND_V_MSG(!ok, false, "PList: " + err_str);
  609. return true;
  610. }
  611. }
  612. bool PList::load_string(const String &p_string, String &r_err_out) {
  613. root = Ref<PListNode>();
  614. int pos = 0;
  615. bool in_plist = false;
  616. bool done_plist = false;
  617. List<Ref<PListNode>> stack;
  618. String key;
  619. while (pos >= 0) {
  620. int open_token_s = p_string.find_char('<', pos);
  621. if (open_token_s == -1) {
  622. r_err_out = "Unexpected end of data. No tags found.";
  623. return false;
  624. }
  625. int open_token_e = p_string.find_char('>', open_token_s);
  626. pos = open_token_e;
  627. String token = p_string.substr(open_token_s + 1, open_token_e - open_token_s - 1);
  628. if (token.is_empty()) {
  629. r_err_out = "Invalid token name.";
  630. return false;
  631. }
  632. String value;
  633. if (token[0] == '?' || token[0] == '!') { // Skip <?xml ... ?> and <!DOCTYPE ... >
  634. int end_token_e = p_string.find_char('>', open_token_s);
  635. pos = end_token_e;
  636. continue;
  637. }
  638. if (token.find("plist", 0) == 0) {
  639. in_plist = true;
  640. continue;
  641. }
  642. if (token == "/plist") {
  643. done_plist = true;
  644. break;
  645. }
  646. if (!in_plist) {
  647. r_err_out = "Node outside of <plist> tag.";
  648. return false;
  649. }
  650. if (token == "dict") {
  651. if (!stack.is_empty()) {
  652. // Add subnode end enter it.
  653. Ref<PListNode> dict = PListNode::new_dict();
  654. dict->data_type = PList::PLNodeType::PL_NODE_TYPE_DICT;
  655. if (!stack.back()->get()->push_subnode(dict, key)) {
  656. r_err_out = "Can't push subnode, invalid parent type.";
  657. return false;
  658. }
  659. stack.push_back(dict);
  660. } else {
  661. // Add root node.
  662. if (root.is_valid()) {
  663. r_err_out = "Root node already set.";
  664. return false;
  665. }
  666. Ref<PListNode> dict = PListNode::new_dict();
  667. stack.push_back(dict);
  668. root = dict;
  669. }
  670. continue;
  671. }
  672. if (token == "/dict") {
  673. // Exit current dict.
  674. if (stack.is_empty() || stack.back()->get()->data_type != PList::PLNodeType::PL_NODE_TYPE_DICT) {
  675. r_err_out = "Mismatched </dict> tag.";
  676. return false;
  677. }
  678. stack.pop_back();
  679. continue;
  680. }
  681. if (token == "array") {
  682. if (!stack.is_empty()) {
  683. // Add subnode end enter it.
  684. Ref<PListNode> arr = PListNode::new_array();
  685. if (!stack.back()->get()->push_subnode(arr, key)) {
  686. r_err_out = "Can't push subnode, invalid parent type.";
  687. return false;
  688. }
  689. stack.push_back(arr);
  690. } else {
  691. // Add root node.
  692. if (root.is_valid()) {
  693. r_err_out = "Root node already set.";
  694. return false;
  695. }
  696. Ref<PListNode> arr = PListNode::new_array();
  697. stack.push_back(arr);
  698. root = arr;
  699. }
  700. continue;
  701. }
  702. if (token == "/array") {
  703. // Exit current array.
  704. if (stack.is_empty() || stack.back()->get()->data_type != PList::PLNodeType::PL_NODE_TYPE_ARRAY) {
  705. r_err_out = "Mismatched </array> tag.";
  706. return false;
  707. }
  708. stack.pop_back();
  709. continue;
  710. }
  711. if (token[token.length() - 1] == '/') {
  712. token = token.substr(0, token.length() - 1);
  713. } else {
  714. int end_token_s = p_string.find("</", pos);
  715. if (end_token_s == -1) {
  716. r_err_out = vformat("Mismatched <%s> tag.", token);
  717. return false;
  718. }
  719. int end_token_e = p_string.find_char('>', end_token_s);
  720. pos = end_token_e;
  721. String end_token = p_string.substr(end_token_s + 2, end_token_e - end_token_s - 2);
  722. if (end_token != token) {
  723. r_err_out = vformat("Mismatched <%s> and <%s> token pair.", token, end_token);
  724. return false;
  725. }
  726. value = p_string.substr(open_token_e + 1, end_token_s - open_token_e - 1);
  727. }
  728. if (token == "key") {
  729. key = value;
  730. } else {
  731. Ref<PListNode> var = nullptr;
  732. if (token == "true") {
  733. var = PListNode::new_bool(true);
  734. } else if (token == "false") {
  735. var = PListNode::new_bool(false);
  736. } else if (token == "integer") {
  737. var = PListNode::new_int(value.to_int());
  738. } else if (token == "real") {
  739. var = PListNode::new_real(value.to_float());
  740. } else if (token == "string") {
  741. var = PListNode::new_string(value);
  742. } else if (token == "data") {
  743. var = PListNode::new_data(value);
  744. } else if (token == "date") {
  745. var = PListNode::new_date(value);
  746. } else {
  747. r_err_out = vformat("Invalid value type: %s.", token);
  748. return false;
  749. }
  750. if (stack.is_empty() || !stack.back()->get()->push_subnode(var, key)) {
  751. r_err_out = "Can't push subnode, invalid parent type.";
  752. return false;
  753. }
  754. }
  755. }
  756. if (!stack.is_empty() || !done_plist) {
  757. r_err_out = "Unexpected end of data. Root node is not closed.";
  758. return false;
  759. }
  760. return true;
  761. }
  762. PackedByteArray PList::save_asn1() const {
  763. if (root.is_null()) {
  764. ERR_FAIL_V_MSG(PackedByteArray(), "PList: Invalid PList, no root node.");
  765. }
  766. size_t size = root->get_asn1_size(1);
  767. uint8_t len_octets = 0;
  768. if (size < 0x80) {
  769. len_octets = 1;
  770. } else {
  771. size = root->get_asn1_size(2);
  772. if (size < 0xFFFF) {
  773. len_octets = 2;
  774. } else {
  775. size = root->get_asn1_size(3);
  776. if (size < 0xFFFFFF) {
  777. len_octets = 3;
  778. } else {
  779. size = root->get_asn1_size(4);
  780. if (size < 0xFFFFFFFF) {
  781. len_octets = 4;
  782. } else {
  783. ERR_FAIL_V_MSG(PackedByteArray(), "PList: Data is too big for ASN.1 serializer, should be < 4 GiB.");
  784. }
  785. }
  786. }
  787. }
  788. PackedByteArray ret;
  789. if (!root->store_asn1(ret, len_octets)) {
  790. ERR_FAIL_V_MSG(PackedByteArray(), "PList: ASN.1 serializer error.");
  791. }
  792. return ret;
  793. }
  794. String PList::save_text() const {
  795. if (root.is_null()) {
  796. ERR_FAIL_V_MSG(String(), "PList: Invalid PList, no root node.");
  797. }
  798. String ret;
  799. ret += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  800. ret += "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
  801. ret += "<plist version=\"1.0\">\n";
  802. root->store_text(ret, 0);
  803. ret += "</plist>\n\n";
  804. return ret;
  805. }
  806. Ref<PListNode> PList::get_root() {
  807. return root;
  808. }