| 1 |
/* $Id$ */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2008 iFAX Solutions, Inc. |
| 4 |
* HylaFAX is a trademark of Silicon Graphics |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 7 |
* its documentation for any purpose is hereby granted without fee, provided |
| 8 |
* that (i) the above copyright notices and this permission notice appear in |
| 9 |
* all copies of the software and related documentation, and (ii) the names of |
| 10 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 11 |
* publicity relating to the software without the specific, prior written |
| 12 |
* permission of Sam Leffler and Silicon Graphics. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 15 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 16 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 17 |
* |
| 18 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 19 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 20 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 21 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 22 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 23 |
* OF THIS SOFTWARE. |
| 24 |
*/ |
| 25 |
#include <stdlib.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include <errno.h> |
| 28 |
#include "Range.h" |
| 29 |
#include "StackBuffer.h" |
| 30 |
#include "SystemLog.h" |
| 31 |
|
| 32 |
#ifdef DEBUGGING |
| 33 |
#define TRACE(fmt, args...) logDebug(fmt, ##args) |
| 34 |
#else |
| 35 |
#define TRACE(...) do {} while (0) |
| 36 |
#endif |
| 37 |
|
| 38 |
Range::Range (unsigned int start, unsigned int end) |
| 39 |
: min(start) |
| 40 |
, max(end) |
| 41 |
, set(false) |
| 42 |
{ |
| 43 |
unsigned int size = end - start; |
| 44 |
|
| 45 |
TRACE("Making map for %lu of %u bytes\n", size, (size / 8 )); |
| 46 |
|
| 47 |
map = (unsigned char*)malloc((size+7) / 8); |
| 48 |
TRACE("malloc returned a map of %p", map); |
| 49 |
|
| 50 |
fxAssert(map != NULL, "Couldn't malloc range map"); |
| 51 |
} |
| 52 |
|
| 53 |
bool Range::parse (const char* parse_string) |
| 54 |
{ |
| 55 |
unsigned int size = max - min; |
| 56 |
|
| 57 |
TRACE("Zeroing map"); |
| 58 |
memset(map, 0, size/8); |
| 59 |
TRACE("Zeroing done"); |
| 60 |
|
| 61 |
char* endptr; |
| 62 |
u_long val; |
| 63 |
u_int last = 0; |
| 64 |
|
| 65 |
char op = ','; |
| 66 |
|
| 67 |
do { |
| 68 |
errno = 0; |
| 69 |
val = strtoul(parse_string, &endptr, 10); |
| 70 |
TRACE("---> start(%p): \"%s\" end(%p): \"%s\" val: %ld errno: %d\n", |
| 71 |
parse_string, parse_string, endptr, endptr, val, errno); |
| 72 |
if (errno) |
| 73 |
break; |
| 74 |
|
| 75 |
if (val > max) |
| 76 |
val = max; |
| 77 |
if (val < min) |
| 78 |
val = min; |
| 79 |
|
| 80 |
switch (op) { |
| 81 |
case ',': |
| 82 |
setMapBit(val - min); |
| 83 |
break; |
| 84 |
case '-': |
| 85 |
for (u_int i = last; i <= val; i++) |
| 86 |
setMapBit(i-min); |
| 87 |
break; |
| 88 |
default: |
| 89 |
TRACE("Don't understand op: %c[%X]\n", op, op); |
| 90 |
} |
| 91 |
|
| 92 |
while (isspace(*endptr)) |
| 93 |
endptr++; |
| 94 |
op = *endptr++; |
| 95 |
while (isspace(*endptr)) |
| 96 |
endptr++; |
| 97 |
|
| 98 |
|
| 99 |
parse_string = endptr; |
| 100 |
last = val; |
| 101 |
} while (op); |
| 102 |
set = true; |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
Range::~Range (void) |
| 107 |
{ |
| 108 |
if (map) |
| 109 |
free(map); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
void Range::setMapBit(unsigned int b) |
| 114 |
{ |
| 115 |
unsigned bb = b / 8; |
| 116 |
unsigned bc = (b % 8) + 1; |
| 117 |
|
| 118 |
TRACE(" - %2d: Flipping bit %d of index %d\n", |
| 119 |
b, bc, bb); |
| 120 |
|
| 121 |
map[bb] |= (1<<(bc-1)); |
| 122 |
} |
| 123 |
|
| 124 |
bool Range::getMapBit(unsigned int b) |
| 125 |
{ |
| 126 |
if (b > max-min) |
| 127 |
return false; |
| 128 |
unsigned bb = b / 8; |
| 129 |
unsigned bc = (b % 8) + 1; |
| 130 |
|
| 131 |
#ifdef DEBUGGING |
| 132 |
fxStackBuffer buf; |
| 133 |
buf.fput("MAP: "); |
| 134 |
for (int i = 0; i < ((max-min)+7)/8; i++) |
| 135 |
buf.fput(" %02X ", map[i] & 0xFF); |
| 136 |
TRACE("%s", (const char*)buf); |
| 137 |
#endif |
| 138 |
|
| 139 |
TRACE(" - %2d: Checking bit %d of index %d: %X\n", |
| 140 |
b, bc, bb, map[bb]); |
| 141 |
|
| 142 |
|
| 143 |
return (map[bb] & (1<<(bc-1))); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
#define PRINT_OR_SYSLOG(fd, buf) \ |
| 148 |
if (fd) \ |
| 149 |
{ \ |
| 150 |
fwrite((const char*)buf, buf.getLength(), 1, fd); \ |
| 151 |
fputc('\n', fd); \ |
| 152 |
} else logDebug("%s", (const char*)buf); |
| 153 |
|
| 154 |
void Range::dump (FILE* fd) |
| 155 |
{ |
| 156 |
unsigned int size = max - min; |
| 157 |
fxStackBuffer buf; |
| 158 |
|
| 159 |
buf.fput("RANGE: %u - %u (%d bits)", min, max, size); |
| 160 |
PRINT_OR_SYSLOG(fd, buf); |
| 161 |
|
| 162 |
buf.reset(); |
| 163 |
buf.fput("MAP: "); |
| 164 |
for (u_int i = 0; i < ((size+9)/10); i++) |
| 165 |
buf.fput("/ %3d \\", (i+1)*10); |
| 166 |
PRINT_OR_SYSLOG(fd, buf); |
| 167 |
|
| 168 |
buf.reset(); |
| 169 |
buf.fput("MAP: "); |
| 170 |
for (u_int i = 0; i < size; i++) |
| 171 |
{ |
| 172 |
int b = i/8; |
| 173 |
int c = i%8; |
| 174 |
buf.fput("%c", (map[b] & (1<<c)) ? '1' : '0'); |
| 175 |
} |
| 176 |
PRINT_OR_SYSLOG(fd, buf); |
| 177 |
|
| 178 |
buf.reset(); |
| 179 |
buf.fput("MAP: "); |
| 180 |
for (u_int i = 0; i < ((size+7)/8); i++) |
| 181 |
{ |
| 182 |
buf.fput("\\ %2.2X /", i); |
| 183 |
} |
| 184 |
PRINT_OR_SYSLOG(fd, buf); |
| 185 |
} |
| 186 |
|
| 187 |
bool Range::contains (unsigned int v) |
| 188 |
{ |
| 189 |
if (! set) |
| 190 |
return true; |
| 191 |
return getMapBit(v-min); |
| 192 |
} |