"Fossies" - the Fresh Open Source Software Archive

Member "gnash-0.8.10/cygnal/libamf/amf.h" (19 Jan 2012, 14489 Bytes) of package /linux/www/old/gnash-0.8.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // 
    2 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
    3 //   Free Software Foundation, Inc
    4 // 
    5 // This program is free software; you can redistribute it and/or modify
    6 // it under the terms of the GNU General Public License as published by
    7 // the Free Software Foundation; either version 3 of the License, or
    8 // (at your option) any later version.
    9 // 
   10 // This program is distributed in the hope that it will be useful,
   11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 // GNU General Public License for more details.
   14 // 
   15 // You should have received a copy of the GNU General Public License
   16 // along with this program; if not, write to the Free Software
   17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   18 
   19 // This file is for the low level support for encoding and decoding AMF objects.
   20 // As this class has no data associated with it, all the methods are static as
   21 // they are for convenience only.
   22 // All the encoding methods return a Buffer class, which is simply an array on
   23 // of unsigned bytes, and a byte count.
   24 // The only extraction classes parse either a raw AMF object or the larger
   25 // "variable"
   26 
   27 #ifndef _AMF_H_
   28 #define _AMF_H_
   29 
   30 #include <string>
   31 #include <cstring>
   32 #include <boost/cstdint.hpp>
   33 #include <boost/shared_ptr.hpp>
   34 
   35 #include "element.h"
   36 #include "dsodefs.h"
   37 
   38 /// Action Message Format specific classes of libamf.
   39 namespace cygnal
   40 {
   41 
   42 // forward declaration
   43 class Buffer;
   44 
   45 /// All numbers in AMF format are 8 byte doubles.
   46 const size_t AMF0_NUMBER_SIZE = 0x08;
   47 
   48 /// \brief The header size in bytes of an common AMF object.
   49 /// The size of an AMF header is a type field (1 byte), followed by a
   50 /// length field. (short)
   51 const boost::uint8_t AMF_HEADER_SIZE = 3;
   52 
   53 /// \brief  The header size of a property.
   54 /// A property is a little different. It always assumes the the
   55 /// first field is a string that's the property name, then the
   56 /// type byte like a regular AMF object and length is used for the
   57 /// data. So a property object header is then only 5 bytes instead
   58 /// of the 6 that one assumes would be used.
   59 const boost::uint8_t AMF_PROP_HEADER_SIZE = 5;
   60 
   61 /// AMF version 0 is supported by default
   62 const boost::uint8_t AMF_VERSION = 0;
   63 
   64 /// For terminating sequences, a byte with value 0x09 is used.
   65 const boost::uint8_t TERMINATOR = 0x09;
   66 
   67 /// \brief The maximum size for a string.
   68 /// As if there is a parsing error, we'll often see the symptom of the length
   69 /// for the following value is bogus. Although the length field is a short, it
   70 /// seems silly to assume we'll ever see a string 65,000 characters long. Still,
   71 /// it makes sense to make this an adjustable thing.
   72 const boost::uint16_t SANE_STR_SIZE = 65535;
   73 
   74 /// Binary representation of an ActionScript object.
   75 //
   76 /// AMF is used to send objects, whether to a SharedObject .sol file,
   77 /// a memory based LocalConnection segment, or over an RTMP connection
   78 /// for streaming.
   79 ///
   80 class DSOEXPORT AMF {
   81 public:
   82 
   83     /// Types of SharedObjects that can be serialized or deserialized.
   84     typedef enum {
   85         CONNECT = 0x01,
   86         DISCONNECT = 0x02,
   87         SET_ATTRIBUTE = 0x03,
   88         UPDATE_DATA = 0x04,
   89         UPDATE_ATTRIBUTE = 0x05,
   90         SEND_MESSAGE = 0x06,
   91         STATUS = 0x07,
   92         CLEAR_DATA = 0x08,
   93         DELETE_DATA = 0x09,
   94         DELETE_ATTRIBYTE = 0x0a,
   95         INITIAL_DATA = 0x0b
   96     } shared_obj_types_e;
   97 
   98     /// Type of file being streamed.
   99     typedef enum {
  100     FILETYPE_ERROR = -1,
  101     FILETYPE_NONE = 0,
  102     FILETYPE_HTML,
  103     FILETYPE_SWF,
  104     FILETYPE_VIDEO,
  105     FILETYPE_AUDIO,
  106     FILETYPE_MP3,
  107     FILETYPE_FCS,
  108     FILETYPE_OSCP
  109     } filetype_e;
  110 
  111     /// Create a new AMF object.
  112     //
  113     /// As most of the methods in the AMF class a static, this
  114     /// is primarily only used when encoding complex objects
  115     /// where the byte count is accumulated.
  116     ///
  117     AMF();
  118 
  119     /// Delete the allocated AMF object
  120     ~AMF();
  121 
  122     /// @name Encoding methods
  123     ///
  124     ///     Methods for encoding data into big endian formatted
  125     ///     raw AMF data. Note that while we could have had a
  126     ///     single overloaded encode method, this is more
  127     ///     explicit, which when it comes to manipulating binary
  128     ///     protocols make the code much more readable.
  129     ///
  130     /// @{
  131 
  132     /// Encode a string object to its serialized representation.
  133     // 
  134     /// @param str a string value
  135     ///
  136     /// @return a binary AMF packet in big endian format
  137     ///
  138     static boost::shared_ptr<Buffer> encodeString(const std::string &str);
  139     
  140     /// Encode an array of ASCII bytes to its serialized representation.
  141     // 
  142     /// @param data The data to serialize into big endian format
  143     /// 
  144     /// @param size The size of the data in bytes
  145     ///
  146     /// @return a binary AMF packet in big endian format
  147     ///
  148     static boost::shared_ptr<Buffer> encodeString(boost::uint8_t *data,
  149                           size_t size);
  150 
  151     /// Encode a String object to its serialized representation.
  152     //
  153     /// A NULL String is a string with no associated data.
  154     ///
  155     /// @return a binary AMF packet in big endian format
  156     ///
  157     static boost::shared_ptr<Buffer> encodeNullString();
  158 
  159     /// Encode a Boolean object to its serialized representation.
  160     //
  161     /// @param flag The boolean value to serialize.
  162     ///
  163     /// @return a binary AMF packet in big endian format
  164     ///
  165     static boost::shared_ptr<Buffer> encodeBoolean(bool flag);
  166 
  167     /// Encode an "Undefined" object to its serialized representation.
  168     //
  169     /// @return a binary AMF packet in big endian format
  170     ///
  171     static boost::shared_ptr<Buffer> encodeUndefined();
  172 
  173     /// Encode a NULL object to its serialized representation.
  174     //
  175     /// A NULL object is often used as a placeholder in RTMP.
  176     ///
  177     /// @return a binary AMF packet in big endian format
  178     ///
  179     static boost::shared_ptr<Buffer> encodeNull();
  180 
  181     /// Encode a "Unsupported" object to its serialized representation.
  182     //
  183     /// @return a binary AMF packet in big endian format
  184     ///
  185     static  boost::shared_ptr<Buffer> encodeUnsupported();
  186 
  187     /// Encode an XML object to its serialized representation.
  188     //
  189     /// @param data A pointer to the raw bytes that becomes the XML data.
  190     /// 
  191     /// @param nbytes The number of bytes to serialize.
  192     ///
  193     /// @return a binary AMF packet in big endian format
  194     ///
  195     static boost::shared_ptr<Buffer> encodeXMLObject(const boost::uint8_t *data,
  196                              size_t nbytes);
  197 
  198     /// Encode a Typed Object to its serialized representation.
  199     //
  200     /// @param data A pointer to the raw bytes that becomes the data.
  201     /// 
  202     /// @param size The number of bytes to serialize.
  203     ///
  204     /// @return a binary AMF packet in big endian format
  205     ///
  206     static boost::shared_ptr<Buffer> encodeTypedObject(const cygnal::Element &data);
  207 
  208     /// Encode a Reference to an object to its serialized representation.
  209     //
  210     /// @param data A pointer to the raw bytes that becomes the data.
  211     /// 
  212     /// @param size The number of bytes to serialize.
  213     ///
  214     /// @return a binary AMF packet in big endian format (header,data)
  215     ///
  216     static boost::shared_ptr<Buffer> encodeReference(boost::uint16_t index);
  217 
  218     /// Encode a Movie Clip (swf data) to its serialized representation.
  219     //
  220     /// @param data A pointer to the raw bytes that becomes the data.
  221     /// 
  222     /// @param size The number of bytes to serialize.
  223     ///
  224     /// @return a binary AMF packet in big endian format (header,data)
  225     ///
  226     static boost::shared_ptr<Buffer> encodeMovieClip(const boost::uint8_t *data,
  227                              size_t size);
  228 
  229     /// Encode an ECMA Array to its serialized representation.
  230     //
  231     /// An ECMA Array, also called a Mixed Array, contains any
  232     /// AMF data type as an item in the array.
  233     ///
  234     /// @param data A pointer to the raw bytes that becomes the data.
  235     /// 
  236     /// @param size The number of bytes to serialize.
  237     ///
  238     /// @return a binary AMF packet in big endian format
  239     ///
  240     static boost::shared_ptr<Buffer> encodeECMAArray(const cygnal::Element &data);
  241 
  242     /// Encode a Long String to its serialized representation.
  243     //
  244     /// @param data A pointer to the raw bytes that becomes the data.
  245     /// 
  246     /// @param size The number of bytes to serialize.
  247     ///
  248     /// @return a binary AMF packet in big endian format
  249     ///
  250     static boost::shared_ptr<Buffer> encodeLongString(const boost::uint8_t *data,
  251                               size_t size);
  252 
  253     /// Encode a Record Set to its serialized representation.
  254     //
  255     /// @param data A pointer to the raw bytes that becomes the data.
  256     /// 
  257     /// @param size The number of bytes to serialize.
  258     ///
  259     /// @return a binary AMF packet in big endian format
  260     ///
  261     static boost::shared_ptr<Buffer> encodeRecordSet(const boost::uint8_t *data,
  262                              size_t size);
  263 
  264     /// Encode a Date to its serialized representation.
  265     //
  266     /// @param data A pointer to the raw bytes that becomes the data.
  267     /// 
  268     /// @return a binary AMF packet in big endian format
  269     ///
  270     static boost::shared_ptr<Buffer> encodeDate(const boost::uint8_t *data);
  271 
  272     /// Encode a Strict Array to its serialized representation.
  273     //
  274     /// A Strict Array is one where all the items are the same
  275     /// data type, commonly either a number or a string.
  276     ///
  277     /// @param data A pointer to the raw bytes that becomes the data.
  278     /// 
  279     /// @param size The number of bytes to serialize.
  280     ///
  281     /// @return a binary AMF packet in big endian format (header,data)
  282     ///
  283     static boost::shared_ptr<Buffer> encodeStrictArray(const cygnal::Element &data);
  284     
  285     /// Encode an object to its serialized representation.
  286     //
  287     /// @param el A smart pointer to an Element class.
  288     /// 
  289     /// @return a binary AMF packet in big endian format
  290     ///
  291     static boost::shared_ptr<Buffer> encodeObject(const cygnal::Element &data);
  292 
  293     /// Encode the end of an object to its serialized representation.
  294     //
  295     /// @return a binary AMF packet in big endian format
  296     ///
  297     static boost::shared_ptr<Buffer> encodeObjectEnd();
  298 
  299     /// Encode a 64 bit number to its serialized representation.
  300     //
  301     /// @param num A double value to serialize.
  302     ///
  303     /// @return a binary AMF packet in big endian format
  304     ///
  305     static boost::shared_ptr<Buffer> encodeNumber(double num);
  306 
  307     /// Encode an Element to its serialized representation.
  308     //
  309     /// @param el A smart pointer to the Element to encode.
  310     ///
  311     /// @return a binary AMF packet in big endian format
  312     ///
  313     static boost::shared_ptr<Buffer> encodeElement(boost::shared_ptr<cygnal::Element> el);
  314 
  315     /// Encode an Element to its serialized representation.
  316     //
  317     /// @param el the Element to encode.
  318     ///
  319     /// @return a binary AMF packet in big endian format
  320     ///
  321     static boost::shared_ptr<Buffer> encodeElement(const cygnal::Element& el);
  322 
  323     /// Encode a variable to its serialized representation.
  324     //
  325     /// @param el A smart pointer to the Element to encode.
  326     ///
  327     /// @return a binary AMF packet in big endian format
  328     ///
  329     boost::shared_ptr<Buffer> encodeProperty(boost::shared_ptr<cygnal::Element> el);
  330 
  331     /// @} end of encoding methods 
  332 
  333     /// @name Decoding methods
  334     ///
  335     ///     Methods for extracting data from big endian formatted raw AMF data.
  336     ///
  337     /// @{
  338 
  339     /// Extract the AMF0 object type from the header.
  340     //
  341     /// @param in The raw data to extract values from.
  342     ///
  343     /// @return The data type from the header
  344     ///
  345     static Element::amf0_type_e extractElementHeader(boost::uint8_t *in)
  346                          { return *(reinterpret_cast<Element::amf0_type_e *>(in)); };
  347 
  348     /// Extract an AMF object from an array of raw bytes.
  349     //
  350     /// An AMF object is one of the support data types.
  351     ///
  352     /// @param in A real pointer to the raw data to start parsing from.
  353     ///
  354     /// @param tooFar A pointer to one-byte-past the last valid memory
  355     ///     address within the buffer.
  356     ///
  357     /// @return A smart ptr to an Element.
  358     ///
  359     /// @remarks May throw a ParserException
  360     ///
  361     boost::shared_ptr<cygnal::Element> extractAMF(boost::uint8_t *in, boost::uint8_t* tooFar);
  362 
  363     /// Extract an AMF object from an array of raw bytes.
  364     //
  365     /// @param buf A smart pointer to a Buffer to parse the data from.
  366     ///
  367     /// @return A smart ptr to an Element.
  368     ///
  369     /// @remarks May throw a ParserException
  370     ///
  371     boost::shared_ptr<cygnal::Element> extractAMF(boost::shared_ptr<Buffer> buf);
  372     
  373     /// Extract a Property.
  374     //
  375     ///     A Property is a standard AMF object preceeded by a
  376     ///     length and an ASCII name field. These are only used
  377     ///     with higher level ActionScript objects.
  378     ///
  379     /// @param in A real pointer to the raw data to start parsing from.
  380     ///
  381     /// @param tooFar A pointer to one-byte-past the last valid memory
  382     ///     address within the buffer.
  383     ///
  384     /// @return A smart ptr to an Element.
  385     ///
  386     /// @remarks May throw a ParserException
  387     ///
  388     boost::shared_ptr<cygnal::Element> extractProperty(boost::uint8_t *in, boost::uint8_t* tooFar);
  389 
  390     /// Extract a Property.
  391     //
  392     ///     A Property is a standard AMF object preceeded by a
  393     ///     length and an ASCII name field. These are only used
  394     ///     with higher level ActionScript objects.
  395     ///
  396     /// @param buf A smart pointer to an Buffer to parse the data from.
  397     ///
  398     /// @return A smart ptr to an Element.
  399     ///
  400     /// @remarks May throw a ParserException
  401     ///
  402     boost::shared_ptr<cygnal::Element> extractProperty(boost::shared_ptr<Buffer> buf);
  403 
  404     /// @} end of decoding methods 
  405 
  406     /// Get the total number of allocated bytes used when serializing.
  407     //
  408     /// @return The total allocated bytes.
  409     ///
  410     size_t totalsize() { return _totalsize; }
  411     
  412 private:
  413 
  414     /// The total number of bytes in serialized ActionScript object.
  415     size_t _totalsize;
  416 
  417 };
  418 
  419 /// Swap bytes in raw data.
  420 //
  421 /// This only swaps bytes if the host byte order is little endian.
  422 ///
  423 /// @param word The address of the data to byte swap.
  424 ///
  425 /// @param size The number of bytes in the data.
  426 ///
  427 /// @return A pointer to the raw data.
  428 ///
  429 DSOEXPORT void *swapBytes(void *word, size_t size);
  430 
  431 
  432 } // end of amf namespace
  433 
  434 // end of _AMF_H_
  435 #endif
  436 
  437 // local Variables:
  438 // mode: C++
  439 // indent-tabs-mode: t
  440 // End: