Skip to content

Commit

Permalink
characters in byte strings are now always numbers, no need to call ord
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@23877 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Sep 21, 2019
1 parent d5ed7b6 commit b8c1ac6
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 44 deletions.
16 changes: 5 additions & 11 deletions src/xpra/codecs/nvenc/encoder.pyx
Expand Up @@ -1112,22 +1112,16 @@ cdef GUID c_parseguid(src) except *:
if len(bsrc)!=len(sample_guid):
raise Exception("invalid GUID format: expected %s characters but got %s" % (len(sample_guid), len(src)))
cdef int i
#deal with differences between python 2 and 3:
def _ord(c):
try:
return ord(c)
except:
return c
#validate the input bytestring:
hexords = tuple(_ord(x) for x in b"0123456789ABCDEF")
hexords = tuple(x for x in b"0123456789ABCDEF")
for i in range(len(sample_guid)):
if _ord(sample_guid[i])==_ord(b"-"):
if sample_guid[i]==ord(b"-"):
#dash must be in the same place:
if _ord(bsrc[i])!=_ord(b"-"):
if bsrc[i]!=ord(b"-"):
raise Exception("invalid GUID format: character at position %s is not '-': %s" % (i, src[i]))
else:
#must be an hex number:
c = _ord(bsrc[i])
c = bsrc[i]
if c not in hexords:
raise Exception("invalid GUID format: character at position %s is not in hex: %s" % (i, chr(c)))
parts = bsrc.split(b"-") #ie: ["CE788D20", "AAA9", ...]
Expand All @@ -1138,7 +1132,7 @@ cdef GUID c_parseguid(src) except *:
log("c_parseguid bytes(%s)=%r", part, binv)
v = 0
for j in range(s):
c = _ord(binv[j])
c = binv[j]
v += c<<((s-j-1)*8)
nparts.append(v)
cdef GUID guid
Expand Down
14 changes: 6 additions & 8 deletions src/xpra/net/bencode/bencode.py
Expand Up @@ -22,8 +22,6 @@ def strindex(s, char, start):
return -1
return i
#the values end up being ints..
def cv(x):
return ord(x)
def b(x):
if isinstance(x, bytes):
return x
Expand All @@ -34,18 +32,18 @@ def decode_int(x, f):
f += 1
newf = strindex(x, 'e', f)
n = int(x[f:newf])
if x[f] == cv('-'):
if x[f + 1] == cv('0'):
if x[f] == ord('-'):
if x[f + 1] == ord('0'):
raise ValueError
elif x[f] == cv('0') and newf != f+1:
elif x[f] == ord('0') and newf != f+1:
raise ValueError
return (n, newf+1)

def decode_string(x, f):
colon = strindex(x, ':', f)
assert colon>=0
n = int(x[f:colon])
if x[f] == cv('0') and colon != f+1:
if x[f] == ord('0') and colon != f+1:
raise ValueError
colon += 1
return (x[colon:colon+n], colon+n)
Expand All @@ -56,7 +54,7 @@ def decode_unicode(x, f):

def decode_list(x, f):
r, f = [], f+1
while x[f] != cv('e'):
while x[f] != ord('e'):
fn = decode_func.get(x[f])
if not fn:
raise ValueError("invalid list entry: %s" % (x[f:]))
Expand All @@ -67,7 +65,7 @@ def decode_list(x, f):
def decode_dict(x, f):
r, f = {}, f+1
#lastkey = None
while x[f] != cv('e'):
while x[f] != ord('e'):
fn = decode_func.get(x[f])
if not fn:
raise ValueError("invalid dict key: %s" % (x[f:]))
Expand Down
2 changes: 1 addition & 1 deletion src/xpra/net/protocol.py
Expand Up @@ -828,7 +828,7 @@ def do_read_parse_thread_loop(self):
if payload_size<0:
#try to handle the first buffer:
buf = read_buffers[0]
if not header and buf[0] not in ("P", ord("P")):
if not header and buf[0]!=ord("P"):
self._invalid_header(buf, "invalid packet header byte %s" % buf)
return
#how much to we need to slice off to complete the header:
Expand Down
9 changes: 5 additions & 4 deletions src/xpra/platform/displayfd.py
Expand Up @@ -80,13 +80,14 @@ def parse_displayfd(buf, err):
if not buf:
err("did not provide a display number using displayfd")
return None
if buf[-1] not in (b'\n', ord(b"\n")):
err("output not terminated by newline: %s" % buf)
if not buf.endswith(b"\n"):
err("output not terminated by newline: '%s'" % buf)
return None
buf = buf.rstrip(b"\n\r")
try:
n = int(buf[:-1])
n = int(buf)
except ValueError:
err("display number is not a valid number: %s" % buf[:-1])
err("display number is not a valid number: %s" % buf)
if n<0 or n>=2**16:
err("provided an invalid display number: %s" % n)
return n
11 changes: 4 additions & 7 deletions src/xpra/server/server_core.py
Expand Up @@ -1041,7 +1041,7 @@ def ssl_wrap():
#verify that this isn't plain HTTP / xpra:
if peek_data:
packet_type = None
if peek_data[0] in ("P", ord("P")):
if peek_data[0]==ord("P"):
packet_type = "xpra"
elif line1.find(b"HTTP/")>0:
packet_type = "HTTP"
Expand Down Expand Up @@ -1080,7 +1080,7 @@ def ssl_wrap():
return
ssllog("ws socket receiving ssl, upgrading")
conn = ssl_wrap()
elif len(peek_data)>=2 and peek_data[0] in ("P", ord("P") and peek_data[1] in ("\x00", 0)):
elif len(peek_data)>=2 and peek_data[0]==ord("P") and peek_data[1]==0:
self.new_conn_err(conn, sock, socktype, socket_info, "xpra",
"packet looks like a plain xpra packet")
return
Expand Down Expand Up @@ -1243,7 +1243,7 @@ def may_wrap_socket(self, conn, socktype, peek_data=b"", line1=b""):
if not peek_data:
netlog("may_wrap_socket: no data, not wrapping")
return True, conn, peek_data
if peek_data[0] in ("P", ord("P")) and peek_data[:5]!=b"POST ":
if peek_data[0]==ord("P") and peek_data[:5]!=b"POST ":
netlog("may_wrap_socket: xpra protocol header '%s', not wrapping", peek_data[0])
#xpra packet header, no need to wrap this connection
return True, conn, peek_data
Expand Down Expand Up @@ -1308,10 +1308,7 @@ def invalid_header(self, proto, data, msg=""):
proto.gibberish(err, data)

def guess_header_protocol(self, v):
try:
c = ord(v[0])
except TypeError:
c = int(v[0])
c = int(v[0])
s = bytestostr(v)
netlog("guess_header_protocol(%r) first char=%#x", repr_ellipsized(s), c)
if c==0x16:
Expand Down
7 changes: 1 addition & 6 deletions src/xpra/util.py
Expand Up @@ -837,12 +837,7 @@ def nonl(x):
return str(x).replace("\n", "\\n").replace("\r", "\\r")

def xor(s1,s2):
def _ord(v):
try:
return ord(v)
except TypeError:
return int(v)
return ''.join(chr(_ord(a) ^ _ord(b)) for a,b in zip(s1,s2))
return b''.join(chr(a ^ b) for a,b in zip(s1,s2))

def engs(v):
if isinstance(v, int):
Expand Down
8 changes: 1 addition & 7 deletions src/xpra/x11/x11_server_base.py
Expand Up @@ -207,13 +207,7 @@ def set_icc_profile(self):
screenlog("set_icc_profile() icc data for %s: %s (%i bytes)",
ui_clients[0], hexstr(data or ""), len(data or ""))
from xpra.x11.gtk_x11.prop import prop_set
#each CARD32 contains just one 8-bit value - don't ask me why
def o(x):
try:
return ord(x)
except:
return x
prop_set(self.root_window, "_ICC_PROFILE", ["u32"], [o(x) for x in data])
prop_set(self.root_window, "_ICC_PROFILE", ["u32"], [ord(x) for x in data])
prop_set(self.root_window, "_ICC_PROFILE_IN_X_VERSION", "u32", 0*100+4) #0.4 -> 0*100+4*1

def reset_icc_profile(self):
Expand Down

0 comments on commit b8c1ac6

Please sign in to comment.