-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzbuf.go
45 lines (41 loc) · 985 Bytes
/
zbuf.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
/*
* Copyright (c) 2017 by Farsight Security, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package nmsg
import (
"bytes"
"compress/zlib"
"encoding/binary"
"io"
)
func zbufDeflate(b []byte) ([]byte, error) {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(len(b)))
w := zlib.NewWriter(buf)
if _, err := w.Write(b); err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func zbufInflate(b []byte) ([]byte, error) {
br := bytes.NewReader(b)
var ilen uint32
binary.Read(br, binary.BigEndian, &ilen)
buf := bytes.NewBuffer(make([]byte, 0, int(ilen)))
r, err := zlib.NewReader(br)
if err != nil {
return nil, err
}
if _, err = io.Copy(buf, r); err != nil {
return nil, err
}
r.Close()
return buf.Bytes(), nil
}