How to encode/decode raw bytes with AMF3?

102 views
Skip to first unread message

Russell Warren

unread,
May 29, 2012, 11:36:57 PM5/29/12
to AmFast
Typical encoding/decoding fails when any byte has an ascii value >=
0x80. Or rather, encoding doesn't complain, but decoding the result
throws an exception. A small example demonstrating this is below.

>>> from amfast.encoder import Encoder
>>> from amfast.decoder import Decoder
>>> enc = Encoder(amf3=True).encode([1, "\x80"])
>>> enc
'\t\x05\x01\x04\x01\x06\x03\x80'
>>> dec = Decoder(amf3=True).decode(enc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/russ/.virtualenvs/amfast_testing/lib/python2.7/site-
packages/amfast/decoder.py", line 29, in decode
return decode(self._getContext(val, amf3))
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 0:
invalid start byte


Is there an easy way to (de)serialize raw byte strings with amfast, as
per the example? It would be nice if 'dec' came back as what was
encoded ([1, "\x80"]).

Russell Warren

unread,
May 30, 2012, 12:11:02 AM5/30/12
to AmFast
Ok... after some digging I found AsByteArray in another post and can
work with that as per below.

>>> from amfast.encoder import Encoder
>>> from amfast.decoder import Decoder
>>> from amfast.class_def.as_types import AsByteArray
>>> enc = Encoder(amf3=True).encode([1, AsByteArray("\x80")])
>>> dec = Decoder(amf3=True).decode(enc)
>>> dec
[1, <amfast.class_def.as_types.AsByteArray object at 0x7ffc207e3b90>]
>>> dec[1] = dec[1].bytes
>>> dec == [1, "\x80"]
True


An option in Decoder.__init__ to decode to AMF3 ByteArrays directly to
non-unicode strings (rather than to AsByteArray instances and needing
to reference .bytes) would be awesome. I've looked into decoder.c and
see where to do it for myself, but will hold off on a patch until I
recover from the willies induced by Py_INCREF/Py_DECREF. I've not
done a direct extension yet (cython is just to easy).
Reply all
Reply to author
Forward
0 new messages