Mercurial > hg > wm
annotate Meerwald/gen_kund3_sig.c @ 20:bd669312f068
suppress warnings, fix link errors
author | Peter Meerwald-Stadler <pmeerw@pmeerw.net> |
---|---|
date | Sat, 28 Jan 2023 23:54:58 +0100 |
parents | ad1d224896c5 |
children |
rev | line source |
---|---|
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 | |
8 | 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]); | |
3 | 132 } |
133 | |
134 if (sig) { | |
135 char line[32]; | |
136 fgets(line, sizeof(line), sig); | |
137 if (strspn(line, "KD3SG") >= 5) { | |
138 if (n == 0) | |
139 fscanf(sig, "%d\n", &n); | |
140 else | |
141 fscanf(sig, "%*d\n"); | |
142 if (q == 0) | |
143 fscanf(sig, "%d\n", &q); | |
144 else | |
145 fscanf(sig, "%*d\n"); | |
146 if (e < 0) | |
147 fscanf(sig, "%d\n", &e); | |
148 else | |
149 fscanf(sig, "%*d\n"); | |
150 if (f == 0) | |
151 fscanf(sig, "%d\n", &f); | |
152 else | |
153 fscanf(sig, "%*d\n"); | |
154 if (!strcmp(F, "")) | |
15 | 155 fscanf(sig, "%[^\n\r]\n", F); |
3 | 156 else |
157 fscanf(sig, "%*[^\n\r]\n"); | |
158 if (l == 0) | |
159 fscanf(sig, "%d\n", &l); | |
160 else | |
161 fscanf(sig, "%*d\n"); | |
162 } | |
163 else { | |
164 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
165 exit(1); | |
166 } | |
8 | 167 fclose(sig); |
3 | 168 } |
169 | |
170 if (s) | |
171 srandom(s); | |
172 else | |
173 srandom(time(NULL) * getpid()); | |
174 | |
175 if (n > 0) { | |
176 n_signature = fread(signature, sizeof(char), i = NBITSTOBYTES(n), in); | |
177 if (n_signature < i) { | |
178 fprintf(stderr, "%s: failed to read all %d signature bits from %s\n", progname, n, input_name); | |
179 exit(1); | |
180 } | |
181 } | |
182 else { | |
183 if (fscanf(in, "%128[^\n\r]", signature) == EOF) { | |
184 fprintf(stderr, "%s: failed to read signature bits from %s\n", progname, input_name); | |
185 exit(1); | |
186 } | |
187 n_signature = strlen(signature); | |
7
2b350281f8b0
fix number of bits written
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
3
diff
changeset
|
188 n = nbit_signature = NBYTESTOBITS(n_signature); |
3 | 189 fprintf(stderr, "%s: got %d signature bits\n", progname, nbit_signature); |
190 } | |
191 | |
192 fprintf(out, "KD3SG\n"); | |
193 fprintf(out, "%d\n", n); | |
194 fprintf(out, "%d\n", q); | |
195 fprintf(out, "%d\n", e); | |
196 fprintf(out, "%d\n", f); | |
197 fprintf(out, "%s\n", F); | |
198 fprintf(out, "%d\n", l); | |
8 | 199 fprintf(out, "%ld\n", random()); |
3 | 200 nbit_signature = NBYTESTOBITS(n_signature); |
201 binstr = malloc((nbit_signature + 1) * sizeof(char)); | |
202 sig_to_binstr(binstr); | |
203 fprintf(out, "%s\n", binstr); | |
204 free(binstr); | |
205 | |
206 fclose(out); | |
207 | |
208 exit(0); | |
209 } |