Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

customizing to_asn1 output #186

Closed
dbressan2 opened this issue Apr 4, 2022 · 6 comments
Closed

customizing to_asn1 output #186

dbressan2 opened this issue Apr 4, 2022 · 6 comments
Assignees
Labels

Comments

@dbressan2
Copy link

Hi

I would like to know the best way to change the look of to_asn1 output for various objects.
I tried to modify the code in pycrate_asn1/asnobj.py.
Let us take an example

Say I want to print the TIME_UTC output as YYMMDDHHMMSSUTC instead of YYMMDDHHMMSSZ.

Is it enough to modify TIME_UTC code ? Do we need to re-build package afterwards ?

class TIME_UTC(_Time):
doc = """
ASN.1 basic type UTCTime object

single value: Python 7-tuple of int (AA, MM, DD, HH, MM, [SS,] Z),
    SS is optional, hence 6th element can be None
    Z corresponds to the UTC decay

%s
""" % ASN1Obj_docstring

TYPE  = TYPE_TIME_UTC
TAG   = 23
_type = TYPE

def _to_asn1(self, val):
    # to be applied to an internal single value `val' to get 
    # an ASN.1 compliant value
    ret = '%.2i%.2i%.2i%.2i%.2i' % val[:5]
    if val[5] is not None:
        ret += '%.2i' % val[5]
    if val[6] == 0:
        ret += 'Z'
    elif val[6] > 0:
        ret += '+%.4i' % val[6]
    else:
        ret += '%.4i' % val[6]
    return ret

Thank you in advance

@p1-bmu p1-bmu self-assigned this Apr 4, 2022
@p1-bmu p1-bmu added the question label Apr 4, 2022
@p1-bmu
Copy link
Contributor

p1-bmu commented Apr 4, 2022

As soon as you patch a file of the library, the change will be effective as soon as you load it (e.g. in case you use only a local version from your $HOME, or you installed with setup.py install develop): no need for re-packaging. This is one of the advantage of Python.

Regarding the modification of the generic to_asn1() in asnobj.py, or the various _to_asn1() methods, this will change the formatting of the output for sure. On the other side, to_asn1() is made to produce ASN.1-compliant syntax and formatting (its output could be ingested by some other ASN.1 tools), it may not be the case anymore if you change those methods.

@dbressan2
Copy link
Author

dbressan2 commented Apr 7, 2022 via email

@p1-bmu
Copy link
Contributor

p1-bmu commented Apr 11, 2022

I don't really understand what / how you want to number items. I see no colour in your provided description, sorry.

@dbressan2
Copy link
Author

dbressan2 commented Apr 11, 2022 via email

@p1-bmu
Copy link
Contributor

p1-bmu commented Apr 26, 2022

You can modify the _to_asn1() method of the _CONSTRUCT_OF class here:

for v in self._val:

To have a pattern like this instead:

            for i, v in enumerate(self._val):
                self._cont._val = v
                val.append('  -- _item_ %i --\n  %s,\n' % (i, self._cont._to_asn1().replace('\n', '\n  ')))

I guess such generated ASN.1 syntax won't be re-encodable with pycrate, as its runtime does not strip ASN.1 comments out.

@dbressan2
Copy link
Author

dbressan2 commented Oct 11, 2022 via email

@p1-bmu p1-bmu closed this as completed Dec 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants