diff --git a/copyparty/httpsrv.py b/copyparty/httpsrv.py index d40373f7..619de298 100644 --- a/copyparty/httpsrv.py +++ b/copyparty/httpsrv.py @@ -5,7 +5,6 @@ import os import sys import time import base64 -import struct import socket import threading @@ -27,6 +26,7 @@ except ImportError: sys.exit(1) from .__init__ import E, MACOS +from .util import spack from .httpconn import HttpConn @@ -199,7 +199,7 @@ class HttpSrv(object): except: pass - v = base64.urlsafe_b64encode(struct.pack(">xxL", int(v))) + v = base64.urlsafe_b64encode(spack(b">xxL", int(v))) self.cb_v = v.decode("ascii")[-4:] self.cb_ts = time.time() return self.cb_v diff --git a/copyparty/szip.py b/copyparty/szip.py index fa573850..2b4c189e 100644 --- a/copyparty/szip.py +++ b/copyparty/szip.py @@ -4,15 +4,14 @@ from __future__ import print_function, unicode_literals import os import time import zlib -import struct from datetime import datetime from .sutil import errdesc -from .util import yieldfile, sanitize_fn +from .util import yieldfile, sanitize_fn, spack, sunpack def dostime2unix(buf): - t, d = struct.unpack("> 5) & 0x3F @@ -36,13 +35,13 @@ def unixtime2dos(ts): bd = ((dy - 1980) << 9) + (dm << 5) + dd bt = (th << 11) + (tm << 5) + ts // 2 - return struct.pack("i", b"idgi") + spack = struct.pack + sunpack = struct.unpack +except: + + def spack(f, *a, **ka): + return struct.pack(f.decode("ascii"), *a, **ka) + + def sunpack(f, *a, **ka): + return struct.unpack(f.decode("ascii"), *a, **ka) + + surrogateescape.register_surrogateescape() FS_ENCODING = sys.getfilesystemencoding() if WINDOWS and PY2: @@ -231,7 +245,7 @@ def nuprint(msg): def rice_tid(): tid = threading.current_thread().ident - c = struct.unpack(b"B" * 5, struct.pack(b">Q", tid)[-5:]) + c = sunpack(b"B" * 5, spack(b">Q", tid)[-5:]) return "".join("\033[1;37;48;5;{}m{:02x}".format(x, x) for x in c) + "\033[0m" @@ -1068,10 +1082,7 @@ def gzip_orig_sz(fn): with open(fsenc(fn), "rb") as f: f.seek(-4, 2) rv = f.read(4) - try: - return struct.unpack(b"I", rv)[0] - except: - return struct.unpack("I", rv)[0] + return sunpack(b"I", rv)[0] def py_desc():