| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package cbrotli |
| 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 |
import "C" |
| 43 |
|
| 44 |
import ( |
| 45 |
"bytes" |
| 46 |
"errors" |
| 47 |
"io" |
| 48 |
"runtime" |
| 49 |
"unsafe" |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
type PreparedDictionary struct { |
| 54 |
opaque *C.BrotliEncoderPreparedDictionary |
| 55 |
pinner *runtime.Pinner |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
type DictionaryType int |
| 60 |
|
| 61 |
const ( |
| 62 |
|
| 63 |
DtRaw DictionaryType = 0 |
| 64 |
|
| 65 |
DtSerialized DictionaryType = 1 |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
func NewPreparedDictionary(data []byte, dictionaryType DictionaryType, quality int) *PreparedDictionary { |
| 72 |
var ptr *C.uint8_t |
| 73 |
if len(data) != 0 { |
| 74 |
ptr = (*C.uint8_t)(&data[0]) |
| 75 |
} |
| 76 |
p := new(runtime.Pinner) |
| 77 |
p.Pin(&data[0]) |
| 78 |
d := C.BrotliEncoderPrepareDictionary(C.BrotliSharedDictionaryType(dictionaryType), C.size_t(len(data)), ptr, C.int(quality), nil, nil, nil) |
| 79 |
return &PreparedDictionary{ |
| 80 |
opaque: d, |
| 81 |
pinner: p, |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
func (p *PreparedDictionary) Close() error { |
| 89 |
|
| 90 |
C.BrotliEncoderDestroyPreparedDictionary(p.opaque) |
| 91 |
p.opaque = nil |
| 92 |
p.pinner.Unpin() |
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
type WriterOptions struct { |
| 98 |
|
| 99 |
|
| 100 |
Quality int |
| 101 |
|
| 102 |
|
| 103 |
LGWin int |
| 104 |
|
| 105 |
Dictionary *PreparedDictionary |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
type Writer struct { |
| 111 |
healthy bool |
| 112 |
dst io.Writer |
| 113 |
state *C.BrotliEncoderState |
| 114 |
buf, encoded []byte |
| 115 |
} |
| 116 |
|
| 117 |
var ( |
| 118 |
errEncode = errors.New("cbrotli: encode error") |
| 119 |
errWriterClosed = errors.New("cbrotli: Writer is closed") |
| 120 |
errWriterUnhealthy = errors.New("cbrotli: Writer is unhealthy") |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
func NewWriter(dst io.Writer, options WriterOptions) *Writer { |
| 126 |
state := C.BrotliEncoderCreateInstance(nil, nil, nil) |
| 127 |
healthy := state != nil |
| 128 |
if C.BrotliEncoderSetParameter( |
| 129 |
state, C.BROTLI_PARAM_QUALITY, (C.uint32_t)(options.Quality)) == 0 { |
| 130 |
healthy = false |
| 131 |
} |
| 132 |
if options.LGWin > 0 { |
| 133 |
if C.BrotliEncoderSetParameter( |
| 134 |
state, C.BROTLI_PARAM_LGWIN, (C.uint32_t)(options.LGWin)) == 0 { |
| 135 |
healthy = false |
| 136 |
} |
| 137 |
} |
| 138 |
if options.Dictionary != nil { |
| 139 |
if C.BrotliEncoderAttachPreparedDictionary(state, options.Dictionary.opaque) == 0 { |
| 140 |
healthy = false |
| 141 |
} |
| 142 |
} |
| 143 |
return &Writer{ |
| 144 |
healthy: healthy, |
| 145 |
dst: dst, |
| 146 |
state: state, |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func (w *Writer) writeChunk(p []byte, op C.BrotliEncoderOperation) (n int, err error) { |
| 151 |
if !w.healthy { |
| 152 |
return 0, errWriterUnhealthy |
| 153 |
} |
| 154 |
if w.state == nil { |
| 155 |
return 0, errWriterClosed |
| 156 |
} |
| 157 |
|
| 158 |
for { |
| 159 |
var data *C.uint8_t |
| 160 |
if len(p) != 0 { |
| 161 |
data = (*C.uint8_t)(&p[0]) |
| 162 |
} |
| 163 |
result := C.CompressStream(w.state, op, data, C.size_t(len(p))) |
| 164 |
if result.success == 0 { |
| 165 |
return n, errEncode |
| 166 |
} |
| 167 |
p = p[int(result.bytes_consumed):] |
| 168 |
n += int(result.bytes_consumed) |
| 169 |
|
| 170 |
length := int(result.output_data_size) |
| 171 |
if length != 0 { |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
output := (*[1 << 30]byte)(unsafe.Pointer(result.output_data))[:length:length] |
| 177 |
_, err = w.dst.Write(output) |
| 178 |
if err != nil { |
| 179 |
return n, err |
| 180 |
} |
| 181 |
} |
| 182 |
if len(p) == 0 && result.has_more == 0 { |
| 183 |
return n, nil |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
func (w *Writer) Flush() error { |
| 193 |
_, err := w.writeChunk(nil, C.BROTLI_OPERATION_FLUSH) |
| 194 |
return err |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
func (w *Writer) Close() error { |
| 199 |
|
| 200 |
_, err := w.writeChunk(nil, C.BROTLI_OPERATION_FINISH) |
| 201 |
|
| 202 |
C.BrotliEncoderDestroyInstance(w.state) |
| 203 |
w.state = nil |
| 204 |
return err |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
func (w *Writer) Write(p []byte) (n int, err error) { |
| 210 |
return w.writeChunk(p, C.BROTLI_OPERATION_PROCESS) |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
func Encode(content []byte, options WriterOptions) ([]byte, error) { |
| 215 |
var buf bytes.Buffer |
| 216 |
writer := NewWriter(&buf, options) |
| 217 |
_, err := writer.Write(content) |
| 218 |
if closeErr := writer.Close(); err == nil { |
| 219 |
err = closeErr |
| 220 |
} |
| 221 |
return buf.Bytes(), err |
| 222 |
} |