Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
func init() {
flag.Usage = func() {
fmt.Println("Usage: xaudiopatch [-p=patched] input.dll [...]")
flag.PrintDefaults()
}
}
var prefix = flag.String("p", "patched-", `Specify prefix for output file. Set to "", overwrite input.`)
func main() {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
flag.Usage()
return
}
for _, f := range args {
dir, file := filepath.Split(f)
err := patch(filepath.Join(dir, file), filepath.Join(dir, *prefix+file))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
var search = []byte{0x4D, 0x06, 0x9F, 0xF1, 0x2C, 0x08, 0x27, 0x4E, 0xBC, 0x73, 0x68, 0x82, 0xA1, 0xBB, 0x8E, 0x4C, 0x00, 0x00, 0x00, 0x00}
var replace = []byte{0x26, 0x0E, 0x87, 0xE4, 0xC5, 0x3C, 0xD2, 0x4C, 0xBA, 0x46, 0xCA, 0x0A, 0x9A, 0x70, 0xED, 0x04, 0x00, 0x00, 0x00, 0x00}
func patch(in string, out string) error {
fmt.Println("Patching", in)
b, err := ioutil.ReadFile(in)
if err != nil {
return err
}
c := bytes.Count(b, search)
fmt.Println("Matching byte sequences:", c, "(should be 1)")
if c != 1 {
fmt.Println("Skipping file...")
return nil
}
b = bytes.Replace(b, search, replace, -1)
fmt.Println("Writing to", out)
err = ioutil.WriteFile(out, b, 0)
if err != nil {
return err
}
return nil
}
/*
The MIT License (MIT)
Copyright (c) 2015 Klaus Post
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment