| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# native.rb - a Real SHIORI loader for ninix |
| 4 |
# Copyright (C) 2024 by Tatakinov |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify it |
| 7 |
# under the terms of the GNU General Public License (version 2) as |
| 8 |
# published by the Free Software Foundation. It is distributed in the |
| 9 |
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
| 10 |
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 11 |
# PURPOSE. See the GNU General Public License for more details. |
| 12 |
# |
| 13 |
|
| 14 |
require "fiddle" |
| 15 |
require "fiddle/import" |
| 16 |
|
| 17 |
require_relative "../logging" |
| 18 |
|
| 19 |
module Native |
| 20 |
|
| 21 |
CP_OEMCP = 1 |
| 22 |
GPTR = 0x40 |
| 23 |
|
| 24 |
extend Fiddle::Importer |
| 25 |
begin |
| 26 |
dlload "kernel32.dll" |
| 27 |
extern "int SetDllDirectoryW(wchar_t *)" |
| 28 |
extern "void *GlobalAlloc(unsigned int, size_t)" |
| 29 |
extern "void *GlobalFree(void *)" |
| 30 |
extern "int WideCharToMultiByte(unsigned int, unsigned long, wchar_t *, int, char *, int, void *, void *)" |
| 31 |
|
| 32 |
$_kernel32 = self |
| 33 |
rescue |
| 34 |
$_kernel32 = nil |
| 35 |
end |
| 36 |
|
| 37 |
class Shiori |
| 38 |
|
| 39 |
def initialize(dll_name) |
| 40 |
@dll_name = dll_name |
| 41 |
@handle = nil |
| 42 |
@func = {} |
| 43 |
end |
| 44 |
|
| 45 |
def find(topdir, dll_name) |
| 46 |
result = 0 |
| 47 |
unless $_kernel32.nil? |
| 48 |
dll_path = File.join(topdir, dll_name) |
| 49 |
dll_dir = File.dirname(dll_path) |
| 50 |
$_kernel32.SetDllDirectoryW(dll_dir.encode('UTF-16LE')) |
| 51 |
begin |
| 52 |
handle = Fiddle::Handle.new(dll_path) |
| 53 |
handle.close |
| 54 |
result = 1000 |
| 55 |
rescue |
| 56 |
result = 0 |
| 57 |
end |
| 58 |
null = Fiddle::Pointer[0] |
| 59 |
$_kernel32.SetDllDirectoryW(null) |
| 60 |
end |
| 61 |
return result |
| 62 |
end |
| 63 |
|
| 64 |
def show_description |
| 65 |
Logging::Logging.info( |
| 66 |
"Shiori: a Real SHIORI loader for ninix\n" \ |
| 67 |
" Copyright (C) 2024 by Tatakinov") |
| 68 |
end |
| 69 |
|
| 70 |
def load(dir: nil) |
| 71 |
unless dir.end_with?(File::SEPARATOR) |
| 72 |
dir = [dir, File::SEPARATOR].join |
| 73 |
end |
| 74 |
dll_path = File.join(dir, @dll_name) |
| 75 |
dll_dir = File.dirname(dll_path) |
| 76 |
$_kernel32.SetDllDirectoryW(dll_dir.encode('UTF-16LE')) |
| 77 |
@handle = Fiddle::Handle.new(dll_path) |
| 78 |
null = Fiddle::Pointer[0] |
| 79 |
$_kernel32.SetDllDirectoryW(null) |
| 80 |
@func[:load] = Fiddle::Function.new(@handle['load'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG], Fiddle::TYPE_INT) |
| 81 |
@func[:request] = Fiddle::Function.new(@handle['request'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOIDP) |
| 82 |
@func[:unload] = Fiddle::Function.new(@handle['unload'], [], Fiddle::TYPE_INT) |
| 83 |
len = $_kernel32.WideCharToMultiByte(CP_OEMCP, 0, dir.encode('UTF-16LE'), dir.size, null, 0, null, null) |
| 84 |
buf = $_kernel32.GlobalAlloc(GPTR, len) |
| 85 |
len = $_kernel32.WideCharToMultiByte(CP_OEMCP, 0, dir.encode('UTF-16LE'), dir.size, buf, len, null, null) |
| 86 |
ret = @func[:load].call(buf, len) |
| 87 |
return ret |
| 88 |
end |
| 89 |
|
| 90 |
def unload |
| 91 |
ret = @func[:unload].call() |
| 92 |
@func = {} |
| 93 |
@handle.close |
| 94 |
end |
| 95 |
|
| 96 |
def request(req) |
| 97 |
buf = $_kernel32.GlobalAlloc(GPTR, req.bytesize + 1) |
| 98 |
buf[0, req.bytesize] = req |
| 99 |
len = [req.bytesize].pack('l!') |
| 100 |
result = @func[:request].call(buf, len) |
| 101 |
len, = len.unpack('l!') |
| 102 |
ret = result[0, len].to_s |
| 103 |
$_kernel32.GlobalFree(result) |
| 104 |
return ret |
| 105 |
end |
| 106 |
end |
| 107 |
end |