This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
77 lines (69 loc) · 1.95 KB
/
util.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
)
// charset
var charset = "0123456789ABCDEF"
// will be thrown
var errUnhex = errors.New("can not unhex")
// maxInt determines the maximum of two given integers a and b since the std
// math package does not include an integer version
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}
// check if function returned an error. Clean up in case of error and and exit.
// this function exists because deferred statements are not executed when invoking
// os.Exit(..). Also, it reduces code size
func check(err error, w *bufio.Writer) {
if err == nil {
return
}
if w != nil {
w.Flush()
}
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
// hex receives an arbitrary byte and converts it to a (high, low)-printable
// hexadecimal representation
func hex(b byte) (byte, byte) {
return charset[b>>0x04], charset[b&0x0F] // high and low nibbles
}
// unhex receives the bytes of a hex (high, low)-printable hexadecimal
// representation and converts them back to the original byte. This function
// returns errUnhex if a byte in either
func unhex(high, low byte) (byte, error) {
var err error
var hNorm, lNorm byte
// convert the most significant nibble
if hNorm, err = normalize(high); err != nil {
return 0, err
}
// convert the least significant nibble
if lNorm, err = normalize(low); err != nil {
return 0, err
}
// join nibbles to a proper byte
return (hNorm << 0x04) | lNorm, nil
}
// normalize a given byte that represents [0-9a-zA-Z] in ascii to its original
// value. This function is used in conjunction with unhex and returns errUnhex
// if b is not within the ascii range specified above.
func normalize(b byte) (byte, error) {
switch {
case '0' <= b && b <= '9':
return b - '0', nil
case 'a' <= b && b <= 'f':
return b - 'a' + 10, nil
case 'A' <= b && b <= 'F':
return b - 'A' + 10, nil
}
// b is not within [0-9a-zA-Z] and can thus not be normalized
return 0, errUnhex
}