Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new load method returning a tuple (an instance of current class and byte length) #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ladaan
Copy link

@ladaan ladaan commented Feb 3, 2020

No description provided.

@wbond
Copy link
Owner

wbond commented Feb 3, 2020

Looks like I need to tweak the dependency resolver now that projects are starting to drop Python 2 support.

Could you describe your use case, perhaps with some example code?

I'm somewhat inclined to create a new method rather than having a param change the type of the return value.

… byte length)

In case there is a byte pool with many records they need to be extracted
separately. Each record might have variable length depending on data in
it.
@codecov
Copy link

codecov bot commented Feb 3, 2020

Codecov Report

Merging #176 into master will decrease coverage by 0.06%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #176      +/-   ##
==========================================
- Coverage   87.16%   87.09%   -0.07%     
==========================================
  Files          21       21              
  Lines        5243     5246       +3     
==========================================
- Hits         4570     4569       -1     
- Misses        673      677       +4
Impacted Files Coverage Δ
asn1crypto/core.py 82.24% <66.66%> (-0.12%) ⬇️
asn1crypto/_int.py 80% <0%> (-20%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c09b87c...afd05cb. Read the comment docs.

@ladaan ladaan changed the title Returning offset for multiple object ASN.1 stream with separated records A new load method returning a tuple (an instance of current class and byte length) Feb 3, 2020
@ladaan
Copy link
Author

ladaan commented Feb 3, 2020

Here is an use case:

cdr_file = open(filename, 'rb')
data = cdr_file.read()

offset = 0
bytes_left = len(data)
while bytes_left > 0:
    asrecord, length = spec_class.load_p(data[offset:])
    records.append(asrecord)
    bytes_left = bytes_left - length
    offset = offset + length

Data on the input are ASN.1 encoded CDR records (several fields per record).
They need to be decoded based on the "spec_class" that is defined separately along with other subclasses (definition of data types of each field etc.)

Possibly you can suggest the method name if you like.

@joernheissler
Copy link
Collaborator

What is "CDR"?
The problem you're trying to solve looks similar to #152.

@ladaan
Copy link
Author

ladaan commented Feb 4, 2020

Thanks I will have a look. CDR is a Call Detail Record. It is from telecommunication terminology. CDRs keeps the history of all dialogues that includes calling and called party and further important fields.

@ladaan
Copy link
Author

ladaan commented Feb 4, 2020

Joern is right. I can use parser.parse method
Is there any more effective use of buffers where I would use pointers instead of a new object (slice of data)?
Such as memoryview (but it is not possible here as the parse method expects a byte string)

Here is the code I used:

data = cdr_file.read()
offset = 0
bytes_left = len(data) 
while bytes_left: 
    parsed_data = parser.parse(data[offset:])
    record = parsed_data[3] + parsed_data[4] + parsed_data[5]
    length = len(record) 
    asrecord = spec_class.load(record)
    records.append(asrecord)
    bytes_left = bytes_left - length
    offset = offset + length

cdr_file.close()

Otherwise my pull request can be closed as a solution exists.

@joernheissler
Copy link
Collaborator

Is there any more effective use of buffers where I would use pointers instead of a new object

Python brings a bytearray object which has efficient append/remove operations on both ends:

>>> from os import urandom
>>> buf = bytearray(urandom(1_000_000_000))
>>> del buf[:567] # finishes near instantly

@joernheissler
Copy link
Collaborator

Otherwise my pull request can be closed as a solution exists.

I don't think there's a solution yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants