/* WARNING: This file was generated by dkct. 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 */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file dk4bf.c The dk4bf module. */ #line 85 "dk4bf.ctr" #include "dk4bf.h" #include "dk4mem.h" #line 93 "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 (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 (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 *bfptr, size_t bitno, dk4_er_t *erp) { size_t bytei; /* Byte index */ size_t biti; /* Bit index within byte */ int back = 0; 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 (NULL != ptr) { dk4mem_release(ptr->d); dk4mem_free(ptr); } }