]> code.delx.au - pymsnt/commitdiff
Now runs on little endian machines.
authorjamesbunton <jamesbunton@55fbd22a-6204-0410-b2f0-b6c764c7e90a>
Fri, 23 Dec 2005 15:00:20 +0000 (15:00 +0000)
committerjamesbunton <jamesbunton@55fbd22a-6204-0410-b2f0-b6c764c7e90a>
Fri, 23 Dec 2005 15:00:20 +0000 (15:00 +0000)
git-svn-id: http://delx.cjb.net/svn/pymsnt/trunk@64 55fbd22a-6204-0410-b2f0-b6c764c7e90a

committer: jamesbunton <jamesbunton@55fbd22a-6204-0410-b2f0-b6c764c7e90a>

src/tlib/msn/msn.py

index a008c5510637e495c932a16fdf67c0ba3b797c52..2759e8f923bab4360db50beb5c388381eef18fb9 100644 (file)
@@ -108,7 +108,7 @@ from twisted.xish.domish import unescapeFromXml
 from tlib import xmlw
 
 # System imports
-import types, operator, os, sys, base64, random, struct, random, sha, base64, StringIO
+import types, operator, os, sys, base64, random, struct, random, sha, base64, StringIO, array, codecs
 from urllib import quote, unquote
 
 
@@ -194,6 +194,17 @@ def ljust(s, n, c):
     """ Needed for Python 2.3 compatibility """
     return s + (n-len(s))*c
 
+if sys.byteorder == "little":
+       def utf16net(s):
+               """ Encodes to utf-16 and ensures network byte order. Strips the BOM """
+               a = array.array("h", s.encode("utf-16")[2:])
+               a.byteswap()
+               return a.tostring()
+else:
+       def utf16net(s):
+               """ Encodes to utf-16 and ensures network byte order. Strips the BOM """
+               return s.encode("utf-16")[2:]
+
 def b64enc(s):
     return base64.encodestring(s).replace("\n", "")
 
@@ -2481,7 +2492,7 @@ class FileContext:
         if MSNP2PDEBUG: print "FileContext packing:", self.filename, self.filesize
         data = struct.pack("<LLQL", 638, 0x03, self.filesize, 0x01)
         data = data[:-1] # Uck, weird, but it works
-        data += self.filename.encode("utf-16")[2:] # Strip off the utf16 BOM
+        data += utf16net(self.filename)
         data = ljust(data, 570, '\0')
         data += struct.pack("<L", 0xFFFFFFFF)
         data = ljust(data, 638, '\0')
@@ -2491,7 +2502,7 @@ class FileContext:
         self.filesize = struct.unpack("<Q", packet[8:16])[0]
         chunk = packet[19:540]
         chunk = chunk[:chunk.find('\x00\x00')]
-        self.filename = unicode(chunk.decode("utf-16"))
+        self.filename = unicode((codecs.BOM_UTF16_BE + chunk).decode("utf-16"))
         if MSNP2PDEBUG: print "FileContext parsed:", self.filesize, self.filename