gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] /srv/bzr/gnash/trunk r11958: Drop a load of unused functi


From: Benjamin Wolsey
Subject: [Gnash-commit] /srv/bzr/gnash/trunk r11958: Drop a load of unused functions that were cluttering the IOChannel interface.
Date: Mon, 22 Feb 2010 08:26:28 +0100
User-agent: Bazaar (2.0.3)

------------------------------------------------------------
revno: 11958 [merge]
committer: Benjamin Wolsey <address@hidden>
branch nick: trunk
timestamp: Mon 2010-02-22 08:26:28 +0100
message:
  Drop a load of unused functions that were cluttering the IOChannel interface.
  They may be reimplemented as free functions using the existing interface in
  the unlikely event that they are ever needed.
  
  Split AMF implementation into low-level (basic types, no VM needed) and
  high-level (as_value and as_object, VM needed) parts.
added:
  libbase/AMF.cpp
  libbase/AMF.h
renamed:
  libcore/AMF.cpp => libcore/AMFConverter.cpp
  libcore/AMF.h => libcore/AMFConverter.h
modified:
  libbase/IOChannel.cpp
  libbase/IOChannel.h
  libbase/Makefile.am
  libbase/tu_file.h
  libcore/Makefile.am
  libcore/as_value.cpp
  libcore/asobj/flash/net/LocalConnection_as.cpp
  libcore/asobj/flash/net/NetConnection_as.cpp
  libcore/asobj/flash/net/NetStream_as.cpp
  libcore/asobj/flash/net/SharedObject_as.cpp
  libcore/AMFConverter.cpp
  libcore/AMFConverter.h
=== added file 'libbase/AMF.cpp'
--- a/libbase/AMF.cpp   1970-01-01 00:00:00 +0000
+++ b/libbase/AMF.cpp   2010-02-20 08:35:06 +0000
@@ -0,0 +1,196 @@
+// AMFConverter.h   High-level functions for converting as_values to AMF.
+// 
+//   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
+//   Foundation, Inc
+// 
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+// 
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+// This file provides high-level function objects for converting ActionScript
+// values to AMF buffers and vice-versa.
+
+#include <map>
+#include <algorithm>
+
+#include "log.h"
+#include "SimpleBuffer.h"
+#include "AMF.h"
+
+// Define this macro to make AMF parsing verbose
+//#define GNASH_DEBUG_AMF_DESERIALIZE 1
+
+// Define this macto to make AMF writing verbose
+// #define GNASH_DEBUG_AMF_SERIALIZE 1
+
+namespace gnash {
+
+namespace AMF {
+
+bool
+readBoolean(const boost::uint8_t*& pos, const boost::uint8_t* _end)
+{
+    if (pos == _end) {
+        throw AMFException("Read past _end of buffer for boolean type");
+    }
+
+    const bool val = *pos;
+    ++pos;
+#ifdef GNASH_DEBUG_AMF_DESERIALIZE
+    log_debug("amf0 read bool: %d", val);
+#endif
+    return val;
+}
+
+double
+readNumber(const boost::uint8_t*& pos, const boost::uint8_t* end)
+{
+
+    if (end - pos < 8) {
+        throw AMFException("Read past _end of buffer for number type");
+    }
+
+    double d;
+    // TODO: may we avoid a copy and swapBytes call
+    //       by bitshifting b[0] trough b[7] ?
+    std::copy(pos, pos + 8, reinterpret_cast<char*>(&d));
+    pos += 8; 
+    swapBytes(&d, 8);
+
+#ifdef GNASH_DEBUG_AMF_DESERIALIZE
+    log_debug("amf0 read double: %e", dub);
+#endif
+
+    return d;
+}
+
+std::string
+readString(const boost::uint8_t*& pos, const boost::uint8_t* end)
+{
+    if (end - pos < 2) {
+        throw AMFException("Read past _end of buffer for string length");
+    }
+
+    const boost::uint16_t si = readNetworkShort(pos);
+    pos += 2;
+
+    if (end - pos < si) {
+        throw AMFException("Read past _end of buffer for string type");
+    }
+
+    const std::string str(reinterpret_cast<const char*>(pos), si);
+    pos += si;
+#ifdef GNASH_DEBUG_AMF_DESERIALIZE
+    log_debug("amf0 read string: %s", str);
+#endif
+    return str;
+}
+
+std::string
+readLongString(const boost::uint8_t*& pos, const boost::uint8_t* end)
+{
+    if (end - pos < 4) {
+        throw AMFException("Read past _end of buffer for long string length");
+    }
+
+    const boost::uint32_t si = readNetworkLong(pos);
+    pos += 4;
+    if (end - pos < si) {
+        throw AMFException("Read past _end of buffer for long string type");
+    }
+
+    const std::string str(reinterpret_cast<const char*>(pos), si);
+    pos += si;
+
+#ifdef GNASH_DEBUG_AMF_DESERIALIZE
+    log_debug("amf0 read long string: %s", str);
+#endif
+
+    return str;
+
+}
+
+void
+writePlainString(SimpleBuffer& buf, const std::string& str, Type t)
+{
+    const size_t len = str.size();
+    switch (t) {
+        default:
+            log_error("writePlainString called with invalid type!");
+            return;
+       
+        case LONG_STRING_AMF0:
+            buf.appendNetworkLong(len);
+            break;
+           
+        case STRING_AMF0:
+            buf.appendNetworkShort(len);
+            break;
+
+    }
+    buf.append(str.c_str(), len);
+}
+
+void
+write(SimpleBuffer& buf, const std::string& str)
+{
+    Type t = str.size() < 65536 ? STRING_AMF0 : LONG_STRING_AMF0;
+       buf.appendByte(t);
+    writePlainString(buf, str, t);
+}
+
+void
+write(SimpleBuffer& buf, double d)
+{
+    buf.appendByte(NUMBER_AMF0);
+    swapBytes(&d, 8);
+    buf.append(&d, 8);
+}
+
+void
+write(SimpleBuffer& buf, bool b)
+{
+       buf.appendByte(BOOLEAN_AMF0);
+    buf.appendByte(b ? 1 : 0);
+}
+
+void*
+swapBytes(void *word, size_t size)
+{
+    union {
+    boost::uint16_t s;
+    struct {
+        boost::uint8_t c0;
+        boost::uint8_t c1;
+    } c;
+    } u;
+       
+    u.s = 1;
+        if (u.c.c0 == 0) {
+        // Big-endian machine: do nothing
+        return word;
+    }
+
+    // Little-endian machine: byte-swap the word
+    // A conveniently-typed pointer to the source data
+    boost::uint8_t *x = static_cast<boost::uint8_t *>(word);
+
+    /// Handle odd as well as even counts of bytes
+    std::reverse(x, x + size);
+    
+    return word;
+}
+
+
+} // namespace AMF
+} // namespace gnash

