简体   繁体   中英

Find and replace (increment) ASN.1 BER hex value

I have a long string of hex (converted from BER ASN.1) where I need to find and increment a particular value which is incorrect.

<TAG> <LENGTH> <VALUE to INCREMENT>

the ASN.1 tag is 84 and the length byte will change from 01 to 02 when the value > 127dec . And the value to increment will therefore become 2 bytes.

The value should start at 00.

eg

- Original file: ...840101...840107...84020085...84020097
- New file:      ...840100...840101...84020080...84020081

Any ideas how best to do this, preferably using standard bash commands?

Ilya Etingof hinted at this already, but to be explicit about it, BER uses TAG, LENGTH, VALUE (TLV) encoding, where the VALUE can itself be a TLV. If you change the length in a TLV that is nested inside a TLV, you will need to update all of the lengths of the enclosing TLVs as well. It is not a simple search/replace operation.

Assuming you have the octet-stream in text work already, you may consider searching/replacing pieces of text with awk or sed . If you can only use bash , may be variable substitution ( ${parameter/pattern/string} or ${parameter:offset:length} ) would work?

Keep in mind however, that BER is quite flexible in the sense that (sometimes) the same data structure may be encoded differently and that would still constitute a valid encoding. The rationale behind that is to allow the encoder to optimize for its very own situation (eg save on memory or CPU cycles or on copying etc).

What I am trying to say that depending on your situation there may be a chance that your search/replace logic may fail. The bullet-proof solution would be to fully decode your BER octet stream, change the data structure you need and re-encode it back into BER.

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