Make defines.is_printable support bytes

`uefi_search.py` calls `is_printable` with bytes, in:

    m = re.compile(bytes(rule['regexp'], 'utf-8')).search(efi.Image)
    if m:
        match_result |= MATCH_REGEXP
        _str = m.group(0)
        hexver = binascii.hexlify(_str)
        printver = f" ('{_str}')" if defines.is_printable(_str) else ''

... because `_str` has actually type `bytes`, not `str`.

When `is_printable` is called with bytes, it always return `False`,
because in

    set(seq).issubset(set(string.printable))

`set(seq)` is a set of integers and `set(string.printable)` is a set of
strings.

Fix this by always converting the parameter to string, using
`bytestostring`. This is not the most efficient way of doing this (a
more efficient would be `set(seq).issubset(set(string.printable.encode()))`
with some caching of the second set) but it is simple and makes caller
less likely to use the function in an unsupported way.

Signed-off-by: Nicolas Iooss <nicolas.iooss_git@polytechnique.org>
This commit is contained in:
Nicolas Iooss
2023-06-02 21:04:55 +00:00
committed by Nathaniel Mitchell
parent c2bcf585ec
commit 36ca6fa5a7
+2 -2
View File
@@ -197,8 +197,8 @@ def os_version() -> Tuple[str, str, str, str]:
return platform.system(), platform.release(), platform.version(), platform.machine()
def is_printable(seq) -> bool:
return set(seq).issubset(set(string.printable))
def is_printable(seq: AnyStr) -> bool:
return set(bytestostring(seq)).issubset(set(string.printable))
def is_hex(maybe_hex: Iterable) -> bool: