[tor-commits] [flashproxy/js] More efficient apply_mask.

dcf at torproject.org dcf at torproject.org
Sat Apr 7 12:16:54 UTC 2012


commit 73e7ffaed410124daecc8edb3fab662a6414e8ee
Author: David Fifield <david at bamsoftware.com>
Date:   Sat Apr 7 04:36:19 2012 -0700

    More efficient apply_mask.
---
 connector.py |   22 +++++++++++++++++-----
 1 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/connector.py b/connector.py
index 23de63d..1689c33 100755
--- a/connector.py
+++ b/connector.py
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import array
 import base64
 import cStringIO
 import getopt
@@ -121,11 +122,22 @@ def format_addr(addr):
 
 
 def apply_mask(payload, mask_key):
-    result = []
-    for i, c in enumerate(payload):
-        mc = chr(ord(payload[i]) ^ ord(mask_key[i%4]))
-        result.append(mc)
-    return "".join(result)
+    result = array.array("l")
+    result.fromstring(payload[:len(payload) // 4 * 4])
+    m, = struct.unpack("=l", mask_key)
+    i = 0
+    while i < len(result):
+        result[i] ^= m
+        i += 1
+    result = result.tostring()
+    i *= 4
+    if i < len(payload):
+        remainder = []
+        while i < len(payload):
+            remainder.append(chr(ord(payload[i]) ^ ord(mask_key[i%4])))
+            i += 1
+        result = result + "".join(remainder)
+    return result
 
 class WebSocketFrame(object):
     def __init__(self):





More information about the tor-commits mailing list