summaryrefslogtreecommitdiff
path: root/support/dktools/dk4bf.c
blob: 414679862155da8aa972852aebbfdca766f09e09 (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
/*
Copyright (C) 2015-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4bf.ctr
*/

/**	@file dk4bf.c The dk4bf module.
*/


#line 97 "dk4bf.ctr"

#include "dk4conf.h"

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

#include "dk4bf.h"
#include "dk4mem.h"





#line 113 "dk4bf.ctr"



/**	Position of single bits within a byte.
*/
static const unsigned char dk4bf_bits_in_byte[] = {
  0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};



dk4_bit_field_t *
dk4bf_open(size_t sz, dk4_er_t *erp)
{
  dk4_bit_field_t	*back	=	NULL;
  size_t		 bsz;
#if	DK4_USE_ASSERT
  assert(0 < sz);
#endif
  if (0 < sz) {
    back = dk4mem_new(dk4_bit_field_t,1,erp);
    if (NULL != back) {
      back->s = sz;
      bsz = sz / 8;
      if (0 != (sz % 8)) {
        bsz++;
      }
      back->d = dk4mem_new(unsigned char,bsz,erp);
      if (NULL == back->d) {
        dk4bf_close(back);
	back = NULL;
      }
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}



int
dk4bf_set(dk4_bit_field_t *dptr, size_t bitno, int val, dk4_er_t *erp)
{
  size_t	 bytei;			/* Byte index */
  size_t	 biti;			/* Bit index */
  int		 back	=	0;
#if	DK4_USE_ASSERT
  assert(NULL != dptr);
#endif
  if (NULL != dptr) {
    if (bitno < dptr->s) {
      bytei = bitno / 8;
      biti  = bitno % 8;
      if (0 != val) {
        (dptr->d)[bytei] |= dk4bf_bits_in_byte[biti];
      } else {
        (dptr->d)[bytei] &= (~(dk4bf_bits_in_byte[biti]));
      }
      back = 1;
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}



int
dk4bf_get(int *dptr, dk4_bit_field_t const *bfptr, size_t bitno, dk4_er_t *erp)
{
  size_t	 bytei;			/* Byte index */
  size_t	 biti;			/* Bit index within byte */
  int		 back	=	0;
#if	DK4_USE_ASSERT
  assert(NULL != dptr);
  assert(NULL != bfptr);
#endif
  if ((NULL != dptr) && (NULL != bfptr)) {
    if (bitno < bfptr->s) {
      bytei = bitno / 8;
      biti  = bitno % 8;
      if (0x00 != (((bfptr->d)[bytei]) & (dk4bf_bits_in_byte[biti]))) {
        *dptr = 1;
      } else {
        *dptr = 0;
      }
      back = 1;
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}



void
dk4bf_close(dk4_bit_field_t *ptr)
{
#if	DK4_USE_ASSERT
  assert(NULL != ptr);
#endif
  if (NULL != ptr) {
    dk4mem_release(ptr->d);
    dk4mem_free(ptr);
  }
}