File: byteset.c

package info (click to toggle)
shush 1.2.3-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 884 kB
  • ctags: 559
  • sloc: ansic: 5,502; sh: 153; lex: 69; makefile: 68; yacc: 54; awk: 31
file content (101 lines) | stat: -rw-r--r-- 1,556 bytes parent folder | download | duplicates (3)
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
/*
** Copyright (C) 2003 Christophe Kalt
**
** This file is part of shush,
** see the LICENSE file for details on your rights.
*/

#include "os.h"

#include <ctype.h>

#include "byteset.h"

static char const rcsid[] = "@(#)$Id: byteset.c 1404 2008-03-08 23:25:46Z kalt $";

static long set[256];
static int once = 1;

/*
** byteset_init
**	Initialize set[] from a range definition string
*/
void
byteset_init(char *definition, int value)
{
    char *str, *tok, *dash;
    int i, j;

    assert( definition != NULL );

    if (once == 1)
      {
	once = 0;
	i = 0;
	while (i < 256)
	    set[i++] = 0;
      }

    str = strdup(definition);
    tok = strtok(str, ",");
    while (tok != NULL)
      {
	if (tok[0] == '-')
	    i = 0;
	else if (isdigit((int) tok[0]) != 0)
	    i = atoi(tok);
	else
	    i = -1;

	j = i;
	dash = index(tok, '-');
	if (dash != NULL)
	  {
	    if (*(dash + 1) != '\0')
	      {
		if (isdigit((int) *(dash + 1)) != 0)
		    j = atoi(dash + 1);
		else
		    j = -1;
	      }
	    else
		j = 255;
	  }

	if (i < 0 || i > 255 || j < 0 || j > 255 || j < i)
	  {
	    error("Invalid range: %s", tok);
	    exit(1);
	  }

	while (i <= j)
	    set[i++] = value;

	tok = strtok(NULL, ",");
      }
    free(str);
}

/*
** byteset_set
**	Sets the value of a byte in the set
*/
void
byteset_set(int byte, int value)
{
    assert( byte >= 0 && byte <= 255 );

    set[byte] = value;
}

/*
** byteset_get
**	Return the value of a byte in the set
*/
int
byteset_get(int byte)
{
    assert( byte >= 0 && byte <= 255 );

    return set[byte];
}