comparison Meerwald/signature.c @ 3:acb6967ee76d

update to 0.5
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Tue, 14 Aug 2007 21:11:21 +0200
parents be303a3f5ea8
children f83ef905a63d
comparison
equal deleted inserted replaced
2:b92f06d9a967 3:acb6967ee76d
10 10
11 void init_signature2_bits() { 11 void init_signature2_bits() {
12 bzero(signature2, sizeof(signature2)); 12 bzero(signature2, sizeof(signature2));
13 } 13 }
14 14
15 int get_signature_bit(int n) { 15 int _get_signature_bit(char *s, int lim, int n) {
16 int byte = n >> 3; 16 int byte = n >> 3;
17 int bit = n & 7; 17 int bit = n & 7;
18 18
19 #ifdef DEBUG 19 #ifdef DEBUG
20 if (byte < 0 || byte >= NSIGNATURE) 20 if (byte < 0 || byte >= lim)
21 fprintf(stderr, "get_signature_bit(): index out of range\n"); 21 fprintf(stderr, "get_signature_bit?(): index out of range\n");
22 #endif 22 #endif
23 23
24 return (signature[byte] & (1 << bit)) >> bit; 24 return (s[byte] & (1 << bit)) >> bit;
25 }
26
27 int get_signature_bit(int n) {
28 return _get_signature_bit(signature, NSIGNATURE, n);
25 } 29 }
26 30
27 int get_signature1_bit(int n) { 31 int get_signature1_bit(int n) {
32 return _get_signature_bit(signature1, NSIGNATURE, n);
33 }
34
35 int get_signature2_bit(int n) {
36 return _get_signature_bit(signature2, NSIGNATURE, n);
37 }
38
39 void _set_signature_bit(char *s, int lim, int n, int v) {
28 int byte = n >> 3; 40 int byte = n >> 3;
29 int bit = n & 7; 41 int bit = n & 7;
30 42
31 #ifdef DEBUG 43 #ifdef DEBUG
32 if (byte < 0 || byte >= NSIGNATURE) 44 if (byte < 0 || byte >= limit / 8)
33 fprintf(stderr, "get_signature1_bit(): index out of range\n"); 45 fprintf(stderr, "get_signature_bit?(): index out of range\n");
34 #endif 46 #endif
35 47
36 return (signature1[byte] & (1 << bit)) >> bit; 48 if (v)
37 } 49 s[byte] |= (1 << bit);
38 50 else
39 int get_signature2_bit(int n) { 51 s[byte] &= ~(1 << bit);
40 int byte = n >> 3;
41 int bit = n & 7;
42
43 #ifdef DEBUG
44 if (byte < 0 || byte >= NSIGNATURE)
45 fprintf(stderr, "get_signature2_bit(): index out of range\n");
46 #endif
47
48 return (signature2[byte] & (1 << bit)) >> bit;
49 } 52 }
50 53
51 void set_signature_bit(int n, int v) { 54 void set_signature_bit(int n, int v) {
52 int byte = n >> 3; 55 _set_signature_bit(signature, NSIGNATURE, n, v);
53 int bit = n & 7;
54
55 #ifdef DEBUG
56 if (byte < 0 || byte >= NSIGNATURE)
57 fprintf(stderr, "get_signature_bit(): index out of range\n");
58 #endif
59
60 if (v)
61 signature[byte] |= (1 << bit);
62 else
63 signature[byte] &= ~(1 << bit);
64 } 56 }
65 57
66 void set_signature1_bit(int n, int v) { 58 void set_signature1_bit(int n, int v) {
67 int byte = n >> 3; 59 _set_signature_bit(signature1, NSIGNATURE, n, v);
68 int bit = n & 7;
69
70 #ifdef DEBUG
71 if (byte < 0 || byte >= NSIGNATURE)
72 fprintf(stderr, "get_signature1_bit(): index out of range\n");
73 #endif
74
75 if (v)
76 signature1[byte] |= (1 << bit);
77 else
78 signature1[byte] &= ~(1 << bit);
79 } 60 }
80 61
81 void set_signature2_bit(int n, int v) { 62 void set_signature2_bit(int n, int v) {
82 int byte = n >> 3; 63 _set_signature_bit(signature2, NSIGNATURE, n, v);
83 int bit = n & 7;
84
85 #ifdef DEBUG
86 if (byte < 0 || byte >= NSIGNATURE)
87 fprintf(stderr, "get_signature2_bit(): index out of range\n");
88 #endif
89
90 if (v)
91 signature2[byte] |= (1 << bit);
92 else
93 signature2[byte] &= ~(1 << bit);
94 } 64 }
95 65
66 int _binstr_to_sig(const char *binstr, char *sig, int *bytes, int *bits) {
67 int n = strlen(binstr);
68 int i;
69
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 }
78
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) {
86 _binstr_to_sig(binstr, signature, &n_signature, &nbit_signature);
87 }
88
89 int binstr_to_sig1(const char *binstr) {
90 _binstr_to_sig(binstr, signature1, &n_signature1, &nbit_signature1);
91 }
92
93 int binstr_to_sig2(const char *binstr) {
94 _binstr_to_sig(binstr, signature2, &n_signature2, &nbit_signature2);
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 }

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