1515package main
1616
1717import (
18+ "bytes"
1819 "crypto/x509"
1920 "encoding/hex"
2021 "encoding/pem"
2122 "flag"
2223 "fmt"
2324 "io/ioutil"
2425 "os"
26+ "strconv"
2527 "strings"
2628 "unicode"
2729)
2830
29- var inPath = flag .String ("i" , "" , "input file to use (defaults to stdin)" )
30- var outPath = flag .String ("o" , "" , "output file to use (defaults to stdout)" )
31- var isPEM = flag .Bool ("pem" , false , "treat the input as PEM and decode the first PEM block" )
32- var isPEMAll = flag .Bool ("pem-all" , false , "treat the input as PEM and decode all PEM blocks" )
33- var pemPassword = flag .String ("pem-password" , "" , "password to use when decrypting PEM blocks" )
34- var isHex = flag .Bool ("hex" , false , "treat the input as hex, ignoring punctuation and whitespace" )
31+ var (
32+ inPath = flag .String ("i" , "" , "input file to use (defaults to stdin)" )
33+ outPath = flag .String ("o" , "" , "output file to use (defaults to stdout)" )
34+ isPEM = flag .Bool ("pem" , false , "treat the input as PEM and decode the first PEM block" )
35+ isPEMAll = flag .Bool ("pem-all" , false , "treat the input as PEM and decode all PEM blocks" )
36+ pemPassword = flag .String ("pem-password" , "" , "password to use when decrypting PEM blocks" )
37+ isHex = flag .Bool ("hex" , false , "treat the input as hex, ignoring punctuation and whitespace" )
38+ isArray = flag .Bool ("array" , false , "treat the input as a array of comma-separated integers" )
39+ )
3540
3641type input struct {
3742 comment string
3843 bytes []byte
3944}
4045
46+ func boolToInt (b bool ) int {
47+ if b {
48+ return 1
49+ } else {
50+ return 0
51+ }
52+ }
53+
4154func main () {
4255 flag .Parse ()
4356
@@ -47,6 +60,12 @@ func main() {
4760 os .Exit (1 )
4861 }
4962
63+ if boolToInt (* isPEM )+ boolToInt (* isPEMAll )+ boolToInt (* isHex )+ boolToInt (* isArray ) > 1 {
64+ fmt .Fprintf (os .Stderr , "At most one of -pem, -pem-all, -hex, and -array may be specified.\n " )
65+ flag .PrintDefaults ()
66+ os .Exit (1 )
67+ }
68+
5069 inFile := os .Stdin
5170 if * inPath != "" {
5271 var err error
@@ -121,6 +140,24 @@ func main() {
121140 os .Exit (1 )
122141 }
123142 inputs = []input {{bytes : inBytes }}
143+ } else if * isArray {
144+ // Trim whitespace and brackets.
145+ trimmed := strings .TrimFunc (string (inBytes ), func (r rune ) bool {
146+ return r == '[' || r == ']' || r == '{' || r == '}' || r == ',' || unicode .IsSpace (r )
147+ })
148+ nums := strings .Split (trimmed , "," )
149+ var buf bytes.Buffer
150+ buf .Grow (len (nums ))
151+ for _ , num := range nums {
152+ num = strings .TrimSpace (num )
153+ v , err := strconv .ParseUint (num , 0 , 8 )
154+ if err != nil {
155+ fmt .Fprintf (os .Stderr , "Error decoding array: %s\n " , err )
156+ os .Exit (1 )
157+ }
158+ buf .WriteByte (byte (v ))
159+ }
160+ inputs = []input {{bytes : buf .Bytes ()}}
124161 } else {
125162 inputs = []input {{bytes : inBytes }}
126163 }
0 commit comments