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
""" 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", "")
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')
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