=== added file 'libbase/AMF.h'
--- a/libbase/AMF.h     1970-01-01 00:00:00 +0000
+++ b/libbase/AMF.h     2010-02-20 07:55:10 +0000
@@ -0,0 +1,191 @@
+// AMF.h    Low level functions for manipulating and reading AMF buffers.
+// 
+//   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
+//   Foundation, Inc
+// 
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+// 
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+// This file provides low-level manipulators for AMF buffers. It can be used
+// without reliance on libcore.
+
+#ifndef GNASH_AMF_H
+#define GNASH_AMF_H
+
+#include <string>
+
+#include "dsodefs.h"
+#include "GnashException.h"
+
+namespace gnash {
+    class SimpleBuffer;
+}
+
+namespace gnash {
+
+/// Functions and classes for handling AMF.
+//
+/// AMF is a simple serialization format for ActionScript objects and values,
+/// allowing them to be stored and transmitted. The AMF namespace provides
+/// both low-level and high-level conversion to and from AMF buffers.
+namespace AMF {
+
+enum Type {
+    NOTYPE            = -1,
+    NUMBER_AMF0       = 0x00,
+    BOOLEAN_AMF0      = 0x01,
+    STRING_AMF0       = 0x02,
+    OBJECT_AMF0       = 0x03,
+    MOVIECLIP_AMF0    = 0x04,
+    NULL_AMF0         = 0x05,
+    UNDEFINED_AMF0    = 0x06,
+    REFERENCE_AMF0    = 0x07,
+    ECMA_ARRAY_AMF0   = 0x08,
+    OBJECT_END_AMF0   = 0x09,
+    STRICT_ARRAY_AMF0 = 0x0a,
+    DATE_AMF0         = 0x0b,
+    LONG_STRING_AMF0  = 0x0c,
+    UNSUPPORTED_AMF0  = 0x0d,
+    RECORD_SET_AMF0   = 0x0e,
+    XML_OBJECT_AMF0   = 0x0f,
+    TYPED_OBJECT_AMF0 = 0x10,
+};
+
+/// Exception for handling malformed buffers.
+//
+/// All low-level reading operations can throw this error.
+struct DSOEXPORT
+AMFException : public GnashException
+{
+    AMFException(const std::string& msg)
+        :
+        GnashException(msg)
+    {}
+};
+
+/// Read a number from an AMF buffer
+//
+/// This does not read a type byte; use AMF::Reader when the type should
+/// be determined from the buffer.
+//
+/// This function will throw an AMFException if it encounters ill-formed AMF.
+double readNumber(const boost::uint8_t*& pos, const boost::uint8_t* end);
+
+/// Read a boolean value from the buffer.
+//
+/// This does not read a type byte; use AMF::Reader when the type should
+/// be determined from the buffer.
+//
+/// This function will throw an AMFException if it encounters ill-formed AMF.
+bool readBoolean(const boost::uint8_t*& pos, const boost::uint8_t* end);
+
+/// Read a string value from the buffer.
+//
+/// This does not read a type byte; use AMF::Reader when the type should
+/// be determined from the buffer.
+//
+/// This function will throw an AMFException if it encounters ill-formed AMF.
+std::string readString(const boost::uint8_t*& pos, const boost::uint8_t* end);
+
+/// Read a long string value from the buffer.
+//
+/// This does not read a type byte; use AMF::Reader when the type should
+/// be determined from the buffer.
+//
+/// This function will throw an AMFException if it encounters ill-formed AMF.
+std::string readLongString(const boost::uint8_t*& pos,
+        const boost::uint8_t* end);
+
+/// Read an unsigned 16-bit value in network byte order.
+//
+/// You must ensure that the buffer contains at least 2 bytes!
+inline boost::uint16_t
+readNetworkShort(const boost::uint8_t* buf)
+{
+    const boost::uint16_t s = buf[0] << 8 | buf[1];
+    return s;
+}
+
+/// Read an unsigned 32-bit value in network byte order.
+//
+/// You must ensure that the buffer contains at least 4 bytes!
+inline boost::uint32_t
+readNetworkLong(const boost::uint8_t* buf)
+{
+    const boost::uint32_t s = buf[0] << 24 | buf[1] << 16 |
+                              buf[2] << 8 | buf[3];
+    return s;
+}
+
+/// Write a string to an AMF buffer.
+//
+/// This function writes the type byte and the string value. It also handles
+/// both long and short strings automatically.
+//
+/// This is overloaded for automatic type deduction to allow the use of
+/// a template for more complex operations. You must be careful when using
+/// it!
+void write(SimpleBuffer& buf, const std::string& str);
+
+/// Write a number to an AMF buffer.
+//
+/// This function writes the type byte and the double value.
+//
+/// This is overloaded for automatic type deduction to allow the use of
+/// a template for more complex operations. You must be careful when using
+/// it!
+void write(SimpleBuffer& buf, double d);
+
+/// Write a boolean value to an AMF buffer.
+//
+/// This function writes the type byte and the boolean value.
+//
+/// This is overloaded for automatic type deduction to allow the use of
+/// a template for more complex operations. You must be careful when using
+/// it!
+void write(SimpleBuffer& buf, bool b);
+
+/// Encode a plain short string to an AMF buffer.
+//
+/// This does not encode a type byte; it is used for cases where a string is
+/// required, such as for the name of an object property, and therefore does
+/// not use a type byte.
+void writePlainString(SimpleBuffer& buf, const std::string& str, Type t);
+
+/// Encode a string-value pair.
+//
+/// This is used for object properties; the string is always encoded with
+/// a 2-byte length.
+template<typename T>
+void
+writeProperty(SimpleBuffer& buf, const std::string& name, const T& t)
+{
+    writePlainString(buf, name, STRING_AMF0);
+    write(buf, t);
+}
+
+/// Swap bytes in raw data.
+//
+///    This only swaps bytes if the host byte order is little endian.
+///
+/// @param word The address of the data to byte swap.
+/// @param size The number of bytes in the data.
+/// @return A pointer to the raw data.
+DSOEXPORT void* swapBytes(void *word, size_t size);
+
+
+} // namespace amf
+} // namespace gnash
+
+#endif

