Pyasn1 decoding with OS Specific carriage return --' \r\n'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haridas
    New Member
    • Feb 2013
    • 1

    Pyasn1 decoding with OS Specific carriage return --' \r\n'

    I'm handling BER encoded Call Data Records (CDR). from GPRS Tunneling protocol. In which I got one field's value with `'\r\n'` in it.

    `'\xa0\x06\x80\ x04\r\n\xc4\x08 6'` -- This is the encoded string or `bytearray` which causing the issue. It has all the TLV information required for the tag [B]a0, and its length is 06. There is an inner tag 80 and its length is 04. But if we take the output in the following way it prints like this.

    The inner tag 80's value causing the issue. Its length is 04 but when we decode it with `pyasn1` the length became 05 since it counts `\r` and `\n` separately. I think the encoder which encodes this data assumes that `\r\n` is a single element. But while decoding this using pyasn1 it throwing an error due to this missing length on the TLV representation of encoded value.

    map(ord, '\xa0\x06\x80\x 04\r\n\xc4\x086 ')
    [160, 6, 128, 4, 13, 10, 196, 8, 54]

    We are using implicit tag numbers.

    Is there any way to solve this using pyasn1's ber decoder and encoder.


    Thanks and Regards,

    Haridas N.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I don't know anything about pyasn1 or the data you are describing. If you could strip out the '\r', would that help with your issue?
    Code:
    >>> "".join('\xa0\x06\x80\x04\r\n\xc4\x086'.split("\r"))
    '\xa0\x06\x80\x04\n\xc4\x086'
    >>> "".join([chr(n) for n in map(ord, '\xa0\x06\x80\x04\r\n\xc4\x086') if n != 13])
    '\xa0\x06\x80\x04\n\xc4\x086'
    >>>

    Comment

    Working...