1
2
3
4
5
6 package hex
7
8 import (
9 "errors"
10 "fmt"
11 "io"
12 "slices"
13 "strings"
14 )
15
16 const (
17 hextable = "0123456789abcdef"
18 reverseHexTable = "" +
19 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
20 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
21 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
22 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff" +
23 "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
24 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
25 "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
26 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
27 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
28 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
29 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
30 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
31 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
32 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
33 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
34 "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
35 )
36
37
38
39 func EncodedLen(n int) int { return n * 2 }
40
41
42
43
44
45 func Encode(dst, src []byte) int {
46 j := 0
47 for _, v := range src {
48 dst[j] = hextable[v>>4]
49 dst[j+1] = hextable[v&0x0f]
50 j += 2
51 }
52 return len(src) * 2
53 }
54
55
56
57 func AppendEncode(dst, src []byte) []byte {
58 n := EncodedLen(len(src))
59 dst = slices.Grow(dst, n)
60 Encode(dst[len(dst):][:n], src)
61 return dst[:len(dst)+n]
62 }
63
64
65
66
67 var ErrLength = errors.New("encoding/hex: odd length hex string")
68
69
70 type InvalidByteError byte
71
72 func (e InvalidByteError) Error() string {
73 return fmt.Sprintf("encoding/hex: invalid byte: %#U", rune(e))
74 }
75
76
77
78 func DecodedLen(x int) int { return x / 2 }
79
80
81
82
83
84
85
86
87 func Decode(dst, src []byte) (int, error) {
88 i, j := 0, 1
89 for ; j < len(src); j += 2 {
90 p := src[j-1]
91 q := src[j]
92
93 a := reverseHexTable[p]
94 b := reverseHexTable[q]
95 if a > 0x0f {
96 return i, InvalidByteError(p)
97 }
98 if b > 0x0f {
99 return i, InvalidByteError(q)
100 }
101 dst[i] = (a << 4) | b
102 i++
103 }
104 if len(src)%2 == 1 {
105
106
107 if reverseHexTable[src[j-1]] > 0x0f {
108 return i, InvalidByteError(src[j-1])
109 }
110 return i, ErrLength
111 }
112 return i, nil
113 }
114
115
116
117
118 func AppendDecode(dst, src []byte) ([]byte, error) {
119 n := DecodedLen(len(src))
120 dst = slices.Grow(dst, n)
121 n, err := Decode(dst[len(dst):][:n], src)
122 return dst[:len(dst)+n], err
123 }
124
125
126 func EncodeToString(src []byte) string {
127 dst := make([]byte, EncodedLen(len(src)))
128 Encode(dst, src)
129 return string(dst)
130 }
131
132
133
134
135
136
137
138 func DecodeString(s string) ([]byte, error) {
139 dst := make([]byte, DecodedLen(len(s)))
140 n, err := Decode(dst, []byte(s))
141 return dst[:n], err
142 }
143
144
145
146 func Dump(data []byte) string {
147 if len(data) == 0 {
148 return ""
149 }
150
151 var buf strings.Builder
152
153
154
155 buf.Grow((1 + ((len(data) - 1) / 16)) * 79)
156
157 dumper := Dumper(&buf)
158 dumper.Write(data)
159 dumper.Close()
160 return buf.String()
161 }
162
163
164 const bufferSize = 1024
165
166 type encoder struct {
167 w io.Writer
168 err error
169 out [bufferSize]byte
170 }
171
172
173 func NewEncoder(w io.Writer) io.Writer {
174 return &encoder{w: w}
175 }
176
177 func (e *encoder) Write(p []byte) (n int, err error) {
178 for len(p) > 0 && e.err == nil {
179 chunkSize := bufferSize / 2
180 if len(p) < chunkSize {
181 chunkSize = len(p)
182 }
183
184 var written int
185 encoded := Encode(e.out[:], p[:chunkSize])
186 written, e.err = e.w.Write(e.out[:encoded])
187 n += written / 2
188 p = p[chunkSize:]
189 }
190 return n, e.err
191 }
192
193 type decoder struct {
194 r io.Reader
195 err error
196 in []byte
197 arr [bufferSize]byte
198 }
199
200
201
202 func NewDecoder(r io.Reader) io.Reader {
203 return &decoder{r: r}
204 }
205
206 func (d *decoder) Read(p []byte) (n int, err error) {
207
208 if len(d.in) < 2 && d.err == nil {
209 var numCopy, numRead int
210 numCopy = copy(d.arr[:], d.in)
211 numRead, d.err = d.r.Read(d.arr[numCopy:])
212 d.in = d.arr[:numCopy+numRead]
213 if d.err == io.EOF && len(d.in)%2 != 0 {
214
215 if a := reverseHexTable[d.in[len(d.in)-1]]; a > 0x0f {
216 d.err = InvalidByteError(d.in[len(d.in)-1])
217 } else {
218 d.err = io.ErrUnexpectedEOF
219 }
220 }
221 }
222
223
224 if numAvail := len(d.in) / 2; len(p) > numAvail {
225 p = p[:numAvail]
226 }
227 numDec, err := Decode(p, d.in[:len(p)*2])
228 d.in = d.in[2*numDec:]
229 if err != nil {
230 d.in, d.err = nil, err
231 }
232
233 if len(d.in) < 2 {
234 return numDec, d.err
235 }
236 return numDec, nil
237 }
238
239
240
241
242 func Dumper(w io.Writer) io.WriteCloser {
243 return &dumper{w: w}
244 }
245
246 type dumper struct {
247 w io.Writer
248 rightChars [18]byte
249 buf [14]byte
250 used int
251 n uint
252 closed bool
253 }
254
255 func toChar(b byte) byte {
256 if b < 32 || b > 126 {
257 return '.'
258 }
259 return b
260 }
261
262 func (h *dumper) Write(data []byte) (n int, err error) {
263 if h.closed {
264 return 0, errors.New("encoding/hex: dumper closed")
265 }
266
267
268
269
270 for i := range data {
271 if h.used == 0 {
272
273
274 h.buf[0] = byte(h.n >> 24)
275 h.buf[1] = byte(h.n >> 16)
276 h.buf[2] = byte(h.n >> 8)
277 h.buf[3] = byte(h.n)
278 Encode(h.buf[4:], h.buf[:4])
279 h.buf[12] = ' '
280 h.buf[13] = ' '
281 _, err = h.w.Write(h.buf[4:])
282 if err != nil {
283 return
284 }
285 }
286 Encode(h.buf[:], data[i:i+1])
287 h.buf[2] = ' '
288 l := 3
289 if h.used == 7 {
290
291 h.buf[3] = ' '
292 l = 4
293 } else if h.used == 15 {
294
295
296 h.buf[3] = ' '
297 h.buf[4] = '|'
298 l = 5
299 }
300 _, err = h.w.Write(h.buf[:l])
301 if err != nil {
302 return
303 }
304 n++
305 h.rightChars[h.used] = toChar(data[i])
306 h.used++
307 h.n++
308 if h.used == 16 {
309 h.rightChars[16] = '|'
310 h.rightChars[17] = '\n'
311 _, err = h.w.Write(h.rightChars[:])
312 if err != nil {
313 return
314 }
315 h.used = 0
316 }
317 }
318 return
319 }
320
321 func (h *dumper) Close() (err error) {
322
323 if h.closed {
324 return
325 }
326 h.closed = true
327 if h.used == 0 {
328 return
329 }
330 h.buf[0] = ' '
331 h.buf[1] = ' '
332 h.buf[2] = ' '
333 h.buf[3] = ' '
334 h.buf[4] = '|'
335 nBytes := h.used
336 for h.used < 16 {
337 l := 3
338 if h.used == 7 {
339 l = 4
340 } else if h.used == 15 {
341 l = 5
342 }
343 _, err = h.w.Write(h.buf[:l])
344 if err != nil {
345 return
346 }
347 h.used++
348 }
349 h.rightChars[nBytes] = '|'
350 h.rightChars[nBytes+1] = '\n'
351 _, err = h.w.Write(h.rightChars[:nBytes+2])
352 return
353 }
354
View as plain text