1 /* compile.h
2  *
3  * $Id$
4  *
5  * Notice: This header file contains declarations of functions and types that
6  * are just used internally. All library functions and types that are supposed
7  * to be publicly accessable are defined in ./src/ming.h.
8  */
9 
10 #ifndef SWF_COMPILE_H_INCLUDED
11 #define SWF_COMPILE_H_INCLUDED
12 
13 #include "ming.h"
14 
15 extern int swfVersion;
16 
17 typedef struct _buffer *Buffer;
18 
19 /* shut up bison.simple */
20 void yyerror(char *msg);
21 int yylex();
22 
23 #ifndef max
24   #define max(x,y)	(((x)>(y))?(x):(y))
25 #endif
26 
27 enum
28 {
29   PUSH_STRING = 0,
30   PUSH_FLOAT = 1,
31   PUSH_NULL = 2,
32   PUSH_UNDEF = 3,
33   PUSH_REGISTER = 4,
34   PUSH_BOOLEAN = 5,
35   PUSH_DOUBLE = 6,
36   PUSH_INT = 7,
37   PUSH_CONSTANT = 8,
38   PUSH_CONSTANT16 = 9
39 };
40 
41 typedef enum
42 {
43   FUNCTION_RANDOM,
44   FUNCTION_LENGTH,
45   FUNCTION_TIME,
46   FUNCTION_INT,
47   FUNCTION_CONCAT,
48   FUNCTION_DUPLICATECLIP
49 } SWFActionFunction;
50 
51 typedef enum
52 {
53   GETURL_METHOD_NOSEND = 0,
54   GETURL_METHOD_GET    = 1,
55   GETURL_METHOD_POST   = 2
56 } SWFGetUrl2Method;
57 
58 typedef enum
59 {
60 	/** Bind one register to "this" */
61 	PRELOAD_THIS = 1,
62 
63 	/** No "this" variable accessible by-name */
64 	SUPPRESS_THIS = 2,
65 
66 	/** Bind one register to "arguments" */
67 	PRELOAD_ARGUMENTS = 4,
68 
69 	/** No "argument" variable accessible by-name */
70 	SUPPRESS_ARGUMENTS = 8,
71 
72 	/** Bind one register to "super" */
73 	PRELOAD_SUPER = 16,
74 
75 	/** No "super" variable accessible by-name */
76 	SUPPRESS_SUPER = 32,
77 
78 	/** Bind one register to "_root" */
79 	PRELOAD_ROOT = 64,
80 
81 	/** Bind one register to "_parent" */
82 	PRELOAD_PARENT = 128,
83 
84 	/** Bind one register to "_global" */
85 	PRELOAD_GLOBAL = 256
86 
87 } SWFDefineFunction2Flags;
88 
89 #define GETURL_LOADMOVIE 0x40
90 #define GETURL_LOADVARIABLES 0x80
91 
92 #define MAGIC_CONTINUE_NUMBER 0x7FFE
93 #define MAGIC_BREAK_NUMBER    0x7FFF
94 
95 #define MAGIC_CONTINUE_NUMBER_LO 0xFE
96 #define MAGIC_CONTINUE_NUMBER_HI 0x7F
97 #define MAGIC_BREAK_NUMBER_LO    0xFF
98 #define MAGIC_BREAK_NUMBER_HI    0x7F
99 
100 #define BUFFER_INCREMENT 128
101 
102 struct _buffer
103 {
104   byte *buffer;
105   byte *pos;
106   int buffersize;
107   int free;
108   byte *pushloc;
109   int hasObject;  // simplify grammar (e.g. DELETE rule);
110 };
111 
112 #define BUFFER_SIZE sizeof(struct _buffer)
113 
114 struct exprlist_s
115 {
116 	Buffer buffer;
117 	int count;
118 };
119 
120 struct function_s
121 {
122 	char *name;
123 	struct exprlist_s params;
124 	Buffer code;
125 	int flags;
126 };
127 typedef struct function_s *ASFunction;
128 
129 struct variable_s
130 {
131 	char *name;
132 	Buffer initCode;
133 };
134 typedef struct variable_s *ASVariable;
135 
136 typedef enum
137 {
138 	UNDEF,
139 	METHOD,
140 	VARIABLE,
141 	BUFF
142 } ClassMemberType;
143 
144 struct class_member_s
145 {
146 	ClassMemberType type;
147 	union
148 	{
149 		ASFunction function;
150 		ASVariable var;
151 		Buffer buffer;
152 	} element;
153 	struct class_member_s *next;
154 };
155 typedef struct class_member_s *ASClassMember;
156 
157 struct class_s
158 {
159 	char *name;
160 	ASClassMember members;
161 };
162 typedef struct class_s *ASClass;
163 
164 struct switchcase
165 {	Buffer cond, action;
166 	int condlen, actlen, isbreak;
167 };
168 
169 struct switchcases
170 {
171 	struct switchcase *list;
172 	int count;
173 };
174 
175 enum ctx
176 {
177 	CTX_FUNCTION = 1,
178 	CTX_LOOP,
179 	CTX_FOR_IN,
180 	CTX_SWITCH,
181 
182 	CTX_BREAK,
183 	CTX_CONTINUE
184 };
185 
186 void addctx(enum ctx val);
187 void delctx(enum ctx val);
188 int chkctx(enum ctx val);
189 
190 void checkByteOrder();
191 
192 /* This is the only function needs be visible: */
193 SWFAction compileSWFActionCode(const char *script);
194 
195 /* create/destroy buffer object */
196 Buffer newBuffer();
197 void destroyBuffer(Buffer out);
198 int bufferConcat(Buffer a, Buffer b);        /* destroys b. */
199 int bufferConcatSimple(Buffer a, Buffer b);
200 int bufferWriteBuffer(Buffer a, Buffer b);   /* doesn't. */
201 
202 /* utilities for writing */
203 void bufferGrow(Buffer out);
204 void bufferCheckSize(Buffer out, int bytes);
205 
206 int bufferLength(Buffer out);
207 
208 /* constant pool stuff */
209 int addConstant(const char *s);
210 int bufferWriteConstants(Buffer out);
211 #define MAXCONSTANTPOOLSIZE 65533
212 
213 /* write data to buffer */
214 int bufferWriteOp(Buffer out, int data);
215 int bufferWritePushOp(Buffer out);
216 int bufferWriteU8(Buffer out, int data);
217 int bufferWriteS16(Buffer out, int data);
218 int bufferWriteData(Buffer out, const byte *buffer, int bytes);
219 int bufferWriteHardString(Buffer out, const char *string, int length);
220 int bufferWriteConstantString(Buffer out, const char *string, int length);
221 int bufferWriteString(Buffer out, const char *string, int length);
222 int bufferWritePushString(Buffer out, char *string, int length);
223 int bufferWriteInt(Buffer out, int i);
224 int bufferWriteFloat(Buffer out, float f);
225 int bufferWriteDouble(Buffer out, double d);
226 int bufferWriteNull(Buffer out);
227 int bufferWriteUndef(Buffer out);
228 int bufferWriteBoolean(Buffer out, int val);
229 int bufferWriteRegister(Buffer out, int num);
230 int bufferWriteSetRegister(Buffer out, int num);
231 int bufferWriteProperty(Buffer out, char *string);
232 int bufferWriteWTHITProperty(Buffer out);
233 int lookupProperty(char *string);
234 
235 /* concat b to a, destroy b */
236 char *stringConcat(char *a, char *b);
237 
238 /* resolve magic number standins to relative offsets */
239 #define bufferResolveJumps(buf) bufferResolveJumpsFull(buf, \
240     buf->pos, buf->buffer)
241 void bufferResolveJumpsFull(Buffer out, byte *break_ptr, byte *continue_ptr);
242 void bufferResolveSwitch(Buffer buffer, struct switchcases *slp);
243 
244 void bufferPatchPushLength(Buffer buffer, int len);
245 
246 int bufferWriteFunction(Buffer out, ASFunction function, int version);
247 int bufferWriteClass(Buffer out, ASClass clazz);
248 
249 ASFunction newASFunction();
250 ASVariable newASVariable(char *, Buffer);
251 ASClass newASClass(char *name, ASClassMember members);
252 
253 ASClassMember newASClassMember_function(ASFunction func);
254 ASClassMember newASClassMember_function(ASFunction func);
255 ASClassMember newASClassMember_buffer(Buffer buf);
256 ASClassMember newASClassMember_variable(ASVariable var);
257 void ASClassMember_append(ASClassMember m0, ASClassMember end);
258 
259 /* rather than setting globals... */
260 void swf4ParseInit(const char *string, int debug, int version);
261 void swf5ParseInit(const char *string, int debug, int version);
262 
263 int swf4parse(void *b);
264 int swf5parse(void *b);
265 
266 #endif /* SWF_COMPILE_H_INCLUDED */
267