2

I am trying to write a Basic Encoding Rules codec in D, and I would like the ability to encode data with indefinite length encoding, in which the length byte is set to 0x80 and the end of the value bytes is demarcated with a double null 0x00 0x00 (End of Content). However, there are times when a double null is part of the actual value being encoded. If you have an OCTET STRING, for instance, two adjacent bytes could be 0x00 0x00, which would get interpreted as the END OF CONTENT instead of just a part of the encoded value, resulting in the truncation of the encoded value (best-case scenario). Is there some way to encode double nulls without them being interpreted as END OF CONTENT? Or are you expected to only encode values that don't have double nulls? I have yet to find anything about this in any specification.

Jonathan Wilbur
  • 1,057
  • 10
  • 19

1 Answers1

3

You never put plain OCTET STRING content between 0x80 and 0x00 0x00. Instead you put [likely pieces] of definite-length encoded OCTET STRING there.

In other words, indefinite length encoded, single-octet chunked "AB" ASCII string (e.g. 0x41 0x42 in hex) would look like this:

0x24 0x80      0x04 0x01 0x41       0x04 0x1 0x42     0x00 0x00 
^              ^                    ^                 ^
outer header   inner chunk #1       inner chunk #2    outer EOO sentinel

The end result is that the decoder does not have to scan the actual payload searching for EOO mark because payload margins are clearly defined by the definite length encoded pieces.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • In this case the outer header should be represented as a constructor so it should be encoded as 0x24. It will have the bit 6 of identifier tag (P/C) set to constructed. – pepo Oct 12 '17 at 18:49
  • @pepo Indeed! Thanks! ;-) – Ilya Etingof Oct 12 '17 at 19:36
  • This works for Octet Strings, but how do i encode, when i have the Integer 0x‭10000000FFFFFFFC‬ and I dont follow DER Rules, but really BER Rules, which allow even Primitives to have Indefinte Length Encoding. – Andreas Oct 10 '18 at 15:29