From 7f52c92fab6a49149643feeb9b3e5c06ef249ec1 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 22 Jan 2025 23:27:48 +0700 Subject: all: replace "lib/bytes.ReadXxx" with standard library Package "encoding/binary" from standard library support reading integer from bytes using BigEndian, LittleEndian variables. --- lib/bytes/bytes.go | 48 ----------------------------------------- lib/bytes/bytes_example_test.go | 40 ---------------------------------- lib/dns/message_header.go | 17 ++++++++------- lib/dns/message_question.go | 13 ++++++----- lib/dns/rdata_opt.go | 6 ++---- lib/dns/rdata_svcb.go | 22 +++++++++---------- lib/dns/resource_record.go | 34 ++++++++++++++--------------- 7 files changed, 44 insertions(+), 136 deletions(-) diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go index 94e7d402..ce15e30a 100644 --- a/lib/bytes/bytes.go +++ b/lib/bytes/bytes.go @@ -267,54 +267,6 @@ func ReadHexByte(data []byte, x int) (b byte, ok bool) { return b, true } -// ReadInt16 read int16 value from "data" start at index "x". -// It will return 0 if "x" is out of range. -func ReadInt16(data []byte, x uint) (v int16) { - if x+1 >= uint(len(data)) { - return 0 - } - v = int16(data[x]) << 8 - v |= int16(data[x+1]) - return v -} - -// ReadInt32 read int32 value from "data" start at index "x". -// It will return 0 if "x" is out of range. -func ReadInt32(data []byte, x uint) (v int32) { - if x+3 >= uint(len(data)) { - return 0 - } - v = int32(data[x]) << 24 - v |= int32(data[x+1]) << 16 - v |= int32(data[x+2]) << 8 - v |= int32(data[x+3]) - return v -} - -// ReadUint16 read uint16 value from "data" start at index "x". -// If x is out of range, it will return 0. -func ReadUint16(data []byte, x uint) (v uint16) { - if x+1 >= uint(len(data)) { - return 0 - } - v = uint16(data[x]) << 8 - v |= uint16(data[x+1]) - return v -} - -// ReadUint32 read uint32 value from "data" start at index "x". -// If x is out of range, it will return 0. -func ReadUint32(data []byte, x uint) (v uint32) { - if x+3 >= uint(len(data)) { - return 0 - } - v = uint32(data[x]) << 24 - v |= uint32(data[x+1]) << 16 - v |= uint32(data[x+2]) << 8 - v |= uint32(data[x+3]) - return v -} - // RemoveSpaces remove all spaces from input in. func RemoveSpaces(in []byte) (out []byte) { var c byte diff --git a/lib/bytes/bytes_example_test.go b/lib/bytes/bytes_example_test.go index 5ca7a1ea..670a28c6 100644 --- a/lib/bytes/bytes_example_test.go +++ b/lib/bytes/bytes_example_test.go @@ -168,46 +168,6 @@ func ExampleReadHexByte() { // 0 false } -func ExampleReadInt16() { - fmt.Println(libbytes.ReadInt16([]byte{0x01, 0x02, 0x03, 0x04}, 3)) // x is out of range. - fmt.Println(libbytes.ReadInt16([]byte{0x01, 0x02, 0x03, 0x04}, 0)) // 0x0102 - fmt.Println(libbytes.ReadInt16([]byte{0x01, 0x02, 0xf0, 0x04}, 2)) // 0xf004 - // Output: - // 0 - // 258 - // -4092 -} - -func ExampleReadInt32() { - fmt.Println(libbytes.ReadInt32([]byte{0x01, 0x02, 0x03, 0x04}, 1)) // x is out of range. - fmt.Println(libbytes.ReadInt32([]byte{0x01, 0x02, 0x03, 0x04}, 0)) // 0x01020304 - fmt.Println(libbytes.ReadInt32([]byte{0xf1, 0x02, 0x03, 0x04}, 0)) // 0xf1020304 - // Output: - // 0 - // 16909060 - // -251526396 -} - -func ExampleReadUint16() { - fmt.Println(libbytes.ReadUint16([]byte{0x01, 0x02, 0xf0, 0x04}, 3)) // x is out of range. - fmt.Println(libbytes.ReadUint16([]byte{0x01, 0x02, 0xf0, 0x04}, 0)) // 0x0102 - fmt.Println(libbytes.ReadUint16([]byte{0x01, 0x02, 0xf0, 0x04}, 2)) // 0xf004 - // Output: - // 0 - // 258 - // 61444 -} - -func ExampleReadUint32() { - fmt.Println(libbytes.ReadUint32([]byte{0x01, 0x02, 0x03, 0x04}, 1)) // x is out of range. - fmt.Println(libbytes.ReadUint32([]byte{0x01, 0x02, 0x03, 0x04}, 0)) // 0x01020304 - fmt.Println(libbytes.ReadUint32([]byte{0xf1, 0x02, 0x03, 0x04}, 0)) // 0xf1020304 - // Output: - // 0 - // 16909060 - // 4043440900 -} - func ExampleRemoveSpaces() { var ( in = []byte(" a\nb\tc d\r") diff --git a/lib/dns/message_header.go b/lib/dns/message_header.go index aa505aec..3329e7cf 100644 --- a/lib/dns/message_header.go +++ b/lib/dns/message_header.go @@ -1,10 +1,11 @@ -// Copyright 2018, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2018 M. Shulhan +// +// SPDX-License-Identifier: BSD-3-Clause package dns import ( + "encoding/binary" "errors" "fmt" @@ -177,7 +178,7 @@ func (hdr *MessageHeader) unpack(packet []byte) (err error) { return fmt.Errorf(`unknown response code=%d`, hdr.RCode) } - hdr.ID = libbytes.ReadUint16(packet, 0) + hdr.ID = binary.BigEndian.Uint16(packet) hdr.IsQuery = packet[2]&headerIsResponse != headerIsResponse hdr.IsAA = packet[2]&headerIsAA == headerIsAA @@ -185,10 +186,10 @@ func (hdr *MessageHeader) unpack(packet []byte) (err error) { hdr.IsRD = packet[2]&headerIsRD == headerIsRD hdr.IsRA = packet[3]&headerIsRA == headerIsRA - hdr.QDCount = libbytes.ReadUint16(packet, 4) - hdr.ANCount = libbytes.ReadUint16(packet, 6) - hdr.NSCount = libbytes.ReadUint16(packet, 8) - hdr.ARCount = libbytes.ReadUint16(packet, 10) + hdr.QDCount = binary.BigEndian.Uint16(packet[4:]) + hdr.ANCount = binary.BigEndian.Uint16(packet[6:]) + hdr.NSCount = binary.BigEndian.Uint16(packet[8:]) + hdr.ARCount = binary.BigEndian.Uint16(packet[10:]) return nil } diff --git a/lib/dns/message_question.go b/lib/dns/message_question.go index 8f24ed0a..b0be7018 100644 --- a/lib/dns/message_question.go +++ b/lib/dns/message_question.go @@ -1,14 +1,13 @@ -// Copyright 2018, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2018 M. Shulhan +// +// SPDX-License-Identifier: BSD-3-Clause package dns import ( + "encoding/binary" "fmt" "strings" - - libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes" ) // MessageQuestion contains the "question" in most queries. @@ -93,9 +92,9 @@ func (qst *MessageQuestion) unpack(packet []byte) (err error) { } qst.Name = sb.String() - qst.Type = RecordType(libbytes.ReadUint16(packet, uint(x))) + qst.Type = RecordType(binary.BigEndian.Uint16(packet[x:])) x += 2 - qst.Class = RecordClass(libbytes.ReadUint16(packet, uint(x))) + qst.Class = RecordClass(binary.BigEndian.Uint16(packet[x:])) return nil } diff --git a/lib/dns/rdata_opt.go b/lib/dns/rdata_opt.go index 15efcea6..771475cf 100644 --- a/lib/dns/rdata_opt.go +++ b/lib/dns/rdata_opt.go @@ -8,8 +8,6 @@ import ( "encoding/binary" "fmt" "strings" - - libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes" ) // RDataOPT define format of RDATA for OPT. @@ -70,10 +68,10 @@ func (opt *RDataOPT) unpack(rdlen int, packet []byte) (err error) { for x < rdlen { var optvar = RDataOPTVar{} - optvar.Code = libbytes.ReadUint16(packet, uint(x)) + optvar.Code = binary.BigEndian.Uint16(packet[x:]) x += 2 - var optlen = int(libbytes.ReadUint16(packet, uint(x))) + var optlen = int(binary.BigEndian.Uint16(packet[x:])) x += 2 if x+optlen > len(packet) { diff --git a/lib/dns/rdata_svcb.go b/lib/dns/rdata_svcb.go index c9c16d45..75099b55 100644 --- a/lib/dns/rdata_svcb.go +++ b/lib/dns/rdata_svcb.go @@ -15,8 +15,6 @@ import ( "sort" "strconv" "strings" - - libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes" ) // List of known parameter names for SVCB. @@ -560,7 +558,7 @@ func (svcb *RDataSVCB) parseParams(zp *zoneParser) (err error) { } func (svcb *RDataSVCB) unpack(packet, rdata []byte, start uint) (err error) { - svcb.Priority = libbytes.ReadUint16(rdata, 0) + svcb.Priority = binary.BigEndian.Uint16(rdata) rdata = rdata[2:] start += 2 @@ -583,7 +581,7 @@ func (svcb *RDataSVCB) unpackParams(packet []byte) (err error) { var keyid uint16 for len(packet) > 0 { - keyid = libbytes.ReadUint16(packet, 0) + keyid = binary.BigEndian.Uint16(packet) packet = packet[2:] switch int(keyid) { @@ -623,7 +621,7 @@ func (svcb *RDataSVCB) unpackParamMandatory(packet []byte) ([]byte, error) { return packet, errors.New(`missing mandatory key value`) } - var size = libbytes.ReadUint16(packet, 0) + var size = binary.BigEndian.Uint16(packet) if size <= 0 { return packet, fmt.Errorf(`invalid mandatory length %d`, size) } @@ -639,7 +637,7 @@ func (svcb *RDataSVCB) unpackParamMandatory(packet []byte) ([]byte, error) { return packet, fmt.Errorf(`missing mandatory value on index %d`, len(listValue)) } - var keyid = libbytes.ReadUint16(packet, 0) + var keyid = binary.BigEndian.Uint16(packet) packet = packet[2:] var keyName = svcbKeyName(int(keyid)) @@ -658,7 +656,7 @@ func (svcb *RDataSVCB) unpackParamALPN(packet []byte) ([]byte, error) { return packet, fmt.Errorf(`%s: missing length and value`, logp) } - var total = int(libbytes.ReadUint16(packet, 0)) + var total = int(binary.BigEndian.Uint16(packet)) if total <= 0 { return packet, fmt.Errorf(`%s: invalid length %d`, logp, total) } @@ -698,13 +696,13 @@ func (svcb *RDataSVCB) unpackParamPort(packet []byte) ([]byte, error) { return packet, fmt.Errorf(`%s: missing value`, logp) } - var u16 = libbytes.ReadUint16(packet, 0) + var u16 = binary.BigEndian.Uint16(packet) if u16 <= 0 { return packet, fmt.Errorf(`%s: invalid length %d`, logp, u16) } packet = packet[2:] - u16 = libbytes.ReadUint16(packet, 0) + u16 = binary.BigEndian.Uint16(packet) if u16 <= 0 { return packet, fmt.Errorf(`%s: invalid port %d`, logp, u16) } @@ -723,7 +721,7 @@ func (svcb *RDataSVCB) unpackParamIpv4hint(packet []byte) ([]byte, error) { return packet, fmt.Errorf(`%s: missing value`, logp) } - var size = int(libbytes.ReadUint16(packet, 0)) + var size = int(binary.BigEndian.Uint16(packet)) if size <= 0 { return nil, fmt.Errorf(`%s: invalid length %d`, logp, size) } @@ -754,7 +752,7 @@ func (svcb *RDataSVCB) unpackParamIpv6hint(packet []byte) ([]byte, error) { return packet, fmt.Errorf(`%s: missing value`, logp) } - var size = int(libbytes.ReadUint16(packet, 0)) + var size = int(binary.BigEndian.Uint16(packet)) if size <= 0 { return nil, fmt.Errorf(`%s: invalid length %d`, logp, size) } @@ -786,7 +784,7 @@ func (svcb *RDataSVCB) unpackParamGeneric(packet []byte, keyid int) ([]byte, err return nil, fmt.Errorf(`%s: missing parameter value`, logp) } - var size = int(libbytes.ReadUint16(packet, 0)) + var size = int(binary.BigEndian.Uint16(packet)) if size <= 0 { return packet, fmt.Errorf(`%s: invalid length %d`, logp, size) } diff --git a/lib/dns/resource_record.go b/lib/dns/resource_record.go index c0c04b23..d7762e9a 100644 --- a/lib/dns/resource_record.go +++ b/lib/dns/resource_record.go @@ -1,16 +1,16 @@ -// Copyright 2018, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// SPDX-FileCopyrightText: 2018 M. Shulhan +// +// SPDX-License-Identifier: BSD-3-Clause package dns import ( + "encoding/binary" "fmt" "log" "net" "strings" - libbytes "git.sr.ht/~shulhan/pakakeh.go/lib/bytes" libnet "git.sr.ht/~shulhan/pakakeh.go/lib/net" ) @@ -196,14 +196,14 @@ func (rr *ResourceRecord) unpack(packet []byte, startIdx uint) (x uint, err erro return x, fmt.Errorf("%s: %w", logp, err) } - rr.Type = RecordType(libbytes.ReadUint16(packet, x)) + rr.Type = RecordType(binary.BigEndian.Uint16(packet[x:])) x += 2 - rr.Class = RecordClass(libbytes.ReadUint16(packet, x)) + rr.Class = RecordClass(binary.BigEndian.Uint16(packet[x:])) x += 2 rr.idxTTL = uint16(x) - rr.TTL = libbytes.ReadUint32(packet, x) + rr.TTL = binary.BigEndian.Uint32(packet[x:]) x += 4 - rr.rdlen = libbytes.ReadUint16(packet, x) + rr.rdlen = binary.BigEndian.Uint16(packet[x:]) x += 2 var lenXRdata = x + uint(rr.rdlen) @@ -462,7 +462,7 @@ func (rr *ResourceRecord) unpackMX(packet []byte, startIdx uint) (err error) { rr.Value = rrMX - rrMX.Preference = libbytes.ReadInt16(packet, startIdx) + rrMX.Preference = int16(binary.BigEndian.Uint16(packet[startIdx:])) rrMX.Exchange, _, err = unpackDomainName(packet, startIdx+2) @@ -498,11 +498,11 @@ func (rr *ResourceRecord) unpackSRV(packet []byte, x uint) (err error) { rrSRV.Name = rr.Name[y:] // Unpack RDATA - rrSRV.Priority = libbytes.ReadUint16(packet, x) + rrSRV.Priority = binary.BigEndian.Uint16(packet[x:]) x += 2 - rrSRV.Weight = libbytes.ReadUint16(packet, x) + rrSRV.Weight = binary.BigEndian.Uint16(packet[x:]) x += 2 - rrSRV.Port = libbytes.ReadUint16(packet, x) + rrSRV.Port = binary.BigEndian.Uint16(packet[x:]) x += 2 rrSRV.Target, _, err = unpackDomainName(packet, x) @@ -603,15 +603,15 @@ func (rr *ResourceRecord) unpackSOA(packet []byte, startIdx uint) (err error) { x += uint(len(rrSOA.RName) + 2) } - rrSOA.Serial = libbytes.ReadUint32(packet, x) + rrSOA.Serial = binary.BigEndian.Uint32(packet[x:]) x += 4 - rrSOA.Refresh = libbytes.ReadInt32(packet, x) + rrSOA.Refresh = int32(binary.BigEndian.Uint32(packet[x:])) x += 4 - rrSOA.Retry = libbytes.ReadInt32(packet, x) + rrSOA.Retry = int32(binary.BigEndian.Uint32(packet[x:])) x += 4 - rrSOA.Expire = libbytes.ReadInt32(packet, x) + rrSOA.Expire = int32(binary.BigEndian.Uint32(packet[x:])) x += 4 - rrSOA.Minimum = libbytes.ReadUint32(packet, x) + rrSOA.Minimum = binary.BigEndian.Uint32(packet[x:]) return nil } -- cgit v1.3