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