Debian Codegraph @

core/io/plist.cpp

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