open-dis / open-dis-python

Python implementation of the IEEE-1278.1 Distributed Interactive Simulation (DIS) application protocol v7
BSD 2-Clause "Simplified" License
48 stars 31 forks source link

VariableDatum object does not serialize padding #22

Closed SnowFlake8i8 closed 3 years ago

SnowFlake8i8 commented 3 years ago

The variable datum object only serialises the actual data, but recieved pdus will be parsed as though padding has been sent as well. This results in the variable datum object ignoring several of the bits it recieves, and attempting to read more from the input stream than has been sent.

Issue in code:

(code from line 3579 of dis7.py, my comments start with #########)

def serialize(self, outputStream):
        """serialize the class """
        outputStream.write_unsigned_int(self.variableDatumID);
        outputStream.write_unsigned_int(self.variableDatumLength);
        for x in range(self.variableDatumLength // 8): # length is in bits
            outputStream.write_byte(self.variableData[x])

        ######### no padding is sent, only data from variableData within the size of variableDatumLength field

 def parse(self, inputStream):
        """"Parse a message. This may recursively call embedded objects."""
        self.variableDatumID = inputStream.read_unsigned_int();
        self.variableDatumLength = inputStream.read_unsigned_int();
        for x in range(self.variableDatumLength // 8): # length is in bits
            self.variableData.append(inputStream.read_byte());

              ######## the parse function expects padding
    # Skip over padding
    # "This field shall be padded at the end to make the length a multiple of 64-bits."
    for x in range(self.datumPaddingSizeInBits() // 8):
        inputStream.read_byte()

Expected behaviour:

The serialise function should send a number of zeros corresponding to the padding size of the datum.

Fix:

This isssue could be fixed by adding the following code to the end of the serialize function:

for x in range(self.datumPaddingSizeInBits() // 8):
            inputStream.write_byte(0)
leif81 commented 3 years ago

Thankyou @SnowFlake8i8 . Would you like to submit a PR for this?

SnowFlake8i8 commented 3 years ago

Done @leif81 :)

leif81 commented 3 years ago

thankyou @SnowFlake8i8 !