diff --git a/parse.go b/parse.go index 446d4f4..bd9c449 100644 --- a/parse.go +++ b/parse.go @@ -3,7 +3,7 @@ package mapparser import ( "encoding/binary" "errors" - "strconv" + "fmt" "github.com/minetest-go/types" ) @@ -46,11 +46,11 @@ func Parse(data []byte) (*types.MapBlock, error) { params_width := data[offset+1] if content_width != 2 { - return nil, errors.New("content_width = " + strconv.Itoa(int(content_width))) + return nil, fmt.Errorf("content_width unexpected: %d", content_width) } if params_width != 2 { - return nil, errors.New("params_width = " + strconv.Itoa(int(params_width))) + return nil, fmt.Errorf("params_width unexpected: %d", params_width) } // mapdata offset diff --git a/parse_block_mapping.go b/parse_block_mapping.go index 1d4f0b8..c8e1424 100644 --- a/parse_block_mapping.go +++ b/parse_block_mapping.go @@ -7,6 +7,11 @@ import ( ) func parseBlockMapping(data []byte, offset *int, mapblock *types.MapBlock) { + if *offset >= len(data) { + // eof + return + } + if len(data) > (*offset + 2) { // disk-data has per-block mapping, network-data has a global mapping numMappings := int(binary.BigEndian.Uint16(data[*offset:])) diff --git a/parse_static_objects.go b/parse_static_objects.go index 40aa3c6..23faecd 100644 --- a/parse_static_objects.go +++ b/parse_static_objects.go @@ -4,6 +4,12 @@ import "encoding/binary" func parseStaticObjects(data []byte, offset *int) { *offset++ //static objects version + + if *offset >= len(data) { + // eof + return + } + staticObjectsCount := int(binary.BigEndian.Uint16(data[*offset:])) *offset += 2 for i := 0; i < staticObjectsCount; i++ { diff --git a/parse_test.go b/parse_test.go index ae995db..ce57a4d 100644 --- a/parse_test.go +++ b/parse_test.go @@ -3,7 +3,7 @@ package mapparser import ( "encoding/json" "fmt" - "io/ioutil" + "os" "strconv" "testing" @@ -67,7 +67,7 @@ func validateMapblock(t *testing.T, mapblock *types.MapBlock) { func TestParse(t *testing.T) { - data, err := ioutil.ReadFile("testdata/0.0.0") + data, err := os.ReadFile("testdata/0.0.0") if err != nil { t.Error(err) } @@ -168,7 +168,7 @@ func TestParseError2(t *testing.T) { func TestParseZstd(t *testing.T) { - data, err := ioutil.ReadFile("testdata/zstd-block.bin") + data, err := os.ReadFile("testdata/zstd-block.bin") if err != nil { t.Error(err) } @@ -186,7 +186,7 @@ func TestParseZstd(t *testing.T) { func TestParse2(t *testing.T) { - data, err := ioutil.ReadFile("testdata/11.0.2") + data, err := os.ReadFile("testdata/11.0.2") if err != nil { t.Error(err) } @@ -210,7 +210,7 @@ func TestParse2(t *testing.T) { func TestParse3(t *testing.T) { - data, err := ioutil.ReadFile("testdata/0.1.0") + data, err := os.ReadFile("testdata/0.1.0") if err != nil { t.Error(err) } @@ -225,7 +225,7 @@ func TestParse3(t *testing.T) { } func TestParseMetadata(t *testing.T) { - data, err := ioutil.ReadFile("testdata/mb-with-metadata.bin") + data, err := os.ReadFile("testdata/mb-with-metadata.bin") if err != nil { t.Error(err) } @@ -244,7 +244,7 @@ func TestParseMetadata(t *testing.T) { } func TestParseNetworkBlock(t *testing.T) { - data, err := ioutil.ReadFile("testdata/network-blockdata.bin") + data, err := os.ReadFile("testdata/network-blockdata.bin") if err != nil { t.Error(err) } @@ -265,3 +265,22 @@ func TestParseNetworkBlock(t *testing.T) { } fmt.Println(string(str)) } + +func TestParseNetworkBlock2(t *testing.T) { + data, err := os.ReadFile("testdata/mapblock3516192727.bin") + if err != nil { + t.Error(err) + } + + mb, err := Parse(data) + + if err != nil { + t.Error(err) + } + + str, err := json.MarshalIndent(mb, "", " ") + if err != nil { + t.Error(err) + } + fmt.Println(string(str)) +} diff --git a/testdata/mapblock3516192727.bin b/testdata/mapblock3516192727.bin new file mode 100644 index 0000000..ae33a72 Binary files /dev/null and b/testdata/mapblock3516192727.bin differ