-
Notifications
You must be signed in to change notification settings - Fork 28
/
swfmill.cpp
482 lines (406 loc) · 12.2 KB
/
swfmill.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <libexslt/exslt.h>
#include "SWFFile.h"
#include "swft.h"
#include "xslt.h"
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <dirent.h>
using namespace SWF;
bool quiet = false;
bool verbose = false;
bool dump = false;
bool nonet = false;
const char *internal_stylesheet = NULL;
void usage() {
fprintf( stderr,
"\n"PACKAGE_NAME" "PACKAGE_VERSION"\n"
" XML-based SWF processing tool\n"
"\n"
"usage: swfmill [<options>] <command>\n"
"\n"
"<command> is one of:\n"
" swf2xml <in> [<out>]\n"
" convert from SWF to XML.\n"
" <in> is a single SWF file, or 'stdin'\n"
" <out> is a single XML file, or (by default) 'stdout'\n"
"\n"
" xml2swf <in> [<out>]\n"
" convert from XML to SWF.\n"
" <in> is a single XML file, or 'stdin'\n"
" <out> is a single SWF file, or (by default) 'stdout'\n"
"\n"
" simple <in> [<out>]\n"
" convert from a movie definition file to SWF.\n"
" <in> is a single XML file, or 'stdin'\n"
" <out> is a single SWF file, or (by default) 'stdout'\n"
" (for details, see README)\n"
"\n"
" xslt <xsl> <in> [<out>"
//" [<param>*]"
"]\n"
" transform <in> to <out> as described by <xsl>.\n"
" <xsl> is the XSLT stylesheet,\n"
" and can use the swft: extension.\n"
" <in> must be some XML (depends on <xsl>)\n"
" <out> is either SWF (when it ends in .swf)\n"
" or XML, by default on 'stdout'\n"
// " <param>* is a whitespace-separated list of name=value\n"
// " assignments for xsl parameters\n"
"\n"
"<option>s are:\n"
" -h print this help and quit\n"
" -v verbose output\n"
" -V extra-verbose debugging output\n"
" -d dump SWF data when loaded (for debugging)\n"
" -n deactivate libxml network access\n"
"\n"
"Please report bugs at http://bugs.launchpad.net/swfmill/+filebug\n\n"
);
}
// helper func
xsltStylesheetPtr xsltParseStylesheetMemory( const char *buffer, int size ) {
xmlDocPtr doc = xmlParseMemory( buffer, size );
if( !doc ) return NULL;
xsltStylesheetPtr ret = xsltParseStylesheetDoc( doc );
// xmlFreeDoc( doc );
return ret;
}
int swfmill_swf2xml( int argc, char *argv[] ) {
const char *infile = NULL, *outfile = "stdout";
bool success = false;
FILE *in_fp=0, *out_fp=0;
bool std_in, std_out;
File input;
unsigned int filesize, size, xmlsize;
struct stat filestat;
char sig;
Context ctx;
// parse filenames
if( argc < 1 || argc > 2 ) {
usage();
goto fail;
}
infile = argv[0];
if( argc>1 ) outfile = argv[1];
// open files
std_in = !strncmp( infile, "stdin", 5 );
std_out = !strncmp( outfile, "stdout", 6 );
in_fp = std_in ? stdin : fopen( infile, "rb" );
if( !in_fp ) {
fprintf(stderr,"ERROR: could not open file %s for reading\n", infile?infile:"stdin" );
goto fail;
}
if( !quiet ) fprintf(stderr,"Reading from %s\n", infile );
// stat filesize
filesize = (unsigned int)-1;
if( !std_in ) {
stat( infile, &filestat );
filesize = filestat.st_size;
// fprintf(stderr,"Filesize: %u\n",filesize);
}
// setup context
ctx.debugTrace = verbose;
ctx.quiet = quiet;
// treat input as SWF, produce XML
if( (size = input.load( in_fp, &ctx, filesize )) != 0 ) {
if( dump ) input.dump();
out_fp = std_out ? stdout : fopen( outfile, "wb" );
if( !out_fp ) {
fprintf(stderr,"ERROR: could not open file %s for writing\n", outfile );
goto fail;
}
if( !quiet ) fprintf(stderr,"Writing XML to %s\n", outfile );
if( (xmlsize = input.saveXML( out_fp, &ctx )) != 0 ) {
if( !quiet ) fprintf(stderr,"XML saved to %s (%i bytes).\n", outfile, xmlsize );
success = true;
}
}
fail:
if( in_fp && !std_in ) fclose(in_fp);
if( out_fp && !std_out ) fclose(out_fp);
return success ? 0 : -1;
}
int swfmill_xml2swf( int argc, char *argv[] ) {
const char *infile = NULL, *outfile = "stdout";
bool success = false;
FILE *in_fp=0, *out_fp=0;
bool std_in, std_out;
const char *filename;
xsltStylesheetPtr transform = NULL;
File input;
int size, xmlsize;
Context ctx;
xmlDocPtr doc = NULL, doc2;
// parse filenames
if( argc < 1 || argc > 2 ) {
usage();
goto fail;
}
infile = argv[0];
if( argc>1 ) outfile = argv[1];
// open files
std_in = !strncmp( infile, "stdin", 5 );
std_out = !strncmp( outfile, "stdout", 6 );
in_fp = std_in ? stdin : fopen( infile, "rb" );
if( !in_fp ) {
fprintf(stderr,"ERROR: could not open file %s for reading\n", infile?infile:"stdin" );
goto fail;
}
if( !quiet ) fprintf(stderr,"Reading from %s\n", infile );
// setup context
ctx.debugTrace = verbose;
ctx.quiet = quiet;
{
filename = std_in ? "-" : infile ;
doc = xmlParseFile( filename );
if( !doc ) {
fprintf( stderr, "ERROR: input document %s could not be read.\n", infile );
goto fail;
}
// see if one of the internal stylesheets should be used
if( internal_stylesheet ) {
transform = xsltParseStylesheetMemory( internal_stylesheet, strlen(internal_stylesheet) );
if( !transform ) {
fprintf( stderr, "ERROR: internal stylesheet could not be read.\n" );
goto fail;
}
const char *params[3];
params[0] = "quiet";
params[1] = "\"false\"";
params[2] = NULL;
if( quiet ) {
params[1] = "\"true\"";
}
doc2 = xsltApplyStylesheet( transform, doc, (const char **)¶ms );
if( !doc2 ) {
fprintf( stderr, "ERROR: transformation failed.\n" );
goto fail;
}
xmlFreeDoc( doc );
doc = doc2;
}
// treat input as XML, produce SWF
input.setXML( doc->xmlRootNode, NULL );
if( dump ) input.dump();
out_fp = std_out ? stdout : fopen( outfile, "wb" );
if( !out_fp ) {
fprintf(stderr,"ERROR: could not open file '%s' for writing\n", outfile );
goto fail;
}
if( !quiet ) fprintf(stderr,"Writing SWF to %s\n", outfile );
if( (size = input.save( out_fp, &ctx )) != 0 ) {
success = true;
if( !quiet ) fprintf(stderr,"SWF saved to %s (%i bytes).\n", outfile, size );
}
}
fail:
if( doc ) xmlFreeDoc( doc );
if( in_fp && !std_in ) fclose(in_fp);
if( out_fp && !std_out ) fclose(out_fp);
return success ? 0 : -1;
}
int swfmill_do_xslt( xmlDocPtr doc, xsltStylesheetPtr transform, const char *outfile );
int swfmill_xslt( int argc, char *argv[] ) {
const char *xslfile, *infile, *outfile = "stdout";
xmlDocPtr doc = NULL;
xsltStylesheetPtr transform;
// parse filenames
if( argc < 2 || argc > 3 ) {
usage();
goto fail;
}
xslfile = argv[0];
infile = argv[1];
if( argc>2 ) outfile = argv[2];
doc = xmlParseFile( infile );
if( !doc ) {
fprintf( stderr, "ERROR: input document %s could not be read.\n", infile );
goto fail;
}
transform = xsltParseStylesheetFile( (const xmlChar *)xslfile );
if( !transform ) {
fprintf( stderr, "ERROR: stylesheet %s could not be read.\n", xslfile );
goto fail;
}
return swfmill_do_xslt( doc, transform, outfile );
fail:
return -1;
}
int swfmill_do_xslt( xmlDocPtr doc, xsltStylesheetPtr transform, const char *outfile ) {
xmlDocPtr doc2 = NULL;
FILE *out_fp;
const char *ext;
Context ctx;
int size;
const char *params[3];
params[0] = "quiet";
params[1] = "\"FALSE\"";
params[2] = NULL;
if( quiet ) {
params[1] = "\"FALSE\"";
}
// setup context
ctx.debugTrace = verbose;
ctx.quiet = quiet;
if( !quiet ) fprintf( stderr, "Applying XSLT...\n" );
doc2 = xsltApplyStylesheet( transform, doc, params );
if( !doc2 ) {
fprintf( stderr, "ERROR: transformation failed.\n" );
goto fail;
}
if( (ext = strrchr(outfile,'.'))!=NULL && !strncmp( ".swf", ext, 4 ) ) {
if( !quiet ) fprintf( stderr, "Saving to SWF %s\n", outfile);
File swf;
swf.setXML( doc2->xmlRootNode, NULL );
out_fp = fopen( outfile, "wb" );
if( !out_fp ) {
fprintf(stderr,"ERROR: could not open file '%s' for writing\n", outfile );
goto fail;
}
if( !quiet ) fprintf(stderr,"Writing SWF to %s\n", outfile );
if( (size = swf.save( out_fp, &ctx )) != 0 ) {
if( !quiet ) fprintf(stderr,"SWF saved to %s (%i bytes).\n", outfile, size );
if( doc ) xmlFreeDoc(doc);
if( doc2 ) xmlFreeDoc(doc2);
if( transform ) xsltFreeStylesheet( transform );
return 0;
}
} else {
bool std_out = !strncmp( outfile, "stdout", 6 );
const char *file = std_out ? "-" : outfile;
return xsltSaveResultToFilename( file, doc2, transform, 0 );
}
fail:
if( doc ) xmlFreeDoc(doc);
if( doc2 ) xmlFreeDoc(doc2);
if( transform ) xsltFreeStylesheet( transform );
return -1;
}
void swfmill_create_library( xmlNodePtr lib, const char *filename );
void swfmill_create_library_dir( xmlNodePtr lib, const char *dir ) {
char tmp[256];
struct dirent *e;
DIR *d = opendir( dir );
if( d == NULL ) return;
while( true ) {
e = readdir(d);
if( e==NULL ) break;
if( e->d_name[0] == '.' ) continue; // no hidden files
snprintf( tmp, 1023, "%s/%s", dir, e->d_name );
swfmill_create_library( lib, tmp );
}
}
void swfmill_create_library( xmlNodePtr lib, const char *filename ) {
struct stat s;
if( stat(filename,&s) ) return;
/*
int i=strlen(filename)-1;
while( i>0 && filename[i]!='.' ) i--;
const char *ext = &filename[i];
*/
if( S_ISDIR(s.st_mode) ) {
swfmill_create_library_dir( lib, filename );
} else {
// if( !strcmp( ext, ".jpg" ) || !strcmp( ext, ".jpeg" ) ) {
xmlNodePtr node = xmlNewChild( lib, NULL, (const xmlChar*)"clip", NULL );
xmlSetProp( node, (const xmlChar *)"id", (const xmlChar *)filename );
xmlSetProp( node, (const xmlChar *)"import", (const xmlChar *)filename );
}
// fprintf(stderr, "clip %s: %s (%s)\n", filename, S_ISDIR(s.st_mode)?"dir":"file", ext );
}
int swfmill_library( int argc, char *argv[] ) {
if( argc < 2 ) {
fprintf( stderr, "library creation needs at least two arguments\n\tswfmill library <input file(s)/dir(s)> <output>.swf\n" );
return -1;
}
const char *outfile = argv[argc-1];
argc--;
internal_stylesheet = xslt_simple;
xmlDocPtr doc = xmlNewDoc((const xmlChar*)"1.0");
doc->children = xmlNewDocNode( doc, NULL, (const xmlChar*)"movie", NULL );
xmlNodePtr lib = xmlNewChild( doc->children, NULL, (const xmlChar *)"library", NULL );
for( int i=0; i<argc; i++ ) {
swfmill_create_library( lib, argv[i] );
}
xmlNodePtr node = xmlNewChild( lib, NULL, (const xmlChar *)"frame", NULL );
// xmlSetProp( node, (const xmlChar*)"id", (const xmlChar *)"foo");
xsltStylesheetPtr transform = xsltParseStylesheetMemory( internal_stylesheet, strlen(internal_stylesheet) );
if( !transform ) {
fprintf( stderr, "ERROR: internal stylesheet could not be read.\n" );
return -1;
}
swfmill_do_xslt( doc, transform, outfile );
}
int main( int argc, char *argv[] ) {
char *command = NULL;
swft_register();
exsltRegisterAll();
quiet = true;
// parse args
int i=1;
for( ; i<argc && command==NULL; i++ ) {
if( argv[i][0] == '-' ) {
int swallow=0;
for( int j=1; j<strlen(argv[i]); j++ ) {
switch( argv[i][j] ) {
case 'q':
quiet = true;
break;
case 'v':
quiet = false;
break;
case 'V':
verbose = true;
break;
case 'd':
dump = true;
break;
case 'n':
nonet = true;
break;
case 'h':
case '?':
usage();
goto fail;
break;
default:
fprintf(stderr,"ERROR: unknown option %c\n",argv[i][j]);
usage();
goto fail;
}
}
i+=swallow;
} else {
if( !command ) command=argv[i];
}
}
if( !command ) {
usage();
goto fail;
}
if( nonet ) xmlSetExternalEntityLoader( xmlNoNetExternalEntityLoader );
if( !quiet ) fprintf( stderr, "%s %s\n", PACKAGE_NAME, PACKAGE_VERSION );
if( !strcmp( command, "swf2xml" ) ) {
return swfmill_swf2xml( argc-i, &argv[i] );
} else if( !strcmp( command, "xml2swf" ) ) {
return swfmill_xml2swf( argc-i, &argv[i] );
} else if( !strcmp( command, "xslt" ) ) {
return swfmill_xslt( argc-i, &argv[i] );
} else if( !strcmp( command, "simple" ) ) {
internal_stylesheet = xslt_simple;
return swfmill_xml2swf( argc-i, &argv[i] );
} else if( !strcmp( command, "library" ) ) {
return swfmill_library( argc-i, &argv[i] );
} else {
fprintf(stderr,"ERROR: unknown command %s\n", command );
usage();
goto fail;
}
fail:
return -1;
}