summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2021-10-09 16:37:51 -0400
committersiddharth <s@ricketyspace.net>2021-10-09 16:37:51 -0400
commitc55b79d72f7e2520e1f4bebd5877bbfa927feab7 (patch)
treeb2c0e17f52463d1f2c7b11756910690526eecb70 /lib
parentfafa79cc3198e980e60de0b23af7e7ad03e37aad (diff)
lib: add BytesToUint32s
Diffstat (limited to 'lib')
-rw-r--r--lib/byte.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/byte.go b/lib/byte.go
index 48f3a38..fbdfae9 100644
--- a/lib/byte.go
+++ b/lib/byte.go
@@ -43,3 +43,25 @@ func ByteIsUpper(b byte) bool {
func BytesEqual(a, b []byte) bool {
return BlocksEqual(a, b)
}
+
+func BytesToUint32s(bs []byte) []uint32 {
+ u32s := make([]uint32, 0)
+
+ ui := uint32(0) // 32-bit word.
+ ab := uint(32) // Available bits in ui.
+ for _, b := range bs {
+ if ab == 0 {
+ // ui full; add to u32s and reset ui.
+ u32s = append(u32s, ui)
+ ui = uint32(0)
+ ab = 32
+ }
+ // Stuff byte into ui.
+ ui = ui | uint32(b)<<(ab-8)
+ ab = ab - 8
+ }
+ if ui > 0 {
+ u32s = append(u32s, ui)
+ }
+ return u32s
+}