summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-01-22 23:44:59 +0700
committerShulhan <ms@kilabit.info>2025-01-23 00:01:35 +0700
commit7041153f3c1f464464cbf8faf7541821dc33e14d (patch)
tree1f765e305ed1a1377ff91af86c8665cf10155ae1
parent7f52c92fab6a49149643feeb9b3e5c06ef249ec1 (diff)
downloadpakakeh.go-7041153f3c1f464464cbf8faf7541821dc33e14d.tar.xz
all: replace [lib/bytes.WriteXxx] with standard library
The "encoding/binary.BigEndian" in standard library provides method PutXxx that do the same thing.
-rw-r--r--lib/bytes/bytes.go22
-rw-r--r--lib/bytes/bytes_example_test.go42
-rw-r--r--lib/dns/message.go29
-rw-r--r--lib/dns/message_header.go10
4 files changed, 21 insertions, 82 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index ce15e30a..ae1c9234 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -437,25 +437,3 @@ func WordIndexes(s []byte, word []byte) (idxs []int) {
return idxs
}
-
-// WriteUint16 write uint16 value "v" into "data" start at position "x".
-// If x is out range, the data will not change.
-func WriteUint16(data []byte, x uint, v uint16) {
- if x+1 >= uint(len(data)) {
- return
- }
- data[x] = byte(v >> 8)
- data[x+1] = byte(v)
-}
-
-// WriteUint32 write uint32 value into "data" start at position "x".
-// If x is out range, the data will not change.
-func WriteUint32(data []byte, x uint, v uint32) {
- if x+3 >= uint(len(data)) {
- return
- }
- data[x] = byte(v >> 24)
- data[x+1] = byte(v >> 16)
- data[x+2] = byte(v >> 8)
- data[x+3] = byte(v)
-}
diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go
index 670a28c6..86450ff2 100644
--- a/lib/bytes/bytes_example_test.go
+++ b/lib/bytes/bytes_example_test.go
@@ -265,45 +265,3 @@ func ExampleWordIndexes() {
// [0 11]
// []
}
-
-func ExampleWriteUint16() {
- data := []byte("Hello, world!")
-
- var v uint16
-
- v = 'h' << 8
- v |= 'E'
-
- libbytes.WriteUint16(data, uint(len(data)-1), v) // Index out of range
- fmt.Println(string(data))
-
- libbytes.WriteUint16(data, 0, v)
- fmt.Println(string(data))
- // Output:
- // Hello, world!
- // hEllo, world!
-}
-
-func ExampleWriteUint32() {
- data := []byte("Hello, world!")
-
- var v uint32
-
- v = 'h' << 24
- v |= 'E' << 16
- v |= 'L' << 8
- v |= 'L'
-
- libbytes.WriteUint32(data, uint(len(data)-1), v) // Index out of range
- fmt.Println(string(data))
-
- libbytes.WriteUint32(data, 0, v)
- fmt.Println(string(data))
-
- libbytes.WriteUint32(data, 7, v)
- fmt.Println(string(data))
- // Output:
- // Hello, world!
- // hELLo, world!
- // hELLo, hELLd!
-}
diff --git a/lib/dns/message.go b/lib/dns/message.go
index 2777ea58..d3d55b01 100644
--- a/lib/dns/message.go
+++ b/lib/dns/message.go
@@ -12,7 +12,6 @@ import (
"strings"
"git.sr.ht/~shulhan/pakakeh.go/lib/ascii"
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
libnet "git.sr.ht/~shulhan/pakakeh.go/lib/net"
"git.sr.ht/~shulhan/pakakeh.go/lib/reflect"
)
@@ -586,7 +585,7 @@ func (msg *Message) packTextAsDomain(rr *ResourceRecord) {
msg.packet = binary.BigEndian.AppendUint16(msg.packet, 0)
n = msg.packDomainName([]byte(rrText), true)
- libbytes.WriteUint16(msg.packet, off, uint16(n))
+ binary.BigEndian.PutUint16(msg.packet[off:], uint16(n))
}
func (msg *Message) packSOA(rr *ResourceRecord) {
@@ -617,7 +616,7 @@ func (msg *Message) packSOA(rr *ResourceRecord) {
total += 20
// Write rdlength.
- libbytes.WriteUint16(msg.packet, off, uint16(total))
+ binary.BigEndian.PutUint16(msg.packet[off:], uint16(total))
}
func (msg *Message) packWKS(rr *ResourceRecord) {
@@ -664,7 +663,7 @@ func (msg *Message) packMINFO(rr *ResourceRecord) {
n += msg.packDomainName([]byte(rrMInfo.EmailBox), true)
// Write rdlength.
- libbytes.WriteUint16(msg.packet, off, uint16(n))
+ binary.BigEndian.PutUint16(msg.packet[off:], uint16(n))
}
func (msg *Message) packMX(rr *ResourceRecord) {
@@ -685,7 +684,7 @@ func (msg *Message) packMX(rr *ResourceRecord) {
n = msg.packDomainName([]byte(rrMX.Exchange), true)
// Write rdlength.
- libbytes.WriteUint16(msg.packet, off, uint16(n+2))
+ binary.BigEndian.PutUint16(msg.packet[off:], uint16(n+2))
}
func (msg *Message) packTXT(rr *ResourceRecord) {
@@ -718,7 +717,7 @@ func (msg *Message) packSRV(rr *ResourceRecord) {
n = msg.packDomainName([]byte(rrSRV.Target), false) + 6
// Write rdlength.
- libbytes.WriteUint16(msg.packet, off, uint16(n))
+ binary.BigEndian.PutUint16(msg.packet[off:], uint16(n))
}
func (msg *Message) packAAAA(rr *ResourceRecord) {
@@ -765,7 +764,7 @@ func (msg *Message) packSVCB(rr *ResourceRecord) {
var n = svcb.pack(msg)
// Write rdlength.
- libbytes.WriteUint16(msg.packet, off, uint16(n))
+ binary.BigEndian.PutUint16(msg.packet[off:], uint16(n))
}
func (msg *Message) packHTTPS(rr *ResourceRecord) {
@@ -791,7 +790,7 @@ func (msg *Message) packHTTPS(rr *ResourceRecord) {
// In HTTPS (AliasMode), Params is ignored.
// Write rdlength.
- libbytes.WriteUint16(msg.packet, off, uint16(n))
+ binary.BigEndian.PutUint16(msg.packet[off:], uint16(n))
}
func (msg *Message) packIPv4(addr string) {
@@ -952,7 +951,7 @@ func (msg *Message) SetAuthorativeAnswer(isAA bool) {
func (msg *Message) SetID(id uint16) {
msg.Header.ID = id
if len(msg.packet) > 2 {
- libbytes.WriteUint16(msg.packet, 0, id)
+ binary.BigEndian.PutUint16(msg.packet, id)
}
}
@@ -1012,7 +1011,9 @@ func (msg *Message) SubTTL(n uint32) {
} else {
msg.Answer[x].TTL -= n
}
- libbytes.WriteUint32(msg.packet, uint(msg.Answer[x].idxTTL), msg.Answer[x].TTL)
+ binary.BigEndian.PutUint32(
+ msg.packet[msg.Answer[x].idxTTL:],
+ msg.Answer[x].TTL)
}
for x = 0; x < len(msg.Authority); x++ {
if msg.Authority[x].TTL < n {
@@ -1020,7 +1021,9 @@ func (msg *Message) SubTTL(n uint32) {
} else {
msg.Authority[x].TTL -= n
}
- libbytes.WriteUint32(msg.packet, uint(msg.Authority[x].idxTTL), msg.Authority[x].TTL)
+ binary.BigEndian.PutUint32(
+ msg.packet[msg.Authority[x].idxTTL:],
+ msg.Authority[x].TTL)
}
for x = 0; x < len(msg.Additional); x++ {
if msg.Additional[x].Type == RecordTypeOPT {
@@ -1031,7 +1034,9 @@ func (msg *Message) SubTTL(n uint32) {
} else {
msg.Additional[x].TTL -= n
}
- libbytes.WriteUint32(msg.packet, uint(msg.Additional[x].idxTTL), msg.Additional[x].TTL)
+ binary.BigEndian.PutUint32(
+ msg.packet[msg.Additional[x].idxTTL:],
+ msg.Additional[x].TTL)
}
}
diff --git a/lib/dns/message_header.go b/lib/dns/message_header.go
index 3329e7cf..fa9ca774 100644
--- a/lib/dns/message_header.go
+++ b/lib/dns/message_header.go
@@ -8,8 +8,6 @@ import (
"encoding/binary"
"errors"
"fmt"
-
- libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes"
)
const (
@@ -156,10 +154,10 @@ func (hdr *MessageHeader) pack() []byte {
packet[2] = b0
packet[3] = b1
- libbytes.WriteUint16(packet[:], 4, hdr.QDCount)
- libbytes.WriteUint16(packet[:], 6, hdr.ANCount)
- libbytes.WriteUint16(packet[:], 8, hdr.NSCount)
- libbytes.WriteUint16(packet[:], 10, hdr.ARCount)
+ binary.BigEndian.PutUint16(packet[4:], hdr.QDCount)
+ binary.BigEndian.PutUint16(packet[6:], hdr.ANCount)
+ binary.BigEndian.PutUint16(packet[8:], hdr.NSCount)
+ binary.BigEndian.PutUint16(packet[10:], hdr.ARCount)
return packet[:]
}