chat_xdk 0.7.10-x86_64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,266 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ffi"
4
+ require "rbconfig"
5
+
6
+ module ChatXdk
7
+ # Resolves and loads the chat_xdk_ffi shared library.
8
+ #
9
+ # Search order:
10
+ # 1. ENV["CHAT_XDK_LIB_PATH"] (either a library file path or a directory)
11
+ # 2. Bundled native/<rid>/<lib> shipped inside the gem
12
+ # 3. System library search path (libchat_xdk_ffi)
13
+ module NativeLoader
14
+ LIB_BASENAME = "chat_xdk_ffi"
15
+ private_constant :LIB_BASENAME
16
+
17
+ module_function
18
+
19
+ # Runtime identifier aligned with the release matrix:
20
+ # osx-arm64, osx-x64, linux-x64, linux-arm64, win-x64.
21
+ def detect_rid(host_os = RbConfig::CONFIG["host_os"], host_cpu = RbConfig::CONFIG["host_cpu"])
22
+ os = host_os.to_s.downcase
23
+ cpu = host_cpu.to_s.downcase
24
+ arm = cpu.include?("aarch64") || cpu.include?("arm64")
25
+ x64 = cpu.include?("x86_64") || cpu.include?("amd64") || cpu == "x64"
26
+
27
+ if os.include?("darwin")
28
+ return "osx-arm64" if arm
29
+ return "osx-x64" if x64
30
+ elsif os.include?("linux")
31
+ return "linux-arm64" if arm
32
+ return "linux-x64" if x64
33
+ elsif os.include?("mingw") || os.include?("mswin") || os.include?("cygwin")
34
+ return "win-x64" if x64
35
+ end
36
+ nil
37
+ end
38
+
39
+ def library_file_name(rid)
40
+ case rid
41
+ when "osx-arm64", "osx-x64"
42
+ "lib#{LIB_BASENAME}.dylib"
43
+ when "linux-x64", "linux-arm64"
44
+ "lib#{LIB_BASENAME}.so"
45
+ when "win-x64"
46
+ "#{LIB_BASENAME}.dll"
47
+ else
48
+ raise ArgumentError, "unsupported rid: #{rid}"
49
+ end
50
+ end
51
+
52
+ def candidate_paths
53
+ candidates = []
54
+ rid = detect_rid
55
+ file_name = rid ? library_file_name(rid) : nil
56
+
57
+ env_path = ENV["CHAT_XDK_LIB_PATH"]
58
+ if env_path && !env_path.empty?
59
+ if File.directory?(env_path)
60
+ candidates << File.join(env_path, file_name) if file_name
61
+ else
62
+ candidates << env_path
63
+ end
64
+ end
65
+
66
+ if rid && file_name
67
+ bundled = File.expand_path(File.join("native", rid, file_name), __dir__)
68
+ candidates << bundled if File.file?(bundled)
69
+ end
70
+
71
+ # System search fallback
72
+ candidates << LIB_BASENAME
73
+ candidates << file_name if file_name
74
+ candidates.uniq
75
+ end
76
+ end
77
+ private_constant :NativeLoader
78
+
79
+ # Internal FFI bindings to the chat_xdk_ffi C ABI.
80
+ module Native
81
+ extend FFI::Library
82
+
83
+ EXPECTED_ABI_VERSION = 1
84
+
85
+ ffi_lib NativeLoader.candidate_paths
86
+
87
+ # Returned by value from every fallible chat_xdk_* C ABI entry point.
88
+ class FfiResult < FFI::Struct
89
+ layout :data, :pointer,
90
+ :error, :pointer
91
+ end
92
+
93
+ attach_function :chat_xdk_abi_version, [], :uint32
94
+
95
+ attach_function :chat_xdk_new, [:pointer, :pointer], FfiResult.by_value
96
+ attach_function :chat_xdk_free, [:pointer], :void
97
+ attach_function :chat_xdk_free_string, [:pointer], :void
98
+
99
+ attach_function :chat_xdk_is_unlocked, [:pointer], FfiResult.by_value
100
+ attach_function :chat_xdk_has_identity_key, [:pointer], FfiResult.by_value
101
+ attach_function :chat_xdk_set_reject_unverified, [:pointer, :int32], FfiResult.by_value
102
+ attach_function :chat_xdk_set_identity, [:pointer, :pointer], FfiResult.by_value
103
+ attach_function :chat_xdk_set_cache_keys, [:pointer, :int32], FfiResult.by_value
104
+ attach_function :chat_xdk_set_signing_keys, [:pointer, :pointer], FfiResult.by_value
105
+ attach_function :chat_xdk_add_signing_keys, [:pointer, :pointer], FfiResult.by_value
106
+ attach_function :chat_xdk_add_public_keys, [:pointer, :pointer], FfiResult.by_value
107
+ attach_function :chat_xdk_missing_public_keys, [:pointer, :pointer], FfiResult.by_value
108
+ attach_function :chat_xdk_missing_public_keys_for, [:pointer, :pointer, :pointer], FfiResult.by_value
109
+ attach_function :chat_xdk_lock, [:pointer], FfiResult.by_value
110
+
111
+ attach_function :chat_xdk_update_config, [:pointer, :pointer], FfiResult.by_value
112
+ attach_function :chat_xdk_setup, [:pointer, :pointer, :size_t], FfiResult.by_value, blocking: true
113
+ attach_function :chat_xdk_unlock, [:pointer, :pointer, :size_t], FfiResult.by_value, blocking: true
114
+ attach_function :chat_xdk_delete, [:pointer], FfiResult.by_value, blocking: true
115
+ attach_function :chat_xdk_change_pin, [:pointer, :pointer, :size_t, :pointer, :size_t], FfiResult.by_value, blocking: true
116
+
117
+ attach_function :chat_xdk_generate_keypairs, [:pointer], FfiResult.by_value
118
+ attach_function :chat_xdk_get_public_keys, [:pointer], FfiResult.by_value
119
+ attach_function :chat_xdk_get_public_key_fingerprint, [:pointer], FfiResult.by_value
120
+ attach_function :chat_xdk_export_keys, [:pointer], FfiResult.by_value
121
+ attach_function :chat_xdk_import_keys, [:pointer, :pointer, :size_t, :pointer], FfiResult.by_value
122
+
123
+ attach_function :chat_xdk_extract_conversation_keys, [:pointer, :pointer], FfiResult.by_value, blocking: true
124
+ attach_function :chat_xdk_decrypt_events, [:pointer, :pointer], FfiResult.by_value, blocking: true
125
+ attach_function :chat_xdk_decrypt_event, [:pointer, :pointer], FfiResult.by_value, blocking: true
126
+
127
+ attach_function :chat_xdk_prepare_conversation_key_change, [:pointer, :pointer], FfiResult.by_value
128
+ attach_function :chat_xdk_prepare_group_members_change, [:pointer, :pointer], FfiResult.by_value
129
+ attach_function :chat_xdk_prepare_group_create, [:pointer, :pointer], FfiResult.by_value
130
+ attach_function :chat_xdk_prepare_message_delete, [:pointer, :pointer], FfiResult.by_value
131
+ attach_function :chat_xdk_prepare_group_members_remove, [:pointer, :pointer], FfiResult.by_value
132
+ attach_function :chat_xdk_prepare_group_leave, [:pointer, :pointer], FfiResult.by_value
133
+ attach_function :chat_xdk_prepare_group_title_change, [:pointer, :pointer], FfiResult.by_value
134
+ attach_function :chat_xdk_prepare_group_avatar_change, [:pointer, :pointer], FfiResult.by_value
135
+ attach_function :chat_xdk_prepare_group_description_change, [:pointer, :pointer], FfiResult.by_value
136
+ attach_function :chat_xdk_prepare_group_admins_add, [:pointer, :pointer], FfiResult.by_value
137
+ attach_function :chat_xdk_prepare_group_admins_remove, [:pointer, :pointer], FfiResult.by_value
138
+ attach_function :chat_xdk_prepare_group_invite_enable, [:pointer, :pointer], FfiResult.by_value
139
+ attach_function :chat_xdk_prepare_group_invite_disable, [:pointer, :pointer], FfiResult.by_value
140
+ attach_function :chat_xdk_prepare_group_join_request, [:pointer, :pointer], FfiResult.by_value
141
+ attach_function :chat_xdk_prepare_group_join_reject, [:pointer, :pointer], FfiResult.by_value
142
+ attach_function :chat_xdk_prepare_message_ttl_change, [:pointer, :pointer], FfiResult.by_value
143
+ attach_function :chat_xdk_prepare_message_ttl_remove, [:pointer, :pointer], FfiResult.by_value
144
+ attach_function :chat_xdk_prepare_conversation_mute, [:pointer, :pointer], FfiResult.by_value
145
+ attach_function :chat_xdk_prepare_screen_capture, [:pointer, :pointer], FfiResult.by_value
146
+ attach_function :chat_xdk_prepare_conversation_archive, [:pointer, :pointer], FfiResult.by_value
147
+ attach_function :chat_xdk_prepare_mark_conversation_read, [:pointer, :pointer], FfiResult.by_value
148
+
149
+ attach_function :chat_xdk_encrypt_message, [:pointer, :pointer], FfiResult.by_value
150
+ attach_function :chat_xdk_encrypt_forward, [:pointer, :pointer], FfiResult.by_value
151
+ attach_function :chat_xdk_encrypt_reply, [:pointer, :pointer], FfiResult.by_value
152
+ attach_function :chat_xdk_encrypt_add_reaction, [:pointer, :pointer], FfiResult.by_value
153
+ attach_function :chat_xdk_encrypt_remove_reaction, [:pointer, :pointer], FfiResult.by_value
154
+ attach_function :chat_xdk_encrypt_edit, [:pointer, :pointer], FfiResult.by_value
155
+
156
+ attach_function :chat_xdk_decrypt_conversation_key, [:pointer, :pointer], FfiResult.by_value
157
+ attach_function :chat_xdk_decrypt, [:pointer, :pointer, :pointer], FfiResult.by_value
158
+
159
+ attach_function :chat_xdk_encrypt_file, [:pointer, :pointer], FfiResult.by_value, blocking: true
160
+ attach_function :chat_xdk_decrypt_file, [:pointer, :pointer], FfiResult.by_value, blocking: true
161
+ attach_function :chat_xdk_encrypt_file_with_key, [:pointer, :pointer], FfiResult.by_value, blocking: true
162
+ attach_function :chat_xdk_decrypt_file_with_key, [:pointer, :pointer], FfiResult.by_value, blocking: true
163
+
164
+ attach_function :chat_xdk_file_encryptor_new, [:pointer, :pointer, :pointer], FfiResult.by_value
165
+ attach_function :chat_xdk_file_encryptor_push, [:pointer, :pointer], FfiResult.by_value, blocking: true
166
+ attach_function :chat_xdk_file_encryptor_finish, [:pointer], FfiResult.by_value, blocking: true
167
+ attach_function :chat_xdk_file_encryptor_free, [:pointer], :void
168
+
169
+ attach_function :chat_xdk_file_decryptor_new, [:pointer, :pointer, :pointer], FfiResult.by_value
170
+ attach_function :chat_xdk_file_decryptor_push, [:pointer, :pointer], FfiResult.by_value, blocking: true
171
+ attach_function :chat_xdk_file_decryptor_finish, [:pointer], FfiResult.by_value, blocking: true
172
+ attach_function :chat_xdk_file_decryptor_free, [:pointer], :void
173
+
174
+ attach_function :chat_xdk_sign, [:pointer, :pointer, :size_t], FfiResult.by_value
175
+ attach_function :chat_xdk_verify, [:pointer, :pointer, :pointer, :pointer, :size_t], FfiResult.by_value
176
+ attach_function :chat_xdk_verify_key_binding, [:pointer, :pointer, :pointer, :pointer], FfiResult.by_value
177
+ attach_function :chat_xdk_matches_registered_key, [:pointer, :pointer], FfiResult.by_value
178
+
179
+ attach_function :chat_xdk_encrypted_file_size, [:uint64], FfiResult.by_value
180
+ attach_function :chat_xdk_bytes_to_base64, [:pointer, :size_t], FfiResult.by_value
181
+ attach_function :chat_xdk_base64_to_bytes, [:pointer], FfiResult.by_value
182
+ attach_function :chat_xdk_bytes_to_hex, [:pointer, :size_t], FfiResult.by_value
183
+ attach_function :chat_xdk_hex_to_bytes, [:pointer], FfiResult.by_value
184
+ attach_function :chat_xdk_detect_mime_type, [:pointer, :size_t], FfiResult.by_value
185
+ attach_function :chat_xdk_detect_image_dimensions, [:pointer, :size_t], FfiResult.by_value
186
+
187
+ # Internal feature-switch hooks (not part of the public API).
188
+ attach_function :chat_xdk_internal_feature_start, [:pointer], FfiResult.by_value
189
+ attach_function :chat_xdk_internal_feature_complete, [:uint64, :int32, :pointer, :size_t], FfiResult.by_value
190
+ attach_function :chat_xdk_internal_feature_configure, [:pointer, :pointer, :uint64, :uint64], FfiResult.by_value
191
+ attach_function :chat_xdk_internal_feature_set_default_endpoint, [:pointer], FfiResult.by_value
192
+
193
+ abi = chat_xdk_abi_version
194
+ if abi != EXPECTED_ABI_VERSION
195
+ raise ChatXdk::Error.new(
196
+ "chat_xdk_ffi ABI version mismatch: expected #{EXPECTED_ABI_VERSION}, got #{abi}",
197
+ kind: :internal
198
+ )
199
+ end
200
+
201
+ module_function
202
+
203
+ # Consume an FfiResult, freeing both native strings (which zeroizes them
204
+ # on the native side). Raises the matching ChatXdk::Error on error.
205
+ def consume(result)
206
+ data_ptr = result[:data]
207
+ error_ptr = result[:error]
208
+ begin
209
+ data = data_ptr.null? ? nil : data_ptr.read_string.force_encoding(Encoding::UTF_8)
210
+ error = error_ptr.null? ? nil : error_ptr.read_string.force_encoding(Encoding::UTF_8)
211
+ ensure
212
+ chat_xdk_free_string(data_ptr) unless data_ptr.null?
213
+ chat_xdk_free_string(error_ptr) unless error_ptr.null?
214
+ end
215
+ raise ChatXdk::Error.from_document(error) if error
216
+
217
+ data || ""
218
+ end
219
+
220
+ def consume_bool(result)
221
+ consume(result) == "true"
222
+ end
223
+
224
+ # Allocate a NUL-terminated UTF-8 C string pointer.
225
+ def utf8_ptr(str)
226
+ raise ArgumentError, "expected String, got #{str.class}" unless str.is_a?(String)
227
+
228
+ encoded = str.encode(Encoding::UTF_8)
229
+ if encoded.include?("\0")
230
+ raise ChatXdk::InvalidInputError.new("String argument contains NUL byte", kind: :invalid_input)
231
+ end
232
+
233
+ FFI::MemoryPointer.from_string(encoded)
234
+ end
235
+
236
+ # Yield (ptr, byte_len) for a binary buffer without adding a NUL byte.
237
+ # When wipe: true, zeroize the native buffer before releasing it.
238
+ def with_bytes(data, wipe: false)
239
+ raise ArgumentError, "expected String, got #{data.class}" unless data.is_a?(String)
240
+
241
+ bytes = data.b
242
+ len = bytes.bytesize
243
+ return yield(FFI::Pointer::NULL, 0) if len == 0
244
+
245
+ ptr = FFI::MemoryPointer.new(:uint8, len)
246
+ ptr.put_bytes(0, bytes)
247
+ begin
248
+ yield(ptr, len)
249
+ ensure
250
+ ptr.put_bytes(0, "\0" * len) if wipe
251
+ ptr.free
252
+ end
253
+ end
254
+
255
+ def b64_encode(bytes)
256
+ [bytes.b].pack("m0")
257
+ end
258
+
259
+ def b64_decode(str)
260
+ pad = (4 - (str.bytesize % 4)) % 4
261
+ padded = pad == 0 ? str : "#{str}#{'=' * pad}"
262
+ padded.unpack1("m0").force_encoding(Encoding::BINARY)
263
+ end
264
+ end
265
+ private_constant :Native
266
+ end