Skip to content

Commit 703c9c3

Browse files
committed
start drafting a datetime object for deserialization
1 parent b7dd509 commit 703c9c3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/dnfile/base.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import enum
99
import struct as _struct
1010
import logging
11+
import datetime
1112
import functools as _functools
1213
import itertools as _itertools
1314
from typing import TYPE_CHECKING, Any, Dict, List, Type, Tuple, Union, Generic, TypeVar, Optional, Sequence
@@ -980,3 +981,32 @@ def set_data(self, data: bytes):
980981
@abc.abstractmethod
981982
def parse(self):
982983
raise NotImplementedError()
984+
985+
986+
class DateTimeStruct(Structure):
987+
Ticks: int
988+
Kind: enums.DateTimeKind
989+
990+
991+
class DateTime(object):
992+
def __init__(self, rva: int, raw_bytes: bytes):
993+
self.struct: Optional[DateTimeStruct] = None
994+
self.raw: bytes = raw_bytes
995+
self.value: Optional[datetime.datetime] = None
996+
997+
def parse(self):
998+
if not self.raw:
999+
# TODO: warn/error
1000+
return
1001+
# Should be 64 bites
1002+
if len(self.raw) != 8:
1003+
# TODO: warn/error
1004+
return
1005+
x = _struct.unpack("<q", self.raw)[0]
1006+
self.struct = DateTimeStruct()
1007+
self.struct.Ticks = x & 0x3FFFFFFFFFFFFFFF
1008+
self.struct.Kind = x >> 62
1009+
# https://stackoverflow.com/questions/3169517/python-c-sharp-binary-datetime-encoding
1010+
secs = self.struct.Ticks / 10.0 ** 7
1011+
delta = datetime.timedelta(seconds=secs)
1012+
self.value = datetime.datetime(1, 1, 1) + delta

src/dnfile/enums.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,17 @@ class AssemblyHashAlgorithm(_enum.IntEnum):
861861
SHA256 = 0x800c
862862
SHA384 = 0x800d
863863
SHA512 = 0x800e
864+
865+
866+
class DateTimeKind(_enum.IntEnum):
867+
"""
868+
Per Microsoft documenation, provide additional context to DateTime instances.
869+
870+
REFERENCE:
871+
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/DateTime.cs
872+
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/DateTimeKind.cs
873+
"""
874+
Unspecified = 0
875+
Utc = 1
876+
Local = 2
877+
LocalAmbiguousDst = 3

0 commit comments

Comments
 (0)