new parameter -s textonly
[swftools.git] / lib / example / protect.c
1 /* protect.c 
2
3    Example for protecting/unprotecting SWFs.
4    
5    Part of the swftools package.
6
7    Copyright (C) 2004 Matthias Kramm
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22
23 #include <stdio.h>
24 #include "../rfxswf.h"
25
26 int main(int argn,char ** argv)
27
28     int fi;
29     SWF swf;
30     TAG* tag;
31     char* outfilename = "test.swf";
32     char*oldpassword = "fireworks";
33     char*newpassword = "cyberdine";
34
35     if (argn<1)
36     { 
37         return 0;
38     }
39         
40     fi = open(argv[1],O_RDONLY|O_BINARY);
41         
42     if (fi<=0)
43     { 
44         fprintf(stderr,"Couldn't open %s\n", argv[1]);
45     }
46
47     if(swf_ReadSWF(fi,&swf)<0)
48     { 
49         fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",argv[1]);
50         close(fi);
51     }
52
53     tag = swf.firstTag;
54
55     while(tag) {
56         TAG*next = tag->next;
57         //void  swf_SetPassword(TAG * t, const char * password);
58         //int   swf_VerifyPassword(TAG * t, const char * password);
59         if(tag->id == ST_PROTECT) {
60             int ret;
61             ret = swf_VerifyPassword(tag, oldpassword);
62             if(!ret) printf("Password validation failed\n");
63             else printf("Password ok\n");
64
65             swf_DeleteTag(&swf, tag);
66         }
67         tag = next;
68     }
69
70     tag = swf_InsertTag(swf.firstTag, ST_PROTECT);
71     swf_SetPassword(tag, newpassword);
72
73     fi = open(outfilename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
74     if(fi<0) {
75         fprintf(stderr, "couldn't create output file %s", outfilename);
76     }
77     if(swf_WriteSWF(fi, &swf)<0) 
78         fprintf(stderr, "WriteSWF() failed.\n");
79
80     close(fi);
81     
82     return 0;
83 }
84