Skip to content

Commit

Permalink
Merge pull request #112 from mossmann/string-by-index
Browse files Browse the repository at this point in the history
Support specification of string descriptor index
  • Loading branch information
antoinevg authored Aug 26, 2024
2 parents 9052b04 + f4be389 commit f83bf3f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
25 changes: 14 additions & 11 deletions facedancer/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
5 changes: 4 additions & 1 deletion facedancer/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f83bf3f

Please sign in to comment.