mqtt_udp icon indicating copy to clipboard operation
mqtt_udp copied to clipboard

python3 decode mqtt remaining length swapped

Open Ercsion opened this issue 1 year ago • 0 comments

Bug or feature

python3 decode mqtt remaining length swapped

What is wrong / not implemented

when mqtt length > 127, for example 428 which 0x1AC in hex, MQTT header.length bytes is [AC 03] length decode to [16 03] as: 0xAC & 0x7F = 0x2C 0xAC bit 7 is setted then : (0x2C<<7) | (0x03 & 0x7F) = 0x1603 0x03 bit 7 is not setted then return 0x1603

How should it be

0xAC & 0x7F = 0x2C 0xAC bit 7 is setted then : (0x2C) | ((0x03 & 0x7F)<<7) = 0x1AC 0x03 bit 7 is not setted then return 0x1AC

Correct behaviour is documented in (URL)

pass

Environment

~~* GitHub commit:~~

  • Prog language: Python3.10
  • OS/hardware:Window10

Fixed/implemented in

  • [x] Pyhon implementation
  • [ ] Java implementation
  • [ ] Java tools
  • [ ] C implementation/Unix
  • [ ] C implementation/Embedded
  • [ ] Lua implementation
  • [ ] CodeSys implementation

Patch

def unpack_remaining_length(pkt):
    remaining_length = 0
--    eaten = 0;
++    eaten = 0
    while True:
        b = pkt[0]
        eaten += 1
        pkt = pkt[1:]
--        remaining_length = remaining_length << 7
--        remaining_length = remaining_length | (b & 0x7F)
++        remaining_length += (b & 0x7F) << (7 * (eaten - 1))
        if (b & 0x80) == 0:
            break
    return remaining_length, pkt, eaten

Ercsion avatar Nov 13 '23 07:11 Ercsion