forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
export.go
153 lines (140 loc) · 3.78 KB
/
export.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
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package utils
import (
"bytes"
"compress/zlib"
"fmt"
"github.com/dnaproject2/DNA/common/serialization"
"io"
"io/ioutil"
)
const (
COMPRESS_TYPE_ZLIB = iota
)
const (
DEFAULT_COMPRESS_TYPE = COMPRESS_TYPE_ZLIB
EXPORT_BLOCK_METADATA_LEN = 256
EXPORT_BLOCK_METADATA_VERSION = 1
)
type ExportBlockMetadata struct {
Version byte
CompressType byte
StartBlockHeight uint32
EndBlockHeight uint32
}
func NewExportBlockMetadata() *ExportBlockMetadata {
return &ExportBlockMetadata{
Version: EXPORT_BLOCK_METADATA_VERSION,
CompressType: DEFAULT_COMPRESS_TYPE,
}
}
func (this *ExportBlockMetadata) Serialize(w io.Writer) error {
metadata := make([]byte, EXPORT_BLOCK_METADATA_LEN, EXPORT_BLOCK_METADATA_LEN)
buf := bytes.NewBuffer(nil)
err := serialization.WriteByte(buf, this.Version)
if err != nil {
return err
}
err = serialization.WriteByte(buf, this.CompressType)
if err != nil {
return err
}
err = serialization.WriteUint32(buf, this.StartBlockHeight)
if err != nil {
return err
}
err = serialization.WriteUint32(buf, this.EndBlockHeight)
if err != nil {
return err
}
data := buf.Bytes()
if len(data) > EXPORT_BLOCK_METADATA_LEN {
return fmt.Errorf("metata len size larger than %d", EXPORT_BLOCK_METADATA_LEN)
}
copy(metadata, data)
_, err = w.Write(metadata)
return err
}
func (this *ExportBlockMetadata) Deserialize(r io.Reader) error {
metadata := make([]byte, EXPORT_BLOCK_METADATA_LEN, EXPORT_BLOCK_METADATA_LEN)
_, err := io.ReadFull(r, metadata)
if err != nil {
return err
}
if metadata[0] != EXPORT_BLOCK_METADATA_VERSION {
return fmt.Errorf("version unmatch")
}
reader := bytes.NewBuffer(metadata)
ver, err := serialization.ReadByte(reader)
if err != nil {
return err
}
this.Version = ver
compressType, err := serialization.ReadByte(reader)
if err != nil {
return err
}
this.CompressType = compressType
height, err := serialization.ReadUint32(reader)
if err != nil {
return err
}
this.StartBlockHeight = height
height, err = serialization.ReadUint32(reader)
if err != nil {
return err
}
this.EndBlockHeight = height
return nil
}
func CompressBlockData(data []byte, compressType byte) ([]byte, error) {
switch compressType {
case COMPRESS_TYPE_ZLIB:
return ZLibCompress(data)
default:
return nil, fmt.Errorf("unknown compress type")
}
}
func DecompressBlockData(data []byte, compressType byte) ([]byte, error) {
switch compressType {
case COMPRESS_TYPE_ZLIB:
return ZLibDecompress(data)
default:
return nil, fmt.Errorf("unknown compress type")
}
}
func ZLibCompress(data []byte) ([]byte, error) {
buf := bytes.NewBuffer(nil)
zlibWriter := zlib.NewWriter(buf)
_, err := zlibWriter.Write(data)
if err != nil {
return nil, fmt.Errorf("zlibWriter.Write error %s", err)
}
zlibWriter.Close()
return buf.Bytes(), nil
}
func ZLibDecompress(data []byte) ([]byte, error) {
buf := bytes.NewReader(data)
zlibReader, err := zlib.NewReader(buf)
if err != nil {
return nil, fmt.Errorf("zlib.NewReader error %s", err)
}
defer zlibReader.Close()
return ioutil.ReadAll(zlibReader)
}