annotate Meerwald-dir/cmp_xie_sig.c @ 25:5a57a145bccb default tip

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

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.