github.com/gopacket/gopacket@v1.1.0/layers/gen2.go (about)

     1  // Copyright 2012 Google, Inc. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the LICENSE file in the root of the source
     5  // tree.
     6  
     7  //go:build ignore
     8  // +build ignore
     9  
    10  // This binary handles creating string constants and function templates for enums.
    11  //
    12  //	go run gen.go | gofmt > enums_generated.go
    13  package main
    14  
    15  import (
    16  	"fmt"
    17  	"log"
    18  	"os"
    19  	"text/template"
    20  	"time"
    21  )
    22  
    23  const fmtString = `// Copyright 2012 Google, Inc. All rights reserved.
    24  
    25  package layers
    26  
    27  // Created by gen2.go, don't edit manually
    28  // Generated at %s
    29  
    30  import (
    31    "fmt"
    32  
    33    "github.com/gopacket/gopacket"
    34  )
    35  
    36  `
    37  
    38  var funcsTmpl = template.Must(template.New("foo").Parse(`
    39  // Decoder calls {{.Name}}Metadata.DecodeWith's decoder.
    40  func (a {{.Name}}) Decode(data []byte, p gopacket.PacketBuilder) error {
    41  	if int(a) >= {{.Num}} {
    42  		errDecoder := errorDecoderFor{{.Name}}(a)
    43  		return &errDecoder
    44  	}
    45  
    46  	return {{.Name}}Metadata[a].DecodeWith.Decode(data, p)
    47  }
    48  // String returns {{.Name}}Metadata.Name.
    49  func (a {{.Name}}) String() string {
    50  	if int(a) >= {{.Num}} {
    51  		return "Unknown{{.Name}}"
    52  	}
    53  
    54  	return {{.Name}}Metadata[a].Name
    55  }
    56  // LayerType returns {{.Name}}Metadata.LayerType.
    57  func (a {{.Name}}) LayerType() gopacket.LayerType {
    58  	if int(a) >= {{.Num}} {
    59  		return 0
    60  	}
    61  
    62  	return {{.Name}}Metadata[a].LayerType
    63  }
    64  
    65  type errorDecoderFor{{.Name}} int
    66  func (a *errorDecoderFor{{.Name}}) Decode(data []byte, p gopacket.PacketBuilder) error {
    67    return a
    68  }
    69  func (a *errorDecoderFor{{.Name}}) Error() string {
    70    return fmt.Sprintf("Unable to decode {{.Name}} %d", int(*a))
    71  }
    72  
    73  var errorDecodersFor{{.Name}} [{{.Num}}]errorDecoderFor{{.Name}}
    74  var {{.Name}}Metadata [{{.Num}}]EnumMetadata
    75  
    76  func initUnknownTypesFor{{.Name}}() {
    77    for i := 0; i < {{.Num}}; i++ {
    78      errorDecodersFor{{.Name}}[i] = errorDecoderFor{{.Name}}(i)
    79      {{.Name}}Metadata[i] = EnumMetadata{
    80        DecodeWith: &errorDecodersFor{{.Name}}[i],
    81        Name: "Unknown{{.Name}}",
    82      }
    83    }
    84  }
    85  `))
    86  
    87  func main() {
    88  	fmt.Fprintf(os.Stderr, "Writing results to stdout\n")
    89  	fmt.Printf(fmtString, time.Now())
    90  	types := []struct {
    91  		Name string
    92  		Num  int
    93  	}{
    94  		{"LinkType", 277},
    95  		{"EthernetType", 65536},
    96  		{"PPPType", 65536},
    97  		{"IPProtocol", 256},
    98  		{"SCTPChunkType", 256},
    99  		{"PPPoECode", 256},
   100  		{"FDDIFrameControl", 256},
   101  		{"EAPOLType", 256},
   102  		{"ProtocolFamily", 256},
   103  		{"Dot11Type", 256},
   104  		{"USBTransportType", 256},
   105  	}
   106  
   107  	fmt.Println("func init() {")
   108  	for _, t := range types {
   109  		fmt.Printf("initUnknownTypesFor%s()\n", t.Name)
   110  	}
   111  	fmt.Println("initActualTypeData()")
   112  	fmt.Println("}")
   113  	for _, t := range types {
   114  		if err := funcsTmpl.Execute(os.Stdout, t); err != nil {
   115  			log.Fatalf("Failed to execute template %s: %v", t.Name, err)
   116  		}
   117  	}
   118  }