Skip to content

Commit

Permalink
Fix nbios.NCBStruct packing
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Oct 15, 2024
1 parent c717bfa commit dfe8dc7
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions win32/Lib/netbios.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

import struct
from collections.abc import Iterable

import win32wnet

Expand Down Expand Up @@ -191,28 +194,26 @@ def Netbios(ncb):


class NCBStruct:
def __init__(self, items):
def __init__(self, items: Iterable[tuple[str, str]]) -> None:
self._format = "".join([item[0] for item in items])
self._items = items
self._buffer_ = win32wnet.NCBBuffer(struct.calcsize(self._format))

for format, name in self._items:
if len(format) == 1:
if format == "c":
val = "\0"
val: bytes | int = b"\0"
else:
val = 0
else:
l = int(format[:-1])
val = "\0" * l
val = b"\0" * l
self.__dict__[name] = val

def _pack(self):
vals = []
for format, name in self._items:
vals.append(self.__dict__.get(name))
vals = [self.__dict__.get(name) for format, name in self._items]

self._buffer_[:] = struct.pack(*(self._format,) + tuple(vals))
self._buffer_[:] = struct.pack(self._format, *vals)

def _unpack(self):
items = struct.unpack(self._format, self._buffer_)
Expand Down

0 comments on commit dfe8dc7

Please sign in to comment.