@@ -592,28 +592,31 @@ class KernelPatcher {
592
592
}
593
593
594
594
/* *
595
- * Simple find and replace in kernel memory.
595
+ * Find one pattern with optional masking within a block of memory
596
+ *
597
+ * @param pattern pattern to search
598
+ * @param patternMask pattern mask
599
+ * @param patternSize size of pattern
600
+ * @param data a block of memory
601
+ * @param dataSize size of memory
602
+ * @param dataOffset data offset, to be set by this function
603
+ *
604
+ * @return true if pattern is found in data
596
605
*/
597
- static inline bool findAndReplace (void *data, size_t dataSize, const void *find, size_t findSize, const void *replace, size_t replaceSize) {
598
- void *res;
599
- if (UNLIKELY ((res = lilu_os_memmem (data, dataSize, find, findSize)) != nullptr )) {
600
- if (UNLIKELY (MachInfo::setKernelWriting (true , KernelPatcher::kernelWriteLock) != KERN_SUCCESS)) {
601
- SYSLOG (" patcher" , " failed to obtain write permissions for f/r" );
602
- return false ;
603
- }
606
+ EXPORT static bool findPattern (const void *pattern, const void *patternMask, size_t patternSize, const void *data, size_t dataSize, size_t *dataOffset);
604
607
605
- lilu_os_memcpy (res, replace, replaceSize);
606
-
607
- if (UNLIKELY (MachInfo::setKernelWriting (false , KernelPatcher::kernelWriteLock) != KERN_SUCCESS)) {
608
- SYSLOG (" patcher" , " failed to restore write permissions for f/r" );
609
- }
610
-
611
- return true ;
612
- }
608
+ /* *
609
+ * Simple find and replace with masking in kernel memory.
610
+ */
611
+ EXPORT static bool findAndReplaceWithMask (void *data, size_t dataSize, const void *find, size_t findSize, const void *findMask, size_t findMaskSize, const void *replace, size_t replaceSize, const void *replaceMask, size_t replaceMaskSize, size_t count=0 , size_t skip=0 );
613
612
614
- return false ;
613
+ /* *
614
+ * Simple find and replace in kernel memory.
615
+ */
616
+ static inline bool findAndReplace (void *data, size_t dataSize, const void *find, size_t findSize, const void *replace, size_t replaceSize) {
617
+ return findAndReplaceWithMask (data, dataSize, find, findSize, nullptr , 0 , replace, replaceSize, nullptr , 0 , 0 , 0 );
615
618
}
616
-
619
+
617
620
/* *
618
621
* Simple find and replace in kernel memory but require both `find` and `replace` buffers to have the same length
619
622
*/
@@ -622,6 +625,14 @@ class KernelPatcher {
622
625
return findAndReplace (data, dataSize, find, N, replace, N);
623
626
}
624
627
628
+ /* *
629
+ * Simple find and replace with masking in kernel memory but require both `find` and `replace` buffers and masking buffers to have the same length
630
+ */
631
+ template <size_t N>
632
+ static inline bool findAndReplaceWithMask (void *data, size_t dataSize, const uint8_t (&find)[N], const uint8_t (&findMask)[N], const uint8_t (&replace)[N], const uint8_t (&replaceMask)[N], size_t count, size_t skip) {
633
+ return findAndReplaceWithMask (data, dataSize, find, N, findMask, N, replace, N, replaceMask, N, count, skip);
634
+ }
635
+
625
636
private:
626
637
/* *
627
638
* Jump type for routing
@@ -707,6 +718,26 @@ class KernelPatcher {
707
718
*/
708
719
bool routeMultipleInternal (size_t id, RouteRequest *requests, size_t num, mach_vm_address_t start=0 , size_t size=0 , bool kernelRoute=true , bool force=false , JumpType jumpType=JumpType::Auto);
709
720
721
+ /* *
722
+ * Simple find and replace with masking in kernel memory
723
+ *
724
+ * @param data kernel memory
725
+ * @param dataSize size of kernel memory
726
+ * @param find find pattern
727
+ * @param findSize size of find pattern
728
+ * @param findMask find masking pattern
729
+ * @param findMaskSize size of find masking pattern
730
+ * @param replace replace pattern
731
+ * @param replaceSize size of replace pattern
732
+ * @param replaceMask replace masking pattern
733
+ * @param replaceMaskSize repalce masking pattern
734
+ * @param count maximum times of patching
735
+ * @param skip number of skipping times before performing replacement
736
+ *
737
+ * @return true if the finding and replacing performance is successful
738
+ */
739
+ static bool findAndReplaceWithMaskInternal (void *data, size_t dataSize, const void *find, size_t findSize, const void *findMask, size_t findMaskSize, const void *replace, size_t replaceSize, const void *replaceMask, size_t replaceMaskSize, size_t count, size_t skip);
740
+
710
741
#ifdef LILU_KEXTPATCH_SUPPORT
711
742
/* *
712
743
* Process loaded kext
0 commit comments