fix crashes reported by Debian Cylab Mayhem Team
[swftools.git] / lib / action / assembler.c
blob8526e99905fdc866f84e95b577bdc12ab1a465b6
1 /*
2 Ming, an SWF output library
3 Copyright (C) 2002 Opaque Industries - http://www.opaque.net/
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "assembler.h"
24 #include "compile.h"
25 #include "action.h"
28 int len;
29 Buffer asmBuffer;
30 int nLabels;
32 struct label
34 char *name;
35 int offset;
38 struct label labels[256];
41 static int
42 findLabel(char *label)
44 int i;
46 for ( i=0; i<nLabels; ++i )
48 if ( strcmp(label, labels[i].name) == 0 )
49 return i;
52 return -1;
56 static void
57 addLabel(char *label)
59 int i = findLabel(label);
61 if ( i == -1 )
63 labels[nLabels].name = strdup(label);
64 labels[nLabels].offset = len;
65 ++nLabels;
67 else
68 labels[i].offset = len;
72 int
73 bufferBranchTarget(Buffer output, char *label)
75 int i = findLabel(label);
77 if ( i == -1 )
79 i = nLabels;
80 addLabel(label);
83 return bufferWriteS16(output, i);
87 void
88 bufferPatchTargets(Buffer buffer)
90 int l, i = 0;
91 unsigned char *output = buffer->buffer;
93 while ( i < len )
95 if ( output[i] & 0x80 ) /* then it's a multibyte instruction */
97 if ( output[i] == SWFACTION_BRANCHALWAYS ||
98 output[i] == SWFACTION_BRANCHIFTRUE )
100 int target, offset;
102 i += 3; /* plus instruction plus two-byte length */
104 target = output[i];
105 offset = labels[target].offset - (i+2);
106 output[i] = offset & 0xff;
107 output[++i] = (offset>>8) & 0xff;
108 ++i;
110 else
112 ++i;
113 l = output[i];
114 ++i;
115 l += output[i]<<8;
117 i += l+1;
120 else
121 ++i;
127 * Local variables:
128 * tab-width: 2
129 * c-basic-offset: 2
130 * End: