Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

[macOS] Fix ASN.1 size encoding.

Pāvels Nadtočajevs017a0203c410
diff --git a/core/io/plist.cpp b/core/io/plist.cpp
index 219926da156..e6974fce0f5 100644
--- a/core/io/plist.cpp
+++ b/core/io/plist.cpp
@@ -224,41 +224,83 @@ bool PListNode::push_subnode(const Ref<PListNode> &p_node, const String &p_key)
}
}
-size_t PListNode::get_asn1_size(uint8_t p_len_octets) const {
+uint8_t PListNode::_get_len_octets(size_t p_size) {
+ uint8_t len_octets = 0;
+ if (p_size <= 0xFF) {
+ len_octets = 1;
+ } else if (p_size <= 0xFFFF) {
+ len_octets = 2;
+ } else if (p_size <= 0xFFFFFF) {
+ len_octets = 3;
+ } else if (p_size <= 0xFFFFFFFF) {
+ len_octets = 4;
+ } else {
+ ERR_FAIL_V_MSG(0, "PList: Data is too big for ASN.1 serializer, should be < 4 GiB.");
+ }
+ return len_octets;
+}
+
+int PListNode::_asn1_size_len(size_t p_size) {
+ uint8_t len_octets = _get_len_octets(p_size);
+ if (len_octets > 1 || p_size >= 0x80) {
+ return len_octets + 1;
+ } else {
+ return 1;
+ }
+}
+
+void PListNode::_store_asn1_size(PackedByteArray &p_stream, size_t p_size) {
+ uint8_t len_octets = _get_len_octets(p_size);
+
+ if (len_octets > 1 || p_size >= 0x80) {
+ p_stream.push_back(0x80 + len_octets);
+ }
+ for (int i = len_octets - 1; i >= 0; i--) {
+ uint8_t x = (p_size >> i * 8) & 0xFF;
+ p_stream.push_back(x);
+ }
+}
+
+size_t PListNode::_get_asn1_size() const {
// Get size of all data, excluding type and size information.
switch (data_type) {
case PList::PLNodeType::PL_NODE_TYPE_NIL: {
- return 0;
+ return 2;
} break;
case PList::PLNodeType::PL_NODE_TYPE_DATA:
case PList::PLNodeType::PL_NODE_TYPE_DATE: {
ERR_FAIL_V_MSG(0, "PList: DATE and DATA nodes are not supported by ASN.1 serialization.");
} break;
case PList::PLNodeType::PL_NODE_TYPE_STRING: {
- return data_string.length();
+ size_t len = data_string.length();
+ len += _asn1_size_len(len) + 1;
+ return len;
} break;
case PList::PLNodeType::PL_NODE_TYPE_BOOLEAN: {
- return 1;
+ return _asn1_size_len(1) + 1 + 1;
} break;
case PList::PLNodeType::PL_NODE_TYPE_INTEGER:
case PList::PLNodeType::PL_NODE_TYPE_REAL: {
- return 4;
+ return _asn1_size_len(4) + 1 + 4;
} break;
case PList::PLNodeType::PL_NODE_TYPE_ARRAY: {
size_t size = 0;
for (int i = 0; i < data_array.size(); i++) {
- size += 1 + _asn1_size_len(p_len_octets) + data_array[i]->get_asn1_size(p_len_octets);
+ size += data_array[i]->_get_asn1_size();
}
+ size += _asn1_size_len(size) + 1;
return size;
} break;
case PList::PLNodeType::PL_NODE_TYPE_DICT: {
size_t size = 0;
-
for (const KeyValue<String, Ref<PListNode>> &E : data_dict) {
- size += 1 + _asn1_size_len(p_len_octets); // Sequence.
- size += 1 + _asn1_size_len(p_len_octets) + E.key.utf8().length(); //Key.
- size += 1 + _asn1_size_len(p_len_octets) + E.value->get_asn1_size(p_len_octets); // Value.
+ CharString cs = E.key.utf8();
+ uint32_t str_size = cs.length();
+
+ size_t seq_size = E.value->_get_asn1_size() + str_size + 1 + str_size;
+ size += 1 + _asn1_size_len(seq_size) + seq_size;
}
+ size += 1 + _asn1_size_len(size);
return size;
} break;
default: {
@@ -267,31 +309,13 @@ size_t PListNode::get_asn1_size(uint8_t p_len_octets) const {
}
}
-int PListNode::_asn1_size_len(uint8_t p_len_octets) {
- if (p_len_octets > 1) {
- return p_len_octets + 1;
- } else {
- return 1;
- }
-}
-
-void PListNode::store_asn1_size(PackedByteArray &p_stream, uint8_t p_len_octets) const {
- uint32_t size = get_asn1_size(p_len_octets);
- if (p_len_octets > 1) {
- p_stream.push_back(0x80 + p_len_octets);
- }
- for (int i = p_len_octets - 1; i >= 0; i--) {
- uint8_t x = (size >> i * 8) & 0xFF;
- p_stream.push_back(x);
- }
-}
-
-bool PListNode::store_asn1(PackedByteArray &p_stream, uint8_t p_len_octets) const {
+bool PListNode::store_asn1(PackedByteArray &p_stream) const {
// Convert to binary ASN1 stream.
bool valid = true;
switch (data_type) {
case PList::PLNodeType::PL_NODE_TYPE_NIL: {
- // Nothing to store.
+ p_stream.push_back(0x05);
+ p_stream.push_back(0x00);
} break;
case PList::PLNodeType::PL_NODE_TYPE_DATE:
case PList::PLNodeType::PL_NODE_TYPE_DATA: {
@@ -299,23 +323,23 @@ bool PListNode::store_asn1(PackedByteArray &p_stream, uint8_t p_len_octets) cons
} break;
case PList::PLNodeType::PL_NODE_TYPE_STRING: {
p_stream.push_back(0x0C);
- store_asn1_size(p_stream, p_len_octets);
+ _store_asn1_size(p_stream, data_string.size());
for (int i = 0; i < data_string.size(); i++) {
p_stream.push_back(data_string[i]);
}
} break;
case PList::PLNodeType::PL_NODE_TYPE_BOOLEAN: {
p_stream.push_back(0x01);
- store_asn1_size(p_stream, p_len_octets);
+ _store_asn1_size(p_stream, 1);
if (data_bool) {
- p_stream.push_back(0x01);
+ p_stream.push_back(0xFF);
} else {
p_stream.push_back(0x00);
}
} break;
case PList::PLNodeType::PL_NODE_TYPE_INTEGER: {
p_stream.push_back(0x02);
- store_asn1_size(p_stream, p_len_octets);
+ _store_asn1_size(p_stream, 4);
for (int i = 4; i >= 0; i--) {
uint8_t x = (data_int >> i * 8) & 0xFF;
p_stream.push_back(x);
@@ -323,7 +347,7 @@ bool PListNode::store_asn1(PackedByteArray &p_stream, uint8_t p_len_octets) cons
} break;
case PList::PLNodeType::PL_NODE_TYPE_REAL: {
p_stream.push_back(0x03);
- store_asn1_size(p_stream, p_len_octets);
+ _store_asn1_size(p_stream, 4);
for (int i = 4; i >= 0; i--) {
uint8_t x = (data_int >> i * 8) & 0xFF;
p_stream.push_back(x);
@@ -331,42 +355,42 @@ bool PListNode::store_asn1(PackedByteArray &p_stream, uint8_t p_len_octets) cons
} break;
case PList::PLNodeType::PL_NODE_TYPE_ARRAY: {
p_stream.push_back(0x30); // Sequence.
- store_asn1_size(p_stream, p_len_octets);
+ size_t size = 0;
+ for (int i = 0; i < data_array.size(); i++) {
+ size += data_array[i]->_get_asn1_size();
+ }
+ _store_asn1_size(p_stream, size);
for (int i = 0; i < data_array.size(); i++) {
- valid = valid && data_array[i]->store_asn1(p_stream, p_len_octets);
+ valid = valid && data_array[i]->store_asn1(p_stream);
}
} break;
case PList::PLNodeType::PL_NODE_TYPE_DICT: {
p_stream.push_back(0x31); // Set.
- store_asn1_size(p_stream, p_len_octets);
+ size_t size = 0;
for (const KeyValue<String, Ref<PListNode>> &E : data_dict) {
CharString cs = E.key.utf8();
- uint32_t size = cs.length();
+ uint32_t str_size = cs.length();
+
+ size_t seq_size = E.value->_get_asn1_size() + str_size + 1 + _asn1_size_len(str_size);
+ size += 1 + _asn1_size_len(seq_size) + seq_size;
+ }
+ _store_asn1_size(p_stream, size);
+ for (const KeyValue<String, Ref<PListNode>> &E : data_dict) {
+ CharString cs = E.key.utf8();
+ uint32_t str_size = cs.length();
// Sequence.
p_stream.push_back(0x30);
- uint32_t seq_size = 2 * (1 + _asn1_size_len(p_len_octets)) + size + E.value->get_asn1_size(p_len_octets);
- if (p_len_octets > 1) {
- p_stream.push_back(0x80 + p_len_octets);
- }
- for (int i = p_len_octets - 1; i >= 0; i--) {
- uint8_t x = (seq_size >> i * 8) & 0xFF;
- p_stream.push_back(x);
- }
+ size_t seq_size = E.value->_get_asn1_size() + str_size + 1 + _asn1_size_len(str_size);
+ _store_asn1_size(p_stream, seq_size);
// Key.
p_stream.push_back(0x0C);
- if (p_len_octets > 1) {
- p_stream.push_back(0x80 + p_len_octets);
- }
- for (int i = p_len_octets - 1; i >= 0; i--) {
- uint8_t x = (size >> i * 8) & 0xFF;
- p_stream.push_back(x);
- }
- for (uint32_t i = 0; i < size; i++) {
+ _store_asn1_size(p_stream, str_size);
+ for (uint32_t i = 0; i < str_size; i++) {
p_stream.push_back(cs[i]);
}
// Value.
- valid = valid && E.value->store_asn1(p_stream, p_len_octets);
+ valid = valid && E.value->store_asn1(p_stream);
}
} break;
}
@@ -820,31 +844,8 @@ PackedByteArray PList::save_asn1() const {
if (root.is_null()) {
ERR_FAIL_V_MSG(PackedByteArray(), "PList: Invalid PList, no root node.");
}
- size_t size = root->get_asn1_size(1);
- uint8_t len_octets = 0;
- if (size < 0x80) {
- len_octets = 1;
- } else {
- size = root->get_asn1_size(2);
- if (size < 0xFFFF) {
- len_octets = 2;
- } else {
- size = root->get_asn1_size(3);
- if (size < 0xFFFFFF) {
- len_octets = 3;
- } else {
- size = root->get_asn1_size(4);
- if (size < 0xFFFFFFFF) {
- len_octets = 4;
- } else {
- ERR_FAIL_V_MSG(PackedByteArray(), "PList: Data is too big for ASN.1 serializer, should be < 4 GiB.");
- }
- }
- }
- }
-
PackedByteArray ret;
- if (!root->store_asn1(ret, len_octets)) {
+ if (!root->store_asn1(ret)) {
ERR_FAIL_V_MSG(PackedByteArray(), "PList: ASN.1 serializer error.");
}
return ret;
diff --git a/core/io/plist.h b/core/io/plist.h
index f2c35eb91de..826a41d3034 100644
--- a/core/io/plist.h
+++ b/core/io/plist.h
@@ -87,7 +87,11 @@ public:
class PListNode : public RefCounted {
GDSOFTCLASS(PListNode, RefCounted);
- static int _asn1_size_len(uint8_t p_len_octets);
+ static int _asn1_size_len(size_t p_size);
+ static void _store_asn1_size(PackedByteArray &p_stream, size_t p_size);
+ static uint8_t _get_len_octets(size_t p_size);
+
+ size_t _get_asn1_size() const;
public:
PList::PLNodeType data_type = PList::PLNodeType::PL_NODE_TYPE_NIL;
@@ -116,9 +120,6 @@ public:
bool push_subnode(const Ref<PListNode> &p_node, const String &p_key = "");
- size_t get_asn1_size(uint8_t p_len_octets) const;
-
- void store_asn1_size(PackedByteArray &p_stream, uint8_t p_len_octets) const;
- bool store_asn1(PackedByteArray &p_stream, uint8_t p_len_octets) const;
+ bool store_asn1(PackedByteArray &p_stream) const;
void store_text(String &p_stream, uint8_t p_indent) const;
};