简体   繁体   中英

How do you escape double null octets within an octet string encoded with indefinite length in BER?

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.

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM