annotate Meerwald/signature.c @ 5:8191b855cd5e

Added tag v0.5 for changeset 9cf6ec970448
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Tue, 14 Aug 2007 21:13:29 +0200
parents acb6967ee76d
children f83ef905a63d
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 "signature.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 void init_signature_bits() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 bzero(signature, sizeof(signature));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 void init_signature1_bits() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 bzero(signature1, sizeof(signature1));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 void init_signature2_bits() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 bzero(signature2, sizeof(signature2));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
15 int _get_signature_bit(char *s, int lim, int n) {
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 int byte = n >> 3;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 int bit = n & 7;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 #ifdef DEBUG
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
20 if (byte < 0 || byte >= lim)
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
21 fprintf(stderr, "get_signature_bit?(): index out of range\n");
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
24 return (s[byte] & (1 << bit)) >> bit;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
25 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
26
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
27 int get_signature_bit(int n) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
28 return _get_signature_bit(signature, NSIGNATURE, n);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
29 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
30
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
31 int get_signature1_bit(int n) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
32 return _get_signature_bit(signature1, NSIGNATURE, n);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 int get_signature2_bit(int n) {
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
36 return _get_signature_bit(signature2, NSIGNATURE, n);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
39 void _set_signature_bit(char *s, int lim, int n, int v) {
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 int byte = n >> 3;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 int bit = n & 7;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 #ifdef DEBUG
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
44 if (byte < 0 || byte >= limit / 8)
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
45 fprintf(stderr, "get_signature_bit?(): index out of range\n");
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 if (v)
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
49 s[byte] |= (1 << bit);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 else
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
51 s[byte] &= ~(1 << bit);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
52 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
53
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
54 void set_signature_bit(int n, int v) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
55 _set_signature_bit(signature, NSIGNATURE, n, v);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
56 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
57
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
58 void set_signature1_bit(int n, int v) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
59 _set_signature_bit(signature1, NSIGNATURE, n, v);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 void set_signature2_bit(int n, int v) {
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
63 _set_signature_bit(signature2, NSIGNATURE, n, v);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
64 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
65
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
66 int _binstr_to_sig(const char *binstr, char *sig, int *bytes, int *bits) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
67 int n = strlen(binstr);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
68 int i;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
70 for (i = 0; i < n; i++) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
71 if (binstr[i] == '0')
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
72 _set_signature_bit(sig, NSIGNATURE, i, 0);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
73 else if (binstr[i] == '1')
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
74 _set_signature_bit(sig, NSIGNATURE, i, 1);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
75 else
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
76 return 0;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
77 }
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
79 *bytes = (n % 8 > 0) ? n / 8 + 1 : n / 8;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
80 *bits = n;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
81
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
82 return 1;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
83 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
84
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
85 int binstr_to_sig(const char *binstr) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
86 _binstr_to_sig(binstr, signature, &n_signature, &nbit_signature);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
89 int binstr_to_sig1(const char *binstr) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
90 _binstr_to_sig(binstr, signature1, &n_signature1, &nbit_signature1);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
91 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
92
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
93 int binstr_to_sig2(const char *binstr) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
94 _binstr_to_sig(binstr, signature2, &n_signature2, &nbit_signature2);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
95 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
96
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
97 int _sig_to_binstr(char *binstr, char *sig, int bits) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
98 int i;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
99
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
100 for (i = 0; i < bits; i++)
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
101 binstr[i] = _get_signature_bit(sig, NSIGNATURE, i) ? '1' : '0';
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
102
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
103 binstr[bits] = '\0';
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
104
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
105 return 1;
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
106 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
107
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
108 int sig_to_binstr(char *binstr) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
109 return _sig_to_binstr(binstr, signature, nbit_signature);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
110 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
111
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
112 int sig1_to_binstr(char *binstr) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
113 return _sig_to_binstr(binstr, signature1, nbit_signature1);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
114 }
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
115
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
116 int sig2_to_binstr(char *binstr) {
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
117 return _sig_to_binstr(binstr, signature2, nbit_signature2);
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
118 }

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