-
Notifications
You must be signed in to change notification settings - Fork 5
/
func.go
142 lines (131 loc) · 3.56 KB
/
func.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
// -----------------------------------------------------------------------------
// github.com/balacode/go-delta go-delta/[func.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package delta
import (
"bytes"
"compress/zlib"
"crypto/sha512"
"io"
)
// -----------------------------------------------------------------------------
// # Helper Functions: Compression
// compressBytes compresses an array of bytes and
// returns the ZLIB-compressed array of bytes.
func compressBytes(data []byte) []byte {
if DebugTiming {
tmr.Start("compressBytes")
defer tmr.Stop("compressBytes")
}
if len(data) == 0 {
return nil
}
// zip data in standard manner
var b bytes.Buffer
w := zlib.NewWriter(&b)
_, err := w.Write(data)
w.Close()
//
// log any problem
const ERRM = "Failed compressing data with zlib:"
if err != nil {
mod.Error(ERRM, err)
return nil
}
ret := b.Bytes()
if len(ret) < 3 {
mod.Error(ERRM, "length < 3")
return nil
}
return ret
} // compressBytes
// uncompressBytes uncompresses a ZLIB-compressed array of bytes.
func uncompressBytes(data []byte) []byte {
readCloser, err := zlib.NewReader(bytes.NewReader(data))
if err != nil {
mod.Error("uncompressBytes:", err)
return nil
}
ret := bytes.NewBuffer(make([]byte, 0, 8192))
io.Copy(ret, readCloser)
readCloser.Close()
return ret.Bytes()
} // uncompressBytes
// -----------------------------------------------------------------------------
// # Helper Functions
// makeHash returns the SHA-512 hash of byte slice 'data'.
func makeHash(data []byte) []byte {
if DebugTiming {
tmr.Start("makeHash")
defer tmr.Stop("makeHash")
}
if len(data) == 0 {
return nil
}
ret := sha512.Sum512(data)
return ret[:]
} // makeHash
// readHash returns the SHA-512 hash of the bytes from 'stream'.
func readHash(stream io.Reader) []byte {
if DebugTiming {
tmr.Start("readHash")
defer tmr.Stop("readHash")
}
hasher := sha512.New()
buf := make([]byte, TempBufferSize)
for first := true; ; first = false {
n, err := stream.Read(buf)
if err == io.EOF && first {
return nil
}
if err == io.EOF {
if n != 0 {
mod.Error("Expected zero: n =", n)
}
break
}
if err != nil {
mod.Error("Failed reading:", err)
return nil
}
if n == 0 {
break
}
n, err = hasher.Write(buf[:n])
if err != nil {
mod.Error("Failed writing:", err)
return nil
}
}
ret := hasher.Sum(nil)
return ret
} // readHash
// readLen returns the total size of 'stream' in bytes.
// After a call to readLen, the current reading
// position returns to the start or the stream.
func readLen(stream io.ReadSeeker) int {
ret, _ := stream.Seek(0, io.SeekEnd)
stream.Seek(0, io.SeekStart)
return int(ret)
} // readLen
// readStream _ _
func readStream(from io.ReadSeeker, to []byte) (n int64, err error) {
// read from the stream
{
var num int
num, err = from.Read(to)
n = int64(num)
}
if err == io.EOF {
if n != 0 {
mod.Error("Expected zero: n =", n)
}
return -1, nil
}
if err != nil {
return -1, mod.Error("Failed reading:", err)
}
return n, err
} // readStream
// end