Class: FFI::Pointer

Inherits:
Object show all
Defined in:
lib/puppet/util/windows/api_types.rb

Constant Summary collapse

NULL_HANDLE =
0
NULL_TERMINATOR_WCHAR =
0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_string_to_wide_string(str, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/puppet/util/windows/api_types.rb', line 24

def self.from_string_to_wide_string(str, &block)
  str = Puppet::Util::Windows::String.wide_string(str)
  FFI::MemoryPointer.new(:byte, str.bytesize) do |ptr|
    # uchar here is synonymous with byte
    ptr.put_array_of_uchar(0, str.bytes.to_a)

    yield ptr
  end

  # ptr has already had free called, so nothing to return
  nil
end

Instance Method Details

#read_arbitrary_wide_string_up_to(max_char_length = 512) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/puppet/util/windows/api_types.rb', line 62

def read_arbitrary_wide_string_up_to(max_char_length = 512)
  # max_char_length is number of wide chars (typically excluding NULLs), *not* bytes
  # use a pointer to read one UTF-16LE char (2 bytes) at a time
  wchar_ptr = FFI::Pointer.new(:wchar, address)

  # now iterate 2 bytes at a time until an offset lower than max_char_length is found
  0.upto(max_char_length - 1) do |i|
    if wchar_ptr[i].read_wchar == NULL_TERMINATOR_WCHAR
      return read_wide_string(i)
    end
  end

  read_wide_string(max_char_length)
end

#read_com_memory_pointer(&block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/puppet/util/windows/api_types.rb', line 94

def read_com_memory_pointer(&block)
  ptr = nil
  begin
    ptr = read_pointer
    yield ptr
  ensure
    FFI::WIN32::CoTaskMemFree(ptr) if ptr && ! ptr.null?
  end

  # ptr has already had CoTaskMemFree called, so nothing to return
  nil
end

#read_handleObject



49
50
51
# File 'lib/puppet/util/windows/api_types.rb', line 49

def read_handle
  type_size == 4 ? read_uint32 : read_uint64
end

#read_wide_string(char_length, dst_encoding = Encoding::UTF_8) ⇒ Object



56
57
58
59
60
# File 'lib/puppet/util/windows/api_types.rb', line 56

def read_wide_string(char_length, dst_encoding = Encoding::UTF_8)
  # char_length is number of wide chars (typically excluding NULLs), *not* bytes
  str = get_bytes(0, char_length * 2).force_encoding('UTF-16LE')
  str.encode(dst_encoding)
end

#read_win32_boolObject



37
38
39
40
41
# File 'lib/puppet/util/windows/api_types.rb', line 37

def read_win32_bool
  # BOOL is always a 32-bit integer in Win32
  # some Win32 APIs return 1 for true, while others are non-0
  read_int32 != FFI::WIN32_FALSE
end

#read_win32_local_pointer(&block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/puppet/util/windows/api_types.rb', line 77

def read_win32_local_pointer(&block)
  ptr = nil
  begin
    ptr = read_pointer
    yield ptr
  ensure
    if ptr && ! ptr.null?
      if FFI::WIN32::LocalFree(ptr.address) != FFI::Pointer::NULL_HANDLE
        Puppet.debug "LocalFree memory leak"
      end
    end
  end

  # ptr has already had LocalFree called, so nothing to return
  nil
end