Chameleon

Chameleon Svn Source Tree

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

Source at commit 606 created 13 years 5 months ago.
By meklort, Kext patcher update. Can now patch both the gma950 binary and plist in mem. Will add error detections and device id detection to determine if a patch is required
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)
79{
80if(!find ||
81 !replace ||
82 !string ||
83 strlen(find) != strlen(replace)) return;
84
85char* str = string;
86while(strncmp(str, find, strlen(find)-1))
87{
88str++;
89}
90strncpy(str, replace, strlen(replace) - 1);// don't copy the null char
91}
92

Archive Download this file

Revision: 606