=== modified file 'libbase/IOChannel.cpp'
--- a/libbase/IOChannel.cpp     2010-01-18 07:33:35 +0000
+++ b/libbase/IOChannel.cpp     2010-02-22 06:49:35 +0000
@@ -37,15 +37,6 @@
     return(result);
 }
 
-long double
-IOChannel::read_le_double64() 
-{
-    return static_cast<long double> (
-        static_cast<boost::int64_t> (read_le32()) |
-        static_cast<boost::int64_t> (read_le32()) << 32
-    );
-}
-
 boost::uint16_t
 IOChannel::read_le16()
 {
@@ -54,33 +45,6 @@
     return(result);
 }
 
-void
-IOChannel::write_le32(boost::uint32_t u)
-{
-        write_byte(static_cast<boost::int8_t>(u));
-        write_byte(static_cast<boost::int8_t>(u>>8));
-        write_byte(static_cast<boost::int8_t>(u>>16));
-        write_byte(static_cast<boost::int8_t>(u>>24));
-}
-
-void
-IOChannel::write_le16(boost::uint16_t u)
-{
-    write_byte(static_cast<boost::int8_t>(u));
-    write_byte(static_cast<boost::int8_t>(u>>8));
-}
-
-void
-IOChannel::write_string(const char* src)
-{
-    for (;;)
-    {
-        write_byte(*src);
-        if (*src == 0) break;
-        src++;
-        }
-}
-
 int
 IOChannel::read_string(char* dst, int max_length) 
 {
@@ -97,20 +61,6 @@
     return -1;
 }
 
