/usr/lib/ruby/vendor_ruby/ffi/bit_masks/bit_mask.rb is in ruby-ffi-bit-masks 0.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | require 'ffi'
module FFI
module BitMasks
#
# The bitmask data converter.
#
class BitMask
include DataConverter
#
# Flags of the bitmask.
#
# @return [Hash{Symbol => Integer}]
# The mapping of bit-flags to bitmasks.
#
attr_reader :flags
#
# The masks of the bitmask.
#
# @return [Hash{Integer => Symbol}]
# The mapping of bitmasks to bit-flags.
#
attr_reader :bitmasks
#
# The underlying native type.
#
# @return [FFI::Type]
# The FFI primitive.
#
attr_reader :native_type
#
# Initializes a new bitmask.
#
# @param [Hash{Symbol => Integer}] flags
# The flags and their masks.
#
# @param [Symbol] type
# The underlying type.
#
def initialize(flags,type=:uint)
@flags = flags
@bitmasks = flags.invert
@native_type = FFI.find_type(type)
end
#
# The Symbols that can be passed to the data converter.
#
# @return [Array<Symbol>]
# The Array of Symbols.
#
# @note For compatibility with `FFI::Enum`.
#
def symbols
@flags.keys
end
#
# The mapping of acceptable Symbols to their Integer equivalents.
#
# @return [Hash{Symbol => Integer}]
# The mapping of Symbols.
#
# @note For compatibility with `FFI::Enum`.
#
def symbol_map
@flags
end
#
# @note For compatibility with `FFI::Enum`.
#
alias to_h symbol_map
#
# @note For compatibility with `FFI::Enum`.
#
alias to_hash symbol_map
#
# Maps flags to masks and vice versa.
#
# @overload [](query)
#
# @param [Symbol] query
# The flag name.
#
# @return [Integer]
# The mask for the flag.
#
# @overload [](query)
#
# @param [Integer] query
# The mask.
#
# @return [Symbol]
# The flag for the mask.
#
def [](query)
case query
when Symbol then @flags[query]
when Integer then @bitmasks[query]
end
end
#
# @note For compatibility with `FFI::Enum`.
#
alias find []
#
# Converts flags to a bitmask.
#
# @overload to_native(value)
#
# @param [Hash{Symbol => Boolean}] value
# The flags and their values.
#
# @return [Integer]
# The bitmask for the given flags.
#
# @overload to_native(value)
#
# @param [#to_int] value
# The raw bitmask.
#
# @return [Integer]
# The bitmask.
#
# @raise [ArgumentError]
#
def to_native(value,ctx=nil)
uint = 0
case value
when Hash
uint = 0
value.each do |flag,value|
if (@flags.has_key?(flag) && value)
uint |= @flags[flag]
end
end
return uint
else
if value.respond_to?(:to_int)
int = value.to_int
@bitmasks.each_key do |mask|
uint |= (int & mask)
end
return uint
else
raise(ArgumentError,"invalid bitmask value #{value.inspect}")
end
end
end
#
# Converts a bitmask into multiple flags.
#
# @param [Integer] value
# The raw bitmask.
#
# @return [Hash{Symbol => Boolean}]
# The flags for the bitmask.
#
def from_native(value,ctx=nil)
flags = {}
@flags.each do |flag,bitmask|
flags[flag] ||= ((value & bitmask) == bitmask)
end
return flags
end
def reference_required?
false
end
end
end
end
|