Debian Codegraph @

ztoc/compression/gzip_zinfo.go

raw symbols SCIP
1 /*
2 Copyright The Soci Snapshotter Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package compression
18
19 // #cgo CFLAGS: -I${SRCDIR}/
20 // #cgo LDFLAGS: -L${SRCDIR}/../out -l:libz.a
21 // #include "gzip_zinfo.h"
22 // #include <stdlib.h>
23 // #include <stdint.h>
24 import "C"
25
26 import (
27 "fmt"
28 "unsafe"
29 )
30
31 // GzipZinfo is a go struct wrapper of the gzip zinfo's C implementation.
32 type GzipZinfo struct {
33 cZinfo *C.struct_gzip_zinfo
34 }
35
36 // newGzipZinfo creates a new instance of `GzipZinfo` from cZinfo byte blob on zTOC.
37 func newGzipZinfo(zinfoBytes []byte) (*GzipZinfo, error) {
38 if len(zinfoBytes) == 0 {
39 return nil, fmt.Errorf("empty checkpoints")
40 }
41 cZinfo := C.blob_to_zinfo(unsafe.Pointer(&zinfoBytes[0]), C.off_t(len(zinfoBytes)))
42 if cZinfo == nil {
43 return nil, fmt.Errorf("cannot convert blob to gzip_zinfo")
44 }
45 return &GzipZinfo{
46 cZinfo: cZinfo,
47 }, nil
48 }
49
50 // newGzipZinfoFromFile creates a new instance of `GzipZinfo` given gzip file name and span size.
51 func newGzipZinfoFromFile(gzipFile string, spanSize int64) (*GzipZinfo, error) {
52 cstr := C.CString(gzipFile)
53 defer C.free(unsafe.Pointer(cstr))
54
55 var cZinfo *C.struct_gzip_zinfo
56 ret := C.generate_zinfo_from_file(cstr, C.off_t(spanSize), &cZinfo)
57 if int(ret) < 0 {
58 return nil, fmt.Errorf("could not generate gzip zinfo. gzip error: %v", ret)
59 }
60
61 return &GzipZinfo{
62 cZinfo: cZinfo,
63 }, nil
64 }
65
66 // Close calls `C.free` on the pointer to `C.struct_gzip_zinfo`.
67 func (i *GzipZinfo) Close() {
68 if i.cZinfo != nil {
69 C.free(unsafe.Pointer(i.cZinfo))
70 }
71 }
72
73 // Bytes returns the byte slice containing the zinfo.
74 func (i *GzipZinfo) Bytes() ([]byte, error) {
75 blobSize := C.get_blob_size(i.cZinfo)
76 bytes := make([]byte, uint64(blobSize))
77 if len(bytes) == 0 {
78 return nil, fmt.Errorf("could not allocate byte array of size %d", blobSize)
79 }
80
81 ret := C.zinfo_to_blob(i.cZinfo, unsafe.Pointer(&bytes[0]))
82 if int(ret) <= 0 {
83 return nil, fmt.Errorf("could not serialize gzip zinfo to byte array; gzip error: %v", ret)
84 }
85 return bytes, nil
86 }
87
88 // MaxSpanID returns the max span ID.
89 func (i *GzipZinfo) MaxSpanID() SpanID {
90 return SpanID(C.get_max_span_id(i.cZinfo))
91 }
92
93 // SpanSize returns the span size of the constructed ztoc.
94 func (i *GzipZinfo) SpanSize() Offset {
95 return Offset(i.cZinfo.span_size)
96 }
97
98 // UncompressedOffsetToSpanID returns the ID of the span containing the data pointed by uncompressed offset.
99 func (i *GzipZinfo) UncompressedOffsetToSpanID(offset Offset) SpanID {
100 return SpanID(C.pt_index_from_ucmp_offset(i.cZinfo, C.long(offset)))
101 }
102
103 // ExtractDataFromBuffer wraps the call to `C.extract_data_from_buffer`, which takes in the compressed bytes
104 // and returns the decompressed bytes.
105 func (i *GzipZinfo) ExtractDataFromBuffer(compressedBuf []byte, uncompressedSize, uncompressedOffset Offset, spanID SpanID) ([]byte, error) {
106 if len(compressedBuf) == 0 {
107 return nil, fmt.Errorf("empty compressed buffer")
108 }
109 if uncompressedSize < 0 {
110 return nil, fmt.Errorf("invalid uncompressed size: %d", uncompressedSize)
111 }
112 if uncompressedSize == 0 {
113 return []byte{}, nil
114 }
115 bytes := make([]byte, uncompressedSize)
116 ret := C.extract_data_from_buffer(
117 unsafe.Pointer(&compressedBuf[0]),
118 C.off_t(len(compressedBuf)),
119 i.cZinfo,
120 C.off_t(uncompressedOffset),
121 unsafe.Pointer(&bytes[0]),
122 C.off_t(uncompressedSize),
123 C.int(spanID),
124 )
125 if ret <= 0 {
126 return bytes, fmt.Errorf("error extracting data; return code: %v", ret)
127 }
128
129 return bytes, nil
130 }
131
132 // ExtractDataFromFile wraps `C.extract_data_from_file` and returns the decompressed bytes given the name of the .tar.gz file,
133 // offset and the size in uncompressed stream.
134 func (i *GzipZinfo) ExtractDataFromFile(fileName string, uncompressedSize, uncompressedOffset Offset) ([]byte, error) {
135 cstr := C.CString(fileName)
136 defer C.free(unsafe.Pointer(cstr))
137 if uncompressedSize < 0 {
138 return nil, fmt.Errorf("invalid uncompressed size: %d", uncompressedSize)
139 }
140 if uncompressedSize == 0 {
141 return []byte{}, nil
142 }
143 bytes := make([]byte, uncompressedSize)
144 ret := C.extract_data_from_file(cstr, i.cZinfo, C.off_t(uncompressedOffset), unsafe.Pointer(&bytes[0]), C.int(uncompressedSize))
145 if ret <= 0 {
146 return nil, fmt.Errorf("unable to extract data; return code = %v", ret)
147 }
148
149 return bytes, nil
150 }
151
152 // StartCompressedOffset returns the start offset of the span in the compressed stream.
153 func (i *GzipZinfo) StartCompressedOffset(spanID SpanID) Offset {
154 start := i.getCompressedOffset(spanID)
155 if i.hasBits(spanID) {
156 start--
157 }
158 return start
159 }
160
161 // EndCompressedOffset returns the end offset of the span in the compressed stream. If
162 // it's the last span, returns the size of the compressed stream.
163 func (i *GzipZinfo) EndCompressedOffset(spanID SpanID, fileSize Offset) Offset {
164 if spanID == i.MaxSpanID() {
165 return fileSize
166 }
167 return i.getCompressedOffset(spanID + 1)
168 }
169
170 // StartUncompressedOffset returns the start offset of the span in the uncompressed stream.
171 func (i *GzipZinfo) StartUncompressedOffset(spanID SpanID) Offset {
172 return i.getUncompressedOffset(spanID)
173 }
174
175 // EndUncompressedOffset returns the end offset of the span in the uncompressed stream. If
176 // it's the last span, returns the size of the uncompressed stream.
177 func (i *GzipZinfo) EndUncompressedOffset(spanID SpanID, fileSize Offset) Offset {
178 if spanID == i.MaxSpanID() {
179 return fileSize
180 }
181 return i.getUncompressedOffset(spanID + 1)
182 }
183
184 // getCompressedOffset wraps `C.get_comp_off` and returns the offset for the span in the compressed stream.
185 func (i *GzipZinfo) getCompressedOffset(spanID SpanID) Offset {
186 return Offset(C.get_comp_off(i.cZinfo, C.int(spanID)))
187 }
188
189 // hasBits wraps `C.has_bits` and returns true if any data is contained in the previous span.
190 func (i *GzipZinfo) hasBits(spanID SpanID) bool {
191 return C.has_bits(i.cZinfo, C.int(spanID)) != 0
192 }
193
194 // getUncompressedOffset wraps `C.get_uncomp_off` and returns the offset for the span in the uncompressed stream.
195 func (i *GzipZinfo) getUncompressedOffset(spanID SpanID) Offset {
196 return Offset(C.get_ucomp_off(i.cZinfo, C.int(spanID)))
197 }