-void
-IOChannel::write_float32(float value)
-{
-    union alias {
-        float    f;
-        boost::uint32_t    i;
-    } u;
-
-    BOOST_STATIC_ASSERT(sizeof(alias) == sizeof(boost::uint32_t));
-    
-    u.f = value;
-    write_le32(u.i);
-}
-
 float
 IOChannel::read_float32()
 {
@@ -136,12 +86,6 @@
     return u;
 }
 
-void
-IOChannel::write_byte(boost::uint8_t u)
-{
-    write(&u, 1); // will trhow on error it seems
-}
-
 std::streamsize
 IOChannel::write(const void* /*src*/, std::streamsize /*num*/)
 {

=== modified file 'libbase/IOChannel.h'
--- a/libbase/IOChannel.h       2010-01-18 07:35:15 +0000
+++ b/libbase/IOChannel.h       2010-02-22 06:49:35 +0000
@@ -50,45 +50,18 @@
     ///
     boost::uint32_t read_le32();
 
-    /// \brief Read a 64-bit word from a little-ending stream,
-    /// returning it as a native-endian word.
-    //
-    /// Throw IOException on premature EOF
-    ///
-    /// TODO: define a platform-neutral type for 64 bits.
-    ///
-    long double read_le_double64();
-
     /// Read a 16-bit word from a little-endian stream.
     //
     /// Throw IOException on error
     ///
     boost::uint16_t read_le16();
 
-    /// Write a 32-bit word to a little-endian stream.
-    //
-    /// Throw IOException on error
-    ///
-    void write_le32(boost::uint32_t u);
-
-    /// \brief Write a 16-bit word to a little-endian stream.
-    //
-    /// Throw IOException on error
-    ///
-    void write_le16(boost::uint16_t u);
-
     /// Read a single byte from the stream
     //
     /// Throw IOException on error
     ///
     boost::uint8_t read_byte();
-
-    /// write a single byte to the stream
-    //
-    /// Throw IOException on error
-    ///
-    void write_byte(boost::uint8_t u);
-
+    
     /// Read the given number of bytes from the stream
     //
     /// Return the number of bytes actually read. 
@@ -120,12 +93,6 @@
     ///
     virtual std::streamsize write(const void* src, std::streamsize num);
 
-    /// \brief Write a 0-terminated string to a stream.
-    //
-    /// Throw IOException on error
-    ///
-    void write_string(const char* src);
-
     /// \brief
     /// Read up to max_length characters, returns the number of characters 
     /// read, or -1 if the string length is longer than max_length.
@@ -140,16 +107,7 @@
     /// Throw IOException on error
     ///
     int    read_string(char* dst, int max_length);
-
-    /// Write a 32-bit float to a stream in little-endian order.
-    //
-    /// NOTE: this currently relies on host FP format being the same
-        ///       as the Flash one (presumably IEEE 754).
-    ///
-    /// Throw IOException on error
-    ///
-    void write_float32(float value);
-
+    
     /// Read a 32-bit float from a little-endian stream.
     //
     /// NOTE: this currently relies on host FP format being the

=== modified file 'libbase/Makefile.am'
--- a/libbase/Makefile.am       2010-02-09 07:41:18 +0000
+++ b/libbase/Makefile.am       2010-02-20 07:55:10 +0000
@@ -62,6 +62,7 @@
        GnashImage.cpp \
        GnashImageJpeg.cpp \
        GnashFileUtilities.cpp \
+       AMF.cpp \
        log.cpp \
        memory.cpp \
        rc.cpp \
@@ -170,6 +171,7 @@
        ref_counted.h \
        GC.h \
        GnashException.h \
+       AMF.h \
        dsodefs.h \
        utility.h \
        log.h \

=== modified file 'libbase/tu_file.h'
--- a/libbase/tu_file.h 2009-02-10 11:33:38 +0000
+++ b/libbase/tu_file.h 2010-02-22 06:53:48 +0000
@@ -38,28 +38,14 @@
     ///
     boost::uint32_t read_le32() 
     {
-           // read8() is boost::uint8_t, so no masks with 0xff are required.
-           boost::uint32_t result = static_cast<boost::uint32_t>(read8());
-           result |= static_cast<boost::uint32_t>(read8()) << 8;
-           result |= static_cast<boost::uint32_t>(read8()) << 16;
-           result |= static_cast<boost::uint32_t>(read8()) << 24;
+           // read_byte() is boost::uint8_t, so no masks with 0xff are 
required.
+           boost::uint32_t result = static_cast<boost::uint32_t>(read_byte());
+           result |= static_cast<boost::uint32_t>(read_byte()) << 8;
+           result |= static_cast<boost::uint32_t>(read_byte()) << 16;
+           result |= static_cast<boost::uint32_t>(read_byte()) << 24;
            return(result);
     }
        
-       /// \brief Read a 64-bit word from a little-ending stream,
-       /// returning it as a native-endian word.
-       //
-       /// TODO: define what happens when the stream is in
-       ///       error condition, see bad().
-       /// TODO: define a platform-neutral type for 64 bits.
-       long double read_le_double64()
-       {
-               return static_cast<long double> (
-                       static_cast<boost::int64_t> (read_le32()) |
-                       static_cast<boost::int64_t> (read_le32()) << 32
-               );
-       }
-
     /// \brief Read a 16-bit word from a little-endian stream.
     //
     /// TODO: define what happens when the stream
@@ -67,48 +53,21 @@
     ///
     boost::uint16_t read_le16()
     {
-           boost::uint16_t result = static_cast<boost::uint16_t>(read8());
-           result |= static_cast<boost::uint16_t>(read8()) << 8;
+           boost::uint16_t result = static_cast<boost::uint16_t>(read_byte());
+           result |= static_cast<boost::uint16_t>(read_byte()) << 8;
            return(result);
     }
-
-    /// \brief Write a 32-bit word to a little-endian stream.
-    //
-    /// TODO: define what happens when the stream
-    ///       is in error condition, see bad().
-    ///
-    void       write_le32(boost::uint32_t u)
-    {
-        write8(static_cast<boost::int8_t>(u));
-        write8(static_cast<boost::int8_t>(u>>8));
-        write8(static_cast<boost::int8_t>(u>>16));
-        write8(static_cast<boost::int8_t>(u>>24));
-    }
-
-    /// \brief Write a 16-bit word to a little-endian stream.
-    //
-    /// TODO: define what happens when the stream
-    ///       is in error condition, see bad().
-    ///
-    void write_le16(boost::uint16_t u)
-    {
-        write8(static_cast<boost::int8_t>(u));
-        write8(static_cast<boost::int8_t>(u>>8));
-    }
     
     /// \brief Read a single byte from the stream
     //
     /// TODO: define what happens when the stream
     ///       is in error condition, see bad().
     ///
-    boost::uint8_t read_byte() { return read8(); }
-
-    /// \brief write a single byte to the stream
-    //
-    /// TODO: define what happens when the stream
-    ///       is in error condition, see bad().
-    ///
-    void write_byte(boost::uint8_t u) { write8(u); }
+    boost::uint8_t read_byte() {
+        boost::uint8_t u;
+        read(&u, 1);
+        return u;
+    }
     
     /// \brief Read the given number of bytes from the stream
     //
@@ -168,54 +127,6 @@
     size_t size() const;
     
 private:
-
-    boost::uint64_t    read64()
-    {
-        boost::uint64_t u;
-        read(&u, 8);
-        return u;
-    }
-
-    boost::uint32_t    read32()
-    {
-        boost::uint32_t u;
-        read(&u, 4);
-        return u;
-    }
-
-    boost::uint16_t read16()
-    {
-        boost::uint16_t u;
-        read(&u, 2);
-        return u;
-    }
-    
-    boost::uint8_t     read8()
-    {
-        boost::uint8_t u;
-        read(&u, 1);
-        return u;
-    }
-    
-    void write64(boost::uint64_t u)
-    {
-        write(&u, 8);
-    }
-
-    void write32(boost::uint32_t u)
-    {
-        write(&u, 4);
-    }
-
-    void write16(boost::uint16_t u)
-    {
-        write(&u, 2);
-    }
-
-    void write8(boost::uint8_t u)
-    {
-        write(&u, 1);
-    }
     
     void close();
     

=== renamed file 'libcore/AMF.cpp' => 'libcore/AMFConverter.cpp'
--- a/libcore/AMF.cpp   2010-02-18 13:41:08 +0000
+++ b/libcore/AMFConverter.cpp  2010-02-20 07:55:10 +0000
@@ -21,6 +21,7 @@
 
 #include "SimpleBuffer.h"
 #include "AMF.h"
+#include "AMFConverter.h"
 #include "namedStrings.h"
 #include "as_value.h"
 #include "as_object.h"
@@ -30,7 +31,6 @@
 #include "xml/XMLDocument_as.h"
 #include "Array_as.h"
 
-
 // Define this macro to make AMF parsing verbose
 //#define GNASH_DEBUG_AMF_DESERIALIZE 1
 
@@ -43,13 +43,6 @@
 
 namespace {
 
-    inline boost::uint16_t readNetworkShort(const boost::uint8_t* buf);
-    inline boost::uint32_t readNetworkLong(const boost::uint8_t* buf);
-
-}
-
-namespace {
-
 /// Class used to serialize properties of an object to a buffer
 class ObjectSerializer : public AbstractPropertyVisitor
 {
@@ -117,9 +110,7 @@
 bool
 Writer::writePropertyName(const std::string& name)
 {
-    boost::uint16_t namelen = name.size();
-    _buf.appendNetworkShort(namelen);
-    _buf.append(name.c_str(), namelen);
+    writePlainString(_buf, name, STRING_AMF0);
     return true;
 }
 
@@ -181,8 +172,8 @@
             xml->toString(s, true);
 
             const std::string& xmlstr = s.str();
-            _buf.appendNetworkLong(xmlstr.size());
-            _buf.append(xmlstr.c_str(), xmlstr.size());
+            writePlainString(_buf, xmlstr, LONG_STRING_AMF0);
+
             return true;
         }
 
@@ -260,37 +251,24 @@
 bool
 Writer::writeString(const std::string& str)
 {
-    const size_t strlen = str.size();
-    if (strlen <= 65535) {
-#ifdef GNASH_DEBUG_AMF_SERIALIZE
-        log_debug(_("amf: serializing string '%s'"), str);
-#endif
-        _buf.appendByte(STRING_AMF0);
-        _buf.appendNetworkShort(strlen);
-        _buf.append(str.c_str(), strlen);
-        return true;
-    }
-
-#ifdef GNASH_DEBUG_AMF_SERIALIZE
-    log_debug(_("amf: serializing long string '%s'"), str);
-#endif
-    _buf.appendByte(LONG_STRING_AMF0);
-    _buf.appendNetworkLong(strlen);
-    _buf.append(str.c_str(), strlen);
+    write(_buf, str);
     return true;
 }
 
 bool
 Writer::writeNumber(double d)
 {
-#ifdef GNASH_DEBUG_AMF_SERIALIZE
-    log_debug(_("amf: serializing number '%g'"), d);
-#endif
-    _buf.appendByte(NUMBER_AMF0);
-    swapBytes(&d, 8);
-    _buf.append(&d, 8);
-    return true;
-}
+    write(_buf, d);
+    return true;
+}
+
+bool
+Writer::writeBoolean(bool b)
+{
+    write(_buf, b);
+    return true;
+}
+
 
 bool
 Writer::writeUndefined()
@@ -319,21 +297,6 @@
 }
 
 bool
