maplelib

package module
v0.0.0-...-57a8655 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2015 License: GPL-3.0 Imports: 3 Imported by: 0

README

Various Go utilities related to MapleStory (encryption, packets, and so on).

Support me!

Like my releases? Donate me a coffe!

Paypal: click

Litecoin: LUZm98D1nPhNQBw9QjkSS9XJee9X5hPjw3

Bitcoin: 15Jz8stcnkorzwCbUNk3qQbg2H9eySKXtb or Bitcoin QR Code

Dogecoin: DDaYKDUxib2SnVEk9trG98ajCyu1Hw9zgQ or Dogecoin QR Code

Getting started

Make sure that you have git and go installed and run

go get github.com/jteeuwen/go-pkg-xmlx
go get github.com/Francesco149/maplelib

You can also manually clone the repository anywhere you want by running

git clone https://github.com/Francesco149/maplelib.git

To verify that jteeuwen's xml library and my library are installed and working, run

go test github.com/jteeuwen/go-pkg-xmlx/...
go test github.com/Francesco149/maplelib/...

Examples

Building, encrypting, decrypting and decoding a packet:

package main

import (
	"fmt"
	"crypto/rand"
)
import "github.com/Francesco149/maplelib"

func main() {
	// initialize random initialization vector
	initializationRandomness := [4]byte{}
	rand.Read(initializationRandomness[:])

	// initialize crypto for maple v62
	crypt := maplelib.NewCrypt(initializationRandomness, 62)
	fmt.Println("crypt =", crypt)

	// build a new packet
	p := maplelib.NewPacket()
	p.Encode4(0x00000000) // placeholder for encrypted header
	p.Encode2(255)
	p.EncodeString("Hello world!")
	p.Encode4s(-5000)
	fmt.Println("p =", p)

	// encrypt the packet
	crypt.Encrypt([]byte(p))
	fmt.Println("encrypted p =", p)

	// get the original packet length from the encrypted packet
	// note: you would normally copy packetlen bytes from whatever source 
	// your packets are coming from to a new packet
	packetlen := maplelib.GetPacketLength(p)
	fmt.Println("decrypted length:", packetlen)

	// decrypt the packet
	// (note: you normally have to call .Shuffle after every encrypt/decrypt)
	p = p[4:] // skip first 4 bytes (encrypted header)
	crypt.Decrypt([]byte(p))
	fmt.Println("decrypted p =", p)

	// decode the stuff we encoded earlier
	it := p.Begin()

	word, err := it.Decode2()
	checkError(err)

	str, err := it.DecodeString()
	checkError(err)

	signed_dword, err := it.Decode4s()
	checkError(err)

	fmt.Println("word =", word, "\nstr =", str, "\nsigned_dword =", signed_dword)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

Reading wz xml files:

package main

import (
	"fmt"
	"path/filepath"
	"os"
)

import "github.com/Francesco149/maplelib/wz"

func main() {
	// path is just the full or relative path to your wz xml directory
	// I'm just retrieving my test xml files' directory for this example
	path := filepath.Join(os.Getenv("GOPATH"), "src", "github.com",
		"Francesco149", "maplelib", "wz", "testfiles")

	// opening the xml wz root folder
	x, err := wz.NewMapleDataProvider(path)
	checkError(err)

	// opening an img xml file (0003.img.xml)
	img, err := x.Get("TamingMob.wz/0003.img")
	checkError(err)

	// retrieving the INT info/speed value inside 0003.img.xml
	// NOTE: you should normally error check for nil on the returned pointer (val)
	val := wz.GetInt(img.ChildByPath("info/speed")) // returns a *int32
	fmt.Println("TamingMob.wz/0003.img.xml -> info -> speed =", *val)

	// retrieving the FLOAT info/swim value inside 0003.img.xml
	val2 := wz.GetFloat(img.ChildByPath("info/swim")) // return a *float32
	fmt.Println("TamingMob.wz/0003.img.xml -> info -> swim =", *val2)
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

Documentation

You can view the documentation as HTML by simply running

godoc -http=":6060"

and visiting

http://localhost:6060/pkg/github.com/Francesco149/maplelib/

Documentation

Overview

Package maplelib contains various go utilities related to MapleStory (encryption, packets, and so on)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Crc32

func Crc32(crc uint32, data []byte) uint32

Crc32 calculates the checksum for data using the initial checksum value crc. Maplestory's CRC32 uses the IEEE polynomial without mirroring.

func GetPacketLength

func GetPacketLength(encryptedHeader []byte) int

GetPacketLength decodes the packet length from the encrypted header. NOTE: this size does not include the 4-byte encrypted header.

Types

type Crypt

type Crypt struct {
	// contains filtered or unexported fields
}

A Crypt is an encryption key for MapleStory packets. It consists of a 4-byte value repeated four times for a total of 16 bytes.

MapleStory uses two keys: one is used to encrypt sent packets and the other is used to decrypt received packets. This is valid for both the client and the server. The two keys are randomly generated when a client connects and sent in the unencrypted handshake packet which is the first packet that is sent to a client that connects to a server. The MapleStory protocol will shift encryption keys every time a packet is send or received, so you will have to call .Shuffle() every time this happens.

func NewCrypt

func NewCrypt(key [4]byte, mapleVersion uint16) Crypt

NewCrypt initializes and returns an encryption key

func (*Crypt) Decrypt

func (c *Crypt) Decrypt(buffer []byte)

Decrypt decrypts the given array of bytes. NOTE: you must omit the first 4 bytes (encrypted header)

func (*Crypt) DecryptNoShanda

func (c *Crypt) DecryptNoShanda(buffer []byte)

DecryptNoShanda decrypts the given array of bytes only using maple's AES. NOTE: you must omit the first 4 bytes (encrypted header)

func (*Crypt) Encrypt

func (c *Crypt) Encrypt(buffer []byte)

Encrypt encrypts the given array of bytes. NOTE: the array must have 4 bytes of space at the beginning for the encrypted header

func (*Crypt) EncryptNoShanda

func (c *Crypt) EncryptNoShanda(buffer []byte)

EncryptNoShanda encrypts the given array of bytes only using maple's AES. NOTE: the array must have 4 bytes of space at the beginning for the encrypted header

func (*Crypt) IV

func (c *Crypt) IV() []byte

IV returns the current initialization vector of this key (16 bytes)

func (*Crypt) MapleVersion

func (c *Crypt) MapleVersion() uint16

MapleVersion returns the target MapleStory version for this encryption key

func (*Crypt) Shuffle

func (c *Crypt) Shuffle()

Shuffle shuffles the current key after an encryption or decryption

func (Crypt) String

func (c Crypt) String() string

type EndOfPacketError

type EndOfPacketError struct {
	// contains filtered or unexported fields
}

A EndOfPacketError is returned when trying to read past the end of the packet

func (EndOfPacketError) Error

func (e EndOfPacketError) Error() string

type Packet

type Packet []byte

A Packet is an array of bytes that contains a decrypted MapleStory packet. All of the numeric values are encoded in little endian.

func NewPacket

func NewPacket() Packet

NewPacket initializes an empty packet NOTE: do not create packets with make or new, as that will cause unexpected behaviour

func (*Packet) Append

func (p *Packet) Append(data []byte)

Append appends raw data at the end of the packet

func (Packet) At

func (p Packet) At(i int) PacketIterator

At returns a packet iterator that points at the desired position

func (Packet) Begin

func (p Packet) Begin() PacketIterator

Begin returns a packet iterator that points to the beginning of the packet

func (*Packet) Encode1

func (p *Packet) Encode1(b byte)

Encode1 encodes and appends a byte to the packet

func (*Packet) Encode1s

func (p *Packet) Encode1s(b int8)

func (*Packet) Encode2

func (p *Packet) Encode2(w uint16)

Encode2 encodes and appends a word to the packet

func (*Packet) Encode2s

func (p *Packet) Encode2s(b int16)

func (*Packet) Encode4

func (p *Packet) Encode4(dw uint32)

Encode4 encodes and appends a dword to the packet

func (*Packet) Encode4s

func (p *Packet) Encode4s(b int32)

func (*Packet) Encode8

func (p *Packet) Encode8(qw uint64)

Encode8 encodes and appends a qword to the packet

func (*Packet) Encode8s

func (p *Packet) Encode8s(b int64)

func (*Packet) EncodeBuffer

func (p *Packet) EncodeBuffer(b []byte)

EncodeBuffer encodes and appends a buffer to the packet using 2 bytes for the length followed by the data

func (*Packet) EncodeString

func (p *Packet) EncodeString(str string)

EncodeString encodes and appends a string to the packet using 2 bytes for the length followed by the text bytes

func (Packet) String

func (p Packet) String() string

type PacketIterator

type PacketIterator []byte

A PacketIterator is a slice of the packet array which is used as an iterator when reading values

func (*PacketIterator) Decode1

func (it *PacketIterator) Decode1() (res byte, err error)

Decode1 decodes a byte at the current position of the iterator which is then incremented

func (*PacketIterator) Decode1s

func (it *PacketIterator) Decode1s() (res int8, err error)

Decode1 with signed values

func (*PacketIterator) Decode2

func (it *PacketIterator) Decode2() (res uint16, err error)

Decode2 decodes a word (2 bytes) at the current position of the iterator which is then incremented

func (*PacketIterator) Decode2s

func (it *PacketIterator) Decode2s() (res int16, err error)

Decode2 with signed values

func (*PacketIterator) Decode4

func (it *PacketIterator) Decode4() (res uint32, err error)

Decode4 decodes a dword (4 bytes) at the current position of the iterator which is then incremented

func (*PacketIterator) Decode4s

func (it *PacketIterator) Decode4s() (res int32, err error)

Decode4 with signed values

func (*PacketIterator) Decode8

func (it *PacketIterator) Decode8() (res uint64, err error)

Decode8 decodes a qword (8 bytes) at the current position of the iterator which is then incremented

func (*PacketIterator) Decode8s

func (it *PacketIterator) Decode8s() (res int64, err error)

Decode8 with signed values

func (*PacketIterator) DecodeBuffer

func (it *PacketIterator) DecodeBuffer() (res []byte, err error)

DecodeBuffer decodes a buffer and returns a slice of the packet that points to the buffer NOTE: the returned slice is NOT a copy and any operation on it will affect the packet

func (*PacketIterator) DecodeString

func (it *PacketIterator) DecodeString() (res string, err error)

DecodeString decodes a string and returns it as a copy of the data

func (*PacketIterator) PopBytes

func (it *PacketIterator) PopBytes(count int) (res []byte, err error)

PopBytes returns a slice of the packet of count length starting from the iterator. NOTE: the returned slice is NOT a copy and any operation on it will affect the packet.

func (*PacketIterator) Skip

func (it *PacketIterator) Skip(count int) error

Skip moves the iterator forward by count bytes.

Directories

Path Synopsis
Package wz contains various utilities to parse Wz data 90% of this package is ported directly from OdinMS, so credits to them
Package wz contains various utilities to parse Wz data 90% of this package is ported directly from OdinMS, so credits to them

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL