summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-10-14 21:30:49 +0700
committerShulhan <ms@kilabit.info>2021-10-14 21:30:49 +0700
commit2080593f8856863d8819062eede8ea59036b3299 (patch)
tree1d5b302f754c014506c0bdb97f471d4217fcc26e
parent8a0352321439e1fe10cfeeebf94372af9c9f7c04 (diff)
downloadpakakeh.go-2080593f8856863d8819062eede8ea59036b3299.tar.xz
lib/bytes: refactoring AppendXxx functions
Previously, we pass pointer to slice on AppendInt16, AppendInt32, AppendUint16, and AppendUint32 functions. This model of function signature is not a Go idiom. It is written when I am still new to Go.
-rw-r--r--lib/bytes/bytes.go44
-rw-r--r--lib/bytes/bytes_example_test.go65
-rw-r--r--lib/dns/dotclient.go2
-rw-r--r--lib/dns/message.go56
-rw-r--r--lib/dns/sectionheader.go8
-rw-r--r--lib/dns/tcpclient.go2
6 files changed, 123 insertions, 54 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index 6c1e6db8..f5a60f3c 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -13,39 +13,43 @@ import (
)
//
-// AppendInt16 into slice of byte.
+// AppendInt16 append an int16 value into slice of byte.
//
-func AppendInt16(data *[]byte, v int16) {
- *data = append(*data, byte(v>>8))
- *data = append(*data, byte(v))
+func AppendInt16(data []byte, v int16) []byte {
+ data = append(data, byte(v>>8))
+ data = append(data, byte(v))
+ return data
}
//
-// AppendInt32 into slice of byte.
+// AppendInt32 append an int32 value into slice of byte.
//
-func AppendInt32(data *[]byte, v int32) {
- *data = append(*data, byte(v>>24))
- *data = append(*data, byte(v>>16))
- *data = append(*data, byte(v>>8))
- *data = append(*data, byte(v))
+func AppendInt32(data []byte, v int32) []byte {
+ data = append(data, byte(v>>24))
+ data = append(data, byte(v>>16))
+ data = append(data, byte(v>>8))
+ data = append(data, byte(v))
+ return data
}
//
-// AppendUint16 into slice of byte.
+// AppendUint16 append an uint16 value into slice of byte.
//
-func AppendUint16(data *[]byte, v uint16) {
- *data = append(*data, byte(v>>8))
- *data = append(*data, byte(v))
+func AppendUint16(data []byte, v uint16) []byte {
+ data = append(data, byte(v>>8))
+ data = append(data, byte(v))
+ return data
}
//
-// AppendUint32 into slice of byte.
+// AppendUint32 append an uint32 value into slice of byte.
//
-func AppendUint32(data *[]byte, v uint32) {
- *data = append(*data, byte(v>>24))
- *data = append(*data, byte(v>>16))
- *data = append(*data, byte(v>>8))
- *data = append(*data, byte(v))
+func AppendUint32(data []byte, v uint32) []byte {
+ data = append(data, byte(v>>24))
+ data = append(data, byte(v>>16))
+ data = append(data, byte(v>>8))
+ data = append(data, byte(v))
+ return data
}
//
diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go
index dcb0e1ec..c1ef70bb 100644
--- a/lib/bytes/bytes_example_test.go
+++ b/lib/bytes/bytes_example_test.go
@@ -6,8 +6,73 @@ package bytes
import (
"fmt"
+ "math"
)
+func ExampleAppendInt16() {
+ for _, v := range []int16{math.MinInt16, 0xab, 0xabc, math.MaxInt16} {
+ out := AppendInt16([]byte{}, v)
+ fmt.Printf("%6d => %#04x => %#02v\n", v, v, out)
+ }
+ // Output:
+ // -32768 => -0x8000 => []byte{0x80, 0x00}
+ // 171 => 0x00ab => []byte{0x00, 0xab}
+ // 2748 => 0x0abc => []byte{0x0a, 0xbc}
+ // 32767 => 0x7fff => []byte{0x7f, 0xff}
+}
+
+func ExampleAppendInt32() {
+ for _, v := range []int32{math.MinInt32, 0xab, 0xabc, math.MaxInt32} {
+ out := AppendInt32([]byte{}, v)
+ fmt.Printf("%11d => %#x => %#v\n", v, v, out)
+ }
+ // Output:
+ // -2147483648 => -0x80000000 => []byte{0x80, 0x0, 0x0, 0x0}
+ // 171 => 0xab => []byte{0x0, 0x0, 0x0, 0xab}
+ // 2748 => 0xabc => []byte{0x0, 0x0, 0xa, 0xbc}
+ // 2147483647 => 0x7fffffff => []byte{0x7f, 0xff, 0xff, 0xff}
+}
+
+func ExampleAppendUint16() {
+ inputs := []uint16{0, 0xab, 0xabc, math.MaxInt16, math.MaxUint16}
+ for _, v := range inputs {
+ out := AppendUint16([]byte{}, v)
+ fmt.Printf("%5d => %#04x => %#02v\n", v, v, out)
+ }
+
+ v := inputs[4] + 1 // MaxUint16 + 1
+ out := AppendUint16([]byte{}, v)
+ fmt.Printf("%5d => %#04x => %#02v\n", v, v, out)
+
+ // Output:
+ // 0 => 0x0000 => []byte{0x00, 0x00}
+ // 171 => 0x00ab => []byte{0x00, 0xab}
+ // 2748 => 0x0abc => []byte{0x0a, 0xbc}
+ // 32767 => 0x7fff => []byte{0x7f, 0xff}
+ // 65535 => 0xffff => []byte{0xff, 0xff}
+ // 0 => 0x0000 => []byte{0x00, 0x00}
+}
+
+func ExampleAppendUint32() {
+ inputs := []uint32{0, 0xab, 0xabc, math.MaxInt32, math.MaxUint32}
+ for _, v := range inputs {
+ out := AppendUint32([]byte{}, v)
+ fmt.Printf("%11d => %#x => %#v\n", v, v, out)
+ }
+
+ v := inputs[4] + 2 // MaxUint32 + 2
+ out := AppendUint32([]byte{}, v)
+ fmt.Printf("%11d => %#x => %#v\n", v, v, out)
+
+ // Output:
+ // 0 => 0x0 => []byte{0x0, 0x0, 0x0, 0x0}
+ // 171 => 0xab => []byte{0x0, 0x0, 0x0, 0xab}
+ // 2748 => 0xabc => []byte{0x0, 0x0, 0xa, 0xbc}
+ // 2147483647 => 0x7fffffff => []byte{0x7f, 0xff, 0xff, 0xff}
+ // 4294967295 => 0xffffffff => []byte{0xff, 0xff, 0xff, 0xff}
+ // 1 => 0x1 => []byte{0x0, 0x0, 0x0, 0x1}
+}
+
func ExampleCutUntilToken() {
line := []byte(`abc \def ghi`)
diff --git a/lib/dns/dotclient.go b/lib/dns/dotclient.go
index ff2fc630..1399973e 100644
--- a/lib/dns/dotclient.go
+++ b/lib/dns/dotclient.go
@@ -170,7 +170,7 @@ func (cl *DoTClient) Write(msg []byte) (n int, err error) {
lenmsg := len(msg)
packet := make([]byte, 0, 2+lenmsg)
- libbytes.AppendUint16(&packet, uint16(lenmsg))
+ packet = libbytes.AppendUint16(packet, uint16(lenmsg))
packet = append(packet, msg...)
n, err = cl.conn.Write(packet)
diff --git a/lib/dns/message.go b/lib/dns/message.go
index d32f84f4..37d9c8a1 100644
--- a/lib/dns/message.go
+++ b/lib/dns/message.go
@@ -332,8 +332,8 @@ func (msg *Message) packDomainName(dname []byte, doCompress bool) (n int) {
func (msg *Message) packQuestion() {
msg.packDomainName([]byte(msg.Question.Name), false)
- libbytes.AppendUint16(&msg.packet, msg.Question.Type)
- libbytes.AppendUint16(&msg.packet, msg.Question.Class)
+ msg.packet = libbytes.AppendUint16(msg.packet, msg.Question.Type)
+ msg.packet = libbytes.AppendUint16(msg.packet, msg.Question.Class)
msg.off += 4
}
@@ -350,8 +350,8 @@ func (msg *Message) packRR(rr *ResourceRecord) {
msg.packDomainName([]byte(rr.Name), true)
}
- libbytes.AppendUint16(&msg.packet, rr.Type)
- libbytes.AppendUint16(&msg.packet, rr.Class)
+ msg.packet = libbytes.AppendUint16(msg.packet, rr.Type)
+ msg.packet = libbytes.AppendUint16(msg.packet, rr.Class)
msg.off += 4
if rr.Type == QueryTypeOPT {
@@ -367,7 +367,7 @@ func (msg *Message) packRR(rr *ResourceRecord) {
}
rr.offTTL = uint(msg.off)
- libbytes.AppendUint32(&msg.packet, rr.TTL)
+ msg.packet = libbytes.AppendUint32(msg.packet, rr.TTL)
msg.off += 4
msg.packRData(rr)
@@ -417,7 +417,7 @@ func (msg *Message) packRData(rr *ResourceRecord) {
}
func (msg *Message) packA(rr *ResourceRecord) {
- libbytes.AppendUint16(&msg.packet, rdataIPv4Size)
+ msg.packet = libbytes.AppendUint16(msg.packet, rdataIPv4Size)
msg.off += 2
rrText, _ := rr.Value.(string)
@@ -439,7 +439,7 @@ func (msg *Message) packA(rr *ResourceRecord) {
func (msg *Message) packTextAsDomain(rr *ResourceRecord) {
// Reserve two octets for rdlength
- libbytes.AppendUint16(&msg.packet, 0)
+ msg.packet = libbytes.AppendUint16(msg.packet, 0)
off := uint(msg.off)
msg.off += 2
@@ -451,7 +451,7 @@ func (msg *Message) packTextAsDomain(rr *ResourceRecord) {
func (msg *Message) packSOA(rr *ResourceRecord) {
// Reserve two octets for rdlength.
- libbytes.AppendUint16(&msg.packet, 0)
+ msg.packet = libbytes.AppendUint16(msg.packet, 0)
off := uint(msg.off)
msg.off += 2
@@ -460,11 +460,11 @@ func (msg *Message) packSOA(rr *ResourceRecord) {
n := msg.packDomainName([]byte(rrSOA.MName), true)
n += msg.packDomainName([]byte(rrSOA.RName), true)
- libbytes.AppendUint32(&msg.packet, rrSOA.Serial)
- libbytes.AppendInt32(&msg.packet, rrSOA.Refresh)
- libbytes.AppendInt32(&msg.packet, rrSOA.Retry)
- libbytes.AppendInt32(&msg.packet, rrSOA.Expire)
- libbytes.AppendUint32(&msg.packet, rrSOA.Minimum)
+ msg.packet = libbytes.AppendUint32(msg.packet, rrSOA.Serial)
+ msg.packet = libbytes.AppendInt32(msg.packet, rrSOA.Refresh)
+ msg.packet = libbytes.AppendInt32(msg.packet, rrSOA.Retry)
+ msg.packet = libbytes.AppendInt32(msg.packet, rrSOA.Expire)
+ msg.packet = libbytes.AppendUint32(msg.packet, rrSOA.Minimum)
// Write rdlength.
libbytes.WriteUint16(&msg.packet, off, uint16(n+20))
@@ -476,7 +476,7 @@ func (msg *Message) packWKS(rr *ResourceRecord) {
// Write rdlength.
n := uint16(5 + len(rrWKS.BitMap))
- libbytes.AppendUint16(&msg.packet, n)
+ msg.packet = libbytes.AppendUint16(msg.packet, n)
msg.off += 2
msg.packet = append(msg.packet, rrWKS.Address[:4]...)
@@ -491,7 +491,7 @@ func (msg *Message) packHINFO(rr *ResourceRecord) {
// Write rdlength.
n := len(rrHInfo.CPU)
n += len(rrHInfo.OS)
- libbytes.AppendUint16(&msg.packet, uint16(n))
+ msg.packet = libbytes.AppendUint16(msg.packet, uint16(n))
msg.off += 2
msg.packet = append(msg.packet, rrHInfo.CPU...)
msg.packet = append(msg.packet, rrHInfo.OS...)
@@ -503,7 +503,7 @@ func (msg *Message) packMINFO(rr *ResourceRecord) {
// Reserve two octets for rdlength.
off := uint(msg.off)
- libbytes.AppendUint16(&msg.packet, 0)
+ msg.packet = libbytes.AppendUint16(msg.packet, 0)
msg.off += 2
n := msg.packDomainName([]byte(rrMInfo.RMailBox), true)
@@ -518,10 +518,10 @@ func (msg *Message) packMX(rr *ResourceRecord) {
// Reserve two octets for rdlength.
off := uint(msg.off)
- libbytes.AppendUint16(&msg.packet, 0)
+ msg.packet = libbytes.AppendUint16(msg.packet, 0)
msg.off += 2
- libbytes.AppendInt16(&msg.packet, rrMX.Preference)
+ msg.packet = libbytes.AppendInt16(msg.packet, rrMX.Preference)
msg.off += 2
n := msg.packDomainName([]byte(rrMX.Exchange), true)
@@ -534,7 +534,7 @@ func (msg *Message) packTXT(rr *ResourceRecord) {
rrText, _ := rr.Value.(string)
n := uint16(len(rrText))
- libbytes.AppendUint16(&msg.packet, n+1)
+ msg.packet = libbytes.AppendUint16(msg.packet, n+1)
msg.off += 2
msg.packet = append(msg.packet, byte(n))
@@ -547,14 +547,14 @@ func (msg *Message) packSRV(rr *ResourceRecord) {
// Reserve two octets for rdlength
off := uint(msg.off)
- libbytes.AppendUint16(&msg.packet, 0)
+ msg.packet = libbytes.AppendUint16(msg.packet, 0)
msg.off += 2
- libbytes.AppendUint16(&msg.packet, rrSRV.Priority)
+ msg.packet = libbytes.AppendUint16(msg.packet, rrSRV.Priority)
msg.off += 2
- libbytes.AppendUint16(&msg.packet, rrSRV.Weight)
+ msg.packet = libbytes.AppendUint16(msg.packet, rrSRV.Weight)
msg.off += 2
- libbytes.AppendUint16(&msg.packet, rrSRV.Port)
+ msg.packet = libbytes.AppendUint16(msg.packet, rrSRV.Port)
msg.off += 2
n := msg.packDomainName([]byte(rrSRV.Target), false) + 6
@@ -566,7 +566,7 @@ func (msg *Message) packSRV(rr *ResourceRecord) {
func (msg *Message) packAAAA(rr *ResourceRecord) {
rrText, _ := rr.Value.(string)
- libbytes.AppendUint16(&msg.packet, rdataIPv6Size)
+ msg.packet = libbytes.AppendUint16(msg.packet, rdataIPv6Size)
msg.off += 2
ip := net.ParseIP(rrText)
@@ -585,7 +585,7 @@ func (msg *Message) packOPT(rr *ResourceRecord) {
// Reserve two octets for rdlength.
off := uint(msg.off)
- libbytes.AppendUint16(&msg.packet, 0)
+ msg.packet = libbytes.AppendUint16(msg.packet, 0)
msg.off += 2
if rrOPT.Length == 0 {
@@ -593,14 +593,14 @@ func (msg *Message) packOPT(rr *ResourceRecord) {
}
// Pack OPT rdata
- libbytes.AppendUint16(&msg.packet, rrOPT.Code)
+ msg.packet = libbytes.AppendUint16(msg.packet, rrOPT.Code)
// Values of less than 512 bytes MUST be treated as equal to 512
// bytes (RFC6891 P11).
if rrOPT.Length < 512 {
- libbytes.AppendUint16(&msg.packet, 512)
+ msg.packet = libbytes.AppendUint16(msg.packet, 512)
} else {
- libbytes.AppendUint16(&msg.packet, rrOPT.Length)
+ msg.packet = libbytes.AppendUint16(msg.packet, rrOPT.Length)
}
msg.packet = append(msg.packet, rrOPT.Data[:rrOPT.Length]...)
diff --git a/lib/dns/sectionheader.go b/lib/dns/sectionheader.go
index 7008a4a4..2844fcfb 100644
--- a/lib/dns/sectionheader.go
+++ b/lib/dns/sectionheader.go
@@ -156,10 +156,10 @@ func (hdr *SectionHeader) pack() []byte {
packet[2] = b0
packet[3] = b1
- libbytes.AppendUint16(&packet, hdr.QDCount)
- libbytes.AppendUint16(&packet, hdr.ANCount)
- libbytes.AppendUint16(&packet, hdr.NSCount)
- libbytes.AppendUint16(&packet, hdr.ARCount)
+ packet = libbytes.AppendUint16(packet, hdr.QDCount)
+ packet = libbytes.AppendUint16(packet, hdr.ANCount)
+ packet = libbytes.AppendUint16(packet, hdr.NSCount)
+ packet = libbytes.AppendUint16(packet, hdr.ARCount)
return packet
}
diff --git a/lib/dns/tcpclient.go b/lib/dns/tcpclient.go
index 477a3080..e88ad7d7 100644
--- a/lib/dns/tcpclient.go
+++ b/lib/dns/tcpclient.go
@@ -183,7 +183,7 @@ func (cl *TCPClient) Write(msg []byte) (n int, err error) {
lenmsg := len(msg)
packet := make([]byte, 0, 2+lenmsg)
- libbytes.AppendUint16(&packet, uint16(lenmsg))
+ packet = libbytes.AppendUint16(packet, uint16(lenmsg))
packet = append(packet, msg...)
n, err = cl.conn.Write(packet)