3
|
1 #include "wm.h"
|
|
2 #include "signature.h"
|
|
3
|
|
4 char *progname;
|
|
5
|
|
6 void usage(void) {
|
|
7 fprintf(stderr, "usage: %s [-e n] [-f n] [-F file] [-l n] [-n n] [-o file] [-q n] [-s file] [-S n] [-v n] file\n\n", progname);
|
|
8 fprintf(stderr, "\t-h\t\tprint usage\n");
|
|
9 fprintf(stderr, "\t-e n\t\twavelet filtering method (default 2)\n");
|
|
10 fprintf(stderr, "\t-f n\t\tfilter number (default 1)\n");
|
|
11 fprintf(stderr, "\t-F file\t\tfilter definition file (default 'filter.dat')\n");
|
|
12 fprintf(stderr, "\t-l n\t\tembedding level (default 1)\n");
|
|
13 fprintf(stderr, "\t-n n\t\twatermark bit length\n");
|
|
14 fprintf(stderr, "\t-o file\t\toutput file\n");
|
|
15 fprintf(stderr, "\t-q n\t\tsignature strength (default 4)\n");
|
|
16 fprintf(stderr, "\t-s file\t\tuse signature file's embedding information\n");
|
|
17 fprintf(stderr, "\t-S n\t\tseed\n");
|
|
18 fprintf(stderr, "\t-v n\t\tverbosity level\n");
|
|
19 exit(0);
|
|
20 }
|
|
21
|
|
22 int main(int argc, char *argv[]) {
|
|
23 FILE *in = stdin;
|
|
24 FILE *out = stdout;
|
|
25 FILE *sig = NULL;
|
|
26
|
|
27 char output_name[MAXPATHLEN] = "(stdout)";
|
|
28 char input_name[MAXPATHLEN] = "(stdin)";
|
|
29 char signature_name[MAXPATHLEN];
|
|
30 char *binstr;
|
|
31
|
|
32 int verbose = 0;
|
|
33 int c;
|
|
34 int i;
|
|
35 int l = 1;
|
|
36 int n = 0;
|
|
37 int s = 0;
|
|
38 int q = 4;
|
|
39 int e = 2;
|
|
40 int f = 1;
|
|
41 char F[MAXPATHLEN] = "filter.dat";
|
|
42
|
|
43 progname = argv[0];
|
|
44
|
|
45 wm_init();
|
|
46
|
|
47 while ((c = getopt(argc, argv, "e:f:F:h?l:n:o:q:s:S:v:")) != EOF) {
|
|
48 switch (c) {
|
|
49 case 'e':
|
|
50 e = atoi(optarg);
|
|
51 if (e < 0) {
|
|
52 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, e);
|
|
53 }
|
|
54 break;
|
|
55 case 'f':
|
|
56 f = atoi(optarg);
|
|
57 if (f <= 0) {
|
|
58 fprintf(stderr, "%s: filter number %d out of range\n", progname, f);
|
|
59 exit(1);
|
|
60 }
|
|
61 break;
|
|
62 case 'F':
|
|
63 strcpy(F, optarg);
|
|
64 break;
|
|
65 case 'h':
|
|
66 case '?':
|
|
67 usage();
|
|
68 break;
|
|
69 case 'l':
|
|
70 l = atoi(optarg);
|
|
71 if (l < 1) {
|
|
72 fprintf(stderr, "%s: embedding level out of range\n", progname);
|
|
73 exit(1);
|
|
74 }
|
|
75 break;
|
|
76 case 'n':
|
|
77 n = atoi(optarg);
|
|
78 if (n < 1 || n > 1000) {
|
|
79 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
|
|
80 exit(1);
|
|
81 }
|
|
82 break;
|
|
83 case 'o':
|
|
84 if ((out = fopen(optarg, "wb")) == NULL) {
|
|
85 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
86 exit(1);
|
|
87 }
|
|
88 strcpy(output_name, optarg);
|
|
89 break;
|
|
90 case 'q':
|
|
91 q = atoi(optarg);
|
|
92 if (q < 1) {
|
|
93 fprintf(stderr, "%s: signature strength %d out of range\n", progname, q);
|
|
94 exit(1);
|
|
95 }
|
|
96 break;
|
|
97 case 's':
|
|
98 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
99 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
100 exit(1);
|
|
101 }
|
|
102 strcpy(signature_name, optarg);
|
|
103 break;
|
|
104 case 'S':
|
|
105 s = atoi(optarg);
|
|
106 break;
|
|
107 case 'v':
|
|
108 verbose = atoi(optarg);
|
|
109 if (verbose < 0) {
|
|
110 fprintf(stderr, "%s: verbosity level %d out of range", progname, verbose);
|
|
111 exit(1);
|
|
112 }
|
|
113 break;
|
|
114 }
|
|
115 }
|
|
116
|
|
117 argc -= optind;
|
|
118 argv += optind;
|
|
119
|
|
120 if (argc > 1) {
|
|
121 usage();
|
|
122 exit(1);
|
|
123 }
|
|
124
|
|
125 if (argc == 1 && *argv[0] != '-')
|
|
126 if ((in = fopen(argv[0], "rb")) == NULL) {
|
|
127 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
128 exit(1);
|
|
129 }
|
|
130 else
|
|
131 strcpy(input_name, argv[0]);
|
|
132
|
|
133 if (sig) {
|
|
134 char line[32];
|
|
135 fgets(line, sizeof(line), sig);
|
|
136 if (strspn(line, "KD3SG") >= 5) {
|
|
137 if (n == 0)
|
|
138 fscanf(sig, "%d\n", &n);
|
|
139 else
|
|
140 fscanf(sig, "%*d\n");
|
|
141 if (q == 0)
|
|
142 fscanf(sig, "%d\n", &q);
|
|
143 else
|
|
144 fscanf(sig, "%*d\n");
|
|
145 if (e < 0)
|
|
146 fscanf(sig, "%d\n", &e);
|
|
147 else
|
|
148 fscanf(sig, "%*d\n");
|
|
149 if (f == 0)
|
|
150 fscanf(sig, "%d\n", &f);
|
|
151 else
|
|
152 fscanf(sig, "%*d\n");
|
|
153 if (!strcmp(F, ""))
|
|
154 fscanf(sig, "%[^\n\r]\n", &F);
|
|
155 else
|
|
156 fscanf(sig, "%*[^\n\r]\n");
|
|
157 if (l == 0)
|
|
158 fscanf(sig, "%d\n", &l);
|
|
159 else
|
|
160 fscanf(sig, "%*d\n");
|
|
161 }
|
|
162 else {
|
|
163 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
164 exit(1);
|
|
165 }
|
|
166 close(sig);
|
|
167 }
|
|
168
|
|
169 if (s)
|
|
170 srandom(s);
|
|
171 else
|
|
172 srandom(time(NULL) * getpid());
|
|
173
|
|
174 if (n > 0) {
|
|
175 n_signature = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in);
|
|
176 if (n_signature < i) {
|
|
177 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name);
|
|
178 exit(1);
|
|
179 }
|
|
180 }
|
|
181 else {
|
|
182 if (fscanf(in, "%128[^\n\r]", signature) == EOF) {
|
|
183 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name);
|
|
184 exit(1);
|
|
185 }
|
|
186 n_signature = strlen(signature);
|
|
187 nbit_signature = NBYTESTOBITS(n_signature);
|
|
188 fprintf(stderr, "%s: got %d signature bits\n", progname, nbit_signature);
|
|
189 }
|
|
190
|
|
191 fprintf(out, "KD3SG\n");
|
|
192 fprintf(out, "%d\n", n);
|
|
193 fprintf(out, "%d\n", q);
|
|
194 fprintf(out, "%d\n", e);
|
|
195 fprintf(out, "%d\n", f);
|
|
196 fprintf(out, "%s\n", F);
|
|
197 fprintf(out, "%d\n", l);
|
|
198 fprintf(out, "%d\n", random());
|
|
199 nbit_signature = NBYTESTOBITS(n_signature);
|
|
200 binstr = malloc((nbit_signature + 1) * sizeof(char));
|
|
201 sig_to_binstr(binstr);
|
|
202 fprintf(out, "%s\n", binstr);
|
|
203 free(binstr);
|
|
204
|
|
205 fclose(out);
|
|
206
|
|
207 exit(0);
|
|
208 }
|