-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
171 lines (146 loc) · 3.06 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
//#include <string.h>
import "C"
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"os"
"strings"
"sync"
"time"
"unsafe"
)
func int16atobytes(a []int16) []byte {
b := make([]byte, len(a)*2)
pa := unsafe.Pointer(&a[0])
pb := unsafe.Pointer(&b[0])
C.memcpy(pb, pa, C.size_t(len(b)))
return b
}
func bytestoint16a(b []byte) []int16 {
a := make([]int16, len(b)/2)
pa := unsafe.Pointer(&a[0])
pb := unsafe.Pointer(&b[0])
C.memcpy(pa, pb, C.size_t(len(b)))
return a
}
func inIntSlice(i int, si []int) bool {
for _, j := range si {
if i == j {
return true
}
}
return false
}
func jsonifyWhatever(i interface{}) string {
jsonb, err := json.Marshal(i)
if err != nil {
log.Panic(err)
}
return string(jsonb)
}
// Converts whatever to a json string in bytes
func jsonifyWhateverToBytes(i interface{}) []byte {
jsonb, err := json.Marshal(i)
if err != nil {
log.Panic(err)
}
return jsonb
}
// WithMutex extends the Mutex type with the convenient .With(func) function
type WithMutex struct {
sync.Mutex
}
// With executes the given function with the mutex locked
func (m *WithMutex) With(f func()) {
m.Mutex.Lock()
f()
m.Mutex.Unlock()
}
// Converts the given Unix timestamp to time.Time
func unixTimeStampToUTCTime(ts int) time.Time {
return time.Unix(int64(ts), 0)
}
// Gets the current Unix timestamp in UTC
func getNowUTC() int64 {
return time.Now().UTC().Unix()
}
// Mashals the given map of strings to JSON
func stringMap2JsonBytes(m map[string]string) []byte {
b, err := json.Marshal(m)
if err != nil {
log.Panicln("Cannot json-ise the map:", err)
}
return b
}
// Returns a hex-encoded hash of the given byte slice
func hashBytesToHexString(b []byte) string {
hash := sha256.Sum256(b)
return hex.EncodeToString(hash[:])
}
// Returns a hex-encoded hash of the given file
func hashFileToHexString(fileName string) (string, error) {
file, err := os.Open(fileName)
if err != nil {
return "", err
}
defer file.Close()
hash := sha256.New()
_, err = io.Copy(hash, file)
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
// Checks if a string appears in the slice of strings
func inStringSlice(s string, a []string) bool {
for _, ss := range a {
if ss == s {
return true
}
}
return false
}
func bytesToBytesLiteral(buf []byte) string {
parts := []string{}
for _, b := range buf {
parts = append(parts, fmt.Sprintf("0x%x", b))
}
return strings.Join(parts, ",")
}
func mustDecodeBase64URL(s string) []byte {
buf, err := base64.RawURLEncoding.DecodeString(s)
if err != nil {
log.Panic(err)
}
return buf
}
func mustEncodeBase64URL(b []byte) string {
return base64.RawURLEncoding.EncodeToString(b)
}
func countStartZeroBits(b []byte) int {
nBits := 0
for i := 0; i < len(b); i++ {
if b[i] == 0 {
nBits += 8
} else {
for z := uint(7); z >= 0; z-- {
if b[i]&(1<<z) == 0 {
nBits++
} else {
break
}
}
}
}
return nBits
}
func stringMapHasKey(m *map[string]string, k string) bool {
_, ok := (*m)[k]
return ok
}