-Writer::writeBoolean(bool b)
-{
-#ifdef GNASH_DEBUG_AMF_SERIALIZE
-    log_debug(_("amf: serializing boolean '%s'"), b);
-#endif
-    _buf.appendByte(BOOLEAN_AMF0);
-    
-    if (b) _buf.appendByte(1);
-    else _buf.appendByte(0);
-
-    return true;
-}
-
-
-bool
 Reader::operator()(as_value& val, Type t)
 {
 
@@ -648,135 +611,5 @@
     return date;
 }
 
-void*
-swapBytes(void *word, size_t size)
-{
-    union {
-    boost::uint16_t s;
-    struct {
-        boost::uint8_t c0;
-        boost::uint8_t c1;
-    } c;
-    } u;
-       
-    u.s = 1;
-        if (u.c.c0 == 0) {
-        // Big-endian machine: do nothing
-        return word;
-    }
-
-    // Little-endian machine: byte-swap the word
-    // A conveniently-typed pointer to the source data
-    boost::uint8_t *x = static_cast<boost::uint8_t *>(word);
-
-    /// Handle odd as well as even counts of bytes
-    std::reverse(x, x + size);
-    
-    return word;
-}
-
-
-bool
-readBoolean(const boost::uint8_t*& pos, const boost::uint8_t* _end)
-{
-    if (pos == _end) {
-        throw AMFException("Read past _end of buffer for boolean type");
-    }
-
-    const bool val = *pos;
-    ++pos;
-#ifdef GNASH_DEBUG_AMF_DESERIALIZE
-    log_debug("amf0 read bool: %d", val);
-#endif
-    return val;
-}
-
-double
-readNumber(const boost::uint8_t*& pos, const boost::uint8_t* end)
-{
-
-    if (end - pos < 8) {
-        throw AMFException("Read past _end of buffer for number type");
-    }
-
-    double d;
-    // TODO: may we avoid a copy and swapBytes call
-    //       by bitshifting b[0] trough b[7] ?
-    std::copy(pos, pos + 8, reinterpret_cast<char*>(&d));
-    pos += 8; 
-    swapBytes(&d, 8);
-
-#ifdef GNASH_DEBUG_AMF_DESERIALIZE
-    log_debug("amf0 read double: %e", dub);
-#endif
-
-    return d;
-}
-
-std::string
-readString(const boost::uint8_t*& pos, const boost::uint8_t* end)
-{
-    if (end - pos < 2) {
-        throw AMFException("Read past _end of buffer for string length");
-    }
-
-    const boost::uint16_t si = readNetworkShort(pos);
-    pos += 2;
-
-    if (end - pos < si) {
-        throw AMFException("Read past _end of buffer for string type");
-    }
-
-    const std::string str(reinterpret_cast<const char*>(pos), si);
-    pos += si;
-#ifdef GNASH_DEBUG_AMF_DESERIALIZE
-    log_debug("amf0 read string: %s", str);
-#endif
-    return str;
-}
-
-std::string
-readLongString(const boost::uint8_t*& pos, const boost::uint8_t* end)
-{
-    if (end - pos < 4) {
-        throw AMFException("Read past _end of buffer for long string length");
-    }
-
-    const boost::uint32_t si = readNetworkLong(pos);
-    pos += 4;
-    if (end - pos < si) {
-        throw AMFException("Read past _end of buffer for long string type");
-    }
-
-    const std::string str(reinterpret_cast<const char*>(pos), si);
-    pos += si;
-
-#ifdef GNASH_DEBUG_AMF_DESERIALIZE
-    log_debug("amf0 read long string: %s", str);
-#endif
-
-    return str;
-
-}
-
-namespace {
-
-inline boost::uint16_t
-readNetworkShort(const boost::uint8_t* buf)
-{
-    boost::uint16_t s = buf[0] << 8 | buf[1];
-    return s;
-}
-
-inline boost::uint32_t
-readNetworkLong(const boost::uint8_t* buf)
-{
-    boost::uint32_t s = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
-    return s;
-}
-
-} // anonymous namespace
-
-
 } // namespace AMF
 } // namespace gnash

