diff --git a/facedancer/descriptor.py b/facedancer/descriptor.py index f21cc170..55047793 100644 --- a/facedancer/descriptor.py +++ b/facedancer/descriptor.py @@ -100,24 +100,27 @@ def __init__(self): self.indexes = {} + def add_string(self, string, index=None): + """Add a Python string as a new string descriptor, and return an index. - def _allocate_index(self): - """ Grabs a unique, ascending string index for the current string. """ - - index = self.next_index - self.next_index += 1 - - return index - + The specified index is used for the new string descriptor, overwriting + any previous descriptor with the same index. If an index is not + specified, a new, unique, incrementing index is allocated. + """ - def add_string(self, string): - """ Adds a python string to the string manager, and returns an index. """ + if index is None: + index = self.next_index - index = self._allocate_index() + if index in self.descriptors: + old_string = self.descriptors[index].python_string + self.indexes.pop(old_string) self.descriptors[index] = USBStringDescriptor.from_string(string, index=index) self.indexes[string] = index + while self.next_index in self.descriptors: + self.next_index += 1 + return index diff --git a/facedancer/device.py b/facedancer/device.py index a8984116..53c5a64f 100644 --- a/facedancer/device.py +++ b/facedancer/device.py @@ -697,7 +697,10 @@ def get_configuration_descriptor(self, index: int) -> bytes: def handle_get_supported_languages_descriptor(self) -> bytes: - """ Returns the special string-descriptor-zero that indicates which languages are supported. """ + """Return the special string-descriptor-zero that indicates which languages are supported.""" + + if self.supported_languages is None: + return None # Our string descriptor is going to have two header bytes, plus two bytes # for each language.