Root/
Source at commit 1079 created 13 years 1 month ago. By meklort, began implimenting Bios disk changes. Code taken from biosfn.c | |
---|---|
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 | ␊ |
12 | int replace_patern(char* pattern, char* replacement, char* buffer, long buffer_size)␊ |
13 | {␊ |
14 | ␉long index = 0;␊ |
15 | ␉int num_replaced = 0;␊ |
16 | ␉// NOTE: patehrn and replacement are null terminated. This may change later␊ |
17 | ␉// If I need to replce null bytes␊ |
18 | ␉␊ |
19 | ␉if(!pattern || ␊ |
20 | ␉ !replacement ||␊ |
21 | ␉ !buffer ||␊ |
22 | ␉ strlen(pattern) != strlen(replacement)␊ |
23 | ␉ ) return 0;␊ |
24 | ␉␊ |
25 | ␉␊ |
26 | ␉while(index < buffer_size - strlen(pattern))␊ |
27 | ␉{␊ |
28 | ␉␉bool located = true;␊ |
29 | ␉␉int i = 0;␊ |
30 | ␉␉while(located && i < strlen(pattern))␊ |
31 | ␉␉{␊ |
32 | ␉␉␉if(pattern[i] != buffer[i + index]) located = false;␊ |
33 | ␉␉␉i++;␊ |
34 | ␉␉}␊ |
35 | ␉␉␊ |
36 | ␉␉if(located)␊ |
37 | ␉␉{␊ |
38 | ␉␉␉printf("Located patern\n");␊ |
39 | ␉␉␉index += strlen(pattern) - 1;␊ |
40 | ␉␉␉num_replaced++;␊ |
41 | ␉␉}␊ |
42 | ␉␉␊ |
43 | ␉␉index++;␊ |
44 | ␉}␊ |
45 | ␉␊ |
46 | ␉return num_replaced;␊ |
47 | }␊ |
48 | ␊ |
49 | ␊ |
50 | int replace_word(uint32_t pattern, uint32_t repalcement, char* buffer, long buffer_size)␊ |
51 | {␊ |
52 | ␉int num_replaced = 0;␊ |
53 | ␉char* tmp = buffer;␊ |
54 | ␉␊ |
55 | ␉if(!buffer || !buffer_size) return 0;␊ |
56 | ␉␊ |
57 | ␉␊ |
58 | ␉while(tmp < buffer + buffer_size - sizeof(uint32_t))␊ |
59 | ␉{␊ |
60 | ␊ |
61 | ␉␉uint32_t* current= (uint32_t*)tmp;␊ |
62 | ␉␉␊ |
63 | ␉␉if(*current == pattern)␊ |
64 | ␉␉{␊ |
65 | ␉␉␉*current = repalcement;␊ |
66 | ␉␉␉num_replaced++;␊ |
67 | ␉␉␉tmp += 4;␊ |
68 | ␉␉}␊ |
69 | ␉␉else ␊ |
70 | ␉␉{␊ |
71 | ␉␉␉tmp++;␊ |
72 | ␉␉}␊ |
73 | ␉}␊ |
74 | ␉␊ |
75 | ␉return num_replaced;␊ |
76 | }␊ |
77 | ␊ |
78 | void replace_string(char* find, const char* replace, char* string, int length)␊ |
79 | {␊ |
80 | ␉if(!find ||␊ |
81 | ␉ !replace ||␊ |
82 | ␉ !string ||␊ |
83 | ␉ !length ||␊ |
84 | ␉ strlen(find) != strlen(replace)) return;␊ |
85 | ␉␊ |
86 | ␉char* str = string;␊ |
87 | ␉while(length && strncmp(str, find, strlen(find)-1))␊ |
88 | ␉{␊ |
89 | ␉␉length--;␊ |
90 | ␉␉str++;␊ |
91 | ␉}␊ |
92 | ␉strncpy(str, replace, strlen(replace));␉// don't copy the null char␊ |
93 | }␊ |
94 | ␊ |
95 | void replace_bytes(char* find, int find_size, char* replace, int replace_size, char* exec, int length)␊ |
96 | {␊ |
97 | ␉if(!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␊ |
102 | ␉␉return;␊ |
103 | ␉␊ |
104 | ␉char* search = exec;␉␊ |
105 | ␉␊ |
106 | ␉␊ |
107 | ␉while(memcmp(search, find, find_size) != 0␊ |
108 | ␉␉ && ((search - exec) < length))␊ |
109 | ␉{␊ |
110 | ␉␉search++;␊ |
111 | ␉}␊ |
112 | ␊ |
113 | ␉if((search - exec) < length)␊ |
114 | ␉{␊ |
115 | ␉␉// Mem found, replace it␊ |
116 | ␉␉memcpy(search, replace, replace_size);␊ |
117 | ␉}␊ |
118 | }␊ |
119 | ␊ |
120 |