0
|
1 #include "wm.h"
|
|
2 #include "signature.h"
|
|
3
|
|
4 char *progname;
|
|
5
|
|
6 void usage(void) {
|
|
7 fprintf(stderr, "usage: %s [-a n] [-g n] [-o file] [-s n] file\n\n", progname);
|
|
8 fprintf(stderr, "\t-a n\t\talpha factor (default 0.25)\n");
|
|
9 fprintf(stderr, "\t-g n\t\tgamma factor (default 1.0)\n");
|
|
10 fprintf(stderr, "\t-s n\t\tseed (default 0)\n");
|
|
11 fprintf(stderr, "\t-h\t\tprint usage\n");
|
|
12 fprintf(stderr, "\t-n n\t\twatermark length (default 100)\n");
|
|
13 fprintf(stderr, "\t-o file\t\toutput file\n");
|
|
14 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n");
|
|
15 fprintf(stderr, "\t-S n\t\tseed\n");
|
|
16 exit(0);
|
|
17 }
|
|
18
|
|
19 int main(int argc, char *argv[]) {
|
|
20 FILE *in = stdin;
|
|
21 FILE *out = stdout;
|
|
22 FILE *sig = NULL;
|
|
23
|
|
24 char output_name[MAXPATHLEN] = "(stdout)";
|
|
25 char input_name[MAXPATHLEN] = "(stdin)";
|
|
26 char signature_name[MAXPATHLEN];
|
|
27
|
|
28 int c;
|
|
29 int i;
|
|
30 int n = 100, nb;
|
|
31 double a = 0.25;
|
|
32 double g = 1.0;
|
|
33 int s = 0;
|
|
34
|
|
35 progname = argv[0];
|
|
36
|
|
37 #ifdef __EMX__
|
|
38 _fsetmode(in, "b");
|
|
39 _fsetmode(out, "b");
|
|
40 #endif
|
|
41
|
|
42 while ((c = getopt(argc, argv, "a:g:h?n:o:s:S:")) != EOF) {
|
|
43 switch (c) {
|
|
44 case 'a':
|
|
45 a = atof(optarg);
|
|
46 if (a <= 0.0) {
|
|
47 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a);
|
|
48 exit(1);
|
|
49 }
|
|
50 break;
|
|
51 case 'g':
|
|
52 g = atof(optarg);
|
|
53 if (g <= 0.0) {
|
|
54 fprintf(stderr, "%s: gamma factor %f out of range\n", progname, a);
|
|
55 exit(1);
|
|
56 }
|
|
57 break;
|
|
58 case 'h':
|
|
59 case '?':
|
|
60 usage();
|
|
61 break;
|
|
62 case 'n':
|
|
63 n = atoi(optarg);
|
|
64 if (n < 1 || n > 1000) {
|
|
65 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
|
|
66 exit(1);
|
|
67 }
|
|
68 break;
|
|
69 case 'o':
|
|
70 if ((out = fopen(optarg, "w")) == NULL) {
|
|
71 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
72 exit(1);
|
|
73 }
|
|
74 strcpy(output_name, optarg);
|
|
75 break;
|
|
76 case 's':
|
|
77 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
78 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
79 exit(1);
|
|
80 }
|
|
81 strcpy(signature_name, optarg);
|
|
82 break;
|
|
83 case 'S':
|
|
84 s = atoi(optarg);
|
|
85 break;
|
|
86 }
|
|
87 }
|
|
88
|
|
89 argc -= optind;
|
|
90 argv += optind;
|
|
91
|
|
92 if (argc > 1) {
|
|
93 usage();
|
|
94 exit(1);
|
|
95 }
|
|
96
|
8
|
97 if (argc == 1 && *argv[0] != '-') {
|
|
98 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
99 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
100 exit(1);
|
|
101 }
|
|
102 else
|
|
103 strcpy(input_name, argv[0]);
|
0
|
104 }
|
|
105
|
|
106 if (!s)
|
|
107 s = time(NULL) * getpid();
|
|
108 srandom(s);
|
|
109
|
|
110 if (sig) {
|
|
111 char line[128];
|
|
112 fgets(line, sizeof(line), sig);
|
|
113 if (strspn(line, "FR2SG") >= 5) {
|
|
114 }
|
|
115 else {
|
|
116 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
117 exit(1);
|
|
118 }
|
|
119 fclose(sig);
|
|
120 }
|
|
121
|
|
122 if (n > 0) {
|
|
123 nb = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in);
|
|
124 if (nb < i) {
|
|
125 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name);
|
|
126 exit(1);
|
|
127 }
|
|
128 }
|
|
129 else {
|
|
130 if (fscanf(in, "%128[^\n\r]", signature) == EOF) {
|
|
131 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name);
|
|
132 exit(1);
|
|
133 }
|
|
134 nb = strlen(signature);
|
|
135 n = NBYTESTOBITS(nb);
|
|
136 fprintf(stderr, "%s: got %d signature bits\n", progname, n);
|
|
137 }
|
|
138
|
|
139 fprintf(out, "FR2SG\n");
|
|
140 fprintf(out, "%d\n", n);
|
|
141 fprintf(out, "%f\n", a);
|
|
142 fprintf(out, "%f\n", g);
|
|
143 fprintf(out, "%d\n", s);
|
|
144 fwrite(signature, sizeof(char), nb, out);
|
|
145 fprintf(out, "\n");
|
|
146
|
|
147 fclose(out);
|
|
148
|
|
149 exit(0);
|
|
150 }
|