"Fossies" - the Fresh Open Source Software Archive

Member "gnash-0.8.10/libcore/parser/action_buffer.h" (19 Jan 2012, 6100 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 #ifndef GNASH_ACTION_BUFFER_H
   20 #define GNASH_ACTION_BUFFER_H
   21 
   22 #include <string>
   23 #include <vector> 
   24 #include <map> 
   25 #include <boost/noncopyable.hpp>
   26 #include <boost/cstdint.hpp> 
   27 
   28 #include "GnashException.h"
   29 #include "ConstantPool.h"
   30 #include "log.h"
   31 
   32 // Forward declarations
   33 namespace gnash {
   34     class as_value;
   35     class movie_definition;
   36     class SWFStream; // for read signature
   37 }
   38 
   39 namespace gnash {
   40 
   41 /// A code segment.
   42 //
   43 /// This currently holds the actions in a memory
   44 /// buffer, but I'm workin toward making this unneeded
   45 /// so to eventually use a gnash::stream directly and
   46 /// avoid full loads. (not before profiling!).
   47 //
   48 /// Good, would make jumping to other tags possible.
   49 class action_buffer : boost::noncopyable
   50 {
   51 public:
   52 
   53     action_buffer(const movie_definition& md);
   54 
   55     /// Read action bytes from input stream up to but not including endPos
   56     //
   57     /// @param endPos
   58     /// One past last valid-to-read byte position.
   59     /// Make sure it's > then in.tell() and
   60     /// <= in.get_tag_end_position() or an assertion will
   61     /// fail.
   62     ///
   63     void read(SWFStream& in, unsigned long endPos);
   64 
   65     size_t size() const { return m_buffer.size(); }
   66 
   67     boost::uint8_t operator[] (size_t off) const
   68     {
   69         if (off >= m_buffer.size()) {
   70             throw ActionParserException (_("Attempt to read outside "
   71                         "action buffer"));
   72         }
   73         return m_buffer[off];
   74     }
   75 
   76     /// Disassemble instruction at given offset and return as a string
   77     std::string disasm(size_t pc) const;
   78 
   79     /// Get a null-terminated string from given offset
   80     //
   81     /// Useful to hide complexity of underlying buffer access.
   82     ///
   83     const char* read_string(size_t pc) const
   84     {
   85         assert(pc <= m_buffer.size() );
   86         if (pc == m_buffer.size())
   87         {
   88             throw ActionParserException(_("Asked to read string when only "
   89                 "1 byte remains in the buffer"));
   90         }
   91         return reinterpret_cast<const char*>(&m_buffer[pc]);
   92     }
   93 
   94     /// Get a pointer to the current instruction within the code
   95     const unsigned char* getFramePointer(size_t pc) const
   96     {
   97         assert (pc < m_buffer.size());
   98         return reinterpret_cast<const unsigned char*>(&m_buffer.at(pc));
   99     }
  100 
  101     /// Get a signed integer value from given offset
  102     //
  103     /// Useful to hide complexity of underlying buffer access.
  104     ///
  105     boost::int16_t read_int16(size_t pc) const
  106     {
  107         if (pc + 1 >= m_buffer.size()) {
  108             throw ActionParserException(_("Attempt to read outside action buffer limits"));
  109         }
  110         boost::int16_t ret = (m_buffer[pc] | (m_buffer[pc + 1] << 8));
  111         return ret;
  112     }
  113 
  114     /// Get an unsigned short integer value from given offset
  115     /// read_int16 should check buffer boundaries.
  116     boost::uint16_t read_uint16(size_t pc) const
  117     {
  118         return static_cast<boost::uint16_t>(read_int16(pc));
  119     }
  120 
  121     /// Read a 32-bit integer starting at given offset.
  122     //
  123     /// Useful to hide complexity of underlying buffer access.
  124     ///
  125     boost::int32_t read_int32(size_t pc) const
  126     {
  127         if (pc + 3 >= m_buffer.size()) {
  128             throw ActionParserException(_("Attempt to read outside action buffer limits"));
  129         }
  130         
  131         boost::int32_t  val = m_buffer[pc]
  132               | (m_buffer[pc + 1] << 8)
  133               | (m_buffer[pc + 2] << 16)
  134               | (m_buffer[pc + 3] << 24);
  135         return val;
  136     }
  137 
  138     /// Read a little-endian 32-bit float starting at given offset
  139     //
  140     /// Useful to hide complexity of underlying buffer access.
  141     ///
  142     float read_float_little(size_t pc) const;
  143 
  144     /// Read a 64-bit double starting at given offset.
  145     //
  146     /// wacky format: 45670123
  147     /// Useful to hide complexity of underlying buffer access.
  148     ///
  149     double read_double_wacky(size_t pc) const;
  150 
  151     /// Return a value from the constant pool
  152     const char* dictionary_get(size_t n) const
  153     {
  154         if ( _pools.empty() ) return 0;
  155 
  156         // We'll query the last inserted one for now (highest PC)
  157         const ConstantPool& pool = _pools.rbegin()->second;
  158 
  159         if ( n < pool.size() ) return pool[n];
  160 
  161         else return 0;
  162     }
  163 
  164     /// Read an SWF::ACTION_CONSTANTPOOL opcode and return as a dictionary
  165     //
  166     /// Don't read stop_pc or later. 
  167     ///
  168     /// A dictionary is a table of indexed strings to be
  169     /// used in action blocks to reduce their size.
  170     /// This parser caches dictionaries to avoid reindexing them
  171     /// when encountered multiple times.
  172     ///
  173     /// Note that there can be multiple constant pools in the
  174     /// same action buffer.
  175     /// See testsuite/misc-swfmill.all/*dict*
  176     ///
  177     const ConstantPool& readConstantPool(size_t start_pc, size_t stop_pc) const;
  178 
  179     /// Return url of the SWF this action block was found in
  180     const std::string& getDefinitionURL() const;
  181 
  182     /// Return version of the SWF this action block was found in
  183     int getDefinitionVersion() const;
  184 
  185     const movie_definition& getMovieDefinition() const {
  186         return _src;
  187     }
  188 
  189 private:
  190 
  191     /// the code itself, as read from the SWF
  192     std::vector<boost::uint8_t> m_buffer;
  193 
  194     /// The set of ConstantPools found in this action_buffer
  195     typedef std::map<size_t, ConstantPool> PoolsMap;
  196     mutable PoolsMap _pools;
  197 
  198     /// The movie_definition containing this action buffer
  199     //
  200     /// This pointer will be used to determine domain-based
  201     /// permissions to grant to the action code.
  202     /// 
  203     const movie_definition& _src;
  204 };
  205 
  206 
  207 }   // end namespace gnash
  208 
  209 
  210 #endif // GNASH_ACTION_BUFFER_H
  211 
  212 
  213 // Local Variables:
  214 // mode: C++
  215 // indent-tabs-mode: t
  216 // End: