Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions impacket/tds.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def parse(self):
Parsed value in its appropriate Python type, or 'NULL' if empty
"""
if self["TotalLength"] == 0:
return "NULL"
return None

data = self["Data"]

Expand Down Expand Up @@ -707,7 +707,7 @@ def _parseValue(self, baseType, data, properties):
# date: 3-byte unsigned integer (days since year 1)
# VARIANT_PROPBYTES = 0
if len(data) < 3:
return "NULL"
return None
dateValue = struct.unpack("<L", data[:3] + b"\x00")[0]
return datetime.date.fromordinal(dateValue)

Expand Down Expand Up @@ -744,7 +744,7 @@ def _parseValue(self, baseType, data, properties):
timeBytes = 3 if scale <= 2 else (4 if scale <= 4 else 5)

if len(data) < timeBytes + 3:
return "NULL"
return None

# Parse time part
if timeBytes == 3:
Expand Down Expand Up @@ -785,7 +785,7 @@ def _parseValue(self, baseType, data, properties):
timeBytes = 3 if scale <= 2 else (4 if scale <= 4 else 5)

if len(data) < timeBytes + 5:
return "NULL"
return None

# Parse time part
if timeBytes == 3:
Expand Down Expand Up @@ -835,7 +835,7 @@ def _parseValue(self, baseType, data, properties):
scale = properties[1] if len(properties) > 1 else 0

if len(data) == 0:
return "NULL"
return None

# First byte is sign (1 = positive, 0 = negative)
sign = 1 if data[0] == 1 else -1
Expand Down Expand Up @@ -1660,8 +1660,9 @@ def processColMeta(self):

col["minLenght"] = 0
for row in self.rows:
if len(str(row[col["Name"]])) > col["minLenght"]:
col["minLenght"] = len(str(row[col["Name"]]))
display = "NULL" if row[col["Name"]] is None else str(row[col["Name"]])
if len(display) > col["minLenght"]:
col["minLenght"] = len(display)
if col["minLenght"] < col["Length"]:
col["Length"] = col["minLenght"]

Expand Down Expand Up @@ -1691,8 +1692,10 @@ def printRows(self):
self.printColumnsHeader()
for row in self.rows:
for col in self.colMeta:
value = row[col["Name"]]
display = "NULL" if value is None else value
self.__rowsPrinter.logMessage(
col["Format"] % row[col["Name"]] + self.COL_SEPARATOR
col["Format"] % display + self.COL_SEPARATOR
)
self.__rowsPrinter.logMessage("\r")

Expand Down Expand Up @@ -1776,7 +1779,7 @@ def parseRow(self, token, tuplemode=False):
value = data[:charLen].decode("utf-16le")
data = data[charLen:]
else:
value = "NULL"
value = None

elif _type == TDS_BIGVARCHRTYPE:
charLen = struct.unpack("<H", data[:2])[0]
Expand All @@ -1793,7 +1796,7 @@ def parseRow(self, token, tuplemode=False):
except UnicodeDecodeError:
value = raw.decode("utf-8", errors="replace")
else:
value = "NULL"
value = None

elif _type == TDS_GUIDTYPE:
uuidLen = ord(data[0:1])
Expand All @@ -1803,13 +1806,13 @@ def parseRow(self, token, tuplemode=False):
value = uuid.bin_to_string(uu)
data = data[uuidLen:]
else:
value = "NULL"
value = None

elif (_type == TDS_NTEXTTYPE) | (_type == TDS_IMAGETYPE):
# Skip the pointer data
charLen = ord(data[0:1])
if charLen == 0:
value = "NULL"
value = None
data = data[1:]
else:
data = data[1 + charLen + 8 :]
Expand All @@ -1822,13 +1825,13 @@ def parseRow(self, token, tuplemode=False):
value = binascii.b2a_hex(data[:charLen])
data = data[charLen:]
else:
value = "NULL"
value = None

elif _type == TDS_TEXTTYPE:
# Skip the pointer data
charLen = ord(data[0:1])
if charLen == 0:
value = "NULL"
value = None
data = data[1:]
else:
data = data[1 + charLen + 8 :]
Expand All @@ -1838,7 +1841,7 @@ def parseRow(self, token, tuplemode=False):
value = data[:charLen]
data = data[charLen:]
else:
value = "NULL"
value = None

elif (_type == TDS_BIGVARBINTYPE) | (_type == TDS_BIGBINARYTYPE):
charLen = struct.unpack("<H", data[: struct.calcsize("<H")])[0]
Expand All @@ -1847,23 +1850,21 @@ def parseRow(self, token, tuplemode=False):
value = binascii.b2a_hex(data[:charLen])
data = data[charLen:]
else:
value = "NULL"
value = None

elif (
(_type == TDS_DATETIM4TYPE)
| (_type == TDS_DATETIMNTYPE)
| (_type == TDS_DATETIMETYPE)
):
value = ""
value = None
if _type == TDS_DATETIMNTYPE:
# For DATETIMNTYPE, the only valid lengths are 0x04 and 0x08, which map to smalldatetime and
# datetime SQL data _types respectively.
if ord(data[0:1]) == 4:
_type = TDS_DATETIM4TYPE
elif ord(data[0:1]) == 8:
_type = TDS_DATETIMETYPE
else:
value = "NULL"
data = data[1:]
if _type == TDS_DATETIMETYPE:
# datetime is represented in the following sequence:
Expand Down Expand Up @@ -1892,7 +1893,7 @@ def parseRow(self, token, tuplemode=False):
timeValue = struct.unpack("<H", data[: struct.calcsize("<H")])[0]
data = data[struct.calcsize("<H") :]
baseDate = datetime.date(1900, 1, 1)
if value != "NULL":
if value is not None:
dateValue = datetime.date.fromordinal(
baseDate.toordinal() + dateValue
)
Expand Down Expand Up @@ -1933,7 +1934,7 @@ def parseRow(self, token, tuplemode=False):
value = struct.unpack(fmt, data[:valueSize])[0]
data = data[valueSize:]
else:
value = "NULL"
value = None

elif _type == TDS_MONEYNTYPE:
valueSize = ord(data[:1])
Expand All @@ -1951,7 +1952,7 @@ def parseRow(self, token, tuplemode=False):
value = Decimal(raw) / Decimal(10000)
data = data[valueSize:]
else:
value = "NULL"
value = None

elif _type == TDS_BIGCHARTYPE:
# print "BIGC"
Expand Down Expand Up @@ -1991,7 +1992,7 @@ def parseRow(self, token, tuplemode=False):
value = datetime.date.fromordinal(dateValue)
data = data[valueSize:]
else:
value = "NULL"
value = None

elif (_type == TDS_BITTYPE) | (_type == TDS_INT1TYPE):
# print "BITTYPE"
Expand All @@ -2003,7 +2004,7 @@ def parseRow(self, token, tuplemode=False):
data = data[1:]

if valueLen == 0:
value = "NULL"
value = None
else:
raw = data[:valueLen]
data = data[valueLen:]
Expand Down Expand Up @@ -2032,7 +2033,7 @@ def parseRow(self, token, tuplemode=False):
else:
value = data[:valueSize]
else:
value = "NULL"
value = None
data = data[valueSize:]

elif _type == TDS_INTNTYPE:
Expand All @@ -2054,7 +2055,7 @@ def parseRow(self, token, tuplemode=False):
value = struct.unpack(fmt, data[:valueSize])[0]
data = data[valueSize:]
else:
value = "NULL"
value = None
elif _type == TDS_SSVARIANTTYPE:
totalLength = struct.unpack("<L", data[:4])[0]

Expand Down
Loading