ref: 17ede35bdd7237b9d3353d996fad3b1cf506fa5f
parent: fca3ac4c15e4691e230c2f90f8af71313cddc0a5
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jun 17 04:44:05 EDT 2017
Add host-order getters for ints. Ugly, but necessary for some file formats.
--- a/lib/std/getint.myr
+++ b/lib/std/getint.myr
@@ -1,14 +1,28 @@
+use "die"
+use "memops"
+
pkg std =
- generic getle64 : (buf : byte[:] -> @a::(numeric,integral))
- generic getbe64 : (buf : byte[:] -> @a::(numeric,integral))
- generic getle32 : (buf : byte[:] -> @a::(numeric,integral))
- generic getbe32 : (buf : byte[:] -> @a::(numeric,integral))
- generic getle16 : (buf : byte[:] -> @a::(numeric,integral))
- generic getbe16 : (buf : byte[:] -> @a::(numeric,integral))
- generic getle8 : (buf : byte[:] -> @a::(numeric,integral))
- generic getbe8 : (buf : byte[:] -> @a::(numeric,integral))
+ generic gethost64 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle64 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe64 : (buf : byte[:] -> @a::(numeric,integral))
+ generic gethost32 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle32 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe32 : (buf : byte[:] -> @a::(numeric,integral))
+ generic gethost16 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle16 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe16 : (buf : byte[:] -> @a::(numeric,integral))
+ generic gethost8 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle8 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe8 : (buf : byte[:] -> @a::(numeric,integral))
;;
+generic gethost64 = {buf -> @a::(numeric,integral)+ var val : int64
+ iassert(buf.len >= 8, "gethost64: index out of bounds")
+ memblit((&val : byte#), (buf : byte#), 8)
+ -> (val : @a::(numeric,integral))
+}
+
generic getbe64 = {buf -> @a::(numeric,integral)-> ((buf[0] : @a::(numeric,integral)) << 56) | \
((buf[1] : @a::(numeric,integral)) << 48) | \
@@ -31,6 +45,13 @@
((buf[7] : @a::(numeric,integral)) << 56)
}
+generic gethost32 = {buf -> @a::(numeric,integral)+ var val : int32
+ iassert(buf.len >= 4, "gethost32: index out of bounds")
+ memblit((&val : byte#), (buf : byte#), 4)
+ -> (val : @a::(numeric,integral))
+}
+
generic getbe32 = {buf-> ((buf[0] : @a::(numeric,integral)) << 24) | \
((buf[1] : @a::(numeric,integral)) << 16) | \
@@ -45,6 +66,13 @@
((buf[3] : @a::(numeric,integral)) << 24)
}
+generic gethost16 = {buf -> @a::(numeric,integral)+ var val : int16
+ iassert(buf.len >= 2, "gethost16: index out of bounds")
+ memblit((&val : byte#), (buf : byte#), 4)
+ -> (val : @a::(numeric,integral))
+}
+
generic getbe16 = {buf-> ((buf[0] : @a::(numeric,integral)) << 8) | \
((buf[1] : @a::(numeric,integral)) << 0)
@@ -53,6 +81,10 @@
generic getle16 = {buf-> ((buf[0] : @a::(numeric,integral)) << 0) | \
((buf[1] : @a::(numeric,integral)) << 8)
+}
+
+generic gethost8 = {buf+ -> (buf[0] : @a::(numeric,integral)) << 0
}
generic getbe8 = {buf--- a/lib/std/putint.myr
+++ b/lib/std/putint.myr
@@ -1,30 +1,39 @@
-use "assert"
+use "die"
+use "memops"
use "types"
pkg std =
- generic putle64 : (buf : byte[:], v : @a::(numeric,integral) -> size)
- generic putbe64 : (buf : byte[:], v : @a::(numeric,integral) -> size)
- generic putle32 : (buf : byte[:], v : @a::(numeric,integral) -> size)
- generic putbe32 : (buf : byte[:], v : @a::(numeric,integral) -> size)
- generic putle16 : (buf : byte[:], v : @a::(numeric,integral) -> size)
- generic putbe16 : (buf : byte[:], v : @a::(numeric,integral) -> size)
- generic putle8 : (buf : byte[:], v : @a::(numeric,integral) -> size)
- generic putbe8 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic puthost64 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putle64 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putbe64 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic puthost32 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putle32 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putbe32 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic puthost16 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putle16 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putbe16 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic puthost8 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putle8 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putbe8 : (buf : byte[:], v : @a::(numeric,integral) -> size)
;;
-generic putle64 = {buf, v; -> putle(buf, (v : uint64), 8)}-generic putbe64 = {buf, v; -> putbe(buf, (v : uint64), 8)}-generic putle32 = {buf, v; -> putle(buf, (v : uint64), 4)}-generic putbe32 = {buf, v; -> putbe(buf, (v : uint64), 4)}-generic putle16 = {buf, v; -> putle(buf, (v : uint64), 2)}-generic putbe16 = {buf, v; -> putbe(buf, (v : uint64), 2)}-generic putle8 = {buf, v; -> putle(buf, (v : uint64), 1)}-generic putbe8 = {buf, v; -> putbe(buf, (v : uint64), 1)}+generic puthost64 = {buf, v; -> puthost(buf, (v : uint64), 8)}+generic putle64 = {buf, v; -> putle(buf, (v : uint64), 8)}+generic putbe64 = {buf, v; -> putbe(buf, (v : uint64), 8)}+generic puthost32 = {buf, v; -> puthost(buf, (v : uint64), 4)}+generic putle32 = {buf, v; -> putle(buf, (v : uint64), 4)}+generic putbe32 = {buf, v; -> putbe(buf, (v : uint64), 4)}+generic puthost16 = {buf, v; -> puthost(buf, (v : uint64), 2)}+generic putle16 = {buf, v; -> putle(buf, (v : uint64), 2)}+generic putbe16 = {buf, v; -> putbe(buf, (v : uint64), 2)}+generic puthost8 = {buf, v; -> puthost(buf, (v : uint64), 1)}+generic putle8 = {buf, v; -> putle(buf, (v : uint64), 1)}+generic putbe8 = {buf, v; -> putbe(buf, (v : uint64), 1)} const putbe = {buf, val, nvar k
- assert(buf.len >= n, "buffer too small")
+ iassert(buf.len >= n, "buffer too small")
for var i = 0; i < n; i++
k = val >> (8*(n-i-1))
buf[i] = (k & 0xff: byte)
@@ -33,10 +42,16 @@
}
const putle = {buf, val, n- assert(buf.len >= n, "buffer too small")
+ iassert(buf.len >= n, "buffer too small")
for var i = 0; i < n; i++
buf[i] = (val & 0xff : byte)
val >>= 8
;;
+ -> (n : size)
+}
+
+const puthost = {buf, val, n+ iassert(buf.len >= n, "buffer too small")
+ memblit((buf : byte#), (&val : byte#), n)
-> (n : size)
}
--
⑨