-
Notifications
You must be signed in to change notification settings - Fork 2
/
format.go
145 lines (124 loc) · 3.38 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package gonut
import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"math"
)
type FormatTemplate struct {
Data []byte
}
func (f *FormatTemplate) ToBinary() []byte {
return f.Data
}
func (f *FormatTemplate) ToBase64() []byte {
return []byte(base64.StdEncoding.EncodeToString(f.Data))
}
func (f *FormatTemplate) ToHex() []byte {
return []byte(hex.EncodeToString(f.Data))
}
func (f *FormatTemplate) ToRubyC() []byte {
buffer := bytes.NewBufferString("unsigned char buf[] = ")
rows := Convert1d2d(f.Data, 16)
for _, row := range rows {
buffer.WriteString("\n\"")
for _, c := range row {
buffer.WriteString(fmt.Sprintf("\\x%02x", c))
}
buffer.WriteString("\"")
}
buffer.WriteString(";\n")
return buffer.Bytes()
}
func (f *FormatTemplate) ToPython() []byte {
buffer := bytes.NewBufferString("buf = b\"\"")
rows := Convert1d2d(f.Data, 12)
for _, row := range rows {
buffer.WriteString("\nbuf += b\"")
for _, c := range row {
buffer.WriteString(fmt.Sprintf("\\x%02x", c))
}
buffer.WriteString("\"")
}
buffer.WriteString("\n")
return buffer.Bytes()
}
func (f *FormatTemplate) ToPowerShell() []byte {
buffer := bytes.NewBufferString("[Byte[]] $buf = ")
for _, c := range f.Data {
buffer.WriteString(fmt.Sprintf("0x%02x,", c))
}
buffer.Bytes()[buffer.Len()-1] = '\n'
return buffer.Bytes()
}
func (f *FormatTemplate) ToCSharp() []byte {
buffer := bytes.NewBufferString(fmt.Sprintf("byte[] buf = new byte[%d] {", len(f.Data)))
rows := Convert1d2d(f.Data, 12)
for _, row := range rows {
buffer.WriteString("\n")
for _, c := range row {
buffer.WriteString(fmt.Sprintf("0x%02x,", c))
}
}
buffer.Bytes()[buffer.Len()-1] = '}'
buffer.WriteString(";\n")
return buffer.Bytes()
}
func (f *FormatTemplate) ToGolang() []byte {
buffer := bytes.NewBufferString(fmt.Sprintf("buf := []byte{"))
rows := Convert1d2d(f.Data, 12)
for _, row := range rows {
buffer.WriteString("\n")
for _, c := range row {
buffer.WriteString(fmt.Sprintf("0x%02x,", c))
}
}
buffer.WriteString("\n}\n")
return buffer.Bytes()
}
func (f *FormatTemplate) ToRust() []byte {
buffer := bytes.NewBufferString(fmt.Sprintf("let buf: [u8; %d] = [", len(f.Data)))
rows := Convert1d2d(f.Data, 12)
for _, row := range rows {
buffer.WriteString("\n")
for _, c := range row {
buffer.WriteString(fmt.Sprintf("0x%02x,", c))
}
}
buffer.WriteString("\n];\n")
return buffer.Bytes()
}
func (f *FormatTemplate) ToUUID() []byte {
buffer := bytes.NewBufferString("")
rows := Convert1d2d(f.Data, 16)
for _, row := range rows {
if len(row) < 16 {
row = append(row, bytes.Repeat([]byte{byte(0x90)}, 16-len(row))...)
}
buffer.WriteString(fmt.Sprintf("%02x%02x%02x%02x-", row[3], row[2], row[1], row[0]))
buffer.WriteString(fmt.Sprintf("%02x%02x-", row[5], row[4]))
buffer.WriteString(fmt.Sprintf("%02x%02x-", row[7], row[6]))
buffer.WriteString(fmt.Sprintf("%02x%02x-", row[8], row[9]))
buffer.WriteString(fmt.Sprintf("%08x", row[10:16]))
buffer.WriteString("\n")
}
return buffer.Bytes()
}
func Convert1d2d(data []byte, n int) [][]byte {
length := len(data)
count := int(math.Ceil(float64(length) / float64(n)))
rows := make([][]byte, count)
for i := 0; i < count; i++ {
start := i * n
end := start + n
if end > length {
end = length
}
rows[i] = data[start:end]
}
return rows
}
func NewFormatTemplate(data []byte) *FormatTemplate {
return &FormatTemplate{Data: data}
}