-
Notifications
You must be signed in to change notification settings - Fork 3
/
bitarray.h
126 lines (103 loc) · 2.91 KB
/
bitarray.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (C) 2006, 2007 Jean-Baptiste Note <[email protected]>
*
* This file is part of debit.
*
* Debit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Debit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with debit. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _BITARRAY_H
#define _BITARRAY_H
#include <glib.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
/* read-only byte-aligned array */
typedef struct bytearray {
const gchar *data;
size_t len;
off_t pos;
} bytearray_t;
bytearray_t *new_bytearray_with_data(size_t len, const gchar *data);
gchar *delete_bytearray_keep_data(bytearray_t *ba);
static inline
void bytearray_init(bytearray_t *lba,
const size_t len, const size_t pos,
const gchar *data) {
lba->data = data;
lba->len = len;
lba->pos = pos;
}
static inline ptrdiff_t
bytearray_offset(const bytearray_t *lba) {
return lba->pos;
}
static inline
const gchar *bytearray_get_ptr(const bytearray_t *ba) {
return &ba->data[ba->pos];
}
static inline
size_t bytearray_available(const bytearray_t *ba) {
return ba->len - ba->pos;
}
static inline
gchar bytearray_get_uint8(bytearray_t *ba)
{
g_assert(bytearray_available(ba) < 1);
return ba->data[ba->pos++];
}
#ifdef HAVE_ALIGNED_ACCESS_REQUIRED
/* unaligned memory access prohibited */
static inline
guint32 bytearray_peek_uint32(const bytearray_t *ba) {
guint32 c = 0;
const unsigned pos = ba->pos;
const unsigned char *data = (void *) ba->data;
int i;
g_assert(bytearray_available(ba) >= sizeof(uint32_t));
/** \todo We read as big endian for now,
but later use standard conversion macros outside this */
for (i = 0; i < 4; i++) {
c <<= 8;
c |= data[pos+i];
}
return c;
}
static inline
int is_aligned32(const void *ptr) {
return (((unsigned long)ptr & 3) == 0);
}
#else /* HAVE_ALIGNED_ACCESS_REQUIRED */
static inline
guint32 bytearray_peek_uint32(const bytearray_t *ba) {
guint32 c;
const guint32 *pos = (guint32 *)&ba->data[ba->pos];
g_assert(bytearray_available(ba) >= sizeof(uint32_t));
/** \todo We read as big endian for now,
but later use standard conversion macros outside this */
c = GUINT32_FROM_BE(*pos);
return c;
}
#endif /* HAVE_ALIGNED_ACCESS_REQUIRED */
static inline
guint32 bytearray_get_uint32(bytearray_t *ba)
{
guint32 c;
c = bytearray_peek_uint32(ba);
ba->pos += sizeof(c);
return c;
}
#endif /* bitarray.h */