/
util.go
73 lines (63 loc) · 1.57 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
package git4go
import (
"crypto/sha1"
"runtime"
"strconv"
"strings"
)
func Is64bit() bool {
return strings.HasSuffix(runtime.GOARCH, "64")
}
// todo: this code doesn't work on spark or any big endien arches
func ntohl(input uint32) uint32 {
return (input&0xff)<<24 | (input&0xff00)<<8 |
(input&0xff0000)>>8 | (input&0xff000000)>>24
}
func ntohlFromBytes(buffer []byte, offset int) uint32 {
return uint32(buffer[offset])<<24 | uint32(buffer[offset+1])<<16 |
uint32(buffer[offset+2])<<8 | uint32(buffer[offset+3])
}
func ntohs(input uint16) uint16 {
return (input&0xff)<<8 | (input&0xff00)>>8
}
func ntohsFromBytes(buffer []byte, offset int) uint16 {
return uint16(buffer[offset])<<8 | uint16(buffer[offset+1])
}
func findChar(input []byte, char byte, offset, bound int) int {
for offset < bound {
if input[offset] == char {
return offset
}
offset++
}
return -1
}
func readString(input []byte, offset, bound int) (string, int) {
initialOffset := offset
for offset < bound {
if input[offset] == 0 {
str := string(input[initialOffset:offset])
return str, offset + 1
}
offset++
}
return "", -1
}
func strtol32(buffer []byte, startOffset, bound, base int) (int64, int) {
for offset := startOffset; offset < bound; offset++ {
c := buffer[offset]
if c < '0' || '9' < c {
attr, _ := strconv.ParseInt(string(buffer[startOffset:offset]), base, 64)
return attr, offset + 1
}
}
return -1, startOffset
}
func calcHash(buffer []byte) *Oid {
h := sha1.New()
h.Write(buffer)
sha1Hash := h.Sum(nil)
oid := new(Oid)
copy(oid[:], sha1Hash[:])
return oid
}