=== renamed file 'libcore/AMF.h' => 'libcore/AMFConverter.h'
--- a/libcore/AMF.h     2010-02-18 07:43:24 +0000
+++ b/libcore/AMFConverter.h    2010-02-20 07:55:10 +0000
@@ -17,8 +17,8 @@
 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 
-#ifndef GNASH_AMF_H
-#define GNASH_AMF_H
+#ifndef GNASH_AMFCONVERTER_H
+#define GNASH_AMFCONVERTER_H
 
 #include <map>
 #include <string>
@@ -27,6 +27,7 @@
 #include "as_value.h"
 #include "dsodefs.h"
 #include "GnashException.h"
+#include "AMF.h"
 
 namespace gnash {
     class as_object;
@@ -36,34 +37,13 @@
 
 namespace gnash {
 
-/// Simple functions and classes for handling AMF.
+/// Functions and classes for handling AMF.
 //
 /// AMF is a simple serialization format for ActionScript objects and values,
 /// allowing them to be stored and transmitted. These classes convert between
 /// AMF buffers and the objects they contain.
 namespace AMF {
 
-enum Type {
-    NOTYPE            = -1,
-    NUMBER_AMF0       = 0x00,
-    BOOLEAN_AMF0      = 0x01,
-    STRING_AMF0       = 0x02,
-    OBJECT_AMF0       = 0x03,
-    MOVIECLIP_AMF0    = 0x04,
-    NULL_AMF0         = 0x05,
-    UNDEFINED_AMF0    = 0x06,
-    REFERENCE_AMF0    = 0x07,
-    ECMA_ARRAY_AMF0   = 0x08,
-    OBJECT_END_AMF0   = 0x09,
-    STRICT_ARRAY_AMF0 = 0x0a,
-    DATE_AMF0         = 0x0b,
-    LONG_STRING_AMF0  = 0x0c,
-    UNSUPPORTED_AMF0  = 0x0d,
-    RECORD_SET_AMF0   = 0x0e,
-    XML_OBJECT_AMF0   = 0x0f,
-    TYPED_OBJECT_AMF0 = 0x10,
-};
-
 /// A class to compose AMF buffers.
 //
 /// A single AMF::Writer class can take successive values and encode them
@@ -123,18 +103,6 @@
 
 };
 
-/// Exception for handling malformed buffers.
-//
-/// This exception is thrown only during reading.
-struct DSOEXPORT
-AMFException : public GnashException
-{
-    AMFException(const std::string& msg)
-        :
-        GnashException(msg)
-    {}
-};
-
 
 /// Deserialize an AMF buffer to as_values.
 //
@@ -215,52 +183,6 @@
 
 };
 
-/// Read a number from an AMF buffer
-//
-/// This does not read a type byte; use AMF::Reader when the type should
-/// be determined from the buffer.
-//
-/// This function will throw an AMFException if it encounters ill-formed AMF.
-double readNumber(const boost::uint8_t*& pos, const boost::uint8_t* end);
-
-/// Read a boolean value from the buffer.
-//
-/// This does not read a type byte; use AMF::Reader when the type should
-/// be determined from the buffer.
-//
-/// This function will throw an AMFException if it encounters ill-formed AMF.
-bool readBoolean(const boost::uint8_t*& pos, const boost::uint8_t* end);
-
-/// Read a string value from the buffer.
-//
-/// This does not read a type byte; use AMF::Reader when the type should
-/// be determined from the buffer.
-//
-/// This function will throw an AMFException if it encounters ill-formed AMF.
-std::string readString(const boost::uint8_t*& pos, const boost::uint8_t* end);
-
-/// Read a long string value from the buffer.
-//
-/// This does not read a type byte; use AMF::Reader when the type should
-/// be determined from the buffer.
-//
-/// This function will throw an AMFException if it encounters ill-formed AMF.
-std::string readLongString(const boost::uint8_t*& pos,
-        const boost::uint8_t* end);
-
-/// Swap bytes in raw data.
-//
-///    This only swaps bytes if the host byte order is little endian.
-///
-/// @param word The address of the data to byte swap.
-///
-/// @param size The number of bytes in the data.
-///
-/// @return A pointer to the raw data.
-///
-DSOEXPORT void* swapBytes(void *word, size_t size);
-
-
 } // namespace amf
 } // namespace gnash
 

