## modules/ma/bitmask.c

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. MA_free
2. MA_isset
3. MA_set
4. MA_clear
5. MA_new
6. MA_prt
7. MA_and
8. MA_xor
9. MA_or
10. MA_bitcount
11. MA_to_string
12. main

    /***************************************
      $Revision: 1.7 $

      bitmask (ma) - bitmask.c - library for manipulating fixed size bitmasks.

      Status: NOT REVUED, TESTED, INCOMPLETE

      Design and implementation by: Marek Bukowy, Chris Ottrey.

      ******************/ /******************
      Copyright (c) 1999                              RIPE NCC

      All Rights Reserved

      Permission to use, copy, modify, and distribute this software and its
      documentation for any purpose and without fee is hereby granted,
      provided that the above copyright notice appear in all copies and that
      both that copyright notice and this permission notice appear in
      supporting documentation, and that the name of the author not be
      used in advertising or publicity pertaining to distribution of the
      software without specific, written prior permission.

      THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      ***************************************/

    #include <stdio.h>
    #include <stdarg.h>
    #include <stdlib.h>
    #include <assert.h>

    #include <string.h>

    #include "bitmask.h"

    #include "globaldefs.h"

    void MA_free(mask_t *m) {
    /* [<][>][^][v][top][bottom][index][help] */
      wr_free(m);
    } /* MA_free() */

    int MA_isset(mask_t d, int b) {
    /* [<][>][^][v][top][bottom][index][help] */
      return(
     (d.data[b / MASK_BITS_BASESIZE] &(1 <<(b % MASK_BITS_BASESIZE)))
       > 0);
    }

    void MA_set(mask_t *m_ptr, unsigned b, int v) {
    /* [<][>][^][v][top][bottom][index][help] */
      assert(b < MASK_MAX);
      assert(v == 0 || v == 1);

      if(v == 1)
        m_ptr->data[b / MASK_BITS_BASESIZE] |=(1 <<(b % MASK_BITS_BASESIZE));
      else
        m_ptr->data[b / MASK_BITS_BASESIZE] &= ~(1 <<(b % MASK_BITS_BASESIZE));
    }

    void MA_clear(mask_t *m_ptr) {
    /* [<][>][^][v][top][bottom][index][help] */
      memset(m_ptr, 0, sizeof(mask_t));
    } /* MA_clear() */

    /* set bits in dptr. variable number of args, terminated by MA_END */
    mask_t MA_new(int n,...) {
    /* [<][>][^][v][top][bottom][index][help] */
      va_list         ap;
      mask_t d;

      MA_clear(&d);

      /* cover the "empty mask" case => MA_new(MA_END) invoked */
      for ( va_start(ap, n); n != MA_END; n = va_arg(ap, int) ) {
          MA_set(&d, n, 1);
      }
      va_end(ap);

      return d;
    }

    void MA_prt(mask_t m) {
    /* [<][>][^][v][top][bottom][index][help] */
      int  i;

      for (i = 0; i < MASK_MAX; i++) {
        printf("%d", MA_isset(m, i));
      }

      printf("\n");

      for (i = 0; i < MASK_BITS_WORDS; i++) {
        printf("0x%0*X ", 2 * sizeof(MASK_BITS_BASETYPE), m.data[i]);
      }

      printf("\n");
    }

    /* Perform a logical AND on two masks.
       Author: ottrey
       Date: Tue Jul  6 13:28:24 CEST 1999
       NB: This operation could/should be done a word at a time?
    */
    mask_t MA_and(mask_t a, mask_t b) {
    /* [<][>][^][v][top][bottom][index][help] */
      mask_t c;
      int i;

      for (i=0; i < MASK_BITS_WORDS; i++ )  {
        c.data[i] = a.data[i] & b.data[i];
      }
      return c;
    } /* MA_and() */

    /* Perform a logical XOR on two masks.
       Author: ottrey
       Date: Thu Jul  8 14:50:14 CEST 1999
       NB: This operation could/should be done a word at a time?
    */
    mask_t MA_xor(mask_t a, mask_t b) {
    /* [<][>][^][v][top][bottom][index][help] */
      mask_t c;
      int i;

      for (i=0; i < MASK_BITS_WORDS; i++ )  {
        c.data[i] = a.data[i] ^ b.data[i];
      }

      return c;

    } /* MA_xor() */

    /* Perform a logical OR on two masks.
       Author: ottrey
       Date: Thu Jul  8 16:34:34 CEST 1999
       NB: This operation could/should be done a word at a time?
    */
    mask_t MA_or(mask_t a, mask_t b) {
    /* [<][>][^][v][top][bottom][index][help] */
      mask_t c;
      int i;

      for (i=0; i < MASK_BITS_WORDS; i++ )  {
        c.data[i] = a.data[i] | b.data[i];
      }

      return c;

    } /* MA_or() */

    /* Counts the number of bits set. */
    int MA_bitcount(mask_t m) {
    /* [<][>][^][v][top][bottom][index][help] */
      int  i;
      int count=0;

      for (i=0; i < MASK_MAX; i++) {
        count += MA_isset(m, i);
      }

      return count;

    } /* MA_bitcount() */

    /* Convert the bitmap to a string comprising of tokens from an array of tokens.
       Author: ottrey
       Date: Tue Jul  6 13:28:24 CEST 1999
    */
    char *MA_to_string(mask_t mask, char * const *tokens) {
    /* [<][>][^][v][top][bottom][index][help] */
      char *str;
      char str_buf[STR_L];
      int count;
      int i;
      int length;

      strcpy(str_buf, "");

      count=0;
      sprintf(str_buf, "{");
      for (i=0;  tokens[i] != NULL; i++) {
        if ( MA_isset(mask, i) != 0) {
          strcat(str_buf, tokens[i]);
          strcat(str_buf, ",");
          count++;
        }
      }
      if (count == 0) {
        strcat(str_buf, "NULL ");
      }
      length = strlen(str_buf);
      str_buf[length-1] = '}';

      str = (char *)calloc(1, strlen(str_buf)+1);
      strcpy(str, str_buf);

      return str;

    } /* MA_to_string() */

    #ifdef MODULE_TEST
    void
    main() {
    /* [<][>][^][v][top][bottom][index][help] */
      mask_t  d;

      d = MA_new(1, 4, 56, 3, 5, 7, 19, MA_END);

      MA_prt(d);

      MA_set(&d, 3, 0);
      MA_set(&d, 7, 0);
      MA_set(&d, 9, 0);
      MA_set(&d, 19, 0);

      MA_prt(d);
    }
    #endif

/* [<][>][^][v][top][bottom][index][help] */