Chameleon

Chameleon Svn Source Tree

Root/branches/meklort/i386/modules/KextPatcher/hex_editor.c

Source at commit 647 created 9 years 6 months ago.
By meklort, Kext patcher update. Fixed bug with HDA patches, untested on 10.6.2 pre kexts. Fixed bug with gma 3150 kext, patch now works on all 10.6.x installs. I need to clean up the patcher / make an api still
1/*
2 * hex_editor.c
3 *
4 *
5 * Created by Meklort on 10/19/10.
6 * Copyright 2010 Evan Lojewski. All rights reserved.
7 *
8 */
9
10#include "hex_editor.h"
11
12int replace_patern(char* pattern, char* replacement, char* buffer, long buffer_size)
13{
14long index = 0;
15int num_replaced = 0;
16// NOTE: patehrn and replacement are null terminated. This may change later
17// If I need to replce null bytes
18
19if(!pattern ||
20 !replacement ||
21 !buffer ||
22 strlen(pattern) != strlen(replacement)
23 ) return 0;
24
25
26while(index < buffer_size - strlen(pattern))
27{
28bool located = true;
29int i = 0;
30while(located && i < strlen(pattern))
31{
32if(pattern[i] != buffer[i + index]) located = false;
33i++;
34}
35
36if(located)
37{
38printf("Located patern\n");
39index += strlen(pattern) - 1;
40num_replaced++;
41}
42
43index++;
44}
45
46return num_replaced;
47}
48
49
50int replace_word(uint32_t pattern, uint32_t repalcement, char* buffer, long buffer_size)
51{
52int num_replaced = 0;
53char* tmp = buffer;
54
55if(!buffer || !buffer_size) return 0;
56
57
58while(tmp < buffer + buffer_size - sizeof(uint32_t))
59{
60
61uint32_t* current= (uint32_t*)tmp;
62
63if(*current == pattern)
64{
65*current = repalcement;
66num_replaced++;
67tmp += 4;
68}
69else
70{
71tmp++;
72}
73}
74
75return num_replaced;
76}
77
78void replace_string(char* find, char* replace, char* string, int length)
79{
80if(!find ||
81 !replace ||
82 !string ||
83 !length ||
84 strlen(find) != strlen(replace)) return;
85
86char* str = string;
87while(length && strncmp(str, find, strlen(find)-1))
88{
89length--;
90str++;
91}
92strncpy(str, replace, strlen(replace));// don't copy the null char
93}
94
95void replace_bytes(char* find, int find_size, char* replace, int replace_size, char* exec, int length)
96{
97if(!find ||
98 !replace ||
99 !exec ||
100 !length ||
101 find_size > replace_size)// Allow find_size to be less than replace_size. Will overwrite bytes including and *after* located pattern
102return;
103
104char* search = exec;
105
106
107while(memcmp(search, find, find_size) != 0
108 && ((search - exec) < length))
109{
110search++;
111}
112
113if((search - exec) < length)
114{
115// Mem found, replace it
116memcpy(search, replace, replace_size);
117}
118}
119
120

Archive Download this file

Revision: 647