plist.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. load_string(p_string);
  426. }
  427. uint64_t PList::read_bplist_var_size_int(Ref<FileAccess> p_file, uint8_t p_size) {
  428. uint64_t pos = p_file->get_position();
  429. uint64_t ret = 0;
  430. switch (p_size) {
  431. case 1: {
  432. ret = p_file->get_8();
  433. } break;
  434. case 2: {
  435. ret = BSWAP16(p_file->get_16());
  436. } break;
  437. case 3: {
  438. ret = BSWAP32(p_file->get_32() & 0x00FFFFFF);
  439. } break;
  440. case 4: {
  441. ret = BSWAP32(p_file->get_32());
  442. } break;
  443. case 5: {
  444. ret = BSWAP64(p_file->get_64() & 0x000000FFFFFFFFFF);
  445. } break;
  446. case 6: {
  447. ret = BSWAP64(p_file->get_64() & 0x0000FFFFFFFFFFFF);
  448. } break;
  449. case 7: {
  450. ret = BSWAP64(p_file->get_64() & 0x00FFFFFFFFFFFFFF);
  451. } break;
  452. case 8: {
  453. ret = BSWAP64(p_file->get_64());
  454. } break;
  455. default: {
  456. ret = 0;
  457. }
  458. }
  459. p_file->seek(pos + p_size);
  460. return ret;
  461. }
  462. Ref<PListNode> PList::read_bplist_obj(Ref<FileAccess> p_file, uint64_t p_offset_idx) {
  463. Ref<PListNode> node;
  464. node.instantiate();
  465. uint64_t ot_off = trailer.offset_table_start + p_offset_idx * trailer.offset_size;
  466. p_file->seek(ot_off);
  467. uint64_t marker_off = read_bplist_var_size_int(p_file, trailer.offset_size);
  468. ERR_FAIL_COND_V_MSG(marker_off == 0, Ref<PListNode>(), "Invalid marker size.");
  469. p_file->seek(marker_off);
  470. uint8_t marker = p_file->get_8();
  471. uint8_t marker_type = marker & 0xF0;
  472. uint64_t marker_size = marker & 0x0F;
  473. switch (marker_type) {
  474. case 0x00: {
  475. if (marker_size == 0x00) {
  476. node->data_type = PL_NODE_TYPE_NIL;
  477. } else if (marker_size == 0x08) {
  478. node->data_type = PL_NODE_TYPE_BOOLEAN;
  479. node->data_bool = false;
  480. } else if (marker_size == 0x09) {
  481. node->data_type = PL_NODE_TYPE_BOOLEAN;
  482. node->data_bool = true;
  483. } else {
  484. ERR_FAIL_V_MSG(Ref<PListNode>(), "Invalid nil/bool marker value.");
  485. }
  486. } break;
  487. case 0x10: {
  488. node->data_type = PL_NODE_TYPE_INTEGER;
  489. node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, pow(2, marker_size)));
  490. } break;
  491. case 0x20: {
  492. node->data_type = PL_NODE_TYPE_REAL;
  493. node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, pow(2, marker_size)));
  494. } break;
  495. case 0x30: {
  496. node->data_type = PL_NODE_TYPE_DATE;
  497. node->data_int = BSWAP64(p_file->get_64());
  498. node->data_string = Time::get_singleton()->get_datetime_string_from_unix_time(node->data_real + 978307200.0).utf8();
  499. } break;
  500. case 0x40: {
  501. if (marker_size == 0x0F) {
  502. uint8_t ext = p_file->get_8() & 0xF;
  503. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  504. }
  505. node->data_type = PL_NODE_TYPE_DATA;
  506. PackedByteArray buf;
  507. buf.resize(marker_size + 1);
  508. p_file->get_buffer(reinterpret_cast<uint8_t *>(buf.ptrw()), marker_size);
  509. node->data_string = CryptoCore::b64_encode_str(buf.ptr(), buf.size()).utf8();
  510. } break;
  511. case 0x50: {
  512. if (marker_size == 0x0F) {
  513. uint8_t ext = p_file->get_8() & 0xF;
  514. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  515. }
  516. node->data_type = PL_NODE_TYPE_STRING;
  517. node->data_string.resize(marker_size + 1);
  518. p_file->get_buffer(reinterpret_cast<uint8_t *>(node->data_string.ptrw()), marker_size);
  519. } break;
  520. case 0x60: {
  521. if (marker_size == 0x0F) {
  522. uint8_t ext = p_file->get_8() & 0xF;
  523. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  524. }
  525. Char16String cs16;
  526. cs16.resize(marker_size + 1);
  527. for (uint64_t i = 0; i < marker_size; i++) {
  528. cs16[i] = BSWAP16(p_file->get_16());
  529. }
  530. node->data_type = PL_NODE_TYPE_STRING;
  531. node->data_string = String::utf16(cs16.ptr(), cs16.length()).utf8();
  532. } break;
  533. case 0x80: {
  534. node->data_type = PL_NODE_TYPE_INTEGER;
  535. node->data_int = static_cast<int64_t>(read_bplist_var_size_int(p_file, marker_size + 1));
  536. } break;
  537. case 0xA0:
  538. case 0xC0: {
  539. if (marker_size == 0x0F) {
  540. uint8_t ext = p_file->get_8() & 0xF;
  541. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  542. }
  543. uint64_t pos = p_file->get_position();
  544. node->data_type = PL_NODE_TYPE_ARRAY;
  545. for (uint64_t i = 0; i < marker_size; i++) {
  546. p_file->seek(pos + trailer.ref_size * i);
  547. uint64_t ref = read_bplist_var_size_int(p_file, trailer.ref_size);
  548. Ref<PListNode> element = read_bplist_obj(p_file, ref);
  549. ERR_FAIL_COND_V(element.is_null(), Ref<PListNode>());
  550. node->data_array.push_back(element);
  551. }
  552. } break;
  553. case 0xD0: {
  554. if (marker_size == 0x0F) {
  555. uint8_t ext = p_file->get_8() & 0xF;
  556. marker_size = read_bplist_var_size_int(p_file, pow(2, ext));
  557. }
  558. uint64_t pos = p_file->get_position();
  559. node->data_type = PL_NODE_TYPE_DICT;
  560. for (uint64_t i = 0; i < marker_size; i++) {
  561. p_file->seek(pos + trailer.ref_size * i);
  562. uint64_t key_ref = read_bplist_var_size_int(p_file, trailer.ref_size);
  563. p_file->seek(pos + trailer.ref_size * (i + marker_size));
  564. uint64_t obj_ref = read_bplist_var_size_int(p_file, trailer.ref_size);
  565. Ref<PListNode> element_key = read_bplist_obj(p_file, key_ref);
  566. ERR_FAIL_COND_V(element_key.is_null() || element_key->data_type != PL_NODE_TYPE_STRING, Ref<PListNode>());
  567. Ref<PListNode> element = read_bplist_obj(p_file, obj_ref);
  568. ERR_FAIL_COND_V(element.is_null(), Ref<PListNode>());
  569. node->data_dict[String::utf8(element_key->data_string.ptr(), element_key->data_string.length())] = element;
  570. }
  571. } break;
  572. default: {
  573. ERR_FAIL_V_MSG(Ref<PListNode>(), "Invalid marker type.");
  574. }
  575. }
  576. return node;
  577. }
  578. bool PList::load_file(const String &p_filename) {
  579. root = Ref<PListNode>();
  580. Ref<FileAccess> fb = FileAccess::open(p_filename, FileAccess::READ);
  581. if (fb.is_null()) {
  582. return false;
  583. }
  584. unsigned char magic[8];
  585. fb->get_buffer(magic, 8);
  586. if (String((const char *)magic, 8) == "bplist00") {
  587. fb->seek_end(-26);
  588. trailer.offset_size = fb->get_8();
  589. trailer.ref_size = fb->get_8();
  590. trailer.object_num = BSWAP64(fb->get_64());
  591. trailer.root_offset_idx = BSWAP64(fb->get_64());
  592. trailer.offset_table_start = BSWAP64(fb->get_64());
  593. root = read_bplist_obj(fb, trailer.root_offset_idx);
  594. return root.is_valid();
  595. } else {
  596. // Load text plist.
  597. Error err;
  598. Vector<uint8_t> array = FileAccess::get_file_as_bytes(p_filename, &err);
  599. ERR_FAIL_COND_V(err != OK, false);
  600. String ret;
  601. ret.parse_utf8((const char *)array.ptr(), array.size());
  602. return load_string(ret);
  603. }
  604. }
  605. bool PList::load_string(const String &p_string) {
  606. root = Ref<PListNode>();
  607. int pos = 0;
  608. bool in_plist = false;
  609. bool done_plist = false;
  610. List<Ref<PListNode>> stack;
  611. String key;
  612. while (pos >= 0) {
  613. int open_token_s = p_string.find("<", pos);
  614. if (open_token_s == -1) {
  615. ERR_FAIL_V_MSG(false, "PList: Unexpected end of data. No tags found.");
  616. }
  617. int open_token_e = p_string.find(">", open_token_s);
  618. pos = open_token_e;
  619. String token = p_string.substr(open_token_s + 1, open_token_e - open_token_s - 1);
  620. if (token.is_empty()) {
  621. ERR_FAIL_V_MSG(false, "PList: Invalid token name.");
  622. }
  623. String value;
  624. if (token[0] == '?' || token[0] == '!') { // Skip <?xml ... ?> and <!DOCTYPE ... >
  625. int end_token_e = p_string.find(">", open_token_s);
  626. pos = end_token_e;
  627. continue;
  628. }
  629. if (token.find("plist", 0) == 0) {
  630. in_plist = true;
  631. continue;
  632. }
  633. if (token == "/plist") {
  634. done_plist = true;
  635. break;
  636. }
  637. if (!in_plist) {
  638. ERR_FAIL_V_MSG(false, "PList: Node outside of <plist> tag.");
  639. }
  640. if (token == "dict") {
  641. if (!stack.is_empty()) {
  642. // Add subnode end enter it.
  643. Ref<PListNode> dict = PListNode::new_dict();
  644. dict->data_type = PList::PLNodeType::PL_NODE_TYPE_DICT;
  645. if (!stack.back()->get()->push_subnode(dict, key)) {
  646. ERR_FAIL_V_MSG(false, "PList: Can't push subnode, invalid parent type.");
  647. }
  648. stack.push_back(dict);
  649. } else {
  650. // Add root node.
  651. if (!root.is_null()) {
  652. ERR_FAIL_V_MSG(false, "PList: Root node already set.");
  653. }
  654. Ref<PListNode> dict = PListNode::new_dict();
  655. stack.push_back(dict);
  656. root = dict;
  657. }
  658. continue;
  659. }
  660. if (token == "/dict") {
  661. // Exit current dict.
  662. if (stack.is_empty() || stack.back()->get()->data_type != PList::PLNodeType::PL_NODE_TYPE_DICT) {
  663. ERR_FAIL_V_MSG(false, "PList: Mismatched </dict> tag.");
  664. }
  665. stack.pop_back();
  666. continue;
  667. }
  668. if (token == "array") {
  669. if (!stack.is_empty()) {
  670. // Add subnode end enter it.
  671. Ref<PListNode> arr = PListNode::new_array();
  672. if (!stack.back()->get()->push_subnode(arr, key)) {
  673. ERR_FAIL_V_MSG(false, "PList: Can't push subnode, invalid parent type.");
  674. }
  675. stack.push_back(arr);
  676. } else {
  677. // Add root node.
  678. if (!root.is_null()) {
  679. ERR_FAIL_V_MSG(false, "PList: Root node already set.");
  680. }
  681. Ref<PListNode> arr = PListNode::new_array();
  682. stack.push_back(arr);
  683. root = arr;
  684. }
  685. continue;
  686. }
  687. if (token == "/array") {
  688. // Exit current array.
  689. if (stack.is_empty() || stack.back()->get()->data_type != PList::PLNodeType::PL_NODE_TYPE_ARRAY) {
  690. ERR_FAIL_V_MSG(false, "PList: Mismatched </array> tag.");
  691. }
  692. stack.pop_back();
  693. continue;
  694. }
  695. if (token[token.length() - 1] == '/') {
  696. token = token.substr(0, token.length() - 1);
  697. } else {
  698. int end_token_s = p_string.find("</", pos);
  699. if (end_token_s == -1) {
  700. ERR_FAIL_V_MSG(false, vformat("PList: Mismatched <%s> tag.", token));
  701. }
  702. int end_token_e = p_string.find(">", end_token_s);
  703. pos = end_token_e;
  704. String end_token = p_string.substr(end_token_s + 2, end_token_e - end_token_s - 2);
  705. if (end_token != token) {
  706. ERR_FAIL_V_MSG(false, vformat("PList: Mismatched <%s> and <%s> token pair.", token, end_token));
  707. }
  708. value = p_string.substr(open_token_e + 1, end_token_s - open_token_e - 1);
  709. }
  710. if (token == "key") {
  711. key = value;
  712. } else {
  713. Ref<PListNode> var = nullptr;
  714. if (token == "true") {
  715. var = PListNode::new_bool(true);
  716. } else if (token == "false") {
  717. var = PListNode::new_bool(false);
  718. } else if (token == "integer") {
  719. var = PListNode::new_int(value.to_int());
  720. } else if (token == "real") {
  721. var = PListNode::new_real(value.to_float());
  722. } else if (token == "string") {
  723. var = PListNode::new_string(value);
  724. } else if (token == "data") {
  725. var = PListNode::new_data(value);
  726. } else if (token == "date") {
  727. var = PListNode::new_date(value);
  728. } else {
  729. ERR_FAIL_V_MSG(false, "PList: Invalid value type.");
  730. }
  731. if (stack.is_empty() || !stack.back()->get()->push_subnode(var, key)) {
  732. ERR_FAIL_V_MSG(false, "PList: Can't push subnode, invalid parent type.");
  733. }
  734. }
  735. }
  736. if (!stack.is_empty() || !done_plist) {
  737. ERR_FAIL_V_MSG(false, "PList: Unexpected end of data. Root node is not closed.");
  738. }
  739. return true;
  740. }
  741. PackedByteArray PList::save_asn1() const {
  742. if (root == nullptr) {
  743. ERR_FAIL_V_MSG(PackedByteArray(), "PList: Invalid PList, no root node.");
  744. }
  745. size_t size = root->get_asn1_size(1);
  746. uint8_t len_octets = 0;
  747. if (size < 0x80) {
  748. len_octets = 1;
  749. } else {
  750. size = root->get_asn1_size(2);
  751. if (size < 0xFFFF) {
  752. len_octets = 2;
  753. } else {
  754. size = root->get_asn1_size(3);
  755. if (size < 0xFFFFFF) {
  756. len_octets = 3;
  757. } else {
  758. size = root->get_asn1_size(4);
  759. if (size < 0xFFFFFFFF) {
  760. len_octets = 4;
  761. } else {
  762. ERR_FAIL_V_MSG(PackedByteArray(), "PList: Data is too big for ASN.1 serializer, should be < 4 GiB.");
  763. }
  764. }
  765. }
  766. }
  767. PackedByteArray ret;
  768. if (!root->store_asn1(ret, len_octets)) {
  769. ERR_FAIL_V_MSG(PackedByteArray(), "PList: ASN.1 serializer error.");
  770. }
  771. return ret;
  772. }
  773. String PList::save_text() const {
  774. if (root == nullptr) {
  775. ERR_FAIL_V_MSG(String(), "PList: Invalid PList, no root node.");
  776. }
  777. String ret;
  778. ret += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  779. ret += "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
  780. ret += "<plist version=\"1.0\">\n";
  781. root->store_text(ret, 0);
  782. ret += "</plist>\n\n";
  783. return ret;
  784. }
  785. Ref<PListNode> PList::get_root() {
  786. return root;
  787. }