[tor-commits] [flashproxy/js] Rewrite apply_mask to use bytes.

dcf at torproject.org dcf at torproject.org
Sat Apr 7 17:55:49 UTC 2012


commit 4c9298cfed95f32561a61874d329e8328410bf62
Author: David Fifield <david at bamsoftware.com>
Date:   Sat Apr 7 10:31:56 2012 -0700

    Rewrite apply_mask to use bytes.
    
    Unlike the struct module, the array module doesn't guarantee the size of
    types. apply_mask failed because it assumed that a long "l" was four
    bytes, when it could be 8 (or really, anything greater than 4).
---
 connector.py |   27 ++++++++++++++-------------
 1 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/connector.py b/connector.py
index 8653907..d10a6ab 100755
--- a/connector.py
+++ b/connector.py
@@ -126,22 +126,23 @@ def format_addr(addr):
 
 
 def apply_mask(payload, mask_key):
-    result = array.array("l")
-    result.fromstring(payload[:len(payload) // 4 * 4])
-    m, = struct.unpack("=l", mask_key)
+    result = array.array("B", payload)
+    m = array.array("B", mask_key)
     i = 0
+    while i < len(result) - 7:
+        result[i] ^= m[0]
+        result[i+1] ^= m[1]
+        result[i+2] ^= m[2]
+        result[i+3] ^= m[3]
+        result[i+4] ^= m[0]
+        result[i+5] ^= m[1]
+        result[i+6] ^= m[2]
+        result[i+7] ^= m[3]
+        i += 8
     while i < len(result):
-        result[i] ^= m
+        result[i] ^= m[i%4]
         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
+    return result.tostring()
 
 class WebSocketFrame(object):
     def __init__(self):





More information about the tor-commits mailing list