EVerest / cbexigen

cbExiGen - The V2GTP EXI codec generator for cbV2G
Apache License 2.0
32 stars 18 forks source link

Incorrect code generated for choice sequence? #56

Closed bklop closed 10 months ago

bklop commented 11 months ago

In case of a choice sequence (for example in din_PGPDataType), the generated encoding code looks wrong/weird.

For example. in encode_din_PGPDataType we find this:

            // Grammar: ID=81; read/write bits=2; START (PGPKeyID), START (PGPKeyPacket)
            if (PGPDataType->choice_1_isUsed == 1u)
            {
                error = exi_basetypes_encoder_nbit_uint(stream, 2, 0);
                if (error == EXI_ERROR__NO_ERROR)
                {
                    // Event: START (PGPKeyID, base64Binary); next=82
                    error = exi_basetypes_encoder_nbit_uint(stream, 1, 0);
                    if (error == EXI_ERROR__NO_ERROR)
                    {
                        error = exi_basetypes_encoder_uint_16(stream, (uint16_t)PGPDataType->choice_1.PGPKeyID.bytesLen);
                        if (error == EXI_ERROR__NO_ERROR)
                        {
                            error = exi_basetypes_encoder_bytes(stream, PGPDataType->choice_1.PGPKeyID.bytesLen, PGPDataType->choice_1.PGPKeyID.bytes, din_base64Binary_BYTES_SIZE);
                            if (error == EXI_ERROR__NO_ERROR)
                            {
                                // encode END Element
                                error = exi_basetypes_encoder_nbit_uint(stream, 1, 0);
                                if (error == EXI_ERROR__NO_ERROR)
                                {
                                    grammar_id = 82;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                error = exi_basetypes_encoder_nbit_uint(stream, 2, 1);
                if (error == EXI_ERROR__NO_ERROR)
                {
                    // Event: START (PGPKeyPacket, base64Binary); next=83
                    error = exi_basetypes_encoder_nbit_uint(stream, 1, 0);
                    if (error == EXI_ERROR__NO_ERROR)
                    {
                        error = exi_basetypes_encoder_uint_16(stream, (uint16_t)PGPDataType->choice_1.PGPKeyPacket.bytesLen);
                        if (error == EXI_ERROR__NO_ERROR)
                        {
                            error = exi_basetypes_encoder_bytes(stream, PGPDataType->choice_1.PGPKeyPacket.bytesLen, PGPDataType->choice_1.PGPKeyPacket.bytes, din_base64Binary_BYTES_SIZE);
                            if (error == EXI_ERROR__NO_ERROR)
                            {
                                // encode END Element
                                error = exi_basetypes_encoder_nbit_uint(stream, 1, 0);
                                if (error == EXI_ERROR__NO_ERROR)
                                {
                                    grammar_id = 83;
                                }
                            }
                        }
                    }
                }
            }
            break;

So if choice_1 is not used (i.e. choice_1_isUsed is 0), we still access choice_1.PGPKeyPacket in the else case.

barsnick commented 10 months ago

Hi @bklop, we are aware that there are coding issues with choice sequences. These are somewhat special and require special handling.

Yet according to the requirements [V2G2-771] (ISO 15118-2) and [V2G20-771] (ISO 15118-20) ("The following message elements of the XML Signature framework shall not be used"), the KeyInfo element is not to be used. The only choice sequences occur in PGPData, which is an element in KeyInfo. (And in DIN 70121, messages and their elements are not signed.)

As long as there is no actual use for choice sequences, and the generated code compiles correctly, we see no need to implement them correctly.

bklop commented 10 months ago

Thanks for the explanation!