2 A tool for modifying swfs on the tag level
4 Part of the swftools package.
6 Copyright (c) 2008/2009 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 #include "../config.h"
27 #include "../lib/rfxswf.h"
28 #include "../lib/args.h"
30 static char * filename = 0;
33 struct options_t options[] =
40 int args_callback_option(char*name,char*val)
42 if(!strcmp(name, "V")) {
43 printf("swfedit - part of %s %s\n", PACKAGE, VERSION);
45 } else if(!strcmp(name, "v")) {
49 printf("Unknown option: -%s\n", name);
54 int args_callback_longoption(char*name,char*val)
56 return args_long2shortoption(options, name, val);
58 void args_callback_usage(char*name)
60 printf("Usage: %s [-at] file.swf\n", name);
61 printf("\t-h , --help\t\t Print help and exit\n");
62 printf("\t-v , --verbose\t\t Be more verbose\n");
63 printf("\t-V , --version\t\t Print program version and exit\n");
65 int args_callback_command(char*name,char*val)
68 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n", filename, name);
74 void dumpTag(FILE*fo, char*prefix, TAG*tag)
77 for(t=0;t<tag->len;t++) {
79 fprintf(fo, "%s| ", prefix);
80 fprintf(fo, "%02x ", tag->data[t]);
81 if((t && ((t&15)==15)) || (t==tag->len-1)) {
87 void dumpFile(SWF*swf, FILE*fo)
89 TAG* tag = swf->firstTag;
95 memset(whitespace, 32, 32); whitespace[32] = 0;
97 fprintf(fo, "Version: %d\n", swf->fileVersion);
98 fprintf(fo, "FrameRate: %f\n",swf->frameRate/256.0);
99 fprintf(fo, "FrameCount: %d\n",swf->frameCount);
100 fprintf(fo, "Width: %.2f\n",(swf->movieSize.xmax-swf->movieSize.xmin)/20.0);
101 fprintf(fo, "X-Offset: %.2f\n", swf->movieSize.xmin/20.0);
102 fprintf(fo, "Height: %.2f\n",(swf->movieSize.ymax-swf->movieSize.ymin)/20.0);
103 fprintf(fo, "Y-Offset: %.2f\n", swf->movieSize.ymin/20.0);
108 if(swf_isDefiningTag(tag)) {
109 fprintf(fo, "%s%s <%d>\n", prefix, swf_TagGetName(tag), swf_GetDefineID(tag));
110 swf_SetTagPos(tag, 2);
111 dumpTag(fo, prefix, tag);
112 } else if(swf_isPseudoDefiningTag(tag)) {
113 fprintf(fo, "%s%s <%d>\n", prefix, swf_TagGetName(tag), swf_GetDefineID(tag));
114 swf_SetTagPos(tag, 2);
115 dumpTag(fo, prefix, tag);
116 } else if(tag->id == ST_PLACEOBJECT || tag->id == ST_PLACEOBJECT2) {
118 swf_GetPlaceObject(tag, &po);
119 fprintf(fo, "%s%s <%d>\n", prefix,swf_TagGetName(tag), po.id);
121 swf_SetTagPos(tag, 0);
122 dumpTag(fo, prefix, tag);
123 swf_PlaceObjectFree(&po);
125 fprintf(fo, "%s%s\n", prefix, swf_TagGetName(tag));
126 dumpTag(fo, prefix, tag);
129 if(tag->id == ST_DEFINESPRITE) {
133 prefix = &whitespace[32-indent];
134 } else if(tag->id == ST_END) {
138 prefix = &whitespace[32-indent];
145 static void readline(FILE*fi, char*line, int maxlen) {
148 if(!fread(&line[pos],1,1,fi))
151 /* cut of preceding whitespace */
152 if(pos == 0 && (line[0] == 32 || line[0] == '\t'))
155 if(line[pos] == 13 || line[pos]==10)
162 /* cut off comments */
163 char*x=strchr(line,'#');
169 /* cut off trailing whitespace */
170 while(pos>=1 && (line[pos-1]==32 || line[pos-1]==9)) {
180 while(*s==32 || *s=='\t') s++;
181 sscanf(s, "%f%n", &x, &n);
183 fprintf(stderr, "Not a float: %s\n", s);
190 while(*s==32 || *s=='\t') s++;
191 sscanf(s, "%d%n", &i, &n);
193 fprintf(stderr, "Not an integer: %s\n", s);
198 return (int)(getFloat(x)*20);
203 int swf_TagNameToID(char*name)
207 memset(&tag, 0, sizeof(tag));
209 lookup = (char**)malloc(sizeof(char*)*65536);
210 for(t=0;t<65536;t++) {
212 lookup[t] = swf_TagGetName(&tag);
215 for(t=0;t<65536;t++) {
216 if(lookup[t] && !strcasecmp(name, lookup[t]))
219 fprintf(stderr, "Not a tag name: \"%s\"\n", name);
223 void parseFile(FILE*fi, SWF*swf)
227 memset(swf, 0, sizeof(SWF));
230 readline(fi, line, 1024);
233 colon = strchr(line, ':');
235 int num = colon - line;
237 if(num == 9 && !strncmp(line, "FrameRate", num)) {
238 swf->frameRate = getFloat(colon+1)*256;
239 } else if(num == 10 && !strncmp(line, "FrameCount", num)) {
240 swf->frameCount = getInt(colon+1);
241 } else if(num == 7 && !strncmp(line, "Version", num)) {
242 swf->fileVersion = getInt(colon+1);
243 } else if(num == 5 && !strncmp(line, "Width", num)) {
244 int width = getTwip(colon+1);
245 swf->movieSize.xmax += width;
246 } else if(num == 6 && !strncmp(line, "Height", num)) {
247 int height = getTwip(colon+1);
248 swf->movieSize.ymax += height;
249 } else if(num == 8 && !strncmp(line, "X-Offset", num)) {
250 int xoffset = getTwip(colon+1);
251 swf->movieSize.xmin += xoffset;
252 swf->movieSize.xmax += xoffset;
253 } else if(num == 8 && !strncmp(line, "Y-Offset", num)) {
254 int yoffset = getTwip(colon+1);
255 swf->movieSize.ymin += yoffset;
256 swf->movieSize.ymax += yoffset;
258 fprintf(stderr, "Ignored line \"%s\"\n", line);
265 readline(fi, line, 1024);
268 s = strchr(line, ' ');
269 br = strchr(line, '|');
272 /* DEFINESHAPE <id> ... type line */
274 tagname = strdup(line);
276 tagname = strdup(line);
279 id = swf_TagNameToID(tagname);
282 fprintf(stderr, "Ignored line \"%s\"\n", line);
285 tag = swf_InsertTag(tag, id);
289 /* | 00 34 fe c7 ... type line */
295 fprintf(stderr, "Discarded unassignable data %s\n", line);
301 if((*p>='a' && *p<='f') ||
302 (*p>='A' && *p<='F')) {
307 else if(*p>='0' && *p<='9') {
324 char swf_IsSWF(char*filename)
326 int fi = open(filename,O_RDONLY|O_BINARY);
332 if((buf[0] == 'F' || buf[0] == 'C') && buf[1] == 'W' && buf[2] == 'S')
337 int main (int argc,char ** argv)
343 processargs(argc, argv);
346 fprintf(stderr, "You must supply a filename.\n");
349 if(swf_IsSWF(filename)) {
350 fi = open(filename,O_RDONLY|O_BINARY);
352 perror("Couldn't open file: ");
355 if FAILED(swf_ReadSWF(fi,&swf)) {
356 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
360 dumpFile(&swf, stdout);
364 FILE*fi = fopen(filename, "rb");
365 parseFile(fi, &newswf);
368 char*sname = "output.swf";
369 f = open(sname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
370 if FAILED(swf_WriteSWF(f,&newswf)) {
371 fprintf(stderr, "Unable to write output file: %s\n", sname);