1
0
forked from MTSR/mapserver

testdata and parse impl

This commit is contained in:
Thomas Rudin 2019-01-05 21:35:20 +01:00
parent 0507d13d63
commit 9b4df6099d
16 changed files with 171 additions and 0 deletions

2
go.mod
View File

@ -1 +1,3 @@
module mapserver
require github.com/sirupsen/logrus v1.3.0

11
go.sum Normal file
View File

@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

125
mapblockparser/parse.go Normal file
View File

@ -0,0 +1,125 @@
package mapblockparser
import (
"errors"
"compress/zlib"
"bytes"
"io"
"strconv"
log "github.com/sirupsen/logrus"
)
type MapBlock struct {
Version byte
Underground bool
Mapdata []byte
}
func readU16(data []byte, offset int){
}
func readU32(data []byte, offset int){
}
type CountedReader struct {
reader io.Reader
Count int
}
func (r *CountedReader) Read(p []byte) (int, error){
i,err := r.reader.Read(p)
r.Count += i
return i,err
}
func (r *CountedReader) ReadByte() (byte, error){
r.Count++;
return r.reader.ReadByte()
}
func parseMapdata(mapblock *MapBlock, data []byte) (int, error) {
r := bytes.NewReader(data)
cr := new(CountedReader)
cr.reader = r
z, err := zlib.NewReader(cr)
if err != nil {
return 0, err
}
defer z.Close()
buf := new(bytes.Buffer)
io.Copy(buf, z)
if buf.Len() != 16384 {
return 0, errors.New("Mapdata length invalid: " + strconv.Itoa(buf.Len()))
}
mapblock.Mapdata = buf.Bytes()
return cr.Count, nil
}
func parseMetadata(mapblock *MapBlock, data []byte) (int, error) {
r := bytes.NewReader(data)
cr := new(CountedReader)
cr.reader = r
z, err := zlib.NewReader(cr)
if err != nil {
return 0, err
}
defer z.Close()
buf := new(bytes.Buffer)
io.Copy(buf, z)
log.Println("Metadata length ", buf.Len())
return cr.Count, nil
}
func Parse(data []byte) (*MapBlock, error) {
mapblock := MapBlock{}
if len(data) == 0 {
return nil, errors.New("no data")
}
log.Println("data-length: ", len(data))
offset := 0
// version
mapblock.Version = data[0]
//flags
flags := data[1]
mapblock.Underground = (flags & 0x01) == 0x01
//mapdata (blocks)
offset = 6
//metadata
count, err := parseMapdata(&mapblock, data[offset:])
if err != nil {
return nil, err
}
log.Println("Mapdata length: ", count)
offset += count
log.Println("New offset: ", offset)
count, err = parseMetadata(&mapblock, data[offset:])
if err != nil {
return nil, err
}
return &mapblock, nil
}

View File

@ -0,0 +1,33 @@
package mapblockparser
import (
"testing"
"io/ioutil"
"strconv"
mapblockparser "mapserver/mapblockparser"
)
func TestParse(t *testing.T){
data, err := ioutil.ReadFile("testdata/0.0.0")
if err != nil {
t.Error(err)
}
mapblock, err := mapblockparser.Parse(data)
if err != nil {
t.Error(err)
}
if mapblock.Version != 28 {
t.Error("wrong mapblock version: " + strconv.Itoa(int(mapblock.Version)))
}
if !mapblock.Underground {
t.Error("Underground flag")
}
if len(mapblock.Mapdata) != 16384 {
t.Error("Mapdata length wrong")
}
}

BIN
mapblockparser/testdata/0.-1.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.0.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.1.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.10.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.2.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.3.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.4.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.5.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.6.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.7.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.8.0 vendored Normal file

Binary file not shown.

BIN
mapblockparser/testdata/0.9.0 vendored Normal file

Binary file not shown.