Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TaomeeDLLUnpack
package main
import (
"log"
"os"
)
var logErr = log.New(os.Stderr, "[Error]", 0)
package main
import (
"fmt"
"os"
"strconv"
)
// args
var (
srcFilePath string
startOffset int64
)
const defaultStartOffset = 7
func parseArgs() {
argN := len(os.Args)
switch argN {
case 1:
// print help
fmt.Println("")
fmt.Println("TaomeeDLLUnpack <file> [startOffset]")
fmt.Println("The result will be save to " + getSavePath("<file>"))
os.Exit(0)
case 2:
// TaomeeDLLUnpack <file>
startOffset = defaultStartOffset
case 3:
// TaomeeDLLUnpack <file> [startOffset]
startOffset, _ = strconv.ParseInt(os.Args[2], 10, 64)
default:
logErr.Fatalf("wrong %d arguments\n", argN)
os.Exit(1)
}
srcFilePath = os.Args[1]
}
func main() {
parseArgs()
saveFilePath := getSavePath(srcFilePath)
allowOverwrite := false
// check destination
if existFile(saveFilePath) {
// prompt for overwriting
fmt.Println("Destination File Already Exist.")
fmt.Println("Allow overwriting it? (true/false)")
fmt.Scanf("%t", &allowOverwrite)
// terminate if not allow overwriting
if !allowOverwrite {
fmt.Println("Aborted.")
os.Exit(1)
}
}
err := unpackFile(saveFilePath, srcFilePath, allowOverwrite)
if err != nil {
logErr.Fatalln(err.Error())
}else{
fmt.Println("Result has been saved to ", saveFilePath)
}
}
package main
import (
"compress/zlib"
"io"
"os"
)
func unpackFile(destFilePath, srcFilePath string, allowOverwrite bool) (err error) {
// open source
fSrc, err := os.Open(srcFilePath)
if err != nil {
return err
}
defer fSrc.Close()
// skip source head
_, err = fSrc.Seek(startOffset, io.SeekStart)
if err != nil {
return err
}
// open destination
var openMode = os.O_CREATE|os.O_EXCL
if allowOverwrite {
openMode = os.O_CREATE|os.O_TRUNC
}
fDst, err := os.OpenFile(destFilePath, openMode, 0755)
if err != nil {
return err
}
defer fDst.Close()
err = zlibUncompress(fDst, fSrc)
if err != nil {
fDst.Close()
if err := os.Remove(destFilePath); err != nil {
logErr.Println(err.Error())
}
}
return err
}
func zlibUncompress(dest io.Writer, src io.Reader) (err error) {
// zlib unpack
resultReader, err := zlib.NewReader(src)
if err != nil {
return err
}
// save result
_, err = io.Copy(dest, resultReader)
return err
}
func getSavePath(srcFilePath string) string {
return srcFilePath + "_unpack.swf"
}
func existFile(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.