=== modified file 'libcore/Makefile.am'
--- a/libcore/Makefile.am       2010-02-15 17:09:55 +0000
+++ b/libcore/Makefile.am       2010-02-20 07:55:10 +0000
@@ -60,7 +60,7 @@
        event_id.cpp \
        Relay.cpp \
        as_object.cpp \
-       AMF.cpp \
+       AMFConverter.cpp \
        as_value.cpp \
        DisplayObjectContainer.cpp \
        DisplayObject.cpp \
@@ -232,7 +232,7 @@
        ObjectURI.h \
        Property.h \
        PropertyList.h \
-       AMF.h \
+       AMFConverter.h \
        as_value.h \
        PropFlags.h     \
        CharacterProxy.h \

=== modified file 'libcore/as_value.cpp'
--- a/libcore/as_value.cpp      2010-02-10 09:47:04 +0000
+++ b/libcore/as_value.cpp      2010-02-20 07:55:10 +0000
@@ -37,8 +37,7 @@
 #include "StringPredicates.h"
 #include "Global_as.h"
 #include "String_as.h"
-#include "AMF.h"
-
+#include "AMFConverter.h"
 
 #include <boost/shared_ptr.hpp>
 #include <cmath> 

=== modified file 'libcore/asobj/flash/net/LocalConnection_as.cpp'
--- a/libcore/asobj/flash/net/LocalConnection_as.cpp    2010-02-11 08:49:34 
+0000
+++ b/libcore/asobj/flash/net/LocalConnection_as.cpp    2010-02-20 07:55:10 
+0000
@@ -33,7 +33,7 @@
 #include "namedStrings.h"
 #include "StringPredicates.h"
 #include "as_value.h"
