- how to decode Decoding Multiple CDR Records from a File with asn1tools (python)

1k Views Asked by At

how to decode Decoding Multiple CDR Records from a File with asn1tools.. this is my python code:

### input file name 
fileName='D:/Python/Asn1/SIO/bHWMSC12021043012454329.dat'
## create Dict from asn file struct 
Foo = asn1tools.compile_files('asn_Huawei.asn',cache_dir='My-Cache',numeric_enums=True)
## Open binary file        
with open(fileName,"rb+") as binaryfile:
    buffer = binaryfile.read()
    ## Match and decode all record with Dict        
    decoded = Foo.decode('CallEventRecord',buffer)
    print(decoded)

print(decoded) give only first record. My file contain 1550 records.... how to read Tag by tag my file with asn1tools

3

There are 3 best solutions below

0
Ahmed ElHaw On

I got the same issue, trying to figure it out. You can +1 this issue

I managed to decode multiCDR files with a combination of pyasn1 and asn1tools, I'll update when it is fully tested.

0
Tracy On

I would use "decode_with_length" instead of decode.

decoded, lenDecoded = Foo.decode_with_length('CallEventRecord',buffer)
buffer = buffer[lenDecoded:]

Then just loop until the buffer is empty.

0
zeitghaist On

For me (it highly depends how your Files are actually internally structured and what your goal is) the so far fastest method is to use find to jump to the next relevant ticket, mine always start with 'b\xa7':

    with open(file, 'rb') as encoded_bytes:
        data = encoded_bytes.read()
        encoded_bytes.close()

    file_len = len(data)

    while index < file_len:
            next_occurence = data[index:].find(b'\xa7')
            if next_occurence < 0:
                break
            ....
       
            index += next_occurence
            decoded_record, record_length =   schema.decode_with_length('CallEventRecord', data[index:])
            .....
            index += 1
            continue