Skip to content

Commit

Permalink
Merge pull request #41 from saiko-tech/crc32
Browse files Browse the repository at this point in the history
implement Bsp.CRC32() - aka CRC_MapFile()
  • Loading branch information
Galaco authored Dec 9, 2021
2 parents 28c0900 + 78401ed commit 286dd90
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.bsp.gz filter=lfs diff=lfs merge=lfs -text
38 changes: 38 additions & 0 deletions crc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package bsp

import (
"hash/crc32"
"sort"
)

func (bsp *Bsp) CRC32() (uint32, error) {
const lumpCount = 64

crc := crc32.NewIEEE()

lumpList := make([]LumpId, lumpCount)
for i := 0; i < lumpCount; i++ {
lumpList[i] = LumpId(i)
}

sort.Slice(lumpList, func(i, j int) bool {
return bsp.header.Lumps[lumpList[i]].Offset < bsp.header.Lumps[lumpList[j]].Offset
})

for i := 0; i < lumpCount; i++ {
l := lumpList[i]
if l == LumpEntities {
continue
}

_, err := crc.Write(bsp.RawLump(l).raw)
if err != nil {
return 0, err
}
}

// see CRC32_Final
res := crc.Sum32() ^ 0xFFFFFFFF

return res, nil
}
37 changes: 37 additions & 0 deletions crc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package bsp_test

import (
"compress/gzip"
"os"
"testing"

"github.com/galaco/bsp"
)

func TestBsp_Crc(t *testing.T) {
f, err := os.Open("ar_baggage.bsp.gz")
if err != nil {
t.Error(err)
}

gzR, err := gzip.NewReader(f)
if err != nil {
t.Error(err)
}

bspF, err := bsp.ReadFromStream(gzR)
if err != nil {
t.Error(err)
}

res, err := bspF.CRC32()
if err != nil {
t.Error("unexpected error:", err)
}

const expected = 2836609078

if res != expected {
t.Errorf("CRC incorrect, expected %d got %d", expected, res)
}
}

0 comments on commit 286dd90

Please sign in to comment.