-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package gopwn | ||
|
||
import ( | ||
"encoding/binary" | ||
) | ||
|
||
// P16L packs a uint16 into a byte slice in little endian format | ||
func P16L(i uint16) []byte { | ||
b := make([]byte, 2) | ||
binary.LittleEndian.PutUint16(b, i) | ||
return b | ||
} | ||
|
||
// P16L packs a uint16 into a byte slice in big endian format | ||
func P16B(i uint16) []byte { | ||
b := make([]byte, 2) | ||
binary.BigEndian.PutUint16(b, i) | ||
return b | ||
} | ||
|
||
// P32L packs a uint32 into a byte slice in little endian format | ||
func P32L(i uint32) []byte { | ||
b := make([]byte, 4) | ||
binary.LittleEndian.PutUint32(b, i) | ||
return b | ||
} | ||
|
||
// P32L packs a uint32 into a byte slice in big endian format | ||
func P32B(i uint32) []byte { | ||
b := make([]byte, 4) | ||
binary.BigEndian.PutUint32(b, i) | ||
return b | ||
} | ||
|
||
// P64L packs a uint64 into a byte slice in little endian format | ||
func P64L(i uint64) []byte { | ||
b := make([]byte, 8) | ||
binary.LittleEndian.PutUint64(b, i) | ||
return b | ||
} | ||
|
||
// P32L packs a uint32 into a byte slice in big endian format | ||
func P64B(i uint64) []byte { | ||
b := make([]byte, 8) | ||
binary.BigEndian.PutUint64(b, i) | ||
return b | ||
} | ||
|
||
// U16L unpacks a byte slice in little endian format into a uint16 | ||
func U16L(b []byte) uint16 { | ||
return binary.LittleEndian.Uint16(b) | ||
} | ||
|
||
// U16L unpacks a byte slice in big endian format into a uint16 | ||
func U16B(b []byte) uint16 { | ||
return binary.BigEndian.Uint16(b) | ||
} | ||
|
||
// U32L unpacks a byte slice in little endian format into a uint32 | ||
func U32L(b []byte) uint32 { | ||
return binary.LittleEndian.Uint32(b) | ||
} | ||
|
||
// U32L unpacks a byte slice in big endian format into a uint32 | ||
func U32B(b []byte) uint32 { | ||
return binary.BigEndian.Uint32(b) | ||
} | ||
|
||
// U64L unpacks a byte slice in little endian format into a uint64 | ||
func U64L(b []byte) uint64 { | ||
return binary.LittleEndian.Uint64(b) | ||
} | ||
|
||
// U64L unpacks a byte slice in big endian format into a uint64 | ||
func U64B(b []byte) uint64 { | ||
return binary.BigEndian.Uint64(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package gopwn | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPack(t *testing.T) { | ||
t.Run("16 LittleEndian", func(t *testing.T) { | ||
b := P16L(0xdead) | ||
assert.Equal(t, []byte("\xad\xde"), b) | ||
}) | ||
|
||
t.Run("16 BigEndian", func(t *testing.T) { | ||
b := P16B(0xdead) | ||
assert.Equal(t, []byte("\xde\xad"), b) | ||
}) | ||
|
||
t.Run("32 LittleEndian", func(t *testing.T) { | ||
b := P32L(0xdeadbeef) | ||
assert.Equal(t, []byte("\xef\xbe\xad\xde"), b) | ||
}) | ||
|
||
t.Run("32 BigEndian", func(t *testing.T) { | ||
b := P32B(0xdeadbeef) | ||
assert.Equal(t, []byte("\xde\xad\xbe\xef"), b) | ||
}) | ||
|
||
t.Run("64 LittleEndian", func(t *testing.T) { | ||
b := P64L(0xdeadbeef) | ||
assert.Equal(t, []byte("\xef\xbe\xad\xde\x00\x00\x00\x00"), b) | ||
}) | ||
|
||
t.Run("64 BigEndian", func(t *testing.T) { | ||
b := P64B(0xdeadbeef) | ||
assert.Equal(t, []byte("\x00\x00\x00\x00\xde\xad\xbe\xef"), b) | ||
}) | ||
} | ||
|
||
func TestUnPack(t *testing.T) { | ||
t.Run("16 LittleEndian", func(t *testing.T) { | ||
i := U16L([]byte("\xad\xde")) | ||
assert.Equal(t, uint16(0xdead), i) | ||
}) | ||
|
||
t.Run("16 BigEndian", func(t *testing.T) { | ||
i := U16B([]byte("\xde\xad")) | ||
assert.Equal(t, uint16(0xdead), i) | ||
}) | ||
|
||
t.Run("32 LittleEndian", func(t *testing.T) { | ||
i := U32L([]byte("\xef\xbe\xad\xde")) | ||
assert.Equal(t, uint32(0xdeadbeef), i) | ||
}) | ||
|
||
t.Run("32 BigEndian", func(t *testing.T) { | ||
i := U32B([]byte("\xde\xad\xbe\xef")) | ||
assert.Equal(t, uint32(0xdeadbeef), i) | ||
}) | ||
|
||
t.Run("64 LittleEndian", func(t *testing.T) { | ||
i := U64L([]byte("\xef\xbe\xad\xde\x00\x00\x00\x00")) | ||
assert.Equal(t, uint64(0xdeadbeef), i) | ||
}) | ||
|
||
t.Run("64 BigEndian", func(t *testing.T) { | ||
i := U64B([]byte("\x00\x00\x00\x00\xde\xad\xbe\xef")) | ||
assert.Equal(t, uint64(0xdeadbeef), i) | ||
}) | ||
} |