pycrate icon indicating copy to clipboard operation
pycrate copied to clipboard

Decoding into a local rather than a global object

Open ibookstein opened this issue 6 years ago • 1 comments

I've poked around a bit and read the documentation, and had a question regarding best-practice usage. The documentation's example for decoding a buffer into an ASN.1 object decodes the buffer into a global instance of a constructed type. This is a global that is mutated for the entire application. Is there a correct/documented way wherein I could decode my data into a private, local instance of the desired type, rather than mutating global state? A simple attempt involving copy.deepcopy seemed to work, but I don't know if that's a good idea or not. The clone method I saw for some types in the repository isn't present on the compiled types.

ibookstein avatar Aug 21 '18 18:08 ibookstein

You are experiencing a limitation of the ASN.1 runtime (see https://github.com/ANSSI-FR/pycrate/wiki/Compiling-asn1-specifications#limitations, last bullet). Duplicating locally each ASN.1 structures when we want to encode or decode a value would be costly, hence the use of global objects, which _val attribute gets written / read by the application. Also, I did not write a clone method because of the complexity involved for large objects. Using deepcopy seems a way (the only easy one ?) to handle this object structure duplication. If you come with a stable and efficient solution, feel free to propose an extension to the current code and documentation.

mitshell avatar Aug 21 '18 20:08 mitshell