Skip to content

Commit

Permalink
Merge pull request #1085 from wader/modernize
Browse files Browse the repository at this point in the history
go: Run modernize
  • Loading branch information
wader authored Feb 23, 2025
2 parents 2533680 + f5a4ea9 commit 9a5f54d
Show file tree
Hide file tree
Showing 54 changed files with 125 additions and 161 deletions.
2 changes: 1 addition & 1 deletion format/apple/macho/macho.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func machoDecode(d *decode.D) any {

d.SeekAbs(int64(symOff) * 8)
d.FieldArray("symbols", func(d *decode.D) {
for i := 0; i < int(nSyms); i++ {
for range int(nSyms) {
symbolTypeMap := scalar.UintMapSymStr{
0x0: "undef",
0x1: "abs",
Expand Down
2 changes: 1 addition & 1 deletion format/apple/macho/macho_fat.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func machoFatDecode(d *decode.D) any {

narchs := d.FieldU32("narchs")
d.FieldArray("archs", func(d *decode.D) {
for i := 0; i < int(narchs); i++ {
for range int(narchs) {
d.FieldStruct("arch", func(d *decode.D) {
// beware cputype and cpusubtype changes from ofile header to fat header
cpuType := d.FieldU32("cputype", cpuTypes, scalar.UintHex)
Expand Down
6 changes: 3 additions & 3 deletions format/bzip2/bzip2.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type bitFlipReader struct {

func (bfr bitFlipReader) Read(p []byte) (n int, err error) {
n, err = bfr.r.Read(p)
for i := 0; i < n; i++ {
for i := range n {
p[i] = bits.Reverse8(p[i])
}
return n, err
Expand Down Expand Up @@ -77,7 +77,7 @@ func bzip2Decode(d *decode.D) any {

d.SeekRel(-16)
ranges := 0
for i := 0; i < 16; i++ {
for range 16 {
if d.Bool() {
ranges++
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func bzip2Decode(d *decode.D) any {
// "It is important to note that none of the fields within a StreamBlock or StreamFooter are necessarily byte-aligned"
const footerByteSize = 10
compressedSize := (readCompressedSize - compressedStart) - footerByteSize*8
for i := 0; i < 8; i++ {
for range 8 {
d.SeekAbs(compressedStart + compressedSize)
if d.PeekUintBits(48) == footerMagic {
break
Expand Down
16 changes: 8 additions & 8 deletions format/elf/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func elfDecodeSymbolHashTable(d *decode.D) {

repeatFn := func(r int, fn func(d *decode.D)) func(d *decode.D) {
return func(d *decode.D) {
for i := 0; i < r; i++ {
for range r {
fn(d)
}
}
Expand All @@ -531,7 +531,7 @@ func elfDecodeSymbolHashTable(d *decode.D) {
}

func elfDecodeSymbolTable(d *decode.D, ec elfContext, nEntries int, strTab string) {
for i := 0; i < nEntries; i++ {
for range nEntries {
d.FieldStruct("symbol", func(d *decode.D) {
switch ec.archBits {
case 32:
Expand Down Expand Up @@ -566,7 +566,7 @@ func elfDecodeGNUHash(d *decode.D, ec elfContext, size int64, strTab string) {

repeatFn := func(r int, fn func(d *decode.D)) func(d *decode.D) {
return func(d *decode.D) {
for i := 0; i < r; i++ {
for range r {
fn(d)
}
}
Expand Down Expand Up @@ -631,7 +631,7 @@ type symbol struct {
func elfReadSymbolTable(d *decode.D, ec *elfContext, sh sectionHeader) []symbol {
var ss []symbol

for i := 0; i < int(sh.size/sh.entSize); i++ {
for range int(sh.size / sh.entSize) {
var name uint64
var value uint64
switch ec.archBits {
Expand Down Expand Up @@ -681,7 +681,7 @@ func readStrTab(d *decode.D, firstBit int64, nBytes int64) string {
}

func elfReadSectionHeaders(d *decode.D, ec *elfContext) {
for i := 0; i < ec.shNum; i++ {
for i := range ec.shNum {
d.SeekAbs(ec.shOff + int64(i)*ec.shEntSize)
var sh sectionHeader

Expand Down Expand Up @@ -924,7 +924,7 @@ func elfDecodeProgramHeader(d *decode.D, ec elfContext) {
}

func elfDecodeProgramHeaders(d *decode.D, ec elfContext) {
for i := 0; i < ec.phNum; i++ {
for i := range ec.phNum {
d.FieldStruct("program_header", func(d *decode.D) {
d.SeekAbs(ec.phOff + int64(i)*ec.phSize)
elfDecodeProgramHeader(d, ec)
Expand Down Expand Up @@ -972,7 +972,7 @@ func elfDecodeDynamicTag(d *decode.D, ec elfContext, dc dynamicContext) {
}

func elfDecodeDynamicTags(d *decode.D, ec elfContext, dc dynamicContext) {
for i := 0; i < dc.entries; i++ {
for range dc.entries {
d.FieldStruct("dynamic_tags", func(d *decode.D) {
elfDecodeDynamicTag(d, ec, dc)
})
Expand Down Expand Up @@ -1100,7 +1100,7 @@ func elfDecodeSectionHeader(d *decode.D, ec elfContext, sh sectionHeader) {
}

func elfDecodeSectionHeaders(d *decode.D, ec elfContext) {
for i := 0; i < ec.shNum; i++ {
for i := range ec.shNum {
d.SeekAbs(ec.shOff + int64(i)*ec.shEntSize)
d.FieldStruct("section_header", func(d *decode.D) {
elfDecodeSectionHeader(d, ec, ec.sections[i])
Expand Down
2 changes: 1 addition & 1 deletion format/fit/fit.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var fitCRCTable = [16]uint16{
func calcCRC(bytes []byte) uint16 {
var crc uint16
crc = 0
for i := 0; i < len(bytes); i++ {
for i := range bytes {
// compute checksum of lower four bits of byte
var checkByte = bytes[i]
var tmp = fitCRCTable[crc&0xf]
Expand Down
32 changes: 16 additions & 16 deletions format/flac/flac_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func frameDecode(d *decode.D) any {

var channelSamples [][]int64
d.FieldArray("subframes", func(d *decode.D) {
for channelIndex := 0; channelIndex < channels; channelIndex++ {
for channelIndex := range channels {
d.FieldStruct("subframe", func(d *decode.D) {
// <1> Zero bit padding, to prevent sync-fooling string of 1s
d.FieldU1("zero_bit", d.UintAssert(0))
Expand Down Expand Up @@ -387,7 +387,7 @@ func frameDecode(d *decode.D) any {
}

d.FieldArray("warmup_samples", func(d *decode.D) {
for i := 0; i < n; i++ {
for i := range n {
samples[i] = d.FieldS("value", sampleSize)
}
})
Expand Down Expand Up @@ -423,7 +423,7 @@ func frameDecode(d *decode.D) any {
d.FieldValueUint("rice_partitions", uint64(ricePartitions))

d.FieldArray("partitions", func(d *decode.D) {
for i := 0; i < ricePartitions; i++ {
for i := range ricePartitions {
d.FieldStruct("partition", func(d *decode.D) {
// Encoding parameter:
// <4(+5)> Encoding parameter:
Expand Down Expand Up @@ -468,14 +468,14 @@ func frameDecode(d *decode.D) any {
d.RangeFn(d.Pos(), int64(count*escapeSampleSize), func(d *decode.D) {
d.FieldRawLen("samples", int64(count*escapeSampleSize))
})
for j := 0; j < count; j++ {
for range count {
samples[n] = d.S(escapeSampleSize)
n++
}
}
} else {
samplesStart := d.Pos()
for j := 0; j < count; j++ {
for range count {
high := d.Unary(0)
low := d.U(riceParameter)
samples[n] = mathx.ZigZag[uint64, int64](high<<riceParameter | low)
Expand All @@ -495,7 +495,7 @@ func frameDecode(d *decode.D) any {
decodeLPC := func(lpcOrder int, samples []int64, coeffs []int64, shift int64) {
for i := lpcOrder; i < len(samples); i++ {
r := int64(0)
for j := 0; j < len(coeffs); j++ {
for j := range coeffs {
c := coeffs[j]
s := samples[i-j-1]
r += c * s
Expand All @@ -509,7 +509,7 @@ func frameDecode(d *decode.D) any {
case SubframeConstant:
// <n> Unencoded constant value of the subblock, n = frame's bits-per-sample.
v := d.FieldS("value", subframeSampleSize)
for i := 0; i < blockSize; i++ {
for i := range blockSize {
samples[i] = v
}
case SubframeVerbatim:
Expand All @@ -519,7 +519,7 @@ func frameDecode(d *decode.D) any {
d.FieldRawLen("samples", d.BitsLeft())
})

for i := 0; i < blockSize; i++ {
for i := range blockSize {
samples[i] = d.S(subframeSampleSize)
}
case SubframeFixed:
Expand Down Expand Up @@ -550,7 +550,7 @@ func frameDecode(d *decode.D) any {
// <n> Unencoded predictor coefficients (n = qlp coeff precision * lpc order) (NOTE: the coefficients are signed two's-complement).
var coeffs []int64
d.FieldArray("coefficients", func(d *decode.D) {
for i := 0; i < lpcOrder; i++ {
for range lpcOrder {
coeffs = append(coeffs, d.FieldS("value", precision))
}
})
Expand All @@ -560,7 +560,7 @@ func frameDecode(d *decode.D) any {
}

if wastedBitsK != 0 {
for i := 0; i < len(samples); i++ {
for i := range samples {
samples[i] <<= wastedBitsK
}
}
Expand All @@ -578,7 +578,7 @@ func frameDecode(d *decode.D) any {
d.FieldRawLen("footer_crc", 16, d.ValidateBitBuf(footerCRC.Sum(nil)), scalar.RawHex)

streamSamples := len(channelSamples[0])
for j := 0; j < len(channelSamples); j++ {
for j := range channelSamples {
if streamSamples > len(channelSamples[j]) {
d.Fatalf("different amount of samples in channels %d != %d", streamSamples, len(channelSamples[j]))
}
Expand All @@ -589,15 +589,15 @@ func frameDecode(d *decode.D) any {
// side = left - right
switch channelAssignment {
case ChannelLeftSide:
for i := 0; i < len(channelSamples[0]); i++ {
for i := range channelSamples[0] {
channelSamples[1][i] = channelSamples[0][i] - channelSamples[1][i]
}
case ChannelSideRight:
for i := 0; i < len(channelSamples[0]); i++ {
for i := range channelSamples[0] {
channelSamples[0][i] = channelSamples[1][i] + channelSamples[0][i]
}
case ChannelMidSide:
for i := 0; i < len(channelSamples[0]); i++ {
for i := range channelSamples[0] {
m := channelSamples[0][i]
s := channelSamples[1][i]
m = m<<1 | s&1
Expand All @@ -623,8 +623,8 @@ func frameDecode(d *decode.D) any {
}

// TODO: speedup by using more cache friendly memory layout for samples
for i := 0; i < streamSamples; i++ {
for j := 0; j < len(channelSamples); j++ {
for i := range streamSamples {
for j := range channelSamples {

s := channelSamples[j][i]
switch outSampleSize {
Expand Down
2 changes: 1 addition & 1 deletion format/gif/gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var extensionNames = scalar.UintMapSymStr{

func fieldColorMap(d *decode.D, name string, bitDepth int) {
d.FieldArray(name, func(d *decode.D) {
for i := 0; i < 1<<bitDepth; i++ {
for range 1 << bitDepth {
d.FieldArray("color", func(d *decode.D) {
d.FieldU8("r")
d.FieldU8("g")
Expand Down
5 changes: 3 additions & 2 deletions format/inet/flowsdecoder/flowsdecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/binary"
"fmt"
"net"
"slices"

"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/ip4defrag"
Expand Down Expand Up @@ -117,14 +118,14 @@ func (fd *Decoder) New(net, transport gopacket.Flow, tcp *layers.TCP, ac reassem
stream := &TCPConnection{
Client: &TCPDirection{
Endpoint: TCPEndpoint{
IP: append([]byte(nil), net.Src().Raw()...),
IP: slices.Clone(net.Src().Raw()),
Port: clientPort,
},
Buffer: &bytes.Buffer{},
},
Server: &TCPDirection{
Endpoint: TCPEndpoint{
IP: append([]byte(nil), net.Dst().Raw()...),
IP: slices.Clone(net.Dst().Raw()),
Port: serverPort,
},
Buffer: &bytes.Buffer{},
Expand Down
6 changes: 1 addition & 5 deletions format/inet/sll2_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ func decodeSLL2(d *decode.D) any {
d.FieldU32("interface_index")
arpHdrType := d.FieldU16("arphdr_type", arpHdrTypeMAp)
d.FieldU8("packet_type", sllPacketTypeMap)
addressLength := d.FieldU8("link_address_length", d.UintValidateRange(0, 8))
// "If there are more than 8 bytes, only the first 8 bytes are present"
if addressLength > 8 {
addressLength = 8
}
addressLength := min(d.FieldU8("link_address_length", d.UintValidateRange(0, 8)), 8)
// TODO: maybe skip padding and always read 8 bytes?
d.FieldU("link_address", int(addressLength)*8)
addressDiff := 8 - addressLength
Expand Down
2 changes: 1 addition & 1 deletion format/jpeg/jp2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func jp2cDecode(d *decode.D) any {
d.FieldU32("yto_siz")
cSiz := d.FieldU16("c_siz")
d.FieldArray("components", func(d *decode.D) {
for i := 0; i < int(cSiz); i++ {
for range int(cSiz) {
d.FieldStruct("component", func(d *decode.D) {
d.FieldU8("s_sizi")
d.FieldU8("xr_sizi")
Expand Down
2 changes: 1 addition & 1 deletion format/leveldb/leveldb_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func readKeyValueContents(
restartOffset = size*8 - (1+numRestarts)*uint32BitSize
d.SeekAbs(start + restartOffset)
d.FieldArray("restarts", func(d *decode.D) {
for i := 0; i < int(numRestarts); i++ {
for range int(numRestarts) {
d.FieldU32("restart")
}
})
Expand Down
6 changes: 3 additions & 3 deletions format/matroska/matroska.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func decodeLacingFn(d *decode.D, lacingType int, fn func(d *decode.D)) {
case lacingTypeXiph:
numLaces := int(d.FieldU8("num_laces"))
d.FieldArray("lace_sizes", func(d *decode.D) {
for i := 0; i < numLaces; i++ {
for range numLaces {
s := int64(d.FieldUintFn("lace_size", decodeXiphLaceSize))
laceSizes = append(laceSizes, s)
}
Expand All @@ -144,7 +144,7 @@ func decodeLacingFn(d *decode.D, lacingType int, fn func(d *decode.D)) {
d.FieldArray("lace_sizes", func(d *decode.D) {
s := int64(d.FieldUintFn("lace_size", decodeVint)) // first is unsigned, not ranged shifted
laceSizes = append(laceSizes, s)
for i := 0; i < numLaces-1; i++ {
for range numLaces - 1 {
d := int64(d.FieldUintFn("lace_size_delta", decodeRawVint))
// range shifting
switch {
Expand Down Expand Up @@ -178,7 +178,7 @@ func decodeLacingFn(d *decode.D, lacingType int, fn func(d *decode.D)) {
case lacingTypeFixed:
numLaces := int(d.FieldU8("num_laces"))
fixedSize := (d.BitsLeft() / 8) / int64(numLaces+1)
for i := 0; i < numLaces+1; i++ {
for range numLaces + 1 {
laceSizes = append(laceSizes, fixedSize)
}
default:
Expand Down
2 changes: 1 addition & 1 deletion format/mp3/mp3_frame_vbri.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func mp3FrameTagVBRIDecode(d *decode.D) any {
tocEntrySize := d.FieldU16("toc_entry_size", d.UintAssert(1, 2, 3, 4), scalar.UintDescription("Size per table entry"))
d.FieldU16("frame_per_entry", scalar.UintDescription("Frames per table entry"))
d.FieldArray("toc", func(d *decode.D) {
for i := 0; i < int(tocEntries); i++ {
for range int(tocEntries) {
d.FieldU("entry", int(tocEntrySize)*8)
}
})
Expand Down
2 changes: 1 addition & 1 deletion format/mp3/mp3_frame_xing.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func mp3FrameTagXingDecode(d *decode.D) any {
}
if tocPresent {
d.FieldArray("toc", func(d *decode.D) {
for i := 0; i < 100; i++ {
for range 100 {
d.FieldU8("entry")
}
})
Expand Down
4 changes: 2 additions & 2 deletions format/mp4/boxes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ func decodeBox(ctx *decodeContext, d *decode.D, typ string) {
// ISO-639-2/T as 3*5 bit integers - 0x60
d.FieldStrFn("language", func(d *decode.D) string {
s := ""
for i := 0; i < 3; i++ {
for range 3 {
s += fmt.Sprintf("%c", int(d.U5())+0x60)
}
return s
Expand Down Expand Up @@ -1832,7 +1832,7 @@ func decodeBox(ctx *decodeContext, d *decode.D, typ string) {
case "ulst":
nu := d.FieldU16("nu")
d.FieldArray("uids", func(d *decode.D) {
for i := 0; i < int(nu); i++ {
for range int(nu) {
d.FieldRawLen("uid", 128)
}
})
Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/avc_pps.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func avcPPSDecode(d *decode.D) any {
picScalingMatrixPresentFlag := d.FieldBool("pic_scaling_matrix_present_flag")
if picScalingMatrixPresentFlag {
d.FieldArray("pic_scaling_list", func(d *decode.D) {
for i := 0; i < 6; i++ {
for range 6 {
d.Bool()
}
})
Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/hevc_sps.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func profileLayerDecode(d *decode.D, prefix string, profilePresent bool, levelPr
generalProfileIdc := d.FieldU5(prefix + "profile_idc")
var generalProfileCompatibilityFlags [32]bool
d.FieldArray(prefix+"profile_compatibility_flags", func(d *decode.D) {
for j := 0; j < 32; j++ {
for j := range 32 {
generalProfileCompatibilityFlags[j] = d.FieldBool(prefix + "profile_compatibility_flag")
}
})
Expand Down
Loading

0 comments on commit 9a5f54d

Please sign in to comment.