1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-01-13 17:16:23 +01:00

Simplify rpmquery.unpack_string

There is no need to unpack a single byte because it is not
affected by (byte) endianness (and that's what struct.unpack is
about). Moreover, rpmquery.unpack_string now supports an optional
encoding parameter, which could be used by the python3 port to
decode a string. Note: in general we cannot assume that all strings
in a rpm are utf-8 encoded (it is possible to build a rpm that
contains illegal utf-8 sequences).
This commit is contained in:
Marcus Huewe 2019-01-14 16:40:09 +01:00
parent e608200414
commit 28eeff8683

View File

@ -370,16 +370,15 @@ class RpmQuery(packagequery.PackageQuery, packagequery.PackageQueryResult):
def filename(name, epoch, version, release, arch):
return '%s-%s-%s.%s.rpm' % (name, version, release, arch)
def unpack_string(data):
def unpack_string(data, encoding=None):
"""unpack a '\\0' terminated string from data"""
val = ''
for c in data:
c, = struct.unpack('!c', c)
if c == '\0':
break
else:
val += c
return val
idx = data.find(b'\0')
if idx == -1:
raise ValueError('illegal string: not \\0 terminated')
data = data[:idx]
if encoding is not None:
data = data.decode(encoding)
return data
if __name__ == '__main__':
import sys