plist.cpp 26 KB

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