ProtoCore
v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
bitio.h
Go to the documentation of this file.
1
// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2
// SPDX-License-Identifier: AGPL-3.0-or-later
3
4
/**
5
* @file bitio.h
6
* @brief LSB-first bit writer over a caller-owned byte buffer - one source of truth.
7
*
8
* The DEFLATE encoder (network_drivers/presentation/codec/deflate) and the SSH zlib@openssh.com stream compressor
9
* (ssh/transport/ssh_zlib) each carried a byte-for-byte copy of the same bit writer: pack bits LSB-first into a
10
* @c uint32_t accumulator, spill whole bytes to the output, and latch @c overflow when the buffer is full. It
11
* lives here once.
12
*
13
* NOTE: distinct from bytes.h's @c pc_bw_* helpers, which are a BYTE-oriented (big-endian) codec cursor. This
14
* is a BIT writer (@c pc_bitw_*), for the DEFLATE bitstream. Header-only, pure (only @c <stdint.h> /
15
* @c <stddef.h>), zero link cost when unused.
16
*
17
* @author Douglas Quigg (dstroy0)
18
* @date 2026
19
*/
20
21
#ifndef PROTOCORE_BITIO_H
22
#define PROTOCORE_BITIO_H
23
24
#include <stddef.h>
25
#include <stdint.h>
26
27
/** @brief LSB-first bit writer over the caller's output buffer; @c overflow latches once @c cap is exceeded. */
28
struct
pc_bit_writer
29
{
30
uint8_t *
out
;
31
size_t
cap
;
32
size_t
cnt
;
///< bytes written so far
33
uint32_t
acc
;
///< bit accumulator (LSB-first)
34
int
nbits
;
///< bits currently buffered (< 8 between calls)
35
bool
overflow
;
36
};
37
38
/** @brief Append the low @p n bits of @p bits, LSB-first, spilling any completed bytes to the output. */
39
inline
void
pc_bitw_put
(
pc_bit_writer
*w, uint32_t bits,
int
n)
40
{
41
w->
acc
|= bits << w->
nbits
;
42
w->
nbits
+= n;
43
while
(w->
nbits
>= 8)
44
{
45
if
(w->
cnt
>= w->
cap
)
46
{
47
w->
overflow
=
true
;
48
return
;
49
}
50
w->
out
[w->
cnt
++] = (uint8_t)(w->
acc
& 0xFF);
51
w->
acc
>>= 8;
52
w->
nbits
-= 8;
53
}
54
}
55
56
/** @brief Flush any partial byte, padding the high bits with zero (byte alignment). */
57
inline
void
pc_bitw_align
(
pc_bit_writer
*w)
58
{
59
if
(w->
nbits
> 0)
60
{
61
if
(w->
cnt
>= w->
cap
)
62
{
63
w->
overflow
=
true
;
64
return
;
65
}
66
w->
out
[w->
cnt
++] = (uint8_t)(w->
acc
& 0xFF);
67
w->
acc
= 0;
68
w->
nbits
= 0;
69
}
70
}
71
72
#endif
// PROTOCORE_BITIO_H
pc_bitw_align
void pc_bitw_align(pc_bit_writer *w)
Flush any partial byte, padding the high bits with zero (byte alignment).
Definition
bitio.h:57
pc_bitw_put
void pc_bitw_put(pc_bit_writer *w, uint32_t bits, int n)
Append the low n bits of bits, LSB-first, spilling any completed bytes to the output.
Definition
bitio.h:39
pc_bit_writer
LSB-first bit writer over the caller's output buffer; overflow latches once cap is exceeded.
Definition
bitio.h:29
pc_bit_writer::nbits
int nbits
bits currently buffered (< 8 between calls)
Definition
bitio.h:34
pc_bit_writer::cnt
size_t cnt
bytes written so far
Definition
bitio.h:32
pc_bit_writer::acc
uint32_t acc
bit accumulator (LSB-first)
Definition
bitio.h:33
pc_bit_writer::cap
size_t cap
Definition
bitio.h:31
pc_bit_writer::overflow
bool overflow
Definition
bitio.h:35
pc_bit_writer::out
uint8_t * out
Definition
bitio.h:30
src
shared_primitives
bitio.h
Generated by
1.9.8