Skip to content

Commit

Permalink
chore: Use little endian for VarInt write
Browse files Browse the repository at this point in the history
  • Loading branch information
layou233 committed Sep 8, 2024
1 parent 1f44918 commit 76061d4
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions common/mcprotocol/varint.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@ func PutVarInt(bs []byte, n int32) (numWrite int) {
bs[0] = byte(num)
return 1
} else if num&0xFFFFC000 == 0 {
result := uint16((num&0x7F|0x80)<<8 | (num >> 7))
binary.BigEndian.PutUint16(bs, result)
result := uint16((num&0x7F | 0x80) | (num>>7)<<8)
binary.LittleEndian.PutUint16(bs, result)
return 2
} else if num&0xFFE00000 == 0 {
bs[2] = byte(num >> 14)
startingBytes := uint16((num&0x7F|0x80)<<8 | ((num>>7)&0x7F | 0x80))
binary.BigEndian.PutUint16(bs, startingBytes)
startingBytes := uint16((num&0x7F | 0x80) | ((num>>7)&0x7F|0x80)<<8)
binary.LittleEndian.PutUint16(bs, startingBytes)
return 3
} else if num&0xF0000000 == 0 {
result := (num&0x7F|0x80)<<24 | (((num>>7)&0x7F | 0x80) << 16) |
((num>>14)&0x7F|0x80)<<8 | (num >> 21)
binary.BigEndian.PutUint32(bs, result)
result := (num&0x7F | 0x80) | ((num>>7)&0x7F|0x80)<<8 |
((num>>14)&0x7F|0x80)<<16 | (num>>21)<<24
binary.LittleEndian.PutUint32(bs, result)
return 4
} else {
bs[4] = byte(num >> 28)
startingBytes := (num&0x7F|0x80)<<24 | ((num>>7)&0x7F|0x80)<<16 |
((num>>14)&0x7F|0x80)<<8 | ((num>>21)&0x7F | 0x80)
binary.BigEndian.PutUint32(bs, startingBytes)
startingBytes := (num&0x7F | 0x80) | ((num>>7)&0x7F|0x80)<<8 |
((num>>14)&0x7F|0x80)<<16 | ((num>>21)&0x7F|0x80)<<24
binary.LittleEndian.PutUint32(bs, startingBytes)
return 5
}
}
Expand Down

0 comments on commit 76061d4

Please sign in to comment.