| 1 |
NAME |
| 2 |
Data::BitMask - bitmask manipulation |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Data::BitMask; |
| 6 |
|
| 7 |
my $FileMask = Data::BitMask->new( |
| 8 |
READ => 1, |
| 9 |
WRITE => 2, |
| 10 |
EXECUTE => 4, |
| 11 |
RX => 5, |
| 12 |
RWX => 7, |
| 13 |
FULL => 7, |
| 14 |
); |
| 15 |
|
| 16 |
my $mask = $FileMask->build_mask('READ|WRITE'); |
| 17 |
print Data::Dumper->Dump([ |
| 18 |
$FileMask->explain_mask($mask), |
| 19 |
$FileMask->break_mask($mask) |
| 20 |
]); |
| 21 |
|
| 22 |
my $mask2 = $FileMask->build_mask({FULL => 1, WRITE => 0}); |
| 23 |
|
| 24 |
DESCRIPTION |
| 25 |
This module allows one to create bitmask manipulator objects that can be |
| 26 |
used to create bitmask values based on a list of constants, as well as |
| 27 |
to break apart masks using those constants. The advantages are that you |
| 28 |
don't have to pollute namespaces to use constants, you can ensure that |
| 29 |
only appropriate constants are used for specific masks, you can easily |
| 30 |
break apart and explain masks, and in general it is much easier for the |
| 31 |
user to interact with masks. |
| 32 |
|
| 33 |
The module only interacts with masks that fit in Perl integers. In some |
| 34 |
places, it presumes that you are using 32 bit integers (i.e. |
| 35 |
canonicalizing negative values). |
| 36 |
|
| 37 |
The module expends a modest amount of overhead in creating the |
| 38 |
"Data::BitMask" object so as to speed up future mask manipulations. |
| 39 |
|
| 40 |
Calls to "build_mask", "break_mask", and "explain_mask" are memoized to |
| 41 |
speed up repeated calls with the same parameters. |
| 42 |
|
| 43 |
Installation instructions |
| 44 |
There are four options for installing this module: |
| 45 |
|
| 46 |
* Using "Module::Build 0.24" or later: |
| 47 |
|
| 48 |
Build.PL |
| 49 |
perl build test |
| 50 |
perl build install |
| 51 |
|
| 52 |
* Using "ExtUtils::MakeMaker" (part of the Perl core): |
| 53 |
|
| 54 |
Makefile.PL |
| 55 |
make test |
| 56 |
make install |
| 57 |
|
| 58 |
* Using the PPM (the file has the extension ".ppm.zip") on CPAN and |
| 59 |
installing under ActivePerl for Win32 by unzipping the ".ppm.zip" |
| 60 |
file and then: |
| 61 |
|
| 62 |
ppm install Data-BitMask.ppd |
| 63 |
|
| 64 |
* Installing manually by copying "lib/Data/Bitmask.pm" to |
| 65 |
"perl/site/lib/Data/Bitmask.pm". |
| 66 |
|
| 67 |
Suggested Module Implementation |
| 68 |
Here is one suggested approach to using bitmask manipulators in a |
| 69 |
module. |
| 70 |
|
| 71 |
{ |
| 72 |
my $cache; |
| 73 |
sub SECURITY_INFORMATION { |
| 74 |
$cache ||= Data::BitMask->new( |
| 75 |
OWNER_SECURITY_INFORMATION => 0x1, |
| 76 |
GROUP_SECURITY_INFORMATION => 0x2, |
| 77 |
DACL_SECURITY_INFORMATION => 0x4, |
| 78 |
SACL_SECURITY_INFORMATION => 0x8, |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
The bitmask manipulator can then be accessed as: |
| 84 |
|
| 85 |
&SECURITY_INFORMATION->build_mask('DACL_SECURITY_INFORMATION'); |
| 86 |
|
| 87 |
Or, if you are outside of the module, as: |
| 88 |
|
| 89 |
&Win32::Security::SECURITY_INFORMATION->build_mask('DACL_SECURITY_INFORMATION'); |
| 90 |
|
| 91 |
This has several advantages: |
| 92 |
|
| 93 |
* Demand creation of the "Data::Bitmask" object. Creating objects with |
| 94 |
huge numbers of constants (i.e. hundreds or thousands) can be a bit |
| 95 |
time consuming, so this delays creation until the object actually |
| 96 |
gets used. At the same time, the created object is cached. |
| 97 |
|
| 98 |
* Easy access from within in the module, reasonably easy access from |
| 99 |
outside the module. |
| 100 |
|
| 101 |
* If the user wants even easier access from outside the module, you |
| 102 |
can support Exporter and let the sub be exported. |
| 103 |
|
| 104 |
Method Reference |
| 105 |
new |
| 106 |
Creates a new bitmask manipulator. Pass a list of constant and value |
| 107 |
pairs. The constants do not have to be disjoint, but order does matter. |
| 108 |
When executing "explain_mask" or "explain_const", constants that are |
| 109 |
earlier in the list take precedence over those later in the list. |
| 110 |
Constant names are not allowed to have space or pipes in them, and |
| 111 |
constant values have to be integers. Constant names are case insensitive |
| 112 |
but preserving. |
| 113 |
|
| 114 |
If the passed value for the constant name is an anonymous array, then it |
| 115 |
is presumed that the name is the first value and that the remainder |
| 116 |
consists of name-value pairs of parameters. The only currently supported |
| 117 |
parameter is "full_match", which implies that the constant should only |
| 118 |
be returned from "break_mask" or "explain_mask" if it perfectly matches |
| 119 |
the mask being explained. For example: |
| 120 |
|
| 121 |
[qw(FILES_ONLY_NO_INHERIT full_match 1)] => 1, |
| 122 |
|
| 123 |
add_constants |
| 124 |
Adds constants to an existing bitmask manipulator. Pass a list of |
| 125 |
constant and value pairs as for "new". Constants will be added to the |
| 126 |
end of the list (see "new" for an explanation of ordering concerns). |
| 127 |
|
| 128 |
The main use for "add_constants" is adding aggregate constants created |
| 129 |
by using "build_mask". |
| 130 |
|
| 131 |
build_mask |
| 132 |
This takes one of three things as a parameter: |
| 133 |
|
| 134 |
* scalar - string is split on '"|"' and/or whitespace to generate a |
| 135 |
list of constants |
| 136 |
|
| 137 |
* ARRAY ref - elements are the list of constants |
| 138 |
|
| 139 |
* HASH ref - keys with true values are the list of constants; keys |
| 140 |
with false values are subtracted from the resultant mask |
| 141 |
|
| 142 |
In all situations, integers are legal in place of constant names and are |
| 143 |
treated as the value, after adding 2**32 to any negative integers. |
| 144 |
|
| 145 |
break_mask |
| 146 |
Breaks a mask apart. Pass a mask value as an integer. Returns a hash of |
| 147 |
all constants whose values are subsets of the passed mask. Values are |
| 148 |
set to 1 so the result can safely be passed to "build_mask". |
| 149 |
|
| 150 |
Commonly used for operations like: |
| 151 |
|
| 152 |
if ($MaskManipulator->break_mask($my_mask_value)->{CONSTANT}) { |
| 153 |
|
| 154 |
Note that "break_mask" accepts |
| 155 |
|
| 156 |
To eliminate a constant from explain_mask or break_mask unless it |
| 157 |
perfectly matches, use "full_match" constants. |
| 158 |
|
| 159 |
explain_mask |
| 160 |
Explains a mask in terms of a relatively minimal set of constants. Pass |
| 161 |
either a mask value as an integer or any valid parameter for |
| 162 |
"build_mask". Returns a hash of constants that will recreate the mask. |
| 163 |
Many times, this will be the minimum number of constants necessary to |
| 164 |
describe the mask. Note that creating the true minimum set of constants |
| 165 |
is somewhat painful (see Knapsack problem). |
| 166 |
|
| 167 |
The algorithm used by "explain_mask" is to first test for a constant |
| 168 |
that perfectly matches the mask. If one is found, this is the obvious |
| 169 |
answer. In the absence of a perfect match, "break_mask" is used to |
| 170 |
generate a maximal solution. All simply occluded constants are then |
| 171 |
eliminated (that is to say, all constants in the list whose values are |
| 172 |
subsets of another single constant). This means, for instance, that if |
| 173 |
you had only three constants, AB => 3, BC => 6, and AC => 5, |
| 174 |
"explain_mask" would return all three when passed the value 7 because no |
| 175 |
one constant is a subset of any single one of the others. |
| 176 |
|
| 177 |
To eliminate a constant from explain_mask or break_mask unless it |
| 178 |
perfectly matches, use "full_match" constants. |
| 179 |
|
| 180 |
build_const |
| 181 |
This takes one of two things as a parameter: |
| 182 |
|
| 183 |
* scalar integer - if a scalar integer is passed, then the value is |
| 184 |
simply returned, after adding 2**32 to any negative integers |
| 185 |
|
| 186 |
* scalar - string is looked up in the list of constants |
| 187 |
|
| 188 |
explain_const |
| 189 |
Looks for a perfect match for the passed mask value. Pass either a mask |
| 190 |
value as an integer or any valid parameter for "build_mask". If one is |
| 191 |
not found, it croaks. |
| 192 |
|
| 193 |
get_constants |
| 194 |
Returns all constants passed either to "new" or "add_constants". |
| 195 |
|
| 196 |
AUTHOR |
| 197 |
Toby Ovod-Everett, [email protected] |
| 198 |
|
| 199 |
LICENSE |
| 200 |
Copyright 2003, 2004 Toby Ovod-Everett. All rights reserved. This |
| 201 |
program is free software; you can redistribute it and/or modify it under |
| 202 |
the same terms as Perl itself. |
| 203 |
|