3
|
1 #include "wm.h"
|
|
2 #include "signature.h"
|
|
3
|
|
4 char *progname;
|
|
5
|
|
6 void usage(void) {
|
|
7 fprintf(stderr, "usage: %s [-h] [-C] [-o file] [-v n] -s file file\n\n", progname);
|
|
8 fprintf(stderr, "\t-C\t\toutput correlation only\n");
|
|
9 fprintf(stderr, "\t-h\t\tprint usage\n");
|
|
10 fprintf(stderr, "\t-o file\t\toutput file\n");
|
|
11 fprintf(stderr, "\t-s file\t\toriginal signature file\n");
|
|
12 fprintf(stderr, "\t-v n\t\tverbosity level\n");
|
|
13 exit(0);
|
|
14 }
|
|
15
|
|
16 int main(int argc, char *argv[]) {
|
|
17
|
|
18 FILE *in = stdin;
|
|
19 FILE *out = stdout;
|
|
20 FILE *sig = NULL;
|
|
21
|
|
22 char signature_name[MAXPATHLEN];
|
|
23 char output_name[MAXPATHLEN] = "(stdout)";
|
|
24 char input_name[MAXPATHLEN] = "(stdin)";
|
|
25
|
|
26 char *binstr1;
|
|
27 char *binstr2;
|
|
28
|
|
29 int correlation_only = 0;
|
|
30
|
|
31 int c, i;
|
|
32 int quality = 0;
|
|
33 int corr = 0, match = 0;
|
|
34 int verbose = 0;
|
|
35 int filter = 0;
|
|
36 int method = 0;
|
|
37 int level = 0;
|
|
38 char filter_name[MAXPATHLEN] = "";
|
|
39
|
|
40 int seed;
|
|
41 char line[32];
|
|
42
|
|
43 progname = argv[0];
|
|
44
|
|
45 while ((c = getopt(argc, argv, "h?Co:s:v:")) != EOF) {
|
|
46 switch (c) {
|
|
47 case 'h':
|
|
48 case '?':
|
|
49 usage();
|
|
50 break;
|
|
51 case 'C':
|
|
52 correlation_only = 1;
|
|
53 break;
|
|
54 case 'o':
|
|
55 if ((out = fopen(optarg, "w")) == NULL) {
|
|
56 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
|
|
57 exit(1);
|
|
58 }
|
|
59 strcpy(output_name, optarg);
|
|
60 break;
|
|
61 case 's':
|
|
62 if ((sig = fopen(optarg, "r")) == NULL) {
|
|
63 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg);
|
|
64 exit(1);
|
|
65 }
|
|
66 strcpy(signature_name, optarg);
|
|
67 break;
|
|
68 case 'v':
|
|
69 verbose = atoi(optarg);
|
|
70 if (verbose < 0) {
|
|
71 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose);
|
|
72 exit(1);
|
|
73 }
|
|
74 break;
|
|
75 }
|
|
76 }
|
|
77
|
|
78 argc -= optind;
|
|
79 argv += optind;
|
|
80
|
|
81 if (argc > 1) {
|
|
82 usage();
|
|
83 exit(1);
|
|
84 }
|
|
85
|
8
|
86 if (argc == 1 && *argv[0] != '-') {
|
3
|
87 if ((in = fopen(argv[0], "r")) == NULL) {
|
|
88 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]);
|
|
89 exit(1);
|
|
90 }
|
|
91 else
|
|
92 strcpy(input_name, argv[0]);
|
8
|
93 }
|
|
94
|
3
|
95 if (sig) {
|
|
96 fgets(line, sizeof(line), sig);
|
|
97 if (strspn(line, "KD3SG") >= 5) {
|
|
98 fscanf(sig, "%d\n", &nbit_signature1);
|
|
99 fscanf(sig, "%d\n", &quality);
|
|
100 fscanf(sig, "%d\n", &method);
|
|
101 fscanf(sig, "%d\n", &filter);
|
|
102 fscanf(sig, "%[^\n\r]\n", &filter_name);
|
|
103 fscanf(sig, "%d\n", &level);
|
|
104 fscanf(sig, "%d\n", &seed);
|
|
105 srandom(seed);
|
|
106 n_signature1 = NBITSTOBYTES(nbit_signature1);
|
|
107
|
|
108 binstr1 = malloc((nbit_signature1 + 1) * sizeof(char));
|
|
109 fscanf(sig, "%[01]\n", binstr1);
|
|
110 binstr_to_sig1(binstr1);
|
|
111 free(binstr1);
|
|
112 }
|
|
113 else {
|
|
114 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name);
|
|
115 exit(1);
|
|
116 }
|
8
|
117 fclose(sig);
|
3
|
118 }
|
|
119 else {
|
|
120 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname);
|
|
121 exit(1);
|
|
122 }
|
|
123
|
|
124 fgets(line, sizeof(line), in);
|
|
125 if (strspn(line, "KD3WM") >= 5) {
|
|
126 fscanf(in, "%d\n", &nbit_signature2);
|
|
127 n_signature2 = NBITSTOBYTES(nbit_signature2);
|
|
128 binstr2 = malloc((nbit_signature2 + 1) * sizeof(char));
|
|
129 fscanf(in, "%[01]\n", binstr2);
|
|
130 binstr_to_sig2(binstr2);
|
|
131 free(binstr2);
|
|
132 }
|
|
133 else {
|
|
134 fprintf(stderr, "%s: invalid watermark file %s\n", progname, input_name);
|
|
135 exit(1);
|
|
136 }
|
|
137
|
|
138 if (verbose > 0) {
|
|
139 fprintf(stderr, "signature length: %d\n", nbit_signature1);
|
|
140 fprintf(stderr, "watermark length: %d\n", nbit_signature2);
|
|
141 }
|
|
142
|
|
143 for (i = 0; i < nbit_signature2; i++)
|
|
144 if (get_signature1_bit(i % nbit_signature1) == get_signature2_bit(i))
|
|
145 corr++, match++;
|
|
146 else
|
|
147 corr--;
|
|
148
|
|
149 if (correlation_only)
|
|
150 fprintf(out, "%lf\n", (double) corr / nbit_signature2);
|
|
151 else {
|
|
152 fprintf(out, "bit matches: %d/%d\n", match, nbit_signature2);
|
|
153 fprintf(out, "correlation: %lf\n", (double) corr / nbit_signature2);
|
|
154 }
|
|
155
|
|
156 exit(0);
|
|
157 }
|