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