Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support decoding UTF-16 paths #50

Merged
merged 1 commit into from
Jul 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions u4pak.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,12 @@ def base_offset(self):
return self.offset

def read_path(stream: io.BufferedReader, encoding: str = 'utf-8') -> str:
path_len, = st_unpack('<I',stream.read(4))
return stream.read(path_len).rstrip(b'\0').decode(encoding).replace('/',os.path.sep)
path_len, = st_unpack('<i',stream.read(4))
if path_len < 0:
# in at least some format versions, this indicates a UTF-16 path
path_len = -2 * path_len
encoding = 'utf-16le'
return stream.read(path_len).decode(encoding).rstrip('\0').replace('/',os.path.sep)

def pack_path(path: str, encoding: str = 'utf-8') -> bytes:
encoded_path = path.replace(os.path.sep, '/').encode('utf-8') + b'\0'
Expand Down