-
Notifications
You must be signed in to change notification settings - Fork 3
/
gzip.go
236 lines (219 loc) · 4.8 KB
/
gzip.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) 2023, Intel Corporation.
// SPDX-License-Identifier: BSD-3-Clause
package compress
import (
"compress/flate"
"compress/gzip"
"encoding/binary"
"io"
"github.com/intel/ixl-go/errors"
)
// Header is same as gzip.Header
type Header = gzip.Header
// Gzip is an object to hold the state for compress data using gzip format.
type Gzip struct {
Header
UTF8 bool // can be used with gzip command
wroteHeader bool
level int
w io.Writer
buf []byte
sum int64
opts []Option
compressor *Deflate
}
var fixedGzipHeader = [3]byte{0x1f, 0x8b, 8}
// NewGzip create a new Gzip.
func NewGzip(w io.Writer, opts ...Option) *Gzip {
g := &Gzip{Header: Header{OS: 255}, w: w}
g.w = w
g.opts = opts
return g
}
// Gzip format: https://www.rfc-editor.org/rfc/rfc1952#page-4
func (g *Gzip) writeHeader() (err error) {
flag := gzipFlag(0)
bCap := 10 // |ID1|ID2|CM|FLG|MTIME(4 byte)|XFL|OS|
if len(g.Extra) != 0 {
bCap += 2 + len(g.Extra) // xlen + bytes
flag |= gzipFileExtra
}
if len(g.Name) != 0 {
bCap += len(g.Name) + 1 // zero-terminated
flag |= gzipFileName
}
if len(g.Comment) != 0 {
bCap += len(g.Comment) + 1 // zero-terminated
flag |= gzipFileComment
}
if cap(g.buf) > bCap {
g.buf = g.buf[:bCap]
} else {
g.buf = make([]byte, bCap)
}
// ID1|ID2|CM
copy(g.buf[:3], fixedGzipHeader[:])
g.buf[3] = uint8(flag)
// MTIME
sec := g.ModTime.Unix()
if sec < 0 {
sec = 0
}
binary.LittleEndian.PutUint32(g.buf[4:8], uint32(sec))
level := 0
if g.level == flate.BestCompression {
level = 2
} else if g.level == flate.BestSpeed {
level = 4
}
g.buf[8] = byte(level)
g.buf[9] = g.OS
idx := 10
if len(g.Extra) != 0 {
binary.LittleEndian.PutUint16(g.buf[idx:idx+2], uint16(len(g.Extra)))
idx += 2
copy(g.buf[idx:], g.Extra)
idx += len(g.Extra)
}
if len(g.Name) != 0 {
idx, err = g.writeHeaderStr(idx, g.Name)
if err != nil {
return err
}
}
if len(g.Comment) != 0 {
idx, err = g.writeHeaderStr(idx, g.Comment)
if err != nil {
return err
}
}
_, err = g.w.Write(g.buf[:idx])
return err
}
func (g *Gzip) writeHeaderStr(idx int, str string) (nidx int, err error) {
if g.UTF8 {
for _, char := range str {
if char == 0 {
return idx, errors.ErrZeroByte
}
}
copy(g.buf[idx:], []byte(g.Name))
idx += len(g.Name)
g.buf[idx] = 0
idx++
return idx, nil
}
safe := true
for _, char := range str {
if char == 0 || char > 0xff {
return idx, errors.ErrNonLatin1Header
}
if char > 0x7f {
safe = false
break
}
}
if safe {
copy(g.buf[idx:], []byte(g.Name))
idx += len(g.Name)
g.buf[idx] = 0
idx++
return idx, nil
}
for _, char := range str {
g.buf[idx] = byte(char)
idx++
}
g.buf[idx] = 0
idx++
return idx, nil
}
// writeBlock compresses the block and writes it to underlying writer.
//
// Notice:
// 1. The block first byte address must be aligned to a multiple of 64 bytes.
// You can use `mem.Alloc64ByteAligned` function to alloc a 64 bytes aligned bytes.
// 2. The `last` argument must be true if the block is the last block in the stream.
// 3. For most scenarios, you should use the `ReadFrom` method.
func (g *Gzip) writeBlock(block []byte, last bool) (n int, err error) {
if !g.wroteHeader {
err = g.writeHeader()
if err != nil {
return 0, err
}
g.wroteHeader = true
}
if g.compressor == nil {
g.compressor, err = NewDeflate(g.w, g.opts...)
if err != nil {
return 0, err
}
}
g.sum += int64(len(block))
n, err = g.compressor.writeBlock(block, last)
if err != nil {
return n, err
}
if last {
err = g.writeTailer(g.sum)
if err != nil {
return 0, err
}
}
return
}
// ReadFrom reads all data from `r` and compresses the data and then writes compressed data into underlying writer `w`.
func (g *Gzip) ReadFrom(reader io.Reader) (n int64, err error) {
if !g.wroteHeader {
err = g.writeHeader()
if err != nil {
return 0, err
}
}
if g.compressor == nil {
g.compressor, err = NewDeflate(g.w)
if err != nil {
return 0, err
}
}
n, err = g.compressor.ReadFrom(reader)
if err != nil && err != io.EOF {
return n, err
}
err = g.writeTailer(n)
return n, err
}
func (g *Gzip) writeTailer(n int64) error {
crc := g.compressor.crc
buf := g.buf[:8]
binary.LittleEndian.PutUint32(buf[:4], crc)
binary.LittleEndian.PutUint32(buf[4:], uint32(n))
_, err := g.w.Write(buf)
return err
}
// Reset the internal states for reusing the object.
func (g *Gzip) Reset(w io.Writer) {
g.w = w
g.sum = 0
g.wroteHeader = false
g.Header = Header{OS: 255}
if g.compressor != nil {
g.compressor.Reset(g.w)
}
}
// Close the writer.
func (g *Gzip) Close() error {
closer, ok := g.w.(io.Closer)
if ok {
return closer.Close()
}
return nil
}
type gzipFlag uint8
const (
gzipFileText gzipFlag = 1 << iota
gzipFileHCRC
gzipFileExtra
gzipFileName
gzipFileComment
)