-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathHIDEAGEM.py
243 lines (185 loc) · 6.71 KB
/
HIDEAGEM.py
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#
# 888 888 8888888 8888888b. 8888888888 d8888 .d8888b. 8888888888 888b d888
# 888 888 888 888 "Y88b 888 d88888 d88P Y88b 888 8888b d8888
# 888 888 888 888 888 888 d88P888 888 888 888 88888b.d88888
# 8888888888 888 888 888 8888888 d88P 888 888 8888888 888Y88888P888
# 888 888 888 888 888 888 d88P 888 888 88888 888 888 Y888P 888
# 888 888 888 888 888 888 d88P 888 888 888 888 888 Y8P 888
# 888 888 888 888 .d88P 888 d8888888888 Y88b d88P 888 888 " 888
# 888 888 8888888 8888888P" 8888888888 d88P 888 "Y8888P88 8888888888 888 888
#
# COPYRIGHT (c) 2024 WWW.CYBERGEM.NET
import os
import math
import ctypes
import argparse
import numpy as np
from ctypes import POINTER
#
# HIDEAGEM C INTERFACE
#
HIDEAGEM_CORE = None
# Try to load HIDEAGEM library from current directory, then try bin/
dll_name = "HIDEAGEM.dll" if os.name == 'nt' else "HIDEAGEM.so"
this_dir = os.path.dirname(os.path.realpath(__file__))
dll_path = os.path.join(this_dir, dll_name)
if not os.path.exists(dll_path):
dll_path = os.path.join(this_dir, "bin", dll_name)
# Load HIDEAGEM library
if os.name == 'posix':
HIDEAGEM_CORE = ctypes.CDLL(dll_path)
else:
HIDEAGEM_CORE = ctypes.WinDLL(dll_path, winmode=0)
# Hide Gems
HIDEAGEM_CORE.HIDEAGEM_HIDE_GEMS_C.argtypes = [
ctypes.c_int,
ctypes.c_void_p,
ctypes.c_uint64,
ctypes.POINTER(ctypes.POINTER(ctypes.c_char)),
ctypes.c_int,
ctypes.c_char_p,
ctypes.c_int,
ctypes.POINTER(ctypes.c_uint64),
ctypes.c_bool
]
HIDEAGEM_CORE.HIDEAGEM_HIDE_GEMS_C.restype = ctypes.POINTER(ctypes.c_uint8)
# Find Gems
HIDEAGEM_CORE.HIDEAGEM_FIND_GEMS_C.argtypes = [
ctypes.POINTER(ctypes.c_void_p),
ctypes.c_uint64, # Ocean Size
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_bool
]
HIDEAGEM_CORE.HIDEAGEM_FIND_GEMS_C.restype = None
# Memory management
HIDEAGEM_CORE.HIDEAGEM_FREE_OCEAN_C.argtypes = [ctypes.c_void_p]
HIDEAGEM_CORE.HIDEAGEM_FREE_OCEAN_C.restype = None
# Debug
HIDEAGEM_CORE.HIDEAGEM_RUN_UNIT_TESTS_C.argtypes = [
ctypes.c_bool,
ctypes.c_bool
]
#
# FIND GEMS
#
def run_find_gems(args):
if not args.ocean:
print("\nGem Ocean is missing.\n")
return
if args.timetrap is not None:
b_time_trap = True
else:
b_time_trap = False
with open(args.ocean, 'rb') as file:
ocean_bytes = file.read()
password_c = b''
if args.password is not None:
password_c = args.password.encode()
ocean_size = len(ocean_bytes)
mutable_array = (ctypes.c_char * len(ocean_bytes)).from_buffer(bytearray(ocean_bytes))
ocean_ptr = ctypes.cast(mutable_array, ctypes.POINTER(ctypes.c_void_p))
output_dir = None if args.output is None else args.output.encode()
HIDEAGEM_CORE.HIDEAGEM_FIND_GEMS_C(
ocean_ptr,
ocean_size,
password_c,
output_dir,
b_time_trap
)
#
# DEMO MODE <3
#
def run_demo_mode(args):
# Run unit tests
HIDEAGEM_CORE.HIDEAGEM_RUN_UNIT_TESTS_C(
True, # Loop
True # Demo mode
)
#
# HIDE GEMS
#
def run_hide_gems(args):
# Load the ocean as a byte array directly
with open(args.ocean, 'rb') as file:
ocean_bytes = file.read()
ocean_size = len(ocean_bytes)
mutable_array = (ctypes.c_char * len(ocean_bytes)).from_buffer(bytearray(ocean_bytes))
ocean_ptr = ctypes.cast(mutable_array, ctypes.POINTER(ctypes.c_void_p))
# Convert list of paths to C-compatible array of byte strings
files = [f.encode('utf-8') for f in args.files]
arr = (ctypes.c_char_p * len(files))(*files)
password_c = b''
if args.password is not None:
password_c = args.password.encode()
if args.timetrap is not None:
time_trap = args.timetrap
gem_protocol = 2 # Auto protocol
else:
time_trap = -1 # Time Trap disabled
gem_protocol = 0 # Auto protocol
# Prepare a c_uint64 variable to capture the output size
out_ocean_size = ctypes.c_uint64()
# Hide Gem Files in Ocean.
# Returned GEM_OCEAN memory must be freed below!
GEM_OCEAN = HIDEAGEM_CORE.HIDEAGEM_HIDE_GEMS_C(
gem_protocol,
ocean_ptr,
ocean_size,
ctypes.cast(arr, ctypes.POINTER(ctypes.POINTER(ctypes.c_char))), # Assuming arr is an array of string pointers
len(args.files),
password_c,
time_trap,
ctypes.byref(out_ocean_size), # Pass the reference to out_ocean_size
args.validate
)
if not GEM_OCEAN:
print("Gem Ocean is nullptr!")
return
if args.output is None: # Not saving Gem to disk
print("No output directory specified. Gem not saved to disk.\n")
return
if not os.path.exists(args.output):
print(f"Creating directory: {args.output}\n")
os.makedirs(args.output)
# Write Gem to disk
base_name, _ = os.path.splitext(os.path.basename(args.ocean)) # Ignore the original extension
output_file_path = os.path.join(args.output, f"{base_name}.png")
counter = 1
while os.path.exists(output_file_path):
output_file_path = os.path.join(args.output, f"{base_name}_{counter}.png")
counter += 1
with open(output_file_path, 'wb') as output_file:
output_file.write(bytearray((ctypes.c_uint8 * out_ocean_size.value).from_address(ctypes.addressof(GEM_OCEAN.contents))))
#
# !!! FREE GEM OCEAN MEMORY !!!
#
HIDEAGEM_CORE.HIDEAGEM_FREE_OCEAN_C( GEM_OCEAN );
print(f"SAVED GEM: {output_file_path}\n")
#
# RUN UNIT TESTS
#
def run_unit_tests(args):
# Run unit tests
HIDEAGEM_CORE.HIDEAGEM_RUN_UNIT_TESTS_C(
True, # Loop
False # Demo mode
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="CLI for Hide and Find Gems")
parser.add_argument('mode', choices=['hide', 'find', 'demo', 'unit'], help='Gem Mode')
parser.add_argument("--ocean", type=str, help="Cover media bit Ocean")
parser.add_argument("--files", nargs='+', help="Gem Files to hide")
parser.add_argument("--output", type=str, help="Output directory")
parser.add_argument("--password", type=str, help="Gem password")
parser.add_argument("--validate", action="store_true", help="Validate Gem hide")
parser.add_argument("--timetrap", type=int, nargs='?', const=0, default=None, help="Time Trap level")
args = parser.parse_args()
if args.mode == "hide":
run_hide_gems(args)
elif args.mode == "find":
run_find_gems(args)
elif args.mode == "unit":
run_unit_tests(args)
elif args.mode == "demo":
run_demo_mode(args)