Gnash  0.8.11dev
action_buffer.h
Go to the documentation of this 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 <cstdint>
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 
42 //
47 //
49 class action_buffer : boost::noncopyable
50 {
51 public:
52 
54 
56  //
63  void read(SWFStream& in, unsigned long endPos);
64 
65  size_t size() const { return m_buffer.size(); }
66 
67  std::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 
77  std::string disasm(size_t pc) const;
78 
80  //
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 
95  const unsigned char* getFramePointer(size_t pc) const
96  {
97  assert (pc < m_buffer.size());
98  return &m_buffer.at(pc);
99  }
100 
102  //
105  std::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  std::int16_t ret = (m_buffer[pc] | (m_buffer[pc + 1] << 8));
111  return ret;
112  }
113 
116  std::uint16_t read_uint16(size_t pc) const
117  {
118  return static_cast<std::uint16_t>(read_int16(pc));
119  }
120 
122  //
125  std::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  std::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 
139  //
142  float read_float_little(size_t pc) const;
143 
145  //
149  double read_double_wacky(size_t pc) const;
150 
152  const char* dictionary_get(size_t n) const
153  {
154  if ( _pools.empty() ) return nullptr;
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 nullptr;
162  }
163 
165  //
177  const ConstantPool& readConstantPool(size_t start_pc, size_t stop_pc) const;
178 
180  const std::string& getDefinitionURL() const;
181 
183  int getDefinitionVersion() const;
184 
186  return _src;
187  }
188 
189 private:
190 
192  std::vector<std::uint8_t> m_buffer;
193 
195  typedef std::map<size_t, ConstantPool> PoolsMap;
196  mutable PoolsMap _pools;
197 
199  //
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:
int getDefinitionVersion() const
Return version of the SWF this action block was found in.
Definition: action_buffer.cpp:479
std::string disasm(size_t pc) const
Disassemble instruction at given offset and return as a string.
Definition: action_buffer.cpp:454
const char * read_string(size_t pc) const
Get a null-terminated string from given offset.
Definition: action_buffer.h:83
Client program&#39;s interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
const char * dictionary_get(size_t n) const
Return a value from the constant pool.
Definition: action_buffer.h:152
action_buffer(const movie_definition &md)
Definition: action_buffer.cpp:40
Definition: GnashException.h:181
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
const movie_definition & getMovieDefinition() const
Definition: action_buffer.h:185
std::uint16_t read_uint16(size_t pc) const
read_int16 should check buffer boundaries.
Definition: action_buffer.h:116
Definition: GnashKey.h:160
std::int16_t read_int16(size_t pc) const
Get a signed integer value from given offset.
Definition: action_buffer.h:105
const unsigned char * getFramePointer(size_t pc) const
Get a pointer to the current instruction within the code.
Definition: action_buffer.h:95
#define _(String)
Definition: log.h:44
std::uint8_t operator[](size_t off) const
Definition: action_buffer.h:67
std::int32_t read_int32(size_t pc) const
Read a 32-bit integer starting at given offset.
Definition: action_buffer.h:125
void read(SWFStream &in, unsigned long endPos)
Read action bytes from input stream up to but not including endPos.
Definition: action_buffer.cpp:48
const ConstantPool & readConstantPool(size_t start_pc, size_t stop_pc) const
Read an SWF::ACTION_CONSTANTPOOL opcode and return as a dictionary.
Definition: action_buffer.cpp:101
double read_double_wacky(size_t pc) const
Read a 64-bit double starting at given offset.
Definition: action_buffer.cpp:467
float read_float_little(size_t pc) const
Read a little-endian 32-bit float starting at given offset.
Definition: action_buffer.cpp:461
A code segment.
Definition: action_buffer.h:49
SWF stream wrapper class.
Definition: SWFStream.h:58
size_t size() const
Definition: action_buffer.h:65
std::vector< const char * > ConstantPool
An indexed list of strings (must match the definition in action_buffer.h)
Definition: ConstantPool.h:27
const std::string & getDefinitionURL() const
Return url of the SWF this action block was found in.
Definition: action_buffer.cpp:473