1 Star 0 Fork 1

zscome/gopacket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gen.go 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2019 The GoPacket Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
// +build ignore
// This file generates LayersDecoder function for DecodingLayerContainer
// go run gen.go | gofmt > layers_decoder.go
package main
import (
"fmt"
"os"
"time"
)
const headerFmt = `// Copyright 2019 The GoPacket Authors. All rights reserved.
package gopacket
// Created by gen.go, don't edit manually
// Generated at %s
// LayersDecoder returns DecodingLayerFunc for specified
// DecodingLayerContainer, LayerType value to start decoding with and
// some DecodeFeedback.
func LayersDecoder(dl DecodingLayerContainer, first LayerType, df DecodeFeedback) DecodingLayerFunc {
firstDec, ok := dl.Decoder(first)
if !ok {
return func([]byte, *[]LayerType) (LayerType, error) {
return first, nil
}
}
`
var funcBody = `return func(data []byte, decoded *[]LayerType) (LayerType, error) {
*decoded = (*decoded)[:0] // Truncated decoded layers.
typ := first
decoder := firstDec
for {
if err := decoder.DecodeFromBytes(data, df); err != nil {
return LayerTypeZero, err
}
*decoded = append(*decoded, typ)
typ = decoder.NextLayerType()
if data = decoder.LayerPayload(); len(data) == 0 {
break
}
if decoder, ok = dlc.Decoder(typ); !ok {
return typ, nil
}
}
return LayerTypeZero, nil
}`
func main() {
fmt.Fprintf(os.Stderr, "Writing results to stdout\n")
types := []string{
"DecodingLayerSparse",
"DecodingLayerArray",
"DecodingLayerMap",
}
fmt.Printf(headerFmt, time.Now())
for _, t := range types {
fmt.Printf("if dlc, ok := dl.(%s); ok {", t)
fmt.Println(funcBody)
fmt.Println("}")
}
fmt.Println("dlc := dl")
fmt.Println(funcBody)
fmt.Println("}")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zsome/gopacket.git
git@gitee.com:zsome/gopacket.git
zsome
gopacket
gopacket
master

搜索帮助