From 002b07f0c5706fbfebd5bcc4ff920aa24c20e7b7 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 2 May 2024 20:29:00 +0200 Subject: [PATCH] Reduce the size of `deserializeMsgPack()` --- .../MsgPack/MsgPackDeserializer.hpp | 230 ++++++------------ 1 file changed, 79 insertions(+), 151 deletions(-) diff --git a/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp b/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp index 7d30ac03..8ad3e90b 100644 --- a/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp +++ b/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp @@ -52,6 +52,82 @@ class MsgPackDeserializer { ARDUINOJSON_ASSERT(variant != 0); } + size_t size = 0; + + switch (code) { + case 0xc4: // bin 8 + case 0xc7: // ext 8 + case 0xd9: // str 8 + uint8_t size8; + err = readInteger(size8); + size = size8; + break; + + case 0xc5: // bin 16 + case 0xc8: // ext 16 + case 0xda: // str 16 + case 0xdc: // array 16 + case 0xde: // map 16 + uint16_t size16; + err = readInteger(size16); + size = size16; + break; + + case 0xc6: // bin 32 + case 0xc9: // ext 32 + case 0xdb: // str 32 + case 0xdd: // array 32 + case 0xdf: // map 32 + uint32_t size32; + err = readInteger(size32); + size = size32; + break; + } + + if (err) + return err; + + switch (code & 0xf0) { + case 0x90: // fixarray + case 0x80: // fixmap + size = code & 0x0F; + break; + } + + switch (code & 0xe0) { + case 0xa0: // fixstr + size = code & 0x1f; + break; + } + + // array 16, 32 and fixarray + if (code == 0xdc || code == 0xdd || (code & 0xf0) == 0x90) + return readArray(variant, size, filter, nestingLimit); + + // map 16, 32 and fixmap + if (code == 0xde || code == 0xdf || (code & 0xf0) == 0x80) + return readObject(variant, size, filter, nestingLimit); + + // str 8, 16, 32 and fixstr + if (code == 0xd9 || code == 0xda || code == 0xdb || (code & 0xe0) == 0xa0) { + if (allowValue) + return readString(variant, size); + else + return skipBytes(size); + } + + // bin 8, 16, 32 + if (code == 0xc4 || code == 0xc5 || code == 0xc6) { + if (allowValue) + return readBinary(variant, size); + else + return skipBytes(size); + } + + // ext 8, 16, 32 + if (code == 0xc7 || code == 0xc8 || code == 0xc9) + return skipBytes(size + 1); + switch (code) { case 0xc0: // already null @@ -70,33 +146,6 @@ class MsgPackDeserializer { variant->setBoolean(true); return DeserializationError::Ok; - case 0xc4: - if (allowValue) - return readBinary(variant); - else - return skipString(); - - case 0xc5: - if (allowValue) - return readBinary(variant); - else - return skipString(); - - case 0xc6: - if (allowValue) - return readBinary(variant); - else - return skipString(); - - case 0xc7: // ext 8 (not supported) - return skipExt(); - - case 0xc8: // ext 16 (not supported) - return skipExt(); - - case 0xc9: // ext 32 (not supported) - return skipExt(); - case 0xca: if (allowValue) return readFloat(variant); @@ -180,56 +229,11 @@ class MsgPackDeserializer { case 0xd8: // fixext 16 (not supported) return skipBytes(17); - case 0xd9: + default: // fixint if (allowValue) - return readString(variant); - else - return skipString(); - - case 0xda: - if (allowValue) - return readString(variant); - else - return skipString(); - - case 0xdb: - if (allowValue) - return readString(variant); - else - return skipString(); - - case 0xdc: - return readArray(variant, filter, nestingLimit); - - case 0xdd: - return readArray(variant, filter, nestingLimit); - - case 0xde: - return readObject(variant, filter, nestingLimit); - - case 0xdf: - return readObject(variant, filter, nestingLimit); + variant->setInteger(static_cast(code)); + return DeserializationError::Ok; } - - switch (code & 0xf0) { - case 0x80: - return readObject(variant, code & 0x0F, filter, nestingLimit); - - case 0x90: - return readArray(variant, code & 0x0F, filter, nestingLimit); - } - - if ((code & 0xe0) == 0xa0) { - if (allowValue) - return readString(variant, code & 0x1f); - else - return skipBytes(code & 0x1f); - } - - if (allowValue) - variant->setInteger(static_cast(code)); - - return DeserializationError::Ok; } DeserializationError::Code readByte(uint8_t& value) { @@ -337,18 +341,6 @@ class MsgPackDeserializer { return DeserializationError::Ok; } - template - DeserializationError::Code readString(VariantData* variant) { - DeserializationError::Code err; - T size; - - err = readInteger(size); - if (err) - return err; - - return readString(variant, size); - } - template DeserializationError::Code readString() { DeserializationError::Code err; @@ -361,18 +353,6 @@ class MsgPackDeserializer { return readString(size); } - template - DeserializationError::Code skipString() { - DeserializationError::Code err; - T size; - - err = readInteger(size); - if (err) - return err; - - return skipBytes(size); - } - DeserializationError::Code readString(VariantData* variant, size_t n) { DeserializationError::Code err; @@ -392,18 +372,6 @@ class MsgPackDeserializer { return readBytes(p, n); } - template - DeserializationError::Code readBinary(VariantData* variant) { - DeserializationError::Code err; - T size; - - err = readInteger(size); - if (err) - return err; - - return readBinary(variant, size); - } - DeserializationError::Code readBinary(VariantData* variant, size_t n) { DeserializationError::Code err; @@ -415,20 +383,6 @@ class MsgPackDeserializer { return DeserializationError::Ok; } - template - DeserializationError::Code readArray( - VariantData* variant, TFilter filter, - DeserializationOption::NestingLimit nestingLimit) { - DeserializationError::Code err; - TSize size; - - err = readInteger(size); - if (err) - return err; - - return readArray(variant, size, filter, nestingLimit); - } - template DeserializationError::Code readArray( VariantData* variant, size_t n, TFilter filter, @@ -470,20 +424,6 @@ class MsgPackDeserializer { return DeserializationError::Ok; } - template - DeserializationError::Code readObject( - VariantData* variant, TFilter filter, - DeserializationOption::NestingLimit nestingLimit) { - DeserializationError::Code err; - TSize size; - - err = readInteger(size); - if (err) - return err; - - return readObject(variant, size, filter, nestingLimit); - } - template DeserializationError::Code readObject( VariantData* variant, size_t n, TFilter filter, @@ -557,18 +497,6 @@ class MsgPackDeserializer { } } - template - DeserializationError::Code skipExt() { - DeserializationError::Code err; - T size; - - err = readInteger(size); - if (err) - return err; - - return skipBytes(size + 1U); - } - ResourceManager* resources_; TReader reader_; StringBuffer stringBuffer_;