Skip to content

Commit

Permalink
Merge pull request #1534 from gpotter2/master
Browse files Browse the repository at this point in the history
[Contribs] Big cleanup
  • Loading branch information
guedou committed Oct 26, 2018
2 parents 234badf + e8523c8 commit 87916fd
Show file tree
Hide file tree
Showing 13 changed files with 946 additions and 1,529 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ New protocols can go either in `scapy/layers` or to
on common networks, while protocols in `scapy/contrib` should be
uncommon or specific.

To be precise, `scapy/layers` protocols should not be importing `scapy/contrib`
protocols, whereas `scapy/contrib` protocols may import both `scapy/contrib` and
`scapy/layers` protocols.

### Features

Protocol-related features should be implemented within the same module
Expand Down
47 changes: 15 additions & 32 deletions scapy/asn1/asn1.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,57 +440,40 @@ class ASN1_UTC_TIME(ASN1_STRING):
tag = ASN1_Class_UNIVERSAL.UTC_TIME

def __init__(self, val):
super(ASN1_UTC_TIME, self).__init__(val)
ASN1_STRING.__init__(self, val)

def __setattr__(self, name, value):
if isinstance(value, bytes):
value = plain_str(value)
if name == "val":
pretty_time = None
if isinstance(self, ASN1_GENERALIZED_TIME):
_len = 15
_format = "%Y%m%d%H%M%S"
else:
_len = 13
_format = "%y%m%d%H%M%S"
_nam = self.tag._asn1_obj.__name__[4:].lower()
if (isinstance(value, str) and
len(value) == 13 and value[-1] == "Z"):
dt = datetime.strptime(value[:-1], "%y%m%d%H%M%S")
len(value) == _len and value[-1] == "Z"):
dt = datetime.strptime(value[:-1], _format)
pretty_time = dt.strftime("%b %d %H:%M:%S %Y GMT")
else:
pretty_time = "%s [invalid utc_time]" % value
super(ASN1_UTC_TIME, self).__setattr__("pretty_time", pretty_time)
super(ASN1_UTC_TIME, self).__setattr__(name, value)
pretty_time = "%s [invalid %s]" % (value, _nam)
ASN1_STRING.__setattr__(self, "pretty_time", pretty_time)
ASN1_STRING.__setattr__(self, name, value)
elif name == "pretty_time":
print("Invalid operation: pretty_time rewriting is not supported.")
else:
super(ASN1_UTC_TIME, self).__setattr__(name, value)
ASN1_STRING.__setattr__(self, name, value)

def __repr__(self):
return "%s %s" % (self.pretty_time, ASN1_STRING.__repr__(self))


class ASN1_GENERALIZED_TIME(ASN1_STRING):
class ASN1_GENERALIZED_TIME(ASN1_UTC_TIME):
tag = ASN1_Class_UNIVERSAL.GENERALIZED_TIME

def __init__(self, val):
super(ASN1_GENERALIZED_TIME, self).__init__(val)

def __setattr__(self, name, value):
if isinstance(value, bytes):
value = plain_str(value)
if name == "val":
pretty_time = None
if (isinstance(value, str) and
len(value) == 15 and value[-1] == "Z"):
dt = datetime.strptime(value[:-1], "%Y%m%d%H%M%S")
pretty_time = dt.strftime("%b %d %H:%M:%S %Y GMT")
else:
pretty_time = "%s [invalid generalized_time]" % value
super(ASN1_GENERALIZED_TIME, self).__setattr__("pretty_time", pretty_time) # noqa: E501
super(ASN1_GENERALIZED_TIME, self).__setattr__(name, value)
elif name == "pretty_time":
print("Invalid operation: pretty_time rewriting is not supported.")
else:
super(ASN1_GENERALIZED_TIME, self).__setattr__(name, value)

def __repr__(self):
return "%s %s" % (self.pretty_time, ASN1_STRING.__repr__(self))


class ASN1_ISO646_STRING(ASN1_STRING):
tag = ASN1_Class_UNIVERSAL.ISO646_STRING
Expand Down
Loading

0 comments on commit 87916fd

Please sign in to comment.