-#include "AMF.h"
+#include "AMFConverter.h"
 #include "ClockTime.h"
 #include "GnashAlgorithm.h"
 

=== modified file 'libcore/asobj/flash/net/NetConnection_as.cpp'
--- a/libcore/asobj/flash/net/NetConnection_as.cpp      2010-02-10 09:32:07 
+0000
+++ b/libcore/asobj/flash/net/NetConnection_as.cpp      2010-02-20 07:55:10 
+0000
@@ -38,7 +38,7 @@
 #include "GnashAlgorithm.h"
 #include "fn_call.h"
 #include "Global_as.h"
-#include "AMF.h"
+#include "AMFConverter.h"
 
 #include <iostream>
 #include <string>

=== modified file 'libcore/asobj/flash/net/NetStream_as.cpp'
--- a/libcore/asobj/flash/net/NetStream_as.cpp  2010-02-10 09:47:04 +0000
+++ b/libcore/asobj/flash/net/NetStream_as.cpp  2010-02-20 07:55:10 +0000
@@ -39,7 +39,7 @@
 #include "StreamProvider.h"
 #include "sound_handler.h"
 #include "GnashSystemNetHeaders.h"
-#include "AMF.h"
+#include "AMFConverter.h"
 
 // Define the following macro to have status notification handling debugged
 //#define GNASH_DEBUG_STATUS

=== modified file 'libcore/asobj/flash/net/SharedObject_as.cpp'
--- a/libcore/asobj/flash/net/SharedObject_as.cpp       2010-02-15 13:45:41 
+0000
+++ b/libcore/asobj/flash/net/SharedObject_as.cpp       2010-02-20 07:55:10 
+0000
@@ -44,7 +44,7 @@
 #include "URL.h"
 #include "NetConnection_as.h"
 #include "Object.h"
-#include "AMF.h"
+#include "AMFConverter.h"
 #include "GnashAlgorithm.h"
 
 #include <boost/scoped_array.hpp>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]