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 /**	@file dk4bitsh.c The dk4bitsh module.
15 */
16 
17 
18 #include "dk4conf.h"
19 
20 #if	DK4_HAVE_ASSERT_H
21 #ifndef	ASSERT_H_INCLUDED
22 #include <assert.h>
23 #define	ASSERT_H_INCLUDED 1
24 #endif
25 #endif
26 
27 #include <libdk4c/dk4bitsh.h>
28 #include <libdk4c/dk4edstm.h>
29 #include <libdk4base/dk4mem.h>
30 
31 
32 
33 
34 
35 
36 
37 
38 void
39 
dk4bit_shift_init(dk4_bit_shift_t * bs,dk4_er_t * erp)40 dk4bit_shift_init(dk4_bit_shift_t *bs, dk4_er_t *erp)
41 {
42 
43 #if	DK4_USE_ASSERT
44   assert(NULL != bs);
45 #endif
46   if (NULL != bs) {
47     DK4_MEMRES(bs, sizeof(dk4_bit_shift_t));
48     bs->pos = 0;
49     bs->obyte = 0x00;
50   } else {
51     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
52   }
53 
54 }
55 
56 
57 
58 int
59 
dk4bit_shift_add(dk4_bit_shift_t * bs,int bit,dk4_er_t * erp)60 dk4bit_shift_add(dk4_bit_shift_t *bs, int bit, dk4_er_t *erp)
61 {
62   int		 back	=	DK4_EDSTM_ERROR;
63 
64 #if	DK4_USE_ASSERT
65   assert(NULL != bs);
66 #endif
67   if (NULL != bs) {
68     back = DK4_EDSTM_ACCEPT;
69     switch (bs->pos) {
70       case 0: {
71         if (0 != bit) { bs->obyte = 0x80; } else { bs->obyte = 0x00; }
72 	bs->pos = 1;
73       } break;
74       case 1: {
75         if (0 != bit) { bs->obyte |= 0x40; }
76 	bs->pos = 2;
77       } break;
78       case 2: {
79         if (0 != bit) { bs->obyte |= 0x20; }
80 	bs->pos = 3;
81       } break;
82       case 3: {
83         if (0 != bit) { bs->obyte |= 0x10; }
84 	bs->pos = 4;
85       } break;
86       case 4: {
87         if (0 != bit) { bs->obyte |= 0x08; }
88 	bs->pos = 5;
89       } break;
90       case 5: {
91         if (0 != bit) { bs->obyte |= 0x04; }
92 	bs->pos = 6;
93       } break;
94       case 6: {
95         if (0 != bit) { bs->obyte |= 0x02; }
96 	bs->pos = 7;
97       } break;
98       case 7: {
99         if (0 != bit) { bs->obyte |= 0x01; }
100 	bs->pos = 0;
101 	back = DK4_EDSTM_FINISHED;
102       } break;
103     }
104   } else {
105     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
106   }
107 
108   return back;
109 }
110 
111 
112 
113 int
114 
dk4bit_shift_finish(dk4_bit_shift_t * bs,dk4_er_t * erp)115 dk4bit_shift_finish(dk4_bit_shift_t *bs, dk4_er_t *erp)
116 {
117   int		 back	=	DK4_EDSTM_ERROR;
118 
119 #if	DK4_USE_ASSERT
120   assert(NULL != bs);
121 #endif
122   if (NULL != bs) {
123     if (0 != bs->pos) {
124       back = DK4_EDSTM_FINISHED;
125     } else {
126       back = DK4_EDSTM_ACCEPT;
127     }
128   } else {
129     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
130   }
131 
132   return back;
133 }
134 
135 
136 
137 unsigned char
138 
dk4bit_shift_output(dk4_bit_shift_t const * bs,dk4_er_t * erp)139 dk4bit_shift_output(dk4_bit_shift_t const *bs, dk4_er_t *erp)
140 {
141   unsigned char	 back;
142 
143 #if	DK4_USE_ASSERT
144   assert(NULL != bs);
145 #endif
146   if (NULL != bs) {
147     back = bs->obyte;
148   } else {
149     back = 0x00;
150     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
151   }
152 
153   return back;
154 }
155 
156