title: Can ASN.1 support putting field lengths in a different place?
body: <p>I have a protocol whose on-the-wire format is already defined and I'd like to use ASN.1 to encode/decode it, but it seems to break the defined BER/DER/PER options. For whatever reason, the protocol developers did <strong>not</strong> put payload sizes/lengths immediately before the payload itself - so I can't use the automatic BER/DER. But since the payload can be variable length, I can't use PER either. Here's an example:</p>
  
  <pre><code>     12'b           12'b       4'b     4'b   
  |------------|--------------|-------|------|
  | some stuff | payload size | blah2 | blah |    Header
  |------------------------------------------|
  |              payload word 1              |
  |------------------------------------------|
  |                    ...                   |    Payload
  |------------------------------------------|
  |              payload word N              |
  |------------------------------------------|
  | much stuff | many bits | such doge | wow |    Trailer
  |------------------------------------------|
  </code></pre>
  
  <p>So maybe two questions here:</p>
  
  <ol>
  <li>Is there a way using one of the ASN.1 encodings to specify certain fields as the length for a later field - so you could say something like bits 9-20 contain the length for bits 33-N*32, but you're skipping bits 21-32 which can have other unrelated junk in them? </li>
  <li>I can see how an algorithm / rule could be written to support the above, so if there isn't a way to currently do this with ASN.1, is there a way (and documentation) on how to write a new rule or extension of some kind to the existing encodings?</li>
  </ol>
  
  <p>EDIT</p>
  
  <p>To clarify why I'm bringing up ASN.1, without repeating <a href="https://stackoverflow.com/questions/50818005/wire-protocol-serialization">a previous question</a>, is because it's almost exactly what I'm looking for - just apparently without a way to handle the particular use case I'm asking about here. I need to deserialize existing binary protocols and I'd rather not write my own since there are already many <a href="https://en.wikipedia.org/wiki/Comparison_of_data_serialization_formats" rel="nofollow noreferrer">tools</a> claiming they can do some form of this. If someone has another suggestion I'd gladly try it.</p>