1
0
Fork 0
mirror of https://github.com/Luzifer/sii.git synced 2024-12-21 00:21:15 +00:00
sii/t3nk/3nk.go
Knut Ahlers ab39609f45
Implement Save-Game Editor (#1)
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-12-28 13:06:28 +00:00

118 lines
3.5 KiB
Go

package t3nk
import (
"bytes"
"encoding/binary"
"io"
"math/rand"
"time"
"github.com/pkg/errors"
)
var (
// Key table entries were calculated from this formula:
// Key[i] = (((i shl 2) xor not i) shl 3) xor i
//
// Or simpler: Stole it from https://github.com/idma88/3nK_Transcode/blob/master/Source/SII_3nK_Transcoder.pas
keyTable = []byte{
0xf8, 0xd1, 0xaa, 0x83, 0x5c, 0x75, 0x0e, 0x27, 0xb0, 0x99, 0xe2, 0xcb, 0x14, 0x3d, 0x46, 0x6f,
0x68, 0x41, 0x3a, 0x13, 0xcc, 0xe5, 0x9e, 0xb7, 0x20, 0x09, 0x72, 0x5b, 0x84, 0xad, 0xd6, 0xff,
0xd8, 0xf1, 0x8a, 0xa3, 0x7c, 0x55, 0x2e, 0x07, 0x90, 0xb9, 0xc2, 0xeb, 0x34, 0x1d, 0x66, 0x4f,
0x48, 0x61, 0x1a, 0x33, 0xec, 0xc5, 0xbe, 0x97, 0x00, 0x29, 0x52, 0x7b, 0xa4, 0x8d, 0xf6, 0xdf,
0xb8, 0x91, 0xea, 0xc3, 0x1c, 0x35, 0x4e, 0x67, 0xf0, 0xd9, 0xa2, 0x8b, 0x54, 0x7d, 0x06, 0x2f,
0x28, 0x01, 0x7a, 0x53, 0x8c, 0xa5, 0xde, 0xf7, 0x60, 0x49, 0x32, 0x1b, 0xc4, 0xed, 0x96, 0xbf,
0x98, 0xb1, 0xca, 0xe3, 0x3c, 0x15, 0x6e, 0x47, 0xd0, 0xf9, 0x82, 0xab, 0x74, 0x5d, 0x26, 0x0f,
0x08, 0x21, 0x5a, 0x73, 0xac, 0x85, 0xfe, 0xd7, 0x40, 0x69, 0x12, 0x3b, 0xe4, 0xcd, 0xb6, 0x9f,
0x78, 0x51, 0x2a, 0x03, 0xdc, 0xf5, 0x8e, 0xa7, 0x30, 0x19, 0x62, 0x4b, 0x94, 0xbd, 0xc6, 0xef,
0xe8, 0xc1, 0xba, 0x93, 0x4c, 0x65, 0x1e, 0x37, 0xa0, 0x89, 0xf2, 0xdb, 0x04, 0x2d, 0x56, 0x7f,
0x58, 0x71, 0x0a, 0x23, 0xfc, 0xd5, 0xae, 0x87, 0x10, 0x39, 0x42, 0x6b, 0xb4, 0x9d, 0xe6, 0xcf,
0xc8, 0xe1, 0x9a, 0xb3, 0x6c, 0x45, 0x3e, 0x17, 0x80, 0xa9, 0xd2, 0xfb, 0x24, 0x0d, 0x76, 0x5f,
0x38, 0x11, 0x6a, 0x43, 0x9c, 0xb5, 0xce, 0xe7, 0x70, 0x59, 0x22, 0x0b, 0xd4, 0xfd, 0x86, 0xaf,
0xa8, 0x81, 0xfa, 0xd3, 0x0c, 0x25, 0x5e, 0x77, 0xe0, 0xc9, 0xb2, 0x9b, 0x44, 0x6d, 0x16, 0x3f,
0x18, 0x31, 0x4a, 0x63, 0xbc, 0x95, 0xee, 0xc7, 0x50, 0x79, 0x02, 0x2b, 0xf4, 0xdd, 0xa6, 0x8f,
0x88, 0xa1, 0xda, 0xf3, 0x2c, 0x05, 0x7e, 0x57, 0xc0, 0xe9, 0x92, 0xbb, 0x64, 0x4d, 0x36, 0x1f,
}
sii3nKSignature = uint32(0x014B6E33) // 3nK#01
)
type sii3nKHeader struct {
Signature uint32
UnkByte uint8
Seed uint8
}
func Decode(r io.Reader) (io.Reader, error) {
var hdr sii3nKHeader
if err := binary.Read(r, binary.LittleEndian, &hdr); err != nil {
return nil, errors.Wrap(err, "Unable to read header")
}
if hdr.Signature != sii3nKSignature {
return nil, errors.Errorf("Unexpected file signature: %d (expected %d)", hdr.Signature, sii3nKSignature)
}
var (
buf = new(bytes.Buffer)
tBuf = make([]byte, 16*1024) // Buffer size = 16K
)
for {
n, err := r.Read(tBuf)
if n > 0 {
buf.Write(transcode(tBuf, n, hdr.Seed)[:n])
}
if err != nil {
if err == io.EOF {
break
}
return nil, errors.Wrap(err, "Unable to read from input")
}
}
return buf, nil
}
func Encode(r io.Reader) (io.Reader, error) {
rand.Seed(time.Now().UnixNano())
var hdr = sii3nKHeader{
Signature: sii3nKSignature,
Seed: uint8(rand.Intn(256)),
}
var (
buf = new(bytes.Buffer)
tBuf = make([]byte, 16*1024) // Buffer size = 16K
)
if err := binary.Write(buf, binary.LittleEndian, hdr); err != nil {
return nil, errors.Wrap(err, "Unable to write header")
}
for {
n, err := r.Read(tBuf)
if n > 0 {
buf.Write(transcode(tBuf, n, hdr.Seed)[:n])
}
if err != nil {
if err == io.EOF {
break
}
return nil, errors.Wrap(err, "Unable to read from input")
}
}
return buf, nil
}
func transcode(in []byte, size int, seed uint8) []byte {
var out = make([]byte, len(in))
for i := 0; i < size; i++ {
out[i] = in[i] ^ keyTable[byte(seed+uint8(i))]
}
return out
}