📄 swfdec_as_interpret.c
字号:
/* Swfdec * Copyright (C) 2007 Benjamin Otte <otte@gnome.org> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include "swfdec_as_interpret.h"#include "swfdec_as_array.h"#include "swfdec_as_context.h"#include "swfdec_as_frame_internal.h"#include "swfdec_as_function.h"#include "swfdec_as_script_function.h"#include "swfdec_as_stack.h"#include "swfdec_as_string.h"#include "swfdec_as_strings.h"#include "swfdec_as_super.h"#include "swfdec_as_with.h"#include "swfdec_debug.h"#include <errno.h>#include <math.h>#include <string.h>#include "swfdec_decoder.h"#include "swfdec_movie.h"#include "swfdec_player_internal.h"#include "swfdec_sprite.h"#include "swfdec_sprite_movie.h"#include "swfdec_swf_instance.h"/* Define this to get SWFDEC_WARN'd about missing properties of objects. * This can be useful to find out about unimplemented native properties, * but usually just causes a lot of spam. *///#define SWFDEC_WARN_MISSING_PROPERTIES/*** SUPPORT FUNCTIONS ***/#define swfdec_action_has_register(cx, i) \ ((i) < (cx)->frame->n_registers)/*** ALL THE ACTION IS HERE ***/static voidswfdec_action_stop (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ if (SWFDEC_IS_SPRITE_MOVIE (cx->frame->target)) SWFDEC_SPRITE_MOVIE (cx->frame->target)->playing = FALSE; else SWFDEC_ERROR ("no movie to stop");}static voidswfdec_action_play (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ if (SWFDEC_IS_SPRITE_MOVIE (cx->frame->target)) SWFDEC_SPRITE_MOVIE (cx->frame->target)->playing = TRUE; else SWFDEC_ERROR ("no movie to play");}static voidswfdec_action_next_frame (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ if (SWFDEC_IS_SPRITE_MOVIE (cx->frame->target)) { SwfdecSpriteMovie *movie = SWFDEC_SPRITE_MOVIE (cx->frame->target); if (movie->frame < movie->n_frames) { swfdec_sprite_movie_goto (movie, movie->frame + 1); } else { SWFDEC_INFO ("can't execute nextFrame, already at last frame"); } } else { SWFDEC_ERROR ("no movie to nextFrame on"); }}static voidswfdec_action_previous_frame (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ if (SWFDEC_IS_SPRITE_MOVIE (cx->frame->target)) { SwfdecSpriteMovie *movie = SWFDEC_SPRITE_MOVIE (cx->frame->target); if (movie->frame > 1) { swfdec_sprite_movie_goto (movie, movie->frame - 1); } else { SWFDEC_INFO ("can't execute previousFrame, already at first frame"); } } else { SWFDEC_ERROR ("no movie to previousFrame on"); }}static voidswfdec_action_goto_frame (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ guint frame; if (len != 2) { SWFDEC_ERROR ("GotoFrame action length invalid (is %u, should be 2", len); return; } frame = GUINT16_FROM_LE (*((guint16 *) data)); if (SWFDEC_IS_SPRITE_MOVIE (cx->frame->target)) { SwfdecSpriteMovie *movie = SWFDEC_SPRITE_MOVIE (cx->frame->target); swfdec_sprite_movie_goto (movie, frame + 1); movie->playing = FALSE; } else { SWFDEC_ERROR ("no movie to goto on"); }}static voidswfdec_action_goto_label (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ if (!memchr (data, 0, len)) { SWFDEC_ERROR ("GotoLabel action does not specify a string"); return; } if (SWFDEC_IS_SPRITE_MOVIE (cx->frame->target)) { SwfdecSpriteMovie *movie = SWFDEC_SPRITE_MOVIE (cx->frame->target); int frame; if (movie->sprite == NULL || (frame = swfdec_sprite_get_frame (movie->sprite, (const char *) data)) == -1) return; swfdec_sprite_movie_goto (movie, frame + 1); movie->playing = FALSE; } else { SWFDEC_ERROR ("no movie to goto on"); }}/* returns: frame to go to or 0 on error */static guintswfdec_value_to_frame (SwfdecAsContext *cx, SwfdecSpriteMovie *movie, SwfdecAsValue *val){ int frame; if (movie->sprite == NULL) return 0; if (SWFDEC_AS_VALUE_IS_STRING (val)) { const char *name = SWFDEC_AS_VALUE_GET_STRING (val); double d; if (strchr (name, ':')) { SWFDEC_ERROR ("FIXME: handle targets"); } /* treat valid encoded numbers as numbers, otherwise assume it's a frame label */ d = swfdec_as_value_to_number (cx, val); if (isnan (d)) frame = swfdec_sprite_get_frame (movie->sprite, name) + 1; else frame = d; } else if (SWFDEC_AS_VALUE_IS_NUMBER (val)) { frame = swfdec_as_value_to_integer (cx, val); } else { SWFDEC_WARNING ("cannot convert value to frame number"); /* FIXME: how do we treat undefined etc? */ frame = 0; } return frame <= 0 ? 0 : frame;}static voidswfdec_action_goto_frame2 (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ SwfdecBits bits; guint bias; gboolean play; SwfdecAsValue *val; swfdec_bits_init_data (&bits, data, len); if (swfdec_bits_getbits (&bits, 6)) { SWFDEC_WARNING ("reserved bits in GotoFrame2 aren't 0"); } bias = swfdec_bits_getbit (&bits); play = swfdec_bits_getbit (&bits); if (bias) { bias = swfdec_bits_get_u16 (&bits); } val = swfdec_as_stack_peek (cx, 1); /* now set it */ if (SWFDEC_IS_SPRITE_MOVIE (cx->frame->target)) { SwfdecSpriteMovie *movie = SWFDEC_SPRITE_MOVIE (cx->frame->target); guint frame = swfdec_value_to_frame (cx, movie, val); if (frame > 0) { frame += bias; frame = CLAMP (frame, 1, movie->n_frames); swfdec_sprite_movie_goto (movie, frame); movie->playing = play; } } else { SWFDEC_ERROR ("no movie to GotoFrame2 on"); } swfdec_as_stack_pop (cx);}static voidswfdec_script_skip_actions (SwfdecAsContext *cx, guint jump){ SwfdecScript *script = cx->frame->script; guint8 *pc = cx->frame->pc; guint8 *endpc = script->buffer->data + script->buffer->length; /* jump instructions */ do { if (pc >= endpc) break; if (*pc & 0x80) { if (pc + 2 >= endpc) break; pc += 3 + GUINT16_FROM_LE (*((guint16 *) (pc + 1))); } else { pc++; } } while (jump-- > 0); cx->frame->pc = pc;}#if 0static voidswfdec_action_wait_for_frame2 (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ jsval val; if (len != 1) { SWFDEC_ERROR ("WaitForFrame2 needs a 1-byte data"); return JS_FALSE; } val = cx->fp->sp[-1]; cx->fp->sp--; if (SWFDEC_IS_SPRITE_MOVIE (cx->frame->target)) SwfdecMovie *movie = SWFDEC_MOVIE (cx->frame->target); int frame = swfdec_value_to_frame (cx, movie, val); guint jump = data[2]; guint loaded; if (frame < 0) return JS_TRUE; if (SWFDEC_IS_ROOT_MOVIE (movie)) { SwfdecDecoder *dec = SWFDEC_ROOT_MOVIE (movie)->decoder; loaded = dec->frames_loaded; g_assert (loaded <= movie->n_frames); } else { loaded = movie->n_frames; } if (loaded <= (guint) frame) swfdec_script_skip_actions (cx, jump); } else { SWFDEC_ERROR ("no movie to WaitForFrame2 on"); } return JS_TRUE;}#endifstatic voidswfdec_action_wait_for_frame (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ SwfdecSpriteMovie *movie; guint frame, jump, loaded; if (len != 3) { SWFDEC_ERROR ("WaitForFrame action length invalid (is %u, should be 3", len); return; } if (!SWFDEC_IS_SPRITE_MOVIE (cx->frame->target)) { SWFDEC_ERROR ("no movie for WaitForFrame"); return; } movie = SWFDEC_SPRITE_MOVIE (cx->frame->target); frame = data[0] || (data[1] << 8); jump = data[2]; if (SWFDEC_MOVIE (movie)->swf->movie == movie) { SwfdecDecoder *dec = SWFDEC_MOVIE (movie)->swf->decoder; loaded = dec->frames_loaded; g_assert (loaded <= movie->n_frames); if (loaded == dec->frames_total) loaded = G_MAXUINT; } else { loaded = G_MAXUINT; } if (loaded <= frame) swfdec_script_skip_actions (cx, jump);}static voidswfdec_action_constant_pool (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ SwfdecConstantPool *pool; SwfdecAsFrame *frame; frame = cx->frame; pool = swfdec_constant_pool_new_from_action (data, len, cx->version); if (pool == NULL) return; swfdec_constant_pool_attach_to_context (pool, cx); if (frame->constant_pool) swfdec_constant_pool_free (frame->constant_pool); frame->constant_pool = pool; if (frame->constant_pool_buffer) swfdec_buffer_unref (frame->constant_pool_buffer); frame->constant_pool_buffer = swfdec_buffer_new_subbuffer (frame->script->buffer, data - frame->script->buffer->data, len);}static voidswfdec_action_push (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ SwfdecBits bits; swfdec_bits_init_data (&bits, data, len); while (swfdec_bits_left (&bits)) { guint type = swfdec_bits_get_u8 (&bits); SWFDEC_LOG ("push type %u", type); swfdec_as_stack_ensure_free (cx, 1); switch (type) { case 0: /* string */ { char *s = swfdec_bits_get_string_with_version (&bits, cx->version); if (s == NULL) return; SWFDEC_AS_VALUE_SET_STRING (swfdec_as_stack_push (cx), swfdec_as_context_give_string (cx, s)); break; } case 1: /* float */ SWFDEC_AS_VALUE_SET_NUMBER (swfdec_as_stack_push (cx), swfdec_bits_get_float (&bits)); break; case 2: /* null */ SWFDEC_AS_VALUE_SET_NULL (swfdec_as_stack_push (cx)); break; case 3: /* undefined */ SWFDEC_AS_VALUE_SET_UNDEFINED (swfdec_as_stack_push (cx)); break; case 4: /* register */ { guint regnum = swfdec_bits_get_u8 (&bits); if (!swfdec_action_has_register (cx, regnum)) { SWFDEC_ERROR ("cannot Push register %u: not enough registers", regnum); SWFDEC_AS_VALUE_SET_UNDEFINED (swfdec_as_stack_push (cx)); } else { *swfdec_as_stack_push (cx) = cx->frame->registers[regnum]; } break; } case 5: /* boolean */ SWFDEC_AS_VALUE_SET_BOOLEAN (swfdec_as_stack_push (cx), swfdec_bits_get_u8 (&bits) ? TRUE : FALSE); break; case 6: /* double */ SWFDEC_AS_VALUE_SET_NUMBER (swfdec_as_stack_push (cx), swfdec_bits_get_double (&bits)); break; case 7: /* 32bit int */ SWFDEC_AS_VALUE_SET_NUMBER (swfdec_as_stack_push (cx), (int) swfdec_bits_get_u32 (&bits)); break; case 8: /* 8bit ConstantPool address */ { guint i = swfdec_bits_get_u8 (&bits); SwfdecConstantPool *pool = cx->frame->constant_pool; if (pool == NULL) { SWFDEC_ERROR ("no constant pool to push from"); return; } if (i >= swfdec_constant_pool_size (pool)) { SWFDEC_ERROR ("constant pool index %u too high - only %u elements", i, swfdec_constant_pool_size (pool)); return; } SWFDEC_AS_VALUE_SET_STRING (swfdec_as_stack_push (cx), swfdec_constant_pool_get (pool, i)); break; } case 9: /* 16bit ConstantPool address */ { guint i = swfdec_bits_get_u16 (&bits); SwfdecConstantPool *pool = cx->frame->constant_pool; if (pool == NULL) { SWFDEC_ERROR ("no constant pool to push from"); return; } if (i >= swfdec_constant_pool_size (pool)) { SWFDEC_ERROR ("constant pool index %u too high - only %u elements", i, swfdec_constant_pool_size (pool)); return; } SWFDEC_AS_VALUE_SET_STRING (swfdec_as_stack_push (cx), swfdec_constant_pool_get (pool, i)); break; } default: SWFDEC_ERROR ("Push: type %u not implemented", type); return; } }}static voidswfdec_action_get_variable (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ const char *s; s = swfdec_as_value_to_string (cx, swfdec_as_stack_peek (cx, 1)); swfdec_as_context_eval (cx, NULL, s, swfdec_as_stack_peek (cx, 1));#ifdef SWFDEC_WARN_MISSING_PROPERTIES if (SWFDEC_AS_VALUE_IS_UNDEFINED (swfdec_as_stack_peek (cx, 1))) { SWFDEC_WARNING ("no variable named %s", s); }#endif}static voidswfdec_action_set_variable (SwfdecAsContext *cx, guint action, const guint8 *data, guint len){ const char *s; s = swfdec_as_value_to_string (cx, swfdec_as_stack_peek (cx, 2)); swfdec_as_context_eval_set (cx, NULL, s, swfdec_as_stack_peek (cx, 1)); swfdec_as_stack_pop_n (cx, 2);}static const char *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -