1 /*
2 Copyright (C) 2015-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: dk4bitsh.ctr
12 */
13 
14 #ifndef DK4BITSH_H_INCLUDED
15 /** Avoid multiple inclusions. */
16 #define DK4BITSH_H_INCLUDED 1
17 
18 
19 /**	@file	dk4bitsh.h	Bit shifting.
20 */
21 
22 #ifndef	DK4CONF_H_INCLUDED
23 #if	DK4_BUILDING_DKTOOLS4
24 #include "dk4conf.h"
25 #else
26 #include <dktools-4/dk4conf.h>
27 #endif
28 #endif
29 
30 #ifndef	DK4ERROR_H_INCLUDED
31 #if	DK4_BUILDING_DKTOOLS4
32 #include <libdk4base/dk4error.h>
33 #else
34 #include <dktools-4/dk4error.h>
35 #endif
36 #endif
37 
38 
39 /**	Bit shifter.
40 */
41 typedef struct {
42   int			pos;	/**< Current bit position. */
43   unsigned char		obyte;	/**< Output byte. */
44 } dk4_bit_shift_t;
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /**	Initialize a bit shifter before using it.
51 	@param	bs	Bit shifter to initialize.
52 	@param	erp	Error report, may be NULL.
53 
54 	Error codes:
55 	- DK4_E_INVALID_ARGUMENTS	if bs is NULL.
56 */
57 void
58 
59 dk4bit_shift_init(dk4_bit_shift_t *bs, dk4_er_t *erp);
60 
61 /**	Add one byte.
62 	@param	bs	Bit shifter.
63 	@param	bit	Bit value, 0 or non-0.
64 	@param	erp	Error report, may be NULL.
65 	@return	Action to take, one from:
66 	- DK4_EDSTM_ACCEPT<br>
67 	  if the bit was stored in the bit shifter. No action necessary.
68 	- DK4_EDSTM_FINISHED<br>
69 	  if one byte was completed by adding the bit.
70 	  Use dk4bit_shift_output() to retrieve the finished byte.
71 	- DK4_EDSTM_ERROR<br>
72 	  if an error occured.
73 
74 	Error codes:
75 	- DK4_E_INVALID_ARGUMENTS<br>
76 	  if bs is NULL.
77 */
78 int
79 
80 dk4bit_shift_add(dk4_bit_shift_t *bs, int bit, dk4_er_t *erp);
81 
82 /**	Check for unused content in the bit shifter after adding all bits.
83 	@param	bs	Bit shifter.
84 	@param	erp	Error report, may be NULL.
85 	@return	Action to take, one from:
86 	- DK4_EDSTM_ACCEPT<br>
87 	  if no action is required.
88 	- DK4_EDSTM_FINISHED<br>
89 	  if significant bits are available for a byte.
90 	  Use dk4bit_shift_output() to retrieve the byte value.
91 	- DK4_EDSTM_ERROR<br>
92 	  if an error occured.
93 
94 	Error codes:
95 	- DK4_E_INVALID_ARGUMENTS<br>
96 	  if bs is NULL.
97 */
98 int
99 
100 dk4bit_shift_finish(dk4_bit_shift_t *bs, dk4_er_t *erp);
101 
102 /**	Retrieve previous completed byte or final incomplete byte.
103 	@param	bs	Bit shifter.
104 	@param	erp	Error report, may be NULL.
105 	@return	Byte value consisting of the last bits added.
106 */
107 unsigned char
108 
109 dk4bit_shift_output(dk4_bit_shift_t const *bs, dk4_er_t *erp);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 
116 #endif
117