summaryrefslogtreecommitdiff
path: root/support/dktools/dk4c32.ctr
blob: 162d4c20c3903aa58390bc889415aeef35291456 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
SPDX-License-Identifier:	BSD-3-Clause



%%	header

/**	@file
	32 bit character decoding.

	CRT on Windows: Not used.
*/

#ifndef DK4CONF_H_INCLUDED
#if	DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif

#ifndef DK4TYPES_H_INCLUDED
#if	DK4_BUILDING_DKTOOLS4
#include "dk4types.h"
#else
#include <dktools-4/dk4types.h>
#endif
#endif

/**	Decoder structure to build a 32 bit character from 4 4 4 4 bytes.
*/
typedef struct {
  dk4_c32_t	val;	/**< Value constructed from the bytes. */
  int		msb;	/**< Flag: First byte is MSB. */
  int		nb;	/**< Number of next byte. */
} dk4_c32_byte_decoder_t;

#ifdef __cplusplus
extern "C" {
#endif

/**	Initialize the decoder.
	@param	ptr	Decoder to initialize.
	@param	msb	Flag: Most significant byte first.
*/
void
dk4c32_decoder_init(dk4_c32_byte_decoder_t *ptr, int msb);

/**	Add one byte to the decoder.
	@param	ptr
	@param	inbyte
	@return	DK4_EDSTM_ACCEPT if the byte was added to the internal
	decoder state (no action necessary) or
	DK4_EDSTM_FINISHED if one output was finished (use
	dk4c32_decoder_get() to obtain the value).
*/
int
dk4c32_decoder_add(dk4_c32_byte_decoder_t *ptr, unsigned char inbyte);

/**	Retrieve output value.
	@param	ptr	Decoder to retrieve value from.
	@return	Output produced by the decoder.
*/
dk4_c32_t
dk4c32_decoder_get(dk4_c32_byte_decoder_t const *ptr);

/**	Check whether the decoder is empty (nothing is stored in the
	internal state).
	@param	ptr	Decoder to check.
	@return	1 on success (decoder empty), 0 otherwise.
*/
int
dk4c32_decoder_is_empty(dk4_c32_byte_decoder_t const *ptr);

#ifdef __cplusplus
}
#endif



%%	module

#include "dk4conf.h"
#include "dk4types.h"
#include "dk4c32.h"

#ifndef DK4EDSTM_H_INCLUDED
#include "dk4edstm.h"
#endif

#if DK4_HAVE_STDLIB_H
#ifndef STDLIB_H_INCLUDED
#include <stdlib.h>
#define	STDLIB_H_INCLUDED 1
#endif
#else
#ifndef STDIO_H_INCLUDED
#include <stdio.h>
#define	STDIO_H_INCLUDED 1
#endif
#endif

#if	DK4_HAVE_ASSERT_H
#ifndef	ASSERT_H_INCLUDED
#include <assert.h>
#define	ASSERT_H_INCLUDED 1
#endif
#endif



void
dk4c32_decoder_init(dk4_c32_byte_decoder_t *ptr, int msb)
{
#if	DK4_USE_ASSERT
  assert(NULL != ptr);
#endif
  if (NULL != ptr) {
    ptr->val = dkC32(0);
    ptr->msb = msb;
    ptr->nb  = 0;
  }
}



int
dk4c32_decoder_add(dk4_c32_byte_decoder_t *ptr, unsigned char inbyte)
{
  int		back	=	DK4_EDSTM_ERROR;
#if	DK4_USE_ASSERT
  assert(NULL != ptr);
#endif
  if (NULL != ptr) {
    switch (ptr->nb) {
      case 3: {
        if (ptr->msb) {
	  ptr->val |= (dkC32(0x000000FF) & ((dk4_c32_t)inbyte));
	} else {
	  ptr->val |= (dkC32(0xFF000000) & (((dk4_c32_t)inbyte) << 24));
	}
	ptr->nb = 0;
	back = DK4_EDSTM_FINISHED;
      } break;
      case 2: {
        if (ptr->msb) {
	  ptr->val |= (dkC32(0x0000FF00) & (((dk4_c32_t)inbyte) <<  8));
	} else {
	  ptr->val |= (dkC32(0x00FF0000) & (((dk4_c32_t)inbyte) << 16));
	}
	ptr->nb = 3;
	back = DK4_EDSTM_ACCEPT;
      } break;
      case 1: {
        if (ptr->msb) {
	  ptr->val |= (dkC32(0x00FF0000) & (((dk4_c32_t)inbyte) << 16));
	} else {
	  ptr->val |= (dkC32(0x0000FF00) & (((dk4_c32_t)inbyte) <<  8));
	}
	ptr->nb = 2;
	back = DK4_EDSTM_ACCEPT;
      } break;
      default: {
        if (ptr->msb) {
	  ptr->val = (dkC32(0xFF000000) & (((dk4_c32_t)inbyte) << 24));
	} else {
	  ptr->val = (dkC32(0x000000FF) & ((dk4_c32_t)inbyte));
	}
	ptr->nb = 1;
	back = DK4_EDSTM_ACCEPT;
      } break;
    }
  }
  return back;
}



dk4_c32_t
dk4c32_decoder_get(dk4_c32_byte_decoder_t const *ptr)
{
  dk4_c32_t	back	=	dkC32(0);
#if	DK4_USE_ASSERT
  assert(NULL != ptr);
#endif
  if (NULL != ptr) {
    back = ptr->val;
  }
  return  back;
}



int
dk4c32_decoder_is_empty(dk4_c32_byte_decoder_t const *ptr)
{
  int	back	=	0;
#if	DK4_USE_ASSERT
  assert(NULL != ptr);
#endif
  if (NULL != ptr) {
    if (0 == ptr->nb) {
      back = 1;
    }
  }
  return back;
}