Skip to content

Commit

Permalink
major code rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
IlliaYalovoi committed Jan 20, 2024
1 parent 0fab822 commit 2005e59
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all: build-windows

# Not powershell compatible. Meant to be used from unix shell
build-windows:
env GOOS=windows GOARCH=amd64 go build -o ./build/universal-checksum-patcher.exe *.go
17 changes: 11 additions & 6 deletions hex.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package main

const limit = 14
const (
limit = 14
startLength = 3
endLength = 6
)

var (
start1 = []byte{0x48, 0x8B, 0x12}
start2 = []byte{0x48, 0x8D, 0x0D}
start3 = []byte{0x48, 0x8B, 0xD0}
end = []byte{0x85, 0xC0}
replacement = []byte{0x31, 0xC0}
start1 = []byte{0x48, 0x8B, 0x12}
start2 = []byte{0x48, 0x8D, 0x0D}
start3 = []byte{0x48, 0x8B, 0xD0}

end = []byte{0x85, 0xC0, 0x0F, 0x94, 0xC3, 0xE8}
replacement = []byte{0x31, 0xC0, 0x0F, 0x94, 0xC3, 0xE8}
)
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func main() {
}

fmt.Println("Press enter to exit")
fmt.Scanln()
_, _ = fmt.Scanln()
}
32 changes: 25 additions & 7 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,41 @@ func applyPatch(filename string) error {
return nil
}

func isStartCandidate(bytes []byte) bool {
return isByteSlicesEqual(bytes, start1) || isByteSlicesEqual(bytes, start2) || isByteSlicesEqual(bytes, start3)
}

func isEndCandidate(bytes []byte) bool {
return isByteSlicesEqual(bytes, end)
}

func modifyBytes(bytes []byte) error {
atLeastOnePatched := false

for i := 0; i < len(bytes); i++ {
if !isByteSlicesEqual(bytes[i:i+len(start1)], start1) &&
!isByteSlicesEqual(bytes[i:i+len(start2)], start2) &&
!isByteSlicesEqual(bytes[i:i+len(start3)], start3) {
if i > len(bytes)-limit {
break
}

if !isStartCandidate(bytes[i : i+startLength]) {
continue
}

for j := i + len(start1); j < i+len(start1)+limit && j < len(bytes); j++ {
if !isByteSlicesEqual(bytes[j:j+len(end)], end) {
for j := i + startLength; j < i+startLength+limit && j < len(bytes)-endLength; j++ {
if !isEndCandidate(bytes[j : j+endLength]) {
continue
}

copy(bytes[j:j+len(end)], replacement)
return nil
for k := 0; k < len(replacement); k++ {
bytes[j+k] = replacement[k]
}

atLeastOnePatched = true
}
}

if atLeastOnePatched {
return nil
}
return ErrNoMatch
}

0 comments on commit 2005e59

Please sign in to comment.