3030#include " l2c_fcr_hook.h"
3131
3232extern " C" {
33- #include " xz.h"
33+ #include " xz.h"
3434}
3535
3636#define LOG_TAG " LibrePodsHook"
@@ -39,13 +39,13 @@ extern "C" {
3939
4040static HookFunType hook_func = nullptr ;
4141
42- static uint8_t (*original_l2c_fcr_chk_chan_modes)(void *) = nullptr;
43- static tBTA_STATUS (*original_BTA_DmSetLocalDiRecord)(
44- tSDP_DI_RECORD*, uint32_t *) = nullptr;
42+ static uint8_t (*original_l2c_fcr_chk_chan_modes)(void *) = nullptr;
43+
44+ static tBTA_STATUS (*original_BTA_DmSetLocalDiRecord)( tSDP_DI_RECORD *, uint32_t *) = nullptr;
4545
4646static std::atomic<bool > enableSdpHook (false );
4747
48- uint8_t fake_l2c_fcr_chk_chan_modes (void * p_ccb) {
48+ uint8_t fake_l2c_fcr_chk_chan_modes (void * p_ccb) {
4949 LOGI (" fake_l2c_fcr_chk_chan_modes called" );
5050 uint8_t orig = 0 ;
5151 if (original_l2c_fcr_chk_chan_modes)
@@ -55,13 +55,13 @@ uint8_t fake_l2c_fcr_chk_chan_modes(void* p_ccb) {
5555 return 1 ;
5656}
5757
58- tBTA_STATUS fake_BTA_DmSetLocalDiRecord (
59- tSDP_DI_RECORD* p_device_info,
60- uint32_t * p_handle) {
58+ tBTA_STATUS fake_BTA_DmSetLocalDiRecord (tSDP_DI_RECORD *p_device_info, uint32_t *p_handle) {
6159
6260 LOGI (" fake_BTA_DmSetLocalDiRecord called" );
6361
64- if (original_BTA_DmSetLocalDiRecord && enableSdpHook.load (std::memory_order_relaxed)) original_BTA_DmSetLocalDiRecord (p_device_info, p_handle);
62+ if (original_BTA_DmSetLocalDiRecord &&
63+ enableSdpHook.load (std::memory_order_relaxed))
64+ original_BTA_DmSetLocalDiRecord (p_device_info, p_handle);
6565
6666 LOGI (" fake_BTA_DmSetLocalDiRecord: modifying vendor to 0x004C, vendor_id_source to 0x0001" );
6767
@@ -70,14 +70,15 @@ tBTA_STATUS fake_BTA_DmSetLocalDiRecord(
7070 p_device_info->vendor_id_source = 0x0001 ;
7171 }
7272
73- LOGI (" fake_BTA_DmSetLocalDiRecord: returning status %d" , original_BTA_DmSetLocalDiRecord ? original_BTA_DmSetLocalDiRecord (p_device_info, p_handle) : BTA_FAILURE);
74- return original_BTA_DmSetLocalDiRecord ? original_BTA_DmSetLocalDiRecord (p_device_info, p_handle) : BTA_FAILURE;
73+ LOGI (" fake_BTA_DmSetLocalDiRecord: returning status %d" ,
74+ original_BTA_DmSetLocalDiRecord ? original_BTA_DmSetLocalDiRecord (p_device_info, p_handle)
75+ : BTA_FAILURE);
76+ return original_BTA_DmSetLocalDiRecord ? original_BTA_DmSetLocalDiRecord (p_device_info,
77+ p_handle)
78+ : BTA_FAILURE;
7579}
7680
77- static bool decompressXZ (
78- const uint8_t * input,
79- size_t input_size,
80- std::vector<uint8_t >& output) {
81+ static bool decompressXZ (const uint8_t *input, size_t input_size, std::vector<uint8_t > &output) {
8182
8283 LOGI (" decompressXZ called with input_size: %zu" , input_size);
8384
@@ -86,7 +87,7 @@ static bool decompressXZ(
8687 xz_crc64_init ();
8788#endif
8889
89- struct xz_dec * dec = xz_dec_init (XZ_DYNALLOC, 64U << 20 );
90+ struct xz_dec * dec = xz_dec_init (XZ_DYNALLOC, 64U << 20 );
9091 if (!dec) {
9192 LOGE (" decompressXZ: xz_dec_init failed" );
9293 return false ;
@@ -106,7 +107,8 @@ static bool decompressXZ(
106107
107108 LOGI (" decompressXZ: entering decompression loop" );
108109 while (true ) {
109- LOGI (" decompressXZ: xz_dec_run iteration, buf.in_pos: %zu, buf.out_pos: %zu" , buf.in_pos , buf.out_pos );
110+ LOGI (" decompressXZ: xz_dec_run iteration, buf.in_pos: %zu, buf.out_pos: %zu" , buf.in_pos ,
111+ buf.out_pos );
110112 enum xz_ret ret = xz_dec_run (dec, &buf);
111113
112114 LOGI (" decompressXZ: xz_dec_run returned %d" , ret);
@@ -135,10 +137,10 @@ static bool decompressXZ(
135137 return true ;
136138}
137139
138- static bool getLibraryPath (const char * name, std::string& out) {
140+ static bool getLibraryPath (const char * name, std::string & out) {
139141 LOGI (" getLibraryPath called with name: %s" , name);
140142
141- FILE* fp = fopen (" /proc/self/maps" , " r" );
143+ FILE * fp = fopen (" /proc/self/maps" , " r" );
142144 if (!fp) {
143145 LOGE (" getLibraryPath: fopen failed" );
144146 return false ;
@@ -150,7 +152,7 @@ static bool getLibraryPath(const char* name, std::string& out) {
150152 while (fgets (line, sizeof (line), fp)) {
151153 if (strstr (line, name)) {
152154 LOGI (" getLibraryPath: found line containing %s" , name);
153- char * path = strchr (line, ' /' );
155+ char * path = strchr (line, ' /' );
154156 if (path) {
155157 out = path;
156158 out.erase (out.find (' \n ' ));
@@ -166,10 +168,10 @@ static bool getLibraryPath(const char* name, std::string& out) {
166168 return false ;
167169}
168170
169- static uintptr_t getModuleBase (const char * name) {
171+ static uintptr_t getModuleBase (const char * name) {
170172 LOGI (" getModuleBase called with name: %s" , name);
171173
172- FILE* fp = fopen (" /proc/self/maps" , " r" );
174+ FILE * fp = fopen (" /proc/self/maps" , " r" );
173175 if (!fp) {
174176 LOGE (" getModuleBase: fopen failed" );
175177 return 0 ;
@@ -192,25 +194,23 @@ static uintptr_t getModuleBase(const char* name) {
192194 return base;
193195}
194196
195- static uint64_t findSymbolOffsetDynsym (
196- const std::vector<uint8_t >& elf,
197- const char * symbol_substring) {
197+ static uint64_t
198+ findSymbolOffsetDynsym (const std::vector<uint8_t > &elf, const char *symbol_substring) {
198199
199200 LOGI (" findSymbolOffsetDynsym called with %s" , symbol_substring);
200201
201- auto * eh = reinterpret_cast <const Elf64_Ehdr*>(elf.data ());
202- auto * shdr = reinterpret_cast <const Elf64_Shdr*>(
202+ auto * eh = reinterpret_cast <const Elf64_Ehdr *>(elf.data ());
203+ auto * shdr = reinterpret_cast <const Elf64_Shdr *>(
203204 elf.data () + eh->e_shoff );
204205
205- const char * shstr =
206- reinterpret_cast <const char *>(
207- elf.data () + shdr[eh->e_shstrndx ].sh_offset );
206+ const char *shstr = reinterpret_cast <const char *>(
207+ elf.data () + shdr[eh->e_shstrndx ].sh_offset );
208208
209- const Elf64_Shdr* dynsym = nullptr ;
210- const Elf64_Shdr* dynstr = nullptr ;
209+ const Elf64_Shdr * dynsym = nullptr ;
210+ const Elf64_Shdr * dynstr = nullptr ;
211211
212212 for (int i = 0 ; i < eh->e_shnum ; ++i) {
213- const char * secname = shstr + shdr[i].sh_name ;
213+ const char * secname = shstr + shdr[i].sh_name ;
214214
215215 if (!strcmp (secname, " .dynsym" ))
216216 dynsym = &shdr[i];
@@ -223,25 +223,23 @@ static uint64_t findSymbolOffsetDynsym(
223223 return 0 ;
224224 }
225225
226- auto * symbols = reinterpret_cast <const Elf64_Sym*>(
226+ auto * symbols = reinterpret_cast <const Elf64_Sym *>(
227227 elf.data () + dynsym->sh_offset );
228228
229- const char * strings =
230- reinterpret_cast <const char *>(
231- elf.data () + dynstr->sh_offset );
229+ const char *strings = reinterpret_cast <const char *>(
230+ elf.data () + dynstr->sh_offset );
232231
233232 size_t count = dynsym->sh_size / sizeof (Elf64_Sym);
234233
235234 LOGI (" findSymbolOffsetDynsym: scanning %zu symbols" , count);
236235
237236 for (size_t i = 0 ; i < count; ++i) {
238- const char * name = strings + symbols[i].st_name ;
237+ const char * name = strings + symbols[i].st_name ;
239238
240- if (strstr (name, symbol_substring) &&
241- ELF64_ST_TYPE (symbols[i].st_info ) == STT_FUNC) {
239+ if (strstr (name, symbol_substring) && ELF64_ST_TYPE (symbols[i].st_info ) == STT_FUNC) {
242240
243- LOGI (" findSymbolOffsetDynsym: matched %s @ 0x%lx" ,
244- name, (unsigned long )symbols[i].st_value );
241+ LOGI (" findSymbolOffsetDynsym: matched %s @ 0x%lx" , name,
242+ (unsigned long ) symbols[i].st_value );
245243
246244 return symbols[i].st_value ;
247245 }
@@ -251,26 +249,23 @@ static uint64_t findSymbolOffsetDynsym(
251249 return 0 ;
252250}
253251
254- static uint64_t findSymbolOffset (
255- const std::vector<uint8_t >& elf,
256- const char * symbol_substring) {
252+ static uint64_t findSymbolOffset (const std::vector<uint8_t > &elf, const char *symbol_substring) {
257253
258254 LOGI (" findSymbolOffset called with symbol_substring: %s" , symbol_substring);
259255
260- auto * eh = reinterpret_cast <const Elf64_Ehdr*>(elf.data ());
261- auto * shdr = reinterpret_cast <const Elf64_Shdr*>(
256+ auto * eh = reinterpret_cast <const Elf64_Ehdr *>(elf.data ());
257+ auto * shdr = reinterpret_cast <const Elf64_Shdr *>(
262258 elf.data () + eh->e_shoff );
263259
264- const char * shstr =
265- reinterpret_cast <const char *>(
266- elf.data () + shdr[eh->e_shstrndx ].sh_offset );
260+ const char *shstr = reinterpret_cast <const char *>(
261+ elf.data () + shdr[eh->e_shstrndx ].sh_offset );
267262
268- const Elf64_Shdr* symtab = nullptr ;
269- const Elf64_Shdr* strtab = nullptr ;
263+ const Elf64_Shdr * symtab = nullptr ;
264+ const Elf64_Shdr * strtab = nullptr ;
270265
271266 LOGI (" findSymbolOffset: parsing ELF sections" );
272267 for (int i = 0 ; i < eh->e_shnum ; ++i) {
273- const char * secname = shstr + shdr[i].sh_name ;
268+ const char * secname = shstr + shdr[i].sh_name ;
274269 if (!strcmp (secname, " .symtab" ))
275270 symtab = &shdr[i];
276271 if (!strcmp (secname, " .strtab" ))
@@ -283,23 +278,22 @@ static uint64_t findSymbolOffset(
283278 }
284279 LOGI (" findSymbolOffset: found symtab and strtab" );
285280
286- auto * symbols = reinterpret_cast <const Elf64_Sym*>(
281+ auto * symbols = reinterpret_cast <const Elf64_Sym *>(
287282 elf.data () + symtab->sh_offset );
288283
289- const char * strings =
290- reinterpret_cast <const char *>(
291- elf.data () + strtab->sh_offset );
284+ const char *strings = reinterpret_cast <const char *>(
285+ elf.data () + strtab->sh_offset );
292286
293287 size_t count = symtab->sh_size / sizeof (Elf64_Sym);
294288
295289 LOGI (" findSymbolOffset: scanning %zu symbols" , count);
296290 for (size_t i = 0 ; i < count; ++i) {
297- const char * name = strings + symbols[i].st_name ;
291+ const char * name = strings + symbols[i].st_name ;
298292
299- if (strstr (name, symbol_substring) &&
300- ELF64_ST_TYPE (symbols[i].st_info ) == STT_FUNC) {
293+ if (strstr (name, symbol_substring) && ELF64_ST_TYPE (symbols[i].st_info ) == STT_FUNC) {
301294
302- LOGI (" findSymbolOffset: matched symbol %s at 0x%lx" , name, (unsigned long )symbols[i].st_value );
295+ LOGI (" findSymbolOffset: matched symbol %s at 0x%lx" , name,
296+ (unsigned long ) symbols[i].st_value );
303297
304298 return symbols[i].st_value ;
305299 }
@@ -309,7 +303,7 @@ static uint64_t findSymbolOffset(
309303 return 0 ;
310304}
311305
312- static bool hookLibrary (const char * libname) {
306+ static bool hookLibrary (const char * libname) {
313307 LOGI (" hookLibrary called with libname: %s" , libname);
314308
315309 if (!hook_func) {
@@ -336,19 +330,18 @@ static bool hookLibrary(const char* libname) {
336330 close (fd);
337331 return false ;
338332 }
339- LOGI (" hookLibrary: opened file, size: %lld" , (long long )st.st_size );
333+ LOGI (" hookLibrary: opened file, size: %lld" , (long long ) st.st_size );
340334
341335 std::vector<uint8_t > file (st.st_size );
342336 read (fd, file.data (), st.st_size );
343337 close (fd);
344338
345- auto * eh = reinterpret_cast <Elf64_Ehdr*>(file.data ());
346- auto * shdr = reinterpret_cast <Elf64_Shdr*>(
339+ auto * eh = reinterpret_cast <Elf64_Ehdr *>(file.data ());
340+ auto * shdr = reinterpret_cast <Elf64_Shdr *>(
347341 file.data () + eh->e_shoff );
348342
349- const char * shstr =
350- reinterpret_cast <const char *>(
351- file.data () + shdr[eh->e_shstrndx ].sh_offset );
343+ const char *shstr = reinterpret_cast <const char *>(
344+ file.data () + shdr[eh->e_shstrndx ].sh_offset );
352345
353346 uint64_t chk_offset = 0 ;
354347 uint64_t sdp_offset = 0 ;
@@ -357,22 +350,16 @@ static bool hookLibrary(const char* libname) {
357350 if (!strcmp (shstr + shdr[i].sh_name , " .gnu_debugdata" )) {
358351 LOGI (" hookLibrary: found .gnu_debugdata section" );
359352
360- std::vector<uint8_t > compressed (
361- file.begin () + shdr[i].sh_offset ,
362- file.begin () + shdr[i].sh_offset + shdr[i].sh_size );
353+ std::vector<uint8_t > compressed (file.begin () + shdr[i].sh_offset ,
354+ file.begin () + shdr[i].sh_offset + shdr[i].sh_size );
363355
364356 std::vector<uint8_t > decompressed;
365357
366- if (decompressXZ (
367- compressed.data (),
368- compressed.size (),
369- decompressed)) {
358+ if (decompressXZ (compressed.data (), compressed.size (), decompressed)) {
370359
371- chk_offset = findSymbolOffset (decompressed,
372- " l2c_fcr_chk_chan_modes" );
360+ chk_offset = findSymbolOffset (decompressed, " l2c_fcr_chk_chan_modes" );
373361
374- sdp_offset = findSymbolOffset (decompressed,
375- " BTA_DmSetLocalDiRecord" );
362+ sdp_offset = findSymbolOffset (decompressed, " BTA_DmSetLocalDiRecord" );
376363 } else {
377364 LOGE (" debugdata decompress failed" );
378365 }
@@ -383,14 +370,12 @@ static bool hookLibrary(const char* libname) {
383370
384371 if (!chk_offset) {
385372 LOGI (" fallback dynsym chk" );
386- chk_offset = findSymbolOffsetDynsym (file,
387- " l2c_fcr_chk_chan_modes" );
373+ chk_offset = findSymbolOffsetDynsym (file, " l2c_fcr_chk_chan_modes" );
388374 }
389375
390376 if (!sdp_offset) {
391377 LOGI (" fallback dynsym sdp" );
392- sdp_offset = findSymbolOffsetDynsym (file,
393- " BTA_DmSetLocalDiRecord" );
378+ sdp_offset = findSymbolOffsetDynsym (file, " BTA_DmSetLocalDiRecord" );
394379 }
395380
396381 uintptr_t base = getModuleBase (libname);
@@ -400,25 +385,23 @@ static bool hookLibrary(const char* libname) {
400385 }
401386
402387 if (chk_offset) {
403- void * target = reinterpret_cast <void *>(base + chk_offset);
404- hook_func (target,
405- (void *)fake_l2c_fcr_chk_chan_modes,
406- (void **)&original_l2c_fcr_chk_chan_modes);
388+ void *target = reinterpret_cast <void *>(base + chk_offset);
389+ hook_func (target, (void *) fake_l2c_fcr_chk_chan_modes,
390+ (void **) &original_l2c_fcr_chk_chan_modes);
407391 LOGI (" hooked chk" );
408392 }
409393
410394 if (sdp_offset) {
411- void * target = reinterpret_cast <void *>(base + sdp_offset);
412- hook_func (target,
413- (void *)fake_BTA_DmSetLocalDiRecord,
414- (void **)&original_BTA_DmSetLocalDiRecord);
395+ void *target = reinterpret_cast <void *>(base + sdp_offset);
396+ hook_func (target, (void *) fake_BTA_DmSetLocalDiRecord,
397+ (void **) &original_BTA_DmSetLocalDiRecord);
415398 LOGI (" hooked sdp" );
416399 }
417400
418401 return chk_offset || sdp_offset;
419402}
420403
421- static void on_library_loaded (const char * name, void *) {
404+ static void on_library_loaded (const char * name, void *) {
422405 LOGI (" on_library_loaded called with name: %s" , name);
423406
424407 if (strstr (name, " libbluetooth_jni.so" )) {
@@ -432,20 +415,19 @@ static void on_library_loaded(const char* name, void*) {
432415 }
433416}
434417
435- extern " C"
436- [[gnu::visibility(" default" )]]
418+ extern " C" [[gnu::visibility(" default" )]]
437419[[gnu::used]]
438- NativeOnModuleLoaded native_init (const NativeAPIEntries* entries) {
420+ NativeOnModuleLoaded native_init (const NativeAPIEntries * entries) {
439421 LOGI (" native_init called with entries: %p" , entries);
440- hook_func = (HookFunType)entries->hook_func ;
441- LOGI (" LibrePodsNativeHook initialized, sdp hook enabled: %d" , enableSdpHook.load (std::memory_order_relaxed));
422+ hook_func = (HookFunType) entries->hook_func ;
423+ LOGI (" LibrePodsNativeHook initialized, sdp hook enabled: %d" ,
424+ enableSdpHook.load (std::memory_order_relaxed));
442425 return on_library_loaded;
443426}
444427
445- extern " C"
446- JNIEXPORT void JNICALL
447- Java_me_kavishdevar_librepods_utils_NativeBridge_setSdpHook (
448- JNIEnv*, jobject thiz, jboolean enable) {
428+ extern " C" JNIEXPORT void JNICALL
429+ Java_me_kavishdevar_librepods_utils_NativeBridge_setSdpHook (JNIEnv *, jobject thiz,
430+ jboolean enable) {
449431 LOGI (" setSdpHook called with enable: %d" , enable);
450432 enableSdpHook.store (enable, std::memory_order_relaxed);
451433
0 commit comments