workaround a stupid issue in how decoding is performed in mutt *sigh*
[apps/madmutt.git] / lib-lib / bits.h
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  */
17 /*
18  * Copyright © 2007 Pierre Habouzit
19  */
20
21 #ifndef MUTT_LIB_LIB_BITS_H
22 #define MUTT_LIB_LIB_BITS_H
23
24 typedef struct bits_t {
25     int start;
26     int size;
27     unsigned char *bits;
28 } bits_t;
29
30 void bits_extend(bits_t *bits, int pos);
31
32 DO_INIT(bits_t, bits);
33 static inline void bits_wipe(bits_t *bits) {
34     p_delete(&bits->bits);
35 }
36 DO_NEW(bits_t, bits);
37 DO_DELETE(bits_t, bits);
38
39 static inline void bits_clear(bits_t *bits)
40 {
41     bits_wipe(bits);
42     bits_init(bits);
43 }
44
45 static inline bool bit_isset(const bits_t *bits, int pos)
46 {
47     int offs = (pos >> 3) - bits->start;
48     if (offs < 0 || offs > bits->size)
49         return false;
50
51     return (bits->bits[offs] & (1 << (pos & 7))) != 0;
52 }
53
54 static inline void bit_set(bits_t *bits, int pos)
55 {
56     int offs = (pos >> 3) - bits->start;
57     if (offs < 0 || offs > bits->size) {
58         bits_extend(bits, pos);
59         offs = (pos >> 3) - bits->start;
60     }
61     bits->bits[offs] |= 1 << (pos & 7);
62 }
63
64 static inline void bit_clear(bits_t *bits, int pos)
65 {
66     int offs = (pos >> 3) - bits->start;
67     if (offs < 0 || offs > bits->size)
68         return;
69     bits->bits[offs] &= ~(1 << (pos & 7));
70 }
71
72 static inline void bit_toggle(bits_t *bits, int pos)
73 {
74     int offs = (pos >> 3) - bits->start;
75     if (offs < 0 || offs > bits->size) {
76         bits_extend(bits, pos);
77         offs = (pos >> 3) - bits->start;
78     }
79     bits->bits[offs] ^= 1 << (pos & 7);
80 }
81
82
83 #endif /* MUTT_LIB_LIB_BITS_H */