Mercurial > hg > wm
diff 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 |
line wrap: on
line diff
--- a/Meerwald/signature.c Tue Aug 14 19:59:48 2007 +0200 +++ b/Meerwald/signature.c Tue Aug 14 21:11:21 2007 +0200 @@ -12,84 +12,107 @@ bzero(signature2, sizeof(signature2)); } -int get_signature_bit(int n) { - int byte = n >> 3; - int bit = n & 7; - -#ifdef DEBUG - if (byte < 0 || byte >= NSIGNATURE) - fprintf(stderr, "get_signature_bit(): index out of range\n"); -#endif - - return (signature[byte] & (1 << bit)) >> bit; -} - -int get_signature1_bit(int n) { +int _get_signature_bit(char *s, int lim, int n) { int byte = n >> 3; int bit = n & 7; #ifdef DEBUG - if (byte < 0 || byte >= NSIGNATURE) - fprintf(stderr, "get_signature1_bit(): index out of range\n"); + if (byte < 0 || byte >= lim) + fprintf(stderr, "get_signature_bit?(): index out of range\n"); #endif - return (signature1[byte] & (1 << bit)) >> bit; + return (s[byte] & (1 << bit)) >> bit; +} + +int get_signature_bit(int n) { + return _get_signature_bit(signature, NSIGNATURE, n); +} + +int get_signature1_bit(int n) { + return _get_signature_bit(signature1, NSIGNATURE, n); } int get_signature2_bit(int n) { - int byte = n >> 3; - int bit = n & 7; - -#ifdef DEBUG - if (byte < 0 || byte >= NSIGNATURE) - fprintf(stderr, "get_signature2_bit(): index out of range\n"); -#endif - - return (signature2[byte] & (1 << bit)) >> bit; + return _get_signature_bit(signature2, NSIGNATURE, n); } -void set_signature_bit(int n, int v) { +void _set_signature_bit(char *s, int lim, int n, int v) { int byte = n >> 3; int bit = n & 7; #ifdef DEBUG - if (byte < 0 || byte >= NSIGNATURE) - fprintf(stderr, "get_signature_bit(): index out of range\n"); -#endif - - if (v) - signature[byte] |= (1 << bit); - else - signature[byte] &= ~(1 << bit); -} - -void set_signature1_bit(int n, int v) { - int byte = n >> 3; - int bit = n & 7; - -#ifdef DEBUG - if (byte < 0 || byte >= NSIGNATURE) - fprintf(stderr, "get_signature1_bit(): index out of range\n"); + if (byte < 0 || byte >= limit / 8) + fprintf(stderr, "get_signature_bit?(): index out of range\n"); #endif if (v) - signature1[byte] |= (1 << bit); + s[byte] |= (1 << bit); else - signature1[byte] &= ~(1 << bit); + s[byte] &= ~(1 << bit); +} + +void set_signature_bit(int n, int v) { + _set_signature_bit(signature, NSIGNATURE, n, v); +} + +void set_signature1_bit(int n, int v) { + _set_signature_bit(signature1, NSIGNATURE, n, v); } void set_signature2_bit(int n, int v) { - int byte = n >> 3; - int bit = n & 7; + _set_signature_bit(signature2, NSIGNATURE, n, v); +} + +int _binstr_to_sig(const char *binstr, char *sig, int *bytes, int *bits) { + int n = strlen(binstr); + int i; -#ifdef DEBUG - if (byte < 0 || byte >= NSIGNATURE) - fprintf(stderr, "get_signature2_bit(): index out of range\n"); -#endif + for (i = 0; i < n; i++) { + if (binstr[i] == '0') + _set_signature_bit(sig, NSIGNATURE, i, 0); + else if (binstr[i] == '1') + _set_signature_bit(sig, NSIGNATURE, i, 1); + else + return 0; + } - if (v) - signature2[byte] |= (1 << bit); - else - signature2[byte] &= ~(1 << bit); + *bytes = (n % 8 > 0) ? n / 8 + 1 : n / 8; + *bits = n; + + return 1; +} + +int binstr_to_sig(const char *binstr) { + _binstr_to_sig(binstr, signature, &n_signature, &nbit_signature); } +int binstr_to_sig1(const char *binstr) { + _binstr_to_sig(binstr, signature1, &n_signature1, &nbit_signature1); +} + +int binstr_to_sig2(const char *binstr) { + _binstr_to_sig(binstr, signature2, &n_signature2, &nbit_signature2); +} + +int _sig_to_binstr(char *binstr, char *sig, int bits) { + int i; + + for (i = 0; i < bits; i++) + binstr[i] = _get_signature_bit(sig, NSIGNATURE, i) ? '1' : '0'; + + binstr[bits] = '\0'; + + return 1; +} + +int sig_to_binstr(char *binstr) { + return _sig_to_binstr(binstr, signature, nbit_signature); +} + +int sig1_to_binstr(char *binstr) { + return _sig_to_binstr(binstr, signature1, nbit_signature1); +} + +int sig2_to_binstr(char *binstr) { + return _sig_to_binstr(binstr, signature2, nbit_signature2); +}