comparison Meerwald-dir/signature-utils.c @ 24:9f20bce6184e v0.7

move directories, support netpbm 11
author Peter Meerwald-Stadler <pmeerw@pmeerw.net>
date Fri, 20 Dec 2024 13:08:59 +0100
parents Meerwald/signature-utils.c@bd669312f068
children
comparison
equal deleted inserted replaced
23:71dd4b96221b 24:9f20bce6184e
1 #include <string.h>
2 #include <strings.h>
3 #include "signature-utils.h"
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
17 int _get_signature_bit(char *s, int lim, int n) {
18 int byte = n >> 3;
19 int bit = n & 7;
20
21 #ifdef DEBUG
22 if (byte < 0 || byte >= lim)
23 fprintf(stderr, "get_signature_bit?(): index out of range\n");
24 #endif
25
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);
35 }
36
37 int get_signature2_bit(int n) {
38 return _get_signature_bit(signature2, NSIGNATURE, n);
39 }
40
41 void _set_signature_bit(char *s, int limit, int n, int v) {
42 int byte = n >> 3;
43 int bit = n & 7;
44
45 #ifdef DEBUG
46 if (byte < 0 || byte >= limit / 8)
47 fprintf(stderr, "get_signature_bit?(): index out of range\n");
48 #endif
49
50 if (v)
51 s[byte] |= (1 << bit);
52 else
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);
62 }
63
64 void set_signature2_bit(int n, int v) {
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;
71
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 }
80
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) {
88 return _binstr_to_sig(binstr, signature, &n_signature, &nbit_signature);
89 }
90
91 int binstr_to_sig1(const char *binstr) {
92 return _binstr_to_sig(binstr, signature1, &n_signature1, &nbit_signature1);
93 }
94
95 int binstr_to_sig2(const char *binstr) {
96 return _binstr_to_sig(binstr, signature2, &n_signature2, &nbit_signature2);
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 }

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